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.
55 /* Make a fourcc for border checking. */
56 #define BORDER_FOURCC FOURCC('B', 'O', 'R', 'D')
58 #endif /* DEBUG_libfirm */
60 static firm_dbg_module_t *dbg;
64 #define IF_EDGE_HASH(e) ((e)->src)
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 */
182 * Add an interval border to the list of a block's list
183 * of interval border.
184 * @note You always have to create the use before the def.
185 * @param env The environment.
186 * @param head The list head to enqueue the borders.
187 * @param irn The node (value) the border belongs to.
188 * @param pressure The pressure at this point in time.
189 * @param step A time step for the border.
190 * @param is_def Is the border a use or a def.
191 * @return The created border.
193 static INLINE border_t *border_add(be_chordal_env_t *env, struct list_head *head,
194 ir_node *irn, unsigned step, unsigned pressure,
195 unsigned is_def, unsigned is_real)
202 b = obstack_alloc(&env->obst, sizeof(*b));
204 /* also allocate the def and tie it to the use. */
205 def = obstack_alloc(&env->obst, sizeof(*def));
210 * Set the link field of the irn to the def.
211 * This strongly relies on the fact, that the use is always
212 * made before the def.
214 set_irn_link(irn, def);
217 b->magic = BORDER_FOURCC;
218 def->magic = BORDER_FOURCC;
223 * If the def is encountered, the use was made and so was the
224 * the def node (see the code above). It was placed into the
225 * link field of the irn, so we can get it there.
228 b = get_irn_link(irn);
231 assert(b && b->magic == BORDER_FOURCC && "Illegal border encountered");
235 b->pressure = pressure;
237 b->is_real = is_real;
240 list_add_tail(&b->list, head);
241 DBG((dbg, LEVEL_5, "\t\t%s adding %+F, step: %d\n",
242 is_def ? "def" : "use", irn, step));
248 * Annotate the register pressure to the nodes and compute
249 * the liveness intervals.
250 * @param block The block to do it for.
251 * @param env_ptr The environment.
253 static void pressure(ir_node *block, void *env_ptr)
255 /* Convenience macro for a def */
256 #define border_def(irn, step, real) \
257 border_add(env, head, irn, step, pressure--, 1, real)
259 /* Convenience macro for a use */
260 #define border_use(irn, step, real) \
261 border_add(env, head, irn, step, ++pressure, 0, real)
263 be_chordal_env_t *env = env_ptr;
264 bitset_t *live = env->live;
269 unsigned pressure = 0;
270 struct list_head *head;
271 pset *live_in = put_live_in(block, pset_new_ptr_default());
272 pset *live_end = put_live_end(block, pset_new_ptr_default());
273 const arch_register_class_t *cls = env->cls;
275 DBG((dbg, LEVEL_1, "Computing pressure in block %+F\n", block));
276 bitset_clear_all(live);
278 /* Set up the border list in the block info */
279 head = obstack_alloc(&env->obst, sizeof(*head));
280 INIT_LIST_HEAD(head);
281 pmap_insert(env->border_heads, block, head);
284 * Make final uses of all values live out of the block.
285 * They are necessary to build up real intervals.
287 for(irn = pset_first(live_end); irn; irn = pset_next(live_end)) {
288 DBG((dbg, LEVEL_3, "\tMaking live: %+F/%d\n", irn, get_irn_graph_nr(irn)));
289 bitset_set(live, get_irn_graph_nr(irn));
290 if(arch_irn_has_reg_class(env->arch_env, irn, 0, cls))
291 border_use(irn, step, 0);
296 * Determine the last uses of a value inside the block, since they are
297 * relevant for the interval borders.
299 sched_foreach_reverse(block, irn) {
300 DBG((dbg, LEVEL_1, "\tinsn: %+F, pressure: %d\n", irn, pressure));
301 DBG((dbg, LEVEL_2, "\tlive: %b\n", live));
304 * If the node defines some value, which can put into a
305 * register of the current class, make a border for it.
307 if(arch_irn_has_reg_class(env->arch_env, irn, 0, cls)) {
309 int nr = get_irn_graph_nr(irn);
311 bitset_clear(live, nr);
312 border_def(irn, step, 1);
315 bitset_foreach(live, elm)
316 add_if(env, nr, (int) elm);
321 * If the node is no phi node we can examine the uses.
324 for(i = 0, n = get_irn_arity(irn); i < n; ++i) {
325 ir_node *op = get_irn_n(irn, i);
327 if(arch_irn_has_reg_class(env->arch_env, op, 0, cls)) {
328 int nr = get_irn_graph_nr(op);
330 DBG((dbg, LEVEL_4, "\t\tpos: %d, use: %+F\n", i, op));
332 if(!bitset_is_set(live, nr)) {
333 border_use(op, step, 1);
334 bitset_set(live, nr);
343 * Add initial defs for all values live in.
345 for(irn = pset_first(live_in); irn; irn = pset_next(live_in)) {
346 if(arch_irn_has_reg_class(env->arch_env, irn, 0, cls)) {
348 /* Mark the value live in. */
349 bitset_set(live, get_irn_graph_nr(irn));
352 border_def(irn, step, 0);
360 static void assign(ir_node *block, void *env_ptr)
362 be_chordal_env_t *env = env_ptr;
363 struct obstack *obst = &env->obst;
364 bitset_t *live = env->live;
365 bitset_t *colors = env->colors;
366 bitset_t *in_colors = env->in_colors;
367 const arch_register_class_t *cls = env->cls;
369 /* Mark the obstack level and allocate the temporary tmp_colors */
370 void *obstack_level = obstack_base(obst);
371 /*bitset_t *tmp_colors = bitset_obstack_alloc(obst, env->colors_n);*/
375 struct list_head *head = get_block_border_head(env, block);
376 pset *live_in = put_live_in(block, pset_new_ptr_default());
378 bitset_clear_all(live);
379 bitset_clear_all(colors);
380 bitset_clear_all(in_colors);
382 DBG((dbg, LEVEL_4, "Assigning colors for block %+F\n", block));
383 DBG((dbg, LEVEL_4, "\tusedef chain for block\n"));
384 list_for_each_entry(border_t, b, head, list) {
385 DBG((dbg, LEVEL_4, "\t%s %+F/%d\n", b->is_def ? "def" : "use",
386 b->irn, get_irn_graph_nr(b->irn)));
390 * Add initial defs for all values live in.
391 * Since their colors have already been assigned (The dominators were
392 * allocated before), we have to mark their colors as used also.
394 for(irn = pset_first(live_in); irn; irn = pset_next(live_in)) {
395 if(arch_irn_has_reg_class(env->arch_env, irn, 0, cls)) {
396 const arch_register_t *reg = arch_get_irn_register(env->arch_env, irn, 0);
399 assert(reg && "Node must have been assigned a register");
400 col = arch_register_get_index(reg);
402 /* Mark the color of the live in value as used. */
403 bitset_set(colors, col);
404 bitset_set(in_colors, col);
406 /* Mark the value live in. */
407 bitset_set(live, get_irn_graph_nr(irn));
412 * Mind that the sequence of defs from back to front defines a perfect
413 * elimination order. So, coloring the definitions from first to last
416 list_for_each_entry_reverse(border_t, b, head, list) {
417 ir_node *irn = b->irn;
418 int nr = get_irn_graph_nr(irn);
421 * Assign a color, if it is a local def. Global defs already have a
424 if(b->is_def && !is_live_in(block, irn)) {
425 const arch_register_t *reg;
428 DBG((dbg, LEVEL_4, "\tcolors in use: %b\n", colors));
430 col = bitset_next_clear(colors, 0);
431 reg = arch_register_for_index(env->cls, col);
433 assert(arch_get_irn_register(env->arch_env, irn, 0) == NULL
434 && "This node must not have been assigned a register yet");
435 assert(!bitset_is_set(live, nr) && "Value's definition must not have been encountered");
437 bitset_set(colors, col);
438 bitset_set(live, nr);
440 arch_set_irn_register(env->arch_env, irn, 0, reg);
441 DBG((dbg, LEVEL_1, "\tassigning register %s(%d) to %+F\n",
442 arch_register_get_name(reg), col, irn));
445 /* Clear the color upon a use. */
446 else if(!b->is_def) {
447 const arch_register_t *reg = arch_get_irn_register(env->arch_env, irn, 0);
450 assert(reg && "Register must have been assigned");
452 col = arch_register_get_index(reg);
453 assert(bitset_is_set(live, nr) && "Cannot have a non live use");
455 bitset_clear(colors, col);
456 bitset_clear(live, nr);
460 /* Free the auxillary data on the obstack. */
461 obstack_free(obst, obstack_level);
466 void be_ra_chordal_init(void)
468 dbg = firm_dbg_register(DBG_CHORDAL);
469 firm_dbg_set_mask(dbg, 0);
472 be_chordal_env_t *be_ra_chordal(ir_graph *irg,
473 const arch_env_t *arch_env,
474 const arch_register_class_t *cls)
476 int node_count = get_graph_node_count(irg);
477 int colors_n = arch_register_class_n_regs(cls);
478 be_chordal_env_t *env = malloc(sizeof(*env));
480 if(get_irg_dom_state(irg) != dom_consistent)
483 obstack_init(&env->obst);
486 env->edges = new_set(if_edge_cmp, node_count);
487 env->nodes = new_set(if_node_cmp, node_count);
490 env->live = bitset_obstack_alloc(&env->obst, node_count);
491 env->colors = bitset_obstack_alloc(&env->obst, colors_n);
492 env->in_colors = bitset_obstack_alloc(&env->obst, colors_n);
493 env->colors_n = colors_n;
495 env->arch_env = arch_env;
497 env->border_heads = pmap_create();
499 /* First, determine the pressure */
500 dom_tree_walk_irg(irg, pressure, NULL, env);
502 /* Insert probable spills */
503 be_ra_chordal_spill(env);
505 /* Assign the colors */
506 dom_tree_walk_irg(irg, assign, NULL, env);
512 #ifdef DUMP_INTERVALS
517 ir_snprintf(buf, sizeof(buf), "ifg_%s_%F.eps", cls->name, irg);
518 plotter = new_plotter_ps(buf);
520 draw_interval_tree(&draw_chordal_def_opts, env, plotter, arch_env, cls);
521 plotter_free(plotter);
527 void be_ra_chordal_check(be_chordal_env_t *chordal_env) {
528 const arch_env_t *arch_env;
531 ir_node **nodes, *n1, *n2;
534 arch_env = chordal_env->arch_env;
536 /* Collect all irns */
538 pmap_foreach(chordal_env->border_heads, pme) {
540 struct list_head *head = pme->value;
541 list_for_each_entry(border_t, curr, head, list)
542 if (curr->is_def && curr->is_real)
543 if (arch_get_irn_reg_class(arch_env, curr->irn, arch_pos_make_out(0)) == chordal_env->cls)
544 obstack_ptr_grow(&ob, curr->irn);
546 obstack_ptr_grow(&ob, NULL);
547 nodes = (ir_node **) obstack_finish(&ob);
550 for (i = 0, n1 = nodes[i]; n1; n1 = nodes[++i]) {
551 const arch_register_t *n1_reg, *n2_reg;
553 n1_reg = arch_get_irn_register(arch_env, n1, 0);
554 assert(arch_reg_is_allocatable(arch_env, n1, arch_pos_make_out(0), n1_reg) && "Register constraint does not hold");
556 for (o = i+1, n2 = nodes[o]; n2; n2 = nodes[++o]) {
557 n2_reg = arch_get_irn_register(arch_env, n2, 0);
558 assert(!(nodes_interfere(chordal_env, n1, n2) && n1_reg == n2_reg) && "Interfering values have the same color!");
561 obstack_free(&ob, NULL);
564 /* BETTER #ifdef BUILD_GRAPH --> faster version of checker with edges */
566 void be_ra_chordal_done(be_chordal_env_t *env)
571 for(ifn = set_first(env->nodes); ifn; ifn = set_next(env->nodes))
578 pmap_destroy(env->border_heads);
579 obstack_free(&env->obst, NULL);
583 int nodes_interfere(const be_chordal_env_t *env, const ir_node *a, const ir_node *b)
586 return are_connected(env, get_irn_graph_nr(a), get_irn_graph_nr(b));
588 return values_interfere(a, b);
589 #endif /* BUILD_GRAPH */
594 set *be_ra_get_ifg_edges(const be_chordal_env_t *env) {
598 set *be_ra_get_ifg_nodes(const be_chordal_env_t *env) {