BugFix r19562: get_nodes_block(skip_Proj(get_irn_n(n,i))) is subtile
[libfirm] / ir / ana / ircfscc.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     Compute the strongly connected regions and build backedge/cfloop
23  *            datastructures. A variation on the Tarjan algorithm. See also
24  *            [Trapp:99], Chapter 5.2.1.2.
25  * @author    Goetz Lindenmaier
26  * @date      7.2002
27  * @version   $Id$
28  */
29 #ifdef HAVE_CONFIG_H
30 #include "config.h"
31 #endif
32
33 #ifdef HAVE_STRING_H
34 #include <string.h>
35 #endif
36
37 #include "irloop_t.h"
38 #include "irnode_t.h"
39 #include "irgraph_t.h"
40 #include "array.h"
41 #include "pmap.h"
42 #include "irgwalk.h"
43 #include "irprog_t.h"
44 #include "irdump.h"
45
46 #define NO_CFLOOPS_WITHOUT_HEAD 1
47
48 /** The outermost graph the scc is computed for */
49 static ir_graph *outermost_ir_graph;
50 /** Current cfloop construction is working on. */
51 static ir_loop *current_loop;
52 /** Counts the number of allocated cfloop nodes.
53  * Each cfloop node gets a unique number.
54  * @todo What for? ev. remove.
55  */
56 static int loop_node_cnt = 0;
57 /** Counter to generate depth first numbering of visited nodes. */
58 static int current_dfn = 1;
59
60 static int max_loop_depth = 0;
61
62 void link_to_reg_end(ir_node *n, void *env);
63
64 /**********************************************************************/
65 /* Node attributes                                                   **/
66 /**********************************************************************/
67
68 /**********************************************************************/
69 /* Node attributes needed for the construction.                      **/
70 /**********************************************************************/
71
72 /**
73  * The SCC info. Additional fields for an ir-node needed for the
74  * construction.
75  */
76 typedef struct scc_info {
77         int in_stack;          /**< Marks whether node is on the stack. */
78         int dfn;               /**< Depth first search number. */
79         int uplink;            /**< dfn number of ancestor. */
80 } scc_info;
81
82 /** Allocate a new scc_info on the given obstack */
83 static INLINE scc_info *new_scc_info(struct obstack *obst) {
84         scc_info *info = obstack_alloc(obst, sizeof(*info));
85         memset(info, 0, sizeof(*info));
86         return info;
87 }
88
89 /**
90  * Marks the node n to be on the stack.
91  */
92 static INLINE void mark_irn_in_stack(ir_node *n) {
93         scc_info *info = get_irn_link(n);
94         info->in_stack = 1;
95 }
96
97 /**
98  * Marks the node n to be not on the stack.
99  */
100 static INLINE void mark_irn_not_in_stack(ir_node *n) {
101         scc_info *info = get_irn_link(n);
102         info->in_stack = 0;
103 }
104
105 /**
106  * Returns whether node n is on the stack.
107  */
108 static INLINE int irn_is_in_stack(ir_node *n) {
109         scc_info *info = get_irn_link(n);
110         return info->in_stack;
111 }
112
113 /**
114  * Sets node n uplink value.
115  */
116 static INLINE void set_irn_uplink(ir_node *n, int uplink) {
117         scc_info *info = get_irn_link(n);
118         info->uplink = uplink;
119 }
120
121 /**
122  * Return node n uplink value.
123  */
124 static INLINE int get_irn_uplink(ir_node *n) {
125         scc_info *info = get_irn_link(n);
126         return info->uplink;
127 }
128
129 /**
130  * Sets node n dfn value.
131  */
132 static INLINE void set_irn_dfn(ir_node *n, int dfn) {
133         scc_info *info = get_irn_link(n);
134         info->dfn = dfn;
135 }
136
137 /**
138  * Returns node n dfn value.
139  */
140 static INLINE int get_irn_dfn(ir_node *n) {
141         scc_info *info = get_irn_link(n);
142         return info->dfn;
143 }
144
145 /**********************************************************************/
146 /* A stack.                                                          **/
147 /**********************************************************************/
148
149 /** An IR-node stack */
150 static ir_node **stack = NULL;
151 /** The top (index) of the IR-node stack */
152 static int tos = 0;
153
154 /**
155  * Initializes the IR-node stack
156  */
157 static INLINE void init_stack(void) {
158         if (stack) {
159                 ARR_RESIZE(ir_node *, stack, 1000);
160         } else {
161                 stack = NEW_ARR_F(ir_node *, 1000);
162         }
163         tos = 0;
164 }
165
166 /**
167  * Push a node n onto the IR-node stack.
168  */
169 static INLINE void push(ir_node *n) {
170         if (tos == ARR_LEN(stack)) {
171                 int nlen = ARR_LEN(stack) * 2;
172                 ARR_RESIZE(ir_node *, stack, nlen);
173         }
174         stack[tos++] = n;
175         mark_irn_in_stack(n);
176 }
177
178 /**
179  * Pop a node from the IR-node stack and return it.
180  */
181 static INLINE ir_node *pop(void) {
182         ir_node *n = stack[--tos];
183         mark_irn_not_in_stack(n);
184         return n;
185 }
186
187 /**
188  * The nodes from tos up to n belong to the current loop.
189  * Removes them from the stack and adds them to the current loop.
190  */
191 static INLINE void pop_scc_to_loop(ir_node *n) {
192         ir_node *m;
193
194         do {
195                 m = pop();
196                 loop_node_cnt++;
197                 set_irn_dfn(m, loop_node_cnt);
198                 add_loop_node(current_loop, m);
199                 set_irn_loop(m, current_loop);
200         } while (m != n);
201 }
202
203 /* GL ??? my last son is my grandson???  Removes cfloops with no
204    ir_nodes in them.  Such loops have only another loop as son. (Why
205    can't they have two loops as sons? Does it never get that far? ) */
206 static void close_loop(ir_loop *l) {
207         int last = get_loop_n_elements(l) - 1;
208         loop_element lelement = get_loop_element(l, last);
209         ir_loop *last_son = lelement.son;
210
211         if (get_kind(last_son) == k_ir_loop &&
212             get_loop_n_elements(last_son) == 1) {
213                 ir_loop *gson;
214
215                 lelement = get_loop_element(last_son, 0);
216                 gson = lelement.son;
217                 if (get_kind(gson) == k_ir_loop) {
218                         loop_element new_last_son;
219
220                         gson->outer_loop = l;
221                         new_last_son.son = gson;
222                         l->children[last] = new_last_son;
223
224                         /* the loop last_son is dead now, recover at least some memory */
225                         DEL_ARR_F(last_son->children);
226                 }
227         }
228
229         current_loop = l;
230 }
231
232 /**
233  * Removes and unmarks all nodes up to n from the stack.
234  * The nodes must be visited once more to assign them to a scc.
235  */
236 static INLINE void pop_scc_unmark_visit(ir_node *n) {
237         ir_node *m;
238
239         do {
240                 m = pop();
241                 set_irn_visited(m, 0);
242         } while (m != n);
243 }
244
245 /**********************************************************************/
246 /* The loop datastructure.                                           **/
247 /**********************************************************************/
248
249 /**
250  * Allocates a new loop as son of current_loop.  Sets current_loop
251  * to the new loop and returns its father.
252  * The loop is allocated on the outermost_ir_graphs's obstack.
253  */
254 static ir_loop *new_loop(void) {
255         ir_loop *father = current_loop;
256         ir_loop *son    = alloc_loop(father, outermost_ir_graph->obst);
257
258         if (son->depth > max_loop_depth) max_loop_depth = son->depth;
259         current_loop = son;
260         return father;
261 }
262
263 /**********************************************************************/
264 /* Constructing and destructing the loop/backedge information.       **/
265 /**********************************************************************/
266
267 /* Initialization steps. **********************************************/
268
269 /**
270  * Allocates a scc_info for every Block node n.
271  * Clear the backedges for all nodes.
272  * Called from a walker.
273  */
274 static INLINE void init_node(ir_node *n, void *env) {
275         struct obstack *obst = env;
276         if (is_Block(n))
277                 set_irn_link(n, new_scc_info(obst));
278         clear_backedges(n);
279 }
280
281 /**
282  * Initializes the common global settings for the scc algorithm
283  */
284 static INLINE void init_scc_common(void) {
285         current_dfn   = 1;
286         loop_node_cnt = 0;
287         init_stack();
288 }
289
290 /**
291  * Initializes the scc algorithm for the intraprocedural case.
292  * Add scc info to every block node.
293  */
294 static INLINE void init_scc(ir_graph *irg, struct obstack *obst) {
295         init_scc_common();
296         irg_walk_graph(irg, init_node, NULL, obst);
297 }
298
299 #ifdef INTERPROCEDURAL_VIEW
300 /**
301  * Initializes the scc algorithm for the interprocedural case.
302  */
303 static INLINE void init_ip_scc(struct obstack *obst) {
304         init_scc_common();
305         cg_walk(init_node, NULL, obst);
306
307 #if EXPERIMENTAL_CFLOOP_TREE
308         cg_walk(link_to_reg_end, NULL, NULL);
309 #endif
310 }
311 #endif
312
313 /**
314  * Condition for breaking the recursion: n is the block
315  * that gets the initial control flow from the Start node.
316  */
317 static int is_outermost_StartBlock(ir_node *n) {
318         /* Test whether this is the outermost Start node.  If so
319            recursion must end. */
320         assert(is_Block(n));
321         if (get_Block_n_cfgpreds(n) == 1  &&
322             is_Start(skip_Proj(get_Block_cfgpred(n, 0))) &&
323             get_Block_cfgpred_block(n, 0) == n) {
324                 return 1;
325         }
326         return 0;
327 }
328
329 /** Returns non-zero if n is a loop header, i.e., it is a Block node
330  *  and has predecessors within the cfloop and out of the cfloop.
331  *
332  *  @param n     the block node to check
333  *  @param root  only needed for assertion.
334  */
335 static int is_head(ir_node *n, ir_node *root) {
336         int i, arity;
337         int some_outof_loop = 0, some_in_loop = 0;
338
339         assert(is_Block(n));
340
341         if (!is_outermost_StartBlock(n)) {
342                 arity = get_Block_n_cfgpreds(n);
343                 for (i = 0; i < arity; i++) {
344                         ir_node *pred = get_nodes_block(skip_Proj(get_irn_n(n, i)));
345                         if (is_backedge(n, i)) continue;
346                         if (!irn_is_in_stack(pred)) {
347                                 some_outof_loop = 1;
348                         } else {
349                                 assert(get_irn_uplink(pred) >= get_irn_uplink(root));
350                                 some_in_loop = 1;
351                         }
352                 }
353         }
354         return some_outof_loop & some_in_loop;
355 }
356
357
358 /**
359  * Returns non-zero if n is possible loop head of an endless loop.
360  * I.e., it is a Block node and has only predecessors
361  * within the loop.
362  *
363  * @param n     the block node to check
364  * @param root  only needed for assertion.
365  */
366 static int is_endless_head(ir_node *n, ir_node *root) {
367         int i, arity;
368         int some_outof_loop = 0, some_in_loop = 0;
369
370         assert(is_Block(n));
371         /* Test for legal loop header: Block, Phi, ... */
372         if (!is_outermost_StartBlock(n)) {
373                 arity = get_Block_n_cfgpreds(n);
374                 for (i = 0; i < arity; i++) {
375                         ir_node *pred = get_nodes_block(skip_Proj(get_irn_n(n, i)));
376                         if (is_backedge(n, i))
377                                 continue;
378                         if (!irn_is_in_stack(pred)) {
379                                 some_outof_loop = 1;
380                         } else {
381                                 assert(get_irn_uplink(pred) >= get_irn_uplink(root));
382                                 some_in_loop = 1;
383                         }
384                 }
385         }
386         return !some_outof_loop && some_in_loop;
387 }
388
389 /**
390  * Returns index of the predecessor with the smallest dfn number
391  * greater-equal than limit.
392  */
393 static int smallest_dfn_pred(ir_node *n, int limit) {
394         int i, index = -2, min = -1;
395
396         if (!is_outermost_StartBlock(n)) {
397                 int arity = get_Block_n_cfgpreds(n);
398                 for (i = 0; i < arity; i++) {
399                         ir_node *pred = get_nodes_block(skip_Proj(get_irn_n(n, i)));
400                         if (is_backedge(n, i) || !irn_is_in_stack(pred))
401                                 continue;
402                         if (get_irn_dfn(pred) >= limit && (min == -1 || get_irn_dfn(pred) < min)) {
403                                 index = i;
404                                 min = get_irn_dfn(pred);
405                         }
406                 }
407         }
408         return index;
409 }
410
411 /**
412  * Returns index of the predecessor with the largest dfn number.
413  */
414 static int largest_dfn_pred(ir_node *n) {
415         int i, index = -2, max = -1;
416
417         if (!is_outermost_StartBlock(n)) {
418                 int arity = get_Block_n_cfgpreds(n);
419                 for (i = 0; i < arity; i++) {
420                         ir_node *pred = get_nodes_block(skip_Proj(get_irn_n(n, i)));
421                         if (is_backedge(n, i) || !irn_is_in_stack(pred))
422                                 continue;
423                         if (get_irn_dfn(pred) > max) {
424                                 index = i;
425                                 max = get_irn_dfn(pred);
426                         }
427                 }
428         }
429         return index;
430 }
431
432 /**
433  * Searches the stack for possible loop heads.  Tests these for backedges.
434  * If it finds a head with an unmarked backedge it marks this edge and
435  * returns the tail of the loop.
436  * If it finds no backedge returns NULL.
437  */
438 static ir_node *find_tail(ir_node *n) {
439         ir_node *m;
440         int i, res_index = -2;
441
442         m = stack[tos-1];  /* tos = top of stack */
443         if (is_head(m, n)) {
444                 res_index = smallest_dfn_pred(m, 0);
445                 if ((res_index == -2) &&  /* no smallest dfn pred found. */
446                         (n ==  m))
447                         return NULL;
448         } else {
449                 if (m == n)
450                         return NULL;
451                 for (i = tos-2; i >= 0; --i) {
452
453                         m = stack[i];
454                         if (is_head(m, n)) {
455                                 res_index = smallest_dfn_pred(m, get_irn_dfn(m) + 1);
456                                 if (res_index == -2)  /* no smallest dfn pred found. */
457                                         res_index = largest_dfn_pred(m);
458
459                                 if ((m == n) && (res_index == -2)) {
460                                         i = -1;
461                                 }
462                                 break;
463                         }
464
465
466                         /* We should not walk past our selves on the stack:  The upcoming nodes
467                            are not in this loop. We assume a loop not reachable from Start. */
468                         if (m == n) {
469                                 i = -1;
470                                 break;
471                         }
472                 }
473
474                 if (i < 0) {
475                         /* A dead loop not reachable from Start. */
476                         for (i = tos-2; i >= 0; --i) {
477                                 m = stack[i];
478                                 if (is_endless_head (m, n)) {
479                                         res_index = smallest_dfn_pred (m, get_irn_dfn(m) + 1);
480                                         if (res_index == -2)  /* no smallest dfn pred found. */
481                                                 res_index = largest_dfn_pred (m);
482                                         break;
483                                 }
484                                 if (m == n) break;   /* It's not an unreachable loop, either. */
485                         }
486                         //assert(0 && "no head found on stack");
487                 }
488         }
489         assert(res_index > -2);
490
491         set_backedge(m, res_index);
492         return is_outermost_StartBlock(n) ? NULL : get_nodes_block(skip_Proj(get_irn_n(m, res_index)));
493 }
494
495 /**
496  * returns non.zero if l is the outermost loop.
497  */
498 INLINE static int is_outermost_loop(ir_loop *l) {
499         return l == get_loop_outer_loop(l);
500 }
501
502 /*-----------------------------------------------------------*
503  *                   The core algorithm.                     *
504  *-----------------------------------------------------------*/
505
506 /**
507  * Walks over all blocks of a graph
508  */
509 static void cfscc(ir_node *n) {
510         int i;
511
512         assert(is_Block(n));
513
514         if (irn_visited(n)) return;
515         mark_irn_visited(n);
516
517         /* Initialize the node */
518         set_irn_dfn(n, current_dfn);      /* Depth first number for this node */
519         set_irn_uplink(n, current_dfn);   /* ... is default uplink. */
520         set_irn_loop(n, NULL);
521         ++current_dfn;
522         push(n);
523
524         if (!is_outermost_StartBlock(n)) {
525                 int arity = get_Block_n_cfgpreds(n);
526
527                 for (i = 0; i < arity; i++) {
528                         ir_node *m;
529
530                         if (is_backedge(n, i))
531                                 continue;
532                         m = get_nodes_block(skip_Proj(get_irn_n(n, i)));
533
534                         cfscc(m);
535                         if (irn_is_in_stack(m)) {
536                                 /* Uplink of m is smaller if n->m is a backedge.
537                                    Propagate the uplink to mark the cfloop. */
538                                 if (get_irn_uplink(m) < get_irn_uplink(n))
539                                         set_irn_uplink(n, get_irn_uplink(m));
540                         }
541                 }
542         }
543
544         if (get_irn_dfn(n) == get_irn_uplink(n)) {
545                 /* This condition holds for
546                    1) the node with the incoming backedge.
547                       That is: We found a cfloop!
548                    2) Straight line code, because no uplink has been propagated, so the
549                       uplink still is the same as the dfn.
550
551                    But n might not be a proper cfloop head for the analysis. Proper cfloop
552                    heads are Block and Phi nodes. find_tail searches the stack for
553                    Block's and Phi's and takes those nodes as cfloop heads for the current
554                    cfloop instead and marks the incoming edge as backedge. */
555
556                 ir_node *tail = find_tail(n);
557                 if (tail) {
558                         /* We have a cfloop, that is no straight line code,
559                            because we found a cfloop head!
560                            Next actions: Open a new cfloop on the cfloop tree and
561                            try to find inner cfloops */
562
563 #if NO_CFLOOPS_WITHOUT_HEAD
564
565                         /* This is an adaption of the algorithm from fiasco / optscc to
566                          * avoid cfloops without Block or Phi as first node.  This should
567                          * severely reduce the number of evaluations of nodes to detect
568                          * a fixpoint in the heap analysis.
569                          * Further it avoids cfloops without firm nodes that cause errors
570                          * in the heap analyses. */
571
572                         ir_loop *l;
573                         int close;
574                         if ((get_loop_n_elements(current_loop) > 0) || (is_outermost_loop(current_loop))) {
575                                 l = new_loop();
576                                 close = 1;
577                         } else {
578                                 l = current_loop;
579                                 close = 0;
580                         }
581
582 #else
583
584                         ir_loop *l = new_loop();
585
586 #endif
587
588                         /* Remove the cfloop from the stack ... */
589                         pop_scc_unmark_visit (n);
590
591                         /* The current backedge has been marked, that is temporarily eliminated,
592                            by find tail. Start the scc algorithm
593                            anew on the subgraph thats left (the current cfloop without the backedge)
594                            in order to find more inner cfloops. */
595
596                         cfscc(tail);
597
598                         assert(irn_visited(n));
599 #if NO_CFLOOPS_WITHOUT_HEAD
600                         if (close)
601 #endif
602                                 close_loop(l);
603                 } else {
604                         /* AS: No cfloop head was found, that is we have straight line code.
605                                Pop all nodes from the stack to the current cfloop. */
606                         pop_scc_to_loop(n);
607                 }
608         }
609 }
610
611 /* Constructs control flow backedge information for irg. */
612 int construct_cf_backedges(ir_graph *irg) {
613         ir_graph *rem = current_ir_graph;
614         ir_loop *head_rem;
615         ir_node *end = get_irg_end(irg);
616         struct obstack temp;
617         int i;
618
619         assert(!get_interprocedural_view() &&
620                 "use construct_ip_cf_backedges()");
621         max_loop_depth = 0;
622
623         current_ir_graph   = irg;
624         outermost_ir_graph = irg;
625
626         obstack_init(&temp);
627         init_scc(irg, &temp);
628
629         current_loop = NULL;
630         new_loop();  /* sets current_loop */
631         head_rem = current_loop; /* Just for assertion */
632
633         inc_irg_visited(irg);
634
635         /* walk over all blocks of the graph, including keep alives */
636         cfscc(get_irg_end_block(irg));
637         for (i = get_End_n_keepalives(end) - 1; i >= 0; --i) {
638                 ir_node *el = get_End_keepalive(end, i);
639                 if (is_Block(el))
640                         cfscc(el);
641         }
642         obstack_free(&temp, NULL);
643
644         assert(head_rem == current_loop);
645         mature_loops(current_loop, irg->obst);
646         set_irg_loop(irg, current_loop);
647         set_irg_loopinfo_state(irg, loopinfo_cf_consistent);
648         assert(get_irg_loop(irg)->kind == k_ir_loop);
649
650         current_ir_graph = rem;
651         return max_loop_depth;
652 }
653
654 void assure_cf_loop(ir_graph *irg) {
655         irg_loopinfo_state state = get_irg_loopinfo_state(irg);
656
657         if (state != loopinfo_cf_consistent)
658                 construct_cf_backedges(irg);
659 }
660
661 #ifdef INTERPROCEDURAL_VIEW
662 int construct_ip_cf_backedges (void) {
663         ir_graph *rem = current_ir_graph;
664         int rem_ipv = get_interprocedural_view();
665         struct obstack temp;
666         int i;
667
668         assert(get_irp_ip_view_state() == ip_view_valid);
669         max_loop_depth = 0;
670         outermost_ir_graph = get_irp_main_irg();
671
672         obstack_init(&temp);
673         init_ip_scc(&temp);
674
675         current_loop = NULL;
676         new_loop();  /* sets current_loop */
677         set_interprocedural_view(1);
678
679         inc_max_irg_visited();
680         for (i = 0; i < get_irp_n_irgs(); i++)
681           set_irg_visited(get_irp_irg(i), get_max_irg_visited());
682
683         /** We have to start the walk at the same nodes as cg_walk. **/
684         /* Walk starting at unreachable procedures. Only these
685          * have End blocks visible in interprocedural view. */
686         for (i = 0; i < get_irp_n_irgs(); i++) {
687                 ir_node *sb;
688                 current_ir_graph = get_irp_irg(i);
689
690                 sb = get_irg_start_block(current_ir_graph);
691
692                 if ((get_Block_n_cfgpreds(sb) > 1) ||
693                         (get_nodes_block(get_Block_cfgpred(sb, 0)) != sb)) continue;
694
695                 cfscc(get_irg_end_block(current_ir_graph));
696         }
697
698         /* Check whether we walked all procedures: there could be procedures
699            with cyclic calls but no call from the outside. */
700         for (i = 0; i < get_irp_n_irgs(); i++) {
701                 ir_node *sb;
702                 current_ir_graph = get_irp_irg(i);
703
704                 /* Test start block: if inner procedure end and end block are not
705                  * visible and therefore not marked. */
706                 sb = get_irg_start_block(current_ir_graph);
707                 if (get_irn_visited(sb) < get_irg_visited(current_ir_graph)) cfscc(sb);
708         }
709
710         /* Walk all endless cfloops in inner procedures.
711          * We recognize an inner procedure if the End node is not visited. */
712         for (i = 0; i < get_irp_n_irgs(); i++) {
713                 ir_node *e;
714                 current_ir_graph = get_irp_irg(i);
715
716                 e = get_irg_end(current_ir_graph);
717                 if (get_irn_visited(e) < get_irg_visited(current_ir_graph)) {
718                         int j;
719                         /* Don't visit the End node. */
720                         for (j = 0; j < get_End_n_keepalives(e); j++) {
721                                 ir_node *el = get_End_keepalive(e, j);
722                                 if (is_Block(el)) cfscc(el);
723                         }
724                 }
725         }
726
727         set_irg_loop(outermost_ir_graph, current_loop);
728         set_irg_loopinfo_state(current_ir_graph, loopinfo_cf_ip_consistent);
729         assert(get_irg_loop(outermost_ir_graph)->kind == k_ir_loop);
730
731         obstack_free(&temp, NULL);
732         current_ir_graph = rem;
733         set_interprocedural_view(rem_ipv);
734         return max_loop_depth;
735 }
736 #endif
737
738 /**
739  * Clear the intra- and the interprocedural
740  * backedge information pf a block.
741  */
742 static void reset_backedges(ir_node *block) {
743         int rem;
744
745         assert(is_Block(block));
746 #ifdef INTERPROCEDURAL_VIEW
747         rem = get_interprocedural_view();
748         set_interprocedural_view(1);
749         clear_backedges(block);
750         set_interprocedural_view(0);
751         clear_backedges(block);
752         set_interprocedural_view(rem);
753 #else
754         (void) rem;
755         clear_backedges(block);
756 #endif
757 }
758
759 /**
760  * Reset all backedges of the first block of
761  * a loop as well as all loop info for all nodes of this loop.
762  * Recurse into all nested loops.
763  */
764 static void loop_reset_backedges(ir_loop *l) {
765         int i;
766         reset_backedges(get_loop_node(l, 0));
767         for (i = 0; i < get_loop_n_nodes(l); ++i)
768                 set_irn_loop(get_loop_node(l, i), NULL);
769         for (i = 0; i < get_loop_n_sons(l); ++i) {
770                 loop_reset_backedges(get_loop_son(l, i));
771         }
772 }
773
774 /* Removes all cfloop information.
775    Resets all backedges */
776 void free_cfloop_information(ir_graph *irg) {
777         ir_loop *loop = get_irg_loop(irg);
778         if (loop != NULL) {
779                 loop_reset_backedges(loop);
780                 set_irg_loop(irg, NULL);
781         }
782         set_irg_loopinfo_state(irg, loopinfo_none);
783         /* We cannot free the cfloop nodes, they are on the obstack. */
784 }
785
786
787 void free_all_cfloop_information(void) {
788         int i;
789 #ifdef INTERPROCEDURAL_VIEW
790         int rem = get_interprocedural_view();
791         set_interprocedural_view(1);  /* To visit all filter nodes */
792 #endif
793         for (i = get_irp_n_irgs() - 1; i >= 0; --i) {
794                 free_cfloop_information(get_irp_irg(i));
795         }
796 #ifdef INTERPROCEDURAL_VIEW
797         set_interprocedural_view(rem);
798 #endif
799 }