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