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