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 equality = ((void*)next_read == NULL);
392 if (equality != pred_expression->value)
393 predicate_correct = false;
396 predicate_correct = false;
397 model_print("unkown predicate token\n");
402 if (predicate_correct) {
412 /* Infer predicate expressions, which are generated in FuncNode::generate_predicates */
413 void FuncNode::infer_predicates(FuncInst * next_inst, ModelAction * next_act,
414 HashTable<void *, ModelAction *, uintptr_t, 0> * loc_act_map,
415 SnapVector<struct half_pred_expr *> * half_pred_expressions)
417 void * loc = next_act->get_location();
419 if (next_inst->is_read()) {
421 if ( loc_act_map->contains(loc) ) {
422 ModelAction * last_act = loc_act_map->get(loc);
423 FuncInst * last_inst = get_inst(last_act);
424 struct half_pred_expr * expression = new half_pred_expr(EQUALITY, last_inst);
425 half_pred_expressions->push_back(expression);
426 } else if ( next_inst->is_single_location() ){
427 loc_set_t * loc_may_equal = loc_may_equal_map->get(loc);
429 if (loc_may_equal != NULL) {
430 loc_set_iter * loc_it = loc_may_equal->iterator();
431 while (loc_it->hasNext()) {
432 void * neighbor = loc_it->next();
433 if (loc_act_map->contains(neighbor)) {
434 ModelAction * last_act = loc_act_map->get(neighbor);
435 FuncInst * last_inst = get_inst(last_act);
437 struct half_pred_expr * expression = new half_pred_expr(EQUALITY, last_inst);
438 half_pred_expressions->push_back(expression);
443 // next_inst is not single location
444 uint64_t read_val = next_act->get_reads_from_value();
446 // only infer NULLITY predicate when it is actually NULL.
447 if ( (void*)read_val == NULL) {
448 struct half_pred_expr * expression = new half_pred_expr(NULLITY, NULL);
449 half_pred_expressions->push_back(expression);
454 // TODO: do anything here?
458 /* Able to generate complex predicates when there are multiple predciate expressions */
459 void FuncNode::generate_predicates(Predicate ** curr_pred, FuncInst * next_inst,
460 SnapVector<struct half_pred_expr *> * half_pred_expressions)
462 if (half_pred_expressions->size() == 0) {
463 Predicate * new_pred = new Predicate(next_inst);
464 (*curr_pred)->add_child(new_pred);
465 new_pred->set_parent(*curr_pred);
467 /* entry predicates and predicates containing pure write actions
468 * have no predicate expressions */
469 if ( (*curr_pred)->is_entry_predicate() )
470 new_pred->add_predicate_expr(NOPREDICATE, NULL, true);
471 else if (next_inst->is_write()) {
472 /* next_inst->is_write() <==> pure writes */
473 new_pred->add_predicate_expr(NOPREDICATE, NULL, true);
479 SnapVector<Predicate *> predicates;
481 struct half_pred_expr * half_expr = (*half_pred_expressions)[0];
482 predicates.push_back(new Predicate(next_inst));
483 predicates.push_back(new Predicate(next_inst));
485 predicates[0]->add_predicate_expr(half_expr->token, half_expr->func_inst, true);
486 predicates[1]->add_predicate_expr(half_expr->token, half_expr->func_inst, false);
488 for (uint i = 1; i < half_pred_expressions->size(); i++) {
489 half_expr = (*half_pred_expressions)[i];
491 uint old_size = predicates.size();
492 for (uint j = 0; j < old_size; j++) {
493 Predicate * pred = predicates[j];
494 Predicate * new_pred = new Predicate(next_inst);
495 new_pred->copy_predicate_expr(pred);
497 pred->add_predicate_expr(half_expr->token, half_expr->func_inst, true);
498 new_pred->add_predicate_expr(half_expr->token, half_expr->func_inst, false);
500 predicates.push_back(new_pred);
504 for (uint i = 0; i < predicates.size(); i++) {
505 Predicate * pred= predicates[i];
506 (*curr_pred)->add_child(pred);
507 pred->set_parent(*curr_pred);
510 /* Free memories allocated by infer_predicate */
511 for (uint i = 0; i < half_pred_expressions->size(); i++) {
512 struct half_pred_expr * tmp = (*half_pred_expressions)[i];
517 /* Amend predicates that contain no predicate expressions. Currenlty only amend with NULLITY predicates */
518 bool FuncNode::amend_predicate_expr(Predicate ** curr_pred, FuncInst * next_inst, ModelAction * next_act)
520 // there should only be only child
521 Predicate * unset_pred = (*curr_pred)->get_children()->back();
522 uint64_t read_val = next_act->get_reads_from_value();
524 // only generate NULLITY predicate when it is actually NULL.
525 if ( !next_inst->is_single_location() && (void*)read_val == NULL ) {
526 Predicate * new_pred = new Predicate(next_inst);
528 (*curr_pred)->add_child(new_pred);
529 new_pred->set_parent(*curr_pred);
531 unset_pred->add_predicate_expr(NULLITY, NULL, false);
532 new_pred->add_predicate_expr(NULLITY, NULL, true);
540 void FuncNode::add_to_val_loc_map(uint64_t val, void * loc)
542 loc_set_t * locations = val_loc_map->get(val);
544 if (locations == NULL) {
545 locations = new loc_set_t();
546 val_loc_map->put(val, locations);
549 update_loc_may_equal_map(loc, locations);
551 // values_may_read_from->add(val);
554 void FuncNode::add_to_val_loc_map(value_set_t * values, void * loc)
559 value_set_iter * it = values->iterator();
560 while (it->hasNext()) {
561 uint64_t val = it->next();
562 add_to_val_loc_map(val, loc);
566 void FuncNode::update_loc_may_equal_map(void * new_loc, loc_set_t * old_locations)
568 if ( old_locations->contains(new_loc) )
571 loc_set_t * neighbors = loc_may_equal_map->get(new_loc);
573 if (neighbors == NULL) {
574 neighbors = new loc_set_t();
575 loc_may_equal_map->put(new_loc, neighbors);
578 loc_set_iter * loc_it = old_locations->iterator();
579 while (loc_it->hasNext()) {
580 // new_loc: { old_locations, ... }
581 void * member = loc_it->next();
582 neighbors->add(member);
584 // for each i in old_locations, i : { new_loc, ... }
585 loc_set_t * _neighbors = loc_may_equal_map->get(member);
586 if (_neighbors == NULL) {
587 _neighbors = new loc_set_t();
588 loc_may_equal_map->put(member, _neighbors);
590 _neighbors->add(new_loc);
594 /* Every time a thread enters a function, set its position to the predicate tree entry */
595 void FuncNode::init_predicate_tree_position(thread_id_t tid)
597 int thread_id = id_to_int(tid);
598 if (predicate_tree_position.size() <= (uint) thread_id)
599 predicate_tree_position.resize(thread_id + 1);
601 predicate_tree_position[thread_id] = predicate_tree_entry;
604 void FuncNode::set_predicate_tree_position(thread_id_t tid, Predicate * pred)
606 int thread_id = id_to_int(tid);
607 predicate_tree_position[thread_id] = pred;
610 /* @return The position of a thread in a predicate tree */
611 Predicate * FuncNode::get_predicate_tree_position(thread_id_t tid)
613 int thread_id = id_to_int(tid);
614 return predicate_tree_position[thread_id];
617 /* Make sure elements of thrd_inst_act_map are initialized properly when threads enter functions */
618 void FuncNode::init_inst_act_map(thread_id_t tid)
620 int thread_id = id_to_int(tid);
621 SnapVector<inst_act_map_t *> * thrd_inst_act_map = history->getThrdInstActMap(func_id);
622 uint old_size = thrd_inst_act_map->size();
624 if (thrd_inst_act_map->size() <= (uint) thread_id) {
625 uint new_size = thread_id + 1;
626 thrd_inst_act_map->resize(new_size);
628 for (uint i = old_size; i < new_size; i++)
629 (*thrd_inst_act_map)[i] = new inst_act_map_t(128);
633 /* Reset elements of thrd_inst_act_map when threads exit functions */
634 void FuncNode::reset_inst_act_map(thread_id_t tid)
636 int thread_id = id_to_int(tid);
637 SnapVector<inst_act_map_t *> * thrd_inst_act_map = history->getThrdInstActMap(func_id);
639 inst_act_map_t * map = (*thrd_inst_act_map)[thread_id];
643 void FuncNode::update_inst_act_map(thread_id_t tid, ModelAction * read_act)
645 int thread_id = id_to_int(tid);
646 SnapVector<inst_act_map_t *> * thrd_inst_act_map = history->getThrdInstActMap(func_id);
648 inst_act_map_t * map = (*thrd_inst_act_map)[thread_id];
649 FuncInst * read_inst = get_inst(read_act);
650 map->put(read_inst, read_act);
653 inst_act_map_t * FuncNode::get_inst_act_map(thread_id_t tid)
655 int thread_id = id_to_int(tid);
656 SnapVector<inst_act_map_t *> * thrd_inst_act_map = history->getThrdInstActMap(func_id);
658 return (*thrd_inst_act_map)[thread_id];
661 /* Add FuncNodes that this node may follow */
662 void FuncNode::add_out_edge(FuncNode * other)
664 if ( !edge_table.contains(other) ) {
665 edge_table.put(other, OUT_EDGE);
666 out_edges.push_back(other);
670 edge_type_t edge = edge_table.get(other);
671 if (edge == IN_EDGE) {
672 edge_table.put(other, BI_EDGE);
673 out_edges.push_back(other);
677 /* Compute the distance between this FuncNode and the target node.
678 * Return -1 if the target node is unreachable or the actual distance
679 * is greater than max_step.
681 int FuncNode::compute_distance(FuncNode * target, int max_step)
685 else if (target == this)
688 SnapList<FuncNode *> queue;
689 HashTable<FuncNode *, int, uintptr_t, 0> distances(128);
691 queue.push_back(this);
692 distances.put(this, 0);
694 while (!queue.empty()) {
695 FuncNode * curr = queue.front();
697 int dist = distances.get(curr);
699 if (max_step <= dist)
702 ModelList<FuncNode *> * outEdges = curr->get_out_edges();
703 mllnode<FuncNode *> * it;
704 for (it = outEdges->begin(); it != NULL; it = it->getNext()) {
705 FuncNode * out_node = it->getVal();
707 /* This node has not been visited before */
708 if ( !distances.contains(out_node) ) {
709 if (out_node == target)
712 queue.push_back(out_node);
713 distances.put(out_node, dist + 1);
718 /* Target node is unreachable */
722 void FuncNode::print_predicate_tree()
724 model_print("digraph function_%s {\n", func_name);
725 predicate_tree_entry->print_pred_subtree();
726 predicate_tree_exit->print_predicate();
727 model_print("}\n"); // end of graph
730 void FuncNode::print_val_loc_map()
733 value_set_iter * val_it = values_may_read_from->iterator();
734 while (val_it->hasNext()) {
735 uint64_t value = val_it->next();
736 model_print("val %llx: ", value);
738 loc_set_t * locations = val_loc_map->get(value);
739 loc_set_iter * loc_it = locations->iterator();
740 while (loc_it->hasNext()) {
741 void * location = loc_it->next();
742 model_print("%p ", location);