Add edges between FuncNodes
[c11tester.git] / funcnode.cc
1 #include "funcnode.h"
2
3 FuncNode::FuncNode(ModelHistory * history) :
4         history(history),
5         exit_count(0),
6         func_inst_map(),
7         inst_list(),
8         entry_insts(),
9         predicate_tree_position(),
10         edge_table(32),
11         out_edges(),
12         in_edges()
13 {
14         predicate_tree_entry = new Predicate(NULL, true);
15         predicate_tree_entry->add_predicate_expr(NOPREDICATE, NULL, true);
16
17         // memories that are reclaimed after each execution
18         action_list_buffer = new SnapList<action_list_t *>();
19         read_locations = new loc_set_t();
20         val_loc_map = new HashTable<uint64_t, loc_set_t *, uint64_t, 0>();
21         loc_may_equal_map = new HashTable<void *, loc_set_t *, uintptr_t, 0>();
22         thrd_inst_act_map = new SnapVector<inst_act_map_t *>();
23
24         //values_may_read_from = new value_set_t();
25 }
26
27 /* Reallocate snapshotted memories when new executions start */
28 void FuncNode::set_new_exec_flag()
29 {
30         for (mllnode<FuncInst *> * it = inst_list.begin(); it != NULL; it = it->getNext()) {
31                 FuncInst * inst = it->getVal();
32                 inst->unset_location();
33         }
34
35         action_list_buffer = new SnapList<action_list_t *>();
36         read_locations = new loc_set_t();
37         val_loc_map = new HashTable<uint64_t, loc_set_t *, uint64_t, 0>();
38         loc_may_equal_map = new HashTable<void *, loc_set_t *, uintptr_t, 0>();
39         thrd_inst_act_map = new SnapVector<inst_act_map_t *>();
40
41         //values_may_read_from = new value_set_t();
42 }
43
44 /* Check whether FuncInst with the same type, position, and location
45  * as act has been added to func_inst_map or not. If not, add it.
46  *
47  * Note: currently, actions with the same position are filtered out by process_action,
48  * so the collision list of FuncInst is not used. May remove it later. 
49  */
50 void FuncNode::add_inst(ModelAction *act)
51 {
52         ASSERT(act);
53         const char * position = act->get_position();
54
55         /* THREAD* actions, ATOMIC_LOCK, ATOMIC_TRYLOCK, and ATOMIC_UNLOCK
56          * actions are not tagged with their source line numbers
57          */
58         if (position == NULL)
59                 return;
60
61         if ( func_inst_map.contains(position) ) {
62                 FuncInst * inst = func_inst_map.get(position);
63
64                 ASSERT(inst->get_type() == act->get_type());
65
66                 // locations are set to NULL when new executions start
67                 if (inst->get_location() == NULL)
68                         inst->set_location(act->get_location());
69
70                 if (inst->get_location() != act->get_location())
71                         inst->not_single_location();
72
73                 return;
74         }
75
76         FuncInst * func_inst = new FuncInst(act, this);
77
78         func_inst_map.put(position, func_inst);
79         inst_list.push_back(func_inst);
80 }
81
82 /* Get the FuncInst with the same type, position, and location
83  * as act
84  *
85  * @return FuncInst with the same type, position, and location as act */
86 FuncInst * FuncNode::get_inst(ModelAction *act)
87 {
88         ASSERT(act);
89         const char * position = act->get_position();
90
91         /* THREAD* actions, ATOMIC_LOCK, ATOMIC_TRYLOCK, and ATOMIC_UNLOCK
92          * actions are not tagged with their source line numbers
93          */
94         if (position == NULL)
95                 return NULL;
96
97         FuncInst * inst = func_inst_map.get(position);
98         if (inst == NULL)
99                 return NULL;
100
101         action_type inst_type = inst->get_type();
102         action_type act_type = act->get_type();
103
104         // else if branch: an RMWRCAS action is converted to a RMW or READ action
105         if (inst_type == act_type)
106                 return inst;
107         else if (inst_type == ATOMIC_RMWRCAS &&
108                         (act_type == ATOMIC_RMW || act_type == ATOMIC_READ))
109                 return inst;
110
111         return NULL;
112 }
113
114
115 void FuncNode::add_entry_inst(FuncInst * inst)
116 {
117         if (inst == NULL)
118                 return;
119
120         mllnode<FuncInst *> * it;
121         for (it = entry_insts.begin(); it != NULL; it = it->getNext()) {
122                 if (inst == it->getVal())
123                         return;
124         }
125
126         entry_insts.push_back(inst);
127 }
128
129 /**
130  * @brief Convert ModelAdtion list to FuncInst list 
131  * @param act_list A list of ModelActions
132  */
133 void FuncNode::update_tree(action_list_t * act_list)
134 {
135         if (act_list == NULL || act_list->size() == 0)
136                 return;
137
138         HashTable<void *, value_set_t *, uintptr_t, 4> * write_history = history->getWriteHistory();
139
140         /* build inst_list from act_list for later processing */
141         func_inst_list_t inst_list;
142         action_list_t rw_act_list;
143
144         for (sllnode<ModelAction *> * it = act_list->begin(); it != NULL; it = it->getNext()) {
145                 ModelAction * act = it->getVal();
146                 FuncInst * func_inst = get_inst(act);
147
148                 if (func_inst == NULL)
149                         continue;
150
151                 inst_list.push_back(func_inst);
152
153                 /* NOTE: for rmw actions, func_inst and act may have different
154                  * action types because of action type conversion in ModelExecution
155                  * func_inst->is_write() <==> pure writes (excluding rmw) */
156                 if (func_inst->is_write()) {
157                         // model_print("write detected\n");
158                         rw_act_list.push_back(act);
159                 }
160
161                 /* func_inst->is_read() <==> read + rmw */
162                 if (func_inst->is_read()) {
163                         rw_act_list.push_back(act);
164                         /* If func_inst may only read_from a single location, then:
165                          *
166                          * The first time an action reads from some location,
167                          * import all the values that have been written to this
168                          * location from ModelHistory and notify ModelHistory
169                          * that this FuncNode may read from this location.
170                          */
171                         void * loc = act->get_location();
172                         if (!read_locations->contains(loc) && func_inst->is_single_location()) {
173                                 read_locations->add(loc);
174                                 value_set_t * write_values = write_history->get(loc);
175                                 add_to_val_loc_map(write_values, loc);
176                                 history->add_to_loc_func_nodes_map(loc, this);
177                         }
178                 }
179         }
180
181 //      model_print("function %s\n", func_name);
182 //      print_val_loc_map();
183
184         update_inst_tree(&inst_list);
185         update_predicate_tree(&rw_act_list);
186
187 //      print_predicate_tree();
188 }
189
190 /** 
191  * @brief Link FuncInsts in inst_list  - add one FuncInst to another's predecessors and successors
192  * @param inst_list A list of FuncInsts
193  */
194 void FuncNode::update_inst_tree(func_inst_list_t * inst_list)
195 {
196         if (inst_list == NULL)
197                 return;
198         else if (inst_list->size() == 0)
199                 return;
200
201         /* start linking */
202         sllnode<FuncInst *>* it = inst_list->begin();
203         sllnode<FuncInst *>* prev;
204
205         /* add the first instruction to the list of entry insts */
206         FuncInst * entry_inst = it->getVal();
207         add_entry_inst(entry_inst);
208
209         it = it->getNext();
210         while (it != NULL) {
211                 prev = it->getPrev();
212
213                 FuncInst * prev_inst = prev->getVal();
214                 FuncInst * curr_inst = it->getVal();
215
216                 prev_inst->add_succ(curr_inst);
217                 curr_inst->add_pred(prev_inst);
218
219                 it = it->getNext();
220         }
221 }
222
223 void FuncNode::update_predicate_tree(action_list_t * act_list)
224 {
225         if (act_list == NULL || act_list->size() == 0)
226                 return;
227
228         /* map a FuncInst to the its predicate */
229         HashTable<FuncInst *, Predicate *, uintptr_t, 0> inst_pred_map(128);
230
231         // number FuncInsts to detect loops
232         HashTable<FuncInst *, uint32_t, uintptr_t, 0> inst_id_map(128);
233         uint32_t inst_counter = 0;
234
235         HashTable<void *, ModelAction *, uintptr_t, 0> loc_act_map(128);
236         HashTable<FuncInst *, ModelAction *, uintptr_t, 0> inst_act_map(128);
237
238         sllnode<ModelAction *> *it = act_list->begin();
239         Predicate * curr_pred = predicate_tree_entry;
240         while (it != NULL) {
241                 ModelAction * next_act = it->getVal();
242                 FuncInst * next_inst = get_inst(next_act);
243
244                 SnapVector<Predicate *> unset_predicates = SnapVector<Predicate *>();
245                 bool branch_found = follow_branch(&curr_pred, next_inst, next_act, &inst_act_map, &unset_predicates);
246
247                 // A branch with unset predicate expression is detected
248                 if (!branch_found && unset_predicates.size() != 0) {
249                         ASSERT(unset_predicates.size() == 1);
250                         Predicate * one_branch = unset_predicates[0];
251
252                         bool amended = amend_predicate_expr(&curr_pred, next_inst, next_act);
253                         if (amended)
254                                 continue;
255                         else {
256                                 curr_pred = one_branch;
257                                 branch_found = true;
258                         }
259                 }
260
261                 // Detect loops
262                 if (!branch_found && inst_id_map.contains(next_inst)) {
263                         FuncInst * curr_inst = curr_pred->get_func_inst();
264                         uint32_t curr_id = inst_id_map.get(curr_inst);
265                         uint32_t next_id = inst_id_map.get(next_inst);
266
267                         if (curr_id >= next_id) {
268                                 Predicate * old_pred = inst_pred_map.get(next_inst);
269                                 Predicate * back_pred = old_pred->get_parent();
270
271                                 curr_pred->add_backedge(back_pred);
272                                 curr_pred = back_pred;
273
274                                 continue;
275                         }
276                 }
277
278                 // Generate new branches
279                 if (!branch_found) {
280                         SnapVector<struct half_pred_expr *> half_pred_expressions;
281                         infer_predicates(next_inst, next_act, &loc_act_map, &half_pred_expressions);
282                         generate_predicates(&curr_pred, next_inst, &half_pred_expressions);
283                         continue;
284                 }
285
286                 if (next_act->is_write())
287                         curr_pred->set_write(true);
288
289                 inst_pred_map.put(next_inst, curr_pred);
290                 if (!inst_id_map.contains(next_inst))
291                         inst_id_map.put(next_inst, inst_counter++);
292
293                 loc_act_map.put(next_act->get_location(), next_act);
294                 inst_act_map.put(next_inst, next_act);
295                 it = it->getNext();
296         }
297 }
298
299 /* Given curr_pred and next_inst, find the branch following curr_pred that
300  * contains next_inst and the correct predicate. 
301  * @return true if branch found, false otherwise.
302  */
303 bool FuncNode::follow_branch(Predicate ** curr_pred, FuncInst * next_inst, ModelAction * next_act,
304         HashTable<FuncInst *, ModelAction *, uintptr_t, 0> * inst_act_map,
305         SnapVector<Predicate *> * unset_predicates)
306 {
307         /* check if a branch with func_inst and corresponding predicate exists */
308         bool branch_found = false;
309         ModelVector<Predicate *> * branches = (*curr_pred)->get_children();
310         for (uint i = 0; i < branches->size(); i++) {
311                 Predicate * branch = (*branches)[i];
312                 if (branch->get_func_inst() != next_inst)
313                         continue;
314
315                 /* check against predicate expressions */
316                 bool predicate_correct = true;
317                 PredExprSet * pred_expressions = branch->get_pred_expressions();
318                 PredExprSetIter * pred_expr_it = pred_expressions->iterator();
319
320                 /* Only read and rmw actions my have unset predicate expressions */
321                 if (pred_expressions->getSize() == 0) {
322                         predicate_correct = false;
323                         unset_predicates->push_back(branch);
324                 }
325
326                 while (pred_expr_it->hasNext()) {
327                         pred_expr * pred_expression = pred_expr_it->next();
328                         uint64_t last_read, next_read;
329                         bool equality;
330
331                         switch(pred_expression->token) {
332                                 case NOPREDICATE:
333                                         predicate_correct = true;
334                                         break;
335                                 case EQUALITY:
336                                         FuncInst * to_be_compared;
337                                         ModelAction * last_act;
338
339                                         to_be_compared = pred_expression->func_inst;
340                                         last_act = inst_act_map->get(to_be_compared);
341
342                                         last_read = last_act->get_reads_from_value();
343                                         next_read = next_act->get_reads_from_value();
344                                         equality = (last_read == next_read);
345                                         if (equality != pred_expression->value)
346                                                 predicate_correct = false;
347
348                                         break;
349                                 case NULLITY:
350                                         next_read = next_act->get_reads_from_value();
351                                         equality = ((void*)next_read == NULL);
352                                         if (equality != pred_expression->value)
353                                                 predicate_correct = false;
354                                         break;
355                                 default:
356                                         predicate_correct = false;
357                                         model_print("unkown predicate token\n");
358                                         break;
359                         }
360                 }
361
362                 if (predicate_correct) {
363                         *curr_pred = branch;
364                         branch_found = true;
365                         break;
366                 }
367         }
368
369         return branch_found;
370 }
371
372 /* Infer predicate expressions, which are generated in FuncNode::generate_predicates */
373 void FuncNode::infer_predicates(FuncInst * next_inst, ModelAction * next_act,
374         HashTable<void *, ModelAction *, uintptr_t, 0> * loc_act_map,
375         SnapVector<struct half_pred_expr *> * half_pred_expressions)
376 {
377         void * loc = next_act->get_location();
378
379         if (next_inst->is_read()) {
380                 /* read + rmw */
381                 if ( loc_act_map->contains(loc) ) {
382                         ModelAction * last_act = loc_act_map->get(loc);
383                         FuncInst * last_inst = get_inst(last_act);
384                         struct half_pred_expr * expression = new half_pred_expr(EQUALITY, last_inst);
385                         half_pred_expressions->push_back(expression);
386                 } else if ( next_inst->is_single_location() ){
387                         loc_set_t * loc_may_equal = loc_may_equal_map->get(loc);
388
389                         if (loc_may_equal != NULL) {
390                                 loc_set_iter * loc_it = loc_may_equal->iterator();
391                                 while (loc_it->hasNext()) {
392                                         void * neighbor = loc_it->next();
393                                         if (loc_act_map->contains(neighbor)) {
394                                                 ModelAction * last_act = loc_act_map->get(neighbor);
395                                                 FuncInst * last_inst = get_inst(last_act);
396
397                                                 struct half_pred_expr * expression = new half_pred_expr(EQUALITY, last_inst);
398                                                 half_pred_expressions->push_back(expression);
399                                         }
400                                 }
401                         }
402                 } else {
403                         // next_inst is not single location
404                         uint64_t read_val = next_act->get_reads_from_value();
405
406                         // only infer NULLITY predicate when it is actually NULL.
407                         if ( (void*)read_val == NULL) {
408                                 struct half_pred_expr * expression = new half_pred_expr(NULLITY, NULL);
409                                 half_pred_expressions->push_back(expression);
410                         }
411                 }
412         } else {
413                 /* Pure writes */
414                 // TODO: do anything here?
415         }
416 }
417
418 /* Able to generate complex predicates when there are multiple predciate expressions */
419 void FuncNode::generate_predicates(Predicate ** curr_pred, FuncInst * next_inst,
420         SnapVector<struct half_pred_expr *> * half_pred_expressions)
421 {
422         if (half_pred_expressions->size() == 0) {
423                 Predicate * new_pred = new Predicate(next_inst);
424                 (*curr_pred)->add_child(new_pred);
425                 new_pred->set_parent(*curr_pred);
426
427                 /* entry predicates and predicates containing pure write actions
428                  * have no predicate expressions */
429                 if ( (*curr_pred)->is_entry_predicate() )
430                         new_pred->add_predicate_expr(NOPREDICATE, NULL, true);
431                 else if (next_inst->is_write()) {
432                         /* next_inst->is_write() <==> pure writes */
433                         new_pred->add_predicate_expr(NOPREDICATE, NULL, true);
434                 }
435
436                 return;
437         }
438
439         SnapVector<Predicate *> predicates;
440
441         struct half_pred_expr * half_expr = (*half_pred_expressions)[0];
442         predicates.push_back(new Predicate(next_inst));
443         predicates.push_back(new Predicate(next_inst));
444
445         predicates[0]->add_predicate_expr(half_expr->token, half_expr->func_inst, true);
446         predicates[1]->add_predicate_expr(half_expr->token, half_expr->func_inst, false);
447
448         for (uint i = 1; i < half_pred_expressions->size(); i++) {
449                 half_expr = (*half_pred_expressions)[i];
450
451                 uint old_size = predicates.size();
452                 for (uint j = 0; j < old_size; j++) {
453                         Predicate * pred = predicates[j];
454                         Predicate * new_pred = new Predicate(next_inst);
455                         new_pred->copy_predicate_expr(pred);
456
457                         pred->add_predicate_expr(half_expr->token, half_expr->func_inst, true);
458                         new_pred->add_predicate_expr(half_expr->token, half_expr->func_inst, false);
459
460                         predicates.push_back(new_pred);
461                 }
462         }
463
464         for (uint i = 0; i < predicates.size(); i++) {
465                 Predicate * pred= predicates[i];
466                 (*curr_pred)->add_child(pred);
467                 pred->set_parent(*curr_pred);
468         }
469 }
470
471 /* Amend predicates that contain no predicate expressions. Currenlty only amend with NULLITY predicates */
472 bool FuncNode::amend_predicate_expr(Predicate ** curr_pred, FuncInst * next_inst, ModelAction * next_act)
473 {
474         // there should only be only child
475         Predicate * unset_pred = (*curr_pred)->get_children()->back();
476         uint64_t read_val = next_act->get_reads_from_value();
477
478         // only generate NULLITY predicate when it is actually NULL.
479         if ( !next_inst->is_single_location() && (void*)read_val == NULL ) {
480                 Predicate * new_pred = new Predicate(next_inst);
481
482                 (*curr_pred)->add_child(new_pred);
483                 new_pred->set_parent(*curr_pred);
484
485                 unset_pred->add_predicate_expr(NULLITY, NULL, false);
486                 new_pred->add_predicate_expr(NULLITY, NULL, true);
487
488                 return true;
489         }
490
491         return false;
492 }
493
494 void FuncNode::add_to_val_loc_map(uint64_t val, void * loc)
495 {
496         loc_set_t * locations = val_loc_map->get(val);
497
498         if (locations == NULL) {
499                 locations = new loc_set_t();
500                 val_loc_map->put(val, locations);
501         }
502
503         update_loc_may_equal_map(loc, locations);
504         locations->add(loc);
505         // values_may_read_from->add(val);
506 }
507
508 void FuncNode::add_to_val_loc_map(value_set_t * values, void * loc)
509 {
510         if (values == NULL)
511                 return;
512
513         value_set_iter * it = values->iterator();
514         while (it->hasNext()) {
515                 uint64_t val = it->next();
516                 add_to_val_loc_map(val, loc);
517         }
518 }
519
520 void FuncNode::update_loc_may_equal_map(void * new_loc, loc_set_t * old_locations)
521 {
522         if ( old_locations->contains(new_loc) )
523                 return;
524
525         loc_set_t * neighbors = loc_may_equal_map->get(new_loc);
526
527         if (neighbors == NULL) {
528                 neighbors = new loc_set_t();
529                 loc_may_equal_map->put(new_loc, neighbors);
530         }
531
532         loc_set_iter * loc_it = old_locations->iterator();
533         while (loc_it->hasNext()) {
534                 // new_loc: { old_locations, ... }
535                 void * member = loc_it->next();
536                 neighbors->add(member);
537
538                 // for each i in old_locations, i : { new_loc, ... }
539                 loc_set_t * _neighbors = loc_may_equal_map->get(member);
540                 if (_neighbors == NULL) {
541                         _neighbors = new loc_set_t();
542                         loc_may_equal_map->put(member, _neighbors);
543                 }
544                 _neighbors->add(new_loc);
545         }
546 }
547
548 /* Every time a thread enters a function, set its position to the predicate tree entry */
549 void FuncNode::init_predicate_tree_position(thread_id_t tid)
550 {
551         int thread_id = id_to_int(tid);
552         if (predicate_tree_position.size() <= (uint) thread_id)
553                 predicate_tree_position.resize(thread_id + 1);
554
555         predicate_tree_position[thread_id] = predicate_tree_entry;
556 }
557
558 void FuncNode::set_predicate_tree_position(thread_id_t tid, Predicate * pred)
559 {
560         int thread_id = id_to_int(tid);
561         predicate_tree_position[thread_id] = pred;
562 }
563
564 /* @return The position of a thread in a predicate tree */
565 Predicate * FuncNode::get_predicate_tree_position(thread_id_t tid)
566 {
567         int thread_id = id_to_int(tid);
568         return predicate_tree_position[thread_id];
569 }
570
571 /* Make sure elements of thrd_inst_act_map are initialized properly when threads enter functions */
572 void FuncNode::init_inst_act_map(thread_id_t tid)
573 {
574         int thread_id = id_to_int(tid);
575         uint old_size = thrd_inst_act_map->size();
576
577         if (thrd_inst_act_map->size() <= (uint) thread_id) {
578                 uint new_size = thread_id + 1;
579                 thrd_inst_act_map->resize(new_size);
580
581                 for (uint i = old_size; i < new_size; i++)
582                         (*thrd_inst_act_map)[i] = new inst_act_map_t(128);
583         }
584 }
585
586 /* Reset elements of thrd_inst_act_map when threads exit functions */
587 void FuncNode::reset_inst_act_map(thread_id_t tid)
588 {
589         int thread_id = id_to_int(tid);
590         inst_act_map_t * map = (*thrd_inst_act_map)[thread_id];
591         map->reset();
592 }
593
594 void FuncNode::update_inst_act_map(thread_id_t tid, ModelAction * read_act)
595 {
596         int thread_id = id_to_int(tid);
597         inst_act_map_t * map = (*thrd_inst_act_map)[thread_id];
598         FuncInst * read_inst = get_inst(read_act);
599         map->put(read_inst, read_act);
600 }
601
602 inst_act_map_t * FuncNode::get_inst_act_map(thread_id_t tid)
603 {
604         int thread_id = id_to_int(tid);
605         return (*thrd_inst_act_map)[thread_id];
606 }
607
608 /* Add FuncNodes that this node may follow */
609 void FuncNode::add_out_edge(FuncNode * other)
610 {
611         if ( !edge_table.contains(other) ) {
612                 edge_table.put(other, OUT_EDGE);
613                 out_edges.push_back(other);
614                 return;
615         }
616
617         edge_type_t edge = edge_table.get(other);
618         if (edge == IN_EDGE) {
619                 edge_table.put(other, BI_EDGE);
620                 out_edges.push_back(other);
621         }
622 }
623
624 /* Add FuncNodes that come before this node */
625 void FuncNode::add_in_edge(FuncNode * other)
626 {
627         if ( !edge_table.contains(other) ) {
628                 edge_table.put(other, IN_EDGE);
629                 in_edges.push_back(other);
630                 return;
631         }
632
633         edge_type_t edge = edge_table.get(other);
634         if (edge == OUT_EDGE) {
635                 edge_table.put(other, BI_EDGE);
636                 in_edges.push_back(other);
637         }
638 }
639
640
641 void FuncNode::print_predicate_tree()
642 {
643         model_print("digraph function_%s {\n", func_name);
644         predicate_tree_entry->print_pred_subtree();
645         model_print("}\n");     // end of graph
646 }
647
648 void FuncNode::print_val_loc_map()
649 {
650 /*
651         value_set_iter * val_it = values_may_read_from->iterator();
652         while (val_it->hasNext()) {
653                 uint64_t value = val_it->next();
654                 model_print("val %llx: ", value);
655
656                 loc_set_t * locations = val_loc_map->get(value);
657                 loc_set_iter * loc_it = locations->iterator();
658                 while (loc_it->hasNext()) {
659                         void * location = loc_it->next();
660                         model_print("%p ", location);
661                 }
662                 model_print("\n");
663         }
664 */
665 }