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"
42 /* Make a fourcc for border checking. */
43 #define BORDER_FOURCC FOURCC('B', 'O', 'R', 'D')
45 #endif /* DEBUG_libfirm */
47 #define TEST_COLORS 2048
49 static firm_dbg_module_t *dbg;
52 * Environment for each of the chordal register allocator phases
54 typedef struct _env_t {
55 struct obstack obst; /**< An obstack for temporary storage. */
57 set *graph; /**< The interference graph. */
60 bitset_t *live; /**< A liveness bitset. */
61 bitset_t *colors; /**< The color mask. */
62 bitset_t *in_colors; /**< Colors used by live in values. */
63 int colors_n; /**< The number of colors. */
67 typedef struct _be_chordal_dump_params_t {
71 } be_chordal_dump_params_t;
73 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");
158 typedef struct _tree_layout_info_t {
165 } tree_layout_info_t;
167 typedef struct _tree_layout_params_t {
172 } tree_layout_params_t;
174 static const tree_layout_params_t tree_layout_params = {
178 static void dump_tree_collect(ir_node *block, void *env)
181 struct list_head *border_head;
183 int pre_num = get_Block_dom_tree_pre_num(block);
184 tree_layout_info_t *info = env;
185 tree_layout_info_t *i = &info[pre_num];
189 i->steps = list_empty(border_head) ? 0 : list_entry(border_head->prev, border_t, list)->step;
192 dominates_for_each(block, curr)
196 static void dump_tree_assign_x(ir_node *block, void *env)
198 unsigned min_x = -1, max_x = 0;
202 int pre_num = get_Block_dom_tree_pre_num(block);
203 tree_layout_info_t *info = env;
204 tree_layout_info_t *i = &info[pre_num];
209 dominates_for_each(block, curr) {
210 tree_layout_info_t *ci = &info[get_Block_dom_tree_pre_num(curr)];
211 max_x = MAX(max_x, ci->x);
212 min_x = MIN(min_x, ci->x);
216 i->x = (max_x - mix_x) / n_childs;
219 static void dump_tree_assign_y(ir_node *block, void *env)
221 unsigned min_x = -1, max_x = 0;
225 int pre_num = get_Block_dom_tree_pre_num(block);
226 tree_layout_info_t *info = env;
227 tree_layout_info_t *i = &info[pre_num];
228 ir_block *idom = get_Block_idom(block);
232 tree_layout_info_t *idom_info = &info[get_Block_dom_tree_pre_num(idom)];
233 i->y = idom_info->y + idom_info->steps * params->step_len + params->y_dist;
237 static void draw_block(ir_block *bl, void *env)
241 static void dump_interval_tree(ir_graph *irg, const tree_layout_params_t *params)
244 int n_blocks = get_Block_dom_max_subtree_pre_num(get_irg_start_block(irg));
245 tree_layout_info_t *info = malloc(sizeof(info[0]) * n_blocks);
247 /* Fill the info array. */
248 dom_tree_walk_irg(irg, NULL, dump_tree_collect, info);
250 /* Assign the child slots. */
251 for(i = 0, slot = 0; i < n_blocks; ++i) {
252 tree_layout_info_t *i = &info[i];
254 i->child_slot = slot++;
255 i->x = i->child_slot * params->max_color * params->interval_dist + params->block_dist;
259 dom_tree_walk_irg(irg, NULL, dump_tree_assign_xy, info);
268 #define IF_EDGE_HASH(e) ((e)->src)
270 static int if_edge_cmp(const void *p1, const void *p2, size_t size)
272 const if_edge_t *e1 = p1;
273 const if_edge_t *e2 = p2;
275 return !(e1->src == e2->src && e1->tgt == e2->tgt);
278 static INLINE if_edge_t *edge_init(if_edge_t *edge, int src, int tgt)
280 /* Bring the smaller entry to src. */
292 static INLINE void add_if(const env_t *env, int src, int tgt)
295 edge_init(&edge, src, tgt);
296 set_insert(env->graph, &edge, sizeof(edge), IF_EDGE_HASH(&edge));
299 static INLINE int are_connected(const env_t *env, int src, int tgt)
302 edge_init(&edge, src, tgt);
303 return set_find(env->graph, &edge, sizeof(edge), IF_EDGE_HASH(&edge)) != NULL;
306 static void dump_ifg(ir_graph *irg, set *edges, const char *filename)
308 static const char *colors[] = {
388 static const int n_colors = sizeof(colors) / sizeof(colors[0]);
392 if((f = fopen(filename, "wt")) != NULL) {
396 bitset_t *bs = bitset_malloc(get_graph_node_count(irg));
398 ir_fprintf(f, "graph \"%F\" {\n", irg);
399 fprintf(f, "\tnode [shape=box,style=filled]\n");
401 for(edge = set_first(edges); edge; edge = set_next(edges)) {
402 bitset_set(bs, edge->src);
403 bitset_set(bs, edge->tgt);
407 fprintf(f, "\tx [label=\"nodes: %lu, edges: %d\"]\n", bitset_popcnt(bs), n_edges);
409 bitset_foreach(bs, pos) {
411 ir_node *irn = get_irn_for_graph_nr(irg, nr);
412 int color = get_irn_color(irn);
414 ir_fprintf(f, "\tn%d [label=\"%n\",color=\"%s\"]\n", nr, irn,
415 color >= 0 && color < n_colors ? colors[color] : "black");
418 for(edge = set_first(edges); edge; edge = set_next(edges)) {
419 fprintf(f, "\tn%d -- n%d [len=5]\n", edge->src, edge->tgt);
430 #endif /* BUILD_GRAPH */
433 * Add an interval border to the list of a block's list
434 * of interval border.
435 * @note You always have to create the use before the def.
436 * @param env The environment.
437 * @param head The list head to enqueue the borders.
438 * @param irn The node (value) the border belongs to.
439 * @param pressure The pressure at this point in time.
440 * @param step A time step for the border.
441 * @param is_def Is the border a use or a def.
442 * @return The created border.
444 static INLINE border_t *border_add(env_t *env, struct list_head *head,
445 ir_node *irn, unsigned step, unsigned pressure,
446 unsigned is_def, unsigned is_real)
453 b = obstack_alloc(&env->obst, sizeof(*b));
455 /* also allocate the def and tie it to the use. */
456 def = obstack_alloc(&env->obst, sizeof(*def));
461 * Set the link field of the irn to the def.
462 * This strongly relies on the fact, that the use is always
463 * made before the def.
465 set_irn_link(irn, def);
468 b->magic = BORDER_FOURCC;
469 def->magic = BORDER_FOURCC;
474 * If the def is encountered, the use was made and so was the
475 * the def node (see the code above). It was placed into the
476 * link field of the irn, so we can get it there.
479 b = get_irn_link(irn);
482 assert(b && b->magic == BORDER_FOURCC && "Illegal border encountered");
486 b->pressure = pressure;
488 b->is_real = is_real;
491 list_add_tail(&b->list, head);
492 DBG((dbg, LEVEL_5, "\t\t%s adding %n, step: %d\n",
493 is_def ? "def" : "use", irn, step));
499 * Annotate the register pressure to the nodes and compute
500 * the liveness intervals.
501 * @param block The block to do it for.
502 * @param env_ptr The environment.
504 static void pressure(ir_node *block, void *env_ptr)
506 /* Convenience macro for a def */
507 #define border_def(irn, step, real) \
508 border_add(env, head, irn, step, pressure--, 1, real)
510 /* Convenience macro for a use */
511 #define border_use(irn, step, real) \
512 border_add(env, head, irn, step, ++pressure, 0, real)
514 env_t *env = env_ptr;
515 bitset_t *live = env->live;
520 unsigned pressure = 0;
521 struct list_head *head;
522 pset *live_in = get_live_in(block);
523 pset *live_end = get_live_end(block);
525 DBG((dbg, LEVEL_1, "Computing pressure in block %n\n", block));
526 bitset_clear_all(live);
528 /* Set up the border list in the block info */
529 head = &get_ra_block_info(block)->border_head;
530 INIT_LIST_HEAD(head);
533 * Make final uses of all values live out of the block.
534 * They are neccessary to build up real intervals.
536 for(irn = pset_first(live_end); irn; irn = pset_next(live_end)) {
537 DBG((dbg, LEVEL_3, "\tMaking live: %n/%d\n", irn, get_irn_graph_nr(irn)));
538 bitset_set(live, get_irn_graph_nr(irn));
539 if(is_allocatable_irn(irn))
540 border_use(irn, step, 0);
546 * Determine the last uses of a value inside the block, since they are
547 * relevant for the interval borders.
549 sched_foreach_reverse(block, irn) {
550 DBG((dbg, LEVEL_1, "\tinsn: %n, pressure: %d\n", irn, pressure));
551 DBG((dbg, LEVEL_2, "\tlive: %b\n", live));
553 /* Erase the color of each node encountered. */
554 set_irn_color(irn, NO_COLOR);
557 * If the node defines a datab value, i.e. something, registers must
558 * be allocated for, add a new def border to the border list.
560 if(is_allocatable_irn(irn)) {
561 int nr = get_irn_graph_nr(irn);
563 bitset_clear(live, nr);
564 border_def(irn, step, 1);
569 bitset_foreach(live, elm) {
570 int live_nr = (int) elm;
571 add_if(env, nr, live_nr);
578 * If the node is no phi node we can examine the uses.
581 for(i = 0, n = get_irn_arity(irn); i < n; ++i) {
582 ir_node *op = get_irn_n(irn, i);
584 if(is_allocatable_irn(op)) {
585 int nr = get_irn_graph_nr(op);
587 DBG((dbg, LEVEL_4, "\t\tpos: %d, use: %n\n", i, op));
589 if(!bitset_is_set(live, nr)) {
590 border_use(op, step, 1);
591 bitset_set(live, nr);
601 * Add initial defs for all values live in.
603 for(irn = pset_first(live_in); irn; irn = pset_next(live_in)) {
604 if(is_allocatable_irn(irn)) {
606 /* Mark the value live in. */
607 bitset_set(live, get_irn_graph_nr(irn));
610 border_def(irn, step, 0);
615 static void assign(ir_node *block, void *env_ptr)
617 env_t *env = env_ptr;
618 struct obstack *obst = &env->obst;
619 bitset_t *live = env->live;
620 bitset_t *colors = env->colors;
621 bitset_t *in_colors = env->in_colors;
623 /* The used colors will remain on the obstack. */
624 bitset_t *used_colors = bitset_obstack_alloc(obst, env->colors_n);
626 /* Mark the obstack level and allocate the temporary tmp_colors */
627 void *obstack_level = obstack_base(obst);
628 /*bitset_t *tmp_colors = bitset_obstack_alloc(obst, env->colors_n);*/
632 struct list_head *head = &get_ra_block_info(block)->border_head;
633 pset *live_in = get_live_in(block);
635 bitset_clear_all(live);
636 bitset_clear_all(colors);
637 bitset_clear_all(in_colors);
639 DBG((dbg, LEVEL_4, "Assigning colors for block %n\n", block));
640 DBG((dbg, LEVEL_4, "\tusedef chain for block\n"));
641 list_for_each_entry(border_t, b, head, list) {
642 DBG((dbg, LEVEL_4, "\t%s %n/%d\n", b->is_def ? "def" : "use",
643 b->irn, get_irn_graph_nr(b->irn)));
647 * Add initial defs for all values live in.
648 * Since their colors have already been assigned (The dominators were
649 * allocated before), we have to mark their colors as used also.
651 for(irn = pset_first(live_in); irn; irn = pset_next(live_in)) {
652 if(is_allocatable_irn(irn)) {
653 int col = get_irn_color(irn);
655 /* Mark the color of the live in value as used. */
656 assert(is_color(col) && "Node must have been assigned a color.");
657 bitset_set(colors, col);
658 bitset_set(in_colors, col);
659 bitset_set(used_colors, col);
661 /* Mark the value live in. */
662 bitset_set(live, get_irn_graph_nr(irn));
667 * Mind that the sequence of defs from back to front defines a perfect
668 * elimination order. So, coloring the definitions from first to last
671 list_for_each_entry_reverse(border_t, b, head, list) {
672 const ir_node *irn = b->irn;
673 int nr = get_irn_graph_nr(irn);
676 * Assign a color, if it is a local def. Global defs already have a
679 if(b->is_def && !is_live_in(block, irn)) {
680 ra_node_info_t *ri = get_ra_node_info(irn);
683 DBG((dbg, LEVEL_4, "\tcolors in use: %b\n", colors));
686 * Try to assign live out values colors which are not used by live
690 if(is_live_out(block, irn)) {
693 bitset_copy(tmp_colors, colors);
694 bitset_or(tmp_colors, in_colors);
695 next_clear = bitset_next_clear(tmp_colors, 0);
696 col = next_clear != -1 ? next_clear : NO_COLOR;
698 DBG((dbg, LEVEL_5, "next clear in only outs %b: %d\n", tmp_colors, col));
702 /* If a color is not yet assigned, do it now. */
704 col = bitset_next_clear(colors, 0);
706 assert(!is_color(get_irn_color(irn)) && "Color must not have assigned");
707 assert(!bitset_is_set(live, nr) && "Value def must not have been encountered");
709 bitset_set(colors, col);
710 bitset_set(used_colors, col);
711 bitset_set(live, nr);
715 DBG((dbg, LEVEL_1, "\tassigning color %d to %n\n", col, irn));
718 /* Clear the color upon a use. */
719 else if(!b->is_def) {
720 int col = get_irn_color(irn);
722 assert(bitset_is_set(live, nr) && "Cannot have a non live use");
723 assert(is_color(col) && "A color must have been assigned");
725 bitset_clear(colors, col);
726 bitset_clear(live, nr);
730 #ifdef DUMP_INTERVALS
731 draw_interval_graphs(block, head, &dump_params);
739 ir_snprintf(buf, sizeof(buf), "pres_%s_bl_%N.txt",
740 get_entity_name(get_irg_entity(irg)), block);
742 if((f = fopen(buf, "wt")) != NULL) {
743 sched_foreach_reverse(block, irn) {
744 if(is_allocatable_irn(irn))
745 ir_fprintf(f, "\"%n\" %d %d\n", irn, sched_get_time_step(irn),
746 get_ra_node_info(irn)->pressure);
756 * Allocate the used colors array in the blocks ra info structure and
759 get_ra_block_info(block)->used_colors = used_colors;
761 /* Free the auxillary data on the obstack. */
762 obstack_free(obst, obstack_level);
765 void be_ra_chordal_init(void)
767 dbg = firm_dbg_register(DBG_BERA);
768 firm_dbg_set_mask(dbg, 0);
771 void be_ra_chordal(ir_graph *irg)
773 int node_count = get_graph_node_count(irg);
774 env_t *env = malloc(sizeof(*env));
776 if(get_irg_dom_state(irg) != dom_consistent)
779 obstack_init(&env->obst);
782 env->graph = new_set(if_edge_cmp, node_count);
785 env->live = bitset_obstack_alloc(&env->obst, node_count);
786 env->colors = bitset_obstack_alloc(&env->obst, TEST_COLORS);
787 env->in_colors = bitset_obstack_alloc(&env->obst, TEST_COLORS);
788 env->colors_n = TEST_COLORS;
790 /* First, determine the pressure */
791 dom_tree_walk_irg(irg, pressure, NULL, env);
793 /* Insert probable spills */
794 be_ra_chordal_spill(irg);
796 /* Assign the colors */
797 dom_tree_walk_irg(irg, assign, NULL, env);
803 ir_snprintf(buf, sizeof(buf), "ifg_%s.dot", get_entity_name(get_irg_entity(irg)));
804 dump_ifg(irg, env->graph, buf);
808 set_irg_ra_link(irg, env);
811 void be_ra_chordal_done(ir_graph *irg)
813 env_t *env = get_irg_ra_link(irg);
819 obstack_free(&env->obst, NULL);
823 int phi_ops_interfere(const ir_node *a, const ir_node *b)
826 ir_graph *irg = get_irn_irg(a);
827 env_t *env = get_irg_ra_link(irg);
829 assert(irg == get_irn_irg(b) && "Both nodes must be in the same graph");
831 return are_connected(env, get_irn_graph_nr(a), get_irn_graph_nr(b));
833 return values_interfere(a, b);
834 #endif /* BUILD_GRAPH */
838 set *be_ra_get_ifg(ir_graph *irg) {
839 return ((env_t *)get_irg_ra_link(irg))->graph;