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");
369 if (predicate_correct) {
379 /* Infer predicate expressions, which are generated in FuncNode::generate_predicates */
380 void FuncNode::infer_predicates(FuncInst * next_inst, ModelAction * next_act,
381 HashTable<void *, ModelAction *, uintptr_t, 0> * loc_act_map,
382 SnapVector<struct half_pred_expr *> * half_pred_expressions)
384 void * loc = next_act->get_location();
386 if (next_inst->is_read()) {
388 if ( loc_act_map->contains(loc) ) {
389 ModelAction * last_act = loc_act_map->get(loc);
390 FuncInst * last_inst = get_inst(last_act);
391 struct half_pred_expr * expression = new half_pred_expr(EQUALITY, last_inst);
392 half_pred_expressions->push_back(expression);
393 } else if ( next_inst->is_single_location() ){
394 loc_set_t * loc_may_equal = loc_may_equal_map->get(loc);
396 if (loc_may_equal != NULL) {
397 loc_set_iter * loc_it = loc_may_equal->iterator();
398 while (loc_it->hasNext()) {
399 void * neighbor = loc_it->next();
400 if (loc_act_map->contains(neighbor)) {
401 ModelAction * last_act = loc_act_map->get(neighbor);
402 FuncInst * last_inst = get_inst(last_act);
404 struct half_pred_expr * expression = new half_pred_expr(EQUALITY, last_inst);
405 half_pred_expressions->push_back(expression);
410 // next_inst is not single location
411 uint64_t read_val = next_act->get_reads_from_value();
413 // only infer NULLITY predicate when it is actually NULL.
414 if ( (void*)read_val == NULL) {
415 struct half_pred_expr * expression = new half_pred_expr(NULLITY, NULL);
416 half_pred_expressions->push_back(expression);
421 // TODO: do anything here?
425 /* Able to generate complex predicates when there are multiple predciate expressions */
426 void FuncNode::generate_predicates(Predicate ** curr_pred, FuncInst * next_inst,
427 SnapVector<struct half_pred_expr *> * half_pred_expressions)
429 if (half_pred_expressions->size() == 0) {
430 Predicate * new_pred = new Predicate(next_inst);
431 (*curr_pred)->add_child(new_pred);
432 new_pred->set_parent(*curr_pred);
434 /* entry predicates and predicates containing pure write actions
435 * have no predicate expressions */
436 if ( (*curr_pred)->is_entry_predicate() )
437 new_pred->add_predicate_expr(NOPREDICATE, NULL, true);
438 else if (next_inst->is_write()) {
439 /* next_inst->is_write() <==> pure writes */
440 new_pred->add_predicate_expr(NOPREDICATE, NULL, true);
446 SnapVector<Predicate *> predicates;
448 struct half_pred_expr * half_expr = (*half_pred_expressions)[0];
449 predicates.push_back(new Predicate(next_inst));
450 predicates.push_back(new Predicate(next_inst));
452 predicates[0]->add_predicate_expr(half_expr->token, half_expr->func_inst, true);
453 predicates[1]->add_predicate_expr(half_expr->token, half_expr->func_inst, false);
455 for (uint i = 1; i < half_pred_expressions->size(); i++) {
456 half_expr = (*half_pred_expressions)[i];
458 uint old_size = predicates.size();
459 for (uint j = 0; j < old_size; j++) {
460 Predicate * pred = predicates[j];
461 Predicate * new_pred = new Predicate(next_inst);
462 new_pred->copy_predicate_expr(pred);
464 pred->add_predicate_expr(half_expr->token, half_expr->func_inst, true);
465 new_pred->add_predicate_expr(half_expr->token, half_expr->func_inst, false);
467 predicates.push_back(new_pred);
471 for (uint i = 0; i < predicates.size(); i++) {
472 Predicate * pred= predicates[i];
473 (*curr_pred)->add_child(pred);
474 pred->set_parent(*curr_pred);
477 /* Free memories allocated by infer_predicate */
478 for (uint i = 0; i < half_pred_expressions->size(); i++) {
479 struct half_pred_expr * tmp = (*half_pred_expressions)[i];
484 /* Amend predicates that contain no predicate expressions. Currenlty only amend with NULLITY predicates */
485 bool FuncNode::amend_predicate_expr(Predicate ** curr_pred, FuncInst * next_inst, ModelAction * next_act)
487 // there should only be only child
488 Predicate * unset_pred = (*curr_pred)->get_children()->back();
489 uint64_t read_val = next_act->get_reads_from_value();
491 // only generate NULLITY predicate when it is actually NULL.
492 if ( !next_inst->is_single_location() && (void*)read_val == NULL ) {
493 Predicate * new_pred = new Predicate(next_inst);
495 (*curr_pred)->add_child(new_pred);
496 new_pred->set_parent(*curr_pred);
498 unset_pred->add_predicate_expr(NULLITY, NULL, false);
499 new_pred->add_predicate_expr(NULLITY, NULL, true);
507 void FuncNode::add_to_val_loc_map(uint64_t val, void * loc)
509 loc_set_t * locations = val_loc_map->get(val);
511 if (locations == NULL) {
512 locations = new loc_set_t();
513 val_loc_map->put(val, locations);
516 update_loc_may_equal_map(loc, locations);
518 // values_may_read_from->add(val);
521 void FuncNode::add_to_val_loc_map(value_set_t * values, void * loc)
526 value_set_iter * it = values->iterator();
527 while (it->hasNext()) {
528 uint64_t val = it->next();
529 add_to_val_loc_map(val, loc);
533 void FuncNode::update_loc_may_equal_map(void * new_loc, loc_set_t * old_locations)
535 if ( old_locations->contains(new_loc) )
538 loc_set_t * neighbors = loc_may_equal_map->get(new_loc);
540 if (neighbors == NULL) {
541 neighbors = new loc_set_t();
542 loc_may_equal_map->put(new_loc, neighbors);
545 loc_set_iter * loc_it = old_locations->iterator();
546 while (loc_it->hasNext()) {
547 // new_loc: { old_locations, ... }
548 void * member = loc_it->next();
549 neighbors->add(member);
551 // for each i in old_locations, i : { new_loc, ... }
552 loc_set_t * _neighbors = loc_may_equal_map->get(member);
553 if (_neighbors == NULL) {
554 _neighbors = new loc_set_t();
555 loc_may_equal_map->put(member, _neighbors);
557 _neighbors->add(new_loc);
561 /* Every time a thread enters a function, set its position to the predicate tree entry */
562 void FuncNode::init_predicate_tree_position(thread_id_t tid)
564 int thread_id = id_to_int(tid);
565 if (predicate_tree_position.size() <= (uint) thread_id)
566 predicate_tree_position.resize(thread_id + 1);
568 predicate_tree_position[thread_id] = predicate_tree_entry;
571 void FuncNode::set_predicate_tree_position(thread_id_t tid, Predicate * pred)
573 int thread_id = id_to_int(tid);
574 predicate_tree_position[thread_id] = pred;
577 /* @return The position of a thread in a predicate tree */
578 Predicate * FuncNode::get_predicate_tree_position(thread_id_t tid)
580 int thread_id = id_to_int(tid);
581 return predicate_tree_position[thread_id];
584 /* Make sure elements of thrd_inst_act_map are initialized properly when threads enter functions */
585 void FuncNode::init_inst_act_map(thread_id_t tid)
587 int thread_id = id_to_int(tid);
588 uint old_size = thrd_inst_act_map->size();
590 if (thrd_inst_act_map->size() <= (uint) thread_id) {
591 uint new_size = thread_id + 1;
592 thrd_inst_act_map->resize(new_size);
594 for (uint i = old_size; i < new_size; i++)
595 (*thrd_inst_act_map)[i] = new inst_act_map_t(128);
599 /* Reset elements of thrd_inst_act_map when threads exit functions */
600 void FuncNode::reset_inst_act_map(thread_id_t tid)
602 int thread_id = id_to_int(tid);
603 inst_act_map_t * map = (*thrd_inst_act_map)[thread_id];
607 void FuncNode::update_inst_act_map(thread_id_t tid, ModelAction * read_act)
609 int thread_id = id_to_int(tid);
610 inst_act_map_t * map = (*thrd_inst_act_map)[thread_id];
611 FuncInst * read_inst = get_inst(read_act);
612 map->put(read_inst, read_act);
615 inst_act_map_t * FuncNode::get_inst_act_map(thread_id_t tid)
617 int thread_id = id_to_int(tid);
618 return (*thrd_inst_act_map)[thread_id];
621 /* Add FuncNodes that this node may follow */
622 void FuncNode::add_out_edge(FuncNode * other)
624 if ( !edge_table.contains(other) ) {
625 edge_table.put(other, OUT_EDGE);
626 out_edges.push_back(other);
630 edge_type_t edge = edge_table.get(other);
631 if (edge == IN_EDGE) {
632 edge_table.put(other, BI_EDGE);
633 out_edges.push_back(other);
637 void FuncNode::print_predicate_tree()
639 model_print("digraph function_%s {\n", func_name);
640 predicate_tree_entry->print_pred_subtree();
641 model_print("}\n"); // end of graph
644 void FuncNode::print_val_loc_map()
647 value_set_iter * val_it = values_may_read_from->iterator();
648 while (val_it->hasNext()) {
649 uint64_t value = val_it->next();
650 model_print("val %llx: ", value);
652 loc_set_t * locations = val_loc_map->get(value);
653 loc_set_iter * loc_it = locations->iterator();
654 while (loc_it->hasNext()) {
655 void * location = loc_it->next();
656 model_print("%p ", location);