fixed a bunch of warnings (in OPTIMIZE mode)
[libfirm] / ir / ana / interval_analysis.c
1 /*
2  * Copyright (C) 1995-2007 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   Implementation of interval analysis
23  * @version $Id$
24  */
25 #ifdef HAVE_CONFIG_H
26 #include "config.h"
27 #endif
28
29 #ifdef HAVE_STRING_H
30 #include <string.h>
31 #endif
32
33 #include "interval_analysis.h"
34 #include "execution_frequency.h"
35 #include "firm_common_t.h"
36 #include "set.h"
37 #include "array.h"
38
39 #include "irloop.h"
40 #include "irnode.h"
41 #include "irdump_t.h"
42 #include "irdom.h"
43 #include "irflag.h"
44 #include "irprintf.h"
45 #include "hashptr.h"
46
47 /*------------------------------------------------------------------*/
48 /* A new in array via a hashmap. */
49 /* The in array refers to the loop the block is contained in if the */
50 /* block is not in blocks loop. */
51 /*------------------------------------------------------------------*/
52
53 typedef struct {
54   void *reg;
55   void **in_array;
56   void **op_array;
57   int n_outs;
58   int n_exc_outs;
59 } region_attr;
60
61 static set *region_attr_set = NULL;
62
63 int region_attr_cmp(const void *e1, const void *e2, size_t size) {
64   region_attr *ra1 = (region_attr *)e1;
65   region_attr *ra2 = (region_attr *)e2;
66   (void) size;
67   return (ra1->reg != ra2->reg);
68 }
69
70 static INLINE int attr_set_hash(region_attr *a) {
71   return HASH_PTR(a->reg);
72 }
73
74 static INLINE region_attr *get_region_attr(void *region) {
75   region_attr r_attr, *res;
76   r_attr.reg = region;
77
78   res = set_find(region_attr_set, &r_attr, sizeof(r_attr), attr_set_hash(&r_attr));
79
80   if (!res) {
81     r_attr.in_array = NEW_ARR_F(void *, 0);
82     if (is_ir_loop(region))
83       r_attr.op_array = NEW_ARR_F(void *, 0);
84     else
85       r_attr.op_array = NULL;
86     r_attr.n_outs = 0;
87     r_attr.n_exc_outs = 0;
88     res = set_insert(region_attr_set, &r_attr, sizeof(r_attr), attr_set_hash(&r_attr));
89   }
90
91   return res;
92 }
93
94 int get_region_n_ins(void *region) {
95   return ARR_LEN(get_region_attr(region)->in_array);
96 }
97
98 void *get_region_in(void *region, int pos) {
99   assert(0 <= pos && pos < get_region_n_ins(region));
100   return ((get_region_attr(region)->in_array)[pos]);
101 }
102
103 void add_region_in (void *region, void *in) {
104   ARR_APP1(void *, get_region_attr(region)->in_array, in);
105   get_region_attr(in)->n_outs++;
106 }
107
108 int get_region_n_outs(void *region) {
109   return get_region_attr(region)->n_outs;
110 }
111
112 int get_region_n_exc_outs(void *region) {
113   return get_region_attr(region)->n_exc_outs;
114 }
115
116 void inc_region_n_exc_outs(void *region) {
117   (get_region_attr(region)->n_exc_outs)++;
118 }
119
120 void *get_loop_cfop(void *region, int pos) {
121   assert(0 <= pos && pos < get_region_n_ins(region));
122   return ((get_region_attr(region)->op_array)[pos]);
123 }
124
125 void add_loop_cfop (void *region, void *cfop) {
126   assert(cfop);
127   ARR_APP1(void *, get_region_attr(region)->op_array, cfop);
128 }
129
130 static INLINE void exc_outs(void *reg, ir_node *cfop) {
131   if (is_fragile_op(cfop) || (is_fragile_Proj(cfop)))
132     inc_region_n_exc_outs(reg);
133 }
134
135 /*------------------------------------------------------------------*/
136 /* Algorithm to construct the interval edges based on a loop tree. */
137 /* Walk a loop and add all edges.  Walk inner loops by recursion. */
138 /*------------------------------------------------------------------*/
139
140 /* return non-zero if outer can be reached from inner via the outer loop relation */
141 static int find_outer_loop(ir_loop *inner, ir_loop *outer, ir_node *b, ir_node *cfop) {
142   if (get_loop_outer_loop(inner) == outer) {
143     add_region_in(inner, b);
144     add_loop_cfop(inner, cfop);
145     exc_outs(b, cfop);
146     return 1;
147   }
148   return 0;
149 }
150
151 static int test_loop_nest(ir_node *pred_b, ir_loop *nest) {
152   int i, n_elems = get_loop_n_elements(nest);
153
154   for (i = 0; (i < n_elems); ++i) {
155     loop_element e = get_loop_element(nest, i);
156     switch (*e.kind) {
157     case k_ir_node: {
158       if (e.node == pred_b) return 1;
159     } break;
160     case k_ir_loop: {
161       if (test_loop_nest(pred_b, e.son)) return 1;
162     } break;
163     default: break;
164     }
165   }
166   return 0;
167 }
168
169 static int find_inner_loop(ir_node *b, ir_loop *l, ir_node *pred, ir_node *cfop) {
170   int i, n_elems = get_loop_n_elements(l);
171   int found = 0;
172
173   for (i = 0; (i < n_elems) && !found; ++i) {
174     loop_element e = get_loop_element(l, i);
175     switch (*e.kind) {
176     case k_ir_node: {
177       if (e.node == b) return 0;
178     } break;
179     case k_ir_loop: {
180       found = test_loop_nest(pred, e.son);
181       if (found) {
182         add_region_in(b, e.son);
183         exc_outs(e.son, cfop);
184         //if (is_fragile_op(cfop)) inc_region_n_exc_outs(b);
185         return found;
186       }
187     } break;
188     default: break;
189     }
190   }
191   return found;
192 }
193
194
195 static int find_previous_loop(ir_loop *l, ir_loop *pred_l, ir_node *b,
196                               ir_node *pred_b, ir_node *cfop)
197 {
198   ir_loop *outer = get_loop_outer_loop(l);
199   int found, i;
200   int l_pos = get_loop_element_pos(outer, l);
201   (void) pred_l;
202   assert(l_pos > -1);
203   assert(l_pos > 0 && "Is this a necessary condition?  There could be a perfect nest ...");
204
205   for (i = l_pos -1, found = 0; i > -1 && !found; --i) {
206     ir_loop *k = get_loop_element(outer, i).son;
207     if (is_ir_loop(k)) {
208       found = test_loop_nest(pred_b, k);
209       if (found) {
210         add_region_in(l, k);
211         //if (is_fragile_op(cfop)) inc_region_n_exc_outs(k);
212         exc_outs(k, cfop);
213         add_loop_cfop(l, cfop);
214         add_region_in(b, NULL);
215       }
216     }
217   }
218
219   return found;
220 }
221
222
223 /**
224  * Compute the edges for the interval graph.
225  *
226  * @param b The block for which to construct the edges.
227  * @param l The loop of b.
228  *
229  * There are four cases:
230  * - The pred block is in the same loop.  Add a normal block-block edge.
231  * - The pred block is in a loop contained in this loop, somewhere down in
232  *   the nesting. The predecessor of this block is the outermost loop of the nest
233  *   directly contained in l.
234  * - The pred block is in the outer loop of l.  l gets an edge to the pred block.
235  * - The outer loop of l contains another loop k just before l.  The control flow
236  *   branches directly from loop k to loop l.  Add an edge l->k.  Watch it: k must
237  *   not be a direct predecessor of l in the loop tree!
238  */
239 static void construct_interval_block(ir_node *b, ir_loop *l) {
240   int i, n_cfgpreds = get_Block_n_cfgpreds(b);
241
242   if (b == get_irg_start_block(current_ir_graph)) return;
243   /* We want nice blocks. */
244   assert(n_cfgpreds > 0);
245
246   for (i = 0; i < n_cfgpreds; ++i) {
247     ir_node *cfop, *pred;
248     ir_loop *pred_l;
249
250     if (is_backedge(b, i)) {
251       if (b != get_loop_element(l, 0).node) {
252         if (get_firm_verbosity()) {
253                 ir_printf("Loophead not at loop position 0. %+F\n", b);
254         }
255       }
256       /* There are no backedges in the interval decomposition. */
257       add_region_in(b, NULL);
258       continue;
259     }
260
261     cfop = get_Block_cfgpred(b, i);
262     if (is_Proj(cfop)) {
263       if (get_irn_op(get_Proj_pred(cfop)) != op_Cond) {
264         cfop = skip_Proj(cfop);
265       } else {
266         assert(get_nodes_block(cfop) == get_nodes_block(skip_Proj(cfop)));
267       }
268     }
269
270     pred = skip_Proj(get_nodes_block(cfop));
271     /* We want nice blocks. */
272     assert(   get_irn_op(pred) != op_Bad
273            && get_irn_op(skip_Proj(get_Block_cfgpred(b, i))) != op_Bad);
274     pred_l = get_irn_loop(pred);
275     if (pred_l == l) {
276       add_region_in(b, pred);
277       //if (is_fragile_op(cfop)) inc_region_n_exc_outs(b);
278       exc_outs(pred, cfop);
279     } else {
280       int found = find_inner_loop(b, l, pred, cfop);
281       if (!found) {
282             if (b != get_loop_element(l, 0).node) {
283               if (get_firm_verbosity()) {
284                 ir_printf("Loop entry not at loop position 0. %+F\n", b);
285               }
286             }
287             found = find_outer_loop(l, pred_l, pred, cfop);
288             if (found) add_region_in(b, NULL);  /* placeholder */
289       }
290       if (!found) {
291         found = find_previous_loop(l, pred_l, b, pred, cfop);
292       }
293       if (!found) {
294             assert(is_backedge(b, i));
295             assert(found && "backedge from inner loop");
296       }
297     }
298
299     if (b != get_loop_element(l, 0).node) {
300       /* Check for improper region */
301       if (has_backedges(b)) {
302             ir_fprintf(stderr, "Improper Region!!!!!! %+F\n", b);
303       }
304     }
305   }
306 }
307
308 static void construct_interval_edges(ir_loop *l) {
309   int i, n_elems = get_loop_n_elements(l);
310   for (i = 0; i < n_elems; ++i) {
311     loop_element e = get_loop_element(l, i);
312     switch (*e.kind) {
313     case k_ir_node: {
314       construct_interval_block(e.node, l);
315     } break;
316     case k_ir_loop: {
317       construct_interval_edges(e.son);
318     } break;
319     default: break;
320     }
321   }
322 }
323
324 void construct_intervals(ir_graph *irg) {
325   ir_loop *l;
326   ir_graph *rem = current_ir_graph;
327   current_ir_graph = irg;
328
329   if (!region_attr_set)
330     region_attr_set = new_set(region_attr_cmp, 256);
331
332   construct_cf_backedges(current_ir_graph);
333
334   l = get_irg_loop(current_ir_graph);
335
336   construct_interval_edges(l);
337
338   current_ir_graph = rem;
339 }
340
341 void free_intervals(void) {
342   //void **ins;
343   if (!region_attr_set) return;
344   /* @@@ mem leak
345   for (ins = (void **)pmap_first(region_in_map);
346        ins;
347        ins = (void **)pmap_next(region_in_map)) {
348     //DEL_ARR_F(ins);
349   }
350   */
351   del_set(region_attr_set);
352   region_attr_set = NULL;
353 }
354
355 /*------------------------------------------------------------------*/
356 /* A vcg dumper showing an interval decomposition of a cfg.         */
357 /*                                                                  */
358 /*------------------------------------------------------------------*/
359
360 void dump_region_edges(FILE *F, void *reg) {
361   int i, n_ins = get_region_n_ins(reg);
362
363   if (is_ir_node(reg) && get_Block_n_cfgpreds((ir_node *)reg) > get_region_n_ins(reg)) {
364     for (i = n_ins; i < get_Block_n_cfgpreds((ir_node *)reg); ++i) {
365       if (is_backedge((ir_node *)reg, i))
366         fprintf (F, "backedge: { sourcename: \"");
367       else
368         fprintf (F, "edge: { sourcename: \"");
369       PRINT_NODEID(((ir_node *)reg));
370       fprintf (F, "\" targetname: \"");
371       PRINT_NODEID(get_nodes_block(skip_Proj(get_Block_cfgpred((ir_node *)reg, i))));
372       fprintf (F, "\" " BLOCK_EDGE_ATTR "}\n");
373     }
374   }
375
376   for (i = 0; i < n_ins; ++i) {
377     void *target = get_region_in(reg, i);
378
379     if (is_ir_node(reg)) {
380       if (get_Block_n_cfgpreds((ir_node *)reg) != get_region_n_ins(reg)) {
381         ir_printf("n_cfgpreds = %d, n_ins = %d\n %+F\n", get_Block_n_cfgpreds((ir_node *)reg), get_region_n_ins(reg), (ir_node*) reg);
382       }
383     }
384
385     if ((!target || (is_ir_node(reg) && !is_ir_node(target))) && i < get_Block_n_cfgpreds((ir_node *)reg)) {
386       assert(is_ir_node(reg));
387       if (is_backedge((ir_node *)reg, i))
388         fprintf (F, "backedge: { sourcename: \"");
389       else
390         fprintf (F, "edge: { sourcename: \"");
391       PRINT_NODEID(((ir_node *)reg));
392       fprintf (F, "\" targetname: \"");
393       PRINT_NODEID(get_nodes_block(skip_Proj(get_Block_cfgpred((ir_node *)reg, i))));
394       fprintf (F, "\" " BLOCK_EDGE_ATTR "}\n");
395
396       if (!target) continue;
397     }
398
399     fprintf (F, "edge: { sourcename: \"");
400     if (is_ir_node(reg)) {
401       PRINT_NODEID(((ir_node *)reg));
402     } else {
403       PRINT_LOOPID(((ir_loop *)reg));
404     }
405     fprintf (F, "\" targetname: \"");
406     if (is_ir_node(target)) {
407       PRINT_NODEID(((ir_node *)target));
408     } else {
409       PRINT_LOOPID(((ir_loop *)target));
410     }
411     fprintf (F, "\"");
412     if (is_ir_node(reg) && is_fragile_op(skip_Proj(get_Block_cfgpred(reg, i))))
413       fprintf(F, EXC_CF_EDGE_ATTR);
414     fprintf (F, "}\n");
415   }
416 }
417
418 #include "execution_frequency.h"
419
420 void dump_interval_block(FILE *F, ir_node *block) {
421   int i, fl;
422   /* This is a block. Dump a node for the block. */
423   fprintf (F, "node: {title: \""); PRINT_NODEID(block);
424   fprintf (F, "\" label: \"");
425   if (block == get_irg_start_block(get_irn_irg(block)))
426     fprintf(F, "Start ");
427   if (block == get_irg_end_block(get_irn_irg(block)))
428     fprintf(F, "End ");
429
430   fprintf (F, "%s ", get_op_name(get_irn_op(block)));
431   PRINT_NODEID(block);
432   fprintf(F, " freq: %9.4lf", get_region_exec_freq(block));
433   fprintf(F, " n_outs: %d", get_region_n_outs(block));
434   fprintf(F, " n_exc_outs: %d", get_region_n_exc_outs(block));
435   fprintf (F, "\" ");
436   fprintf(F, "info1:\"");
437   if (dump_dominator_information_flag)
438     fprintf(F, "dom depth %d\n", get_Block_dom_depth(block));
439
440   /* show arity and possible Bad predecessors of the block */
441   fprintf(F, "arity: %d\n", get_Block_n_cfgpreds(block));
442   for (fl = i = 0; i < get_Block_n_cfgpreds(block); ++i) {
443     ir_node *pred = get_Block_cfgpred(block, i);
444     if (is_Bad(pred)) {
445       if (! fl)
446         fprintf(F, "Bad pred at pos: ");
447       fprintf(F, "%d ", i);
448       fl = 1;
449     }
450   }
451   if (fl)
452     fprintf(F, "\n");
453
454   fprintf (F, "\"");  /* closing quote of info */
455
456   if ((block == get_irg_start_block(get_irn_irg(block))) ||
457       (block == get_irg_end_block(get_irn_irg(block)))     )
458     fprintf(F, " color:blue ");
459   else if (fl)
460     fprintf(F, " color:yellow ");
461
462   fprintf (F, "}\n");
463 }
464
465 void dump_interval_loop(FILE *F, ir_loop *l) {
466   int i, n_elems = get_loop_n_elements(l);
467
468   fprintf(F, "graph: { title: \"");
469   PRINT_LOOPID(l);
470   fprintf(F, "\" label: \"loop %d", get_loop_loop_nr(l));
471   fprintf(F, " freq: %9.4lf", get_region_exec_freq(l));
472   fprintf(F, " n_outs: %d", get_region_n_outs(l));
473   fprintf(F, " n_exc_outs: %d", get_region_n_exc_outs(l));
474   fprintf(F, "\" status:clustered color:white \n");
475
476   for (i = 0; i < n_elems; ++i) {
477     loop_element e = get_loop_element(l, i);
478     dump_region_edges(F, e.node);
479     switch (*e.kind) {
480     case k_ir_node: {
481       dump_interval_block(F, e.node);
482     } break;
483     case k_ir_loop: {
484       dump_interval_loop(F, e.son);
485     } break;
486     default: break;
487     }
488   }
489
490   fprintf(F, "}\n\n");
491 }
492
493
494 void dump_interval_graph(ir_graph *irg, const char *suffix) {
495   FILE *f;
496
497   if (!is_filtered_dump_name(get_entity_ident(get_irg_entity(irg))))
498     return;
499
500   f = vcg_open(irg, suffix, "-intervals");
501   dump_vcg_header(f, get_irg_dump_name(irg), NULL);
502
503   current_ir_graph = irg;
504
505   dump_interval_loop(f, get_irg_loop(current_ir_graph));
506
507   vcg_close(f);
508 }