remove doubly included config.h
[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 #include <string.h>
28
29 #include "debug.h"
30 #include "interval_analysis.h"
31 #include "execution_frequency.h"
32 #include "set.h"
33 #include "array.h"
34
35 #include "irloop.h"
36 #include "irnode.h"
37 #include "irdump_t.h"
38 #include "irdom.h"
39 #include "irflag.h"
40 #include "irprintf.h"
41 #include "hashptr.h"
42
43 DEBUG_ONLY(static firm_dbg_module_t *dbg);
44
45 /*------------------------------------------------------------------*/
46 /* A new in array via a hashmap. */
47 /* The in array refers to the loop the block is contained in if the */
48 /* block is not in blocks loop. */
49 /*------------------------------------------------------------------*/
50
51 /** The attributes of a region. */
52 typedef struct {
53         void *reg;        /**< The region: A block or a loop. */
54         void **in_array;  /**< New in-array for this region, may contain NULL (to be synchronized with block inputs). */
55         void **op_array;  /**< If reg is a loop, the control flow operations leading to this loop. */
56         int n_outs;       /**< The number of out edges of this region. */
57         int n_exc_outs;   /**< The number of exception out edges of this region. */
58 } region_attr;
59
60 /** A Hashset containing the region attributes. */
61 static set *region_attr_set = NULL;
62
63 /**
64  * Compare two region attributes for identical regions.
65  */
66 static int region_attr_cmp(const void *e1, const void *e2, size_t size)
67 {
68         region_attr *ra1 = (region_attr *)e1;
69         region_attr *ra2 = (region_attr *)e2;
70         (void) size;
71         return (ra1->reg != ra2->reg);
72 }
73
74 /** Hash a region attribute (the region only). */
75 static inline int attr_set_hash(region_attr *a)
76 {
77         return HASH_PTR(a->reg);
78 }
79
80 /**
81  * Return the region attribute for a given region.
82  * Allocate one if not exists.
83  *
84  * @param region  the region
85  */
86 static inline region_attr *get_region_attr(void *region)
87 {
88         region_attr r_attr, *res;
89         r_attr.reg = region;
90
91         res = set_find(region_attr_set, &r_attr, sizeof(r_attr), attr_set_hash(&r_attr));
92
93         if (res == NULL) {
94                 r_attr.in_array = NEW_ARR_F(void *, 0);
95                 if (is_ir_loop(region))
96                         r_attr.op_array = NEW_ARR_F(void *, 0);
97                 else
98                         r_attr.op_array = NULL;
99                 r_attr.n_outs     = 0;
100                 r_attr.n_exc_outs = 0;
101                 res = set_insert(region_attr_set, &r_attr, sizeof(r_attr), attr_set_hash(&r_attr));
102         }
103
104         return res;
105 }
106
107 int get_region_n_ins(void *region)
108 {
109         return ARR_LEN(get_region_attr(region)->in_array);
110 }
111
112 void *get_region_in(void *region, int pos)
113 {
114         assert(0 <= pos && pos < get_region_n_ins(region));
115         return ((get_region_attr(region)->in_array)[pos]);
116 }
117
118 void add_region_in(void *region, void *in)
119 {
120         ARR_APP1(void *, get_region_attr(region)->in_array, in);
121         get_region_attr(in)->n_outs++;
122 }
123
124 int get_region_n_outs(void *region)
125 {
126         return get_region_attr(region)->n_outs;
127 }
128
129 int get_region_n_exc_outs(void *region)
130 {
131         return get_region_attr(region)->n_exc_outs;
132 }
133
134 void inc_region_n_exc_outs(void *region)
135 {
136         (get_region_attr(region)->n_exc_outs)++;
137 }
138
139 void *get_loop_cfop(void *region, int pos)
140 {
141         assert(0 <= pos && pos < get_region_n_ins(region));
142         return ((get_region_attr(region)->op_array)[pos]);
143 }
144
145 /** Add a control flow op to a loop region. */
146 static inline void add_loop_cfop(void *region, void *cfop)
147 {
148         assert(cfop);
149         ARR_APP1(void *, get_region_attr(region)->op_array, cfop);
150 }
151
152 /**
153  * Increase the number of exception outputs if a control flow
154  * operation (that is inside the given region) is a fragile operation.
155  *
156  * @param reg   a region
157  * @param cfop  a control flow operation leaving this region
158  */
159 static inline void exc_outs(void *reg, ir_node *cfop)
160 {
161         if (is_fragile_op(cfop) || is_fragile_Proj(cfop))
162                 inc_region_n_exc_outs(reg);
163 }
164
165 /*------------------------------------------------------------------*/
166 /* Algorithm to construct the interval edges based on a loop tree. */
167 /* Walk a loop and add all edges.  Walk inner loops by recursion. */
168 /*------------------------------------------------------------------*/
169
170 /**
171  * Check if the loop of a given block is the outer loop of the current one.
172  * If yes, add an edge from the block to the region of the current loop.
173  *
174  * @param inner  the current (possible inner) loop
175  * @param outer  the loop of blk
176  * @param blk    a block
177  * @param cfop   the control flow op leaving blk
178  *
179  * @return  non-zero if outer can be reached from inner via the outer loop relation
180  */
181 static int find_outer_loop(ir_loop *inner, ir_loop *outer, ir_node *blk, ir_node *cfop)
182 {
183         if (get_loop_outer_loop(inner) == outer) {
184                 add_region_in(inner, blk);
185                 add_loop_cfop(inner, cfop);
186                 exc_outs(blk, cfop);
187                 return 1;
188         }
189         return 0;
190 }
191
192 /**
193  * Check if a given block can be found in a given loop
194  * or its nesting loops.
195  *
196  * @param blk   a block
197  * @param loop  a loop
198  */
199 static int test_loop_nest(ir_node *blk, ir_loop *loop)
200 {
201         int i, n_elems = get_loop_n_elements(loop);
202
203         for (i = 0; i < n_elems; ++i) {
204                 loop_element e = get_loop_element(loop, i);
205                 switch (*e.kind) {
206                 case k_ir_node:
207                         if (e.node == blk)
208                                 return 1;
209                         break;
210                 case k_ir_loop:
211                         if (test_loop_nest(blk, e.son))
212                                 return 1;
213                         break;
214                 default:
215                         break;
216                 }
217         }
218         return 0;
219 }
220
221 /**
222  * Check if pred is a block from an inner loop jumping via cfop to the block blk.
223  * If yes, add an edge from pred's loop to the region blk.
224
225  * @param blk   a block
226  * @param l     the loop of blk
227  * @param pred  a predecessor block of blk
228  * @param cfop  the control flow op from pred to blk
229  *
230  * @return non-zero if pred is from an inner loop
231  */
232 static int find_inner_loop(ir_node *blk, ir_loop *l, ir_node *pred, ir_node *cfop)
233 {
234         int i, n_elems = get_loop_n_elements(l);
235         int found = 0;
236
237         for (i = 0; i < n_elems; ++i) {
238                 loop_element e = get_loop_element(l, i);
239                 switch (*e.kind) {
240                 case k_ir_node:
241                         if (e.node == blk) {
242                                 /* stop the search if we reach blk, pred cannot be found
243                                    later in the loop */
244                                 return 0;
245                         }
246                         break;
247                 case k_ir_loop:
248                         found = test_loop_nest(pred, e.son);
249                         if (found) {
250                                 add_region_in(blk, e.son);
251                                 exc_outs(e.son, cfop);
252                                 return found;
253                         }
254                         break;
255                 default:
256                         break;
257                 }
258         }
259         return found;
260 }
261
262 /**
263  * Check if a predecessor block pred_b is from a previous loop of the
264  * block b.
265  *
266  * @param l       the loop of the block b
267  * @param pred_l  the loop of the block pred_b
268  * @param b       the block
269  * @param pred_b  the predecessor block
270  * @param cfop    the control flow node leaving pred_b for b
271  *
272  * @return non-zero if pred is from an previous loop
273  */
274 static int find_previous_loop(ir_loop *l, ir_loop *pred_l, ir_node *b,
275                               ir_node *pred_b, ir_node *cfop)
276 {
277         ir_loop *outer = get_loop_outer_loop(l);
278         int found, i;
279         int l_pos = get_loop_element_pos(outer, l);
280         (void) pred_l;
281         assert(l_pos > -1);
282         assert(l_pos > 0 && "Is this a necessary condition?  There could be a perfect nest ...");
283
284         for (i = l_pos -1, found = 0; i > -1 && !found; --i) {
285                 ir_loop *k = get_loop_element(outer, i).son;
286                 if (is_ir_loop(k)) {
287                         found = test_loop_nest(pred_b, k);
288                         if (found) {
289                                 add_region_in(l, k);
290                                 exc_outs(k, cfop);
291                                 add_loop_cfop(l, cfop);
292                                 /* placeholder: the edge is in the loop region */
293                                 add_region_in(b, NULL);
294                         }
295                 }
296         }
297
298         return found;
299 }
300
301
302 /**
303  * Compute the edges for the interval graph.
304  *
305  * @param blk  The block for which to construct the edges.
306  * @param l    The loop of blk.
307  *
308  * There are four cases:
309  * - A predecessor block is in the same loop.  Add a normal block-block edge.
310  * - A predecessor block is in a loop contained in this loop, somewhere down in
311  *   the nesting.  The predecessor of this block is the outermost loop of the nest
312  *   directly contained in l.
313  * - A predecessor block is in the outer loop of l.  l gets an edge to the predecessor block.
314  * - The outer loop of l contains another loop k just before l.  The control flow
315  *   branches directly from loop k to loop l.  Add an edge l->k.  Watch it: k must
316  *   not be a direct predecessor of l in the loop tree!
317  */
318 static void construct_interval_block(ir_node *blk, ir_loop *l)
319 {
320         int i, n_cfgpreds;
321
322         if (blk == get_irg_start_block(current_ir_graph))
323                 return;
324
325         n_cfgpreds = get_Block_n_cfgpreds(blk);
326         /* We want nice blocks. */
327         assert(n_cfgpreds > 0);
328
329         for (i = 0; i < n_cfgpreds; ++i) {
330                 ir_node *cfop, *pred;
331                 ir_loop *pred_l;
332
333                 if (is_backedge(blk, i)) {
334                         if (blk != get_loop_element(l, 0).node) {
335                                 DB((dbg, LEVEL_1, "Loophead not at loop position 0. %+F\n", blk));
336                         }
337                         /* There are no backedges in the interval decomposition. */
338                         add_region_in(blk, NULL);
339                         continue;
340                 }
341
342                 cfop = get_Block_cfgpred(blk, i);
343                 if (is_Proj(cfop)) {
344                         ir_node *op = skip_Proj(cfop);
345                         if (is_fragile_op(op) && get_Proj_proj(cfop) == pn_Generic_X_except) {
346                                 /*
347                                  * Skip the Proj for the exception flow only, leave the
348                                  * not exception flow Proj's intact.
349                                  * If the old semantic is used (only one exception Proj) this
350                                  * should lead to the same representation as before.
351                                  */
352                                 cfop = op;
353                         } else {
354                                 assert(get_nodes_block(cfop) == get_nodes_block(skip_Proj(cfop)));
355                         }
356                 }
357
358                 pred = skip_Proj(get_nodes_block(cfop));
359                 /* We want nice blocks. */
360                 assert(!is_Bad(pred) && !is_Bad(skip_Proj(get_Block_cfgpred(blk, i))));
361                 pred_l = get_irn_loop(pred);
362                 if (pred_l == l) {
363                         /* first case: both blocks are in the same loop */
364                         add_region_in(blk, pred);
365                         exc_outs(pred, cfop);
366                 } else {
367                         /* check for the second case: pred is from an inner loop */
368                         int found = find_inner_loop(blk, l, pred, cfop);
369                         if (!found) {
370                                 if (blk != get_loop_element(l, 0).node) {
371                                         DB((dbg, LEVEL_1, "Loop entry not at loop position 0. %+F\n", blk));
372                                 }
373                                 /* check for the third case: pred is in an outer loop */
374                                 found = find_outer_loop(l, pred_l, pred, cfop);
375                                 if (found) {
376                                           /* placeholder: the edge is added to the loop region */
377                                         add_region_in(blk, NULL);
378                                 } else {
379                                         /* fourth case: pred is from the previous loop */
380                                         found = find_previous_loop(l, pred_l, blk, pred, cfop);
381
382                                         assert(found && "decomposition failed");
383                                 }
384                         }
385                 }
386
387 #ifdef DEBUG_libfirm
388                 if (blk != get_loop_element(l, 0).node) {
389                         /* Check for improper region. But these can happen, so what? */
390                         if (has_backedges(blk)) {
391                                 DB((dbg, LEVEL_1, "Improper Region %+F\n", blk));
392                         }
393                 }
394 #endif
395         }
396 }
397
398 /**
399  * Construct interval edges for a given (control flow) loop.
400  *
401  * @param l  the cf loop
402  */
403 static void construct_interval_edges(ir_loop *l)
404 {
405         int i, n_elems = get_loop_n_elements(l);
406         for (i = 0; i < n_elems; ++i) {
407                 loop_element e = get_loop_element(l, i);
408                 switch (*e.kind) {
409                 case k_ir_node:
410                         construct_interval_block(e.node, l);
411                         break;
412                 case k_ir_loop:
413                         construct_interval_edges(e.son);
414                         break;
415                 default:
416                         break;
417                 }
418         }
419 }
420
421 void construct_intervals(ir_graph *irg)
422 {
423         ir_loop  *l;
424         ir_graph *rem = current_ir_graph;
425
426         current_ir_graph = irg;
427
428         FIRM_DBG_REGISTER(dbg, "firm.ana.interval");
429
430         if (region_attr_set == NULL)
431                 region_attr_set = new_set(region_attr_cmp, 256);
432
433         construct_cf_backedges(irg);
434
435         l = get_irg_loop(irg);
436
437         construct_interval_edges(l);
438
439         current_ir_graph = rem;
440 }
441
442 void free_intervals(void)
443 {
444         region_attr *res;
445
446         if (region_attr_set == NULL)
447                 return;
448
449         for (res = set_first(region_attr_set);
450              res != NULL;
451              res = set_next(region_attr_set)) {
452                 DEL_ARR_F(res->in_array);
453                 if (res->op_array != NULL)
454                         DEL_ARR_F(res->op_array);
455         }
456
457         del_set(region_attr_set);
458         region_attr_set = NULL;
459 }
460
461 /*------------------------------------------------------------------*/
462 /* A vcg dumper showing an interval decomposition of a cfg.         */
463 /*                                                                  */
464 /*------------------------------------------------------------------*/
465
466 void dump_region_edges(FILE *F, void *reg)
467 {
468         int i, n_ins = get_region_n_ins(reg);
469
470         if (is_ir_node(reg)) {
471                 ir_node *irn = reg;
472                 if (get_Block_n_cfgpreds(irn) > get_region_n_ins(reg)) {
473                         for (i = n_ins; i < get_Block_n_cfgpreds(irn); ++i) {
474                                 if (is_backedge(irn, i))
475                                         fprintf(F, "backedge: { sourcename: \"");
476                                 else
477                                         fprintf(F, "edge: { sourcename: \"");
478                                 PRINT_NODEID(irn);
479                                 fprintf(F, "\" targetname: \"");
480                                 PRINT_NODEID(get_nodes_block(skip_Proj(get_Block_cfgpred(irn, i))));
481                                 fprintf(F, "\" " BLOCK_EDGE_ATTR "}\n");
482                         }
483                 }
484         }
485
486         for (i = 0; i < n_ins; ++i) {
487                 void *target = get_region_in(reg, i);
488
489                 if (is_ir_node(reg)) {
490                         ir_node *irn = reg;
491                         if (get_Block_n_cfgpreds(irn) != get_region_n_ins(reg)) {
492                                 ir_printf("n_cfgpreds = %d, n_ins = %d\n %+F\n", get_Block_n_cfgpreds(irn), get_region_n_ins(reg), irn);
493                         }
494                 }
495
496                 if ((!target || (is_ir_node(reg) && !is_ir_node(target))) && i < get_Block_n_cfgpreds((ir_node *)reg)) {
497                         assert(is_ir_node(reg));
498                         if (is_backedge((ir_node *)reg, i))
499                                 fprintf(F, "backedge: { sourcename: \"");
500                         else
501                                 fprintf(F, "edge: { sourcename: \"");
502                         PRINT_NODEID(((ir_node *)reg));
503                         fprintf(F, "\" targetname: \"");
504                         PRINT_NODEID(get_nodes_block(skip_Proj(get_Block_cfgpred((ir_node *)reg, i))));
505                         fprintf(F, "\" " BLOCK_EDGE_ATTR "}\n");
506
507                         if (!target) continue;
508                 }
509
510                 fprintf(F, "edge: { sourcename: \"");
511                 if (is_ir_node(reg)) {
512                         PRINT_NODEID(((ir_node *)reg));
513                 } else {
514                         PRINT_LOOPID(((ir_loop *)reg));
515                 }
516                 fprintf(F, "\" targetname: \"");
517                 if (is_ir_node(target)) {
518                         PRINT_NODEID(((ir_node *)target));
519                 } else {
520                         PRINT_LOOPID(((ir_loop *)target));
521                 }
522                 fprintf(F, "\"");
523                 if (is_ir_node(reg) && is_fragile_op(skip_Proj(get_Block_cfgpred(reg, i))))
524                         fprintf(F, EXC_CF_EDGE_ATTR);
525                 fprintf(F, "}\n");
526         }
527 }
528
529 #include "execution_frequency.h"
530
531 static void dump_interval_block(FILE *F, ir_node *block)
532 {
533         int i, fl;
534         /* This is a block. Dump a node for the block. */
535         fprintf(F, "node: {title: \""); PRINT_NODEID(block);
536         fprintf(F, "\" label: \"");
537         if (block == get_irg_start_block(get_irn_irg(block)))
538                 fprintf(F, "Start ");
539         if (block == get_irg_end_block(get_irn_irg(block)))
540                 fprintf(F, "End ");
541
542         fprintf(F, "%s ", get_op_name(get_irn_op(block)));
543         PRINT_NODEID(block);
544         fprintf(F, " freq: %9.4lf", get_region_exec_freq(block));
545         fprintf(F, " n_outs: %d", get_region_n_outs(block));
546         fprintf(F, " n_exc_outs: %d", get_region_n_exc_outs(block));
547         fprintf(F, "\" ");
548         fprintf(F, "info1:\"");
549         if (dump_dominator_information_flag)
550                 fprintf(F, "dom depth %d\n", get_Block_dom_depth(block));
551
552         /* show arity and possible Bad predecessors of the block */
553         fprintf(F, "arity: %d\n", get_Block_n_cfgpreds(block));
554         for (fl = i = 0; i < get_Block_n_cfgpreds(block); ++i) {
555                 ir_node *pred = get_Block_cfgpred(block, i);
556                 if (is_Bad(pred)) {
557                         if (! fl)
558                                 fprintf(F, "Bad pred at pos: ");
559                         fprintf(F, "%d ", i);
560                         fl = 1;
561                 }
562         }
563         if (fl)
564                 fprintf(F, "\n");
565
566         fprintf(F, "\"");  /* closing quote of info */
567
568         if ((block == get_irg_start_block(get_irn_irg(block))) ||
569                 (block == get_irg_end_block(get_irn_irg(block)))     )
570                 fprintf(F, " color:blue ");
571         else if (fl)
572                 fprintf(F, " color:yellow ");
573
574         fprintf(F, "}\n");
575 }
576
577 static void dump_interval_loop(FILE *F, ir_loop *l)
578 {
579         int i, n_elems = get_loop_n_elements(l);
580
581         fprintf(F, "graph: { title: \"");
582         PRINT_LOOPID(l);
583         fprintf(F, "\" label: \"loop %d", get_loop_loop_nr(l));
584         fprintf(F, " freq: %9.4lf", get_region_exec_freq(l));
585         fprintf(F, " n_outs: %d", get_region_n_outs(l));
586         fprintf(F, " n_exc_outs: %d", get_region_n_exc_outs(l));
587         fprintf(F, "\" status:clustered color:white \n");
588
589         for (i = 0; i < n_elems; ++i) {
590                 loop_element e = get_loop_element(l, i);
591                 dump_region_edges(F, e.node);
592                 switch (*e.kind) {
593                 case k_ir_node:
594                         dump_interval_block(F, e.node);
595                         break;
596                 case k_ir_loop:
597                         dump_interval_loop(F, e.son);
598                         break;
599                 default:
600                         break;
601                 }
602         }
603
604         fprintf(F, "}\n\n");
605 }
606
607
608 void dump_interval_graph(ir_graph *irg, const char *suffix)
609 {
610         FILE     *f;
611         ir_graph *rem;
612
613         if (!is_filtered_dump_name(get_entity_ident(get_irg_entity(irg))))
614                 return;
615
616         f = vcg_open(irg, suffix, "-intervals");
617         dump_vcg_header(f, get_irg_dump_name(irg), NULL, NULL);
618
619         rem              = current_ir_graph;
620         current_ir_graph = irg;
621
622         dump_interval_loop(f, get_irg_loop(irg));
623
624         dump_vcg_footer(f);
625         fclose(f);
626
627         current_ir_graph = rem;
628 }