6 #include "concretepredicate.h"
11 FuncNode::FuncNode(ModelHistory * history) :
18 predicate_tree_position(),
22 predicate_tree_entry = new Predicate(NULL, true);
23 predicate_tree_entry->add_predicate_expr(NOPREDICATE, NULL, true);
25 predicate_tree_exit = new Predicate(NULL, false, true);
26 predicate_tree_exit->alloc_pre_exit_predicates();
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 /* entry predicates and predicates containing pure write actions
476 * have no predicate expressions */
477 if ( (*curr_pred)->is_entry_predicate() )
478 new_pred->add_predicate_expr(NOPREDICATE, NULL, true);
479 else if (next_inst->is_write()) {
480 /* next_inst->is_write() <==> pure writes */
481 new_pred->add_predicate_expr(NOPREDICATE, NULL, true);
487 SnapVector<Predicate *> predicates;
489 struct half_pred_expr * half_expr = (*half_pred_expressions)[0];
490 predicates.push_back(new Predicate(next_inst));
491 predicates.push_back(new Predicate(next_inst));
493 predicates[0]->add_predicate_expr(half_expr->token, half_expr->func_inst, true);
494 predicates[1]->add_predicate_expr(half_expr->token, half_expr->func_inst, false);
496 for (uint i = 1;i < half_pred_expressions->size();i++) {
497 half_expr = (*half_pred_expressions)[i];
499 uint old_size = predicates.size();
500 for (uint j = 0;j < old_size;j++) {
501 Predicate * pred = predicates[j];
502 Predicate * new_pred = new Predicate(next_inst);
503 new_pred->copy_predicate_expr(pred);
505 pred->add_predicate_expr(half_expr->token, half_expr->func_inst, true);
506 new_pred->add_predicate_expr(half_expr->token, half_expr->func_inst, false);
508 predicates.push_back(new_pred);
512 for (uint i = 0;i < predicates.size();i++) {
513 Predicate * pred= predicates[i];
514 (*curr_pred)->add_child(pred);
515 pred->set_parent(*curr_pred);
518 /* Free memories allocated by infer_predicate */
519 for (uint i = 0;i < half_pred_expressions->size();i++) {
520 struct half_pred_expr * tmp = (*half_pred_expressions)[i];
525 /* Amend predicates that contain no predicate expressions. Currenlty only amend with NULLITY predicates */
526 bool FuncNode::amend_predicate_expr(Predicate ** curr_pred, FuncInst * next_inst, ModelAction * next_act)
528 // there should only be only child
529 Predicate * unset_pred = (*curr_pred)->get_children()->back();
530 uint64_t read_val = next_act->get_reads_from_value();
532 // only generate NULLITY predicate when it is actually NULL.
533 if ( !next_inst->is_single_location() && (void*)read_val == NULL ) {
534 Predicate * new_pred = new Predicate(next_inst);
536 (*curr_pred)->add_child(new_pred);
537 new_pred->set_parent(*curr_pred);
539 unset_pred->add_predicate_expr(NULLITY, NULL, false);
540 new_pred->add_predicate_expr(NULLITY, NULL, true);
548 void FuncNode::add_to_val_loc_map(uint64_t val, void * loc)
550 loc_set_t * locations = val_loc_map->get(val);
552 if (locations == NULL) {
553 locations = new loc_set_t();
554 val_loc_map->put(val, locations);
557 update_loc_may_equal_map(loc, locations);
559 // values_may_read_from->add(val);
562 void FuncNode::add_to_val_loc_map(value_set_t * values, void * loc)
567 value_set_iter * it = values->iterator();
568 while (it->hasNext()) {
569 uint64_t val = it->next();
570 add_to_val_loc_map(val, loc);
574 void FuncNode::update_loc_may_equal_map(void * new_loc, loc_set_t * old_locations)
576 if ( old_locations->contains(new_loc) )
579 loc_set_t * neighbors = loc_may_equal_map->get(new_loc);
581 if (neighbors == NULL) {
582 neighbors = new loc_set_t();
583 loc_may_equal_map->put(new_loc, neighbors);
586 loc_set_iter * loc_it = old_locations->iterator();
587 while (loc_it->hasNext()) {
588 // new_loc: { old_locations, ... }
589 void * member = loc_it->next();
590 neighbors->add(member);
592 // for each i in old_locations, i : { new_loc, ... }
593 loc_set_t * _neighbors = loc_may_equal_map->get(member);
594 if (_neighbors == NULL) {
595 _neighbors = new loc_set_t();
596 loc_may_equal_map->put(member, _neighbors);
598 _neighbors->add(new_loc);
602 /* Every time a thread enters a function, set its position to the predicate tree entry */
603 void FuncNode::init_predicate_tree_position(thread_id_t tid)
605 int thread_id = id_to_int(tid);
606 if (predicate_tree_position.size() <= (uint) thread_id)
607 predicate_tree_position.resize(thread_id + 1);
609 predicate_tree_position[thread_id] = predicate_tree_entry;
612 void FuncNode::set_predicate_tree_position(thread_id_t tid, Predicate * pred)
614 int thread_id = id_to_int(tid);
615 predicate_tree_position[thread_id] = pred;
618 /* @return The position of a thread in a predicate tree */
619 Predicate * FuncNode::get_predicate_tree_position(thread_id_t tid)
621 int thread_id = id_to_int(tid);
622 return predicate_tree_position[thread_id];
625 /* Make sure elements of thrd_inst_act_map are initialized properly when threads enter functions */
626 void FuncNode::init_inst_act_map(thread_id_t tid)
628 int thread_id = id_to_int(tid);
629 SnapVector<inst_act_map_t *> * thrd_inst_act_map = history->getThrdInstActMap(func_id);
630 uint old_size = thrd_inst_act_map->size();
632 if (thrd_inst_act_map->size() <= (uint) thread_id) {
633 uint new_size = thread_id + 1;
634 thrd_inst_act_map->resize(new_size);
636 for (uint i = old_size;i < new_size;i++)
637 (*thrd_inst_act_map)[i] = new inst_act_map_t(128);
641 /* Reset elements of thrd_inst_act_map when threads exit functions */
642 void FuncNode::reset_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);
647 inst_act_map_t * map = (*thrd_inst_act_map)[thread_id];
651 void FuncNode::update_inst_act_map(thread_id_t tid, ModelAction * read_act)
653 int thread_id = id_to_int(tid);
654 SnapVector<inst_act_map_t *> * thrd_inst_act_map = history->getThrdInstActMap(func_id);
656 inst_act_map_t * map = (*thrd_inst_act_map)[thread_id];
657 FuncInst * read_inst = get_inst(read_act);
658 map->put(read_inst, read_act);
661 inst_act_map_t * FuncNode::get_inst_act_map(thread_id_t tid)
663 int thread_id = id_to_int(tid);
664 SnapVector<inst_act_map_t *> * thrd_inst_act_map = history->getThrdInstActMap(func_id);
666 return (*thrd_inst_act_map)[thread_id];
669 /* Add FuncNodes that this node may follow */
670 void FuncNode::add_out_edge(FuncNode * other)
672 if ( !edge_table.contains(other) ) {
673 edge_table.put(other, OUT_EDGE);
674 out_edges.push_back(other);
678 edge_type_t edge = edge_table.get(other);
679 if (edge == IN_EDGE) {
680 edge_table.put(other, BI_EDGE);
681 out_edges.push_back(other);
685 /* Compute the distance between this FuncNode and the target node.
686 * Return -1 if the target node is unreachable or the actual distance
687 * is greater than max_step.
689 int FuncNode::compute_distance(FuncNode * target, int max_step)
693 else if (target == this)
696 SnapList<FuncNode *> queue;
697 HashTable<FuncNode *, int, uintptr_t, 0> distances(128);
699 queue.push_back(this);
700 distances.put(this, 0);
702 while (!queue.empty()) {
703 FuncNode * curr = queue.front();
705 int dist = distances.get(curr);
707 if (max_step <= dist)
710 ModelList<FuncNode *> * outEdges = curr->get_out_edges();
711 mllnode<FuncNode *> * it;
712 for (it = outEdges->begin();it != NULL;it = it->getNext()) {
713 FuncNode * out_node = it->getVal();
715 /* This node has not been visited before */
716 if ( !distances.contains(out_node) ) {
717 if (out_node == target)
720 queue.push_back(out_node);
721 distances.put(out_node, dist + 1);
726 /* Target node is unreachable */
730 void FuncNode::assign_base_score()
732 SnapVector<Predicate *> leaves;
733 SnapList<Predicate *> queue;
734 queue.push_front(predicate_tree_entry);
737 while ( !queue.empty() ) {
738 Predicate * node = queue.back();
741 ModelVector<Predicate *> * children = node->get_children();
742 if (children->empty()) {
744 leaves.push_back(node);
747 for (uint i = 0; i < children->size(); i++)
748 queue.push_front( (*children)[i] );
751 // assign scores for internal nodes;
752 while ( !leaves.empty() ) {
753 Predicate * leaf = leaves.back();
756 Predicate * curr = leaf->get_parent();
757 while (curr != NULL) {
758 if (curr->get_weight() != 0) {
763 ModelVector<Predicate *> * children = curr->get_children();
764 double weight_sum = 0;
765 bool has_unassigned_node = false;
767 for (uint i = 0; i < children->size(); i++) {
768 Predicate * child = (*children)[i];
770 // If a child has unassigned weight
771 double weight = child->get_weight();
773 has_unassigned_node = true;
776 weight_sum += weight;
779 if (!has_unassigned_node) {
780 double average_weight = (double) weight_sum / (double) children->size();
781 double weight = average_weight * pow(0.9, curr->get_depth());
782 curr->set_weight(weight);
786 curr = curr->get_parent();
791 void FuncNode::print_predicate_tree()
793 model_print("digraph function_%s {\n", func_name);
794 predicate_tree_entry->print_pred_subtree();
795 predicate_tree_exit->print_predicate();
796 model_print("}\n"); // end of graph