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