Add failed predicates to predicate trace; remove unused codes
[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
224         if (act->is_read()) {
225
226                 /* If func_inst may only read_from a single location, then:
227                  *
228                  * The first time an action reads from some location,
229                  * import all the values that have been written to this
230                  * location from ModelHistory and notify ModelHistory
231                  * that this FuncNode may read from this location.
232                  */
233                 if (!read_locations->contains(loc) && func_inst->is_single_location()) {
234                         read_locations->add(loc);
235                         value_set_t * write_values = write_history->get(loc);
236                         add_to_val_loc_map(write_values, loc);
237                         history->update_loc_rd_func_nodes_map(loc, this);
238                 }
239         }
240
241 //      update_inst_tree(&inst_list); TODO
242
243         update_predicate_tree(act);
244
245 //      print_predicate_tree();
246 }
247
248 /**
249  * @brief Link FuncInsts in inst_list  - add one FuncInst to another's predecessors and successors
250  * @param inst_list A list of FuncInsts
251  */
252 void FuncNode::update_inst_tree(func_inst_list_t * inst_list)
253 {
254         if (inst_list == NULL)
255                 return;
256         else if (inst_list->size() == 0)
257                 return;
258
259         /* start linking */
260         sllnode<FuncInst *>* it = inst_list->begin();
261         sllnode<FuncInst *>* prev;
262
263         /* add the first instruction to the list of entry insts */
264         FuncInst * entry_inst = it->getVal();
265         add_entry_inst(entry_inst);
266
267         it = it->getNext();
268         while (it != NULL) {
269                 prev = it->getPrev();
270
271                 FuncInst * prev_inst = prev->getVal();
272                 FuncInst * curr_inst = it->getVal();
273
274                 prev_inst->add_succ(curr_inst);
275                 curr_inst->add_pred(prev_inst);
276
277                 it = it->getNext();
278         }
279 }
280
281 void FuncNode::update_predicate_tree(ModelAction * next_act)
282 {
283         thread_id_t tid = next_act->get_tid();
284         int thread_id = id_to_int(tid);
285         uint32_t this_marker = thrd_markers[thread_id]->back();
286         int recursion_depth = thrd_recursion_depth[thread_id];
287
288         loc_inst_map_t * loc_inst_map = thrd_loc_inst_maps[thread_id]->back();
289         inst_pred_map_t * inst_pred_map = thrd_inst_pred_maps[thread_id]->back();
290         inst_id_map_t * inst_id_map = thrd_inst_id_maps[thread_id]->back();
291
292         Predicate * curr_pred = get_predicate_tree_position(tid);
293         NewFuzzer * fuzzer = (NewFuzzer *)model->get_execution()->getFuzzer();
294         Predicate * selected_branch = fuzzer->get_selected_child_branch(tid);
295
296         bool amended;
297         while (true) {
298                 FuncInst * next_inst = get_inst(next_act);
299
300                 Predicate * unset_predicate = NULL;
301                 bool branch_found = follow_branch(&curr_pred, next_inst, next_act, &unset_predicate);
302
303                 // A branch with unset predicate expression is detected
304                 if (!branch_found && unset_predicate != NULL) {
305                         amended = amend_predicate_expr(curr_pred, next_inst, next_act);
306                         if (amended)
307                                 continue;
308                         else {
309                                 curr_pred = unset_predicate;
310                                 branch_found = true;
311                         }
312                 }
313
314                 // Detect loops
315                 if (!branch_found && inst_id_map->contains(next_inst)) {
316                         FuncInst * curr_inst = curr_pred->get_func_inst();
317                         uint32_t curr_id = inst_id_map->get(curr_inst);
318                         uint32_t next_id = inst_id_map->get(next_inst);
319
320                         if (curr_id >= next_id) {
321                                 Predicate * old_pred = inst_pred_map->get(next_inst);
322                                 Predicate * back_pred = old_pred->get_parent();
323
324                                 // Add to the set of backedges
325                                 curr_pred->add_backedge(back_pred);
326                                 curr_pred = back_pred;
327
328                                 continue;
329                         }
330                 }
331
332                 // Generate new branches
333                 if (!branch_found) {
334                         SnapVector<struct half_pred_expr *> half_pred_expressions;
335                         infer_predicates(next_inst, next_act, &half_pred_expressions);
336                         generate_predicates(curr_pred, next_inst, &half_pred_expressions);
337                         continue;
338                 }
339
340                 if (next_act->is_write()) {
341                         curr_pred->set_write(true);
342                 }
343
344                 if (next_act->is_read()) {
345                         /* Only need to store the locations of read actions */
346                         loc_inst_map->put(next_act->get_location(), next_inst);
347                 }
348
349                 inst_pred_map->put(next_inst, curr_pred);
350                 set_predicate_tree_position(tid, curr_pred);
351
352                 if (!inst_id_map->contains(next_inst))
353                         inst_id_map->put(next_inst, inst_counter++);
354
355                 curr_pred->incr_expl_count();
356                 add_predicate_to_trace(tid, curr_pred);
357                 if (next_act->is_read())
358                         next_inst->set_associated_read(tid, recursion_depth, this_marker, next_act->get_reads_from_value());
359
360                 break;
361         }
362
363         // A check
364         if (next_act->is_read()) {
365                 if (selected_branch != NULL && !amended)
366                         ASSERT(selected_branch == curr_pred);
367         }
368 }
369
370 /* Given curr_pred and next_inst, find the branch following curr_pred that
371  * contains next_inst and the correct predicate.
372  * @return true if branch found, false otherwise.
373  */
374 bool FuncNode::follow_branch(Predicate ** curr_pred, FuncInst * next_inst,
375                                                                                                                  ModelAction * next_act, Predicate ** unset_predicate)
376 {
377         /* Check if a branch with func_inst and corresponding predicate exists */
378         bool branch_found = false;
379         thread_id_t tid = next_act->get_tid();
380         int thread_id = id_to_int(tid);
381         uint32_t this_marker = thrd_markers[thread_id]->back();
382         int recursion_depth = thrd_recursion_depth[thread_id];
383
384         ModelVector<Predicate *> * branches = (*curr_pred)->get_children();
385         for (uint i = 0;i < branches->size();i++) {
386                 Predicate * branch = (*branches)[i];
387                 if (branch->get_func_inst() != next_inst)
388                         continue;
389
390                 /* Check against predicate expressions */
391                 bool predicate_correct = true;
392                 PredExprSet * pred_expressions = branch->get_pred_expressions();
393
394                 /* Only read and rmw actions my have unset predicate expressions */
395                 if (pred_expressions->getSize() == 0) {
396                         predicate_correct = false;
397
398                         if (*unset_predicate == NULL)
399                                 *unset_predicate = branch;
400                         else
401                                 ASSERT(false);
402
403                         continue;
404                 }
405
406                 PredExprSetIter * pred_expr_it = pred_expressions->iterator();
407                 while (pred_expr_it->hasNext()) {
408                         pred_expr * pred_expression = pred_expr_it->next();
409                         uint64_t last_read, next_read;
410                         bool equality;
411
412                         switch(pred_expression->token) {
413                         case NOPREDICATE:
414                                 predicate_correct = true;
415                                 break;
416                         case EQUALITY:
417                                 FuncInst * to_be_compared;
418                                 to_be_compared = pred_expression->func_inst;
419
420                                 last_read = to_be_compared->get_associated_read(tid, recursion_depth, this_marker);
421                                 ASSERT(last_read != VALUE_NONE);
422
423                                 next_read = next_act->get_reads_from_value();
424                                 equality = (last_read == next_read);
425                                 if (equality != pred_expression->value)
426                                         predicate_correct = false;
427
428                                 break;
429                         case NULLITY:
430                                 next_read = next_act->get_reads_from_value();
431                                 // TODO: implement likely to be null
432                                 equality = ( (void*) (next_read & 0xffffffff) == NULL);
433                                 if (equality != pred_expression->value)
434                                         predicate_correct = false;
435                                 break;
436                         default:
437                                 predicate_correct = false;
438                                 model_print("unkown predicate token\n");
439                                 break;
440                         }
441                 }
442
443                 delete pred_expr_it;
444
445                 if (predicate_correct) {
446                         *curr_pred = branch;
447                         branch_found = true;
448                         break;
449                 }
450         }
451
452         return branch_found;
453 }
454
455 /* Infer predicate expressions, which are generated in FuncNode::generate_predicates */
456 void FuncNode::infer_predicates(FuncInst * next_inst, ModelAction * next_act,
457                                                                                                                                 SnapVector<struct half_pred_expr *> * half_pred_expressions)
458 {
459         void * loc = next_act->get_location();
460         int thread_id = id_to_int(next_act->get_tid());
461         loc_inst_map_t * loc_inst_map = thrd_loc_inst_maps[thread_id]->back();
462
463         if (next_inst->is_read()) {
464                 /* read + rmw */
465                 if ( loc_inst_map->contains(loc) ) {
466                         FuncInst * last_inst = loc_inst_map->get(loc);
467                         struct half_pred_expr * expression = new half_pred_expr(EQUALITY, last_inst);
468                         half_pred_expressions->push_back(expression);
469                 } else if ( next_inst->is_single_location() ) {
470                         loc_set_t * loc_may_equal = loc_may_equal_map->get(loc);
471
472                         if (loc_may_equal != NULL) {
473                                 loc_set_iter * loc_it = loc_may_equal->iterator();
474                                 while (loc_it->hasNext()) {
475                                         void * neighbor = loc_it->next();
476                                         if (loc_inst_map->contains(neighbor)) {
477                                                 FuncInst * last_inst = loc_inst_map->get(neighbor);
478
479                                                 struct half_pred_expr * expression = new half_pred_expr(EQUALITY, last_inst);
480                                                 half_pred_expressions->push_back(expression);
481                                         }
482                                 }
483
484                                 delete loc_it;
485                         }
486                 } else {
487                         // next_inst is not single location
488                         uint64_t read_val = next_act->get_reads_from_value();
489
490                         // only infer NULLITY predicate when it is actually NULL.
491                         if ( (void*)read_val == NULL) {
492                                 struct half_pred_expr * expression = new half_pred_expr(NULLITY, NULL);
493                                 half_pred_expressions->push_back(expression);
494                         }
495                 }
496         } else {
497                 /* Pure writes */
498                 // TODO: do anything here?
499         }
500 }
501
502 /* Able to generate complex predicates when there are multiple predciate expressions */
503 void FuncNode::generate_predicates(Predicate * curr_pred, FuncInst * next_inst,
504                                                                                                                                          SnapVector<struct half_pred_expr *> * half_pred_expressions)
505 {
506         if (half_pred_expressions->size() == 0) {
507                 Predicate * new_pred = new Predicate(next_inst);
508                 curr_pred->add_child(new_pred);
509                 new_pred->set_parent(curr_pred);
510
511                 /* entry predicates and predicates containing pure write actions
512                  * have no predicate expressions */
513                 if ( curr_pred->is_entry_predicate() )
514                         new_pred->add_predicate_expr(NOPREDICATE, NULL, true);
515                 else if (next_inst->is_write()) {
516                         /* next_inst->is_write() <==> pure writes */
517                         new_pred->add_predicate_expr(NOPREDICATE, NULL, true);
518                 }
519
520                 return;
521         }
522
523         SnapVector<Predicate *> predicates;
524
525         struct half_pred_expr * half_expr = (*half_pred_expressions)[0];
526         predicates.push_back(new Predicate(next_inst));
527         predicates.push_back(new Predicate(next_inst));
528
529         predicates[0]->add_predicate_expr(half_expr->token, half_expr->func_inst, true);
530         predicates[1]->add_predicate_expr(half_expr->token, half_expr->func_inst, false);
531
532         for (uint i = 1;i < half_pred_expressions->size();i++) {
533                 half_expr = (*half_pred_expressions)[i];
534
535                 uint old_size = predicates.size();
536                 for (uint j = 0;j < old_size;j++) {
537                         Predicate * pred = predicates[j];
538                         Predicate * new_pred = new Predicate(next_inst);
539                         new_pred->copy_predicate_expr(pred);
540
541                         pred->add_predicate_expr(half_expr->token, half_expr->func_inst, true);
542                         new_pred->add_predicate_expr(half_expr->token, half_expr->func_inst, false);
543
544                         predicates.push_back(new_pred);
545                 }
546         }
547
548         for (uint i = 0;i < predicates.size();i++) {
549                 Predicate * pred= predicates[i];
550                 curr_pred->add_child(pred);
551                 pred->set_parent(curr_pred);
552         }
553
554         /* Free memories allocated by infer_predicate */
555         for (uint i = 0;i < half_pred_expressions->size();i++) {
556                 struct half_pred_expr * tmp = (*half_pred_expressions)[i];
557                 snapshot_free(tmp);
558         }
559 }
560
561 /* Amend predicates that contain no predicate expressions. Currenlty only amend with NULLITY predicates */
562 bool FuncNode::amend_predicate_expr(Predicate * curr_pred, FuncInst * next_inst, ModelAction * next_act)
563 {
564         ModelVector<Predicate *> * children = curr_pred->get_children();
565
566         Predicate * unset_pred = NULL;
567         for (uint i = 0;i < children->size();i++) {
568                 Predicate * child = (*children)[i];
569                 if (child->get_func_inst() == next_inst) {
570                         unset_pred = child;
571                         break;
572                 }
573         }
574
575         uint64_t read_val = next_act->get_reads_from_value();
576
577         // only generate NULLITY predicate when it is actually NULL.
578         if ( !next_inst->is_single_location() && (void*)read_val == NULL ) {
579                 Predicate * new_pred = new Predicate(next_inst);
580
581                 curr_pred->add_child(new_pred);
582                 new_pred->set_parent(curr_pred);
583
584                 unset_pred->add_predicate_expr(NULLITY, NULL, false);
585                 new_pred->add_predicate_expr(NULLITY, NULL, true);
586
587                 return true;
588         }
589
590         return false;
591 }
592
593 void FuncNode::add_to_val_loc_map(uint64_t val, void * loc)
594 {
595         loc_set_t * locations = val_loc_map->get(val);
596
597         if (locations == NULL) {
598                 locations = new loc_set_t();
599                 val_loc_map->put(val, locations);
600         }
601
602         update_loc_may_equal_map(loc, locations);
603         locations->add(loc);
604         // values_may_read_from->add(val);
605 }
606
607 void FuncNode::add_to_val_loc_map(value_set_t * values, void * loc)
608 {
609         if (values == NULL)
610                 return;
611
612         value_set_iter * it = values->iterator();
613         while (it->hasNext()) {
614                 uint64_t val = it->next();
615                 add_to_val_loc_map(val, loc);
616         }
617
618         delete it;
619 }
620
621 void FuncNode::update_loc_may_equal_map(void * new_loc, loc_set_t * old_locations)
622 {
623         if ( old_locations->contains(new_loc) )
624                 return;
625
626         loc_set_t * neighbors = loc_may_equal_map->get(new_loc);
627
628         if (neighbors == NULL) {
629                 neighbors = new loc_set_t();
630                 loc_may_equal_map->put(new_loc, neighbors);
631         }
632
633         loc_set_iter * loc_it = old_locations->iterator();
634         while (loc_it->hasNext()) {
635                 // new_loc: { old_locations, ... }
636                 void * member = loc_it->next();
637                 neighbors->add(member);
638
639                 // for each i in old_locations, i : { new_loc, ... }
640                 loc_set_t * _neighbors = loc_may_equal_map->get(member);
641                 if (_neighbors == NULL) {
642                         _neighbors = new loc_set_t();
643                         loc_may_equal_map->put(member, _neighbors);
644                 }
645                 _neighbors->add(new_loc);
646         }
647
648         delete loc_it;
649 }
650
651 void FuncNode::set_predicate_tree_position(thread_id_t tid, Predicate * pred)
652 {
653         int thread_id = id_to_int(tid);
654         ModelVector<Predicate *> * stack = thrd_predicate_tree_position[thread_id];
655         (*stack)[stack->size() - 1] = pred;
656 }
657
658 /* @return The position of a thread in a predicate tree */
659 Predicate * FuncNode::get_predicate_tree_position(thread_id_t tid)
660 {
661         int thread_id = id_to_int(tid);
662         return thrd_predicate_tree_position[thread_id]->back();
663 }
664
665 void FuncNode::add_predicate_to_trace(thread_id_t tid, Predicate * pred)
666 {
667         int thread_id = id_to_int(tid);
668         thrd_predicate_trace[thread_id]->back()->push_back(pred);
669 }
670
671 void FuncNode::init_marker(thread_id_t tid)
672 {
673         marker++;
674
675         int thread_id = id_to_int(tid);
676         int old_size = thrd_markers.size();
677
678         if (old_size < thread_id + 1) {
679                 thrd_markers.resize(thread_id + 1);
680
681                 for (int i = old_size; i < thread_id + 1; i++) {
682                         thrd_markers[i] = new ModelVector<uint32_t>();
683                         thrd_recursion_depth.push_back(-1);
684                 }
685         }
686
687         thrd_markers[thread_id]->push_back(marker);
688         thrd_recursion_depth[thread_id]++;
689 }
690
691 uint32_t FuncNode::get_marker(thread_id_t tid)
692 {
693         int thread_id = id_to_int(tid);
694         return thrd_markers[thread_id]->back();
695 }
696
697 int FuncNode::get_recursion_depth(thread_id_t tid)
698 {
699         return thrd_recursion_depth[id_to_int(tid)];
700 }
701
702 /* Make sure elements of maps are initialized properly when threads enter functions */
703 void FuncNode::init_local_maps(thread_id_t tid)
704 {
705         int thread_id = id_to_int(tid);
706         int old_size = thrd_loc_inst_maps.size();
707
708         if (old_size < thread_id + 1) {
709                 int new_size = thread_id + 1;
710
711                 thrd_loc_inst_maps.resize(new_size);
712                 thrd_inst_id_maps.resize(new_size);
713                 thrd_inst_pred_maps.resize(new_size);
714
715                 for (int i = old_size; i < new_size; i++) {
716                         thrd_loc_inst_maps[i] = new ModelVector<loc_inst_map_t *>;
717                         thrd_inst_id_maps[i] = new ModelVector<inst_id_map_t *>;
718                         thrd_inst_pred_maps[i] = new ModelVector<inst_pred_map_t *>;
719                 }
720         }
721
722         ModelVector<loc_inst_map_t *> * map = thrd_loc_inst_maps[thread_id];
723         int index = thrd_recursion_depth[thread_id];
724
725         // If there are recursive calls, push more hashtables into the vector.
726         if (map->size() < (uint) index + 1) {
727                 thrd_loc_inst_maps[thread_id]->push_back(new loc_inst_map_t(64));
728                 thrd_inst_id_maps[thread_id]->push_back(new inst_id_map_t(64));
729                 thrd_inst_pred_maps[thread_id]->push_back(new inst_pred_map_t(64));
730         }
731
732         ASSERT(map->size() == (uint) index + 1);
733 }
734
735 /* Reset elements of maps when threads exit functions */
736 void FuncNode::reset_local_maps(thread_id_t tid)
737 {
738         int thread_id = id_to_int(tid);
739         int index = thrd_recursion_depth[thread_id];
740
741         // When recursive call ends, keep only one hashtable in the vector
742         if (index > 0) {
743                 delete thrd_loc_inst_maps[thread_id]->back();
744                 delete thrd_inst_id_maps[thread_id]->back();
745                 delete thrd_inst_pred_maps[thread_id]->back();
746
747                 thrd_loc_inst_maps[thread_id]->pop_back();
748                 thrd_inst_id_maps[thread_id]->pop_back();
749                 thrd_inst_pred_maps[thread_id]->pop_back();
750         } else {
751                 thrd_loc_inst_maps[thread_id]->back()->reset();
752                 thrd_inst_id_maps[thread_id]->back()->reset();
753                 thrd_inst_pred_maps[thread_id]->back()->reset();
754         }
755 }
756
757 void FuncNode::init_predicate_tree_data_structure(thread_id_t tid)
758 {
759         int thread_id = id_to_int(tid);
760         int old_size = thrd_predicate_tree_position.size();
761
762         if (old_size < thread_id + 1) {
763                 thrd_predicate_tree_position.resize(thread_id + 1);
764                 thrd_predicate_trace.resize(thread_id + 1);
765
766                 for (int i = old_size; i < thread_id + 1; i++) {
767                         thrd_predicate_tree_position[i] = new ModelVector<Predicate *>();
768                         thrd_predicate_trace[i] = new ModelVector<predicate_trace_t *>();
769                 }
770         }
771
772         thrd_predicate_tree_position[thread_id]->push_back(predicate_tree_entry);
773         thrd_predicate_trace[thread_id]->push_back(new predicate_trace_t());
774 }
775
776 void FuncNode::reset_predicate_tree_data_structure(thread_id_t tid)
777 {
778         int thread_id = id_to_int(tid);
779         thrd_predicate_tree_position[thread_id]->pop_back();
780
781         // Free memories allocated in init_predicate_tree_data_structure
782         predicate_trace_t * trace = thrd_predicate_trace[thread_id]->back();
783         delete trace;
784         thrd_predicate_trace[thread_id]->pop_back();
785 }
786
787 /* Add FuncNodes that this node may follow */
788 void FuncNode::add_out_edge(FuncNode * other)
789 {
790         if ( !edge_table.contains(other) ) {
791                 edge_table.put(other, OUT_EDGE);
792                 out_edges.push_back(other);
793                 return;
794         }
795
796         edge_type_t edge = edge_table.get(other);
797         if (edge == IN_EDGE) {
798                 edge_table.put(other, BI_EDGE);
799                 out_edges.push_back(other);
800         }
801 }
802
803 /* Compute the distance between this FuncNode and the target node.
804  * Return -1 if the target node is unreachable or the actual distance
805  * is greater than max_step.
806  */
807 int FuncNode::compute_distance(FuncNode * target, int max_step)
808 {
809         if (target == NULL)
810                 return -1;
811         else if (target == this)
812                 return 0;
813
814         SnapList<FuncNode *> queue;
815         HashTable<FuncNode *, int, uintptr_t, 0> distances(128);
816
817         queue.push_back(this);
818         distances.put(this, 0);
819
820         while (!queue.empty()) {
821                 FuncNode * curr = queue.front();
822                 queue.pop_front();
823                 int dist = distances.get(curr);
824
825                 if (max_step <= dist)
826                         return -1;
827
828                 ModelList<FuncNode *> * outEdges = curr->get_out_edges();
829                 mllnode<FuncNode *> * it;
830                 for (it = outEdges->begin();it != NULL;it = it->getNext()) {
831                         FuncNode * out_node = it->getVal();
832
833                         /* This node has not been visited before */
834                         if ( !distances.contains(out_node) ) {
835                                 if (out_node == target)
836                                         return dist + 1;
837
838                                 queue.push_back(out_node);
839                                 distances.put(out_node, dist + 1);
840                         }
841                 }
842         }
843
844         /* Target node is unreachable */
845         return -1;
846 }
847
848 void FuncNode::update_predicate_tree_weight(thread_id_t tid)
849 {
850         predicate_trace_t * trace = thrd_predicate_trace[id_to_int(tid)]->back();
851
852         // Update predicate weights based on prediate trace
853         for (mllnode<Predicate *> * rit = trace->end(); rit != NULL; rit = rit->getPrev()) {
854                 Predicate * node = rit->getVal();
855                 ModelVector<Predicate *> * children = node->get_children();
856
857                 if (children->size() == 0) {
858                         double weight = 100.0 / sqrt(node->get_expl_count() + node->get_fail_count() + 1);
859                         node->set_weight(weight);
860                 } else {
861                         double weight_sum = 0.0;
862                         for (uint i = 0;i < children->size();i++) {
863                                 Predicate * child = (*children)[i];
864                                 double weight = child->get_weight();
865                                 weight_sum += weight;
866                         }
867
868                         double average_weight = (double) weight_sum / (double) children->size();
869                         double weight = average_weight * pow(0.9, node->get_depth());
870                         node->set_weight(weight);
871                 }
872         }
873 }
874
875 void FuncNode::print_predicate_tree()
876 {
877         model_print("digraph function_%s {\n", func_name);
878         predicate_tree_entry->print_pred_subtree();
879         predicate_tree_exit->print_predicate();
880         model_print("}\n");     // end of graph
881 }