cf651ae8679a4ddcba176caa9a3b9d2ccb8f7b91
[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 //      ASSERT(inst->get_location() == act->get_location());
74
75         action_type inst_type = inst->get_type();
76         action_type act_type = act->get_type();
77
78         // else if branch: an RMWRCAS action is converted to a RMW or READ action
79         if (inst_type == act_type)
80                 return inst;
81         else if (inst_type == ATOMIC_RMWRCAS &&
82                         (act_type == ATOMIC_RMW || act_type == ATOMIC_READ))
83                 return inst;
84
85         return NULL;
86 }
87
88
89 void FuncNode::add_entry_inst(FuncInst * inst)
90 {
91         if (inst == NULL)
92                 return;
93
94         mllnode<FuncInst *> * it;
95         for (it = entry_insts.begin(); it != NULL; it = it->getNext()) {
96                 if (inst == it->getVal())
97                         return;
98         }
99
100         entry_insts.push_back(inst);
101 }
102
103 /**
104  * @brief Convert ModelAdtion list to FuncInst list 
105  * @param act_list A list of ModelActions
106  */
107 void FuncNode::update_tree(action_list_t * act_list)
108 {
109         if (act_list == NULL)
110                 return;
111         else if (act_list->size() == 0)
112                 return;
113
114         /* build inst_list from act_list for later processing */
115         func_inst_list_t inst_list;
116         action_list_t read_act_list;
117         HashTable<ModelAction *, FuncInst *, uintptr_t, 4> act_inst_map;
118
119         for (sllnode<ModelAction *> * it = act_list->begin(); it != NULL; it = it->getNext()) {
120                 ModelAction * act = it->getVal();
121                 FuncInst * func_inst = get_inst(act);
122
123                 if (func_inst == NULL)
124                         continue;
125
126                 inst_list.push_back(func_inst);
127
128 //              model_print("position: %s ", act->get_position());
129 //              act->print();
130
131                 if (func_inst->is_read()) {
132                         read_act_list.push_back(act);
133                         act_inst_map.put(act, func_inst);
134                 }
135         }
136
137         update_inst_tree(&inst_list);
138         update_predicate_tree(&read_act_list, &act_inst_map);
139 }
140
141 /** 
142  * @brief Link FuncInsts in inst_list  - add one FuncInst to another's predecessors and successors
143  * @param inst_list A list of FuncInsts
144  */
145 void FuncNode::update_inst_tree(func_inst_list_t * inst_list)
146 {
147         if (inst_list == NULL)
148                 return;
149         else if (inst_list->size() == 0)
150                 return;
151
152         /* start linking */
153         sllnode<FuncInst *>* it = inst_list->begin();
154         sllnode<FuncInst *>* prev;
155
156         /* add the first instruction to the list of entry insts */
157         FuncInst * entry_inst = it->getVal();
158         add_entry_inst(entry_inst);
159
160         it = it->getNext();
161         while (it != NULL) {
162                 prev = it->getPrev();
163
164                 FuncInst * prev_inst = prev->getVal();
165                 FuncInst * curr_inst = it->getVal();
166
167                 prev_inst->add_succ(curr_inst);
168                 curr_inst->add_pred(prev_inst);
169
170                 it = it->getNext();
171         }
172 }
173
174 /* @param tid thread id
175  * Store the values read by atomic read actions into thrd_read_map */
176 void FuncNode::store_read(ModelAction * act, uint32_t tid)
177 {
178         ASSERT(act);
179
180         void * location = act->get_location();
181         uint64_t read_from_val = act->get_reads_from_value();
182
183         /* resize and initialize */
184         uint32_t old_size = thrd_read_map.size();
185         if (old_size <= tid) {
186                 thrd_read_map.resize(tid + 1);
187                 for (uint32_t i = old_size; i < tid + 1;i++)
188                         thrd_read_map[i] = new read_map_t();
189         }
190
191         read_map_t * read_map = thrd_read_map[tid];
192         read_map->put(location, read_from_val);
193
194         /* Store the memory locations where atomic reads happen */
195         // read_locations.add(location);
196 }
197
198 uint64_t FuncNode::query_last_read(void * location, uint32_t tid)
199 {
200         if (thrd_read_map.size() <= tid)
201                 return 0xdeadbeef;
202
203         read_map_t * read_map = thrd_read_map[tid];
204
205         /* last read value not found */
206         if ( !read_map->contains(location) )
207                 return 0xdeadbeef;
208
209         uint64_t read_val = read_map->get(location);
210         return read_val;
211 }
212
213 /* @param tid thread id
214  * Reset read map for a thread. This function shall only be called
215  * when a thread exits a function
216  */
217 void FuncNode::clear_read_map(uint32_t tid)
218 {
219         if (thrd_read_map.size() <= tid)
220                 return;
221
222         thrd_read_map[tid]->reset();
223 }
224
225 void FuncNode::update_predicate_tree(action_list_t * act_list, HashTable<ModelAction *, FuncInst *, uintptr_t, 4> * act_inst_map)
226 {
227         if (act_list == NULL || act_list->size() == 0)
228                 return;
229 /*
230         if (predicate_tree_initialized) {
231                 return;
232         }
233         predicate_tree_initialized = true;
234 */
235         /* map a FuncInst to the parent of its predicate */
236         HashTable<FuncInst *, Predicate *, uintptr_t, 0> inst_pred_map(128);
237         HashTable<void *, ModelAction *, uintptr_t, 0> loc_act_map(128);
238
239         sllnode<ModelAction *> *it = act_list->begin();
240         Predicate * curr_pred = predicate_tree_entry;
241
242         while (it != NULL) {
243                 ModelAction * next_act = it->getVal();
244                 FuncInst * next_inst = act_inst_map->get(next_act);
245                 Predicate * old_pred = curr_pred;
246
247                 bool branch_found = follow_branch(&curr_pred, next_inst, next_act, &loc_act_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(next_inst)) {
258                                 back_pred = inst_pred_map.get(next_inst);
259                                 curr_pred->set_backedge(back_pred);
260                                 curr_pred = back_pred;
261                                 continue;
262                         }
263                 }
264
265                 if (!inst_pred_map.contains(next_inst))
266                         inst_pred_map.put(next_inst, old_pred);
267
268                 if (!branch_found) {
269                         if ( loc_act_map.contains(next_act->get_location()) ) {
270                                 Predicate * new_pred1 = new Predicate(next_inst);
271                                 new_pred1->add_predicate(EQUALITY, next_act->get_location(), true);
272
273                                 Predicate * new_pred2 = new Predicate(next_inst);
274                                 new_pred2->add_predicate(EQUALITY, next_act->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                                 ModelAction * last_act = loc_act_map.get(next_act->get_location());
282                                 uint64_t last_read = last_act->get_reads_from_value();
283                                 uint64_t next_read = next_act->get_reads_from_value();
284
285                                 if ( last_read == next_read )
286                                         curr_pred = new_pred1;
287                                 else
288                                         curr_pred = new_pred2;
289                         } else {
290                                 Predicate * new_pred = new Predicate(next_inst);
291                                 curr_pred->add_child(new_pred);
292                                 //new_pred->add_parent(curr_pred);
293
294                                 curr_pred = new_pred;
295                         }
296                 }
297
298                 loc_act_map.put(next_act->get_location(), next_act);
299                 it = it->getNext();
300         }
301
302 //      model_print("function %s\n", func_name);
303 //      print_predicate_tree();
304 }
305
306 /* Given curr_pred and next_inst, find the branch following curr_pred that contains next_inst and the correct predicate
307  * @return true if branch found, false otherwise.
308  */
309 bool FuncNode::follow_branch(Predicate ** curr_pred, FuncInst * next_inst, ModelAction * next_act,
310         HashTable<void *, ModelAction *, uintptr_t, 0> * loc_act_map)
311 {
312         /* check if a branch with func_inst and corresponding predicate exists */
313         bool branch_found = false;
314         ModelVector<Predicate *> * branches = (*curr_pred)->get_children();
315         for (uint i = 0; i < branches->size(); i++) {
316                 Predicate * branch = (*branches)[i];
317                 if (branch->get_func_inst() != next_inst)
318                         continue;
319
320                 PredExprSet * pred_expressions = branch->get_pred_expressions();
321
322                 /* no predicate, follow the only branch */
323                 if (pred_expressions->getSize() == 0) {
324 //                      model_print("no predicate exists: "); next_inst->print();
325                         *curr_pred = branch;
326                         branch_found = true;
327                         break;
328                 }
329
330                 PredExprSetIter * pred_expr_it = pred_expressions->iterator();
331                 while (pred_expr_it->hasNext()) {
332                         pred_expr * pred_expression = pred_expr_it->next();
333                         uint64_t last_read, next_read;
334                         ModelAction * last_act;
335                         bool equality;
336
337                         switch(pred_expression->token) {
338                                 case EQUALITY:
339                                         last_act = loc_act_map->get(next_act->get_location());
340                                         last_read = last_act->get_reads_from_value();
341                                         next_read = next_act->get_reads_from_value();
342
343                                         equality = (last_read == next_read);
344
345                                         if (equality == pred_expression->value) {
346                                                 *curr_pred = branch;
347 //                                              model_print("predicate: token: %d, location: %p, value: %d - ", pred_expression->token, pred_expression->location, pred_expression->value); next_inst->print();
348                                                 branch_found = true;
349                                         }
350                                         break;
351                                 case NULLITY:
352                                         break;
353                                 default:
354                                         model_print("unkown predicate token\n");
355                                         break;
356                         }
357                 }
358
359         }
360
361         return branch_found;
362 }
363
364 void FuncNode::print_predicate_tree()
365 {
366         model_print("digraph function_%s {\n", func_name);
367         predicate_tree_entry->print_pred_subtree();
368         model_print("}\n");     // end of graph
369 }
370
371 /* @param tid thread id
372  * Print the values read by the last read actions for each memory location
373  */
374 /*
375 void FuncNode::print_last_read(uint32_t tid)
376 {
377         ASSERT(thrd_read_map.size() > tid);
378         read_map_t * read_map = thrd_read_map[tid];
379
380         mllnode<void *> * it;
381         for (it = read_locations.begin();it != NULL;it=it->getNext()) {
382                 if ( !read_map->contains(it->getVal()) )
383                         break;
384
385                 uint64_t read_val = read_map->get(it->getVal());
386                 model_print("last read of thread %d at %p: 0x%x\n", tid, it->getVal(), read_val);
387         }
388 }
389 */