3 FuncNode::FuncNode(ModelHistory * history) :
5 predicate_tree_initialized(false),
13 predicate_tree_entry = new Predicate(NULL, true);
14 predicate_tree_entry->add_predicate_expr(NOPREDICATE, NULL, true);
16 // memory will be reclaimed after each execution
17 read_locations = new loc_set_t();
18 val_loc_map = new HashTable<uint64_t, loc_set_t *, uint64_t, 0>();
19 loc_may_equal_map = new HashTable<void *, loc_set_t *, uintptr_t, 0>();
20 values_may_read_from = new value_set_t();
23 /* Reallocate some snapshotted memories when new executions start */
24 void FuncNode::set_new_exec_flag()
26 // for (uint i = 0; i < thrd_read_map.size(); i++)
27 // thrd_read_map[i] = new read_map_t();
29 for (mllnode<FuncInst *> * it = inst_list.begin(); it != NULL; it = it->getNext()) {
30 FuncInst * inst = it->getVal();
31 inst->reset_location();
34 read_locations = new loc_set_t();
35 val_loc_map = new HashTable<uint64_t, loc_set_t *, uint64_t, 0>();
36 loc_may_equal_map = new HashTable<void *, loc_set_t *, uintptr_t, 0>();
37 values_may_read_from = new value_set_t();
40 /* Check whether FuncInst with the same type, position, and location
41 * as act has been added to func_inst_map or not. If not, add it.
43 * Note: currently, actions with the same position are filtered out by process_action,
44 * so the collision list of FuncInst is not used. May remove it later.
46 void FuncNode::add_inst(ModelAction *act)
49 const char * position = act->get_position();
51 /* THREAD* actions, ATOMIC_LOCK, ATOMIC_TRYLOCK, and ATOMIC_UNLOCK
52 * actions are not tagged with their source line numbers
57 if ( func_inst_map.contains(position) ) {
58 FuncInst * inst = func_inst_map.get(position);
60 ASSERT(inst->get_type() == act->get_type());
62 // locations are set to NULL when new executions start
63 if (inst->get_location() == NULL)
64 inst->set_location(act->get_location());
66 if (inst->get_location() != act->get_location())
67 inst->not_single_location();
72 FuncInst * func_inst = new FuncInst(act, this);
74 func_inst_map.put(position, func_inst);
75 inst_list.push_back(func_inst);
78 /* Get the FuncInst with the same type, position, and location
81 * @return FuncInst with the same type, position, and location as act */
82 FuncInst * FuncNode::get_inst(ModelAction *act)
85 const char * position = act->get_position();
87 /* THREAD* actions, ATOMIC_LOCK, ATOMIC_TRYLOCK, and ATOMIC_UNLOCK
88 * actions are not tagged with their source line numbers
93 FuncInst * inst = func_inst_map.get(position);
97 action_type inst_type = inst->get_type();
98 action_type act_type = act->get_type();
100 // else if branch: an RMWRCAS action is converted to a RMW or READ action
101 if (inst_type == act_type)
103 else if (inst_type == ATOMIC_RMWRCAS &&
104 (act_type == ATOMIC_RMW || act_type == ATOMIC_READ))
111 void FuncNode::add_entry_inst(FuncInst * inst)
116 mllnode<FuncInst *> * it;
117 for (it = entry_insts.begin(); it != NULL; it = it->getNext()) {
118 if (inst == it->getVal())
122 entry_insts.push_back(inst);
126 * @brief Convert ModelAdtion list to FuncInst list
127 * @param act_list A list of ModelActions
129 void FuncNode::update_tree(action_list_t * act_list)
131 if (act_list == NULL || act_list->size() == 0)
134 HashTable<void *, value_set_t *, uintptr_t, 4> * write_history = history->getWriteHistory();
136 /* build inst_list from act_list for later processing */
137 func_inst_list_t inst_list;
138 action_list_t read_act_list;
140 for (sllnode<ModelAction *> * it = act_list->begin(); it != NULL; it = it->getNext()) {
141 ModelAction * act = it->getVal();
142 FuncInst * func_inst = get_inst(act);
144 if (func_inst == NULL)
147 inst_list.push_back(func_inst);
149 if (func_inst->is_read()) {
150 read_act_list.push_back(act);
152 /* the first time an action reads from some location, import all the values that have
153 * been written to this location from ModelHistory and notify ModelHistory that this
154 * FuncNode may read from this location.
156 void * loc = act->get_location();
157 if (!read_locations->contains(loc)) {
158 read_locations->add(loc);
159 value_set_t * write_values = write_history->get(loc);
160 add_to_val_loc_map(write_values, loc);
161 history->add_to_loc_func_nodes_map(loc, this);
166 model_print("function %s\n", func_name);
167 // print_val_loc_map();
169 update_inst_tree(&inst_list);
170 update_predicate_tree(&read_act_list);
171 // deep_update(predicate_tree_entry);
173 print_predicate_tree();
177 * @brief Link FuncInsts in inst_list - add one FuncInst to another's predecessors and successors
178 * @param inst_list A list of FuncInsts
180 void FuncNode::update_inst_tree(func_inst_list_t * inst_list)
182 if (inst_list == NULL)
184 else if (inst_list->size() == 0)
188 sllnode<FuncInst *>* it = inst_list->begin();
189 sllnode<FuncInst *>* prev;
191 /* add the first instruction to the list of entry insts */
192 FuncInst * entry_inst = it->getVal();
193 add_entry_inst(entry_inst);
197 prev = it->getPrev();
199 FuncInst * prev_inst = prev->getVal();
200 FuncInst * curr_inst = it->getVal();
202 prev_inst->add_succ(curr_inst);
203 curr_inst->add_pred(prev_inst);
209 /* @param tid thread id
210 * Store the values read by atomic read actions into thrd_read_map */
211 void FuncNode::store_read(ModelAction * act, uint32_t tid)
216 void * location = act->get_location();
217 uint64_t read_from_val = act->get_reads_from_value();
219 // resize and initialize
220 uint32_t old_size = thrd_read_map.size();
221 if (old_size <= tid) {
222 thrd_read_map.resize(tid + 1);
223 for (uint32_t i = old_size; i < tid + 1;i++)
224 thrd_read_map[i] = new read_map_t();
227 read_map_t * read_map = thrd_read_map[tid];
228 read_map->put(location, read_from_val);
232 uint64_t FuncNode::query_last_read(void * location, uint32_t tid)
235 if (thrd_read_map.size() <= tid)
238 read_map_t * read_map = thrd_read_map[tid];
240 // last read value not found
241 if ( !read_map->contains(location) )
244 uint64_t read_val = read_map->get(location);
249 /* @param tid thread id
250 * Reset read map for a thread. This function shall only be called
251 * when a thread exits a function
253 void FuncNode::clear_read_map(uint32_t tid)
256 if (thrd_read_map.size() <= tid)
259 thrd_read_map[tid]->reset();
263 void FuncNode::update_predicate_tree(action_list_t * act_list)
265 if (act_list == NULL || act_list->size() == 0)
268 /* map a FuncInst to the its predicate */
269 HashTable<FuncInst *, Predicate *, uintptr_t, 0> inst_pred_map(128);
271 // number FuncInsts to detect loops
272 HashTable<FuncInst *, uint32_t, uintptr_t, 0> inst_id_map(128);
273 uint32_t inst_counter = 0;
275 HashTable<void *, ModelAction *, uintptr_t, 0> loc_act_map(128);
276 HashTable<FuncInst *, ModelAction *, uintptr_t, 0> inst_act_map(128);
278 sllnode<ModelAction *> *it = act_list->begin();
279 Predicate * curr_pred = predicate_tree_entry;
281 ModelAction * next_act = it->getVal();
282 FuncInst * next_inst = get_inst(next_act);
283 SnapVector<Predicate *> * unset_predicates = new SnapVector<Predicate *>();
285 bool branch_found = follow_branch(&curr_pred, next_inst, next_act, &inst_act_map, unset_predicates);
287 // no predicate expressions, follow the only branch
288 if (!branch_found && unset_predicates->size() != 0) {
289 ASSERT(unset_predicates->size() == 1);
290 Predicate * one_branch = (*unset_predicates)[0];
291 curr_pred = one_branch;
295 delete unset_predicates;
298 if (!branch_found && inst_id_map.contains(next_inst)) {
299 FuncInst * curr_inst = curr_pred->get_func_inst();
300 uint32_t curr_id = inst_id_map.get(curr_inst);
301 uint32_t next_id = inst_id_map.get(next_inst);
303 if (curr_id >= next_id) {
304 Predicate * old_pred = inst_pred_map.get(next_inst);
305 Predicate * back_pred = old_pred->get_parent();
307 curr_pred->add_backedge(back_pred);
308 curr_pred = back_pred;
314 // generate new branches
316 SnapVector<struct half_pred_expr *> half_pred_expressions;
317 void * loc = next_act->get_location();
319 if ( loc_act_map.contains(loc) ) {
320 ModelAction * last_act = loc_act_map.get(loc);
321 FuncInst * last_inst = get_inst(last_act);
322 struct half_pred_expr * expression = new half_pred_expr(EQUALITY, last_inst);
323 half_pred_expressions.push_back(expression);
324 } else if ( next_inst->is_single_location() ){
325 loc_set_t * loc_may_equal = loc_may_equal_map->get(loc);
327 if (loc_may_equal != NULL) {
328 loc_set_iter * loc_it = loc_may_equal->iterator();
329 while (loc_it->hasNext()) {
330 void * neighbor = loc_it->next();
331 if (loc_act_map.contains(neighbor)) {
332 ModelAction * last_act = loc_act_map.get(neighbor);
333 FuncInst * last_inst = get_inst(last_act);
334 struct half_pred_expr * expression = new half_pred_expr(EQUALITY, last_inst);
335 half_pred_expressions.push_back(expression);
340 // next_inst is not single location
341 struct half_pred_expr * expression = new half_pred_expr(NULLITY, NULL);
342 half_pred_expressions.push_back(expression);
345 if (half_pred_expressions.size() == 0) {
346 // no predicate needs to be generated
347 Predicate * new_pred = new Predicate(next_inst);
348 curr_pred->add_child(new_pred);
349 new_pred->set_parent(curr_pred);
351 if (curr_pred->is_entry_predicate())
352 new_pred->add_predicate_expr(NOPREDICATE, NULL, true);
354 curr_pred = new_pred;
356 generate_predicate(&curr_pred, next_inst, &half_pred_expressions);
357 bool branch_found = follow_branch(&curr_pred, next_inst, next_act, &inst_act_map, NULL);
358 ASSERT(branch_found);
362 inst_pred_map.put(next_inst, curr_pred);
363 if (!inst_id_map.contains(next_inst))
364 inst_id_map.put(next_inst, inst_counter++);
366 loc_act_map.put(next_act->get_location(), next_act);
367 inst_act_map.put(next_inst, next_act);
372 void FuncNode::deep_update(Predicate * curr_pred)
374 FuncInst * func_inst = curr_pred->get_func_inst();
375 if (func_inst != NULL && !func_inst->is_single_location()) {
376 bool has_null_pred = false;
377 PredExprSet * pred_expressions = curr_pred->get_pred_expressions();
378 PredExprSetIter * pred_expr_it = pred_expressions->iterator();
379 while (pred_expr_it->hasNext()) {
380 pred_expr * pred_expression = pred_expr_it->next();
381 if (pred_expression->token == NULLITY) {
382 has_null_pred = true;
387 if (!has_null_pred) {
388 // func_inst->print();
389 Predicate * another_branch = new Predicate(func_inst);
390 another_branch->copy_predicate_expr(curr_pred);
391 another_branch->add_predicate_expr(NULLITY, NULL, 1);
392 curr_pred->add_predicate_expr(NULLITY, NULL, 0);
394 Predicate * parent = curr_pred->get_parent();
395 parent->add_child(another_branch);
399 ModelVector<Predicate *> * branches = curr_pred->get_children();
400 for (uint i = 0; i < branches->size(); i++) {
401 Predicate * branch = (*branches)[i];
406 /* Given curr_pred and next_inst, find the branch following curr_pred that
407 * contains next_inst and the correct predicate.
408 * @return true if branch found, false otherwise.
410 bool FuncNode::follow_branch(Predicate ** curr_pred, FuncInst * next_inst, ModelAction * next_act,
411 HashTable<FuncInst *, ModelAction *, uintptr_t, 0> * inst_act_map,
412 SnapVector<Predicate *> * unset_predicates)
414 /* check if a branch with func_inst and corresponding predicate exists */
415 bool branch_found = false;
416 ModelVector<Predicate *> * branches = (*curr_pred)->get_children();
417 for (uint i = 0; i < branches->size(); i++) {
418 Predicate * branch = (*branches)[i];
419 if (branch->get_func_inst() != next_inst)
422 /* check against predicate expressions */
423 bool predicate_correct = true;
424 PredExprSet * pred_expressions = branch->get_pred_expressions();
425 PredExprSetIter * pred_expr_it = pred_expressions->iterator();
427 if (pred_expressions->getSize() == 0) {
428 predicate_correct = false;
429 unset_predicates->push_back(branch);
432 while (pred_expr_it->hasNext()) {
433 pred_expr * pred_expression = pred_expr_it->next();
434 uint64_t last_read, next_read;
437 switch(pred_expression->token) {
439 predicate_correct = true;
442 FuncInst * to_be_compared;
443 ModelAction * last_act;
445 to_be_compared = pred_expression->func_inst;
446 last_act = inst_act_map->get(to_be_compared);
448 last_read = last_act->get_reads_from_value();
449 next_read = next_act->get_reads_from_value();
450 equality = (last_read == next_read);
451 if (equality != pred_expression->value)
452 predicate_correct = false;
456 next_read = next_act->get_reads_from_value();
457 equality = ((void*)next_read == NULL);
458 if (equality != pred_expression->value)
459 predicate_correct = false;
462 predicate_correct = false;
463 model_print("unkown predicate token\n");
468 if (predicate_correct) {
478 /* Able to generate complex predicates when there are multiple predciate expressions */
479 void FuncNode::generate_predicate(Predicate ** curr_pred, FuncInst * next_inst,
480 SnapVector<struct half_pred_expr *> * half_pred_expressions)
482 ASSERT(half_pred_expressions->size() != 0);
483 SnapVector<Predicate *> predicates;
485 struct half_pred_expr * half_expr = (*half_pred_expressions)[0];
486 predicates.push_back(new Predicate(next_inst));
487 predicates.push_back(new Predicate(next_inst));
489 predicates[0]->add_predicate_expr(half_expr->token, half_expr->func_inst, true);
490 predicates[1]->add_predicate_expr(half_expr->token, half_expr->func_inst, false);
492 for (uint i = 1; i < half_pred_expressions->size(); i++) {
493 half_expr = (*half_pred_expressions)[i];
495 uint old_size = predicates.size();
496 for (uint j = 0; j < old_size; j++) {
497 Predicate * pred = predicates[j];
498 Predicate * new_pred = new Predicate(next_inst);
499 new_pred->copy_predicate_expr(pred);
501 pred->add_predicate_expr(half_expr->token, half_expr->func_inst, true);
502 new_pred->add_predicate_expr(half_expr->token, half_expr->func_inst, false);
504 predicates.push_back(new_pred);
508 for (uint i = 0; i < predicates.size(); i++) {
509 Predicate * pred= predicates[i];
510 (*curr_pred)->add_child(pred);
511 pred->set_parent(*curr_pred);
516 void FuncNode::add_to_val_loc_map(uint64_t val, void * loc)
518 loc_set_t * locations = val_loc_map->get(val);
520 if (locations == NULL) {
521 locations = new loc_set_t();
522 val_loc_map->put(val, locations);
525 update_loc_may_equal_map(loc, locations);
527 values_may_read_from->add(val);
530 void FuncNode::add_to_val_loc_map(value_set_t * values, void * loc)
532 value_set_iter * it = values->iterator();
533 while (it->hasNext()) {
534 uint64_t val = it->next();
535 add_to_val_loc_map(val, loc);
539 void FuncNode::update_loc_may_equal_map(void * new_loc, loc_set_t * old_locations)
541 loc_set_t * neighbors = loc_may_equal_map->get(new_loc);
543 if (neighbors == NULL) {
544 neighbors = new loc_set_t();
545 loc_may_equal_map->put(new_loc, neighbors);
548 loc_set_iter * loc_it = old_locations->iterator();
549 while (loc_it->hasNext()) {
550 // new_loc: { old_locations, ... }
551 void * member = loc_it->next();
552 neighbors->add(member);
554 // for each i in old_locations, i : { new_loc, ... }
555 loc_set_t * _neighbors = loc_may_equal_map->get(member);
556 if (_neighbors == NULL) {
557 _neighbors = new loc_set_t();
558 loc_may_equal_map->put(member, _neighbors);
560 _neighbors->add(new_loc);
564 void FuncNode::print_predicate_tree()
566 model_print("digraph function_%s {\n", func_name);
567 predicate_tree_entry->print_pred_subtree();
568 model_print("}\n"); // end of graph
571 void FuncNode::print_val_loc_map()
573 value_set_iter * val_it = values_may_read_from->iterator();
574 while (val_it->hasNext()) {
575 uint64_t value = val_it->next();
576 model_print("val %llx: ", value);
578 loc_set_t * locations = val_loc_map->get(value);
579 loc_set_iter * loc_it = locations->iterator();
580 while (loc_it->hasNext()) {
581 void * location = loc_it->next();
582 model_print("%p ", location);
588 /* @param tid thread id
589 * Print the values read by the last read actions for each memory location
592 void FuncNode::print_last_read(uint32_t tid)
594 ASSERT(thrd_read_map.size() > tid);
595 read_map_t * read_map = thrd_read_map[tid];
597 mllnode<void *> * it;
598 for (it = read_locations.begin();it != NULL;it=it->getNext()) {
599 if ( !read_map->contains(it->getVal()) )
602 uint64_t read_val = read_map->get(it->getVal());
603 model_print("last read of thread %d at %p: 0x%x\n", tid, it->getVal(), read_val);