2 * Chordal register allocation.
3 * @author Sebastian Hack
6 * Copyright (C) Universitaet Karlsruhe
7 * Released under the GPL
29 #include "bipartite.h"
32 #include "irgraph_t.h"
33 #include "irprintf_t.h"
43 #include "besched_t.h"
49 #include "bechordal_t.h"
50 #include "bechordal_draw.h"
52 #define DBG_LEVEL SET_LEVEL_0
53 #define DBG_LEVEL_CHECK SET_LEVEL_0
57 #define DUMP_INTERVALS
59 typedef struct _be_chordal_alloc_env_t {
60 be_chordal_env_t *chordal_env;
62 pset *pre_colored; /**< Set of precolored nodes. */
63 bitset_t *live; /**< A liveness bitset. */
64 bitset_t *colors; /**< The color mask. */
65 bitset_t *ignore_colors; /**< A mask of colors which shall be not used in allocation (ignored). */
66 bitset_t *in_colors; /**< Colors used by live in values. */
67 int colors_n; /**< The number of colors. */
68 } be_chordal_alloc_env_t;
72 /* Make a fourcc for border checking. */
73 #define BORDER_FOURCC FOURCC('B', 'O', 'R', 'D')
75 static void check_border_list(struct list_head *head)
78 list_for_each_entry(border_t, x, head, list) {
79 assert(x->magic == BORDER_FOURCC);
83 static void check_heads(be_chordal_env_t *env)
86 for(ent = pmap_first(env->border_heads); ent; ent = pmap_next(env->border_heads)) {
87 /* ir_printf("checking border list of block %+F\n", ent->key); */
88 check_border_list(ent->value);
94 * Add an interval border to the list of a block's list
96 * @note You always have to create the use before the def.
97 * @param env The environment.
98 * @param head The list head to enqueue the borders.
99 * @param irn The node (value) the border belongs to.
100 * @param pressure The pressure at this point in time.
101 * @param step A time step for the border.
102 * @param is_def Is the border a use or a def.
103 * @return The created border.
105 static INLINE border_t *border_add(be_chordal_env_t *env, struct list_head *head,
106 ir_node *irn, unsigned step, unsigned pressure,
107 unsigned is_def, unsigned is_real)
114 b = obstack_alloc(&env->obst, sizeof(*b));
116 /* also allocate the def and tie it to the use. */
117 def = obstack_alloc(&env->obst, sizeof(*def));
118 memset(def, 0, sizeof(*def));
123 * Set the link field of the irn to the def.
124 * This strongly relies on the fact, that the use is always
125 * made before the def.
127 set_irn_link(irn, def);
129 b->magic = BORDER_FOURCC;
130 def->magic = BORDER_FOURCC;
134 * If the def is encountered, the use was made and so was the
135 * the def node (see the code above). It was placed into the
136 * link field of the irn, so we can get it there.
139 b = get_irn_link(irn);
141 assert(b && b->magic == BORDER_FOURCC && "Illegal border encountered");
144 b->pressure = pressure;
146 b->is_real = is_real;
149 list_add_tail(&b->list, head);
150 DBG((env->dbg, LEVEL_5, "\t\t%s adding %+F, step: %d\n", is_def ? "def" : "use", irn, step));
157 * Check, if an irn is of the register class currently under processing.
158 * @param env The chordal environment.
159 * @param irn The node.
160 * @return 1, if the node is of that register class, 0 if not.
162 static INLINE int has_reg_class(const be_chordal_env_t *env, const ir_node *irn)
164 return arch_irn_has_reg_class(env->main_env->arch_env, irn, -1, env->cls);
167 #define has_limited_constr(req, irn) \
168 (arch_get_register_req(arch_env, (req), irn, -1) && (req)->type == arch_register_req_type_limited)
170 typedef struct _operand_t operand_t;
177 arch_register_req_t req;
185 unsigned has_constraints : 1;
188 static insn_t *scan_insn(be_chordal_env_t *env, ir_node *irn, struct obstack *obst)
190 const arch_env_t *arch_env = env->main_env->arch_env;
195 insn = obstack_alloc(obst, sizeof(insn[0]));
196 memset(insn, 0, sizeof(insn[0]));
198 insn->next_insn = sched_next(irn);
199 if(get_irn_mode(irn) == mode_T) {
202 for(p = sched_next(irn); is_Proj(p); p = sched_next(p)) {
203 if(arch_irn_consider_in_reg_alloc(arch_env, env->cls, p)) {
206 o.pos = -(get_Proj_proj(p) + 1);
208 arch_get_register_req(arch_env, &o.req, p, -1);
209 obstack_grow(obst, &o, sizeof(o));
211 insn->has_constraints |= arch_register_req_is(&o.req, limited);
218 else if(arch_irn_consider_in_reg_alloc(arch_env, env->cls, irn)) {
223 arch_get_register_req(arch_env, &o.req, irn, -1);
224 obstack_grow(obst, &o, sizeof(o));
226 insn->has_constraints |= arch_register_req_is(&o.req, limited);
229 insn->use_start = insn->n_ops;
231 for(i = 0, n = get_irn_arity(irn); i < n; ++i) {
232 ir_node *op = get_irn_n(irn, i);
234 if(arch_irn_consider_in_reg_alloc(arch_env, env->cls, op)) {
239 arch_get_register_req(arch_env, &o.req, irn, i);
240 obstack_grow(obst, &o, sizeof(o));
242 insn->has_constraints |= arch_register_req_is(&o.req, limited);
246 insn->ops = obstack_finish(obst);
250 static operand_t *find_unpaired_use(insn_t *insn, const operand_t *op, int can_be_constrained)
253 operand_t *res = NULL;
255 for(i = insn->use_start; i < insn->n_ops; ++i) {
256 operand_t *op = &insn->ops[i];
257 int has_constraint = arch_register_req_is(&op->req, limited);
259 if(!values_interfere(op->carrier, op->irn) && !op->partner && (!has_constraint || can_be_constrained)) {
260 if(arch_register_req_is(&op->req, should_be_same) && op->req.other == op->carrier)
270 static void pair_up_operands(insn_t *insn)
272 firm_dbg_module_t *dbg = firm_dbg_register("firm.be.chordal.constr");
275 for(i = 0; i < insn->use_start; ++i) {
276 operand_t *op = &insn->ops[i];
277 int has_constraint = arch_register_req_is(&op->req, limited);
278 operand_t *partner = find_unpaired_use(insn, op, !has_constraint);
281 op->partner = partner;
282 partner->partner = op;
287 static ir_node *handle_constraints(be_chordal_alloc_env_t *alloc_env, ir_node *irn)
289 be_chordal_env_t *env = alloc_env->chordal_env;
290 void *base = obstack_base(&env->obst);
291 insn_t *insn = scan_insn(env, irn, &env->obst);
292 ir_node *res = insn->next_insn;
294 if(insn->has_constraints) {
295 firm_dbg_module_t *dbg = firm_dbg_register("firm.be.chordal.constr");
296 const arch_env_t *aenv = env->main_env->arch_env;
297 int n_regs = env->cls->n_regs;
298 bitset_t *bs = bitset_alloca(n_regs);
299 ir_node **alloc_nodes = alloca(n_regs * sizeof(alloc_nodes[0]));
300 bipartite_t *bp = bipartite_new(n_regs, n_regs);
301 int *assignment = alloca(n_regs * sizeof(assignment[0]));
302 pmap *partners = pmap_create();
306 const ir_edge_t *edge;
307 ir_node *perm = insert_Perm_after(aenv, env->cls, env->dom_front, sched_prev(irn));
309 /* Registers are propagated by insert_Perm_after(). Clean them here! */
311 foreach_out_edge(perm, edge) {
312 ir_node *proj = get_edge_src_irn(edge);
313 arch_set_irn_register(aenv, proj, NULL);
318 be_liveness(env->irg);
319 insn = scan_insn(env, irn, &env->obst);
321 DBG((dbg, LEVEL_1, "handling constraints for %+F\n", irn));
324 * If there was no Perm made, nothing was alive in this register class.
325 * This means, that the node has no operands, thus no input constraints.
326 * so it had output constraints. The other results then can be assigned freeliy.
329 pair_up_operands(insn);
331 for(i = 0, n_alloc = 0; i < insn->n_ops; ++i) {
332 operand_t *op = &insn->ops[i];
333 if(arch_register_req_is(&op->req, limited)) {
334 pmap_insert(partners, op->carrier, op->partner ? op->partner->carrier : NULL);
335 alloc_nodes[n_alloc] = op->carrier;
337 DBG((dbg, LEVEL_2, "\tassociating %+F and %+F\n", op->carrier, pmap_get(partners, op->carrier)));
339 bitset_clear_all(bs);
340 op->req.limited(op->req.limited_env, bs);
341 bitset_andnot(bs, alloc_env->ignore_colors);
343 bitset_foreach(bs, col)
344 bipartite_add(bp, n_alloc, col);
351 foreach_out_edge(perm, edge) {
352 ir_node *proj = get_edge_src_irn(edge);
354 assert(is_Proj(proj));
356 if(values_interfere(proj, irn)) {
357 assert(n_alloc < n_regs);
358 alloc_nodes[n_alloc] = proj;
359 pmap_insert(partners, proj, NULL);
361 bitset_clear_all(bs);
362 arch_get_allocatable_regs(aenv, proj, -1, bs);
363 bitset_andnot(bs, alloc_env->ignore_colors);
364 bitset_foreach(bs, col)
365 bipartite_add(bp, n_alloc, col);
372 bipartite_matching(bp, assignment);
374 for(i = 0; i < n_alloc; ++i) {
377 const arch_register_t *reg = arch_register_for_index(env->cls, assignment[i]);
379 nodes[0] = alloc_nodes[i];
380 nodes[1] = pmap_get(partners, alloc_nodes[i]);
382 for(j = 0; j < 2; ++j) {
386 arch_set_irn_register(aenv, nodes[j], reg);
387 pset_hinsert_ptr(alloc_env->pre_colored, nodes[j]);
388 DBG((dbg, LEVEL_2, "\tsetting %+F to register %s\n", nodes[j], reg->name));
394 bitset_clear_all(bs);
395 foreach_out_edge(perm, edge) {
396 ir_node *proj = get_edge_src_irn(edge);
397 const arch_register_t *reg = arch_get_irn_register(aenv, proj);
400 bitset_set(bs, reg->index);
403 // bitset_or(bs, alloc_env->ignore_colors);
404 foreach_out_edge(perm, edge) {
405 ir_node *proj = get_edge_src_irn(edge);
406 const arch_register_t *reg = arch_get_irn_register(aenv, proj);
408 DBG((dbg, LEVEL_2, "\tchecking reg of %+F: %s\n", proj, reg ? reg->name : "<none>"));
411 col = bitset_next_clear(bs, 0);
412 reg = arch_register_for_index(env->cls, col);
413 bitset_set(bs, reg->index);
414 arch_set_irn_register(aenv, proj, reg);
415 pset_insert_ptr(alloc_env->pre_colored, proj);
416 DBG((dbg, LEVEL_2, "\tsetting %+F to register %s\n", proj, reg->name));
421 pmap_destroy(partners);
424 obstack_free(&env->obst, base);
429 * Handle constraint nodes in each basic block.
430 * be_insert_constr_perms() inserts Perm nodes which perm
431 * over all values live at the constrained node right in front
432 * of the constrained node. These Perms signal a constrained node.
433 * For further comments, refer to handle_constraints_at_perm().
435 static void constraints(ir_node *bl, void *data)
437 firm_dbg_module_t *dbg = firm_dbg_register("firm.be.chordal.constr");
438 be_chordal_alloc_env_t *env = data;
439 arch_env_t *arch_env = env->chordal_env->main_env->arch_env;
442 for(irn = sched_first(bl); !sched_is_end(irn);) {
443 irn = handle_constraints(env, irn);
448 * Annotate the register pressure to the nodes and compute
449 * the liveness intervals.
450 * @param block The block to do it for.
451 * @param env_ptr The environment.
453 static void pressure(ir_node *block, void *env_ptr)
455 /* Convenience macro for a def */
456 #define border_def(irn, step, real) \
457 border_add(env, head, irn, step, pressure--, 1, real)
459 /* Convenience macro for a use */
460 #define border_use(irn, step, real) \
461 border_add(env, head, irn, step, ++pressure, 0, real)
463 be_chordal_alloc_env_t *alloc_env = env_ptr;
464 be_chordal_env_t *env = alloc_env->chordal_env;
465 bitset_t *live = alloc_env->live;
466 firm_dbg_module_t *dbg = env->dbg;
471 unsigned pressure = 0;
472 struct list_head *head;
473 pset *live_in = put_live_in(block, pset_new_ptr_default());
474 pset *live_end = put_live_end(block, pset_new_ptr_default());
476 DBG((dbg, LEVEL_1, "Computing pressure in block %+F\n", block));
477 bitset_clear_all(live);
479 /* Set up the border list in the block info */
480 head = obstack_alloc(&env->obst, sizeof(*head));
481 INIT_LIST_HEAD(head);
482 assert(pmap_get(env->border_heads, block) == NULL);
483 pmap_insert(env->border_heads, block, head);
486 * Make final uses of all values live out of the block.
487 * They are necessary to build up real intervals.
489 for(irn = pset_first(live_end); irn; irn = pset_next(live_end)) {
490 if(has_reg_class(env, irn)) {
491 DBG((dbg, LEVEL_3, "\tMaking live: %+F/%d\n", irn, get_irn_graph_nr(irn)));
492 bitset_set(live, get_irn_graph_nr(irn));
493 border_use(irn, step, 0);
499 * Determine the last uses of a value inside the block, since they are
500 * relevant for the interval borders.
502 sched_foreach_reverse(block, irn) {
503 DBG((dbg, LEVEL_1, "\tinsn: %+F, pressure: %d\n", irn, pressure));
504 DBG((dbg, LEVEL_2, "\tlive: %b\n", live));
507 * If the node defines some value, which can put into a
508 * register of the current class, make a border for it.
510 if(has_reg_class(env, irn)) {
511 int nr = get_irn_graph_nr(irn);
513 bitset_clear(live, nr);
514 border_def(irn, step, 1);
518 * If the node is no phi node we can examine the uses.
521 for(i = 0, n = get_irn_arity(irn); i < n; ++i) {
522 ir_node *op = get_irn_n(irn, i);
524 if(has_reg_class(env, op)) {
525 int nr = get_irn_graph_nr(op);
527 DBG((dbg, LEVEL_4, "\t\tpos: %d, use: %+F\n", i, op));
529 if(!bitset_is_set(live, nr)) {
530 border_use(op, step, 1);
531 bitset_set(live, nr);
540 * Add initial defs for all values live in.
542 for(irn = pset_first(live_in); irn; irn = pset_next(live_in)) {
543 if(has_reg_class(env, irn)) {
545 /* Mark the value live in. */
546 bitset_set(live, get_irn_graph_nr(irn));
549 border_def(irn, step, 0);
558 static void assign(ir_node *block, void *env_ptr)
560 be_chordal_alloc_env_t *alloc_env = env_ptr;
561 be_chordal_env_t *env = alloc_env->chordal_env;
562 firm_dbg_module_t *dbg = env->dbg;
563 bitset_t *live = alloc_env->live;
564 bitset_t *colors = alloc_env->colors;
565 bitset_t *in_colors = alloc_env->in_colors;
566 const arch_env_t *arch_env = env->main_env->arch_env;
570 struct list_head *head = get_block_border_head(env, block);
571 pset *live_in = put_live_in(block, pset_new_ptr_default());
573 bitset_clear_all(live);
574 bitset_clear_all(colors);
575 bitset_clear_all(in_colors);
577 DBG((dbg, LEVEL_4, "Assigning colors for block %+F\n", block));
578 DBG((dbg, LEVEL_4, "\tusedef chain for block\n"));
579 list_for_each_entry(border_t, b, head, list) {
580 DBG((dbg, LEVEL_4, "\t%s %+F/%d\n", b->is_def ? "def" : "use",
581 b->irn, get_irn_graph_nr(b->irn)));
585 * Add initial defs for all values live in.
586 * Since their colors have already been assigned (The dominators were
587 * allocated before), we have to mark their colors as used also.
589 for(irn = pset_first(live_in); irn; irn = pset_next(live_in)) {
590 if(has_reg_class(env, irn)) {
591 const arch_register_t *reg = arch_get_irn_register(arch_env, irn);
594 assert(reg && "Node must have been assigned a register");
595 col = arch_register_get_index(reg);
597 /* Mark the color of the live in value as used. */
598 bitset_set(colors, col);
599 bitset_set(in_colors, col);
601 /* Mark the value live in. */
602 bitset_set(live, get_irn_graph_nr(irn));
607 * Mind that the sequence of defs from back to front defines a perfect
608 * elimination order. So, coloring the definitions from first to last
611 list_for_each_entry_reverse(border_t, b, head, list) {
612 ir_node *irn = b->irn;
613 int nr = get_irn_graph_nr(irn);
616 * Assign a color, if it is a local def. Global defs already have a
619 if(b->is_def && !is_live_in(block, irn)) {
620 const arch_register_t *reg;
623 if(pset_find_ptr(alloc_env->pre_colored, irn)) {
624 reg = arch_get_irn_register(arch_env, irn);
626 assert(!bitset_is_set(colors, col) && "pre-colored register must be free");
630 col = bitset_next_clear(colors, 0);
631 reg = arch_register_for_index(env->cls, col);
632 assert(arch_get_irn_register(arch_env, irn) == NULL && "This node must not have been assigned a register yet");
635 bitset_set(colors, col);
636 arch_set_irn_register(arch_env, irn, reg);
638 DBG((dbg, LEVEL_1, "\tassigning register %s(%d) to %+F\n",
639 arch_register_get_name(reg), col, irn));
641 assert(!bitset_is_set(live, nr) && "Value's definition must not have been encountered");
642 bitset_set(live, nr);
645 /* Clear the color upon a use. */
646 else if(!b->is_def) {
647 const arch_register_t *reg = arch_get_irn_register(arch_env, irn);
650 assert(reg && "Register must have been assigned");
652 col = arch_register_get_index(reg);
653 assert(bitset_is_set(live, nr) && "Cannot have a non live use");
655 bitset_clear(colors, col);
656 bitset_clear(live, nr);
665 void be_ra_chordal_color(be_chordal_env_t *chordal_env)
668 int colors_n = arch_register_class_n_regs(chordal_env->cls);
669 ir_graph *irg = chordal_env->irg;
671 be_chordal_alloc_env_t env;
673 if(get_irg_dom_state(irg) != dom_consistent)
676 env.chordal_env = chordal_env;
677 env.colors_n = colors_n;
678 env.colors = bitset_malloc(colors_n);
679 env.ignore_colors = bitset_malloc(colors_n);
680 env.in_colors = bitset_malloc(colors_n);
681 env.pre_colored = pset_new_ptr_default();
683 bitset_clear_all(env.ignore_colors);
685 for(i = 0; i < chordal_env->cls->n_regs; ++i) {
686 const arch_register_t *reg = &chordal_env->cls->regs[i];
687 if(arch_register_type_is(reg, ignore))
688 bitset_set(env.ignore_colors, reg->index);
692 /* Handle register targeting constraints */
693 dom_tree_walk_irg(irg, constraints, NULL, &env);
697 snprintf(buf, sizeof(buf), "-%s-constr", chordal_env->cls->name);
698 dump_ir_block_graph_sched(chordal_env->irg, buf);
702 env.live = bitset_malloc(get_graph_node_count(chordal_env->irg));
704 /* First, determine the pressure */
705 dom_tree_walk_irg(irg, pressure, NULL, &env);
707 /* Assign the colors */
708 dom_tree_walk_irg(irg, assign, NULL, &env);
710 be_numbering_done(irg);
712 #ifdef DUMP_INTERVALS
717 ir_snprintf(buf, sizeof(buf), "ifg_%s_%F.eps", chordal_env->cls->name, irg);
718 plotter = new_plotter_ps(buf);
720 draw_interval_tree(&draw_chordal_def_opts, chordal_env, plotter);
721 plotter_free(plotter);
728 free(env.ignore_colors);
730 del_pset(env.pre_colored);