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