3 FuncNode::FuncNode(ModelHistory * history) :
9 predicate_tree_position(),
14 predicate_tree_entry = new Predicate(NULL, true);
15 predicate_tree_entry->add_predicate_expr(NOPREDICATE, NULL, true);
17 // memories that are reclaimed after each execution
18 action_list_buffer = new SnapList<action_list_t *>();
19 read_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 val_loc_map = new HashTable<uint64_t, loc_set_t *, uint64_t, 0>();
38 loc_may_equal_map = new HashTable<void *, loc_set_t *, uintptr_t, 0>();
39 thrd_inst_act_map = new SnapVector<inst_act_map_t *>();
41 //values_may_read_from = new value_set_t();
44 /* Check whether FuncInst with the same type, position, and location
45 * as act has been added to func_inst_map or not. If not, add it.
47 * Note: currently, actions with the same position are filtered out by process_action,
48 * so the collision list of FuncInst is not used. May remove it later.
50 void FuncNode::add_inst(ModelAction *act)
53 const char * position = act->get_position();
55 /* THREAD* actions, ATOMIC_LOCK, ATOMIC_TRYLOCK, and ATOMIC_UNLOCK
56 * actions are not tagged with their source line numbers
61 if ( func_inst_map.contains(position) ) {
62 FuncInst * inst = func_inst_map.get(position);
64 ASSERT(inst->get_type() == act->get_type());
66 // locations are set to NULL when new executions start
67 if (inst->get_location() == NULL)
68 inst->set_location(act->get_location());
70 if (inst->get_location() != act->get_location())
71 inst->not_single_location();
76 FuncInst * func_inst = new FuncInst(act, this);
78 func_inst_map.put(position, func_inst);
79 inst_list.push_back(func_inst);
82 /* Get the FuncInst with the same type, position, and location
85 * @return FuncInst with the same type, position, and location as act */
86 FuncInst * FuncNode::get_inst(ModelAction *act)
89 const char * position = act->get_position();
91 /* THREAD* actions, ATOMIC_LOCK, ATOMIC_TRYLOCK, and ATOMIC_UNLOCK
92 * actions are not tagged with their source line numbers
97 FuncInst * inst = func_inst_map.get(position);
101 action_type inst_type = inst->get_type();
102 action_type act_type = act->get_type();
104 // else if branch: an RMWRCAS action is converted to a RMW or READ action
105 if (inst_type == act_type)
107 else if (inst_type == ATOMIC_RMWRCAS &&
108 (act_type == ATOMIC_RMW || act_type == ATOMIC_READ))
115 void FuncNode::add_entry_inst(FuncInst * inst)
120 mllnode<FuncInst *> * it;
121 for (it = entry_insts.begin(); it != NULL; it = it->getNext()) {
122 if (inst == it->getVal())
126 entry_insts.push_back(inst);
130 * @brief Convert ModelAdtion list to FuncInst list
131 * @param act_list A list of ModelActions
133 void FuncNode::update_tree(action_list_t * act_list)
135 if (act_list == NULL || act_list->size() == 0)
138 HashTable<void *, value_set_t *, uintptr_t, 4> * write_history = history->getWriteHistory();
140 /* build inst_list from act_list for later processing */
141 func_inst_list_t inst_list;
142 action_list_t rw_act_list;
144 for (sllnode<ModelAction *> * it = act_list->begin(); it != NULL; it = it->getNext()) {
145 ModelAction * act = it->getVal();
146 FuncInst * func_inst = get_inst(act);
148 if (func_inst == NULL)
151 inst_list.push_back(func_inst);
153 /* NOTE: for rmw actions, func_inst and act may have different
154 * action types because of action type conversion in ModelExecution
155 * func_inst->is_write() <==> pure writes (excluding rmw) */
156 if (func_inst->is_write()) {
157 // model_print("write detected\n");
158 rw_act_list.push_back(act);
161 /* func_inst->is_read() <==> read + rmw */
162 if (func_inst->is_read()) {
163 rw_act_list.push_back(act);
164 /* If func_inst may only read_from a single location, then:
166 * The first time an action reads from some location,
167 * import all the values that have been written to this
168 * location from ModelHistory and notify ModelHistory
169 * that this FuncNode may read from this location.
171 void * loc = act->get_location();
172 if (!read_locations->contains(loc) && func_inst->is_single_location()) {
173 read_locations->add(loc);
174 value_set_t * write_values = write_history->get(loc);
175 add_to_val_loc_map(write_values, loc);
176 history->add_to_loc_func_nodes_map(loc, this);
181 // model_print("function %s\n", func_name);
182 // print_val_loc_map();
184 update_inst_tree(&inst_list);
185 update_predicate_tree(&rw_act_list);
187 // print_predicate_tree();
191 * @brief Link FuncInsts in inst_list - add one FuncInst to another's predecessors and successors
192 * @param inst_list A list of FuncInsts
194 void FuncNode::update_inst_tree(func_inst_list_t * inst_list)
196 if (inst_list == NULL)
198 else if (inst_list->size() == 0)
202 sllnode<FuncInst *>* it = inst_list->begin();
203 sllnode<FuncInst *>* prev;
205 /* add the first instruction to the list of entry insts */
206 FuncInst * entry_inst = it->getVal();
207 add_entry_inst(entry_inst);
211 prev = it->getPrev();
213 FuncInst * prev_inst = prev->getVal();
214 FuncInst * curr_inst = it->getVal();
216 prev_inst->add_succ(curr_inst);
217 curr_inst->add_pred(prev_inst);
223 void FuncNode::update_predicate_tree(action_list_t * act_list)
225 if (act_list == NULL || act_list->size() == 0)
228 /* map a FuncInst to the its predicate */
229 HashTable<FuncInst *, Predicate *, uintptr_t, 0> inst_pred_map(128);
231 // number FuncInsts to detect loops
232 HashTable<FuncInst *, uint32_t, uintptr_t, 0> inst_id_map(128);
233 uint32_t inst_counter = 0;
235 HashTable<void *, ModelAction *, uintptr_t, 0> loc_act_map(128);
236 HashTable<FuncInst *, ModelAction *, uintptr_t, 0> inst_act_map(128);
238 sllnode<ModelAction *> *it = act_list->begin();
239 Predicate * curr_pred = predicate_tree_entry;
241 ModelAction * next_act = it->getVal();
242 FuncInst * next_inst = get_inst(next_act);
244 SnapVector<Predicate *> unset_predicates = SnapVector<Predicate *>();
245 bool branch_found = follow_branch(&curr_pred, next_inst, next_act, &inst_act_map, &unset_predicates);
247 // A branch with unset predicate expression is detected
248 if (!branch_found && unset_predicates.size() != 0) {
249 ASSERT(unset_predicates.size() == 1);
250 Predicate * one_branch = unset_predicates[0];
252 bool amended = amend_predicate_expr(&curr_pred, next_inst, next_act);
256 curr_pred = one_branch;
262 if (!branch_found && inst_id_map.contains(next_inst)) {
263 FuncInst * curr_inst = curr_pred->get_func_inst();
264 uint32_t curr_id = inst_id_map.get(curr_inst);
265 uint32_t next_id = inst_id_map.get(next_inst);
267 if (curr_id >= next_id) {
268 Predicate * old_pred = inst_pred_map.get(next_inst);
269 Predicate * back_pred = old_pred->get_parent();
271 curr_pred->add_backedge(back_pred);
272 curr_pred = back_pred;
278 // Generate new branches
280 SnapVector<struct half_pred_expr *> half_pred_expressions;
281 infer_predicates(next_inst, next_act, &loc_act_map, &half_pred_expressions);
282 generate_predicates(&curr_pred, next_inst, &half_pred_expressions);
286 if (next_act->is_write())
287 curr_pred->set_write(true);
289 inst_pred_map.put(next_inst, curr_pred);
290 if (!inst_id_map.contains(next_inst))
291 inst_id_map.put(next_inst, inst_counter++);
293 loc_act_map.put(next_act->get_location(), next_act);
294 inst_act_map.put(next_inst, next_act);
299 /* Given curr_pred and next_inst, find the branch following curr_pred that
300 * contains next_inst and the correct predicate.
301 * @return true if branch found, false otherwise.
303 bool FuncNode::follow_branch(Predicate ** curr_pred, FuncInst * next_inst, ModelAction * next_act,
304 HashTable<FuncInst *, ModelAction *, uintptr_t, 0> * inst_act_map,
305 SnapVector<Predicate *> * unset_predicates)
307 /* check if a branch with func_inst and corresponding predicate exists */
308 bool branch_found = false;
309 ModelVector<Predicate *> * branches = (*curr_pred)->get_children();
310 for (uint i = 0; i < branches->size(); i++) {
311 Predicate * branch = (*branches)[i];
312 if (branch->get_func_inst() != next_inst)
315 /* check against predicate expressions */
316 bool predicate_correct = true;
317 PredExprSet * pred_expressions = branch->get_pred_expressions();
318 PredExprSetIter * pred_expr_it = pred_expressions->iterator();
320 /* Only read and rmw actions my have unset predicate expressions */
321 if (pred_expressions->getSize() == 0) {
322 predicate_correct = false;
323 unset_predicates->push_back(branch);
326 while (pred_expr_it->hasNext()) {
327 pred_expr * pred_expression = pred_expr_it->next();
328 uint64_t last_read, next_read;
331 switch(pred_expression->token) {
333 predicate_correct = true;
336 FuncInst * to_be_compared;
337 ModelAction * last_act;
339 to_be_compared = pred_expression->func_inst;
340 last_act = inst_act_map->get(to_be_compared);
342 last_read = last_act->get_reads_from_value();
343 next_read = next_act->get_reads_from_value();
344 equality = (last_read == next_read);
345 if (equality != pred_expression->value)
346 predicate_correct = false;
350 next_read = next_act->get_reads_from_value();
351 equality = ((void*)next_read == NULL);
352 if (equality != pred_expression->value)
353 predicate_correct = false;
356 predicate_correct = false;
357 model_print("unkown predicate token\n");
362 if (predicate_correct) {
372 /* Infer predicate expressions, which are generated in FuncNode::generate_predicates */
373 void FuncNode::infer_predicates(FuncInst * next_inst, ModelAction * next_act,
374 HashTable<void *, ModelAction *, uintptr_t, 0> * loc_act_map,
375 SnapVector<struct half_pred_expr *> * half_pred_expressions)
377 void * loc = next_act->get_location();
379 if (next_inst->is_read()) {
381 if ( loc_act_map->contains(loc) ) {
382 ModelAction * last_act = loc_act_map->get(loc);
383 FuncInst * last_inst = get_inst(last_act);
384 struct half_pred_expr * expression = new half_pred_expr(EQUALITY, last_inst);
385 half_pred_expressions->push_back(expression);
386 } else if ( next_inst->is_single_location() ){
387 loc_set_t * loc_may_equal = loc_may_equal_map->get(loc);
389 if (loc_may_equal != NULL) {
390 loc_set_iter * loc_it = loc_may_equal->iterator();
391 while (loc_it->hasNext()) {
392 void * neighbor = loc_it->next();
393 if (loc_act_map->contains(neighbor)) {
394 ModelAction * last_act = loc_act_map->get(neighbor);
395 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);
403 // next_inst is not single location
404 uint64_t read_val = next_act->get_reads_from_value();
406 // only infer NULLITY predicate when it is actually NULL.
407 if ( (void*)read_val == NULL) {
408 struct half_pred_expr * expression = new half_pred_expr(NULLITY, NULL);
409 half_pred_expressions->push_back(expression);
414 // TODO: do anything here?
418 /* Able to generate complex predicates when there are multiple predciate expressions */
419 void FuncNode::generate_predicates(Predicate ** curr_pred, FuncInst * next_inst,
420 SnapVector<struct half_pred_expr *> * half_pred_expressions)
422 if (half_pred_expressions->size() == 0) {
423 Predicate * new_pred = new Predicate(next_inst);
424 (*curr_pred)->add_child(new_pred);
425 new_pred->set_parent(*curr_pred);
427 /* entry predicates and predicates containing pure write actions
428 * have no predicate expressions */
429 if ( (*curr_pred)->is_entry_predicate() )
430 new_pred->add_predicate_expr(NOPREDICATE, NULL, true);
431 else if (next_inst->is_write()) {
432 /* next_inst->is_write() <==> pure writes */
433 new_pred->add_predicate_expr(NOPREDICATE, NULL, true);
439 SnapVector<Predicate *> predicates;
441 struct half_pred_expr * half_expr = (*half_pred_expressions)[0];
442 predicates.push_back(new Predicate(next_inst));
443 predicates.push_back(new Predicate(next_inst));
445 predicates[0]->add_predicate_expr(half_expr->token, half_expr->func_inst, true);
446 predicates[1]->add_predicate_expr(half_expr->token, half_expr->func_inst, false);
448 for (uint i = 1; i < half_pred_expressions->size(); i++) {
449 half_expr = (*half_pred_expressions)[i];
451 uint old_size = predicates.size();
452 for (uint j = 0; j < old_size; j++) {
453 Predicate * pred = predicates[j];
454 Predicate * new_pred = new Predicate(next_inst);
455 new_pred->copy_predicate_expr(pred);
457 pred->add_predicate_expr(half_expr->token, half_expr->func_inst, true);
458 new_pred->add_predicate_expr(half_expr->token, half_expr->func_inst, false);
460 predicates.push_back(new_pred);
464 for (uint i = 0; i < predicates.size(); i++) {
465 Predicate * pred= predicates[i];
466 (*curr_pred)->add_child(pred);
467 pred->set_parent(*curr_pred);
471 /* Amend predicates that contain no predicate expressions. Currenlty only amend with NULLITY predicates */
472 bool FuncNode::amend_predicate_expr(Predicate ** curr_pred, FuncInst * next_inst, ModelAction * next_act)
474 // there should only be only child
475 Predicate * unset_pred = (*curr_pred)->get_children()->back();
476 uint64_t read_val = next_act->get_reads_from_value();
478 // only generate NULLITY predicate when it is actually NULL.
479 if ( !next_inst->is_single_location() && (void*)read_val == NULL ) {
480 Predicate * new_pred = new Predicate(next_inst);
482 (*curr_pred)->add_child(new_pred);
483 new_pred->set_parent(*curr_pred);
485 unset_pred->add_predicate_expr(NULLITY, NULL, false);
486 new_pred->add_predicate_expr(NULLITY, NULL, true);
494 void FuncNode::add_to_val_loc_map(uint64_t val, void * loc)
496 loc_set_t * locations = val_loc_map->get(val);
498 if (locations == NULL) {
499 locations = new loc_set_t();
500 val_loc_map->put(val, locations);
503 update_loc_may_equal_map(loc, locations);
505 // values_may_read_from->add(val);
508 void FuncNode::add_to_val_loc_map(value_set_t * values, void * loc)
513 value_set_iter * it = values->iterator();
514 while (it->hasNext()) {
515 uint64_t val = it->next();
516 add_to_val_loc_map(val, loc);
520 void FuncNode::update_loc_may_equal_map(void * new_loc, loc_set_t * old_locations)
522 if ( old_locations->contains(new_loc) )
525 loc_set_t * neighbors = loc_may_equal_map->get(new_loc);
527 if (neighbors == NULL) {
528 neighbors = new loc_set_t();
529 loc_may_equal_map->put(new_loc, neighbors);
532 loc_set_iter * loc_it = old_locations->iterator();
533 while (loc_it->hasNext()) {
534 // new_loc: { old_locations, ... }
535 void * member = loc_it->next();
536 neighbors->add(member);
538 // for each i in old_locations, i : { new_loc, ... }
539 loc_set_t * _neighbors = loc_may_equal_map->get(member);
540 if (_neighbors == NULL) {
541 _neighbors = new loc_set_t();
542 loc_may_equal_map->put(member, _neighbors);
544 _neighbors->add(new_loc);
548 /* Every time a thread enters a function, set its position to the predicate tree entry */
549 void FuncNode::init_predicate_tree_position(thread_id_t tid)
551 int thread_id = id_to_int(tid);
552 if (predicate_tree_position.size() <= (uint) thread_id)
553 predicate_tree_position.resize(thread_id + 1);
555 predicate_tree_position[thread_id] = predicate_tree_entry;
558 void FuncNode::set_predicate_tree_position(thread_id_t tid, Predicate * pred)
560 int thread_id = id_to_int(tid);
561 predicate_tree_position[thread_id] = pred;
564 /* @return The position of a thread in a predicate tree */
565 Predicate * FuncNode::get_predicate_tree_position(thread_id_t tid)
567 int thread_id = id_to_int(tid);
568 return predicate_tree_position[thread_id];
571 /* Make sure elements of thrd_inst_act_map are initialized properly when threads enter functions */
572 void FuncNode::init_inst_act_map(thread_id_t tid)
574 int thread_id = id_to_int(tid);
575 uint old_size = thrd_inst_act_map->size();
577 if (thrd_inst_act_map->size() <= (uint) thread_id) {
578 uint new_size = thread_id + 1;
579 thrd_inst_act_map->resize(new_size);
581 for (uint i = old_size; i < new_size; i++)
582 (*thrd_inst_act_map)[i] = new inst_act_map_t(128);
586 /* Reset elements of thrd_inst_act_map when threads exit functions */
587 void FuncNode::reset_inst_act_map(thread_id_t tid)
589 int thread_id = id_to_int(tid);
590 inst_act_map_t * map = (*thrd_inst_act_map)[thread_id];
594 void FuncNode::update_inst_act_map(thread_id_t tid, ModelAction * read_act)
596 int thread_id = id_to_int(tid);
597 inst_act_map_t * map = (*thrd_inst_act_map)[thread_id];
598 FuncInst * read_inst = get_inst(read_act);
599 map->put(read_inst, read_act);
602 inst_act_map_t * FuncNode::get_inst_act_map(thread_id_t tid)
604 int thread_id = id_to_int(tid);
605 return (*thrd_inst_act_map)[thread_id];
608 /* Add FuncNodes that this node may follow */
609 void FuncNode::add_out_edge(FuncNode * other)
611 if ( !edge_table.contains(other) ) {
612 edge_table.put(other, OUT_EDGE);
613 out_edges.push_back(other);
617 edge_type_t edge = edge_table.get(other);
618 if (edge == IN_EDGE) {
619 edge_table.put(other, BI_EDGE);
620 out_edges.push_back(other);
624 /* Add FuncNodes that come before this node */
625 void FuncNode::add_in_edge(FuncNode * other)
627 if ( !edge_table.contains(other) ) {
628 edge_table.put(other, IN_EDGE);
629 in_edges.push_back(other);
633 edge_type_t edge = edge_table.get(other);
634 if (edge == OUT_EDGE) {
635 edge_table.put(other, BI_EDGE);
636 in_edges.push_back(other);
641 void FuncNode::print_predicate_tree()
643 model_print("digraph function_%s {\n", func_name);
644 predicate_tree_entry->print_pred_subtree();
645 model_print("}\n"); // end of graph
648 void FuncNode::print_val_loc_map()
651 value_set_iter * val_it = values_may_read_from->iterator();
652 while (val_it->hasNext()) {
653 uint64_t value = val_it->next();
654 model_print("val %llx: ", value);
656 loc_set_t * locations = val_loc_map->get(value);
657 loc_set_iter * loc_it = locations->iterator();
658 while (loc_it->hasNext()) {
659 void * location = loc_it->next();
660 model_print("%p ", location);