6 #include "concretepredicate.h"
11 FuncNode::FuncNode(ModelHistory * history) :
18 predicate_tree_position(),
23 predicate_tree_entry = new Predicate(NULL, true);
24 predicate_tree_entry->add_predicate_expr(NOPREDICATE, NULL, true);
26 predicate_tree_exit = new Predicate(NULL, false, true);
27 predicate_tree_exit->set_depth(MAX_DEPTH);
29 /* Snapshot data structures below */
30 action_list_buffer = new SnapList<action_list_t *>();
31 read_locations = new loc_set_t();
32 write_locations = new loc_set_t();
33 val_loc_map = new HashTable<uint64_t, loc_set_t *, uint64_t, 0, snapshot_malloc, snapshot_calloc, snapshot_free, int64_hash>();
34 loc_may_equal_map = new HashTable<void *, loc_set_t *, uintptr_t, 0>();
36 //values_may_read_from = new value_set_t();
39 /* Reallocate snapshotted memories when new executions start */
40 void FuncNode::set_new_exec_flag()
42 action_list_buffer = new SnapList<action_list_t *>();
43 read_locations = new loc_set_t();
44 write_locations = new loc_set_t();
45 val_loc_map = new HashTable<uint64_t, loc_set_t *, uint64_t, 0, snapshot_malloc, snapshot_calloc, snapshot_free, int64_hash>();
46 loc_may_equal_map = new HashTable<void *, loc_set_t *, uintptr_t, 0>();
48 //values_may_read_from = new value_set_t();
51 /* Check whether FuncInst with the same type, position, and location
52 * as act has been added to func_inst_map or not. If not, add it.
54 void FuncNode::add_inst(ModelAction *act)
57 const char * position = act->get_position();
59 /* THREAD* actions, ATOMIC_LOCK, ATOMIC_TRYLOCK, and ATOMIC_UNLOCK
60 * actions are not tagged with their source line numbers
65 FuncInst * func_inst = func_inst_map.get(position);
67 /* This position has not been inserted into hashtable before */
68 if (func_inst == NULL) {
69 func_inst = create_new_inst(act);
70 func_inst_map.put(position, func_inst);
74 /* Volatile variables that use ++ or -- syntax may result in read and write actions with the same position */
75 if (func_inst->get_type() != act->get_type()) {
76 FuncInst * collision_inst = func_inst->search_in_collision(act);
78 if (collision_inst == NULL) {
79 collision_inst = create_new_inst(act);
80 func_inst->add_to_collision(collision_inst);
83 func_inst = collision_inst;
87 ASSERT(func_inst->get_type() == act->get_type());
88 int curr_execution_number = model->get_execution_number();
90 /* Reset locations when new executions start */
91 if (func_inst->get_execution_number() != curr_execution_number) {
92 func_inst->set_location(act->get_location());
93 func_inst->set_execution_number(curr_execution_number);
96 /* Mark the memory location of such inst as not unique */
97 if (func_inst->get_location() != act->get_location())
98 func_inst->not_single_location();
101 FuncInst * FuncNode::create_new_inst(ModelAction * act)
103 FuncInst * func_inst = new FuncInst(act, this);
104 int exec_num = model->get_execution_number();
105 func_inst->set_execution_number(exec_num);
107 inst_list.push_back(func_inst);
113 /* Get the FuncInst with the same type, position, and location
116 * @return FuncInst with the same type, position, and location as act */
117 FuncInst * FuncNode::get_inst(ModelAction *act)
120 const char * position = act->get_position();
122 /* THREAD* actions, ATOMIC_LOCK, ATOMIC_TRYLOCK, and ATOMIC_UNLOCK
123 * actions are not tagged with their source line numbers
125 if (position == NULL)
128 FuncInst * inst = func_inst_map.get(position);
132 action_type inst_type = inst->get_type();
133 action_type act_type = act->get_type();
135 if (inst_type == act_type) {
138 /* RMWRCAS actions are converted to RMW or READ actions */
139 else if (inst_type == ATOMIC_RMWRCAS &&
140 (act_type == ATOMIC_RMW || act_type == ATOMIC_READ)) {
143 /* Return the FuncInst in the collision list */
145 return inst->search_in_collision(act);
150 void FuncNode::add_entry_inst(FuncInst * inst)
155 mllnode<FuncInst *> * it;
156 for (it = entry_insts.begin();it != NULL;it = it->getNext()) {
157 if (inst == it->getVal())
161 entry_insts.push_back(inst);
165 * @brief Convert ModelAdtion list to FuncInst list
166 * @param act_list A list of ModelActions
168 void FuncNode::update_tree(action_list_t * act_list)
170 if (act_list == NULL || act_list->size() == 0)
173 HashTable<void *, value_set_t *, uintptr_t, 0> * write_history = history->getWriteHistory();
175 /* build inst_list from act_list for later processing */
176 func_inst_list_t inst_list;
177 action_list_t rw_act_list;
179 for (sllnode<ModelAction *> * it = act_list->begin();it != NULL;it = it->getNext()) {
180 ModelAction * act = it->getVal();
181 FuncInst * func_inst = get_inst(act);
182 void * loc = act->get_location();
184 if (func_inst == NULL)
187 inst_list.push_back(func_inst);
188 bool act_added = false;
190 if (act->is_write()) {
191 rw_act_list.push_back(act);
193 if (!write_locations->contains(loc)) {
194 write_locations->add(loc);
195 history->update_loc_wr_func_nodes_map(loc, this);
199 if (act->is_read()) {
201 rw_act_list.push_back(act);
203 /* If func_inst may only read_from a single location, then:
205 * The first time an action reads from some location,
206 * import all the values that have been written to this
207 * location from ModelHistory and notify ModelHistory
208 * that this FuncNode may read from this location.
210 if (!read_locations->contains(loc) && func_inst->is_single_location()) {
211 read_locations->add(loc);
212 value_set_t * write_values = write_history->get(loc);
213 add_to_val_loc_map(write_values, loc);
214 history->update_loc_rd_func_nodes_map(loc, this);
219 // model_print("function %s\n", func_name);
220 // print_val_loc_map();
222 update_inst_tree(&inst_list);
223 update_predicate_tree(&rw_act_list);
225 // print_predicate_tree();
229 * @brief Link FuncInsts in inst_list - add one FuncInst to another's predecessors and successors
230 * @param inst_list A list of FuncInsts
232 void FuncNode::update_inst_tree(func_inst_list_t * inst_list)
234 if (inst_list == NULL)
236 else if (inst_list->size() == 0)
240 sllnode<FuncInst *>* it = inst_list->begin();
241 sllnode<FuncInst *>* prev;
243 /* add the first instruction to the list of entry insts */
244 FuncInst * entry_inst = it->getVal();
245 add_entry_inst(entry_inst);
249 prev = it->getPrev();
251 FuncInst * prev_inst = prev->getVal();
252 FuncInst * curr_inst = it->getVal();
254 prev_inst->add_succ(curr_inst);
255 curr_inst->add_pred(prev_inst);
261 void FuncNode::update_predicate_tree(action_list_t * act_list)
263 if (act_list == NULL || act_list->size() == 0)
268 /* Map a FuncInst to the its predicate */
269 HashTable<FuncInst *, Predicate *, uintptr_t, 0> inst_pred_map(128);
271 // Number FuncInsts to detect loops
272 HashTable<FuncInst *, uint32_t, uintptr_t, 0> inst_id_map(128);
273 uint32_t inst_counter = 0;
275 /* Only need to store the locations of read actions */
276 HashTable<void *, ModelAction *, uintptr_t, 0> loc_act_map(128);
278 sllnode<ModelAction *> *it = act_list->begin();
279 Predicate * curr_pred = predicate_tree_entry;
281 ModelAction * next_act = it->getVal();
282 FuncInst * next_inst = get_inst(next_act);
283 next_inst->set_associated_act(next_act, marker);
285 SnapVector<Predicate *> unset_predicates = SnapVector<Predicate *>();
286 bool branch_found = follow_branch(&curr_pred, next_inst, next_act, &unset_predicates);
288 // A branch with unset predicate expression is detected
289 if (!branch_found && unset_predicates.size() != 0) {
290 ASSERT(unset_predicates.size() == 1);
291 Predicate * one_branch = unset_predicates[0];
293 bool amended = amend_predicate_expr(curr_pred, next_inst, next_act);
297 curr_pred = one_branch;
303 if (!branch_found && inst_id_map.contains(next_inst)) {
304 FuncInst * curr_inst = curr_pred->get_func_inst();
305 uint32_t curr_id = inst_id_map.get(curr_inst);
306 uint32_t next_id = inst_id_map.get(next_inst);
308 if (curr_id >= next_id) {
309 Predicate * old_pred = inst_pred_map.get(next_inst);
310 Predicate * back_pred = old_pred->get_parent();
312 curr_pred->add_backedge(back_pred);
313 curr_pred = back_pred;
318 // Generate new branches
320 SnapVector<struct half_pred_expr *> half_pred_expressions;
321 infer_predicates(next_inst, next_act, &loc_act_map, &half_pred_expressions);
322 generate_predicates(curr_pred, next_inst, &half_pred_expressions);
326 if (next_act->is_write())
327 curr_pred->set_write(true);
329 if (next_act->is_read()) {
330 loc_act_map.put(next_act->get_location(), next_act);
333 inst_pred_map.put(next_inst, curr_pred);
334 if (!inst_id_map.contains(next_inst))
335 inst_id_map.put(next_inst, inst_counter++);
338 curr_pred->incr_expl_count();
341 if (curr_pred->get_exit() == NULL) {
342 // Exit predicate is unset yet
343 curr_pred->set_exit(predicate_tree_exit);
347 /* Given curr_pred and next_inst, find the branch following curr_pred that
348 * contains next_inst and the correct predicate.
349 * @return true if branch found, false otherwise.
351 bool FuncNode::follow_branch(Predicate ** curr_pred, FuncInst * next_inst,
352 ModelAction * next_act, SnapVector<Predicate *> * unset_predicates)
354 /* Check if a branch with func_inst and corresponding predicate exists */
355 bool branch_found = false;
356 ModelVector<Predicate *> * branches = (*curr_pred)->get_children();
357 for (uint i = 0;i < branches->size();i++) {
358 Predicate * branch = (*branches)[i];
359 if (branch->get_func_inst() != next_inst)
362 /* Check against predicate expressions */
363 bool predicate_correct = true;
364 PredExprSet * pred_expressions = branch->get_pred_expressions();
365 PredExprSetIter * pred_expr_it = pred_expressions->iterator();
367 /* Only read and rmw actions my have unset predicate expressions */
368 if (pred_expressions->getSize() == 0) {
369 predicate_correct = false;
370 unset_predicates->push_back(branch);
373 while (pred_expr_it->hasNext()) {
374 pred_expr * pred_expression = pred_expr_it->next();
375 uint64_t last_read, next_read;
378 switch(pred_expression->token) {
380 predicate_correct = true;
383 FuncInst * to_be_compared;
384 ModelAction * last_act;
386 to_be_compared = pred_expression->func_inst;
387 last_act = to_be_compared->get_associated_act(marker);
389 last_read = last_act->get_reads_from_value();
390 next_read = next_act->get_reads_from_value();
391 equality = (last_read == next_read);
392 if (equality != pred_expression->value)
393 predicate_correct = false;
397 next_read = next_act->get_reads_from_value();
398 // TODO: implement likely to be null
399 equality = ( (void*) (next_read & 0xffffffff) == NULL);
400 if (equality != pred_expression->value)
401 predicate_correct = false;
404 predicate_correct = false;
405 model_print("unkown predicate token\n");
410 if (predicate_correct) {
420 /* Infer predicate expressions, which are generated in FuncNode::generate_predicates */
421 void FuncNode::infer_predicates(FuncInst * next_inst, ModelAction * next_act,
422 HashTable<void *, ModelAction *, uintptr_t, 0> * loc_act_map,
423 SnapVector<struct half_pred_expr *> * half_pred_expressions)
425 void * loc = next_act->get_location();
427 if (next_inst->is_read()) {
429 if ( loc_act_map->contains(loc) ) {
430 ModelAction * last_act = loc_act_map->get(loc);
431 FuncInst * last_inst = get_inst(last_act);
432 struct half_pred_expr * expression = new half_pred_expr(EQUALITY, last_inst);
433 half_pred_expressions->push_back(expression);
434 } else if ( next_inst->is_single_location() ) {
435 loc_set_t * loc_may_equal = loc_may_equal_map->get(loc);
437 if (loc_may_equal != NULL) {
438 loc_set_iter * loc_it = loc_may_equal->iterator();
439 while (loc_it->hasNext()) {
440 void * neighbor = loc_it->next();
441 if (loc_act_map->contains(neighbor)) {
442 ModelAction * last_act = loc_act_map->get(neighbor);
443 FuncInst * last_inst = get_inst(last_act);
445 struct half_pred_expr * expression = new half_pred_expr(EQUALITY, last_inst);
446 half_pred_expressions->push_back(expression);
451 // next_inst is not single location
452 uint64_t read_val = next_act->get_reads_from_value();
454 // only infer NULLITY predicate when it is actually NULL.
455 if ( (void*)read_val == NULL) {
456 struct half_pred_expr * expression = new half_pred_expr(NULLITY, NULL);
457 half_pred_expressions->push_back(expression);
462 // TODO: do anything here?
466 /* Able to generate complex predicates when there are multiple predciate expressions */
467 void FuncNode::generate_predicates(Predicate * curr_pred, FuncInst * next_inst,
468 SnapVector<struct half_pred_expr *> * half_pred_expressions)
470 if (half_pred_expressions->size() == 0) {
471 Predicate * new_pred = new Predicate(next_inst);
472 curr_pred->add_child(new_pred);
473 new_pred->set_parent(curr_pred);
475 /* Maintain predicate leaves */
476 predicate_leaves.add(new_pred);
477 predicate_leaves.remove(curr_pred);
479 /* entry predicates and predicates containing pure write actions
480 * have no predicate expressions */
481 if ( curr_pred->is_entry_predicate() )
482 new_pred->add_predicate_expr(NOPREDICATE, NULL, true);
483 else if (next_inst->is_write()) {
484 /* next_inst->is_write() <==> pure writes */
485 new_pred->add_predicate_expr(NOPREDICATE, NULL, true);
491 SnapVector<Predicate *> predicates;
493 struct half_pred_expr * half_expr = (*half_pred_expressions)[0];
494 predicates.push_back(new Predicate(next_inst));
495 predicates.push_back(new Predicate(next_inst));
497 predicates[0]->add_predicate_expr(half_expr->token, half_expr->func_inst, true);
498 predicates[1]->add_predicate_expr(half_expr->token, half_expr->func_inst, false);
500 for (uint i = 1;i < half_pred_expressions->size();i++) {
501 half_expr = (*half_pred_expressions)[i];
503 uint old_size = predicates.size();
504 for (uint j = 0;j < old_size;j++) {
505 Predicate * pred = predicates[j];
506 Predicate * new_pred = new Predicate(next_inst);
507 new_pred->copy_predicate_expr(pred);
509 pred->add_predicate_expr(half_expr->token, half_expr->func_inst, true);
510 new_pred->add_predicate_expr(half_expr->token, half_expr->func_inst, false);
512 predicates.push_back(new_pred);
516 for (uint i = 0;i < predicates.size();i++) {
517 Predicate * pred= predicates[i];
518 curr_pred->add_child(pred);
519 pred->set_parent(curr_pred);
521 /* Add new predicate leaves */
522 predicate_leaves.add(pred);
525 /* Remove predicate node that has children */
526 predicate_leaves.remove(curr_pred);
528 /* Free memories allocated by infer_predicate */
529 for (uint i = 0;i < half_pred_expressions->size();i++) {
530 struct half_pred_expr * tmp = (*half_pred_expressions)[i];
535 /* Amend predicates that contain no predicate expressions. Currenlty only amend with NULLITY predicates */
536 bool FuncNode::amend_predicate_expr(Predicate * curr_pred, FuncInst * next_inst, ModelAction * next_act)
538 // there should only be only child
539 Predicate * unset_pred = curr_pred->get_children()->back();
540 uint64_t read_val = next_act->get_reads_from_value();
542 // only generate NULLITY predicate when it is actually NULL.
543 if ( !next_inst->is_single_location() && (void*)read_val == NULL ) {
544 Predicate * new_pred = new Predicate(next_inst);
546 curr_pred->add_child(new_pred);
547 new_pred->set_parent(curr_pred);
549 unset_pred->add_predicate_expr(NULLITY, NULL, false);
550 new_pred->add_predicate_expr(NULLITY, NULL, true);
558 void FuncNode::add_to_val_loc_map(uint64_t val, void * loc)
560 loc_set_t * locations = val_loc_map->get(val);
562 if (locations == NULL) {
563 locations = new loc_set_t();
564 val_loc_map->put(val, locations);
567 update_loc_may_equal_map(loc, locations);
569 // values_may_read_from->add(val);
572 void FuncNode::add_to_val_loc_map(value_set_t * values, void * loc)
577 value_set_iter * it = values->iterator();
578 while (it->hasNext()) {
579 uint64_t val = it->next();
580 add_to_val_loc_map(val, loc);
584 void FuncNode::update_loc_may_equal_map(void * new_loc, loc_set_t * old_locations)
586 if ( old_locations->contains(new_loc) )
589 loc_set_t * neighbors = loc_may_equal_map->get(new_loc);
591 if (neighbors == NULL) {
592 neighbors = new loc_set_t();
593 loc_may_equal_map->put(new_loc, neighbors);
596 loc_set_iter * loc_it = old_locations->iterator();
597 while (loc_it->hasNext()) {
598 // new_loc: { old_locations, ... }
599 void * member = loc_it->next();
600 neighbors->add(member);
602 // for each i in old_locations, i : { new_loc, ... }
603 loc_set_t * _neighbors = loc_may_equal_map->get(member);
604 if (_neighbors == NULL) {
605 _neighbors = new loc_set_t();
606 loc_may_equal_map->put(member, _neighbors);
608 _neighbors->add(new_loc);
612 /* Every time a thread enters a function, set its position to the predicate tree entry */
613 void FuncNode::init_predicate_tree_position(thread_id_t tid)
615 int thread_id = id_to_int(tid);
616 if (predicate_tree_position.size() <= (uint) thread_id)
617 predicate_tree_position.resize(thread_id + 1);
619 predicate_tree_position[thread_id] = predicate_tree_entry;
622 void FuncNode::set_predicate_tree_position(thread_id_t tid, Predicate * pred)
624 int thread_id = id_to_int(tid);
625 predicate_tree_position[thread_id] = pred;
628 /* @return The position of a thread in a predicate tree */
629 Predicate * FuncNode::get_predicate_tree_position(thread_id_t tid)
631 int thread_id = id_to_int(tid);
632 return predicate_tree_position[thread_id];
635 /* Make sure elements of thrd_inst_act_map are initialized properly when threads enter functions */
636 void FuncNode::init_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);
640 uint old_size = thrd_inst_act_map->size();
642 if (thrd_inst_act_map->size() <= (uint) thread_id) {
643 uint new_size = thread_id + 1;
644 thrd_inst_act_map->resize(new_size);
646 for (uint i = old_size;i < new_size;i++)
647 (*thrd_inst_act_map)[i] = new inst_act_map_t(128);
651 /* Reset elements of thrd_inst_act_map when threads exit functions */
652 void FuncNode::reset_inst_act_map(thread_id_t tid)
654 int thread_id = id_to_int(tid);
655 SnapVector<inst_act_map_t *> * thrd_inst_act_map = history->getThrdInstActMap(func_id);
657 inst_act_map_t * map = (*thrd_inst_act_map)[thread_id];
661 void FuncNode::update_inst_act_map(thread_id_t tid, ModelAction * read_act)
663 int thread_id = id_to_int(tid);
664 SnapVector<inst_act_map_t *> * thrd_inst_act_map = history->getThrdInstActMap(func_id);
666 inst_act_map_t * map = (*thrd_inst_act_map)[thread_id];
667 FuncInst * read_inst = get_inst(read_act);
668 map->put(read_inst, read_act);
671 inst_act_map_t * FuncNode::get_inst_act_map(thread_id_t tid)
673 int thread_id = id_to_int(tid);
674 SnapVector<inst_act_map_t *> * thrd_inst_act_map = history->getThrdInstActMap(func_id);
676 return (*thrd_inst_act_map)[thread_id];
679 /* Add FuncNodes that this node may follow */
680 void FuncNode::add_out_edge(FuncNode * other)
682 if ( !edge_table.contains(other) ) {
683 edge_table.put(other, OUT_EDGE);
684 out_edges.push_back(other);
688 edge_type_t edge = edge_table.get(other);
689 if (edge == IN_EDGE) {
690 edge_table.put(other, BI_EDGE);
691 out_edges.push_back(other);
695 /* Compute the distance between this FuncNode and the target node.
696 * Return -1 if the target node is unreachable or the actual distance
697 * is greater than max_step.
699 int FuncNode::compute_distance(FuncNode * target, int max_step)
703 else if (target == this)
706 SnapList<FuncNode *> queue;
707 HashTable<FuncNode *, int, uintptr_t, 0> distances(128);
709 queue.push_back(this);
710 distances.put(this, 0);
712 while (!queue.empty()) {
713 FuncNode * curr = queue.front();
715 int dist = distances.get(curr);
717 if (max_step <= dist)
720 ModelList<FuncNode *> * outEdges = curr->get_out_edges();
721 mllnode<FuncNode *> * it;
722 for (it = outEdges->begin();it != NULL;it = it->getNext()) {
723 FuncNode * out_node = it->getVal();
725 /* This node has not been visited before */
726 if ( !distances.contains(out_node) ) {
727 if (out_node == target)
730 queue.push_back(out_node);
731 distances.put(out_node, dist + 1);
736 /* Target node is unreachable */
740 /* Implement quick sort to sort leaves before assigning base scores */
741 static int partition(SnapVector<Predicate *> * arr, int low, int high)
743 unsigned int pivot = (*arr)[high]->get_depth();
746 for (int j = low; j <= high - 1; j++) {
747 if ( (*arr)[j]->get_depth() < pivot ) {
749 Predicate *tmp = (*arr)[i];
750 (*arr)[i] = (*arr)[j];
755 Predicate * tmp = (*arr)[i + 1];
756 (*arr)[i + 1] = (*arr)[high];
762 /* Implement quick sort to sort leaves before assigning base scores */
763 static void quickSort(SnapVector<Predicate *> * arr, int low, int high)
766 int pi = partition(arr, low, high);
768 quickSort(arr, low, pi - 1);
769 quickSort(arr, pi + 1, high);
773 void FuncNode::assign_base_score()
775 PredSetIter * it = predicate_leaves.iterator();
776 SnapVector<Predicate *> leaves;
777 while (it->hasNext()) {
778 Predicate * pred = it->next();
780 leaves.push_back(pred);
783 quickSort(&leaves, 0, leaves.size() - 1);
785 // assign scores for internal nodes;
786 while ( !leaves.empty() ) {
787 Predicate * leaf = leaves.back();
790 Predicate * curr = leaf->get_parent();
791 while (curr != NULL) {
792 if (curr->get_weight() != 0) {
797 ModelVector<Predicate *> * children = curr->get_children();
798 double weight_sum = 0;
799 bool has_unassigned_node = false;
801 for (uint i = 0; i < children->size(); i++) {
802 Predicate * child = (*children)[i];
804 // If a child has unassigned weight
805 double weight = child->get_weight();
807 has_unassigned_node = true;
810 weight_sum += weight;
813 if (!has_unassigned_node) {
814 double average_weight = (double) weight_sum / (double) children->size();
815 double weight = average_weight * pow(0.9, curr->get_depth());
816 curr->set_weight(weight);
820 curr = curr->get_parent();
825 void FuncNode::print_predicate_tree()
827 model_print("digraph function_%s {\n", func_name);
828 predicate_tree_entry->print_pred_subtree();
829 predicate_tree_exit->print_predicate();
830 model_print("}\n"); // end of graph