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