4 predicate_tree_initialized(false),
5 predicate_tree_entry(new Predicate(NULL, true)),
12 /* Check whether FuncInst with the same type, position, and location
13 * as act has been added to func_inst_map or not. If not, add it.
15 * Note: currently, actions with the same position are filtered out by process_action,
16 * so the collision list of FuncInst is not used. May remove it later.
18 void FuncNode::add_inst(ModelAction *act)
21 const char * position = act->get_position();
23 /* THREAD* actions, ATOMIC_LOCK, ATOMIC_TRYLOCK, and ATOMIC_UNLOCK
24 * actions are not tagged with their source line numbers
29 if ( func_inst_map.contains(position) ) {
30 FuncInst * inst = func_inst_map.get(position);
32 if (inst->get_type() != act->get_type() ) {
33 // model_print("action with a different type occurs at line number %s\n", position);
34 FuncInst * func_inst = inst->search_in_collision(act);
36 if (func_inst != NULL)
39 func_inst = new FuncInst(act, this);
40 inst->get_collisions()->push_back(func_inst);
41 inst_list.push_back(func_inst); // delete?
47 FuncInst * func_inst = new FuncInst(act, this);
49 func_inst_map.put(position, func_inst);
50 inst_list.push_back(func_inst);
53 /* Get the FuncInst with the same type, position, and location
56 * @return FuncInst with the same type, position, and location as act */
57 FuncInst * FuncNode::get_inst(ModelAction *act)
60 const char * position = act->get_position();
62 /* THREAD* actions, ATOMIC_LOCK, ATOMIC_TRYLOCK, and ATOMIC_UNLOCK
63 * actions are not tagged with their source line numbers
68 FuncInst * inst = func_inst_map.get(position);
72 // ASSERT(inst->get_location() == act->get_location());
74 action_type inst_type = inst->get_type();
75 action_type act_type = act->get_type();
77 // else if branch: an RMWRCAS action is converted to a RMW or READ action
78 if (inst_type == act_type)
80 else if (inst_type == ATOMIC_RMWRCAS &&
81 (act_type == ATOMIC_RMW || act_type == ATOMIC_READ))
88 void FuncNode::add_entry_inst(FuncInst * inst)
93 mllnode<FuncInst *> * it;
94 for (it = entry_insts.begin(); it != NULL; it = it->getNext()) {
95 if (inst == it->getVal())
99 entry_insts.push_back(inst);
103 * @brief Convert ModelAdtion list to FuncInst list
104 * @param act_list A list of ModelActions
106 void FuncNode::update_tree(action_list_t * act_list)
108 if (act_list == NULL)
110 else if (act_list->size() == 0)
113 /* build inst_list from act_list for later processing */
114 func_inst_list_t inst_list;
115 action_list_t read_act_list;
117 for (sllnode<ModelAction *> * it = act_list->begin(); it != NULL; it = it->getNext()) {
118 ModelAction * act = it->getVal();
119 FuncInst * func_inst = get_inst(act);
121 if (func_inst == NULL)
124 inst_list.push_back(func_inst);
126 // model_print("position: %s ", act->get_position());
129 if (func_inst->is_read())
130 read_act_list.push_back(act);
133 update_inst_tree(&inst_list);
134 update_predicate_tree(&read_act_list);
138 * @brief Link FuncInsts in inst_list - add one FuncInst to another's predecessors and successors
139 * @param inst_list A list of FuncInsts
141 void FuncNode::update_inst_tree(func_inst_list_t * inst_list)
143 if (inst_list == NULL)
145 else if (inst_list->size() == 0)
149 sllnode<FuncInst *>* it = inst_list->begin();
150 sllnode<FuncInst *>* prev;
152 /* add the first instruction to the list of entry insts */
153 FuncInst * entry_inst = it->getVal();
154 add_entry_inst(entry_inst);
158 prev = it->getPrev();
160 FuncInst * prev_inst = prev->getVal();
161 FuncInst * curr_inst = it->getVal();
163 prev_inst->add_succ(curr_inst);
164 curr_inst->add_pred(prev_inst);
170 /* @param tid thread id
171 * Store the values read by atomic read actions into thrd_read_map */
172 void FuncNode::store_read(ModelAction * act, uint32_t tid)
176 void * location = act->get_location();
177 uint64_t read_from_val = act->get_reads_from_value();
179 /* resize and initialize */
180 uint32_t old_size = thrd_read_map.size();
181 if (old_size <= tid) {
182 thrd_read_map.resize(tid + 1);
183 for (uint32_t i = old_size; i < tid + 1;i++)
184 thrd_read_map[i] = new read_map_t();
187 read_map_t * read_map = thrd_read_map[tid];
188 read_map->put(location, read_from_val);
190 /* Store the memory locations where atomic reads happen */
191 // read_locations.add(location);
194 uint64_t FuncNode::query_last_read(void * location, uint32_t tid)
196 if (thrd_read_map.size() <= tid)
199 read_map_t * read_map = thrd_read_map[tid];
201 /* last read value not found */
202 if ( !read_map->contains(location) )
205 uint64_t read_val = read_map->get(location);
209 /* @param tid thread id
210 * Reset read map for a thread. This function shall only be called
211 * when a thread exits a function
213 void FuncNode::clear_read_map(uint32_t tid)
215 if (thrd_read_map.size() <= tid)
218 thrd_read_map[tid]->reset();
221 void FuncNode::update_predicate_tree(action_list_t * act_list)
223 if (act_list == NULL || act_list->size() == 0)
226 if (predicate_tree_initialized) {
229 predicate_tree_initialized = true;
231 /* map a FuncInst to the parent of its predicate */
232 HashTable<FuncInst *, Predicate *, uintptr_t, 0> inst_pred_map(128);
233 HashTable<void *, ModelAction *, uintptr_t, 0> loc_act_map(128);
235 sllnode<ModelAction *> *it = act_list->begin();
236 Predicate * curr_pred = predicate_tree_entry;
239 ModelAction * next_act = it->getVal();
240 FuncInst * next_inst = get_inst(next_act);
241 Predicate * old_pred = curr_pred;
243 bool branch_found = follow_branch(&curr_pred, next_inst, next_act, &loc_act_map);
247 Predicate * back_pred = curr_pred->get_backedge();
248 if (back_pred != NULL) {
249 curr_pred = back_pred;
253 if (inst_pred_map.contains(next_inst)) {
254 back_pred = inst_pred_map.get(next_inst);
255 curr_pred->set_backedge(back_pred);
256 curr_pred = back_pred;
261 if (!inst_pred_map.contains(next_inst))
262 inst_pred_map.put(next_inst, old_pred);
265 if ( loc_act_map.contains(next_act->get_location()) ) {
266 Predicate * new_pred1 = new Predicate(next_inst);
267 new_pred1->add_predicate(EQUALITY, next_act->get_location(), true);
269 Predicate * new_pred2 = new Predicate(next_inst);
270 new_pred2->add_predicate(EQUALITY, next_act->get_location(), false);
272 curr_pred->add_child(new_pred1);
273 curr_pred->add_child(new_pred2);
274 //new_pred1->add_parent(curr_pred);
275 //new_pred2->add_parent(curr_pred);
277 ModelAction * last_act = loc_act_map.get(next_act->get_location());
278 uint64_t last_read = last_act->get_reads_from_value();
279 uint64_t next_read = next_act->get_reads_from_value();
281 if ( last_read == next_read )
282 curr_pred = new_pred1;
284 curr_pred = new_pred2;
286 Predicate * new_pred = new Predicate(next_inst);
287 curr_pred->add_child(new_pred);
288 //new_pred->add_parent(curr_pred);
290 curr_pred = new_pred;
294 loc_act_map.put(next_act->get_location(), next_act);
298 // model_print("function %s\n", func_name);
299 // print_predicate_tree();
302 /* Given curr_pred and next_inst, find the branch following curr_pred that contains next_inst and the correct predicate
303 * @return true if branch found, false otherwise.
305 bool FuncNode::follow_branch(Predicate ** curr_pred, FuncInst * next_inst, ModelAction * next_act,
306 HashTable<void *, ModelAction *, uintptr_t, 0> * loc_act_map)
308 /* check if a branch with func_inst and corresponding predicate exists */
309 bool branch_found = false;
310 ModelVector<Predicate *> * branches = (*curr_pred)->get_children();
311 for (uint i = 0; i < branches->size(); i++) {
312 Predicate * branch = (*branches)[i];
313 if (branch->get_func_inst() != next_inst)
316 PredExprSet * pred_expressions = branch->get_pred_expressions();
318 /* no predicate, follow the only branch */
319 if (pred_expressions->getSize() == 0) {
320 // model_print("no predicate exists: "); next_inst->print();
326 PredExprSetIter * pred_expr_it = pred_expressions->iterator();
327 while (pred_expr_it->hasNext()) {
328 pred_expr * pred_expression = pred_expr_it->next();
329 uint64_t last_read, next_read;
330 ModelAction * last_act;
333 switch(pred_expression->token) {
335 last_act = loc_act_map->get(next_act->get_location());
336 last_read = last_act->get_reads_from_value();
337 next_read = next_act->get_reads_from_value();
339 equality = (last_read == next_read);
341 if (equality == pred_expression->value) {
343 // model_print("predicate: token: %d, location: %p, value: %d - ", pred_expression->token, pred_expression->location, pred_expression->value); next_inst->print();
350 model_print("unkown predicate token\n");
360 void FuncNode::print_predicate_tree()
362 model_print("digraph function_%s {\n", func_name);
363 predicate_tree_entry->print_pred_subtree();
364 model_print("}\n"); // end of graph
367 /* @param tid thread id
368 * Print the values read by the last read actions for each memory location
371 void FuncNode::print_last_read(uint32_t tid)
373 ASSERT(thrd_read_map.size() > tid);
374 read_map_t * read_map = thrd_read_map[tid];
376 mllnode<void *> * it;
377 for (it = read_locations.begin();it != NULL;it=it->getNext()) {
378 if ( !read_map->contains(it->getVal()) )
381 uint64_t read_val = read_map->get(it->getVal());
382 model_print("last read of thread %d at %p: 0x%x\n", tid, it->getVal(), read_val);