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