2 * Chordal register allocation.
3 * @author Sebastian Hack
6 * Copyright (C) Universitaet Karlsruhe
7 * Released under the GPL
23 #include "irgraph_t.h"
24 #include "irprintf_t.h"
34 #include "besched_t.h"
38 #include "bechordal_t.h"
39 #include "bechordal_draw.h"
41 #define DBG_LEVEL SET_LEVEL_0
42 #define DBG_LEVEL_CHECK SET_LEVEL_0
50 #if defined(DUMP_IFG) && !defined(BUILD_GRAPH)
51 #error Must define BUILD_GRAPH to be able to dump it.
57 /* Make a fourcc for border checking. */
58 #define BORDER_FOURCC FOURCC('B', 'O', 'R', 'D')
60 static firm_dbg_module_t *dbg;
64 #define IF_EDGE_HASH(e) ((e)->src ^ (e)->tgt)
65 #define IF_NODE_HASH(n) ((n)->nnr)
67 static int if_edge_cmp(const void *p1, const void *p2, size_t size)
69 const if_edge_t *e1 = p1;
70 const if_edge_t *e2 = p2;
72 return !(e1->src == e2->src && e1->tgt == e2->tgt);
75 static int if_node_cmp(const void *p1, const void *p2, size_t size)
77 const if_node_t *n1 = p1;
78 const if_node_t *n2 = p2;
80 return n1->nnr != n2->nnr;
83 static INLINE if_edge_t *edge_init(if_edge_t *edge, int src, int tgt)
85 /* Bring the smaller entry to src. */
97 static INLINE void add_if(const be_chordal_env_t *env, int src, int tgt)
100 if_node_t node, *src_node, *tgt_node;
102 edge_init(&edge, src, tgt);
103 set_insert(env->edges, &edge, sizeof(edge), IF_EDGE_HASH(&edge));
107 node.neighb = pset_new_ptr(8);
108 src_node = set_insert(env->nodes, &node, sizeof(node), IF_NODE_HASH(&node));
110 node.neighb = pset_new_ptr(8);
111 tgt_node = set_insert(env->nodes, &node, sizeof(node), IF_NODE_HASH(&node));
113 /* insert neighbors into nodes */
114 pset_insert_ptr(src_node->neighb, tgt_node);
115 pset_insert_ptr(tgt_node->neighb, src_node);
118 static INLINE int are_connected(const be_chordal_env_t *env, int src, int tgt)
121 edge_init(&edge, src, tgt);
122 return set_find(env->edges, &edge, sizeof(edge), IF_EDGE_HASH(&edge)) != NULL;
125 int ifg_has_edge(const be_chordal_env_t *env, const if_node_t *n1, const if_node_t* n2) {
126 return are_connected(env, n1->nnr, n2->nnr);
131 static void dump_ifg(const be_chordal_env_t *env)
134 set *edges = env->edges;
135 ir_graph *irg = env->irg;
138 ir_snprintf(filename, sizeof(filename), "ifg_%s_%F.dot", env->cls->name, irg);
140 if((f = fopen(filename, "wt")) != NULL) {
144 bitset_t *bs = bitset_malloc(get_graph_node_count(irg));
146 ir_fprintf(f, "graph \"%F\" {\n", irg);
147 fprintf(f, "\tnode [shape=box,style=filled]\n");
149 for(edge = set_first(edges); edge; edge = set_next(edges)) {
150 bitset_set(bs, edge->src);
151 bitset_set(bs, edge->tgt);
155 fprintf(f, "\tx [label=\"nodes: %u, edges: %d\"]\n", bitset_popcnt(bs), n_edges);
157 bitset_foreach(bs, pos) {
159 ir_node *irn = get_irn_for_graph_nr(irg, nr);
161 ir_fprintf(f, "\tn%d [label=\"%+F\"]\n", nr, irn);
164 for(edge = set_first(edges); edge; edge = set_next(edges)) {
165 fprintf(f, "\tn%d -- n%d [len=5]\n", edge->src, edge->tgt);
176 #endif /* DUMP_IFG */
178 #endif /* BUILD_GRAPH */
180 static void check_border_list(struct list_head *head)
183 list_for_each_entry(border_t, x, head, list) {
184 assert(x->magic == BORDER_FOURCC);
188 static void check_heads(be_chordal_env_t *env)
191 for(ent = pmap_first(env->border_heads); ent; ent = pmap_next(env->border_heads)) {
192 /* ir_printf("checking border list of block %+F\n", ent->key); */
193 check_border_list(ent->value);
199 * Add an interval border to the list of a block's list
200 * of interval border.
201 * @note You always have to create the use before the def.
202 * @param env The environment.
203 * @param head The list head to enqueue the borders.
204 * @param irn The node (value) the border belongs to.
205 * @param pressure The pressure at this point in time.
206 * @param step A time step for the border.
207 * @param is_def Is the border a use or a def.
208 * @return The created border.
210 static INLINE border_t *border_add(be_chordal_env_t *env, struct list_head *head,
211 ir_node *irn, unsigned step, unsigned pressure,
212 unsigned is_def, unsigned is_real)
219 b = obstack_alloc(&env->obst, sizeof(*b));
221 /* also allocate the def and tie it to the use. */
222 def = obstack_alloc(&env->obst, sizeof(*def));
223 memset(def, 0, sizeof(*def));
228 * Set the link field of the irn to the def.
229 * This strongly relies on the fact, that the use is always
230 * made before the def.
232 set_irn_link(irn, def);
234 b->magic = BORDER_FOURCC;
235 def->magic = BORDER_FOURCC;
239 * If the def is encountered, the use was made and so was the
240 * the def node (see the code above). It was placed into the
241 * link field of the irn, so we can get it there.
244 b = get_irn_link(irn);
246 assert(b && b->magic == BORDER_FOURCC && "Illegal border encountered");
249 b->pressure = pressure;
251 b->is_real = is_real;
254 list_add_tail(&b->list, head);
255 DBG((dbg, LEVEL_5, "\t\t%s adding %+F, step: %d\n",
256 is_def ? "def" : "use", irn, step));
262 static INLINE int has_reg_class(const be_chordal_env_t *env, const ir_node *irn)
264 return arch_irn_has_reg_class(env->session_env->main_env->arch_env,
265 irn, arch_pos_make_out(0), env->cls);
269 * Annotate the register pressure to the nodes and compute
270 * the liveness intervals.
271 * @param block The block to do it for.
272 * @param env_ptr The environment.
274 static void pressure(ir_node *block, void *env_ptr)
276 /* Convenience macro for a def */
277 #define border_def(irn, step, real) \
278 border_add(env, head, irn, step, pressure--, 1, real)
280 /* Convenience macro for a use */
281 #define border_use(irn, step, real) \
282 border_add(env, head, irn, step, ++pressure, 0, real)
284 be_chordal_env_t *env = env_ptr;
285 bitset_t *live = env->live;
290 unsigned pressure = 0;
291 struct list_head *head;
292 pset *live_in = put_live_in(block, pset_new_ptr_default());
293 pset *live_end = put_live_end(block, pset_new_ptr_default());
295 DBG((dbg, LEVEL_1, "Computing pressure in block %+F\n", block));
296 bitset_clear_all(live);
298 /* Set up the border list in the block info */
299 head = obstack_alloc(&env->obst, sizeof(*head));
300 INIT_LIST_HEAD(head);
301 assert(pmap_get(env->border_heads, block) == NULL);
302 pmap_insert(env->border_heads, block, head);
305 * Make final uses of all values live out of the block.
306 * They are necessary to build up real intervals.
308 for(irn = pset_first(live_end); irn; irn = pset_next(live_end)) {
309 if(has_reg_class(env, irn)) {
310 DBG((dbg, LEVEL_3, "\tMaking live: %+F/%d\n", irn, get_irn_graph_nr(irn)));
311 bitset_set(live, get_irn_graph_nr(irn));
312 border_use(irn, step, 0);
318 * Determine the last uses of a value inside the block, since they are
319 * relevant for the interval borders.
321 sched_foreach_reverse(block, irn) {
322 DBG((dbg, LEVEL_1, "\tinsn: %+F, pressure: %d\n", irn, pressure));
323 DBG((dbg, LEVEL_2, "\tlive: %b\n", live));
326 * If the node defines some value, which can put into a
327 * register of the current class, make a border for it.
329 if(has_reg_class(env, irn)) {
331 int nr = get_irn_graph_nr(irn);
333 bitset_clear(live, nr);
334 border_def(irn, step, 1);
337 bitset_foreach(live, elm)
338 add_if(env, nr, (int) elm);
343 * If the node is no phi node we can examine the uses.
346 for(i = 0, n = get_irn_arity(irn); i < n; ++i) {
347 ir_node *op = get_irn_n(irn, i);
349 if(has_reg_class(env, op)) {
350 int nr = get_irn_graph_nr(op);
352 DBG((dbg, LEVEL_4, "\t\tpos: %d, use: %+F\n", i, op));
354 if(!bitset_is_set(live, nr)) {
355 border_use(op, step, 1);
356 bitset_set(live, nr);
365 * Add initial defs for all values live in.
367 for(irn = pset_first(live_in); irn; irn = pset_next(live_in)) {
368 if(has_reg_class(env, irn)) {
370 /* Mark the value live in. */
371 bitset_set(live, get_irn_graph_nr(irn));
374 border_def(irn, step, 0);
383 static void assign(ir_node *block, void *env_ptr)
385 be_chordal_env_t *env = env_ptr;
386 bitset_t *live = env->live;
387 bitset_t *colors = env->colors;
388 bitset_t *in_colors = env->in_colors;
389 const arch_env_t *arch_env = env->session_env->main_env->arch_env;
393 struct list_head *head = get_block_border_head(env, block);
394 pset *live_in = put_live_in(block, pset_new_ptr_default());
396 bitset_clear_all(live);
397 bitset_clear_all(colors);
398 bitset_clear_all(in_colors);
400 DBG((dbg, LEVEL_4, "Assigning colors for block %+F\n", block));
401 DBG((dbg, LEVEL_4, "\tusedef chain for block\n"));
402 list_for_each_entry(border_t, b, head, list) {
403 DBG((dbg, LEVEL_4, "\t%s %+F/%d\n", b->is_def ? "def" : "use",
404 b->irn, get_irn_graph_nr(b->irn)));
408 * Add initial defs for all values live in.
409 * Since their colors have already been assigned (The dominators were
410 * allocated before), we have to mark their colors as used also.
412 for(irn = pset_first(live_in); irn; irn = pset_next(live_in)) {
413 if(has_reg_class(env, irn)) {
414 const arch_register_t *reg = arch_get_irn_register(arch_env, irn, 0);
417 assert(reg && "Node must have been assigned a register");
418 col = arch_register_get_index(reg);
420 /* Mark the color of the live in value as used. */
421 bitset_set(colors, col);
422 bitset_set(in_colors, col);
424 /* Mark the value live in. */
425 bitset_set(live, get_irn_graph_nr(irn));
430 * Mind that the sequence of defs from back to front defines a perfect
431 * elimination order. So, coloring the definitions from first to last
434 list_for_each_entry_reverse(border_t, b, head, list) {
435 ir_node *irn = b->irn;
436 int nr = get_irn_graph_nr(irn);
439 * Assign a color, if it is a local def. Global defs already have a
442 if(b->is_def && !is_live_in(block, irn)) {
443 const arch_register_t *reg;
446 DBG((dbg, LEVEL_4, "\tcolors in use: %b\n", colors));
448 col = bitset_next_clear(colors, 0);
449 reg = arch_register_for_index(env->cls, col);
451 assert(arch_get_irn_register(arch_env, irn, 0) == NULL && "This node must not have been assigned a register yet");
452 assert(!bitset_is_set(live, nr) && "Value's definition must not have been encountered");
454 bitset_set(colors, col);
455 bitset_set(live, nr);
457 arch_set_irn_register(arch_env, irn, 0, reg);
458 DBG((dbg, LEVEL_1, "\tassigning register %s(%d) to %+F\n",
459 arch_register_get_name(reg), col, irn));
462 /* Clear the color upon a use. */
463 else if(!b->is_def) {
464 const arch_register_t *reg = arch_get_irn_register(arch_env, irn, 0);
467 assert(reg && "Register must have been assigned");
469 col = arch_register_get_index(reg);
470 assert(bitset_is_set(live, nr) && "Cannot have a non live use");
472 bitset_clear(colors, col);
473 bitset_clear(live, nr);
480 void be_ra_chordal_init(void)
482 dbg = firm_dbg_register(DBG_CHORDAL);
483 firm_dbg_set_mask(dbg, DBG_LEVEL);
486 be_chordal_env_t *be_ra_chordal(
487 const be_main_session_env_t *session,
488 const arch_register_class_t *cls)
490 ir_graph *irg = session->irg;
491 int node_count = get_graph_node_count(irg);
492 int colors_n = arch_register_class_n_regs(cls);
493 be_chordal_env_t *env = malloc(sizeof(*env));
495 if(get_irg_dom_state(irg) != dom_consistent)
498 obstack_init(&env->obst);
501 env->edges = new_set(if_edge_cmp, node_count);
502 env->nodes = new_set(if_node_cmp, node_count);
505 env->session_env = session;
506 env->live = bitset_obstack_alloc(&env->obst, node_count);
507 env->colors = bitset_obstack_alloc(&env->obst, colors_n);
508 env->in_colors = bitset_obstack_alloc(&env->obst, colors_n);
509 env->colors_n = colors_n;
511 env->border_heads = pmap_create();
513 /* First, determine the pressure */
514 dom_tree_walk_irg(irg, pressure, NULL, env);
516 /* Insert probable spills */
517 be_ra_chordal_spill(env);
519 /* Assign the colors */
520 dom_tree_walk_irg(irg, assign, NULL, env);
526 #ifdef DUMP_INTERVALS
531 ir_snprintf(buf, sizeof(buf), "ifg_%s_%F.eps", cls->name, irg);
532 plotter = new_plotter_ps(buf);
534 draw_interval_tree(&draw_chordal_def_opts, env, plotter, env->session_env->main_env->arch_env, cls);
535 plotter_free(plotter);
541 void be_ra_chordal_check(be_chordal_env_t *chordal_env) {
542 const arch_env_t *arch_env = chordal_env->session_env->main_env->arch_env;
545 ir_node **nodes, *n1, *n2;
548 /* Collect all irns */
550 pmap_foreach(chordal_env->border_heads, pme) {
552 struct list_head *head = pme->value;
553 list_for_each_entry(border_t, curr, head, list)
554 if (curr->is_def && curr->is_real)
555 if (arch_get_irn_reg_class(arch_env, curr->irn, arch_pos_make_out(0)) == chordal_env->cls)
556 obstack_ptr_grow(&ob, curr->irn);
558 obstack_ptr_grow(&ob, NULL);
559 nodes = (ir_node **) obstack_finish(&ob);
562 for (i = 0, n1 = nodes[i]; n1; n1 = nodes[++i]) {
563 const arch_register_t *n1_reg, *n2_reg;
565 n1_reg = arch_get_irn_register(arch_env, n1, 0);
566 if (!arch_reg_is_allocatable(arch_env, n1, arch_pos_make_out(0), n1_reg)) {
567 DBG((dbg, 0, "Register assigned to %+F is not allowed\n", n1));
568 assert(0 && "Register constraint does not hold");
570 for (o = i+1, n2 = nodes[o]; n2; n2 = nodes[++o]) {
571 n2_reg = arch_get_irn_register(arch_env, n2, 0);
572 if (nodes_interfere(chordal_env, n1, n2) && n1_reg == n2_reg) {
573 DBG((dbg, 0, "Values %+F and %+F interfere and have the same register assigned\n", n1, n2));
574 assert(0 && "Interfering values have the same color!");
578 obstack_free(&ob, NULL);
581 /* BETTER #ifdef BUILD_GRAPH --> faster version of checker with edges */
583 void be_ra_chordal_done(be_chordal_env_t *env)
588 for(ifn = set_first(env->nodes); ifn; ifn = set_next(env->nodes))
595 pmap_destroy(env->border_heads);
596 obstack_free(&env->obst, NULL);
602 int nodes_interfere(const be_chordal_env_t *env, const ir_node *a, const ir_node *b)
605 return are_connected(env, get_irn_graph_nr(a), get_irn_graph_nr(b));
607 return values_interfere(a, b);
608 #endif /* BUILD_GRAPH */
613 set *be_ra_get_ifg_edges(const be_chordal_env_t *env) {
617 set *be_ra_get_ifg_nodes(const be_chordal_env_t *env) {
624 const be_main_session_env_t *env;
625 const arch_register_class_t *cls;
626 } check_pressure_info_t;
629 static int check_pressure_has_class(const check_pressure_info_t *i, const ir_node *irn)
631 return arch_irn_has_reg_class(i->env->main_env->arch_env,
632 irn, arch_pos_make_out(0), i->cls);
635 static void check_pressure_walker(ir_node *bl, void *data)
637 firm_dbg_module_t *dbg = firm_dbg_register("be.ra.pressure");
638 check_pressure_info_t *info = data;
639 int n_regs = arch_register_class_n_regs(info->cls);
641 pset *live = pset_new_ptr_default();
646 firm_dbg_set_mask(dbg, DBG_LEVEL_CHECK);
648 live_foreach(bl, li) {
649 if(live_is_end(li) && check_pressure_has_class(info, li->irn)) {
650 ir_node *irn = (ir_node *) li->irn;
651 pset_insert_ptr(live, irn);
655 DBG((dbg, LEVEL_1, "end set for %+F\n", bl));
656 for(irn = pset_first(live); irn; irn = pset_next(live))
657 DBG((dbg, LEVEL_1, "\t%+F\n", irn));
659 sched_foreach_reverse(bl, irn) {
661 int pressure = pset_count(live);
663 DBG((dbg, LEVEL_1, "%+10F@%+10F: pressure %d\n", bl, irn, pressure));
665 if(pressure > n_regs) {
667 ir_printf("%+10F@%+10F: pressure to high: %d\n", bl, irn, pressure);
668 for(x = pset_first(live); x; x = pset_next(live))
669 ir_printf("\t%+10F\n", x);
672 if(check_pressure_has_class(info, irn))
673 pset_remove_ptr(live, irn);
675 for(i = 0, n = get_irn_arity(irn); i < n; i++) {
676 ir_node *op = get_irn_n(irn, i);
677 if(check_pressure_has_class(info, op) && !is_Phi(irn))
678 pset_insert_ptr(live, op);
684 void be_check_pressure(const be_main_session_env_t *env, const arch_register_class_t *cls)
686 check_pressure_info_t i;
689 irg_block_walk_graph(env->irg, check_pressure_walker, NULL, &i);