2 * Chordal register allocation.
3 * @author Sebastian Hack
6 * Copyright (C) Universitaet Karlsruhe
7 * Released under the GPL
22 #include "irgraph_t.h"
23 #include "irprintf_t.h"
34 #include "besched_t.h"
36 #include "bechordal_t.h"
44 #if defined(DUMP_IFG) && !defined(BUILD_GRAPH)
52 /* Make a fourcc for border checking. */
53 #define BORDER_FOURCC FOURCC('B', 'O', 'R', 'D')
55 #endif /* DEBUG_libfirm */
57 #define TEST_COLORS 2048
59 static firm_dbg_module_t *dbg;
62 * Environment for each of the chordal register allocator phases
64 typedef struct _env_t {
65 struct obstack obst; /**< An obstack for temporary storage. */
66 ir_graph *irg; /**< The graph the reg alloc is running on. */
68 set *nodes; /**< The interference graph nodes. */
69 set *edges; /**< The interference graph edges. */
72 bitset_t *live; /**< A liveness bitset. */
73 bitset_t *colors; /**< The color mask. */
74 bitset_t *in_colors; /**< Colors used by live in values. */
75 int colors_n; /**< The number of colors. */
76 const arch_env_t *arch_env; /**< The arch interface environment. */
77 const arch_register_class_t *cls; /**< The current register class. */
78 void *data; /**< Some pointer, to which different
79 phases can attach data to. */
83 typedef struct _be_chordal_dump_params_t {
87 } be_chordal_dump_params_t;
89 static const be_chordal_dump_params_t dump_params = {
97 static INLINE void intv_filename(char *s, size_t n,
98 const env_t *env, ir_node *block)
100 ir_snprintf(s, n, "intv_%F_%s_bl%N.eps",
101 env->irg, env->cls->name, block);
104 static void draw_interval_graph(const env_t *env,
105 ir_node *block, const be_chordal_dump_params_t *params)
108 int x_dist = params->x_dist;
109 int y_dist = params->y_dist;
110 ir_graph *irg = env->irg;
111 struct list_head *border_head = get_block_border_head(block);
116 intv_filename(buf, sizeof(buf), env, block);
118 if((f = fopen(buf, "wt")) != NULL) {
120 int *seen = xcalloc(get_graph_node_count(irg), sizeof(seen[0]));
121 int last_pos = list_empty(border_head) ? 0 : list_entry(border_head->prev, border_t, list)->step;
124 list_for_each_entry_reverse(border_t, b, border_head, list) {
125 const ir_node *irn = b->irn;
126 int col = get_irn_color(irn);
127 max_col = max_col > col ? max_col : col;
130 fprintf(f, "%%!PS-Adobe-2.0\n");
131 fprintf(f, "%%%%BoundingBox: -10 -10 %d %d\n",
132 x_dist * last_pos + x_dist, y_dist * max_col + y_dist);
133 fprintf(f, "/mainfont /Courier findfont %f scalefont def\n", params->font_scale);
134 fprintf(f, "mainfont setfont\n");
135 fprintf(f, "0.2 setlinewidth\n");
137 for(i = 0; i <= last_pos; ++i) {
138 fprintf(f, "0 0 0 setrgbcolor\n");
139 fprintf(f, "%d %d moveto\n", i * x_dist, -2);
140 fprintf(f, "%d %d lineto\n", i * x_dist, max_col * y_dist + 2);
141 fprintf(f, "stroke\n");
143 fprintf(f, "0.5 setlinewidth\n");
145 list_for_each_entry_reverse(border_t, b, border_head, list) {
146 const ir_node *irn = b->irn;
147 int nr = get_irn_graph_nr(irn);
152 int col = get_irn_color(irn);
154 int pos = last_pos - seen[nr];
155 int end_pos = last_pos - b->step;
156 int live_in = is_live_in(block, irn);
157 int live_end = is_live_end(block, irn);
158 int y_val = y_dist * col;
161 int green = live_end;
164 fprintf(f, "0 0 0 setrgbcolor\n");
165 fprintf(f, "%d %d moveto\n", x_dist * pos + 2, y_val + 2);
166 ir_fprintf(f, "(%n/%d%s) show\n", irn, nr, is_phi_operand(irn) ? "*" : "");
167 fprintf(f, "%d %d %d setrgbcolor\n", red, green, blue);
168 fprintf(f, "%d %d moveto\n", x_dist * pos, y_val);
169 fprintf(f, "%d %d lineto\n", (x_dist * end_pos) - 5, y_val);
170 fprintf(f, "stroke\n");
179 static void dump_block(ir_node *bl, void *data)
181 const env_t *env = data;
185 draw_interval_graph(env, bl, &dump_params);
187 intv_filename(buf, sizeof(buf), env, bl);
188 ir_fprintf(f, "\tb%N [shape=\"epsf\" shapefile=\"%s\"];\n", bl, buf);
191 static void dump_edges(ir_node *bl, void *data)
193 const env_t *env = data;
199 for(i = 0, n = get_irn_arity(bl); i < n; ++i) {
200 ir_node *pred = get_irn_n(bl, i);
201 ir_fprintf(f, "\tb%N -> b%N;\n", get_nodes_block(pred), bl);
205 for(dom = get_Block_dominated_first(bl); dom;
206 dom = get_Block_dominated_next(dom)) {
208 ir_fprintf(f, "\tb%N -> b%N;\n", dom, bl);
212 static void dump_intv_cfg(env_t *env)
217 ir_snprintf(buf, sizeof(buf), "intv_cfg_%s_%F.dot",
218 env->cls->name, env->irg);
220 if((f = fopen(buf, "wt")) != NULL) {
221 void *old_data = env->data;
224 ir_fprintf(f, "digraph G {\n");
225 ir_fprintf(f, "\tgraph [rankdir=\"LR\", ordering=\"out\"];\n");
226 dom_tree_walk_irg(env->irg, dump_block, dump_edges, env);
227 // irg_block_walk_graph(env->irg, dump_block, dump_edges, env);
228 ir_fprintf(f, "}\n");
231 env->data = old_data;
239 #define IF_EDGE_HASH(e) ((e)->src)
240 #define IF_NODE_HASH(n) ((n)->nnr)
242 static int if_edge_cmp(const void *p1, const void *p2, size_t size)
244 const if_edge_t *e1 = p1;
245 const if_edge_t *e2 = p2;
247 return !(e1->src == e2->src && e1->tgt == e2->tgt);
250 static int if_node_cmp(const void *p1, const void *p2, size_t size)
252 const if_node_t *n1 = p1;
253 const if_node_t *n2 = p2;
255 return n1->nnr != n2->nnr;
258 static INLINE if_edge_t *edge_init(if_edge_t *edge, int src, int tgt)
260 /* Bring the smaller entry to src. */
272 static INLINE void add_if(const env_t *env, int src, int tgt)
275 if_node_t node, *src_node, *tgt_node;
277 edge_init(&edge, src, tgt);
278 set_insert(env->edges, &edge, sizeof(edge), IF_EDGE_HASH(&edge));
282 node.neighb = pset_new_ptr(8);
283 src_node = set_insert(env->nodes, &node, sizeof(node), IF_NODE_HASH(&node));
285 node.neighb = pset_new_ptr(8);
286 tgt_node = set_insert(env->nodes, &node, sizeof(node), IF_NODE_HASH(&node));
288 /* insert neighbors into nodes */
289 pset_insert_ptr(src_node->neighb, tgt_node);
290 pset_insert_ptr(tgt_node->neighb, src_node);
293 static INLINE int are_connected(const env_t *env, int src, int tgt)
296 edge_init(&edge, src, tgt);
297 return set_find(env->edges, &edge, sizeof(edge), IF_EDGE_HASH(&edge)) != NULL;
300 int ifg_has_edge(const ir_graph *irg, if_node_t *n1, if_node_t* n2) {
301 return are_connected(get_irg_ra_link(irg), n1->nnr, n2->nnr);
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: %u, 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 */
432 * Add an interval border to the list of a block's list
433 * of interval border.
434 * @note You always have to create the use before the def.
435 * @param env The environment.
436 * @param head The list head to enqueue the borders.
437 * @param irn The node (value) the border belongs to.
438 * @param pressure The pressure at this point in time.
439 * @param step A time step for the border.
440 * @param is_def Is the border a use or a def.
441 * @return The created border.
443 static INLINE border_t *border_add(env_t *env, struct list_head *head,
444 ir_node *irn, unsigned step, unsigned pressure,
445 unsigned is_def, unsigned is_real)
452 b = obstack_alloc(&env->obst, sizeof(*b));
454 /* also allocate the def and tie it to the use. */
455 def = obstack_alloc(&env->obst, sizeof(*def));
460 * Set the link field of the irn to the def.
461 * This strongly relies on the fact, that the use is always
462 * made before the def.
464 set_irn_link(irn, def);
467 b->magic = BORDER_FOURCC;
468 def->magic = BORDER_FOURCC;
473 * If the def is encountered, the use was made and so was the
474 * the def node (see the code above). It was placed into the
475 * link field of the irn, so we can get it there.
478 b = get_irn_link(irn);
481 assert(b && b->magic == BORDER_FOURCC && "Illegal border encountered");
485 b->pressure = pressure;
487 b->is_real = is_real;
490 list_add_tail(&b->list, head);
491 DBG((dbg, LEVEL_5, "\t\t%s adding %n, step: %d\n",
492 is_def ? "def" : "use", irn, step));
498 * Annotate the register pressure to the nodes and compute
499 * the liveness intervals.
500 * @param block The block to do it for.
501 * @param env_ptr The environment.
503 static void pressure(ir_node *block, void *env_ptr)
505 /* Convenience macro for a def */
506 #define border_def(irn, step, real) \
507 border_add(env, head, irn, step, pressure--, 1, real)
509 /* Convenience macro for a use */
510 #define border_use(irn, step, real) \
511 border_add(env, head, irn, step, ++pressure, 0, real)
513 env_t *env = env_ptr;
514 bitset_t *live = env->live;
519 unsigned pressure = 0;
520 struct list_head *head;
521 pset *live_in = get_live_in(block);
522 pset *live_end = get_live_end(block);
523 const arch_register_class_t *cls = env->cls;
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(arch_irn_has_reg_class(env->arch_env, irn, 0, cls))
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 some value, which can put into a
558 * register of the current class, make a border for it.
560 if(arch_irn_has_reg_class(env->arch_env, irn, 0, cls)) {
562 int nr = get_irn_graph_nr(irn);
564 bitset_clear(live, nr);
565 border_def(irn, step, 1);
568 bitset_foreach(live, elm)
569 add_if(env, nr, (int) elm);
574 * If the node is no phi node we can examine the uses.
577 for(i = 0, n = get_irn_arity(irn); i < n; ++i) {
578 ir_node *op = get_irn_n(irn, i);
580 if(arch_irn_has_reg_class(env->arch_env, op, 0, cls)) {
581 int nr = get_irn_graph_nr(op);
583 DBG((dbg, LEVEL_4, "\t\tpos: %d, use: %n\n", i, op));
585 if(!bitset_is_set(live, nr)) {
586 border_use(op, step, 1);
587 bitset_set(live, nr);
597 * Add initial defs for all values live in.
599 for(irn = pset_first(live_in); irn; irn = pset_next(live_in)) {
600 if(arch_irn_has_reg_class(env->arch_env, irn, 0, cls)) {
602 /* Mark the value live in. */
603 bitset_set(live, get_irn_graph_nr(irn));
606 border_def(irn, step, 0);
611 static void assign(ir_node *block, void *env_ptr)
613 env_t *env = env_ptr;
614 struct obstack *obst = &env->obst;
615 bitset_t *live = env->live;
616 bitset_t *colors = env->colors;
617 bitset_t *in_colors = env->in_colors;
618 const arch_register_class_t *cls = env->cls;
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_irn_has_reg_class(env->arch_env, irn, 0, cls)) {
647 const arch_register_t *reg = arch_get_irn_register(env->arch_env, irn, 0);
650 assert(reg && "Nodfe must have been assigned a register");
651 col = arch_register_get_index(reg);
653 /* Mark the color of the live in value as used. */
654 bitset_set(colors, col);
655 bitset_set(in_colors, col);
657 /* Mark the value live in. */
658 bitset_set(live, get_irn_graph_nr(irn));
663 * Mind that the sequence of defs from back to front defines a perfect
664 * elimination order. So, coloring the definitions from first to last
667 list_for_each_entry_reverse(border_t, b, head, list) {
668 ir_node *irn = b->irn;
669 int nr = get_irn_graph_nr(irn);
672 * Assign a color, if it is a local def. Global defs already have a
675 if(b->is_def && !is_live_in(block, irn)) {
676 const arch_register_t *reg;
679 DBG((dbg, LEVEL_4, "\tcolors in use: %b\n", colors));
681 col = bitset_next_clear(colors, 0);
682 reg = arch_register_for_index(env->cls, col);
684 assert(arch_get_irn_register(env->arch_env, irn, 0) == NULL
685 && "This node must not have been assigned a register yet");
686 assert(!bitset_is_set(live, nr) && "Value's definition must not have been encountered");
688 bitset_set(colors, col);
689 bitset_set(live, nr);
691 arch_set_irn_register(env->arch_env, irn, 0, reg);
692 DBG((dbg, LEVEL_1, "\tassigning register %s(%d) to %n\n",
693 arch_register_get_name(reg), col, irn));
696 /* Clear the color upon a use. */
697 else if(!b->is_def) {
698 const arch_register_t *reg = arch_get_irn_register(env->arch_env, irn, 0);
701 assert(reg && "Register must have been assigned");
703 col = arch_register_get_index(reg);
704 assert(bitset_is_set(live, nr) && "Cannot have a non live use");
706 bitset_clear(colors, col);
707 bitset_clear(live, nr);
711 /* Free the auxillary data on the obstack. */
712 obstack_free(obst, obstack_level);
715 void be_ra_chordal_init(void)
717 dbg = firm_dbg_register(DBG_BERA);
718 firm_dbg_set_mask(dbg, 0);
721 void be_ra_chordal(ir_graph *irg,
722 const arch_env_t *arch_env,
723 const arch_register_class_t *cls)
725 int node_count = get_graph_node_count(irg);
726 int colors_n = arch_register_class_n_regs(cls);
727 env_t *env = malloc(sizeof(*env));
729 if(get_irg_dom_state(irg) != dom_consistent)
732 obstack_init(&env->obst);
735 env->edges = new_set(if_edge_cmp, node_count);
736 env->nodes = new_set(if_node_cmp, node_count);
739 env->live = bitset_obstack_alloc(&env->obst, node_count);
740 env->colors = bitset_obstack_alloc(&env->obst, colors_n);
741 env->in_colors = bitset_obstack_alloc(&env->obst, colors_n);
742 env->colors_n = colors_n;
744 env->arch_env = arch_env;
747 /* First, determine the pressure */
748 dom_tree_walk_irg(irg, pressure, NULL, env);
750 /* Insert probable spills */
751 be_ra_chordal_spill(irg);
753 /* Assign the colors */
754 dom_tree_walk_irg(irg, assign, NULL, env);
760 ir_snprintf(buf, sizeof(buf), "ifg_%F.dot", irg);
761 dump_ifg(irg, env->edges, buf);
765 #ifdef DUMP_INTERVALS
769 set_irg_ra_link(irg, env);
772 void be_ra_chordal_done(ir_graph *irg)
774 env_t *env = get_irg_ra_link(irg);
779 for(ifn = set_first(env->nodes); ifn; ifn = set_next(env->nodes))
786 obstack_free(&env->obst, NULL);
790 int phi_ops_interfere(const ir_node *a, const ir_node *b)
793 ir_graph *irg = get_irn_irg(a);
794 env_t *env = get_irg_ra_link(irg);
796 assert(irg == get_irn_irg(b) && "Both nodes must be in the same graph");
798 return are_connected(env, get_irn_graph_nr(a), get_irn_graph_nr(b));
800 return values_interfere(a, b);
801 #endif /* BUILD_GRAPH */
806 * Deprecated. Use be_ra_get_ifg_edges instead.
808 set *be_ra_get_ifg(ir_graph *irg) {
809 return ((env_t *)get_irg_ra_link(irg))->edges;
812 set *be_ra_get_ifg_edges(ir_graph *irg) {
813 return ((env_t *)get_irg_ra_link(irg))->edges;
816 set *be_ra_get_ifg_nodes(ir_graph *irg) {
817 return ((env_t *)get_irg_ra_link(irg))->nodes;