3 FuncNode::FuncNode(ModelHistory * history) :
9 predicate_tree_position(),
13 predicate_tree_entry = new Predicate(NULL, true);
14 predicate_tree_entry->add_predicate_expr(NOPREDICATE, NULL, true);
16 // memories that are reclaimed after each execution
17 action_list_buffer = new SnapList<action_list_t *>();
18 read_locations = new loc_set_t();
19 val_loc_map = new HashTable<uint64_t, loc_set_t *, uint64_t, 0>();
20 loc_may_equal_map = new HashTable<void *, loc_set_t *, uintptr_t, 0>();
21 thrd_inst_act_map = new SnapVector<inst_act_map_t *>();
23 //values_may_read_from = new value_set_t();
26 /* Reallocate snapshotted memories when new executions start */
27 void FuncNode::set_new_exec_flag()
29 for (mllnode<FuncInst *> * it = inst_list.begin(); it != NULL; it = it->getNext()) {
30 FuncInst * inst = it->getVal();
31 inst->unset_location();
34 action_list_buffer = new SnapList<action_list_t *>();
35 read_locations = new loc_set_t();
36 val_loc_map = new HashTable<uint64_t, loc_set_t *, uint64_t, 0>();
37 loc_may_equal_map = new HashTable<void *, loc_set_t *, uintptr_t, 0>();
38 thrd_inst_act_map = new SnapVector<inst_act_map_t *>();
40 //values_may_read_from = new value_set_t();
43 /* Check whether FuncInst with the same type, position, and location
44 * as act has been added to func_inst_map or not. If not, add it.
46 * Note: currently, actions with the same position are filtered out by process_action,
47 * so the collision list of FuncInst is not used. May remove it later.
49 void FuncNode::add_inst(ModelAction *act)
52 const char * position = act->get_position();
54 /* THREAD* actions, ATOMIC_LOCK, ATOMIC_TRYLOCK, and ATOMIC_UNLOCK
55 * actions are not tagged with their source line numbers
60 if ( func_inst_map.contains(position) ) {
61 FuncInst * inst = func_inst_map.get(position);
63 ASSERT(inst->get_type() == act->get_type());
65 // locations are set to NULL when new executions start
66 if (inst->get_location() == NULL)
67 inst->set_location(act->get_location());
69 if (inst->get_location() != act->get_location())
70 inst->not_single_location();
75 FuncInst * func_inst = new FuncInst(act, this);
77 func_inst_map.put(position, func_inst);
78 inst_list.push_back(func_inst);
81 /* Get the FuncInst with the same type, position, and location
84 * @return FuncInst with the same type, position, and location as act */
85 FuncInst * FuncNode::get_inst(ModelAction *act)
88 const char * position = act->get_position();
90 /* THREAD* actions, ATOMIC_LOCK, ATOMIC_TRYLOCK, and ATOMIC_UNLOCK
91 * actions are not tagged with their source line numbers
96 FuncInst * inst = func_inst_map.get(position);
100 action_type inst_type = inst->get_type();
101 action_type act_type = act->get_type();
103 // else if branch: an RMWRCAS action is converted to a RMW or READ action
104 if (inst_type == act_type)
106 else if (inst_type == ATOMIC_RMWRCAS &&
107 (act_type == ATOMIC_RMW || act_type == ATOMIC_READ))
114 void FuncNode::add_entry_inst(FuncInst * inst)
119 mllnode<FuncInst *> * it;
120 for (it = entry_insts.begin(); it != NULL; it = it->getNext()) {
121 if (inst == it->getVal())
125 entry_insts.push_back(inst);
129 * @brief Convert ModelAdtion list to FuncInst list
130 * @param act_list A list of ModelActions
132 void FuncNode::update_tree(action_list_t * act_list)
134 if (act_list == NULL || act_list->size() == 0)
137 HashTable<void *, value_set_t *, uintptr_t, 4> * write_history = history->getWriteHistory();
139 /* build inst_list from act_list for later processing */
140 func_inst_list_t inst_list;
141 action_list_t rw_act_list;
143 for (sllnode<ModelAction *> * it = act_list->begin(); it != NULL; it = it->getNext()) {
144 ModelAction * act = it->getVal();
145 FuncInst * func_inst = get_inst(act);
147 if (func_inst == NULL)
150 inst_list.push_back(func_inst);
152 /* NOTE: for rmw actions, func_inst and act may have different
153 * action types because of action type conversion in ModelExecution
154 * func_inst->is_write() <==> pure writes (excluding rmw) */
155 if (func_inst->is_write()) {
156 // model_print("write detected\n");
157 rw_act_list.push_back(act);
160 /* func_inst->is_read() <==> read + rmw */
161 if (func_inst->is_read()) {
162 rw_act_list.push_back(act);
163 /* If func_inst may only read_from a single location, then:
165 * The first time an action reads from some location,
166 * import all the values that have been written to this
167 * location from ModelHistory and notify ModelHistory
168 * that this FuncNode may read from this location.
170 void * loc = act->get_location();
171 if (!read_locations->contains(loc) && func_inst->is_single_location()) {
172 read_locations->add(loc);
173 value_set_t * write_values = write_history->get(loc);
174 add_to_val_loc_map(write_values, loc);
175 history->add_to_loc_func_nodes_map(loc, this);
180 // model_print("function %s\n", func_name);
181 // print_val_loc_map();
183 update_inst_tree(&inst_list);
184 update_predicate_tree(&rw_act_list);
186 // print_predicate_tree();
190 * @brief Link FuncInsts in inst_list - add one FuncInst to another's predecessors and successors
191 * @param inst_list A list of FuncInsts
193 void FuncNode::update_inst_tree(func_inst_list_t * inst_list)
195 if (inst_list == NULL)
197 else if (inst_list->size() == 0)
201 sllnode<FuncInst *>* it = inst_list->begin();
202 sllnode<FuncInst *>* prev;
204 /* add the first instruction to the list of entry insts */
205 FuncInst * entry_inst = it->getVal();
206 add_entry_inst(entry_inst);
210 prev = it->getPrev();
212 FuncInst * prev_inst = prev->getVal();
213 FuncInst * curr_inst = it->getVal();
215 prev_inst->add_succ(curr_inst);
216 curr_inst->add_pred(prev_inst);
222 void FuncNode::update_predicate_tree(action_list_t * act_list)
224 if (act_list == NULL || act_list->size() == 0)
227 /* map a FuncInst to the its predicate */
228 HashTable<FuncInst *, Predicate *, uintptr_t, 0> inst_pred_map(128);
230 // number FuncInsts to detect loops
231 HashTable<FuncInst *, uint32_t, uintptr_t, 0> inst_id_map(128);
232 uint32_t inst_counter = 0;
234 HashTable<void *, ModelAction *, uintptr_t, 0> loc_act_map(128);
235 HashTable<FuncInst *, ModelAction *, uintptr_t, 0> inst_act_map(128);
237 sllnode<ModelAction *> *it = act_list->begin();
238 Predicate * curr_pred = predicate_tree_entry;
240 ModelAction * next_act = it->getVal();
241 FuncInst * next_inst = get_inst(next_act);
243 SnapVector<Predicate *> unset_predicates = SnapVector<Predicate *>();
244 bool branch_found = follow_branch(&curr_pred, next_inst, next_act, &inst_act_map, &unset_predicates);
246 // A branch with unset predicate expression is detected
247 if (!branch_found && unset_predicates.size() != 0) {
248 ASSERT(unset_predicates.size() == 1);
249 Predicate * one_branch = unset_predicates[0];
251 bool amended = amend_predicate_expr(&curr_pred, next_inst, next_act);
255 curr_pred = one_branch;
261 if (!branch_found && inst_id_map.contains(next_inst)) {
262 FuncInst * curr_inst = curr_pred->get_func_inst();
263 uint32_t curr_id = inst_id_map.get(curr_inst);
264 uint32_t next_id = inst_id_map.get(next_inst);
266 if (curr_id >= next_id) {
267 Predicate * old_pred = inst_pred_map.get(next_inst);
268 Predicate * back_pred = old_pred->get_parent();
270 curr_pred->add_backedge(back_pred);
271 curr_pred = back_pred;
277 // Generate new branches
279 SnapVector<struct half_pred_expr *> half_pred_expressions;
280 infer_predicates(next_inst, next_act, &loc_act_map, &half_pred_expressions);
281 generate_predicates(&curr_pred, next_inst, &half_pred_expressions);
285 if (next_act->is_write())
286 curr_pred->set_write(true);
288 inst_pred_map.put(next_inst, curr_pred);
289 if (!inst_id_map.contains(next_inst))
290 inst_id_map.put(next_inst, inst_counter++);
292 loc_act_map.put(next_act->get_location(), next_act);
293 inst_act_map.put(next_inst, next_act);
298 /* Given curr_pred and next_inst, find the branch following curr_pred that
299 * contains next_inst and the correct predicate.
300 * @return true if branch found, false otherwise.
302 bool FuncNode::follow_branch(Predicate ** curr_pred, FuncInst * next_inst, ModelAction * next_act,
303 HashTable<FuncInst *, ModelAction *, uintptr_t, 0> * inst_act_map,
304 SnapVector<Predicate *> * unset_predicates)
306 /* check if a branch with func_inst and corresponding predicate exists */
307 bool branch_found = false;
308 ModelVector<Predicate *> * branches = (*curr_pred)->get_children();
309 for (uint i = 0; i < branches->size(); i++) {
310 Predicate * branch = (*branches)[i];
311 if (branch->get_func_inst() != next_inst)
314 /* check against predicate expressions */
315 bool predicate_correct = true;
316 PredExprSet * pred_expressions = branch->get_pred_expressions();
317 PredExprSetIter * pred_expr_it = pred_expressions->iterator();
319 /* Only read and rmw actions my have unset predicate expressions */
320 if (pred_expressions->getSize() == 0) {
321 predicate_correct = false;
322 unset_predicates->push_back(branch);
325 while (pred_expr_it->hasNext()) {
326 pred_expr * pred_expression = pred_expr_it->next();
327 uint64_t last_read, next_read;
330 switch(pred_expression->token) {
332 predicate_correct = true;
335 FuncInst * to_be_compared;
336 ModelAction * last_act;
338 to_be_compared = pred_expression->func_inst;
339 last_act = inst_act_map->get(to_be_compared);
341 last_read = last_act->get_reads_from_value();
342 next_read = next_act->get_reads_from_value();
343 equality = (last_read == next_read);
344 if (equality != pred_expression->value)
345 predicate_correct = false;
349 next_read = next_act->get_reads_from_value();
350 equality = ((void*)next_read == NULL);
351 if (equality != pred_expression->value)
352 predicate_correct = false;
355 predicate_correct = false;
356 model_print("unkown predicate token\n");
361 if (predicate_correct) {
371 /* Infer predicate expressions, which are generated in FuncNode::generate_predicates */
372 void FuncNode::infer_predicates(FuncInst * next_inst, ModelAction * next_act,
373 HashTable<void *, ModelAction *, uintptr_t, 0> * loc_act_map,
374 SnapVector<struct half_pred_expr *> * half_pred_expressions)
376 void * loc = next_act->get_location();
378 if (next_inst->is_read()) {
380 if ( loc_act_map->contains(loc) ) {
381 ModelAction * last_act = loc_act_map->get(loc);
382 FuncInst * last_inst = get_inst(last_act);
383 struct half_pred_expr * expression = new half_pred_expr(EQUALITY, last_inst);
384 half_pred_expressions->push_back(expression);
385 } else if ( next_inst->is_single_location() ){
386 loc_set_t * loc_may_equal = loc_may_equal_map->get(loc);
388 if (loc_may_equal != NULL) {
389 loc_set_iter * loc_it = loc_may_equal->iterator();
390 while (loc_it->hasNext()) {
391 void * neighbor = loc_it->next();
392 if (loc_act_map->contains(neighbor)) {
393 ModelAction * last_act = loc_act_map->get(neighbor);
394 FuncInst * last_inst = get_inst(last_act);
396 struct half_pred_expr * expression = new half_pred_expr(EQUALITY, last_inst);
397 half_pred_expressions->push_back(expression);
402 // next_inst is not single location
403 uint64_t read_val = next_act->get_reads_from_value();
405 // only infer NULLITY predicate when it is actually NULL.
406 if ( (void*)read_val == NULL) {
407 struct half_pred_expr * expression = new half_pred_expr(NULLITY, NULL);
408 half_pred_expressions->push_back(expression);
413 // TODO: do anything here?
417 /* Able to generate complex predicates when there are multiple predciate expressions */
418 void FuncNode::generate_predicates(Predicate ** curr_pred, FuncInst * next_inst,
419 SnapVector<struct half_pred_expr *> * half_pred_expressions)
421 if (half_pred_expressions->size() == 0) {
422 Predicate * new_pred = new Predicate(next_inst);
423 (*curr_pred)->add_child(new_pred);
424 new_pred->set_parent(*curr_pred);
426 /* entry predicates and predicates containing pure write actions
427 * have no predicate expressions */
428 if ( (*curr_pred)->is_entry_predicate() )
429 new_pred->add_predicate_expr(NOPREDICATE, NULL, true);
430 else if (next_inst->is_write()) {
431 /* next_inst->is_write() <==> pure writes */
432 new_pred->add_predicate_expr(NOPREDICATE, NULL, true);
438 SnapVector<Predicate *> predicates;
440 struct half_pred_expr * half_expr = (*half_pred_expressions)[0];
441 predicates.push_back(new Predicate(next_inst));
442 predicates.push_back(new Predicate(next_inst));
444 predicates[0]->add_predicate_expr(half_expr->token, half_expr->func_inst, true);
445 predicates[1]->add_predicate_expr(half_expr->token, half_expr->func_inst, false);
447 for (uint i = 1; i < half_pred_expressions->size(); i++) {
448 half_expr = (*half_pred_expressions)[i];
450 uint old_size = predicates.size();
451 for (uint j = 0; j < old_size; j++) {
452 Predicate * pred = predicates[j];
453 Predicate * new_pred = new Predicate(next_inst);
454 new_pred->copy_predicate_expr(pred);
456 pred->add_predicate_expr(half_expr->token, half_expr->func_inst, true);
457 new_pred->add_predicate_expr(half_expr->token, half_expr->func_inst, false);
459 predicates.push_back(new_pred);
463 for (uint i = 0; i < predicates.size(); i++) {
464 Predicate * pred= predicates[i];
465 (*curr_pred)->add_child(pred);
466 pred->set_parent(*curr_pred);
470 /* Amend predicates that contain no predicate expressions. Currenlty only amend with NULLITY predicates */
471 bool FuncNode::amend_predicate_expr(Predicate ** curr_pred, FuncInst * next_inst, ModelAction * next_act)
473 // there should only be only child
474 Predicate * unset_pred = (*curr_pred)->get_children()->back();
475 uint64_t read_val = next_act->get_reads_from_value();
477 // only generate NULLITY predicate when it is actually NULL.
478 if ( !next_inst->is_single_location() && (void*)read_val == NULL ) {
479 Predicate * new_pred = new Predicate(next_inst);
481 (*curr_pred)->add_child(new_pred);
482 new_pred->set_parent(*curr_pred);
484 unset_pred->add_predicate_expr(NULLITY, NULL, false);
485 new_pred->add_predicate_expr(NULLITY, NULL, true);
493 void FuncNode::add_to_val_loc_map(uint64_t val, void * loc)
495 loc_set_t * locations = val_loc_map->get(val);
497 if (locations == NULL) {
498 locations = new loc_set_t();
499 val_loc_map->put(val, locations);
502 update_loc_may_equal_map(loc, locations);
504 // values_may_read_from->add(val);
507 void FuncNode::add_to_val_loc_map(value_set_t * values, void * loc)
512 value_set_iter * it = values->iterator();
513 while (it->hasNext()) {
514 uint64_t val = it->next();
515 add_to_val_loc_map(val, loc);
519 void FuncNode::update_loc_may_equal_map(void * new_loc, loc_set_t * old_locations)
521 if ( old_locations->contains(new_loc) )
524 loc_set_t * neighbors = loc_may_equal_map->get(new_loc);
526 if (neighbors == NULL) {
527 neighbors = new loc_set_t();
528 loc_may_equal_map->put(new_loc, neighbors);
531 loc_set_iter * loc_it = old_locations->iterator();
532 while (loc_it->hasNext()) {
533 // new_loc: { old_locations, ... }
534 void * member = loc_it->next();
535 neighbors->add(member);
537 // for each i in old_locations, i : { new_loc, ... }
538 loc_set_t * _neighbors = loc_may_equal_map->get(member);
539 if (_neighbors == NULL) {
540 _neighbors = new loc_set_t();
541 loc_may_equal_map->put(member, _neighbors);
543 _neighbors->add(new_loc);
547 /* Every time a thread enters a function, set its position to the predicate tree entry */
548 void FuncNode::init_predicate_tree_position(thread_id_t tid)
550 int thread_id = id_to_int(tid);
551 if (predicate_tree_position.size() <= (uint) thread_id)
552 predicate_tree_position.resize(thread_id + 1);
554 predicate_tree_position[thread_id] = predicate_tree_entry;
557 void FuncNode::set_predicate_tree_position(thread_id_t tid, Predicate * pred)
559 int thread_id = id_to_int(tid);
560 predicate_tree_position[thread_id] = pred;
563 /* @return The position of a thread in a predicate tree */
564 Predicate * FuncNode::get_predicate_tree_position(thread_id_t tid)
566 int thread_id = id_to_int(tid);
567 return predicate_tree_position[thread_id];
570 /* Make sure elements of thrd_inst_act_map are initialized properly when threads enter functions */
571 void FuncNode::init_inst_act_map(thread_id_t tid)
573 int thread_id = id_to_int(tid);
574 uint old_size = thrd_inst_act_map->size();
576 if (thrd_inst_act_map->size() <= (uint) thread_id) {
577 uint new_size = thread_id + 1;
578 thrd_inst_act_map->resize(new_size);
580 for (uint i = old_size; i < new_size; i++)
581 (*thrd_inst_act_map)[i] = new inst_act_map_t(128);
585 /* Reset elements of thrd_inst_act_map when threads exit functions */
586 void FuncNode::reset_inst_act_map(thread_id_t tid)
588 int thread_id = id_to_int(tid);
589 inst_act_map_t * map = (*thrd_inst_act_map)[thread_id];
593 void FuncNode::update_inst_act_map(thread_id_t tid, ModelAction * read_act)
595 int thread_id = id_to_int(tid);
596 inst_act_map_t * map = (*thrd_inst_act_map)[thread_id];
597 FuncInst * read_inst = get_inst(read_act);
598 map->put(read_inst, read_act);
601 inst_act_map_t * FuncNode::get_inst_act_map(thread_id_t tid)
603 int thread_id = id_to_int(tid);
604 return (*thrd_inst_act_map)[thread_id];
607 /* Add FuncNodes that this node may follow */
608 void FuncNode::add_out_edge(FuncNode * other)
610 if ( !edge_table.contains(other) ) {
611 edge_table.put(other, OUT_EDGE);
612 out_edges.push_back(other);
616 edge_type_t edge = edge_table.get(other);
617 if (edge == IN_EDGE) {
618 edge_table.put(other, BI_EDGE);
619 out_edges.push_back(other);
623 void FuncNode::print_predicate_tree()
625 model_print("digraph function_%s {\n", func_name);
626 predicate_tree_entry->print_pred_subtree();
627 model_print("}\n"); // end of graph
630 void FuncNode::print_val_loc_map()
633 value_set_iter * val_it = values_may_read_from->iterator();
634 while (val_it->hasNext()) {
635 uint64_t value = val_it->next();
636 model_print("val %llx: ", value);
638 loc_set_t * locations = val_loc_map->get(value);
639 loc_set_iter * loc_it = locations->iterator();
640 while (loc_it->hasNext()) {
641 void * location = loc_it->next();
642 model_print("%p ", location);