3 FuncNode::FuncNode(ModelHistory * history) :
9 predicate_tree_position(),
13 predicate_tree_entry = new Predicate(NULL, true);
14 predicate_tree_entry->add_predicate_expr(NOPREDICATE, NULL, true);
16 // Memories that are reclaimed after each execution
17 action_list_buffer = new SnapList<action_list_t *>();
18 read_locations = new loc_set_t();
19 write_locations = new loc_set_t();
20 val_loc_map = new HashTable<uint64_t, loc_set_t *, uint64_t, 0>();
21 loc_may_equal_map = new HashTable<void *, loc_set_t *, uintptr_t, 0>();
22 thrd_inst_act_map = new SnapVector<inst_act_map_t *>();
24 //values_may_read_from = new value_set_t();
27 /* Reallocate snapshotted memories when new executions start */
28 void FuncNode::set_new_exec_flag()
30 for (mllnode<FuncInst *> * it = inst_list.begin(); it != NULL; it = it->getNext()) {
31 FuncInst * inst = it->getVal();
32 inst->unset_location();
35 action_list_buffer = new SnapList<action_list_t *>();
36 read_locations = new loc_set_t();
37 write_locations = new loc_set_t();
38 val_loc_map = new HashTable<uint64_t, loc_set_t *, uint64_t, 0>();
39 loc_may_equal_map = new HashTable<void *, loc_set_t *, uintptr_t, 0>();
40 thrd_inst_act_map = new SnapVector<inst_act_map_t *>();
42 //values_may_read_from = new value_set_t();
45 /* Check whether FuncInst with the same type, position, and location
46 * as act has been added to func_inst_map or not. If not, add it.
48 * Note: currently, actions with the same position are filtered out by process_action,
49 * so the collision list of FuncInst is not used. May remove it later.
51 void FuncNode::add_inst(ModelAction *act)
54 const char * position = act->get_position();
56 /* THREAD* actions, ATOMIC_LOCK, ATOMIC_TRYLOCK, and ATOMIC_UNLOCK
57 * actions are not tagged with their source line numbers
62 if ( func_inst_map.contains(position) ) {
63 FuncInst * inst = func_inst_map.get(position);
65 ASSERT(inst->get_type() == act->get_type());
67 // locations are set to NULL when new executions start
68 if (inst->get_location() == NULL)
69 inst->set_location(act->get_location());
71 if (inst->get_location() != act->get_location())
72 inst->not_single_location();
77 FuncInst * func_inst = new FuncInst(act, this);
79 func_inst_map.put(position, func_inst);
80 inst_list.push_back(func_inst);
83 /* Get the FuncInst with the same type, position, and location
86 * @return FuncInst with the same type, position, and location as act */
87 FuncInst * FuncNode::get_inst(ModelAction *act)
90 const char * position = act->get_position();
92 /* THREAD* actions, ATOMIC_LOCK, ATOMIC_TRYLOCK, and ATOMIC_UNLOCK
93 * actions are not tagged with their source line numbers
98 FuncInst * inst = func_inst_map.get(position);
102 action_type inst_type = inst->get_type();
103 action_type act_type = act->get_type();
105 // else if branch: an RMWRCAS action is converted to a RMW or READ action
106 if (inst_type == act_type)
108 else if (inst_type == ATOMIC_RMWRCAS &&
109 (act_type == ATOMIC_RMW || act_type == ATOMIC_READ))
116 void FuncNode::add_entry_inst(FuncInst * inst)
121 mllnode<FuncInst *> * it;
122 for (it = entry_insts.begin(); it != NULL; it = it->getNext()) {
123 if (inst == it->getVal())
127 entry_insts.push_back(inst);
131 * @brief Convert ModelAdtion list to FuncInst list
132 * @param act_list A list of ModelActions
134 void FuncNode::update_tree(action_list_t * act_list)
136 if (act_list == NULL || act_list->size() == 0)
139 HashTable<void *, value_set_t *, uintptr_t, 4> * write_history = history->getWriteHistory();
141 /* build inst_list from act_list for later processing */
142 func_inst_list_t inst_list;
143 action_list_t rw_act_list;
145 for (sllnode<ModelAction *> * it = act_list->begin(); it != NULL; it = it->getNext()) {
146 ModelAction * act = it->getVal();
147 FuncInst * func_inst = get_inst(act);
148 void * loc = act->get_location();
150 if (func_inst == NULL)
153 inst_list.push_back(func_inst);
154 bool act_added = false;
156 if (act->is_write()) {
157 rw_act_list.push_back(act);
159 if (!write_locations->contains(loc)) {
160 write_locations->add(loc);
161 history->update_loc_wr_func_nodes_map(loc, this);
166 if (act->is_read()) {
168 rw_act_list.push_back(act);
170 /* If func_inst may only read_from a single location, then:
172 * The first time an action reads from some location,
173 * import all the values that have been written to this
174 * location from ModelHistory and notify ModelHistory
175 * that this FuncNode may read from this location.
177 if (!read_locations->contains(loc) && func_inst->is_single_location()) {
178 read_locations->add(loc);
179 value_set_t * write_values = write_history->get(loc);
180 add_to_val_loc_map(write_values, loc);
181 history->update_loc_func_nodes_map(loc, this);
186 // model_print("function %s\n", func_name);
187 // print_val_loc_map();
189 update_inst_tree(&inst_list);
190 update_predicate_tree(&rw_act_list);
192 // print_predicate_tree();
196 * @brief Link FuncInsts in inst_list - add one FuncInst to another's predecessors and successors
197 * @param inst_list A list of FuncInsts
199 void FuncNode::update_inst_tree(func_inst_list_t * inst_list)
201 if (inst_list == NULL)
203 else if (inst_list->size() == 0)
207 sllnode<FuncInst *>* it = inst_list->begin();
208 sllnode<FuncInst *>* prev;
210 /* add the first instruction to the list of entry insts */
211 FuncInst * entry_inst = it->getVal();
212 add_entry_inst(entry_inst);
216 prev = it->getPrev();
218 FuncInst * prev_inst = prev->getVal();
219 FuncInst * curr_inst = it->getVal();
221 prev_inst->add_succ(curr_inst);
222 curr_inst->add_pred(prev_inst);
228 void FuncNode::update_predicate_tree(action_list_t * act_list)
230 if (act_list == NULL || act_list->size() == 0)
233 /* Map a FuncInst to the its predicate */
234 HashTable<FuncInst *, Predicate *, uintptr_t, 0> inst_pred_map(128);
236 // Number FuncInsts to detect loops
237 HashTable<FuncInst *, uint32_t, uintptr_t, 0> inst_id_map(128);
238 uint32_t inst_counter = 0;
240 /* Only need to store the locations of read actions */
241 HashTable<void *, ModelAction *, uintptr_t, 0> loc_act_map(128);
242 HashTable<FuncInst *, ModelAction *, uintptr_t, 0> inst_act_map(128);
244 sllnode<ModelAction *> *it = act_list->begin();
245 Predicate * curr_pred = predicate_tree_entry;
247 ModelAction * next_act = it->getVal();
248 FuncInst * next_inst = get_inst(next_act);
250 SnapVector<Predicate *> unset_predicates = SnapVector<Predicate *>();
251 bool branch_found = follow_branch(&curr_pred, next_inst, next_act, &inst_act_map, &unset_predicates);
253 // A branch with unset predicate expression is detected
254 if (!branch_found && unset_predicates.size() != 0) {
255 ASSERT(unset_predicates.size() == 1);
256 Predicate * one_branch = unset_predicates[0];
258 bool amended = amend_predicate_expr(&curr_pred, next_inst, next_act);
262 curr_pred = one_branch;
268 if (!branch_found && inst_id_map.contains(next_inst)) {
269 FuncInst * curr_inst = curr_pred->get_func_inst();
270 uint32_t curr_id = inst_id_map.get(curr_inst);
271 uint32_t next_id = inst_id_map.get(next_inst);
273 if (curr_id >= next_id) {
274 Predicate * old_pred = inst_pred_map.get(next_inst);
275 Predicate * back_pred = old_pred->get_parent();
277 curr_pred->add_backedge(back_pred);
278 curr_pred = back_pred;
284 // Generate new branches
286 SnapVector<struct half_pred_expr *> half_pred_expressions;
287 infer_predicates(next_inst, next_act, &loc_act_map, &half_pred_expressions);
288 generate_predicates(&curr_pred, next_inst, &half_pred_expressions);
292 if (next_act->is_write())
293 curr_pred->set_write(true);
295 if (next_act->is_read()) {
296 loc_act_map.put(next_act->get_location(), next_act);
299 inst_act_map.put(next_inst, next_act);
300 inst_pred_map.put(next_inst, curr_pred);
301 if (!inst_id_map.contains(next_inst))
302 inst_id_map.put(next_inst, inst_counter++);
308 /* Given curr_pred and next_inst, find the branch following curr_pred that
309 * contains next_inst and the correct predicate.
310 * @return true if branch found, false otherwise.
312 bool FuncNode::follow_branch(Predicate ** curr_pred, FuncInst * next_inst, ModelAction * next_act,
313 HashTable<FuncInst *, ModelAction *, uintptr_t, 0> * inst_act_map,
314 SnapVector<Predicate *> * unset_predicates)
316 /* check if a branch with func_inst and corresponding predicate exists */
317 bool branch_found = false;
318 ModelVector<Predicate *> * branches = (*curr_pred)->get_children();
319 for (uint i = 0; i < branches->size(); i++) {
320 Predicate * branch = (*branches)[i];
321 if (branch->get_func_inst() != next_inst)
324 /* check against predicate expressions */
325 bool predicate_correct = true;
326 PredExprSet * pred_expressions = branch->get_pred_expressions();
327 PredExprSetIter * pred_expr_it = pred_expressions->iterator();
329 /* Only read and rmw actions my have unset predicate expressions */
330 if (pred_expressions->getSize() == 0) {
331 predicate_correct = false;
332 unset_predicates->push_back(branch);
335 while (pred_expr_it->hasNext()) {
336 pred_expr * pred_expression = pred_expr_it->next();
337 uint64_t last_read, next_read;
340 switch(pred_expression->token) {
342 predicate_correct = true;
345 FuncInst * to_be_compared;
346 ModelAction * last_act;
348 to_be_compared = pred_expression->func_inst;
349 last_act = inst_act_map->get(to_be_compared);
351 last_read = last_act->get_reads_from_value();
352 next_read = next_act->get_reads_from_value();
353 equality = (last_read == next_read);
354 if (equality != pred_expression->value)
355 predicate_correct = false;
359 next_read = next_act->get_reads_from_value();
360 equality = ((void*)next_read == NULL);
361 if (equality != pred_expression->value)
362 predicate_correct = false;
365 predicate_correct = false;
366 model_print("unkown predicate token\n");
371 if (predicate_correct) {
381 /* Infer predicate expressions, which are generated in FuncNode::generate_predicates */
382 void FuncNode::infer_predicates(FuncInst * next_inst, ModelAction * next_act,
383 HashTable<void *, ModelAction *, uintptr_t, 0> * loc_act_map,
384 SnapVector<struct half_pred_expr *> * half_pred_expressions)
386 void * loc = next_act->get_location();
388 if (next_inst->is_read()) {
390 if ( loc_act_map->contains(loc) ) {
391 ModelAction * last_act = loc_act_map->get(loc);
392 FuncInst * last_inst = get_inst(last_act);
393 struct half_pred_expr * expression = new half_pred_expr(EQUALITY, last_inst);
394 half_pred_expressions->push_back(expression);
395 } else if ( next_inst->is_single_location() ){
396 loc_set_t * loc_may_equal = loc_may_equal_map->get(loc);
398 if (loc_may_equal != NULL) {
399 loc_set_iter * loc_it = loc_may_equal->iterator();
400 while (loc_it->hasNext()) {
401 void * neighbor = loc_it->next();
402 if (loc_act_map->contains(neighbor)) {
403 ModelAction * last_act = loc_act_map->get(neighbor);
404 FuncInst * last_inst = get_inst(last_act);
406 struct half_pred_expr * expression = new half_pred_expr(EQUALITY, last_inst);
407 half_pred_expressions->push_back(expression);
412 // next_inst is not single location
413 uint64_t read_val = next_act->get_reads_from_value();
415 // only infer NULLITY predicate when it is actually NULL.
416 if ( (void*)read_val == NULL) {
417 struct half_pred_expr * expression = new half_pred_expr(NULLITY, NULL);
418 half_pred_expressions->push_back(expression);
423 // TODO: do anything here?
427 /* Able to generate complex predicates when there are multiple predciate expressions */
428 void FuncNode::generate_predicates(Predicate ** curr_pred, FuncInst * next_inst,
429 SnapVector<struct half_pred_expr *> * half_pred_expressions)
431 if (half_pred_expressions->size() == 0) {
432 Predicate * new_pred = new Predicate(next_inst);
433 (*curr_pred)->add_child(new_pred);
434 new_pred->set_parent(*curr_pred);
436 /* entry predicates and predicates containing pure write actions
437 * have no predicate expressions */
438 if ( (*curr_pred)->is_entry_predicate() )
439 new_pred->add_predicate_expr(NOPREDICATE, NULL, true);
440 else if (next_inst->is_write()) {
441 /* next_inst->is_write() <==> pure writes */
442 new_pred->add_predicate_expr(NOPREDICATE, NULL, true);
448 SnapVector<Predicate *> predicates;
450 struct half_pred_expr * half_expr = (*half_pred_expressions)[0];
451 predicates.push_back(new Predicate(next_inst));
452 predicates.push_back(new Predicate(next_inst));
454 predicates[0]->add_predicate_expr(half_expr->token, half_expr->func_inst, true);
455 predicates[1]->add_predicate_expr(half_expr->token, half_expr->func_inst, false);
457 for (uint i = 1; i < half_pred_expressions->size(); i++) {
458 half_expr = (*half_pred_expressions)[i];
460 uint old_size = predicates.size();
461 for (uint j = 0; j < old_size; j++) {
462 Predicate * pred = predicates[j];
463 Predicate * new_pred = new Predicate(next_inst);
464 new_pred->copy_predicate_expr(pred);
466 pred->add_predicate_expr(half_expr->token, half_expr->func_inst, true);
467 new_pred->add_predicate_expr(half_expr->token, half_expr->func_inst, false);
469 predicates.push_back(new_pred);
473 for (uint i = 0; i < predicates.size(); i++) {
474 Predicate * pred= predicates[i];
475 (*curr_pred)->add_child(pred);
476 pred->set_parent(*curr_pred);
479 /* Free memories allocated by infer_predicate */
480 for (uint i = 0; i < half_pred_expressions->size(); i++) {
481 struct half_pred_expr * tmp = (*half_pred_expressions)[i];
486 /* Amend predicates that contain no predicate expressions. Currenlty only amend with NULLITY predicates */
487 bool FuncNode::amend_predicate_expr(Predicate ** curr_pred, FuncInst * next_inst, ModelAction * next_act)
489 // there should only be only child
490 Predicate * unset_pred = (*curr_pred)->get_children()->back();
491 uint64_t read_val = next_act->get_reads_from_value();
493 // only generate NULLITY predicate when it is actually NULL.
494 if ( !next_inst->is_single_location() && (void*)read_val == NULL ) {
495 Predicate * new_pred = new Predicate(next_inst);
497 (*curr_pred)->add_child(new_pred);
498 new_pred->set_parent(*curr_pred);
500 unset_pred->add_predicate_expr(NULLITY, NULL, false);
501 new_pred->add_predicate_expr(NULLITY, NULL, true);
509 void FuncNode::add_to_val_loc_map(uint64_t val, void * loc)
511 loc_set_t * locations = val_loc_map->get(val);
513 if (locations == NULL) {
514 locations = new loc_set_t();
515 val_loc_map->put(val, locations);
518 update_loc_may_equal_map(loc, locations);
520 // values_may_read_from->add(val);
523 void FuncNode::add_to_val_loc_map(value_set_t * values, void * loc)
528 value_set_iter * it = values->iterator();
529 while (it->hasNext()) {
530 uint64_t val = it->next();
531 add_to_val_loc_map(val, loc);
535 void FuncNode::update_loc_may_equal_map(void * new_loc, loc_set_t * old_locations)
537 if ( old_locations->contains(new_loc) )
540 loc_set_t * neighbors = loc_may_equal_map->get(new_loc);
542 if (neighbors == NULL) {
543 neighbors = new loc_set_t();
544 loc_may_equal_map->put(new_loc, neighbors);
547 loc_set_iter * loc_it = old_locations->iterator();
548 while (loc_it->hasNext()) {
549 // new_loc: { old_locations, ... }
550 void * member = loc_it->next();
551 neighbors->add(member);
553 // for each i in old_locations, i : { new_loc, ... }
554 loc_set_t * _neighbors = loc_may_equal_map->get(member);
555 if (_neighbors == NULL) {
556 _neighbors = new loc_set_t();
557 loc_may_equal_map->put(member, _neighbors);
559 _neighbors->add(new_loc);
563 /* Every time a thread enters a function, set its position to the predicate tree entry */
564 void FuncNode::init_predicate_tree_position(thread_id_t tid)
566 int thread_id = id_to_int(tid);
567 if (predicate_tree_position.size() <= (uint) thread_id)
568 predicate_tree_position.resize(thread_id + 1);
570 predicate_tree_position[thread_id] = predicate_tree_entry;
573 void FuncNode::set_predicate_tree_position(thread_id_t tid, Predicate * pred)
575 int thread_id = id_to_int(tid);
576 predicate_tree_position[thread_id] = pred;
579 /* @return The position of a thread in a predicate tree */
580 Predicate * FuncNode::get_predicate_tree_position(thread_id_t tid)
582 int thread_id = id_to_int(tid);
583 return predicate_tree_position[thread_id];
586 /* Make sure elements of thrd_inst_act_map are initialized properly when threads enter functions */
587 void FuncNode::init_inst_act_map(thread_id_t tid)
589 int thread_id = id_to_int(tid);
590 uint old_size = thrd_inst_act_map->size();
592 if (thrd_inst_act_map->size() <= (uint) thread_id) {
593 uint new_size = thread_id + 1;
594 thrd_inst_act_map->resize(new_size);
596 for (uint i = old_size; i < new_size; i++)
597 (*thrd_inst_act_map)[i] = new inst_act_map_t(128);
601 /* Reset elements of thrd_inst_act_map when threads exit functions */
602 void FuncNode::reset_inst_act_map(thread_id_t tid)
604 int thread_id = id_to_int(tid);
605 inst_act_map_t * map = (*thrd_inst_act_map)[thread_id];
609 void FuncNode::update_inst_act_map(thread_id_t tid, ModelAction * read_act)
611 int thread_id = id_to_int(tid);
612 inst_act_map_t * map = (*thrd_inst_act_map)[thread_id];
613 FuncInst * read_inst = get_inst(read_act);
614 map->put(read_inst, read_act);
617 inst_act_map_t * FuncNode::get_inst_act_map(thread_id_t tid)
619 int thread_id = id_to_int(tid);
620 return (*thrd_inst_act_map)[thread_id];
623 /* Add FuncNodes that this node may follow */
624 void FuncNode::add_out_edge(FuncNode * other)
626 if ( !edge_table.contains(other) ) {
627 edge_table.put(other, OUT_EDGE);
628 out_edges.push_back(other);
632 edge_type_t edge = edge_table.get(other);
633 if (edge == IN_EDGE) {
634 edge_table.put(other, BI_EDGE);
635 out_edges.push_back(other);
639 void FuncNode::print_predicate_tree()
641 model_print("digraph function_%s {\n", func_name);
642 predicate_tree_entry->print_pred_subtree();
643 model_print("}\n"); // end of graph
646 void FuncNode::print_val_loc_map()
649 value_set_iter * val_it = values_may_read_from->iterator();
650 while (val_it->hasNext()) {
651 uint64_t value = val_it->next();
652 model_print("val %llx: ", value);
654 loc_set_t * locations = val_loc_map->get(value);
655 loc_set_iter * loc_it = locations->iterator();
656 while (loc_it->hasNext()) {
657 void * location = loc_it->next();
658 model_print("%p ", location);