Need to handle recursive calls
[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 #include <cmath>
10
11 FuncNode::FuncNode(ModelHistory * history) :
12         history(history),
13         inst_counter(1),
14         marker(1),
15         thrd_markers(),
16         thrd_recursion_depth(),
17         func_inst_map(),
18         inst_list(),
19         entry_insts(),
20         thrd_inst_pred_map(),
21         thrd_inst_id_map(),
22         thrd_loc_inst_map(),
23         thrd_predicate_tree_position(),
24         thrd_predicate_trace(),
25         edge_table(32),
26         out_edges()
27 {
28         predicate_tree_entry = new Predicate(NULL, true);
29         predicate_tree_entry->add_predicate_expr(NOPREDICATE, NULL, true);
30
31         predicate_tree_exit = new Predicate(NULL, false, true);
32         predicate_tree_exit->set_depth(MAX_DEPTH);
33
34         /* Snapshot data structures below */
35         read_locations = new loc_set_t();
36         write_locations = new loc_set_t();
37         val_loc_map = new HashTable<uint64_t, loc_set_t *, uint64_t, 0, snapshot_malloc, snapshot_calloc, snapshot_free, int64_hash>();
38         loc_may_equal_map = new HashTable<void *, loc_set_t *, uintptr_t, 0>();
39
40         //values_may_read_from = new value_set_t();
41 }
42
43 /* Reallocate snapshotted memories when new executions start */
44 void FuncNode::set_new_exec_flag()
45 {
46         read_locations = new loc_set_t();
47         write_locations = new loc_set_t();
48         val_loc_map = new HashTable<uint64_t, loc_set_t *, uint64_t, 0, snapshot_malloc, snapshot_calloc, snapshot_free, int64_hash>();
49         loc_may_equal_map = new HashTable<void *, loc_set_t *, uintptr_t, 0>();
50
51         //values_may_read_from = new value_set_t();
52 }
53
54 /* Check whether FuncInst with the same type, position, and location
55  * as act has been added to func_inst_map or not. If not, add it.
56  */
57 void FuncNode::add_inst(ModelAction *act)
58 {
59         ASSERT(act);
60         const char * position = act->get_position();
61
62         /* THREAD* actions, ATOMIC_LOCK, ATOMIC_TRYLOCK, and ATOMIC_UNLOCK
63          * actions are not tagged with their source line numbers
64          */
65         if (position == NULL)
66                 return;
67
68         FuncInst * func_inst = func_inst_map.get(position);
69
70         /* This position has not been inserted into hashtable before */
71         if (func_inst == NULL) {
72                 func_inst = create_new_inst(act);
73                 func_inst_map.put(position, func_inst);
74                 return;
75         }
76
77         /* Volatile variables that use ++ or -- syntax may result in read and write actions with the same position */
78         if (func_inst->get_type() != act->get_type()) {
79                 FuncInst * collision_inst = func_inst->search_in_collision(act);
80
81                 if (collision_inst == NULL) {
82                         collision_inst = create_new_inst(act);
83                         func_inst->add_to_collision(collision_inst);
84                         return;
85                 } else {
86                         func_inst = collision_inst;
87                 }
88         }
89
90         ASSERT(func_inst->get_type() == act->get_type());
91         int curr_execution_number = model->get_execution_number();
92
93         /* Reset locations when new executions start */
94         if (func_inst->get_execution_number() != curr_execution_number) {
95                 func_inst->set_location(act->get_location());
96                 func_inst->set_execution_number(curr_execution_number);
97         }
98
99         /* Mark the memory location of such inst as not unique */
100         if (func_inst->get_location() != act->get_location())
101                 func_inst->not_single_location();
102 }
103
104 FuncInst * FuncNode::create_new_inst(ModelAction * act)
105 {
106         FuncInst * func_inst = new FuncInst(act, this);
107         int exec_num = model->get_execution_number();
108         func_inst->set_execution_number(exec_num);
109
110         inst_list.push_back(func_inst);
111
112         return func_inst;
113 }
114
115
116 /* Get the FuncInst with the same type, position, and location
117  * as act
118  *
119  * @return FuncInst with the same type, position, and location as act */
120 FuncInst * FuncNode::get_inst(ModelAction *act)
121 {
122         ASSERT(act);
123         const char * position = act->get_position();
124
125         /* THREAD* actions, ATOMIC_LOCK, ATOMIC_TRYLOCK, and ATOMIC_UNLOCK
126          * actions are not tagged with their source line numbers
127          */
128         if (position == NULL)
129                 return NULL;
130
131         FuncInst * inst = func_inst_map.get(position);
132         if (inst == NULL)
133                 return NULL;
134
135         action_type inst_type = inst->get_type();
136         action_type act_type = act->get_type();
137
138         if (inst_type == act_type) {
139                 return inst;
140         }
141         /* RMWRCAS actions are converted to RMW or READ actions */
142         else if (inst_type == ATOMIC_RMWRCAS &&
143                                          (act_type == ATOMIC_RMW || act_type == ATOMIC_READ)) {
144                 return inst;
145         }
146         /* Return the FuncInst in the collision list */
147         else {
148                 return inst->search_in_collision(act);
149         }
150 }
151
152 void FuncNode::add_entry_inst(FuncInst * inst)
153 {
154         if (inst == NULL)
155                 return;
156
157         mllnode<FuncInst *> * it;
158         for (it = entry_insts.begin();it != NULL;it = it->getNext()) {
159                 if (inst == it->getVal())
160                         return;
161         }
162
163         entry_insts.push_back(inst);
164 }
165
166 void FuncNode::function_entry_handler(thread_id_t tid)
167 {
168         init_marker(tid);
169         init_inst_act_map(tid);
170         init_local_maps(tid);
171         init_predicate_tree_data_structure(tid);
172 }
173
174 void FuncNode::function_exit_handler(thread_id_t tid)
175 {
176         int thread_id = id_to_int(tid);
177         thrd_recursion_depth[thread_id]--;
178         thrd_markers[thread_id]->pop_back();
179
180         reset_inst_act_map(tid);
181         reset_local_maps(tid);
182
183         Predicate * exit_pred = get_predicate_tree_position(tid);
184         if (exit_pred->get_exit() == NULL) {
185                 // Exit predicate is unset yet
186                 exit_pred->set_exit(predicate_tree_exit);
187         }
188
189         update_predicate_tree_weight(tid);
190         reset_predicate_tree_data_structure(tid);
191 }
192
193 /**
194  * @brief Convert ModelAdtion list to FuncInst list
195  * @param act_list A list of ModelActions
196  */
197 void FuncNode::update_tree(ModelAction * act)
198 {
199         bool should_process = act->is_read() || act->is_write();
200         if (!should_process)
201                 return;
202
203         HashTable<void *, value_set_t *, uintptr_t, 0> * write_history = history->getWriteHistory();
204
205         /* build inst_list from act_list for later processing */
206 //      func_inst_list_t inst_list;
207
208         FuncInst * func_inst = get_inst(act);
209         void * loc = act->get_location();
210
211         if (func_inst == NULL)
212                 return;
213
214 //      inst_list.push_back(func_inst);
215
216         if (act->is_write()) {
217                 if (!write_locations->contains(loc)) {
218                         write_locations->add(loc);
219                         history->update_loc_wr_func_nodes_map(loc, this);
220                 }
221
222                 // Do not process writes for now
223                 return;
224         }
225
226         if (act->is_read()) {
227
228                 /* If func_inst may only read_from a single location, then:
229                  *
230                  * The first time an action reads from some location,
231                  * import all the values that have been written to this
232                  * location from ModelHistory and notify ModelHistory
233                  * that this FuncNode may read from this location.
234                  */
235                 if (!read_locations->contains(loc) && func_inst->is_single_location()) {
236                         read_locations->add(loc);
237                         value_set_t * write_values = write_history->get(loc);
238                         add_to_val_loc_map(write_values, loc);
239                         history->update_loc_rd_func_nodes_map(loc, this);
240                 }
241         }
242
243 //      update_inst_tree(&inst_list); TODO
244         update_predicate_tree(act);
245
246 //      print_predicate_tree();
247 }
248
249 /**
250  * @brief Link FuncInsts in inst_list  - add one FuncInst to another's predecessors and successors
251  * @param inst_list A list of FuncInsts
252  */
253 void FuncNode::update_inst_tree(func_inst_list_t * inst_list)
254 {
255         if (inst_list == NULL)
256                 return;
257         else if (inst_list->size() == 0)
258                 return;
259
260         /* start linking */
261         sllnode<FuncInst *>* it = inst_list->begin();
262         sllnode<FuncInst *>* prev;
263
264         /* add the first instruction to the list of entry insts */
265         FuncInst * entry_inst = it->getVal();
266         add_entry_inst(entry_inst);
267
268         it = it->getNext();
269         while (it != NULL) {
270                 prev = it->getPrev();
271
272                 FuncInst * prev_inst = prev->getVal();
273                 FuncInst * curr_inst = it->getVal();
274
275                 prev_inst->add_succ(curr_inst);
276                 curr_inst->add_pred(prev_inst);
277
278                 it = it->getNext();
279         }
280 }
281
282 void FuncNode::update_predicate_tree(ModelAction * next_act)
283 {
284         thread_id_t tid = next_act->get_tid();
285         int thread_id = id_to_int(tid);
286         uint32_t this_marker = thrd_markers[thread_id]->back();
287         int recursion_depth = thrd_recursion_depth[thread_id];
288
289         loc_inst_map_t * loc_inst_map = thrd_loc_inst_map[thread_id];
290         inst_pred_map_t * inst_pred_map = thrd_inst_pred_map[thread_id];
291         inst_id_map_t * inst_id_map = thrd_inst_id_map[thread_id];
292
293         Predicate * curr_pred = get_predicate_tree_position(tid);
294         while (true) {
295                 FuncInst * next_inst = get_inst(next_act);
296                 next_inst->set_associated_read(tid, recursion_depth, this_marker, next_act->get_reads_from_value());
297
298                 Predicate * unset_predicate = NULL;
299                 bool branch_found = follow_branch(&curr_pred, next_inst, next_act, &unset_predicate);
300
301                 // A branch with unset predicate expression is detected
302                 if (!branch_found && unset_predicate != NULL) {
303                         bool amended = amend_predicate_expr(curr_pred, next_inst, next_act);
304                         if (amended)
305                                 continue;
306                         else {
307                                 curr_pred = unset_predicate;
308                                 branch_found = true;
309                         }
310                 }
311
312                 // Detect loops
313                 if (!branch_found && inst_id_map->contains(next_inst)) {
314                         FuncInst * curr_inst = curr_pred->get_func_inst();
315                         uint32_t curr_id = inst_id_map->get(curr_inst);
316                         uint32_t next_id = inst_id_map->get(next_inst);
317
318                         if (curr_id >= next_id) {
319                                 Predicate * old_pred = inst_pred_map->get(next_inst);
320                                 Predicate * back_pred = old_pred->get_parent();
321
322                                 // Add to the set of backedges
323                                 curr_pred->add_backedge(back_pred);
324                                 curr_pred = back_pred;
325
326                                 continue;
327                         }
328                 }
329
330                 // Generate new branches
331                 if (!branch_found) {
332                         SnapVector<struct half_pred_expr *> half_pred_expressions;
333                         infer_predicates(next_inst, next_act, &half_pred_expressions);
334                         generate_predicates(curr_pred, next_inst, &half_pred_expressions);
335                         continue;
336                 }
337
338                 if (next_act->is_write())
339                         curr_pred->set_write(true);
340
341                 if (next_act->is_read()) {
342                         /* Only need to store the locations of read actions */
343                         loc_inst_map->put(next_inst->get_location(), next_inst);
344                 }
345
346                 inst_pred_map->put(next_inst, curr_pred);
347                 set_predicate_tree_position(tid, curr_pred);
348
349                 if (!inst_id_map->contains(next_inst))
350                         inst_id_map->put(next_inst, inst_counter++);
351
352                 curr_pred->incr_expl_count();
353                 add_predicate_to_trace(tid, curr_pred);
354                 break;
355         }
356 }
357
358 /* Given curr_pred and next_inst, find the branch following curr_pred that
359  * contains next_inst and the correct predicate.
360  * @return true if branch found, false otherwise.
361  */
362 bool FuncNode::follow_branch(Predicate ** curr_pred, FuncInst * next_inst,
363                                                                                                                  ModelAction * next_act, Predicate ** unset_predicate)
364 {
365         /* Check if a branch with func_inst and corresponding predicate exists */
366         bool branch_found = false;
367         thread_id_t tid = next_act->get_tid();
368         int thread_id = id_to_int(tid);
369         uint32_t this_marker = thrd_markers[thread_id]->back();
370         int recursion_depth = thrd_recursion_depth[thread_id];
371
372         ModelVector<Predicate *> * branches = (*curr_pred)->get_children();
373         for (uint i = 0;i < branches->size();i++) {
374                 Predicate * branch = (*branches)[i];
375                 if (branch->get_func_inst() != next_inst)
376                         continue;
377
378                 /* Check against predicate expressions */
379                 bool predicate_correct = true;
380                 PredExprSet * pred_expressions = branch->get_pred_expressions();
381
382                 /* Only read and rmw actions my have unset predicate expressions */
383                 if (pred_expressions->getSize() == 0) {
384                         predicate_correct = false;
385
386                         if (*unset_predicate == NULL)
387                                 *unset_predicate = branch;
388                         else
389                                 ASSERT(false);
390
391                         continue;
392                 }
393
394                 PredExprSetIter * pred_expr_it = pred_expressions->iterator();
395                 while (pred_expr_it->hasNext()) {
396                         pred_expr * pred_expression = pred_expr_it->next();
397                         uint64_t last_read, next_read;
398                         bool equality;
399
400                         switch(pred_expression->token) {
401                         case NOPREDICATE:
402                                 predicate_correct = true;
403                                 break;
404                         case EQUALITY:
405                                 FuncInst * to_be_compared;
406                                 to_be_compared = pred_expression->func_inst;
407
408                                 last_read = to_be_compared->get_associated_read(tid, recursion_depth, this_marker);
409                                 ASSERT(last_read != VALUE_NONE);
410
411                                 next_read = next_act->get_reads_from_value();
412                                 equality = (last_read == next_read);
413                                 if (equality != pred_expression->value)
414                                         predicate_correct = false;
415
416                                 break;
417                         case NULLITY:
418                                 next_read = next_act->get_reads_from_value();
419                                 // TODO: implement likely to be null
420                                 equality = ( (void*) (next_read & 0xffffffff) == NULL);
421                                 if (equality != pred_expression->value)
422                                         predicate_correct = false;
423                                 break;
424                         default:
425                                 predicate_correct = false;
426                                 model_print("unkown predicate token\n");
427                                 break;
428                         }
429                 }
430
431                 delete pred_expr_it;
432
433                 if (predicate_correct) {
434                         *curr_pred = branch;
435                         branch_found = true;
436                         break;
437                 }
438         }
439
440         return branch_found;
441 }
442
443 /* Infer predicate expressions, which are generated in FuncNode::generate_predicates */
444 void FuncNode::infer_predicates(FuncInst * next_inst, ModelAction * next_act,
445                                                                                                                                 SnapVector<struct half_pred_expr *> * half_pred_expressions)
446 {
447         void * loc = next_act->get_location();
448         int thread_id = id_to_int(next_act->get_tid());
449         loc_inst_map_t * loc_inst_map = thrd_loc_inst_map[thread_id];
450
451         if (next_inst->is_read()) {
452                 /* read + rmw */
453                 if ( loc_inst_map->contains(loc) ) {
454                         FuncInst * last_inst = loc_inst_map->get(loc);
455                         struct half_pred_expr * expression = new half_pred_expr(EQUALITY, last_inst);
456                         half_pred_expressions->push_back(expression);
457                 } else if ( next_inst->is_single_location() ) {
458                         loc_set_t * loc_may_equal = loc_may_equal_map->get(loc);
459
460                         if (loc_may_equal != NULL) {
461                                 loc_set_iter * loc_it = loc_may_equal->iterator();
462                                 while (loc_it->hasNext()) {
463                                         void * neighbor = loc_it->next();
464                                         if (loc_inst_map->contains(neighbor)) {
465                                                 FuncInst * last_inst = loc_inst_map->get(neighbor);
466
467                                                 struct half_pred_expr * expression = new half_pred_expr(EQUALITY, last_inst);
468                                                 half_pred_expressions->push_back(expression);
469                                         }
470                                 }
471
472                                 delete loc_it;
473                         }
474                 } else {
475                         // next_inst is not single location
476                         uint64_t read_val = next_act->get_reads_from_value();
477
478                         // only infer NULLITY predicate when it is actually NULL.
479                         if ( (void*)read_val == NULL) {
480                                 struct half_pred_expr * expression = new half_pred_expr(NULLITY, NULL);
481                                 half_pred_expressions->push_back(expression);
482                         }
483                 }
484         } else {
485                 /* Pure writes */
486                 // TODO: do anything here?
487         }
488 }
489
490 /* Able to generate complex predicates when there are multiple predciate expressions */
491 void FuncNode::generate_predicates(Predicate * curr_pred, FuncInst * next_inst,
492                                                                                                                                          SnapVector<struct half_pred_expr *> * half_pred_expressions)
493 {
494         if (half_pred_expressions->size() == 0) {
495                 Predicate * new_pred = new Predicate(next_inst);
496                 curr_pred->add_child(new_pred);
497                 new_pred->set_parent(curr_pred);
498
499                 /* entry predicates and predicates containing pure write actions
500                  * have no predicate expressions */
501                 if ( curr_pred->is_entry_predicate() )
502                         new_pred->add_predicate_expr(NOPREDICATE, NULL, true);
503                 else if (next_inst->is_write()) {
504                         /* next_inst->is_write() <==> pure writes */
505                         new_pred->add_predicate_expr(NOPREDICATE, NULL, true);
506                 }
507
508                 return;
509         }
510
511         SnapVector<Predicate *> predicates;
512
513         struct half_pred_expr * half_expr = (*half_pred_expressions)[0];
514         predicates.push_back(new Predicate(next_inst));
515         predicates.push_back(new Predicate(next_inst));
516
517         predicates[0]->add_predicate_expr(half_expr->token, half_expr->func_inst, true);
518         predicates[1]->add_predicate_expr(half_expr->token, half_expr->func_inst, false);
519
520         for (uint i = 1;i < half_pred_expressions->size();i++) {
521                 half_expr = (*half_pred_expressions)[i];
522
523                 uint old_size = predicates.size();
524                 for (uint j = 0;j < old_size;j++) {
525                         Predicate * pred = predicates[j];
526                         Predicate * new_pred = new Predicate(next_inst);
527                         new_pred->copy_predicate_expr(pred);
528
529                         pred->add_predicate_expr(half_expr->token, half_expr->func_inst, true);
530                         new_pred->add_predicate_expr(half_expr->token, half_expr->func_inst, false);
531
532                         predicates.push_back(new_pred);
533                 }
534         }
535
536         for (uint i = 0;i < predicates.size();i++) {
537                 Predicate * pred= predicates[i];
538                 curr_pred->add_child(pred);
539                 pred->set_parent(curr_pred);
540         }
541
542         /* Free memories allocated by infer_predicate */
543         for (uint i = 0;i < half_pred_expressions->size();i++) {
544                 struct half_pred_expr * tmp = (*half_pred_expressions)[i];
545                 snapshot_free(tmp);
546         }
547 }
548
549 /* Amend predicates that contain no predicate expressions. Currenlty only amend with NULLITY predicates */
550 bool FuncNode::amend_predicate_expr(Predicate * curr_pred, FuncInst * next_inst, ModelAction * next_act)
551 {
552         ModelVector<Predicate *> * children = curr_pred->get_children();
553
554         Predicate * unset_pred = NULL;
555         for (uint i = 0;i < children->size();i++) {
556                 Predicate * child = (*children)[i];
557                 if (child->get_func_inst() == next_inst) {
558                         unset_pred = child;
559                         break;
560                 }
561         }
562
563         uint64_t read_val = next_act->get_reads_from_value();
564
565         // only generate NULLITY predicate when it is actually NULL.
566         if ( !next_inst->is_single_location() && (void*)read_val == NULL ) {
567                 Predicate * new_pred = new Predicate(next_inst);
568
569                 curr_pred->add_child(new_pred);
570                 new_pred->set_parent(curr_pred);
571
572                 unset_pred->add_predicate_expr(NULLITY, NULL, false);
573                 new_pred->add_predicate_expr(NULLITY, NULL, true);
574
575                 return true;
576         }
577
578         return false;
579 }
580
581 void FuncNode::add_to_val_loc_map(uint64_t val, void * loc)
582 {
583         loc_set_t * locations = val_loc_map->get(val);
584
585         if (locations == NULL) {
586                 locations = new loc_set_t();
587                 val_loc_map->put(val, locations);
588         }
589
590         update_loc_may_equal_map(loc, locations);
591         locations->add(loc);
592         // values_may_read_from->add(val);
593 }
594
595 void FuncNode::add_to_val_loc_map(value_set_t * values, void * loc)
596 {
597         if (values == NULL)
598                 return;
599
600         value_set_iter * it = values->iterator();
601         while (it->hasNext()) {
602                 uint64_t val = it->next();
603                 add_to_val_loc_map(val, loc);
604         }
605
606         delete it;
607 }
608
609 void FuncNode::update_loc_may_equal_map(void * new_loc, loc_set_t * old_locations)
610 {
611         if ( old_locations->contains(new_loc) )
612                 return;
613
614         loc_set_t * neighbors = loc_may_equal_map->get(new_loc);
615
616         if (neighbors == NULL) {
617                 neighbors = new loc_set_t();
618                 loc_may_equal_map->put(new_loc, neighbors);
619         }
620
621         loc_set_iter * loc_it = old_locations->iterator();
622         while (loc_it->hasNext()) {
623                 // new_loc: { old_locations, ... }
624                 void * member = loc_it->next();
625                 neighbors->add(member);
626
627                 // for each i in old_locations, i : { new_loc, ... }
628                 loc_set_t * _neighbors = loc_may_equal_map->get(member);
629                 if (_neighbors == NULL) {
630                         _neighbors = new loc_set_t();
631                         loc_may_equal_map->put(member, _neighbors);
632                 }
633                 _neighbors->add(new_loc);
634         }
635
636         delete loc_it;
637 }
638
639 void FuncNode::set_predicate_tree_position(thread_id_t tid, Predicate * pred)
640 {
641         int thread_id = id_to_int(tid);
642         ModelVector<Predicate *> * stack = thrd_predicate_tree_position[thread_id];
643         (*stack)[stack->size() - 1] = pred;
644 }
645
646 /* @return The position of a thread in a predicate tree */
647 Predicate * FuncNode::get_predicate_tree_position(thread_id_t tid)
648 {
649         int thread_id = id_to_int(tid);
650         return thrd_predicate_tree_position[thread_id]->back();
651 }
652
653 void FuncNode::add_predicate_to_trace(thread_id_t tid, Predicate * pred)
654 {
655         int thread_id = id_to_int(tid);
656         thrd_predicate_trace[thread_id]->back()->push_back(pred);
657 }
658
659 /* Make sure elements of thrd_inst_act_map are initialized properly when threads enter functions */
660 void FuncNode::init_inst_act_map(thread_id_t tid)
661 {
662         int thread_id = id_to_int(tid);
663         SnapVector<inst_act_map_t *> * thrd_inst_act_map = history->getThrdInstActMap(func_id);
664         uint old_size = thrd_inst_act_map->size();
665
666         if (old_size <= (uint) thread_id) {
667                 uint new_size = thread_id + 1;
668                 thrd_inst_act_map->resize(new_size);
669
670                 for (uint i = old_size;i < new_size;i++)
671                         (*thrd_inst_act_map)[i] = new inst_act_map_t(128);
672         }
673 }
674
675 /* Reset elements of thrd_inst_act_map when threads exit functions */
676 void FuncNode::reset_inst_act_map(thread_id_t tid)
677 {
678         int thread_id = id_to_int(tid);
679         SnapVector<inst_act_map_t *> * thrd_inst_act_map = history->getThrdInstActMap(func_id);
680
681         inst_act_map_t * map = (*thrd_inst_act_map)[thread_id];
682         map->reset();
683 }
684
685 void FuncNode::update_inst_act_map(thread_id_t tid, ModelAction * read_act)
686 {
687         int thread_id = id_to_int(tid);
688         SnapVector<inst_act_map_t *> * thrd_inst_act_map = history->getThrdInstActMap(func_id);
689
690         inst_act_map_t * map = (*thrd_inst_act_map)[thread_id];
691         FuncInst * read_inst = get_inst(read_act);
692         map->put(read_inst, read_act);
693 }
694
695 inst_act_map_t * FuncNode::get_inst_act_map(thread_id_t tid)
696 {
697         int thread_id = id_to_int(tid);
698         SnapVector<inst_act_map_t *> * thrd_inst_act_map = history->getThrdInstActMap(func_id);
699
700         return (*thrd_inst_act_map)[thread_id];
701 }
702
703 void FuncNode::init_marker(thread_id_t tid)
704 {
705         marker++;
706
707         int thread_id = id_to_int(tid);
708         int old_size = thrd_markers.size();
709
710         if (old_size < thread_id + 1) {
711                 thrd_markers.resize(thread_id + 1);
712
713                 for (int i = old_size; i < thread_id + 1; i++) {
714                         thrd_markers[i] = new ModelVector<uint32_t>();
715                         thrd_recursion_depth.push_back(-1);
716                 }
717         }
718
719         thrd_markers[thread_id]->push_back(marker);
720         thrd_recursion_depth[thread_id]++;
721 }
722
723 /* Make sure elements of maps are initialized properly when threads enter functions */
724 void FuncNode::init_local_maps(thread_id_t tid)
725 {
726         int thread_id = id_to_int(tid);
727         int old_size = thrd_loc_inst_map.size();
728
729         if (old_size < thread_id + 1) {
730                 int new_size = thread_id + 1;
731
732                 thrd_loc_inst_map.resize(new_size);
733                 thrd_inst_id_map.resize(new_size);
734                 thrd_inst_pred_map.resize(new_size);
735
736                 for (int i = old_size; i < new_size; i++) {
737                         thrd_loc_inst_map[i] = new loc_inst_map_t(128);
738                         thrd_inst_id_map[i] = new inst_id_map_t(128);
739                         thrd_inst_pred_map[i] = new inst_pred_map_t(128);
740                 }
741         }
742 }
743
744 /* Reset elements of maps when threads exit functions */
745 void FuncNode::reset_local_maps(thread_id_t tid)
746 {
747         int thread_id = id_to_int(tid);
748         thrd_loc_inst_map[thread_id]->reset();
749         thrd_inst_id_map[thread_id]->reset();
750         thrd_inst_pred_map[thread_id]->reset();
751 }
752
753 void FuncNode::init_predicate_tree_data_structure(thread_id_t tid)
754 {
755         int thread_id = id_to_int(tid);
756         int old_size = thrd_predicate_tree_position.size();
757
758         if (old_size < thread_id + 1) {
759                 thrd_predicate_tree_position.resize(thread_id + 1);
760                 thrd_predicate_trace.resize(thread_id + 1);
761
762                 for (int i = old_size; i < thread_id + 1; i++) {
763                         thrd_predicate_tree_position[i] = new ModelVector<Predicate *>();
764                         thrd_predicate_trace[i] = new ModelVector<predicate_trace_t *>();
765                 }
766         }
767
768         thrd_predicate_tree_position[thread_id]->push_back(predicate_tree_entry);
769         thrd_predicate_trace[thread_id]->push_back(new predicate_trace_t());
770 }
771
772 void FuncNode::reset_predicate_tree_data_structure(thread_id_t tid)
773 {
774         int thread_id = id_to_int(tid);
775         thrd_predicate_tree_position[thread_id]->pop_back();
776
777         // Free memories allocated in init_predicate_tree_data_structure
778         predicate_trace_t * trace = thrd_predicate_trace[thread_id]->back();
779         delete trace;
780         thrd_predicate_trace[thread_id]->pop_back();
781 }
782
783 /* Add FuncNodes that this node may follow */
784 void FuncNode::add_out_edge(FuncNode * other)
785 {
786         if ( !edge_table.contains(other) ) {
787                 edge_table.put(other, OUT_EDGE);
788                 out_edges.push_back(other);
789                 return;
790         }
791
792         edge_type_t edge = edge_table.get(other);
793         if (edge == IN_EDGE) {
794                 edge_table.put(other, BI_EDGE);
795                 out_edges.push_back(other);
796         }
797 }
798
799 /* Compute the distance between this FuncNode and the target node.
800  * Return -1 if the target node is unreachable or the actual distance
801  * is greater than max_step.
802  */
803 int FuncNode::compute_distance(FuncNode * target, int max_step)
804 {
805         if (target == NULL)
806                 return -1;
807         else if (target == this)
808                 return 0;
809
810         SnapList<FuncNode *> queue;
811         HashTable<FuncNode *, int, uintptr_t, 0> distances(128);
812
813         queue.push_back(this);
814         distances.put(this, 0);
815
816         while (!queue.empty()) {
817                 FuncNode * curr = queue.front();
818                 queue.pop_front();
819                 int dist = distances.get(curr);
820
821                 if (max_step <= dist)
822                         return -1;
823
824                 ModelList<FuncNode *> * outEdges = curr->get_out_edges();
825                 mllnode<FuncNode *> * it;
826                 for (it = outEdges->begin();it != NULL;it = it->getNext()) {
827                         FuncNode * out_node = it->getVal();
828
829                         /* This node has not been visited before */
830                         if ( !distances.contains(out_node) ) {
831                                 if (out_node == target)
832                                         return dist + 1;
833
834                                 queue.push_back(out_node);
835                                 distances.put(out_node, dist + 1);
836                         }
837                 }
838         }
839
840         /* Target node is unreachable */
841         return -1;
842 }
843
844 void FuncNode::update_predicate_tree_weight(thread_id_t tid)
845 {
846         predicate_trace_t * trace = thrd_predicate_trace[id_to_int(tid)]->back();
847
848         // Update predicate weights based on prediate trace
849         for (mllnode<Predicate *> * rit = trace->end(); rit != NULL; rit = rit->getPrev()) {
850                 Predicate * node = rit->getVal();
851                 ModelVector<Predicate *> * children = node->get_children();
852
853                 if (children->size() == 0) {
854                         double weight = 100.0 / sqrt(node->get_expl_count() + node->get_fail_count() + 1);
855                         node->set_weight(weight);
856                 } else {
857                         double weight_sum = 0.0;
858                         for (uint i = 0;i < children->size();i++) {
859                                 Predicate * child = (*children)[i];
860                                 double weight = child->get_weight();
861                                 weight_sum += weight;
862                         }
863
864                         double average_weight = (double) weight_sum / (double) children->size();
865                         double weight = average_weight * pow(0.9, node->get_depth());
866                         node->set_weight(weight);
867                 }
868         }
869 }
870
871 void FuncNode::print_predicate_tree()
872 {
873         model_print("digraph function_%s {\n", func_name);
874         predicate_tree_entry->print_pred_subtree();
875         predicate_tree_exit->print_predicate();
876         model_print("}\n");     // end of graph
877 }