edae4265e8296ca315e848049dbd3670eb9ff50f
[libfirm] / ir / ana / irscc.c
1 /*
2  * Copyright (C) 1995-2011 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
23  *              backedge/loop datastructures.
24  *              A variation on the Tarjan algorithm. See also [Trapp:99],
25  *              Chapter 5.2.1.2.
26  * @author   Goetz Lindenmaier
27  * @date     7.2002
28  */
29 #include "config.h"
30
31 #include <string.h>
32 #include <stdlib.h>
33
34 #include "irloop_t.h"
35
36 #include "irprog_t.h"
37 #include "irgraph_t.h"
38 #include "irnode_t.h"
39 #include "irgwalk.h"
40 #include "irdump.h"
41 #include "array.h"
42 #include "pmap.h"
43 #include "ircons.h"
44
45 /** The outermost graph the scc is computed for. */
46 static ir_graph *outermost_ir_graph;
47 /** Current loop construction is working on. */
48 static ir_loop *current_loop;
49 /** Counts the number of allocated loop nodes.
50  *  Each loop node gets a unique number.
51  *  @todo What for? ev. remove.
52  */
53 static int loop_node_cnt = 0;
54 /** Counter to generate depth first numbering of visited nodes. */
55 static int current_dfn = 1;
56
57 static unsigned max_loop_depth = 0;
58
59 void link_to_reg_end(ir_node *n, void *env);
60 void set_projx_link(ir_node *cb_projx, ir_node *end_projx);
61 ir_node *get_projx_link(ir_node *cb_projx);
62
63 /**********************************************************************/
64 /* Node attributes                                                   **/
65 /**********************************************************************/
66
67 /**********************************************************************/
68 /* Node attributes needed for the construction.                      **/
69 /**********************************************************************/
70
71 typedef struct scc_info {
72         int in_stack;          /**< Marks whether node is on the stack. */
73         int dfn;               /**< Depth first search number. */
74         int uplink;            /**< dfn number of ancestor. */
75         /*  ir_loop *loop;         *//* Refers to the containing loop. */
76         /*
77             struct section *section;
78             xset def;
79             xset use;
80         */
81 } scc_info;
82
83 /**
84  * Allocates a new SCC info on the given obstack.
85  */
86 static inline scc_info *new_scc_info(struct obstack *obst)
87 {
88         return OALLOCZ(obst, scc_info);
89 }
90
91 /**
92  * Mark node n being on the SCC stack.
93  */
94 static inline void mark_irn_in_stack(ir_node *n)
95 {
96         scc_info *scc = (scc_info*) get_irn_link(n);
97         assert(scc);
98         scc->in_stack = 1;
99 }
100
101 /**
102 * Mark node n NOT being on the SCC stack.
103 */
104 static inline void mark_irn_not_in_stack(ir_node *n)
105 {
106         scc_info *scc = (scc_info*) get_irn_link(n);
107         assert(scc);
108         scc->in_stack = 0;
109 }
110
111 /**
112  * Checks if a node is on the SCC stack.
113  */
114 static inline int irn_is_in_stack(ir_node *n)
115 {
116         scc_info *scc = (scc_info*) get_irn_link(n);
117         assert(scc);
118         return scc->in_stack;
119 }
120
121 /**
122  * Sets the uplink number for a node.
123  */
124 static inline void set_irn_uplink(ir_node *n, int uplink)
125 {
126         scc_info *scc = (scc_info*) get_irn_link(n);
127         assert(scc);
128         scc->uplink = uplink;
129 }
130
131 /**
132  * Returns the uplink number for a node.
133  */
134 static int get_irn_uplink(ir_node *n)
135 {
136         scc_info *scc = (scc_info*) get_irn_link(n);
137         assert(scc);
138         return scc->uplink;
139 }
140
141 /**
142  * Sets the depth-first-search number for a node.
143  */
144 static inline void set_irn_dfn(ir_node *n, int dfn)
145 {
146         scc_info *scc = (scc_info*) get_irn_link(n);
147         assert(scc);
148         scc->dfn = dfn;
149 }
150
151 /**
152  * Returns the depth-first-search number of a node.
153  */
154 static int get_irn_dfn(ir_node *n)
155 {
156         scc_info *scc = (scc_info*) get_irn_link(n);
157         assert(scc);
158         return scc->dfn;
159 }
160
161 #if 0
162 static ir_loop *find_nodes_loop(ir_node *n, ir_loop *l)
163 {
164         int i;
165         ir_loop *res = NULL;
166
167         /* Test whether n is contained in this loop. */
168         for (i = 0; i < get_loop_n_nodes(l); i++)
169                 if (n == get_loop_node(l, i)) return l;
170
171         /* Is this a leave in the loop tree? If so loop not found. */
172         if (get_loop_n_sons(l) == 0) return NULL;
173
174         /* Else descend in the loop tree. */
175         for (i = 0; i < get_loop_n_sons(l); i++) {
176                 res = find_nodes_loop(n, get_loop_son(l, i));
177                 if (res) break;
178         }
179         return res;
180 }
181
182 /* @@@ temporary implementation, costly!!! */
183 ir_loop * get_irn_loop(ir_node *n)
184 {
185         ir_loop *l = get_irg_loop(current_ir_graph);
186         l = find_nodes_loop(n, l);
187         return l;
188 }
189 #endif
190
191 /**********************************************************************/
192 /* A stack.                                                          **/
193 /**********************************************************************/
194
195 static ir_node **stack = NULL;
196 static size_t tos = 0;                /* top of stack */
197
198 /**
199  * initializes the stack
200  */
201 static inline void init_stack(void)
202 {
203         if (stack) {
204                 ARR_RESIZE(ir_node *, stack, 1000);
205         } else {
206                 stack = NEW_ARR_F(ir_node *, 1000);
207         }
208         tos = 0;
209 }
210
211 /**
212  * Frees the stack.
213  */
214 static void finish_stack(void)
215 {
216         DEL_ARR_F(stack);
217         stack = NULL;
218 }
219
220 /**
221  * push a node onto the stack
222  *
223  * @param n  The node to push
224  */
225 static inline void push(ir_node *n)
226 {
227         if (tos == ARR_LEN(stack)) {
228                 size_t nlen = ARR_LEN(stack) * 2;
229                 ARR_RESIZE(ir_node *, stack, nlen);
230         }
231         stack[tos++] = n;
232         mark_irn_in_stack(n);
233 }
234
235 /**
236  * pop a node from the stack
237  *
238  * @return  The topmost node
239  */
240 static inline ir_node *pop(void)
241 {
242         ir_node *n;
243
244         assert(tos > 0);
245         n = stack[--tos];
246         mark_irn_not_in_stack(n);
247         return n;
248 }
249
250 /**
251  * The nodes up to n belong to the current loop.
252  * Removes them from the stack and adds them to the current loop.
253  */
254 static inline void pop_scc_to_loop(ir_node *n)
255 {
256         ir_node *m;
257
258         do {
259                 m = pop();
260
261                 loop_node_cnt++;
262                 set_irn_dfn(m, loop_node_cnt);
263                 add_loop_node(current_loop, m);
264                 set_irn_loop(m, current_loop);
265         } while (m != n);
266 }
267
268 /* GL ??? my last son is my grandson???  Removes loops with no
269    ir_nodes in them.  Such loops have only another loop as son. (Why
270    can't they have two loops as sons? Does it never get that far? ) */
271 static void close_loop(ir_loop *l)
272 {
273         size_t last = get_loop_n_elements(l) - 1;
274         loop_element lelement = get_loop_element(l, last);
275         ir_loop *last_son = lelement.son;
276
277         if (get_kind(last_son) == k_ir_loop &&
278                 get_loop_n_elements(last_son) == 1) {
279                         ir_loop *gson;
280
281                         lelement = get_loop_element(last_son, 0);
282                         gson = lelement.son;
283
284                         if (get_kind(gson) == k_ir_loop) {
285                                 loop_element new_last_son;
286
287                                 gson->outer_loop = l;
288                                 new_last_son.son = gson;
289                                 l->children[last] = new_last_son;
290                         }
291         }
292
293         current_loop = l;
294 }
295
296 /* Removes and unmarks all nodes up to n from the stack.
297    The nodes must be visited once more to assign them to a scc. */
298 static inline void pop_scc_unmark_visit(ir_node *n)
299 {
300         ir_node *m = NULL;
301
302         while (m != n) {
303                 m = pop();
304                 set_irn_visited(m, 0);
305         }
306 }
307
308 /**********************************************************************/
309 /* The loop datastructure.                                           **/
310 /**********************************************************************/
311
312 /* Allocates a new loop as son of current_loop.  Sets current_loop
313    to the new loop and returns the father. */
314 static ir_loop *new_loop(void)
315 {
316         ir_loop *father = current_loop;
317         ir_loop *son    = alloc_loop(father, get_irg_obstack(outermost_ir_graph));
318
319         if (son->depth > max_loop_depth) max_loop_depth = son->depth;
320         current_loop = son;
321         return father;
322 }
323
324 /**********************************************************************/
325 /* Constructing and destructing the loop/backedge information.       **/
326 /**********************************************************************/
327
328 /* Initialization steps. **********************************************/
329
330 static inline void init_node(ir_node *n, void *env)
331 {
332         struct obstack *obst = (struct obstack*) env;
333         set_irn_link(n, new_scc_info(obst));
334         clear_backedges(n);
335 }
336
337 static inline void init_scc_common(void)
338 {
339         current_dfn = 1;
340         loop_node_cnt = 0;
341         init_stack();
342 }
343
344 static inline void init_scc(ir_graph *irg, struct obstack *obst)
345 {
346         init_scc_common();
347         irg_walk_graph(irg, init_node, NULL, obst);
348         /*
349         irg_walk (irg, link_to_reg_end, NULL, NULL);
350         */
351 }
352
353 static inline void finish_scc(void)
354 {
355         finish_stack();
356 }
357
358 /**
359  * Check whether a given node represents the outermost Start
360  * block. In intra-procedural view this is the start block of the
361  * current graph, in interprocedural view it is the start block
362  * of the outer most graph.
363  *
364  * @param n  the node to check
365  *
366  * This is the condition for breaking the scc recursion.
367  */
368 static int is_outermost_Start(ir_node *n)
369 {
370         /* Test whether this is the outermost Start node. */
371         if (is_Block(n) && get_Block_n_cfgpreds(n) == 1) {
372                 ir_node *pred = skip_Proj(get_Block_cfgpred(n, 0));
373             if (is_Start(pred) && get_nodes_block(pred) == n)
374                         return 1;
375         }
376         return 0;
377 }
378
379 /* When to walk from nodes to blocks. Only for Control flow operations? */
380 static inline int get_start_index(ir_node *n)
381 {
382 #undef BLOCK_BEFORE_NODE
383 #define BLOCK_BEFORE_NODE 1
384
385 #if BLOCK_BEFORE_NODE
386
387         /* This version assures, that all nodes are ordered absolutely.  This allows
388            to undef all nodes in the heap analysis if the block is false, which
389            means not reachable.
390            I.e., with this code, the order on the loop tree is correct. But a
391            (single) test showed the loop tree is deeper. */
392         if (is_Phi(n)   ||
393             is_Block(n) ||
394             (get_irg_pinned(get_irn_irg(n)) == op_pin_state_floats &&
395               get_irn_pinned(n)              == op_pin_state_floats))
396                 // Here we could test for backedge at -1 which is illegal
397                 return 0;
398         else
399                 return -1;
400
401 #else
402
403         /* This version causes deeper loop trees (at least we verified this
404            for Polymor).
405            But it guarantees that Blocks are analysed before nodes contained in the
406            block.  If so, we can set the value to undef if the block is not \
407            executed. */
408         if (is_cfop(n) || is_fragile_op(n) || is_Start(n))
409                 return -1;
410         else
411                 return 0;
412
413 #endif
414 }
415
416 /**
417  * Return non-zero if the given node is a legal loop header:
418  * Block, Phi
419  *
420  * @param n  the node to check
421  */
422 static inline int is_possible_loop_head(ir_node *n)
423 {
424         return is_Block(n) || is_Phi(n);
425 }
426
427 /**
428  * Returns non-zero if n is a loop header, i.e., it is a Block or Phi
429  * node and has predecessors within the loop and out of the loop.
430  *
431  * @param n    the node to check
432  * @param root only needed for assertion.
433  */
434 static int is_head(ir_node *n, ir_node *root)
435 {
436         int i, arity;
437         int some_outof_loop = 0, some_in_loop = 0;
438
439         /* Test for legal loop header: Block, Phi, ... */
440         if (!is_possible_loop_head(n))
441                 return 0;
442
443         if (!is_outermost_Start(n)) {
444 #ifndef NDEBUG
445                 int uplink = get_irn_uplink(root);
446 #else
447                 (void) root;
448 #endif
449                 arity = get_irn_arity(n);
450                 for (i = get_start_index(n); i < arity; i++) {
451                         ir_node *pred;
452                         if (is_backedge(n, i))
453                                 continue;
454                         pred = get_irn_n(n, i);
455                         if (! irn_is_in_stack(pred)) {
456                                 some_outof_loop = 1;
457                         } else {
458                                 assert(get_irn_uplink(pred) >= uplink);
459                                 some_in_loop = 1;
460                         }
461                 }
462         }
463         return some_outof_loop & some_in_loop;
464 }
465
466 /**
467  * Returns non-zero if n is possible loop head of an endless loop.
468  * I.e., it is a Block or Phi node and has only predecessors
469  * within the loop.
470  *
471  * @param n    the node to check
472  * @param root only needed for assertion.
473  */
474 static int is_endless_head(ir_node *n, ir_node *root)
475 {
476         int i, arity;
477         int none_outof_loop = 1, some_in_loop = 0;
478
479         /* Test for legal loop header: Block, Phi, ... */
480         if (!is_possible_loop_head(n))
481                 return 0;
482
483         if (!is_outermost_Start(n)) {
484 #ifndef NDEBUG
485                 int uplink = get_irn_uplink(root);
486 #else
487                 (void) root;
488 #endif
489                 arity = get_irn_arity(n);
490                 for (i = get_start_index(n); i < arity; i++) {
491                         ir_node *pred;
492                         if (is_backedge(n, i))
493                                 continue;
494                         pred = get_irn_n(n, i);
495                         if (!irn_is_in_stack(pred)) {
496                                 none_outof_loop = 0;
497                         } else {
498                                 assert(get_irn_uplink(pred) >= uplink);
499                                 some_in_loop = 1;
500                         }
501                 }
502         }
503         return none_outof_loop & some_in_loop;
504 }
505
506 /** Returns index of the predecessor with the smallest dfn number
507     greater-equal than limit. */
508 static int smallest_dfn_pred(ir_node *n, int limit)
509 {
510         int i, index = -2, min = -1;
511
512         if (!is_outermost_Start(n)) {
513                 int arity = get_irn_arity(n);
514                 for (i = get_start_index(n); i < arity; i++) {
515                         ir_node *pred = get_irn_n(n, i);
516                         if (is_backedge(n, i) || !irn_is_in_stack(pred))
517                                 continue;
518                         if (get_irn_dfn(pred) >= limit && (min == -1 || get_irn_dfn(pred) < min)) {
519                                 index = i;
520                                 min = get_irn_dfn(pred);
521                         }
522                 }
523         }
524         return index;
525 }
526
527 /**
528  * Returns index of the predecessor with the largest dfn number.
529  */
530 static int largest_dfn_pred(ir_node *n)
531 {
532         int i, index = -2, max = -1;
533
534         if (!is_outermost_Start(n)) {
535                 int arity = get_irn_arity(n);
536                 for (i = get_start_index(n); i < arity; i++) {
537                         ir_node *pred = get_irn_n(n, i);
538                         if (is_backedge (n, i) || !irn_is_in_stack(pred))
539                                 continue;
540                         if (get_irn_dfn(pred) > max) {
541                                 index = i;
542                                 max = get_irn_dfn(pred);
543                         }
544                 }
545         }
546         return index;
547 }
548
549 /**
550  * Searches the stack for possible loop heads.  Tests these for backedges.
551  * If it finds a head with an unmarked backedge it marks this edge and
552  * returns the tail of the loop.
553  * If it finds no backedge returns NULL.
554  * ("disable_backedge" in fiasco)
555  *
556  * @param n  A node where uplink == dfn.
557  */
558 static ir_node *find_tail(ir_node *n)
559 {
560         ir_node *m;
561         int i, res_index = -2;
562
563         /*
564         if (!icfg && rm_cyclic_phis && remove_cyclic_phis (n)) return NULL;
565          */
566         m = stack[tos-1];  /* tos = top of stack */
567         if (is_head(m, n)) {
568                 res_index = smallest_dfn_pred(m, 0);
569                 if ((res_index == -2) &&  /* no smallest dfn pred found. */
570                         (n ==  m))
571                         return NULL;
572         } else {
573                 if (m == n) return NULL;    // Is this to catch Phi - self loops?
574                 for (i = tos-2; i >= 0; --i) {
575                         m = stack[i];
576
577                         if (is_head(m, n)) {
578                                 res_index = smallest_dfn_pred(m, get_irn_dfn(m) + 1);
579                                 if (res_index == -2)  /* no smallest dfn pred found. */
580                                         res_index = largest_dfn_pred(m);
581
582                                 if ((m == n) && (res_index == -2)) {  /* don't walk past loop head. */
583                                         i = -1;
584                                 }
585                                 break;
586                         }
587
588                         /* We should not walk past our selves on the stack:  The upcoming nodes
589                            are not in this loop. We assume a loop not reachable from Start. */
590                         if (m == n) {
591                                 i = -1;
592                                 break;
593                         }
594                 }
595
596                 if (i < 0) {
597                         /* A dead loop not reachable from Start. */
598                         for (i = tos-2; i >= 0; --i) {
599                                 m = stack[i];
600                                 if (is_endless_head(m, n)) {
601                                         res_index = smallest_dfn_pred(m, get_irn_dfn(m) + 1);
602                                         if (res_index == -2)  /* no smallest dfn pred found. */
603                                                 res_index = largest_dfn_pred (m);
604                                         break;
605                                 }
606                                 /* It's not an unreachable loop, either. */
607                                 if (m == n)
608                                         break;
609                         }
610                         //assert(0 && "no head found on stack");
611                 }
612
613         }
614         if (res_index <= -2) {
615                 /* It's a completely bad loop: without Phi/Block nodes that can
616                    be a head. I.e., the code is "dying".  We break the loop by
617                    setting Bad nodes. */
618                 ir_graph *irg   = get_irn_irg(n);
619                 ir_mode  *mode  = get_irn_mode(n);
620                 ir_node  *bad   = new_r_Bad(irg, mode);
621                 int       arity = get_irn_arity(n);
622                 for (i = -1; i < arity; ++i) {
623                         set_irn_n(n, i, bad);
624                 }
625                 return NULL;
626         }
627         assert(res_index > -2);
628
629         set_backedge(m, res_index);
630         return is_outermost_Start(n) ? NULL : get_irn_n(m, res_index);
631 }
632
633 static inline int is_outermost_loop(ir_loop *l)
634 {
635         return l == get_loop_outer_loop(l);
636 }
637
638 /*-----------------------------------------------------------*
639  *                   The core algorithm.                     *
640  *-----------------------------------------------------------*/
641
642 /**
643  * The core algorithm: Find strongly coupled components.
644  *
645  * @param n  node to start
646  */
647 static void scc(ir_node *n)
648 {
649         if (irn_visited_else_mark(n))
650                 return;
651
652         /* Initialize the node */
653         set_irn_dfn(n, current_dfn);      /* Depth first number for this node */
654         set_irn_uplink(n, current_dfn);   /* ... is default uplink. */
655         set_irn_loop(n, NULL);
656         ++current_dfn;
657         push(n);
658
659         /* AS: get_start_index might return -1 for Control Flow Nodes, and thus a negative
660            array index would be passed to is_backedge(). But CFG Nodes dont't have a backedge array,
661            so is_backedge does not access array[-1] but correctly returns false! */
662
663         if (!is_outermost_Start(n)) {
664                 int i, arity = get_irn_arity(n);
665
666                 for (i = get_start_index(n); i < arity; ++i) {
667                         ir_node *m;
668                         if (is_backedge(n, i))
669                                 continue;
670                         m = get_irn_n(n, i);
671                         scc(m);
672                         if (irn_is_in_stack(m)) {
673                                 /* Uplink of m is smaller if n->m is a backedge.
674                                    Propagate the uplink to mark the loop. */
675                                 if (get_irn_uplink(m) < get_irn_uplink(n))
676                                         set_irn_uplink(n, get_irn_uplink(m));
677                         }
678                 }
679         }
680
681         if (get_irn_dfn(n) == get_irn_uplink(n)) {
682                 /* This condition holds for
683                    1) the node with the incoming backedge.
684                       That is: We found a loop!
685                    2) Straight line code, because no uplink has been propagated, so the
686                       uplink still is the same as the dfn.
687
688                    But n might not be a proper loop head for the analysis. Proper loop
689                    heads are Block and Phi nodes. find_tail() searches the stack for
690                    Block's and Phi's and takes those nodes as loop heads for the current
691                    loop instead and marks the incoming edge as backedge. */
692
693                 ir_node *tail = find_tail(n);
694                 if (tail != NULL) {
695                         /* We have a loop, that is no straight line code,
696                            because we found a loop head!
697                            Next actions: Open a new loop on the loop tree and
698                                          try to find inner loops */
699
700                         /* This is an adaption of the algorithm from fiasco / optscc to
701                          * avoid loops without Block or Phi as first node.  This should
702                          * severely reduce the number of evaluations of nodes to detect
703                          * a fixpoint in the heap analysis.
704                          * Further it avoids loops without firm nodes that cause errors
705                          * in the heap analyses.
706                          * But attention:  don't do it for the outermost loop:  This loop
707                          * is not iterated.  A first block can be a loop head in case of
708                          * an endless recursion. */
709
710                         ir_loop *l;
711                         int close;
712                         if ((get_loop_n_elements(current_loop) > 0) || (is_outermost_loop(current_loop))) {
713                                 l = new_loop();
714                                 close = 1;
715                         } else {
716                                 l = current_loop;
717                                 close = 0;
718                         }
719
720                         /* Remove the loop from the stack ... */
721                         pop_scc_unmark_visit(n);
722
723                         /* The current backedge has been marked, that is temporarily eliminated,
724                            by find tail. Start the scc algorithm
725                            again on the subgraph that is left (the current loop without the backedge)
726                            in order to find more inner loops. */
727                         scc(tail);
728
729                         assert(irn_visited(n));
730                         if (close)
731                                 close_loop(l);
732                 } else {
733                         /* No loop head was found, that is we have straight line code.
734                            Pop all nodes from the stack to the current loop. */
735                         pop_scc_to_loop(n);
736                 }
737         }
738 }
739
740 int construct_backedges(ir_graph *irg)
741 {
742         ir_graph *rem = current_ir_graph;
743         ir_loop *head_rem;
744         struct obstack temp;
745
746         max_loop_depth = 0;
747         current_ir_graph   = irg;
748         outermost_ir_graph = irg;
749
750         obstack_init(&temp);
751         init_scc(irg, &temp);
752
753         current_loop = NULL;
754         new_loop();  /* sets current_loop */
755         head_rem = current_loop; /* Just for assertion */
756
757         inc_irg_visited(irg);
758
759         scc(get_irg_end(irg));
760
761         finish_scc();
762         obstack_free(&temp, NULL);
763
764         assert(head_rem == current_loop);
765         mature_loops(current_loop, get_irg_obstack(irg));
766         set_irg_loop(irg, current_loop);
767         add_irg_properties(irg, IR_GRAPH_PROPERTY_CONSISTENT_LOOPINFO);
768         assert(get_irg_loop(irg)->kind == k_ir_loop);
769         current_ir_graph = rem;
770         return max_loop_depth;
771 }
772
773 static void reset_backedges(ir_node *n)
774 {
775         if (is_possible_loop_head(n)) {
776                 clear_backedges(n);
777         }
778 }
779
780 /*
781 static void loop_reset_backedges(ir_loop *l)
782 {
783         int i;
784         reset_backedges(get_loop_node(l, 0));
785         for (i = 0; i < get_loop_n_nodes(l); ++i)
786                 set_irn_loop(get_loop_node(l, i), NULL);
787         for (i = 0; i < get_loop_n_sons(l); ++i) {
788                 loop_reset_backedges(get_loop_son(l, i));
789         }
790 }
791 */
792
793 static void loop_reset_node(ir_node *n, void *env)
794 {
795         (void) env;
796         set_irn_loop(n, NULL);
797         reset_backedges(n);
798 }
799
800 void free_loop_information(ir_graph *irg)
801 {
802         /* We can not use this recursion, as the loop might contain
803            illegal nodes by now.  Why else would we throw away the
804            representation?
805         if (get_irg_loop(irg)) loop_reset_backedges(get_irg_loop(irg));
806         */
807         irg_walk_graph(irg, loop_reset_node, NULL, NULL);
808         set_irg_loop(irg, NULL);
809         clear_irg_properties(current_ir_graph, IR_GRAPH_PROPERTY_CONSISTENT_LOOPINFO);
810         /* We cannot free the loop nodes, they are on the obstack. */
811 }
812
813 void free_all_loop_information(void)
814 {
815         size_t i;
816         for (i = 0; i < get_irp_n_irgs(); i++) {
817                 free_loop_information(get_irp_irg(i));
818         }
819 }
820
821 /* ------------------------------------------------------------------- */
822 /* Simple analyses based on the loop information                       */
823 /* ------------------------------------------------------------------- */
824
825 static int is_loop_variant(ir_loop *l, ir_loop *b)
826 {
827         size_t i, n_elems;
828
829         if (l == b) return 1;
830
831         n_elems = get_loop_n_elements(l);
832         for (i = 0; i < n_elems; ++i) {
833                 loop_element e = get_loop_element(l, i);
834                 if (is_ir_loop(e.kind))
835                         if (is_loop_variant(e.son, b))
836                                 return 1;
837         }
838
839         return 0;
840 }
841
842 int is_loop_invariant(const ir_node *n, const ir_node *block)
843 {
844         ir_loop *l = get_irn_loop(block);
845         const ir_node *b = is_Block(n) ? n : get_nodes_block(n);
846         return !is_loop_variant(l, get_irn_loop(b));
847 }