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