6 #include "concretepredicate.h"
8 FuncNode::FuncNode(ModelHistory * history) :
14 predicate_tree_position(),
18 predicate_tree_entry = new Predicate(NULL, true);
19 predicate_tree_entry->add_predicate_expr(NOPREDICATE, NULL, true);
21 // Memories that are reclaimed after each execution
22 action_list_buffer = new SnapList<action_list_t *>();
23 read_locations = new loc_set_t();
24 write_locations = new loc_set_t();
25 val_loc_map = new HashTable<uint64_t, loc_set_t *, uint64_t, 0>();
26 loc_may_equal_map = new HashTable<void *, loc_set_t *, uintptr_t, 0>();
27 thrd_inst_act_map = new SnapVector<inst_act_map_t *>();
29 //values_may_read_from = new value_set_t();
32 /* Reallocate snapshotted memories when new executions start */
33 void FuncNode::set_new_exec_flag()
35 for (mllnode<FuncInst *> * it = inst_list.begin(); it != NULL; it = it->getNext()) {
36 FuncInst * inst = it->getVal();
37 inst->unset_location();
40 action_list_buffer = new SnapList<action_list_t *>();
41 read_locations = new loc_set_t();
42 write_locations = new loc_set_t();
43 val_loc_map = new HashTable<uint64_t, loc_set_t *, uint64_t, 0>();
44 loc_may_equal_map = new HashTable<void *, loc_set_t *, uintptr_t, 0>();
45 thrd_inst_act_map = new SnapVector<inst_act_map_t *>();
47 //values_may_read_from = new value_set_t();
50 /* Check whether FuncInst with the same type, position, and location
51 * as act has been added to func_inst_map or not. If not, add it.
53 * Note: currently, actions with the same position are filtered out by process_action,
54 * so the collision list of FuncInst is not used. May remove it later.
56 void FuncNode::add_inst(ModelAction *act)
59 const char * position = act->get_position();
61 /* THREAD* actions, ATOMIC_LOCK, ATOMIC_TRYLOCK, and ATOMIC_UNLOCK
62 * actions are not tagged with their source line numbers
67 if ( func_inst_map.contains(position) ) {
68 FuncInst * inst = func_inst_map.get(position);
70 ASSERT(inst->get_type() == act->get_type());
72 // locations are set to NULL when new executions start
73 if (inst->get_location() == NULL)
74 inst->set_location(act->get_location());
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, 4> * 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);
171 if (act->is_read()) {
173 rw_act_list.push_back(act);
175 /* If func_inst may only read_from a single location, then:
177 * The first time an action reads from some location,
178 * import all the values that have been written to this
179 * location from ModelHistory and notify ModelHistory
180 * that this FuncNode may read from this location.
182 if (!read_locations->contains(loc) && func_inst->is_single_location()) {
183 read_locations->add(loc);
184 value_set_t * write_values = write_history->get(loc);
185 add_to_val_loc_map(write_values, loc);
186 history->update_loc_func_nodes_map(loc, this);
191 // model_print("function %s\n", func_name);
192 // print_val_loc_map();
194 update_inst_tree(&inst_list);
195 update_predicate_tree(&rw_act_list);
197 // print_predicate_tree();
201 * @brief Link FuncInsts in inst_list - add one FuncInst to another's predecessors and successors
202 * @param inst_list A list of FuncInsts
204 void FuncNode::update_inst_tree(func_inst_list_t * inst_list)
206 if (inst_list == NULL)
208 else if (inst_list->size() == 0)
212 sllnode<FuncInst *>* it = inst_list->begin();
213 sllnode<FuncInst *>* prev;
215 /* add the first instruction to the list of entry insts */
216 FuncInst * entry_inst = it->getVal();
217 add_entry_inst(entry_inst);
221 prev = it->getPrev();
223 FuncInst * prev_inst = prev->getVal();
224 FuncInst * curr_inst = it->getVal();
226 prev_inst->add_succ(curr_inst);
227 curr_inst->add_pred(prev_inst);
233 void FuncNode::update_predicate_tree(action_list_t * act_list)
235 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);
247 HashTable<FuncInst *, ModelAction *, uintptr_t, 0> inst_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);
255 SnapVector<Predicate *> unset_predicates = SnapVector<Predicate *>();
256 bool branch_found = follow_branch(&curr_pred, next_inst, next_act, &inst_act_map, &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_act_map.put(next_inst, next_act);
305 inst_pred_map.put(next_inst, curr_pred);
306 if (!inst_id_map.contains(next_inst))
307 inst_id_map.put(next_inst, inst_counter++);
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, ModelAction * next_act,
318 HashTable<FuncInst *, ModelAction *, uintptr_t, 0> * inst_act_map,
319 SnapVector<Predicate *> * unset_predicates)
321 /* check if a branch with func_inst and corresponding predicate exists */
322 bool branch_found = false;
323 ModelVector<Predicate *> * branches = (*curr_pred)->get_children();
324 for (uint i = 0; i < branches->size(); i++) {
325 Predicate * branch = (*branches)[i];
326 if (branch->get_func_inst() != next_inst)
329 /* Check against predicate expressions */
330 bool predicate_correct = true;
331 PredExprSet * pred_expressions = branch->get_pred_expressions();
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 ConcretePredicate * concrete_pred = branch->evaluate(inst_act_map, next_act->get_tid());
340 SnapVector<struct concrete_pred_expr> * concrete_exprs = concrete_pred->getExpressions();
341 for (uint i = 0; i < concrete_exprs->size(); i++) {
342 struct concrete_pred_expr concrete = (*concrete_exprs)[i];
346 switch (concrete.token) {
348 predicate_correct = true;
351 next_read = next_act->get_reads_from_value();
352 equality = (next_read == concrete.value);
353 if (equality != concrete.equality)
354 predicate_correct = false;
357 next_read = next_act->get_reads_from_value();
358 equality = ((void*)next_read == NULL);
359 if (equality != concrete.equality)
360 predicate_correct = false;
363 predicate_correct = false;
364 model_print("unkown predicate token\n");
368 delete concrete_pred;
370 if (predicate_correct) {
380 /* Infer predicate expressions, which are generated in FuncNode::generate_predicates */
381 void FuncNode::infer_predicates(FuncInst * next_inst, ModelAction * next_act,
382 HashTable<void *, ModelAction *, uintptr_t, 0> * loc_act_map,
383 SnapVector<struct half_pred_expr *> * half_pred_expressions)
385 void * loc = next_act->get_location();
387 if (next_inst->is_read()) {
389 if ( loc_act_map->contains(loc) ) {
390 ModelAction * last_act = loc_act_map->get(loc);
391 FuncInst * last_inst = get_inst(last_act);
392 struct half_pred_expr * expression = new half_pred_expr(EQUALITY, last_inst);
393 half_pred_expressions->push_back(expression);
394 } else if ( next_inst->is_single_location() ){
395 loc_set_t * loc_may_equal = loc_may_equal_map->get(loc);
397 if (loc_may_equal != NULL) {
398 loc_set_iter * loc_it = loc_may_equal->iterator();
399 while (loc_it->hasNext()) {
400 void * neighbor = loc_it->next();
401 if (loc_act_map->contains(neighbor)) {
402 ModelAction * last_act = loc_act_map->get(neighbor);
403 FuncInst * last_inst = get_inst(last_act);
405 struct half_pred_expr * expression = new half_pred_expr(EQUALITY, last_inst);
406 half_pred_expressions->push_back(expression);
411 // next_inst is not single location
412 uint64_t read_val = next_act->get_reads_from_value();
414 // only infer NULLITY predicate when it is actually NULL.
415 if ( (void*)read_val == NULL) {
416 struct half_pred_expr * expression = new half_pred_expr(NULLITY, NULL);
417 half_pred_expressions->push_back(expression);
422 // TODO: do anything here?
426 /* Able to generate complex predicates when there are multiple predciate expressions */
427 void FuncNode::generate_predicates(Predicate ** curr_pred, FuncInst * next_inst,
428 SnapVector<struct half_pred_expr *> * half_pred_expressions)
430 if (half_pred_expressions->size() == 0) {
431 Predicate * new_pred = new Predicate(next_inst);
432 (*curr_pred)->add_child(new_pred);
433 new_pred->set_parent(*curr_pred);
435 /* entry predicates and predicates containing pure write actions
436 * have no predicate expressions */
437 if ( (*curr_pred)->is_entry_predicate() )
438 new_pred->add_predicate_expr(NOPREDICATE, NULL, true);
439 else if (next_inst->is_write()) {
440 /* next_inst->is_write() <==> pure writes */
441 new_pred->add_predicate_expr(NOPREDICATE, NULL, true);
447 SnapVector<Predicate *> predicates;
449 struct half_pred_expr * half_expr = (*half_pred_expressions)[0];
450 predicates.push_back(new Predicate(next_inst));
451 predicates.push_back(new Predicate(next_inst));
453 predicates[0]->add_predicate_expr(half_expr->token, half_expr->func_inst, true);
454 predicates[1]->add_predicate_expr(half_expr->token, half_expr->func_inst, false);
456 for (uint i = 1; i < half_pred_expressions->size(); i++) {
457 half_expr = (*half_pred_expressions)[i];
459 uint old_size = predicates.size();
460 for (uint j = 0; j < old_size; j++) {
461 Predicate * pred = predicates[j];
462 Predicate * new_pred = new Predicate(next_inst);
463 new_pred->copy_predicate_expr(pred);
465 pred->add_predicate_expr(half_expr->token, half_expr->func_inst, true);
466 new_pred->add_predicate_expr(half_expr->token, half_expr->func_inst, false);
468 predicates.push_back(new_pred);
472 for (uint i = 0; i < predicates.size(); i++) {
473 Predicate * pred= predicates[i];
474 (*curr_pred)->add_child(pred);
475 pred->set_parent(*curr_pred);
478 /* Free memories allocated by infer_predicate */
479 for (uint i = 0; i < half_pred_expressions->size(); i++) {
480 struct half_pred_expr * tmp = (*half_pred_expressions)[i];
485 /* Amend predicates that contain no predicate expressions. Currenlty only amend with NULLITY predicates */
486 bool FuncNode::amend_predicate_expr(Predicate ** curr_pred, FuncInst * next_inst, ModelAction * next_act)
488 // there should only be only child
489 Predicate * unset_pred = (*curr_pred)->get_children()->back();
490 uint64_t read_val = next_act->get_reads_from_value();
492 // only generate NULLITY predicate when it is actually NULL.
493 if ( !next_inst->is_single_location() && (void*)read_val == NULL ) {
494 Predicate * new_pred = new Predicate(next_inst);
496 (*curr_pred)->add_child(new_pred);
497 new_pred->set_parent(*curr_pred);
499 unset_pred->add_predicate_expr(NULLITY, NULL, false);
500 new_pred->add_predicate_expr(NULLITY, NULL, true);
508 void FuncNode::add_to_val_loc_map(uint64_t val, void * loc)
510 loc_set_t * locations = val_loc_map->get(val);
512 if (locations == NULL) {
513 locations = new loc_set_t();
514 val_loc_map->put(val, locations);
517 update_loc_may_equal_map(loc, locations);
519 // values_may_read_from->add(val);
522 void FuncNode::add_to_val_loc_map(value_set_t * values, void * loc)
527 value_set_iter * it = values->iterator();
528 while (it->hasNext()) {
529 uint64_t val = it->next();
530 add_to_val_loc_map(val, loc);
534 void FuncNode::update_loc_may_equal_map(void * new_loc, loc_set_t * old_locations)
536 if ( old_locations->contains(new_loc) )
539 loc_set_t * neighbors = loc_may_equal_map->get(new_loc);
541 if (neighbors == NULL) {
542 neighbors = new loc_set_t();
543 loc_may_equal_map->put(new_loc, neighbors);
546 loc_set_iter * loc_it = old_locations->iterator();
547 while (loc_it->hasNext()) {
548 // new_loc: { old_locations, ... }
549 void * member = loc_it->next();
550 neighbors->add(member);
552 // for each i in old_locations, i : { new_loc, ... }
553 loc_set_t * _neighbors = loc_may_equal_map->get(member);
554 if (_neighbors == NULL) {
555 _neighbors = new loc_set_t();
556 loc_may_equal_map->put(member, _neighbors);
558 _neighbors->add(new_loc);
562 /* Every time a thread enters a function, set its position to the predicate tree entry */
563 void FuncNode::init_predicate_tree_position(thread_id_t tid)
565 int thread_id = id_to_int(tid);
566 if (predicate_tree_position.size() <= (uint) thread_id)
567 predicate_tree_position.resize(thread_id + 1);
569 predicate_tree_position[thread_id] = predicate_tree_entry;
572 void FuncNode::set_predicate_tree_position(thread_id_t tid, Predicate * pred)
574 int thread_id = id_to_int(tid);
575 predicate_tree_position[thread_id] = pred;
578 /* @return The position of a thread in a predicate tree */
579 Predicate * FuncNode::get_predicate_tree_position(thread_id_t tid)
581 int thread_id = id_to_int(tid);
582 return predicate_tree_position[thread_id];
585 /* Make sure elements of thrd_inst_act_map are initialized properly when threads enter functions */
586 void FuncNode::init_inst_act_map(thread_id_t tid)
588 int thread_id = id_to_int(tid);
589 uint old_size = thrd_inst_act_map->size();
591 if (thrd_inst_act_map->size() <= (uint) thread_id) {
592 uint new_size = thread_id + 1;
593 thrd_inst_act_map->resize(new_size);
595 for (uint i = old_size; i < new_size; i++)
596 (*thrd_inst_act_map)[i] = new inst_act_map_t(128);
600 /* Reset elements of thrd_inst_act_map when threads exit functions */
601 void FuncNode::reset_inst_act_map(thread_id_t tid)
603 int thread_id = id_to_int(tid);
604 inst_act_map_t * map = (*thrd_inst_act_map)[thread_id];
608 void FuncNode::update_inst_act_map(thread_id_t tid, ModelAction * read_act)
610 int thread_id = id_to_int(tid);
611 inst_act_map_t * map = (*thrd_inst_act_map)[thread_id];
612 FuncInst * read_inst = get_inst(read_act);
613 map->put(read_inst, read_act);
616 inst_act_map_t * FuncNode::get_inst_act_map(thread_id_t tid)
618 int thread_id = id_to_int(tid);
619 return (*thrd_inst_act_map)[thread_id];
622 /* Add FuncNodes that this node may follow */
623 void FuncNode::add_out_edge(FuncNode * other)
625 if ( !edge_table.contains(other) ) {
626 edge_table.put(other, OUT_EDGE);
627 out_edges.push_back(other);
631 edge_type_t edge = edge_table.get(other);
632 if (edge == IN_EDGE) {
633 edge_table.put(other, BI_EDGE);
634 out_edges.push_back(other);
638 void FuncNode::print_predicate_tree()
640 model_print("digraph function_%s {\n", func_name);
641 predicate_tree_entry->print_pred_subtree();
642 model_print("}\n"); // end of graph
645 void FuncNode::print_val_loc_map()
648 value_set_iter * val_it = values_may_read_from->iterator();
649 while (val_it->hasNext()) {
650 uint64_t value = val_it->next();
651 model_print("val %llx: ", value);
653 loc_set_t * locations = val_loc_map->get(value);
654 loc_set_iter * loc_it = locations->iterator();
655 while (loc_it->hasNext()) {
656 void * location = loc_it->next();
657 model_print("%p ", location);