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"
33 #include "besched_t.h"
37 #include "bechordal_t.h"
38 #include "bechordal_draw.h"
42 #define DUMP_INTERVALS
46 #if defined(DUMP_IFG) && !defined(BUILD_GRAPH)
47 #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 #endif /* DEBUG_libfirm */
59 static firm_dbg_module_t *dbg;
63 #define IF_EDGE_HASH(e) ((e)->src)
64 #define IF_NODE_HASH(n) ((n)->nnr)
66 static int if_edge_cmp(const void *p1, const void *p2, size_t size)
68 const if_edge_t *e1 = p1;
69 const if_edge_t *e2 = p2;
71 return !(e1->src == e2->src && e1->tgt == e2->tgt);
74 static int if_node_cmp(const void *p1, const void *p2, size_t size)
76 const if_node_t *n1 = p1;
77 const if_node_t *n2 = p2;
79 return n1->nnr != n2->nnr;
82 static INLINE if_edge_t *edge_init(if_edge_t *edge, int src, int tgt)
84 /* Bring the smaller entry to src. */
96 static INLINE void add_if(const be_chordal_env_t *env, int src, int tgt)
99 if_node_t node, *src_node, *tgt_node;
101 edge_init(&edge, src, tgt);
102 set_insert(env->edges, &edge, sizeof(edge), IF_EDGE_HASH(&edge));
106 node.neighb = pset_new_ptr(8);
107 src_node = set_insert(env->nodes, &node, sizeof(node), IF_NODE_HASH(&node));
109 node.neighb = pset_new_ptr(8);
110 tgt_node = set_insert(env->nodes, &node, sizeof(node), IF_NODE_HASH(&node));
112 /* insert neighbors into nodes */
113 pset_insert_ptr(src_node->neighb, tgt_node);
114 pset_insert_ptr(tgt_node->neighb, src_node);
117 static INLINE int are_connected(const be_chordal_env_t *env, int src, int tgt)
120 edge_init(&edge, src, tgt);
121 return set_find(env->edges, &edge, sizeof(edge), IF_EDGE_HASH(&edge)) != NULL;
124 int ifg_has_edge(const be_chordal_env_t *env, const if_node_t *n1, const if_node_t* n2) {
125 return are_connected(env, n1->nnr, n2->nnr);
130 static void dump_ifg(const be_chordal_env_t *env)
132 static const char *colors[] = {
212 static const int n_colors = sizeof(colors) / sizeof(colors[0]);
215 set *edges = env->edges;
216 ir_graph *irg = env->irg;
219 ir_snprintf(filename, sizeof(filename), "ifg_%s_%F.dot", env->cls->name, irg);
221 if((f = fopen(filename, "wt")) != NULL) {
225 bitset_t *bs = bitset_malloc(get_graph_node_count(irg));
227 ir_fprintf(f, "graph \"%F\" {\n", irg);
228 fprintf(f, "\tnode [shape=box,style=filled]\n");
230 for(edge = set_first(edges); edge; edge = set_next(edges)) {
231 bitset_set(bs, edge->src);
232 bitset_set(bs, edge->tgt);
236 fprintf(f, "\tx [label=\"nodes: %u, edges: %d\"]\n", bitset_popcnt(bs), n_edges);
238 bitset_foreach(bs, pos) {
240 ir_node *irn = get_irn_for_graph_nr(irg, nr);
241 int color = get_irn_color(irn);
243 ir_fprintf(f, "\tn%d [label=\"%n\",color=\"%s\"]\n", nr, irn,
244 color >= 0 && color < n_colors ? colors[color] : "black");
247 for(edge = set_first(edges); edge; edge = set_next(edges)) {
248 fprintf(f, "\tn%d -- n%d [len=5]\n", edge->src, edge->tgt);
259 #endif /* DUMP_IFG */
261 #endif /* BUILD_GRAPH */
265 * Add an interval border to the list of a block's list
266 * of interval border.
267 * @note You always have to create the use before the def.
268 * @param env The environment.
269 * @param head The list head to enqueue the borders.
270 * @param irn The node (value) the border belongs to.
271 * @param pressure The pressure at this point in time.
272 * @param step A time step for the border.
273 * @param is_def Is the border a use or a def.
274 * @return The created border.
276 static INLINE border_t *border_add(be_chordal_env_t *env, struct list_head *head,
277 ir_node *irn, unsigned step, unsigned pressure,
278 unsigned is_def, unsigned is_real)
285 b = obstack_alloc(&env->obst, sizeof(*b));
287 /* also allocate the def and tie it to the use. */
288 def = obstack_alloc(&env->obst, sizeof(*def));
293 * Set the link field of the irn to the def.
294 * This strongly relies on the fact, that the use is always
295 * made before the def.
297 set_irn_link(irn, def);
300 b->magic = BORDER_FOURCC;
301 def->magic = BORDER_FOURCC;
306 * If the def is encountered, the use was made and so was the
307 * the def node (see the code above). It was placed into the
308 * link field of the irn, so we can get it there.
311 b = get_irn_link(irn);
314 assert(b && b->magic == BORDER_FOURCC && "Illegal border encountered");
318 b->pressure = pressure;
320 b->is_real = is_real;
323 list_add_tail(&b->list, head);
324 DBG((dbg, LEVEL_5, "\t\t%s adding %n, step: %d\n",
325 is_def ? "def" : "use", irn, step));
331 * Annotate the register pressure to the nodes and compute
332 * the liveness intervals.
333 * @param block The block to do it for.
334 * @param env_ptr The environment.
336 static void pressure(ir_node *block, void *env_ptr)
338 /* Convenience macro for a def */
339 #define border_def(irn, step, real) \
340 border_add(env, head, irn, step, pressure--, 1, real)
342 /* Convenience macro for a use */
343 #define border_use(irn, step, real) \
344 border_add(env, head, irn, step, ++pressure, 0, real)
346 be_chordal_env_t *env = env_ptr;
347 bitset_t *live = env->live;
352 unsigned pressure = 0;
353 struct list_head *head;
354 pset *live_in = get_live_in(block);
355 pset *live_end = get_live_end(block);
356 const arch_register_class_t *cls = env->cls;
358 DBG((dbg, LEVEL_1, "Computing pressure in block %n\n", block));
359 bitset_clear_all(live);
361 /* Set up the border list in the block info */
362 head = obstack_alloc(&env->obst, sizeof(*head));
363 INIT_LIST_HEAD(head);
364 pmap_insert(env->border_heads, block, head);
367 * Make final uses of all values live out of the block.
368 * They are neccessary to build up real intervals.
370 for(irn = pset_first(live_end); irn; irn = pset_next(live_end)) {
371 DBG((dbg, LEVEL_3, "\tMaking live: %n/%d\n", irn, get_irn_graph_nr(irn)));
372 bitset_set(live, get_irn_graph_nr(irn));
373 if(arch_irn_has_reg_class(env->arch_env, irn, 0, cls))
374 border_use(irn, step, 0);
380 * Determine the last uses of a value inside the block, since they are
381 * relevant for the interval borders.
383 sched_foreach_reverse(block, irn) {
384 DBG((dbg, LEVEL_1, "\tinsn: %n, pressure: %d\n", irn, pressure));
385 DBG((dbg, LEVEL_2, "\tlive: %b\n", live));
388 * If the node defines some value, which can put into a
389 * register of the current class, make a border for it.
391 if(arch_irn_has_reg_class(env->arch_env, irn, 0, cls)) {
393 int nr = get_irn_graph_nr(irn);
395 bitset_clear(live, nr);
396 border_def(irn, step, 1);
399 bitset_foreach(live, elm)
400 add_if(env, nr, (int) elm);
405 * If the node is no phi node we can examine the uses.
408 for(i = 0, n = get_irn_arity(irn); i < n; ++i) {
409 ir_node *op = get_irn_n(irn, i);
411 if(arch_irn_has_reg_class(env->arch_env, op, 0, cls)) {
412 int nr = get_irn_graph_nr(op);
414 DBG((dbg, LEVEL_4, "\t\tpos: %d, use: %n\n", i, op));
416 if(!bitset_is_set(live, nr)) {
417 border_use(op, step, 1);
418 bitset_set(live, nr);
428 * Add initial defs for all values live in.
430 for(irn = pset_first(live_in); irn; irn = pset_next(live_in)) {
431 if(arch_irn_has_reg_class(env->arch_env, irn, 0, cls)) {
433 /* Mark the value live in. */
434 bitset_set(live, get_irn_graph_nr(irn));
437 border_def(irn, step, 0);
442 static void assign(ir_node *block, void *env_ptr)
444 be_chordal_env_t *env = env_ptr;
445 struct obstack *obst = &env->obst;
446 bitset_t *live = env->live;
447 bitset_t *colors = env->colors;
448 bitset_t *in_colors = env->in_colors;
449 const arch_register_class_t *cls = env->cls;
451 /* Mark the obstack level and allocate the temporary tmp_colors */
452 void *obstack_level = obstack_base(obst);
453 /*bitset_t *tmp_colors = bitset_obstack_alloc(obst, env->colors_n);*/
457 struct list_head *head = get_block_border_head(env, block);
458 pset *live_in = get_live_in(block);
460 bitset_clear_all(live);
461 bitset_clear_all(colors);
462 bitset_clear_all(in_colors);
464 DBG((dbg, LEVEL_4, "Assigning colors for block %n\n", block));
465 DBG((dbg, LEVEL_4, "\tusedef chain for block\n"));
466 list_for_each_entry(border_t, b, head, list) {
467 DBG((dbg, LEVEL_4, "\t%s %n/%d\n", b->is_def ? "def" : "use",
468 b->irn, get_irn_graph_nr(b->irn)));
472 * Add initial defs for all values live in.
473 * Since their colors have already been assigned (The dominators were
474 * allocated before), we have to mark their colors as used also.
476 for(irn = pset_first(live_in); irn; irn = pset_next(live_in)) {
477 if(arch_irn_has_reg_class(env->arch_env, irn, 0, cls)) {
478 const arch_register_t *reg = arch_get_irn_register(env->arch_env, irn, 0);
481 assert(reg && "Nodfe must have been assigned a register");
482 col = arch_register_get_index(reg);
484 /* Mark the color of the live in value as used. */
485 bitset_set(colors, col);
486 bitset_set(in_colors, col);
488 /* Mark the value live in. */
489 bitset_set(live, get_irn_graph_nr(irn));
494 * Mind that the sequence of defs from back to front defines a perfect
495 * elimination order. So, coloring the definitions from first to last
498 list_for_each_entry_reverse(border_t, b, head, list) {
499 ir_node *irn = b->irn;
500 int nr = get_irn_graph_nr(irn);
503 * Assign a color, if it is a local def. Global defs already have a
506 if(b->is_def && !is_live_in(block, irn)) {
507 const arch_register_t *reg;
510 DBG((dbg, LEVEL_4, "\tcolors in use: %b\n", colors));
512 col = bitset_next_clear(colors, 0);
513 reg = arch_register_for_index(env->cls, col);
515 assert(arch_get_irn_register(env->arch_env, irn, 0) == NULL
516 && "This node must not have been assigned a register yet");
517 assert(!bitset_is_set(live, nr) && "Value's definition must not have been encountered");
519 bitset_set(colors, col);
520 bitset_set(live, nr);
522 arch_set_irn_register(env->arch_env, irn, 0, reg);
523 DBG((dbg, LEVEL_1, "\tassigning register %s(%d) to %n\n",
524 arch_register_get_name(reg), col, irn));
527 /* Clear the color upon a use. */
528 else if(!b->is_def) {
529 const arch_register_t *reg = arch_get_irn_register(env->arch_env, irn, 0);
532 assert(reg && "Register must have been assigned");
534 col = arch_register_get_index(reg);
535 assert(bitset_is_set(live, nr) && "Cannot have a non live use");
537 bitset_clear(colors, col);
538 bitset_clear(live, nr);
542 /* Free the auxillary data on the obstack. */
543 obstack_free(obst, obstack_level);
546 void be_ra_chordal_init(void)
548 dbg = firm_dbg_register(DBG_CHORDAL);
549 firm_dbg_set_mask(dbg, 0);
552 be_chordal_env_t *be_ra_chordal(ir_graph *irg,
553 const arch_env_t *arch_env,
554 const arch_register_class_t *cls)
556 int node_count = get_graph_node_count(irg);
557 int colors_n = arch_register_class_n_regs(cls);
558 be_chordal_env_t *env = malloc(sizeof(*env));
560 if(get_irg_dom_state(irg) != dom_consistent)
563 obstack_init(&env->obst);
566 env->edges = new_set(if_edge_cmp, node_count);
567 env->nodes = new_set(if_node_cmp, node_count);
570 env->live = bitset_obstack_alloc(&env->obst, node_count);
571 env->colors = bitset_obstack_alloc(&env->obst, colors_n);
572 env->in_colors = bitset_obstack_alloc(&env->obst, colors_n);
573 env->colors_n = colors_n;
575 env->arch_env = arch_env;
577 env->border_heads = pmap_create();
579 /* First, determine the pressure */
580 dom_tree_walk_irg(irg, pressure, NULL, env);
582 /* Insert probable spills */
583 be_ra_chordal_spill(env);
585 /* Assign the colors */
586 dom_tree_walk_irg(irg, assign, NULL, env);
592 #ifdef DUMP_INTERVALS
597 ir_snprintf(buf, sizeof(buf), "ifg_%s_%F.eps", cls->name, irg);
598 plotter = new_plotter_ps(buf);
600 draw_interval_tree(&draw_chordal_def_opts, env, plotter, arch_env, cls);
601 plotter_free(plotter);
608 void be_ra_chordal_done(be_chordal_env_t *env)
613 for(ifn = set_first(env->nodes); ifn; ifn = set_next(env->nodes))
620 pmap_destroy(env->border_heads);
621 obstack_free(&env->obst, NULL);
625 int nodes_interfere(const be_chordal_env_t *env, const ir_node *a, const ir_node *b)
628 return are_connected(env, get_irn_graph_nr(a), get_irn_graph_nr(b));
630 return values_interfere(a, b);
631 #endif /* BUILD_GRAPH */
636 set *be_ra_get_ifg_edges(const be_chordal_env_t *env) {
640 set *be_ra_get_ifg_nodes(const be_chordal_env_t *env) {