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