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);
25 /* Snapshot data structures below */
26 action_list_buffer = new SnapList<action_list_t *>();
27 read_locations = new loc_set_t();
28 write_locations = new loc_set_t();
29 val_loc_map = new HashTable<uint64_t, loc_set_t *, uint64_t, 0, snapshot_malloc, snapshot_calloc, snapshot_free, int64_hash>();
30 loc_may_equal_map = new HashTable<void *, loc_set_t *, uintptr_t, 0>();
32 //values_may_read_from = new value_set_t();
35 /* Reallocate snapshotted memories when new executions start */
36 void FuncNode::set_new_exec_flag()
38 action_list_buffer = new SnapList<action_list_t *>();
39 read_locations = new loc_set_t();
40 write_locations = new loc_set_t();
41 val_loc_map = new HashTable<uint64_t, loc_set_t *, uint64_t, 0, snapshot_malloc, snapshot_calloc, snapshot_free, int64_hash>();
42 loc_may_equal_map = new HashTable<void *, loc_set_t *, uintptr_t, 0>();
44 //values_may_read_from = new value_set_t();
47 /* Check whether FuncInst with the same type, position, and location
48 * as act has been added to func_inst_map or not. If not, add it.
50 void FuncNode::add_inst(ModelAction *act)
53 const char * position = act->get_position();
55 /* THREAD* actions, ATOMIC_LOCK, ATOMIC_TRYLOCK, and ATOMIC_UNLOCK
56 * actions are not tagged with their source line numbers
61 FuncInst * func_inst = func_inst_map.get(position);
63 /* This position has not been inserted into hashtable before */
64 if (func_inst == NULL) {
65 func_inst = create_new_inst(act);
66 func_inst_map.put(position, func_inst);
70 /* Volatile variables that use ++ or -- syntax may result in read and write actions with the same position */
71 if (func_inst->get_type() != act->get_type()) {
72 FuncInst * collision_inst = func_inst->search_in_collision(act);
74 if (collision_inst == NULL) {
75 collision_inst = create_new_inst(act);
76 func_inst->add_to_collision(collision_inst);
79 func_inst = collision_inst;
83 ASSERT(func_inst->get_type() == act->get_type());
84 int curr_execution_number = model->get_execution_number();
86 /* Reset locations when new executions start */
87 if (func_inst->get_execution_number() != curr_execution_number) {
88 func_inst->set_location(act->get_location());
89 func_inst->set_execution_number(curr_execution_number);
92 /* Mark the memory location of such inst as not unique */
93 if (func_inst->get_location() != act->get_location())
94 func_inst->not_single_location();
97 FuncInst * FuncNode::create_new_inst(ModelAction * act)
99 FuncInst * func_inst = new FuncInst(act, this);
100 int exec_num = model->get_execution_number();
101 func_inst->set_execution_number(exec_num);
103 inst_list.push_back(func_inst);
109 /* Get the FuncInst with the same type, position, and location
112 * @return FuncInst with the same type, position, and location as act */
113 FuncInst * FuncNode::get_inst(ModelAction *act)
116 const char * position = act->get_position();
118 /* THREAD* actions, ATOMIC_LOCK, ATOMIC_TRYLOCK, and ATOMIC_UNLOCK
119 * actions are not tagged with their source line numbers
121 if (position == NULL)
124 FuncInst * inst = func_inst_map.get(position);
128 action_type inst_type = inst->get_type();
129 action_type act_type = act->get_type();
131 if (inst_type == act_type) {
134 /* RMWRCAS actions are converted to RMW or READ actions */
135 else if (inst_type == ATOMIC_RMWRCAS &&
136 (act_type == ATOMIC_RMW || act_type == ATOMIC_READ)) {
139 /* Return the FuncInst in the collision list */
141 return inst->search_in_collision(act);
146 void FuncNode::add_entry_inst(FuncInst * inst)
151 mllnode<FuncInst *> * it;
152 for (it = entry_insts.begin();it != NULL;it = it->getNext()) {
153 if (inst == it->getVal())
157 entry_insts.push_back(inst);
161 * @brief Convert ModelAdtion list to FuncInst list
162 * @param act_list A list of ModelActions
164 void FuncNode::update_tree(action_list_t * act_list)
166 if (act_list == NULL || act_list->size() == 0)
169 HashTable<void *, value_set_t *, uintptr_t, 0> * write_history = history->getWriteHistory();
171 /* build inst_list from act_list for later processing */
172 func_inst_list_t inst_list;
173 action_list_t rw_act_list;
175 for (sllnode<ModelAction *> * it = act_list->begin();it != NULL;it = it->getNext()) {
176 ModelAction * act = it->getVal();
177 FuncInst * func_inst = get_inst(act);
178 void * loc = act->get_location();
180 if (func_inst == NULL)
183 inst_list.push_back(func_inst);
184 bool act_added = false;
186 if (act->is_write()) {
187 rw_act_list.push_back(act);
189 if (!write_locations->contains(loc)) {
190 write_locations->add(loc);
191 history->update_loc_wr_func_nodes_map(loc, this);
195 if (act->is_read()) {
197 rw_act_list.push_back(act);
199 /* If func_inst may only read_from a single location, then:
201 * The first time an action reads from some location,
202 * import all the values that have been written to this
203 * location from ModelHistory and notify ModelHistory
204 * that this FuncNode may read from this location.
206 if (!read_locations->contains(loc) && func_inst->is_single_location()) {
207 read_locations->add(loc);
208 value_set_t * write_values = write_history->get(loc);
209 add_to_val_loc_map(write_values, loc);
210 history->update_loc_rd_func_nodes_map(loc, this);
215 // model_print("function %s\n", func_name);
216 // print_val_loc_map();
218 update_inst_tree(&inst_list);
219 update_predicate_tree(&rw_act_list);
221 // print_predicate_tree();
225 * @brief Link FuncInsts in inst_list - add one FuncInst to another's predecessors and successors
226 * @param inst_list A list of FuncInsts
228 void FuncNode::update_inst_tree(func_inst_list_t * inst_list)
230 if (inst_list == NULL)
232 else if (inst_list->size() == 0)
236 sllnode<FuncInst *>* it = inst_list->begin();
237 sllnode<FuncInst *>* prev;
239 /* add the first instruction to the list of entry insts */
240 FuncInst * entry_inst = it->getVal();
241 add_entry_inst(entry_inst);
245 prev = it->getPrev();
247 FuncInst * prev_inst = prev->getVal();
248 FuncInst * curr_inst = it->getVal();
250 prev_inst->add_succ(curr_inst);
251 curr_inst->add_pred(prev_inst);
257 void FuncNode::update_predicate_tree(action_list_t * act_list)
259 if (act_list == NULL || act_list->size() == 0)
264 /* Map a FuncInst to the its predicate */
265 HashTable<FuncInst *, Predicate *, uintptr_t, 0> inst_pred_map(128);
267 // Number FuncInsts to detect loops
268 HashTable<FuncInst *, uint32_t, uintptr_t, 0> inst_id_map(128);
269 uint32_t inst_counter = 0;
271 /* Only need to store the locations of read actions */
272 HashTable<void *, ModelAction *, uintptr_t, 0> loc_act_map(128);
274 sllnode<ModelAction *> *it = act_list->begin();
275 Predicate * curr_pred = predicate_tree_entry;
277 ModelAction * next_act = it->getVal();
278 FuncInst * next_inst = get_inst(next_act);
279 next_inst->set_associated_act(next_act, marker);
281 SnapVector<Predicate *> unset_predicates = SnapVector<Predicate *>();
282 bool branch_found = follow_branch(&curr_pred, next_inst, next_act, &unset_predicates);
284 // A branch with unset predicate expression is detected
285 if (!branch_found && unset_predicates.size() != 0) {
286 ASSERT(unset_predicates.size() == 1);
287 Predicate * one_branch = unset_predicates[0];
289 bool amended = amend_predicate_expr(&curr_pred, next_inst, next_act);
293 curr_pred = one_branch;
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, &loc_act_map, &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 loc_act_map.put(next_act->get_location(), next_act);
329 inst_pred_map.put(next_inst, curr_pred);
330 if (!inst_id_map.contains(next_inst))
331 inst_id_map.put(next_inst, inst_counter++);
334 curr_pred->incr_expl_count();
337 curr_pred->set_exit(predicate_tree_exit);
340 /* Given curr_pred and next_inst, find the branch following curr_pred that
341 * contains next_inst and the correct predicate.
342 * @return true if branch found, false otherwise.
344 bool FuncNode::follow_branch(Predicate ** curr_pred, FuncInst * next_inst,
345 ModelAction * next_act, SnapVector<Predicate *> * unset_predicates)
347 /* Check if a branch with func_inst and corresponding predicate exists */
348 bool branch_found = false;
349 ModelVector<Predicate *> * branches = (*curr_pred)->get_children();
350 for (uint i = 0;i < branches->size();i++) {
351 Predicate * branch = (*branches)[i];
352 if (branch->get_func_inst() != next_inst)
355 /* Check against predicate expressions */
356 bool predicate_correct = true;
357 PredExprSet * pred_expressions = branch->get_pred_expressions();
358 PredExprSetIter * pred_expr_it = pred_expressions->iterator();
360 /* Only read and rmw actions my have unset predicate expressions */
361 if (pred_expressions->getSize() == 0) {
362 predicate_correct = false;
363 unset_predicates->push_back(branch);
366 while (pred_expr_it->hasNext()) {
367 pred_expr * pred_expression = pred_expr_it->next();
368 uint64_t last_read, next_read;
371 switch(pred_expression->token) {
373 predicate_correct = true;
376 FuncInst * to_be_compared;
377 ModelAction * last_act;
379 to_be_compared = pred_expression->func_inst;
380 last_act = to_be_compared->get_associated_act(marker);
382 last_read = last_act->get_reads_from_value();
383 next_read = next_act->get_reads_from_value();
384 equality = (last_read == next_read);
385 if (equality != pred_expression->value)
386 predicate_correct = false;
390 next_read = next_act->get_reads_from_value();
391 // TODO: implement likely to be null
392 equality = ( (void*) (next_read & 0xffffffff) == NULL);
393 if (equality != pred_expression->value)
394 predicate_correct = false;
397 predicate_correct = false;
398 model_print("unkown predicate token\n");
403 if (predicate_correct) {
413 /* Infer predicate expressions, which are generated in FuncNode::generate_predicates */
414 void FuncNode::infer_predicates(FuncInst * next_inst, ModelAction * next_act,
415 HashTable<void *, ModelAction *, uintptr_t, 0> * loc_act_map,
416 SnapVector<struct half_pred_expr *> * half_pred_expressions)
418 void * loc = next_act->get_location();
420 if (next_inst->is_read()) {
422 if ( loc_act_map->contains(loc) ) {
423 ModelAction * last_act = loc_act_map->get(loc);
424 FuncInst * last_inst = get_inst(last_act);
425 struct half_pred_expr * expression = new half_pred_expr(EQUALITY, last_inst);
426 half_pred_expressions->push_back(expression);
427 } else if ( next_inst->is_single_location() ) {
428 loc_set_t * loc_may_equal = loc_may_equal_map->get(loc);
430 if (loc_may_equal != NULL) {
431 loc_set_iter * loc_it = loc_may_equal->iterator();
432 while (loc_it->hasNext()) {
433 void * neighbor = loc_it->next();
434 if (loc_act_map->contains(neighbor)) {
435 ModelAction * last_act = loc_act_map->get(neighbor);
436 FuncInst * last_inst = get_inst(last_act);
438 struct half_pred_expr * expression = new half_pred_expr(EQUALITY, last_inst);
439 half_pred_expressions->push_back(expression);
444 // next_inst is not single location
445 uint64_t read_val = next_act->get_reads_from_value();
447 // only infer NULLITY predicate when it is actually NULL.
448 if ( (void*)read_val == NULL) {
449 struct half_pred_expr * expression = new half_pred_expr(NULLITY, NULL);
450 half_pred_expressions->push_back(expression);
455 // TODO: do anything here?
459 /* Able to generate complex predicates when there are multiple predciate expressions */
460 void FuncNode::generate_predicates(Predicate ** curr_pred, FuncInst * next_inst,
461 SnapVector<struct half_pred_expr *> * half_pred_expressions)
463 if (half_pred_expressions->size() == 0) {
464 Predicate * new_pred = new Predicate(next_inst);
465 (*curr_pred)->add_child(new_pred);
466 new_pred->set_parent(*curr_pred);
468 /* entry predicates and predicates containing pure write actions
469 * have no predicate expressions */
470 if ( (*curr_pred)->is_entry_predicate() )
471 new_pred->add_predicate_expr(NOPREDICATE, NULL, true);
472 else if (next_inst->is_write()) {
473 /* next_inst->is_write() <==> pure writes */
474 new_pred->add_predicate_expr(NOPREDICATE, NULL, true);
480 SnapVector<Predicate *> predicates;
482 struct half_pred_expr * half_expr = (*half_pred_expressions)[0];
483 predicates.push_back(new Predicate(next_inst));
484 predicates.push_back(new Predicate(next_inst));
486 predicates[0]->add_predicate_expr(half_expr->token, half_expr->func_inst, true);
487 predicates[1]->add_predicate_expr(half_expr->token, half_expr->func_inst, false);
489 for (uint i = 1;i < half_pred_expressions->size();i++) {
490 half_expr = (*half_pred_expressions)[i];
492 uint old_size = predicates.size();
493 for (uint j = 0;j < old_size;j++) {
494 Predicate * pred = predicates[j];
495 Predicate * new_pred = new Predicate(next_inst);
496 new_pred->copy_predicate_expr(pred);
498 pred->add_predicate_expr(half_expr->token, half_expr->func_inst, true);
499 new_pred->add_predicate_expr(half_expr->token, half_expr->func_inst, false);
501 predicates.push_back(new_pred);
505 for (uint i = 0;i < predicates.size();i++) {
506 Predicate * pred= predicates[i];
507 (*curr_pred)->add_child(pred);
508 pred->set_parent(*curr_pred);
511 /* Free memories allocated by infer_predicate */
512 for (uint i = 0;i < half_pred_expressions->size();i++) {
513 struct half_pred_expr * tmp = (*half_pred_expressions)[i];
518 /* Amend predicates that contain no predicate expressions. Currenlty only amend with NULLITY predicates */
519 bool FuncNode::amend_predicate_expr(Predicate ** curr_pred, FuncInst * next_inst, ModelAction * next_act)
521 // there should only be only child
522 Predicate * unset_pred = (*curr_pred)->get_children()->back();
523 uint64_t read_val = next_act->get_reads_from_value();
525 // only generate NULLITY predicate when it is actually NULL.
526 if ( !next_inst->is_single_location() && (void*)read_val == NULL ) {
527 Predicate * new_pred = new Predicate(next_inst);
529 (*curr_pred)->add_child(new_pred);
530 new_pred->set_parent(*curr_pred);
532 unset_pred->add_predicate_expr(NULLITY, NULL, false);
533 new_pred->add_predicate_expr(NULLITY, NULL, true);
541 void FuncNode::add_to_val_loc_map(uint64_t val, void * loc)
543 loc_set_t * locations = val_loc_map->get(val);
545 if (locations == NULL) {
546 locations = new loc_set_t();
547 val_loc_map->put(val, locations);
550 update_loc_may_equal_map(loc, locations);
552 // values_may_read_from->add(val);
555 void FuncNode::add_to_val_loc_map(value_set_t * values, void * loc)
560 value_set_iter * it = values->iterator();
561 while (it->hasNext()) {
562 uint64_t val = it->next();
563 add_to_val_loc_map(val, loc);
567 void FuncNode::update_loc_may_equal_map(void * new_loc, loc_set_t * old_locations)
569 if ( old_locations->contains(new_loc) )
572 loc_set_t * neighbors = loc_may_equal_map->get(new_loc);
574 if (neighbors == NULL) {
575 neighbors = new loc_set_t();
576 loc_may_equal_map->put(new_loc, neighbors);
579 loc_set_iter * loc_it = old_locations->iterator();
580 while (loc_it->hasNext()) {
581 // new_loc: { old_locations, ... }
582 void * member = loc_it->next();
583 neighbors->add(member);
585 // for each i in old_locations, i : { new_loc, ... }
586 loc_set_t * _neighbors = loc_may_equal_map->get(member);
587 if (_neighbors == NULL) {
588 _neighbors = new loc_set_t();
589 loc_may_equal_map->put(member, _neighbors);
591 _neighbors->add(new_loc);
595 /* Every time a thread enters a function, set its position to the predicate tree entry */
596 void FuncNode::init_predicate_tree_position(thread_id_t tid)
598 int thread_id = id_to_int(tid);
599 if (predicate_tree_position.size() <= (uint) thread_id)
600 predicate_tree_position.resize(thread_id + 1);
602 predicate_tree_position[thread_id] = predicate_tree_entry;
605 void FuncNode::set_predicate_tree_position(thread_id_t tid, Predicate * pred)
607 int thread_id = id_to_int(tid);
608 predicate_tree_position[thread_id] = pred;
611 /* @return The position of a thread in a predicate tree */
612 Predicate * FuncNode::get_predicate_tree_position(thread_id_t tid)
614 int thread_id = id_to_int(tid);
615 return predicate_tree_position[thread_id];
618 /* Make sure elements of thrd_inst_act_map are initialized properly when threads enter functions */
619 void FuncNode::init_inst_act_map(thread_id_t tid)
621 int thread_id = id_to_int(tid);
622 SnapVector<inst_act_map_t *> * thrd_inst_act_map = history->getThrdInstActMap(func_id);
623 uint old_size = thrd_inst_act_map->size();
625 if (thrd_inst_act_map->size() <= (uint) thread_id) {
626 uint new_size = thread_id + 1;
627 thrd_inst_act_map->resize(new_size);
629 for (uint i = old_size;i < new_size;i++)
630 (*thrd_inst_act_map)[i] = new inst_act_map_t(128);
634 /* Reset elements of thrd_inst_act_map when threads exit functions */
635 void FuncNode::reset_inst_act_map(thread_id_t tid)
637 int thread_id = id_to_int(tid);
638 SnapVector<inst_act_map_t *> * thrd_inst_act_map = history->getThrdInstActMap(func_id);
640 inst_act_map_t * map = (*thrd_inst_act_map)[thread_id];
644 void FuncNode::update_inst_act_map(thread_id_t tid, ModelAction * read_act)
646 int thread_id = id_to_int(tid);
647 SnapVector<inst_act_map_t *> * thrd_inst_act_map = history->getThrdInstActMap(func_id);
649 inst_act_map_t * map = (*thrd_inst_act_map)[thread_id];
650 FuncInst * read_inst = get_inst(read_act);
651 map->put(read_inst, read_act);
654 inst_act_map_t * FuncNode::get_inst_act_map(thread_id_t tid)
656 int thread_id = id_to_int(tid);
657 SnapVector<inst_act_map_t *> * thrd_inst_act_map = history->getThrdInstActMap(func_id);
659 return (*thrd_inst_act_map)[thread_id];
662 /* Add FuncNodes that this node may follow */
663 void FuncNode::add_out_edge(FuncNode * other)
665 if ( !edge_table.contains(other) ) {
666 edge_table.put(other, OUT_EDGE);
667 out_edges.push_back(other);
671 edge_type_t edge = edge_table.get(other);
672 if (edge == IN_EDGE) {
673 edge_table.put(other, BI_EDGE);
674 out_edges.push_back(other);
678 /* Compute the distance between this FuncNode and the target node.
679 * Return -1 if the target node is unreachable or the actual distance
680 * is greater than max_step.
682 int FuncNode::compute_distance(FuncNode * target, int max_step)
686 else if (target == this)
689 SnapList<FuncNode *> queue;
690 HashTable<FuncNode *, int, uintptr_t, 0> distances(128);
692 queue.push_back(this);
693 distances.put(this, 0);
695 while (!queue.empty()) {
696 FuncNode * curr = queue.front();
698 int dist = distances.get(curr);
700 if (max_step <= dist)
703 ModelList<FuncNode *> * outEdges = curr->get_out_edges();
704 mllnode<FuncNode *> * it;
705 for (it = outEdges->begin();it != NULL;it = it->getNext()) {
706 FuncNode * out_node = it->getVal();
708 /* This node has not been visited before */
709 if ( !distances.contains(out_node) ) {
710 if (out_node == target)
713 queue.push_back(out_node);
714 distances.put(out_node, dist + 1);
719 /* Target node is unreachable */
723 void FuncNode::print_predicate_tree()
725 model_print("digraph function_%s {\n", func_name);
726 predicate_tree_entry->print_pred_subtree();
727 predicate_tree_exit->print_predicate();
728 model_print("}\n"); // end of graph
731 void FuncNode::print_val_loc_map()
734 value_set_iter * val_it = values_may_read_from->iterator();
735 while (val_it->hasNext()) {
736 uint64_t value = val_it->next();
737 model_print("val %llx: ", value);
739 loc_set_t * locations = val_loc_map->get(value);
740 loc_set_iter * loc_it = locations->iterator();
741 while (loc_it->hasNext()) {
742 void * location = loc_it->next();
743 model_print("%p ", location);