8b541f93177a778ca497cd6c2ad32b658a68897e
[libfirm] / ir / ana / irscc.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
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  * @version  $Id$
29  */
30 #include "config.h"
31
32 #include <string.h>
33 #include <stdlib.h>
34
35 #include "irloop_t.h"
36
37 #include "irprog_t.h"
38 #include "irgraph_t.h"
39 #include "irnode_t.h"
40 #include "irgwalk.h"
41 #include "irdump.h"
42 #include "array.h"
43 #include "pmap.h"
44
45 /* A variant of the loop tree that avoids loops without head.
46    This reduces the depth of the loop tree. */
47 #define NO_LOOPS_WITHOUT_HEAD 1
48
49 /** The outermost graph the scc is computed for. */
50 static ir_graph *outermost_ir_graph;
51 /** Current loop construction is working on. */
52 static ir_loop *current_loop;
53 /** Counts the number of allocated loop nodes.
54  *  Each loop node gets a unique number.
55  *  @todo What for? ev. remove.
56  */
57 static int loop_node_cnt = 0;
58 /** Counter to generate depth first numbering of visited nodes. */
59 static int current_dfn = 1;
60
61 static int max_loop_depth = 0;
62
63 void link_to_reg_end(ir_node *n, void *env);
64 void set_projx_link(ir_node *cb_projx, ir_node *end_projx);
65 ir_node *get_projx_link(ir_node *cb_projx);
66
67 /**********************************************************************/
68 /* Node attributes                                                   **/
69 /**********************************************************************/
70
71 /**********************************************************************/
72 /* Node attributes needed for the construction.                      **/
73 /**********************************************************************/
74
75 typedef struct scc_info {
76         int in_stack;          /**< Marks whether node is on the stack. */
77         int dfn;               /**< Depth first search number. */
78         int uplink;            /**< dfn number of ancestor. */
79         /*  ir_loop *loop;         *//* Refers to the containing loop. */
80         /*
81             struct section *section;
82             xset def;
83             xset use;
84         */
85 } scc_info;
86
87 /**
88  * Allocates a new SCC info on the given obstack.
89  */
90 static inline scc_info *new_scc_info(struct obstack *obst)
91 {
92         return OALLOCZ(obst, scc_info);
93 }
94
95 /**
96  * Mark node n being on the SCC stack.
97  */
98 static inline void mark_irn_in_stack(ir_node *n)
99 {
100         scc_info *scc = get_irn_link(n);
101         assert(scc);
102         scc->in_stack = 1;
103 }
104
105 /**
106 * Mark node n NOT being on the SCC stack.
107 */
108 static inline void mark_irn_not_in_stack(ir_node *n)
109 {
110         scc_info *scc = get_irn_link(n);
111         assert(scc);
112         scc->in_stack = 0;
113 }
114
115 /**
116  * Checks if a node is on the SCC stack.
117  */
118 static inline int irn_is_in_stack(ir_node *n)
119 {
120         scc_info *scc = get_irn_link(n);
121         assert(scc);
122         return scc->in_stack;
123 }
124
125 /**
126  * Sets the uplink number for a node.
127  */
128 static inline void set_irn_uplink(ir_node *n, int uplink)
129 {
130         scc_info *scc = get_irn_link(n);
131         assert(scc);
132         scc->uplink = uplink;
133 }
134
135 /**
136  * Returns the uplink number for a node.
137  */
138 static int get_irn_uplink(ir_node *n)
139 {
140         scc_info *scc = get_irn_link(n);
141         assert(scc);
142         return scc->uplink;
143 }
144
145 /**
146  * Sets the depth-first-search number for a node.
147  */
148 static inline void set_irn_dfn(ir_node *n, int dfn)
149 {
150         scc_info *scc = get_irn_link(n);
151         assert(scc);
152         scc->dfn = dfn;
153 }
154
155 /**
156  * Returns the depth-first-search number of a node.
157  */
158 static int get_irn_dfn(ir_node *n)
159 {
160         scc_info *scc = get_irn_link(n);
161         assert(scc);
162         return scc->dfn;
163 }
164
165 #if 0
166 static ir_loop *find_nodes_loop(ir_node *n, ir_loop *l)
167 {
168         int i;
169         ir_loop *res = NULL;
170
171         /* Test whether n is contained in this loop. */
172         for (i = 0; i < get_loop_n_nodes(l); i++)
173                 if (n == get_loop_node(l, i)) return l;
174
175         /* Is this a leave in the loop tree? If so loop not found. */
176         if (get_loop_n_sons(l) == 0) return NULL;
177
178         /* Else descend in the loop tree. */
179         for (i = 0; i < get_loop_n_sons(l); i++) {
180                 res = find_nodes_loop(n, get_loop_son(l, i));
181                 if (res) break;
182         }
183         return res;
184 }
185
186 /* @@@ temporary implementation, costly!!! */
187 ir_loop * get_irn_loop(ir_node *n)
188 {
189         ir_loop *l = get_irg_loop(current_ir_graph);
190         l = find_nodes_loop(n, l);
191         return l;
192 }
193 #endif
194
195 /**********************************************************************/
196 /* A stack.                                                          **/
197 /**********************************************************************/
198
199 static ir_node **stack = NULL;
200 static int tos = 0;                /* top of stack */
201
202 /**
203  * initializes the stack
204  */
205 static inline void init_stack(void)
206 {
207         if (stack) {
208                 ARR_RESIZE(ir_node *, stack, 1000);
209         } else {
210                 stack = NEW_ARR_F(ir_node *, 1000);
211         }
212         tos = 0;
213 }
214
215 /**
216  * Frees the stack.
217  */
218 static void finish_stack(void)
219 {
220         DEL_ARR_F(stack);
221         stack = NULL;
222 }
223
224 /**
225  * push a node onto the stack
226  *
227  * @param n  The node to push
228  */
229 static inline void push(ir_node *n)
230 {
231         if (tos == ARR_LEN(stack)) {
232                 int nlen = ARR_LEN(stack) * 2;
233                 ARR_RESIZE(ir_node *, stack, nlen);
234         }
235         stack [tos++] = n;
236         mark_irn_in_stack(n);
237 }
238
239 /**
240  * pop a node from the stack
241  *
242  * @return  The topmost node
243  */
244 static inline ir_node *pop(void)
245 {
246         ir_node *n = stack[--tos];
247         mark_irn_not_in_stack(n);
248         return n;
249 }
250
251 /**
252  * The nodes up to n belong to the current loop.
253  * Removes them from the stack and adds them to the current loop.
254  */
255 static inline void pop_scc_to_loop(ir_node *n)
256 {
257         ir_node *m;
258         int i = 0;
259
260         do {
261                 m = pop();
262
263                 loop_node_cnt++;
264                 set_irn_dfn(m, loop_node_cnt);
265                 add_loop_node(current_loop, m);
266                 set_irn_loop(m, current_loop);
267                 ++i;
268         } while (m != n);
269
270         /* i might be bigger than 1 for dead (and that's why bad) loops */
271         /* if (i > 1)
272                 printf("Mehr als eine Iteration!!!!!!!!!!!!!!!!!!!!!!!!!!!!11111\n");
273          */
274 }
275
276 /* GL ??? my last son is my grandson???  Removes loops with no
277    ir_nodes in them.  Such loops have only another loop as son. (Why
278    can't they have two loops as sons? Does it never get that far? ) */
279 static void close_loop(ir_loop *l)
280 {
281         int last = get_loop_n_elements(l) - 1;
282         loop_element lelement = get_loop_element(l, last);
283         ir_loop *last_son = lelement.son;
284
285         if (get_kind(last_son) == k_ir_loop &&
286                 get_loop_n_elements(last_son) == 1) {
287                         ir_loop *gson;
288
289                         lelement = get_loop_element(last_son, 0);
290                         gson = lelement.son;
291
292                         if (get_kind(gson) == k_ir_loop) {
293                                 loop_element new_last_son;
294
295                                 gson->outer_loop = l;
296                                 new_last_son.son = gson;
297                                 l->children[last] = new_last_son;
298                         }
299         }
300
301         current_loop = l;
302 }
303
304 /* Removes and unmarks all nodes up to n from the stack.
305    The nodes must be visited once more to assign them to a scc. */
306 static inline void pop_scc_unmark_visit(ir_node *n)
307 {
308         ir_node *m = NULL;
309
310         while (m != n) {
311                 m = pop();
312                 set_irn_visited(m, 0);
313         }
314 }
315
316 /**********************************************************************/
317 /* The loop datastructure.                                           **/
318 /**********************************************************************/
319
320 /* Allocates a new loop as son of current_loop.  Sets current_loop
321    to the new loop and returns the father. */
322 static ir_loop *new_loop(void)
323 {
324         ir_loop *father = current_loop;
325         ir_loop *son    = alloc_loop(father, outermost_ir_graph->obst);
326
327         if (son->depth > max_loop_depth) max_loop_depth = son->depth;
328         current_loop = son;
329         return father;
330 }
331
332 /**********************************************************************/
333 /* Constructing and destructing the loop/backedge information.       **/
334 /**********************************************************************/
335
336 /* Initialization steps. **********************************************/
337
338 static inline void init_node(ir_node *n, void *env)
339 {
340         struct obstack *obst = env;
341         set_irn_link(n, new_scc_info(obst));
342         clear_backedges(n);
343 }
344
345 static inline void init_scc_common(void)
346 {
347         current_dfn = 1;
348         loop_node_cnt = 0;
349         init_stack();
350 }
351
352 static inline void init_scc(ir_graph *irg, struct obstack *obst)
353 {
354         init_scc_common();
355         irg_walk_graph(irg, init_node, NULL, obst);
356         /*
357         irg_walk (irg, link_to_reg_end, NULL, NULL);
358         */
359 }
360
361 static inline void finish_scc(void)
362 {
363         finish_stack();
364 }
365
366 #ifdef INTERPROCEDURAL_VIEW
367 static inline void init_ip_scc(struct obstack *obst)
368 {
369         init_scc_common();
370         cg_walk(init_node, NULL, obst);
371
372 #if EXPERIMENTAL_LOOP_TREE
373         cg_walk(link_to_reg_end, NULL, NULL);
374 #endif
375 }
376 #endif
377
378 /**
379  * Check weather a given node represents the outer most Start
380  * block. In intra-procedural view this is the start block of the
381  * current graph, in interprocedural view it is the start block
382  * of the outer most graph.
383  *
384  * @param n  the node to check
385  *
386  * This is the condition for breaking the scc recursion.
387  */
388 static int is_outermost_Start(ir_node *n)
389 {
390         /* Test whether this is the outermost Start node. */
391         if (is_Block(n) && get_Block_n_cfgpreds(n) == 1) {
392                 ir_node *pred = skip_Proj(get_Block_cfgpred(n, 0));
393             if (is_Start(pred) && get_nodes_block(pred) == n)
394                         return 1;
395         }
396         return 0;
397 }
398
399 /* When to walk from nodes to blocks. Only for Control flow operations? */
400 static inline int get_start_index(ir_node *n)
401 {
402 #undef BLOCK_BEFORE_NODE
403 #define BLOCK_BEFORE_NODE 1
404
405 #if BLOCK_BEFORE_NODE
406
407         /* This version assures, that all nodes are ordered absolutely.  This allows
408            to undef all nodes in the heap analysis if the block is false, which means
409            not reachable.
410            I.e., with this code, the order on the loop tree is correct. But a (single)
411            test showed the loop tree is deeper.   */
412         if (get_irn_op(n) == op_Phi                      ||
413             is_Block(n)                                  ||
414             (is_Filter(n) && get_interprocedural_view()) || (
415               get_irg_pinned(get_irn_irg(n)) == op_pin_state_floats &&
416               get_irn_pinned(n)              == op_pin_state_floats
417             ))
418                 // Here we could test for backedge at -1 which is illegal
419                 return 0;
420         else
421                 return -1;
422
423 #else
424
425         /* This version causes deeper loop trees (at least we verified this
426            for Polymor).
427            But it guarantees that Blocks are analysed before nodes contained in the
428            block.  If so, we can set the value to undef if the block is not \
429            executed. */
430         if (is_cfop(n) || is_fragile_op(n) || is_Start(n))
431                 return -1;
432         else
433                 return 0;
434
435 #endif
436 }
437
438 /**
439  * Return non-zero if the given node is a legal loop header:
440  * Block, Phi, Filter.
441  *
442  * @param n  the node to check
443  */
444 static inline int is_possible_loop_head(ir_node *n)
445 {
446         ir_op *op = get_irn_op(n);
447         return ((op == op_Block) ||
448                 (op == op_Phi) ||
449                 ((op == op_Filter) && get_interprocedural_view()));
450 }
451
452 /**
453  * Returns non-zero if n is a loop header, i.e., it is a Block, Phi
454  * or Filter node and has predecessors within the loop and out
455  * of the loop.
456  *
457  * @param n    the node to check
458  * @param root only needed for assertion.
459  */
460 static int is_head(ir_node *n, ir_node *root)
461 {
462         int i, arity;
463         int some_outof_loop = 0, some_in_loop = 0;
464
465         /* Test for legal loop header: Block, Phi, ... */
466         if (!is_possible_loop_head(n))
467                 return 0;
468
469         if (!is_outermost_Start(n)) {
470 #ifndef NDEBUG
471                 int uplink = get_irn_uplink(root);
472 #else
473                 (void) root;
474 #endif
475                 arity = get_irn_arity(n);
476                 for (i = get_start_index(n); i < arity; i++) {
477                         ir_node *pred;
478                         if (is_backedge(n, i))
479                                 continue;
480                         pred = get_irn_n(n, i);
481                         if (! irn_is_in_stack(pred)) {
482                                 some_outof_loop = 1;
483                         } else {
484                                 assert(get_irn_uplink(pred) >= uplink);
485                                 some_in_loop = 1;
486                         }
487                 }
488         }
489         return some_outof_loop & some_in_loop;
490 }
491
492 /**
493  * Returns non-zero if n is possible loop head of an endless loop.
494  * I.e., it is a Block, Phi or Filter node and has only predecessors
495  * within the loop.
496  *
497  * @param n    the node to check
498  * @param root only needed for assertion.
499  */
500 static int is_endless_head(ir_node *n, ir_node *root)
501 {
502         int i, arity;
503         int none_outof_loop = 1, some_in_loop = 0;
504
505         /* Test for legal loop header: Block, Phi, ... */
506         if (!is_possible_loop_head(n))
507                 return 0;
508
509         if (!is_outermost_Start(n)) {
510 #ifndef NDEBUG
511                 int uplink = get_irn_uplink(root);
512 #else
513                 (void) root;
514 #endif
515                 arity = get_irn_arity(n);
516                 for (i = get_start_index(n); i < arity; i++) {
517                         ir_node *pred;
518                         if (is_backedge(n, i))
519                                 continue;
520                         pred = get_irn_n(n, i);
521                         if (!irn_is_in_stack(pred)) {
522                                 none_outof_loop = 0;
523                         } else {
524                                 assert(get_irn_uplink(pred) >= uplink);
525                                 some_in_loop = 1;
526                         }
527                 }
528         }
529         return none_outof_loop & some_in_loop;
530 }
531
532 /** Returns index of the predecessor with the smallest dfn number
533     greater-equal than limit. */
534 static int smallest_dfn_pred(ir_node *n, int limit)
535 {
536         int i, index = -2, min = -1;
537
538         if (!is_outermost_Start(n)) {
539                 int arity = get_irn_arity(n);
540                 for (i = get_start_index(n); i < arity; i++) {
541                         ir_node *pred = get_irn_n(n, i);
542                         if (is_backedge(n, i) || !irn_is_in_stack(pred))
543                                 continue;
544                         if (get_irn_dfn(pred) >= limit && (min == -1 || get_irn_dfn(pred) < min)) {
545                                 index = i;
546                                 min = get_irn_dfn(pred);
547                         }
548                 }
549         }
550         return index;
551 }
552
553 /**
554  * Returns index of the predecessor with the largest dfn number.
555  */
556 static int largest_dfn_pred(ir_node *n)
557 {
558         int i, index = -2, max = -1;
559
560         if (!is_outermost_Start(n)) {
561                 int arity = get_irn_arity(n);
562                 for (i = get_start_index(n); i < arity; i++) {
563                         ir_node *pred = get_irn_n(n, i);
564                         if (is_backedge (n, i) || !irn_is_in_stack(pred))
565                                 continue;
566                         if (get_irn_dfn(pred) > max) {
567                                 index = i;
568                                 max = get_irn_dfn(pred);
569                         }
570                 }
571         }
572         return index;
573 }
574
575 /**
576  * Searches the stack for possible loop heads.  Tests these for backedges.
577  * If it finds a head with an unmarked backedge it marks this edge and
578  * returns the tail of the loop.
579  * If it finds no backedge returns NULL.
580  * ("disable_backedge" in fiasco)
581  *
582  * @param n  A node where uplink == dfn.
583  */
584 static ir_node *find_tail(ir_node *n)
585 {
586         ir_node *m;
587         int i, res_index = -2;
588
589         /*
590         if (!icfg && rm_cyclic_phis && remove_cyclic_phis (n)) return NULL;
591          */
592         m = stack[tos-1];  /* tos = top of stack */
593         if (is_head(m, n)) {
594                 res_index = smallest_dfn_pred(m, 0);
595                 if ((res_index == -2) &&  /* no smallest dfn pred found. */
596                         (n ==  m))
597                         return NULL;
598         } else {
599                 if (m == n) return NULL;    // Is this to catch Phi - self loops?
600                 for (i = tos-2; i >= 0; --i) {
601                         m = stack[i];
602
603                         if (is_head(m, n)) {
604                                 res_index = smallest_dfn_pred(m, get_irn_dfn(m) + 1);
605                                 if (res_index == -2)  /* no smallest dfn pred found. */
606                                         res_index = largest_dfn_pred(m);
607
608                                 if ((m == n) && (res_index == -2)) {  /* don't walk past loop head. */
609                                         i = -1;
610                                 }
611                                 break;
612                         }
613
614                         /* We should not walk past our selves on the stack:  The upcoming nodes
615                            are not in this loop. We assume a loop not reachable from Start. */
616                         if (m == n) {
617                                 i = -1;
618                                 break;
619                         }
620                 }
621
622                 if (i < 0) {
623                         /* A dead loop not reachable from Start. */
624                         for (i = tos-2; i >= 0; --i) {
625                                 m = stack[i];
626                                 if (is_endless_head(m, n)) {
627                                         res_index = smallest_dfn_pred(m, get_irn_dfn(m) + 1);
628                                         if (res_index == -2)  /* no smallest dfn pred found. */
629                                                 res_index = largest_dfn_pred (m);
630                                         break;
631                                 }
632                                 if (m == n) { break; }  /* It's not an unreachable loop, either. */
633                         }
634                         //assert(0 && "no head found on stack");
635                 }
636
637         }
638         if (res_index <= -2) {
639                 /* It's a completely bad loop: without Phi/Block nodes that can
640                    be a head. I.e., the code is "dying".  We break the loop by
641                    setting Bad nodes. */
642                 int arity = get_irn_arity(n);
643                 ir_node *bad = get_irg_bad(get_irn_irg(n));
644                 for (i = -1; i < arity; ++i) {
645                         set_irn_n(n, i, bad);
646                 }
647                 return NULL;
648         }
649         assert(res_index > -2);
650
651         set_backedge(m, res_index);
652         return is_outermost_Start(n) ? NULL : get_irn_n(m, res_index);
653 }
654
655
656 #if EXPERIMENTAL_LOOP_TREE
657
658 /*  ----------------------------------------------------------------
659     AS:  This is experimental code to build loop trees suitable for
660     the heap analysis. Does not work correctly right now... :-(
661
662
663     Search in stack for the corresponding first Call-End-ProjX that
664     corresponds to one of the control flow predecessors of the given
665     block, that is the possible callers.
666     returns: the control predecessor to chose\
667     or       -1 if no corresponding Call-End-Node could be found
668              on the stack.
669     - -------------------------------------------------------------- */
670
671 int search_endproj_in_stack(ir_node *start_block)
672 {
673         int i, j;
674         assert(is_Block(start_block));
675         for (i = tos - 1; i >= 0; --i)
676         {
677                 if (get_irn_op(stack[i]) == op_Proj && get_irn_mode(stack[i]) == mode_X &&
678                         get_irn_op(get_irn_n(stack[i], 0)) == op_EndReg)
679                 {
680                         printf("FOUND PROJ!!!!!!!!!!!!!!!!!!!!!!!!!!\n");
681                         ir_node *end_projx = stack[i];
682
683                         int arity = get_irn_arity(start_block);
684                         for (j = 0; j < arity; j++)
685                         {
686                                 ir_node *begin_projx = get_Block_cfgpred(get_irg_start_block(get_irn_irg(end_projx)),
687                                         get_Proj_proj(end_projx));
688                                 if (get_irn_n(start_block, j) == begin_projx)
689                                 {
690                                         printf("FOUND IT!!!!!!!!!!!!!!!!!!\n");
691                                         return(j);
692                                 }
693                         }
694                 }
695         }
696         return(-1);
697 }
698
699
700 static pmap *projx_link = NULL;
701
702 void link_to_reg_end (ir_node *n, void *env)
703 {
704         if (get_irn_op(n) == op_Proj &&
705                 get_irn_mode(n) == mode_X &&
706                 get_irn_op(get_irn_n(n, 0)) == op_EndReg) {
707                         /* Reg End Projx -> Find the CallBegin Projx and hash it */
708                         ir_node *end_projx = n;
709                         ir_node *begin_projx = get_Block_cfgpred(get_irg_start_block(get_irn_irg(end_projx)),
710                                 get_Proj_proj(end_projx));
711                         set_projx_link(begin_projx, end_projx);
712         }
713 }
714
715 void set_projx_link(ir_node *cb_projx, ir_node *end_projx)
716 {
717         if (projx_link == NULL)
718                 projx_link = pmap_create();
719         pmap_insert(projx_link, (void *)cb_projx, (void *)end_projx);
720 }
721
722 ir_node *get_projx_link(ir_node *cb_projx)
723 {
724         return((ir_node *) pmap_get(projx_link, (void *)cb_projx));
725 }
726
727 #endif
728
729 static inline int is_outermost_loop(ir_loop *l)
730 {
731         return l == get_loop_outer_loop(l);
732 }
733
734
735 /*-----------------------------------------------------------*
736  *                   The core algorithm.                     *
737  *-----------------------------------------------------------*/
738
739 /**
740  * The core algorithm: Find strongly coupled components.
741  *
742  * @param n  node to start
743  */
744 static void scc(ir_node *n)
745 {
746         if (irn_visited_else_mark(n))
747                 return;
748
749         /* Initialize the node */
750         set_irn_dfn(n, current_dfn);      /* Depth first number for this node */
751         set_irn_uplink(n, current_dfn);   /* ... is default uplink. */
752         set_irn_loop(n, NULL);
753         ++current_dfn;
754         push(n);
755
756         /* AS: get_start_index might return -1 for Control Flow Nodes, and thus a negative
757            array index would be passed to is_backedge(). But CFG Nodes dont't have a backedge array,
758            so is_backedge does not access array[-1] but correctly returns false! */
759
760         if (!is_outermost_Start(n)) {
761                 int i, arity = get_irn_arity(n);
762
763                 for (i = get_start_index(n); i < arity; ++i) {
764                         ir_node *m;
765                         if (is_backedge(n, i))
766                                 continue;
767                         m = get_irn_n(n, i);
768                         scc(m);
769                         if (irn_is_in_stack(m)) {
770                                 /* Uplink of m is smaller if n->m is a backedge.
771                                    Propagate the uplink to mark the loop. */
772                                 if (get_irn_uplink(m) < get_irn_uplink(n))
773                                         set_irn_uplink(n, get_irn_uplink(m));
774                         }
775                 }
776         }
777
778         if (get_irn_dfn(n) == get_irn_uplink(n)) {
779                 /* This condition holds for
780                    1) the node with the incoming backedge.
781                       That is: We found a loop!
782                    2) Straight line code, because no uplink has been propagated, so the
783                       uplink still is the same as the dfn.
784
785                    But n might not be a proper loop head for the analysis. Proper loop
786                    heads are Block and Phi nodes. find_tail() searches the stack for
787                    Block's and Phi's and takes those nodes as loop heads for the current
788                    loop instead and marks the incoming edge as backedge. */
789
790                 ir_node *tail = find_tail(n);
791                 if (tail != NULL) {
792                         /* We have a loop, that is no straight line code,
793                            because we found a loop head!
794                            Next actions: Open a new loop on the loop tree and
795                                          try to find inner loops */
796
797 #if NO_LOOPS_WITHOUT_HEAD
798                         /* This is an adaption of the algorithm from fiasco / optscc to
799                          * avoid loops without Block or Phi as first node.  This should
800                          * severely reduce the number of evaluations of nodes to detect
801                          * a fixpoint in the heap analysis.
802                          * Further it avoids loops without firm nodes that cause errors
803                          * in the heap analyses.
804                          * But attention:  don't do it for the outermost loop:  This loop
805                          * is not iterated.  A first block can be a loop head in case of
806                          * an endless recursion. */
807
808                         ir_loop *l;
809                         int close;
810                         if ((get_loop_n_elements(current_loop) > 0) || (is_outermost_loop(current_loop))) {
811                                 l = new_loop();
812                                 close = 1;
813                         } else {
814                                 l = current_loop;
815                                 close = 0;
816                         }
817 #else
818                         ir_loop *l = new_loop();
819 #endif
820
821                         /* Remove the loop from the stack ... */
822                         pop_scc_unmark_visit(n);
823
824                         /* The current backedge has been marked, that is temporarily eliminated,
825                            by find tail. Start the scc algorithm
826                            again on the subgraph that is left (the current loop without the backedge)
827                            in order to find more inner loops. */
828                         scc(tail);
829
830                         assert(irn_visited(n));
831 #if NO_LOOPS_WITHOUT_HEAD
832                         if (close)
833 #endif
834                                 close_loop(l);
835                 } else {
836                         /* No loop head was found, that is we have straight line code.
837                            Pop all nodes from the stack to the current loop. */
838                         pop_scc_to_loop(n);
839                 }
840         }
841 }
842
843 #ifdef INTERPROCEDURAL_VIEW
844 static void my_scc(ir_node *n)
845 {
846         int i;
847         if (irn_visited_else_mark(n))
848                 return;
849
850         /* Initialize the node */
851         set_irn_dfn(n, current_dfn);      /* Depth first number for this node */
852         set_irn_uplink(n, current_dfn);   /* ... is default uplink. */
853         set_irn_loop(n, NULL);
854         current_dfn ++;
855         push(n);
856
857         /* AS: get_start_index might return -1 for Control Flow Nodes, and thus a negative
858            array index would be passed to is_backedge(). But CFG Nodes dont't have a backedge array,
859            so is_backedge does not access array[-1] but correctly returns false! */
860
861         if (!is_outermost_Start(n)) {
862                 int arity = get_irn_arity(n);
863
864                 for (i = get_start_index(n); i < arity; i++) {
865                         ir_node *m;
866                         if (is_backedge(n, i)) continue;
867                         m = get_irn_n(n, i); /* get_irn_ip_pred(n, i); */
868                         /* if (!m || is_Unknown(m)) continue; */
869                         my_scc(m);
870                         if (irn_is_in_stack(m)) {
871                                 /* Uplink of m is smaller if n->m is a backedge.
872                                    Propagate the uplink to mark the loop. */
873                                 if (get_irn_uplink(m) < get_irn_uplink(n))
874                                         set_irn_uplink(n, get_irn_uplink(m));
875                         }
876                 }
877         }
878
879         if (get_irn_dfn(n) == get_irn_uplink(n)) {
880                 /* This condition holds for
881                    1) the node with the incoming backedge.
882                       That is: We found a loop!
883                    2) Straight line code, because no uplink has been propagated, so the
884                       uplink still is the same as the dfn.
885
886                    But n might not be a proper loop head for the analysis. Proper loop
887                    heads are Block and Phi nodes. find_tail searches the stack for
888                    Block's and Phi's and takes those nodes as loop heads for the current
889                    loop instead and marks the incoming edge as backedge. */
890
891                 ir_node *tail = find_tail(n);
892                 if (tail) {
893                         /* We have a loop, that is no straight line code,
894                            because we found a loop head!
895                            Next actions: Open a new loop on the loop tree and
896                                          try to find inner loops */
897
898 #if NO_LOOPS_WITHOUT_HEAD
899                         /* This is an adaption of the algorithm from fiasco / optscc to
900                          * avoid loops without Block or Phi as first node.  This should
901                          * severely reduce the number of evaluations of nodes to detect
902                          * a fixpoint in the heap analysis.
903                          * Further it avoids loops without firm nodes that cause errors
904                          * in the heap analyses. */
905
906                         ir_loop *l;
907                         int close;
908                         if ((get_loop_n_elements(current_loop) > 0) || (is_outermost_loop(current_loop))) {
909                                 l = new_loop();
910                                 close = 1;
911                         } else {
912                                 l = current_loop;
913                                 close = 0;
914                         }
915 #else
916                         ir_loop *l = new_loop();
917 #endif
918
919                         /* Remove the loop from the stack ... */
920                         pop_scc_unmark_visit(n);
921
922                         /* The current backedge has been marked, that is temporarily eliminated,
923                            by find tail. Start the scc algorithm
924                            anew on the subgraph that is left (the current loop without the backedge)
925                            in order to find more inner loops. */
926                         my_scc(tail);
927
928                         assert(irn_visited(n));
929 #if NO_LOOPS_WITHOUT_HEAD
930                         if (close)
931 #endif
932                                 close_loop(l);
933                 } else {
934                         /* No loop head was found, that is we have straightline code.
935                            Pop all nodes from the stack to the current loop. */
936                         pop_scc_to_loop(n);
937                 }
938         }
939 }
940 #endif /* INTERPROCEDURAL_VIEW */
941
942 /* Constructs backedge information for irg. In interprocedural view constructs
943    backedges for all methods called by irg, too. */
944 int construct_backedges(ir_graph *irg)
945 {
946         ir_graph *rem = current_ir_graph;
947         ir_loop *head_rem;
948         struct obstack temp;
949
950         assert(!get_interprocedural_view() &&
951                "not implemented, use construct_ip_backedges()");
952
953         max_loop_depth = 0;
954         current_ir_graph   = irg;
955         outermost_ir_graph = irg;
956
957         obstack_init(&temp);
958         init_scc(irg, &temp);
959
960         current_loop = NULL;
961         new_loop();  /* sets current_loop */
962         head_rem = current_loop; /* Just for assertion */
963
964         inc_irg_visited(irg);
965
966         scc(get_irg_end(irg));
967
968         finish_scc();
969         obstack_free(&temp, NULL);
970
971         assert(head_rem == current_loop);
972         mature_loops(current_loop, irg->obst);
973         set_irg_loop(irg, current_loop);
974         set_irg_loopinfo_state(irg, loopinfo_consistent);
975         assert(get_irg_loop(irg)->kind == k_ir_loop);
976         current_ir_graph = rem;
977         return max_loop_depth;
978 }
979
980
981 #ifdef INTERPROCEDURAL_VIEW
982 int construct_ip_backedges(void)
983 {
984         ir_graph *rem = current_ir_graph;
985         int rem_ipv = get_interprocedural_view();
986         int i;
987         strcut obstack temp;
988
989         max_loop_depth = 0;
990         assert(get_irp_ip_view_state() == ip_view_valid);
991
992         outermost_ir_graph = get_irp_main_irg();
993
994         obstack_init(&temp);
995         init_ip_scc(&temp);
996
997         current_loop = NULL;
998         new_loop();  /* sets current_loop */
999         set_interprocedural_view(1);
1000
1001         inc_max_irg_visited();
1002         for (i = 0; i < get_irp_n_irgs(); i++)
1003                 set_irg_visited(get_irp_irg(i), get_max_irg_visited());
1004
1005         /** We have to start the walk at the same nodes as cg_walk. **/
1006         /* Walk starting at unreachable procedures. Only these
1007          * have End blocks visible in interprocedural view. */
1008         for (i = 0; i < get_irp_n_irgs(); i++) {
1009                 ir_node *sb;
1010                 current_ir_graph = get_irp_irg(i);
1011
1012                 sb = get_irg_start_block(current_ir_graph);
1013
1014                 if ((get_Block_n_cfgpreds(sb) > 1) ||
1015                     (get_nodes_block(get_Block_cfgpred(sb, 0)) != sb))
1016                         continue;
1017
1018                 scc(get_irg_end(current_ir_graph));
1019         }
1020
1021         /* Check whether we walked all procedures: there could be procedures
1022            with cyclic calls but no call from the outside. */
1023         for (i = 0; i < get_irp_n_irgs(); i++) {
1024                 ir_node *sb;
1025                 current_ir_graph = get_irp_irg(i);
1026
1027                 /* Test start block: if inner procedure end and end block are not
1028                  * visible and therefore not marked. */
1029                 sb = get_irg_start_block(current_ir_graph);
1030                 if (get_irn_visited(sb) < get_irg_visited(current_ir_graph)) scc(sb);
1031         }
1032
1033         /* Walk all endless loops in inner procedures.
1034          * We recognize an inner procedure if the End node is not visited. */
1035         for (i = 0; i < get_irp_n_irgs(); i++) {
1036                 ir_node *e;
1037                 current_ir_graph = get_irp_irg(i);
1038
1039                 e = get_irg_end(current_ir_graph);
1040                 if (get_irn_visited(e) < get_irg_visited(current_ir_graph)) {
1041                         int j;
1042                         /* Don't visit the End node. */
1043                         for (j = 0; j < get_End_n_keepalives(e); j++)
1044                                 scc(get_End_keepalive(e, j));
1045                 }
1046         }
1047
1048         set_irg_loop(outermost_ir_graph, current_loop);
1049         set_irg_loopinfo_state(current_ir_graph, loopinfo_ip_consistent);
1050         assert(get_irg_loop(outermost_ir_graph)->kind == k_ir_loop);
1051
1052         obstack_free(&temp, NULL);
1053         current_ir_graph = rem;
1054         set_interprocedural_view(rem_ipv);
1055         return max_loop_depth;
1056 }
1057
1058 void my_construct_ip_backedges(void)
1059 {
1060         ir_graph *rem = current_ir_graph;
1061         int rem_ipv = get_interprocedural_view();
1062         int i;
1063
1064         assert(get_irp_ip_view_state() == ip_view_valid);
1065
1066         outermost_ir_graph = get_irp_main_irg();
1067
1068         init_ip_scc();
1069
1070         current_loop = NULL;
1071         new_loop();  /* sets current_loop */
1072         set_interprocedural_view(1);
1073
1074         inc_max_irg_visited();
1075         for (i = 0; i < get_irp_n_irgs(); i++)
1076                 set_irg_visited(get_irp_irg(i), get_max_irg_visited());
1077
1078         /** We have to start the walk at the same nodes as cg_walk. **/
1079         /* Walk starting at unreachable procedures. Only these
1080          * have End blocks visible in interprocedural view. */
1081         for (i = 0; i < get_irp_n_irgs(); i++) {
1082                 ir_node *sb;
1083                 current_ir_graph = get_irp_irg(i);
1084
1085                 sb = get_irg_start_block(current_ir_graph);
1086
1087                 if ((get_Block_n_cfgpreds(sb) > 1) ||
1088                     (get_nodes_block(get_Block_cfgpred(sb, 0)) != sb)) continue;
1089
1090                 my_scc(get_irg_end(current_ir_graph));
1091         }
1092
1093         /* Check whether we walked all procedures: there could be procedures
1094            with cyclic calls but no call from the outside. */
1095         for (i = 0; i < get_irp_n_irgs(); i++) {
1096                 ir_node *sb;
1097                 current_ir_graph = get_irp_irg(i);
1098
1099                 /* Test start block: if inner procedure end and end block are not
1100                  * visible and therefore not marked. */
1101                 sb = get_irg_start_block(current_ir_graph);
1102                 if (get_irn_visited(sb) < get_irg_visited(current_ir_graph))
1103                         scc(sb);
1104         }
1105
1106         /* Walk all endless loops in inner procedures.
1107          * We recognize an inner procedure if the End node is not visited. */
1108         for (i = 0; i < get_irp_n_irgs(); i++) {
1109                 ir_node *e;
1110                 current_ir_graph = get_irp_irg(i);
1111
1112                 e = get_irg_end(current_ir_graph);
1113                 if (get_irn_visited(e) < get_irg_visited(current_ir_graph)) {
1114                         int j;
1115                         /* Don't visit the End node. */
1116                         for (j = 0; j < get_End_n_keepalives(e); j++) scc(get_End_keepalive(e, j));
1117                 }
1118         }
1119
1120         set_irg_loop(outermost_ir_graph, current_loop);
1121         set_irg_loopinfo_state(current_ir_graph, loopinfo_ip_consistent);
1122         assert(get_irg_loop(outermost_ir_graph)->kind == k_ir_loop);
1123
1124         current_ir_graph = rem;
1125         set_interprocedural_view(rem_ipv);
1126 }
1127 #endif
1128
1129 static void reset_backedges(ir_node *n)
1130 {
1131         if (is_possible_loop_head(n)) {
1132 #ifdef INTERPROCEDURAL_VIEW
1133                 int rem = get_interprocedural_view();
1134
1135                 set_interprocedural_view(1);
1136                 clear_backedges(n);
1137                 set_interprocedural_view(1);
1138                 clear_backedges(n);
1139                 set_interprocedural_view(rem);
1140 #else
1141                 clear_backedges(n);
1142 #endif
1143         }
1144 }
1145
1146
1147 /*
1148 static void loop_reset_backedges(ir_loop *l)
1149 {
1150         int i;
1151         reset_backedges(get_loop_node(l, 0));
1152         for (i = 0; i < get_loop_n_nodes(l); ++i)
1153                 set_irn_loop(get_loop_node(l, i), NULL);
1154         for (i = 0; i < get_loop_n_sons(l); ++i) {
1155                 loop_reset_backedges(get_loop_son(l, i));
1156         }
1157 }
1158 */
1159
1160 static void loop_reset_node(ir_node *n, void *env)
1161 {
1162         (void) env;
1163         set_irn_loop(n, NULL);
1164         reset_backedges(n);
1165 }
1166
1167
1168 /** Removes all loop information.
1169     Resets all backedges */
1170 void free_loop_information(ir_graph *irg)
1171 {
1172         /* We can not use this recursion, as the loop might contain
1173            illegal nodes by now.  Why else would we throw away the
1174            representation?
1175         if (get_irg_loop(irg)) loop_reset_backedges(get_irg_loop(irg));
1176         */
1177         irg_walk_graph(irg, loop_reset_node, NULL, NULL);
1178         set_irg_loop(irg, NULL);
1179         set_irg_loopinfo_state(current_ir_graph, loopinfo_none);
1180         /* We cannot free the loop nodes, they are on the obstack. */
1181 }
1182
1183
1184 void free_all_loop_information(void)
1185 {
1186         int i;
1187 #ifdef INTERPROCEDURAL_VIEW
1188         int rem = get_interprocedural_view();
1189         set_interprocedural_view(1);  /* To visit all filter nodes */
1190 #endif
1191         for (i = 0; i < get_irp_n_irgs(); i++) {
1192                 free_loop_information(get_irp_irg(i));
1193         }
1194 #ifdef INTERPROCEDURAL_VIEW
1195         set_interprocedural_view(rem);
1196 #endif
1197 }
1198
1199
1200
1201
1202
1203 /* Debug stuff *************************************************/
1204
1205 static int test_loop_node(ir_loop *l)
1206 {
1207         int i, has_node = 0, found_problem = 0;
1208         loop_element le;
1209
1210         assert(l && l->kind == k_ir_loop);
1211
1212         if (get_loop_n_elements(l) == 0) {
1213                 found_problem = 1;
1214                 dump_loop(l, "-ha");
1215         }
1216
1217         le = get_loop_element(l, 0);
1218         if (*(le.kind) != k_ir_node) {
1219                 assert(le.kind && *(le.kind) == k_ir_loop);
1220
1221                 found_problem = 1;
1222                 dump_loop(l, "-ha");
1223         }
1224
1225         if ((*(le.kind) == k_ir_node) && !is_possible_loop_head(le.node)) {
1226                 found_problem = 1;
1227                 dump_loop(l, "-ha");
1228         }
1229
1230         if ((get_loop_depth(l) != 0) &&
1231                 (*(le.kind) == k_ir_node) && !has_backedges(le.node)) {
1232                         found_problem = 1;
1233                         dump_loop(l, "-ha");
1234         }
1235
1236         /* Recur */
1237         has_node = 0;
1238         for (i = 0; i < get_loop_n_elements(l); ++i) {
1239                 le = get_loop_element(l, i);
1240                 if (*(le.kind) == k_ir_node)
1241                         has_node++;
1242                 else
1243                         if (test_loop_node(le.son)) found_problem = 1;
1244         }
1245
1246         if (has_node == 0) {
1247                 found_problem = 1;
1248                 dump_loop(l, "-ha");
1249         }
1250
1251         return found_problem;
1252 }
1253
1254 /** Prints all loop nodes that
1255  *  - do not have any firm nodes, only loop sons
1256  *  - the header is not a Phi, Block or Filter.
1257  */
1258 void find_strange_loop_nodes(ir_loop *l)
1259 {
1260         int found_problem = 0;
1261         found_problem = test_loop_node(l);
1262         printf("Finished Test\n\n");
1263         if (found_problem) exit(0);
1264
1265 }
1266
1267 /* ------------------------------------------------------------------- */
1268 /* Simple analyses based on the loop information                       */
1269 /* ------------------------------------------------------------------- */
1270
1271 int is_loop_variant(ir_loop *l, ir_loop *b)
1272 {
1273         int i, n_elems;
1274
1275         if (l == b) return 1;
1276
1277         n_elems = get_loop_n_elements(l);
1278         for (i = 0; i < n_elems; ++i) {
1279                 loop_element e = get_loop_element(l, i);
1280                 if (is_ir_loop(e.kind))
1281                         if (is_loop_variant(e.son, b))
1282                                 return 1;
1283         }
1284
1285         return 0;
1286 }
1287
1288 /* Test whether a value is loop invariant.
1289  *
1290  * @param n      The node to be tested.
1291  * @param block  A block node.  We pass the block, not the loop as we must
1292  *               start off with a block loop to find all proper uses.
1293  *
1294  * Returns non-zero, if the node n is not changed in the loop block
1295  * belongs to or in inner loops of this blocks loop. */
1296 int is_loop_invariant(const ir_node *n, const ir_node *block)
1297 {
1298         ir_loop *l = get_irn_loop(block);
1299         const ir_node *b = is_Block(n) ? n : get_nodes_block(n);
1300         return !is_loop_variant(l, get_irn_loop(b));
1301 }