6 #include "concretepredicate.h"
10 FuncNode::FuncNode(ModelHistory * history) :
17 predicate_tree_position(),
21 predicate_tree_entry = new Predicate(NULL, true);
22 predicate_tree_entry->add_predicate_expr(NOPREDICATE, NULL, true);
23 predicate_tree_exit = new Predicate(NULL, false, true);
24 predicate_tree_exit->set_depth(MAX_DEPTH);
26 /* Snapshot data structures below */
27 action_list_buffer = new SnapList<action_list_t *>();
28 read_locations = new loc_set_t();
29 write_locations = new loc_set_t();
30 val_loc_map = new HashTable<uint64_t, loc_set_t *, uint64_t, 0, snapshot_malloc, snapshot_calloc, snapshot_free, int64_hash>();
31 loc_may_equal_map = new HashTable<void *, loc_set_t *, uintptr_t, 0>();
33 //values_may_read_from = new value_set_t();
36 /* Reallocate snapshotted memories when new executions start */
37 void FuncNode::set_new_exec_flag()
39 action_list_buffer = new SnapList<action_list_t *>();
40 read_locations = new loc_set_t();
41 write_locations = new loc_set_t();
42 val_loc_map = new HashTable<uint64_t, loc_set_t *, uint64_t, 0, snapshot_malloc, snapshot_calloc, snapshot_free, int64_hash>();
43 loc_may_equal_map = new HashTable<void *, loc_set_t *, uintptr_t, 0>();
45 //values_may_read_from = new value_set_t();
48 /* Check whether FuncInst with the same type, position, and location
49 * as act has been added to func_inst_map or not. If not, add it.
51 void FuncNode::add_inst(ModelAction *act)
54 const char * position = act->get_position();
56 /* THREAD* actions, ATOMIC_LOCK, ATOMIC_TRYLOCK, and ATOMIC_UNLOCK
57 * actions are not tagged with their source line numbers
62 FuncInst * func_inst = func_inst_map.get(position);
64 /* This position has not been inserted into hashtable before */
65 if (func_inst == NULL) {
66 func_inst = create_new_inst(act);
67 func_inst_map.put(position, func_inst);
71 /* Volatile variables that use ++ or -- syntax may result in read and write actions with the same position */
72 if (func_inst->get_type() != act->get_type()) {
73 FuncInst * collision_inst = func_inst->search_in_collision(act);
75 if (collision_inst == NULL) {
76 collision_inst = create_new_inst(act);
77 func_inst->add_to_collision(collision_inst);
80 func_inst = collision_inst;
84 ASSERT(func_inst->get_type() == act->get_type());
85 int curr_execution_number = model->get_execution_number();
87 /* Reset locations when new executions start */
88 if (func_inst->get_execution_number() != curr_execution_number) {
89 func_inst->set_location(act->get_location());
90 func_inst->set_execution_number(curr_execution_number);
93 /* Mark the memory location of such inst as not unique */
94 if (func_inst->get_location() != act->get_location())
95 func_inst->not_single_location();
98 FuncInst * FuncNode::create_new_inst(ModelAction * act)
100 FuncInst * func_inst = new FuncInst(act, this);
101 int exec_num = model->get_execution_number();
102 func_inst->set_execution_number(exec_num);
104 inst_list.push_back(func_inst);
110 /* Get the FuncInst with the same type, position, and location
113 * @return FuncInst with the same type, position, and location as act */
114 FuncInst * FuncNode::get_inst(ModelAction *act)
117 const char * position = act->get_position();
119 /* THREAD* actions, ATOMIC_LOCK, ATOMIC_TRYLOCK, and ATOMIC_UNLOCK
120 * actions are not tagged with their source line numbers
122 if (position == NULL)
125 FuncInst * inst = func_inst_map.get(position);
129 action_type inst_type = inst->get_type();
130 action_type act_type = act->get_type();
132 if (inst_type == act_type) {
135 /* RMWRCAS actions are converted to RMW or READ actions */
136 else if (inst_type == ATOMIC_RMWRCAS &&
137 (act_type == ATOMIC_RMW || act_type == ATOMIC_READ)) {
140 /* Return the FuncInst in the collision list */
142 return inst->search_in_collision(act);
147 void FuncNode::add_entry_inst(FuncInst * inst)
152 mllnode<FuncInst *> * it;
153 for (it = entry_insts.begin();it != NULL;it = it->getNext()) {
154 if (inst == it->getVal())
158 entry_insts.push_back(inst);
162 * @brief Convert ModelAdtion list to FuncInst list
163 * @param act_list A list of ModelActions
165 void FuncNode::update_tree(action_list_t * act_list)
167 if (act_list == NULL || act_list->size() == 0)
170 HashTable<void *, value_set_t *, uintptr_t, 0> * write_history = history->getWriteHistory();
172 /* build inst_list from act_list for later processing */
173 func_inst_list_t inst_list;
174 action_list_t rw_act_list;
176 for (sllnode<ModelAction *> * it = act_list->begin();it != NULL;it = it->getNext()) {
177 ModelAction * act = it->getVal();
178 FuncInst * func_inst = get_inst(act);
179 void * loc = act->get_location();
181 if (func_inst == NULL)
184 inst_list.push_back(func_inst);
185 bool act_added = false;
187 if (act->is_write()) {
188 rw_act_list.push_back(act);
190 if (!write_locations->contains(loc)) {
191 write_locations->add(loc);
192 history->update_loc_wr_func_nodes_map(loc, this);
196 if (act->is_read()) {
198 rw_act_list.push_back(act);
200 /* If func_inst may only read_from a single location, then:
202 * The first time an action reads from some location,
203 * import all the values that have been written to this
204 * location from ModelHistory and notify ModelHistory
205 * that this FuncNode may read from this location.
207 if (!read_locations->contains(loc) && func_inst->is_single_location()) {
208 read_locations->add(loc);
209 value_set_t * write_values = write_history->get(loc);
210 add_to_val_loc_map(write_values, loc);
211 history->update_loc_rd_func_nodes_map(loc, this);
216 // model_print("function %s\n", func_name);
217 // print_val_loc_map();
219 update_inst_tree(&inst_list);
220 update_predicate_tree(&rw_act_list);
222 // print_predicate_tree();
226 * @brief Link FuncInsts in inst_list - add one FuncInst to another's predecessors and successors
227 * @param inst_list A list of FuncInsts
229 void FuncNode::update_inst_tree(func_inst_list_t * inst_list)
231 if (inst_list == NULL)
233 else if (inst_list->size() == 0)
237 sllnode<FuncInst *>* it = inst_list->begin();
238 sllnode<FuncInst *>* prev;
240 /* add the first instruction to the list of entry insts */
241 FuncInst * entry_inst = it->getVal();
242 add_entry_inst(entry_inst);
246 prev = it->getPrev();
248 FuncInst * prev_inst = prev->getVal();
249 FuncInst * curr_inst = it->getVal();
251 prev_inst->add_succ(curr_inst);
252 curr_inst->add_pred(prev_inst);
258 void FuncNode::update_predicate_tree(action_list_t * act_list)
260 if (act_list == NULL || act_list->size() == 0)
265 /* Map a FuncInst to the its predicate */
266 HashTable<FuncInst *, Predicate *, uintptr_t, 0> inst_pred_map(128);
268 // Number FuncInsts to detect loops
269 HashTable<FuncInst *, uint32_t, uintptr_t, 0> inst_id_map(128);
270 uint32_t inst_counter = 0;
272 /* Only need to store the locations of read actions */
273 HashTable<void *, ModelAction *, uintptr_t, 0> loc_act_map(128);
275 sllnode<ModelAction *> *it = act_list->begin();
276 Predicate * curr_pred = predicate_tree_entry;
278 ModelAction * next_act = it->getVal();
279 FuncInst * next_inst = get_inst(next_act);
280 next_inst->set_associated_act(next_act, marker);
282 SnapVector<Predicate *> unset_predicates = SnapVector<Predicate *>();
283 bool branch_found = follow_branch(&curr_pred, next_inst, next_act, &unset_predicates);
285 // A branch with unset predicate expression is detected
286 if (!branch_found && unset_predicates.size() != 0) {
287 ASSERT(unset_predicates.size() == 1);
288 Predicate * one_branch = unset_predicates[0];
290 bool amended = amend_predicate_expr(&curr_pred, next_inst, next_act);
294 curr_pred = one_branch;
300 if (!branch_found && inst_id_map.contains(next_inst)) {
301 FuncInst * curr_inst = curr_pred->get_func_inst();
302 uint32_t curr_id = inst_id_map.get(curr_inst);
303 uint32_t next_id = inst_id_map.get(next_inst);
305 if (curr_id >= next_id) {
306 Predicate * old_pred = inst_pred_map.get(next_inst);
307 Predicate * back_pred = old_pred->get_parent();
309 curr_pred->add_backedge(back_pred);
310 curr_pred = back_pred;
315 // Generate new branches
317 SnapVector<struct half_pred_expr *> half_pred_expressions;
318 infer_predicates(next_inst, next_act, &loc_act_map, &half_pred_expressions);
319 generate_predicates(&curr_pred, next_inst, &half_pred_expressions);
323 if (next_act->is_write())
324 curr_pred->set_write(true);
326 if (next_act->is_read()) {
327 loc_act_map.put(next_act->get_location(), next_act);
330 inst_pred_map.put(next_inst, curr_pred);
331 if (!inst_id_map.contains(next_inst))
332 inst_id_map.put(next_inst, inst_counter++);
335 curr_pred->incr_expl_count();
338 curr_pred->set_exit(predicate_tree_exit);
341 /* Given curr_pred and next_inst, find the branch following curr_pred that
342 * contains next_inst and the correct predicate.
343 * @return true if branch found, false otherwise.
345 bool FuncNode::follow_branch(Predicate ** curr_pred, FuncInst * next_inst,
346 ModelAction * next_act, SnapVector<Predicate *> * unset_predicates)
348 /* Check if a branch with func_inst and corresponding predicate exists */
349 bool branch_found = false;
350 ModelVector<Predicate *> * branches = (*curr_pred)->get_children();
351 for (uint i = 0;i < branches->size();i++) {
352 Predicate * branch = (*branches)[i];
353 if (branch->get_func_inst() != next_inst)
356 /* Check against predicate expressions */
357 bool predicate_correct = true;
358 PredExprSet * pred_expressions = branch->get_pred_expressions();
359 PredExprSetIter * pred_expr_it = pred_expressions->iterator();
361 /* Only read and rmw actions my have unset predicate expressions */
362 if (pred_expressions->getSize() == 0) {
363 predicate_correct = false;
364 unset_predicates->push_back(branch);
367 while (pred_expr_it->hasNext()) {
368 pred_expr * pred_expression = pred_expr_it->next();
369 uint64_t last_read, next_read;
372 switch(pred_expression->token) {
374 predicate_correct = true;
377 FuncInst * to_be_compared;
378 ModelAction * last_act;
380 to_be_compared = pred_expression->func_inst;
381 last_act = to_be_compared->get_associated_act(marker);
383 last_read = last_act->get_reads_from_value();
384 next_read = next_act->get_reads_from_value();
385 equality = (last_read == next_read);
386 if (equality != pred_expression->value)
387 predicate_correct = false;
391 next_read = next_act->get_reads_from_value();
392 // TODO: implement likely to be null
393 equality = ( (void*) (next_read & 0xffffffff) == NULL);
394 if (equality != pred_expression->value)
395 predicate_correct = false;
398 predicate_correct = false;
399 model_print("unkown predicate token\n");
404 if (predicate_correct) {
414 /* Infer predicate expressions, which are generated in FuncNode::generate_predicates */
415 void FuncNode::infer_predicates(FuncInst * next_inst, ModelAction * next_act,
416 HashTable<void *, ModelAction *, uintptr_t, 0> * loc_act_map,
417 SnapVector<struct half_pred_expr *> * half_pred_expressions)
419 void * loc = next_act->get_location();
421 if (next_inst->is_read()) {
423 if ( loc_act_map->contains(loc) ) {
424 ModelAction * last_act = loc_act_map->get(loc);
425 FuncInst * last_inst = get_inst(last_act);
426 struct half_pred_expr * expression = new half_pred_expr(EQUALITY, last_inst);
427 half_pred_expressions->push_back(expression);
428 } else if ( next_inst->is_single_location() ) {
429 loc_set_t * loc_may_equal = loc_may_equal_map->get(loc);
431 if (loc_may_equal != NULL) {
432 loc_set_iter * loc_it = loc_may_equal->iterator();
433 while (loc_it->hasNext()) {
434 void * neighbor = loc_it->next();
435 if (loc_act_map->contains(neighbor)) {
436 ModelAction * last_act = loc_act_map->get(neighbor);
437 FuncInst * last_inst = get_inst(last_act);
439 struct half_pred_expr * expression = new half_pred_expr(EQUALITY, last_inst);
440 half_pred_expressions->push_back(expression);
445 // next_inst is not single location
446 uint64_t read_val = next_act->get_reads_from_value();
448 // only infer NULLITY predicate when it is actually NULL.
449 if ( (void*)read_val == NULL) {
450 struct half_pred_expr * expression = new half_pred_expr(NULLITY, NULL);
451 half_pred_expressions->push_back(expression);
456 // TODO: do anything here?
460 /* Able to generate complex predicates when there are multiple predciate expressions */
461 void FuncNode::generate_predicates(Predicate ** curr_pred, FuncInst * next_inst,
462 SnapVector<struct half_pred_expr *> * half_pred_expressions)
464 if (half_pred_expressions->size() == 0) {
465 Predicate * new_pred = new Predicate(next_inst);
466 (*curr_pred)->add_child(new_pred);
467 new_pred->set_parent(*curr_pred);
469 /* entry predicates and predicates containing pure write actions
470 * have no predicate expressions */
471 if ( (*curr_pred)->is_entry_predicate() )
472 new_pred->add_predicate_expr(NOPREDICATE, NULL, true);
473 else if (next_inst->is_write()) {
474 /* next_inst->is_write() <==> pure writes */
475 new_pred->add_predicate_expr(NOPREDICATE, NULL, true);
481 SnapVector<Predicate *> predicates;
483 struct half_pred_expr * half_expr = (*half_pred_expressions)[0];
484 predicates.push_back(new Predicate(next_inst));
485 predicates.push_back(new Predicate(next_inst));
487 predicates[0]->add_predicate_expr(half_expr->token, half_expr->func_inst, true);
488 predicates[1]->add_predicate_expr(half_expr->token, half_expr->func_inst, false);
490 for (uint i = 1;i < half_pred_expressions->size();i++) {
491 half_expr = (*half_pred_expressions)[i];
493 uint old_size = predicates.size();
494 for (uint j = 0;j < old_size;j++) {
495 Predicate * pred = predicates[j];
496 Predicate * new_pred = new Predicate(next_inst);
497 new_pred->copy_predicate_expr(pred);
499 pred->add_predicate_expr(half_expr->token, half_expr->func_inst, true);
500 new_pred->add_predicate_expr(half_expr->token, half_expr->func_inst, false);
502 predicates.push_back(new_pred);
506 for (uint i = 0;i < predicates.size();i++) {
507 Predicate * pred= predicates[i];
508 (*curr_pred)->add_child(pred);
509 pred->set_parent(*curr_pred);
512 /* Free memories allocated by infer_predicate */
513 for (uint i = 0;i < half_pred_expressions->size();i++) {
514 struct half_pred_expr * tmp = (*half_pred_expressions)[i];
519 /* Amend predicates that contain no predicate expressions. Currenlty only amend with NULLITY predicates */
520 bool FuncNode::amend_predicate_expr(Predicate ** curr_pred, FuncInst * next_inst, ModelAction * next_act)
522 // there should only be only child
523 Predicate * unset_pred = (*curr_pred)->get_children()->back();
524 uint64_t read_val = next_act->get_reads_from_value();
526 // only generate NULLITY predicate when it is actually NULL.
527 if ( !next_inst->is_single_location() && (void*)read_val == NULL ) {
528 Predicate * new_pred = new Predicate(next_inst);
530 (*curr_pred)->add_child(new_pred);
531 new_pred->set_parent(*curr_pred);
533 unset_pred->add_predicate_expr(NULLITY, NULL, false);
534 new_pred->add_predicate_expr(NULLITY, NULL, true);
542 void FuncNode::add_to_val_loc_map(uint64_t val, void * loc)
544 loc_set_t * locations = val_loc_map->get(val);
546 if (locations == NULL) {
547 locations = new loc_set_t();
548 val_loc_map->put(val, locations);
551 update_loc_may_equal_map(loc, locations);
553 // values_may_read_from->add(val);
556 void FuncNode::add_to_val_loc_map(value_set_t * values, void * loc)
561 value_set_iter * it = values->iterator();
562 while (it->hasNext()) {
563 uint64_t val = it->next();
564 add_to_val_loc_map(val, loc);
568 void FuncNode::update_loc_may_equal_map(void * new_loc, loc_set_t * old_locations)
570 if ( old_locations->contains(new_loc) )
573 loc_set_t * neighbors = loc_may_equal_map->get(new_loc);
575 if (neighbors == NULL) {
576 neighbors = new loc_set_t();
577 loc_may_equal_map->put(new_loc, neighbors);
580 loc_set_iter * loc_it = old_locations->iterator();
581 while (loc_it->hasNext()) {
582 // new_loc: { old_locations, ... }
583 void * member = loc_it->next();
584 neighbors->add(member);
586 // for each i in old_locations, i : { new_loc, ... }
587 loc_set_t * _neighbors = loc_may_equal_map->get(member);
588 if (_neighbors == NULL) {
589 _neighbors = new loc_set_t();
590 loc_may_equal_map->put(member, _neighbors);
592 _neighbors->add(new_loc);
596 /* Every time a thread enters a function, set its position to the predicate tree entry */
597 void FuncNode::init_predicate_tree_position(thread_id_t tid)
599 int thread_id = id_to_int(tid);
600 if (predicate_tree_position.size() <= (uint) thread_id)
601 predicate_tree_position.resize(thread_id + 1);
603 predicate_tree_position[thread_id] = predicate_tree_entry;
606 void FuncNode::set_predicate_tree_position(thread_id_t tid, Predicate * pred)
608 int thread_id = id_to_int(tid);
609 predicate_tree_position[thread_id] = pred;
612 /* @return The position of a thread in a predicate tree */
613 Predicate * FuncNode::get_predicate_tree_position(thread_id_t tid)
615 int thread_id = id_to_int(tid);
616 return predicate_tree_position[thread_id];
619 /* Make sure elements of thrd_inst_act_map are initialized properly when threads enter functions */
620 void FuncNode::init_inst_act_map(thread_id_t tid)
622 int thread_id = id_to_int(tid);
623 SnapVector<inst_act_map_t *> * thrd_inst_act_map = history->getThrdInstActMap(func_id);
624 uint old_size = thrd_inst_act_map->size();
626 if (thrd_inst_act_map->size() <= (uint) thread_id) {
627 uint new_size = thread_id + 1;
628 thrd_inst_act_map->resize(new_size);
630 for (uint i = old_size;i < new_size;i++)
631 (*thrd_inst_act_map)[i] = new inst_act_map_t(128);
635 /* Reset elements of thrd_inst_act_map when threads exit functions */
636 void FuncNode::reset_inst_act_map(thread_id_t tid)
638 int thread_id = id_to_int(tid);
639 SnapVector<inst_act_map_t *> * thrd_inst_act_map = history->getThrdInstActMap(func_id);
641 inst_act_map_t * map = (*thrd_inst_act_map)[thread_id];
645 void FuncNode::update_inst_act_map(thread_id_t tid, ModelAction * read_act)
647 int thread_id = id_to_int(tid);
648 SnapVector<inst_act_map_t *> * thrd_inst_act_map = history->getThrdInstActMap(func_id);
650 inst_act_map_t * map = (*thrd_inst_act_map)[thread_id];
651 FuncInst * read_inst = get_inst(read_act);
652 map->put(read_inst, read_act);
655 inst_act_map_t * FuncNode::get_inst_act_map(thread_id_t tid)
657 int thread_id = id_to_int(tid);
658 SnapVector<inst_act_map_t *> * thrd_inst_act_map = history->getThrdInstActMap(func_id);
660 return (*thrd_inst_act_map)[thread_id];
663 /* Add FuncNodes that this node may follow */
664 void FuncNode::add_out_edge(FuncNode * other)
666 if ( !edge_table.contains(other) ) {
667 edge_table.put(other, OUT_EDGE);
668 out_edges.push_back(other);
672 edge_type_t edge = edge_table.get(other);
673 if (edge == IN_EDGE) {
674 edge_table.put(other, BI_EDGE);
675 out_edges.push_back(other);
679 /* Compute the distance between this FuncNode and the target node.
680 * Return -1 if the target node is unreachable or the actual distance
681 * is greater than max_step.
683 int FuncNode::compute_distance(FuncNode * target, int max_step)
687 else if (target == this)
690 SnapList<FuncNode *> queue;
691 HashTable<FuncNode *, int, uintptr_t, 0> distances(128);
693 queue.push_back(this);
694 distances.put(this, 0);
696 while (!queue.empty()) {
697 FuncNode * curr = queue.front();
699 int dist = distances.get(curr);
701 if (max_step <= dist)
704 ModelList<FuncNode *> * outEdges = curr->get_out_edges();
705 mllnode<FuncNode *> * it;
706 for (it = outEdges->begin();it != NULL;it = it->getNext()) {
707 FuncNode * out_node = it->getVal();
709 /* This node has not been visited before */
710 if ( !distances.contains(out_node) ) {
711 if (out_node == target)
714 queue.push_back(out_node);
715 distances.put(out_node, dist + 1);
720 /* Target node is unreachable */
724 void FuncNode::print_predicate_tree()
726 model_print("digraph function_%s {\n", func_name);
727 predicate_tree_entry->print_pred_subtree();
728 predicate_tree_exit->print_predicate();
729 model_print("}\n"); // end of graph
732 void FuncNode::print_val_loc_map()
735 value_set_iter * val_it = values_may_read_from->iterator();
736 while (val_it->hasNext()) {
737 uint64_t value = val_it->next();
738 model_print("val %llx: ", value);
740 loc_set_t * locations = val_loc_map->get(value);
741 loc_set_iter * loc_it = locations->iterator();
742 while (loc_it->hasNext()) {
743 void * location = loc_it->next();
744 model_print("%p ", location);