Fix warning
[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         edge_table(32),
20         out_edges()
21 {
22         predicate_tree_entry = new Predicate(NULL, true);
23         predicate_tree_entry->add_predicate_expr(NOPREDICATE, NULL, true);
24
25         predicate_tree_exit = new Predicate(NULL, false, true);
26         predicate_tree_exit->alloc_pre_exit_predicates();
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                 /* entry predicates and predicates containing pure write actions
476                  * have no predicate expressions */
477                 if ( (*curr_pred)->is_entry_predicate() )
478                         new_pred->add_predicate_expr(NOPREDICATE, NULL, true);
479                 else if (next_inst->is_write()) {
480                         /* next_inst->is_write() <==> pure writes */
481                         new_pred->add_predicate_expr(NOPREDICATE, NULL, true);
482                 }
483
484                 return;
485         }
486
487         SnapVector<Predicate *> predicates;
488
489         struct half_pred_expr * half_expr = (*half_pred_expressions)[0];
490         predicates.push_back(new Predicate(next_inst));
491         predicates.push_back(new Predicate(next_inst));
492
493         predicates[0]->add_predicate_expr(half_expr->token, half_expr->func_inst, true);
494         predicates[1]->add_predicate_expr(half_expr->token, half_expr->func_inst, false);
495
496         for (uint i = 1;i < half_pred_expressions->size();i++) {
497                 half_expr = (*half_pred_expressions)[i];
498
499                 uint old_size = predicates.size();
500                 for (uint j = 0;j < old_size;j++) {
501                         Predicate * pred = predicates[j];
502                         Predicate * new_pred = new Predicate(next_inst);
503                         new_pred->copy_predicate_expr(pred);
504
505                         pred->add_predicate_expr(half_expr->token, half_expr->func_inst, true);
506                         new_pred->add_predicate_expr(half_expr->token, half_expr->func_inst, false);
507
508                         predicates.push_back(new_pred);
509                 }
510         }
511
512         for (uint i = 0;i < predicates.size();i++) {
513                 Predicate * pred= predicates[i];
514                 (*curr_pred)->add_child(pred);
515                 pred->set_parent(*curr_pred);
516         }
517
518         /* Free memories allocated by infer_predicate */
519         for (uint i = 0;i < half_pred_expressions->size();i++) {
520                 struct half_pred_expr * tmp = (*half_pred_expressions)[i];
521                 snapshot_free(tmp);
522         }
523 }
524
525 /* Amend predicates that contain no predicate expressions. Currenlty only amend with NULLITY predicates */
526 bool FuncNode::amend_predicate_expr(Predicate ** curr_pred, FuncInst * next_inst, ModelAction * next_act)
527 {
528         // there should only be only child
529         Predicate * unset_pred = (*curr_pred)->get_children()->back();
530         uint64_t read_val = next_act->get_reads_from_value();
531
532         // only generate NULLITY predicate when it is actually NULL.
533         if ( !next_inst->is_single_location() && (void*)read_val == NULL ) {
534                 Predicate * new_pred = new Predicate(next_inst);
535
536                 (*curr_pred)->add_child(new_pred);
537                 new_pred->set_parent(*curr_pred);
538
539                 unset_pred->add_predicate_expr(NULLITY, NULL, false);
540                 new_pred->add_predicate_expr(NULLITY, NULL, true);
541
542                 return true;
543         }
544
545         return false;
546 }
547
548 void FuncNode::add_to_val_loc_map(uint64_t val, void * loc)
549 {
550         loc_set_t * locations = val_loc_map->get(val);
551
552         if (locations == NULL) {
553                 locations = new loc_set_t();
554                 val_loc_map->put(val, locations);
555         }
556
557         update_loc_may_equal_map(loc, locations);
558         locations->add(loc);
559         // values_may_read_from->add(val);
560 }
561
562 void FuncNode::add_to_val_loc_map(value_set_t * values, void * loc)
563 {
564         if (values == NULL)
565                 return;
566
567         value_set_iter * it = values->iterator();
568         while (it->hasNext()) {
569                 uint64_t val = it->next();
570                 add_to_val_loc_map(val, loc);
571         }
572 }
573
574 void FuncNode::update_loc_may_equal_map(void * new_loc, loc_set_t * old_locations)
575 {
576         if ( old_locations->contains(new_loc) )
577                 return;
578
579         loc_set_t * neighbors = loc_may_equal_map->get(new_loc);
580
581         if (neighbors == NULL) {
582                 neighbors = new loc_set_t();
583                 loc_may_equal_map->put(new_loc, neighbors);
584         }
585
586         loc_set_iter * loc_it = old_locations->iterator();
587         while (loc_it->hasNext()) {
588                 // new_loc: { old_locations, ... }
589                 void * member = loc_it->next();
590                 neighbors->add(member);
591
592                 // for each i in old_locations, i : { new_loc, ... }
593                 loc_set_t * _neighbors = loc_may_equal_map->get(member);
594                 if (_neighbors == NULL) {
595                         _neighbors = new loc_set_t();
596                         loc_may_equal_map->put(member, _neighbors);
597                 }
598                 _neighbors->add(new_loc);
599         }
600 }
601
602 /* Every time a thread enters a function, set its position to the predicate tree entry */
603 void FuncNode::init_predicate_tree_position(thread_id_t tid)
604 {
605         int thread_id = id_to_int(tid);
606         if (predicate_tree_position.size() <= (uint) thread_id)
607                 predicate_tree_position.resize(thread_id + 1);
608
609         predicate_tree_position[thread_id] = predicate_tree_entry;
610 }
611
612 void FuncNode::set_predicate_tree_position(thread_id_t tid, Predicate * pred)
613 {
614         int thread_id = id_to_int(tid);
615         predicate_tree_position[thread_id] = pred;
616 }
617
618 /* @return The position of a thread in a predicate tree */
619 Predicate * FuncNode::get_predicate_tree_position(thread_id_t tid)
620 {
621         int thread_id = id_to_int(tid);
622         return predicate_tree_position[thread_id];
623 }
624
625 /* Make sure elements of thrd_inst_act_map are initialized properly when threads enter functions */
626 void FuncNode::init_inst_act_map(thread_id_t tid)
627 {
628         int thread_id = id_to_int(tid);
629         SnapVector<inst_act_map_t *> * thrd_inst_act_map = history->getThrdInstActMap(func_id);
630         uint old_size = thrd_inst_act_map->size();
631
632         if (thrd_inst_act_map->size() <= (uint) thread_id) {
633                 uint new_size = thread_id + 1;
634                 thrd_inst_act_map->resize(new_size);
635
636                 for (uint i = old_size;i < new_size;i++)
637                         (*thrd_inst_act_map)[i] = new inst_act_map_t(128);
638         }
639 }
640
641 /* Reset elements of thrd_inst_act_map when threads exit functions */
642 void FuncNode::reset_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
647         inst_act_map_t * map = (*thrd_inst_act_map)[thread_id];
648         map->reset();
649 }
650
651 void FuncNode::update_inst_act_map(thread_id_t tid, ModelAction * read_act)
652 {
653         int thread_id = id_to_int(tid);
654         SnapVector<inst_act_map_t *> * thrd_inst_act_map = history->getThrdInstActMap(func_id);
655
656         inst_act_map_t * map = (*thrd_inst_act_map)[thread_id];
657         FuncInst * read_inst = get_inst(read_act);
658         map->put(read_inst, read_act);
659 }
660
661 inst_act_map_t * FuncNode::get_inst_act_map(thread_id_t tid)
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         return (*thrd_inst_act_map)[thread_id];
667 }
668
669 /* Add FuncNodes that this node may follow */
670 void FuncNode::add_out_edge(FuncNode * other)
671 {
672         if ( !edge_table.contains(other) ) {
673                 edge_table.put(other, OUT_EDGE);
674                 out_edges.push_back(other);
675                 return;
676         }
677
678         edge_type_t edge = edge_table.get(other);
679         if (edge == IN_EDGE) {
680                 edge_table.put(other, BI_EDGE);
681                 out_edges.push_back(other);
682         }
683 }
684
685 /* Compute the distance between this FuncNode and the target node.
686  * Return -1 if the target node is unreachable or the actual distance
687  * is greater than max_step.
688  */
689 int FuncNode::compute_distance(FuncNode * target, int max_step)
690 {
691         if (target == NULL)
692                 return -1;
693         else if (target == this)
694                 return 0;
695
696         SnapList<FuncNode *> queue;
697         HashTable<FuncNode *, int, uintptr_t, 0> distances(128);
698
699         queue.push_back(this);
700         distances.put(this, 0);
701
702         while (!queue.empty()) {
703                 FuncNode * curr = queue.front();
704                 queue.pop_front();
705                 int dist = distances.get(curr);
706
707                 if (max_step <= dist)
708                         return -1;
709
710                 ModelList<FuncNode *> * outEdges = curr->get_out_edges();
711                 mllnode<FuncNode *> * it;
712                 for (it = outEdges->begin();it != NULL;it = it->getNext()) {
713                         FuncNode * out_node = it->getVal();
714
715                         /* This node has not been visited before */
716                         if ( !distances.contains(out_node) ) {
717                                 if (out_node == target)
718                                         return dist + 1;
719
720                                 queue.push_back(out_node);
721                                 distances.put(out_node, dist + 1);
722                         }
723                 }
724         }
725
726         /* Target node is unreachable */
727         return -1;
728 }
729
730 void FuncNode::assign_base_score()
731 {
732         SnapVector<Predicate *> leaves;
733         SnapList<Predicate *> queue;
734         queue.push_front(predicate_tree_entry);
735
736         // assign leaf score
737         while ( !queue.empty() ) {
738                 Predicate * node = queue.back();
739                 queue.pop_back();
740
741                 ModelVector<Predicate *> * children = node->get_children();
742                 if (children->empty()) {
743                         node->set_weight(1);
744                         leaves.push_back(node);
745                 }
746
747                 for (uint i = 0;i < children->size();i++)
748                         queue.push_front( (*children)[i] );
749         }
750
751         // assign scores for internal nodes;
752         while ( !leaves.empty() ) {
753                 Predicate * leaf = leaves.back();
754                 leaves.pop_back();
755
756                 Predicate * curr = leaf->get_parent();
757                 while (curr != NULL) {
758                         if (curr->get_weight() != 0) {
759                                 // Has been exlpored
760                                 break;
761                         }
762
763                         ModelVector<Predicate *> * children = curr->get_children();
764                         double weight_sum = 0;
765                         bool has_unassigned_node = false;
766
767                         for (uint i = 0;i < children->size();i++) {
768                                 Predicate * child = (*children)[i];
769
770                                 // If a child has unassigned weight
771                                 double weight = child->get_weight();
772                                 if (weight == 0) {
773                                         has_unassigned_node = true;
774                                         break;
775                                 } else
776                                         weight_sum += weight;
777                         }
778
779                         if (!has_unassigned_node) {
780                                 double average_weight = (double) weight_sum / (double) children->size();
781                                 double weight = average_weight * pow(0.9, curr->get_depth());
782                                 curr->set_weight(weight);
783                         } else
784                                 break;
785
786                         curr = curr->get_parent();
787                 }
788         }
789 }
790
791 void FuncNode::print_predicate_tree()
792 {
793         model_print("digraph function_%s {\n", func_name);
794         predicate_tree_entry->print_pred_subtree();
795         predicate_tree_exit->print_predicate();
796         model_print("}\n");     // end of graph
797 }