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 _if_edge_t {
49 #define IF_EDGE_HASH(e) ((e)->src)
51 static int if_edge_cmp(const void *p1, const void *p2, size_t size)
53 const if_edge_t *e1 = p1;
54 const if_edge_t *e2 = p2;
56 return !(e1->src == e2->src && e1->tgt == e2->tgt);
59 typedef struct _env_t {
60 struct obstack obst; /**< An obstack for temporary storage. */
61 set *phi_if; /**< The phi interference map. */
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 int colors_n; /**< The number of colors. */
68 typedef struct _be_chordal_dump_params_t {
72 } be_chordal_dump_params_t;
74 static const be_chordal_dump_params_t dump_params = {
80 static void draw_interval_graphs(ir_node *block,
81 struct list_head *border_head,
82 const be_chordal_dump_params_t *params)
85 int x_dist = params->x_dist;
86 int y_dist = params->y_dist;
87 ir_graph *irg = get_irn_irg(block);
92 ir_snprintf(buf, sizeof(buf), "intv_%s_bl%N.eps",
93 get_entity_name(get_irg_entity(irg)), block);
95 if((f = fopen(buf, "wt")) != NULL) {
97 int *seen = xcalloc(get_graph_node_count(irg), sizeof(seen[0]));
98 int last_pos = list_empty(border_head) ? 0 : list_entry(border_head->prev, border_t, list)->step;
101 list_for_each_entry_reverse(border_t, b, border_head, list) {
102 const ir_node *irn = b->irn;
103 int col = get_irn_color(irn);
104 max_col = max_col > col ? max_col : col;
107 fprintf(f, "%%!PS-Adobe-2.0\n");
108 fprintf(f, "%%%%BoundingBox: -10 -10 %d %d\n",
109 x_dist * last_pos + x_dist, y_dist * max_col + y_dist);
110 fprintf(f, "/mainfont /Courier findfont %f scalefont def\n", params->font_scale);
111 fprintf(f, "mainfont setfont\n");
112 fprintf(f, "0.2 setlinewidth\n");
114 for(i = 0; i <= last_pos; ++i) {
115 fprintf(f, "0 0 0 setrgbcolor\n");
116 fprintf(f, "%d %d moveto\n", i * x_dist, -2);
117 fprintf(f, "%d %d lineto\n", i * x_dist, max_col * y_dist + 2);
118 fprintf(f, "stroke\n");
120 fprintf(f, "0.5 setlinewidth\n");
122 list_for_each_entry_reverse(border_t, b, border_head, list) {
123 const ir_node *irn = b->irn;
124 int nr = get_irn_graph_nr(irn);
129 int col = get_irn_color(irn);
131 int pos = last_pos - seen[nr];
132 int end_pos = last_pos - b->step;
133 int live_in = is_live_in(block, irn);
134 int live_out = is_live_out(block, irn);
135 int y_val = y_dist * col;
138 int green = live_out;
141 fprintf(f, "0 0 0 setrgbcolor\n");
142 fprintf(f, "%d %d moveto\n", x_dist * pos + 2, y_val + 2);
143 ir_fprintf(f, "(%n/%d%s) show\n", irn, nr, is_phi_operand(irn) ? "*" : "");
144 fprintf(f, "%d %d %d setrgbcolor\n", red, green, blue);
145 fprintf(f, "%d %d moveto\n", x_dist * pos, y_val);
146 fprintf(f, "%d %d lineto\n", (x_dist * end_pos) - 5, y_val);
147 fprintf(f, "stroke\n");
156 static INLINE if_edge_t *edge_init(if_edge_t *edge, int src, int tgt)
158 /* Bring the smaller entry to src. */
170 static INLINE void add_if(const env_t *env, int src, int tgt)
173 edge_init(&edge, src, tgt);
174 set_insert(env->phi_if, &edge, sizeof(edge), IF_EDGE_HASH(&edge));
177 static INLINE int are_connected(const env_t *env, int src, int tgt)
180 edge_init(&edge, src, tgt);
181 return set_find(env->phi_if, &edge, sizeof(edge), IF_EDGE_HASH(&edge)) != NULL;
186 static INLINE border_t *border_add(env_t *env, struct list_head *head,
187 const ir_node *irn, int step, int is_def)
189 border_t *b = obstack_alloc(&env->obst, sizeof(*b));
193 list_add_tail(&b->list, head);
197 static void block_alloc(ir_node *block, void *env_ptr)
199 env_t *env = env_ptr;
200 struct obstack *obst = &env->obst;
201 void *obstack_level = obstack_base(obst);
202 bitset_t *live = env->live;
203 bitset_t *colors = env->colors;
204 bitset_t *used_colors = bitset_malloc(env->colors_n);
205 ir_graph *irg = get_irn_irg(block);
209 int block_nr = get_block_graph_nr(block);
212 struct list_head head;
213 pset *live_in = get_live_in(block);
214 pset *live_out = get_live_out(block);
215 ir_node *idom = get_Block_idom(block);
218 * Check, if this block has already been processed, if true, return
221 if(bitset_is_set(env->processed, block_nr))
225 * Ensure, that the immediate dominator of this block is allocated
226 * before this block, since the values live in at this block are
227 * defined in the dominators of this block. Coloring the dominators
228 * thus is vital before coloring this block.
231 block_alloc(idom, env);
233 /* Clear the live and allocate the color bitset. */
234 bitset_clear_all(live);
235 bitset_clear_all(colors);
237 INIT_LIST_HEAD(&head);
240 * Make final uses of all values live out of the block.
241 * They are neccessary to build up real intervals.
243 for(irn = pset_first(live_out); irn; irn = pset_next(live_out)) {
244 DBG((dbg, LEVEL_3, "Making live: %n/%d\n", irn, get_irn_graph_nr(irn)));
245 bitset_set(live, get_irn_graph_nr(irn));
246 if(!is_Phi(irn) && is_allocatable_irn(irn))
247 border_add(env, &head, irn, step, 0);
253 * Determine the last uses of a value inside the block, since they are
254 * relevant for the interval borders.
256 sched_foreach_reverse(block, irn) {
257 DBG((dbg, LEVEL_1, "insn: %n\n", irn));
258 DBG((dbg, LEVEL_2, "live: %b\n", live));
260 set_irn_color(irn, NO_COLOR);
263 * If the node defines a datab value, i.e. something, registers must
264 * be allocated for, add a new def border to the border list.
266 if(is_allocatable_irn(irn)) {
267 int nr = get_irn_graph_nr(irn);
269 bitset_clear(live, nr);
270 border_add(env, &head, irn, step, 1);
272 if(is_phi_operand(irn)) {
274 bitset_foreach(live, elm) {
275 int live_nr = (int) elm;
276 ir_node *live_irn = get_irn_for_graph_nr(irg, live_nr);
277 if(is_phi_operand(live_irn)) {
278 DBG((dbg, LEVEL_3, "\t\tinterfering phi operands: %n, %n\n", irn, live_irn));
279 add_if(env, nr, live_nr);
286 * If the node is no phi node we can examine the uses.
289 for(i = 0, n = get_irn_arity(irn); i < n; ++i) {
290 ir_node *op = get_irn_n(irn, i);
292 if(is_allocatable_irn(op)) {
293 int nr = get_irn_graph_nr(op);
295 DBG((dbg, LEVEL_4, "\t\tpos: %d, use: %n\n", i, op));
297 if(!bitset_is_set(live, nr)) {
298 border_add(env, &head, op, step, 0);
299 bitset_set(live, nr);
308 bitset_clear_all(live);
311 * Add initial defs for all values live in.
312 * Since their colors have already been assigned (The dominators were
313 * allocated before), we have to mark their colors as used also.
315 for(irn = pset_first(live_in); irn; irn = pset_next(live_in)) {
316 if(is_allocatable_irn(irn)) {
317 int col = get_irn_color(irn);
319 /* Mark the color of the live in value as used. */
320 assert(is_color(col) && "Node must have been assigned a color.");
321 bitset_set(colors, col);
322 bitset_set(used_colors, col);
324 /* Mark the value live in. */
325 bitset_set(live, get_irn_graph_nr(irn));
328 border_add(env, &head, irn, step, 1);
332 DBG((dbg, LEVEL_4, "usedef chain for block %n\n", block));
333 list_for_each_entry(border_t, b, &head, list) {
334 DBG((dbg, LEVEL_4, "\t%s %n %d\n", b->is_def ? "def" : "use", b->irn, get_irn_graph_nr(b->irn)));
338 * Mind that the sequence of defs from back to front defines a perfect
339 * elimination order. So, coloring the definitions from first to last
342 list_for_each_entry_reverse(border_t, b, &head, list) {
343 const ir_node *irn = b->irn;
344 int nr = get_irn_graph_nr(irn);
347 * Assign a color, if it is a local def. Global defs already have a
350 if(b->is_def && !is_live_in(block, irn)) {
351 ra_node_info_t *ri = get_ra_node_info(irn);
352 int col = bitset_next_clear(colors, 0);
354 assert(!is_color(get_irn_color(irn)) && "Color must not have assigned");
355 assert(!bitset_is_set(live, nr) && "Value def must not have been encountered");
357 bitset_set(colors, col);
358 bitset_set(used_colors, col);
359 bitset_set(live, nr);
362 ri->pressure = bitset_popcnt(colors);
364 DBG((dbg, LEVEL_1, "\tassigning color %d to %n\n", col, irn));
367 /* Clear the color upon a use. */
368 else if(!b->is_def) {
369 int col = get_irn_color(irn);
371 assert(bitset_is_set(live, nr) && "Cannot have a non live use");
372 assert(is_color(col) && "A color must have been assigned");
374 bitset_clear(colors, col);
375 bitset_clear(live, nr);
379 #ifdef DUMP_INTERVALS
380 draw_interval_graphs(block, &head, &dump_params);
388 ir_snprintf(buf, sizeof(buf), "pres_%s_bl_%N.txt",
389 get_entity_name(get_irg_entity(irg)), block);
391 if((f = fopen(buf, "wt")) != NULL) {
392 sched_foreach_reverse(block, irn) {
393 if(is_allocatable_irn(irn))
394 ir_fprintf(f, "\"%n\" %d %d\n", irn, sched_get_time_step(irn),
395 get_ra_node_info(irn)->pressure);
404 * Allocate the used colors array in the blocks ra info structure and
407 get_ra_block_info(block)->used_colors = used_colors;
409 /* Mark this block has processed. */
410 bitset_set(env->processed, block_nr);
412 /* Reset the obstack to its initial level */
413 obstack_free(obst, obstack_level);
416 void be_ra_chordal_init(void)
418 dbg = firm_dbg_register(DBG_BERA);
419 /* firm_dbg_set_mask(dbg, -1); */
422 void be_ra_chordal(ir_graph *irg)
424 int node_count = get_graph_node_count(irg);
425 env_t *env = malloc(sizeof(*env));
427 if(get_irg_dom_state(irg) != dom_consistent)
430 obstack_init(&env->obst);
431 env->phi_if = new_set(if_edge_cmp, node_count);
432 env->live = bitset_obstack_alloc(&env->obst, node_count);
433 env->processed = bitset_obstack_alloc(&env->obst, get_graph_block_count(irg));
434 env->colors = bitset_obstack_alloc(&env->obst, TEST_COLORS);
435 env->colors_n = TEST_COLORS;
437 irg_block_walk_graph(irg, block_alloc, NULL, env);
438 obstack_free(&env->obst, NULL);
440 set_irg_ra_link(irg, env);
443 void be_ra_chordal_done(ir_graph *irg)
445 env_t *env = get_irg_ra_link(irg);
450 int phi_ops_interfere(const ir_node *a, const ir_node *b)
452 ir_graph *irg = get_irn_irg(a);
453 env_t *env = get_irg_ra_link(irg);
455 assert(irg == get_irn_irg(b) && "Both nodes must be in the same graph");
457 return are_connected(env, get_irn_graph_nr(a), get_irn_graph_nr(b));