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