6 predicate_tree_initialized(false),
11 predicate_tree_entry()
14 /* Check whether FuncInst with the same type, position, and location
15 * as act has been added to func_inst_map or not. If so, return it;
16 * if not, add it and return it.
18 * @return FuncInst with the same type, position, and location as act */
19 FuncInst * FuncNode::get_or_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) {
38 // return the FuncInst found in the collision list
42 func_inst = new FuncInst(act, this);
43 inst->get_collisions()->push_back(func_inst);
44 inst_list.push_back(func_inst); // delete?
52 FuncInst * func_inst = new FuncInst(act, this);
54 func_inst_map.put(position, func_inst);
55 inst_list.push_back(func_inst);
60 void FuncNode::add_entry_inst(FuncInst * inst)
65 mllnode<FuncInst *> * it;
66 for (it = entry_insts.begin(); it != NULL; it = it->getNext()) {
67 if (inst == it->getVal())
71 entry_insts.push_back(inst);
75 * @brief Convert ModelAdtion list to FuncInst list
76 * @param act_list A list of ModelActions
78 void FuncNode::update_tree(action_list_t * act_list)
82 else if (act_list->size() == 0)
85 /* build inst_list from act_list for later processing */
86 func_inst_list_t inst_list;
87 func_inst_list_t read_inst_list;
88 HashTable<FuncInst *, uint64_t, uintptr_t, 4> read_val_map;
90 for (sllnode<ModelAction *> * it = act_list->begin(); it != NULL; it = it->getNext()) {
91 ModelAction * act = it->getVal();
92 FuncInst * func_inst = get_or_add_inst(act);
94 if (func_inst == NULL)
97 inst_list.push_back(func_inst);
99 /* if (!predicate_tree_initialized) {
100 model_print("position: %s ", act->get_position());
105 if (func_inst->is_read()) {
106 read_inst_list.push_back(func_inst);
107 read_val_map.put(func_inst, act->get_reads_from_value());
111 update_inst_tree(&inst_list);
112 init_predicate_tree(&read_inst_list, &read_val_map);
116 * @brief Link FuncInsts in inst_list - add one FuncInst to another's predecessors and successors
117 * @param inst_list A list of FuncInsts
119 void FuncNode::update_inst_tree(func_inst_list_t * inst_list)
121 if (inst_list == NULL)
123 else if (inst_list->size() == 0)
127 sllnode<FuncInst *>* it = inst_list->begin();
128 sllnode<FuncInst *>* prev;
130 /* add the first instruction to the list of entry insts */
131 FuncInst * entry_inst = it->getVal();
132 add_entry_inst(entry_inst);
136 prev = it->getPrev();
138 FuncInst * prev_inst = prev->getVal();
139 FuncInst * curr_inst = it->getVal();
141 prev_inst->add_succ(curr_inst);
142 curr_inst->add_pred(prev_inst);
148 /* @param tid thread id
149 * Store the values read by atomic read actions into thrd_read_map */
150 void FuncNode::store_read(ModelAction * act, uint32_t tid)
154 void * location = act->get_location();
155 uint64_t read_from_val = act->get_reads_from_value();
157 /* resize and initialize */
158 uint32_t old_size = thrd_read_map.size();
159 if (old_size <= tid) {
160 thrd_read_map.resize(tid + 1);
161 for (uint32_t i = old_size; i < tid + 1;i++)
162 thrd_read_map[i] = new read_map_t();
165 read_map_t * read_map = thrd_read_map[tid];
166 read_map->put(location, read_from_val);
168 /* Store the memory locations where atomic reads happen */
169 // read_locations.add(location);
172 uint64_t FuncNode::query_last_read(void * location, uint32_t tid)
174 if (thrd_read_map.size() <= tid)
177 read_map_t * read_map = thrd_read_map[tid];
179 /* last read value not found */
180 if ( !read_map->contains(location) )
183 uint64_t read_val = read_map->get(location);
187 /* @param tid thread id
188 * Reset read map for a thread. This function shall only be called
189 * when a thread exits a function
191 void FuncNode::clear_read_map(uint32_t tid)
193 if (thrd_read_map.size() <= tid)
196 thrd_read_map[tid]->reset();
199 void FuncNode::init_predicate_tree(func_inst_list_t * inst_list, HashTable<FuncInst *, uint64_t, uintptr_t, 4> * read_val_map)
201 if (inst_list == NULL || inst_list->size() == 0)
204 if (predicate_tree_initialized) {
208 predicate_tree_initialized = true;
210 // maybe restrict the size of hashtable to save calloc time
211 HashTable<void *, FuncInst *, uintptr_t, 4> loc_inst_map;
213 sllnode<FuncInst *> *it = inst_list->begin();
214 FuncInst * entry_inst = it->getVal();
216 /* entry instruction has no predicate expression */
217 Predicate * curr_pred = new Predicate(entry_inst);
218 predicate_tree_entry.add(curr_pred);
219 loc_inst_map.put(entry_inst->get_location(), entry_inst);
223 FuncInst * curr_inst = it->getVal();
224 if ( loc_inst_map.contains(curr_inst->get_location()) ) {
225 Predicate * new_pred1 = new Predicate(curr_inst);
226 new_pred1->add_predicate(EQUALITY, curr_inst->get_location(), true);
228 Predicate * new_pred2 = new Predicate(curr_inst);
229 new_pred2->add_predicate(EQUALITY, curr_inst->get_location(), false);
231 curr_pred->add_child(new_pred1);
232 curr_pred->add_child(new_pred2);
234 FuncInst * last_inst = loc_inst_map.get(curr_inst->get_location());
236 uint64_t last_read = read_val_map->get(last_inst);
237 if ( last_read == read_val_map->get(curr_inst) )
238 curr_pred = new_pred1;
240 curr_pred = new_pred2;
242 Predicate * new_pred = new Predicate(curr_inst);
243 curr_pred->add_child(new_pred);
244 curr_pred = new_pred;
247 loc_inst_map.put(curr_inst->get_location(), curr_inst);
252 model_print("function %s\n", func_name);
253 print_predicate_tree();
257 void FuncNode::print_predicate_tree()
259 model_print("digraph function_%s {\n", func_name);
260 HSIterator<Predicate *, uintptr_t, 0, model_malloc, model_calloc, model_free> * it;
261 it = predicate_tree_entry.iterator();
263 while (it->hasNext()) {
264 Predicate * p = it->next();
265 p->print_pred_subtree();
267 model_print("}\n"); // end of graph
270 /* @param tid thread id
271 * Print the values read by the last read actions for each memory location
274 void FuncNode::print_last_read(uint32_t tid)
276 ASSERT(thrd_read_map.size() > tid);
277 read_map_t * read_map = thrd_read_map[tid];
279 mllnode<void *> * it;
280 for (it = read_locations.begin();it != NULL;it=it->getNext()) {
281 if ( !read_map->contains(it->getVal()) )
284 uint64_t read_val = read_map->get(it->getVal());
285 model_print("last read of thread %d at %p: 0x%x\n", tid, it->getVal(), read_val);