Move data structures from execution.h to history.h
[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         predicate_tree_position(),
19         predicate_leaves(),
20         edge_table(32),
21         out_edges()
22 {
23         predicate_tree_entry = new Predicate(NULL, true);
24         predicate_tree_entry->add_predicate_expr(NOPREDICATE, NULL, true);
25
26         predicate_tree_exit = new Predicate(NULL, false, true);
27         predicate_tree_exit->set_depth(MAX_DEPTH);
28
29         /* Snapshot data structures below */
30         action_list_buffer = new SnapList<action_list_t *>();
31         read_locations = new loc_set_t();
32         write_locations = new loc_set_t();
33         val_loc_map = new HashTable<uint64_t, loc_set_t *, uint64_t, 0, snapshot_malloc, snapshot_calloc, snapshot_free, int64_hash>();
34         loc_may_equal_map = new HashTable<void *, loc_set_t *, uintptr_t, 0>();
35
36         //values_may_read_from = new value_set_t();
37 }
38
39 /* Reallocate snapshotted memories when new executions start */
40 void FuncNode::set_new_exec_flag()
41 {
42         action_list_buffer = new SnapList<action_list_t *>();
43         read_locations = new loc_set_t();
44         write_locations = new loc_set_t();
45         val_loc_map = new HashTable<uint64_t, loc_set_t *, uint64_t, 0, snapshot_malloc, snapshot_calloc, snapshot_free, int64_hash>();
46         loc_may_equal_map = new HashTable<void *, loc_set_t *, uintptr_t, 0>();
47
48         //values_may_read_from = new value_set_t();
49 }
50
51 /* Check whether FuncInst with the same type, position, and location
52  * as act has been added to func_inst_map or not. If not, add it.
53  */
54 void FuncNode::add_inst(ModelAction *act)
55 {
56         ASSERT(act);
57         const char * position = act->get_position();
58
59         /* THREAD* actions, ATOMIC_LOCK, ATOMIC_TRYLOCK, and ATOMIC_UNLOCK
60          * actions are not tagged with their source line numbers
61          */
62         if (position == NULL)
63                 return;
64
65         FuncInst * func_inst = func_inst_map.get(position);
66
67         /* This position has not been inserted into hashtable before */
68         if (func_inst == NULL) {
69                 func_inst = create_new_inst(act);
70                 func_inst_map.put(position, func_inst);
71                 return;
72         }
73
74         /* Volatile variables that use ++ or -- syntax may result in read and write actions with the same position */
75         if (func_inst->get_type() != act->get_type()) {
76                 FuncInst * collision_inst = func_inst->search_in_collision(act);
77
78                 if (collision_inst == NULL) {
79                         collision_inst = create_new_inst(act);
80                         func_inst->add_to_collision(collision_inst);
81                         return;
82                 } else {
83                         func_inst = collision_inst;
84                 }
85         }
86
87         ASSERT(func_inst->get_type() == act->get_type());
88         int curr_execution_number = model->get_execution_number();
89
90         /* Reset locations when new executions start */
91         if (func_inst->get_execution_number() != curr_execution_number) {
92                 func_inst->set_location(act->get_location());
93                 func_inst->set_execution_number(curr_execution_number);
94         }
95
96         /* Mark the memory location of such inst as not unique */
97         if (func_inst->get_location() != act->get_location())
98                 func_inst->not_single_location();
99 }
100
101 FuncInst * FuncNode::create_new_inst(ModelAction * act)
102 {
103         FuncInst * func_inst = new FuncInst(act, this);
104         int exec_num = model->get_execution_number();
105         func_inst->set_execution_number(exec_num);
106
107         inst_list.push_back(func_inst);
108
109         return func_inst;
110 }
111
112
113 /* Get the FuncInst with the same type, position, and location
114  * as act
115  *
116  * @return FuncInst with the same type, position, and location as act */
117 FuncInst * FuncNode::get_inst(ModelAction *act)
118 {
119         ASSERT(act);
120         const char * position = act->get_position();
121
122         /* THREAD* actions, ATOMIC_LOCK, ATOMIC_TRYLOCK, and ATOMIC_UNLOCK
123          * actions are not tagged with their source line numbers
124          */
125         if (position == NULL)
126                 return NULL;
127
128         FuncInst * inst = func_inst_map.get(position);
129         if (inst == NULL)
130                 return NULL;
131
132         action_type inst_type = inst->get_type();
133         action_type act_type = act->get_type();
134
135         if (inst_type == act_type) {
136                 return inst;
137         }
138         /* RMWRCAS actions are converted to RMW or READ actions */
139         else if (inst_type == ATOMIC_RMWRCAS &&
140                                          (act_type == ATOMIC_RMW || act_type == ATOMIC_READ)) {
141                 return inst;
142         }
143         /* Return the FuncInst in the collision list */
144         else {
145                 return inst->search_in_collision(act);
146         }
147 }
148
149
150 void FuncNode::add_entry_inst(FuncInst * inst)
151 {
152         if (inst == NULL)
153                 return;
154
155         mllnode<FuncInst *> * it;
156         for (it = entry_insts.begin();it != NULL;it = it->getNext()) {
157                 if (inst == it->getVal())
158                         return;
159         }
160
161         entry_insts.push_back(inst);
162 }
163
164 /**
165  * @brief Convert ModelAdtion list to FuncInst list
166  * @param act_list A list of ModelActions
167  */
168 void FuncNode::update_tree(action_list_t * act_list)
169 {
170         if (act_list == NULL || act_list->size() == 0)
171                 return;
172
173         HashTable<void *, value_set_t *, uintptr_t, 0> * write_history = history->getWriteHistory();
174
175         /* build inst_list from act_list for later processing */
176         func_inst_list_t inst_list;
177         action_list_t rw_act_list;
178
179         for (sllnode<ModelAction *> * it = act_list->begin();it != NULL;it = it->getNext()) {
180                 ModelAction * act = it->getVal();
181                 FuncInst * func_inst = get_inst(act);
182                 void * loc = act->get_location();
183
184                 if (func_inst == NULL)
185                         continue;
186
187                 inst_list.push_back(func_inst);
188                 bool act_added = false;
189
190                 if (act->is_write()) {
191                         rw_act_list.push_back(act);
192                         act_added = true;
193                         if (!write_locations->contains(loc)) {
194                                 write_locations->add(loc);
195                                 history->update_loc_wr_func_nodes_map(loc, this);
196                         }
197                 }
198
199                 if (act->is_read()) {
200                         if (!act_added)
201                                 rw_act_list.push_back(act);
202
203                         /* If func_inst may only read_from a single location, then:
204                          *
205                          * The first time an action reads from some location,
206                          * import all the values that have been written to this
207                          * location from ModelHistory and notify ModelHistory
208                          * that this FuncNode may read from this location.
209                          */
210                         if (!read_locations->contains(loc) && func_inst->is_single_location()) {
211                                 read_locations->add(loc);
212                                 value_set_t * write_values = write_history->get(loc);
213                                 add_to_val_loc_map(write_values, loc);
214                                 history->update_loc_rd_func_nodes_map(loc, this);
215                         }
216                 }
217         }
218
219 //      model_print("function %s\n", func_name);
220 //      print_val_loc_map();
221
222         update_inst_tree(&inst_list);
223         update_predicate_tree(&rw_act_list);
224
225 //      print_predicate_tree();
226 }
227
228 /**
229  * @brief Link FuncInsts in inst_list  - add one FuncInst to another's predecessors and successors
230  * @param inst_list A list of FuncInsts
231  */
232 void FuncNode::update_inst_tree(func_inst_list_t * inst_list)
233 {
234         if (inst_list == NULL)
235                 return;
236         else if (inst_list->size() == 0)
237                 return;
238
239         /* start linking */
240         sllnode<FuncInst *>* it = inst_list->begin();
241         sllnode<FuncInst *>* prev;
242
243         /* add the first instruction to the list of entry insts */
244         FuncInst * entry_inst = it->getVal();
245         add_entry_inst(entry_inst);
246
247         it = it->getNext();
248         while (it != NULL) {
249                 prev = it->getPrev();
250
251                 FuncInst * prev_inst = prev->getVal();
252                 FuncInst * curr_inst = it->getVal();
253
254                 prev_inst->add_succ(curr_inst);
255                 curr_inst->add_pred(prev_inst);
256
257                 it = it->getNext();
258         }
259 }
260
261 void FuncNode::update_predicate_tree(action_list_t * act_list)
262 {
263         if (act_list == NULL || act_list->size() == 0)
264                 return;
265
266         incr_marker();
267
268         /* Map a FuncInst to the its predicate */
269         HashTable<FuncInst *, Predicate *, uintptr_t, 0> inst_pred_map(128);
270
271         // Number FuncInsts to detect loops
272         HashTable<FuncInst *, uint32_t, uintptr_t, 0> inst_id_map(128);
273         uint32_t inst_counter = 0;
274
275         /* Only need to store the locations of read actions */
276         HashTable<void *, ModelAction *, uintptr_t, 0> loc_act_map(128);
277
278         sllnode<ModelAction *> *it = act_list->begin();
279         Predicate * curr_pred = predicate_tree_entry;
280         while (it != NULL) {
281                 ModelAction * next_act = it->getVal();
282                 FuncInst * next_inst = get_inst(next_act);
283                 next_inst->set_associated_act(next_act, marker);
284
285                 SnapVector<Predicate *> unset_predicates = SnapVector<Predicate *>();
286                 bool branch_found = follow_branch(&curr_pred, next_inst, next_act, &unset_predicates);
287
288                 // A branch with unset predicate expression is detected
289                 if (!branch_found && unset_predicates.size() != 0) {
290                         ASSERT(unset_predicates.size() == 1);
291                         Predicate * one_branch = unset_predicates[0];
292
293                         bool amended = amend_predicate_expr(curr_pred, next_inst, next_act);
294                         if (amended)
295                                 continue;
296                         else {
297                                 curr_pred = one_branch;
298                                 branch_found = true;
299                         }
300                 }
301
302                 // Detect loops
303                 if (!branch_found && inst_id_map.contains(next_inst)) {
304                         FuncInst * curr_inst = curr_pred->get_func_inst();
305                         uint32_t curr_id = inst_id_map.get(curr_inst);
306                         uint32_t next_id = inst_id_map.get(next_inst);
307
308                         if (curr_id >= next_id) {
309                                 Predicate * old_pred = inst_pred_map.get(next_inst);
310                                 Predicate * back_pred = old_pred->get_parent();
311
312                                 curr_pred->add_backedge(back_pred);
313                                 curr_pred = back_pred;
314                                 continue;
315                         }
316                 }
317
318                 // Generate new branches
319                 if (!branch_found) {
320                         SnapVector<struct half_pred_expr *> half_pred_expressions;
321                         infer_predicates(next_inst, next_act, &loc_act_map, &half_pred_expressions);
322                         generate_predicates(curr_pred, next_inst, &half_pred_expressions);
323                         continue;
324                 }
325
326                 if (next_act->is_write())
327                         curr_pred->set_write(true);
328
329                 if (next_act->is_read()) {
330                         loc_act_map.put(next_act->get_location(), next_act);
331                 }
332
333                 inst_pred_map.put(next_inst, curr_pred);
334                 if (!inst_id_map.contains(next_inst))
335                         inst_id_map.put(next_inst, inst_counter++);
336
337                 it = it->getNext();
338                 curr_pred->incr_expl_count();
339         }
340
341         if (curr_pred->get_exit() == NULL) {
342                 // Exit predicate is unset yet
343                 curr_pred->set_exit(predicate_tree_exit);
344         }
345 }
346
347 /* Given curr_pred and next_inst, find the branch following curr_pred that
348  * contains next_inst and the correct predicate.
349  * @return true if branch found, false otherwise.
350  */
351 bool FuncNode::follow_branch(Predicate ** curr_pred, FuncInst * next_inst,
352 ModelAction * next_act, SnapVector<Predicate *> * unset_predicates)
353 {
354         /* Check if a branch with func_inst and corresponding predicate exists */
355         bool branch_found = false;
356         ModelVector<Predicate *> * branches = (*curr_pred)->get_children();
357         for (uint i = 0;i < branches->size();i++) {
358                 Predicate * branch = (*branches)[i];
359                 if (branch->get_func_inst() != next_inst)
360                         continue;
361
362                 /* Check against predicate expressions */
363                 bool predicate_correct = true;
364                 PredExprSet * pred_expressions = branch->get_pred_expressions();
365                 PredExprSetIter * pred_expr_it = pred_expressions->iterator();
366
367                 /* Only read and rmw actions my have unset predicate expressions */
368                 if (pred_expressions->getSize() == 0) {
369                         predicate_correct = false;
370                         unset_predicates->push_back(branch);
371                 }
372
373                 while (pred_expr_it->hasNext()) {
374                         pred_expr * pred_expression = pred_expr_it->next();
375                         uint64_t last_read, next_read;
376                         bool equality;
377
378                         switch(pred_expression->token) {
379                                 case NOPREDICATE:
380                                         predicate_correct = true;
381                                         break;
382                                 case EQUALITY:
383                                         FuncInst * to_be_compared;
384                                         ModelAction * last_act;
385
386                                         to_be_compared = pred_expression->func_inst;
387                                         last_act = to_be_compared->get_associated_act(marker);
388
389                                         last_read = last_act->get_reads_from_value();
390                                         next_read = next_act->get_reads_from_value();
391                                         equality = (last_read == next_read);
392                                         if (equality != pred_expression->value)
393                                                 predicate_correct = false;
394
395                                         break;
396                                 case NULLITY:
397                                         next_read = next_act->get_reads_from_value();
398                                         // TODO: implement likely to be null
399                                         equality = ( (void*) (next_read & 0xffffffff) == NULL);
400                                         if (equality != pred_expression->value)
401                                                 predicate_correct = false;
402                                         break;
403                                 default:
404                                         predicate_correct = false;
405                                         model_print("unkown predicate token\n");
406                                         break;
407                         }
408                 }
409
410                 if (predicate_correct) {
411                         *curr_pred = branch;
412                         branch_found = true;
413                         break;
414                 }
415         }
416
417         return branch_found;
418 }
419
420 /* Infer predicate expressions, which are generated in FuncNode::generate_predicates */
421 void FuncNode::infer_predicates(FuncInst * next_inst, ModelAction * next_act,
422 HashTable<void *, ModelAction *, uintptr_t, 0> * loc_act_map,
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_base_score()
774 {
775         PredSetIter * it = predicate_leaves.iterator();
776         SnapVector<Predicate *> leaves;
777         while (it->hasNext()) {
778                 Predicate * pred = it->next();
779                 pred->set_weight(1);
780                 leaves.push_back(pred);
781         }
782
783         quickSort(&leaves, 0, leaves.size() - 1);
784
785         // assign scores for internal nodes;
786         while ( !leaves.empty() ) {
787                 Predicate * leaf = leaves.back();
788                 leaves.pop_back();
789
790                 Predicate * curr = leaf->get_parent();
791                 while (curr != NULL) {
792                         if (curr->get_weight() != 0) {
793                                 // Has been exlpored
794                                 break;
795                         }
796
797                         ModelVector<Predicate *> * children = curr->get_children();
798                         double weight_sum = 0;
799                         bool has_unassigned_node = false;
800
801                         for (uint i = 0; i < children->size(); i++) {
802                                 Predicate * child = (*children)[i];
803
804                                 // If a child has unassigned weight
805                                 double weight = child->get_weight();
806                                 if (weight == 0) {
807                                         has_unassigned_node = true;
808                                         break;
809                                 } else
810                                         weight_sum += weight;
811                         }
812
813                         if (!has_unassigned_node) {
814                                 double average_weight = (double) weight_sum / (double) children->size();
815                                 double weight = average_weight * pow(0.9, curr->get_depth());
816                                 curr->set_weight(weight);
817                         } else
818                                 break;
819
820                         curr = curr->get_parent();
821                 }
822         }
823 }
824
825 void FuncNode::print_predicate_tree()
826 {
827         model_print("digraph function_%s {\n", func_name);
828         predicate_tree_entry->print_pred_subtree();
829         predicate_tree_exit->print_predicate();
830         model_print("}\n");     // end of graph
831 }