2 * Copyright (C) 1995-2011 University of Karlsruhe. All right reserved.
4 * This file is part of libFirm.
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.
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.
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
22 * @brief Implementation of interval analysis
30 #include "interval_analysis.h"
31 #include "execution_frequency.h"
43 DEBUG_ONLY(static firm_dbg_module_t *dbg);
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 /*------------------------------------------------------------------*/
51 /** The attributes of a region. */
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. */
60 /** A Hashset containing the region attributes. */
61 static set *region_attr_set = NULL;
64 * Compare two region attributes for identical regions.
66 static int region_attr_cmp(const void *e1, const void *e2, size_t size)
68 region_attr *ra1 = (region_attr *)e1;
69 region_attr *ra2 = (region_attr *)e2;
71 return (ra1->reg != ra2->reg);
74 /** Hash a region attribute (the region only). */
75 static inline int attr_set_hash(region_attr *a)
77 return HASH_PTR(a->reg);
81 * Return the region attribute for a given region.
82 * Allocate one if not exists.
84 * @param region the region
86 static inline region_attr *get_region_attr(void *region)
88 region_attr r_attr, *res;
91 res = (region_attr*) set_find(region_attr_set, &r_attr, sizeof(r_attr), attr_set_hash(&r_attr));
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);
98 r_attr.op_array = NULL;
100 r_attr.n_exc_outs = 0;
101 res = (region_attr*) set_insert(region_attr_set, &r_attr, sizeof(r_attr), attr_set_hash(&r_attr));
107 int get_region_n_ins(void *region)
109 return (int)ARR_LEN(get_region_attr(region)->in_array);
112 void *get_region_in(void *region, int pos)
114 assert(0 <= pos && pos < get_region_n_ins(region));
115 return ((get_region_attr(region)->in_array)[pos]);
118 void add_region_in(void *region, void *in)
120 ARR_APP1(void *, get_region_attr(region)->in_array, in);
121 get_region_attr(in)->n_outs++;
124 int get_region_n_outs(void *region)
126 return get_region_attr(region)->n_outs;
129 int get_region_n_exc_outs(void *region)
131 return get_region_attr(region)->n_exc_outs;
134 static void inc_region_n_exc_outs(void *region)
136 (get_region_attr(region)->n_exc_outs)++;
139 void *get_loop_cfop(void *region, int pos)
141 assert(0 <= pos && pos < get_region_n_ins(region));
142 return ((get_region_attr(region)->op_array)[pos]);
145 /** Add a control flow op to a loop region. */
146 static inline void add_loop_cfop(void *region, void *cfop)
149 ARR_APP1(void *, get_region_attr(region)->op_array, cfop);
153 * Increase the number of exception outputs if a control flow
154 * operation (that is inside the given region) is a fragile operation.
156 * @param reg a region
157 * @param cfop a control flow operation leaving this region
159 static inline void exc_outs(void *reg, ir_node *cfop)
161 if (is_fragile_op(cfop) || is_fragile_Proj(cfop))
162 inc_region_n_exc_outs(reg);
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 /*------------------------------------------------------------------*/
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.
174 * @param inner the current (possible inner) loop
175 * @param outer the loop of blk
177 * @param cfop the control flow op leaving blk
179 * @return non-zero if outer can be reached from inner via the outer loop relation
181 static int find_outer_loop(ir_loop *inner, ir_loop *outer, ir_node *blk, ir_node *cfop)
183 if (get_loop_outer_loop(inner) == outer) {
184 add_region_in(inner, blk);
185 add_loop_cfop(inner, cfop);
193 * Check if a given block can be found in a given loop
194 * or its nesting loops.
199 static int test_loop_nest(ir_node *blk, ir_loop *loop)
201 size_t i, n_elems = get_loop_n_elements(loop);
203 for (i = 0; i < n_elems; ++i) {
204 loop_element e = get_loop_element(loop, i);
211 if (test_loop_nest(blk, e.son))
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.
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
230 * @return non-zero if pred is from an inner loop
232 static int find_inner_loop(ir_node *blk, ir_loop *l, ir_node *pred, ir_node *cfop)
234 size_t i, n_elems = get_loop_n_elements(l);
237 for (i = 0; i < n_elems; ++i) {
238 loop_element e = get_loop_element(l, i);
242 /* stop the search if we reach blk, pred cannot be found
248 found = test_loop_nest(pred, e.son);
250 add_region_in(blk, e.son);
251 exc_outs(e.son, cfop);
263 * Check if a predecessor block pred_b is from a previous loop of the
266 * @param l the loop of the block b
267 * @param pred_l the loop of the block pred_b
269 * @param pred_b the predecessor block
270 * @param cfop the control flow node leaving pred_b for b
272 * @return non-zero if pred is from an previous loop
274 static int find_previous_loop(ir_loop *l, ir_loop *pred_l, ir_node *b,
275 ir_node *pred_b, ir_node *cfop)
277 ir_loop *outer = get_loop_outer_loop(l);
279 size_t i, l_pos = get_loop_element_pos(outer, l);
281 assert(l_pos != INVALID_LOOP_POS);
282 assert(l_pos > 0 && "Is this a necessary condition? There could be a perfect nest ...");
284 for (i = l_pos, found = 0; i > 0;) {
285 ir_loop *k = get_loop_element(outer, --i).son;
287 found = test_loop_nest(pred_b, k);
291 add_loop_cfop(l, cfop);
292 /* placeholder: the edge is in the loop region */
293 add_region_in(b, NULL);
304 * Compute the edges for the interval graph.
306 * @param blk The block for which to construct the edges.
307 * @param l The loop of blk.
309 * There are four cases:
310 * - A predecessor block is in the same loop. Add a normal block-block edge.
311 * - A predecessor block is in a loop contained in this loop, somewhere down in
312 * the nesting. The predecessor of this block is the outermost loop of the nest
313 * directly contained in l.
314 * - A predecessor block is in the outer loop of l. l gets an edge to the predecessor block.
315 * - The outer loop of l contains another loop k just before l. The control flow
316 * branches directly from loop k to loop l. Add an edge l->k. Watch it: k must
317 * not be a direct predecessor of l in the loop tree!
319 static void construct_interval_block(ir_node *blk, ir_loop *l)
323 if (blk == get_irg_start_block(current_ir_graph))
326 n_cfgpreds = get_Block_n_cfgpreds(blk);
327 /* We want nice blocks. */
328 assert(n_cfgpreds > 0);
330 for (i = 0; i < n_cfgpreds; ++i) {
331 ir_node *cfop, *pred;
334 if (is_backedge(blk, i)) {
335 if (blk != get_loop_element(l, 0).node) {
336 DB((dbg, LEVEL_1, "Loophead not at loop position 0. %+F\n", blk));
338 /* There are no backedges in the interval decomposition. */
339 add_region_in(blk, NULL);
343 cfop = get_Block_cfgpred(blk, i);
345 ir_node *op = skip_Proj(cfop);
346 if (is_fragile_op(op) && get_Proj_proj(cfop) == pn_Generic_X_except) {
348 * Skip the Proj for the exception flow only, leave the
349 * not exception flow Proj's intact.
350 * If the old semantic is used (only one exception Proj) this
351 * should lead to the same representation as before.
355 assert(get_nodes_block(cfop) == get_nodes_block(skip_Proj(cfop)));
359 pred = skip_Proj(get_nodes_block(cfop));
360 /* We want nice blocks. */
361 assert(!is_Bad(pred) && !is_Bad(skip_Proj(get_Block_cfgpred(blk, i))));
362 pred_l = get_irn_loop(pred);
364 /* first case: both blocks are in the same loop */
365 add_region_in(blk, pred);
366 exc_outs(pred, cfop);
368 /* check for the second case: pred is from an inner loop */
369 int found = find_inner_loop(blk, l, pred, cfop);
371 if (blk != get_loop_element(l, 0).node) {
372 DB((dbg, LEVEL_1, "Loop entry not at loop position 0. %+F\n", blk));
374 /* check for the third case: pred is in an outer loop */
375 found = find_outer_loop(l, pred_l, pred, cfop);
377 /* placeholder: the edge is added to the loop region */
378 add_region_in(blk, NULL);
380 /* fourth case: pred is from the previous loop */
381 found = find_previous_loop(l, pred_l, blk, pred, cfop);
383 assert(found && "decomposition failed");
389 if (blk != get_loop_element(l, 0).node) {
390 /* Check for improper region. But these can happen, so what? */
391 if (has_backedges(blk)) {
392 DB((dbg, LEVEL_1, "Improper Region %+F\n", blk));
400 * Construct interval edges for a given (control flow) loop.
402 * @param l the cf loop
404 static void construct_interval_edges(ir_loop *l)
406 size_t i, n_elems = get_loop_n_elements(l);
407 for (i = 0; i < n_elems; ++i) {
408 loop_element e = get_loop_element(l, i);
411 construct_interval_block(e.node, l);
414 construct_interval_edges(e.son);
422 void construct_intervals(ir_graph *irg)
425 ir_graph *rem = current_ir_graph;
427 current_ir_graph = irg;
429 FIRM_DBG_REGISTER(dbg, "firm.ana.interval");
431 if (region_attr_set == NULL)
432 region_attr_set = new_set(region_attr_cmp, 256);
434 construct_cf_backedges(irg);
436 l = get_irg_loop(irg);
438 construct_interval_edges(l);
440 current_ir_graph = rem;
443 void free_intervals(void)
447 if (region_attr_set == NULL)
450 for (res = (region_attr*) set_first(region_attr_set);
452 res = (region_attr*) set_next(region_attr_set)) {
453 DEL_ARR_F(res->in_array);
454 if (res->op_array != NULL)
455 DEL_ARR_F(res->op_array);
458 del_set(region_attr_set);
459 region_attr_set = NULL;
462 /*------------------------------------------------------------------*/
463 /* A vcg dumper showing an interval decomposition of a cfg. */
465 /*------------------------------------------------------------------*/
467 static void dump_region_edges(FILE *F, void *reg)
469 int i, n_ins = get_region_n_ins(reg);
471 if (is_ir_node(reg)) {
472 ir_node *irn = (ir_node*) reg;
473 if (get_Block_n_cfgpreds(irn) > get_region_n_ins(reg)) {
474 for (i = n_ins; i < get_Block_n_cfgpreds(irn); ++i) {
475 if (is_backedge(irn, i))
476 fprintf(F, "backedge: { sourcename: \"");
478 fprintf(F, "edge: { sourcename: \"");
480 fprintf(F, "\" targetname: \"");
481 PRINT_NODEID(get_nodes_block(skip_Proj(get_Block_cfgpred(irn, i))));
482 fprintf(F, "\" " BLOCK_EDGE_ATTR "}\n");
487 for (i = 0; i < n_ins; ++i) {
488 void *target = get_region_in(reg, i);
490 if (is_ir_node(reg)) {
491 ir_node *irn = (ir_node*) reg;
492 if (get_Block_n_cfgpreds(irn) != get_region_n_ins(reg)) {
493 ir_printf("n_cfgpreds = %d, n_ins = %d\n %+F\n", get_Block_n_cfgpreds(irn), get_region_n_ins(reg), irn);
497 if ((!target || (is_ir_node(reg) && !is_ir_node(target))) && i < get_Block_n_cfgpreds((ir_node *)reg)) {
498 assert(is_ir_node(reg));
499 if (is_backedge((ir_node *)reg, i))
500 fprintf(F, "backedge: { sourcename: \"");
502 fprintf(F, "edge: { sourcename: \"");
503 PRINT_NODEID(((ir_node *)reg));
504 fprintf(F, "\" targetname: \"");
505 PRINT_NODEID(get_nodes_block(skip_Proj(get_Block_cfgpred((ir_node *)reg, i))));
506 fprintf(F, "\" " BLOCK_EDGE_ATTR "}\n");
508 if (!target) continue;
511 fprintf(F, "edge: { sourcename: \"");
512 if (is_ir_node(reg)) {
513 PRINT_NODEID(((ir_node *)reg));
515 PRINT_LOOPID(((ir_loop *)reg));
517 fprintf(F, "\" targetname: \"");
518 if (is_ir_node(target)) {
519 PRINT_NODEID(((ir_node *)target));
521 PRINT_LOOPID(((ir_loop *)target));
524 if (is_ir_node(reg) && is_fragile_op(skip_Proj(get_Block_cfgpred((ir_node*) reg, i))))
525 fprintf(F, EXC_CF_EDGE_ATTR);
530 #include "execution_frequency.h"
532 static void dump_interval_block(FILE *F, ir_node *block)
535 /* This is a block. Dump a node for the block. */
536 fprintf(F, "node: {title: \""); PRINT_NODEID(block);
537 fprintf(F, "\" label: \"");
538 if (block == get_irg_start_block(get_irn_irg(block)))
539 fprintf(F, "Start ");
540 if (block == get_irg_end_block(get_irn_irg(block)))
543 fprintf(F, "%s ", get_op_name(get_irn_op(block)));
545 fprintf(F, " freq: %9.4lf", get_region_exec_freq(block));
546 fprintf(F, " n_outs: %d", get_region_n_outs(block));
547 fprintf(F, " n_exc_outs: %d", get_region_n_exc_outs(block));
549 fprintf(F, "info1:\"");
550 if (ir_get_dump_flags() & ir_dump_flag_dominance)
551 fprintf(F, "dom depth %d\n", get_Block_dom_depth(block));
553 /* show arity and possible Bad predecessors of the block */
554 fprintf(F, "arity: %d\n", get_Block_n_cfgpreds(block));
555 for (fl = i = 0; i < get_Block_n_cfgpreds(block); ++i) {
556 ir_node *pred = get_Block_cfgpred(block, i);
559 fprintf(F, "Bad pred at pos: ");
560 fprintf(F, "%d ", i);
567 fprintf(F, "\""); /* closing quote of info */
569 if ((block == get_irg_start_block(get_irn_irg(block))) ||
570 (block == get_irg_end_block(get_irn_irg(block))) )
571 fprintf(F, " color:blue ");
573 fprintf(F, " color:yellow ");
578 static void dump_interval_loop(FILE *F, ir_loop *l)
580 size_t i, n_elems = get_loop_n_elements(l);
582 fprintf(F, "graph: { title: \"");
584 fprintf(F, "\" label: \"loop %ld", get_loop_loop_nr(l));
585 fprintf(F, " freq: %9.4lf", get_region_exec_freq(l));
586 fprintf(F, " n_outs: %d", get_region_n_outs(l));
587 fprintf(F, " n_exc_outs: %d", get_region_n_exc_outs(l));
588 fprintf(F, "\" status:clustered color:white \n");
590 for (i = 0; i < n_elems; ++i) {
591 loop_element e = get_loop_element(l, i);
592 dump_region_edges(F, e.node);
595 dump_interval_block(F, e.node);
598 dump_interval_loop(F, e.son);
608 void dump_interval_graph(FILE *out, ir_graph *irg)
610 ir_graph *rem = current_ir_graph;
611 current_ir_graph = irg;
613 dump_vcg_header(out, get_irg_dump_name(irg), NULL, NULL);
614 dump_interval_loop(out, get_irg_loop(irg));
615 dump_vcg_footer(out);
617 current_ir_graph = rem;