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