6 #include "concretepredicate.h"
11 FuncNode::FuncNode(ModelHistory * history) :
21 predicate_tree_position(),
26 predicate_tree_entry = new Predicate(NULL, true);
27 predicate_tree_entry->add_predicate_expr(NOPREDICATE, NULL, true);
29 predicate_tree_exit = new Predicate(NULL, false, true);
30 predicate_tree_exit->set_depth(MAX_DEPTH);
32 /* Snapshot data structures below */
33 action_list_buffer = new SnapList<action_list_t *>();
34 read_locations = new loc_set_t();
35 write_locations = new loc_set_t();
36 val_loc_map = new HashTable<uint64_t, loc_set_t *, uint64_t, 0, snapshot_malloc, snapshot_calloc, snapshot_free, int64_hash>();
37 loc_may_equal_map = new HashTable<void *, loc_set_t *, uintptr_t, 0>();
39 //values_may_read_from = new value_set_t();
42 /* Reallocate snapshotted memories when new executions start */
43 void FuncNode::set_new_exec_flag()
45 action_list_buffer = new SnapList<action_list_t *>();
46 read_locations = new loc_set_t();
47 write_locations = new loc_set_t();
48 val_loc_map = new HashTable<uint64_t, loc_set_t *, uint64_t, 0, snapshot_malloc, snapshot_calloc, snapshot_free, int64_hash>();
49 loc_may_equal_map = new HashTable<void *, loc_set_t *, uintptr_t, 0>();
51 //values_may_read_from = new value_set_t();
54 /* Check whether FuncInst with the same type, position, and location
55 * as act has been added to func_inst_map or not. If not, add it.
57 void FuncNode::add_inst(ModelAction *act)
60 const char * position = act->get_position();
62 /* THREAD* actions, ATOMIC_LOCK, ATOMIC_TRYLOCK, and ATOMIC_UNLOCK
63 * actions are not tagged with their source line numbers
68 FuncInst * func_inst = func_inst_map.get(position);
70 /* This position has not been inserted into hashtable before */
71 if (func_inst == NULL) {
72 func_inst = create_new_inst(act);
73 func_inst_map.put(position, func_inst);
77 /* Volatile variables that use ++ or -- syntax may result in read and write actions with the same position */
78 if (func_inst->get_type() != act->get_type()) {
79 FuncInst * collision_inst = func_inst->search_in_collision(act);
81 if (collision_inst == NULL) {
82 collision_inst = create_new_inst(act);
83 func_inst->add_to_collision(collision_inst);
86 func_inst = collision_inst;
90 ASSERT(func_inst->get_type() == act->get_type());
91 int curr_execution_number = model->get_execution_number();
93 /* Reset locations when new executions start */
94 if (func_inst->get_execution_number() != curr_execution_number) {
95 func_inst->set_location(act->get_location());
96 func_inst->set_execution_number(curr_execution_number);
99 /* Mark the memory location of such inst as not unique */
100 if (func_inst->get_location() != act->get_location())
101 func_inst->not_single_location();
104 FuncInst * FuncNode::create_new_inst(ModelAction * act)
106 FuncInst * func_inst = new FuncInst(act, this);
107 int exec_num = model->get_execution_number();
108 func_inst->set_execution_number(exec_num);
110 inst_list.push_back(func_inst);
116 /* Get the FuncInst with the same type, position, and location
119 * @return FuncInst with the same type, position, and location as act */
120 FuncInst * FuncNode::get_inst(ModelAction *act)
123 const char * position = act->get_position();
125 /* THREAD* actions, ATOMIC_LOCK, ATOMIC_TRYLOCK, and ATOMIC_UNLOCK
126 * actions are not tagged with their source line numbers
128 if (position == NULL)
131 FuncInst * inst = func_inst_map.get(position);
135 action_type inst_type = inst->get_type();
136 action_type act_type = act->get_type();
138 if (inst_type == act_type) {
141 /* RMWRCAS actions are converted to RMW or READ actions */
142 else if (inst_type == ATOMIC_RMWRCAS &&
143 (act_type == ATOMIC_RMW || act_type == ATOMIC_READ)) {
146 /* Return the FuncInst in the collision list */
148 return inst->search_in_collision(act);
153 void FuncNode::add_entry_inst(FuncInst * inst)
158 mllnode<FuncInst *> * it;
159 for (it = entry_insts.begin();it != NULL;it = it->getNext()) {
160 if (inst == it->getVal())
164 entry_insts.push_back(inst);
168 * @brief Convert ModelAdtion list to FuncInst list
169 * @param act_list A list of ModelActions
171 void FuncNode::update_tree(action_list_t * act_list)
173 if (act_list == NULL || act_list->size() == 0)
176 HashTable<void *, value_set_t *, uintptr_t, 0> * write_history = history->getWriteHistory();
178 /* build inst_list from act_list for later processing */
179 func_inst_list_t inst_list;
180 action_list_t rw_act_list;
182 for (sllnode<ModelAction *> * it = act_list->begin();it != NULL;it = it->getNext()) {
183 ModelAction * act = it->getVal();
184 FuncInst * func_inst = get_inst(act);
185 void * loc = act->get_location();
187 if (func_inst == NULL)
190 inst_list.push_back(func_inst);
191 bool act_added = false;
193 if (act->is_write()) {
194 rw_act_list.push_back(act);
196 if (!write_locations->contains(loc)) {
197 write_locations->add(loc);
198 history->update_loc_wr_func_nodes_map(loc, this);
202 if (act->is_read()) {
204 rw_act_list.push_back(act);
206 /* If func_inst may only read_from a single location, then:
208 * The first time an action reads from some location,
209 * import all the values that have been written to this
210 * location from ModelHistory and notify ModelHistory
211 * that this FuncNode may read from this location.
213 if (!read_locations->contains(loc) && func_inst->is_single_location()) {
214 read_locations->add(loc);
215 value_set_t * write_values = write_history->get(loc);
216 add_to_val_loc_map(write_values, loc);
217 history->update_loc_rd_func_nodes_map(loc, this);
222 // model_print("function %s\n", func_name);
223 // print_val_loc_map();
225 update_inst_tree(&inst_list);
226 update_predicate_tree(&rw_act_list);
228 // print_predicate_tree();
232 * @brief Link FuncInsts in inst_list - add one FuncInst to another's predecessors and successors
233 * @param inst_list A list of FuncInsts
235 void FuncNode::update_inst_tree(func_inst_list_t * inst_list)
237 if (inst_list == NULL)
239 else if (inst_list->size() == 0)
243 sllnode<FuncInst *>* it = inst_list->begin();
244 sllnode<FuncInst *>* prev;
246 /* add the first instruction to the list of entry insts */
247 FuncInst * entry_inst = it->getVal();
248 add_entry_inst(entry_inst);
252 prev = it->getPrev();
254 FuncInst * prev_inst = prev->getVal();
255 FuncInst * curr_inst = it->getVal();
257 prev_inst->add_succ(curr_inst);
258 curr_inst->add_pred(prev_inst);
264 void FuncNode::update_predicate_tree(action_list_t * act_list)
266 if (act_list == NULL || act_list->size() == 0)
270 uint32_t inst_counter = 0;
274 inst_pred_map.reset();
277 sllnode<ModelAction *> *it = act_list->begin();
278 Predicate * curr_pred = predicate_tree_entry;
280 ModelAction * next_act = it->getVal();
281 FuncInst * next_inst = get_inst(next_act);
282 next_inst->set_associated_act(next_act, marker);
284 Predicate * unset_predicate = NULL;
285 bool branch_found = follow_branch(&curr_pred, next_inst, next_act, &unset_predicate);
287 // A branch with unset predicate expression is detected
288 if (!branch_found && unset_predicate != NULL) {
289 bool amended = amend_predicate_expr(curr_pred, next_inst, next_act);
293 curr_pred = unset_predicate;
299 if (!branch_found && inst_id_map.contains(next_inst)) {
300 FuncInst * curr_inst = curr_pred->get_func_inst();
301 uint32_t curr_id = inst_id_map.get(curr_inst);
302 uint32_t next_id = inst_id_map.get(next_inst);
304 if (curr_id >= next_id) {
305 Predicate * old_pred = inst_pred_map.get(next_inst);
306 Predicate * back_pred = old_pred->get_parent();
308 curr_pred->add_backedge(back_pred);
309 curr_pred = back_pred;
314 // Generate new branches
316 SnapVector<struct half_pred_expr *> half_pred_expressions;
317 infer_predicates(next_inst, next_act, &half_pred_expressions);
318 generate_predicates(curr_pred, next_inst, &half_pred_expressions);
322 if (next_act->is_write())
323 curr_pred->set_write(true);
325 if (next_act->is_read()) {
326 /* Only need to store the locations of read actions */
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 if (curr_pred->get_exit() == NULL) {
339 // Exit predicate is unset yet
340 curr_pred->set_exit(predicate_tree_exit);
343 update_predicate_tree_weight();
346 /* Given curr_pred and next_inst, find the branch following curr_pred that
347 * contains next_inst and the correct predicate.
348 * @return true if branch found, false otherwise.
350 bool FuncNode::follow_branch(Predicate ** curr_pred, FuncInst * next_inst,
351 ModelAction * next_act, Predicate ** unset_predicate)
353 /* Check if a branch with func_inst and corresponding predicate exists */
354 bool branch_found = false;
355 ModelVector<Predicate *> * branches = (*curr_pred)->get_children();
356 for (uint i = 0;i < branches->size();i++) {
357 Predicate * branch = (*branches)[i];
358 if (branch->get_func_inst() != next_inst)
361 /* Check against predicate expressions */
362 bool predicate_correct = true;
363 PredExprSet * pred_expressions = branch->get_pred_expressions();
365 /* Only read and rmw actions my have unset predicate expressions */
366 if (pred_expressions->getSize() == 0) {
367 predicate_correct = false;
368 if (*unset_predicate == NULL)
369 *unset_predicate = branch;
376 PredExprSetIter * pred_expr_it = pred_expressions->iterator();
377 while (pred_expr_it->hasNext()) {
378 pred_expr * pred_expression = pred_expr_it->next();
379 uint64_t last_read, next_read;
382 switch(pred_expression->token) {
384 predicate_correct = true;
387 FuncInst * to_be_compared;
388 ModelAction * last_act;
390 to_be_compared = pred_expression->func_inst;
391 last_act = to_be_compared->get_associated_act(marker);
393 last_read = last_act->get_reads_from_value();
394 next_read = next_act->get_reads_from_value();
395 equality = (last_read == next_read);
396 if (equality != pred_expression->value)
397 predicate_correct = false;
401 next_read = next_act->get_reads_from_value();
402 // TODO: implement likely to be null
403 equality = ( (void*) (next_read & 0xffffffff) == NULL);
404 if (equality != pred_expression->value)
405 predicate_correct = false;
408 predicate_correct = false;
409 model_print("unkown predicate token\n");
414 if (predicate_correct) {
424 /* Infer predicate expressions, which are generated in FuncNode::generate_predicates */
425 void FuncNode::infer_predicates(FuncInst * next_inst, ModelAction * next_act,
426 SnapVector<struct half_pred_expr *> * half_pred_expressions)
428 void * loc = next_act->get_location();
430 if (next_inst->is_read()) {
432 if ( loc_act_map.contains(loc) ) {
433 ModelAction * last_act = loc_act_map.get(loc);
434 FuncInst * last_inst = get_inst(last_act);
435 struct half_pred_expr * expression = new half_pred_expr(EQUALITY, last_inst);
436 half_pred_expressions->push_back(expression);
437 } else if ( next_inst->is_single_location() ) {
438 loc_set_t * loc_may_equal = loc_may_equal_map->get(loc);
440 if (loc_may_equal != NULL) {
441 loc_set_iter * loc_it = loc_may_equal->iterator();
442 while (loc_it->hasNext()) {
443 void * neighbor = loc_it->next();
444 if (loc_act_map.contains(neighbor)) {
445 ModelAction * last_act = loc_act_map.get(neighbor);
446 FuncInst * last_inst = get_inst(last_act);
448 struct half_pred_expr * expression = new half_pred_expr(EQUALITY, last_inst);
449 half_pred_expressions->push_back(expression);
454 // next_inst is not single location
455 uint64_t read_val = next_act->get_reads_from_value();
457 // only infer NULLITY predicate when it is actually NULL.
458 if ( (void*)read_val == NULL) {
459 struct half_pred_expr * expression = new half_pred_expr(NULLITY, NULL);
460 half_pred_expressions->push_back(expression);
465 // TODO: do anything here?
469 /* Able to generate complex predicates when there are multiple predciate expressions */
470 void FuncNode::generate_predicates(Predicate * curr_pred, FuncInst * next_inst,
471 SnapVector<struct half_pred_expr *> * half_pred_expressions)
473 if (half_pred_expressions->size() == 0) {
474 Predicate * new_pred = new Predicate(next_inst);
475 curr_pred->add_child(new_pred);
476 new_pred->set_parent(curr_pred);
478 /* Maintain predicate leaves */
479 predicate_leaves.add(new_pred);
480 predicate_leaves.remove(curr_pred);
482 /* entry predicates and predicates containing pure write actions
483 * have no predicate expressions */
484 if ( curr_pred->is_entry_predicate() )
485 new_pred->add_predicate_expr(NOPREDICATE, NULL, true);
486 else if (next_inst->is_write()) {
487 /* next_inst->is_write() <==> pure writes */
488 new_pred->add_predicate_expr(NOPREDICATE, NULL, true);
494 SnapVector<Predicate *> predicates;
496 struct half_pred_expr * half_expr = (*half_pred_expressions)[0];
497 predicates.push_back(new Predicate(next_inst));
498 predicates.push_back(new Predicate(next_inst));
500 predicates[0]->add_predicate_expr(half_expr->token, half_expr->func_inst, true);
501 predicates[1]->add_predicate_expr(half_expr->token, half_expr->func_inst, false);
503 for (uint i = 1;i < half_pred_expressions->size();i++) {
504 half_expr = (*half_pred_expressions)[i];
506 uint old_size = predicates.size();
507 for (uint j = 0;j < old_size;j++) {
508 Predicate * pred = predicates[j];
509 Predicate * new_pred = new Predicate(next_inst);
510 new_pred->copy_predicate_expr(pred);
512 pred->add_predicate_expr(half_expr->token, half_expr->func_inst, true);
513 new_pred->add_predicate_expr(half_expr->token, half_expr->func_inst, false);
515 predicates.push_back(new_pred);
519 for (uint i = 0;i < predicates.size();i++) {
520 Predicate * pred= predicates[i];
521 curr_pred->add_child(pred);
522 pred->set_parent(curr_pred);
524 /* Add new predicate leaves */
525 predicate_leaves.add(pred);
528 /* Remove predicate node that has children */
529 predicate_leaves.remove(curr_pred);
531 /* Free memories allocated by infer_predicate */
532 for (uint i = 0;i < half_pred_expressions->size();i++) {
533 struct half_pred_expr * tmp = (*half_pred_expressions)[i];
538 /* Amend predicates that contain no predicate expressions. Currenlty only amend with NULLITY predicates */
539 bool FuncNode::amend_predicate_expr(Predicate * curr_pred, FuncInst * next_inst, ModelAction * next_act)
541 ModelVector<Predicate *> * children = curr_pred->get_children();
542 ASSERT(children->size() == 1);
544 // there should only be only child
545 Predicate * unset_pred = (*children)[0];
546 uint64_t read_val = next_act->get_reads_from_value();
548 // only generate NULLITY predicate when it is actually NULL.
549 if ( !next_inst->is_single_location() && (void*)read_val == NULL ) {
550 Predicate * new_pred = new Predicate(next_inst);
552 curr_pred->add_child(new_pred);
553 new_pred->set_parent(curr_pred);
555 unset_pred->add_predicate_expr(NULLITY, NULL, false);
556 new_pred->add_predicate_expr(NULLITY, NULL, true);
564 void FuncNode::add_to_val_loc_map(uint64_t val, void * loc)
566 loc_set_t * locations = val_loc_map->get(val);
568 if (locations == NULL) {
569 locations = new loc_set_t();
570 val_loc_map->put(val, locations);
573 update_loc_may_equal_map(loc, locations);
575 // values_may_read_from->add(val);
578 void FuncNode::add_to_val_loc_map(value_set_t * values, void * loc)
583 value_set_iter * it = values->iterator();
584 while (it->hasNext()) {
585 uint64_t val = it->next();
586 add_to_val_loc_map(val, loc);
590 void FuncNode::update_loc_may_equal_map(void * new_loc, loc_set_t * old_locations)
592 if ( old_locations->contains(new_loc) )
595 loc_set_t * neighbors = loc_may_equal_map->get(new_loc);
597 if (neighbors == NULL) {
598 neighbors = new loc_set_t();
599 loc_may_equal_map->put(new_loc, neighbors);
602 loc_set_iter * loc_it = old_locations->iterator();
603 while (loc_it->hasNext()) {
604 // new_loc: { old_locations, ... }
605 void * member = loc_it->next();
606 neighbors->add(member);
608 // for each i in old_locations, i : { new_loc, ... }
609 loc_set_t * _neighbors = loc_may_equal_map->get(member);
610 if (_neighbors == NULL) {
611 _neighbors = new loc_set_t();
612 loc_may_equal_map->put(member, _neighbors);
614 _neighbors->add(new_loc);
618 /* Every time a thread enters a function, set its position to the predicate tree entry */
619 void FuncNode::init_predicate_tree_position(thread_id_t tid)
621 int thread_id = id_to_int(tid);
622 if (predicate_tree_position.size() <= (uint) thread_id)
623 predicate_tree_position.resize(thread_id + 1);
625 predicate_tree_position[thread_id] = predicate_tree_entry;
628 void FuncNode::set_predicate_tree_position(thread_id_t tid, Predicate * pred)
630 int thread_id = id_to_int(tid);
631 predicate_tree_position[thread_id] = pred;
634 /* @return The position of a thread in a predicate tree */
635 Predicate * FuncNode::get_predicate_tree_position(thread_id_t tid)
637 int thread_id = id_to_int(tid);
638 return predicate_tree_position[thread_id];
641 /* Make sure elements of thrd_inst_act_map are initialized properly when threads enter functions */
642 void FuncNode::init_inst_act_map(thread_id_t tid)
644 int thread_id = id_to_int(tid);
645 SnapVector<inst_act_map_t *> * thrd_inst_act_map = history->getThrdInstActMap(func_id);
646 uint old_size = thrd_inst_act_map->size();
648 if (thrd_inst_act_map->size() <= (uint) thread_id) {
649 uint new_size = thread_id + 1;
650 thrd_inst_act_map->resize(new_size);
652 for (uint i = old_size;i < new_size;i++)
653 (*thrd_inst_act_map)[i] = new inst_act_map_t(128);
657 /* Reset elements of thrd_inst_act_map when threads exit functions */
658 void FuncNode::reset_inst_act_map(thread_id_t tid)
660 int thread_id = id_to_int(tid);
661 SnapVector<inst_act_map_t *> * thrd_inst_act_map = history->getThrdInstActMap(func_id);
663 inst_act_map_t * map = (*thrd_inst_act_map)[thread_id];
667 void FuncNode::update_inst_act_map(thread_id_t tid, ModelAction * read_act)
669 int thread_id = id_to_int(tid);
670 SnapVector<inst_act_map_t *> * thrd_inst_act_map = history->getThrdInstActMap(func_id);
672 inst_act_map_t * map = (*thrd_inst_act_map)[thread_id];
673 FuncInst * read_inst = get_inst(read_act);
674 map->put(read_inst, read_act);
677 inst_act_map_t * FuncNode::get_inst_act_map(thread_id_t tid)
679 int thread_id = id_to_int(tid);
680 SnapVector<inst_act_map_t *> * thrd_inst_act_map = history->getThrdInstActMap(func_id);
682 return (*thrd_inst_act_map)[thread_id];
685 /* Add FuncNodes that this node may follow */
686 void FuncNode::add_out_edge(FuncNode * other)
688 if ( !edge_table.contains(other) ) {
689 edge_table.put(other, OUT_EDGE);
690 out_edges.push_back(other);
694 edge_type_t edge = edge_table.get(other);
695 if (edge == IN_EDGE) {
696 edge_table.put(other, BI_EDGE);
697 out_edges.push_back(other);
701 /* Compute the distance between this FuncNode and the target node.
702 * Return -1 if the target node is unreachable or the actual distance
703 * is greater than max_step.
705 int FuncNode::compute_distance(FuncNode * target, int max_step)
709 else if (target == this)
712 SnapList<FuncNode *> queue;
713 HashTable<FuncNode *, int, uintptr_t, 0> distances(128);
715 queue.push_back(this);
716 distances.put(this, 0);
718 while (!queue.empty()) {
719 FuncNode * curr = queue.front();
721 int dist = distances.get(curr);
723 if (max_step <= dist)
726 ModelList<FuncNode *> * outEdges = curr->get_out_edges();
727 mllnode<FuncNode *> * it;
728 for (it = outEdges->begin();it != NULL;it = it->getNext()) {
729 FuncNode * out_node = it->getVal();
731 /* This node has not been visited before */
732 if ( !distances.contains(out_node) ) {
733 if (out_node == target)
736 queue.push_back(out_node);
737 distances.put(out_node, dist + 1);
742 /* Target node is unreachable */
746 /* Implement quick sort to sort leaves before assigning base scores */
747 static int partition(SnapVector<Predicate *> * arr, int low, int high)
749 unsigned int pivot = (*arr)[high]->get_depth();
752 for (int j = low; j <= high - 1; j++) {
753 if ( (*arr)[j]->get_depth() < pivot ) {
755 Predicate *tmp = (*arr)[i];
756 (*arr)[i] = (*arr)[j];
761 Predicate * tmp = (*arr)[i + 1];
762 (*arr)[i + 1] = (*arr)[high];
768 /* Implement quick sort to sort leaves before assigning base scores */
769 static void quickSort(SnapVector<Predicate *> * arr, int low, int high)
772 int pi = partition(arr, low, high);
774 quickSort(arr, low, pi - 1);
775 quickSort(arr, pi + 1, high);
779 void FuncNode::assign_initial_weight()
781 PredSetIter * it = predicate_leaves.iterator();
782 SnapVector<Predicate *> leaves;
783 while (it->hasNext()) {
784 Predicate * pred = it->next();
785 double weight = 100.0 / sqrt(pred->get_expl_count() + 1);
786 pred->set_weight(weight);
787 leaves.push_back(pred);
790 quickSort(&leaves, 0, leaves.size() - 1);
792 // assign scores for internal nodes;
793 while ( !leaves.empty() ) {
794 Predicate * leaf = leaves.back();
797 Predicate * curr = leaf->get_parent();
798 while (curr != NULL) {
799 if (curr->get_weight() != 0) {
804 ModelVector<Predicate *> * children = curr->get_children();
805 double weight_sum = 0;
806 bool has_unassigned_node = false;
808 for (uint i = 0; i < children->size(); i++) {
809 Predicate * child = (*children)[i];
811 // If a child has unassigned weight
812 double weight = child->get_weight();
814 has_unassigned_node = true;
817 weight_sum += weight;
820 if (!has_unassigned_node) {
821 double average_weight = (double) weight_sum / (double) children->size();
822 double weight = average_weight * pow(0.9, curr->get_depth());
823 curr->set_weight(weight);
827 curr = curr->get_parent();
832 void FuncNode::update_predicate_tree_weight()
835 // Predicate tree is initially built
836 assign_initial_weight();
842 void FuncNode::print_predicate_tree()
844 model_print("digraph function_%s {\n", func_name);
845 predicate_tree_entry->print_pred_subtree();
846 predicate_tree_exit->print_predicate();
847 model_print("}\n"); // end of graph