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