909d4a8d1e891a72d4c9ae4ea0dc6f6fbd295a40
[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                                         equality = ((void*)next_read == NULL);
392                                         if (equality != pred_expression->value)
393                                                 predicate_correct = false;
394                                         break;
395                                 default:
396                                         predicate_correct = false;
397                                         model_print("unkown predicate token\n");
398                                         break;
399                         }
400                 }
401
402                 if (predicate_correct) {
403                         *curr_pred = branch;
404                         branch_found = true;
405                         break;
406                 }
407         }
408
409         return branch_found;
410 }
411
412 /* Infer predicate expressions, which are generated in FuncNode::generate_predicates */
413 void FuncNode::infer_predicates(FuncInst * next_inst, ModelAction * next_act,
414         HashTable<void *, ModelAction *, uintptr_t, 0> * loc_act_map,
415         SnapVector<struct half_pred_expr *> * half_pred_expressions)
416 {
417         void * loc = next_act->get_location();
418
419         if (next_inst->is_read()) {
420                 /* read + rmw */
421                 if ( loc_act_map->contains(loc) ) {
422                         ModelAction * last_act = loc_act_map->get(loc);
423                         FuncInst * last_inst = get_inst(last_act);
424                         struct half_pred_expr * expression = new half_pred_expr(EQUALITY, last_inst);
425                         half_pred_expressions->push_back(expression);
426                 } else if ( next_inst->is_single_location() ){
427                         loc_set_t * loc_may_equal = loc_may_equal_map->get(loc);
428
429                         if (loc_may_equal != NULL) {
430                                 loc_set_iter * loc_it = loc_may_equal->iterator();
431                                 while (loc_it->hasNext()) {
432                                         void * neighbor = loc_it->next();
433                                         if (loc_act_map->contains(neighbor)) {
434                                                 ModelAction * last_act = loc_act_map->get(neighbor);
435                                                 FuncInst * last_inst = get_inst(last_act);
436
437                                                 struct half_pred_expr * expression = new half_pred_expr(EQUALITY, last_inst);
438                                                 half_pred_expressions->push_back(expression);
439                                         }
440                                 }
441                         }
442                 } else {
443                         // next_inst is not single location
444                         uint64_t read_val = next_act->get_reads_from_value();
445
446                         // only infer NULLITY predicate when it is actually NULL.
447                         if ( (void*)read_val == NULL) {
448                                 struct half_pred_expr * expression = new half_pred_expr(NULLITY, NULL);
449                                 half_pred_expressions->push_back(expression);
450                         }
451                 }
452         } else {
453                 /* Pure writes */
454                 // TODO: do anything here?
455         }
456 }
457
458 /* Able to generate complex predicates when there are multiple predciate expressions */
459 void FuncNode::generate_predicates(Predicate ** curr_pred, FuncInst * next_inst,
460         SnapVector<struct half_pred_expr *> * half_pred_expressions)
461 {
462         if (half_pred_expressions->size() == 0) {
463                 Predicate * new_pred = new Predicate(next_inst);
464                 (*curr_pred)->add_child(new_pred);
465                 new_pred->set_parent(*curr_pred);
466
467                 /* entry predicates and predicates containing pure write actions
468                  * have no predicate expressions */
469                 if ( (*curr_pred)->is_entry_predicate() )
470                         new_pred->add_predicate_expr(NOPREDICATE, NULL, true);
471                 else if (next_inst->is_write()) {
472                         /* next_inst->is_write() <==> pure writes */
473                         new_pred->add_predicate_expr(NOPREDICATE, NULL, true);
474                 }
475
476                 return;
477         }
478
479         SnapVector<Predicate *> predicates;
480
481         struct half_pred_expr * half_expr = (*half_pred_expressions)[0];
482         predicates.push_back(new Predicate(next_inst));
483         predicates.push_back(new Predicate(next_inst));
484
485         predicates[0]->add_predicate_expr(half_expr->token, half_expr->func_inst, true);
486         predicates[1]->add_predicate_expr(half_expr->token, half_expr->func_inst, false);
487
488         for (uint i = 1; i < half_pred_expressions->size(); i++) {
489                 half_expr = (*half_pred_expressions)[i];
490
491                 uint old_size = predicates.size();
492                 for (uint j = 0; j < old_size; j++) {
493                         Predicate * pred = predicates[j];
494                         Predicate * new_pred = new Predicate(next_inst);
495                         new_pred->copy_predicate_expr(pred);
496
497                         pred->add_predicate_expr(half_expr->token, half_expr->func_inst, true);
498                         new_pred->add_predicate_expr(half_expr->token, half_expr->func_inst, false);
499
500                         predicates.push_back(new_pred);
501                 }
502         }
503
504         for (uint i = 0; i < predicates.size(); i++) {
505                 Predicate * pred= predicates[i];
506                 (*curr_pred)->add_child(pred);
507                 pred->set_parent(*curr_pred);
508         }
509
510         /* Free memories allocated by infer_predicate */
511         for (uint i = 0; i < half_pred_expressions->size(); i++) {
512                 struct half_pred_expr * tmp = (*half_pred_expressions)[i];
513                 snapshot_free(tmp);
514         }
515 }
516
517 /* Amend predicates that contain no predicate expressions. Currenlty only amend with NULLITY predicates */
518 bool FuncNode::amend_predicate_expr(Predicate ** curr_pred, FuncInst * next_inst, ModelAction * next_act)
519 {
520         // there should only be only child
521         Predicate * unset_pred = (*curr_pred)->get_children()->back();
522         uint64_t read_val = next_act->get_reads_from_value();
523
524         // only generate NULLITY predicate when it is actually NULL.
525         if ( !next_inst->is_single_location() && (void*)read_val == NULL ) {
526                 Predicate * new_pred = new Predicate(next_inst);
527
528                 (*curr_pred)->add_child(new_pred);
529                 new_pred->set_parent(*curr_pred);
530
531                 unset_pred->add_predicate_expr(NULLITY, NULL, false);
532                 new_pred->add_predicate_expr(NULLITY, NULL, true);
533
534                 return true;
535         }
536
537         return false;
538 }
539
540 void FuncNode::add_to_val_loc_map(uint64_t val, void * loc)
541 {
542         loc_set_t * locations = val_loc_map->get(val);
543
544         if (locations == NULL) {
545                 locations = new loc_set_t();
546                 val_loc_map->put(val, locations);
547         }
548
549         update_loc_may_equal_map(loc, locations);
550         locations->add(loc);
551         // values_may_read_from->add(val);
552 }
553
554 void FuncNode::add_to_val_loc_map(value_set_t * values, void * loc)
555 {
556         if (values == NULL)
557                 return;
558
559         value_set_iter * it = values->iterator();
560         while (it->hasNext()) {
561                 uint64_t val = it->next();
562                 add_to_val_loc_map(val, loc);
563         }
564 }
565
566 void FuncNode::update_loc_may_equal_map(void * new_loc, loc_set_t * old_locations)
567 {
568         if ( old_locations->contains(new_loc) )
569                 return;
570
571         loc_set_t * neighbors = loc_may_equal_map->get(new_loc);
572
573         if (neighbors == NULL) {
574                 neighbors = new loc_set_t();
575                 loc_may_equal_map->put(new_loc, neighbors);
576         }
577
578         loc_set_iter * loc_it = old_locations->iterator();
579         while (loc_it->hasNext()) {
580                 // new_loc: { old_locations, ... }
581                 void * member = loc_it->next();
582                 neighbors->add(member);
583
584                 // for each i in old_locations, i : { new_loc, ... }
585                 loc_set_t * _neighbors = loc_may_equal_map->get(member);
586                 if (_neighbors == NULL) {
587                         _neighbors = new loc_set_t();
588                         loc_may_equal_map->put(member, _neighbors);
589                 }
590                 _neighbors->add(new_loc);
591         }
592 }
593
594 /* Every time a thread enters a function, set its position to the predicate tree entry */
595 void FuncNode::init_predicate_tree_position(thread_id_t tid)
596 {
597         int thread_id = id_to_int(tid);
598         if (predicate_tree_position.size() <= (uint) thread_id)
599                 predicate_tree_position.resize(thread_id + 1);
600
601         predicate_tree_position[thread_id] = predicate_tree_entry;
602 }
603
604 void FuncNode::set_predicate_tree_position(thread_id_t tid, Predicate * pred)
605 {
606         int thread_id = id_to_int(tid);
607         predicate_tree_position[thread_id] = pred;
608 }
609
610 /* @return The position of a thread in a predicate tree */
611 Predicate * FuncNode::get_predicate_tree_position(thread_id_t tid)
612 {
613         int thread_id = id_to_int(tid);
614         return predicate_tree_position[thread_id];
615 }
616
617 /* Make sure elements of thrd_inst_act_map are initialized properly when threads enter functions */
618 void FuncNode::init_inst_act_map(thread_id_t tid)
619 {
620         int thread_id = id_to_int(tid);
621         SnapVector<inst_act_map_t *> * thrd_inst_act_map = history->getThrdInstActMap(func_id);
622         uint old_size = thrd_inst_act_map->size();
623
624         if (thrd_inst_act_map->size() <= (uint) thread_id) {
625                 uint new_size = thread_id + 1;
626                 thrd_inst_act_map->resize(new_size);
627
628                 for (uint i = old_size; i < new_size; i++)
629                         (*thrd_inst_act_map)[i] = new inst_act_map_t(128);
630         }
631 }
632
633 /* Reset elements of thrd_inst_act_map when threads exit functions */
634 void FuncNode::reset_inst_act_map(thread_id_t tid)
635 {
636         int thread_id = id_to_int(tid);
637         SnapVector<inst_act_map_t *> * thrd_inst_act_map = history->getThrdInstActMap(func_id);
638
639         inst_act_map_t * map = (*thrd_inst_act_map)[thread_id];
640         map->reset();
641 }
642
643 void FuncNode::update_inst_act_map(thread_id_t tid, ModelAction * read_act)
644 {
645         int thread_id = id_to_int(tid);
646         SnapVector<inst_act_map_t *> * thrd_inst_act_map = history->getThrdInstActMap(func_id);
647
648         inst_act_map_t * map = (*thrd_inst_act_map)[thread_id];
649         FuncInst * read_inst = get_inst(read_act);
650         map->put(read_inst, read_act);
651 }
652
653 inst_act_map_t * FuncNode::get_inst_act_map(thread_id_t tid)
654 {
655         int thread_id = id_to_int(tid);
656         SnapVector<inst_act_map_t *> * thrd_inst_act_map = history->getThrdInstActMap(func_id);
657
658         return (*thrd_inst_act_map)[thread_id];
659 }
660
661 /* Add FuncNodes that this node may follow */
662 void FuncNode::add_out_edge(FuncNode * other)
663 {
664         if ( !edge_table.contains(other) ) {
665                 edge_table.put(other, OUT_EDGE);
666                 out_edges.push_back(other);
667                 return;
668         }
669
670         edge_type_t edge = edge_table.get(other);
671         if (edge == IN_EDGE) {
672                 edge_table.put(other, BI_EDGE);
673                 out_edges.push_back(other);
674         }
675 }
676
677 /* Compute the distance between this FuncNode and the target node.
678  * Return -1 if the target node is unreachable or the actual distance
679  * is greater than max_step.
680  */
681 int FuncNode::compute_distance(FuncNode * target, int max_step)
682 {
683         if (target == NULL)
684                 return -1;
685         else if (target == this)
686                 return 0;
687
688         SnapList<FuncNode *> queue;
689         HashTable<FuncNode *, int, uintptr_t, 0> distances(128);
690
691         queue.push_back(this);
692         distances.put(this, 0);
693
694         while (!queue.empty()) {
695                 FuncNode * curr = queue.front();
696                 queue.pop_front();
697                 int dist = distances.get(curr);
698
699                 if (max_step <= dist)
700                         return -1;
701
702                 ModelList<FuncNode *> * outEdges = curr->get_out_edges();
703                 mllnode<FuncNode *> * it;
704                 for (it = outEdges->begin(); it != NULL; it = it->getNext()) {
705                         FuncNode * out_node = it->getVal();
706
707                         /* This node has not been visited before */
708                         if ( !distances.contains(out_node) ) {
709                                 if (out_node == target)
710                                         return dist + 1;
711
712                                 queue.push_back(out_node);
713                                 distances.put(out_node, dist + 1);
714                         }
715                 }
716         }
717
718         /* Target node is unreachable */
719         return -1;
720 }
721
722 void FuncNode::print_predicate_tree()
723 {
724         model_print("digraph function_%s {\n", func_name);
725         predicate_tree_entry->print_pred_subtree();
726         predicate_tree_exit->print_predicate();
727         model_print("}\n");     // end of graph
728 }
729
730 void FuncNode::print_val_loc_map()
731 {
732 /*
733         value_set_iter * val_it = values_may_read_from->iterator();
734         while (val_it->hasNext()) {
735                 uint64_t value = val_it->next();
736                 model_print("val %llx: ", value);
737
738                 loc_set_t * locations = val_loc_map->get(value);
739                 loc_set_iter * loc_it = locations->iterator();
740                 while (loc_it->hasNext()) {
741                         void * location = loc_it->next();
742                         model_print("%p ", location);
743                 }
744                 model_print("\n");
745         }
746 */
747 }