do optimization for FuncNode::update_tree
[c11tester.git] / funcnode.cc
1 #include "funcnode.h"
2
3 FuncNode::FuncNode() :
4         predicate_tree_initialized(false),
5         predicate_tree_entry(new Predicate(NULL, true)),
6         func_inst_map(),
7         inst_list(),
8         entry_insts(),
9         thrd_read_map()
10 {}
11
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.
14  *
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. 
17  */
18 void FuncNode::add_inst(ModelAction *act)
19 {
20         ASSERT(act);
21         const char * position = act->get_position();
22
23         /* THREAD* actions, ATOMIC_LOCK, ATOMIC_TRYLOCK, and ATOMIC_UNLOCK
24          * actions are not tagged with their source line numbers
25          */
26         if (position == NULL)
27                 return;
28
29         if ( func_inst_map.contains(position) ) {
30                 FuncInst * inst = func_inst_map.get(position);
31
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);
35
36                         if (func_inst != NULL)
37                                 return;
38
39                         func_inst = new FuncInst(act, this);
40                         inst->get_collisions()->push_back(func_inst);
41                         inst_list.push_back(func_inst); // delete?
42                 }
43
44                 return;
45         }
46
47         FuncInst * func_inst = new FuncInst(act, this);
48
49         func_inst_map.put(position, func_inst);
50         inst_list.push_back(func_inst);
51 }
52
53 /* Get the FuncInst with the same type, position, and location
54  * as act
55  *
56  * @return FuncInst with the same type, position, and location as act */
57 FuncInst * FuncNode::get_inst(ModelAction *act)
58 {
59         ASSERT(act);
60         const char * position = act->get_position();
61
62         /* THREAD* actions, ATOMIC_LOCK, ATOMIC_TRYLOCK, and ATOMIC_UNLOCK
63          * actions are not tagged with their source line numbers
64          */
65         if (position == NULL)
66                 return NULL;
67
68         FuncInst * inst = func_inst_map.get(position);
69         if (inst == NULL)
70                 return NULL;
71
72 //      ASSERT(inst->get_location() == act->get_location());
73
74         action_type inst_type = inst->get_type();
75         action_type act_type = act->get_type();
76
77         // else if branch: an RMWRCAS action is converted to a RMW or READ action
78         if (inst_type == act_type)
79                 return inst;
80         else if (inst_type == ATOMIC_RMWRCAS &&
81                         (act_type == ATOMIC_RMW || act_type == ATOMIC_READ))
82                 return inst;
83
84         return NULL;
85 }
86
87
88 void FuncNode::add_entry_inst(FuncInst * inst)
89 {
90         if (inst == NULL)
91                 return;
92
93         mllnode<FuncInst *> * it;
94         for (it = entry_insts.begin(); it != NULL; it = it->getNext()) {
95                 if (inst == it->getVal())
96                         return;
97         }
98
99         entry_insts.push_back(inst);
100 }
101
102 /**
103  * @brief Convert ModelAdtion list to FuncInst list 
104  * @param act_list A list of ModelActions
105  */
106 void FuncNode::update_tree(action_list_t * act_list)
107 {
108         if (act_list == NULL)
109                 return;
110         else if (act_list->size() == 0)
111                 return;
112
113         /* build inst_list from act_list for later processing */
114         func_inst_list_t inst_list;
115         action_list_t read_act_list;
116
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);
120
121                 if (func_inst == NULL)
122                         continue;
123
124                 inst_list.push_back(func_inst);
125
126 //              model_print("position: %s ", act->get_position());
127 //              act->print();
128
129                 if (func_inst->is_read())
130                         read_act_list.push_back(act);
131         }
132
133         update_inst_tree(&inst_list);
134         update_predicate_tree(&read_act_list);
135 }
136
137 /** 
138  * @brief Link FuncInsts in inst_list  - add one FuncInst to another's predecessors and successors
139  * @param inst_list A list of FuncInsts
140  */
141 void FuncNode::update_inst_tree(func_inst_list_t * inst_list)
142 {
143         if (inst_list == NULL)
144                 return;
145         else if (inst_list->size() == 0)
146                 return;
147
148         /* start linking */
149         sllnode<FuncInst *>* it = inst_list->begin();
150         sllnode<FuncInst *>* prev;
151
152         /* add the first instruction to the list of entry insts */
153         FuncInst * entry_inst = it->getVal();
154         add_entry_inst(entry_inst);
155
156         it = it->getNext();
157         while (it != NULL) {
158                 prev = it->getPrev();
159
160                 FuncInst * prev_inst = prev->getVal();
161                 FuncInst * curr_inst = it->getVal();
162
163                 prev_inst->add_succ(curr_inst);
164                 curr_inst->add_pred(prev_inst);
165
166                 it = it->getNext();
167         }
168 }
169
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)
173 {
174         ASSERT(act);
175
176         void * location = act->get_location();
177         uint64_t read_from_val = act->get_reads_from_value();
178
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();
185         }
186
187         read_map_t * read_map = thrd_read_map[tid];
188         read_map->put(location, read_from_val);
189
190         /* Store the memory locations where atomic reads happen */
191         // read_locations.add(location);
192 }
193
194 uint64_t FuncNode::query_last_read(void * location, uint32_t tid)
195 {
196         if (thrd_read_map.size() <= tid)
197                 return VALUE_NONE;
198
199         read_map_t * read_map = thrd_read_map[tid];
200
201         /* last read value not found */
202         if ( !read_map->contains(location) )
203                 return VALUE_NONE;
204
205         uint64_t read_val = read_map->get(location);
206         return read_val;
207 }
208
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
212  */
213 void FuncNode::clear_read_map(uint32_t tid)
214 {
215         if (thrd_read_map.size() <= tid)
216                 return;
217
218         thrd_read_map[tid]->reset();
219 }
220
221 void FuncNode::update_predicate_tree(action_list_t * act_list)
222 {
223         if (act_list == NULL || act_list->size() == 0)
224                 return;
225 /*
226         if (predicate_tree_initialized) {
227                 return;
228         }
229         predicate_tree_initialized = true;
230 */
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);
234
235         sllnode<ModelAction *> *it = act_list->begin();
236         Predicate * curr_pred = predicate_tree_entry;
237
238         while (it != NULL) {
239                 ModelAction * next_act = it->getVal();
240                 FuncInst * next_inst = get_inst(next_act);
241                 Predicate * old_pred = curr_pred;
242
243                 bool branch_found = follow_branch(&curr_pred, next_inst, next_act, &loc_act_map);
244
245                 // check back edges
246                 if (!branch_found) {
247                         Predicate * back_pred = curr_pred->get_backedge();
248                         if (back_pred != NULL) {
249                                 curr_pred = back_pred;
250                                 continue;
251                         }
252
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;
257                                 continue;
258                         }
259                 }
260
261                 if (!inst_pred_map.contains(next_inst))
262                         inst_pred_map.put(next_inst, old_pred);
263
264                 if (!branch_found) {
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);
268
269                                 Predicate * new_pred2 = new Predicate(next_inst);
270                                 new_pred2->add_predicate(EQUALITY, next_act->get_location(), false);
271
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);
276
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();
280
281                                 if ( last_read == next_read )
282                                         curr_pred = new_pred1;
283                                 else
284                                         curr_pred = new_pred2;
285                         } else {
286                                 Predicate * new_pred = new Predicate(next_inst);
287                                 curr_pred->add_child(new_pred);
288                                 //new_pred->add_parent(curr_pred);
289
290                                 curr_pred = new_pred;
291                         }
292                 }
293
294                 loc_act_map.put(next_act->get_location(), next_act);
295                 it = it->getNext();
296         }
297
298 //      model_print("function %s\n", func_name);
299 //      print_predicate_tree();
300 }
301
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.
304  */
305 bool FuncNode::follow_branch(Predicate ** curr_pred, FuncInst * next_inst, ModelAction * next_act,
306         HashTable<void *, ModelAction *, uintptr_t, 0> * loc_act_map)
307 {
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)
314                         continue;
315
316                 PredExprSet * pred_expressions = branch->get_pred_expressions();
317
318                 /* no predicate, follow the only branch */
319                 if (pred_expressions->getSize() == 0) {
320 //                      model_print("no predicate exists: "); next_inst->print();
321                         *curr_pred = branch;
322                         branch_found = true;
323                         break;
324                 }
325
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;
331                         bool equality;
332
333                         switch(pred_expression->token) {
334                                 case EQUALITY:
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();
338
339                                         equality = (last_read == next_read);
340
341                                         if (equality == pred_expression->value) {
342                                                 *curr_pred = branch;
343 //                                              model_print("predicate: token: %d, location: %p, value: %d - ", pred_expression->token, pred_expression->location, pred_expression->value); next_inst->print();
344                                                 branch_found = true;
345                                         }
346                                         break;
347                                 case NULLITY:
348                                         break;
349                                 default:
350                                         model_print("unkown predicate token\n");
351                                         break;
352                         }
353                 }
354
355         }
356
357         return branch_found;
358 }
359
360 void FuncNode::print_predicate_tree()
361 {
362         model_print("digraph function_%s {\n", func_name);
363         predicate_tree_entry->print_pred_subtree();
364         model_print("}\n");     // end of graph
365 }
366
367 /* @param tid thread id
368  * Print the values read by the last read actions for each memory location
369  */
370 /*
371 void FuncNode::print_last_read(uint32_t tid)
372 {
373         ASSERT(thrd_read_map.size() > tid);
374         read_map_t * read_map = thrd_read_map[tid];
375
376         mllnode<void *> * it;
377         for (it = read_locations.begin();it != NULL;it=it->getNext()) {
378                 if ( !read_map->contains(it->getVal()) )
379                         break;
380
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);
383         }
384 }
385 */