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