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