2 * Chordal register allocation.
3 * @author Sebastian Hack
15 #include "irprintf_t.h"
27 #include "besched_t.h"
33 #define TEST_COLORS 2048
35 static firm_dbg_module_t *dbg;
37 /** An interval border. */
38 typedef struct _border_t {
39 struct list_head list; /**< list head for queuing. */
40 const ir_node *irn; /**< The node. */
41 unsigned step; /**< The number equal to the interval border. */
42 unsigned is_def : 1; /**< Does this border denote a use or a def. */
45 typedef struct _env_t {
46 struct obstack obst; /**< An obstack for temporary storage. */
47 set *phi_if; /**< The phi interference map. */
48 bitset_t *live; /**< A live bitset to use in every block. */
49 bitset_t *processed; /**< A set marking processed blocks. */
50 bitset_t *colors; /**< The color mask. */
51 bitset_t *in_colors; /**< Colors used by live in values. */
52 int colors_n; /**< The number of colors. */
55 typedef struct _be_chordal_dump_params_t {
59 } be_chordal_dump_params_t;
61 static const be_chordal_dump_params_t dump_params = {
67 static void draw_interval_graphs(ir_node *block,
68 struct list_head *border_head,
69 const be_chordal_dump_params_t *params)
72 int x_dist = params->x_dist;
73 int y_dist = params->y_dist;
74 ir_graph *irg = get_irn_irg(block);
79 ir_snprintf(buf, sizeof(buf), "intv_%s_bl%N.eps",
80 get_entity_name(get_irg_entity(irg)), block);
82 if((f = fopen(buf, "wt")) != NULL) {
84 int *seen = xcalloc(get_graph_node_count(irg), sizeof(seen[0]));
85 int last_pos = list_empty(border_head) ? 0 : list_entry(border_head->prev, border_t, list)->step;
88 list_for_each_entry_reverse(border_t, b, border_head, list) {
89 const ir_node *irn = b->irn;
90 int col = get_irn_color(irn);
91 max_col = max_col > col ? max_col : col;
94 fprintf(f, "%%!PS-Adobe-2.0\n");
95 fprintf(f, "%%%%BoundingBox: -10 -10 %d %d\n",
96 x_dist * last_pos + x_dist, y_dist * max_col + y_dist);
97 fprintf(f, "/mainfont /Courier findfont %f scalefont def\n", params->font_scale);
98 fprintf(f, "mainfont setfont\n");
99 fprintf(f, "0.2 setlinewidth\n");
101 for(i = 0; i <= last_pos; ++i) {
102 fprintf(f, "0 0 0 setrgbcolor\n");
103 fprintf(f, "%d %d moveto\n", i * x_dist, -2);
104 fprintf(f, "%d %d lineto\n", i * x_dist, max_col * y_dist + 2);
105 fprintf(f, "stroke\n");
107 fprintf(f, "0.5 setlinewidth\n");
109 list_for_each_entry_reverse(border_t, b, border_head, list) {
110 const ir_node *irn = b->irn;
111 int nr = get_irn_graph_nr(irn);
116 int col = get_irn_color(irn);
118 int pos = last_pos - seen[nr];
119 int end_pos = last_pos - b->step;
120 int live_in = is_live_in(block, irn);
121 int live_end = is_live_end(block, irn);
122 int y_val = y_dist * col;
125 int green = live_end;
128 fprintf(f, "0 0 0 setrgbcolor\n");
129 fprintf(f, "%d %d moveto\n", x_dist * pos + 2, y_val + 2);
130 ir_fprintf(f, "(%n/%d%s) show\n", irn, nr, is_phi_operand(irn) ? "*" : "");
131 fprintf(f, "%d %d %d setrgbcolor\n", red, green, blue);
132 fprintf(f, "%d %d moveto\n", x_dist * pos, y_val);
133 fprintf(f, "%d %d lineto\n", (x_dist * end_pos) - 5, y_val);
134 fprintf(f, "stroke\n");
143 #ifdef USE_OLD_PHI_INTERFERENCE
145 typedef struct _if_edge_t {
149 #define IF_EDGE_HASH(e) ((e)->src)
151 static int if_edge_cmp(const void *p1, const void *p2, size_t size)
153 const if_edge_t *e1 = p1;
154 const if_edge_t *e2 = p2;
156 return !(e1->src == e2->src && e1->tgt == e2->tgt);
159 static INLINE if_edge_t *edge_init(if_edge_t *edge, int src, int tgt)
161 /* Bring the smaller entry to src. */
173 static INLINE void add_if(const env_t *env, int src, int tgt)
176 edge_init(&edge, src, tgt);
177 set_insert(env->phi_if, &edge, sizeof(edge), IF_EDGE_HASH(&edge));
180 static INLINE int are_connected(const env_t *env, int src, int tgt)
183 edge_init(&edge, src, tgt);
184 return set_find(env->phi_if, &edge, sizeof(edge), IF_EDGE_HASH(&edge)) != NULL;
187 #endif /* USE_OLD_PHI_INTERFERENCE */
189 static INLINE border_t *border_add(env_t *env, struct list_head *head,
190 const ir_node *irn, int step, int is_def)
192 border_t *b = obstack_alloc(&env->obst, sizeof(*b));
196 list_add_tail(&b->list, head);
200 static void block_alloc(ir_node *block, void *env_ptr)
202 env_t *env = env_ptr;
203 struct obstack *obst = &env->obst;
204 void *obstack_level = obstack_base(obst);
205 bitset_t *live = env->live;
206 bitset_t *colors = env->colors;
207 bitset_t *in_colors = env->in_colors;
208 bitset_t *used_colors = bitset_malloc(env->colors_n);
209 bitset_t *tmp_colors = bitset_obstack_alloc(obst, env->colors_n);
210 ir_graph *irg = get_irn_irg(block);
214 int block_nr = get_block_graph_nr(block);
217 struct list_head head;
218 pset *live_in = get_live_in(block);
219 pset *live_end = get_live_end(block);
220 ir_node *idom = get_Block_idom(block);
223 * Check, if this block has already been processed, if true, return
226 if(bitset_is_set(env->processed, block_nr))
230 * Ensure, that the immediate dominator of this block is allocated
231 * before this block, since the values live in at this block are
232 * defined in the dominators of this block. Coloring the dominators
233 * thus is vital before coloring this block.
236 block_alloc(idom, env);
238 /* Clear the live and allocate the color bitset. */
239 bitset_clear_all(live);
240 bitset_clear_all(colors);
241 bitset_clear_all(in_colors);
243 INIT_LIST_HEAD(&head);
246 * Make final uses of all values live out of the block.
247 * They are neccessary to build up real intervals.
249 for(irn = pset_first(live_end); irn; irn = pset_next(live_end)) {
250 DBG((dbg, LEVEL_3, "Making live: %n/%d\n", irn, get_irn_graph_nr(irn)));
251 bitset_set(live, get_irn_graph_nr(irn));
252 if(!is_Phi(irn) && is_allocatable_irn(irn))
253 border_add(env, &head, irn, step, 0);
259 * Determine the last uses of a value inside the block, since they are
260 * relevant for the interval borders.
262 sched_foreach_reverse(block, irn) {
263 DBG((dbg, LEVEL_1, "insn: %n\n", irn));
264 DBG((dbg, LEVEL_2, "live: %b\n", live));
266 set_irn_color(irn, NO_COLOR);
269 * If the node defines a datab value, i.e. something, registers must
270 * be allocated for, add a new def border to the border list.
272 if(is_allocatable_irn(irn)) {
273 int nr = get_irn_graph_nr(irn);
275 bitset_clear(live, nr);
276 border_add(env, &head, irn, step, 1);
278 #ifdef USE_OLD_PHI_INTERFERENCE
279 if(is_phi_operand(irn)) {
281 bitset_foreach(live, elm) {
282 int live_nr = (int) elm;
283 ir_node *live_irn = get_irn_for_graph_nr(irg, live_nr);
284 if(is_phi_operand(live_irn)) {
285 DBG((dbg, LEVEL_3, "\t\tinterfering phi operands: %n, %n\n", irn, live_irn));
286 add_if(env, nr, live_nr);
294 * If the node is no phi node we can examine the uses.
297 for(i = 0, n = get_irn_arity(irn); i < n; ++i) {
298 ir_node *op = get_irn_n(irn, i);
300 if(is_allocatable_irn(op)) {
301 int nr = get_irn_graph_nr(op);
303 DBG((dbg, LEVEL_4, "\t\tpos: %d, use: %n\n", i, op));
305 if(!bitset_is_set(live, nr)) {
306 border_add(env, &head, op, step, 0);
307 bitset_set(live, nr);
316 bitset_clear_all(live);
319 * Add initial defs for all values live in.
320 * Since their colors have already been assigned (The dominators were
321 * allocated before), we have to mark their colors as used also.
323 for(irn = pset_first(live_in); irn; irn = pset_next(live_in)) {
324 if(is_allocatable_irn(irn)) {
325 int col = get_irn_color(irn);
327 /* Mark the color of the live in value as used. */
328 assert(is_color(col) && "Node must have been assigned a color.");
329 bitset_set(colors, col);
330 bitset_set(in_colors, col);
331 bitset_set(used_colors, col);
333 /* Mark the value live in. */
334 bitset_set(live, get_irn_graph_nr(irn));
337 border_add(env, &head, irn, step, 1);
341 DBG((dbg, LEVEL_4, "usedef chain for block %n\n", block));
342 list_for_each_entry(border_t, b, &head, list) {
343 DBG((dbg, LEVEL_4, "\t%s %n %d\n", b->is_def ? "def" : "use", b->irn, get_irn_graph_nr(b->irn)));
347 * Mind that the sequence of defs from back to front defines a perfect
348 * elimination order. So, coloring the definitions from first to last
351 list_for_each_entry_reverse(border_t, b, &head, list) {
352 const ir_node *irn = b->irn;
353 int nr = get_irn_graph_nr(irn);
356 * Assign a color, if it is a local def. Global defs already have a
359 if(b->is_def && !is_live_in(block, irn)) {
360 ra_node_info_t *ri = get_ra_node_info(irn);
364 * Try to assign live out values colors which are not used by live
367 if(is_live_out(block, irn)) {
368 bitset_copy(tmp_colors, colors);
369 bitset_andnot(tmp_colors, in_colors);
370 col = bitset_next_clear(tmp_colors, 0);
373 /* If a color is not yet assigned, do it now. */
375 col = bitset_next_clear(colors, 0);
377 assert(!is_color(get_irn_color(irn)) && "Color must not have assigned");
378 assert(!bitset_is_set(live, nr) && "Value def must not have been encountered");
380 bitset_set(colors, col);
381 bitset_set(used_colors, col);
382 bitset_set(live, nr);
385 ri->pressure = bitset_popcnt(colors);
387 DBG((dbg, LEVEL_1, "\tassigning color %d to %n\n", col, irn));
390 /* Clear the color upon a use. */
391 else if(!b->is_def) {
392 int col = get_irn_color(irn);
394 assert(bitset_is_set(live, nr) && "Cannot have a non live use");
395 assert(is_color(col) && "A color must have been assigned");
397 bitset_clear(colors, col);
398 bitset_clear(live, nr);
402 #ifdef DUMP_INTERVALS
403 draw_interval_graphs(block, &head, &dump_params);
411 ir_snprintf(buf, sizeof(buf), "pres_%s_bl_%N.txt",
412 get_entity_name(get_irg_entity(irg)), block);
414 if((f = fopen(buf, "wt")) != NULL) {
415 sched_foreach_reverse(block, irn) {
416 if(is_allocatable_irn(irn))
417 ir_fprintf(f, "\"%n\" %d %d\n", irn, sched_get_time_step(irn),
418 get_ra_node_info(irn)->pressure);
427 * Allocate the used colors array in the blocks ra info structure and
430 get_ra_block_info(block)->used_colors = used_colors;
432 /* Mark this block has processed. */
433 bitset_set(env->processed, block_nr);
435 /* Reset the obstack to its initial level */
436 obstack_free(obst, obstack_level);
439 void be_ra_chordal_init(void)
441 dbg = firm_dbg_register(DBG_BERA);
442 firm_dbg_set_mask(dbg, -1);
445 void be_ra_chordal(ir_graph *irg)
447 int node_count = get_graph_node_count(irg);
448 env_t *env = malloc(sizeof(*env));
450 if(get_irg_dom_state(irg) != dom_consistent)
453 obstack_init(&env->obst);
455 #ifdef USE_OLD_PHI_INTERFERENCE
456 env->phi_if = new_set(if_edge_cmp, node_count);
457 #endif /* USE_OLD_PHI_INTERFERENCE */
459 env->live = bitset_obstack_alloc(&env->obst, node_count);
460 env->processed = bitset_obstack_alloc(&env->obst, get_graph_block_count(irg));
461 env->colors = bitset_obstack_alloc(&env->obst, TEST_COLORS);
462 env->in_colors = bitset_obstack_alloc(&env->obst, TEST_COLORS);
463 env->colors_n = TEST_COLORS;
465 irg_block_walk_graph(irg, block_alloc, NULL, env);
466 obstack_free(&env->obst, NULL);
468 set_irg_ra_link(irg, env);
471 void be_ra_chordal_done(ir_graph *irg)
473 env_t *env = get_irg_ra_link(irg);
478 int phi_ops_interfere(const ir_node *a, const ir_node *b)
480 #ifdef USE_OLD_PHI_INTERFERENCE
481 ir_graph *irg = get_irn_irg(a);
482 env_t *env = get_irg_ra_link(irg);
484 assert(irg == get_irn_irg(b) && "Both nodes must be in the same graph");
486 return are_connected(env, get_irn_graph_nr(a), get_irn_graph_nr(b));
488 return values_interfere(a, b);
489 #endif /* USE_OLD_PHI_INTERFERENCE */