5 predicate_tree_initialized(false),
10 predicate_tree_entry()
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 func_inst_list_t read_inst_list;
115 HashTable<FuncInst *, uint64_t, uintptr_t, 4> read_val_map;
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_inst_list.push_back(func_inst);
131 read_val_map.put(func_inst, act->get_reads_from_value());
135 update_inst_tree(&inst_list);
136 init_predicate_tree(&read_inst_list, &read_val_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::init_predicate_tree(func_inst_list_t * inst_list, HashTable<FuncInst *, uint64_t, uintptr_t, 4> * read_val_map)
225 if (inst_list == NULL || inst_list->size() == 0)
228 if (predicate_tree_initialized) {
231 predicate_tree_initialized = true;
233 // maybe restrict the size of hashtable to save calloc time
234 HashTable<void *, FuncInst *, uintptr_t, 4> loc_inst_map(64);
236 sllnode<FuncInst *> *it = inst_list->begin();
237 FuncInst * entry_inst = it->getVal();
239 /* get the unique Predicate pointer, assuming entry instructions have no predicate expression */
240 Predicate * curr_pred = NULL;
241 PredSetIter * pit = predicate_tree_entry.iterator();
242 while (pit->hasNext()) {
243 Predicate * p = pit->next();
244 p->get_func_inst()->print();
245 if (p->get_func_inst() == entry_inst) {
250 if (curr_pred == NULL) {
251 curr_pred = new Predicate(entry_inst);
252 predicate_tree_entry.add(curr_pred);
255 loc_inst_map.put(entry_inst->get_location(), entry_inst);
259 FuncInst * curr_inst = it->getVal();
260 bool branch_found = false;
262 /* check if a branch with the same func_inst and corresponding predicate exists */
263 ModelVector<Predicate *> * branches = curr_pred->get_children();
264 for (uint i = 0; i < branches->size(); i++) {
265 Predicate * branch = (*branches)[i];
266 if (branch->get_func_inst() != curr_inst)
269 PredExprSet * pred_expressions = branch->get_pred_expressions();
271 /* no predicate, follow the only branch */
272 if (pred_expressions->getSize() == 0) {
273 model_print("no predicate exists: "); curr_inst->print();
279 PredExprSetIter * pred_expr_it = pred_expressions->iterator();
280 while (pred_expr_it->hasNext()) {
281 pred_expr * pred_expression = pred_expr_it->next();
282 uint64_t last_read, curr_read;
283 FuncInst * last_inst;
286 switch(pred_expression->token) {
288 last_inst = loc_inst_map.get(curr_inst->get_location());
289 last_read = read_val_map->get(last_inst);
290 curr_read = read_val_map->get(curr_inst);
291 equality = (last_read == curr_read);
292 if (equality == pred_expression->value) {
294 model_print("predicate: token: %d, location: %p, value: %d - ", pred_expression->token, pred_expression->location, pred_expression->value); curr_inst->print();
301 model_print("unkown predicate token\n");
309 if ( loc_inst_map.contains(curr_inst->get_location()) ) {
310 Predicate * new_pred1 = new Predicate(curr_inst);
311 new_pred1->add_predicate(EQUALITY, curr_inst->get_location(), true);
313 Predicate * new_pred2 = new Predicate(curr_inst);
314 new_pred2->add_predicate(EQUALITY, curr_inst->get_location(), false);
316 curr_pred->add_child(new_pred1);
317 curr_pred->add_child(new_pred2);
319 FuncInst * last_inst = loc_inst_map.get(curr_inst->get_location());
320 uint64_t last_read = read_val_map->get(last_inst);
321 if ( last_read == read_val_map->get(curr_inst) )
322 curr_pred = new_pred1;
324 curr_pred = new_pred2;
326 Predicate * new_pred = new Predicate(curr_inst);
327 curr_pred->add_child(new_pred);
328 curr_pred = new_pred;
332 loc_inst_map.put(curr_inst->get_location(), curr_inst);
337 model_print("function %s\n", func_name);
338 print_predicate_tree();
342 void FuncNode::print_predicate_tree()
344 model_print("digraph function_%s {\n", func_name);
345 PredSetIter * it = predicate_tree_entry.iterator();
347 while (it->hasNext()) {
348 Predicate * p = it->next();
349 p->print_pred_subtree();
351 model_print("}\n"); // end of graph
354 /* @param tid thread id
355 * Print the values read by the last read actions for each memory location
358 void FuncNode::print_last_read(uint32_t tid)
360 ASSERT(thrd_read_map.size() > tid);
361 read_map_t * read_map = thrd_read_map[tid];
363 mllnode<void *> * it;
364 for (it = read_locations.begin();it != NULL;it=it->getNext()) {
365 if ( !read_map->contains(it->getVal()) )
368 uint64_t read_val = read_map->get(it->getVal());
369 model_print("last read of thread %d at %p: 0x%x\n", tid, it->getVal(), read_val);