Free memory
[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                 delete concrete_pred;
369
370                 if (predicate_correct) {
371                         *curr_pred = branch;
372                         branch_found = true;
373                         break;
374                 }
375         }
376
377         return branch_found;
378 }
379
380 /* Infer predicate expressions, which are generated in FuncNode::generate_predicates */
381 void FuncNode::infer_predicates(FuncInst * next_inst, ModelAction * next_act,
382         HashTable<void *, ModelAction *, uintptr_t, 0> * loc_act_map,
383         SnapVector<struct half_pred_expr *> * half_pred_expressions)
384 {
385         void * loc = next_act->get_location();
386
387         if (next_inst->is_read()) {
388                 /* read + rmw */
389                 if ( loc_act_map->contains(loc) ) {
390                         ModelAction * last_act = loc_act_map->get(loc);
391                         FuncInst * last_inst = get_inst(last_act);
392                         struct half_pred_expr * expression = new half_pred_expr(EQUALITY, last_inst);
393                         half_pred_expressions->push_back(expression);
394                 } else if ( next_inst->is_single_location() ){
395                         loc_set_t * loc_may_equal = loc_may_equal_map->get(loc);
396
397                         if (loc_may_equal != NULL) {
398                                 loc_set_iter * loc_it = loc_may_equal->iterator();
399                                 while (loc_it->hasNext()) {
400                                         void * neighbor = loc_it->next();
401                                         if (loc_act_map->contains(neighbor)) {
402                                                 ModelAction * last_act = loc_act_map->get(neighbor);
403                                                 FuncInst * last_inst = get_inst(last_act);
404
405                                                 struct half_pred_expr * expression = new half_pred_expr(EQUALITY, last_inst);
406                                                 half_pred_expressions->push_back(expression);
407                                         }
408                                 }
409                         }
410                 } else {
411                         // next_inst is not single location
412                         uint64_t read_val = next_act->get_reads_from_value();
413
414                         // only infer NULLITY predicate when it is actually NULL.
415                         if ( (void*)read_val == NULL) {
416                                 struct half_pred_expr * expression = new half_pred_expr(NULLITY, NULL);
417                                 half_pred_expressions->push_back(expression);
418                         }
419                 }
420         } else {
421                 /* Pure writes */
422                 // TODO: do anything here?
423         }
424 }
425
426 /* Able to generate complex predicates when there are multiple predciate expressions */
427 void FuncNode::generate_predicates(Predicate ** curr_pred, FuncInst * next_inst,
428         SnapVector<struct half_pred_expr *> * half_pred_expressions)
429 {
430         if (half_pred_expressions->size() == 0) {
431                 Predicate * new_pred = new Predicate(next_inst);
432                 (*curr_pred)->add_child(new_pred);
433                 new_pred->set_parent(*curr_pred);
434
435                 /* entry predicates and predicates containing pure write actions
436                  * have no predicate expressions */
437                 if ( (*curr_pred)->is_entry_predicate() )
438                         new_pred->add_predicate_expr(NOPREDICATE, NULL, true);
439                 else if (next_inst->is_write()) {
440                         /* next_inst->is_write() <==> pure writes */
441                         new_pred->add_predicate_expr(NOPREDICATE, NULL, true);
442                 }
443
444                 return;
445         }
446
447         SnapVector<Predicate *> predicates;
448
449         struct half_pred_expr * half_expr = (*half_pred_expressions)[0];
450         predicates.push_back(new Predicate(next_inst));
451         predicates.push_back(new Predicate(next_inst));
452
453         predicates[0]->add_predicate_expr(half_expr->token, half_expr->func_inst, true);
454         predicates[1]->add_predicate_expr(half_expr->token, half_expr->func_inst, false);
455
456         for (uint i = 1; i < half_pred_expressions->size(); i++) {
457                 half_expr = (*half_pred_expressions)[i];
458
459                 uint old_size = predicates.size();
460                 for (uint j = 0; j < old_size; j++) {
461                         Predicate * pred = predicates[j];
462                         Predicate * new_pred = new Predicate(next_inst);
463                         new_pred->copy_predicate_expr(pred);
464
465                         pred->add_predicate_expr(half_expr->token, half_expr->func_inst, true);
466                         new_pred->add_predicate_expr(half_expr->token, half_expr->func_inst, false);
467
468                         predicates.push_back(new_pred);
469                 }
470         }
471
472         for (uint i = 0; i < predicates.size(); i++) {
473                 Predicate * pred= predicates[i];
474                 (*curr_pred)->add_child(pred);
475                 pred->set_parent(*curr_pred);
476         }
477
478         /* Free memories allocated by infer_predicate */
479         for (uint i = 0; i < half_pred_expressions->size(); i++) {
480                 struct half_pred_expr * tmp = (*half_pred_expressions)[i];
481                 snapshot_free(tmp);
482         }
483 }
484
485 /* Amend predicates that contain no predicate expressions. Currenlty only amend with NULLITY predicates */
486 bool FuncNode::amend_predicate_expr(Predicate ** curr_pred, FuncInst * next_inst, ModelAction * next_act)
487 {
488         // there should only be only child
489         Predicate * unset_pred = (*curr_pred)->get_children()->back();
490         uint64_t read_val = next_act->get_reads_from_value();
491
492         // only generate NULLITY predicate when it is actually NULL.
493         if ( !next_inst->is_single_location() && (void*)read_val == NULL ) {
494                 Predicate * new_pred = new Predicate(next_inst);
495
496                 (*curr_pred)->add_child(new_pred);
497                 new_pred->set_parent(*curr_pred);
498
499                 unset_pred->add_predicate_expr(NULLITY, NULL, false);
500                 new_pred->add_predicate_expr(NULLITY, NULL, true);
501
502                 return true;
503         }
504
505         return false;
506 }
507
508 void FuncNode::add_to_val_loc_map(uint64_t val, void * loc)
509 {
510         loc_set_t * locations = val_loc_map->get(val);
511
512         if (locations == NULL) {
513                 locations = new loc_set_t();
514                 val_loc_map->put(val, locations);
515         }
516
517         update_loc_may_equal_map(loc, locations);
518         locations->add(loc);
519         // values_may_read_from->add(val);
520 }
521
522 void FuncNode::add_to_val_loc_map(value_set_t * values, void * loc)
523 {
524         if (values == NULL)
525                 return;
526
527         value_set_iter * it = values->iterator();
528         while (it->hasNext()) {
529                 uint64_t val = it->next();
530                 add_to_val_loc_map(val, loc);
531         }
532 }
533
534 void FuncNode::update_loc_may_equal_map(void * new_loc, loc_set_t * old_locations)
535 {
536         if ( old_locations->contains(new_loc) )
537                 return;
538
539         loc_set_t * neighbors = loc_may_equal_map->get(new_loc);
540
541         if (neighbors == NULL) {
542                 neighbors = new loc_set_t();
543                 loc_may_equal_map->put(new_loc, neighbors);
544         }
545
546         loc_set_iter * loc_it = old_locations->iterator();
547         while (loc_it->hasNext()) {
548                 // new_loc: { old_locations, ... }
549                 void * member = loc_it->next();
550                 neighbors->add(member);
551
552                 // for each i in old_locations, i : { new_loc, ... }
553                 loc_set_t * _neighbors = loc_may_equal_map->get(member);
554                 if (_neighbors == NULL) {
555                         _neighbors = new loc_set_t();
556                         loc_may_equal_map->put(member, _neighbors);
557                 }
558                 _neighbors->add(new_loc);
559         }
560 }
561
562 /* Every time a thread enters a function, set its position to the predicate tree entry */
563 void FuncNode::init_predicate_tree_position(thread_id_t tid)
564 {
565         int thread_id = id_to_int(tid);
566         if (predicate_tree_position.size() <= (uint) thread_id)
567                 predicate_tree_position.resize(thread_id + 1);
568
569         predicate_tree_position[thread_id] = predicate_tree_entry;
570 }
571
572 void FuncNode::set_predicate_tree_position(thread_id_t tid, Predicate * pred)
573 {
574         int thread_id = id_to_int(tid);
575         predicate_tree_position[thread_id] = pred;
576 }
577
578 /* @return The position of a thread in a predicate tree */
579 Predicate * FuncNode::get_predicate_tree_position(thread_id_t tid)
580 {
581         int thread_id = id_to_int(tid);
582         return predicate_tree_position[thread_id];
583 }
584
585 /* Make sure elements of thrd_inst_act_map are initialized properly when threads enter functions */
586 void FuncNode::init_inst_act_map(thread_id_t tid)
587 {
588         int thread_id = id_to_int(tid);
589         uint old_size = thrd_inst_act_map->size();
590
591         if (thrd_inst_act_map->size() <= (uint) thread_id) {
592                 uint new_size = thread_id + 1;
593                 thrd_inst_act_map->resize(new_size);
594
595                 for (uint i = old_size; i < new_size; i++)
596                         (*thrd_inst_act_map)[i] = new inst_act_map_t(128);
597         }
598 }
599
600 /* Reset elements of thrd_inst_act_map when threads exit functions */
601 void FuncNode::reset_inst_act_map(thread_id_t tid)
602 {
603         int thread_id = id_to_int(tid);
604         inst_act_map_t * map = (*thrd_inst_act_map)[thread_id];
605         map->reset();
606 }
607
608 void FuncNode::update_inst_act_map(thread_id_t tid, ModelAction * read_act)
609 {
610         int thread_id = id_to_int(tid);
611         inst_act_map_t * map = (*thrd_inst_act_map)[thread_id];
612         FuncInst * read_inst = get_inst(read_act);
613         map->put(read_inst, read_act);
614 }
615
616 inst_act_map_t * FuncNode::get_inst_act_map(thread_id_t tid)
617 {
618         int thread_id = id_to_int(tid);
619         return (*thrd_inst_act_map)[thread_id];
620 }
621
622 /* Add FuncNodes that this node may follow */
623 void FuncNode::add_out_edge(FuncNode * other)
624 {
625         if ( !edge_table.contains(other) ) {
626                 edge_table.put(other, OUT_EDGE);
627                 out_edges.push_back(other);
628                 return;
629         }
630
631         edge_type_t edge = edge_table.get(other);
632         if (edge == IN_EDGE) {
633                 edge_table.put(other, BI_EDGE);
634                 out_edges.push_back(other);
635         }
636 }
637
638 void FuncNode::print_predicate_tree()
639 {
640         model_print("digraph function_%s {\n", func_name);
641         predicate_tree_entry->print_pred_subtree();
642         model_print("}\n");     // end of graph
643 }
644
645 void FuncNode::print_val_loc_map()
646 {
647 /*
648         value_set_iter * val_it = values_may_read_from->iterator();
649         while (val_it->hasNext()) {
650                 uint64_t value = val_it->next();
651                 model_print("val %llx: ", value);
652
653                 loc_set_t * locations = val_loc_map->get(value);
654                 loc_set_iter * loc_it = locations->iterator();
655                 while (loc_it->hasNext()) {
656                         void * location = loc_it->next();
657                         model_print("%p ", location);
658                 }
659                 model_print("\n");
660         }
661 */
662 }