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"
36 #define DUMP_INTERVALS
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 *graph; /**< The interference graph. */
67 bitset_t *live; /**< A liveness bitset. */
68 bitset_t *colors; /**< The color mask. */
69 bitset_t *in_colors; /**< Colors used by live in values. */
70 int colors_n; /**< The number of colors. */
71 const arch_isa_if_t *isa; /**< The isa interface. */
72 const arch_register_class_t *cls; /**< The current register class. */
73 void *data; /**< Some pointer, to which different
74 phases can attach data to. */
78 typedef struct _be_chordal_dump_params_t {
82 } be_chordal_dump_params_t;
84 static const be_chordal_dump_params_t dump_params = {
92 static INLINE void intv_filename(char *s, size_t n,
93 const env_t *env, ir_node *block)
95 ir_snprintf(s, n, "intv_%F_%s_bl%N.eps",
96 env->irg, env->cls->name, block);
99 static void draw_interval_graph(const env_t *env,
100 ir_node *block, const be_chordal_dump_params_t *params)
103 int x_dist = params->x_dist;
104 int y_dist = params->y_dist;
105 ir_graph *irg = env->irg;
106 struct list_head *border_head = get_block_border_head(block);
111 intv_filename(buf, sizeof(buf), env, block);
113 if((f = fopen(buf, "wt")) != NULL) {
115 int *seen = xcalloc(get_graph_node_count(irg), sizeof(seen[0]));
116 int last_pos = list_empty(border_head) ? 0 : list_entry(border_head->prev, border_t, list)->step;
119 list_for_each_entry_reverse(border_t, b, border_head, list) {
120 const ir_node *irn = b->irn;
121 int col = get_irn_color(irn);
122 max_col = max_col > col ? max_col : col;
125 fprintf(f, "%%!PS-Adobe-2.0\n");
126 fprintf(f, "%%%%BoundingBox: -10 -10 %d %d\n",
127 x_dist * last_pos + x_dist, y_dist * max_col + y_dist);
128 fprintf(f, "/mainfont /Courier findfont %f scalefont def\n", params->font_scale);
129 fprintf(f, "mainfont setfont\n");
130 fprintf(f, "0.2 setlinewidth\n");
132 for(i = 0; i <= last_pos; ++i) {
133 fprintf(f, "0 0 0 setrgbcolor\n");
134 fprintf(f, "%d %d moveto\n", i * x_dist, -2);
135 fprintf(f, "%d %d lineto\n", i * x_dist, max_col * y_dist + 2);
136 fprintf(f, "stroke\n");
138 fprintf(f, "0.5 setlinewidth\n");
140 list_for_each_entry_reverse(border_t, b, border_head, list) {
141 const ir_node *irn = b->irn;
142 int nr = get_irn_graph_nr(irn);
147 int col = get_irn_color(irn);
149 int pos = last_pos - seen[nr];
150 int end_pos = last_pos - b->step;
151 int live_in = is_live_in(block, irn);
152 int live_end = is_live_end(block, irn);
153 int y_val = y_dist * col;
156 int green = live_end;
159 fprintf(f, "0 0 0 setrgbcolor\n");
160 fprintf(f, "%d %d moveto\n", x_dist * pos + 2, y_val + 2);
161 ir_fprintf(f, "(%n/%d%s) show\n", irn, nr, is_phi_operand(irn) ? "*" : "");
162 fprintf(f, "%d %d %d setrgbcolor\n", red, green, blue);
163 fprintf(f, "%d %d moveto\n", x_dist * pos, y_val);
164 fprintf(f, "%d %d lineto\n", (x_dist * end_pos) - 5, y_val);
165 fprintf(f, "stroke\n");
174 static void dump_block(ir_node *bl, void *data)
176 const env_t *env = data;
180 draw_interval_graph(env, bl, &dump_params);
182 intv_filename(buf, sizeof(buf), env, bl);
183 ir_fprintf(f, "\tb%N [shape=\"epsf\" shapefile=\"%s\"];\n", bl, buf);
186 static void dump_edges(ir_node *bl, void *data)
188 const env_t *env = data;
194 for(i = 0, n = get_irn_arity(bl); i < n; ++i) {
195 ir_node *pred = get_irn_n(bl, i);
196 ir_fprintf(f, "\tb%N -> b%N;\n", get_nodes_block(pred), bl);
200 for(dom = get_Block_dominated_first(bl); dom;
201 dom = get_Block_dominated_next(dom)) {
203 ir_fprintf(f, "\tb%N -> b%N;\n", dom, bl);
207 static void dump_intv_cfg(env_t *env)
212 ir_snprintf(buf, sizeof(buf), "intv_cfg_%s_%F.dot",
213 env->cls->name, env->irg);
215 if((f = fopen(buf, "wt")) != NULL) {
216 void *old_data = env->data;
219 ir_fprintf(f, "digraph G {\n");
220 ir_fprintf(f, "\tgraph [rankdir=\"LR\", ordering=\"out\"];\n");
221 dom_tree_walk_irg(env->irg, dump_block, dump_edges, env);
222 // irg_block_walk_graph(env->irg, dump_block, dump_edges, env);
223 ir_fprintf(f, "}\n");
226 env->data = old_data;
234 #define IF_EDGE_HASH(e) ((e)->src)
236 static int if_edge_cmp(const void *p1, const void *p2, size_t size)
238 const if_edge_t *e1 = p1;
239 const if_edge_t *e2 = p2;
241 return !(e1->src == e2->src && e1->tgt == e2->tgt);
244 static INLINE if_edge_t *edge_init(if_edge_t *edge, int src, int tgt)
246 /* Bring the smaller entry to src. */
258 static INLINE void add_if(const env_t *env, int src, int tgt)
261 edge_init(&edge, src, tgt);
262 set_insert(env->graph, &edge, sizeof(edge), IF_EDGE_HASH(&edge));
265 static INLINE int are_connected(const env_t *env, int src, int tgt)
268 edge_init(&edge, src, tgt);
269 return set_find(env->graph, &edge, sizeof(edge), IF_EDGE_HASH(&edge)) != NULL;
272 static void dump_ifg(ir_graph *irg, set *edges, const char *filename)
274 static const char *colors[] = {
354 static const int n_colors = sizeof(colors) / sizeof(colors[0]);
358 if((f = fopen(filename, "wt")) != NULL) {
362 bitset_t *bs = bitset_malloc(get_graph_node_count(irg));
364 ir_fprintf(f, "graph \"%F\" {\n", irg);
365 fprintf(f, "\tnode [shape=box,style=filled]\n");
367 for(edge = set_first(edges); edge; edge = set_next(edges)) {
368 bitset_set(bs, edge->src);
369 bitset_set(bs, edge->tgt);
373 fprintf(f, "\tx [label=\"nodes: %u, edges: %d\"]\n", bitset_popcnt(bs), n_edges);
375 bitset_foreach(bs, pos) {
377 ir_node *irn = get_irn_for_graph_nr(irg, nr);
378 int color = get_irn_color(irn);
380 ir_fprintf(f, "\tn%d [label=\"%n\",color=\"%s\"]\n", nr, irn,
381 color >= 0 && color < n_colors ? colors[color] : "black");
384 for(edge = set_first(edges); edge; edge = set_next(edges)) {
385 fprintf(f, "\tn%d -- n%d [len=5]\n", edge->src, edge->tgt);
396 #endif /* BUILD_GRAPH */
399 * Add an interval border to the list of a block's list
400 * of interval border.
401 * @note You always have to create the use before the def.
402 * @param env The environment.
403 * @param head The list head to enqueue the borders.
404 * @param irn The node (value) the border belongs to.
405 * @param pressure The pressure at this point in time.
406 * @param step A time step for the border.
407 * @param is_def Is the border a use or a def.
408 * @return The created border.
410 static INLINE border_t *border_add(env_t *env, struct list_head *head,
411 ir_node *irn, unsigned step, unsigned pressure,
412 unsigned is_def, unsigned is_real)
419 b = obstack_alloc(&env->obst, sizeof(*b));
421 /* also allocate the def and tie it to the use. */
422 def = obstack_alloc(&env->obst, sizeof(*def));
427 * Set the link field of the irn to the def.
428 * This strongly relies on the fact, that the use is always
429 * made before the def.
431 set_irn_link(irn, def);
434 b->magic = BORDER_FOURCC;
435 def->magic = BORDER_FOURCC;
440 * If the def is encountered, the use was made and so was the
441 * the def node (see the code above). It was placed into the
442 * link field of the irn, so we can get it there.
445 b = get_irn_link(irn);
448 assert(b && b->magic == BORDER_FOURCC && "Illegal border encountered");
452 b->pressure = pressure;
454 b->is_real = is_real;
457 list_add_tail(&b->list, head);
458 DBG((dbg, LEVEL_5, "\t\t%s adding %n, step: %d\n",
459 is_def ? "def" : "use", irn, step));
465 * Annotate the register pressure to the nodes and compute
466 * the liveness intervals.
467 * @param block The block to do it for.
468 * @param env_ptr The environment.
470 static void pressure(ir_node *block, void *env_ptr)
472 /* Convenience macro for a def */
473 #define border_def(irn, step, real) \
474 border_add(env, head, irn, step, pressure--, 1, real)
476 /* Convenience macro for a use */
477 #define border_use(irn, step, real) \
478 border_add(env, head, irn, step, ++pressure, 0, real)
480 env_t *env = env_ptr;
481 bitset_t *live = env->live;
486 unsigned pressure = 0;
487 struct list_head *head;
488 pset *live_in = get_live_in(block);
489 pset *live_end = get_live_end(block);
490 const arch_register_class_t *cls = env->cls;
491 const arch_isa_if_t *isa = env->isa;
493 DBG((dbg, LEVEL_1, "Computing pressure in block %n\n", block));
494 bitset_clear_all(live);
496 /* Set up the border list in the block info */
497 head = &get_ra_block_info(block)->border_head;
498 INIT_LIST_HEAD(head);
501 * Make final uses of all values live out of the block.
502 * They are neccessary to build up real intervals.
504 for(irn = pset_first(live_end); irn; irn = pset_next(live_end)) {
505 DBG((dbg, LEVEL_3, "\tMaking live: %n/%d\n", irn, get_irn_graph_nr(irn)));
506 bitset_set(live, get_irn_graph_nr(irn));
507 if(arch_isa_irn_has_reg_class(isa, irn, cls))
508 border_use(irn, step, 0);
514 * Determine the last uses of a value inside the block, since they are
515 * relevant for the interval borders.
517 sched_foreach_reverse(block, irn) {
518 DBG((dbg, LEVEL_1, "\tinsn: %n, pressure: %d\n", irn, pressure));
519 DBG((dbg, LEVEL_2, "\tlive: %b\n", live));
521 /* Erase the color of each node encountered. */
522 set_irn_color(irn, NO_COLOR);
525 * If the node defines some value, which can put into a
526 * register of the current class, make a border for it.
528 if(arch_isa_irn_has_reg_class(isa, irn, cls)) {
530 int nr = get_irn_graph_nr(irn);
532 bitset_clear(live, nr);
533 border_def(irn, step, 1);
536 bitset_foreach(live, elm)
537 add_if(env, nr, (int) elm);
542 * If the node is no phi node we can examine the uses.
545 for(i = 0, n = get_irn_arity(irn); i < n; ++i) {
546 ir_node *op = get_irn_n(irn, i);
548 if(arch_isa_irn_has_reg_class(isa, op, cls)) {
549 int nr = get_irn_graph_nr(op);
551 DBG((dbg, LEVEL_4, "\t\tpos: %d, use: %n\n", i, op));
553 if(!bitset_is_set(live, nr)) {
554 border_use(op, step, 1);
555 bitset_set(live, nr);
565 * Add initial defs for all values live in.
567 for(irn = pset_first(live_in); irn; irn = pset_next(live_in)) {
568 if(arch_isa_irn_has_reg_class(isa, irn, cls)) {
570 /* Mark the value live in. */
571 bitset_set(live, get_irn_graph_nr(irn));
574 border_def(irn, step, 0);
579 static void assign(ir_node *block, void *env_ptr)
581 env_t *env = env_ptr;
582 struct obstack *obst = &env->obst;
583 bitset_t *live = env->live;
584 bitset_t *colors = env->colors;
585 bitset_t *in_colors = env->in_colors;
586 const arch_isa_if_t *isa = env->isa;
587 const arch_register_class_t *cls = env->cls;
589 /* The used colors will remain on the obstack. */
590 bitset_t *used_colors = bitset_obstack_alloc(obst, env->colors_n);
592 /* Mark the obstack level and allocate the temporary tmp_colors */
593 void *obstack_level = obstack_base(obst);
594 /*bitset_t *tmp_colors = bitset_obstack_alloc(obst, env->colors_n);*/
598 struct list_head *head = &get_ra_block_info(block)->border_head;
599 pset *live_in = get_live_in(block);
601 bitset_clear_all(live);
602 bitset_clear_all(colors);
603 bitset_clear_all(in_colors);
605 DBG((dbg, LEVEL_4, "Assigning colors for block %n\n", block));
606 DBG((dbg, LEVEL_4, "\tusedef chain for block\n"));
607 list_for_each_entry(border_t, b, head, list) {
608 DBG((dbg, LEVEL_4, "\t%s %n/%d\n", b->is_def ? "def" : "use",
609 b->irn, get_irn_graph_nr(b->irn)));
613 * Add initial defs for all values live in.
614 * Since their colors have already been assigned (The dominators were
615 * allocated before), we have to mark their colors as used also.
617 for(irn = pset_first(live_in); irn; irn = pset_next(live_in)) {
618 if(arch_isa_irn_has_reg_class(isa, irn, cls)) {
619 int col = get_irn_color(irn);
621 /* Mark the color of the live in value as used. */
622 assert(is_color(col) && "Node must have been assigned a color.");
623 bitset_set(colors, col);
624 bitset_set(in_colors, col);
625 bitset_set(used_colors, col);
627 /* Mark the value live in. */
628 bitset_set(live, get_irn_graph_nr(irn));
633 * Mind that the sequence of defs from back to front defines a perfect
634 * elimination order. So, coloring the definitions from first to last
637 list_for_each_entry_reverse(border_t, b, head, list) {
638 const ir_node *irn = b->irn;
639 int nr = get_irn_graph_nr(irn);
642 * Assign a color, if it is a local def. Global defs already have a
645 if(b->is_def && !is_live_in(block, irn)) {
646 ra_node_info_t *ri = get_ra_node_info(irn);
649 DBG((dbg, LEVEL_4, "\tcolors in use: %b\n", colors));
652 * Try to assign live out values colors which are not used by live
656 if(is_live_out(block, irn)) {
659 bitset_copy(tmp_colors, colors);
660 bitset_or(tmp_colors, in_colors);
661 next_clear = bitset_next_clear(tmp_colors, 0);
662 col = next_clear != -1 ? next_clear : NO_COLOR;
664 DBG((dbg, LEVEL_5, "next clear in only outs %b: %d\n", tmp_colors, col));
668 /* If a color is not yet assigned, do it now. */
670 col = bitset_next_clear(colors, 0);
672 assert(!is_color(get_irn_color(irn)) && "Color must not have assigned");
673 assert(!bitset_is_set(live, nr) && "Value def must not have been encountered");
675 bitset_set(colors, col);
676 bitset_set(used_colors, col);
677 bitset_set(live, nr);
681 DBG((dbg, LEVEL_1, "\tassigning color %d to %n\n", col, irn));
684 /* Clear the color upon a use. */
685 else if(!b->is_def) {
686 int col = get_irn_color(irn);
688 assert(bitset_is_set(live, nr) && "Cannot have a non live use");
689 assert(is_color(col) && "A color must have been assigned");
691 bitset_clear(colors, col);
692 bitset_clear(live, nr);
701 ir_snprintf(buf, sizeof(buf), "pres_%s_bl_%N.txt",
702 get_entity_name(get_irg_entity(irg)), block);
704 if((f = fopen(buf, "wt")) != NULL) {
705 sched_foreach_reverse(block, irn) {
706 if(arch_isa_irn_has_reg_class(isa, irn, cls))
707 ir_fprintf(f, "\"%n\" %d %d\n", irn, sched_get_time_step(irn),
708 get_ra_node_info(irn)->pressure);
718 * Allocate the used colors array in the blocks ra info structure and
721 get_ra_block_info(block)->used_colors = used_colors;
723 /* Free the auxillary data on the obstack. */
724 obstack_free(obst, obstack_level);
727 void be_ra_chordal_init(void)
729 dbg = firm_dbg_register(DBG_BERA);
730 firm_dbg_set_mask(dbg, -1);
733 void be_ra_chordal(ir_graph *irg,
734 const arch_isa_if_t *isa,
735 const arch_register_class_t *cls)
737 int node_count = get_graph_node_count(irg);
738 int colors_n = arch_register_class_n_regs(cls);
739 env_t *env = malloc(sizeof(*env));
741 if(get_irg_dom_state(irg) != dom_consistent)
744 obstack_init(&env->obst);
747 env->graph = new_set(if_edge_cmp, node_count);
750 env->live = bitset_obstack_alloc(&env->obst, node_count);
751 env->colors = bitset_obstack_alloc(&env->obst, colors_n);
752 env->in_colors = bitset_obstack_alloc(&env->obst, colors_n);
753 env->colors_n = colors_n;
758 /* First, determine the pressure */
759 dom_tree_walk_irg(irg, pressure, NULL, env);
761 /* Insert probable spills */
762 be_ra_chordal_spill(irg);
764 /* Assign the colors */
765 dom_tree_walk_irg(irg, assign, NULL, env);
771 ir_snprintf(buf, sizeof(buf), "ifg_%F.dot", irg);
772 dump_ifg(irg, env->graph, buf);
776 #ifdef DUMP_INTERVALS
780 set_irg_ra_link(irg, env);
783 void be_ra_chordal_done(ir_graph *irg)
785 env_t *env = get_irg_ra_link(irg);
791 obstack_free(&env->obst, NULL);
795 int phi_ops_interfere(const ir_node *a, const ir_node *b)
798 ir_graph *irg = get_irn_irg(a);
799 env_t *env = get_irg_ra_link(irg);
801 assert(irg == get_irn_irg(b) && "Both nodes must be in the same graph");
803 return are_connected(env, get_irn_graph_nr(a), get_irn_graph_nr(b));
805 return values_interfere(a, b);
806 #endif /* BUILD_GRAPH */
810 set *be_ra_get_ifg(ir_graph *irg) {
811 return ((env_t *)get_irg_ra_link(irg))->graph;