927c5a756ef2bee6c40ee75eab1863718e786bcc
[libfirm] / ir / ana / callgraph.c
1 /*
2  * Project:     libFIRM
3  * File name:   ir/ana/callgraph.c
4  * Purpose:     Representation and computation of the callgraph.
5  * Author:      Goetz Lindenmaier
6  * Modified by:
7  * Created:     21.7.2004
8  * CVS-ID:      $Id$
9  * Copyright:   (c) 2004 Universität Karlsruhe
10  * Licence:     This file protected by GPL -  GNU GENERAL PUBLIC LICENSE.
11  */
12
13 #include "config.h"
14 #include "callgraph.h"
15
16 #include "irloop_t.h"
17 #include "irprog_t.h"
18 #include "irgraph_t.h"
19 #include "irnode_t.h"
20
21 #include "cgana.h"
22
23 #include "array.h"
24 #include "pmap.h"
25
26 #include "irgwalk.h"
27
28 /* For callees, we want to remember the Call nodes, too. */
29 struct ana_entry {
30   ir_graph *irg;
31   ir_node  *call_list;
32   int       max_depth;
33 };
34
35 typedef struct ana_entry ana_entry;
36
37 static int master_cg_visited = 0;
38 static INLINE int  cg_irg_visited     (ir_graph *n);
39 static INLINE void mark_cg_irg_visited(ir_graph *n);
40 static INLINE void set_cg_irg_visited (ir_graph *n, int i);
41
42 irp_callgraph_state get_irp_callgraph_state(void) {
43   return irp->callgraph_state;
44 }
45 void                set_irp_callgraph_state(irp_callgraph_state s) {
46   irp->callgraph_state = s;
47 }
48
49 /* The functions that call irg. */
50 int       get_irg_n_callers(ir_graph *irg) {
51   if (irg->callers) return ARR_LEN(irg->callers);
52   return -1;
53 }
54 ir_graph *get_irg_caller(ir_graph *irg, int pos) {
55   assert (pos >= 0 && pos < get_irg_n_callers(irg));
56   if (irg->callers) return irg->callers[pos];
57   return NULL;
58 }
59 int       is_irg_caller_backedge(ir_graph *irg, int pos) {
60   assert (pos >= 0 && pos < get_irg_n_callers(irg));
61   return  irg->caller_isbe[pos];
62 }
63
64 static void       set_irg_caller_backedge(ir_graph *irg, ir_graph *caller) {
65   int i, n_callers = get_irg_n_callers(irg);
66   for (i = 0; i < n_callers; ++i) {
67     if (get_irg_caller(irg, i) == caller) {
68       irg->caller_isbe[i] = 1;
69       break;
70     }
71   }
72 }
73
74 int       has_irg_caller_backedge(ir_graph *irg) {
75   int i, n_callers = get_irg_n_callers(irg);
76   for (i = 0; i < n_callers; ++i)
77     if (is_irg_caller_backedge(irg, i)) return 1;
78   return 0;
79 }
80
81 int get_irg_caller_loop_depth(ir_graph *irg, int pos) {
82   ir_graph *caller = get_irg_caller(irg, pos);
83
84   /* search the other relation for the corresponding edge. */
85   int pos_callee = -1;
86   int i, n_callees = get_irg_n_callees(caller);
87   for (i = 0; i < n_callees; ++i) {
88     if (get_irg_callee(caller, i) == irg) {
89       pos_callee = i;
90       break;
91     }
92   }
93
94   assert(pos_callee >= 0);
95
96   return get_irg_callee_loop_depth(caller, pos_callee);
97 }
98
99
100 /* The functions called by irg. */
101 int       get_irg_n_callees(ir_graph *irg) {
102   if (irg->callees) return ARR_LEN(irg->callees);
103   return -1;
104 }
105 ir_graph *get_irg_callee(ir_graph *irg, int pos) {
106   assert (pos >= 0 && pos < get_irg_n_callees(irg));
107   if (irg->callees) return ((ana_entry *)irg->callees[pos])->irg;
108   return NULL;
109 }
110 int       is_irg_callee_backedge(ir_graph *irg, int pos) {
111   assert (pos >= 0 && pos < get_irg_n_callees(irg));
112   return irg->callee_isbe[pos];
113 }
114 int       has_irg_callee_backedge(ir_graph *irg) {
115   int i, n_callees = get_irg_n_callees(irg);
116   for (i = 0; i < n_callees; ++i)
117     if (is_irg_callee_backedge(irg, i)) return 1;
118   return 0;
119 }
120 void       set_irg_callee_backedge(ir_graph *irg, int pos) {
121   assert (pos >= 0 && pos < get_irg_n_callees(irg));
122   irg->callee_isbe[pos] = 1;
123 }
124
125 int get_irg_callee_loop_depth(ir_graph *irg, int pos) {
126   assert (pos >= 0 && pos < get_irg_n_callees(irg));
127   if (irg->callees) return ((ana_entry *)irg->callees[pos])->max_depth;
128   return -1;
129 }
130
131 /* --------------------- Compute the callgraph ------------------------ */
132
133 /* Hash an address */
134 #define HASH_ADDRESS(adr)       (((unsigned)(adr)) >> 3)
135
136 /**
137  * Walker called by compute_callgraph()
138  */
139 static void ana_Call(ir_node *n, void *env) {
140   int i, n_callees;
141   ir_graph *irg;
142
143   if (get_irn_op(n) != op_Call) return;
144
145   irg = get_irn_irg(n);
146   n_callees = get_Call_n_callees(n);
147   for (i = 0; i < n_callees; ++i) {
148     entity *callee_e = get_Call_callee(n, i);
149     if (callee_e != unknown_entity) {  /* For unknown caller */
150       ir_graph *callee = get_entity_irg(callee_e);
151       pset_insert((pset *)callee->callers, irg, HASH_ADDRESS(irg));
152
153       ana_entry buf = { callee, NULL, 0};
154       ana_entry *found = pset_find((pset *)irg->callees, &buf, HASH_ADDRESS(callee));
155       if (found) {  /* add Call node to list, compute new nesting. */
156       } else { /* New node, add Call node and init nesting. */
157         found = (ana_entry *) obstack_alloc (irg->obst, sizeof (ana_entry));
158         found->irg = callee;
159         found->call_list = NULL;
160         found->max_depth = 0;
161         pset_insert((pset *)irg->callees, found, HASH_ADDRESS(callee));
162       }
163       int depth = get_loop_depth(get_irn_loop(get_nodes_block(n)));
164       found->max_depth = (depth > found->max_depth) ? depth : found->max_depth;
165     }
166   }
167 }
168
169 /** compare two ir graphs in a ana_entry */
170 static int ana_entry_cmp(const void *elt, const void *key) {
171   const ana_entry *e1 = elt;
172   const ana_entry *e2 = key;
173   return e1->irg != e2->irg;
174 }
175
176 /** compare two ir graphs */
177 static int graph_cmp(const void *elt, const void *key) {
178   return elt != key;
179 }
180
181
182 /* Construct and destruct the callgraph. */
183 void compute_callgraph(void) {
184   int i, n_irgs = get_irp_n_irgs();
185
186   assert(! get_interprocedural_view());  /* Else walking will not reach the Call nodes. */
187
188   /* initialize */
189   free_callgraph();
190   for (i = 0; i < n_irgs; ++i) {
191     ir_graph *irg = get_irp_irg(i);
192     assert(get_irg_callee_info_state(irg) == irg_callee_info_consistent);
193     irg->callees = (ir_graph **)new_pset(ana_entry_cmp, 8);
194     irg->callers = (ir_graph **)new_pset(graph_cmp, 8);
195     construct_cf_backedges(irg);
196   }
197
198   /* Compute the call graph */
199   for (i = 0; i < n_irgs; ++i) {
200     ir_graph *irg = get_irp_irg(i);
201     construct_cf_backedges(irg);
202     irg_walk_graph(irg, ana_Call, NULL, NULL);
203   }
204
205   /* Change the sets to arrays. */
206   for (i = 0; i < n_irgs; ++i) {
207     int j, count;
208     ir_graph *c, *irg = get_irp_irg(i);
209
210     pset *callee_set = (pset *)irg->callees;
211     count = pset_count(callee_set);
212     irg->callees = NEW_ARR_F(ir_graph *, count);
213     irg->callee_isbe = calloc(count, sizeof(int));
214     c = pset_first(callee_set);
215     for (j = 0; j < count; ++j) {
216       irg->callees[j] = c;
217       c = pset_next(callee_set);
218     }
219     del_pset(callee_set);
220     assert(c == NULL);
221
222     pset *caller_set = (pset *)irg->callers;
223     count = pset_count(caller_set);
224     irg->callers = NEW_ARR_F(ir_graph *, count);
225     irg->caller_isbe =  calloc(count, sizeof(int));
226     c = pset_first(caller_set);
227     for (j = 0; j < count; ++j) {
228       irg->callers[j] = c;
229       c = pset_next(caller_set);
230     }
231     del_pset(caller_set);
232     assert(c == NULL);
233   }
234   set_irp_callgraph_state(irp_callgraph_consistent);
235 }
236
237 void free_callgraph(void) {
238   int i, n_irgs = get_irp_n_irgs();
239   for (i = 0; i < n_irgs; ++i) {
240     ir_graph *irg = get_irp_irg(i);
241     if (irg->callees) DEL_ARR_F(irg->callees);
242     if (irg->callers) DEL_ARR_F(irg->callers);
243     if (irg->callee_isbe) DEL_ARR_F(irg->callee_isbe);
244     if (irg->caller_isbe) DEL_ARR_F(irg->caller_isbe);
245     irg->callees = NULL;
246     irg->callers = NULL;
247     irg->callee_isbe = NULL;
248     irg->caller_isbe = NULL;
249   }
250   set_irp_callgraph_state(irp_callgraph_none);
251 }
252
253 /* ----------------------------------------------------------------------------------- */
254 /* A walker for the callgraph                                                          */
255 /* ----------------------------------------------------------------------------------- */
256
257
258 static void do_walk(ir_graph *irg, callgraph_walk_func *pre, callgraph_walk_func *post, void *env) {
259   int i, n_callees;
260
261   if (cg_irg_visited(irg)) return;
262   mark_cg_irg_visited(irg);
263
264   pre(irg, env);
265
266   n_callees = get_irg_n_callees(irg);
267   for (i = 0; i < n_callees; i++) {
268     ir_graph *m = get_irg_callee(irg, i);
269     do_walk(m, pre, post, env);
270   }
271
272   post(irg, env);
273 }
274
275 void callgraph_walk(callgraph_walk_func *pre, callgraph_walk_func *post, void *env) {
276   int i, n_irgs = get_irp_n_irgs();
277   master_cg_visited++;
278
279   do_walk(get_irp_main_irg(), pre, post, env);
280   for (i = 0; i < n_irgs; i++) {
281     ir_graph *irg = get_irp_irg(i);
282     if (!cg_irg_visited(irg) && get_irg_n_callers(irg) == 0)
283       do_walk(irg, pre, post, env);
284   }
285   for (i = 0; i < n_irgs; i++) {
286     ir_graph *irg = get_irp_irg(i);
287     if (!cg_irg_visited(irg))
288       do_walk(irg, pre, post, env);
289   }
290 }
291
292 /* ----------------------------------------------------------------------------------- */
293 /* loop construction algorithm                                                         */
294 /* ----------------------------------------------------------------------------------- */
295
296 static ir_graph *outermost_ir_graph;      /* The outermost graph the scc is computed
297                       for */
298 static ir_loop *current_loop;      /* Current cfloop construction is working
299                       on. */
300 static int loop_node_cnt = 0;      /* Counts the number of allocated cfloop nodes.
301                       Each cfloop node gets a unique number.
302                       What for? ev. remove. @@@ */
303 static int current_dfn = 1;        /* Counter to generate depth first numbering
304                       of visited nodes.  */
305
306
307 /**********************************************************************/
308 /* Node attributes                                                   **/
309 /**********************************************************************/
310
311 typedef struct scc_info {
312   bool in_stack;         /* Marks whether node is on the stack. */
313   int dfn;               /* Depth first search number. */
314   int uplink;            /* dfn number of ancestor. */
315   int visited;
316 } scc_info;
317
318 static INLINE scc_info* new_scc_info(void) {
319   scc_info *info = obstack_alloc (outermost_ir_graph->obst, sizeof (scc_info));
320   memset (info, 0, sizeof (scc_info));
321   return info;
322 }
323
324 static INLINE int
325 cg_irg_visited(ir_graph *n) {
326   return (((scc_info *)n->link)->visited >= master_cg_visited);
327 }
328
329 static INLINE void
330 mark_cg_irg_visited(ir_graph *n) {
331   ((scc_info *)n->link)->visited = master_cg_visited;
332 }
333
334 static INLINE void
335 set_cg_irg_visited(ir_graph *n, int i) {
336   ((scc_info *)n->link)->visited = i;
337 }
338
339 static INLINE int
340 get_cg_irg_visited(ir_graph *n) {
341   return ((scc_info *)n->link)->visited;
342 }
343
344 static INLINE void
345 mark_irg_in_stack (ir_graph *n) {
346   assert(get_irg_link(n));
347   ((scc_info *)n->link)->in_stack = true;
348 }
349
350 static INLINE void
351 mark_irg_not_in_stack (ir_graph *n) {
352   assert(get_irg_link(n));
353   ((scc_info *)n->link)->in_stack = false;
354 }
355
356 static INLINE bool
357 irg_is_in_stack (ir_graph *n) {
358   assert(get_irg_link(n));
359   return ((scc_info *)n->link)->in_stack;
360 }
361
362 static INLINE void
363 set_irg_uplink (ir_graph *n, int uplink) {
364   assert(get_irg_link(n));
365   ((scc_info *)n->link)->uplink = uplink;
366 }
367
368 static INLINE int
369 get_irg_uplink (ir_graph *n) {
370   assert(get_irg_link(n));
371   return ((scc_info *)n->link)->uplink;
372 }
373
374 static INLINE void
375 set_irg_dfn (ir_graph *n, int dfn) {
376   assert(get_irg_link(n));
377   ((scc_info *)n->link)->dfn = dfn;
378 }
379
380 static INLINE int
381 get_irg_dfn (ir_graph *n) {
382   assert(get_irg_link(n));
383   return ((scc_info *)n->link)->dfn;
384 }
385
386 /**********************************************************************/
387 /* A stack.                                                          **/
388 /**********************************************************************/
389
390 static ir_graph **stack = NULL;
391 static int tos = 0;                /* top of stack */
392
393 static INLINE void init_stack(void) {
394   if (stack) {
395     ARR_RESIZE (ir_graph *, stack, 1000);
396   } else {
397     stack = NEW_ARR_F (ir_graph *, 1000);
398   }
399   tos = 0;
400 }
401
402
403 static INLINE void
404 push (ir_graph *n)
405 {
406   if (tos == ARR_LEN (stack)) {
407     int nlen = ARR_LEN (stack) * 2;
408     ARR_RESIZE (ir_node *, stack, nlen);
409   }
410   stack [tos++] = n;
411   mark_irg_in_stack(n);
412 }
413
414 static INLINE ir_graph *
415 pop (void)
416 {
417   ir_graph *n = stack[--tos];
418   mark_irg_not_in_stack(n);
419   return n;
420 }
421
422 /* The nodes up to n belong to the current loop.
423    Removes them from the stack and adds them to the current loop. */
424 static INLINE void
425 pop_scc_to_loop (ir_graph *n)
426 {
427   ir_graph *m;
428
429   do {
430     m = pop();
431     loop_node_cnt++;
432     set_irg_dfn(m, loop_node_cnt);
433     add_loop_node(current_loop, (ir_node *)m);
434     m->l = current_loop;
435     //m->callgraph_loop_depth = current_loop->depth;
436   } while(m != n);
437 }
438
439 /* GL ??? my last son is my grandson???  Removes cfloops with no
440    ir_nodes in them.  Such loops have only another loop as son. (Why
441    can't they have two loops as sons? Does it never get that far? ) */
442 static void close_loop (ir_loop *l)
443 {
444   int last = get_loop_n_elements(l) - 1;
445   loop_element lelement = get_loop_element(l, last);
446   ir_loop *last_son = lelement.son;
447
448   if (get_kind(last_son) == k_ir_loop &&
449       get_loop_n_elements(last_son) == 1) {
450     ir_loop *gson;
451
452     lelement = get_loop_element(last_son, 0);
453     gson = lelement.son;
454     if(get_kind(gson) == k_ir_loop) {
455       loop_element new_last_son;
456
457       gson -> outer_loop = l;
458       new_last_son.son = gson;
459       l -> children[last] = new_last_son;
460     }
461   }
462
463   current_loop = l;
464 }
465
466 /* Removes and unmarks all nodes up to n from the stack.
467    The nodes must be visited once more to assign them to a scc. */
468 static INLINE void
469 pop_scc_unmark_visit (ir_graph *n)
470 {
471   ir_graph *m = NULL;
472
473   while (m != n) {
474     m = pop();
475     set_cg_irg_visited(m, 0);
476   }
477 }
478
479 /**********************************************************************/
480 /* The loop datastructure.                                           **/
481 /**********************************************************************/
482
483 /* Allocates a new loop as son of current_loop.  Sets current_loop
484    to the new loop and returns the father. */
485 static ir_loop *new_loop (void) {
486   ir_loop *father, *son;
487
488   father = current_loop;
489
490   son = (ir_loop *) obstack_alloc (outermost_ir_graph->obst, sizeof (ir_loop));
491   memset (son, 0, sizeof (ir_loop));
492   son->kind = k_ir_loop;
493   son->children = NEW_ARR_F (loop_element, 0);
494   son->n_nodes = 0;
495   son->n_sons = 0;
496   if (father) {
497     son->outer_loop = father;
498     add_loop_son(father, son);
499     son->depth = father->depth+1;
500   } else {  /* The root loop */
501     son->outer_loop = son;
502     son->depth = 0;
503   }
504
505 #ifdef DEBUG_libfirm
506   son->loop_nr = get_irp_new_node_nr();
507   son->link = NULL;
508 #endif
509
510   current_loop = son;
511   return father;
512 }
513
514 /**********************************************************************/
515 /* Constructing and destructing the loop/backedge information.       **/
516 /**********************************************************************/
517
518 /* Initialization steps. **********************************************/
519
520 static INLINE void
521 init_scc (void) {
522   int i;
523   current_dfn = 1;
524   loop_node_cnt = 0;
525   init_stack();
526   for (i = 0; i < get_irp_n_irgs(); ++i) {
527     set_irg_link(get_irp_irg(i), new_scc_info());
528     get_irp_irg(i)->callgraph_recursion_depth = 0;
529     get_irp_irg(i)->callgraph_loop_depth = 0;
530   }
531 }
532
533 /** Returns true if n is a loop header, i.e., it is a Block node
534  *  and has predecessors within the cfloop and out of the cfloop.
535  *
536  *  @param root: only needed for assertion.
537  */
538 static bool
539 is_head (ir_graph *n, ir_graph *root)
540 {
541   int i, arity;
542   int some_outof_loop = 0, some_in_loop = 0;
543
544   arity = get_irg_n_callees(n);
545   for (i = 0; i < arity; i++) {
546     ir_graph *pred = get_irg_callee(n, i);
547     if (is_irg_callee_backedge(n, i)) continue;
548     if (!irg_is_in_stack(pred)) {
549       some_outof_loop = 1;
550     } else {
551       if (get_irg_uplink(pred) < get_irg_uplink(root))  {
552         DDMG(pred); DDMG(root);
553         assert(get_irg_uplink(pred) >= get_irg_uplink(root));
554       }
555       some_in_loop = 1;
556     }
557   }
558
559   return some_outof_loop && some_in_loop;
560 }
561 /* Returns true if n is possible loop head of an endless loop.
562    I.e., it is a Block, Phi or Filter node and has only predecessors
563    within the loop.
564    @arg root: only needed for assertion. */
565 static bool
566 is_endless_head (ir_graph *n, ir_graph *root)
567 {
568   int i, arity;
569   int some_outof_loop = 0, some_in_loop = 0;
570
571   arity = get_irg_n_callees(n);
572   for (i = 0; i < arity; i++) {
573     ir_graph *pred = get_irg_callee(n, i);
574     assert(pred);
575     if (is_irg_callee_backedge(n, i)) { continue; }
576     if (!irg_is_in_stack(pred)) {
577         some_outof_loop = 1;
578     } else {
579       if(get_irg_uplink(pred) < get_irg_uplink(root)) {
580         DDMG(pred); DDMG(root);
581         assert(get_irg_uplink(pred) >= get_irg_uplink(root));
582       }
583       some_in_loop = 1;
584     }
585   }
586
587   return !some_outof_loop && some_in_loop;
588 }
589
590
591 /* Check whether there is a parallel edge in the ip control flow.
592    Only */
593 static bool
594 is_ip_head (ir_graph *n, ir_graph *pred)
595 {
596   int iv_rem = get_interprocedural_view();
597   set_interprocedural_view(true);
598   ir_node *sblock = get_irg_start_block(n);
599   int i, arity = get_Block_n_cfgpreds(sblock);
600   int is_be = 0;
601
602   //printf(" edge from "); DDMG(n);
603   //printf(" to pred   "); DDMG(pred);
604   //printf(" sblock    "); DDMN(sblock);
605
606   for (i = 0; i < arity; i++) {
607     ir_node *pred_cfop = skip_Proj(get_Block_cfgpred(sblock, i));
608     //printf("  "); DDMN(pred_cfop);
609     if (get_irn_op(pred_cfop) == op_CallBegin) {  /* could be Unknown */
610       ir_graph *ip_pred = get_irn_irg(pred_cfop);
611       //printf("   "); DDMG(ip_pred);
612       if ((ip_pred == pred) && is_backedge(sblock, i)) {
613         //printf("   found\n");
614         is_be = 1;
615       }
616     }
617   }
618
619   set_interprocedural_view(iv_rem);
620   return is_be;
621 }
622
623 /* Returns index of the predecessor with the smallest dfn number
624    greater-equal than limit. */
625 static int
626 smallest_dfn_pred (ir_graph *n, int limit)
627 {
628   int i, index = -2, min = -1;
629
630   int arity = get_irg_n_callees(n);
631   for (i = 0; i < arity; i++) {
632     ir_graph *pred = get_irg_callee(n, i);
633     if (is_irg_callee_backedge(n, i) || !irg_is_in_stack(pred)) continue;
634     if (get_irg_dfn(pred) >= limit && (min == -1 || get_irg_dfn(pred) < min)) {
635       index = i;
636       min = get_irg_dfn(pred);
637     }
638   }
639
640   return index;
641 }
642
643 /* Returns index of the predecessor with the largest dfn number. */
644 static int
645 largest_dfn_pred (ir_graph *n)
646 {
647   int i, index = -2, max = -1;
648
649   int arity = get_irg_n_callees(n);
650   for (i = 0; i < arity; i++) {
651     ir_graph *pred = get_irg_callee(n, i);
652     if (is_irg_callee_backedge (n, i) || !irg_is_in_stack(pred)) continue;
653     if (get_irg_dfn(pred) > max) {
654       index = i;
655       max = get_irg_dfn(pred);
656     }
657   }
658
659   return index;
660 }
661
662 #if 0
663 static ir_graph *
664 find_tail (ir_graph *n) {
665   ir_graph *m;
666   int i, res_index = -2;
667
668   /*
669     if (!icfg && rm_cyclic_phis && remove_cyclic_phis (n)) return NULL;
670   */
671   m = stack[tos-1];  /* tos = top of stack */
672   if (is_head (m, n)) {
673     res_index = smallest_dfn_pred(m, 0);
674     if ((res_index == -2) &&  /* no smallest dfn pred found. */
675     (n == m))
676       return NULL;
677   } else {
678     if (m == n) return NULL;    // Is this to catch Phi - self loops?
679     for (i = tos-2; i >= 0; --i) {
680       m = stack[i];
681
682       if (is_head (m, n)) {
683         res_index = smallest_dfn_pred (m, get_irg_dfn(m) + 1);
684         if (res_index == -2)  /* no smallest dfn pred found. */
685           res_index = largest_dfn_pred (m);
686
687         if ((m == n) && (res_index == -2)) {
688           i = -1;
689         }
690         break;
691       }
692
693       /* We should not walk past our selves on the stack:  The upcoming nodes
694          are not in this loop. We assume a loop not reachable from Start. */
695       if (m == n) {
696         i = -1;
697         break;
698       }
699
700     }
701
702     if (i < 0) {
703       /* A dead loop not reachable from Start. */
704       for (i = tos-2; i >= 0; --i) {
705         m = stack[i];
706         if (is_endless_head (m, n)) {
707           res_index = smallest_dfn_pred (m, get_irg_dfn(m) + 1);
708           if (res_index == -2)  /* no smallest dfn pred found. */
709             res_index = largest_dfn_pred (m);
710           break;
711         }
712         if (m == n) { break; }  /* It's not an unreachable loop, either. */
713       }
714       //assert(0 && "no head found on stack");
715     }
716
717   }
718   assert (res_index > -2);
719
720   set_irg_callee_backedge (m, res_index);
721   return get_irg_callee(m, res_index);
722 }
723 #else
724 static ir_graph *
725 find_tail (ir_graph *n) {
726   ir_graph *m;
727   int i, res_index = -2;
728
729   ir_graph *in_and_out    = NULL;
730   ir_graph *only_in       = NULL;
731   ir_graph *ip_in_and_out = NULL;
732   ir_graph *ip_only_in    = NULL;
733
734   //printf("find tail for "); DDMG(n);
735
736   for (i = tos-1; i >= 0; --i) {
737     ir_graph *pred = (i < tos -1) ? stack[i+1] : n;
738     m = stack[i];
739
740     if (is_head (m, n)) {
741       //printf(" found 1a! "); DDM;
742       in_and_out = m;
743       if (is_ip_head(pred, m)) {
744         //printf(" found 1b! "); DDM;
745         ip_in_and_out = m;
746       }
747     } else if (!ip_only_in && is_endless_head(m, n)) {
748       only_in = m;
749       //printf(" found 2a! "); DDM;
750       if (is_ip_head(pred, m)) {
751         //printf(" found 2b! "); DDM;
752         ip_only_in = m;
753       }
754     } else if (is_ip_head(pred, m)) {
755       //printf(" found 3! "); DDM;   This happens for self recursions in the second
756       //assert(0);                   scc iteration (the one to flip the loop.)
757     }
758
759     if (ip_in_and_out) break;    /* That's what we really want. */
760
761     if (m == n) break;   /* Don't walk past n on the stack! */
762   }
763
764
765   if (!in_and_out && !only_in)
766     /* There is no loop */
767     return NULL;
768
769
770   /* Is there a head in the callgraph without a head in the
771      ip cf graph? */
772   assert(in_and_out || only_in);
773
774   m = (ip_in_and_out) ? ip_in_and_out : ip_only_in;
775
776   if (!m)
777     m = (in_and_out) ? in_and_out : only_in;
778
779   //printf("*** head is "); DDMG(m);
780
781   res_index = smallest_dfn_pred (m, get_irg_dfn(m) + 1);
782   if (res_index == -2)  /* no smallest dfn pred found. */
783     res_index = largest_dfn_pred (m);
784
785   set_irg_callee_backedge (m, res_index);
786   ir_graph *res = get_irg_callee(m, res_index);
787   //printf("*** tail is "); DDMG(res);
788   return res;
789 }
790 #endif
791
792
793 /*-----------------------------------------------------------*
794  *                   The core algorithm.                     *
795  *-----------------------------------------------------------*/
796
797
798 static void cgscc (ir_graph *n) {
799   int i;
800
801   if (cg_irg_visited(n)) return;
802   mark_cg_irg_visited(n);
803
804   /* Initialize the node */
805   set_irg_dfn(n, current_dfn);      /* Depth first number for this node */
806   set_irg_uplink(n, current_dfn);   /* ... is default uplink. */
807   current_dfn ++;
808   push(n);
809
810   int arity = get_irg_n_callees(n);
811
812   for (i = 0; i < arity; i++) {
813     ir_graph *m;
814     if (is_irg_callee_backedge(n, i)) continue;
815     m = get_irg_callee(n, i);
816
817     /** This marks the backedge, but does it guarantee a correct loop tree? */
818     //if (m == n) { set_irg_callee_backedge(n, i); continue; }
819
820     cgscc (m);
821     if (irg_is_in_stack(m)) {
822       /* Uplink of m is smaller if n->m is a backedge.
823          Propagate the uplink to mark the cfloop. */
824       if (get_irg_uplink(m) < get_irg_uplink(n))
825         set_irg_uplink(n, get_irg_uplink(m));
826     }
827   }
828
829   if (get_irg_dfn(n) == get_irg_uplink(n)) {
830     /* This condition holds for
831        1) the node with the incoming backedge.
832        That is: We found a cfloop!
833        2) Straight line code, because no uplink has been propagated, so the
834        uplink still is the same as the dfn.
835
836        But n might not be a proper cfloop head for the analysis. Proper cfloop
837        heads are Block and Phi nodes. find_tail searches the stack for
838        Block's and Phi's and takes those nodes as cfloop heads for the current
839        cfloop instead and marks the incoming edge as backedge. */
840
841     ir_graph *tail = find_tail(n);
842     if (tail) {
843       /* We have a cfloop, that is no straight line code,
844          because we found a cfloop head!
845          Next actions: Open a new cfloop on the cfloop tree and
846          try to find inner cfloops */
847
848
849       ir_loop *l = new_loop();
850
851       /* Remove the cfloop from the stack ... */
852       pop_scc_unmark_visit (n);
853
854       /* The current backedge has been marked, that is temporarily eliminated,
855          by find tail. Start the scc algorithm
856          anew on the subgraph thats left (the current cfloop without the backedge)
857          in order to find more inner cfloops. */
858
859       cgscc (tail);
860
861       assert (cg_irg_visited(n));
862       close_loop(l);
863     } else {
864       pop_scc_to_loop(n);
865     }
866   }
867 }
868
869
870
871 static void reset_isbe(void) {
872   int i, n_irgs = get_irp_n_irgs();
873
874   for (i = 0; i < n_irgs; ++i) {
875     int j, n_cs;
876     ir_graph *irg = get_irp_irg(i);
877
878     n_cs = get_irg_n_callers(irg);
879     for (j = 0; j < n_cs; ++j) {
880       irg->caller_isbe[j] = 0;
881     }
882     n_cs = get_irg_n_callees(irg);
883     for (j = 0; j < n_cs; ++j) {
884       irg->callee_isbe[j] = 0;
885     }
886   }
887 }
888
889
890
891
892 /* ----------------------------------------------------------------------------------- */
893 /* Another algorithm to compute recursion nesting depth                                */
894 /* Walk the callgraph.  For each crossed edge increase the loop depth by the edge      */
895 /* weight. Assign graphs the maximal depth.                                            */
896 /* ----------------------------------------------------------------------------------- */
897
898 void compute_loop_depth (ir_graph *irg, void *env) {
899   int current_nesting = *(int *) env;
900   int old_nesting = irg->callgraph_loop_depth;
901   int old_visited = get_cg_irg_visited(irg);
902   int i, n_callees;
903
904   //return ;
905
906   if (cg_irg_visited(irg)) return;
907
908   mark_cg_irg_visited(irg);
909
910   //printf(" old: %d new %d master %d", old_visited, get_cg_irg_visited(irg), master_cg_visited); DDMG(irg);
911
912
913   if (old_nesting < current_nesting)
914     irg->callgraph_loop_depth = current_nesting;
915
916   if (current_nesting > irp->max_callgraph_loop_depth)
917     irp->max_callgraph_loop_depth = current_nesting;
918
919   if ((old_visited +1 < get_cg_irg_visited(irg)) ||   /* not yet visited */
920       (old_nesting < current_nesting)) {        /* propagate larger nesting */
921     /* Don't walk the graph, but a tree that is an unfolded graph. */
922     n_callees = get_irg_n_callees(irg);
923     for (i = 0; i < n_callees; i++) {
924       ir_graph *m = get_irg_callee(irg, i);
925       *(int *)env += get_irg_callee_loop_depth(irg, i);
926       compute_loop_depth(m, env);
927       *(int *)env -= get_irg_callee_loop_depth(irg, i);
928     }
929   }
930
931   set_cg_irg_visited(irg, master_cg_visited-1);
932 }
933
934
935 /* ----------------------------------------------------------------------------------- */
936 /* Another algorithm to compute recursion nesting depth                                */
937 /* Walk the callgraph.  For each crossed loop increase the nesting depth by one.       */
938 /* Assign graphs the maximal nesting depth.   Don't increas if passing loops more than */
939 /* once.                                                                               */
940 /* ----------------------------------------------------------------------------------- */
941
942
943 /* For callees, we want to remember the Call nodes, too. */
944 struct ana_entry2 {
945   ir_loop **loop_stack;
946   int tos;
947   int recursion_nesting;
948 };
949
950 typedef struct ana_entry2 ana_entry2;
951
952 static void push2(ana_entry2 *e, ir_loop *g) {
953   if (ARR_LEN(e->loop_stack) == e->tos) {
954     ARR_APP1(ir_loop *, e->loop_stack, g);
955   } else {
956     e->loop_stack[e->tos] = g;
957   }
958   e->tos ++;
959 }
960
961 static ir_loop *pop2(ana_entry2 *e) {
962   e->tos --;
963   return e->loop_stack[e->tos+1];
964 }
965
966 static int in_stack(ana_entry2 *e, ir_loop *g) {
967   int i;
968   for (i = e->tos-1; i >= 0; --i) {
969     if (e->loop_stack[i] == g) return 1;
970   }
971   return 0;
972 }
973
974 void compute_rec_depth (ir_graph *irg, void *env) {
975   ana_entry2 *e = (ana_entry2 *)env;
976   ir_loop *l = irg->l;
977   int depth, old_depth = irg->callgraph_recursion_depth;
978   int i, n_callees;
979   int pushed = 0;
980
981   if (cg_irg_visited(irg)) return;
982   mark_cg_irg_visited(irg);
983
984   /* -- compute and set the new nesting value -- */
985   if ((l != irp->outermost_cg_loop) && !in_stack(e, l)) {
986     push2(e, l);
987     e->recursion_nesting++;
988     pushed = 1;
989   }
990   depth = e->recursion_nesting;
991
992   if (old_depth < depth)
993     irg->callgraph_recursion_depth = depth;
994
995   if (depth > irp->max_callgraph_recursion_depth)
996     irp->max_callgraph_recursion_depth = depth;
997
998   /* -- spread the nesting value -- */
999   if (depth == 0 || old_depth < depth) {
1000     /* Don't walk the graph, but a tree that is an unfolded graph. */
1001     n_callees = get_irg_n_callees(irg);
1002     for (i = 0; i < n_callees; i++) {
1003       ir_graph *m = get_irg_callee(irg, i);
1004       compute_rec_depth(m, env);
1005     }
1006   }
1007
1008   /* -- clean up -- */
1009   if (pushed) {
1010     pop2(e);
1011     e->recursion_nesting--;
1012   }
1013   set_cg_irg_visited(irg, master_cg_visited-1);
1014 }
1015
1016
1017 /* Compute the backedges that represent recursions. */
1018 void find_callgraph_recursions(void) {
1019   int i, n_irgs = get_irp_n_irgs();
1020
1021   reset_isbe();
1022
1023   /* -- Compute the looptree -- */
1024
1025   /* The outermost graph.  We start here.  Then we start at all
1026      functions in irg list that are never called, then at the remaining
1027      unvisited ones. */
1028   assert(get_irp_main_irg());
1029   outermost_ir_graph = get_irp_main_irg();
1030   init_scc();
1031
1032   current_loop = NULL;
1033   new_loop();  /* sets current_loop */
1034
1035   master_cg_visited++;
1036   cgscc(outermost_ir_graph);
1037   for (i = 0; i < n_irgs; i++) {
1038     ir_graph *irg = get_irp_irg(i);
1039     if (!cg_irg_visited(irg) && get_irg_n_callers(irg) == 0)
1040       cgscc(irg);
1041   }
1042   for (i = 0; i < n_irgs; i++) {
1043     ir_graph *irg = get_irp_irg(i);
1044     if (!cg_irg_visited(irg))
1045       cgscc(irg);
1046   }
1047   irp->outermost_cg_loop = current_loop;
1048
1049   /* -- Reverse the backedge information. -- */
1050   for (i = 0; i < n_irgs; i++) {
1051     ir_graph *irg = get_irp_irg(i);
1052     int j, n_callees = get_irg_n_callees(irg);
1053     for (j = 0; j < n_callees; ++j) {
1054       if (is_irg_callee_backedge(irg, j))
1055         set_irg_caller_backedge(get_irg_callee(irg, j), irg);
1056     }
1057   }
1058
1059   /* -- compute the loop depth  -- */
1060   int current_nesting = 0;
1061   irp->max_callgraph_loop_depth = 0;
1062   master_cg_visited += 2;
1063   //printf (" ** starting at      "); DDMG(get_irp_main_irg());
1064   compute_loop_depth(get_irp_main_irg(), &current_nesting);
1065   for (i = 0; i < n_irgs; i++) {
1066     ir_graph *irg = get_irp_irg(i);
1067     if ((get_cg_irg_visited(irg) < master_cg_visited-1) &&
1068         get_irg_n_callers(irg) == 0) {
1069       compute_loop_depth(irg, &current_nesting);
1070       //printf (" ** starting at      "); DDMG(irg);
1071     }
1072   }
1073   for (i = 0; i < n_irgs; i++) {
1074     ir_graph *irg = get_irp_irg(i);
1075     if (get_cg_irg_visited(irg) < master_cg_visited-1) {
1076       compute_loop_depth(irg, &current_nesting);
1077       //printf (" ** starting at      "); DDMG(irg);
1078     }
1079   }
1080
1081   /* -- compute the recursion depth -- */
1082   ana_entry2 e;
1083   e.loop_stack = NEW_ARR_F(ir_loop *, 0);
1084   e.tos = 0;
1085   e.recursion_nesting = 0;
1086   irp->max_callgraph_recursion_depth = 0;
1087
1088   master_cg_visited += 2;
1089   compute_rec_depth(get_irp_main_irg(), &e);
1090   //printf (" ++ starting at "); DDMG(get_irp_main_irg());
1091   for (i = 0; i < n_irgs; i++) {
1092     ir_graph *irg = get_irp_irg(i);
1093     if ((get_cg_irg_visited(irg) < master_cg_visited-1) &&
1094         get_irg_n_callers(irg) == 0) {
1095       compute_rec_depth(irg, &e);
1096       //printf (" ++ starting at "); DDMG(irg);
1097     }
1098   }
1099   for (i = 0; i < n_irgs; i++) {
1100     ir_graph *irg = get_irp_irg(i);
1101     if (get_cg_irg_visited(irg) < master_cg_visited-1) {
1102       compute_rec_depth(irg, &e);
1103       //printf (" ++ starting at "); DDMG(irg);
1104     }
1105   }
1106
1107   DEL_ARR_F(e.loop_stack);
1108
1109   //dump_callgraph("-with_nesting");
1110
1111   //dump_callgraph_loop_tree(current_loop, "-after_cons");
1112   irp->callgraph_state =  irp_callgraph_and_calltree_consistent;
1113 }
1114
1115 /* Maximal loop depth of all paths from an external visible method to
1116     this irg. */
1117 int       get_irg_loop_depth(ir_graph *irg) {
1118   assert(irp->callgraph_state == irp_callgraph_consistent ||
1119          irp->callgraph_state == irp_callgraph_and_calltree_consistent);
1120   return  irg->callgraph_loop_depth;
1121 }
1122
1123 /* Maximal recursion depth of all paths from an external visible method to
1124    this irg. */
1125 int       get_irg_recursion_depth(ir_graph *irg) {
1126   assert(irp->callgraph_state == irp_callgraph_and_calltree_consistent);
1127   return irg->callgraph_recursion_depth;
1128 }
1129
1130 void analyse_loop_nesting_depth(void) {
1131   entity **free_methods = NULL;
1132   int arr_len;
1133
1134   /* establish preconditions. */
1135   if (get_irp_callee_info_state() != irg_callee_info_consistent) {
1136     cgana(&arr_len, &free_methods);
1137   }
1138
1139   if (irp_callgraph_consistent != get_irp_callgraph_state()) {
1140     compute_callgraph();
1141   }
1142
1143   find_callgraph_recursions();
1144 }