5 predicate_tree_initialized(false),
10 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 not, add it.
17 * Note: currently, actions with the same position are filtered out by process_action,
18 * so the collision list of FuncInst is not used. May remove it later.
20 void FuncNode::add_inst(ModelAction *act)
23 const char * position = act->get_position();
25 /* THREAD* actions, ATOMIC_LOCK, ATOMIC_TRYLOCK, and ATOMIC_UNLOCK
26 * actions are not tagged with their source line numbers
31 if ( func_inst_map.contains(position) ) {
32 FuncInst * inst = func_inst_map.get(position);
34 if (inst->get_type() != act->get_type() ) {
35 // model_print("action with a different type occurs at line number %s\n", position);
36 FuncInst * func_inst = inst->search_in_collision(act);
38 if (func_inst != NULL)
41 func_inst = new FuncInst(act, this);
42 inst->get_collisions()->push_back(func_inst);
43 inst_list.push_back(func_inst); // delete?
49 FuncInst * func_inst = new FuncInst(act, this);
51 func_inst_map.put(position, func_inst);
52 inst_list.push_back(func_inst);
55 /* Get the FuncInst with the same type, position, and location
58 * @return FuncInst with the same type, position, and location as act */
59 FuncInst * FuncNode::get_inst(ModelAction *act)
62 const char * position = act->get_position();
64 /* THREAD* actions, ATOMIC_LOCK, ATOMIC_TRYLOCK, and ATOMIC_UNLOCK
65 * actions are not tagged with their source line numbers
70 FuncInst * inst = func_inst_map.get(position);
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 func_inst_list_t read_inst_list;
116 HashTable<FuncInst *, uint64_t, uintptr_t, 4> read_val_map;
118 for (sllnode<ModelAction *> * it = act_list->begin(); it != NULL; it = it->getNext()) {
119 ModelAction * act = it->getVal();
120 FuncInst * func_inst = get_inst(act);
122 if (func_inst == NULL)
125 inst_list.push_back(func_inst);
127 // model_print("position: %s ", act->get_position());
130 if (func_inst->is_read()) {
131 read_inst_list.push_back(func_inst);
132 read_val_map.put(func_inst, act->get_reads_from_value());
136 update_inst_tree(&inst_list);
137 update_predicate_tree(&read_inst_list, &read_val_map);
141 * @brief Link FuncInsts in inst_list - add one FuncInst to another's predecessors and successors
142 * @param inst_list A list of FuncInsts
144 void FuncNode::update_inst_tree(func_inst_list_t * inst_list)
146 if (inst_list == NULL)
148 else if (inst_list->size() == 0)
152 sllnode<FuncInst *>* it = inst_list->begin();
153 sllnode<FuncInst *>* prev;
155 /* add the first instruction to the list of entry insts */
156 FuncInst * entry_inst = it->getVal();
157 add_entry_inst(entry_inst);
161 prev = it->getPrev();
163 FuncInst * prev_inst = prev->getVal();
164 FuncInst * curr_inst = it->getVal();
166 prev_inst->add_succ(curr_inst);
167 curr_inst->add_pred(prev_inst);
173 /* @param tid thread id
174 * Store the values read by atomic read actions into thrd_read_map */
175 void FuncNode::store_read(ModelAction * act, uint32_t tid)
179 void * location = act->get_location();
180 uint64_t read_from_val = act->get_reads_from_value();
182 /* resize and initialize */
183 uint32_t old_size = thrd_read_map.size();
184 if (old_size <= tid) {
185 thrd_read_map.resize(tid + 1);
186 for (uint32_t i = old_size; i < tid + 1;i++)
187 thrd_read_map[i] = new read_map_t();
190 read_map_t * read_map = thrd_read_map[tid];
191 read_map->put(location, read_from_val);
193 /* Store the memory locations where atomic reads happen */
194 // read_locations.add(location);
197 uint64_t FuncNode::query_last_read(void * location, uint32_t tid)
199 if (thrd_read_map.size() <= tid)
202 read_map_t * read_map = thrd_read_map[tid];
204 /* last read value not found */
205 if ( !read_map->contains(location) )
208 uint64_t read_val = read_map->get(location);
212 /* @param tid thread id
213 * Reset read map for a thread. This function shall only be called
214 * when a thread exits a function
216 void FuncNode::clear_read_map(uint32_t tid)
218 if (thrd_read_map.size() <= tid)
221 thrd_read_map[tid]->reset();
224 void FuncNode::update_predicate_tree(func_inst_list_t * inst_list, HashTable<FuncInst *, uint64_t, uintptr_t, 4> * read_val_map)
226 if (inst_list == NULL || inst_list->size() == 0)
229 if (predicate_tree_initialized) {
232 predicate_tree_initialized = true;
234 // maybe restrict the size of hashtable to save calloc time
235 HashTable<void *, FuncInst *, uintptr_t, 4> loc_inst_map(64);
237 sllnode<FuncInst *> *it = inst_list->begin();
238 FuncInst * entry_inst = it->getVal();
240 /* get the unique Predicate pointer, assuming entry instructions have no predicate expression */
241 Predicate * curr_pred = NULL;
242 PredSetIter * pit = predicate_tree_entry.iterator();
243 while (pit->hasNext()) {
244 Predicate * p = pit->next();
245 if (p->get_func_inst() == entry_inst) {
250 if (curr_pred == NULL) {
251 curr_pred = new Predicate(entry_inst);
252 curr_pred->set_entry_predicate();
253 predicate_tree_entry.add(curr_pred);
256 loc_inst_map.put(entry_inst->get_location(), entry_inst);
260 FuncInst * curr_inst = it->getVal();
261 bool branch_found = follow_branch(&curr_pred, curr_inst, read_val_map, &loc_inst_map);
264 if ( loc_inst_map.contains(curr_inst->get_location()) ) {
265 Predicate * new_pred1 = new Predicate(curr_inst);
266 new_pred1->add_predicate(EQUALITY, curr_inst->get_location(), true);
268 Predicate * new_pred2 = new Predicate(curr_inst);
269 new_pred2->add_predicate(EQUALITY, curr_inst->get_location(), false);
271 /* TODO: add to inst_pred_map */
272 curr_pred->add_child(new_pred1);
273 curr_pred->add_child(new_pred2);
275 FuncInst * last_inst = loc_inst_map.get(curr_inst->get_location());
276 uint64_t last_read = read_val_map->get(last_inst);
277 if ( last_read == read_val_map->get(curr_inst) )
278 curr_pred = new_pred1;
280 curr_pred = new_pred2;
282 Predicate * new_pred = new Predicate(curr_inst);
283 curr_pred->add_child(new_pred);
284 curr_pred = new_pred;
288 loc_inst_map.put(curr_inst->get_location(), curr_inst);
293 // model_print("function %s\n", func_name);
294 // print_predicate_tree();
297 /* Given curr_pred and next_inst, find the branch following curr_pred that contains next_inst and the correct predicate
298 * @return true if branch found, false otherwise.
300 bool FuncNode::follow_branch(Predicate ** curr_pred, FuncInst * next_inst,
301 HashTable<FuncInst *, uint64_t, uintptr_t, 4> * read_val_map, HashTable<void *, FuncInst *, uintptr_t, 4> * loc_inst_map)
303 /* check if a branch with func_inst and corresponding predicate exists */
304 bool branch_found = false;
305 ModelVector<Predicate *> * branches = (*curr_pred)->get_children();
306 for (uint i = 0; i < branches->size(); i++) {
307 Predicate * branch = (*branches)[i];
308 if (branch->get_func_inst() != next_inst)
311 PredExprSet * pred_expressions = branch->get_pred_expressions();
313 /* no predicate, follow the only branch */
314 if (pred_expressions->getSize() == 0) {
315 // model_print("no predicate exists: "); next_inst->print();
321 PredExprSetIter * pred_expr_it = pred_expressions->iterator();
322 while (pred_expr_it->hasNext()) {
323 pred_expr * pred_expression = pred_expr_it->next();
324 uint64_t last_read, curr_read;
325 FuncInst * last_inst;
328 switch(pred_expression->token) {
330 last_inst = loc_inst_map->get(next_inst->get_location());
331 last_read = read_val_map->get(last_inst);
332 curr_read = read_val_map->get(next_inst);
333 equality = (last_read == curr_read);
334 if (equality == pred_expression->value) {
336 // model_print("predicate: token: %d, location: %p, value: %d - ", pred_expression->token, pred_expression->location, pred_expression->value); next_inst->print();
343 model_print("unkown predicate token\n");
353 void FuncNode::print_predicate_tree()
355 model_print("digraph function_%s {\n", func_name);
356 PredSetIter * it = predicate_tree_entry.iterator();
358 while (it->hasNext()) {
359 Predicate * p = it->next();
360 p->print_pred_subtree();
362 model_print("}\n"); // end of graph
365 /* @param tid thread id
366 * Print the values read by the last read actions for each memory location
369 void FuncNode::print_last_read(uint32_t tid)
371 ASSERT(thrd_read_map.size() > tid);
372 read_map_t * read_map = thrd_read_map[tid];
374 mllnode<void *> * it;
375 for (it = read_locations.begin();it != NULL;it=it->getNext()) {
376 if ( !read_map->contains(it->getVal()) )
379 uint64_t read_val = read_map->get(it->getVal());
380 model_print("last read of thread %d at %p: 0x%x\n", tid, it->getVal(), read_val);