2 #include "interval_analysis.h"
4 #include "firm_common_t.h"
13 /*------------------------------------------------------------------*/
14 /* A new in array via a hashmap. */
15 /* The in array refers to the loop the block is contained in if the */
16 /* block is not in blocks loop. */
17 /*------------------------------------------------------------------*/
27 static set *region_attr_set = NULL;
29 int region_attr_cmp(const void *e1, const void *e2, size_t size) {
30 region_attr *ra1 = (region_attr *)e1;
31 region_attr *ra2 = (region_attr *)e2;
32 return (ra1->reg != ra2->reg);
35 static INLINE int attr_set_hash (region_attr *a) {
36 unsigned int v = (unsigned int) a->reg;
40 static INLINE region_attr *get_region_attr(void *region) {
41 region_attr r_attr, *res;
44 res = set_find(region_attr_set, &r_attr, sizeof(r_attr), attr_set_hash(&r_attr));
47 r_attr.in_array = NEW_ARR_F(void *, 0);
48 if (is_ir_loop(region))
49 r_attr.op_array = NEW_ARR_F(void *, 0);
51 r_attr.op_array = NULL;
53 r_attr.n_exc_outs = 0;
54 res = set_insert(region_attr_set, &r_attr, sizeof(r_attr), attr_set_hash(&r_attr));
60 int get_region_n_ins(void *region) {
61 return ARR_LEN(get_region_attr(region)->in_array);
64 void *get_region_in(void *region, int pos) {
65 assert(0 <= pos && pos < get_region_n_ins(region));
66 return (get_region_attr(region)->in_array)[pos];
69 void add_region_in (void *region, void *in) {
70 ARR_APP1(void *, get_region_attr(region)->in_array, in);
71 get_region_attr(in)->n_outs++;
74 int get_region_n_outs(void *region) {
75 return get_region_attr(region)->n_outs;
78 int get_region_n_exc_outs(void *region) {
79 return get_region_attr(region)->n_exc_outs;
82 void inc_region_n_exc_outs(void *region) {
83 (get_region_attr(region)->n_exc_outs)++;
86 void *get_loop_cfop(void *region, int pos) {
87 assert(0 <= pos && pos < get_region_n_ins(region));
88 return (get_region_attr(region)->op_array)[pos];
91 void add_loop_cfop (void *region, void *cfop) {
93 ARR_APP1(void *, get_region_attr(region)->op_array, cfop);
96 static INLINE void exc_outs(void *reg, ir_node *cfop) {
97 if (is_fragile_op(cfop)) inc_region_n_exc_outs(reg);
100 /*------------------------------------------------------------------*/
101 /* Algorithm to construct the interval edges based on a loop tree. */
102 /* Walk a loop and add all edges. Walk inner loops by recursion. */
103 /*------------------------------------------------------------------*/
105 /* return true if outer can be reached from inner via the outer loop relation */
106 static int find_outer_loop(ir_loop *inner, ir_loop *outer, ir_node *b, ir_node *cfop) {
107 if (get_loop_outer_loop(inner) == outer) {
108 add_region_in(inner, b);
109 add_loop_cfop(inner, cfop);
116 static int test_loop_nest(ir_node *pred_b, ir_loop *nest) {
117 int i, n_elems = get_loop_n_elements(nest);
119 for (i = 0; (i < n_elems); ++i) {
120 loop_element e = get_loop_element(nest, i);
123 if (e.node == pred_b) return true;
126 if (test_loop_nest(pred_b, e.son)) return true;
134 static int find_inner_loop(ir_node *b, ir_loop *l, ir_node *pred, ir_node *cfop) {
135 int i, n_elems = get_loop_n_elements(l);
138 for (i = 0; (i < n_elems) && !found; ++i) {
139 loop_element e = get_loop_element(l, i);
142 if (e.node == b) return false;
145 found = test_loop_nest(pred, e.son);
147 add_region_in(b, e.son);
148 exc_outs(e.son, cfop);
149 //if (is_fragile_op(cfop)) inc_region_n_exc_outs(b);
160 static int find_previous_loop(ir_loop *l, ir_loop *pred_l, ir_node *b, ir_node *pred_b, ir_node *cfop) {
161 ir_loop *outer = get_loop_outer_loop(l);
163 int l_pos = get_loop_element_pos(outer, l);
165 assert(l_pos > 0 && "Is this a necessary condition? There could be a perfect nest ...");
167 for (i = l_pos -1, found = false; i > -1 && !found; --i) {
168 ir_loop *k = get_loop_element(outer, i).son;
170 found = test_loop_nest(pred_b, k);
173 //if (is_fragile_op(cfop)) inc_region_n_exc_outs(k);
175 add_loop_cfop(l, cfop);
176 add_region_in(b, NULL);
182 DDMG(current_ir_graph);
193 /* Compute the edges for the interval graph.
195 * @param b The block for which to constuct the edges.
196 * @param l The loop of b.
198 * There are four cases:
199 * - The pred block is in the same loop. Add a normal block-block edge.
200 * - The pred block is in a loop contained in this loop, somewhere down in
201 * the nesting. The predecessor of this block is the outermost loop of the nest
202 * directly contained in l.
203 * - The pred block is in the outer loop of l. l gets an edge to the pred block.
204 * - The outer loop of l contains another loop k just before l. The control flow
205 * branches directly from loop k to loop l. Add an edge l->k. Watch it: k must
206 * not be a direct predecessor of l in the loop tree!
208 static void construct_interval_block(ir_node *b, ir_loop *l) {
209 int i, n_cfgpreds = get_Block_n_cfgpreds(b);
211 if (b == get_irg_start_block(current_ir_graph)) return;
212 /* We want nice blocks. */
213 assert(n_cfgpreds > 0);
215 for (i = 0; i < n_cfgpreds; ++i) {
216 if (is_backedge(b, i)) {
217 /* There are no backedges in the interval decomposition. */
218 add_region_in(b, NULL);
222 ir_node *cfop = skip_Proj(get_Block_cfgpred(b, i));
223 ir_node *pred = get_nodes_block(cfop);
224 /* We want nice blocks. */
225 assert( get_irn_op(pred) != op_Bad
226 && get_irn_op(skip_Proj(get_Block_cfgpred(b, i))) != op_Bad);
227 ir_loop *pred_l = get_irn_loop(pred);
229 add_region_in(b, pred);
230 //if (is_fragile_op(cfop)) inc_region_n_exc_outs(b);
231 exc_outs(pred, cfop);
233 int found = find_inner_loop(b, l, pred, cfop);
235 found = find_outer_loop(l, pred_l, pred, cfop);
236 if (found) add_region_in(b, NULL); /* placeholder */
239 found = find_previous_loop(l, pred_l, b, pred, cfop);
242 DDMG(current_ir_graph);
245 assert(is_backedge(b, i));
246 assert(found && "backedge from inner loop");
250 if (b != get_loop_element(l, 0).node) {
251 /* Check for improper region */
252 if (has_backedges(b)) {
253 printf("Improper Region!!!!!!\n");
254 DDMG(current_ir_graph);
262 static void construct_interval_edges(ir_loop *l) {
263 int i, n_elems = get_loop_n_elements(l);
264 for (i = 0; i < n_elems; ++i) {
265 loop_element e = get_loop_element(l, i);
268 construct_interval_block(e.node, l);
271 construct_interval_edges(e.son);
278 void construct_intervals(ir_graph *irg) {
280 ir_graph *rem = current_ir_graph;
281 current_ir_graph = irg;
283 if (!region_attr_set)
284 region_attr_set = new_set(region_attr_cmp, 256);
286 construct_cf_backedges(current_ir_graph);
289 l = get_irg_loop(current_ir_graph);
291 construct_interval_edges(l);
293 current_ir_graph = rem;
296 void free_intervals(void) {
298 if (!region_attr_set) return;
300 for (ins = (void **)pmap_first(region_in_map);
302 ins = (void **)pmap_next(region_in_map)) {
306 del_set(region_attr_set);
307 region_attr_set = NULL;
310 /*------------------------------------------------------------------*/
311 /* A vcg dumper showing an interval decomposition of a cfg. */
313 /*------------------------------------------------------------------*/
315 void dump_region_edges(FILE *F, void *reg) {
316 int i, n_ins = get_region_n_ins(reg);
318 if (is_ir_node(reg) && get_Block_n_cfgpreds((ir_node *)reg) > get_region_n_ins(reg)) {
319 for (i = n_ins; i < get_Block_n_cfgpreds((ir_node *)reg); ++i) {
320 if (is_backedge((ir_node *)reg, i))
321 fprintf (F, "backedge: { sourcename: \"");
323 fprintf (F, "edge: { sourcename: \"");
324 PRINT_NODEID(((ir_node *)reg));
325 fprintf (F, "\" targetname: \"");
326 PRINT_NODEID(get_nodes_block(skip_Proj(get_Block_cfgpred((ir_node *)reg, i))));
327 fprintf (F, "\" " BLOCK_EDGE_ATTR "}\n");
331 for (i = 0; i < n_ins; ++i) {
332 void *target = get_region_in(reg, i);
334 if (is_ir_node(reg)) {
335 if (get_Block_n_cfgpreds((ir_node *)reg) != get_region_n_ins(reg)) {
336 printf("n_cfgpreds = %d, n_ins = %d\n", get_Block_n_cfgpreds((ir_node *)reg), get_region_n_ins(reg));
337 DDMN((ir_node *)reg);
341 if ((!target || (is_ir_node(reg) && !is_ir_node(target))) && i < get_Block_n_cfgpreds((ir_node *)reg)) {
342 assert(is_ir_node(reg));
343 if (is_backedge((ir_node *)reg, i))
344 fprintf (F, "backedge: { sourcename: \"");
346 fprintf (F, "edge: { sourcename: \"");
347 PRINT_NODEID(((ir_node *)reg));
348 fprintf (F, "\" targetname: \"");
349 PRINT_NODEID(get_nodes_block(skip_Proj(get_Block_cfgpred((ir_node *)reg, i))));
350 fprintf (F, "\" " BLOCK_EDGE_ATTR "}\n");
352 if (!target) continue;
355 fprintf (F, "edge: { sourcename: \"");
356 if (is_ir_node(reg)) {
357 PRINT_NODEID(((ir_node *)reg));
359 PRINT_LOOPID(((ir_loop *)reg));
361 fprintf (F, "\" targetname: \"");
362 if (is_ir_node(target)) {
363 PRINT_NODEID(((ir_node *)target));
365 PRINT_LOOPID(((ir_loop *)target));
368 if (is_ir_node(reg) && is_fragile_op(skip_Proj(get_Block_cfgpred(reg, i))))
369 fprintf(F, EXC_CF_EDGE_ATTR);
374 #include "execution_frequency.h"
376 void dump_interval_block(FILE *F, ir_node *block) {
378 /* This is a block. Dump a node for the block. */
379 fprintf (F, "node: {title: \""); PRINT_NODEID(block);
380 fprintf (F, "\" label: \"");
381 if (block == get_irg_start_block(get_irn_irg(block)))
382 fprintf(F, "Start ");
383 if (block == get_irg_end_block(get_irn_irg(block)))
386 fprintf (F, "%s ", get_op_name(get_irn_op(block)));
388 fprintf(F, " freq: %9.4lf", get_region_exec_freq(block));
389 fprintf(F, " n_outs: %d", get_region_n_outs(block));
390 fprintf(F, " n_exc_outs: %d", get_region_n_exc_outs(block));
392 fprintf(F, "info1:\"");
393 if (dump_dominator_information_flag)
394 fprintf(F, "dom depth %d\n", get_Block_dom_depth(block));
396 /* show arity and possible Bad predecessors of the block */
397 fprintf(F, "arity: %d\n", get_Block_n_cfgpreds(block));
398 for (fl = i = 0; i < get_Block_n_cfgpreds(block); ++i) {
399 ir_node *pred = get_Block_cfgpred(block, i);
402 fprintf(F, "Bad pred at pos: ");
403 fprintf(F, "%d ", i);
410 fprintf (F, "\""); /* closing quote of info */
412 if ((block == get_irg_start_block(get_irn_irg(block))) ||
413 (block == get_irg_end_block(get_irn_irg(block))) )
414 fprintf(F, " color:blue ");
416 fprintf(F, " color:yellow ");
421 void dump_interval_loop(FILE *F, ir_loop *l) {
422 int i, n_elems = get_loop_n_elements(l);
424 fprintf(F, "graph: { title: \"");
426 fprintf(F, "\" label: \"loop %d", get_loop_loop_nr(l));
427 fprintf(F, " freq: %9.4lf", get_region_exec_freq(l));
428 fprintf(F, " n_outs: %d", get_region_n_outs(l));
429 fprintf(F, " n_exc_outs: %d", get_region_n_exc_outs(l));
430 fprintf(F, "\" status:clustered color:white \n");
432 for (i = 0; i < n_elems; ++i) {
433 loop_element e = get_loop_element(l, i);
434 dump_region_edges(F, e.node);
437 dump_interval_block(F, e.node);
440 dump_interval_loop(F, e.son);
450 void dump_interval_graph(ir_graph *irg, const char *suffix) {
453 if (strncmp(get_entity_name(get_irg_entity(irg)), dump_file_filter, strlen(dump_file_filter)) != 0)
456 f = vcg_open(irg, suffix, "-intervals");
457 dump_vcg_header(f, get_irg_dump_name(irg), NULL);
459 current_ir_graph = irg;
461 dump_interval_loop(f, get_irg_loop(current_ir_graph));