2 * Chordal register allocation.
3 * @author Sebastian Hack
18 #include "irgraph_t.h"
19 #include "irprintf_t.h"
30 #include "besched_t.h"
32 #include "bechordal_t.h"
43 /* Make a fourcc for border checking. */
44 #define BORDER_FOURCC FOURCC('B', 'O', 'R', 'D')
46 #endif /* DEBUG_libfirm */
48 #define TEST_COLORS 2048
50 static firm_dbg_module_t *dbg;
53 * Environment for each of the chordal register allocator phases
55 typedef struct _env_t {
56 struct obstack obst; /**< An obstack for temporary storage. */
58 set *graph; /**< The interference graph. */
61 bitset_t *live; /**< A liveness bitset. */
62 bitset_t *colors; /**< The color mask. */
63 bitset_t *in_colors; /**< Colors used by live in values. */
64 int colors_n; /**< The number of colors. */
65 const arch_isa_if_t *isa; /**< The isa interface. */
66 const arch_register_class_t *cls; /**< The current register class. */
70 typedef struct _be_chordal_dump_params_t {
74 } be_chordal_dump_params_t;
76 static const be_chordal_dump_params_t dump_params = {
83 static void draw_interval_graphs(ir_node *block,
84 struct list_head *border_head,
85 const be_chordal_dump_params_t *params)
88 int x_dist = params->x_dist;
89 int y_dist = params->y_dist;
90 ir_graph *irg = get_irn_irg(block);
95 ir_snprintf(buf, sizeof(buf), "intv_%s_bl%N.eps",
96 get_entity_name(get_irg_entity(irg)), block);
98 if((f = fopen(buf, "wt")) != NULL) {
100 int *seen = xcalloc(get_graph_node_count(irg), sizeof(seen[0]));
101 int last_pos = list_empty(border_head) ? 0 : list_entry(border_head->prev, border_t, list)->step;
104 list_for_each_entry_reverse(border_t, b, border_head, list) {
105 const ir_node *irn = b->irn;
106 int col = get_irn_color(irn);
107 max_col = max_col > col ? max_col : col;
110 fprintf(f, "%%!PS-Adobe-2.0\n");
111 fprintf(f, "%%%%BoundingBox: -10 -10 %d %d\n",
112 x_dist * last_pos + x_dist, y_dist * max_col + y_dist);
113 fprintf(f, "/mainfont /Courier findfont %f scalefont def\n", params->font_scale);
114 fprintf(f, "mainfont setfont\n");
115 fprintf(f, "0.2 setlinewidth\n");
117 for(i = 0; i <= last_pos; ++i) {
118 fprintf(f, "0 0 0 setrgbcolor\n");
119 fprintf(f, "%d %d moveto\n", i * x_dist, -2);
120 fprintf(f, "%d %d lineto\n", i * x_dist, max_col * y_dist + 2);
121 fprintf(f, "stroke\n");
123 fprintf(f, "0.5 setlinewidth\n");
125 list_for_each_entry_reverse(border_t, b, border_head, list) {
126 const ir_node *irn = b->irn;
127 int nr = get_irn_graph_nr(irn);
132 int col = get_irn_color(irn);
134 int pos = last_pos - seen[nr];
135 int end_pos = last_pos - b->step;
136 int live_in = is_live_in(block, irn);
137 int live_end = is_live_end(block, irn);
138 int y_val = y_dist * col;
141 int green = live_end;
144 fprintf(f, "0 0 0 setrgbcolor\n");
145 fprintf(f, "%d %d moveto\n", x_dist * pos + 2, y_val + 2);
146 ir_fprintf(f, "(%n/%d%s) show\n", irn, nr, is_phi_operand(irn) ? "*" : "");
147 fprintf(f, "%d %d %d setrgbcolor\n", red, green, blue);
148 fprintf(f, "%d %d moveto\n", x_dist * pos, y_val);
149 fprintf(f, "%d %d lineto\n", (x_dist * end_pos) - 5, y_val);
150 fprintf(f, "stroke\n");
162 #define IF_EDGE_HASH(e) ((e)->src)
164 static int if_edge_cmp(const void *p1, const void *p2, size_t size)
166 const if_edge_t *e1 = p1;
167 const if_edge_t *e2 = p2;
169 return !(e1->src == e2->src && e1->tgt == e2->tgt);
172 static INLINE if_edge_t *edge_init(if_edge_t *edge, int src, int tgt)
174 /* Bring the smaller entry to src. */
186 static INLINE void add_if(const env_t *env, int src, int tgt)
189 edge_init(&edge, src, tgt);
190 set_insert(env->graph, &edge, sizeof(edge), IF_EDGE_HASH(&edge));
193 static INLINE int are_connected(const env_t *env, int src, int tgt)
196 edge_init(&edge, src, tgt);
197 return set_find(env->graph, &edge, sizeof(edge), IF_EDGE_HASH(&edge)) != NULL;
200 static void dump_ifg(ir_graph *irg, set *edges, const char *filename)
202 static const char *colors[] = {
282 static const int n_colors = sizeof(colors) / sizeof(colors[0]);
286 if((f = fopen(filename, "wt")) != NULL) {
290 bitset_t *bs = bitset_malloc(get_graph_node_count(irg));
292 ir_fprintf(f, "graph \"%F\" {\n", irg);
293 fprintf(f, "\tnode [shape=box,style=filled]\n");
295 for(edge = set_first(edges); edge; edge = set_next(edges)) {
296 bitset_set(bs, edge->src);
297 bitset_set(bs, edge->tgt);
301 fprintf(f, "\tx [label=\"nodes: %u, edges: %d\"]\n", bitset_popcnt(bs), n_edges);
303 bitset_foreach(bs, pos) {
305 ir_node *irn = get_irn_for_graph_nr(irg, nr);
306 int color = get_irn_color(irn);
308 ir_fprintf(f, "\tn%d [label=\"%n\",color=\"%s\"]\n", nr, irn,
309 color >= 0 && color < n_colors ? colors[color] : "black");
312 for(edge = set_first(edges); edge; edge = set_next(edges)) {
313 fprintf(f, "\tn%d -- n%d [len=5]\n", edge->src, edge->tgt);
324 #endif /* BUILD_GRAPH */
327 * Add an interval border to the list of a block's list
328 * of interval border.
329 * @note You always have to create the use before the def.
330 * @param env The environment.
331 * @param head The list head to enqueue the borders.
332 * @param irn The node (value) the border belongs to.
333 * @param pressure The pressure at this point in time.
334 * @param step A time step for the border.
335 * @param is_def Is the border a use or a def.
336 * @return The created border.
338 static INLINE border_t *border_add(env_t *env, struct list_head *head,
339 ir_node *irn, unsigned step, unsigned pressure,
340 unsigned is_def, unsigned is_real)
347 b = obstack_alloc(&env->obst, sizeof(*b));
349 /* also allocate the def and tie it to the use. */
350 def = obstack_alloc(&env->obst, sizeof(*def));
355 * Set the link field of the irn to the def.
356 * This strongly relies on the fact, that the use is always
357 * made before the def.
359 set_irn_link(irn, def);
362 b->magic = BORDER_FOURCC;
363 def->magic = BORDER_FOURCC;
368 * If the def is encountered, the use was made and so was the
369 * the def node (see the code above). It was placed into the
370 * link field of the irn, so we can get it there.
373 b = get_irn_link(irn);
376 assert(b && b->magic == BORDER_FOURCC && "Illegal border encountered");
380 b->pressure = pressure;
382 b->is_real = is_real;
385 list_add_tail(&b->list, head);
386 DBG((dbg, LEVEL_5, "\t\t%s adding %n, step: %d\n",
387 is_def ? "def" : "use", irn, step));
393 * Annotate the register pressure to the nodes and compute
394 * the liveness intervals.
395 * @param block The block to do it for.
396 * @param env_ptr The environment.
398 static void pressure(ir_node *block, void *env_ptr)
400 /* Convenience macro for a def */
401 #define border_def(irn, step, real) \
402 border_add(env, head, irn, step, pressure--, 1, real)
404 /* Convenience macro for a use */
405 #define border_use(irn, step, real) \
406 border_add(env, head, irn, step, ++pressure, 0, real)
408 env_t *env = env_ptr;
409 bitset_t *live = env->live;
414 unsigned pressure = 0;
415 struct list_head *head;
416 pset *live_in = get_live_in(block);
417 pset *live_end = get_live_end(block);
418 const arch_register_class_t *cls = env->cls;
419 const arch_isa_if_t *isa = env->isa;
421 DBG((dbg, LEVEL_1, "Computing pressure in block %n\n", block));
422 bitset_clear_all(live);
424 /* Set up the border list in the block info */
425 head = &get_ra_block_info(block)->border_head;
426 INIT_LIST_HEAD(head);
429 * Make final uses of all values live out of the block.
430 * They are neccessary to build up real intervals.
432 for(irn = pset_first(live_end); irn; irn = pset_next(live_end)) {
433 DBG((dbg, LEVEL_3, "\tMaking live: %n/%d\n", irn, get_irn_graph_nr(irn)));
434 bitset_set(live, get_irn_graph_nr(irn));
435 if(arch_isa_irn_has_reg_class(isa, irn, cls))
436 border_use(irn, step, 0);
442 * Determine the last uses of a value inside the block, since they are
443 * relevant for the interval borders.
445 sched_foreach_reverse(block, irn) {
446 DBG((dbg, LEVEL_1, "\tinsn: %n, pressure: %d\n", irn, pressure));
447 DBG((dbg, LEVEL_2, "\tlive: %b\n", live));
449 /* Erase the color of each node encountered. */
450 set_irn_color(irn, NO_COLOR);
453 * If the node defines some value, which can put into a
454 * register of the current class, make a border for it.
456 if(arch_isa_irn_has_reg_class(isa, irn, cls)) {
458 int nr = get_irn_graph_nr(irn);
460 bitset_clear(live, nr);
461 border_def(irn, step, 1);
464 bitset_foreach(live, elm)
465 add_if(env, nr, (int) elm);
470 * If the node is no phi node we can examine the uses.
473 for(i = 0, n = get_irn_arity(irn); i < n; ++i) {
474 ir_node *op = get_irn_n(irn, i);
476 if(arch_isa_irn_has_reg_class(isa, op, cls)) {
477 int nr = get_irn_graph_nr(op);
479 DBG((dbg, LEVEL_4, "\t\tpos: %d, use: %n\n", i, op));
481 if(!bitset_is_set(live, nr)) {
482 border_use(op, step, 1);
483 bitset_set(live, nr);
493 * Add initial defs for all values live in.
495 for(irn = pset_first(live_in); irn; irn = pset_next(live_in)) {
496 if(arch_isa_irn_has_reg_class(isa, irn, cls)) {
498 /* Mark the value live in. */
499 bitset_set(live, get_irn_graph_nr(irn));
502 border_def(irn, step, 0);
507 static void assign(ir_node *block, void *env_ptr)
509 env_t *env = env_ptr;
510 struct obstack *obst = &env->obst;
511 bitset_t *live = env->live;
512 bitset_t *colors = env->colors;
513 bitset_t *in_colors = env->in_colors;
514 const arch_isa_if_t *isa = env->isa;
515 const arch_register_class_t *cls = env->cls;
517 /* The used colors will remain on the obstack. */
518 bitset_t *used_colors = bitset_obstack_alloc(obst, env->colors_n);
520 /* Mark the obstack level and allocate the temporary tmp_colors */
521 void *obstack_level = obstack_base(obst);
522 /*bitset_t *tmp_colors = bitset_obstack_alloc(obst, env->colors_n);*/
526 struct list_head *head = &get_ra_block_info(block)->border_head;
527 pset *live_in = get_live_in(block);
529 bitset_clear_all(live);
530 bitset_clear_all(colors);
531 bitset_clear_all(in_colors);
533 DBG((dbg, LEVEL_4, "Assigning colors for block %n\n", block));
534 DBG((dbg, LEVEL_4, "\tusedef chain for block\n"));
535 list_for_each_entry(border_t, b, head, list) {
536 DBG((dbg, LEVEL_4, "\t%s %n/%d\n", b->is_def ? "def" : "use",
537 b->irn, get_irn_graph_nr(b->irn)));
541 * Add initial defs for all values live in.
542 * Since their colors have already been assigned (The dominators were
543 * allocated before), we have to mark their colors as used also.
545 for(irn = pset_first(live_in); irn; irn = pset_next(live_in)) {
546 if(arch_isa_irn_has_reg_class(isa, irn, cls)) {
547 int col = get_irn_color(irn);
549 /* Mark the color of the live in value as used. */
550 assert(is_color(col) && "Node must have been assigned a color.");
551 bitset_set(colors, col);
552 bitset_set(in_colors, col);
553 bitset_set(used_colors, col);
555 /* Mark the value live in. */
556 bitset_set(live, get_irn_graph_nr(irn));
561 * Mind that the sequence of defs from back to front defines a perfect
562 * elimination order. So, coloring the definitions from first to last
565 list_for_each_entry_reverse(border_t, b, head, list) {
566 const ir_node *irn = b->irn;
567 int nr = get_irn_graph_nr(irn);
570 * Assign a color, if it is a local def. Global defs already have a
573 if(b->is_def && !is_live_in(block, irn)) {
574 ra_node_info_t *ri = get_ra_node_info(irn);
577 DBG((dbg, LEVEL_4, "\tcolors in use: %b\n", colors));
580 * Try to assign live out values colors which are not used by live
584 if(is_live_out(block, irn)) {
587 bitset_copy(tmp_colors, colors);
588 bitset_or(tmp_colors, in_colors);
589 next_clear = bitset_next_clear(tmp_colors, 0);
590 col = next_clear != -1 ? next_clear : NO_COLOR;
592 DBG((dbg, LEVEL_5, "next clear in only outs %b: %d\n", tmp_colors, col));
596 /* If a color is not yet assigned, do it now. */
598 col = bitset_next_clear(colors, 0);
600 assert(!is_color(get_irn_color(irn)) && "Color must not have assigned");
601 assert(!bitset_is_set(live, nr) && "Value def must not have been encountered");
603 bitset_set(colors, col);
604 bitset_set(used_colors, col);
605 bitset_set(live, nr);
609 DBG((dbg, LEVEL_1, "\tassigning color %d to %n\n", col, irn));
612 /* Clear the color upon a use. */
613 else if(!b->is_def) {
614 int col = get_irn_color(irn);
616 assert(bitset_is_set(live, nr) && "Cannot have a non live use");
617 assert(is_color(col) && "A color must have been assigned");
619 bitset_clear(colors, col);
620 bitset_clear(live, nr);
624 #ifdef DUMP_INTERVALS
625 draw_interval_graphs(block, head, &dump_params);
633 ir_snprintf(buf, sizeof(buf), "pres_%s_bl_%N.txt",
634 get_entity_name(get_irg_entity(irg)), block);
636 if((f = fopen(buf, "wt")) != NULL) {
637 sched_foreach_reverse(block, irn) {
638 if(arch_isa_irn_has_reg_class(isa, irn, cls))
639 ir_fprintf(f, "\"%n\" %d %d\n", irn, sched_get_time_step(irn),
640 get_ra_node_info(irn)->pressure);
650 * Allocate the used colors array in the blocks ra info structure and
653 get_ra_block_info(block)->used_colors = used_colors;
655 /* Free the auxillary data on the obstack. */
656 obstack_free(obst, obstack_level);
659 void be_ra_chordal_init(void)
661 dbg = firm_dbg_register(DBG_BERA);
662 firm_dbg_set_mask(dbg, 0);
665 void be_ra_chordal(ir_graph *irg,
666 const arch_isa_if_t *isa,
667 const arch_register_class_t *cls)
669 int node_count = get_graph_node_count(irg);
670 int colors_n = arch_register_class_n_regs(cls);
671 env_t *env = malloc(sizeof(*env));
673 if(get_irg_dom_state(irg) != dom_consistent)
676 obstack_init(&env->obst);
679 env->graph = new_set(if_edge_cmp, node_count);
682 env->live = bitset_obstack_alloc(&env->obst, node_count);
683 env->colors = bitset_obstack_alloc(&env->obst, colors_n);
684 env->in_colors = bitset_obstack_alloc(&env->obst, colors_n);
685 env->colors_n = colors_n;
689 /* First, determine the pressure */
690 dom_tree_walk_irg(irg, pressure, NULL, env);
692 /* Insert probable spills */
693 be_ra_chordal_spill(irg);
695 /* Assign the colors */
696 dom_tree_walk_irg(irg, assign, NULL, env);
702 ir_snprintf(buf, sizeof(buf), "ifg_%s.dot", get_entity_name(get_irg_entity(irg)));
703 dump_ifg(irg, env->graph, buf);
707 set_irg_ra_link(irg, env);
710 void be_ra_chordal_done(ir_graph *irg)
712 env_t *env = get_irg_ra_link(irg);
718 obstack_free(&env->obst, NULL);
722 int phi_ops_interfere(const ir_node *a, const ir_node *b)
725 ir_graph *irg = get_irn_irg(a);
726 env_t *env = get_irg_ra_link(irg);
728 assert(irg == get_irn_irg(b) && "Both nodes must be in the same graph");
730 return are_connected(env, get_irn_graph_nr(a), get_irn_graph_nr(b));
732 return values_interfere(a, b);
733 #endif /* BUILD_GRAPH */
737 set *be_ra_get_ifg(ir_graph *irg) {
738 return ((env_t *)get_irg_ra_link(irg))->graph;