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);
24 // Memories that are reclaimed after each execution
25 action_list_buffer = new SnapList<action_list_t *>();
26 read_locations = new loc_set_t();
27 write_locations = new loc_set_t();
28 val_loc_map = new HashTable<uint64_t, loc_set_t *, uint64_t, 0>();
29 loc_may_equal_map = new HashTable<void *, loc_set_t *, uintptr_t, 0>();
31 //values_may_read_from = new value_set_t();
34 /* Reallocate snapshotted memories when new executions start */
35 void FuncNode::set_new_exec_flag()
37 action_list_buffer = new SnapList<action_list_t *>();
38 read_locations = new loc_set_t();
39 write_locations = new loc_set_t();
40 val_loc_map = new HashTable<uint64_t, loc_set_t *, uint64_t, 0>();
41 loc_may_equal_map = new HashTable<void *, loc_set_t *, uintptr_t, 0>();
43 //values_may_read_from = new value_set_t();
46 /* Check whether FuncInst with the same type, position, and location
47 * as act has been added to func_inst_map or not. If not, add it.
49 * Note: currently, actions with the same position are filtered out by process_action,
50 * so the collision list of FuncInst is not used. May remove it later.
52 void FuncNode::add_inst(ModelAction *act)
55 const char * position = act->get_position();
57 /* THREAD* actions, ATOMIC_LOCK, ATOMIC_TRYLOCK, and ATOMIC_UNLOCK
58 * actions are not tagged with their source line numbers
63 if ( func_inst_map.contains(position) ) {
64 FuncInst * inst = func_inst_map.get(position);
66 ASSERT(inst->get_type() == act->get_type());
67 int curr_execution_number = model->get_execution_number();
69 /* Reset locations when new executions start */
70 if (inst->get_execution_number() != curr_execution_number) {
71 inst->set_location(act->get_location());
72 inst->set_execution_number(curr_execution_number);
75 if (inst->get_location() != act->get_location())
76 inst->not_single_location();
81 FuncInst * func_inst = new FuncInst(act, this);
83 func_inst_map.put(position, func_inst);
84 inst_list.push_back(func_inst);
87 /* Get the FuncInst with the same type, position, and location
90 * @return FuncInst with the same type, position, and location as act */
91 FuncInst * FuncNode::get_inst(ModelAction *act)
94 const char * position = act->get_position();
96 /* THREAD* actions, ATOMIC_LOCK, ATOMIC_TRYLOCK, and ATOMIC_UNLOCK
97 * actions are not tagged with their source line numbers
102 FuncInst * inst = func_inst_map.get(position);
106 action_type inst_type = inst->get_type();
107 action_type act_type = act->get_type();
109 // else if branch: an RMWRCAS action is converted to a RMW or READ action
110 if (inst_type == act_type)
112 else if (inst_type == ATOMIC_RMWRCAS &&
113 (act_type == ATOMIC_RMW || act_type == ATOMIC_READ))
120 void FuncNode::add_entry_inst(FuncInst * inst)
125 mllnode<FuncInst *> * it;
126 for (it = entry_insts.begin(); it != NULL; it = it->getNext()) {
127 if (inst == it->getVal())
131 entry_insts.push_back(inst);
135 * @brief Convert ModelAdtion list to FuncInst list
136 * @param act_list A list of ModelActions
138 void FuncNode::update_tree(action_list_t * act_list)
140 if (act_list == NULL || act_list->size() == 0)
143 HashTable<void *, value_set_t *, uintptr_t, 0> * write_history = history->getWriteHistory();
145 /* build inst_list from act_list for later processing */
146 func_inst_list_t inst_list;
147 action_list_t rw_act_list;
149 for (sllnode<ModelAction *> * it = act_list->begin(); it != NULL; it = it->getNext()) {
150 ModelAction * act = it->getVal();
151 FuncInst * func_inst = get_inst(act);
152 void * loc = act->get_location();
154 if (func_inst == NULL)
157 inst_list.push_back(func_inst);
158 bool act_added = false;
160 if (act->is_write()) {
161 rw_act_list.push_back(act);
163 if (!write_locations->contains(loc)) {
164 write_locations->add(loc);
165 history->update_loc_wr_func_nodes_map(loc, this);
169 if (act->is_read()) {
171 rw_act_list.push_back(act);
173 /* If func_inst may only read_from a single location, then:
175 * The first time an action reads from some location,
176 * import all the values that have been written to this
177 * location from ModelHistory and notify ModelHistory
178 * that this FuncNode may read from this location.
180 if (!read_locations->contains(loc) && func_inst->is_single_location()) {
181 read_locations->add(loc);
182 value_set_t * write_values = write_history->get(loc);
183 add_to_val_loc_map(write_values, loc);
184 history->update_loc_rd_func_nodes_map(loc, this);
189 // model_print("function %s\n", func_name);
190 // print_val_loc_map();
192 update_inst_tree(&inst_list);
193 update_predicate_tree(&rw_act_list);
195 // print_predicate_tree();
199 * @brief Link FuncInsts in inst_list - add one FuncInst to another's predecessors and successors
200 * @param inst_list A list of FuncInsts
202 void FuncNode::update_inst_tree(func_inst_list_t * inst_list)
204 if (inst_list == NULL)
206 else if (inst_list->size() == 0)
210 sllnode<FuncInst *>* it = inst_list->begin();
211 sllnode<FuncInst *>* prev;
213 /* add the first instruction to the list of entry insts */
214 FuncInst * entry_inst = it->getVal();
215 add_entry_inst(entry_inst);
219 prev = it->getPrev();
221 FuncInst * prev_inst = prev->getVal();
222 FuncInst * curr_inst = it->getVal();
224 prev_inst->add_succ(curr_inst);
225 curr_inst->add_pred(prev_inst);
231 void FuncNode::update_predicate_tree(action_list_t * act_list)
233 if (act_list == NULL || act_list->size() == 0)
238 /* Map a FuncInst to the its predicate */
239 HashTable<FuncInst *, Predicate *, uintptr_t, 0> inst_pred_map(128);
241 // Number FuncInsts to detect loops
242 HashTable<FuncInst *, uint32_t, uintptr_t, 0> inst_id_map(128);
243 uint32_t inst_counter = 0;
245 /* Only need to store the locations of read actions */
246 HashTable<void *, ModelAction *, uintptr_t, 0> loc_act_map(128);
248 sllnode<ModelAction *> *it = act_list->begin();
249 Predicate * curr_pred = predicate_tree_entry;
251 ModelAction * next_act = it->getVal();
252 FuncInst * next_inst = get_inst(next_act);
253 next_inst->set_associated_act(next_act, marker);
255 SnapVector<Predicate *> unset_predicates = SnapVector<Predicate *>();
256 bool branch_found = follow_branch(&curr_pred, next_inst, next_act, &unset_predicates);
258 // A branch with unset predicate expression is detected
259 if (!branch_found && unset_predicates.size() != 0) {
260 ASSERT(unset_predicates.size() == 1);
261 Predicate * one_branch = unset_predicates[0];
263 bool amended = amend_predicate_expr(&curr_pred, next_inst, next_act);
267 curr_pred = one_branch;
273 if (!branch_found && inst_id_map.contains(next_inst)) {
274 FuncInst * curr_inst = curr_pred->get_func_inst();
275 uint32_t curr_id = inst_id_map.get(curr_inst);
276 uint32_t next_id = inst_id_map.get(next_inst);
278 if (curr_id >= next_id) {
279 Predicate * old_pred = inst_pred_map.get(next_inst);
280 Predicate * back_pred = old_pred->get_parent();
282 curr_pred->add_backedge(back_pred);
283 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_count();
313 /* Given curr_pred and next_inst, find the branch following curr_pred that
314 * contains next_inst and the correct predicate.
315 * @return true if branch found, false otherwise.
317 bool FuncNode::follow_branch(Predicate ** curr_pred, FuncInst * next_inst,
318 ModelAction * next_act, SnapVector<Predicate *> * unset_predicates)
320 /* Check if a branch with func_inst and corresponding predicate exists */
321 bool branch_found = false;
322 ModelVector<Predicate *> * branches = (*curr_pred)->get_children();
323 for (uint i = 0; i < branches->size(); i++) {
324 Predicate * branch = (*branches)[i];
325 if (branch->get_func_inst() != next_inst)
328 /* Check against predicate expressions */
329 bool predicate_correct = true;
330 PredExprSet * pred_expressions = branch->get_pred_expressions();
331 PredExprSetIter * pred_expr_it = pred_expressions->iterator();
333 /* Only read and rmw actions my have unset predicate expressions */
334 if (pred_expressions->getSize() == 0) {
335 predicate_correct = false;
336 unset_predicates->push_back(branch);
339 while (pred_expr_it->hasNext()) {
340 pred_expr * pred_expression = pred_expr_it->next();
341 uint64_t last_read, next_read;
344 switch(pred_expression->token) {
346 predicate_correct = true;
349 FuncInst * to_be_compared;
350 ModelAction * last_act;
352 to_be_compared = pred_expression->func_inst;
353 last_act = to_be_compared->get_associated_act(marker);
355 last_read = last_act->get_reads_from_value();
356 next_read = next_act->get_reads_from_value();
357 equality = (last_read == next_read);
358 if (equality != pred_expression->value)
359 predicate_correct = false;
363 next_read = next_act->get_reads_from_value();
364 equality = ((void*)next_read == NULL);
365 if (equality != pred_expression->value)
366 predicate_correct = false;
369 predicate_correct = false;
370 model_print("unkown predicate token\n");
375 if (predicate_correct) {
385 /* Infer predicate expressions, which are generated in FuncNode::generate_predicates */
386 void FuncNode::infer_predicates(FuncInst * next_inst, ModelAction * next_act,
387 HashTable<void *, ModelAction *, uintptr_t, 0> * loc_act_map,
388 SnapVector<struct half_pred_expr *> * half_pred_expressions)
390 void * loc = next_act->get_location();
392 if (next_inst->is_read()) {
394 if ( loc_act_map->contains(loc) ) {
395 ModelAction * last_act = loc_act_map->get(loc);
396 FuncInst * last_inst = get_inst(last_act);
397 struct half_pred_expr * expression = new half_pred_expr(EQUALITY, last_inst);
398 half_pred_expressions->push_back(expression);
399 } else if ( next_inst->is_single_location() ){
400 loc_set_t * loc_may_equal = loc_may_equal_map->get(loc);
402 if (loc_may_equal != NULL) {
403 loc_set_iter * loc_it = loc_may_equal->iterator();
404 while (loc_it->hasNext()) {
405 void * neighbor = loc_it->next();
406 if (loc_act_map->contains(neighbor)) {
407 ModelAction * last_act = loc_act_map->get(neighbor);
408 FuncInst * last_inst = get_inst(last_act);
410 struct half_pred_expr * expression = new half_pred_expr(EQUALITY, last_inst);
411 half_pred_expressions->push_back(expression);
416 // next_inst is not single location
417 uint64_t read_val = next_act->get_reads_from_value();
419 // only infer NULLITY predicate when it is actually NULL.
420 if ( (void*)read_val == NULL) {
421 struct half_pred_expr * expression = new half_pred_expr(NULLITY, NULL);
422 half_pred_expressions->push_back(expression);
427 // TODO: do anything here?
431 /* Able to generate complex predicates when there are multiple predciate expressions */
432 void FuncNode::generate_predicates(Predicate ** curr_pred, FuncInst * next_inst,
433 SnapVector<struct half_pred_expr *> * half_pred_expressions)
435 if (half_pred_expressions->size() == 0) {
436 Predicate * new_pred = new Predicate(next_inst);
437 (*curr_pred)->add_child(new_pred);
438 new_pred->set_parent(*curr_pred);
440 /* entry predicates and predicates containing pure write actions
441 * have no predicate expressions */
442 if ( (*curr_pred)->is_entry_predicate() )
443 new_pred->add_predicate_expr(NOPREDICATE, NULL, true);
444 else if (next_inst->is_write()) {
445 /* next_inst->is_write() <==> pure writes */
446 new_pred->add_predicate_expr(NOPREDICATE, NULL, true);
452 SnapVector<Predicate *> predicates;
454 struct half_pred_expr * half_expr = (*half_pred_expressions)[0];
455 predicates.push_back(new Predicate(next_inst));
456 predicates.push_back(new Predicate(next_inst));
458 predicates[0]->add_predicate_expr(half_expr->token, half_expr->func_inst, true);
459 predicates[1]->add_predicate_expr(half_expr->token, half_expr->func_inst, false);
461 for (uint i = 1; i < half_pred_expressions->size(); i++) {
462 half_expr = (*half_pred_expressions)[i];
464 uint old_size = predicates.size();
465 for (uint j = 0; j < old_size; j++) {
466 Predicate * pred = predicates[j];
467 Predicate * new_pred = new Predicate(next_inst);
468 new_pred->copy_predicate_expr(pred);
470 pred->add_predicate_expr(half_expr->token, half_expr->func_inst, true);
471 new_pred->add_predicate_expr(half_expr->token, half_expr->func_inst, false);
473 predicates.push_back(new_pred);
477 for (uint i = 0; i < predicates.size(); i++) {
478 Predicate * pred= predicates[i];
479 (*curr_pred)->add_child(pred);
480 pred->set_parent(*curr_pred);
483 /* Free memories allocated by infer_predicate */
484 for (uint i = 0; i < half_pred_expressions->size(); i++) {
485 struct half_pred_expr * tmp = (*half_pred_expressions)[i];
490 /* Amend predicates that contain no predicate expressions. Currenlty only amend with NULLITY predicates */
491 bool FuncNode::amend_predicate_expr(Predicate ** curr_pred, FuncInst * next_inst, ModelAction * next_act)
493 // there should only be only child
494 Predicate * unset_pred = (*curr_pred)->get_children()->back();
495 uint64_t read_val = next_act->get_reads_from_value();
497 // only generate NULLITY predicate when it is actually NULL.
498 if ( !next_inst->is_single_location() && (void*)read_val == NULL ) {
499 Predicate * new_pred = new Predicate(next_inst);
501 (*curr_pred)->add_child(new_pred);
502 new_pred->set_parent(*curr_pred);
504 unset_pred->add_predicate_expr(NULLITY, NULL, false);
505 new_pred->add_predicate_expr(NULLITY, NULL, true);
513 void FuncNode::add_to_val_loc_map(uint64_t val, void * loc)
515 loc_set_t * locations = val_loc_map->get(val);
517 if (locations == NULL) {
518 locations = new loc_set_t();
519 val_loc_map->put(val, locations);
522 update_loc_may_equal_map(loc, locations);
524 // values_may_read_from->add(val);
527 void FuncNode::add_to_val_loc_map(value_set_t * values, void * loc)
532 value_set_iter * it = values->iterator();
533 while (it->hasNext()) {
534 uint64_t val = it->next();
535 add_to_val_loc_map(val, loc);
539 void FuncNode::update_loc_may_equal_map(void * new_loc, loc_set_t * old_locations)
541 if ( old_locations->contains(new_loc) )
544 loc_set_t * neighbors = loc_may_equal_map->get(new_loc);
546 if (neighbors == NULL) {
547 neighbors = new loc_set_t();
548 loc_may_equal_map->put(new_loc, neighbors);
551 loc_set_iter * loc_it = old_locations->iterator();
552 while (loc_it->hasNext()) {
553 // new_loc: { old_locations, ... }
554 void * member = loc_it->next();
555 neighbors->add(member);
557 // for each i in old_locations, i : { new_loc, ... }
558 loc_set_t * _neighbors = loc_may_equal_map->get(member);
559 if (_neighbors == NULL) {
560 _neighbors = new loc_set_t();
561 loc_may_equal_map->put(member, _neighbors);
563 _neighbors->add(new_loc);
567 /* Every time a thread enters a function, set its position to the predicate tree entry */
568 void FuncNode::init_predicate_tree_position(thread_id_t tid)
570 int thread_id = id_to_int(tid);
571 if (predicate_tree_position.size() <= (uint) thread_id)
572 predicate_tree_position.resize(thread_id + 1);
574 predicate_tree_position[thread_id] = predicate_tree_entry;
577 void FuncNode::set_predicate_tree_position(thread_id_t tid, Predicate * pred)
579 int thread_id = id_to_int(tid);
580 predicate_tree_position[thread_id] = pred;
583 /* @return The position of a thread in a predicate tree */
584 Predicate * FuncNode::get_predicate_tree_position(thread_id_t tid)
586 int thread_id = id_to_int(tid);
587 return predicate_tree_position[thread_id];
590 /* Make sure elements of thrd_inst_act_map are initialized properly when threads enter functions */
591 void FuncNode::init_inst_act_map(thread_id_t tid)
593 int thread_id = id_to_int(tid);
594 SnapVector<inst_act_map_t *> * thrd_inst_act_map = history->getThrdInstActMap(func_id);
595 uint old_size = thrd_inst_act_map->size();
597 if (thrd_inst_act_map->size() <= (uint) thread_id) {
598 uint new_size = thread_id + 1;
599 thrd_inst_act_map->resize(new_size);
601 for (uint i = old_size; i < new_size; i++)
602 (*thrd_inst_act_map)[i] = new inst_act_map_t(128);
606 /* Reset elements of thrd_inst_act_map when threads exit functions */
607 void FuncNode::reset_inst_act_map(thread_id_t tid)
609 int thread_id = id_to_int(tid);
610 SnapVector<inst_act_map_t *> * thrd_inst_act_map = history->getThrdInstActMap(func_id);
612 inst_act_map_t * map = (*thrd_inst_act_map)[thread_id];
616 void FuncNode::update_inst_act_map(thread_id_t tid, ModelAction * read_act)
618 int thread_id = id_to_int(tid);
619 SnapVector<inst_act_map_t *> * thrd_inst_act_map = history->getThrdInstActMap(func_id);
621 inst_act_map_t * map = (*thrd_inst_act_map)[thread_id];
622 FuncInst * read_inst = get_inst(read_act);
623 map->put(read_inst, read_act);
626 inst_act_map_t * FuncNode::get_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);
631 return (*thrd_inst_act_map)[thread_id];
634 /* Add FuncNodes that this node may follow */
635 void FuncNode::add_out_edge(FuncNode * other)
637 if ( !edge_table.contains(other) ) {
638 edge_table.put(other, OUT_EDGE);
639 out_edges.push_back(other);
643 edge_type_t edge = edge_table.get(other);
644 if (edge == IN_EDGE) {
645 edge_table.put(other, BI_EDGE);
646 out_edges.push_back(other);
650 /* Compute the distance between this FuncNode and the target node.
651 * Return -1 if the target node is unreachable or the actual distance
652 * is greater than max_step.
654 int FuncNode::compute_distance(FuncNode * target, int max_step)
658 else if (target == this)
661 SnapList<FuncNode *> queue;
662 HashTable<FuncNode *, int, uintptr_t, 0> distances(128);
664 queue.push_back(this);
665 distances.put(this, 0);
667 while (!queue.empty()) {
668 FuncNode * curr = queue.front();
670 int dist = distances.get(curr);
672 if (max_step <= dist)
675 ModelList<FuncNode *> * outEdges = curr->get_out_edges();
676 mllnode<FuncNode *> * it;
677 for (it = outEdges->begin(); it != NULL; it = it->getNext()) {
678 FuncNode * out_node = it->getVal();
680 /* This node has not been visited before */
681 if ( !distances.contains(out_node) ) {
682 if (out_node == target)
685 queue.push_back(out_node);
686 distances.put(out_node, dist + 1);
691 /* Target node is unreachable */
695 void FuncNode::print_predicate_tree()
697 model_print("digraph function_%s {\n", func_name);
698 predicate_tree_entry->print_pred_subtree();
699 model_print("}\n"); // end of graph
702 void FuncNode::print_val_loc_map()
705 value_set_iter * val_it = values_may_read_from->iterator();
706 while (val_it->hasNext()) {
707 uint64_t value = val_it->next();
708 model_print("val %llx: ", value);
710 loc_set_t * locations = val_loc_map->get(value);
711 loc_set_iter * loc_it = locations->iterator();
712 while (loc_it->hasNext()) {
713 void * location = loc_it->next();
714 model_print("%p ", location);