Handle predicate weight updates
[c11tester.git] / funcnode.cc
1 #include "action.h"
2 #include "history.h"
3 #include "funcnode.h"
4 #include "funcinst.h"
5 #include "predicate.h"
6 #include "concretepredicate.h"
7
8 #include "model.h"
9 #include <cmath>
10
11 FuncNode::FuncNode(ModelHistory * history) :
12         history(history),
13         exit_count(0),
14         inst_counter(1),
15         marker(1),
16         thrd_marker(),
17         func_inst_map(),
18         inst_list(),
19         entry_insts(),
20         thrd_inst_pred_map(),
21         thrd_inst_id_map(),
22         thrd_loc_inst_map(),
23         thrd_predicate_tree_position(),
24         thrd_predicate_trace(),
25         predicate_leaves(),
26         leaves_tmp_storage(),
27         failed_predicates(),
28         edge_table(32),
29         out_edges()
30 {
31         predicate_tree_entry = new Predicate(NULL, true);
32         predicate_tree_entry->add_predicate_expr(NOPREDICATE, NULL, true);
33
34         predicate_tree_exit = new Predicate(NULL, false, true);
35         predicate_tree_exit->set_depth(MAX_DEPTH);
36
37         /* Snapshot data structures below */
38         read_locations = new loc_set_t();
39         write_locations = new loc_set_t();
40         val_loc_map = new HashTable<uint64_t, loc_set_t *, uint64_t, 0, snapshot_malloc, snapshot_calloc, snapshot_free, int64_hash>();
41         loc_may_equal_map = new HashTable<void *, loc_set_t *, uintptr_t, 0>();
42
43         //values_may_read_from = new value_set_t();
44 }
45
46 /* Reallocate snapshotted memories when new executions start */
47 void FuncNode::set_new_exec_flag()
48 {
49         read_locations = new loc_set_t();
50         write_locations = new loc_set_t();
51         val_loc_map = new HashTable<uint64_t, loc_set_t *, uint64_t, 0, snapshot_malloc, snapshot_calloc, snapshot_free, int64_hash>();
52         loc_may_equal_map = new HashTable<void *, loc_set_t *, uintptr_t, 0>();
53
54         //values_may_read_from = new value_set_t();
55 }
56
57 /* Check whether FuncInst with the same type, position, and location
58  * as act has been added to func_inst_map or not. If not, add it.
59  */
60 void FuncNode::add_inst(ModelAction *act)
61 {
62         ASSERT(act);
63         const char * position = act->get_position();
64
65         /* THREAD* actions, ATOMIC_LOCK, ATOMIC_TRYLOCK, and ATOMIC_UNLOCK
66          * actions are not tagged with their source line numbers
67          */
68         if (position == NULL)
69                 return;
70
71         FuncInst * func_inst = func_inst_map.get(position);
72
73         /* This position has not been inserted into hashtable before */
74         if (func_inst == NULL) {
75                 func_inst = create_new_inst(act);
76                 func_inst_map.put(position, func_inst);
77                 return;
78         }
79
80         /* Volatile variables that use ++ or -- syntax may result in read and write actions with the same position */
81         if (func_inst->get_type() != act->get_type()) {
82                 FuncInst * collision_inst = func_inst->search_in_collision(act);
83
84                 if (collision_inst == NULL) {
85                         collision_inst = create_new_inst(act);
86                         func_inst->add_to_collision(collision_inst);
87                         return;
88                 } else {
89                         func_inst = collision_inst;
90                 }
91         }
92
93         ASSERT(func_inst->get_type() == act->get_type());
94         int curr_execution_number = model->get_execution_number();
95
96         /* Reset locations when new executions start */
97         if (func_inst->get_execution_number() != curr_execution_number) {
98                 func_inst->set_location(act->get_location());
99                 func_inst->set_execution_number(curr_execution_number);
100         }
101
102         /* Mark the memory location of such inst as not unique */
103         if (func_inst->get_location() != act->get_location())
104                 func_inst->not_single_location();
105 }
106
107 FuncInst * FuncNode::create_new_inst(ModelAction * act)
108 {
109         FuncInst * func_inst = new FuncInst(act, this);
110         int exec_num = model->get_execution_number();
111         func_inst->set_execution_number(exec_num);
112
113         inst_list.push_back(func_inst);
114
115         return func_inst;
116 }
117
118
119 /* Get the FuncInst with the same type, position, and location
120  * as act
121  *
122  * @return FuncInst with the same type, position, and location as act */
123 FuncInst * FuncNode::get_inst(ModelAction *act)
124 {
125         ASSERT(act);
126         const char * position = act->get_position();
127
128         /* THREAD* actions, ATOMIC_LOCK, ATOMIC_TRYLOCK, and ATOMIC_UNLOCK
129          * actions are not tagged with their source line numbers
130          */
131         if (position == NULL)
132                 return NULL;
133
134         FuncInst * inst = func_inst_map.get(position);
135         if (inst == NULL)
136                 return NULL;
137
138         action_type inst_type = inst->get_type();
139         action_type act_type = act->get_type();
140
141         if (inst_type == act_type) {
142                 return inst;
143         }
144         /* RMWRCAS actions are converted to RMW or READ actions */
145         else if (inst_type == ATOMIC_RMWRCAS &&
146                                          (act_type == ATOMIC_RMW || act_type == ATOMIC_READ)) {
147                 return inst;
148         }
149         /* Return the FuncInst in the collision list */
150         else {
151                 return inst->search_in_collision(act);
152         }
153 }
154
155 void FuncNode::add_entry_inst(FuncInst * inst)
156 {
157         if (inst == NULL)
158                 return;
159
160         mllnode<FuncInst *> * it;
161         for (it = entry_insts.begin();it != NULL;it = it->getNext()) {
162                 if (inst == it->getVal())
163                         return;
164         }
165
166         entry_insts.push_back(inst);
167 }
168
169 void FuncNode::function_entry_handler(thread_id_t tid)
170 {
171         set_marker(tid);
172         init_inst_act_map(tid);
173         init_local_maps(tid);
174         init_predicate_tree_data_structure(tid);
175 }
176
177 void FuncNode::function_exit_handler(thread_id_t tid)
178 {
179         exit_count++;
180
181         reset_inst_act_map(tid);
182         reset_local_maps(tid);
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         int this_marker = thrd_marker[thread_id];
288
289         loc_inst_map_t * loc_inst_map = thrd_loc_inst_map[thread_id];
290         inst_pred_map_t * inst_pred_map = thrd_inst_pred_map[thread_id];
291         inst_id_map_t * inst_id_map = thrd_inst_id_map[thread_id];
292
293         Predicate * curr_pred = get_predicate_tree_position(tid);
294         while (true) {
295                 FuncInst * next_inst = get_inst(next_act);
296                 next_inst->set_associated_read(tid, next_act->get_reads_from_value(), this_marker);
297
298                 Predicate * unset_predicate = NULL;
299                 bool branch_found = follow_branch(&curr_pred, next_inst, next_act, &unset_predicate);
300
301                 // A branch with unset predicate expression is detected
302                 if (!branch_found && unset_predicate != NULL) {
303                         bool amended = amend_predicate_expr(curr_pred, next_inst, next_act);
304                         if (amended)
305                                 continue;
306                         else {
307                                 curr_pred = unset_predicate;
308                                 branch_found = true;
309                         }
310                 }
311
312                 // Detect loops
313                 if (!branch_found && inst_id_map->contains(next_inst)) {
314                         FuncInst * curr_inst = curr_pred->get_func_inst();
315                         uint32_t curr_id = inst_id_map->get(curr_inst);
316                         uint32_t next_id = inst_id_map->get(next_inst);
317
318                         if (curr_id >= next_id) {
319                                 Predicate * old_pred = inst_pred_map->get(next_inst);
320                                 Predicate * back_pred = old_pred->get_parent();
321
322                                 // Add to the set of backedges
323                                 curr_pred->add_backedge(back_pred);
324                                 curr_pred = back_pred;
325
326                                 continue;
327                         }
328                 }
329
330                 // Generate new branches
331                 if (!branch_found) {
332                         SnapVector<struct half_pred_expr *> half_pred_expressions;
333                         infer_predicates(next_inst, next_act, &half_pred_expressions);
334                         generate_predicates(curr_pred, next_inst, &half_pred_expressions);
335                         continue;
336                 }
337
338                 if (next_act->is_write())
339                         curr_pred->set_write(true);
340
341                 if (next_act->is_read()) {
342                         /* Only need to store the locations of read actions */
343                         loc_inst_map->put(next_inst->get_location(), next_inst);
344                 }
345
346                 inst_pred_map->put(next_inst, curr_pred);
347                 set_predicate_tree_position(tid, curr_pred);
348
349                 if (!inst_id_map->contains(next_inst))
350                         inst_id_map->put(next_inst, inst_counter++);
351
352                 curr_pred->incr_expl_count();
353                 add_predicate_to_trace(tid, curr_pred);
354                 break;
355         }
356
357 //      leaves_tmp_storage.push_back(curr_pred);
358 }
359
360 /* Given curr_pred and next_inst, find the branch following curr_pred that
361  * contains next_inst and the correct predicate.
362  * @return true if branch found, false otherwise.
363  */
364 bool FuncNode::follow_branch(Predicate ** curr_pred, FuncInst * next_inst,
365                                                                                                                  ModelAction * next_act, Predicate ** unset_predicate)
366 {
367         /* Check if a branch with func_inst and corresponding predicate exists */
368         bool branch_found = false;
369         thread_id_t tid = next_act->get_tid();
370         int this_marker = thrd_marker[id_to_int(tid)];
371
372         ModelVector<Predicate *> * branches = (*curr_pred)->get_children();
373         for (uint i = 0;i < branches->size();i++) {
374                 Predicate * branch = (*branches)[i];
375                 if (branch->get_func_inst() != next_inst)
376                         continue;
377
378                 /* Check against predicate expressions */
379                 bool predicate_correct = true;
380                 PredExprSet * pred_expressions = branch->get_pred_expressions();
381
382                 /* Only read and rmw actions my have unset predicate expressions */
383                 if (pred_expressions->getSize() == 0) {
384                         predicate_correct = false;
385
386                         if (*unset_predicate == NULL)
387                                 *unset_predicate = branch;
388                         else
389                                 ASSERT(false);
390
391                         continue;
392                 }
393
394                 PredExprSetIter * pred_expr_it = pred_expressions->iterator();
395                 while (pred_expr_it->hasNext()) {
396                         pred_expr * pred_expression = pred_expr_it->next();
397                         uint64_t last_read, next_read;
398                         bool equality;
399
400                         switch(pred_expression->token) {
401                         case NOPREDICATE:
402                                 predicate_correct = true;
403                                 break;
404                         case EQUALITY:
405                                 FuncInst * to_be_compared;
406                                 to_be_compared = pred_expression->func_inst;
407
408                                 last_read = to_be_compared->get_associated_read(tid, this_marker);
409                                 ASSERT(last_read != VALUE_NONE);
410
411                                 next_read = next_act->get_reads_from_value();
412                                 equality = (last_read == next_read);
413                                 if (equality != pred_expression->value)
414                                         predicate_correct = false;
415
416                                 break;
417                         case NULLITY:
418                                 next_read = next_act->get_reads_from_value();
419                                 // TODO: implement likely to be null
420                                 equality = ( (void*) (next_read & 0xffffffff) == NULL);
421                                 if (equality != pred_expression->value)
422                                         predicate_correct = false;
423                                 break;
424                         default:
425                                 predicate_correct = false;
426                                 model_print("unkown predicate token\n");
427                                 break;
428                         }
429                 }
430
431                 delete pred_expr_it;
432
433                 if (predicate_correct) {
434                         *curr_pred = branch;
435                         branch_found = true;
436                         break;
437                 }
438         }
439
440         return branch_found;
441 }
442
443 /* Infer predicate expressions, which are generated in FuncNode::generate_predicates */
444 void FuncNode::infer_predicates(FuncInst * next_inst, ModelAction * next_act,
445                                                                                                                                 SnapVector<struct half_pred_expr *> * half_pred_expressions)
446 {
447         void * loc = next_act->get_location();
448         int thread_id = id_to_int(next_act->get_tid());
449         loc_inst_map_t * loc_inst_map = thrd_loc_inst_map[thread_id];
450
451         if (next_inst->is_read()) {
452                 /* read + rmw */
453                 if ( loc_inst_map->contains(loc) ) {
454                         FuncInst * last_inst = loc_inst_map->get(loc);
455                         struct half_pred_expr * expression = new half_pred_expr(EQUALITY, last_inst);
456                         half_pred_expressions->push_back(expression);
457                 } else if ( next_inst->is_single_location() ) {
458                         loc_set_t * loc_may_equal = loc_may_equal_map->get(loc);
459
460                         if (loc_may_equal != NULL) {
461                                 loc_set_iter * loc_it = loc_may_equal->iterator();
462                                 while (loc_it->hasNext()) {
463                                         void * neighbor = loc_it->next();
464                                         if (loc_inst_map->contains(neighbor)) {
465                                                 FuncInst * last_inst = loc_inst_map->get(neighbor);
466
467                                                 struct half_pred_expr * expression = new half_pred_expr(EQUALITY, last_inst);
468                                                 half_pred_expressions->push_back(expression);
469                                         }
470                                 }
471
472                                 delete loc_it;
473                         }
474                 } else {
475                         // next_inst is not single location
476                         uint64_t read_val = next_act->get_reads_from_value();
477
478                         // only infer NULLITY predicate when it is actually NULL.
479                         if ( (void*)read_val == NULL) {
480                                 struct half_pred_expr * expression = new half_pred_expr(NULLITY, NULL);
481                                 half_pred_expressions->push_back(expression);
482                         }
483                 }
484         } else {
485                 /* Pure writes */
486                 // TODO: do anything here?
487         }
488 }
489
490 /* Able to generate complex predicates when there are multiple predciate expressions */
491 void FuncNode::generate_predicates(Predicate * curr_pred, FuncInst * next_inst,
492                                                                                                                                          SnapVector<struct half_pred_expr *> * half_pred_expressions)
493 {
494         if (half_pred_expressions->size() == 0) {
495                 Predicate * new_pred = new Predicate(next_inst);
496                 curr_pred->add_child(new_pred);
497                 new_pred->set_parent(curr_pred);
498
499                 /* Maintain predicate leaves */
500                 predicate_leaves.add(new_pred);
501                 predicate_leaves.remove(curr_pred);
502
503                 /* entry predicates and predicates containing pure write actions
504                  * have no predicate expressions */
505                 if ( curr_pred->is_entry_predicate() )
506                         new_pred->add_predicate_expr(NOPREDICATE, NULL, true);
507                 else if (next_inst->is_write()) {
508                         /* next_inst->is_write() <==> pure writes */
509                         new_pred->add_predicate_expr(NOPREDICATE, NULL, true);
510                 }
511
512                 return;
513         }
514
515         SnapVector<Predicate *> predicates;
516
517         struct half_pred_expr * half_expr = (*half_pred_expressions)[0];
518         predicates.push_back(new Predicate(next_inst));
519         predicates.push_back(new Predicate(next_inst));
520
521         predicates[0]->add_predicate_expr(half_expr->token, half_expr->func_inst, true);
522         predicates[1]->add_predicate_expr(half_expr->token, half_expr->func_inst, false);
523
524         for (uint i = 1;i < half_pred_expressions->size();i++) {
525                 half_expr = (*half_pred_expressions)[i];
526
527                 uint old_size = predicates.size();
528                 for (uint j = 0;j < old_size;j++) {
529                         Predicate * pred = predicates[j];
530                         Predicate * new_pred = new Predicate(next_inst);
531                         new_pred->copy_predicate_expr(pred);
532
533                         pred->add_predicate_expr(half_expr->token, half_expr->func_inst, true);
534                         new_pred->add_predicate_expr(half_expr->token, half_expr->func_inst, false);
535
536                         predicates.push_back(new_pred);
537                 }
538         }
539
540         for (uint i = 0;i < predicates.size();i++) {
541                 Predicate * pred= predicates[i];
542                 curr_pred->add_child(pred);
543                 pred->set_parent(curr_pred);
544
545                 /* Add new predicate leaves */
546                 predicate_leaves.add(pred);
547         }
548
549         /* Remove predicate node that has children */
550         predicate_leaves.remove(curr_pred);
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 /* Make sure elements of thrd_inst_act_map are initialized properly when threads enter functions */
670 void FuncNode::init_inst_act_map(thread_id_t tid)
671 {
672         int thread_id = id_to_int(tid);
673         SnapVector<inst_act_map_t *> * thrd_inst_act_map = history->getThrdInstActMap(func_id);
674         uint old_size = thrd_inst_act_map->size();
675
676         if (old_size <= (uint) thread_id) {
677                 uint new_size = thread_id + 1;
678                 thrd_inst_act_map->resize(new_size);
679
680                 for (uint i = old_size;i < new_size;i++)
681                         (*thrd_inst_act_map)[i] = new inst_act_map_t(128);
682         }
683 }
684
685 /* Reset elements of thrd_inst_act_map when threads exit functions */
686 void FuncNode::reset_inst_act_map(thread_id_t tid)
687 {
688         int thread_id = id_to_int(tid);
689         SnapVector<inst_act_map_t *> * thrd_inst_act_map = history->getThrdInstActMap(func_id);
690
691         inst_act_map_t * map = (*thrd_inst_act_map)[thread_id];
692         map->reset();
693 }
694
695 void FuncNode::update_inst_act_map(thread_id_t tid, ModelAction * read_act)
696 {
697         int thread_id = id_to_int(tid);
698         SnapVector<inst_act_map_t *> * thrd_inst_act_map = history->getThrdInstActMap(func_id);
699
700         inst_act_map_t * map = (*thrd_inst_act_map)[thread_id];
701         FuncInst * read_inst = get_inst(read_act);
702         map->put(read_inst, read_act);
703 }
704
705 inst_act_map_t * FuncNode::get_inst_act_map(thread_id_t tid)
706 {
707         int thread_id = id_to_int(tid);
708         SnapVector<inst_act_map_t *> * thrd_inst_act_map = history->getThrdInstActMap(func_id);
709
710         return (*thrd_inst_act_map)[thread_id];
711 }
712
713 void FuncNode::set_marker(thread_id_t tid)
714 {
715         marker++;
716         uint thread_id = id_to_int(tid);
717         for (uint i = thrd_marker.size(); i < thread_id + 1; i++) {
718                 thrd_marker.push_back(0);
719         }
720
721         thrd_marker[thread_id] = marker;
722 }
723
724 /* Make sure elements of maps are initialized properly when threads enter functions */
725 void FuncNode::init_local_maps(thread_id_t tid)
726 {
727         int thread_id = id_to_int(tid);
728         uint old_size = thrd_loc_inst_map.size();
729
730         if (old_size <= (uint) thread_id) {
731                 uint new_size = thread_id + 1;
732
733                 thrd_loc_inst_map.resize(new_size);
734                 thrd_inst_id_map.resize(new_size);
735                 thrd_inst_pred_map.resize(new_size);
736
737                 for (uint i = old_size; i < new_size; i++) {
738                         thrd_loc_inst_map[i] = new loc_inst_map_t(128);
739                         thrd_inst_id_map[i] = new inst_id_map_t(128);
740                         thrd_inst_pred_map[i] = new inst_pred_map_t(128);
741                 }
742         }
743 }
744
745 /* Reset elements of maps when threads exit functions */
746 void FuncNode::reset_local_maps(thread_id_t tid)
747 {
748         int thread_id = id_to_int(tid);
749         thrd_loc_inst_map[thread_id]->reset();
750         thrd_inst_id_map[thread_id]->reset();
751         thrd_inst_pred_map[thread_id]->reset();
752 }
753
754 void FuncNode::init_predicate_tree_data_structure(thread_id_t tid)
755 {
756         int thread_id = id_to_int(tid);
757         int old_size = thrd_predicate_tree_position.size();
758
759         if (old_size <= thread_id + 1) {
760                 thrd_predicate_tree_position.resize(thread_id + 1);
761                 thrd_predicate_trace.resize(thread_id + 1);
762
763                 for (int i = old_size; i < thread_id + 1; i++) {
764                         thrd_predicate_tree_position[i] = new ModelVector<Predicate *>();
765                         thrd_predicate_trace[i] = new ModelVector<predicate_trace_t *>();
766                 }
767         }
768
769         thrd_predicate_tree_position[thread_id]->push_back(predicate_tree_entry);
770         thrd_predicate_trace[thread_id]->push_back(new predicate_trace_t());
771 }
772
773 void FuncNode::reset_predicate_tree_data_structure(thread_id_t tid)
774 {
775         int thread_id = id_to_int(tid);
776         thrd_predicate_tree_position[thread_id]->pop_back();
777
778         predicate_trace_t * trace = thrd_predicate_trace[thread_id]->back();
779         delete trace;
780         thrd_predicate_trace[thread_id]->pop_back();
781 }
782
783 /* Add FuncNodes that this node may follow */
784 void FuncNode::add_out_edge(FuncNode * other)
785 {
786         if ( !edge_table.contains(other) ) {
787                 edge_table.put(other, OUT_EDGE);
788                 out_edges.push_back(other);
789                 return;
790         }
791
792         edge_type_t edge = edge_table.get(other);
793         if (edge == IN_EDGE) {
794                 edge_table.put(other, BI_EDGE);
795                 out_edges.push_back(other);
796         }
797 }
798
799 /* Compute the distance between this FuncNode and the target node.
800  * Return -1 if the target node is unreachable or the actual distance
801  * is greater than max_step.
802  */
803 int FuncNode::compute_distance(FuncNode * target, int max_step)
804 {
805         if (target == NULL)
806                 return -1;
807         else if (target == this)
808                 return 0;
809
810         SnapList<FuncNode *> queue;
811         HashTable<FuncNode *, int, uintptr_t, 0> distances(128);
812
813         queue.push_back(this);
814         distances.put(this, 0);
815
816         while (!queue.empty()) {
817                 FuncNode * curr = queue.front();
818                 queue.pop_front();
819                 int dist = distances.get(curr);
820
821                 if (max_step <= dist)
822                         return -1;
823
824                 ModelList<FuncNode *> * outEdges = curr->get_out_edges();
825                 mllnode<FuncNode *> * it;
826                 for (it = outEdges->begin();it != NULL;it = it->getNext()) {
827                         FuncNode * out_node = it->getVal();
828
829                         /* This node has not been visited before */
830                         if ( !distances.contains(out_node) ) {
831                                 if (out_node == target)
832                                         return dist + 1;
833
834                                 queue.push_back(out_node);
835                                 distances.put(out_node, dist + 1);
836                         }
837                 }
838         }
839
840         /* Target node is unreachable */
841         return -1;
842 }
843
844 void FuncNode::add_failed_predicate(Predicate * pred)
845 {
846         failed_predicates.add(pred);
847 }
848
849 /* Implement quick sort to sort leaves before assigning base scores */
850 template<typename _Tp>
851 static int partition(ModelVector<_Tp *> * arr, int low, int high)
852 {
853         unsigned int pivot = (*arr)[high] -> get_depth();
854         int i = low - 1;
855
856         for (int j = low;j <= high - 1;j ++) {
857                 if ( (*arr)[j] -> get_depth() < pivot ) {
858                         i ++;
859                         _Tp * tmp = (*arr)[i];
860                         (*arr)[i] = (*arr)[j];
861                         (*arr)[j] = tmp;
862                 }
863         }
864
865         _Tp * tmp = (*arr)[i + 1];
866         (*arr)[i + 1] = (*arr)[high];
867         (*arr)[high] = tmp;
868
869         return i + 1;
870 }
871
872 /* Implement quick sort to sort leaves before assigning base scores */
873 template<typename _Tp>
874 static void quickSort(ModelVector<_Tp *> * arr, int low, int high)
875 {
876         if (low < high) {
877                 int pi = partition(arr, low, high);
878
879                 quickSort(arr, low, pi - 1);
880                 quickSort(arr, pi + 1, high);
881         }
882 }
883
884 void FuncNode::update_predicate_tree_weight(thread_id_t tid)
885 {
886         failed_predicates.reset();
887
888         predicate_trace_t * trace = thrd_predicate_trace[id_to_int(tid)]->back();
889
890         // Update predicate weights based on prediate trace
891         for (mllnode<Predicate *> * rit = trace->end(); rit != NULL; rit = rit->getPrev()) {
892                 Predicate * node = rit->getVal();
893
894                 if (predicate_leaves.contains(node)) {
895                         double weight = 100.0 / sqrt(node->get_expl_count() + node->get_fail_count() + 1);
896                         node->set_weight(weight);
897                 } else {
898                         double weight_sum = 0.0;
899                         ModelVector<Predicate *> * children = node->get_children();
900                         for (uint i = 0;i < children->size();i++) {
901                                 Predicate * child = (*children)[i];
902                                 double weight = child->get_weight();
903                                 weight_sum += weight;
904                         }
905
906                         double average_weight = (double) weight_sum / (double) children->size();
907                         double weight = average_weight * pow(0.9, node->get_depth());
908                         node->set_weight(weight);
909                 }
910         }
911 }
912
913 void FuncNode::print_predicate_tree()
914 {
915         model_print("digraph function_%s {\n", func_name);
916         predicate_tree_entry->print_pred_subtree();
917         predicate_tree_exit->print_predicate();
918         model_print("}\n");     // end of graph
919 }