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