only generate NULLITY predicate when it has ever been NULL; able to amend predicates...
[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
172         print_predicate_tree();
173 }
174
175 /** 
176  * @brief Link FuncInsts in inst_list  - add one FuncInst to another's predecessors and successors
177  * @param inst_list A list of FuncInsts
178  */
179 void FuncNode::update_inst_tree(func_inst_list_t * inst_list)
180 {
181         if (inst_list == NULL)
182                 return;
183         else if (inst_list->size() == 0)
184                 return;
185
186         /* start linking */
187         sllnode<FuncInst *>* it = inst_list->begin();
188         sllnode<FuncInst *>* prev;
189
190         /* add the first instruction to the list of entry insts */
191         FuncInst * entry_inst = it->getVal();
192         add_entry_inst(entry_inst);
193
194         it = it->getNext();
195         while (it != NULL) {
196                 prev = it->getPrev();
197
198                 FuncInst * prev_inst = prev->getVal();
199                 FuncInst * curr_inst = it->getVal();
200
201                 prev_inst->add_succ(curr_inst);
202                 curr_inst->add_pred(prev_inst);
203
204                 it = it->getNext();
205         }
206 }
207
208 /* @param tid thread id
209  * Store the values read by atomic read actions into thrd_read_map */
210 void FuncNode::store_read(ModelAction * act, uint32_t tid)
211 {
212 /*
213         ASSERT(act);
214
215         void * location = act->get_location();
216         uint64_t read_from_val = act->get_reads_from_value();
217
218         // resize and initialize
219         uint32_t old_size = thrd_read_map.size();
220         if (old_size <= tid) {
221                 thrd_read_map.resize(tid + 1);
222                 for (uint32_t i = old_size; i < tid + 1;i++)
223                         thrd_read_map[i] = new read_map_t();
224         }
225
226         read_map_t * read_map = thrd_read_map[tid];
227         read_map->put(location, read_from_val);
228 */
229 }
230
231 uint64_t FuncNode::query_last_read(void * location, uint32_t tid)
232 {
233 /*
234         if (thrd_read_map.size() <= tid)
235                 return VALUE_NONE;
236
237         read_map_t * read_map = thrd_read_map[tid];
238
239         // last read value not found
240         if ( !read_map->contains(location) )
241                 return VALUE_NONE;
242
243         uint64_t read_val = read_map->get(location);
244         return read_val;
245 */
246 }
247
248 /* @param tid thread id
249  * Reset read map for a thread. This function shall only be called
250  * when a thread exits a function
251  */
252 void FuncNode::clear_read_map(uint32_t tid)
253 {
254 /*
255         if (thrd_read_map.size() <= tid)
256                 return;
257
258         thrd_read_map[tid]->reset();
259 */
260 }
261
262 void FuncNode::update_predicate_tree(action_list_t * act_list)
263 {
264         if (act_list == NULL || act_list->size() == 0)
265                 return;
266
267         /* map a FuncInst to the its predicate */
268         HashTable<FuncInst *, Predicate *, uintptr_t, 0> inst_pred_map(128);
269
270         // number FuncInsts to detect loops
271         HashTable<FuncInst *, uint32_t, uintptr_t, 0> inst_id_map(128);
272         uint32_t inst_counter = 0;
273
274         HashTable<void *, ModelAction *, uintptr_t, 0> loc_act_map(128);
275         HashTable<FuncInst *, ModelAction *, uintptr_t, 0> inst_act_map(128);
276
277         sllnode<ModelAction *> *it = act_list->begin();
278         Predicate * curr_pred = predicate_tree_entry;
279         while (it != NULL) {
280                 ModelAction * next_act = it->getVal();
281                 FuncInst * next_inst = get_inst(next_act);
282
283                 SnapVector<Predicate *> unset_predicates = SnapVector<Predicate *>();
284                 bool branch_found = follow_branch(&curr_pred, next_inst, next_act, &inst_act_map, &unset_predicates);
285
286                 // no predicate expressions
287                 if (!branch_found && unset_predicates.size() != 0) {
288                         ASSERT(unset_predicates.size() == 1);
289                         Predicate * one_branch = unset_predicates[0];
290
291                         bool amended = amend_predicate_expr(&curr_pred, next_inst, next_act);
292                         if (amended)
293                                 continue;
294                         else {
295                                 curr_pred = one_branch;
296                                 branch_found = true;
297                         }
298                 }
299
300                 // detect loops
301                 if (!branch_found && inst_id_map.contains(next_inst)) {
302                         FuncInst * curr_inst = curr_pred->get_func_inst();
303                         uint32_t curr_id = inst_id_map.get(curr_inst);
304                         uint32_t next_id = inst_id_map.get(next_inst);
305
306                         if (curr_id >= next_id) {
307                                 Predicate * old_pred = inst_pred_map.get(next_inst);
308                                 Predicate * back_pred = old_pred->get_parent();
309
310                                 curr_pred->add_backedge(back_pred);
311                                 curr_pred = back_pred;
312
313                                 continue;
314                         }
315                 }
316
317                 // generate new branches
318                 if (!branch_found) {
319                         SnapVector<struct half_pred_expr *> half_pred_expressions;
320                         void * loc = next_act->get_location();
321
322                         if ( loc_act_map.contains(loc) ) {
323                                 ModelAction * last_act = loc_act_map.get(loc);
324                                 FuncInst * last_inst = get_inst(last_act);
325                                 struct half_pred_expr * expression = new half_pred_expr(EQUALITY, last_inst);
326                                 half_pred_expressions.push_back(expression);
327                         } else if ( next_inst->is_single_location() ){
328                                 loc_set_t * loc_may_equal = loc_may_equal_map->get(loc);
329
330                                 if (loc_may_equal != NULL) {
331                                         loc_set_iter * loc_it = loc_may_equal->iterator();
332                                         while (loc_it->hasNext()) {
333                                                 void * neighbor = loc_it->next();
334                                                 if (loc_act_map.contains(neighbor)) {
335                                                         ModelAction * last_act = loc_act_map.get(neighbor);
336                                                         FuncInst * last_inst = get_inst(last_act);
337
338                                                         struct half_pred_expr * expression = new half_pred_expr(EQUALITY, last_inst);
339                                                         half_pred_expressions.push_back(expression);
340                                                 }
341                                         }
342                                 } 
343                         } else {
344                                 // next_inst is not single location
345                                 uint64_t read_val = next_act->get_reads_from_value();
346
347                                 // only generate NULLITY predicate when it is actually NULL.
348                                 if ( (void*)read_val == NULL) {
349                                         struct half_pred_expr * expression = new half_pred_expr(NULLITY, NULL);
350                                         half_pred_expressions.push_back(expression);
351                                 }
352                         }
353
354                         if (half_pred_expressions.size() == 0) {
355                                 // no predicate needs to be generated
356                                 Predicate * new_pred = new Predicate(next_inst);
357                                 curr_pred->add_child(new_pred);
358                                 new_pred->set_parent(curr_pred);
359
360                                 if (curr_pred->is_entry_predicate())
361                                         new_pred->add_predicate_expr(NOPREDICATE, NULL, true);
362
363                                 curr_pred = new_pred;
364                         } else {
365                                 generate_predicate(&curr_pred, next_inst, &half_pred_expressions);
366                                 bool branch_found = follow_branch(&curr_pred, next_inst, next_act, &inst_act_map, NULL);
367                                 ASSERT(branch_found);
368                         }
369                 }
370
371                 inst_pred_map.put(next_inst, curr_pred);
372                 if (!inst_id_map.contains(next_inst))
373                         inst_id_map.put(next_inst, inst_counter++);
374
375                 loc_act_map.put(next_act->get_location(), next_act);
376                 inst_act_map.put(next_inst, next_act);
377                 it = it->getNext();
378         }
379 }
380
381 /* Given curr_pred and next_inst, find the branch following curr_pred that
382  * contains next_inst and the correct predicate. 
383  * @return true if branch found, false otherwise.
384  */
385 bool FuncNode::follow_branch(Predicate ** curr_pred, FuncInst * next_inst, ModelAction * next_act,
386         HashTable<FuncInst *, ModelAction *, uintptr_t, 0> * inst_act_map,
387         SnapVector<Predicate *> * unset_predicates)
388 {
389         /* check if a branch with func_inst and corresponding predicate exists */
390         bool branch_found = false;
391         ModelVector<Predicate *> * branches = (*curr_pred)->get_children();
392         for (uint i = 0; i < branches->size(); i++) {
393                 Predicate * branch = (*branches)[i];
394                 if (branch->get_func_inst() != next_inst)
395                         continue;
396
397                 /* check against predicate expressions */
398                 bool predicate_correct = true;
399                 PredExprSet * pred_expressions = branch->get_pred_expressions();
400                 PredExprSetIter * pred_expr_it = pred_expressions->iterator();
401
402                 if (pred_expressions->getSize() == 0) {
403                         predicate_correct = false;
404                         unset_predicates->push_back(branch);
405                 }
406
407                 while (pred_expr_it->hasNext()) {
408                         pred_expr * pred_expression = pred_expr_it->next();
409                         uint64_t last_read, next_read;
410                         bool equality;
411
412                         switch(pred_expression->token) {
413                                 case NOPREDICATE:
414                                         predicate_correct = true;
415                                         break;
416                                 case EQUALITY:
417                                         FuncInst * to_be_compared;
418                                         ModelAction * last_act;
419
420                                         to_be_compared = pred_expression->func_inst;
421                                         last_act = inst_act_map->get(to_be_compared);
422
423                                         last_read = last_act->get_reads_from_value();
424                                         next_read = next_act->get_reads_from_value();
425                                         equality = (last_read == next_read);
426                                         if (equality != pred_expression->value)
427                                                 predicate_correct = false;
428
429                                         break;
430                                 case NULLITY:
431                                         next_read = next_act->get_reads_from_value();
432                                         equality = ((void*)next_read == NULL);
433                                         if (equality != pred_expression->value)
434                                                 predicate_correct = false;
435                                         break;
436                                 default:
437                                         predicate_correct = false;
438                                         model_print("unkown predicate token\n");
439                                         break;
440                         }
441                 }
442
443                 if (predicate_correct) {
444                         *curr_pred = branch;
445                         branch_found = true;
446                         break;
447                 }
448         }
449
450         return branch_found;
451 }
452
453 /* Able to generate complex predicates when there are multiple predciate expressions */
454 void FuncNode::generate_predicate(Predicate ** curr_pred, FuncInst * next_inst,
455         SnapVector<struct half_pred_expr *> * half_pred_expressions)
456 {
457         ASSERT(half_pred_expressions->size() != 0);
458         SnapVector<Predicate *> predicates;
459
460         struct half_pred_expr * half_expr = (*half_pred_expressions)[0];
461         predicates.push_back(new Predicate(next_inst));
462         predicates.push_back(new Predicate(next_inst));
463
464         predicates[0]->add_predicate_expr(half_expr->token, half_expr->func_inst, true);
465         predicates[1]->add_predicate_expr(half_expr->token, half_expr->func_inst, false);
466
467         for (uint i = 1; i < half_pred_expressions->size(); i++) {
468                 half_expr = (*half_pred_expressions)[i];
469
470                 uint old_size = predicates.size();
471                 for (uint j = 0; j < old_size; j++) {
472                         Predicate * pred = predicates[j];
473                         Predicate * new_pred = new Predicate(next_inst);
474                         new_pred->copy_predicate_expr(pred);
475
476                         pred->add_predicate_expr(half_expr->token, half_expr->func_inst, true);
477                         new_pred->add_predicate_expr(half_expr->token, half_expr->func_inst, false);
478
479                         predicates.push_back(new_pred);
480                 }
481         }
482
483         for (uint i = 0; i < predicates.size(); i++) {
484                 Predicate * pred= predicates[i];
485                 (*curr_pred)->add_child(pred);
486                 pred->set_parent(*curr_pred);
487         }
488 }
489
490 /* Amend predicates that contain no predicate expressions. Currenlty only amend with NULLITY predicates */
491 bool FuncNode::amend_predicate_expr(Predicate ** curr_pred, FuncInst * next_inst, ModelAction * next_act)
492 {
493         // there should only be only child
494         Predicate * unset_pred = (*curr_pred)->get_children()->back();
495         uint64_t read_val = next_act->get_reads_from_value();
496
497         // only generate NULLITY predicate when it is actually NULL.
498         if ( !next_inst->is_single_location() && (void*)read_val == NULL ) {
499                 Predicate * new_pred = new Predicate(next_inst);
500
501                 (*curr_pred)->add_child(new_pred);
502                 new_pred->set_parent(*curr_pred);
503
504                 unset_pred->add_predicate_expr(NULLITY, NULL, false);
505                 new_pred->add_predicate_expr(NULLITY, NULL, true);
506
507                 return true;
508         }
509
510         return false;
511 }
512
513 void FuncNode::add_to_val_loc_map(uint64_t val, void * loc)
514 {
515         loc_set_t * locations = val_loc_map->get(val);
516
517         if (locations == NULL) {
518                 locations = new loc_set_t();
519                 val_loc_map->put(val, locations);
520         }
521
522         update_loc_may_equal_map(loc, locations);
523         locations->add(loc);
524         values_may_read_from->add(val);
525 }
526
527 void FuncNode::add_to_val_loc_map(value_set_t * values, void * loc)
528 {
529         value_set_iter * it = values->iterator();
530         while (it->hasNext()) {
531                 uint64_t val = it->next();
532                 add_to_val_loc_map(val, loc);
533         }
534 }
535
536 void FuncNode::update_loc_may_equal_map(void * new_loc, loc_set_t * old_locations)
537 {
538         loc_set_t * neighbors = loc_may_equal_map->get(new_loc);
539
540         if (neighbors == NULL) {
541                 neighbors = new loc_set_t();
542                 loc_may_equal_map->put(new_loc, neighbors);
543         }
544
545         loc_set_iter * loc_it = old_locations->iterator();
546         while (loc_it->hasNext()) {
547                 // new_loc: { old_locations, ... }
548                 void * member = loc_it->next();
549                 neighbors->add(member);
550
551                 // for each i in old_locations, i : { new_loc, ... }
552                 loc_set_t * _neighbors = loc_may_equal_map->get(member);
553                 if (_neighbors == NULL) {
554                         _neighbors = new loc_set_t();
555                         loc_may_equal_map->put(member, _neighbors);
556                 }
557                 _neighbors->add(new_loc);
558         }
559 }
560
561 void FuncNode::print_predicate_tree()
562 {
563         model_print("digraph function_%s {\n", func_name);
564         predicate_tree_entry->print_pred_subtree();
565         model_print("}\n");     // end of graph
566 }
567
568 void FuncNode::print_val_loc_map()
569 {
570         value_set_iter * val_it = values_may_read_from->iterator();
571         while (val_it->hasNext()) {
572                 uint64_t value = val_it->next();
573                 model_print("val %llx: ", value);
574
575                 loc_set_t * locations = val_loc_map->get(value);
576                 loc_set_iter * loc_it = locations->iterator();
577                 while (loc_it->hasNext()) {
578                         void * location = loc_it->next();
579                         model_print("%p ", location);
580                 }
581                 model_print("\n");
582         }
583 }
584
585 /* @param tid thread id
586  * Print the values read by the last read actions for each memory location
587  */
588 /*
589 void FuncNode::print_last_read(uint32_t tid)
590 {
591         ASSERT(thrd_read_map.size() > tid);
592         read_map_t * read_map = thrd_read_map[tid];
593
594         mllnode<void *> * it;
595         for (it = read_locations.begin();it != NULL;it=it->getNext()) {
596                 if ( !read_map->contains(it->getVal()) )
597                         break;
598
599                 uint64_t read_val = read_map->get(it->getVal());
600                 model_print("last read of thread %d at %p: 0x%x\n", tid, it->getVal(), read_val);
601         }
602 }
603 */