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 true 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 true;
136 if (test_loop_nest(pred_b, e.son)) return true;
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 false;
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 = false; 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);
203 /* Compute the edges for the interval graph.
205 * @param b The block for which to constuct the edges.
206 * @param l The loop of b.
208 * There are four cases:
209 * - The pred block is in the same loop. Add a normal block-block edge.
210 * - The pred block is in a loop contained in this loop, somewhere down in
211 * the nesting. The predecessor of this block is the outermost loop of the nest
212 * directly contained in l.
213 * - The pred block is in the outer loop of l. l gets an edge to the pred block.
214 * - The outer loop of l contains another loop k just before l. The control flow
215 * branches directly from loop k to loop l. Add an edge l->k. Watch it: k must
216 * not be a direct predecessor of l in the loop tree!
218 static void construct_interval_block(ir_node *b, ir_loop *l) {
219 int i, n_cfgpreds = get_Block_n_cfgpreds(b);
221 if (b == get_irg_start_block(current_ir_graph)) return;
222 /* We want nice blocks. */
223 assert(n_cfgpreds > 0);
225 for (i = 0; i < n_cfgpreds; ++i) {
226 ir_node *cfop, *pred;
229 if (is_backedge(b, i)) {
230 if (b != get_loop_element(l, 0).node) {
231 if (get_firm_verbosity()) {
232 printf("Loophead not at loop position 0. "); DDMN(b);
235 /* There are no backedges in the interval decomposition. */
236 add_region_in(b, NULL);
240 cfop = get_Block_cfgpred(b, i);
242 if (get_irn_op(get_Proj_pred(cfop)) != op_Cond) {
243 cfop = skip_Proj(cfop);
245 assert(get_nodes_block(cfop) == get_nodes_block(skip_Proj(cfop)));
249 pred = skip_Proj(get_nodes_block(cfop));
250 /* We want nice blocks. */
251 assert( get_irn_op(pred) != op_Bad
252 && get_irn_op(skip_Proj(get_Block_cfgpred(b, i))) != op_Bad);
253 pred_l = get_irn_loop(pred);
255 add_region_in(b, pred);
256 //if (is_fragile_op(cfop)) inc_region_n_exc_outs(b);
257 exc_outs(pred, cfop);
259 int found = find_inner_loop(b, l, pred, cfop);
261 if (b != get_loop_element(l, 0).node) {
262 if (get_firm_verbosity()) {
263 printf("Loop entry not at loop position 0. "); DDMN(b);
266 found = find_outer_loop(l, pred_l, pred, cfop);
267 if (found) add_region_in(b, NULL); /* placeholder */
270 found = find_previous_loop(l, pred_l, b, pred, cfop);
273 DDMG(current_ir_graph);
276 assert(is_backedge(b, i));
277 assert(found && "backedge from inner loop");
281 if (b != get_loop_element(l, 0).node) {
282 /* Check for improper region */
283 if (has_backedges(b)) {
284 printf("Improper Region!!!!!!\n");
285 DDMG(current_ir_graph);
293 static void construct_interval_edges(ir_loop *l) {
294 int i, n_elems = get_loop_n_elements(l);
295 for (i = 0; i < n_elems; ++i) {
296 loop_element e = get_loop_element(l, i);
299 construct_interval_block(e.node, l);
302 construct_interval_edges(e.son);
309 void construct_intervals(ir_graph *irg) {
311 ir_graph *rem = current_ir_graph;
312 current_ir_graph = irg;
314 if (!region_attr_set)
315 region_attr_set = new_set(region_attr_cmp, 256);
317 construct_cf_backedges(current_ir_graph);
319 l = get_irg_loop(current_ir_graph);
321 construct_interval_edges(l);
323 current_ir_graph = rem;
326 void free_intervals(void) {
328 if (!region_attr_set) return;
330 for (ins = (void **)pmap_first(region_in_map);
332 ins = (void **)pmap_next(region_in_map)) {
336 del_set(region_attr_set);
337 region_attr_set = NULL;
340 /*------------------------------------------------------------------*/
341 /* A vcg dumper showing an interval decomposition of a cfg. */
343 /*------------------------------------------------------------------*/
345 void dump_region_edges(FILE *F, void *reg) {
346 int i, n_ins = get_region_n_ins(reg);
348 if (is_ir_node(reg) && get_Block_n_cfgpreds((ir_node *)reg) > get_region_n_ins(reg)) {
349 for (i = n_ins; i < get_Block_n_cfgpreds((ir_node *)reg); ++i) {
350 if (is_backedge((ir_node *)reg, i))
351 fprintf (F, "backedge: { sourcename: \"");
353 fprintf (F, "edge: { sourcename: \"");
354 PRINT_NODEID(((ir_node *)reg));
355 fprintf (F, "\" targetname: \"");
356 PRINT_NODEID(get_nodes_block(skip_Proj(get_Block_cfgpred((ir_node *)reg, i))));
357 fprintf (F, "\" " BLOCK_EDGE_ATTR "}\n");
361 for (i = 0; i < n_ins; ++i) {
362 void *target = get_region_in(reg, i);
364 if (is_ir_node(reg)) {
365 if (get_Block_n_cfgpreds((ir_node *)reg) != get_region_n_ins(reg)) {
366 printf("n_cfgpreds = %d, n_ins = %d\n", get_Block_n_cfgpreds((ir_node *)reg), get_region_n_ins(reg));
367 DDMN((ir_node *)reg);
371 if ((!target || (is_ir_node(reg) && !is_ir_node(target))) && i < get_Block_n_cfgpreds((ir_node *)reg)) {
372 assert(is_ir_node(reg));
373 if (is_backedge((ir_node *)reg, i))
374 fprintf (F, "backedge: { sourcename: \"");
376 fprintf (F, "edge: { sourcename: \"");
377 PRINT_NODEID(((ir_node *)reg));
378 fprintf (F, "\" targetname: \"");
379 PRINT_NODEID(get_nodes_block(skip_Proj(get_Block_cfgpred((ir_node *)reg, i))));
380 fprintf (F, "\" " BLOCK_EDGE_ATTR "}\n");
382 if (!target) continue;
385 fprintf (F, "edge: { sourcename: \"");
386 if (is_ir_node(reg)) {
387 PRINT_NODEID(((ir_node *)reg));
389 PRINT_LOOPID(((ir_loop *)reg));
391 fprintf (F, "\" targetname: \"");
392 if (is_ir_node(target)) {
393 PRINT_NODEID(((ir_node *)target));
395 PRINT_LOOPID(((ir_loop *)target));
398 if (is_ir_node(reg) && is_fragile_op(skip_Proj(get_Block_cfgpred(reg, i))))
399 fprintf(F, EXC_CF_EDGE_ATTR);
404 #include "execution_frequency.h"
406 void dump_interval_block(FILE *F, ir_node *block) {
408 /* This is a block. Dump a node for the block. */
409 fprintf (F, "node: {title: \""); PRINT_NODEID(block);
410 fprintf (F, "\" label: \"");
411 if (block == get_irg_start_block(get_irn_irg(block)))
412 fprintf(F, "Start ");
413 if (block == get_irg_end_block(get_irn_irg(block)))
416 fprintf (F, "%s ", get_op_name(get_irn_op(block)));
418 fprintf(F, " freq: %9.4lf", get_region_exec_freq(block));
419 fprintf(F, " n_outs: %d", get_region_n_outs(block));
420 fprintf(F, " n_exc_outs: %d", get_region_n_exc_outs(block));
422 fprintf(F, "info1:\"");
423 if (dump_dominator_information_flag)
424 fprintf(F, "dom depth %d\n", get_Block_dom_depth(block));
426 /* show arity and possible Bad predecessors of the block */
427 fprintf(F, "arity: %d\n", get_Block_n_cfgpreds(block));
428 for (fl = i = 0; i < get_Block_n_cfgpreds(block); ++i) {
429 ir_node *pred = get_Block_cfgpred(block, i);
432 fprintf(F, "Bad pred at pos: ");
433 fprintf(F, "%d ", i);
440 fprintf (F, "\""); /* closing quote of info */
442 if ((block == get_irg_start_block(get_irn_irg(block))) ||
443 (block == get_irg_end_block(get_irn_irg(block))) )
444 fprintf(F, " color:blue ");
446 fprintf(F, " color:yellow ");
451 void dump_interval_loop(FILE *F, ir_loop *l) {
452 int i, n_elems = get_loop_n_elements(l);
454 fprintf(F, "graph: { title: \"");
456 fprintf(F, "\" label: \"loop %d", get_loop_loop_nr(l));
457 fprintf(F, " freq: %9.4lf", get_region_exec_freq(l));
458 fprintf(F, " n_outs: %d", get_region_n_outs(l));
459 fprintf(F, " n_exc_outs: %d", get_region_n_exc_outs(l));
460 fprintf(F, "\" status:clustered color:white \n");
462 for (i = 0; i < n_elems; ++i) {
463 loop_element e = get_loop_element(l, i);
464 dump_region_edges(F, e.node);
467 dump_interval_block(F, e.node);
470 dump_interval_loop(F, e.son);
480 void dump_interval_graph(ir_graph *irg, const char *suffix) {
483 if (!is_filtered_dump_name(get_entity_ident(get_irg_entity(irg))))
486 f = vcg_open(irg, suffix, "-intervals");
487 dump_vcg_header(f, get_irg_dump_name(irg), NULL);
489 current_ir_graph = irg;
491 dump_interval_loop(f, get_irg_loop(current_ir_graph));