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