performace fix
[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         //values_may_read_from = new value_set_t();
22 }
23
24 /* Reallocate snapshotted memories when new executions start */
25 void FuncNode::set_new_exec_flag()
26 {
27 //      for (uint i = 0; i < thrd_read_map.size(); i++)
28 //              thrd_read_map[i] = new read_map_t();
29
30         for (mllnode<FuncInst *> * it = inst_list.begin(); it != NULL; it = it->getNext()) {
31                 FuncInst * inst = it->getVal();
32                 inst->unset_location();
33         }
34
35         read_locations = new loc_set_t();
36         val_loc_map = new HashTable<uint64_t, loc_set_t *, uint64_t, 0>();
37         loc_may_equal_map = new HashTable<void *, loc_set_t *, uintptr_t, 0>();
38         //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 /* @param tid thread id
212  * Store the values read by atomic read actions into thrd_read_map */
213 void FuncNode::store_read(ModelAction * act, thread_id_t tid)
214 {
215 /*
216         ASSERT(act);
217
218         void * location = act->get_location();
219         uint64_t read_from_val = act->get_reads_from_value();
220
221         // resize and initialize
222         uint32_t old_size = thrd_read_map.size();
223         if (old_size <= tid) {
224                 thrd_read_map.resize(tid + 1);
225                 for (uint32_t i = old_size; i < tid + 1;i++)
226                         thrd_read_map[i] = new read_map_t();
227         }
228
229         read_map_t * read_map = thrd_read_map[tid];
230         read_map->put(location, read_from_val);
231 */
232 }
233
234 uint64_t FuncNode::query_last_read(void * location, thread_id_t tid)
235 {
236 /*
237         if (thrd_read_map.size() <= tid)
238                 return VALUE_NONE;
239
240         read_map_t * read_map = thrd_read_map[tid];
241
242         // last read value not found
243         if ( !read_map->contains(location) )
244                 return VALUE_NONE;
245
246         uint64_t read_val = read_map->get(location);
247         return read_val;
248 */
249 }
250
251 /* @param tid thread id
252  * Reset read map for a thread. This function shall only be called
253  * when a thread exits a function
254  */
255 void FuncNode::clear_read_map(thread_id_t tid)
256 {
257 /*
258         if (thrd_read_map.size() <= tid)
259                 return;
260
261         thrd_read_map[tid]->reset();
262 */
263 }
264
265 void FuncNode::update_predicate_tree(action_list_t * act_list)
266 {
267         if (act_list == NULL || act_list->size() == 0)
268                 return;
269
270         /* map a FuncInst to the its predicate */
271         HashTable<FuncInst *, Predicate *, uintptr_t, 0> inst_pred_map(128);
272
273         // number FuncInsts to detect loops
274         HashTable<FuncInst *, uint32_t, uintptr_t, 0> inst_id_map(128);
275         uint32_t inst_counter = 0;
276
277         HashTable<void *, ModelAction *, uintptr_t, 0> loc_act_map(128);
278         HashTable<FuncInst *, ModelAction *, uintptr_t, 0> inst_act_map(128);
279
280         sllnode<ModelAction *> *it = act_list->begin();
281         Predicate * curr_pred = predicate_tree_entry;
282         while (it != NULL) {
283                 ModelAction * next_act = it->getVal();
284                 FuncInst * next_inst = get_inst(next_act);
285
286                 SnapVector<Predicate *> unset_predicates = SnapVector<Predicate *>();
287                 bool branch_found = follow_branch(&curr_pred, next_inst, next_act, &inst_act_map, &unset_predicates);
288
289                 // no predicate expressions
290                 if (!branch_found && unset_predicates.size() != 0) {
291                         ASSERT(unset_predicates.size() == 1);
292                         Predicate * one_branch = unset_predicates[0];
293
294                         bool amended = amend_predicate_expr(&curr_pred, next_inst, next_act);
295                         if (amended)
296                                 continue;
297                         else {
298                                 curr_pred = one_branch;
299                                 branch_found = true;
300                         }
301                 }
302
303                 // detect loops
304                 if (!branch_found && inst_id_map.contains(next_inst)) {
305                         FuncInst * curr_inst = curr_pred->get_func_inst();
306                         uint32_t curr_id = inst_id_map.get(curr_inst);
307                         uint32_t next_id = inst_id_map.get(next_inst);
308
309                         if (curr_id >= next_id) {
310                                 Predicate * old_pred = inst_pred_map.get(next_inst);
311                                 Predicate * back_pred = old_pred->get_parent();
312
313                                 curr_pred->add_backedge(back_pred);
314                                 curr_pred = back_pred;
315
316                                 continue;
317                         }
318                 }
319
320                 // generate new branches
321                 if (!branch_found) {
322                         SnapVector<struct half_pred_expr *> half_pred_expressions;
323                         void * loc = next_act->get_location();
324
325                         if ( loc_act_map.contains(loc) ) {
326                                 ModelAction * last_act = loc_act_map.get(loc);
327                                 FuncInst * last_inst = get_inst(last_act);
328                                 struct half_pred_expr * expression = new half_pred_expr(EQUALITY, last_inst);
329                                 half_pred_expressions.push_back(expression);
330                         } else if ( next_inst->is_single_location() ){
331                                 loc_set_t * loc_may_equal = loc_may_equal_map->get(loc);
332
333                                 if (loc_may_equal != NULL) {
334                                         loc_set_iter * loc_it = loc_may_equal->iterator();
335                                         while (loc_it->hasNext()) {
336                                                 void * neighbor = loc_it->next();
337                                                 if (loc_act_map.contains(neighbor)) {
338                                                         ModelAction * last_act = loc_act_map.get(neighbor);
339                                                         FuncInst * last_inst = get_inst(last_act);
340
341                                                         struct half_pred_expr * expression = new half_pred_expr(EQUALITY, last_inst);
342                                                         half_pred_expressions.push_back(expression);
343                                                 }
344                                         }
345                                 } 
346                         } else {
347                                 // next_inst is not single location
348                                 uint64_t read_val = next_act->get_reads_from_value();
349
350                                 // only generate NULLITY predicate when it is actually NULL.
351                                 if ( (void*)read_val == NULL) {
352                                         struct half_pred_expr * expression = new half_pred_expr(NULLITY, NULL);
353                                         half_pred_expressions.push_back(expression);
354                                 }
355                         }
356
357                         if (half_pred_expressions.size() == 0) {
358                                 // no predicate needs to be generated
359                                 Predicate * new_pred = new Predicate(next_inst);
360                                 curr_pred->add_child(new_pred);
361                                 new_pred->set_parent(curr_pred);
362
363                                 if (curr_pred->is_entry_predicate())
364                                         new_pred->add_predicate_expr(NOPREDICATE, NULL, true);
365
366                                 curr_pred = new_pred;
367                         } else {
368                                 generate_predicate(&curr_pred, next_inst, &half_pred_expressions);
369                                 bool branch_found = follow_branch(&curr_pred, next_inst, next_act, &inst_act_map, NULL);
370                                 ASSERT(branch_found);
371                         }
372                 }
373
374                 inst_pred_map.put(next_inst, curr_pred);
375                 if (!inst_id_map.contains(next_inst))
376                         inst_id_map.put(next_inst, inst_counter++);
377
378                 loc_act_map.put(next_act->get_location(), next_act);
379                 inst_act_map.put(next_inst, next_act);
380                 it = it->getNext();
381         }
382 }
383
384 /* Given curr_pred and next_inst, find the branch following curr_pred that
385  * contains next_inst and the correct predicate. 
386  * @return true if branch found, false otherwise.
387  */
388 bool FuncNode::follow_branch(Predicate ** curr_pred, FuncInst * next_inst, ModelAction * next_act,
389         HashTable<FuncInst *, ModelAction *, uintptr_t, 0> * inst_act_map,
390         SnapVector<Predicate *> * unset_predicates)
391 {
392         /* check if a branch with func_inst and corresponding predicate exists */
393         bool branch_found = false;
394         ModelVector<Predicate *> * branches = (*curr_pred)->get_children();
395         for (uint i = 0; i < branches->size(); i++) {
396                 Predicate * branch = (*branches)[i];
397                 if (branch->get_func_inst() != next_inst)
398                         continue;
399
400                 /* check against predicate expressions */
401                 bool predicate_correct = true;
402                 PredExprSet * pred_expressions = branch->get_pred_expressions();
403                 PredExprSetIter * pred_expr_it = pred_expressions->iterator();
404
405                 if (pred_expressions->getSize() == 0) {
406                         predicate_correct = false;
407                         unset_predicates->push_back(branch);
408                 }
409
410                 while (pred_expr_it->hasNext()) {
411                         pred_expr * pred_expression = pred_expr_it->next();
412                         uint64_t last_read, next_read;
413                         bool equality;
414
415                         switch(pred_expression->token) {
416                                 case NOPREDICATE:
417                                         predicate_correct = true;
418                                         break;
419                                 case EQUALITY:
420                                         FuncInst * to_be_compared;
421                                         ModelAction * last_act;
422
423                                         to_be_compared = pred_expression->func_inst;
424                                         last_act = inst_act_map->get(to_be_compared);
425
426                                         last_read = last_act->get_reads_from_value();
427                                         next_read = next_act->get_reads_from_value();
428                                         equality = (last_read == next_read);
429                                         if (equality != pred_expression->value)
430                                                 predicate_correct = false;
431
432                                         break;
433                                 case NULLITY:
434                                         next_read = next_act->get_reads_from_value();
435                                         equality = ((void*)next_read == NULL);
436                                         if (equality != pred_expression->value)
437                                                 predicate_correct = false;
438                                         break;
439                                 default:
440                                         predicate_correct = false;
441                                         model_print("unkown predicate token\n");
442                                         break;
443                         }
444                 }
445
446                 if (predicate_correct) {
447                         *curr_pred = branch;
448                         branch_found = true;
449                         break;
450                 }
451         }
452
453         return branch_found;
454 }
455
456 /* Able to generate complex predicates when there are multiple predciate expressions */
457 void FuncNode::generate_predicate(Predicate ** curr_pred, FuncInst * next_inst,
458         SnapVector<struct half_pred_expr *> * half_pred_expressions)
459 {
460         ASSERT(half_pred_expressions->size() != 0);
461         SnapVector<Predicate *> predicates;
462
463         struct half_pred_expr * half_expr = (*half_pred_expressions)[0];
464         predicates.push_back(new Predicate(next_inst));
465         predicates.push_back(new Predicate(next_inst));
466
467         predicates[0]->add_predicate_expr(half_expr->token, half_expr->func_inst, true);
468         predicates[1]->add_predicate_expr(half_expr->token, half_expr->func_inst, false);
469
470         for (uint i = 1; i < half_pred_expressions->size(); i++) {
471                 half_expr = (*half_pred_expressions)[i];
472
473                 uint old_size = predicates.size();
474                 for (uint j = 0; j < old_size; j++) {
475                         Predicate * pred = predicates[j];
476                         Predicate * new_pred = new Predicate(next_inst);
477                         new_pred->copy_predicate_expr(pred);
478
479                         pred->add_predicate_expr(half_expr->token, half_expr->func_inst, true);
480                         new_pred->add_predicate_expr(half_expr->token, half_expr->func_inst, false);
481
482                         predicates.push_back(new_pred);
483                 }
484         }
485
486         for (uint i = 0; i < predicates.size(); i++) {
487                 Predicate * pred= predicates[i];
488                 (*curr_pred)->add_child(pred);
489                 pred->set_parent(*curr_pred);
490         }
491 }
492
493 /* Amend predicates that contain no predicate expressions. Currenlty only amend with NULLITY predicates */
494 bool FuncNode::amend_predicate_expr(Predicate ** curr_pred, FuncInst * next_inst, ModelAction * next_act)
495 {
496         // there should only be only child
497         Predicate * unset_pred = (*curr_pred)->get_children()->back();
498         uint64_t read_val = next_act->get_reads_from_value();
499
500         // only generate NULLITY predicate when it is actually NULL.
501         if ( !next_inst->is_single_location() && (void*)read_val == NULL ) {
502                 Predicate * new_pred = new Predicate(next_inst);
503
504                 (*curr_pred)->add_child(new_pred);
505                 new_pred->set_parent(*curr_pred);
506
507                 unset_pred->add_predicate_expr(NULLITY, NULL, false);
508                 new_pred->add_predicate_expr(NULLITY, NULL, true);
509
510                 return true;
511         }
512
513         return false;
514 }
515
516 void FuncNode::add_to_val_loc_map(uint64_t val, void * loc)
517 {
518         loc_set_t * locations = val_loc_map->get(val);
519
520         if (locations == NULL) {
521                 locations = new loc_set_t();
522                 val_loc_map->put(val, locations);
523         }
524
525         update_loc_may_equal_map(loc, locations);
526         locations->add(loc);
527         // values_may_read_from->add(val);
528 }
529
530 void FuncNode::add_to_val_loc_map(value_set_t * values, void * loc)
531 {
532         if (values == NULL)
533                 return;
534
535         value_set_iter * it = values->iterator();
536         while (it->hasNext()) {
537                 uint64_t val = it->next();
538                 add_to_val_loc_map(val, loc);
539         }
540 }
541
542 void FuncNode::update_loc_may_equal_map(void * new_loc, loc_set_t * old_locations)
543 {
544         if ( old_locations->contains(new_loc) )
545                 return;
546
547         loc_set_t * neighbors = loc_may_equal_map->get(new_loc);
548
549         if (neighbors == NULL) {
550                 neighbors = new loc_set_t();
551                 loc_may_equal_map->put(new_loc, neighbors);
552         }
553
554         loc_set_iter * loc_it = old_locations->iterator();
555         while (loc_it->hasNext()) {
556                 // new_loc: { old_locations, ... }
557                 void * member = loc_it->next();
558                 neighbors->add(member);
559
560                 // for each i in old_locations, i : { new_loc, ... }
561                 loc_set_t * _neighbors = loc_may_equal_map->get(member);
562                 if (_neighbors == NULL) {
563                         _neighbors = new loc_set_t();
564                         loc_may_equal_map->put(member, _neighbors);
565                 }
566                 _neighbors->add(new_loc);
567         }
568 }
569
570 void FuncNode::init_predicate_tree_position(thread_id_t tid)
571 {
572         uint thread_id = id_to_int(tid);
573         if (predicate_tree_position.size() <= thread_id)
574                 predicate_tree_position.resize(thread_id + 1);
575
576         predicate_tree_position[thread_id] = predicate_tree_entry;
577 }
578
579 void FuncNode::unset_predicate_tree_position(thread_id_t tid)
580 {
581         uint thread_id = id_to_int(tid);
582         predicate_tree_position[thread_id] = NULL;
583 }
584
585 Predicate * FuncNode::get_predicate_tree_position(thread_id_t tid)
586 {
587         uint thread_id = id_to_int(tid);
588         return predicate_tree_position[thread_id];
589 }
590
591 void FuncNode::print_predicate_tree()
592 {
593         model_print("digraph function_%s {\n", func_name);
594         predicate_tree_entry->print_pred_subtree();
595         model_print("}\n");     // end of graph
596 }
597
598 void FuncNode::print_val_loc_map()
599 {
600 /*
601         value_set_iter * val_it = values_may_read_from->iterator();
602         while (val_it->hasNext()) {
603                 uint64_t value = val_it->next();
604                 model_print("val %llx: ", value);
605
606                 loc_set_t * locations = val_loc_map->get(value);
607                 loc_set_iter * loc_it = locations->iterator();
608                 while (loc_it->hasNext()) {
609                         void * location = loc_it->next();
610                         model_print("%p ", location);
611                 }
612                 model_print("\n");
613         }
614 */
615 }
616
617 /* @param tid thread id
618  * Print the values read by the last read actions for each memory location
619  */
620 /*
621 void FuncNode::print_last_read(thread_id_t tid)
622 {
623         ASSERT(thrd_read_map.size() > tid);
624         read_map_t * read_map = thrd_read_map[tid];
625
626         mllnode<void *> * it;
627         for (it = read_locations.begin();it != NULL;it=it->getNext()) {
628                 if ( !read_map->contains(it->getVal()) )
629                         break;
630
631                 uint64_t read_val = read_map->get(it->getVal());
632                 model_print("last read of thread %d at %p: 0x%x\n", tid, it->getVal(), read_val);
633         }
634 }
635 */