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++);
312 /* Given curr_pred and next_inst, find the branch following curr_pred that
313 * contains next_inst and the correct predicate.
314 * @return true if branch found, false otherwise.
316 bool FuncNode::follow_branch(Predicate ** curr_pred, FuncInst * next_inst,
317 ModelAction * next_act, SnapVector<Predicate *> * unset_predicates)
319 /* Check if a branch with func_inst and corresponding predicate exists */
320 bool branch_found = false;
321 ModelVector<Predicate *> * branches = (*curr_pred)->get_children();
322 for (uint i = 0; i < branches->size(); i++) {
323 Predicate * branch = (*branches)[i];
324 if (branch->get_func_inst() != next_inst)
327 /* Check against predicate expressions */
328 bool predicate_correct = true;
329 PredExprSet * pred_expressions = branch->get_pred_expressions();
330 PredExprSetIter * pred_expr_it = pred_expressions->iterator();
332 /* Only read and rmw actions my have unset predicate expressions */
333 if (pred_expressions->getSize() == 0) {
334 predicate_correct = false;
335 unset_predicates->push_back(branch);
338 while (pred_expr_it->hasNext()) {
339 pred_expr * pred_expression = pred_expr_it->next();
340 uint64_t last_read, next_read;
343 switch(pred_expression->token) {
345 predicate_correct = true;
348 FuncInst * to_be_compared;
349 ModelAction * last_act;
351 to_be_compared = pred_expression->func_inst;
352 last_act = to_be_compared->get_associated_act(marker);
354 last_read = last_act->get_reads_from_value();
355 next_read = next_act->get_reads_from_value();
356 equality = (last_read == next_read);
357 if (equality != pred_expression->value)
358 predicate_correct = false;
362 next_read = next_act->get_reads_from_value();
363 equality = ((void*)next_read == NULL);
364 if (equality != pred_expression->value)
365 predicate_correct = false;
368 predicate_correct = false;
369 model_print("unkown predicate token\n");
374 if (predicate_correct) {
384 /* Infer predicate expressions, which are generated in FuncNode::generate_predicates */
385 void FuncNode::infer_predicates(FuncInst * next_inst, ModelAction * next_act,
386 HashTable<void *, ModelAction *, uintptr_t, 0> * loc_act_map,
387 SnapVector<struct half_pred_expr *> * half_pred_expressions)
389 void * loc = next_act->get_location();
391 if (next_inst->is_read()) {
393 if ( loc_act_map->contains(loc) ) {
394 ModelAction * last_act = loc_act_map->get(loc);
395 FuncInst * last_inst = get_inst(last_act);
396 struct half_pred_expr * expression = new half_pred_expr(EQUALITY, last_inst);
397 half_pred_expressions->push_back(expression);
398 } else if ( next_inst->is_single_location() ){
399 loc_set_t * loc_may_equal = loc_may_equal_map->get(loc);
401 if (loc_may_equal != NULL) {
402 loc_set_iter * loc_it = loc_may_equal->iterator();
403 while (loc_it->hasNext()) {
404 void * neighbor = loc_it->next();
405 if (loc_act_map->contains(neighbor)) {
406 ModelAction * last_act = loc_act_map->get(neighbor);
407 FuncInst * last_inst = get_inst(last_act);
409 struct half_pred_expr * expression = new half_pred_expr(EQUALITY, last_inst);
410 half_pred_expressions->push_back(expression);
415 // next_inst is not single location
416 uint64_t read_val = next_act->get_reads_from_value();
418 // only infer NULLITY predicate when it is actually NULL.
419 if ( (void*)read_val == NULL) {
420 struct half_pred_expr * expression = new half_pred_expr(NULLITY, NULL);
421 half_pred_expressions->push_back(expression);
426 // TODO: do anything here?
430 /* Able to generate complex predicates when there are multiple predciate expressions */
431 void FuncNode::generate_predicates(Predicate ** curr_pred, FuncInst * next_inst,
432 SnapVector<struct half_pred_expr *> * half_pred_expressions)
434 if (half_pred_expressions->size() == 0) {
435 Predicate * new_pred = new Predicate(next_inst);
436 (*curr_pred)->add_child(new_pred);
437 new_pred->set_parent(*curr_pred);
439 /* entry predicates and predicates containing pure write actions
440 * have no predicate expressions */
441 if ( (*curr_pred)->is_entry_predicate() )
442 new_pred->add_predicate_expr(NOPREDICATE, NULL, true);
443 else if (next_inst->is_write()) {
444 /* next_inst->is_write() <==> pure writes */
445 new_pred->add_predicate_expr(NOPREDICATE, NULL, true);
451 SnapVector<Predicate *> predicates;
453 struct half_pred_expr * half_expr = (*half_pred_expressions)[0];
454 predicates.push_back(new Predicate(next_inst));
455 predicates.push_back(new Predicate(next_inst));
457 predicates[0]->add_predicate_expr(half_expr->token, half_expr->func_inst, true);
458 predicates[1]->add_predicate_expr(half_expr->token, half_expr->func_inst, false);
460 for (uint i = 1; i < half_pred_expressions->size(); i++) {
461 half_expr = (*half_pred_expressions)[i];
463 uint old_size = predicates.size();
464 for (uint j = 0; j < old_size; j++) {
465 Predicate * pred = predicates[j];
466 Predicate * new_pred = new Predicate(next_inst);
467 new_pred->copy_predicate_expr(pred);
469 pred->add_predicate_expr(half_expr->token, half_expr->func_inst, true);
470 new_pred->add_predicate_expr(half_expr->token, half_expr->func_inst, false);
472 predicates.push_back(new_pred);
476 for (uint i = 0; i < predicates.size(); i++) {
477 Predicate * pred= predicates[i];
478 (*curr_pred)->add_child(pred);
479 pred->set_parent(*curr_pred);
482 /* Free memories allocated by infer_predicate */
483 for (uint i = 0; i < half_pred_expressions->size(); i++) {
484 struct half_pred_expr * tmp = (*half_pred_expressions)[i];
489 /* Amend predicates that contain no predicate expressions. Currenlty only amend with NULLITY predicates */
490 bool FuncNode::amend_predicate_expr(Predicate ** curr_pred, FuncInst * next_inst, ModelAction * next_act)
492 // there should only be only child
493 Predicate * unset_pred = (*curr_pred)->get_children()->back();
494 uint64_t read_val = next_act->get_reads_from_value();
496 // only generate NULLITY predicate when it is actually NULL.
497 if ( !next_inst->is_single_location() && (void*)read_val == NULL ) {
498 Predicate * new_pred = new Predicate(next_inst);
500 (*curr_pred)->add_child(new_pred);
501 new_pred->set_parent(*curr_pred);
503 unset_pred->add_predicate_expr(NULLITY, NULL, false);
504 new_pred->add_predicate_expr(NULLITY, NULL, true);
512 void FuncNode::add_to_val_loc_map(uint64_t val, void * loc)
514 loc_set_t * locations = val_loc_map->get(val);
516 if (locations == NULL) {
517 locations = new loc_set_t();
518 val_loc_map->put(val, locations);
521 update_loc_may_equal_map(loc, locations);
523 // values_may_read_from->add(val);
526 void FuncNode::add_to_val_loc_map(value_set_t * values, void * loc)
531 value_set_iter * it = values->iterator();
532 while (it->hasNext()) {
533 uint64_t val = it->next();
534 add_to_val_loc_map(val, loc);
538 void FuncNode::update_loc_may_equal_map(void * new_loc, loc_set_t * old_locations)
540 if ( old_locations->contains(new_loc) )
543 loc_set_t * neighbors = loc_may_equal_map->get(new_loc);
545 if (neighbors == NULL) {
546 neighbors = new loc_set_t();
547 loc_may_equal_map->put(new_loc, neighbors);
550 loc_set_iter * loc_it = old_locations->iterator();
551 while (loc_it->hasNext()) {
552 // new_loc: { old_locations, ... }
553 void * member = loc_it->next();
554 neighbors->add(member);
556 // for each i in old_locations, i : { new_loc, ... }
557 loc_set_t * _neighbors = loc_may_equal_map->get(member);
558 if (_neighbors == NULL) {
559 _neighbors = new loc_set_t();
560 loc_may_equal_map->put(member, _neighbors);
562 _neighbors->add(new_loc);
566 /* Every time a thread enters a function, set its position to the predicate tree entry */
567 void FuncNode::init_predicate_tree_position(thread_id_t tid)
569 int thread_id = id_to_int(tid);
570 if (predicate_tree_position.size() <= (uint) thread_id)
571 predicate_tree_position.resize(thread_id + 1);
573 predicate_tree_position[thread_id] = predicate_tree_entry;
576 void FuncNode::set_predicate_tree_position(thread_id_t tid, Predicate * pred)
578 int thread_id = id_to_int(tid);
579 predicate_tree_position[thread_id] = pred;
582 /* @return The position of a thread in a predicate tree */
583 Predicate * FuncNode::get_predicate_tree_position(thread_id_t tid)
585 int thread_id = id_to_int(tid);
586 return predicate_tree_position[thread_id];
589 /* Make sure elements of thrd_inst_act_map are initialized properly when threads enter functions */
590 void FuncNode::init_inst_act_map(thread_id_t tid)
592 int thread_id = id_to_int(tid);
593 SnapVector<inst_act_map_t *> * thrd_inst_act_map = history->getThrdInstActMap(func_id);
594 uint old_size = thrd_inst_act_map->size();
596 if (thrd_inst_act_map->size() <= (uint) thread_id) {
597 uint new_size = thread_id + 1;
598 thrd_inst_act_map->resize(new_size);
600 for (uint i = old_size; i < new_size; i++)
601 (*thrd_inst_act_map)[i] = new inst_act_map_t(128);
605 /* Reset elements of thrd_inst_act_map when threads exit functions */
606 void FuncNode::reset_inst_act_map(thread_id_t tid)
608 int thread_id = id_to_int(tid);
609 SnapVector<inst_act_map_t *> * thrd_inst_act_map = history->getThrdInstActMap(func_id);
611 inst_act_map_t * map = (*thrd_inst_act_map)[thread_id];
615 void FuncNode::update_inst_act_map(thread_id_t tid, ModelAction * read_act)
617 int thread_id = id_to_int(tid);
618 SnapVector<inst_act_map_t *> * thrd_inst_act_map = history->getThrdInstActMap(func_id);
620 inst_act_map_t * map = (*thrd_inst_act_map)[thread_id];
621 FuncInst * read_inst = get_inst(read_act);
622 map->put(read_inst, read_act);
625 inst_act_map_t * FuncNode::get_inst_act_map(thread_id_t tid)
627 int thread_id = id_to_int(tid);
628 SnapVector<inst_act_map_t *> * thrd_inst_act_map = history->getThrdInstActMap(func_id);
630 return (*thrd_inst_act_map)[thread_id];
633 /* Add FuncNodes that this node may follow */
634 void FuncNode::add_out_edge(FuncNode * other)
636 if ( !edge_table.contains(other) ) {
637 edge_table.put(other, OUT_EDGE);
638 out_edges.push_back(other);
642 edge_type_t edge = edge_table.get(other);
643 if (edge == IN_EDGE) {
644 edge_table.put(other, BI_EDGE);
645 out_edges.push_back(other);
649 /* Compute the distance between this FuncNode and the target node.
650 * Return -1 if the target node is unreachable or the actual distance
651 * is greater than max_step.
653 int FuncNode::compute_distance(FuncNode * target, int max_step)
657 else if (target == this)
660 SnapList<FuncNode *> queue;
661 HashTable<FuncNode *, int, uintptr_t, 0> distances(128);
663 queue.push_back(this);
664 distances.put(this, 0);
666 while (!queue.empty()) {
667 FuncNode * curr = queue.front();
669 int dist = distances.get(curr);
671 if (max_step <= dist)
674 ModelList<FuncNode *> * outEdges = curr->get_out_edges();
675 mllnode<FuncNode *> * it;
676 for (it = outEdges->begin(); it != NULL; it = it->getNext()) {
677 FuncNode * out_node = it->getVal();
679 /* This node has not been visited before */
680 if ( !distances.contains(out_node) ) {
681 if (out_node == target)
684 queue.push_back(out_node);
685 distances.put(out_node, dist + 1);
690 /* Target node is unreachable */
694 void FuncNode::print_predicate_tree()
696 model_print("digraph function_%s {\n", func_name);
697 predicate_tree_entry->print_pred_subtree();
698 model_print("}\n"); // end of graph
701 void FuncNode::print_val_loc_map()
704 value_set_iter * val_it = values_may_read_from->iterator();
705 while (val_it->hasNext()) {
706 uint64_t value = val_it->next();
707 model_print("val %llx: ", value);
709 loc_set_t * locations = val_loc_map->get(value);
710 loc_set_iter * loc_it = locations->iterator();
711 while (loc_it->hasNext()) {
712 void * location = loc_it->next();
713 model_print("%p ", location);