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