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