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