ab3862861aae07460e8f5e8c62fae6a4a11a7ed6
[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();    /* Only need to store the locations of read actions */
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                 SnapVector<Predicate *> unset_predicates = SnapVector<Predicate *>();
285                 bool branch_found = follow_branch(&curr_pred, next_inst, next_act, &unset_predicates);
286
287                 // A branch with unset predicate expression is detected
288                 if (!branch_found && unset_predicates.size() != 0) {
289                         ASSERT(unset_predicates.size() == 1);
290                         Predicate * one_branch = unset_predicates[0];
291
292                         bool amended = amend_predicate_expr(curr_pred, next_inst, next_act);
293                         if (amended)
294                                 continue;
295                         else {
296                                 curr_pred = one_branch;
297                                 branch_found = true;
298                         }
299                 }
300
301                 // Detect loops
302                 if (!branch_found && inst_id_map.contains(next_inst)) {
303                         FuncInst * curr_inst = curr_pred->get_func_inst();
304                         uint32_t curr_id = inst_id_map.get(curr_inst);
305                         uint32_t next_id = inst_id_map.get(next_inst);
306
307                         if (curr_id >= next_id) {
308                                 Predicate * old_pred = inst_pred_map.get(next_inst);
309                                 Predicate * back_pred = old_pred->get_parent();
310
311                                 curr_pred->add_backedge(back_pred);
312                                 curr_pred = back_pred;
313                                 continue;
314                         }
315                 }
316
317                 // Generate new branches
318                 if (!branch_found) {
319                         SnapVector<struct half_pred_expr *> half_pred_expressions;
320                         infer_predicates(next_inst, next_act, &half_pred_expressions);
321                         generate_predicates(curr_pred, next_inst, &half_pred_expressions);
322                         continue;
323                 }
324
325                 if (next_act->is_write())
326                         curr_pred->set_write(true);
327
328                 if (next_act->is_read()) {
329                         loc_act_map.put(next_act->get_location(), next_act);
330                 }
331
332                 inst_pred_map.put(next_inst, curr_pred);
333                 if (!inst_id_map.contains(next_inst))
334                         inst_id_map.put(next_inst, inst_counter++);
335
336                 it = it->getNext();
337                 curr_pred->incr_expl_count();
338         }
339
340         if (curr_pred->get_exit() == NULL) {
341                 // Exit predicate is unset yet
342                 curr_pred->set_exit(predicate_tree_exit);
343         }
344
345         update_predicate_tree_weight();
346 }
347
348 /* Given curr_pred and next_inst, find the branch following curr_pred that
349  * contains next_inst and the correct predicate.
350  * @return true if branch found, false otherwise.
351  */
352 bool FuncNode::follow_branch(Predicate ** curr_pred, FuncInst * next_inst,
353 ModelAction * next_act, SnapVector<Predicate *> * unset_predicates)
354 {
355         /* Check if a branch with func_inst and corresponding predicate exists */
356         bool branch_found = false;
357         ModelVector<Predicate *> * branches = (*curr_pred)->get_children();
358         for (uint i = 0;i < branches->size();i++) {
359                 Predicate * branch = (*branches)[i];
360                 if (branch->get_func_inst() != next_inst)
361                         continue;
362
363                 /* Check against predicate expressions */
364                 bool predicate_correct = true;
365                 PredExprSet * pred_expressions = branch->get_pred_expressions();
366                 PredExprSetIter * pred_expr_it = pred_expressions->iterator();
367
368                 /* Only read and rmw actions my have unset predicate expressions */
369                 if (pred_expressions->getSize() == 0) {
370                         predicate_correct = false;
371                         unset_predicates->push_back(branch);
372                 }
373
374                 while (pred_expr_it->hasNext()) {
375                         pred_expr * pred_expression = pred_expr_it->next();
376                         uint64_t last_read, next_read;
377                         bool equality;
378
379                         switch(pred_expression->token) {
380                                 case NOPREDICATE:
381                                         predicate_correct = true;
382                                         break;
383                                 case EQUALITY:
384                                         FuncInst * to_be_compared;
385                                         ModelAction * last_act;
386
387                                         to_be_compared = pred_expression->func_inst;
388                                         last_act = to_be_compared->get_associated_act(marker);
389
390                                         last_read = last_act->get_reads_from_value();
391                                         next_read = next_act->get_reads_from_value();
392                                         equality = (last_read == next_read);
393                                         if (equality != pred_expression->value)
394                                                 predicate_correct = false;
395
396                                         break;
397                                 case NULLITY:
398                                         next_read = next_act->get_reads_from_value();
399                                         // TODO: implement likely to be null
400                                         equality = ( (void*) (next_read & 0xffffffff) == NULL);
401                                         if (equality != pred_expression->value)
402                                                 predicate_correct = false;
403                                         break;
404                                 default:
405                                         predicate_correct = false;
406                                         model_print("unkown predicate token\n");
407                                         break;
408                         }
409                 }
410
411                 if (predicate_correct) {
412                         *curr_pred = branch;
413                         branch_found = true;
414                         break;
415                 }
416         }
417
418         return branch_found;
419 }
420
421 /* Infer predicate expressions, which are generated in FuncNode::generate_predicates */
422 void FuncNode::infer_predicates(FuncInst * next_inst, ModelAction * next_act,
423 SnapVector<struct half_pred_expr *> * half_pred_expressions)
424 {
425         void * loc = next_act->get_location();
426
427         if (next_inst->is_read()) {
428                 /* read + rmw */
429                 if ( loc_act_map.contains(loc) ) {
430                         ModelAction * last_act = loc_act_map.get(loc);
431                         FuncInst * last_inst = get_inst(last_act);
432                         struct half_pred_expr * expression = new half_pred_expr(EQUALITY, last_inst);
433                         half_pred_expressions->push_back(expression);
434                 } else if ( next_inst->is_single_location() ) {
435                         loc_set_t * loc_may_equal = loc_may_equal_map->get(loc);
436
437                         if (loc_may_equal != NULL) {
438                                 loc_set_iter * loc_it = loc_may_equal->iterator();
439                                 while (loc_it->hasNext()) {
440                                         void * neighbor = loc_it->next();
441                                         if (loc_act_map.contains(neighbor)) {
442                                                 ModelAction * last_act = loc_act_map.get(neighbor);
443                                                 FuncInst * last_inst = get_inst(last_act);
444
445                                                 struct half_pred_expr * expression = new half_pred_expr(EQUALITY, last_inst);
446                                                 half_pred_expressions->push_back(expression);
447                                         }
448                                 }
449                         }
450                 } else {
451                         // next_inst is not single location
452                         uint64_t read_val = next_act->get_reads_from_value();
453
454                         // only infer NULLITY predicate when it is actually NULL.
455                         if ( (void*)read_val == NULL) {
456                                 struct half_pred_expr * expression = new half_pred_expr(NULLITY, NULL);
457                                 half_pred_expressions->push_back(expression);
458                         }
459                 }
460         } else {
461                 /* Pure writes */
462                 // TODO: do anything here?
463         }
464 }
465
466 /* Able to generate complex predicates when there are multiple predciate expressions */
467 void FuncNode::generate_predicates(Predicate * curr_pred, FuncInst * next_inst,
468 SnapVector<struct half_pred_expr *> * half_pred_expressions)
469 {
470         if (half_pred_expressions->size() == 0) {
471                 Predicate * new_pred = new Predicate(next_inst);
472                 curr_pred->add_child(new_pred);
473                 new_pred->set_parent(curr_pred);
474
475                 /* Maintain predicate leaves */
476                 predicate_leaves.add(new_pred);
477                 predicate_leaves.remove(curr_pred);
478
479                 /* entry predicates and predicates containing pure write actions
480                  * have no predicate expressions */
481                 if ( curr_pred->is_entry_predicate() )
482                         new_pred->add_predicate_expr(NOPREDICATE, NULL, true);
483                 else if (next_inst->is_write()) {
484                         /* next_inst->is_write() <==> pure writes */
485                         new_pred->add_predicate_expr(NOPREDICATE, NULL, true);
486                 }
487
488                 return;
489         }
490
491         SnapVector<Predicate *> predicates;
492
493         struct half_pred_expr * half_expr = (*half_pred_expressions)[0];
494         predicates.push_back(new Predicate(next_inst));
495         predicates.push_back(new Predicate(next_inst));
496
497         predicates[0]->add_predicate_expr(half_expr->token, half_expr->func_inst, true);
498         predicates[1]->add_predicate_expr(half_expr->token, half_expr->func_inst, false);
499
500         for (uint i = 1;i < half_pred_expressions->size();i++) {
501                 half_expr = (*half_pred_expressions)[i];
502
503                 uint old_size = predicates.size();
504                 for (uint j = 0;j < old_size;j++) {
505                         Predicate * pred = predicates[j];
506                         Predicate * new_pred = new Predicate(next_inst);
507                         new_pred->copy_predicate_expr(pred);
508
509                         pred->add_predicate_expr(half_expr->token, half_expr->func_inst, true);
510                         new_pred->add_predicate_expr(half_expr->token, half_expr->func_inst, false);
511
512                         predicates.push_back(new_pred);
513                 }
514         }
515
516         for (uint i = 0;i < predicates.size();i++) {
517                 Predicate * pred= predicates[i];
518                 curr_pred->add_child(pred);
519                 pred->set_parent(curr_pred);
520
521                 /* Add new predicate leaves */
522                 predicate_leaves.add(pred);
523         }
524
525         /* Remove predicate node that has children */
526         predicate_leaves.remove(curr_pred);
527
528         /* Free memories allocated by infer_predicate */
529         for (uint i = 0;i < half_pred_expressions->size();i++) {
530                 struct half_pred_expr * tmp = (*half_pred_expressions)[i];
531                 snapshot_free(tmp);
532         }
533 }
534
535 /* Amend predicates that contain no predicate expressions. Currenlty only amend with NULLITY predicates */
536 bool FuncNode::amend_predicate_expr(Predicate * curr_pred, FuncInst * next_inst, ModelAction * next_act)
537 {
538         // there should only be only child
539         Predicate * unset_pred = curr_pred->get_children()->back();
540         uint64_t read_val = next_act->get_reads_from_value();
541
542         // only generate NULLITY predicate when it is actually NULL.
543         if ( !next_inst->is_single_location() && (void*)read_val == NULL ) {
544                 Predicate * new_pred = new Predicate(next_inst);
545
546                 curr_pred->add_child(new_pred);
547                 new_pred->set_parent(curr_pred);
548
549                 unset_pred->add_predicate_expr(NULLITY, NULL, false);
550                 new_pred->add_predicate_expr(NULLITY, NULL, true);
551
552                 return true;
553         }
554
555         return false;
556 }
557
558 void FuncNode::add_to_val_loc_map(uint64_t val, void * loc)
559 {
560         loc_set_t * locations = val_loc_map->get(val);
561
562         if (locations == NULL) {
563                 locations = new loc_set_t();
564                 val_loc_map->put(val, locations);
565         }
566
567         update_loc_may_equal_map(loc, locations);
568         locations->add(loc);
569         // values_may_read_from->add(val);
570 }
571
572 void FuncNode::add_to_val_loc_map(value_set_t * values, void * loc)
573 {
574         if (values == NULL)
575                 return;
576
577         value_set_iter * it = values->iterator();
578         while (it->hasNext()) {
579                 uint64_t val = it->next();
580                 add_to_val_loc_map(val, loc);
581         }
582 }
583
584 void FuncNode::update_loc_may_equal_map(void * new_loc, loc_set_t * old_locations)
585 {
586         if ( old_locations->contains(new_loc) )
587                 return;
588
589         loc_set_t * neighbors = loc_may_equal_map->get(new_loc);
590
591         if (neighbors == NULL) {
592                 neighbors = new loc_set_t();
593                 loc_may_equal_map->put(new_loc, neighbors);
594         }
595
596         loc_set_iter * loc_it = old_locations->iterator();
597         while (loc_it->hasNext()) {
598                 // new_loc: { old_locations, ... }
599                 void * member = loc_it->next();
600                 neighbors->add(member);
601
602                 // for each i in old_locations, i : { new_loc, ... }
603                 loc_set_t * _neighbors = loc_may_equal_map->get(member);
604                 if (_neighbors == NULL) {
605                         _neighbors = new loc_set_t();
606                         loc_may_equal_map->put(member, _neighbors);
607                 }
608                 _neighbors->add(new_loc);
609         }
610 }
611
612 /* Every time a thread enters a function, set its position to the predicate tree entry */
613 void FuncNode::init_predicate_tree_position(thread_id_t tid)
614 {
615         int thread_id = id_to_int(tid);
616         if (predicate_tree_position.size() <= (uint) thread_id)
617                 predicate_tree_position.resize(thread_id + 1);
618
619         predicate_tree_position[thread_id] = predicate_tree_entry;
620 }
621
622 void FuncNode::set_predicate_tree_position(thread_id_t tid, Predicate * pred)
623 {
624         int thread_id = id_to_int(tid);
625         predicate_tree_position[thread_id] = pred;
626 }
627
628 /* @return The position of a thread in a predicate tree */
629 Predicate * FuncNode::get_predicate_tree_position(thread_id_t tid)
630 {
631         int thread_id = id_to_int(tid);
632         return predicate_tree_position[thread_id];
633 }
634
635 /* Make sure elements of thrd_inst_act_map are initialized properly when threads enter functions */
636 void FuncNode::init_inst_act_map(thread_id_t tid)
637 {
638         int thread_id = id_to_int(tid);
639         SnapVector<inst_act_map_t *> * thrd_inst_act_map = history->getThrdInstActMap(func_id);
640         uint old_size = thrd_inst_act_map->size();
641
642         if (thrd_inst_act_map->size() <= (uint) thread_id) {
643                 uint new_size = thread_id + 1;
644                 thrd_inst_act_map->resize(new_size);
645
646                 for (uint i = old_size;i < new_size;i++)
647                         (*thrd_inst_act_map)[i] = new inst_act_map_t(128);
648         }
649 }
650
651 /* Reset elements of thrd_inst_act_map when threads exit functions */
652 void FuncNode::reset_inst_act_map(thread_id_t tid)
653 {
654         int thread_id = id_to_int(tid);
655         SnapVector<inst_act_map_t *> * thrd_inst_act_map = history->getThrdInstActMap(func_id);
656
657         inst_act_map_t * map = (*thrd_inst_act_map)[thread_id];
658         map->reset();
659 }
660
661 void FuncNode::update_inst_act_map(thread_id_t tid, ModelAction * read_act)
662 {
663         int thread_id = id_to_int(tid);
664         SnapVector<inst_act_map_t *> * thrd_inst_act_map = history->getThrdInstActMap(func_id);
665
666         inst_act_map_t * map = (*thrd_inst_act_map)[thread_id];
667         FuncInst * read_inst = get_inst(read_act);
668         map->put(read_inst, read_act);
669 }
670
671 inst_act_map_t * FuncNode::get_inst_act_map(thread_id_t tid)
672 {
673         int thread_id = id_to_int(tid);
674         SnapVector<inst_act_map_t *> * thrd_inst_act_map = history->getThrdInstActMap(func_id);
675
676         return (*thrd_inst_act_map)[thread_id];
677 }
678
679 /* Add FuncNodes that this node may follow */
680 void FuncNode::add_out_edge(FuncNode * other)
681 {
682         if ( !edge_table.contains(other) ) {
683                 edge_table.put(other, OUT_EDGE);
684                 out_edges.push_back(other);
685                 return;
686         }
687
688         edge_type_t edge = edge_table.get(other);
689         if (edge == IN_EDGE) {
690                 edge_table.put(other, BI_EDGE);
691                 out_edges.push_back(other);
692         }
693 }
694
695 /* Compute the distance between this FuncNode and the target node.
696  * Return -1 if the target node is unreachable or the actual distance
697  * is greater than max_step.
698  */
699 int FuncNode::compute_distance(FuncNode * target, int max_step)
700 {
701         if (target == NULL)
702                 return -1;
703         else if (target == this)
704                 return 0;
705
706         SnapList<FuncNode *> queue;
707         HashTable<FuncNode *, int, uintptr_t, 0> distances(128);
708
709         queue.push_back(this);
710         distances.put(this, 0);
711
712         while (!queue.empty()) {
713                 FuncNode * curr = queue.front();
714                 queue.pop_front();
715                 int dist = distances.get(curr);
716
717                 if (max_step <= dist)
718                         return -1;
719
720                 ModelList<FuncNode *> * outEdges = curr->get_out_edges();
721                 mllnode<FuncNode *> * it;
722                 for (it = outEdges->begin();it != NULL;it = it->getNext()) {
723                         FuncNode * out_node = it->getVal();
724
725                         /* This node has not been visited before */
726                         if ( !distances.contains(out_node) ) {
727                                 if (out_node == target)
728                                         return dist + 1;
729
730                                 queue.push_back(out_node);
731                                 distances.put(out_node, dist + 1);
732                         }
733                 }
734         }
735
736         /* Target node is unreachable */
737         return -1;
738 }
739
740 /* Implement quick sort to sort leaves before assigning base scores */
741 static int partition(SnapVector<Predicate *> * arr, int low, int high)
742 {
743         unsigned int pivot = (*arr)[high]->get_depth();
744         int i = low - 1;
745
746         for (int j = low; j <= high - 1; j++) {
747                 if ( (*arr)[j]->get_depth() < pivot ) {
748                         i++;
749                         Predicate *tmp = (*arr)[i];
750                         (*arr)[i] = (*arr)[j];
751                         (*arr)[j] = tmp;
752                 }
753         }
754
755         Predicate * tmp = (*arr)[i + 1];
756         (*arr)[i + 1] = (*arr)[high];
757         (*arr)[high] = tmp;
758
759         return i + 1;
760 }
761
762 /* Implement quick sort to sort leaves before assigning base scores */
763 static void quickSort(SnapVector<Predicate *> * arr, int low, int high)
764 {
765         if (low < high) {
766                 int pi = partition(arr, low, high);
767
768                 quickSort(arr, low, pi - 1);
769                 quickSort(arr, pi + 1, high);
770         }
771 }
772
773 void FuncNode::assign_initial_weight()
774 {
775         PredSetIter * it = predicate_leaves.iterator();
776         SnapVector<Predicate *> leaves;
777         while (it->hasNext()) {
778                 Predicate * pred = it->next();
779                 double weight = 100.0 / sqrt(pred->get_expl_count() + 1);
780                 pred->set_weight(weight);
781                 leaves.push_back(pred);
782         }
783
784         quickSort(&leaves, 0, leaves.size() - 1);
785
786         // assign scores for internal nodes;
787         while ( !leaves.empty() ) {
788                 Predicate * leaf = leaves.back();
789                 leaves.pop_back();
790
791                 Predicate * curr = leaf->get_parent();
792                 while (curr != NULL) {
793                         if (curr->get_weight() != 0) {
794                                 // Has been exlpored
795                                 break;
796                         }
797
798                         ModelVector<Predicate *> * children = curr->get_children();
799                         double weight_sum = 0;
800                         bool has_unassigned_node = false;
801
802                         for (uint i = 0; i < children->size(); i++) {
803                                 Predicate * child = (*children)[i];
804
805                                 // If a child has unassigned weight
806                                 double weight = child->get_weight();
807                                 if (weight == 0) {
808                                         has_unassigned_node = true;
809                                         break;
810                                 } else
811                                         weight_sum += weight;
812                         }
813
814                         if (!has_unassigned_node) {
815                                 double average_weight = (double) weight_sum / (double) children->size();
816                                 double weight = average_weight * pow(0.9, curr->get_depth());
817                                 curr->set_weight(weight);
818                         } else
819                                 break;
820
821                         curr = curr->get_parent();
822                 }
823         }
824 }
825
826 void FuncNode::update_predicate_tree_weight()
827 {
828         if (marker == 2) {
829                 // Predicate tree is initially built
830                 assign_initial_weight();
831         } else {
832
833         }
834 }
835
836 void FuncNode::print_predicate_tree()
837 {
838         model_print("digraph function_%s {\n", func_name);
839         predicate_tree_entry->print_pred_subtree();
840         predicate_tree_exit->print_predicate();
841         model_print("}\n");     // end of graph
842 }