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