5 predicate_tree_initialized(false),
6 predicate_tree_entry(new Predicate(NULL, true)),
13 /* Check whether FuncInst with the same type, position, and location
14 * as act has been added to func_inst_map or not. If not, add it.
16 * Note: currently, actions with the same position are filtered out by process_action,
17 * so the collision list of FuncInst is not used. May remove it later.
19 void FuncNode::add_inst(ModelAction *act)
22 const char * position = act->get_position();
24 /* THREAD* actions, ATOMIC_LOCK, ATOMIC_TRYLOCK, and ATOMIC_UNLOCK
25 * actions are not tagged with their source line numbers
30 if ( func_inst_map.contains(position) ) {
31 FuncInst * inst = func_inst_map.get(position);
33 if (inst->get_type() != act->get_type() ) {
34 // model_print("action with a different type occurs at line number %s\n", position);
35 FuncInst * func_inst = inst->search_in_collision(act);
37 if (func_inst != NULL)
40 func_inst = new FuncInst(act, this);
41 inst->get_collisions()->push_back(func_inst);
42 inst_list.push_back(func_inst); // delete?
48 FuncInst * func_inst = new FuncInst(act, this);
50 func_inst_map.put(position, func_inst);
51 inst_list.push_back(func_inst);
54 /* Get the FuncInst with the same type, position, and location
57 * @return FuncInst with the same type, position, and location as act */
58 FuncInst * FuncNode::get_inst(ModelAction *act)
61 const char * position = act->get_position();
63 /* THREAD* actions, ATOMIC_LOCK, ATOMIC_TRYLOCK, and ATOMIC_UNLOCK
64 * actions are not tagged with their source line numbers
69 FuncInst * inst = func_inst_map.get(position);
73 // ASSERT(inst->get_location() == act->get_location());
75 action_type inst_type = inst->get_type();
76 action_type act_type = act->get_type();
78 // else if branch: an RMWRCAS action is converted to a RMW or READ action
79 if (inst_type == act_type)
81 else if (inst_type == ATOMIC_RMWRCAS &&
82 (act_type == ATOMIC_RMW || act_type == ATOMIC_READ))
89 void FuncNode::add_entry_inst(FuncInst * inst)
94 mllnode<FuncInst *> * it;
95 for (it = entry_insts.begin(); it != NULL; it = it->getNext()) {
96 if (inst == it->getVal())
100 entry_insts.push_back(inst);
104 * @brief Convert ModelAdtion list to FuncInst list
105 * @param act_list A list of ModelActions
107 void FuncNode::update_tree(action_list_t * act_list)
109 if (act_list == NULL)
111 else if (act_list->size() == 0)
114 /* build inst_list from act_list for later processing */
115 func_inst_list_t inst_list;
116 action_list_t read_act_list;
117 HashTable<ModelAction *, FuncInst *, uintptr_t, 4> act_inst_map;
119 for (sllnode<ModelAction *> * it = act_list->begin(); it != NULL; it = it->getNext()) {
120 ModelAction * act = it->getVal();
121 FuncInst * func_inst = get_inst(act);
123 if (func_inst == NULL)
126 inst_list.push_back(func_inst);
128 // model_print("position: %s ", act->get_position());
131 if (func_inst->is_read()) {
132 read_act_list.push_back(act);
133 act_inst_map.put(act, func_inst);
137 update_inst_tree(&inst_list);
138 update_predicate_tree(&read_act_list, &act_inst_map);
142 * @brief Link FuncInsts in inst_list - add one FuncInst to another's predecessors and successors
143 * @param inst_list A list of FuncInsts
145 void FuncNode::update_inst_tree(func_inst_list_t * inst_list)
147 if (inst_list == NULL)
149 else if (inst_list->size() == 0)
153 sllnode<FuncInst *>* it = inst_list->begin();
154 sllnode<FuncInst *>* prev;
156 /* add the first instruction to the list of entry insts */
157 FuncInst * entry_inst = it->getVal();
158 add_entry_inst(entry_inst);
162 prev = it->getPrev();
164 FuncInst * prev_inst = prev->getVal();
165 FuncInst * curr_inst = it->getVal();
167 prev_inst->add_succ(curr_inst);
168 curr_inst->add_pred(prev_inst);
174 /* @param tid thread id
175 * Store the values read by atomic read actions into thrd_read_map */
176 void FuncNode::store_read(ModelAction * act, uint32_t tid)
180 void * location = act->get_location();
181 uint64_t read_from_val = act->get_reads_from_value();
183 /* resize and initialize */
184 uint32_t old_size = thrd_read_map.size();
185 if (old_size <= tid) {
186 thrd_read_map.resize(tid + 1);
187 for (uint32_t i = old_size; i < tid + 1;i++)
188 thrd_read_map[i] = new read_map_t();
191 read_map_t * read_map = thrd_read_map[tid];
192 read_map->put(location, read_from_val);
194 /* Store the memory locations where atomic reads happen */
195 // read_locations.add(location);
198 uint64_t FuncNode::query_last_read(void * location, uint32_t tid)
200 if (thrd_read_map.size() <= tid)
203 read_map_t * read_map = thrd_read_map[tid];
205 /* last read value not found */
206 if ( !read_map->contains(location) )
209 uint64_t read_val = read_map->get(location);
213 /* @param tid thread id
214 * Reset read map for a thread. This function shall only be called
215 * when a thread exits a function
217 void FuncNode::clear_read_map(uint32_t tid)
219 if (thrd_read_map.size() <= tid)
222 thrd_read_map[tid]->reset();
225 void FuncNode::update_predicate_tree(action_list_t * act_list, HashTable<ModelAction *, FuncInst *, uintptr_t, 4> * act_inst_map)
227 if (act_list == NULL || act_list->size() == 0)
230 if (predicate_tree_initialized) {
233 predicate_tree_initialized = true;
235 /* map a FuncInst to the parent of its predicate */
236 HashTable<FuncInst *, Predicate *, uintptr_t, 0> inst_pred_map(128);
237 HashTable<void *, ModelAction *, uintptr_t, 0> loc_act_map(128);
239 sllnode<ModelAction *> *it = act_list->begin();
240 Predicate * curr_pred = predicate_tree_entry;
243 ModelAction * next_act = it->getVal();
244 FuncInst * next_inst = act_inst_map->get(next_act);
245 Predicate * old_pred = curr_pred;
247 bool branch_found = follow_branch(&curr_pred, next_inst, next_act, &loc_act_map);
251 Predicate * back_pred = curr_pred->get_backedge();
252 if (back_pred != NULL) {
253 curr_pred = back_pred;
257 if (inst_pred_map.contains(next_inst)) {
258 back_pred = inst_pred_map.get(next_inst);
259 curr_pred->set_backedge(back_pred);
260 curr_pred = back_pred;
265 if (!inst_pred_map.contains(next_inst))
266 inst_pred_map.put(next_inst, old_pred);
269 if ( loc_act_map.contains(next_act->get_location()) ) {
270 Predicate * new_pred1 = new Predicate(next_inst);
271 new_pred1->add_predicate(EQUALITY, next_act->get_location(), true);
273 Predicate * new_pred2 = new Predicate(next_inst);
274 new_pred2->add_predicate(EQUALITY, next_act->get_location(), false);
276 curr_pred->add_child(new_pred1);
277 curr_pred->add_child(new_pred2);
278 //new_pred1->add_parent(curr_pred);
279 //new_pred2->add_parent(curr_pred);
281 ModelAction * last_act = loc_act_map.get(next_act->get_location());
282 uint64_t last_read = last_act->get_reads_from_value();
283 uint64_t next_read = next_act->get_reads_from_value();
285 if ( last_read == next_read )
286 curr_pred = new_pred1;
288 curr_pred = new_pred2;
290 Predicate * new_pred = new Predicate(next_inst);
291 curr_pred->add_child(new_pred);
292 //new_pred->add_parent(curr_pred);
294 curr_pred = new_pred;
298 loc_act_map.put(next_act->get_location(), next_act);
302 // model_print("function %s\n", func_name);
303 // print_predicate_tree();
306 /* Given curr_pred and next_inst, find the branch following curr_pred that contains next_inst and the correct predicate
307 * @return true if branch found, false otherwise.
309 bool FuncNode::follow_branch(Predicate ** curr_pred, FuncInst * next_inst, ModelAction * next_act,
310 HashTable<void *, ModelAction *, uintptr_t, 0> * loc_act_map)
312 /* check if a branch with func_inst and corresponding predicate exists */
313 bool branch_found = false;
314 ModelVector<Predicate *> * branches = (*curr_pred)->get_children();
315 for (uint i = 0; i < branches->size(); i++) {
316 Predicate * branch = (*branches)[i];
317 if (branch->get_func_inst() != next_inst)
320 PredExprSet * pred_expressions = branch->get_pred_expressions();
322 /* no predicate, follow the only branch */
323 if (pred_expressions->getSize() == 0) {
324 // model_print("no predicate exists: "); next_inst->print();
330 PredExprSetIter * pred_expr_it = pred_expressions->iterator();
331 while (pred_expr_it->hasNext()) {
332 pred_expr * pred_expression = pred_expr_it->next();
333 uint64_t last_read, next_read;
334 ModelAction * last_act;
337 switch(pred_expression->token) {
339 last_act = loc_act_map->get(next_act->get_location());
340 last_read = last_act->get_reads_from_value();
341 next_read = next_act->get_reads_from_value();
343 equality = (last_read == next_read);
345 if (equality == pred_expression->value) {
347 // model_print("predicate: token: %d, location: %p, value: %d - ", pred_expression->token, pred_expression->location, pred_expression->value); next_inst->print();
354 model_print("unkown predicate token\n");
364 void FuncNode::print_predicate_tree()
366 model_print("digraph function_%s {\n", func_name);
367 predicate_tree_entry->print_pred_subtree();
368 model_print("}\n"); // end of graph
371 /* @param tid thread id
372 * Print the values read by the last read actions for each memory location
375 void FuncNode::print_last_read(uint32_t tid)
377 ASSERT(thrd_read_map.size() > tid);
378 read_map_t * read_map = thrd_read_map[tid];
380 mllnode<void *> * it;
381 for (it = read_locations.begin();it != NULL;it=it->getNext()) {
382 if ( !read_map->contains(it->getVal()) )
385 uint64_t read_val = read_map->get(it->getVal());
386 model_print("last read of thread %d at %p: 0x%x\n", tid, it->getVal(), read_val);