10 #include "interval_analysis.h"
11 #include "execution_frequency.h"
12 #include "firm_common_t.h"
23 /*------------------------------------------------------------------*/
24 /* A new in array via a hashmap. */
25 /* The in array refers to the loop the block is contained in if the */
26 /* block is not in blocks loop. */
27 /*------------------------------------------------------------------*/
37 static set *region_attr_set = NULL;
39 int region_attr_cmp(const void *e1, const void *e2, size_t size) {
40 region_attr *ra1 = (region_attr *)e1;
41 region_attr *ra2 = (region_attr *)e2;
42 return (ra1->reg != ra2->reg);
45 static INLINE int attr_set_hash(region_attr *a) {
46 return HASH_PTR(a->reg);
49 static INLINE region_attr *get_region_attr(void *region) {
50 region_attr r_attr, *res;
53 res = set_find(region_attr_set, &r_attr, sizeof(r_attr), attr_set_hash(&r_attr));
56 r_attr.in_array = NEW_ARR_F(void *, 0);
57 if (is_ir_loop(region))
58 r_attr.op_array = NEW_ARR_F(void *, 0);
60 r_attr.op_array = NULL;
62 r_attr.n_exc_outs = 0;
63 res = set_insert(region_attr_set, &r_attr, sizeof(r_attr), attr_set_hash(&r_attr));
69 int get_region_n_ins(void *region) {
70 return ARR_LEN(get_region_attr(region)->in_array);
73 void *get_region_in(void *region, int pos) {
74 assert(0 <= pos && pos < get_region_n_ins(region));
75 return ((get_region_attr(region)->in_array)[pos]);
78 void add_region_in (void *region, void *in) {
79 ARR_APP1(void *, get_region_attr(region)->in_array, in);
80 get_region_attr(in)->n_outs++;
83 int get_region_n_outs(void *region) {
84 return get_region_attr(region)->n_outs;
87 int get_region_n_exc_outs(void *region) {
88 return get_region_attr(region)->n_exc_outs;
91 void inc_region_n_exc_outs(void *region) {
92 (get_region_attr(region)->n_exc_outs)++;
95 void *get_loop_cfop(void *region, int pos) {
96 assert(0 <= pos && pos < get_region_n_ins(region));
97 return ((get_region_attr(region)->op_array)[pos]);
100 void add_loop_cfop (void *region, void *cfop) {
102 ARR_APP1(void *, get_region_attr(region)->op_array, cfop);
105 static INLINE void exc_outs(void *reg, ir_node *cfop) {
106 if (is_fragile_op(cfop) || (is_fragile_Proj(cfop)))
107 inc_region_n_exc_outs(reg);
110 /*------------------------------------------------------------------*/
111 /* Algorithm to construct the interval edges based on a loop tree. */
112 /* Walk a loop and add all edges. Walk inner loops by recursion. */
113 /*------------------------------------------------------------------*/
115 /* return non-zero if outer can be reached from inner via the outer loop relation */
116 static int find_outer_loop(ir_loop *inner, ir_loop *outer, ir_node *b, ir_node *cfop) {
117 if (get_loop_outer_loop(inner) == outer) {
118 add_region_in(inner, b);
119 add_loop_cfop(inner, cfop);
126 static int test_loop_nest(ir_node *pred_b, ir_loop *nest) {
127 int i, n_elems = get_loop_n_elements(nest);
129 for (i = 0; (i < n_elems); ++i) {
130 loop_element e = get_loop_element(nest, i);
133 if (e.node == pred_b) return 1;
136 if (test_loop_nest(pred_b, e.son)) return 1;
144 static int find_inner_loop(ir_node *b, ir_loop *l, ir_node *pred, ir_node *cfop) {
145 int i, n_elems = get_loop_n_elements(l);
148 for (i = 0; (i < n_elems) && !found; ++i) {
149 loop_element e = get_loop_element(l, i);
152 if (e.node == b) return 0;
155 found = test_loop_nest(pred, e.son);
157 add_region_in(b, e.son);
158 exc_outs(e.son, cfop);
159 //if (is_fragile_op(cfop)) inc_region_n_exc_outs(b);
170 static int find_previous_loop(ir_loop *l, ir_loop *pred_l, ir_node *b, ir_node *pred_b, ir_node *cfop) {
171 ir_loop *outer = get_loop_outer_loop(l);
173 int l_pos = get_loop_element_pos(outer, l);
175 assert(l_pos > 0 && "Is this a necessary condition? There could be a perfect nest ...");
177 for (i = l_pos -1, found = 0; i > -1 && !found; --i) {
178 ir_loop *k = get_loop_element(outer, i).son;
180 found = test_loop_nest(pred_b, k);
183 //if (is_fragile_op(cfop)) inc_region_n_exc_outs(k);
185 add_loop_cfop(l, cfop);
186 add_region_in(b, NULL);
192 DDMG(current_ir_graph);
204 * Compute the edges for the interval graph.
206 * @param b The block for which to construct the edges.
207 * @param l The loop of b.
209 * There are four cases:
210 * - The pred block is in the same loop. Add a normal block-block edge.
211 * - The pred block is in a loop contained in this loop, somewhere down in
212 * the nesting. The predecessor of this block is the outermost loop of the nest
213 * directly contained in l.
214 * - The pred block is in the outer loop of l. l gets an edge to the pred block.
215 * - The outer loop of l contains another loop k just before l. The control flow
216 * branches directly from loop k to loop l. Add an edge l->k. Watch it: k must
217 * not be a direct predecessor of l in the loop tree!
219 static void construct_interval_block(ir_node *b, ir_loop *l) {
220 int i, n_cfgpreds = get_Block_n_cfgpreds(b);
222 if (b == get_irg_start_block(current_ir_graph)) return;
223 /* We want nice blocks. */
224 assert(n_cfgpreds > 0);
226 for (i = 0; i < n_cfgpreds; ++i) {
227 ir_node *cfop, *pred;
230 if (is_backedge(b, i)) {
231 if (b != get_loop_element(l, 0).node) {
232 if (get_firm_verbosity()) {
233 printf("Loophead not at loop position 0. "); DDMN(b);
236 /* There are no backedges in the interval decomposition. */
237 add_region_in(b, NULL);
241 cfop = get_Block_cfgpred(b, i);
243 if (get_irn_op(get_Proj_pred(cfop)) != op_Cond) {
244 cfop = skip_Proj(cfop);
246 assert(get_nodes_block(cfop) == get_nodes_block(skip_Proj(cfop)));
250 pred = skip_Proj(get_nodes_block(cfop));
251 /* We want nice blocks. */
252 assert( get_irn_op(pred) != op_Bad
253 && get_irn_op(skip_Proj(get_Block_cfgpred(b, i))) != op_Bad);
254 pred_l = get_irn_loop(pred);
256 add_region_in(b, pred);
257 //if (is_fragile_op(cfop)) inc_region_n_exc_outs(b);
258 exc_outs(pred, cfop);
260 int found = find_inner_loop(b, l, pred, cfop);
262 if (b != get_loop_element(l, 0).node) {
263 if (get_firm_verbosity()) {
264 printf("Loop entry not at loop position 0. "); DDMN(b);
267 found = find_outer_loop(l, pred_l, pred, cfop);
268 if (found) add_region_in(b, NULL); /* placeholder */
271 found = find_previous_loop(l, pred_l, b, pred, cfop);
274 DDMG(current_ir_graph);
277 assert(is_backedge(b, i));
278 assert(found && "backedge from inner loop");
282 if (b != get_loop_element(l, 0).node) {
283 /* Check for improper region */
284 if (has_backedges(b)) {
285 printf("Improper Region!!!!!!\n");
286 DDMG(current_ir_graph);
294 static void construct_interval_edges(ir_loop *l) {
295 int i, n_elems = get_loop_n_elements(l);
296 for (i = 0; i < n_elems; ++i) {
297 loop_element e = get_loop_element(l, i);
300 construct_interval_block(e.node, l);
303 construct_interval_edges(e.son);
310 void construct_intervals(ir_graph *irg) {
312 ir_graph *rem = current_ir_graph;
313 current_ir_graph = irg;
315 if (!region_attr_set)
316 region_attr_set = new_set(region_attr_cmp, 256);
318 construct_cf_backedges(current_ir_graph);
320 l = get_irg_loop(current_ir_graph);
322 construct_interval_edges(l);
324 current_ir_graph = rem;
327 void free_intervals(void) {
329 if (!region_attr_set) return;
331 for (ins = (void **)pmap_first(region_in_map);
333 ins = (void **)pmap_next(region_in_map)) {
337 del_set(region_attr_set);
338 region_attr_set = NULL;
341 /*------------------------------------------------------------------*/
342 /* A vcg dumper showing an interval decomposition of a cfg. */
344 /*------------------------------------------------------------------*/
346 void dump_region_edges(FILE *F, void *reg) {
347 int i, n_ins = get_region_n_ins(reg);
349 if (is_ir_node(reg) && get_Block_n_cfgpreds((ir_node *)reg) > get_region_n_ins(reg)) {
350 for (i = n_ins; i < get_Block_n_cfgpreds((ir_node *)reg); ++i) {
351 if (is_backedge((ir_node *)reg, i))
352 fprintf (F, "backedge: { sourcename: \"");
354 fprintf (F, "edge: { sourcename: \"");
355 PRINT_NODEID(((ir_node *)reg));
356 fprintf (F, "\" targetname: \"");
357 PRINT_NODEID(get_nodes_block(skip_Proj(get_Block_cfgpred((ir_node *)reg, i))));
358 fprintf (F, "\" " BLOCK_EDGE_ATTR "}\n");
362 for (i = 0; i < n_ins; ++i) {
363 void *target = get_region_in(reg, i);
365 if (is_ir_node(reg)) {
366 if (get_Block_n_cfgpreds((ir_node *)reg) != get_region_n_ins(reg)) {
367 printf("n_cfgpreds = %d, n_ins = %d\n", get_Block_n_cfgpreds((ir_node *)reg), get_region_n_ins(reg));
368 DDMN((ir_node *)reg);
372 if ((!target || (is_ir_node(reg) && !is_ir_node(target))) && i < get_Block_n_cfgpreds((ir_node *)reg)) {
373 assert(is_ir_node(reg));
374 if (is_backedge((ir_node *)reg, i))
375 fprintf (F, "backedge: { sourcename: \"");
377 fprintf (F, "edge: { sourcename: \"");
378 PRINT_NODEID(((ir_node *)reg));
379 fprintf (F, "\" targetname: \"");
380 PRINT_NODEID(get_nodes_block(skip_Proj(get_Block_cfgpred((ir_node *)reg, i))));
381 fprintf (F, "\" " BLOCK_EDGE_ATTR "}\n");
383 if (!target) continue;
386 fprintf (F, "edge: { sourcename: \"");
387 if (is_ir_node(reg)) {
388 PRINT_NODEID(((ir_node *)reg));
390 PRINT_LOOPID(((ir_loop *)reg));
392 fprintf (F, "\" targetname: \"");
393 if (is_ir_node(target)) {
394 PRINT_NODEID(((ir_node *)target));
396 PRINT_LOOPID(((ir_loop *)target));
399 if (is_ir_node(reg) && is_fragile_op(skip_Proj(get_Block_cfgpred(reg, i))))
400 fprintf(F, EXC_CF_EDGE_ATTR);
405 #include "execution_frequency.h"
407 void dump_interval_block(FILE *F, ir_node *block) {
409 /* This is a block. Dump a node for the block. */
410 fprintf (F, "node: {title: \""); PRINT_NODEID(block);
411 fprintf (F, "\" label: \"");
412 if (block == get_irg_start_block(get_irn_irg(block)))
413 fprintf(F, "Start ");
414 if (block == get_irg_end_block(get_irn_irg(block)))
417 fprintf (F, "%s ", get_op_name(get_irn_op(block)));
419 fprintf(F, " freq: %9.4lf", get_region_exec_freq(block));
420 fprintf(F, " n_outs: %d", get_region_n_outs(block));
421 fprintf(F, " n_exc_outs: %d", get_region_n_exc_outs(block));
423 fprintf(F, "info1:\"");
424 if (dump_dominator_information_flag)
425 fprintf(F, "dom depth %d\n", get_Block_dom_depth(block));
427 /* show arity and possible Bad predecessors of the block */
428 fprintf(F, "arity: %d\n", get_Block_n_cfgpreds(block));
429 for (fl = i = 0; i < get_Block_n_cfgpreds(block); ++i) {
430 ir_node *pred = get_Block_cfgpred(block, i);
433 fprintf(F, "Bad pred at pos: ");
434 fprintf(F, "%d ", i);
441 fprintf (F, "\""); /* closing quote of info */
443 if ((block == get_irg_start_block(get_irn_irg(block))) ||
444 (block == get_irg_end_block(get_irn_irg(block))) )
445 fprintf(F, " color:blue ");
447 fprintf(F, " color:yellow ");
452 void dump_interval_loop(FILE *F, ir_loop *l) {
453 int i, n_elems = get_loop_n_elements(l);
455 fprintf(F, "graph: { title: \"");
457 fprintf(F, "\" label: \"loop %d", get_loop_loop_nr(l));
458 fprintf(F, " freq: %9.4lf", get_region_exec_freq(l));
459 fprintf(F, " n_outs: %d", get_region_n_outs(l));
460 fprintf(F, " n_exc_outs: %d", get_region_n_exc_outs(l));
461 fprintf(F, "\" status:clustered color:white \n");
463 for (i = 0; i < n_elems; ++i) {
464 loop_element e = get_loop_element(l, i);
465 dump_region_edges(F, e.node);
468 dump_interval_block(F, e.node);
471 dump_interval_loop(F, e.son);
481 void dump_interval_graph(ir_graph *irg, const char *suffix) {
484 if (!is_filtered_dump_name(get_entity_ident(get_irg_entity(irg))))
487 f = vcg_open(irg, suffix, "-intervals");
488 dump_vcg_header(f, get_irg_dump_name(irg), NULL);
490 current_ir_graph = irg;
492 dump_interval_loop(f, get_irg_loop(current_ir_graph));