ae525d88e3affd5c677636facd238a93808a4a54
[libfirm] / ir / ana / callgraph.c
1 /*
2  * Copyright (C) 1995-2008 University of Karlsruhe.  All right reserved.
3  *
4  * This file is part of libFirm.
5  *
6  * This file may be distributed and/or modified under the terms of the
7  * GNU General Public License version 2 as published by the Free Software
8  * Foundation and appearing in the file LICENSE.GPL included in the
9  * packaging of this file.
10  *
11  * Licensees holding valid libFirm Professional Edition licenses may use
12  * this file in accordance with the libFirm Commercial License.
13  * Agreement provided with the Software.
14  *
15  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
16  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17  * PURPOSE.
18  */
19
20 /**
21  * @file
22  * @brief       Representation and computation of the callgraph.
23  * @author      Goetz Lindenmaier
24  * @date        21.7.2004
25  * @version     $Id$
26  */
27 #include "config.h"
28
29 #include <string.h>
30 #include <stdlib.h>
31
32 #include "callgraph.h"
33
34 #include "irloop_t.h"
35 #include "irprog_t.h"
36 #include "irgraph_t.h"
37 #include "irnode_t.h"
38
39 #include "cgana.h"
40 #include "execution_frequency.h"
41
42 #include "array.h"
43 #include "pmap.h"
44 #include "hashptr.h"
45 #include "raw_bitset.h"
46
47 #include "irgwalk.h"
48
49 static ir_visited_t master_cg_visited = 0;
50 static inline int cg_irg_visited      (ir_graph *n);
51 static inline void mark_cg_irg_visited(ir_graph *n);
52 static inline void set_cg_irg_visited (ir_graph *n, ir_visited_t i);
53
54 /** Returns the callgraph state of the program representation. */
55 irp_callgraph_state get_irp_callgraph_state(void)
56 {
57         return irp->callgraph_state;
58 }
59
60 /* Sets the callgraph state of the program representation. */
61 void set_irp_callgraph_state(irp_callgraph_state s)
62 {
63         irp->callgraph_state = s;
64 }
65
66 /* Returns the number of procedures that call the given irg. */
67 int get_irg_n_callers(const ir_graph *irg)
68 {
69         if (irg->callers) return ARR_LEN(irg->callers);
70         return -1;
71 }
72
73 /* Returns the caller at position pos. */
74 ir_graph *get_irg_caller(const ir_graph *irg, int pos)
75 {
76         assert(pos >= 0 && pos < get_irg_n_callers(irg));
77         if (irg->callers) return irg->callers[pos];
78         return NULL;
79 }
80
81 /* Returns non-zero if the caller at position pos is "a backedge", i.e. a recursion. */
82 int is_irg_caller_backedge(const ir_graph *irg, int pos)
83 {
84         assert(pos >= 0 && pos < get_irg_n_callers(irg));
85         return irg->caller_isbe != NULL ? rbitset_is_set(irg->caller_isbe, pos) : 0;
86 }
87
88 /** Search the caller in the list of all callers and set it's backedge property. */
89 static void set_irg_caller_backedge(ir_graph *irg, ir_graph *caller)
90 {
91         int i, n_callers = get_irg_n_callers(irg);
92
93         /* allocate a new array on demand */
94         if (irg->caller_isbe == NULL)
95                 irg->caller_isbe = rbitset_malloc(n_callers);
96         for (i = 0; i < n_callers; ++i) {
97                 if (get_irg_caller(irg, i) == caller) {
98                         rbitset_set(irg->caller_isbe, i);
99                         break;
100                 }
101         }
102 }
103
104 /* Returns non-zero if the irg has a backedge caller. */
105 int has_irg_caller_backedge(const ir_graph *irg)
106 {
107         int i, n_callers = get_irg_n_callers(irg);
108
109         if (irg->caller_isbe != NULL) {
110                 for (i = 0; i < n_callers; ++i)
111                         if (rbitset_is_set(irg->caller_isbe, i))
112                                 return 1;
113         }
114         return 0;
115 }
116
117 /**
118  * Find the reversion position of a caller.
119  * Given the position pos_caller of an caller of irg, return
120  * irg's callee position on that caller.
121  */
122 static int reverse_pos(const ir_graph *callee, int pos_caller)
123 {
124         ir_graph *caller = get_irg_caller(callee, pos_caller);
125         /* search the other relation for the corresponding edge. */
126         int pos_callee = -1;
127         int i, n_callees = get_irg_n_callees(caller);
128         for (i = 0; i < n_callees; ++i) {
129                 if (get_irg_callee(caller, i) == callee) {
130                         pos_callee = i;
131                         break;
132                 }
133         }
134
135         assert(pos_callee >= 0);
136
137         return pos_callee;
138 }
139
140 /* Returns the maximal loop depth of call nodes that call along this edge. */
141 int get_irg_caller_loop_depth(const ir_graph *irg, int pos)
142 {
143         ir_graph *caller     = get_irg_caller(irg, pos);
144         int       pos_callee = reverse_pos(irg, pos);
145
146         return get_irg_callee_loop_depth(caller, pos_callee);
147 }
148
149
150 /* Returns the number of procedures that are called by the given irg. */
151 int get_irg_n_callees(const ir_graph *irg)
152 {
153         if (irg->callees) return ARR_LEN(irg->callees);
154         return -1;
155 }
156
157 /* Returns the callee at position pos. */
158 ir_graph *get_irg_callee(const ir_graph *irg, int pos)
159 {
160         assert(pos >= 0 && pos < get_irg_n_callees(irg));
161         if (irg->callees) return irg->callees[pos]->irg;
162         return NULL;
163 }
164
165 /* Returns non-zero if the callee at position pos is "a backedge", i.e. a recursion. */
166 int is_irg_callee_backedge(const ir_graph *irg, int pos)
167 {
168         assert(pos >= 0 && pos < get_irg_n_callees(irg));
169         return irg->callee_isbe != NULL ? rbitset_is_set(irg->callee_isbe, pos) : 0;
170 }
171
172 /* Returns non-zero if the irg has a backedge callee. */
173 int has_irg_callee_backedge(const ir_graph *irg)
174 {
175         int i, n_callees = get_irg_n_callees(irg);
176
177         if (irg->callee_isbe != NULL) {
178                 for (i = 0; i < n_callees; ++i)
179                         if (rbitset_is_set(irg->callee_isbe, i))
180                                 return 1;
181         }
182         return 0;
183 }
184
185 /**
186  * Mark the callee at position pos as a backedge.
187  */
188 static void set_irg_callee_backedge(ir_graph *irg, int pos)
189 {
190         int n = get_irg_n_callees(irg);
191
192         /* allocate a new array on demand */
193         if (irg->callee_isbe == NULL)
194                 irg->callee_isbe = rbitset_malloc(n);
195         assert(pos >= 0 && pos < n);
196         rbitset_set(irg->callee_isbe, pos);
197 }
198
199 /* Returns the maximal loop depth of call nodes that call along this edge. */
200 int get_irg_callee_loop_depth(const ir_graph *irg, int pos)
201 {
202         assert(pos >= 0 && pos < get_irg_n_callees(irg));
203         if (irg->callees) return irg->callees[pos]->max_depth;
204         return -1;
205 }
206
207 static double get_irg_callee_execution_frequency(const ir_graph *irg, int pos)
208 {
209         ir_node **arr = irg->callees[pos]->call_list;
210         int i, n_Calls = ARR_LEN(arr);
211         double freq = 0.0;
212
213         for (i = 0; i < n_Calls; ++i) {
214                 freq += get_irn_exec_freq(arr[i]);
215         }
216         return freq;
217 }
218
219 static double get_irg_callee_method_execution_frequency(const ir_graph *irg,
220                                                         int pos)
221 {
222         double call_freq = get_irg_callee_execution_frequency(irg, pos);
223         double meth_freq = get_irg_method_execution_frequency(irg);
224         return call_freq * meth_freq;
225 }
226
227 static double get_irg_caller_method_execution_frequency(const ir_graph *irg,
228                                                         int pos)
229 {
230         ir_graph *caller     = get_irg_caller(irg, pos);
231         int       pos_callee = reverse_pos(irg, pos);
232
233         return get_irg_callee_method_execution_frequency(caller, pos_callee);
234 }
235
236
237 /* --------------------- Compute the callgraph ------------------------ */
238
239 /**
240  * Walker called by compute_callgraph(), analyses all Call nodes.
241  */
242 static void ana_Call(ir_node *n, void *env)
243 {
244         int i, n_callees;
245         ir_graph *irg;
246         (void) env;
247
248         if (! is_Call(n)) return;
249
250         irg = get_irn_irg(n);
251         n_callees = get_Call_n_callees(n);
252         for (i = 0; i < n_callees; ++i) {
253                 ir_entity *callee_e = get_Call_callee(n, i);
254                 ir_graph *callee = get_entity_irg(callee_e);
255
256                 if (callee) {
257                         cg_callee_entry buf;
258                         cg_callee_entry *found;
259                         int depth;
260
261                         buf.irg = callee;
262
263                         pset_insert((pset *)callee->callers, irg, HASH_PTR(irg));
264                         found = pset_find((pset *)irg->callees, &buf, HASH_PTR(callee));
265                         if (found) {  /* add Call node to list, compute new nesting. */
266                                 ir_node **arr = found->call_list;
267                                 ARR_APP1(ir_node *, arr, n);
268                                 found->call_list = arr;
269                         } else { /* New node, add Call node and init nesting. */
270                                 found = OALLOC(irg->obst, cg_callee_entry);
271                                 found->irg = callee;
272                                 found->call_list = NEW_ARR_F(ir_node *, 1);
273                                 found->call_list[0] = n;
274                                 found->max_depth = 0;
275                                 pset_insert((pset *)irg->callees, found, HASH_PTR(callee));
276                         }
277                         depth = get_loop_depth(get_irn_loop(get_nodes_block(n)));
278                         found->max_depth = (depth > found->max_depth) ? depth : found->max_depth;
279                 }
280         }
281 }
282
283 /** compare two ir graphs in a cg_callee_entry */
284 static int cg_callee_entry_cmp(const void *elt, const void *key)
285 {
286         const cg_callee_entry *e1 = elt;
287         const cg_callee_entry *e2 = key;
288         return e1->irg != e2->irg;
289 }
290
291 /** compare two ir graphs for pointer identity */
292 static int graph_cmp(const void *elt, const void *key)
293 {
294         const ir_graph *e1 = elt;
295         const ir_graph *e2 = key;
296         return e1 != e2;
297 }
298
299
300 /* Construct and destruct the callgraph. */
301 void compute_callgraph(void)
302 {
303         int i, n_irgs;
304
305 #ifdef INTERPROCEDURAL_VIEW
306         assert(! get_interprocedural_view());  /* Else walking will not reach the Call nodes. */
307 #endif
308
309         /* initialize */
310         free_callgraph();
311
312         n_irgs = get_irp_n_irgs();
313         for (i = 0; i < n_irgs; ++i) {
314                 ir_graph *irg = get_irp_irg(i);
315                 assert(get_irg_callee_info_state(irg) == irg_callee_info_consistent);
316                 irg->callees = (cg_callee_entry **)new_pset(cg_callee_entry_cmp, 8);
317                 irg->callers = (ir_graph **)new_pset(graph_cmp, 8);
318                 //construct_cf_backedges(irg);
319         }
320
321         /* Compute the call graph */
322         for (i = 0; i < n_irgs; ++i) {
323                 ir_graph *irg = get_irp_irg(i);
324                 construct_cf_backedges(irg);   // We also find the maximal loop depth of a call.
325                 irg_walk_graph(irg, ana_Call, NULL, NULL);
326         }
327
328         /* Change the sets to arrays. */
329         for (i = 0; i < n_irgs; ++i) {
330                 int j, count;
331                 cg_callee_entry *callee;
332                 ir_graph *c, *irg = get_irp_irg(i);
333                 pset *callee_set, *caller_set;
334
335                 callee_set = (pset *)irg->callees;
336                 count = pset_count(callee_set);
337                 irg->callees = NEW_ARR_F(cg_callee_entry *, count);
338                 irg->callee_isbe = NULL;
339                 callee = pset_first(callee_set);
340                 for (j = 0; j < count; ++j) {
341                         irg->callees[j] = callee;
342                         callee = pset_next(callee_set);
343                 }
344                 del_pset(callee_set);
345                 assert(callee == NULL);
346
347                 caller_set = (pset *)irg->callers;
348                 count = pset_count(caller_set);
349                 irg->callers = NEW_ARR_F(ir_graph *, count);
350                 irg->caller_isbe =  NULL;
351                 c = pset_first(caller_set);
352                 for (j = 0; j < count; ++j) {
353                         irg->callers[j] = c;
354                         c = pset_next(caller_set);
355                 }
356                 del_pset(caller_set);
357                 assert(c == NULL);
358         }
359         set_irp_callgraph_state(irp_callgraph_consistent);
360 }
361
362 /* Destruct the callgraph. */
363 void free_callgraph(void)
364 {
365         int i, n_irgs = get_irp_n_irgs();
366         for (i = 0; i < n_irgs; ++i) {
367                 ir_graph *irg = get_irp_irg(i);
368                 if (irg->callees) DEL_ARR_F(irg->callees);
369                 if (irg->callers) DEL_ARR_F(irg->callers);
370                 if (irg->callee_isbe) free(irg->callee_isbe);
371                 if (irg->caller_isbe) free(irg->caller_isbe);
372                 irg->callees = NULL;
373                 irg->callers = NULL;
374                 irg->callee_isbe = NULL;
375                 irg->caller_isbe = NULL;
376         }
377         set_irp_callgraph_state(irp_callgraph_none);
378 }
379
380 /* ----------------------------------------------------------------------------------- */
381 /* A walker for the callgraph                                                          */
382 /* ----------------------------------------------------------------------------------- */
383
384
385 static void do_walk(ir_graph *irg, callgraph_walk_func *pre, callgraph_walk_func *post, void *env)
386 {
387         int i, n_callees;
388
389         if (cg_irg_visited(irg))
390                 return;
391         mark_cg_irg_visited(irg);
392
393         if (pre)
394                 pre(irg, env);
395
396         n_callees = get_irg_n_callees(irg);
397         for (i = 0; i < n_callees; i++) {
398                 ir_graph *m = get_irg_callee(irg, i);
399                 do_walk(m, pre, post, env);
400         }
401
402         if (post)
403                 post(irg, env);
404 }
405
406 void callgraph_walk(callgraph_walk_func *pre, callgraph_walk_func *post, void *env)
407 {
408         int i, n_irgs = get_irp_n_irgs();
409         ++master_cg_visited;
410
411         /* roots are methods which have no callers in the current program */
412         for (i = 0; i < n_irgs; ++i) {
413                 ir_graph *irg = get_irp_irg(i);
414
415                 if (get_irg_n_callers(irg) == 0)
416                         do_walk(irg, pre, post, env);
417         }
418
419         /* in case of unreachable call loops we haven't visited some irgs yet */
420         for (i = 0; i < n_irgs; i++) {
421                 ir_graph *irg = get_irp_irg(i);
422                 do_walk(irg, pre, post, env);
423         }
424 }
425
426 /* ----------------------------------------------------------------------------------- */
427 /* loop construction algorithm                                                         */
428 /* ----------------------------------------------------------------------------------- */
429
430 static ir_graph *outermost_ir_graph;   /**< The outermost graph the scc is computed
431                                             for */
432 static ir_loop *current_loop;      /**< Current cfloop construction is working
433                                         on. */
434 static int loop_node_cnt = 0;      /**< Counts the number of allocated cfloop nodes.
435                                         Each cfloop node gets a unique number.
436                                         What for? ev. remove. @@@ */
437 static int current_dfn = 1;        /**< Counter to generate depth first numbering
438                                         of visited nodes.  */
439
440 /*-----------------*/
441 /* Node attributes */
442 /*-----------------*/
443
444 typedef struct scc_info {
445         int in_stack;          /**< Marks whether node is on the stack. */
446         int dfn;               /**< Depth first search number. */
447         int uplink;            /**< dfn number of ancestor. */
448         int visited;
449 } scc_info;
450
451 /**
452  * allocates a new scc_info on the obstack
453  */
454 static inline scc_info *new_scc_info(struct obstack *obst)
455 {
456         return OALLOCZ(obst, scc_info);
457 }
458
459 /**
460  * Returns non-zero if a graph was already visited.
461  */
462 static inline int cg_irg_visited(ir_graph *irg)
463 {
464         return irg->self_visited >= master_cg_visited;
465 }
466
467 /**
468  * Marks a graph as visited.
469  */
470 static inline void mark_cg_irg_visited(ir_graph *irg)
471 {
472         irg->self_visited = master_cg_visited;
473 }
474
475 /**
476  * Set a graphs visited flag to i.
477  */
478 static inline void set_cg_irg_visited(ir_graph *irg, ir_visited_t i)
479 {
480         irg->self_visited = i;
481 }
482
483 /**
484  * Returns the visited flag of a graph.
485  */
486 static inline ir_visited_t get_cg_irg_visited(ir_graph *irg)
487 {
488         return irg->self_visited;
489 }
490
491 static inline void mark_irg_in_stack(ir_graph *irg)
492 {
493         scc_info *info = get_irg_link(irg);
494         assert(info && "missing call to init_scc()");
495         info->in_stack = 1;
496 }
497
498 static inline void mark_irg_not_in_stack(ir_graph *irg)
499 {
500         scc_info *info = get_irg_link(irg);
501         assert(info && "missing call to init_scc()");
502         info->in_stack = 0;
503 }
504
505 static inline int irg_is_in_stack(ir_graph *irg)
506 {
507         scc_info *info = get_irg_link(irg);
508         assert(info && "missing call to init_scc()");
509         return info->in_stack;
510 }
511
512 static inline void set_irg_uplink(ir_graph *irg, int uplink)
513 {
514         scc_info *info = get_irg_link(irg);
515         assert(info && "missing call to init_scc()");
516         info->uplink = uplink;
517 }
518
519 static inline int get_irg_uplink(ir_graph *irg)
520 {
521         scc_info *info = get_irg_link(irg);
522         assert(info && "missing call to init_scc()");
523         return info->uplink;
524 }
525
526 static inline void set_irg_dfn(ir_graph *irg, int dfn)
527 {
528         scc_info *info = get_irg_link(irg);
529         assert(info && "missing call to init_scc()");
530         info->dfn = dfn;
531 }
532
533 static inline int get_irg_dfn(ir_graph *irg)
534 {
535         scc_info *info = get_irg_link(irg);
536         assert(info && "missing call to init_scc()");
537         return info->dfn;
538 }
539
540 /**********************************************************************/
541 /* A stack.                                                          **/
542 /**********************************************************************/
543
544 static ir_graph **stack = NULL;
545 static int tos = 0;                /**< top of stack */
546
547 /**
548  * Initialize the irg stack.
549  */
550 static inline void init_stack(void)
551 {
552         if (stack) {
553                 ARR_RESIZE(ir_graph *, stack, 1000);
554         } else {
555                 stack = NEW_ARR_F(ir_graph *, 1000);
556         }
557         tos = 0;
558 }
559
560 /**
561  * push a graph on the irg stack
562  * @param n the graph to be pushed
563  */
564 static inline void push(ir_graph *irg)
565 {
566         if (tos == ARR_LEN(stack)) {
567                 int nlen = ARR_LEN(stack) * 2;
568                 ARR_RESIZE(ir_node *, stack, nlen);
569         }
570         stack [tos++] = irg;
571         mark_irg_in_stack(irg);
572 }
573
574 /**
575  * return the topmost graph on the stack and pop it
576  */
577 static inline ir_graph *pop(void)
578 {
579         ir_graph *irg = stack[--tos];
580         mark_irg_not_in_stack(irg);
581         return irg;
582 }
583
584 /**
585  * The nodes up to irg belong to the current loop.
586  * Removes them from the stack and adds them to the current loop.
587  */
588 static inline void pop_scc_to_loop(ir_graph *irg)
589 {
590         ir_graph *m;
591
592         do {
593                 m = pop();
594                 loop_node_cnt++;
595                 set_irg_dfn(m, loop_node_cnt);
596                 add_loop_irg(current_loop, m);
597                 m->l = current_loop;
598                 //m->callgraph_loop_depth = current_loop->depth;
599         } while (m != irg);
600 }
601
602 /* GL ??? my last son is my grandson???  Removes cfloops with no
603    ir_nodes in them.  Such loops have only another loop as son. (Why
604    can't they have two loops as sons? Does it never get that far? ) */
605 static void close_loop(ir_loop *l)
606 {
607         int last = get_loop_n_elements(l) - 1;
608         loop_element lelement = get_loop_element(l, last);
609         ir_loop *last_son = lelement.son;
610
611         if (get_kind(last_son) == k_ir_loop &&
612             get_loop_n_elements(last_son) == 1) {
613                 ir_loop *gson;
614
615                 lelement = get_loop_element(last_son, 0);
616                 gson = lelement.son;
617                 if (get_kind(gson) == k_ir_loop) {
618                         loop_element new_last_son;
619
620                         gson->outer_loop = l;
621                         new_last_son.son = gson;
622                         l->children[last] = new_last_son;
623                 }
624         }
625         current_loop = l;
626 }
627
628 /**
629  * Removes and unmarks all nodes up to n from the stack.
630  * The nodes must be visited once more to assign them to a scc.
631  */
632 static inline void pop_scc_unmark_visit(ir_graph *n)
633 {
634         ir_graph *m = NULL;
635
636         while (m != n) {
637                 m = pop();
638                 set_cg_irg_visited(m, 0);
639         }
640 }
641
642 /**********************************************************************/
643 /* The loop data structure.                                          **/
644 /**********************************************************************/
645
646 /**
647  * Allocates a new loop as son of current_loop.  Sets current_loop
648  * to the new loop and returns the father.
649  */
650 static ir_loop *new_loop(void)
651 {
652         ir_loop *father = current_loop;
653         ir_loop *son    = alloc_loop(father, outermost_ir_graph->obst);
654
655         current_loop = son;
656         return father;
657 }
658
659
660 /**********************************************************************/
661 /* Constructing and destructing the loop/backedge information.       **/
662 /**********************************************************************/
663
664 /* Initialization steps. **********************************************/
665
666 static void init_scc(struct obstack *obst)
667 {
668         int i;
669         int n_irgs;
670
671         current_dfn   = 1;
672         loop_node_cnt = 0;
673         init_stack();
674
675         n_irgs = get_irp_n_irgs();
676         for (i = 0; i < n_irgs; ++i) {
677                 ir_graph *irg = get_irp_irg(i);
678                 set_irg_link(irg, new_scc_info(obst));
679                 irg->callgraph_recursion_depth = 0;
680                 irg->callgraph_loop_depth      = 0;
681         }
682 }
683
684 /** Returns non-zero if n is a loop header, i.e., it is a Block node
685  *  and has predecessors within the cfloop and out of the cfloop.
686  *
687  *  @param root: only needed for assertion.
688  */
689 static int is_head(ir_graph *n, ir_graph *root)
690 {
691         int i, arity;
692         int some_outof_loop = 0, some_in_loop = 0;
693
694         arity = get_irg_n_callees(n);
695         for (i = 0; i < arity; i++) {
696                 ir_graph *pred = get_irg_callee(n, i);
697                 if (is_irg_callee_backedge(n, i)) continue;
698                 if (!irg_is_in_stack(pred)) {
699                         some_outof_loop = 1;
700                 } else {
701                         if (get_irg_uplink(pred) < get_irg_uplink(root))  {
702                                 assert(get_irg_uplink(pred) >= get_irg_uplink(root));
703                         }
704                         some_in_loop = 1;
705                 }
706         }
707
708         return some_outof_loop && some_in_loop;
709 }
710
711 /**
712  * Returns non-zero if n is possible loop head of an endless loop.
713  * I.e., it is a Block, Phi or Filter node and has only predecessors
714  * within the loop.
715  * @arg root: only needed for assertion.
716  */
717 static int is_endless_head(ir_graph *n, ir_graph *root)
718 {
719         int i, arity;
720         int some_outof_loop = 0, some_in_loop = 0;
721
722         arity = get_irg_n_callees(n);
723         for (i = 0; i < arity; i++) {
724                 ir_graph *pred = get_irg_callee(n, i);
725                 assert(pred);
726                 if (is_irg_callee_backedge(n, i))
727                         continue;
728                 if (!irg_is_in_stack(pred)) {
729                         some_outof_loop = 1;
730                 } else {
731                         if (get_irg_uplink(pred) < get_irg_uplink(root)) {
732                                 assert(get_irg_uplink(pred) >= get_irg_uplink(root));
733                         }
734                         some_in_loop = 1;
735                 }
736         }
737         return !some_outof_loop && some_in_loop;
738 }
739
740 #ifdef INTERPROCEDURAL_VIEW
741 /**
742  * Check whether there is a parallel edge in the ip control flow.
743  * Only
744  */
745 static int is_ip_head(ir_graph *n, ir_graph *pred)
746 {
747         int is_be = 0;
748
749         int iv_rem = get_interprocedural_view();
750         set_interprocedural_view(1);
751         {
752                 ir_node *sblock = get_irg_start_block(n);
753                 int i, arity = get_Block_n_cfgpreds(sblock);
754
755                 //printf(" edge from "); DDMG(n);
756                 //printf(" to pred   "); DDMG(pred);
757                 //printf(" sblock    "); DDMN(sblock);
758
759                 for (i = 0; i < arity; i++) {
760                         ir_node *pred_cfop = skip_Proj(get_Block_cfgpred(sblock, i));
761                         //printf("  "); DDMN(pred_cfop);
762                         if (is_CallBegin(pred_cfop)) { /* could be Unknown */
763                                 ir_graph *ip_pred = get_irn_irg(pred_cfop);
764                                 //printf("   "); DDMG(ip_pred);
765                                 if ((ip_pred == pred) && is_backedge(sblock, i)) {
766                                         //printf("   found\n");
767                                         is_be = 1;
768                                 }
769                         }
770                 }
771         }
772         set_interprocedural_view(iv_rem);
773         return is_be;
774 }
775 #endif /* INTERPROCEDURAL_VIEW */
776
777 /**
778  * Returns index of the predecessor with the smallest dfn number
779  * greater-equal than limit.
780  */
781 static int smallest_dfn_pred(ir_graph *n, int limit)
782 {
783         int i, index = -2, min = -1;
784
785         int arity = get_irg_n_callees(n);
786         for (i = 0; i < arity; i++) {
787                 ir_graph *pred = get_irg_callee(n, i);
788                 if (is_irg_callee_backedge(n, i) || !irg_is_in_stack(pred))
789                         continue;
790                 if (get_irg_dfn(pred) >= limit && (min == -1 || get_irg_dfn(pred) < min)) {
791                         index = i;
792                         min = get_irg_dfn(pred);
793                 }
794         }
795
796         return index;
797 }
798
799 /** Returns index of the predecessor with the largest dfn number. */
800 static int largest_dfn_pred(ir_graph *n)
801 {
802         int i, index = -2, max = -1;
803
804         int arity = get_irg_n_callees(n);
805         for (i = 0; i < arity; ++i) {
806                 ir_graph *pred = get_irg_callee(n, i);
807                 if (is_irg_callee_backedge (n, i) || !irg_is_in_stack(pred)) continue;
808                 if (get_irg_dfn(pred) > max) {
809                         index = i;
810                         max = get_irg_dfn(pred);
811                 }
812         }
813
814         return index;
815 }
816
817 #ifndef INTERPROCEDURAL_VIEW
818 static ir_graph *find_tail(ir_graph *n)
819 {
820         ir_graph *m;
821         int i, res_index = -2;
822
823         /*
824         if (!icfg && rm_cyclic_phis && remove_cyclic_phis (n)) return NULL;
825         */
826         m = stack[tos-1];  /* tos = top of stack */
827         if (is_head (m, n)) {
828                 res_index = smallest_dfn_pred(m, 0);
829                 if ((res_index == -2) &&  /* no smallest dfn pred found. */
830                         (n == m))
831                         return NULL;
832         } else {
833                 if (m == n) return NULL;    // Is this to catch Phi - self loops?
834                 for (i = tos-2; i >= 0; --i) {
835                         m = stack[i];
836
837                         if (is_head(m, n)) {
838                                 res_index = smallest_dfn_pred(m, get_irg_dfn(m) + 1);
839                                 if (res_index == -2)  /* no smallest dfn pred found. */
840                                         res_index = largest_dfn_pred(m);
841
842                                 if ((m == n) && (res_index == -2)) {
843                                         i = -1;
844                                 }
845                                 break;
846                         }
847
848                         /* We should not walk past our selves on the stack:  The upcoming nodes
849                            are not in this loop. We assume a loop not reachable from Start. */
850                         if (m == n) {
851                                 i = -1;
852                                 break;
853                         }
854
855                 }
856
857                 if (i < 0) {
858                         /* A dead loop not reachable from Start. */
859                         for (i = tos-2; i >= 0; --i) {
860                                 m = stack[i];
861                                 if (is_endless_head(m, n)) {
862                                         res_index = smallest_dfn_pred(m, get_irg_dfn(m) + 1);
863                                         if (res_index == -2)  /* no smallest dfn pred found. */
864                                                 res_index = largest_dfn_pred(m);
865                                         break;
866                                 }
867                                 if (m == n) { break; }  /* It's not an unreachable loop, either. */
868                         }
869                         //assert(0 && "no head found on stack");
870                 }
871
872         }
873         assert (res_index > -2);
874
875         set_irg_callee_backedge(m, res_index);
876         return get_irg_callee(m, res_index);
877 }
878 #else
879 static ir_graph *find_tail(ir_graph *n)
880 {
881         ir_graph *m;
882         int i, res_index = -2;
883
884         ir_graph *res;
885         ir_graph *in_and_out    = NULL;
886         ir_graph *only_in       = NULL;
887         ir_graph *ip_in_and_out = NULL;
888         ir_graph *ip_only_in    = NULL;
889
890         //printf("find tail for "); DDMG(n);
891
892         for (i = tos-1; i >= 0; --i) {
893                 ir_graph *pred = (i < tos -1) ? stack[i+1] : n;
894                 m = stack[i];
895
896                 if (is_head(m, n)) {
897                         //printf(" found 1a! "); DDM;
898                         in_and_out = m;
899                         if (is_ip_head(pred, m)) {
900                                 //printf(" found 1b! "); DDM;
901                                 ip_in_and_out = m;
902                         }
903                 } else if (!ip_only_in && is_endless_head(m, n)) {
904                         only_in = m;
905                         //printf(" found 2a! "); DDM;
906                         if (is_ip_head(pred, m)) {
907                                 //printf(" found 2b! "); DDM;
908                                 ip_only_in = m;
909                         }
910                 } else if (is_ip_head(pred, m)) {
911                         //printf(" found 3! "); DDM;   This happens for self recursions in the second
912                         //assert(0);                   scc iteration (the one to flip the loop.)
913                 }
914
915                 if (ip_in_and_out) break;    /* That's what we really want. */
916
917                 if (m == n) break;   /* Don't walk past n on the stack! */
918         }
919
920
921         if (!in_and_out && !only_in)
922                 /* There is no loop */
923                 return NULL;
924
925
926         /* Is there a head in the callgraph without a head in the
927            ip cf graph? */
928         assert(in_and_out || only_in);
929
930         m = (ip_in_and_out) ? ip_in_and_out : ip_only_in;
931
932         if (!m)
933                 m = (in_and_out) ? in_and_out : only_in;
934
935         //printf("*** head is "); DDMG(m);
936
937         res_index = smallest_dfn_pred(m, get_irg_dfn(m) + 1);
938         if (res_index == -2)  /* no smallest dfn pred found. */
939                 res_index = largest_dfn_pred(m);
940
941         set_irg_callee_backedge(m, res_index);
942         res = get_irg_callee(m, res_index);
943         //printf("*** tail is "); DDMG(res);
944         return res;
945 }
946 #endif /* INTERPROCEDURAL_VIEW */
947
948 /*-----------------------------------------------------------*
949  *                   The core algorithm.                     *
950  *-----------------------------------------------------------*/
951
952
953 static void cgscc(ir_graph *n)
954 {
955         int i, arity;
956
957         if (cg_irg_visited(n)) return;
958         mark_cg_irg_visited(n);
959
960         /* Initialize the node */
961         set_irg_dfn(n, current_dfn);      /* Depth first number for this node */
962         set_irg_uplink(n, current_dfn);   /* ... is default uplink. */
963         current_dfn ++;
964         push(n);
965
966         arity = get_irg_n_callees(n);
967         for (i = 0; i < arity; i++) {
968                 ir_graph *m;
969                 if (is_irg_callee_backedge(n, i)) continue;
970                 m = get_irg_callee(n, i);
971
972                 /** This marks the backedge, but does it guarantee a correct loop tree? */
973                 //if (m == n) { set_irg_callee_backedge(n, i); continue; }
974
975                 cgscc(m);
976                 if (irg_is_in_stack(m)) {
977                         /* Uplink of m is smaller if n->m is a backedge.
978                            Propagate the uplink to mark the cfloop. */
979                         if (get_irg_uplink(m) < get_irg_uplink(n))
980                                 set_irg_uplink(n, get_irg_uplink(m));
981                 }
982         }
983
984         if (get_irg_dfn(n) == get_irg_uplink(n)) {
985                 /* This condition holds for
986                    1) the node with the incoming backedge.
987                    That is: We found a cfloop!
988                    2) Straight line code, because no uplink has been propagated, so the
989                    uplink still is the same as the dfn.
990
991                    But n might not be a proper cfloop head for the analysis. Proper cfloop
992                    heads are Block and Phi nodes. find_tail searches the stack for
993                    Block's and Phi's and takes those nodes as cfloop heads for the current
994                    cfloop instead and marks the incoming edge as backedge. */
995
996                 ir_graph *tail = find_tail(n);
997                 if (tail) {
998                         /* We have a cfloop, that is no straight line code,
999                            because we found a cfloop head!
1000                            Next actions: Open a new cfloop on the cfloop tree and
1001                            try to find inner cfloops */
1002
1003
1004                         ir_loop *l = new_loop();
1005
1006                         /* Remove the cfloop from the stack ... */
1007                         pop_scc_unmark_visit(n);
1008
1009                         /* The current backedge has been marked, that is temporarily eliminated,
1010                            by find tail. Start the scc algorithm
1011                            anew on the subgraph thats left (the current cfloop without the backedge)
1012                            in order to find more inner cfloops. */
1013
1014                         cgscc(tail);
1015
1016                         assert(cg_irg_visited(n));
1017                         close_loop(l);
1018                 } else {
1019                         pop_scc_to_loop(n);
1020                 }
1021         }
1022 }
1023
1024
1025 /**
1026  * reset the backedge information for all callers in all irgs
1027  */
1028 static void reset_isbe(void)
1029 {
1030         int i, n_irgs = get_irp_n_irgs();
1031
1032         for (i = 0; i < n_irgs; ++i) {
1033                 ir_graph *irg = get_irp_irg(i);
1034
1035                 if (irg->caller_isbe)
1036                         xfree(irg->caller_isbe);
1037                 irg->caller_isbe = NULL;
1038
1039                 if (irg->callee_isbe)
1040                         xfree(irg->callee_isbe);
1041                 irg->callee_isbe = NULL;
1042         }
1043 }
1044
1045 /* ----------------------------------------------------------------------------------- */
1046 /* Another algorithm to compute recursion nesting depth                                */
1047 /* Walk the callgraph.  For each crossed edge increase the loop depth by the edge      */
1048 /* weight. Assign graphs the maximal depth.                                            */
1049 /* ----------------------------------------------------------------------------------- */
1050
1051 static void compute_loop_depth(ir_graph *irg, void *env)
1052 {
1053         int current_nesting = *(int *) env;
1054         int old_nesting = irg->callgraph_loop_depth;
1055         ir_visited_t old_visited = get_cg_irg_visited(irg);
1056         int i, n_callees;
1057
1058         //return ;
1059
1060         if (cg_irg_visited(irg)) return;
1061
1062         mark_cg_irg_visited(irg);
1063
1064         //printf(" old: %d new %d master %d", old_visited, get_cg_irg_visited(irg), master_cg_visited); DDMG(irg);
1065
1066
1067         if (old_nesting < current_nesting)
1068                 irg->callgraph_loop_depth = current_nesting;
1069
1070         if (current_nesting > irp->max_callgraph_loop_depth)
1071                 irp->max_callgraph_loop_depth = current_nesting;
1072
1073         if ((old_visited +1 < get_cg_irg_visited(irg)) ||   /* not yet visited */
1074                 (old_nesting < current_nesting)) {        /* propagate larger nesting */
1075                         /* Don't walk the graph, but a tree that is an unfolded graph. */
1076                         n_callees = get_irg_n_callees(irg);
1077                         for (i = 0; i < n_callees; i++) {
1078                                 ir_graph *m = get_irg_callee(irg, i);
1079                                 *(int *)env += get_irg_callee_loop_depth(irg, i);
1080                                 compute_loop_depth(m, env);
1081                                 *(int *)env -= get_irg_callee_loop_depth(irg, i);
1082                         }
1083         }
1084
1085         set_cg_irg_visited(irg, master_cg_visited-1);
1086 }
1087
1088 /* ------------------------------------------------------------------------------------ */
1089 /* Another algorithm to compute recursion nesting depth                                 */
1090 /* Walk the callgraph.  For each crossed loop increase the nesting depth by one.        */
1091 /* Assign graphs the maximal nesting depth.   Don't increase if passing loops more than */
1092 /* once.                                                                               */
1093 /* ------------------------------------------------------------------------------------ */
1094
1095
1096 /* For callees, we want to remember the Call nodes, too. */
1097 typedef struct ana_entry2 {
1098         ir_loop **loop_stack;   /**< a stack of ir_loop entries */
1099         int tos;                /**< the top of stack entry */
1100         int recursion_nesting;
1101 } ana_entry2;
1102
1103 /**
1104  * push a loop entry on the stack
1105  */
1106 static void push2(ana_entry2 *e, ir_loop *g)
1107 {
1108         if (ARR_LEN(e->loop_stack) == e->tos) {
1109                 ARR_APP1(ir_loop *, e->loop_stack, g);
1110         } else {
1111                 e->loop_stack[e->tos] = g;
1112         }
1113         ++e->tos;
1114 }
1115
1116 /**
1117  * returns the top of stack and pop it
1118  */
1119 static ir_loop *pop2(ana_entry2 *e)
1120 {
1121         return e->loop_stack[--e->tos];
1122 }
1123
1124 /**
1125  * check if a loop g in on the stack. Did not check the TOS.
1126  */
1127 static int in_stack(ana_entry2 *e, ir_loop *g)
1128 {
1129         int i;
1130         for (i = e->tos-1; i >= 0; --i) {
1131                 if (e->loop_stack[i] == g) return 1;
1132         }
1133         return 0;
1134 }
1135
1136 static void compute_rec_depth(ir_graph *irg, void *env)
1137 {
1138         ana_entry2 *e = (ana_entry2 *)env;
1139         ir_loop *l = irg->l;
1140         int depth, old_depth = irg->callgraph_recursion_depth;
1141         int i, n_callees;
1142         int pushed = 0;
1143
1144         if (cg_irg_visited(irg))
1145                 return;
1146         mark_cg_irg_visited(irg);
1147
1148         /* -- compute and set the new nesting value -- */
1149         if ((l != irp->outermost_cg_loop) && !in_stack(e, l)) {
1150                 push2(e, l);
1151                 e->recursion_nesting++;
1152                 pushed = 1;
1153         }
1154         depth = e->recursion_nesting;
1155
1156         if (old_depth < depth)
1157                 irg->callgraph_recursion_depth = depth;
1158
1159         if (depth > irp->max_callgraph_recursion_depth)
1160                 irp->max_callgraph_recursion_depth = depth;
1161
1162         /* -- spread the nesting value -- */
1163         if (depth == 0 || old_depth < depth) {
1164                 /* Don't walk the graph, but a tree that is an unfolded graph.
1165                    Therefore we unset the visited flag at the end. */
1166                 n_callees = get_irg_n_callees(irg);
1167                 for (i = 0; i < n_callees; ++i) {
1168                         ir_graph *m = get_irg_callee(irg, i);
1169                         compute_rec_depth(m, env);
1170                 }
1171         }
1172
1173         /* -- clean up -- */
1174         if (pushed) {
1175                 pop2(e);
1176                 e->recursion_nesting--;
1177         }
1178         set_cg_irg_visited(irg, master_cg_visited-1);
1179 }
1180
1181
1182 /* ----------------------------------------------------------------------------------- */
1183 /* Another algorithm to compute the execution frequency of methods ignoring recursions. */
1184 /* Walk the callgraph.  Ignore backedges.  Use sum of execution frequencies of Call     */
1185 /* nodes to evaluate a callgraph edge.                                                 */
1186 /* ----------------------------------------------------------------------------------- */
1187
1188 /* Returns the method execution frequency of a graph. */
1189 double get_irg_method_execution_frequency(const ir_graph *irg)
1190 {
1191         return irg->method_execution_frequency;
1192 }
1193
1194 /**
1195  * Increase the method execution frequency to freq if its current value is
1196  * smaller then this.
1197  */
1198 static void set_irg_method_execution_frequency(ir_graph *irg, double freq)
1199 {
1200         irg->method_execution_frequency = freq;
1201
1202         if (irp->max_method_execution_frequency < freq)
1203                 irp->max_method_execution_frequency = freq;
1204 }
1205
1206 static void compute_method_execution_frequency(ir_graph *irg, void *env)
1207 {
1208         int i, n_callers;
1209         double freq;
1210         int    found_edge;
1211         int    n_callees;
1212         (void) env;
1213
1214         if (cg_irg_visited(irg))
1215                 return;
1216
1217         /* We need the values of all predecessors (except backedges).
1218            So they must be marked.  Else we will reach the node through
1219            one of the unmarked ones. */
1220         n_callers = get_irg_n_callers(irg);
1221         for (i = 0; i < n_callers; ++i) {
1222                 ir_graph *m = get_irg_caller(irg, i);
1223                 if (is_irg_caller_backedge(irg, i))
1224                         continue;
1225                 if (!cg_irg_visited(m)) {
1226                         return;
1227                 }
1228         }
1229         mark_cg_irg_visited(irg);
1230
1231         /* Compute the new frequency. */
1232         freq = 0;
1233         found_edge = 0;
1234         for (i = 0; i < n_callers; i++) {
1235                 if (! is_irg_caller_backedge(irg, i)) {
1236                         double edge_freq = get_irg_caller_method_execution_frequency(irg, i);
1237                         assert(edge_freq >= 0);
1238                         freq += edge_freq;
1239                         found_edge = 1;
1240                 }
1241         }
1242
1243         if (!found_edge) {
1244                 /* A starting point: method only called from outside,
1245                 or only backedges as predecessors. */
1246                 freq = 1;
1247         }
1248
1249         set_irg_method_execution_frequency(irg, freq);
1250
1251         /* recur */
1252         n_callees = get_irg_n_callees(irg);
1253         for (i = 0; i < n_callees; ++i) {
1254                 compute_method_execution_frequency(get_irg_callee(irg, i), NULL);
1255         }
1256 }
1257
1258
1259 /* ----------------------------------------------------------------------------------- */
1260 /* The recursion stuff driver.                                                         */
1261 /* ----------------------------------------------------------------------------------- */
1262
1263 /* Compute the backedges that represent recursions. */
1264 void find_callgraph_recursions(void)
1265 {
1266         int i, n_irgs;
1267         struct obstack temp;
1268
1269         reset_isbe();
1270
1271         /* -- compute the looptree. -- */
1272
1273         /* The outermost graph.  We start here.  Then we start at all
1274         functions in irg list that are never called, then at the remaining
1275         unvisited ones. The third step is needed for functions that are not
1276         reachable from the outermost graph, but call themselves in a cycle. */
1277         assert(get_irp_main_irg());
1278         outermost_ir_graph = get_irp_main_irg();
1279         obstack_init(&temp);
1280         init_scc(&temp);
1281
1282         current_loop = NULL;
1283         new_loop();  /* sets current_loop */
1284
1285         ++master_cg_visited;
1286         cgscc(outermost_ir_graph);
1287         n_irgs = get_irp_n_irgs();
1288         for (i = 0; i < n_irgs; ++i) {
1289                 ir_graph *irg = get_irp_irg(i);
1290                 if (!cg_irg_visited(irg) && get_irg_n_callers(irg) == 0)
1291                         cgscc(irg);
1292         }
1293         for (i = 0; i < n_irgs; ++i) {
1294                 ir_graph *irg = get_irp_irg(i);
1295                 if (!cg_irg_visited(irg))
1296                         cgscc(irg);
1297         }
1298         obstack_free(&temp, NULL);
1299
1300         irp->outermost_cg_loop = current_loop;
1301         mature_loops(current_loop, outermost_ir_graph->obst);
1302
1303         /* -- Reverse the backedge information. -- */
1304         for (i = 0; i < n_irgs; ++i) {
1305                 ir_graph *irg = get_irp_irg(i);
1306                 int j, n_callees = get_irg_n_callees(irg);
1307                 for (j = 0; j < n_callees; ++j) {
1308                         if (is_irg_callee_backedge(irg, j))
1309                                 set_irg_caller_backedge(get_irg_callee(irg, j), irg);
1310                 }
1311         }
1312
1313         irp->callgraph_state = irp_callgraph_and_calltree_consistent;
1314 }
1315
1316 /* Compute interprocedural performance estimates. */
1317 void compute_performance_estimates(void)
1318 {
1319         int i, n_irgs = get_irp_n_irgs();
1320         int current_nesting;
1321         ana_entry2 e;
1322
1323         assert(get_irp_exec_freq_state() != exec_freq_none && "execution frequency not calculated");
1324
1325         /* -- compute the loop depth  -- */
1326         current_nesting = 0;
1327         irp->max_callgraph_loop_depth = 0;
1328         master_cg_visited += 2;
1329         //printf(" ** starting at      "); DDMG(get_irp_main_irg());
1330         compute_loop_depth(get_irp_main_irg(), &current_nesting);
1331         for (i = 0; i < n_irgs; i++) {
1332                 ir_graph *irg = get_irp_irg(i);
1333                 if ((get_cg_irg_visited(irg) < master_cg_visited-1) &&
1334                         get_irg_n_callers(irg) == 0) {
1335                                 compute_loop_depth(irg, &current_nesting);
1336                                 //printf(" ** starting at      "); DDMG(irg);
1337                 }
1338         }
1339         for (i = 0; i < n_irgs; i++) {
1340                 ir_graph *irg = get_irp_irg(i);
1341                 if (get_cg_irg_visited(irg) < master_cg_visited-1) {
1342                         compute_loop_depth(irg, &current_nesting);
1343                         //printf(" ** starting at      "); DDMG(irg);
1344                 }
1345         }
1346
1347
1348         /* -- compute the recursion depth -- */
1349         e.loop_stack        = NEW_ARR_F(ir_loop *, 0);
1350         e.tos               = 0;
1351         e.recursion_nesting = 0;
1352
1353         irp->max_callgraph_recursion_depth = 0;
1354
1355         master_cg_visited += 2;
1356         compute_rec_depth(get_irp_main_irg(), &e);
1357         //printf(" ++ starting at "); DDMG(get_irp_main_irg());
1358         for (i = 0; i < n_irgs; i++) {
1359                 ir_graph *irg = get_irp_irg(i);
1360                 if ((get_cg_irg_visited(irg) < master_cg_visited-1) &&
1361                         get_irg_n_callers(irg) == 0) {
1362                                 compute_rec_depth(irg, &e);
1363                                 //printf(" ++ starting at "); DDMG(irg);
1364                 }
1365         }
1366         for (i = 0; i < n_irgs; i++) {
1367                 ir_graph *irg = get_irp_irg(i);
1368                 if (get_cg_irg_visited(irg) < master_cg_visited-1) {
1369                         compute_rec_depth(irg, &e);
1370                         //printf(" ++ starting at "); DDMG(irg);
1371                 }
1372         }
1373
1374         DEL_ARR_F(e.loop_stack);
1375
1376         /* -- compute the execution frequency -- */
1377         irp->max_method_execution_frequency = 0;
1378         master_cg_visited += 2;
1379         assert(get_irg_n_callers(get_irp_main_irg()) == 0);
1380         compute_method_execution_frequency(get_irp_main_irg(), NULL);
1381         for (i = 0; i < n_irgs; i++) {
1382                 ir_graph *irg = get_irp_irg(i);
1383                 if ((get_cg_irg_visited(irg) < master_cg_visited-1) &&
1384                         get_irg_n_callers(irg) == 0) {
1385                                 compute_method_execution_frequency(irg, NULL);
1386                 }
1387         }
1388         for (i = 0; i < n_irgs; i++) {
1389                 ir_graph *irg = get_irp_irg(i);
1390                 if (get_cg_irg_visited(irg) < master_cg_visited-1) {
1391                         compute_method_execution_frequency(irg, NULL);
1392                 }
1393         }
1394 }
1395
1396 /* Returns the maximal loop depth of all paths from an external visible method to
1397    this irg. */
1398 int  get_irg_loop_depth(const ir_graph *irg)
1399 {
1400         assert(irp->callgraph_state == irp_callgraph_consistent ||
1401                 irp->callgraph_state == irp_callgraph_and_calltree_consistent);
1402         return  irg->callgraph_loop_depth;
1403 }
1404
1405 /* Returns the maximal recursion depth of all paths from an external visible method to
1406    this irg. */
1407 int get_irg_recursion_depth(const ir_graph *irg)
1408 {
1409         assert(irp->callgraph_state == irp_callgraph_and_calltree_consistent);
1410         return irg->callgraph_recursion_depth;
1411 }
1412
1413 /* Computes the interprocedural loop nesting information. */
1414 void analyse_loop_nesting_depth(void)
1415 {
1416         ir_entity **free_methods = NULL;
1417         int arr_len;
1418
1419         /* establish preconditions. */
1420         if (get_irp_callee_info_state() != irg_callee_info_consistent) {
1421                 cgana(&arr_len, &free_methods);
1422         }
1423
1424         if (irp_callgraph_consistent != get_irp_callgraph_state()) {
1425                 compute_callgraph();
1426         }
1427
1428         find_callgraph_recursions();
1429
1430         compute_performance_estimates();
1431
1432         set_irp_loop_nesting_depth_state(loop_nesting_depth_consistent);
1433 }
1434
1435 loop_nesting_depth_state get_irp_loop_nesting_depth_state(void)
1436 {
1437         return irp->lnd_state;
1438 }
1439 void set_irp_loop_nesting_depth_state(loop_nesting_depth_state s)
1440 {
1441         irp->lnd_state = s;
1442 }
1443 void set_irp_loop_nesting_depth_state_inconsistent(void)
1444 {
1445         if (irp->lnd_state == loop_nesting_depth_consistent)
1446                 irp->lnd_state = loop_nesting_depth_inconsistent;
1447 }