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 = {
79 static void draw_interval_graphs(ir_node *block,
80 struct list_head *border_head,
81 const be_chordal_dump_params_t *params)
84 int x_dist = params->x_dist;
85 int y_dist = params->y_dist;
86 ir_graph *irg = get_irn_irg(block);
91 ir_snprintf(buf, sizeof(buf), "intv_%s_bl%N.eps",
92 get_entity_name(get_irg_entity(irg)), block);
94 if((f = fopen(buf, "wt")) != NULL) {
96 int *seen = xcalloc(get_graph_node_count(irg), sizeof(seen[0]));
97 int last_pos = list_empty(border_head) ? 0 : list_entry(border_head->prev, border_t, list)->step;
100 list_for_each_entry_reverse(border_t, b, border_head, list) {
101 const ir_node *irn = b->irn;
102 int col = get_irn_color(irn);
103 max_col = max_col > col ? max_col : col;
106 fprintf(f, "%%!PS-Adobe-2.0\n");
107 fprintf(f, "%%%%BoundingBox: -10 -10 %d %d\n",
108 x_dist * last_pos + x_dist, y_dist * max_col + y_dist);
109 fprintf(f, "/mainfont /Courier findfont %f scalefont def\n", params->font_scale);
110 fprintf(f, "mainfont setfont\n");
111 fprintf(f, "0.2 setlinewidth\n");
113 for(i = 0; i <= last_pos; ++i) {
114 fprintf(f, "0 0 0 setrgbcolor\n");
115 fprintf(f, "%d %d moveto\n", i * x_dist, -2);
116 fprintf(f, "%d %d lineto\n", i * x_dist, max_col * y_dist + 2);
117 fprintf(f, "stroke\n");
119 fprintf(f, "0.5 setlinewidth\n");
121 list_for_each_entry_reverse(border_t, b, border_head, list) {
122 const ir_node *irn = b->irn;
123 int nr = get_irn_graph_nr(irn);
128 int col = get_irn_color(irn);
130 int pos = last_pos - seen[nr];
131 int end_pos = last_pos - b->step;
132 int live_in = is_live_in(block, irn);
133 int live_end = is_live_end(block, irn);
134 int y_val = y_dist * col;
137 int green = live_end;
140 fprintf(f, "0 0 0 setrgbcolor\n");
141 fprintf(f, "%d %d moveto\n", x_dist * pos + 2, y_val + 2);
142 ir_fprintf(f, "(%n/%d%s) show\n", irn, nr, is_phi_operand(irn) ? "*" : "");
143 fprintf(f, "%d %d %d setrgbcolor\n", red, green, blue);
144 fprintf(f, "%d %d moveto\n", x_dist * pos, y_val);
145 fprintf(f, "%d %d lineto\n", (x_dist * end_pos) - 5, y_val);
146 fprintf(f, "stroke\n");
156 typedef struct _tree_layout_info_t {
163 } tree_layout_info_t;
165 typedef struct _tree_layout_params_t {
170 } tree_layout_params_t;
172 static const tree_layout_params_t tree_layout_params = {
176 static void dump_tree_collect(ir_node *block, void *env)
179 struct list_head *border_head;
181 int pre_num = get_Block_dom_tree_pre_num(block);
182 tree_layout_info_t *info = env;
183 tree_layout_info_t *i = &info[pre_num];
187 i->steps = list_empty(border_head) ? 0 : list_entry(border_head->prev, border_t, list)->step;
190 dominates_for_each(block, curr)
194 static void dump_tree_assign_x(ir_node *block, void *env)
196 unsigned min_x = -1, max_x = 0;
200 int pre_num = get_Block_dom_tree_pre_num(block);
201 tree_layout_info_t *info = env;
202 tree_layout_info_t *i = &info[pre_num];
207 dominates_for_each(block, curr) {
208 tree_layout_info_t *ci = &info[get_Block_dom_tree_pre_num(curr)];
209 max_x = MAX(max_x, ci->x);
210 min_x = MIN(min_x, ci->x);
214 i->x = (max_x - mix_x) / n_childs;
217 static void dump_tree_assign_y(ir_node *block, void *env)
219 unsigned min_x = -1, max_x = 0;
223 int pre_num = get_Block_dom_tree_pre_num(block);
224 tree_layout_info_t *info = env;
225 tree_layout_info_t *i = &info[pre_num];
226 ir_block *idom = get_Block_idom(block);
230 tree_layout_info_t *idom_info = &info[get_Block_dom_tree_pre_num(idom)];
231 i->y = idom_info->y + idom_info->steps * params->step_len + params->y_dist;
235 static void draw_block(ir_block *bl, void *env)
239 static void dump_interval_tree(ir_graph *irg, const tree_layout_params_t *params)
242 int n_blocks = get_Block_dom_max_subtree_pre_num(get_irg_start_block(irg));
243 tree_layout_info_t *info = malloc(sizeof(info[0]) * n_blocks);
245 /* Fill the info array. */
246 dom_tree_walk_irg(irg, NULL, dump_tree_collect, info);
248 /* Assign the child slots. */
249 for(i = 0, slot = 0; i < n_blocks; ++i) {
250 tree_layout_info_t *i = &info[i];
252 i->child_slot = slot++;
253 i->x = i->child_slot * params->max_color * params->interval_dist + params->block_dist;
257 dom_tree_walk_irg(irg, NULL, dump_tree_assign_xy, info);
266 #define IF_EDGE_HASH(e) ((e)->src)
268 static int if_edge_cmp(const void *p1, const void *p2, size_t size)
270 const if_edge_t *e1 = p1;
271 const if_edge_t *e2 = p2;
273 return !(e1->src == e2->src && e1->tgt == e2->tgt);
276 static INLINE if_edge_t *edge_init(if_edge_t *edge, int src, int tgt)
278 /* Bring the smaller entry to src. */
290 static INLINE void add_if(const env_t *env, int src, int tgt)
293 edge_init(&edge, src, tgt);
294 set_insert(env->graph, &edge, sizeof(edge), IF_EDGE_HASH(&edge));
297 static INLINE int are_connected(const env_t *env, int src, int tgt)
300 edge_init(&edge, src, tgt);
301 return set_find(env->graph, &edge, sizeof(edge), IF_EDGE_HASH(&edge)) != NULL;
304 static void dump_ifg(ir_graph *irg, set *edges, const char *filename)
306 static const char *colors[] = {
386 static const int n_colors = sizeof(colors) / sizeof(colors[0]);
390 if((f = fopen(filename, "wt")) != NULL) {
394 bitset_t *bs = bitset_malloc(get_graph_node_count(irg));
396 ir_fprintf(f, "graph \"%F\" {\n", irg);
397 fprintf(f, "\tnode [shape=box,style=filled]\n");
399 for(edge = set_first(edges); edge; edge = set_next(edges)) {
400 bitset_set(bs, edge->src);
401 bitset_set(bs, edge->tgt);
405 fprintf(f, "\tx [label=\"nodes: %d, edges: %d\"]\n", bitset_popcnt(bs), n_edges);
407 bitset_foreach(bs, pos) {
409 ir_node *irn = get_irn_for_graph_nr(irg, nr);
410 int color = get_irn_color(irn);
412 ir_fprintf(f, "\tn%d [label=\"%n\",color=\"%s\"]\n", nr, irn,
413 color >= 0 && color < n_colors ? colors[color] : "black");
416 for(edge = set_first(edges); edge; edge = set_next(edges)) {
417 fprintf(f, "\tn%d -- n%d [len=5]\n", edge->src, edge->tgt);
428 #endif /* BUILD_GRAPH */
431 * Add an interval border to the list of a block's list
432 * of interval border.
433 * @note You always have to create the use before the def.
434 * @param env The environment.
435 * @param head The list head to enqueue the borders.
436 * @param irn The node (value) the border belongs to.
437 * @param pressure The pressure at this point in time.
438 * @param step A time step for the border.
439 * @param is_def Is the border a use or a def.
440 * @return The created border.
442 static INLINE border_t *border_add(env_t *env, struct list_head *head,
443 ir_node *irn, unsigned step, unsigned pressure,
444 unsigned is_def, unsigned is_real)
451 b = obstack_alloc(&env->obst, sizeof(*b));
453 /* also allocate the def and tie it to the use. */
454 def = obstack_alloc(&env->obst, sizeof(*def));
459 * Set the link field of the irn to the def.
460 * This strongly relies on the fact, that the use is always
461 * made before the def.
463 set_irn_link(irn, def);
466 b->magic = BORDER_FOURCC;
467 def->magic = BORDER_FOURCC;
472 * If the def is encountered, the use was made and so was the
473 * the def node (see the code above). It was placed into the
474 * link field of the irn, so we can get it there.
477 b = get_irn_link(irn);
480 assert(b && b->magic == BORDER_FOURCC && "Illegal border encountered");
484 b->pressure = pressure;
486 b->is_real = is_real;
489 list_add_tail(&b->list, head);
490 DBG((dbg, LEVEL_5, "\t\t%s adding %n, step: %d\n",
491 is_def ? "def" : "use", irn, step));
497 * Annotate the register pressure to the nodes and compute
498 * the liveness intervals.
499 * @param block The block to do it for.
500 * @param env_ptr The environment.
502 static void pressure(ir_node *block, void *env_ptr)
504 /* Convenience macro for a def */
505 #define border_def(irn, step, real) \
506 border_add(env, head, irn, step, pressure--, 1, real)
508 /* Convenience macro for a use */
509 #define border_use(irn, step, real) \
510 border_add(env, head, irn, step, ++pressure, 0, real)
512 env_t *env = env_ptr;
513 bitset_t *live = env->live;
518 unsigned pressure = 0;
519 struct list_head *head;
520 pset *live_in = get_live_in(block);
521 pset *live_end = get_live_end(block);
523 DBG((dbg, LEVEL_1, "Computing pressure in block %n\n", block));
524 bitset_clear_all(live);
526 /* Set up the border list in the block info */
527 head = &get_ra_block_info(block)->border_head;
528 INIT_LIST_HEAD(head);
531 * Make final uses of all values live out of the block.
532 * They are neccessary to build up real intervals.
534 for(irn = pset_first(live_end); irn; irn = pset_next(live_end)) {
535 DBG((dbg, LEVEL_3, "\tMaking live: %n/%d\n", irn, get_irn_graph_nr(irn)));
536 bitset_set(live, get_irn_graph_nr(irn));
537 if(is_allocatable_irn(irn))
538 border_use(irn, step, 0);
544 * Determine the last uses of a value inside the block, since they are
545 * relevant for the interval borders.
547 sched_foreach_reverse(block, irn) {
548 DBG((dbg, LEVEL_1, "\tinsn: %n, pressure: %d\n", irn, pressure));
549 DBG((dbg, LEVEL_2, "\tlive: %b\n", live));
551 /* Erase the color of each node encountered. */
552 set_irn_color(irn, NO_COLOR);
555 * If the node defines a datab value, i.e. something, registers must
556 * be allocated for, add a new def border to the border list.
558 if(is_allocatable_irn(irn)) {
559 int nr = get_irn_graph_nr(irn);
561 bitset_clear(live, nr);
562 border_def(irn, step, 1);
567 bitset_foreach(live, elm) {
568 int live_nr = (int) elm;
569 add_if(env, nr, live_nr);
576 * If the node is no phi node we can examine the uses.
579 for(i = 0, n = get_irn_arity(irn); i < n; ++i) {
580 ir_node *op = get_irn_n(irn, i);
582 if(is_allocatable_irn(op)) {
583 int nr = get_irn_graph_nr(op);
585 DBG((dbg, LEVEL_4, "\t\tpos: %d, use: %n\n", i, op));
587 if(!bitset_is_set(live, nr)) {
588 border_use(op, step, 1);
589 bitset_set(live, nr);
599 * Add initial defs for all values live in.
601 for(irn = pset_first(live_in); irn; irn = pset_next(live_in)) {
602 if(is_allocatable_irn(irn)) {
604 /* Mark the value live in. */
605 bitset_set(live, get_irn_graph_nr(irn));
608 border_def(irn, step, 0);
613 static void assign(ir_node *block, void *env_ptr)
615 env_t *env = env_ptr;
616 struct obstack *obst = &env->obst;
617 bitset_t *live = env->live;
618 bitset_t *colors = env->colors;
619 bitset_t *in_colors = env->in_colors;
621 /* The used colors will remain on the obstack. */
622 bitset_t *used_colors = bitset_obstack_alloc(obst, env->colors_n);
624 /* Mark the obstack level and allocate the temporary tmp_colors */
625 void *obstack_level = obstack_base(obst);
626 bitset_t *tmp_colors = bitset_obstack_alloc(obst, env->colors_n);
630 struct list_head *head = &get_ra_block_info(block)->border_head;
631 pset *live_in = get_live_in(block);
633 bitset_clear_all(live);
634 bitset_clear_all(colors);
635 bitset_clear_all(in_colors);
637 DBG((dbg, LEVEL_4, "Assigning colors for block %n\n", block));
638 DBG((dbg, LEVEL_4, "\tusedef chain for block\n"));
639 list_for_each_entry(border_t, b, head, list) {
640 DBG((dbg, LEVEL_4, "\t%s %n/%d\n", b->is_def ? "def" : "use",
641 b->irn, get_irn_graph_nr(b->irn)));
645 * Add initial defs for all values live in.
646 * Since their colors have already been assigned (The dominators were
647 * allocated before), we have to mark their colors as used also.
649 for(irn = pset_first(live_in); irn; irn = pset_next(live_in)) {
650 if(is_allocatable_irn(irn)) {
651 int col = get_irn_color(irn);
653 /* Mark the color of the live in value as used. */
654 assert(is_color(col) && "Node must have been assigned a color.");
655 bitset_set(colors, col);
656 bitset_set(in_colors, col);
657 bitset_set(used_colors, col);
659 /* Mark the value live in. */
660 bitset_set(live, get_irn_graph_nr(irn));
665 * Mind that the sequence of defs from back to front defines a perfect
666 * elimination order. So, coloring the definitions from first to last
669 list_for_each_entry_reverse(border_t, b, head, list) {
670 const ir_node *irn = b->irn;
671 int nr = get_irn_graph_nr(irn);
674 * Assign a color, if it is a local def. Global defs already have a
677 if(b->is_def && !is_live_in(block, irn)) {
678 ra_node_info_t *ri = get_ra_node_info(irn);
681 DBG((dbg, LEVEL_4, "\tcolors in use: %b\n", colors));
684 * Try to assign live out values colors which are not used by live
688 if(is_live_out(block, irn)) {
691 bitset_copy(tmp_colors, colors);
692 bitset_or(tmp_colors, in_colors);
693 next_clear = bitset_next_clear(tmp_colors, 0);
694 col = next_clear != -1 ? next_clear : NO_COLOR;
696 DBG((dbg, LEVEL_5, "next clear in only outs %b: %d\n", tmp_colors, col));
700 /* If a color is not yet assigned, do it now. */
702 col = bitset_next_clear(colors, 0);
704 assert(!is_color(get_irn_color(irn)) && "Color must not have assigned");
705 assert(!bitset_is_set(live, nr) && "Value def must not have been encountered");
707 bitset_set(colors, col);
708 bitset_set(used_colors, col);
709 bitset_set(live, nr);
713 DBG((dbg, LEVEL_1, "\tassigning color %d to %n\n", col, irn));
716 /* Clear the color upon a use. */
717 else if(!b->is_def) {
718 int col = get_irn_color(irn);
720 assert(bitset_is_set(live, nr) && "Cannot have a non live use");
721 assert(is_color(col) && "A color must have been assigned");
723 bitset_clear(colors, col);
724 bitset_clear(live, nr);
728 #ifdef DUMP_INTERVALS
729 draw_interval_graphs(block, head, &dump_params);
737 ir_snprintf(buf, sizeof(buf), "pres_%s_bl_%N.txt",
738 get_entity_name(get_irg_entity(irg)), block);
740 if((f = fopen(buf, "wt")) != NULL) {
741 sched_foreach_reverse(block, irn) {
742 if(is_allocatable_irn(irn))
743 ir_fprintf(f, "\"%n\" %d %d\n", irn, sched_get_time_step(irn),
744 get_ra_node_info(irn)->pressure);
754 * Allocate the used colors array in the blocks ra info structure and
757 get_ra_block_info(block)->used_colors = used_colors;
759 /* Free the auxillary data on the obstack. */
760 obstack_free(obst, obstack_level);
763 void be_ra_chordal_init(void)
765 dbg = firm_dbg_register(DBG_BERA);
766 firm_dbg_set_mask(dbg, 0);
769 void be_ra_chordal(ir_graph *irg)
771 int node_count = get_graph_node_count(irg);
772 env_t *env = malloc(sizeof(*env));
774 if(get_irg_dom_state(irg) != dom_consistent)
777 obstack_init(&env->obst);
780 env->graph = new_set(if_edge_cmp, node_count);
783 env->live = bitset_obstack_alloc(&env->obst, node_count);
784 env->colors = bitset_obstack_alloc(&env->obst, TEST_COLORS);
785 env->in_colors = bitset_obstack_alloc(&env->obst, TEST_COLORS);
786 env->colors_n = TEST_COLORS;
788 /* First, determine the pressure */
789 dom_tree_walk_irg(irg, pressure, NULL, env);
791 /* Insert probable spills */
792 be_ra_chordal_spill(irg);
794 /* Assign the colors */
795 dom_tree_walk_irg(irg, assign, NULL, env);
801 ir_snprintf(buf, sizeof(buf), "ifg_%s.dot", get_entity_name(get_irg_entity(irg)));
802 dump_ifg(irg, env->graph, buf);
806 set_irg_ra_link(irg, env);
809 void be_ra_chordal_done(ir_graph *irg)
811 env_t *env = get_irg_ra_link(irg);
817 obstack_free(&env->obst, NULL);
821 int phi_ops_interfere(const ir_node *a, const ir_node *b)
824 ir_graph *irg = get_irn_irg(a);
825 env_t *env = get_irg_ra_link(irg);
827 assert(irg == get_irn_irg(b) && "Both nodes must be in the same graph");
829 return are_connected(env, get_irn_graph_nr(a), get_irn_graph_nr(b));
831 return values_interfere(a, b);
832 #endif /* BUILD_GRAPH */
836 set *be_ra_get_ifg(ir_graph *irg) {
837 return ((env_t *)get_irg_ra_link(irg))->graph;