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