2 * Chordal register allocation.
3 * @author Sebastian Hack
18 #include "irprintf_t.h"
30 #include "besched_t.h"
41 #ifdef USE_OLD_PHI_INTERFERENCE
47 #define TEST_COLORS 2048
49 static firm_dbg_module_t *dbg;
51 /** An interval border. */
52 typedef struct _border_t {
53 struct list_head list; /**< list head for queuing. */
54 const ir_node *irn; /**< The node. */
55 unsigned step; /**< The number equal to the interval border. */
56 unsigned is_def : 1; /**< Does this border denote a use or a def. */
59 typedef struct _env_t {
60 struct obstack obst; /**< An obstack for temporary storage. */
61 set *graph; /**< The interference graph. */
62 bitset_t *live; /**< A live bitset to use in every block. */
63 bitset_t *processed; /**< A set marking processed blocks. */
64 bitset_t *colors; /**< The color mask. */
65 bitset_t *in_colors; /**< Colors used by live in values. */
66 int colors_n; /**< The number of colors. */
69 typedef struct _be_chordal_dump_params_t {
73 } be_chordal_dump_params_t;
75 static const be_chordal_dump_params_t dump_params = {
81 static void draw_interval_graphs(ir_node *block,
82 struct list_head *border_head,
83 const be_chordal_dump_params_t *params)
86 int x_dist = params->x_dist;
87 int y_dist = params->y_dist;
88 ir_graph *irg = get_irn_irg(block);
93 ir_snprintf(buf, sizeof(buf), "intv_%s_bl%N.eps",
94 get_entity_name(get_irg_entity(irg)), block);
96 if((f = fopen(buf, "wt")) != NULL) {
98 int *seen = xcalloc(get_graph_node_count(irg), sizeof(seen[0]));
99 int last_pos = list_empty(border_head) ? 0 : list_entry(border_head->prev, border_t, list)->step;
102 list_for_each_entry_reverse(border_t, b, border_head, list) {
103 const ir_node *irn = b->irn;
104 int col = get_irn_color(irn);
105 max_col = max_col > col ? max_col : col;
108 fprintf(f, "%%!PS-Adobe-2.0\n");
109 fprintf(f, "%%%%BoundingBox: -10 -10 %d %d\n",
110 x_dist * last_pos + x_dist, y_dist * max_col + y_dist);
111 fprintf(f, "/mainfont /Courier findfont %f scalefont def\n", params->font_scale);
112 fprintf(f, "mainfont setfont\n");
113 fprintf(f, "0.2 setlinewidth\n");
115 for(i = 0; i <= last_pos; ++i) {
116 fprintf(f, "0 0 0 setrgbcolor\n");
117 fprintf(f, "%d %d moveto\n", i * x_dist, -2);
118 fprintf(f, "%d %d lineto\n", i * x_dist, max_col * y_dist + 2);
119 fprintf(f, "stroke\n");
121 fprintf(f, "0.5 setlinewidth\n");
123 list_for_each_entry_reverse(border_t, b, border_head, list) {
124 const ir_node *irn = b->irn;
125 int nr = get_irn_graph_nr(irn);
130 int col = get_irn_color(irn);
132 int pos = last_pos - seen[nr];
133 int end_pos = last_pos - b->step;
134 int live_in = is_live_in(block, irn);
135 int live_end = is_live_end(block, irn);
136 int y_val = y_dist * col;
139 int green = live_end;
142 fprintf(f, "0 0 0 setrgbcolor\n");
143 fprintf(f, "%d %d moveto\n", x_dist * pos + 2, y_val + 2);
144 ir_fprintf(f, "(%n/%d%s) show\n", irn, nr, is_phi_operand(irn) ? "*" : "");
145 fprintf(f, "%d %d %d setrgbcolor\n", red, green, blue);
146 fprintf(f, "%d %d moveto\n", x_dist * pos, y_val);
147 fprintf(f, "%d %d lineto\n", (x_dist * end_pos) - 5, y_val);
148 fprintf(f, "stroke\n");
159 typedef struct _if_edge_t {
163 #define IF_EDGE_HASH(e) ((e)->src)
165 static int if_edge_cmp(const void *p1, const void *p2, size_t size)
167 const if_edge_t *e1 = p1;
168 const if_edge_t *e2 = p2;
170 return !(e1->src == e2->src && e1->tgt == e2->tgt);
173 static INLINE if_edge_t *edge_init(if_edge_t *edge, int src, int tgt)
175 /* Bring the smaller entry to src. */
187 static INLINE void add_if(const env_t *env, int src, int tgt)
190 edge_init(&edge, src, tgt);
191 set_insert(env->graph, &edge, sizeof(edge), IF_EDGE_HASH(&edge));
194 static INLINE int are_connected(const env_t *env, int src, int tgt)
197 edge_init(&edge, src, tgt);
198 return set_find(env->graph, &edge, sizeof(edge), IF_EDGE_HASH(&edge)) != NULL;
201 static void dump_ifg(set *edges, const char *filename)
205 if((f = fopen(filename, "wt")) != NULL) {
208 fprintf(f, "graph G {\n");
210 for(edge = set_first(edges); edge; edge = set_next(edges)) {
211 fprintf(f, "i\tn%d -- n%d\n", edge->src, edge->tgt);
220 #endif /* USE_OLD_PHI_INTERFERENCE || BUILD_GRAPH */
222 static INLINE border_t *border_add(env_t *env, struct list_head *head,
223 const ir_node *irn, int step, int is_def)
225 border_t *b = obstack_alloc(&env->obst, sizeof(*b));
229 list_add_tail(&b->list, head);
233 static void block_alloc(ir_node *block, void *env_ptr)
235 env_t *env = env_ptr;
236 struct obstack *obst = &env->obst;
237 void *obstack_level = obstack_base(obst);
238 bitset_t *live = env->live;
239 bitset_t *colors = env->colors;
240 bitset_t *in_colors = env->in_colors;
241 bitset_t *used_colors = bitset_malloc(env->colors_n);
242 bitset_t *tmp_colors = bitset_obstack_alloc(obst, env->colors_n);
243 ir_graph *irg = get_irn_irg(block);
247 int block_nr = get_block_graph_nr(block);
250 struct list_head head;
251 pset *live_in = get_live_in(block);
252 pset *live_end = get_live_end(block);
253 ir_node *idom = get_Block_idom(block);
256 * Check, if this block has already been processed, if true, return
259 if(bitset_is_set(env->processed, block_nr))
263 * Ensure, that the immediate dominator of this block is allocated
264 * before this block, since the values live in at this block are
265 * defined in the dominators of this block. Coloring the dominators
266 * thus is vital before coloring this block.
269 block_alloc(idom, env);
271 /* Clear the live and allocate the color bitset. */
272 bitset_clear_all(live);
273 bitset_clear_all(colors);
274 bitset_clear_all(in_colors);
276 INIT_LIST_HEAD(&head);
279 * Make final uses of all values live out of the block.
280 * They are neccessary to build up real intervals.
282 for(irn = pset_first(live_end); irn; irn = pset_next(live_end)) {
283 DBG((dbg, LEVEL_3, "Making live: %n/%d\n", irn, get_irn_graph_nr(irn)));
284 bitset_set(live, get_irn_graph_nr(irn));
285 if(!is_Phi(irn) && is_allocatable_irn(irn))
286 border_add(env, &head, irn, step, 0);
292 * Determine the last uses of a value inside the block, since they are
293 * relevant for the interval borders.
295 sched_foreach_reverse(block, irn) {
296 DBG((dbg, LEVEL_1, "insn: %n\n", irn));
297 DBG((dbg, LEVEL_2, "live: %b\n", live));
299 set_irn_color(irn, NO_COLOR);
302 * If the node defines a datab value, i.e. something, registers must
303 * be allocated for, add a new def border to the border list.
305 if(is_allocatable_irn(irn)) {
306 int nr = get_irn_graph_nr(irn);
308 bitset_clear(live, nr);
309 border_add(env, &head, irn, step, 1);
314 bitset_foreach(live, elm) {
315 int live_nr = (int) elm;
316 ir_node *live_irn = get_irn_for_graph_nr(irg, live_nr);
317 if(is_phi_operand(live_irn)) {
318 add_if(env, nr, live_nr);
326 * If the node is no phi node we can examine the uses.
329 for(i = 0, n = get_irn_arity(irn); i < n; ++i) {
330 ir_node *op = get_irn_n(irn, i);
332 if(is_allocatable_irn(op)) {
333 int nr = get_irn_graph_nr(op);
335 DBG((dbg, LEVEL_4, "\t\tpos: %d, use: %n\n", i, op));
337 if(!bitset_is_set(live, nr)) {
338 border_add(env, &head, op, step, 0);
339 bitset_set(live, nr);
348 bitset_clear_all(live);
351 * Add initial defs for all values live in.
352 * Since their colors have already been assigned (The dominators were
353 * allocated before), we have to mark their colors as used also.
355 for(irn = pset_first(live_in); irn; irn = pset_next(live_in)) {
356 if(is_allocatable_irn(irn)) {
357 int col = get_irn_color(irn);
359 /* Mark the color of the live in value as used. */
360 assert(is_color(col) && "Node must have been assigned a color.");
361 bitset_set(colors, col);
362 bitset_set(in_colors, col);
363 bitset_set(used_colors, col);
365 /* Mark the value live in. */
366 bitset_set(live, get_irn_graph_nr(irn));
369 border_add(env, &head, irn, step, 1);
373 DBG((dbg, LEVEL_4, "usedef chain for block %n\n", block));
374 list_for_each_entry(border_t, b, &head, list) {
375 DBG((dbg, LEVEL_4, "\t%s %n %d\n", b->is_def ? "def" : "use", b->irn, get_irn_graph_nr(b->irn)));
379 * Mind that the sequence of defs from back to front defines a perfect
380 * elimination order. So, coloring the definitions from first to last
383 list_for_each_entry_reverse(border_t, b, &head, list) {
384 const ir_node *irn = b->irn;
385 int nr = get_irn_graph_nr(irn);
388 * Assign a color, if it is a local def. Global defs already have a
391 if(b->is_def && !is_live_in(block, irn)) {
392 ra_node_info_t *ri = get_ra_node_info(irn);
395 DBG((dbg, LEVEL_4, "colors in use: %b\n", colors));
398 * Try to assign live out values colors which are not used by live
401 if(is_live_out(block, irn)) {
402 bitset_copy(tmp_colors, colors);
403 bitset_or(tmp_colors, in_colors);
404 col = bitset_next_clear(tmp_colors, 0);
405 DBG((dbg, LEVEL_5, "next clear in only outs %b: %d\n", tmp_colors, col));
408 /* If a color is not yet assigned, do it now. */
410 col = bitset_next_clear(colors, 0);
412 assert(!is_color(get_irn_color(irn)) && "Color must not have assigned");
413 assert(!bitset_is_set(live, nr) && "Value def must not have been encountered");
415 bitset_set(colors, col);
416 bitset_set(used_colors, col);
417 bitset_set(live, nr);
420 ri->pressure = bitset_popcnt(colors);
422 DBG((dbg, LEVEL_1, "\tassigning color %d to %n\n", col, irn));
425 /* Clear the color upon a use. */
426 else if(!b->is_def) {
427 int col = get_irn_color(irn);
429 assert(bitset_is_set(live, nr) && "Cannot have a non live use");
430 assert(is_color(col) && "A color must have been assigned");
432 bitset_clear(colors, col);
433 bitset_clear(live, nr);
437 #ifdef DUMP_INTERVALS
438 draw_interval_graphs(block, &head, &dump_params);
446 ir_snprintf(buf, sizeof(buf), "pres_%s_bl_%N.txt",
447 get_entity_name(get_irg_entity(irg)), block);
449 if((f = fopen(buf, "wt")) != NULL) {
450 sched_foreach_reverse(block, irn) {
451 if(is_allocatable_irn(irn))
452 ir_fprintf(f, "\"%n\" %d %d\n", irn, sched_get_time_step(irn),
453 get_ra_node_info(irn)->pressure);
463 * Allocate the used colors array in the blocks ra info structure and
466 get_ra_block_info(block)->used_colors = used_colors;
468 /* Mark this block has processed. */
469 bitset_set(env->processed, block_nr);
471 /* Reset the obstack to its initial level */
472 obstack_free(obst, obstack_level);
475 void be_ra_chordal_init(void)
477 dbg = firm_dbg_register(DBG_BERA);
478 firm_dbg_set_mask(dbg, -1);
481 void be_ra_chordal(ir_graph *irg)
483 int node_count = get_graph_node_count(irg);
484 env_t *env = malloc(sizeof(*env));
486 if(get_irg_dom_state(irg) != dom_consistent)
489 obstack_init(&env->obst);
492 env->graph = new_set(if_edge_cmp, node_count);
495 env->live = bitset_obstack_alloc(&env->obst, node_count);
496 env->processed = bitset_obstack_alloc(&env->obst, get_graph_block_count(irg));
497 env->colors = bitset_obstack_alloc(&env->obst, TEST_COLORS);
498 env->in_colors = bitset_obstack_alloc(&env->obst, TEST_COLORS);
499 env->colors_n = TEST_COLORS;
501 irg_block_walk_graph(irg, block_alloc, NULL, env);
502 obstack_free(&env->obst, NULL);
508 ir_snprintf(buf, sizeof(buf), "ifg_%s.dot", get_entity_name(get_irg_entity(irg)));
509 dump_ifg(env->graph, buf);
513 set_irg_ra_link(irg, env);
516 void be_ra_chordal_done(ir_graph *irg)
518 env_t *env = get_irg_ra_link(irg);
526 int phi_ops_interfere(const ir_node *a, const ir_node *b)
528 #ifdef USE_OLD_PHI_INTERFERENCE
529 ir_graph *irg = get_irn_irg(a);
530 env_t *env = get_irg_ra_link(irg);
532 assert(irg == get_irn_irg(b) && "Both nodes must be in the same graph");
534 return are_connected(env, get_irn_graph_nr(a), get_irn_graph_nr(b));
536 return values_interfere(a, b);
537 #endif /* USE_OLD_PHI_INTERFERENCE */