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