change the way to detect loops
[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         exit_count(0),
7         func_inst_map(),
8         inst_list(),
9         entry_insts(),
10         thrd_read_map()
11 {
12         predicate_tree_entry->add_predicate_expr(NOPREDICATE, NULL, true);
13 }
14
15 /* Check whether FuncInst with the same type, position, and location
16  * as act has been added to func_inst_map or not. If not, add it.
17  *
18  * Note: currently, actions with the same position are filtered out by process_action,
19  * so the collision list of FuncInst is not used. May remove it later. 
20  */
21 void FuncNode::add_inst(ModelAction *act)
22 {
23         ASSERT(act);
24         const char * position = act->get_position();
25
26         /* THREAD* actions, ATOMIC_LOCK, ATOMIC_TRYLOCK, and ATOMIC_UNLOCK
27          * actions are not tagged with their source line numbers
28          */
29         if (position == NULL)
30                 return;
31
32         if ( func_inst_map.contains(position) ) {
33                 FuncInst * inst = func_inst_map.get(position);
34
35                 ASSERT(inst->get_type() == act->get_type());
36                 if (inst->get_location() != act->get_location())
37                         inst->not_single_location();
38
39                 return;
40         }
41
42         FuncInst * func_inst = new FuncInst(act, this);
43
44         func_inst_map.put(position, func_inst);
45         inst_list.push_back(func_inst);
46 }
47
48 /* Get the FuncInst with the same type, position, and location
49  * as act
50  *
51  * @return FuncInst with the same type, position, and location as act */
52 FuncInst * FuncNode::get_inst(ModelAction *act)
53 {
54         ASSERT(act);
55         const char * position = act->get_position();
56
57         /* THREAD* actions, ATOMIC_LOCK, ATOMIC_TRYLOCK, and ATOMIC_UNLOCK
58          * actions are not tagged with their source line numbers
59          */
60         if (position == NULL)
61                 return NULL;
62
63         FuncInst * inst = func_inst_map.get(position);
64         if (inst == NULL)
65                 return NULL;
66
67         action_type inst_type = inst->get_type();
68         action_type act_type = act->get_type();
69
70         // else if branch: an RMWRCAS action is converted to a RMW or READ action
71         if (inst_type == act_type)
72                 return inst;
73         else if (inst_type == ATOMIC_RMWRCAS &&
74                         (act_type == ATOMIC_RMW || act_type == ATOMIC_READ))
75                 return inst;
76
77         return NULL;
78 }
79
80
81 void FuncNode::add_entry_inst(FuncInst * inst)
82 {
83         if (inst == NULL)
84                 return;
85
86         mllnode<FuncInst *> * it;
87         for (it = entry_insts.begin(); it != NULL; it = it->getNext()) {
88                 if (inst == it->getVal())
89                         return;
90         }
91
92         entry_insts.push_back(inst);
93 }
94
95 /**
96  * @brief Convert ModelAdtion list to FuncInst list 
97  * @param act_list A list of ModelActions
98  */
99 void FuncNode::update_tree(action_list_t * act_list)
100 {
101         if (act_list == NULL)
102                 return;
103         else if (act_list->size() == 0)
104                 return;
105
106         /* build inst_list from act_list for later processing */
107         func_inst_list_t inst_list;
108         action_list_t read_act_list;
109
110         for (sllnode<ModelAction *> * it = act_list->begin(); it != NULL; it = it->getNext()) {
111                 ModelAction * act = it->getVal();
112                 FuncInst * func_inst = get_inst(act);
113
114                 if (func_inst == NULL)
115                         continue;
116
117                 inst_list.push_back(func_inst);
118
119                 if (func_inst->is_read())
120                         read_act_list.push_back(act);
121         }
122
123 //      model_print("function %s\n", func_name);
124         update_inst_tree(&inst_list);
125         update_predicate_tree(&read_act_list);
126 //      deep_update(predicate_tree_entry);
127
128         print_predicate_tree();
129 }
130
131 /** 
132  * @brief Link FuncInsts in inst_list  - add one FuncInst to another's predecessors and successors
133  * @param inst_list A list of FuncInsts
134  */
135 void FuncNode::update_inst_tree(func_inst_list_t * inst_list)
136 {
137         if (inst_list == NULL)
138                 return;
139         else if (inst_list->size() == 0)
140                 return;
141
142         /* start linking */
143         sllnode<FuncInst *>* it = inst_list->begin();
144         sllnode<FuncInst *>* prev;
145
146         /* add the first instruction to the list of entry insts */
147         FuncInst * entry_inst = it->getVal();
148         add_entry_inst(entry_inst);
149
150         it = it->getNext();
151         while (it != NULL) {
152                 prev = it->getPrev();
153
154                 FuncInst * prev_inst = prev->getVal();
155                 FuncInst * curr_inst = it->getVal();
156
157                 prev_inst->add_succ(curr_inst);
158                 curr_inst->add_pred(prev_inst);
159
160                 it = it->getNext();
161         }
162 }
163
164 /* @param tid thread id
165  * Store the values read by atomic read actions into thrd_read_map */
166 void FuncNode::store_read(ModelAction * act, uint32_t tid)
167 {
168         ASSERT(act);
169
170         void * location = act->get_location();
171         uint64_t read_from_val = act->get_reads_from_value();
172
173         /* resize and initialize */
174         uint32_t old_size = thrd_read_map.size();
175         if (old_size <= tid) {
176                 thrd_read_map.resize(tid + 1);
177                 for (uint32_t i = old_size; i < tid + 1;i++)
178                         thrd_read_map[i] = new read_map_t();
179         }
180
181         read_map_t * read_map = thrd_read_map[tid];
182         read_map->put(location, read_from_val);
183
184         /* Store the memory locations where atomic reads happen */
185         // read_locations.add(location);
186 }
187
188 uint64_t FuncNode::query_last_read(void * location, uint32_t tid)
189 {
190         if (thrd_read_map.size() <= tid)
191                 return VALUE_NONE;
192
193         read_map_t * read_map = thrd_read_map[tid];
194
195         /* last read value not found */
196         if ( !read_map->contains(location) )
197                 return VALUE_NONE;
198
199         uint64_t read_val = read_map->get(location);
200         return read_val;
201 }
202
203 /* @param tid thread id
204  * Reset read map for a thread. This function shall only be called
205  * when a thread exits a function
206  */
207 void FuncNode::clear_read_map(uint32_t tid)
208 {
209         if (thrd_read_map.size() <= tid)
210                 return;
211
212         thrd_read_map[tid]->reset();
213 }
214
215 void FuncNode::update_predicate_tree(action_list_t * act_list)
216 {
217         if (act_list == NULL || act_list->size() == 0)
218                 return;
219 /*
220         if (predicate_tree_initialized) {
221                 return;
222         }
223         predicate_tree_initialized = true;
224 */
225         /* map a FuncInst to the its predicate */
226         HashTable<FuncInst *, Predicate *, uintptr_t, 0> inst_pred_map(128);
227
228         // number FuncInsts to detect loops
229         HashTable<FuncInst *, uint32_t, uintptr_t, 0> inst_id_map(128);
230         uint32_t inst_counter = 0;
231
232         HashTable<void *, ModelAction *, uintptr_t, 0> loc_act_map(128);
233         HashTable<FuncInst *, ModelAction *, uintptr_t, 0> inst_act_map(128);
234
235         sllnode<ModelAction *> *it = act_list->begin();
236         Predicate * curr_pred = predicate_tree_entry;
237         while (it != NULL) {
238                 ModelAction * next_act = it->getVal();
239                 FuncInst * next_inst = get_inst(next_act);
240                 SnapVector<Predicate *> * unset_predicates = new SnapVector<Predicate *>();
241
242                 bool branch_found = follow_branch(&curr_pred, next_inst, next_act, &inst_act_map, unset_predicates);
243
244                 // no predicate expressions, follow the only branch
245                 if (!branch_found && unset_predicates->size() != 0) {
246                         ASSERT(unset_predicates->size() == 1);
247                         Predicate * one_branch = (*unset_predicates)[0];
248                         curr_pred = one_branch;
249                         branch_found = true;
250                 }
251
252                 delete unset_predicates;
253
254                 // detect loops
255                 if (!branch_found && inst_id_map.contains(next_inst)) {
256                         FuncInst * curr_inst = curr_pred->get_func_inst();
257                         uint32_t curr_id = inst_id_map.get(curr_inst);
258                         uint32_t next_id = inst_id_map.get(next_inst);
259
260                         if (curr_id >= next_id) {
261                                 Predicate * old_pred = inst_pred_map.get(next_inst);
262                                 Predicate * back_pred = old_pred->get_parent();
263
264                                 curr_pred->add_backedge(back_pred);
265                                 curr_pred = back_pred;
266
267                                 continue;
268                         }
269                 }
270
271                 if (!branch_found) {
272                         if ( loc_act_map.contains(next_act->get_location()) ) {
273                                 ModelAction * last_act = loc_act_map.get(next_act->get_location());
274                                 FuncInst * last_inst = get_inst(last_act);
275
276                                 Predicate * new_pred1 = new Predicate(next_inst);
277                                 new_pred1->add_predicate_expr(EQUALITY, last_inst, true);
278
279                                 Predicate * new_pred2 = new Predicate(next_inst);
280                                 new_pred2->add_predicate_expr(EQUALITY, last_inst, false);
281
282                                 curr_pred->add_child(new_pred1);
283                                 curr_pred->add_child(new_pred2);
284                                 new_pred1->set_parent(curr_pred);
285                                 new_pred2->set_parent(curr_pred);
286
287                                 uint64_t last_read = last_act->get_reads_from_value();
288                                 uint64_t next_read = next_act->get_reads_from_value();
289
290                                 if ( last_read == next_read )
291                                         curr_pred = new_pred1;
292                                 else
293                                         curr_pred = new_pred2;
294                         } else if (!next_inst->is_single_location()) {
295                                 Predicate * new_pred1 = new Predicate(next_inst);
296                                 new_pred1->add_predicate_expr(NULLITY, NULL, true);
297
298                                 Predicate * new_pred2 = new Predicate(next_inst);
299                                 new_pred2->add_predicate_expr(NULLITY, NULL, false);
300
301                                 curr_pred->add_child(new_pred1);
302                                 curr_pred->add_child(new_pred2);
303                                 new_pred1->set_parent(curr_pred);
304                                 new_pred2->set_parent(curr_pred);
305
306                                 uint64_t next_read = next_act->get_reads_from_value();
307                                 bool isnull = ((void*)next_read == NULL);
308                                 if (isnull)
309                                         curr_pred = new_pred1;
310                                 else
311                                         curr_pred = new_pred2;
312                         } else {
313                                 Predicate * new_pred = new Predicate(next_inst);
314                                 curr_pred->add_child(new_pred);
315                                 new_pred->set_parent(curr_pred);
316
317                                 if (curr_pred->is_entry_predicate())
318                                         new_pred->add_predicate_expr(NOPREDICATE, NULL, true);
319
320                                 curr_pred = new_pred;
321                         }
322                 }
323
324                 inst_pred_map.put(next_inst, curr_pred);
325                 if (!inst_id_map.contains(next_inst))
326                         inst_id_map.put(next_inst, inst_counter++);
327
328                 loc_act_map.put(next_act->get_location(), next_act);
329                 inst_act_map.put(next_inst, next_act);
330                 it = it->getNext();
331         }
332 }
333
334 void FuncNode::deep_update(Predicate * curr_pred)
335 {
336         FuncInst * func_inst = curr_pred->get_func_inst();
337         if (func_inst != NULL && !func_inst->is_single_location()) {
338                 bool has_null_pred = false;
339                 PredExprSet * pred_expressions = curr_pred->get_pred_expressions();
340                 PredExprSetIter * pred_expr_it = pred_expressions->iterator();
341                 while (pred_expr_it->hasNext()) {
342                         pred_expr * pred_expression = pred_expr_it->next();
343                         if (pred_expression->token == NULLITY) {
344                                 has_null_pred = true;
345                                 break;
346                         }
347                 }
348
349                 if (!has_null_pred) {
350 //                      func_inst->print();
351                         Predicate * another_branch = new Predicate(func_inst);
352                         another_branch->copy_predicate_expr(curr_pred);
353                         another_branch->add_predicate_expr(NULLITY, NULL, 1);
354                         curr_pred->add_predicate_expr(NULLITY, NULL, 0);
355
356                         Predicate * parent = curr_pred->get_parent();
357                         parent->add_child(another_branch);
358                 }
359         }
360
361         ModelVector<Predicate *> * branches = curr_pred->get_children();
362         for (uint i = 0; i < branches->size(); i++) {
363                 Predicate * branch = (*branches)[i];
364                 deep_update(branch);
365         }
366 }
367
368 /* Given curr_pred and next_inst, find the branch following curr_pred that
369  * contains next_inst and the correct predicate. 
370  * @return true if branch found, false otherwise.
371  */
372 bool FuncNode::follow_branch(Predicate ** curr_pred, FuncInst * next_inst, ModelAction * next_act,
373         HashTable<FuncInst *, ModelAction *, uintptr_t, 0> * inst_act_map,
374         SnapVector<Predicate *> * unset_predicates)
375 {
376         /* check if a branch with func_inst and corresponding predicate exists */
377         bool branch_found = false;
378         ModelVector<Predicate *> * branches = (*curr_pred)->get_children();
379         for (uint i = 0; i < branches->size(); i++) {
380                 Predicate * branch = (*branches)[i];
381                 if (branch->get_func_inst() != next_inst)
382                         continue;
383
384                 /* check against predicate expressions */
385                 bool predicate_correct = true;
386                 PredExprSet * pred_expressions = branch->get_pred_expressions();
387                 PredExprSetIter * pred_expr_it = pred_expressions->iterator();
388
389                 if (pred_expressions->getSize() == 0) {
390                         predicate_correct = false;
391                         unset_predicates->push_back(branch);
392                 }
393
394                 while (pred_expr_it->hasNext()) {
395                         pred_expr * pred_expression = pred_expr_it->next();
396                         uint64_t last_read, next_read;
397                         bool equality;
398
399                         switch(pred_expression->token) {
400                                 case NOPREDICATE:
401                                         predicate_correct = true;
402                                         break;
403                                 case EQUALITY:
404                                         FuncInst * to_be_compared;
405                                         ModelAction * last_act;
406
407                                         to_be_compared = pred_expression->func_inst;
408                                         last_act = inst_act_map->get(to_be_compared);
409
410                                         last_read = last_act->get_reads_from_value();
411                                         next_read = next_act->get_reads_from_value();
412                                         equality = (last_read == next_read);
413                                         if (equality != pred_expression->value)
414                                                 predicate_correct = false;
415
416                                         break;
417                                 case NULLITY:
418                                         next_read = next_act->get_reads_from_value();
419                                         equality = ((void*)next_read == NULL);
420                                         if (equality != pred_expression->value)
421                                                 predicate_correct = false;
422                                         break;
423                                 default:
424                                         predicate_correct = false;
425                                         model_print("unkown predicate token\n");
426                                         break;
427                         }
428                 }
429
430                 if (predicate_correct) {
431                         *curr_pred = branch;
432                         branch_found = true;
433                         break;
434                 }
435         }
436
437         return branch_found;
438 }
439
440 void FuncNode::print_predicate_tree()
441 {
442         model_print("digraph function_%s {\n", func_name);
443         predicate_tree_entry->print_pred_subtree();
444         model_print("}\n");     // end of graph
445 }
446
447 /* @param tid thread id
448  * Print the values read by the last read actions for each memory location
449  */
450 /*
451 void FuncNode::print_last_read(uint32_t tid)
452 {
453         ASSERT(thrd_read_map.size() > tid);
454         read_map_t * read_map = thrd_read_map[tid];
455
456         mllnode<void *> * it;
457         for (it = read_locations.begin();it != NULL;it=it->getNext()) {
458                 if ( !read_map->contains(it->getVal()) )
459                         break;
460
461                 uint64_t read_val = read_map->get(it->getVal());
462                 model_print("last read of thread %d at %p: 0x%x\n", tid, it->getVal(), read_val);
463         }
464 }
465 */