2 * Copyright (C) 1995-2008 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 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 int 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 int 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 int l_pos = get_loop_element_pos(outer, l);
282 assert(l_pos > 0 && "Is this a necessary condition? There could be a perfect nest ...");
284 for (i = l_pos -1, found = 0; i > -1 && !found; --i) {
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);
303 * Compute the edges for the interval graph.
305 * @param blk The block for which to construct the edges.
306 * @param l The loop of blk.
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!
318 static void construct_interval_block(ir_node *blk, ir_loop *l)
322 if (blk == get_irg_start_block(current_ir_graph))
325 n_cfgpreds = get_Block_n_cfgpreds(blk);
326 /* We want nice blocks. */
327 assert(n_cfgpreds > 0);
329 for (i = 0; i < n_cfgpreds; ++i) {
330 ir_node *cfop, *pred;
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));
337 /* There are no backedges in the interval decomposition. */
338 add_region_in(blk, NULL);
342 cfop = get_Block_cfgpred(blk, i);
344 ir_node *op = skip_Proj(cfop);
345 if (is_fragile_op(op) && get_Proj_proj(cfop) == pn_Generic_X_except) {
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.
354 assert(get_nodes_block(cfop) == get_nodes_block(skip_Proj(cfop)));
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);
363 /* first case: both blocks are in the same loop */
364 add_region_in(blk, pred);
365 exc_outs(pred, cfop);
367 /* check for the second case: pred is from an inner loop */
368 int found = find_inner_loop(blk, l, pred, cfop);
370 if (blk != get_loop_element(l, 0).node) {
371 DB((dbg, LEVEL_1, "Loop entry not at loop position 0. %+F\n", blk));
373 /* check for the third case: pred is in an outer loop */
374 found = find_outer_loop(l, pred_l, pred, cfop);
376 /* placeholder: the edge is added to the loop region */
377 add_region_in(blk, NULL);
379 /* fourth case: pred is from the previous loop */
380 found = find_previous_loop(l, pred_l, blk, pred, cfop);
382 assert(found && "decomposition failed");
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));
399 * Construct interval edges for a given (control flow) loop.
401 * @param l the cf loop
403 static void construct_interval_edges(ir_loop *l)
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);
410 construct_interval_block(e.node, l);
413 construct_interval_edges(e.son);
421 void construct_intervals(ir_graph *irg)
424 ir_graph *rem = current_ir_graph;
426 current_ir_graph = irg;
428 FIRM_DBG_REGISTER(dbg, "firm.ana.interval");
430 if (region_attr_set == NULL)
431 region_attr_set = new_set(region_attr_cmp, 256);
433 construct_cf_backedges(irg);
435 l = get_irg_loop(irg);
437 construct_interval_edges(l);
439 current_ir_graph = rem;
442 void free_intervals(void)
446 if (region_attr_set == NULL)
449 for (res = (region_attr*) set_first(region_attr_set);
451 res = (region_attr*) 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);
457 del_set(region_attr_set);
458 region_attr_set = NULL;
461 /*------------------------------------------------------------------*/
462 /* A vcg dumper showing an interval decomposition of a cfg. */
464 /*------------------------------------------------------------------*/
466 static void dump_region_edges(FILE *F, void *reg)
468 int i, n_ins = get_region_n_ins(reg);
470 if (is_ir_node(reg)) {
471 ir_node *irn = (ir_node*) 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: \"");
477 fprintf(F, "edge: { sourcename: \"");
479 fprintf(F, "\" targetname: \"");
480 PRINT_NODEID(get_nodes_block(skip_Proj(get_Block_cfgpred(irn, i))));
481 fprintf(F, "\" " BLOCK_EDGE_ATTR "}\n");
486 for (i = 0; i < n_ins; ++i) {
487 void *target = get_region_in(reg, i);
489 if (is_ir_node(reg)) {
490 ir_node *irn = (ir_node*) 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);
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: \"");
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");
507 if (!target) continue;
510 fprintf(F, "edge: { sourcename: \"");
511 if (is_ir_node(reg)) {
512 PRINT_NODEID(((ir_node *)reg));
514 PRINT_LOOPID(((ir_loop *)reg));
516 fprintf(F, "\" targetname: \"");
517 if (is_ir_node(target)) {
518 PRINT_NODEID(((ir_node *)target));
520 PRINT_LOOPID(((ir_loop *)target));
523 if (is_ir_node(reg) && is_fragile_op(skip_Proj(get_Block_cfgpred((ir_node*) reg, i))))
524 fprintf(F, EXC_CF_EDGE_ATTR);
529 #include "execution_frequency.h"
531 static void dump_interval_block(FILE *F, ir_node *block)
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)))
542 fprintf(F, "%s ", get_op_name(get_irn_op(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));
548 fprintf(F, "info1:\"");
549 if (ir_get_dump_flags() & ir_dump_flag_dominance)
550 fprintf(F, "dom depth %d\n", get_Block_dom_depth(block));
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);
558 fprintf(F, "Bad pred at pos: ");
559 fprintf(F, "%d ", i);
566 fprintf(F, "\""); /* closing quote of info */
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 ");
572 fprintf(F, " color:yellow ");
577 static void dump_interval_loop(FILE *F, ir_loop *l)
579 int i, n_elems = get_loop_n_elements(l);
581 fprintf(F, "graph: { title: \"");
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");
589 for (i = 0; i < n_elems; ++i) {
590 loop_element e = get_loop_element(l, i);
591 dump_region_edges(F, e.node);
594 dump_interval_block(F, e.node);
597 dump_interval_loop(F, e.son);
607 void dump_interval_graph(FILE *out, ir_graph *irg)
609 ir_graph *rem = current_ir_graph;
610 current_ir_graph = irg;
612 dump_vcg_header(out, get_irg_dump_name(irg), NULL, NULL);
613 dump_interval_loop(out, get_irg_loop(irg));
614 dump_vcg_footer(out);
616 current_ir_graph = rem;