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"
47 #if defined(DUMP_IFG) && !defined(BUILD_GRAPH)
48 #error Must define BUILD_GRAPH to be able to dump it.
54 /* Make a fourcc for border checking. */
55 #define BORDER_FOURCC FOURCC('B', 'O', 'R', 'D')
57 static firm_dbg_module_t *dbg;
61 #define IF_EDGE_HASH(e) ((e)->src)
62 #define IF_NODE_HASH(n) ((n)->nnr)
64 static int if_edge_cmp(const void *p1, const void *p2, size_t size)
66 const if_edge_t *e1 = p1;
67 const if_edge_t *e2 = p2;
69 return !(e1->src == e2->src && e1->tgt == e2->tgt);
72 static int if_node_cmp(const void *p1, const void *p2, size_t size)
74 const if_node_t *n1 = p1;
75 const if_node_t *n2 = p2;
77 return n1->nnr != n2->nnr;
80 static INLINE if_edge_t *edge_init(if_edge_t *edge, int src, int tgt)
82 /* Bring the smaller entry to src. */
94 static INLINE void add_if(const be_chordal_env_t *env, int src, int tgt)
97 if_node_t node, *src_node, *tgt_node;
99 edge_init(&edge, src, tgt);
100 set_insert(env->edges, &edge, sizeof(edge), IF_EDGE_HASH(&edge));
104 node.neighb = pset_new_ptr(8);
105 src_node = set_insert(env->nodes, &node, sizeof(node), IF_NODE_HASH(&node));
107 node.neighb = pset_new_ptr(8);
108 tgt_node = set_insert(env->nodes, &node, sizeof(node), IF_NODE_HASH(&node));
110 /* insert neighbors into nodes */
111 pset_insert_ptr(src_node->neighb, tgt_node);
112 pset_insert_ptr(tgt_node->neighb, src_node);
115 static INLINE int are_connected(const be_chordal_env_t *env, int src, int tgt)
118 edge_init(&edge, src, tgt);
119 return set_find(env->edges, &edge, sizeof(edge), IF_EDGE_HASH(&edge)) != NULL;
122 int ifg_has_edge(const be_chordal_env_t *env, const if_node_t *n1, const if_node_t* n2) {
123 return are_connected(env, n1->nnr, n2->nnr);
128 static void dump_ifg(const be_chordal_env_t *env)
131 set *edges = env->edges;
132 ir_graph *irg = env->irg;
135 ir_snprintf(filename, sizeof(filename), "ifg_%s_%F.dot", env->cls->name, irg);
137 if((f = fopen(filename, "wt")) != NULL) {
141 bitset_t *bs = bitset_malloc(get_graph_node_count(irg));
143 ir_fprintf(f, "graph \"%F\" {\n", irg);
144 fprintf(f, "\tnode [shape=box,style=filled]\n");
146 for(edge = set_first(edges); edge; edge = set_next(edges)) {
147 bitset_set(bs, edge->src);
148 bitset_set(bs, edge->tgt);
152 fprintf(f, "\tx [label=\"nodes: %u, edges: %d\"]\n", bitset_popcnt(bs), n_edges);
154 bitset_foreach(bs, pos) {
156 ir_node *irn = get_irn_for_graph_nr(irg, nr);
158 ir_fprintf(f, "\tn%d [label=\"%+F\"]\n", nr, irn);
161 for(edge = set_first(edges); edge; edge = set_next(edges)) {
162 fprintf(f, "\tn%d -- n%d [len=5]\n", edge->src, edge->tgt);
173 #endif /* DUMP_IFG */
175 #endif /* BUILD_GRAPH */
177 static void check_border_list(struct list_head *head)
180 list_for_each_entry(border_t, x, head, list) {
181 assert(x->magic == BORDER_FOURCC);
185 static void check_heads(be_chordal_env_t *env)
188 for(ent = pmap_first(env->border_heads); ent; ent = pmap_next(env->border_heads)) {
189 /* ir_printf("checking border list of block %+F\n", ent->key); */
190 check_border_list(ent->value);
196 * Add an interval border to the list of a block's list
197 * of interval border.
198 * @note You always have to create the use before the def.
199 * @param env The environment.
200 * @param head The list head to enqueue the borders.
201 * @param irn The node (value) the border belongs to.
202 * @param pressure The pressure at this point in time.
203 * @param step A time step for the border.
204 * @param is_def Is the border a use or a def.
205 * @return The created border.
207 static INLINE border_t *border_add(be_chordal_env_t *env, struct list_head *head,
208 ir_node *irn, unsigned step, unsigned pressure,
209 unsigned is_def, unsigned is_real)
216 b = obstack_alloc(&env->obst, sizeof(*b));
218 /* also allocate the def and tie it to the use. */
219 def = obstack_alloc(&env->obst, sizeof(*def));
220 memset(def, 0, sizeof(*def));
225 * Set the link field of the irn to the def.
226 * This strongly relies on the fact, that the use is always
227 * made before the def.
229 set_irn_link(irn, def);
231 b->magic = BORDER_FOURCC;
232 def->magic = BORDER_FOURCC;
236 * If the def is encountered, the use was made and so was the
237 * the def node (see the code above). It was placed into the
238 * link field of the irn, so we can get it there.
241 b = get_irn_link(irn);
243 assert(b && b->magic == BORDER_FOURCC && "Illegal border encountered");
246 b->pressure = pressure;
248 b->is_real = is_real;
252 list_add_tail(&b->list, head);
254 DBG((dbg, LEVEL_5, "\t\t%s adding %+F, step: %d\n",
255 is_def ? "def" : "use", irn, step));
261 static INLINE int has_reg_class(const be_chordal_env_t *env, const ir_node *irn)
263 return arch_irn_has_reg_class(env->arch_env, irn, arch_pos_make_out(0), env->cls);
267 * Annotate the register pressure to the nodes and compute
268 * the liveness intervals.
269 * @param block The block to do it for.
270 * @param env_ptr The environment.
272 static void pressure(ir_node *block, void *env_ptr)
274 /* Convenience macro for a def */
275 #define border_def(irn, step, real) \
276 border_add(env, head, irn, step, pressure--, 1, real)
278 /* Convenience macro for a use */
279 #define border_use(irn, step, real) \
280 border_add(env, head, irn, step, ++pressure, 0, real)
282 be_chordal_env_t *env = env_ptr;
283 bitset_t *live = env->live;
288 unsigned pressure = 0;
289 struct list_head *head;
290 pset *live_in = put_live_in(block, pset_new_ptr_default());
291 pset *live_end = put_live_end(block, pset_new_ptr_default());
292 const arch_register_class_t *cls = env->cls;
294 DBG((dbg, LEVEL_1, "Computing pressure in block %+F\n", block));
295 bitset_clear_all(live);
297 /* Set up the border list in the block info */
298 head = obstack_alloc(&env->obst, sizeof(*head));
299 INIT_LIST_HEAD(head);
300 assert(pmap_get(env->border_heads, block) == NULL);
301 pmap_insert(env->border_heads, block, head);
304 * Make final uses of all values live out of the block.
305 * They are necessary to build up real intervals.
307 for(irn = pset_first(live_end); irn; irn = pset_next(live_end)) {
308 DBG((dbg, LEVEL_3, "\tMaking live: %+F/%d\n", irn, get_irn_graph_nr(irn)));
309 bitset_set(live, get_irn_graph_nr(irn));
310 if(has_reg_class(env, irn))
311 border_use(irn, step, 0);
316 * Determine the last uses of a value inside the block, since they are
317 * relevant for the interval borders.
319 sched_foreach_reverse(block, irn) {
320 DBG((dbg, LEVEL_1, "\tinsn: %+F, pressure: %d\n", irn, pressure));
321 DBG((dbg, LEVEL_2, "\tlive: %b\n", live));
324 * If the node defines some value, which can put into a
325 * register of the current class, make a border for it.
327 if(has_reg_class(env, irn)) {
329 int nr = get_irn_graph_nr(irn);
331 bitset_clear(live, nr);
332 border_def(irn, step, 1);
335 bitset_foreach(live, elm)
336 add_if(env, nr, (int) elm);
341 * If the node is no phi node we can examine the uses.
344 for(i = 0, n = get_irn_arity(irn); i < n; ++i) {
345 ir_node *op = get_irn_n(irn, i);
347 if(has_reg_class(env, op)) {
348 int nr = get_irn_graph_nr(op);
350 DBG((dbg, LEVEL_4, "\t\tpos: %d, use: %+F\n", i, op));
352 if(!bitset_is_set(live, nr)) {
353 border_use(op, step, 1);
354 bitset_set(live, nr);
363 * Add initial defs for all values live in.
365 for(irn = pset_first(live_in); irn; irn = pset_next(live_in)) {
366 if(has_reg_class(env, irn)) {
368 /* Mark the value live in. */
369 bitset_set(live, get_irn_graph_nr(irn));
372 border_def(irn, step, 0);
382 static void assign(ir_node *block, void *env_ptr)
384 be_chordal_env_t *env = env_ptr;
385 struct obstack *obst = &env->obst;
386 bitset_t *live = env->live;
387 bitset_t *colors = env->colors;
388 bitset_t *in_colors = env->in_colors;
389 const arch_register_class_t *cls = env->cls;
391 /* Mark the obstack level and allocate the temporary tmp_colors */
392 void *obstack_level = obstack_base(obst);
393 /*bitset_t *tmp_colors = bitset_obstack_alloc(obst, env->colors_n);*/
397 struct list_head *head = get_block_border_head(env, block);
398 pset *live_in = put_live_in(block, pset_new_ptr_default());
403 bitset_clear_all(live);
404 bitset_clear_all(colors);
405 bitset_clear_all(in_colors);
407 DBG((dbg, LEVEL_4, "Assigning colors for block %+F\n", block));
408 DBG((dbg, LEVEL_4, "\tusedef chain for block\n"));
409 list_for_each_entry(border_t, b, head, list) {
410 DBG((dbg, LEVEL_4, "\t%s %+F/%d\n", b->is_def ? "def" : "use",
411 b->irn, get_irn_graph_nr(b->irn)));
415 * Add initial defs for all values live in.
416 * Since their colors have already been assigned (The dominators were
417 * allocated before), we have to mark their colors as used also.
419 for(irn = pset_first(live_in); irn; irn = pset_next(live_in)) {
420 if(has_reg_class(env, irn)) {
421 const arch_register_t *reg = arch_get_irn_register(env->arch_env, irn, 0);
424 assert(reg && "Node must have been assigned a register");
425 col = arch_register_get_index(reg);
427 /* Mark the color of the live in value as used. */
428 bitset_set(colors, col);
429 bitset_set(in_colors, col);
431 /* Mark the value live in. */
432 bitset_set(live, get_irn_graph_nr(irn));
437 * Mind that the sequence of defs from back to front defines a perfect
438 * elimination order. So, coloring the definitions from first to last
441 list_for_each_entry_reverse(border_t, b, head, list) {
442 ir_node *irn = b->irn;
443 int nr = get_irn_graph_nr(irn);
446 * Assign a color, if it is a local def. Global defs already have a
449 if(b->is_def && !is_live_in(block, irn)) {
450 const arch_register_t *reg;
453 DBG((dbg, LEVEL_4, "\tcolors in use: %b\n", colors));
455 col = bitset_next_clear(colors, 0);
456 reg = arch_register_for_index(env->cls, col);
458 assert(arch_get_irn_register(env->arch_env, irn, 0) == NULL
459 && "This node must not have been assigned a register yet");
460 assert(!bitset_is_set(live, nr) && "Value's definition must not have been encountered");
462 bitset_set(colors, col);
463 bitset_set(live, nr);
465 arch_set_irn_register(env->arch_env, irn, 0, reg);
466 DBG((dbg, LEVEL_1, "\tassigning register %s(%d) to %+F\n",
467 arch_register_get_name(reg), col, irn));
470 /* Clear the color upon a use. */
471 else if(!b->is_def) {
472 const arch_register_t *reg = arch_get_irn_register(env->arch_env, irn, 0);
475 assert(reg && "Register must have been assigned");
477 col = arch_register_get_index(reg);
478 assert(bitset_is_set(live, nr) && "Cannot have a non live use");
480 bitset_clear(colors, col);
481 bitset_clear(live, nr);
485 /* Free the auxillary data on the obstack. */
486 obstack_free(obst, obstack_level);
491 void be_ra_chordal_init(void)
493 dbg = firm_dbg_register(DBG_CHORDAL);
494 firm_dbg_set_mask(dbg, -1);
497 be_chordal_env_t *be_ra_chordal(ir_graph *irg,
498 const arch_env_t *arch_env,
499 const arch_register_class_t *cls)
501 int node_count = get_graph_node_count(irg);
502 int colors_n = arch_register_class_n_regs(cls);
503 be_chordal_env_t *env = malloc(sizeof(*env));
505 if(get_irg_dom_state(irg) != dom_consistent)
508 obstack_init(&env->obst);
511 env->edges = new_set(if_edge_cmp, node_count);
512 env->nodes = new_set(if_node_cmp, node_count);
515 env->live = bitset_obstack_alloc(&env->obst, node_count);
516 env->colors = bitset_obstack_alloc(&env->obst, colors_n);
517 env->in_colors = bitset_obstack_alloc(&env->obst, colors_n);
518 env->colors_n = colors_n;
520 env->arch_env = arch_env;
522 env->border_heads = pmap_create();
524 /* First, determine the pressure */
525 dom_tree_walk_irg(irg, pressure, NULL, env);
527 /* Insert probable spills */
528 be_ra_chordal_spill(env);
530 /* Assign the colors */
531 dom_tree_walk_irg(irg, assign, NULL, env);
537 #ifdef DUMP_INTERVALS
542 ir_snprintf(buf, sizeof(buf), "ifg_%s_%F.eps", cls->name, irg);
543 plotter = new_plotter_ps(buf);
545 draw_interval_tree(&draw_chordal_def_opts, env, plotter, arch_env, cls);
546 plotter_free(plotter);
552 void be_ra_chordal_check(be_chordal_env_t *chordal_env) {
553 const arch_env_t *arch_env;
556 ir_node **nodes, *n1, *n2;
559 arch_env = chordal_env->arch_env;
561 /* Collect all irns */
563 pmap_foreach(chordal_env->border_heads, pme) {
565 struct list_head *head = pme->value;
566 list_for_each_entry(border_t, curr, head, list)
567 if (curr->is_def && curr->is_real)
568 if (arch_get_irn_reg_class(arch_env, curr->irn, arch_pos_make_out(0)) == chordal_env->cls)
569 obstack_ptr_grow(&ob, curr->irn);
571 obstack_ptr_grow(&ob, NULL);
572 nodes = (ir_node **) obstack_finish(&ob);
575 for (i = 0, n1 = nodes[i]; n1; n1 = nodes[++i]) {
576 const arch_register_t *n1_reg, *n2_reg;
578 n1_reg = arch_get_irn_register(arch_env, n1, 0);
579 assert(arch_reg_is_allocatable(arch_env, n1, arch_pos_make_out(0), n1_reg) && "Register constraint does not hold");
581 for (o = i+1, n2 = nodes[o]; n2; n2 = nodes[++o]) {
582 n2_reg = arch_get_irn_register(arch_env, n2, 0);
583 assert(!(nodes_interfere(chordal_env, n1, n2) && n1_reg == n2_reg) && "Interfering values have the same color!");
586 obstack_free(&ob, NULL);
589 /* BETTER #ifdef BUILD_GRAPH --> faster version of checker with edges */
591 void be_ra_chordal_done(be_chordal_env_t *env)
596 for(ifn = set_first(env->nodes); ifn; ifn = set_next(env->nodes))
603 pmap_destroy(env->border_heads);
604 obstack_free(&env->obst, NULL);
608 int nodes_interfere(const be_chordal_env_t *env, const ir_node *a, const ir_node *b)
611 return are_connected(env, get_irn_graph_nr(a), get_irn_graph_nr(b));
613 return values_interfere(a, b);
614 #endif /* BUILD_GRAPH */
619 set *be_ra_get_ifg_edges(const be_chordal_env_t *env) {
623 set *be_ra_get_ifg_nodes(const be_chordal_env_t *env) {