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. */
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_end = is_live_end(block, irn);
135 int y_val = y_dist * col;
138 int green = live_end;
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");
157 typedef struct _tree_layout_info_t {
164 } tree_layout_info_t;
166 typedef struct _tree_layout_params_t {
171 } tree_layout_params_t;
173 static const tree_layout_params_t tree_layout_params = {
177 static void dump_tree_collect(ir_node *block, void *env)
180 struct list_head *border_head;
182 int pre_num = get_Block_dom_tree_pre_num(block);
183 tree_layout_info_t *info = env;
184 tree_layout_info_t *i = &info[pre_num];
188 i->steps = list_empty(border_head) ? 0 : list_entry(border_head->prev, border_t, list)->step;
191 dominates_for_each(block, curr)
195 static void dump_tree_assign_x(ir_node *block, void *env)
197 unsigned min_x = -1, max_x = 0;
201 int pre_num = get_Block_dom_tree_pre_num(block);
202 tree_layout_info_t *info = env;
203 tree_layout_info_t *i = &info[pre_num];
208 dominates_for_each(block, curr) {
209 tree_layout_info_t *ci = &info[get_Block_dom_tree_pre_num(curr)];
210 max_x = MAX(max_x, ci->x);
211 min_x = MIN(min_x, ci->x);
215 i->x = (max_x - mix_x) / n_childs;
218 static void dump_tree_assign_y(ir_node *block, void *env)
220 unsigned min_x = -1, max_x = 0;
224 int pre_num = get_Block_dom_tree_pre_num(block);
225 tree_layout_info_t *info = env;
226 tree_layout_info_t *i = &info[pre_num];
227 ir_block *idom = get_Block_idom(block);
231 tree_layout_info_t *idom_info = &info[get_Block_dom_tree_pre_num(idom)];
232 i->y = idom_info->y + idom_info->steps * params->step_len + params->y_dist;
236 static void draw_block(ir_block *bl, void *env)
240 static void dump_interval_tree(ir_graph *irg, const tree_layout_params_t *params)
243 int n_blocks = get_Block_dom_max_subtree_pre_num(get_irg_start_block(irg));
244 tree_layout_info_t *info = malloc(sizeof(info[0]) * n_blocks);
246 /* Fill the info array. */
247 dom_tree_walk_irg(irg, NULL, dump_tree_collect, info);
249 /* Assign the child slots. */
250 for(i = 0, slot = 0; i < n_blocks; ++i) {
251 tree_layout_info_t *i = &info[i];
253 i->child_slot = slot++;
254 i->x = i->child_slot * params->max_color * params->interval_dist + params->block_dist;
258 dom_tree_walk_irg(irg, NULL, dump_tree_assign_xy, info);
266 typedef struct _if_edge_t {
270 #define IF_EDGE_HASH(e) ((e)->src)
272 static int if_edge_cmp(const void *p1, const void *p2, size_t size)
274 const if_edge_t *e1 = p1;
275 const if_edge_t *e2 = p2;
277 return !(e1->src == e2->src && e1->tgt == e2->tgt);
280 static INLINE if_edge_t *edge_init(if_edge_t *edge, int src, int tgt)
282 /* Bring the smaller entry to src. */
294 static INLINE void add_if(const env_t *env, int src, int tgt)
297 edge_init(&edge, src, tgt);
298 set_insert(env->graph, &edge, sizeof(edge), IF_EDGE_HASH(&edge));
301 static INLINE int are_connected(const env_t *env, int src, int tgt)
304 edge_init(&edge, src, tgt);
305 return set_find(env->graph, &edge, sizeof(edge), IF_EDGE_HASH(&edge)) != NULL;
308 static void dump_ifg(ir_graph *irg, set *edges, const char *filename)
310 static const char *colors[] = {
390 static const int n_colors = sizeof(colors) / sizeof(colors[0]);
394 if((f = fopen(filename, "wt")) != NULL) {
398 bitset_t *bs = bitset_malloc(get_graph_node_count(irg));
400 ir_fprintf(f, "graph \"%F\" {\n", irg);
401 fprintf(f, "\tnode [shape=box,style=filled]\n");
403 for(edge = set_first(edges); edge; edge = set_next(edges)) {
404 bitset_set(bs, edge->src);
405 bitset_set(bs, edge->tgt);
409 fprintf(f, "\tx [label=\"nodes: %d, edges: %d\"]\n", bitset_popcnt(bs), n_edges);
411 bitset_foreach(bs, pos) {
413 ir_node *irn = get_irn_for_graph_nr(irg, nr);
414 int color = get_irn_color(irn);
416 ir_fprintf(f, "\tn%d [label=\"%n\",color=\"%s\"]\n", nr, irn,
417 color >= 0 && color < n_colors ? colors[color] : "black");
420 for(edge = set_first(edges); edge; edge = set_next(edges)) {
421 fprintf(f, "\tn%d -- n%d [len=5]\n", edge->src, edge->tgt);
432 #endif /* BUILD_GRAPH */
435 * Add an interval border to the list of a block's list
436 * of interval border.
437 * @note You always have to create the use before the def.
438 * @param env The environment.
439 * @param head The list head to enqueue the borders.
440 * @param irn The node (value) the border belongs to.
441 * @param pressure The pressure at this point in time.
442 * @param step A time step for the border.
443 * @param is_def Is the border a use or a def.
444 * @return The created border.
446 static INLINE border_t *border_add(env_t *env, struct list_head *head,
447 ir_node *irn, unsigned step, unsigned pressure,
448 unsigned is_def, unsigned is_real)
455 b = obstack_alloc(&env->obst, sizeof(*b));
457 /* also allocate the def and tie it to the use. */
458 def = obstack_alloc(&env->obst, sizeof(*def));
463 * Set the link field of the irn to the def.
464 * This strongly relies on the fact, that the use is always
465 * made before the def.
467 set_irn_link(irn, def);
470 b->magic = BORDER_FOURCC;
471 def->magic = BORDER_FOURCC;
476 * If the def is encountered, the use was made and so was the
477 * the def node (see the code above). It was placed into the
478 * link field of the irn, so we can get it there.
481 b = get_irn_link(irn);
484 assert(b && b->magic == BORDER_FOURCC && "Illegal border encountered");
490 b->is_real = is_real;
493 list_add_tail(&b->list, head);
494 DBG((dbg, LEVEL_5, "\t\t%s adding %n, step: %d\n",
495 is_def ? "def" : "use", irn, step));
501 * Annotate the register pressure to the nodes and compute
502 * the liveness intervals.
503 * @param block The block to do it for.
504 * @param env_ptr The environment.
506 static void pressure(ir_node *block, void *env_ptr)
508 /* Convenience macro for a def */
509 #define border_def(irn, step, real) \
510 border_add(env, head, irn, step, pressure--, 1, real)
512 /* Convenience macro for a use */
513 #define border_use(irn, step, real) \
514 border_add(env, head, irn, step, ++pressure, 0, real)
516 env_t *env = env_ptr;
517 bitset_t *live = env->live;
522 unsigned pressure = 0;
523 struct list_head *head;
524 pset *live_in = get_live_in(block);
525 pset *live_end = get_live_end(block);
527 DBG((dbg, LEVEL_1, "Computing pressure in block %n\n", block));
528 bitset_clear_all(live);
530 /* Set up the border list in the block info */
531 head = &get_ra_block_info(block)->border_head;
532 INIT_LIST_HEAD(head);
535 * Make final uses of all values live out of the block.
536 * They are neccessary to build up real intervals.
538 for(irn = pset_first(live_end); irn; irn = pset_next(live_end)) {
539 DBG((dbg, LEVEL_3, "\tMaking live: %n/%d\n", irn, get_irn_graph_nr(irn)));
540 bitset_set(live, get_irn_graph_nr(irn));
541 if(is_allocatable_irn(irn))
542 border_use(irn, step, 0);
548 * Determine the last uses of a value inside the block, since they are
549 * relevant for the interval borders.
551 sched_foreach_reverse(block, irn) {
552 DBG((dbg, LEVEL_1, "\tinsn: %n, pressure: %d\n", irn, pressure));
553 DBG((dbg, LEVEL_2, "\tlive: %b\n", live));
555 /* Erase the color of each node encountered. */
556 set_irn_color(irn, NO_COLOR);
559 * If the node defines a datab value, i.e. something, registers must
560 * be allocated for, add a new def border to the border list.
562 if(is_allocatable_irn(irn)) {
563 int nr = get_irn_graph_nr(irn);
565 bitset_clear(live, nr);
566 border_def(irn, step, 1);
571 bitset_foreach(live, elm) {
572 int live_nr = (int) elm;
573 add_if(env, nr, live_nr);
580 * If the node is no phi node we can examine the uses.
583 for(i = 0, n = get_irn_arity(irn); i < n; ++i) {
584 ir_node *op = get_irn_n(irn, i);
586 if(is_allocatable_irn(op)) {
587 int nr = get_irn_graph_nr(op);
589 DBG((dbg, LEVEL_4, "\t\tpos: %d, use: %n\n", i, op));
591 if(!bitset_is_set(live, nr)) {
592 border_use(op, step, 1);
593 bitset_set(live, nr);
603 * Add initial defs for all values live in.
605 for(irn = pset_first(live_in); irn; irn = pset_next(live_in)) {
606 if(is_allocatable_irn(irn)) {
608 /* Mark the value live in. */
609 bitset_set(live, get_irn_graph_nr(irn));
612 border_def(irn, step, 0);
617 static void assign(ir_node *block, void *env_ptr)
619 env_t *env = env_ptr;
620 struct obstack *obst = &env->obst;
621 bitset_t *live = env->live;
622 bitset_t *colors = env->colors;
623 bitset_t *in_colors = env->in_colors;
625 /* The used colors will remain on the obstack. */
626 bitset_t *used_colors = bitset_obstack_alloc(obst, env->colors_n);
628 /* Mark the obstack level and allocate the temporary tmp_colors */
629 void *obstack_level = obstack_base(obst);
630 bitset_t *tmp_colors = bitset_obstack_alloc(obst, env->colors_n);
634 struct list_head *head = &get_ra_block_info(block)->border_head;
635 pset *live_in = get_live_in(block);
637 bitset_clear_all(live);
638 bitset_clear_all(colors);
639 bitset_clear_all(in_colors);
641 DBG((dbg, LEVEL_4, "Assigning colors for block %n\n", block));
642 DBG((dbg, LEVEL_4, "\tusedef chain for block\n"));
643 list_for_each_entry(border_t, b, head, list) {
644 DBG((dbg, LEVEL_4, "\t%s %n/%d\n", b->is_def ? "def" : "use",
645 b->irn, get_irn_graph_nr(b->irn)));
649 * Add initial defs for all values live in.
650 * Since their colors have already been assigned (The dominators were
651 * allocated before), we have to mark their colors as used also.
653 for(irn = pset_first(live_in); irn; irn = pset_next(live_in)) {
654 if(is_allocatable_irn(irn)) {
655 int col = get_irn_color(irn);
657 /* Mark the color of the live in value as used. */
658 assert(is_color(col) && "Node must have been assigned a color.");
659 bitset_set(colors, col);
660 bitset_set(in_colors, col);
661 bitset_set(used_colors, col);
663 /* Mark the value live in. */
664 bitset_set(live, get_irn_graph_nr(irn));
669 * Mind that the sequence of defs from back to front defines a perfect
670 * elimination order. So, coloring the definitions from first to last
673 list_for_each_entry_reverse(border_t, b, head, list) {
674 const ir_node *irn = b->irn;
675 int nr = get_irn_graph_nr(irn);
678 * Assign a color, if it is a local def. Global defs already have a
681 if(b->is_def && !is_live_in(block, irn)) {
682 ra_node_info_t *ri = get_ra_node_info(irn);
685 DBG((dbg, LEVEL_4, "\tcolors in use: %b\n", colors));
688 * Try to assign live out values colors which are not used by live
692 if(is_live_out(block, irn)) {
695 bitset_copy(tmp_colors, colors);
696 bitset_or(tmp_colors, in_colors);
697 next_clear = bitset_next_clear(tmp_colors, 0);
698 col = next_clear != -1 ? next_clear : NO_COLOR;
700 DBG((dbg, LEVEL_5, "next clear in only outs %b: %d\n", tmp_colors, col));
704 /* If a color is not yet assigned, do it now. */
706 col = bitset_next_clear(colors, 0);
708 assert(!is_color(get_irn_color(irn)) && "Color must not have assigned");
709 assert(!bitset_is_set(live, nr) && "Value def must not have been encountered");
711 bitset_set(colors, col);
712 bitset_set(used_colors, col);
713 bitset_set(live, nr);
717 DBG((dbg, LEVEL_1, "\tassigning color %d to %n\n", col, irn));
720 /* Clear the color upon a use. */
721 else if(!b->is_def) {
722 int col = get_irn_color(irn);
724 assert(bitset_is_set(live, nr) && "Cannot have a non live use");
725 assert(is_color(col) && "A color must have been assigned");
727 bitset_clear(colors, col);
728 bitset_clear(live, nr);
732 #ifdef DUMP_INTERVALS
733 draw_interval_graphs(block, head, &dump_params);
741 ir_snprintf(buf, sizeof(buf), "pres_%s_bl_%N.txt",
742 get_entity_name(get_irg_entity(irg)), block);
744 if((f = fopen(buf, "wt")) != NULL) {
745 sched_foreach_reverse(block, irn) {
746 if(is_allocatable_irn(irn))
747 ir_fprintf(f, "\"%n\" %d %d\n", irn, sched_get_time_step(irn),
748 get_ra_node_info(irn)->pressure);
758 * Allocate the used colors array in the blocks ra info structure and
761 get_ra_block_info(block)->used_colors = used_colors;
763 /* Free the auxillary data on the obstack. */
764 obstack_free(obst, obstack_level);
767 void be_ra_chordal_init(void)
769 dbg = firm_dbg_register(DBG_BERA);
770 /* firm_dbg_set_mask(dbg, -1); */
773 void be_ra_chordal(ir_graph *irg)
775 int node_count = get_graph_node_count(irg);
776 env_t *env = malloc(sizeof(*env));
778 if(get_irg_dom_state(irg) != dom_consistent)
781 obstack_init(&env->obst);
784 env->graph = new_set(if_edge_cmp, node_count);
787 env->live = bitset_obstack_alloc(&env->obst, node_count);
788 env->colors = bitset_obstack_alloc(&env->obst, TEST_COLORS);
789 env->in_colors = bitset_obstack_alloc(&env->obst, TEST_COLORS);
790 env->colors_n = TEST_COLORS;
792 /* First, determine the pressure */
793 dom_tree_walk_irg(irg, pressure, NULL, env);
795 /* Insert probable spills */
796 be_ra_chordal_spill(irg);
798 /* Assign the colors */
799 dom_tree_walk_irg(irg, assign, NULL, env);
805 ir_snprintf(buf, sizeof(buf), "ifg_%s.dot", get_entity_name(get_irg_entity(irg)));
806 dump_ifg(irg, env->graph, buf);
810 set_irg_ra_link(irg, env);
813 void be_ra_chordal_done(ir_graph *irg)
815 env_t *env = get_irg_ra_link(irg);
821 obstack_free(&env->obst, NULL);
825 int phi_ops_interfere(const ir_node *a, const ir_node *b)
828 ir_graph *irg = get_irn_irg(a);
829 env_t *env = get_irg_ra_link(irg);
831 assert(irg == get_irn_irg(b) && "Both nodes must be in the same graph");
833 return are_connected(env, get_irn_graph_nr(a), get_irn_graph_nr(b));
835 return values_interfere(a, b);
836 #endif /* BUILD_GRAPH */