Remove an unused data structure
[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 read_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_read()) {
151                         read_act_list.push_back(act);
152
153                         /* If func_inst may only read_from a single location, then:
154                          *
155                          * The first time an action reads from some location, import all the values that have
156                          * been written to this location from ModelHistory and notify ModelHistory that this
157                          * FuncNode may read from this location.
158                          */
159                         void * loc = act->get_location();
160                         if (!read_locations->contains(loc) && func_inst->is_single_location()) {
161                                 read_locations->add(loc);
162                                 value_set_t * write_values = write_history->get(loc);
163                                 add_to_val_loc_map(write_values, loc);
164                                 history->add_to_loc_func_nodes_map(loc, this);
165                         }
166                 }
167         }
168
169 //      model_print("function %s\n", func_name);
170 //      print_val_loc_map();
171
172         update_inst_tree(&inst_list);
173         update_predicate_tree(&read_act_list);
174
175 //      print_predicate_tree();
176 }
177
178 /** 
179  * @brief Link FuncInsts in inst_list  - add one FuncInst to another's predecessors and successors
180  * @param inst_list A list of FuncInsts
181  */
182 void FuncNode::update_inst_tree(func_inst_list_t * inst_list)
183 {
184         if (inst_list == NULL)
185                 return;
186         else if (inst_list->size() == 0)
187                 return;
188
189         /* start linking */
190         sllnode<FuncInst *>* it = inst_list->begin();
191         sllnode<FuncInst *>* prev;
192
193         /* add the first instruction to the list of entry insts */
194         FuncInst * entry_inst = it->getVal();
195         add_entry_inst(entry_inst);
196
197         it = it->getNext();
198         while (it != NULL) {
199                 prev = it->getPrev();
200
201                 FuncInst * prev_inst = prev->getVal();
202                 FuncInst * curr_inst = it->getVal();
203
204                 prev_inst->add_succ(curr_inst);
205                 curr_inst->add_pred(prev_inst);
206
207                 it = it->getNext();
208         }
209 }
210
211 void FuncNode::update_predicate_tree(action_list_t * act_list)
212 {
213         if (act_list == NULL || act_list->size() == 0)
214                 return;
215
216         /* map a FuncInst to the its predicate */
217         HashTable<FuncInst *, Predicate *, uintptr_t, 0> inst_pred_map(128);
218
219         // number FuncInsts to detect loops
220         HashTable<FuncInst *, uint32_t, uintptr_t, 0> inst_id_map(128);
221         uint32_t inst_counter = 0;
222
223         HashTable<void *, ModelAction *, uintptr_t, 0> loc_act_map(128);
224         HashTable<FuncInst *, ModelAction *, uintptr_t, 0> inst_act_map(128);
225
226         sllnode<ModelAction *> *it = act_list->begin();
227         Predicate * curr_pred = predicate_tree_entry;
228         while (it != NULL) {
229                 ModelAction * next_act = it->getVal();
230                 FuncInst * next_inst = get_inst(next_act);
231
232                 SnapVector<Predicate *> unset_predicates = SnapVector<Predicate *>();
233                 bool branch_found = follow_branch(&curr_pred, next_inst, next_act, &inst_act_map, &unset_predicates);
234
235                 // no predicate expressions
236                 if (!branch_found && unset_predicates.size() != 0) {
237                         ASSERT(unset_predicates.size() == 1);
238                         Predicate * one_branch = unset_predicates[0];
239
240                         bool amended = amend_predicate_expr(&curr_pred, next_inst, next_act);
241                         if (amended)
242                                 continue;
243                         else {
244                                 curr_pred = one_branch;
245                                 branch_found = true;
246                         }
247                 }
248
249                 // detect loops
250                 if (!branch_found && inst_id_map.contains(next_inst)) {
251                         FuncInst * curr_inst = curr_pred->get_func_inst();
252                         uint32_t curr_id = inst_id_map.get(curr_inst);
253                         uint32_t next_id = inst_id_map.get(next_inst);
254
255                         if (curr_id >= next_id) {
256                                 Predicate * old_pred = inst_pred_map.get(next_inst);
257                                 Predicate * back_pred = old_pred->get_parent();
258
259                                 curr_pred->add_backedge(back_pred);
260                                 curr_pred = back_pred;
261
262                                 continue;
263                         }
264                 }
265
266                 // generate new branches
267                 if (!branch_found) {
268                         SnapVector<struct half_pred_expr *> half_pred_expressions;
269                         void * loc = next_act->get_location();
270
271                         if ( loc_act_map.contains(loc) ) {
272                                 ModelAction * last_act = loc_act_map.get(loc);
273                                 FuncInst * last_inst = get_inst(last_act);
274                                 struct half_pred_expr * expression = new half_pred_expr(EQUALITY, last_inst);
275                                 half_pred_expressions.push_back(expression);
276                         } else if ( next_inst->is_single_location() ){
277                                 loc_set_t * loc_may_equal = loc_may_equal_map->get(loc);
278
279                                 if (loc_may_equal != NULL) {
280                                         loc_set_iter * loc_it = loc_may_equal->iterator();
281                                         while (loc_it->hasNext()) {
282                                                 void * neighbor = loc_it->next();
283                                                 if (loc_act_map.contains(neighbor)) {
284                                                         ModelAction * last_act = loc_act_map.get(neighbor);
285                                                         FuncInst * last_inst = get_inst(last_act);
286
287                                                         struct half_pred_expr * expression = new half_pred_expr(EQUALITY, last_inst);
288                                                         half_pred_expressions.push_back(expression);
289                                                 }
290                                         }
291                                 } 
292                         } else {
293                                 // next_inst is not single location
294                                 uint64_t read_val = next_act->get_reads_from_value();
295
296                                 // only generate NULLITY predicate when it is actually NULL.
297                                 if ( (void*)read_val == NULL) {
298                                         struct half_pred_expr * expression = new half_pred_expr(NULLITY, NULL);
299                                         half_pred_expressions.push_back(expression);
300                                 }
301                         }
302
303                         if (half_pred_expressions.size() == 0) {
304                                 // no predicate needs to be generated
305                                 Predicate * new_pred = new Predicate(next_inst);
306                                 curr_pred->add_child(new_pred);
307                                 new_pred->set_parent(curr_pred);
308
309                                 if (curr_pred->is_entry_predicate())
310                                         new_pred->add_predicate_expr(NOPREDICATE, NULL, true);
311
312                                 curr_pred = new_pred;
313                         } else {
314                                 generate_predicate(&curr_pred, next_inst, &half_pred_expressions);
315                                 bool branch_found = follow_branch(&curr_pred, next_inst, next_act, &inst_act_map, NULL);
316                                 ASSERT(branch_found);
317                         }
318                 }
319
320                 inst_pred_map.put(next_inst, curr_pred);
321                 if (!inst_id_map.contains(next_inst))
322                         inst_id_map.put(next_inst, inst_counter++);
323
324                 loc_act_map.put(next_act->get_location(), next_act);
325                 inst_act_map.put(next_inst, next_act);
326                 it = it->getNext();
327         }
328 }
329
330 /* Given curr_pred and next_inst, find the branch following curr_pred that
331  * contains next_inst and the correct predicate. 
332  * @return true if branch found, false otherwise.
333  */
334 bool FuncNode::follow_branch(Predicate ** curr_pred, FuncInst * next_inst, ModelAction * next_act,
335         HashTable<FuncInst *, ModelAction *, uintptr_t, 0> * inst_act_map,
336         SnapVector<Predicate *> * unset_predicates)
337 {
338         /* check if a branch with func_inst and corresponding predicate exists */
339         bool branch_found = false;
340         ModelVector<Predicate *> * branches = (*curr_pred)->get_children();
341         for (uint i = 0; i < branches->size(); i++) {
342                 Predicate * branch = (*branches)[i];
343                 if (branch->get_func_inst() != next_inst)
344                         continue;
345
346                 /* check against predicate expressions */
347                 bool predicate_correct = true;
348                 PredExprSet * pred_expressions = branch->get_pred_expressions();
349                 PredExprSetIter * pred_expr_it = pred_expressions->iterator();
350
351                 if (pred_expressions->getSize() == 0) {
352                         predicate_correct = false;
353                         unset_predicates->push_back(branch);
354                 }
355
356                 while (pred_expr_it->hasNext()) {
357                         pred_expr * pred_expression = pred_expr_it->next();
358                         uint64_t last_read, next_read;
359                         bool equality;
360
361                         switch(pred_expression->token) {
362                                 case NOPREDICATE:
363                                         predicate_correct = true;
364                                         break;
365                                 case EQUALITY:
366                                         FuncInst * to_be_compared;
367                                         ModelAction * last_act;
368
369                                         to_be_compared = pred_expression->func_inst;
370                                         last_act = inst_act_map->get(to_be_compared);
371
372                                         last_read = last_act->get_reads_from_value();
373                                         next_read = next_act->get_reads_from_value();
374                                         equality = (last_read == next_read);
375                                         if (equality != pred_expression->value)
376                                                 predicate_correct = false;
377
378                                         break;
379                                 case NULLITY:
380                                         next_read = next_act->get_reads_from_value();
381                                         equality = ((void*)next_read == NULL);
382                                         if (equality != pred_expression->value)
383                                                 predicate_correct = false;
384                                         break;
385                                 default:
386                                         predicate_correct = false;
387                                         model_print("unkown predicate token\n");
388                                         break;
389                         }
390                 }
391
392                 if (predicate_correct) {
393                         *curr_pred = branch;
394                         branch_found = true;
395                         break;
396                 }
397         }
398
399         return branch_found;
400 }
401
402 /* Able to generate complex predicates when there are multiple predciate expressions */
403 void FuncNode::generate_predicate(Predicate ** curr_pred, FuncInst * next_inst,
404         SnapVector<struct half_pred_expr *> * half_pred_expressions)
405 {
406         ASSERT(half_pred_expressions->size() != 0);
407         SnapVector<Predicate *> predicates;
408
409         struct half_pred_expr * half_expr = (*half_pred_expressions)[0];
410         predicates.push_back(new Predicate(next_inst));
411         predicates.push_back(new Predicate(next_inst));
412
413         predicates[0]->add_predicate_expr(half_expr->token, half_expr->func_inst, true);
414         predicates[1]->add_predicate_expr(half_expr->token, half_expr->func_inst, false);
415
416         for (uint i = 1; i < half_pred_expressions->size(); i++) {
417                 half_expr = (*half_pred_expressions)[i];
418
419                 uint old_size = predicates.size();
420                 for (uint j = 0; j < old_size; j++) {
421                         Predicate * pred = predicates[j];
422                         Predicate * new_pred = new Predicate(next_inst);
423                         new_pred->copy_predicate_expr(pred);
424
425                         pred->add_predicate_expr(half_expr->token, half_expr->func_inst, true);
426                         new_pred->add_predicate_expr(half_expr->token, half_expr->func_inst, false);
427
428                         predicates.push_back(new_pred);
429                 }
430         }
431
432         for (uint i = 0; i < predicates.size(); i++) {
433                 Predicate * pred= predicates[i];
434                 (*curr_pred)->add_child(pred);
435                 pred->set_parent(*curr_pred);
436         }
437 }
438
439 /* Amend predicates that contain no predicate expressions. Currenlty only amend with NULLITY predicates */
440 bool FuncNode::amend_predicate_expr(Predicate ** curr_pred, FuncInst * next_inst, ModelAction * next_act)
441 {
442         // there should only be only child
443         Predicate * unset_pred = (*curr_pred)->get_children()->back();
444         uint64_t read_val = next_act->get_reads_from_value();
445
446         // only generate NULLITY predicate when it is actually NULL.
447         if ( !next_inst->is_single_location() && (void*)read_val == NULL ) {
448                 Predicate * new_pred = new Predicate(next_inst);
449
450                 (*curr_pred)->add_child(new_pred);
451                 new_pred->set_parent(*curr_pred);
452
453                 unset_pred->add_predicate_expr(NULLITY, NULL, false);
454                 new_pred->add_predicate_expr(NULLITY, NULL, true);
455
456                 return true;
457         }
458
459         return false;
460 }
461
462 void FuncNode::add_to_val_loc_map(uint64_t val, void * loc)
463 {
464         loc_set_t * locations = val_loc_map->get(val);
465
466         if (locations == NULL) {
467                 locations = new loc_set_t();
468                 val_loc_map->put(val, locations);
469         }
470
471         update_loc_may_equal_map(loc, locations);
472         locations->add(loc);
473         // values_may_read_from->add(val);
474 }
475
476 void FuncNode::add_to_val_loc_map(value_set_t * values, void * loc)
477 {
478         if (values == NULL)
479                 return;
480
481         value_set_iter * it = values->iterator();
482         while (it->hasNext()) {
483                 uint64_t val = it->next();
484                 add_to_val_loc_map(val, loc);
485         }
486 }
487
488 void FuncNode::update_loc_may_equal_map(void * new_loc, loc_set_t * old_locations)
489 {
490         if ( old_locations->contains(new_loc) )
491                 return;
492
493         loc_set_t * neighbors = loc_may_equal_map->get(new_loc);
494
495         if (neighbors == NULL) {
496                 neighbors = new loc_set_t();
497                 loc_may_equal_map->put(new_loc, neighbors);
498         }
499
500         loc_set_iter * loc_it = old_locations->iterator();
501         while (loc_it->hasNext()) {
502                 // new_loc: { old_locations, ... }
503                 void * member = loc_it->next();
504                 neighbors->add(member);
505
506                 // for each i in old_locations, i : { new_loc, ... }
507                 loc_set_t * _neighbors = loc_may_equal_map->get(member);
508                 if (_neighbors == NULL) {
509                         _neighbors = new loc_set_t();
510                         loc_may_equal_map->put(member, _neighbors);
511                 }
512                 _neighbors->add(new_loc);
513         }
514 }
515
516 void FuncNode::init_predicate_tree_position(thread_id_t tid)
517 {
518         int thread_id = id_to_int(tid);
519         if (predicate_tree_position.size() <= (uint) thread_id)
520                 predicate_tree_position.resize(thread_id + 1);
521
522         predicate_tree_position[thread_id] = predicate_tree_entry;
523 }
524
525 void FuncNode::set_predicate_tree_position(thread_id_t tid, Predicate * pred)
526 {
527         int thread_id = id_to_int(tid);
528         predicate_tree_position[thread_id] = pred;
529 }
530
531 Predicate * FuncNode::get_predicate_tree_position(thread_id_t tid)
532 {
533         int thread_id = id_to_int(tid);
534         return predicate_tree_position[thread_id];
535 }
536
537 void FuncNode::init_inst_act_map(thread_id_t tid)
538 {
539         int thread_id = id_to_int(tid);
540         uint old_size = thrd_inst_act_map->size();
541
542         if (thrd_inst_act_map->size() <= (uint) thread_id) {
543                 uint new_size = thread_id + 1;
544                 thrd_inst_act_map->resize(new_size);
545
546                 for (uint i = old_size; i < new_size; i++)
547                         (*thrd_inst_act_map)[i] = new inst_act_map_t(128);
548         }
549 }
550
551 void FuncNode::reset_inst_act_map(thread_id_t tid)
552 {
553         int thread_id = id_to_int(tid);
554         inst_act_map_t * map = (*thrd_inst_act_map)[thread_id];
555         map->reset();
556 }
557
558 void FuncNode::update_inst_act_map(thread_id_t tid, ModelAction * read_act)
559 {
560         int thread_id = id_to_int(tid);
561         inst_act_map_t * map = (*thrd_inst_act_map)[thread_id];
562         FuncInst * read_inst = get_inst(read_act);
563         map->put(read_inst, read_act);
564 }
565
566 inst_act_map_t * FuncNode::get_inst_act_map(thread_id_t tid)
567 {
568         int thread_id = id_to_int(tid);
569         return (*thrd_inst_act_map)[thread_id];
570 }
571
572 void FuncNode::print_predicate_tree()
573 {
574         model_print("digraph function_%s {\n", func_name);
575         predicate_tree_entry->print_pred_subtree();
576         model_print("}\n");     // end of graph
577 }
578
579 void FuncNode::print_val_loc_map()
580 {
581 /*
582         value_set_iter * val_it = values_may_read_from->iterator();
583         while (val_it->hasNext()) {
584                 uint64_t value = val_it->next();
585                 model_print("val %llx: ", value);
586
587                 loc_set_t * locations = val_loc_map->get(value);
588                 loc_set_iter * loc_it = locations->iterator();
589                 while (loc_it->hasNext()) {
590                         void * location = loc_it->next();
591                         model_print("%p ", location);
592                 }
593                 model_print("\n");
594         }
595 */
596 }