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 action_type inst_type = inst->get_type();
74 action_type act_type = act->get_type();
76 // else if branch: an RMWRCAS action is converted to a RMW or READ action
77 if (inst_type == act_type)
79 else if (inst_type == ATOMIC_RMWRCAS &&
80 (act_type == ATOMIC_RMW || act_type == ATOMIC_READ))
87 void FuncNode::add_entry_inst(FuncInst * inst)
92 mllnode<FuncInst *> * it;
93 for (it = entry_insts.begin(); it != NULL; it = it->getNext()) {
94 if (inst == it->getVal())
98 entry_insts.push_back(inst);
102 * @brief Convert ModelAdtion list to FuncInst list
103 * @param act_list A list of ModelActions
105 void FuncNode::update_tree(action_list_t * act_list)
107 if (act_list == NULL)
109 else if (act_list->size() == 0)
112 /* build inst_list from act_list for later processing */
113 func_inst_list_t inst_list;
114 action_list_t read_act_list;
115 HashTable<ModelAction *, FuncInst *, uintptr_t, 4> act_inst_map(128);
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);
131 act_inst_map.put(act, func_inst);
135 update_inst_tree(&inst_list);
136 update_predicate_tree(&read_act_list, &act_inst_map);
140 * @brief Link FuncInsts in inst_list - add one FuncInst to another's predecessors and successors
141 * @param inst_list A list of FuncInsts
143 void FuncNode::update_inst_tree(func_inst_list_t * inst_list)
145 if (inst_list == NULL)
147 else if (inst_list->size() == 0)
151 sllnode<FuncInst *>* it = inst_list->begin();
152 sllnode<FuncInst *>* prev;
154 /* add the first instruction to the list of entry insts */
155 FuncInst * entry_inst = it->getVal();
156 add_entry_inst(entry_inst);
160 prev = it->getPrev();
162 FuncInst * prev_inst = prev->getVal();
163 FuncInst * curr_inst = it->getVal();
165 prev_inst->add_succ(curr_inst);
166 curr_inst->add_pred(prev_inst);
172 /* @param tid thread id
173 * Store the values read by atomic read actions into thrd_read_map */
174 void FuncNode::store_read(ModelAction * act, uint32_t tid)
178 void * location = act->get_location();
179 uint64_t read_from_val = act->get_reads_from_value();
181 /* resize and initialize */
182 uint32_t old_size = thrd_read_map.size();
183 if (old_size <= tid) {
184 thrd_read_map.resize(tid + 1);
185 for (uint32_t i = old_size; i < tid + 1;i++)
186 thrd_read_map[i] = new read_map_t();
189 read_map_t * read_map = thrd_read_map[tid];
190 read_map->put(location, read_from_val);
192 /* Store the memory locations where atomic reads happen */
193 // read_locations.add(location);
196 uint64_t FuncNode::query_last_read(void * location, uint32_t tid)
198 if (thrd_read_map.size() <= tid)
201 read_map_t * read_map = thrd_read_map[tid];
203 /* last read value not found */
204 if ( !read_map->contains(location) )
207 uint64_t read_val = read_map->get(location);
211 /* @param tid thread id
212 * Reset read map for a thread. This function shall only be called
213 * when a thread exits a function
215 void FuncNode::clear_read_map(uint32_t tid)
217 if (thrd_read_map.size() <= tid)
220 thrd_read_map[tid]->reset();
223 void FuncNode::update_predicate_tree(action_list_t * act_list, HashTable<ModelAction *, FuncInst *, uintptr_t, 4> * act_inst_map)
225 if (act_list == NULL || act_list->size() == 0)
228 if (predicate_tree_initialized) {
231 predicate_tree_initialized = true;
233 /* map a FuncInst to the parent of its predicate */
234 HashTable<FuncInst *, Predicate *, uintptr_t, 0> inst_pred_map(128);
235 HashTable<FuncInst *, uint64_t, uintptr_t, 4> read_val_map(128);
236 HashTable<void *, FuncInst *, uintptr_t, 4> loc_inst_map(128);
238 sllnode<ModelAction *> *it = act_list->begin();
239 Predicate * curr_pred = predicate_tree_entry;
242 ModelAction * curr_act = it->getVal();
243 FuncInst * curr_inst = act_inst_map->get(curr_act);
244 Predicate * old_pred = curr_pred;
245 read_val_map.put(curr_inst, curr_act->get_reads_from_value());
247 bool branch_found = follow_branch(&curr_pred, curr_inst, &read_val_map, &loc_inst_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(curr_inst)) {
258 back_pred = inst_pred_map.get(curr_inst);
259 curr_pred->set_backedge(back_pred);
260 curr_pred = back_pred;
265 if (!inst_pred_map.contains(curr_inst))
266 inst_pred_map.put(curr_inst, old_pred);
269 if ( loc_inst_map.contains(curr_inst->get_location()) ) {
270 Predicate * new_pred1 = new Predicate(curr_inst);
271 new_pred1->add_predicate(EQUALITY, curr_inst->get_location(), true);
273 Predicate * new_pred2 = new Predicate(curr_inst);
274 new_pred2->add_predicate(EQUALITY, curr_inst->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 FuncInst * last_inst = loc_inst_map.get(curr_inst->get_location());
282 uint64_t last_read = read_val_map.get(last_inst);
283 if ( last_read == read_val_map.get(curr_inst) )
284 curr_pred = new_pred1;
286 curr_pred = new_pred2;
288 Predicate * new_pred = new Predicate(curr_inst);
289 curr_pred->add_child(new_pred);
290 //new_pred->add_parent(curr_pred);
292 curr_pred = new_pred;
296 loc_inst_map.put(curr_inst->get_location(), curr_inst);
300 // model_print("function %s\n", func_name);
301 // print_predicate_tree();
304 /* Given curr_pred and next_inst, find the branch following curr_pred that contains next_inst and the correct predicate
305 * @return true if branch found, false otherwise.
307 bool FuncNode::follow_branch(Predicate ** curr_pred, FuncInst * next_inst,
308 HashTable<FuncInst *, uint64_t, uintptr_t, 4> * read_val_map, HashTable<void *, FuncInst *, uintptr_t, 4> * loc_inst_map)
310 /* check if a branch with func_inst and corresponding predicate exists */
311 bool branch_found = false;
312 ModelVector<Predicate *> * branches = (*curr_pred)->get_children();
313 for (uint i = 0; i < branches->size(); i++) {
314 Predicate * branch = (*branches)[i];
315 if (branch->get_func_inst() != next_inst)
318 PredExprSet * pred_expressions = branch->get_pred_expressions();
320 /* no predicate, follow the only branch */
321 if (pred_expressions->getSize() == 0) {
322 // model_print("no predicate exists: "); next_inst->print();
328 PredExprSetIter * pred_expr_it = pred_expressions->iterator();
329 while (pred_expr_it->hasNext()) {
330 pred_expr * pred_expression = pred_expr_it->next();
331 uint64_t last_read, next_read;
332 FuncInst * last_inst;
335 switch(pred_expression->token) {
337 last_inst = loc_inst_map->get(next_inst->get_location());
338 last_read = read_val_map->get(last_inst);
339 next_read = read_val_map->get(next_inst);
340 equality = (last_read == next_read);
342 if (equality == pred_expression->value) {
344 // model_print("predicate: token: %d, location: %p, value: %d - ", pred_expression->token, pred_expression->location, pred_expression->value); next_inst->print();
351 model_print("unkown predicate token\n");
361 void FuncNode::print_predicate_tree()
363 model_print("digraph function_%s {\n", func_name);
364 predicate_tree_entry->print_pred_subtree();
365 model_print("}\n"); // end of graph
368 /* @param tid thread id
369 * Print the values read by the last read actions for each memory location
372 void FuncNode::print_last_read(uint32_t tid)
374 ASSERT(thrd_read_map.size() > tid);
375 read_map_t * read_map = thrd_read_map[tid];
377 mllnode<void *> * it;
378 for (it = read_locations.begin();it != NULL;it=it->getNext()) {
379 if ( !read_map->contains(it->getVal()) )
382 uint64_t read_val = read_map->get(it->getVal());
383 model_print("last read of thread %d at %p: 0x%x\n", tid, it->getVal(), read_val);