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