3 FuncNode::FuncNode(ModelHistory * history) :
5 predicate_tree_initialized(false),
6 predicate_tree_entry(new Predicate(NULL, true)),
14 predicate_tree_entry->add_predicate_expr(NOPREDICATE, NULL, true);
17 void FuncNode::set_new_exec_flag()
19 for (uint i = 0; i < thrd_read_map.size(); i++)
20 thrd_read_map[i] = new read_map_t();
22 for (mllnode<FuncInst *> * it = inst_list.begin(); it != NULL; it = it->getNext()) {
23 FuncInst * inst = it->getVal();
24 inst->reset_location();
28 /* Check whether FuncInst with the same type, position, and location
29 * as act has been added to func_inst_map or not. If not, add it.
31 * Note: currently, actions with the same position are filtered out by process_action,
32 * so the collision list of FuncInst is not used. May remove it later.
34 void FuncNode::add_inst(ModelAction *act)
37 const char * position = act->get_position();
39 /* THREAD* actions, ATOMIC_LOCK, ATOMIC_TRYLOCK, and ATOMIC_UNLOCK
40 * actions are not tagged with their source line numbers
45 if ( func_inst_map.contains(position) ) {
46 FuncInst * inst = func_inst_map.get(position);
48 ASSERT(inst->get_type() == act->get_type());
50 // locations are set to NULL when new executions start
51 if (inst->get_location() == NULL)
52 inst->set_location(act->get_location());
54 if (inst->get_location() != act->get_location())
55 inst->not_single_location();
60 FuncInst * func_inst = new FuncInst(act, this);
62 func_inst_map.put(position, func_inst);
63 inst_list.push_back(func_inst);
66 /* Get the FuncInst with the same type, position, and location
69 * @return FuncInst with the same type, position, and location as act */
70 FuncInst * FuncNode::get_inst(ModelAction *act)
73 const char * position = act->get_position();
75 /* THREAD* actions, ATOMIC_LOCK, ATOMIC_TRYLOCK, and ATOMIC_UNLOCK
76 * actions are not tagged with their source line numbers
81 FuncInst * inst = func_inst_map.get(position);
85 action_type inst_type = inst->get_type();
86 action_type act_type = act->get_type();
88 // else if branch: an RMWRCAS action is converted to a RMW or READ action
89 if (inst_type == act_type)
91 else if (inst_type == ATOMIC_RMWRCAS &&
92 (act_type == ATOMIC_RMW || act_type == ATOMIC_READ))
99 void FuncNode::add_entry_inst(FuncInst * inst)
104 mllnode<FuncInst *> * it;
105 for (it = entry_insts.begin(); it != NULL; it = it->getNext()) {
106 if (inst == it->getVal())
110 entry_insts.push_back(inst);
114 * @brief Convert ModelAdtion list to FuncInst list
115 * @param act_list A list of ModelActions
117 void FuncNode::update_tree(action_list_t * act_list)
119 if (act_list == NULL || act_list->size() == 0)
122 /* build inst_list from act_list for later processing */
123 func_inst_list_t inst_list;
124 action_list_t read_act_list;
126 for (sllnode<ModelAction *> * it = act_list->begin(); it != NULL; it = it->getNext()) {
127 ModelAction * act = it->getVal();
128 FuncInst * func_inst = get_inst(act);
130 if (func_inst == NULL)
133 inst_list.push_back(func_inst);
135 if (func_inst->is_read())
136 read_act_list.push_back(act);
139 // model_print("function %s\n", func_name);
140 update_inst_tree(&inst_list);
141 update_predicate_tree(&read_act_list);
142 // deep_update(predicate_tree_entry);
144 // print_predicate_tree();
148 * @brief Link FuncInsts in inst_list - add one FuncInst to another's predecessors and successors
149 * @param inst_list A list of FuncInsts
151 void FuncNode::update_inst_tree(func_inst_list_t * inst_list)
153 if (inst_list == NULL)
155 else if (inst_list->size() == 0)
159 sllnode<FuncInst *>* it = inst_list->begin();
160 sllnode<FuncInst *>* prev;
162 /* add the first instruction to the list of entry insts */
163 FuncInst * entry_inst = it->getVal();
164 add_entry_inst(entry_inst);
168 prev = it->getPrev();
170 FuncInst * prev_inst = prev->getVal();
171 FuncInst * curr_inst = it->getVal();
173 prev_inst->add_succ(curr_inst);
174 curr_inst->add_pred(prev_inst);
180 /* @param tid thread id
181 * Store the values read by atomic read actions into thrd_read_map */
182 void FuncNode::store_read(ModelAction * act, uint32_t tid)
186 void * location = act->get_location();
187 uint64_t read_from_val = act->get_reads_from_value();
189 /* resize and initialize */
191 uint32_t old_size = thrd_read_map.size();
192 if (old_size <= tid) {
193 thrd_read_map.resize(tid + 1);
194 for (uint32_t i = old_size; i < tid + 1;i++)
195 thrd_read_map[i] = new read_map_t();
198 read_map_t * read_map = thrd_read_map[tid];
199 read_map->put(location, read_from_val);
201 /* Store the memory locations where atomic reads happen */
202 // read_locations.add(location);
205 uint64_t FuncNode::query_last_read(void * location, uint32_t tid)
207 if (thrd_read_map.size() <= tid)
210 read_map_t * read_map = thrd_read_map[tid];
212 /* last read value not found */
213 if ( !read_map->contains(location) )
216 uint64_t read_val = read_map->get(location);
220 /* @param tid thread id
221 * Reset read map for a thread. This function shall only be called
222 * when a thread exits a function
224 void FuncNode::clear_read_map(uint32_t tid)
226 if (thrd_read_map.size() <= tid)
229 thrd_read_map[tid]->reset();
232 void FuncNode::update_predicate_tree(action_list_t * act_list)
234 if (act_list == NULL || act_list->size() == 0)
237 if (predicate_tree_initialized) {
240 predicate_tree_initialized = true;
242 /* map a FuncInst to the its predicate */
243 HashTable<FuncInst *, Predicate *, uintptr_t, 0> inst_pred_map(128);
245 // number FuncInsts to detect loops
246 HashTable<FuncInst *, uint32_t, uintptr_t, 0> inst_id_map(128);
247 uint32_t inst_counter = 0;
249 HashTable<void *, ModelAction *, uintptr_t, 0> loc_act_map(128);
250 HashTable<FuncInst *, ModelAction *, uintptr_t, 0> inst_act_map(128);
252 sllnode<ModelAction *> *it = act_list->begin();
253 Predicate * curr_pred = predicate_tree_entry;
255 ModelAction * next_act = it->getVal();
256 FuncInst * next_inst = get_inst(next_act);
257 SnapVector<Predicate *> * unset_predicates = new SnapVector<Predicate *>();
259 bool branch_found = follow_branch(&curr_pred, next_inst, next_act, &inst_act_map, unset_predicates);
261 // no predicate expressions, follow the only branch
262 if (!branch_found && unset_predicates->size() != 0) {
263 ASSERT(unset_predicates->size() == 1);
264 Predicate * one_branch = (*unset_predicates)[0];
265 curr_pred = one_branch;
269 delete unset_predicates;
272 if (!branch_found && inst_id_map.contains(next_inst)) {
273 FuncInst * curr_inst = curr_pred->get_func_inst();
274 uint32_t curr_id = inst_id_map.get(curr_inst);
275 uint32_t next_id = inst_id_map.get(next_inst);
277 if (curr_id >= next_id) {
278 Predicate * old_pred = inst_pred_map.get(next_inst);
279 Predicate * back_pred = old_pred->get_parent();
281 curr_pred->add_backedge(back_pred);
282 curr_pred = back_pred;
289 if ( loc_act_map.contains(next_act->get_location()) ) {
290 ModelAction * last_act = loc_act_map.get(next_act->get_location());
291 FuncInst * last_inst = get_inst(last_act);
293 Predicate * new_pred1 = new Predicate(next_inst);
294 new_pred1->add_predicate_expr(EQUALITY, last_inst, true);
296 Predicate * new_pred2 = new Predicate(next_inst);
297 new_pred2->add_predicate_expr(EQUALITY, last_inst, false);
299 curr_pred->add_child(new_pred1);
300 curr_pred->add_child(new_pred2);
301 new_pred1->set_parent(curr_pred);
302 new_pred2->set_parent(curr_pred);
304 uint64_t last_read = last_act->get_reads_from_value();
305 uint64_t next_read = next_act->get_reads_from_value();
307 if ( last_read == next_read )
308 curr_pred = new_pred1;
310 curr_pred = new_pred2;
311 } else if (!next_inst->is_single_location()) {
312 Predicate * new_pred1 = new Predicate(next_inst);
313 new_pred1->add_predicate_expr(NULLITY, NULL, true);
315 Predicate * new_pred2 = new Predicate(next_inst);
316 new_pred2->add_predicate_expr(NULLITY, NULL, false);
318 curr_pred->add_child(new_pred1);
319 curr_pred->add_child(new_pred2);
320 new_pred1->set_parent(curr_pred);
321 new_pred2->set_parent(curr_pred);
323 uint64_t next_read = next_act->get_reads_from_value();
324 bool isnull = ((void*)next_read == NULL);
326 curr_pred = new_pred1;
328 curr_pred = new_pred2;
330 Predicate * new_pred = new Predicate(next_inst);
331 curr_pred->add_child(new_pred);
332 new_pred->set_parent(curr_pred);
334 if (curr_pred->is_entry_predicate())
335 new_pred->add_predicate_expr(NOPREDICATE, NULL, true);
337 curr_pred = new_pred;
341 inst_pred_map.put(next_inst, curr_pred);
342 if (!inst_id_map.contains(next_inst))
343 inst_id_map.put(next_inst, inst_counter++);
345 loc_act_map.put(next_act->get_location(), next_act);
346 inst_act_map.put(next_inst, next_act);
351 void FuncNode::deep_update(Predicate * curr_pred)
353 FuncInst * func_inst = curr_pred->get_func_inst();
354 if (func_inst != NULL && !func_inst->is_single_location()) {
355 bool has_null_pred = false;
356 PredExprSet * pred_expressions = curr_pred->get_pred_expressions();
357 PredExprSetIter * pred_expr_it = pred_expressions->iterator();
358 while (pred_expr_it->hasNext()) {
359 pred_expr * pred_expression = pred_expr_it->next();
360 if (pred_expression->token == NULLITY) {
361 has_null_pred = true;
366 if (!has_null_pred) {
367 // func_inst->print();
368 Predicate * another_branch = new Predicate(func_inst);
369 another_branch->copy_predicate_expr(curr_pred);
370 another_branch->add_predicate_expr(NULLITY, NULL, 1);
371 curr_pred->add_predicate_expr(NULLITY, NULL, 0);
373 Predicate * parent = curr_pred->get_parent();
374 parent->add_child(another_branch);
378 ModelVector<Predicate *> * branches = curr_pred->get_children();
379 for (uint i = 0; i < branches->size(); i++) {
380 Predicate * branch = (*branches)[i];
385 /* Given curr_pred and next_inst, find the branch following curr_pred that
386 * contains next_inst and the correct predicate.
387 * @return true if branch found, false otherwise.
389 bool FuncNode::follow_branch(Predicate ** curr_pred, FuncInst * next_inst, ModelAction * next_act,
390 HashTable<FuncInst *, ModelAction *, uintptr_t, 0> * inst_act_map,
391 SnapVector<Predicate *> * unset_predicates)
393 /* check if a branch with func_inst and corresponding predicate exists */
394 bool branch_found = false;
395 ModelVector<Predicate *> * branches = (*curr_pred)->get_children();
396 for (uint i = 0; i < branches->size(); i++) {
397 Predicate * branch = (*branches)[i];
398 if (branch->get_func_inst() != next_inst)
401 /* check against predicate expressions */
402 bool predicate_correct = true;
403 PredExprSet * pred_expressions = branch->get_pred_expressions();
404 PredExprSetIter * pred_expr_it = pred_expressions->iterator();
406 if (pred_expressions->getSize() == 0) {
407 predicate_correct = false;
408 unset_predicates->push_back(branch);
411 while (pred_expr_it->hasNext()) {
412 pred_expr * pred_expression = pred_expr_it->next();
413 uint64_t last_read, next_read;
416 switch(pred_expression->token) {
418 predicate_correct = true;
421 FuncInst * to_be_compared;
422 ModelAction * last_act;
424 to_be_compared = pred_expression->func_inst;
425 last_act = inst_act_map->get(to_be_compared);
427 last_read = last_act->get_reads_from_value();
428 next_read = next_act->get_reads_from_value();
429 equality = (last_read == next_read);
430 if (equality != pred_expression->value)
431 predicate_correct = false;
435 next_read = next_act->get_reads_from_value();
436 equality = ((void*)next_read == NULL);
437 if (equality != pred_expression->value)
438 predicate_correct = false;
441 predicate_correct = false;
442 model_print("unkown predicate token\n");
447 if (predicate_correct) {
457 void FuncNode::print_predicate_tree()
459 model_print("digraph function_%s {\n", func_name);
460 predicate_tree_entry->print_pred_subtree();
461 model_print("}\n"); // end of graph
464 /* @param tid thread id
465 * Print the values read by the last read actions for each memory location
468 void FuncNode::print_last_read(uint32_t tid)
470 ASSERT(thrd_read_map.size() > tid);
471 read_map_t * read_map = thrd_read_map[tid];
473 mllnode<void *> * it;
474 for (it = read_locations.begin();it != NULL;it=it->getNext()) {
475 if ( !read_map->contains(it->getVal()) )
478 uint64_t read_val = read_map->get(it->getVal());
479 model_print("last read of thread %d at %p: 0x%x\n", tid, it->getVal(), read_val);