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) {
67 region_attr *ra1 = (region_attr *)e1;
68 region_attr *ra2 = (region_attr *)e2;
70 return (ra1->reg != ra2->reg);
73 /** Hash a region attribute (the region only). */
74 static inline int attr_set_hash(region_attr *a) {
75 return HASH_PTR(a->reg);
79 * Return the region attribute for a given region.
80 * Allocate one if not exists.
82 * @param region the region
84 static inline region_attr *get_region_attr(void *region) {
85 region_attr r_attr, *res;
88 res = set_find(region_attr_set, &r_attr, sizeof(r_attr), attr_set_hash(&r_attr));
91 r_attr.in_array = NEW_ARR_F(void *, 0);
92 if (is_ir_loop(region))
93 r_attr.op_array = NEW_ARR_F(void *, 0);
95 r_attr.op_array = NULL;
97 r_attr.n_exc_outs = 0;
98 res = set_insert(region_attr_set, &r_attr, sizeof(r_attr), attr_set_hash(&r_attr));
104 int get_region_n_ins(void *region) {
105 return ARR_LEN(get_region_attr(region)->in_array);
108 void *get_region_in(void *region, int pos) {
109 assert(0 <= pos && pos < get_region_n_ins(region));
110 return ((get_region_attr(region)->in_array)[pos]);
113 void add_region_in(void *region, void *in) {
114 ARR_APP1(void *, get_region_attr(region)->in_array, in);
115 get_region_attr(in)->n_outs++;
118 int get_region_n_outs(void *region) {
119 return get_region_attr(region)->n_outs;
122 int get_region_n_exc_outs(void *region) {
123 return get_region_attr(region)->n_exc_outs;
126 void inc_region_n_exc_outs(void *region) {
127 (get_region_attr(region)->n_exc_outs)++;
130 void *get_loop_cfop(void *region, int pos) {
131 assert(0 <= pos && pos < get_region_n_ins(region));
132 return ((get_region_attr(region)->op_array)[pos]);
135 /** Add a control flow op to a loop region. */
136 static inline void add_loop_cfop(void *region, void *cfop) {
138 ARR_APP1(void *, get_region_attr(region)->op_array, cfop);
142 * Increase the number of exception outputs if a control flow
143 * operation (that is inside the given region) is a fragile operation.
145 * @param reg a region
146 * @param cfop a control flow operation leaving this region
148 static inline void exc_outs(void *reg, ir_node *cfop) {
149 if (is_fragile_op(cfop) || is_fragile_Proj(cfop))
150 inc_region_n_exc_outs(reg);
153 /*------------------------------------------------------------------*/
154 /* Algorithm to construct the interval edges based on a loop tree. */
155 /* Walk a loop and add all edges. Walk inner loops by recursion. */
156 /*------------------------------------------------------------------*/
159 * Check if the loop of a given block is the outer loop of the current one.
160 * If yes, add an edge from the block to the region of the current loop.
162 * @param inner the current (possible inner) loop
163 * @param outer the loop of blk
165 * @param cfop the control flow op leaving blk
167 * @return non-zero if outer can be reached from inner via the outer loop relation
169 static int find_outer_loop(ir_loop *inner, ir_loop *outer, ir_node *blk, ir_node *cfop) {
170 if (get_loop_outer_loop(inner) == outer) {
171 add_region_in(inner, blk);
172 add_loop_cfop(inner, cfop);
180 * Check if a given block can be found in a given loop
181 * or its nesting loops.
186 static int test_loop_nest(ir_node *blk, ir_loop *loop) {
187 int i, n_elems = get_loop_n_elements(loop);
189 for (i = 0; i < n_elems; ++i) {
190 loop_element e = get_loop_element(loop, i);
197 if (test_loop_nest(blk, e.son))
208 * Check if pred is a block from an inner loop jumping via cfop to the block blk.
209 * If yes, add an edge from pred's loop to the region blk.
212 * @param l the loop of blk
213 * @param pred a predecessor block of blk
214 * @param cfop the control flow op from pred to blk
216 * @return non-zero if pred is from an inner loop
218 static int find_inner_loop(ir_node *blk, ir_loop *l, ir_node *pred, ir_node *cfop) {
219 int i, n_elems = get_loop_n_elements(l);
222 for (i = 0; i < n_elems; ++i) {
223 loop_element e = get_loop_element(l, i);
227 /* stop the search if we reach blk, pred cannot be found
233 found = test_loop_nest(pred, e.son);
235 add_region_in(blk, e.son);
236 exc_outs(e.son, cfop);
248 * Check if a predecessor block pred_b is from a previous loop of the
251 * @param l the loop of the block b
252 * @param pred_l the loop of the block pred_b
254 * @param pred_b the predecessor block
255 * @param cfop the control flow node leaving pred_b for b
257 * @return non-zero if pred is from an previous loop
259 static int find_previous_loop(ir_loop *l, ir_loop *pred_l, ir_node *b,
260 ir_node *pred_b, ir_node *cfop)
262 ir_loop *outer = get_loop_outer_loop(l);
264 int l_pos = get_loop_element_pos(outer, l);
267 assert(l_pos > 0 && "Is this a necessary condition? There could be a perfect nest ...");
269 for (i = l_pos -1, found = 0; i > -1 && !found; --i) {
270 ir_loop *k = get_loop_element(outer, i).son;
272 found = test_loop_nest(pred_b, k);
276 add_loop_cfop(l, cfop);
277 /* placeholder: the edge is in the loop region */
278 add_region_in(b, NULL);
288 * Compute the edges for the interval graph.
290 * @param blk The block for which to construct the edges.
291 * @param l The loop of blk.
293 * There are four cases:
294 * - A predecessor block is in the same loop. Add a normal block-block edge.
295 * - A predecessor block is in a loop contained in this loop, somewhere down in
296 * the nesting. The predecessor of this block is the outermost loop of the nest
297 * directly contained in l.
298 * - A predecessor block is in the outer loop of l. l gets an edge to the predecessor block.
299 * - The outer loop of l contains another loop k just before l. The control flow
300 * branches directly from loop k to loop l. Add an edge l->k. Watch it: k must
301 * not be a direct predecessor of l in the loop tree!
303 static void construct_interval_block(ir_node *blk, ir_loop *l) {
306 if (blk == get_irg_start_block(current_ir_graph))
309 n_cfgpreds = get_Block_n_cfgpreds(blk);
310 /* We want nice blocks. */
311 assert(n_cfgpreds > 0);
313 for (i = 0; i < n_cfgpreds; ++i) {
314 ir_node *cfop, *pred;
317 if (is_backedge(blk, i)) {
318 if (blk != get_loop_element(l, 0).node) {
319 DB((dbg, LEVEL_1, "Loophead not at loop position 0. %+F\n", blk));
321 /* There are no backedges in the interval decomposition. */
322 add_region_in(blk, NULL);
326 cfop = get_Block_cfgpred(blk, i);
328 ir_node *op = skip_Proj(cfop);
329 if (is_fragile_op(op) && get_Proj_proj(cfop) == pn_Generic_X_except) {
331 * Skip the Proj for the exception flow only, leave the
332 * not exception flow Proj's intact.
333 * If the old semantic is used (only one exception Proj) this
334 * should lead to the same representation as before.
338 assert(get_nodes_block(cfop) == get_nodes_block(skip_Proj(cfop)));
342 pred = skip_Proj(get_nodes_block(cfop));
343 /* We want nice blocks. */
344 assert(!is_Bad(pred) && !is_Bad(skip_Proj(get_Block_cfgpred(blk, i))));
345 pred_l = get_irn_loop(pred);
347 /* first case: both blocks are in the same loop */
348 add_region_in(blk, pred);
349 exc_outs(pred, cfop);
351 /* check for the second case: pred is from an inner loop */
352 int found = find_inner_loop(blk, l, pred, cfop);
354 if (blk != get_loop_element(l, 0).node) {
355 DB((dbg, LEVEL_1, "Loop entry not at loop position 0. %+F\n", blk));
357 /* check for the third case: pred is in an outer loop */
358 found = find_outer_loop(l, pred_l, pred, cfop);
360 /* placeholder: the edge is added to the loop region */
361 add_region_in(blk, NULL);
363 /* fourth case: pred is from the previous loop */
364 found = find_previous_loop(l, pred_l, blk, pred, cfop);
366 assert(found && "decomposition failed");
372 if (blk != get_loop_element(l, 0).node) {
373 /* Check for improper region. But these can happen, so what? */
374 if (has_backedges(blk)) {
375 DB((dbg, LEVEL_1, "Improper Region %+F\n", blk));
383 * Construct interval edges for a given (control flow) loop.
385 * @param l the cf loop
387 static void construct_interval_edges(ir_loop *l) {
388 int i, n_elems = get_loop_n_elements(l);
389 for (i = 0; i < n_elems; ++i) {
390 loop_element e = get_loop_element(l, i);
393 construct_interval_block(e.node, l);
396 construct_interval_edges(e.son);
404 void construct_intervals(ir_graph *irg) {
406 ir_graph *rem = current_ir_graph;
408 current_ir_graph = irg;
410 FIRM_DBG_REGISTER(dbg, "firm.ana.interval");
412 if (region_attr_set == NULL)
413 region_attr_set = new_set(region_attr_cmp, 256);
415 construct_cf_backedges(irg);
417 l = get_irg_loop(irg);
419 construct_interval_edges(l);
421 current_ir_graph = rem;
424 void free_intervals(void) {
427 if (region_attr_set == NULL)
430 for (res = set_first(region_attr_set);
432 res = set_next(region_attr_set)) {
433 DEL_ARR_F(res->in_array);
434 if (res->op_array != NULL)
435 DEL_ARR_F(res->op_array);
438 del_set(region_attr_set);
439 region_attr_set = NULL;
442 /*------------------------------------------------------------------*/
443 /* A vcg dumper showing an interval decomposition of a cfg. */
445 /*------------------------------------------------------------------*/
447 void dump_region_edges(FILE *F, void *reg) {
448 int i, n_ins = get_region_n_ins(reg);
450 if (is_ir_node(reg)) {
452 if (get_Block_n_cfgpreds(irn) > get_region_n_ins(reg)) {
453 for (i = n_ins; i < get_Block_n_cfgpreds(irn); ++i) {
454 if (is_backedge(irn, i))
455 fprintf(F, "backedge: { sourcename: \"");
457 fprintf(F, "edge: { sourcename: \"");
459 fprintf(F, "\" targetname: \"");
460 PRINT_NODEID(get_nodes_block(skip_Proj(get_Block_cfgpred(irn, i))));
461 fprintf(F, "\" " BLOCK_EDGE_ATTR "}\n");
466 for (i = 0; i < n_ins; ++i) {
467 void *target = get_region_in(reg, i);
469 if (is_ir_node(reg)) {
471 if (get_Block_n_cfgpreds(irn) != get_region_n_ins(reg)) {
472 ir_printf("n_cfgpreds = %d, n_ins = %d\n %+F\n", get_Block_n_cfgpreds(irn), get_region_n_ins(reg), irn);
476 if ((!target || (is_ir_node(reg) && !is_ir_node(target))) && i < get_Block_n_cfgpreds((ir_node *)reg)) {
477 assert(is_ir_node(reg));
478 if (is_backedge((ir_node *)reg, i))
479 fprintf(F, "backedge: { sourcename: \"");
481 fprintf(F, "edge: { sourcename: \"");
482 PRINT_NODEID(((ir_node *)reg));
483 fprintf(F, "\" targetname: \"");
484 PRINT_NODEID(get_nodes_block(skip_Proj(get_Block_cfgpred((ir_node *)reg, i))));
485 fprintf(F, "\" " BLOCK_EDGE_ATTR "}\n");
487 if (!target) continue;
490 fprintf(F, "edge: { sourcename: \"");
491 if (is_ir_node(reg)) {
492 PRINT_NODEID(((ir_node *)reg));
494 PRINT_LOOPID(((ir_loop *)reg));
496 fprintf(F, "\" targetname: \"");
497 if (is_ir_node(target)) {
498 PRINT_NODEID(((ir_node *)target));
500 PRINT_LOOPID(((ir_loop *)target));
503 if (is_ir_node(reg) && is_fragile_op(skip_Proj(get_Block_cfgpred(reg, i))))
504 fprintf(F, EXC_CF_EDGE_ATTR);
509 #include "execution_frequency.h"
511 static void dump_interval_block(FILE *F, ir_node *block) {
513 /* This is a block. Dump a node for the block. */
514 fprintf(F, "node: {title: \""); PRINT_NODEID(block);
515 fprintf(F, "\" label: \"");
516 if (block == get_irg_start_block(get_irn_irg(block)))
517 fprintf(F, "Start ");
518 if (block == get_irg_end_block(get_irn_irg(block)))
521 fprintf(F, "%s ", get_op_name(get_irn_op(block)));
523 fprintf(F, " freq: %9.4lf", get_region_exec_freq(block));
524 fprintf(F, " n_outs: %d", get_region_n_outs(block));
525 fprintf(F, " n_exc_outs: %d", get_region_n_exc_outs(block));
527 fprintf(F, "info1:\"");
528 if (dump_dominator_information_flag)
529 fprintf(F, "dom depth %d\n", get_Block_dom_depth(block));
531 /* show arity and possible Bad predecessors of the block */
532 fprintf(F, "arity: %d\n", get_Block_n_cfgpreds(block));
533 for (fl = i = 0; i < get_Block_n_cfgpreds(block); ++i) {
534 ir_node *pred = get_Block_cfgpred(block, i);
537 fprintf(F, "Bad pred at pos: ");
538 fprintf(F, "%d ", i);
545 fprintf(F, "\""); /* closing quote of info */
547 if ((block == get_irg_start_block(get_irn_irg(block))) ||
548 (block == get_irg_end_block(get_irn_irg(block))) )
549 fprintf(F, " color:blue ");
551 fprintf(F, " color:yellow ");
556 static void dump_interval_loop(FILE *F, ir_loop *l) {
557 int i, n_elems = get_loop_n_elements(l);
559 fprintf(F, "graph: { title: \"");
561 fprintf(F, "\" label: \"loop %d", get_loop_loop_nr(l));
562 fprintf(F, " freq: %9.4lf", get_region_exec_freq(l));
563 fprintf(F, " n_outs: %d", get_region_n_outs(l));
564 fprintf(F, " n_exc_outs: %d", get_region_n_exc_outs(l));
565 fprintf(F, "\" status:clustered color:white \n");
567 for (i = 0; i < n_elems; ++i) {
568 loop_element e = get_loop_element(l, i);
569 dump_region_edges(F, e.node);
572 dump_interval_block(F, e.node);
575 dump_interval_loop(F, e.son);
586 void dump_interval_graph(ir_graph *irg, const char *suffix) {
590 if (!is_filtered_dump_name(get_entity_ident(get_irg_entity(irg))))
593 f = vcg_open(irg, suffix, "-intervals");
594 dump_vcg_header(f, get_irg_dump_name(irg), NULL, NULL);
596 rem = current_ir_graph;
597 current_ir_graph = irg;
599 dump_interval_loop(f, get_irg_loop(irg));
604 current_ir_graph = rem;