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