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 0 //SET_LEVEL_4
48 #if defined(DUMP_IFG) && !defined(BUILD_GRAPH)
49 #error Must define BUILD_GRAPH to be able to dump it.
55 /* Make a fourcc for border checking. */
56 #define BORDER_FOURCC FOURCC('B', 'O', 'R', 'D')
58 static firm_dbg_module_t *dbg;
62 #define IF_EDGE_HASH(e) ((e)->src)
63 #define IF_NODE_HASH(n) ((n)->nnr)
65 static int if_edge_cmp(const void *p1, const void *p2, size_t size)
67 const if_edge_t *e1 = p1;
68 const if_edge_t *e2 = p2;
70 return !(e1->src == e2->src && e1->tgt == e2->tgt);
73 static int if_node_cmp(const void *p1, const void *p2, size_t size)
75 const if_node_t *n1 = p1;
76 const if_node_t *n2 = p2;
78 return n1->nnr != n2->nnr;
81 static INLINE if_edge_t *edge_init(if_edge_t *edge, int src, int tgt)
83 /* Bring the smaller entry to src. */
95 static INLINE void add_if(const be_chordal_env_t *env, int src, int tgt)
98 if_node_t node, *src_node, *tgt_node;
100 edge_init(&edge, src, tgt);
101 set_insert(env->edges, &edge, sizeof(edge), IF_EDGE_HASH(&edge));
105 node.neighb = pset_new_ptr(8);
106 src_node = set_insert(env->nodes, &node, sizeof(node), IF_NODE_HASH(&node));
108 node.neighb = pset_new_ptr(8);
109 tgt_node = set_insert(env->nodes, &node, sizeof(node), IF_NODE_HASH(&node));
111 /* insert neighbors into nodes */
112 pset_insert_ptr(src_node->neighb, tgt_node);
113 pset_insert_ptr(tgt_node->neighb, src_node);
116 static INLINE int are_connected(const be_chordal_env_t *env, int src, int tgt)
119 edge_init(&edge, src, tgt);
120 return set_find(env->edges, &edge, sizeof(edge), IF_EDGE_HASH(&edge)) != NULL;
123 int ifg_has_edge(const be_chordal_env_t *env, const if_node_t *n1, const if_node_t* n2) {
124 return are_connected(env, n1->nnr, n2->nnr);
129 static void dump_ifg(const be_chordal_env_t *env)
132 set *edges = env->edges;
133 ir_graph *irg = env->irg;
136 ir_snprintf(filename, sizeof(filename), "ifg_%s_%F.dot", env->cls->name, irg);
138 if((f = fopen(filename, "wt")) != NULL) {
142 bitset_t *bs = bitset_malloc(get_graph_node_count(irg));
144 ir_fprintf(f, "graph \"%F\" {\n", irg);
145 fprintf(f, "\tnode [shape=box,style=filled]\n");
147 for(edge = set_first(edges); edge; edge = set_next(edges)) {
148 bitset_set(bs, edge->src);
149 bitset_set(bs, edge->tgt);
153 fprintf(f, "\tx [label=\"nodes: %u, edges: %d\"]\n", bitset_popcnt(bs), n_edges);
155 bitset_foreach(bs, pos) {
157 ir_node *irn = get_irn_for_graph_nr(irg, nr);
159 ir_fprintf(f, "\tn%d [label=\"%+F\"]\n", nr, irn);
162 for(edge = set_first(edges); edge; edge = set_next(edges)) {
163 fprintf(f, "\tn%d -- n%d [len=5]\n", edge->src, edge->tgt);
174 #endif /* DUMP_IFG */
176 #endif /* BUILD_GRAPH */
178 static void check_border_list(struct list_head *head)
181 list_for_each_entry(border_t, x, head, list) {
182 assert(x->magic == BORDER_FOURCC);
186 static void check_heads(be_chordal_env_t *env)
189 for(ent = pmap_first(env->border_heads); ent; ent = pmap_next(env->border_heads)) {
190 /* ir_printf("checking border list of block %+F\n", ent->key); */
191 check_border_list(ent->value);
197 * Add an interval border to the list of a block's list
198 * of interval border.
199 * @note You always have to create the use before the def.
200 * @param env The environment.
201 * @param head The list head to enqueue the borders.
202 * @param irn The node (value) the border belongs to.
203 * @param pressure The pressure at this point in time.
204 * @param step A time step for the border.
205 * @param is_def Is the border a use or a def.
206 * @return The created border.
208 static INLINE border_t *border_add(be_chordal_env_t *env, struct list_head *head,
209 ir_node *irn, unsigned step, unsigned pressure,
210 unsigned is_def, unsigned is_real)
217 b = obstack_alloc(&env->obst, sizeof(*b));
219 /* also allocate the def and tie it to the use. */
220 def = obstack_alloc(&env->obst, sizeof(*def));
221 memset(def, 0, sizeof(*def));
226 * Set the link field of the irn to the def.
227 * This strongly relies on the fact, that the use is always
228 * made before the def.
230 set_irn_link(irn, def);
232 b->magic = BORDER_FOURCC;
233 def->magic = BORDER_FOURCC;
237 * If the def is encountered, the use was made and so was the
238 * the def node (see the code above). It was placed into the
239 * link field of the irn, so we can get it there.
242 b = get_irn_link(irn);
244 assert(b && b->magic == BORDER_FOURCC && "Illegal border encountered");
247 b->pressure = pressure;
249 b->is_real = is_real;
253 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->arch_env, irn, arch_pos_make_out(0), env->cls);
268 * Annotate the register pressure to the nodes and compute
269 * the liveness intervals.
270 * @param block The block to do it for.
271 * @param env_ptr The environment.
273 static void pressure(ir_node *block, void *env_ptr)
275 /* Convenience macro for a def */
276 #define border_def(irn, step, real) \
277 border_add(env, head, irn, step, pressure--, 1, real)
279 /* Convenience macro for a use */
280 #define border_use(irn, step, real) \
281 border_add(env, head, irn, step, ++pressure, 0, real)
283 be_chordal_env_t *env = env_ptr;
284 bitset_t *live = env->live;
289 unsigned pressure = 0;
290 struct list_head *head;
291 pset *live_in = put_live_in(block, pset_new_ptr_default());
292 pset *live_end = put_live_end(block, pset_new_ptr_default());
293 const arch_register_class_t *cls = env->cls;
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 DBG((dbg, LEVEL_3, "\tMaking live: %+F/%d\n", irn, get_irn_graph_nr(irn)));
310 bitset_set(live, get_irn_graph_nr(irn));
311 if(has_reg_class(env, irn))
312 border_use(irn, step, 0);
317 * Determine the last uses of a value inside the block, since they are
318 * relevant for the interval borders.
320 sched_foreach_reverse(block, irn) {
321 DBG((dbg, LEVEL_1, "\tinsn: %+F, pressure: %d\n", irn, pressure));
322 DBG((dbg, LEVEL_2, "\tlive: %b\n", live));
325 * If the node defines some value, which can put into a
326 * register of the current class, make a border for it.
328 if(has_reg_class(env, irn)) {
330 int nr = get_irn_graph_nr(irn);
332 bitset_clear(live, nr);
333 border_def(irn, step, 1);
336 bitset_foreach(live, elm)
337 add_if(env, nr, (int) elm);
342 * If the node is no phi node we can examine the uses.
345 for(i = 0, n = get_irn_arity(irn); i < n; ++i) {
346 ir_node *op = get_irn_n(irn, i);
348 if(has_reg_class(env, op)) {
349 int nr = get_irn_graph_nr(op);
351 DBG((dbg, LEVEL_4, "\t\tpos: %d, use: %+F\n", i, op));
353 if(!bitset_is_set(live, nr)) {
354 border_use(op, step, 1);
355 bitset_set(live, nr);
364 * Add initial defs for all values live in.
366 for(irn = pset_first(live_in); irn; irn = pset_next(live_in)) {
367 if(has_reg_class(env, irn)) {
369 /* Mark the value live in. */
370 bitset_set(live, get_irn_graph_nr(irn));
373 border_def(irn, step, 0);
383 static void assign(ir_node *block, void *env_ptr)
385 be_chordal_env_t *env = env_ptr;
386 struct obstack *obst = &env->obst;
387 bitset_t *live = env->live;
388 bitset_t *colors = env->colors;
389 bitset_t *in_colors = env->in_colors;
390 const arch_register_class_t *cls = env->cls;
392 /* Mark the obstack level and allocate the temporary tmp_colors */
393 void *obstack_level = obstack_base(obst);
394 /*bitset_t *tmp_colors = bitset_obstack_alloc(obst, env->colors_n);*/
398 struct list_head *head = get_block_border_head(env, block);
399 pset *live_in = put_live_in(block, pset_new_ptr_default());
404 bitset_clear_all(live);
405 bitset_clear_all(colors);
406 bitset_clear_all(in_colors);
408 DBG((dbg, LEVEL_4, "Assigning colors for block %+F\n", block));
409 DBG((dbg, LEVEL_4, "\tusedef chain for block\n"));
410 list_for_each_entry(border_t, b, head, list) {
411 DBG((dbg, LEVEL_4, "\t%s %+F/%d\n", b->is_def ? "def" : "use",
412 b->irn, get_irn_graph_nr(b->irn)));
416 * Add initial defs for all values live in.
417 * Since their colors have already been assigned (The dominators were
418 * allocated before), we have to mark their colors as used also.
420 for(irn = pset_first(live_in); irn; irn = pset_next(live_in)) {
421 if(has_reg_class(env, irn)) {
422 const arch_register_t *reg = arch_get_irn_register(env->arch_env, irn, 0);
425 assert(reg && "Node must have been assigned a register");
426 col = arch_register_get_index(reg);
428 /* Mark the color of the live in value as used. */
429 bitset_set(colors, col);
430 bitset_set(in_colors, col);
432 /* Mark the value live in. */
433 bitset_set(live, get_irn_graph_nr(irn));
438 * Mind that the sequence of defs from back to front defines a perfect
439 * elimination order. So, coloring the definitions from first to last
442 list_for_each_entry_reverse(border_t, b, head, list) {
443 ir_node *irn = b->irn;
444 int nr = get_irn_graph_nr(irn);
447 * Assign a color, if it is a local def. Global defs already have a
450 if(b->is_def && !is_live_in(block, irn)) {
451 const arch_register_t *reg;
454 DBG((dbg, LEVEL_4, "\tcolors in use: %b\n", colors));
456 col = bitset_next_clear(colors, 0);
457 reg = arch_register_for_index(env->cls, col);
459 assert(arch_get_irn_register(env->arch_env, irn, 0) == NULL
460 && "This node must not have been assigned a register yet");
461 assert(!bitset_is_set(live, nr) && "Value's definition must not have been encountered");
463 bitset_set(colors, col);
464 bitset_set(live, nr);
466 arch_set_irn_register(env->arch_env, irn, 0, reg);
467 DBG((dbg, LEVEL_1, "\tassigning register %s(%d) to %+F\n",
468 arch_register_get_name(reg), col, irn));
471 /* Clear the color upon a use. */
472 else if(!b->is_def) {
473 const arch_register_t *reg = arch_get_irn_register(env->arch_env, irn, 0);
476 assert(reg && "Register must have been assigned");
478 col = arch_register_get_index(reg);
479 assert(bitset_is_set(live, nr) && "Cannot have a non live use");
481 bitset_clear(colors, col);
482 bitset_clear(live, nr);
486 /* Free the auxillary data on the obstack. */
487 //obstack_free(obst, obstack_level);
492 void be_ra_chordal_init(void)
494 dbg = firm_dbg_register(DBG_CHORDAL);
495 firm_dbg_set_mask(dbg, DBG_LEVEL);
498 be_chordal_env_t *be_ra_chordal(ir_graph *irg,
499 const arch_env_t *arch_env,
500 const arch_register_class_t *cls)
502 int node_count = get_graph_node_count(irg);
503 int colors_n = arch_register_class_n_regs(cls);
504 be_chordal_env_t *env = malloc(sizeof(*env));
506 if(get_irg_dom_state(irg) != dom_consistent)
509 obstack_init(&env->obst);
512 env->edges = new_set(if_edge_cmp, node_count);
513 env->nodes = new_set(if_node_cmp, node_count);
516 env->live = bitset_obstack_alloc(&env->obst, node_count);
517 env->colors = bitset_obstack_alloc(&env->obst, colors_n);
518 env->in_colors = bitset_obstack_alloc(&env->obst, colors_n);
519 env->colors_n = colors_n;
521 env->arch_env = arch_env;
523 env->border_heads = pmap_create();
525 /* First, determine the pressure */
526 dom_tree_walk_irg(irg, pressure, NULL, env);
528 /* Insert probable spills */
529 be_ra_chordal_spill(env);
531 /* Assign the colors */
532 dom_tree_walk_irg(irg, assign, NULL, env);
538 #ifdef DUMP_INTERVALS
543 ir_snprintf(buf, sizeof(buf), "ifg_%s_%F.eps", cls->name, irg);
544 plotter = new_plotter_ps(buf);
546 draw_interval_tree(&draw_chordal_def_opts, env, plotter, arch_env, cls);
547 plotter_free(plotter);
553 void be_ra_chordal_check(be_chordal_env_t *chordal_env) {
554 const arch_env_t *arch_env;
557 ir_node **nodes, *n1, *n2;
560 arch_env = chordal_env->arch_env;
562 /* Collect all irns */
564 pmap_foreach(chordal_env->border_heads, pme) {
566 struct list_head *head = pme->value;
567 list_for_each_entry(border_t, curr, head, list)
568 if (curr->is_def && curr->is_real)
569 if (arch_get_irn_reg_class(arch_env, curr->irn, arch_pos_make_out(0)) == chordal_env->cls)
570 obstack_ptr_grow(&ob, curr->irn);
572 obstack_ptr_grow(&ob, NULL);
573 nodes = (ir_node **) obstack_finish(&ob);
576 for (i = 0, n1 = nodes[i]; n1; n1 = nodes[++i]) {
577 const arch_register_t *n1_reg, *n2_reg;
579 n1_reg = arch_get_irn_register(arch_env, n1, 0);
580 if (!arch_reg_is_allocatable(arch_env, n1, arch_pos_make_out(0), n1_reg)) {
581 DBG((dbg, 0, "Register assigned to %+F is not allowed\n", n1));
582 assert(0 && "Register constraint does not hold");
584 for (o = i+1, n2 = nodes[o]; n2; n2 = nodes[++o]) {
585 n2_reg = arch_get_irn_register(arch_env, n2, 0);
586 if (nodes_interfere(chordal_env, n1, n2) && n1_reg == n2_reg) {
587 DBG((dbg, 0, "Values %+F and %+F interfere and have the same regiseter assigned\n", n1, n2));
588 assert(0 && "Interfering values have the same color!");
592 obstack_free(&ob, NULL);
595 /* BETTER #ifdef BUILD_GRAPH --> faster version of checker with edges */
597 void be_ra_chordal_done(be_chordal_env_t *env)
602 for(ifn = set_first(env->nodes); ifn; ifn = set_next(env->nodes))
609 pmap_destroy(env->border_heads);
610 obstack_free(&env->obst, NULL);
616 int nodes_interfere(const be_chordal_env_t *env, const ir_node *a, const ir_node *b)
619 return are_connected(env, get_irn_graph_nr(a), get_irn_graph_nr(b));
621 return values_interfere(a, b);
622 #endif /* BUILD_GRAPH */
627 set *be_ra_get_ifg_edges(const be_chordal_env_t *env) {
631 set *be_ra_get_ifg_nodes(const be_chordal_env_t *env) {