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"
40 #if defined(DUMP_IFG) && !defined(BUILD_GRAPH)
48 /* Make a fourcc for border checking. */
49 #define BORDER_FOURCC FOURCC('B', 'O', 'R', 'D')
51 #endif /* DEBUG_libfirm */
53 #define TEST_COLORS 2048
55 static firm_dbg_module_t *dbg;
58 * Environment for each of the chordal register allocator phases
60 typedef struct _env_t {
61 struct obstack obst; /**< An obstack for temporary storage. */
62 ir_graph *irg; /**< The graph the reg alloc is running on. */
64 set *nodes; /**< The interference graph nodes. */
65 set *edges; /**< The interference graph edges. */
68 bitset_t *live; /**< A liveness bitset. */
69 bitset_t *colors; /**< The color mask. */
70 bitset_t *in_colors; /**< Colors used by live in values. */
71 int colors_n; /**< The number of colors. */
72 const arch_isa_if_t *isa; /**< The isa interface. */
73 const arch_register_class_t *cls; /**< The current register class. */
74 void *data; /**< Some pointer, to which different
75 phases can attach data to. */
79 typedef struct _be_chordal_dump_params_t {
83 } be_chordal_dump_params_t;
85 static const be_chordal_dump_params_t dump_params = {
93 static INLINE void intv_filename(char *s, size_t n,
94 const env_t *env, ir_node *block)
96 ir_snprintf(s, n, "intv_%F_%s_bl%N.eps",
97 env->irg, env->cls->name, block);
100 static void draw_interval_graph(const env_t *env,
101 ir_node *block, const be_chordal_dump_params_t *params)
104 int x_dist = params->x_dist;
105 int y_dist = params->y_dist;
106 ir_graph *irg = env->irg;
107 struct list_head *border_head = get_block_border_head(block);
112 intv_filename(buf, sizeof(buf), env, block);
114 if((f = fopen(buf, "wt")) != NULL) {
116 int *seen = xcalloc(get_graph_node_count(irg), sizeof(seen[0]));
117 int last_pos = list_empty(border_head) ? 0 : list_entry(border_head->prev, border_t, list)->step;
120 list_for_each_entry_reverse(border_t, b, border_head, list) {
121 const ir_node *irn = b->irn;
122 int col = get_irn_color(irn);
123 max_col = max_col > col ? max_col : col;
126 fprintf(f, "%%!PS-Adobe-2.0\n");
127 fprintf(f, "%%%%BoundingBox: -10 -10 %d %d\n",
128 x_dist * last_pos + x_dist, y_dist * max_col + y_dist);
129 fprintf(f, "/mainfont /Courier findfont %f scalefont def\n", params->font_scale);
130 fprintf(f, "mainfont setfont\n");
131 fprintf(f, "0.2 setlinewidth\n");
133 for(i = 0; i <= last_pos; ++i) {
134 fprintf(f, "0 0 0 setrgbcolor\n");
135 fprintf(f, "%d %d moveto\n", i * x_dist, -2);
136 fprintf(f, "%d %d lineto\n", i * x_dist, max_col * y_dist + 2);
137 fprintf(f, "stroke\n");
139 fprintf(f, "0.5 setlinewidth\n");
141 list_for_each_entry_reverse(border_t, b, border_head, list) {
142 const ir_node *irn = b->irn;
143 int nr = get_irn_graph_nr(irn);
148 int col = get_irn_color(irn);
150 int pos = last_pos - seen[nr];
151 int end_pos = last_pos - b->step;
152 int live_in = is_live_in(block, irn);
153 int live_end = is_live_end(block, irn);
154 int y_val = y_dist * col;
157 int green = live_end;
160 fprintf(f, "0 0 0 setrgbcolor\n");
161 fprintf(f, "%d %d moveto\n", x_dist * pos + 2, y_val + 2);
162 ir_fprintf(f, "(%n/%d%s) show\n", irn, nr, is_phi_operand(irn) ? "*" : "");
163 fprintf(f, "%d %d %d setrgbcolor\n", red, green, blue);
164 fprintf(f, "%d %d moveto\n", x_dist * pos, y_val);
165 fprintf(f, "%d %d lineto\n", (x_dist * end_pos) - 5, y_val);
166 fprintf(f, "stroke\n");
175 static void dump_block(ir_node *bl, void *data)
177 const env_t *env = data;
181 draw_interval_graph(env, bl, &dump_params);
183 intv_filename(buf, sizeof(buf), env, bl);
184 ir_fprintf(f, "\tb%N [shape=\"epsf\" shapefile=\"%s\"];\n", bl, buf);
187 static void dump_edges(ir_node *bl, void *data)
189 const env_t *env = data;
195 for(i = 0, n = get_irn_arity(bl); i < n; ++i) {
196 ir_node *pred = get_irn_n(bl, i);
197 ir_fprintf(f, "\tb%N -> b%N;\n", get_nodes_block(pred), bl);
201 for(dom = get_Block_dominated_first(bl); dom;
202 dom = get_Block_dominated_next(dom)) {
204 ir_fprintf(f, "\tb%N -> b%N;\n", dom, bl);
208 static void dump_intv_cfg(env_t *env)
213 ir_snprintf(buf, sizeof(buf), "intv_cfg_%s_%F.dot",
214 env->cls->name, env->irg);
216 if((f = fopen(buf, "wt")) != NULL) {
217 void *old_data = env->data;
220 ir_fprintf(f, "digraph G {\n");
221 ir_fprintf(f, "\tgraph [rankdir=\"LR\", ordering=\"out\"];\n");
222 dom_tree_walk_irg(env->irg, dump_block, dump_edges, env);
223 // irg_block_walk_graph(env->irg, dump_block, dump_edges, env);
224 ir_fprintf(f, "}\n");
227 env->data = old_data;
235 #define IF_EDGE_HASH(e) ((e)->src)
236 #define IF_NODE_HASH(n) ((n)->nnr)
238 static int if_edge_cmp(const void *p1, const void *p2, size_t size)
240 const if_edge_t *e1 = p1;
241 const if_edge_t *e2 = p2;
243 return !(e1->src == e2->src && e1->tgt == e2->tgt);
246 static int if_node_cmp(const void *p1, const void *p2, size_t size)
248 const if_node_t *n1 = p1;
249 const if_node_t *n2 = p2;
251 return n1->nnr != n2->nnr;
254 static INLINE if_edge_t *edge_init(if_edge_t *edge, int src, int tgt)
256 /* Bring the smaller entry to src. */
268 static INLINE void add_if(const env_t *env, int src, int tgt)
271 if_node_t node, *src_node, *tgt_node;
273 edge_init(&edge, src, tgt);
274 set_insert(env->edges, &edge, sizeof(edge), IF_EDGE_HASH(&edge));
278 node.neighb = pset_new_ptr(8);
279 src_node = set_insert(env->nodes, &node, sizeof(node), IF_NODE_HASH(&node));
281 node.neighb = pset_new_ptr(8);
282 tgt_node = set_insert(env->nodes, &node, sizeof(node), IF_NODE_HASH(&node));
284 /* insert neighbors into nodes */
285 pset_insert_ptr(src_node->neighb, tgt_node);
286 pset_insert_ptr(tgt_node->neighb, src_node);
289 static INLINE int are_connected(const env_t *env, int src, int tgt)
292 edge_init(&edge, src, tgt);
293 return set_find(env->edges, &edge, sizeof(edge), IF_EDGE_HASH(&edge)) != NULL;
296 int ifg_has_edge(const ir_graph *irg, if_node_t *n1, if_node_t* n2) {
297 return are_connected(get_irg_ra_link(irg), n1->nnr, n2->nnr);
300 static void dump_ifg(ir_graph *irg, set *edges, const char *filename)
302 static const char *colors[] = {
382 static const int n_colors = sizeof(colors) / sizeof(colors[0]);
386 if((f = fopen(filename, "wt")) != NULL) {
390 bitset_t *bs = bitset_malloc(get_graph_node_count(irg));
392 ir_fprintf(f, "graph \"%F\" {\n", irg);
393 fprintf(f, "\tnode [shape=box,style=filled]\n");
395 for(edge = set_first(edges); edge; edge = set_next(edges)) {
396 bitset_set(bs, edge->src);
397 bitset_set(bs, edge->tgt);
401 fprintf(f, "\tx [label=\"nodes: %u, edges: %d\"]\n", bitset_popcnt(bs), n_edges);
403 bitset_foreach(bs, pos) {
405 ir_node *irn = get_irn_for_graph_nr(irg, nr);
406 int color = get_irn_color(irn);
408 ir_fprintf(f, "\tn%d [label=\"%n\",color=\"%s\"]\n", nr, irn,
409 color >= 0 && color < n_colors ? colors[color] : "black");
412 for(edge = set_first(edges); edge; edge = set_next(edges)) {
413 fprintf(f, "\tn%d -- n%d [len=5]\n", edge->src, edge->tgt);
424 #endif /* BUILD_GRAPH */
427 * Add an interval border to the list of a block's list
428 * of interval border.
429 * @note You always have to create the use before the def.
430 * @param env The environment.
431 * @param head The list head to enqueue the borders.
432 * @param irn The node (value) the border belongs to.
433 * @param pressure The pressure at this point in time.
434 * @param step A time step for the border.
435 * @param is_def Is the border a use or a def.
436 * @return The created border.
438 static INLINE border_t *border_add(env_t *env, struct list_head *head,
439 ir_node *irn, unsigned step, unsigned pressure,
440 unsigned is_def, unsigned is_real)
447 b = obstack_alloc(&env->obst, sizeof(*b));
449 /* also allocate the def and tie it to the use. */
450 def = obstack_alloc(&env->obst, sizeof(*def));
455 * Set the link field of the irn to the def.
456 * This strongly relies on the fact, that the use is always
457 * made before the def.
459 set_irn_link(irn, def);
462 b->magic = BORDER_FOURCC;
463 def->magic = BORDER_FOURCC;
468 * If the def is encountered, the use was made and so was the
469 * the def node (see the code above). It was placed into the
470 * link field of the irn, so we can get it there.
473 b = get_irn_link(irn);
476 assert(b && b->magic == BORDER_FOURCC && "Illegal border encountered");
480 b->pressure = pressure;
482 b->is_real = is_real;
485 list_add_tail(&b->list, head);
486 DBG((dbg, LEVEL_5, "\t\t%s adding %n, step: %d\n",
487 is_def ? "def" : "use", irn, step));
493 * Annotate the register pressure to the nodes and compute
494 * the liveness intervals.
495 * @param block The block to do it for.
496 * @param env_ptr The environment.
498 static void pressure(ir_node *block, void *env_ptr)
500 /* Convenience macro for a def */
501 #define border_def(irn, step, real) \
502 border_add(env, head, irn, step, pressure--, 1, real)
504 /* Convenience macro for a use */
505 #define border_use(irn, step, real) \
506 border_add(env, head, irn, step, ++pressure, 0, real)
508 env_t *env = env_ptr;
509 bitset_t *live = env->live;
514 unsigned pressure = 0;
515 struct list_head *head;
516 pset *live_in = get_live_in(block);
517 pset *live_end = get_live_end(block);
518 const arch_register_class_t *cls = env->cls;
519 const arch_isa_if_t *isa = env->isa;
521 DBG((dbg, LEVEL_1, "Computing pressure in block %n\n", block));
522 bitset_clear_all(live);
524 /* Set up the border list in the block info */
525 head = &get_ra_block_info(block)->border_head;
526 INIT_LIST_HEAD(head);
529 * Make final uses of all values live out of the block.
530 * They are neccessary to build up real intervals.
532 for(irn = pset_first(live_end); irn; irn = pset_next(live_end)) {
533 DBG((dbg, LEVEL_3, "\tMaking live: %n/%d\n", irn, get_irn_graph_nr(irn)));
534 bitset_set(live, get_irn_graph_nr(irn));
535 if(arch_isa_irn_has_reg_class(isa, irn, cls))
536 border_use(irn, step, 0);
542 * Determine the last uses of a value inside the block, since they are
543 * relevant for the interval borders.
545 sched_foreach_reverse(block, irn) {
546 DBG((dbg, LEVEL_1, "\tinsn: %n, pressure: %d\n", irn, pressure));
547 DBG((dbg, LEVEL_2, "\tlive: %b\n", live));
549 /* Erase the color of each node encountered. */
550 set_irn_color(irn, NO_COLOR);
553 * If the node defines some value, which can put into a
554 * register of the current class, make a border for it.
556 if(arch_isa_irn_has_reg_class(isa, irn, cls)) {
558 int nr = get_irn_graph_nr(irn);
560 bitset_clear(live, nr);
561 border_def(irn, step, 1);
564 bitset_foreach(live, elm)
565 add_if(env, nr, (int) elm);
570 * If the node is no phi node we can examine the uses.
573 for(i = 0, n = get_irn_arity(irn); i < n; ++i) {
574 ir_node *op = get_irn_n(irn, i);
576 if(arch_isa_irn_has_reg_class(isa, op, cls)) {
577 int nr = get_irn_graph_nr(op);
579 DBG((dbg, LEVEL_4, "\t\tpos: %d, use: %n\n", i, op));
581 if(!bitset_is_set(live, nr)) {
582 border_use(op, step, 1);
583 bitset_set(live, nr);
593 * Add initial defs for all values live in.
595 for(irn = pset_first(live_in); irn; irn = pset_next(live_in)) {
596 if(arch_isa_irn_has_reg_class(isa, irn, cls)) {
598 /* Mark the value live in. */
599 bitset_set(live, get_irn_graph_nr(irn));
602 border_def(irn, step, 0);
607 static void assign(ir_node *block, void *env_ptr)
609 env_t *env = env_ptr;
610 struct obstack *obst = &env->obst;
611 bitset_t *live = env->live;
612 bitset_t *colors = env->colors;
613 bitset_t *in_colors = env->in_colors;
614 const arch_isa_if_t *isa = env->isa;
615 const arch_register_class_t *cls = env->cls;
617 /* The used colors will remain on the obstack. */
618 bitset_t *used_colors = bitset_obstack_alloc(obst, env->colors_n);
620 /* Mark the obstack level and allocate the temporary tmp_colors */
621 void *obstack_level = obstack_base(obst);
622 /*bitset_t *tmp_colors = bitset_obstack_alloc(obst, env->colors_n);*/
626 struct list_head *head = &get_ra_block_info(block)->border_head;
627 pset *live_in = get_live_in(block);
629 bitset_clear_all(live);
630 bitset_clear_all(colors);
631 bitset_clear_all(in_colors);
633 DBG((dbg, LEVEL_4, "Assigning colors for block %n\n", block));
634 DBG((dbg, LEVEL_4, "\tusedef chain for block\n"));
635 list_for_each_entry(border_t, b, head, list) {
636 DBG((dbg, LEVEL_4, "\t%s %n/%d\n", b->is_def ? "def" : "use",
637 b->irn, get_irn_graph_nr(b->irn)));
641 * Add initial defs for all values live in.
642 * Since their colors have already been assigned (The dominators were
643 * allocated before), we have to mark their colors as used also.
645 for(irn = pset_first(live_in); irn; irn = pset_next(live_in)) {
646 if(arch_isa_irn_has_reg_class(isa, irn, cls)) {
647 int col = get_irn_color(irn);
649 /* Mark the color of the live in value as used. */
650 assert(is_color(col) && "Node must have been assigned a color.");
651 bitset_set(colors, col);
652 bitset_set(in_colors, col);
653 bitset_set(used_colors, col);
655 /* Mark the value live in. */
656 bitset_set(live, get_irn_graph_nr(irn));
661 * Mind that the sequence of defs from back to front defines a perfect
662 * elimination order. So, coloring the definitions from first to last
665 list_for_each_entry_reverse(border_t, b, head, list) {
666 const ir_node *irn = b->irn;
667 int nr = get_irn_graph_nr(irn);
670 * Assign a color, if it is a local def. Global defs already have a
673 if(b->is_def && !is_live_in(block, irn)) {
674 ra_node_info_t *ri = get_ra_node_info(irn);
677 DBG((dbg, LEVEL_4, "\tcolors in use: %b\n", colors));
680 * Try to assign live out values colors which are not used by live
684 if(is_live_out(block, irn)) {
687 bitset_copy(tmp_colors, colors);
688 bitset_or(tmp_colors, in_colors);
689 next_clear = bitset_next_clear(tmp_colors, 0);
690 col = next_clear != -1 ? next_clear : NO_COLOR;
692 DBG((dbg, LEVEL_5, "next clear in only outs %b: %d\n", tmp_colors, col));
696 /* If a color is not yet assigned, do it now. */
698 col = bitset_next_clear(colors, 0);
700 assert(!is_color(get_irn_color(irn)) && "Color must not have assigned");
701 assert(!bitset_is_set(live, nr) && "Value def must not have been encountered");
703 bitset_set(colors, col);
704 bitset_set(used_colors, col);
705 bitset_set(live, nr);
709 DBG((dbg, LEVEL_1, "\tassigning color %d to %n\n", col, irn));
712 /* Clear the color upon a use. */
713 else if(!b->is_def) {
714 int col = get_irn_color(irn);
716 assert(bitset_is_set(live, nr) && "Cannot have a non live use");
717 assert(is_color(col) && "A color must have been assigned");
719 bitset_clear(colors, col);
720 bitset_clear(live, nr);
729 ir_snprintf(buf, sizeof(buf), "pres_%s_bl_%N.txt",
730 get_entity_name(get_irg_entity(irg)), block);
732 if((f = fopen(buf, "wt")) != NULL) {
733 sched_foreach_reverse(block, irn) {
734 if(arch_isa_irn_has_reg_class(isa, irn, cls))
735 ir_fprintf(f, "\"%n\" %d %d\n", irn, sched_get_time_step(irn),
736 get_ra_node_info(irn)->pressure);
746 * Allocate the used colors array in the blocks ra info structure and
749 get_ra_block_info(block)->used_colors = used_colors;
751 /* Free the auxillary data on the obstack. */
752 obstack_free(obst, obstack_level);
755 void be_ra_chordal_init(void)
757 dbg = firm_dbg_register(DBG_BERA);
758 firm_dbg_set_mask(dbg, 0);
761 void be_ra_chordal(ir_graph *irg,
762 const arch_isa_if_t *isa,
763 const arch_register_class_t *cls)
765 int node_count = get_graph_node_count(irg);
766 int colors_n = arch_register_class_n_regs(cls);
767 env_t *env = malloc(sizeof(*env));
769 if(get_irg_dom_state(irg) != dom_consistent)
772 obstack_init(&env->obst);
775 env->edges = new_set(if_edge_cmp, node_count);
776 env->nodes = new_set(if_node_cmp, node_count);
779 env->live = bitset_obstack_alloc(&env->obst, node_count);
780 env->colors = bitset_obstack_alloc(&env->obst, colors_n);
781 env->in_colors = bitset_obstack_alloc(&env->obst, colors_n);
782 env->colors_n = colors_n;
787 /* First, determine the pressure */
788 dom_tree_walk_irg(irg, pressure, NULL, env);
790 /* Insert probable spills */
791 be_ra_chordal_spill(irg);
793 /* Assign the colors */
794 dom_tree_walk_irg(irg, assign, NULL, env);
800 ir_snprintf(buf, sizeof(buf), "ifg_%F.dot", irg);
801 dump_ifg(irg, env->edges, buf);
805 #ifdef DUMP_INTERVALS
809 set_irg_ra_link(irg, env);
812 void be_ra_chordal_done(ir_graph *irg)
814 env_t *env = get_irg_ra_link(irg);
819 for(ifn = set_first(env->nodes); ifn; ifn = set_next(env->nodes))
826 obstack_free(&env->obst, NULL);
830 int phi_ops_interfere(const ir_node *a, const ir_node *b)
833 ir_graph *irg = get_irn_irg(a);
834 env_t *env = get_irg_ra_link(irg);
836 assert(irg == get_irn_irg(b) && "Both nodes must be in the same graph");
838 return are_connected(env, get_irn_graph_nr(a), get_irn_graph_nr(b));
840 return values_interfere(a, b);
841 #endif /* BUILD_GRAPH */
846 * Deprecated. Use be_ra_get_ifg_edges instead.
848 set *be_ra_get_ifg(ir_graph *irg) {
849 return ((env_t *)get_irg_ra_link(irg))->edges;
852 set *be_ra_get_ifg_edges(ir_graph *irg) {
853 return ((env_t *)get_irg_ra_link(irg))->edges;
856 set *be_ra_get_ifg_nodes(ir_graph *irg) {
857 return ((env_t *)get_irg_ra_link(irg))->nodes;