compileable with -Wall and bugfixing
[libfirm] / ir / ana / irscc.c
1 /* Copyright (C) 2002 by Universitaet Karlsruhe
2 ** All rights reserved.
3 **
4 ** Authors:  Goetz Lindenmaier
5 **
6 ** irscc.c  Computing the strongly connected regions and building
7 ** backedge/loop datastructures.
8 **
9 */
10
11 /* $Id$ */
12
13 #include <string.h>
14
15 #include "irloop_t.h"
16 #include "irnode.h"
17 #include "irgraph_t.h"
18 #include "array.h"
19 #include "xprintf.h"
20 #include "irgwalk.h"
21
22
23 ir_graph *outermost_ir_graph;      /* The outermost graph the scc is computed
24                                       for */
25 static ir_loop *current_loop;      /* Current loop construction is working
26                                       on. */
27 static int loop_node_cnt = 0;      /* Counts the number of allocated loop nodes.
28                                       Each loop node gets a unique number.
29                                       What for? ev. remove. @@@ */
30 static int current_dfn = 1;        /* Counter to generate depth first numbering
31                                       of visited nodes.  */
32
33 /**********************************************************************/
34 /* Node attributes needed for the construction.                      **/
35 /**********************************************************************/
36
37 typedef struct scc_info {
38   bool in_stack;         /* Marks whether node is on the stack. */
39   int dfn;               /* Depth first search number. */
40   int uplink;            /* dfn number of ancestor. */
41   ir_loop *loop;         /* Refers to the containing loop. */
42   /*
43       struct section *section;
44       xset def;
45       xset use;
46   */
47 } scc_info;
48
49 static INLINE scc_info* new_scc_info() {
50   scc_info *info = obstack_alloc (outermost_ir_graph->obst, sizeof (scc_info));
51   memset (info, 0, sizeof (scc_info));
52   return info;
53 }
54
55 static INLINE void
56 mark_irn_in_stack (ir_node *n) {
57   assert(get_irn_link(n));
58   ((scc_info *)get_irn_link(n))->in_stack = true;
59 }
60
61 static INLINE void
62 mark_irn_not_in_stack (ir_node *n) {
63   assert(get_irn_link(n));
64   ((scc_info *)get_irn_link(n))->in_stack = false;
65 }
66
67 static INLINE bool
68 irn_is_in_stack (ir_node *n) {
69   assert(get_irn_link(n));
70   return ((scc_info *)get_irn_link(n))->in_stack;
71 }
72
73 static INLINE void
74 set_irn_uplink (ir_node *n, int uplink) {
75   assert(get_irn_link(n));
76   ((scc_info *)get_irn_link(n))->uplink = uplink;
77 }
78
79 static INLINE int
80 get_irn_uplink (ir_node *n) {
81   assert(get_irn_link(n));
82   return ((scc_info *)get_irn_link(n))->uplink;
83 }
84
85 static INLINE void
86 set_irn_dfn (ir_node *n, int dfn) {
87   if (! get_irn_link(n)) { DDMN(n); DDME(get_irg_ent(current_ir_graph));}
88   assert(get_irn_link(n));
89   ((scc_info *)get_irn_link(n))->dfn = dfn;
90 }
91
92 static INLINE int
93 get_irn_dfn (ir_node *n) {
94   assert(get_irn_link(n));
95   return ((scc_info *)get_irn_link(n))->dfn;
96 }
97
98 /* Uses temporary information to set the loop */
99 static INLINE void
100 set_irn_loop_tmp (ir_node *n, ir_loop* loop) {
101   assert(get_irn_link(n));
102   ((scc_info *)get_irn_link(n))->loop = loop;
103 }
104
105 /* Uses temporary information to get the loop */
106 static INLINE ir_loop *
107 get_irn_loop_tmp (ir_node *n) {
108   assert(get_irn_link(n));
109   return ((scc_info *)get_irn_link(n))->loop;
110 }
111
112 ir_loop *find_nodes_loop (ir_node *n, ir_loop *l) {
113   int i;
114   ir_loop *res = NULL;
115
116   /* Test whether n is contained in this loop. */
117   for (i = 0; i < get_loop_n_nodes(l); i++)
118     if (n == get_loop_node(l, i)) return l;
119
120   /* Is this a leave in the loop tree? If so loop not found. */
121   if (get_loop_n_sons(l) == 0) return NULL;
122
123   /* Else descend in the loop tree. */
124   for (i = 0; i < get_loop_n_sons(l); i++) {
125     res = find_nodes_loop(n, get_loop_son(l, i));
126     if (res) break;
127   }
128   return res;
129 }
130
131 /* @@@ temporary implementation, costly!!! */
132 ir_loop * get_irn_loop(ir_node *n) {
133   ir_loop *l = get_irg_loop(current_ir_graph);
134   l = find_nodes_loop(n, l);
135   return l;
136 }
137
138 /**********************************************************************/
139 /* A stack.                                                          **/
140 /**********************************************************************/
141
142 static ir_node **stack = NULL;
143 static int tos = 0;                /* top of stack */
144
145 static INLINE void init_stack() {
146   if (stack) {
147     ARR_RESIZE (ir_node *, stack, 1000);
148   } else {
149     stack = NEW_ARR_F (ir_node *, 1000);
150   }
151   tos = 0;
152 }
153
154 static INLINE void free_stack() {
155   DEL_ARR_F(stack);
156   stack = NULL;
157   tos = 0;
158 }
159
160 static INLINE void
161 push (ir_node *n)
162 {
163   //DDMN(n);
164
165   if (tos == ARR_LEN (stack)) {
166     int nlen = ARR_LEN (stack) * 2;
167     ARR_RESIZE (ir_node *, stack, nlen);
168   }
169   stack [tos++] = n;
170   mark_irn_in_stack(n);
171 }
172
173 static INLINE ir_node *
174 pop (void)
175 {
176   ir_node *n = stack[--tos];
177   mark_irn_not_in_stack(n);
178   return n;
179 }
180
181 /* The nodes up to n belong to the current loop.
182    Removes them from the stack and adds them to the current loop. */
183 static INLINE void
184 pop_scc_to_loop (ir_node *n)
185 {
186   ir_node *m;
187
188   for (;;) {
189     m = pop();
190     set_irn_dfn(m, loop_node_cnt);
191     loop_node_cnt++;
192     add_loop_node(current_loop, m);
193     set_irn_loop_tmp(m, current_loop);
194     if (m==n) break;
195   }
196 }
197
198 /* Removes and unmarks all nodes up to n from the stack.
199    The nodes must be visited once more to assign them to a scc. */
200 static INLINE void
201 pop_scc_unmark_visit (ir_node *n)
202 {
203   ir_node *m = NULL;
204
205   while (m != n) {
206     m = pop();
207     set_irn_visited(m, 0);
208   }
209 }
210
211 /**********************************************************************/
212 /* The loop datastructure.                                           **/
213 /**********************************************************************/
214
215 /* Allocates a new loop as son of current_loop.  Sets current_loop
216    to the new loop and returns the father. */
217 ir_loop *new_loop (void) {
218   ir_loop *father, *son;
219
220   father = current_loop;
221
222   son = (ir_loop *) obstack_alloc (outermost_ir_graph->obst, sizeof (ir_loop));
223   memset (son, 0, sizeof (ir_loop));
224   son->kind = k_ir_loop;
225   son->sons = NEW_ARR_F (ir_loop *, 0);
226   son->nodes = NEW_ARR_F (ir_node *, 0);
227   if (father) {
228     son->outer_loop = father;
229     add_loop_son(father, son);
230     son->depth = father->depth+1;
231   } else {  /* The root loop */
232     son->outer_loop = son;
233     son->depth = 0;
234   }
235
236   current_loop = son;
237   return father;
238 }
239
240 /* Finishes the datastructures, copies the arrays to the obstack
241    of current_ir_graph. */
242 void mature_loop (ir_loop *loop) {
243   ir_loop **new_sons;
244   ir_node **new_nods;
245
246   new_sons = NEW_ARR_D (ir_loop *, current_ir_graph->obst, ARR_LEN(loop->sons));
247   memcpy (new_sons, loop->sons, sizeof (ir_loop *) * ARR_LEN(loop->sons));
248   DEL_ARR_F(loop->sons);
249   loop->sons = new_sons;
250 }
251
252 /* Returns outer loop, itself if outermost. */
253 ir_loop *get_loop_outer_loop (ir_loop *loop) {
254   assert(loop && loop->kind == k_ir_loop);
255   return loop->outer_loop;
256 }
257
258 /* Returns nesting depth of this loop */
259 int get_loop_depth (ir_loop *loop) {
260   assert(loop); assert(loop->kind == k_ir_loop);
261   return loop->depth;
262 }
263
264 /* @@@ sons are the inner loops _and_ all nodes within them. */
265 /* Returns the number of inner loops */
266 int      get_loop_n_sons (ir_loop *loop) {
267   assert(loop && loop->kind == k_ir_loop);
268   return ARR_LEN(loop->sons);
269 }
270 ir_loop *get_loop_son (ir_loop *loop, int pos) {
271   assert(loop && loop->kind == k_ir_loop);
272   return loop->sons[pos];
273 }
274 static INLINE void
275 add_loop_son(ir_loop *loop, ir_loop *son) {
276   assert(loop && loop->kind == k_ir_loop);
277   ARR_APP1 (ir_loop *, loop->sons, son);
278 }
279
280 /* Returns the number of nodes in the loop */
281 int      get_loop_n_nodes (ir_loop *loop) {
282   assert(loop); assert(loop->kind == k_ir_loop);
283   return ARR_LEN(loop->nodes);
284 }
285 ir_node *get_loop_node (ir_loop *loop, int pos) {
286   assert(loop && loop->kind == k_ir_loop);
287   return loop->nodes[pos];
288 }
289 static INLINE void
290 add_loop_node(ir_loop *loop, ir_node *n) {
291   assert(loop && loop->kind == k_ir_loop);
292   ARR_APP1 (ir_node *, loop->nodes, n);
293 }
294
295 /* The outermost loop is remarked in the surrounding graph. */
296 void     set_irg_loop(ir_graph *irg, ir_loop *loop) {
297   assert(irg);
298   irg->loop = loop;
299 }
300 ir_loop *get_irg_loop(ir_graph *irg) {
301   assert(irg);
302   return irg->loop;
303 }
304
305 /**********************************************************************/
306 /* Constructing and destructing the loop/backedge information.       **/
307 /**********************************************************************/
308
309 /* Initialization steps. **********************************************/
310
311 static INLINE void
312 init_node (ir_node *n, void *env) {
313   set_irn_link (n, new_scc_info());
314   clear_backedges(n);
315 }
316
317 static INLINE void
318 init_scc (ir_graph *irg) {
319   current_dfn = 1;
320   loop_node_cnt = 0;
321   init_stack();
322   irg_walk_graph (irg, init_node, NULL, NULL);
323   /*
324   irg_walk (irg, link_to_reg_end, NULL, NULL);
325   */
326 }
327
328 /* Condition for breaking the recursion. */
329 bool is_outermost_Start(ir_node *n) {
330   /* Test whether this is the outermost Start node.  If so
331      recursion must end. */
332   if ((get_irn_op(n) == op_Block) &&
333       (n == get_irg_start_block(current_ir_graph))) {
334     if ((!interprocedural_view)  ||
335         (current_ir_graph == outermost_ir_graph))
336       return true;
337   }
338   return false;
339 }
340
341 /* Don't walk from nodes to blocks except for Control flow operations. */
342 static INLINE int
343 get_start_index(ir_node *n) {
344   if (is_cfop(n) || is_fragile_op(n) || get_irn_op(n) == op_Start)
345     return -1;
346   else
347     return 0;
348 }
349
350 /* Returns current_ir_graph and set it to the irg of predecessor index
351    of node n. */
352 static INLINE ir_graph *
353 switch_irg (ir_node *n, int index) {
354   ir_graph *old_current = current_ir_graph;
355
356   if (interprocedural_view) {
357     /* Only Filter and Block nodes can have predecessors in other graphs. */
358     if (get_irn_op(n) == op_Filter)
359       n = get_nodes_Block(n);
360     if (get_irn_op(n) == op_Block) {
361       ir_node *cfop = skip_Proj(get_Block_cfgpred(n, index));
362       if (is_ip_cfop(cfop)) {
363         current_ir_graph = get_irn_irg(cfop);
364         set_irg_visited(current_ir_graph, get_max_irg_visited());
365       }
366     }
367   }
368
369   return old_current;
370 }
371
372 /* Walks up the stack passing n and then finding the node
373    where we walked into the irg n is contained in.
374    Here we switch the irg. */
375 static ir_graph *
376 find_irg_on_stack (ir_node *n) {
377   ir_node *m;
378   ir_graph *old_current = current_ir_graph;
379   int i;
380
381   if (interprocedural_view) {
382     for (i = tos; i >= 0; i--) {
383       if (stack[i] == n) break;
384     }
385     if (i < 0) i = tos;
386
387     //printf(" Here\n");
388
389     assert (i >= 0);
390     for (; i >= 0; i--) {
391       m = stack[i];
392       //printf(" Visiting %d ", i); DDMN(m);
393       if (is_ip_cfop(m)) {
394         current_ir_graph = get_irn_irg(m);
395         break;
396       }
397       if (get_irn_op(m) == op_Filter) {
398         /* Find the corresponding ip_cfop */
399         ir_node *pred = stack[i+1];
400         int j;
401         for (j = 0; j < get_Filter_n_cg_preds(m); j++)
402           if (get_Filter_cg_pred(m, j) == pred) break;
403         if (j >= get_Filter_n_cg_preds(m))
404           /* It is a filter we didn't pass as the predecessors are marked. */
405           continue;
406         assert(get_Filter_cg_pred(m, j) == pred);
407         switch_irg(m, j);
408         break;
409       }
410     }
411   }
412
413   return old_current;
414 }
415
416 /* Returns true if n is a loop header, i.e., it is a Block, Phi
417    or Filter node and has predecessors within the loop and out
418    of the loop. */
419 static bool
420 is_head (ir_node *n, ir_node *root)
421 {
422   int i;
423   int some_outof_loop = 0,  some_in_loop = 0;
424
425   /* Test for legal loop header */
426   if (!((get_irn_op(n) == op_Block) ||
427         (get_irn_op(n) == op_Phi) ||
428         ((get_irn_op(n) == op_Filter) && interprocedural_view)))
429     return false;
430
431   if (!is_outermost_Start(n)) {
432     for (i = get_start_index(n); i < get_irn_arity(n); i++) {
433       ir_node *pred = get_irn_n(n, i);
434       assert(pred);
435       if (is_backedge(n, i)) continue;
436       if (!irn_is_in_stack(pred)) {
437         some_outof_loop = 1;
438       } else {
439         assert(get_irn_uplink(pred) >= get_irn_uplink(root));
440         some_in_loop = 1;
441       }
442     }
443   }
444   return some_outof_loop && some_in_loop;
445 }
446
447 /* Returns index of the predecessor with the smallest dfn number
448    greater-equal than limit. */
449 static int
450 smallest_dfn_pred (ir_node *n, int limit)
451 {
452   int i, index = -2, min = -1;
453
454   if (!is_outermost_Start(n)) {
455     for (i = get_start_index(n); i < get_irn_arity(n); i++) {
456       ir_node *pred = get_irn_n(n, i);
457       assert(pred);
458       if (is_backedge(n, i) || !irn_is_in_stack(pred)) continue;
459       if (get_irn_dfn(pred) >= limit
460         && (min == -1 || get_irn_dfn(pred) < min)) {
461         index = i;
462         min = get_irn_dfn(pred);
463       }
464     }
465   }
466   return index;
467 }
468
469 /* Returns index of the predecessor with the largest dfn number. */
470 static int
471 largest_dfn_pred (ir_node *n)
472 {
473   int i, index = -2, max = -1;
474
475   if (!is_outermost_Start(n)) {
476     for (i = get_start_index(n); i < get_irn_arity(n); i++) {
477       ir_node *pred = get_irn_n(n, i);
478       if (is_backedge (n, i) || !irn_is_in_stack(pred)) continue;
479       if (get_irn_dfn(pred) > max) {
480         index = i;
481         max = get_irn_dfn(pred);
482       }
483     }
484   }
485   return index;
486 }
487
488 /* Searches the stack for possible loop heads.  Tests these for backedges.
489    If it finds a head with an unmarked backedge it marks this edge and
490    returns the tail of the loop.
491    If it finds no backedge returns NULL. */
492 static ir_node *
493 find_tail (ir_node *n) {
494   ir_node *m;
495   int i, res_index = -2;
496
497   /*
498     if (!icfg && rm_cyclic_phis && remove_cyclic_phis (n)) return NULL;
499   */
500
501   m = stack[tos-1];
502   if (is_head (m, n)) {
503     res_index = smallest_dfn_pred(m, 0);
504     if ((res_index == -2) &&  /* no smallest dfn pred found. */
505         (n == m))
506       return NULL;
507   } else {
508     if (m == n) return NULL;
509     for (i = tos-2; ; --i) {
510       m = stack[i];
511       if (is_head (m, n)) {
512         res_index = smallest_dfn_pred (m, get_irn_dfn(m) + 1);
513         if (res_index == -2)  /* no smallest dfn pred found. */
514           res_index = largest_dfn_pred (m);
515         break;
516       }
517     }
518   }
519   assert (res_index > -2);
520
521   set_backedge (m, res_index);
522   return is_outermost_Start(n) ? NULL : get_irn_n(m, res_index);
523 }
524
525
526 /* The core algorithm. *****************************************/
527
528 void scc (ir_node *n) {
529   int i;
530   ir_graph *rem;
531
532   if (irn_visited(n)) return;
533   mark_irn_visited(n);
534
535   /* Initialize the node */
536   set_irn_dfn(n, current_dfn);      /* Depth first number for this node */
537   set_irn_uplink(n, current_dfn);   /* ... is default uplink. */
538   set_irn_loop_tmp(n, NULL);
539   current_dfn ++;
540
541   /* What's this good for?
542   n->ana.scc.section = NULL;
543   */
544
545   push(n);
546
547   if (!is_outermost_Start(n)) {
548     for (i = get_start_index(n); i < get_irn_arity(n); i++) {
549       ir_node *m;
550       if (is_backedge(n, i)) continue;
551
552       m = get_irn_ip_pred(n, i);
553       if (!m) continue;
554       scc (m);
555       return_recur(n, i);
556
557       if (irn_is_in_stack(m)) {
558         /* Uplink of m is smaller if n->m is a backedge.
559            Propagate the uplink to mark the loop. */
560         if (get_irn_uplink(m) < get_irn_uplink(n))
561           set_irn_uplink(n, get_irn_uplink(m));
562       }
563     }
564   }
565   if (get_irn_dfn(n) == get_irn_uplink(n)) {
566     /* This condition holds for the node with the incoming backedge. */
567     ir_node *tail = find_tail(n);
568     if (tail) {
569       /* We found a new loop! */
570       ir_loop *l = new_loop();
571       /* Remove the loop from the stack ... */
572       pop_scc_unmark_visit (n);
573       /* and recompute it in a better order; and so that it goes into
574          the new loop. */
575       rem = find_irg_on_stack(tail);
576       scc (tail);
577       current_ir_graph = rem;
578
579       assert (irn_visited(n));
580
581       current_loop = l;
582     } else {
583       pop_scc_to_loop(n);
584     }
585   }
586 }
587
588 /* Constructs backedge information for irg. In interprocedural view constructs
589    backedges for all methods called by irg, too. */
590 void construct_backedges(ir_graph *irg) {
591   ir_graph *rem = current_ir_graph;
592   ir_loop *head_rem;
593   int i;
594
595   current_ir_graph = irg;
596   outermost_ir_graph = irg;
597
598   init_scc(irg);
599
600   current_loop = NULL;
601   new_loop();  /* sets current_loop */
602   head_rem = current_loop; /* Just for assertion */
603
604   if (interprocedural_view) {
605     set_irg_visited(irg, inc_max_irg_visited());
606     init_ip_walk ();
607   } else {
608     inc_irg_visited(irg);
609   }
610
611   scc(get_irg_end(irg));
612   for (i = 0; i < get_End_n_keepalives(get_irg_end(irg)); i++)
613     scc(get_End_keepalive(get_irg_end(irg), i));
614
615   if (interprocedural_view) finish_ip_walk();
616
617   assert(head_rem == current_loop);
618   set_irg_loop(irg, current_loop);
619   assert(get_irg_loop(irg)->kind == k_ir_loop);
620   /*
621   irg->loops = current_loop;
622   if (icfg == 1) {
623     int count = 0;
624     int depth = 0;
625     count_loop (the_loop, &count, &depth);
626     }
627   }
628   */
629   current_ir_graph = rem;
630 }