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