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 // Memories that are reclaimed after each execution
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>();
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>();
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 * Note: currently, actions with the same position are filtered out by process_action,
51 * so the collision list of FuncInst is not used. May remove it later.
53 void FuncNode::add_inst(ModelAction *act)
56 const char * position = act->get_position();
58 /* THREAD* actions, ATOMIC_LOCK, ATOMIC_TRYLOCK, and ATOMIC_UNLOCK
59 * actions are not tagged with their source line numbers
64 if ( func_inst_map.contains(position) ) {
65 FuncInst * inst = func_inst_map.get(position);
67 ASSERT(inst->get_type() == act->get_type());
68 int curr_execution_number = model->get_execution_number();
70 /* Reset locations when new executions start */
71 if (inst->get_execution_number() != curr_execution_number) {
72 inst->set_location(act->get_location());
73 inst->set_execution_number(curr_execution_number);
76 if (inst->get_location() != act->get_location())
77 inst->not_single_location();
82 FuncInst * func_inst = new FuncInst(act, this);
84 func_inst_map.put(position, func_inst);
85 inst_list.push_back(func_inst);
88 /* Get the FuncInst with the same type, position, and location
91 * @return FuncInst with the same type, position, and location as act */
92 FuncInst * FuncNode::get_inst(ModelAction *act)
95 const char * position = act->get_position();
97 /* THREAD* actions, ATOMIC_LOCK, ATOMIC_TRYLOCK, and ATOMIC_UNLOCK
98 * actions are not tagged with their source line numbers
100 if (position == NULL)
103 FuncInst * inst = func_inst_map.get(position);
107 action_type inst_type = inst->get_type();
108 action_type act_type = act->get_type();
110 // else if branch: an RMWRCAS action is converted to a RMW or READ action
111 if (inst_type == act_type)
113 else if (inst_type == ATOMIC_RMWRCAS &&
114 (act_type == ATOMIC_RMW || act_type == ATOMIC_READ))
121 void FuncNode::add_entry_inst(FuncInst * inst)
126 mllnode<FuncInst *> * it;
127 for (it = entry_insts.begin(); it != NULL; it = it->getNext()) {
128 if (inst == it->getVal())
132 entry_insts.push_back(inst);
136 * @brief Convert ModelAdtion list to FuncInst list
137 * @param act_list A list of ModelActions
139 void FuncNode::update_tree(action_list_t * act_list)
141 if (act_list == NULL || act_list->size() == 0)
144 HashTable<void *, value_set_t *, uintptr_t, 0> * write_history = history->getWriteHistory();
146 /* build inst_list from act_list for later processing */
147 func_inst_list_t inst_list;
148 action_list_t rw_act_list;
150 for (sllnode<ModelAction *> * it = act_list->begin(); it != NULL; it = it->getNext()) {
151 ModelAction * act = it->getVal();
152 FuncInst * func_inst = get_inst(act);
153 void * loc = act->get_location();
155 if (func_inst == NULL)
158 inst_list.push_back(func_inst);
159 bool act_added = false;
161 if (act->is_write()) {
162 rw_act_list.push_back(act);
164 if (!write_locations->contains(loc)) {
165 write_locations->add(loc);
166 history->update_loc_wr_func_nodes_map(loc, this);
170 if (act->is_read()) {
172 rw_act_list.push_back(act);
174 /* If func_inst may only read_from a single location, then:
176 * The first time an action reads from some location,
177 * import all the values that have been written to this
178 * location from ModelHistory and notify ModelHistory
179 * that this FuncNode may read from this location.
181 if (!read_locations->contains(loc) && func_inst->is_single_location()) {
182 read_locations->add(loc);
183 value_set_t * write_values = write_history->get(loc);
184 add_to_val_loc_map(write_values, loc);
185 history->update_loc_rd_func_nodes_map(loc, this);
190 // model_print("function %s\n", func_name);
191 // print_val_loc_map();
193 update_inst_tree(&inst_list);
194 update_predicate_tree(&rw_act_list);
196 // print_predicate_tree();
200 * @brief Link FuncInsts in inst_list - add one FuncInst to another's predecessors and successors
201 * @param inst_list A list of FuncInsts
203 void FuncNode::update_inst_tree(func_inst_list_t * inst_list)
205 if (inst_list == NULL)
207 else if (inst_list->size() == 0)
211 sllnode<FuncInst *>* it = inst_list->begin();
212 sllnode<FuncInst *>* prev;
214 /* add the first instruction to the list of entry insts */
215 FuncInst * entry_inst = it->getVal();
216 add_entry_inst(entry_inst);
220 prev = it->getPrev();
222 FuncInst * prev_inst = prev->getVal();
223 FuncInst * curr_inst = it->getVal();
225 prev_inst->add_succ(curr_inst);
226 curr_inst->add_pred(prev_inst);
232 void FuncNode::update_predicate_tree(action_list_t * act_list)
234 if (act_list == NULL || act_list->size() == 0)
239 /* Map a FuncInst to the its predicate */
240 HashTable<FuncInst *, Predicate *, uintptr_t, 0> inst_pred_map(128);
242 // Number FuncInsts to detect loops
243 HashTable<FuncInst *, uint32_t, uintptr_t, 0> inst_id_map(128);
244 uint32_t inst_counter = 0;
246 /* Only need to store the locations of read actions */
247 HashTable<void *, ModelAction *, uintptr_t, 0> loc_act_map(128);
249 sllnode<ModelAction *> *it = act_list->begin();
250 Predicate * curr_pred = predicate_tree_entry;
252 ModelAction * next_act = it->getVal();
253 FuncInst * next_inst = get_inst(next_act);
254 next_inst->set_associated_act(next_act, marker);
256 SnapVector<Predicate *> unset_predicates = SnapVector<Predicate *>();
257 bool branch_found = follow_branch(&curr_pred, next_inst, next_act, &unset_predicates);
259 // A branch with unset predicate expression is detected
260 if (!branch_found && unset_predicates.size() != 0) {
261 ASSERT(unset_predicates.size() == 1);
262 Predicate * one_branch = unset_predicates[0];
264 bool amended = amend_predicate_expr(&curr_pred, next_inst, next_act);
268 curr_pred = one_branch;
274 if (!branch_found && inst_id_map.contains(next_inst)) {
275 FuncInst * curr_inst = curr_pred->get_func_inst();
276 uint32_t curr_id = inst_id_map.get(curr_inst);
277 uint32_t next_id = inst_id_map.get(next_inst);
279 if (curr_id >= next_id) {
280 Predicate * old_pred = inst_pred_map.get(next_inst);
281 Predicate * back_pred = old_pred->get_parent();
283 curr_pred->add_backedge(back_pred);
284 curr_pred = back_pred;
289 // Generate new branches
291 SnapVector<struct half_pred_expr *> half_pred_expressions;
292 infer_predicates(next_inst, next_act, &loc_act_map, &half_pred_expressions);
293 generate_predicates(&curr_pred, next_inst, &half_pred_expressions);
297 if (next_act->is_write())
298 curr_pred->set_write(true);
300 if (next_act->is_read()) {
301 loc_act_map.put(next_act->get_location(), next_act);
304 inst_pred_map.put(next_inst, curr_pred);
305 if (!inst_id_map.contains(next_inst))
306 inst_id_map.put(next_inst, inst_counter++);
309 curr_pred->incr_expl_count();
312 curr_pred->set_exit(predicate_tree_exit);
315 /* Given curr_pred and next_inst, find the branch following curr_pred that
316 * contains next_inst and the correct predicate.
317 * @return true if branch found, false otherwise.
319 bool FuncNode::follow_branch(Predicate ** curr_pred, FuncInst * next_inst,
320 ModelAction * next_act, SnapVector<Predicate *> * unset_predicates)
322 /* Check if a branch with func_inst and corresponding predicate exists */
323 bool branch_found = false;
324 ModelVector<Predicate *> * branches = (*curr_pred)->get_children();
325 for (uint i = 0; i < branches->size(); i++) {
326 Predicate * branch = (*branches)[i];
327 if (branch->get_func_inst() != next_inst)
330 /* Check against predicate expressions */
331 bool predicate_correct = true;
332 PredExprSet * pred_expressions = branch->get_pred_expressions();
333 PredExprSetIter * pred_expr_it = pred_expressions->iterator();
335 /* Only read and rmw actions my have unset predicate expressions */
336 if (pred_expressions->getSize() == 0) {
337 predicate_correct = false;
338 unset_predicates->push_back(branch);
341 while (pred_expr_it->hasNext()) {
342 pred_expr * pred_expression = pred_expr_it->next();
343 uint64_t last_read, next_read;
346 switch(pred_expression->token) {
348 predicate_correct = true;
351 FuncInst * to_be_compared;
352 ModelAction * last_act;
354 to_be_compared = pred_expression->func_inst;
355 last_act = to_be_compared->get_associated_act(marker);
357 last_read = last_act->get_reads_from_value();
358 next_read = next_act->get_reads_from_value();
359 equality = (last_read == next_read);
360 if (equality != pred_expression->value)
361 predicate_correct = false;
365 next_read = next_act->get_reads_from_value();
366 equality = ((void*)next_read == NULL);
367 if (equality != pred_expression->value)
368 predicate_correct = false;
371 predicate_correct = false;
372 model_print("unkown predicate token\n");
377 if (predicate_correct) {
387 /* Infer predicate expressions, which are generated in FuncNode::generate_predicates */
388 void FuncNode::infer_predicates(FuncInst * next_inst, ModelAction * next_act,
389 HashTable<void *, ModelAction *, uintptr_t, 0> * loc_act_map,
390 SnapVector<struct half_pred_expr *> * half_pred_expressions)
392 void * loc = next_act->get_location();
394 if (next_inst->is_read()) {
396 if ( loc_act_map->contains(loc) ) {
397 ModelAction * last_act = loc_act_map->get(loc);
398 FuncInst * last_inst = get_inst(last_act);
399 struct half_pred_expr * expression = new half_pred_expr(EQUALITY, last_inst);
400 half_pred_expressions->push_back(expression);
401 } else if ( next_inst->is_single_location() ){
402 loc_set_t * loc_may_equal = loc_may_equal_map->get(loc);
404 if (loc_may_equal != NULL) {
405 loc_set_iter * loc_it = loc_may_equal->iterator();
406 while (loc_it->hasNext()) {
407 void * neighbor = loc_it->next();
408 if (loc_act_map->contains(neighbor)) {
409 ModelAction * last_act = loc_act_map->get(neighbor);
410 FuncInst * last_inst = get_inst(last_act);
412 struct half_pred_expr * expression = new half_pred_expr(EQUALITY, last_inst);
413 half_pred_expressions->push_back(expression);
418 // next_inst is not single location
419 uint64_t read_val = next_act->get_reads_from_value();
421 // only infer NULLITY predicate when it is actually NULL.
422 if ( (void*)read_val == NULL) {
423 struct half_pred_expr * expression = new half_pred_expr(NULLITY, NULL);
424 half_pred_expressions->push_back(expression);
429 // TODO: do anything here?
433 /* Able to generate complex predicates when there are multiple predciate expressions */
434 void FuncNode::generate_predicates(Predicate ** curr_pred, FuncInst * next_inst,
435 SnapVector<struct half_pred_expr *> * half_pred_expressions)
437 if (half_pred_expressions->size() == 0) {
438 Predicate * new_pred = new Predicate(next_inst);
439 (*curr_pred)->add_child(new_pred);
440 new_pred->set_parent(*curr_pred);
442 /* entry predicates and predicates containing pure write actions
443 * have no predicate expressions */
444 if ( (*curr_pred)->is_entry_predicate() )
445 new_pred->add_predicate_expr(NOPREDICATE, NULL, true);
446 else if (next_inst->is_write()) {
447 /* next_inst->is_write() <==> pure writes */
448 new_pred->add_predicate_expr(NOPREDICATE, NULL, true);
454 SnapVector<Predicate *> predicates;
456 struct half_pred_expr * half_expr = (*half_pred_expressions)[0];
457 predicates.push_back(new Predicate(next_inst));
458 predicates.push_back(new Predicate(next_inst));
460 predicates[0]->add_predicate_expr(half_expr->token, half_expr->func_inst, true);
461 predicates[1]->add_predicate_expr(half_expr->token, half_expr->func_inst, false);
463 for (uint i = 1; i < half_pred_expressions->size(); i++) {
464 half_expr = (*half_pred_expressions)[i];
466 uint old_size = predicates.size();
467 for (uint j = 0; j < old_size; j++) {
468 Predicate * pred = predicates[j];
469 Predicate * new_pred = new Predicate(next_inst);
470 new_pred->copy_predicate_expr(pred);
472 pred->add_predicate_expr(half_expr->token, half_expr->func_inst, true);
473 new_pred->add_predicate_expr(half_expr->token, half_expr->func_inst, false);
475 predicates.push_back(new_pred);
479 for (uint i = 0; i < predicates.size(); i++) {
480 Predicate * pred= predicates[i];
481 (*curr_pred)->add_child(pred);
482 pred->set_parent(*curr_pred);
485 /* Free memories allocated by infer_predicate */
486 for (uint i = 0; i < half_pred_expressions->size(); i++) {
487 struct half_pred_expr * tmp = (*half_pred_expressions)[i];
492 /* Amend predicates that contain no predicate expressions. Currenlty only amend with NULLITY predicates */
493 bool FuncNode::amend_predicate_expr(Predicate ** curr_pred, FuncInst * next_inst, ModelAction * next_act)
495 // there should only be only child
496 Predicate * unset_pred = (*curr_pred)->get_children()->back();
497 uint64_t read_val = next_act->get_reads_from_value();
499 // only generate NULLITY predicate when it is actually NULL.
500 if ( !next_inst->is_single_location() && (void*)read_val == NULL ) {
501 Predicate * new_pred = new Predicate(next_inst);
503 (*curr_pred)->add_child(new_pred);
504 new_pred->set_parent(*curr_pred);
506 unset_pred->add_predicate_expr(NULLITY, NULL, false);
507 new_pred->add_predicate_expr(NULLITY, NULL, true);
515 void FuncNode::add_to_val_loc_map(uint64_t val, void * loc)
517 loc_set_t * locations = val_loc_map->get(val);
519 if (locations == NULL) {
520 locations = new loc_set_t();
521 val_loc_map->put(val, locations);
524 update_loc_may_equal_map(loc, locations);
526 // values_may_read_from->add(val);
529 void FuncNode::add_to_val_loc_map(value_set_t * values, void * loc)
534 value_set_iter * it = values->iterator();
535 while (it->hasNext()) {
536 uint64_t val = it->next();
537 add_to_val_loc_map(val, loc);
541 void FuncNode::update_loc_may_equal_map(void * new_loc, loc_set_t * old_locations)
543 if ( old_locations->contains(new_loc) )
546 loc_set_t * neighbors = loc_may_equal_map->get(new_loc);
548 if (neighbors == NULL) {
549 neighbors = new loc_set_t();
550 loc_may_equal_map->put(new_loc, neighbors);
553 loc_set_iter * loc_it = old_locations->iterator();
554 while (loc_it->hasNext()) {
555 // new_loc: { old_locations, ... }
556 void * member = loc_it->next();
557 neighbors->add(member);
559 // for each i in old_locations, i : { new_loc, ... }
560 loc_set_t * _neighbors = loc_may_equal_map->get(member);
561 if (_neighbors == NULL) {
562 _neighbors = new loc_set_t();
563 loc_may_equal_map->put(member, _neighbors);
565 _neighbors->add(new_loc);
569 /* Every time a thread enters a function, set its position to the predicate tree entry */
570 void FuncNode::init_predicate_tree_position(thread_id_t tid)
572 int thread_id = id_to_int(tid);
573 if (predicate_tree_position.size() <= (uint) thread_id)
574 predicate_tree_position.resize(thread_id + 1);
576 predicate_tree_position[thread_id] = predicate_tree_entry;
579 void FuncNode::set_predicate_tree_position(thread_id_t tid, Predicate * pred)
581 int thread_id = id_to_int(tid);
582 predicate_tree_position[thread_id] = pred;
585 /* @return The position of a thread in a predicate tree */
586 Predicate * FuncNode::get_predicate_tree_position(thread_id_t tid)
588 int thread_id = id_to_int(tid);
589 return predicate_tree_position[thread_id];
592 /* Make sure elements of thrd_inst_act_map are initialized properly when threads enter functions */
593 void FuncNode::init_inst_act_map(thread_id_t tid)
595 int thread_id = id_to_int(tid);
596 SnapVector<inst_act_map_t *> * thrd_inst_act_map = history->getThrdInstActMap(func_id);
597 uint old_size = thrd_inst_act_map->size();
599 if (thrd_inst_act_map->size() <= (uint) thread_id) {
600 uint new_size = thread_id + 1;
601 thrd_inst_act_map->resize(new_size);
603 for (uint i = old_size; i < new_size; i++)
604 (*thrd_inst_act_map)[i] = new inst_act_map_t(128);
608 /* Reset elements of thrd_inst_act_map when threads exit functions */
609 void FuncNode::reset_inst_act_map(thread_id_t tid)
611 int thread_id = id_to_int(tid);
612 SnapVector<inst_act_map_t *> * thrd_inst_act_map = history->getThrdInstActMap(func_id);
614 inst_act_map_t * map = (*thrd_inst_act_map)[thread_id];
618 void FuncNode::update_inst_act_map(thread_id_t tid, ModelAction * read_act)
620 int thread_id = id_to_int(tid);
621 SnapVector<inst_act_map_t *> * thrd_inst_act_map = history->getThrdInstActMap(func_id);
623 inst_act_map_t * map = (*thrd_inst_act_map)[thread_id];
624 FuncInst * read_inst = get_inst(read_act);
625 map->put(read_inst, read_act);
628 inst_act_map_t * FuncNode::get_inst_act_map(thread_id_t tid)
630 int thread_id = id_to_int(tid);
631 SnapVector<inst_act_map_t *> * thrd_inst_act_map = history->getThrdInstActMap(func_id);
633 return (*thrd_inst_act_map)[thread_id];
636 /* Add FuncNodes that this node may follow */
637 void FuncNode::add_out_edge(FuncNode * other)
639 if ( !edge_table.contains(other) ) {
640 edge_table.put(other, OUT_EDGE);
641 out_edges.push_back(other);
645 edge_type_t edge = edge_table.get(other);
646 if (edge == IN_EDGE) {
647 edge_table.put(other, BI_EDGE);
648 out_edges.push_back(other);
652 /* Compute the distance between this FuncNode and the target node.
653 * Return -1 if the target node is unreachable or the actual distance
654 * is greater than max_step.
656 int FuncNode::compute_distance(FuncNode * target, int max_step)
660 else if (target == this)
663 SnapList<FuncNode *> queue;
664 HashTable<FuncNode *, int, uintptr_t, 0> distances(128);
666 queue.push_back(this);
667 distances.put(this, 0);
669 while (!queue.empty()) {
670 FuncNode * curr = queue.front();
672 int dist = distances.get(curr);
674 if (max_step <= dist)
677 ModelList<FuncNode *> * outEdges = curr->get_out_edges();
678 mllnode<FuncNode *> * it;
679 for (it = outEdges->begin(); it != NULL; it = it->getNext()) {
680 FuncNode * out_node = it->getVal();
682 /* This node has not been visited before */
683 if ( !distances.contains(out_node) ) {
684 if (out_node == target)
687 queue.push_back(out_node);
688 distances.put(out_node, dist + 1);
693 /* Target node is unreachable */
697 void FuncNode::print_predicate_tree()
699 model_print("digraph function_%s {\n", func_name);
700 predicate_tree_entry->print_pred_subtree();
701 predicate_tree_exit->print_predicate();
702 model_print("}\n"); // end of graph
705 void FuncNode::print_val_loc_map()
708 value_set_iter * val_it = values_may_read_from->iterator();
709 while (val_it->hasNext()) {
710 uint64_t value = val_it->next();
711 model_print("val %llx: ", value);
713 loc_set_t * locations = val_loc_map->get(value);
714 loc_set_iter * loc_it = locations->iterator();
715 while (loc_it->hasNext()) {
716 void * location = loc_it->next();
717 model_print("%p ", location);