2 * This file is part of libFirm.
3 * Copyright (C) 2012 University of Karlsruhe.
8 * @brief Chordal register allocation.
9 * @author Sebastian Hack
14 #include "bechordal_common.h"
15 #include "bechordal_draw.h"
16 #include "bechordal_t.h"
18 #include "beintlive_t.h"
24 #define USE_HUNGARIAN 0
27 #include "hungarian.h"
29 #include "bipartite.h"
32 DEBUG_ONLY(static firm_dbg_module_t *dbg = NULL;)
34 static int get_next_free_reg(bitset_t *const available)
36 return bitset_next_set(available, 0);
39 static unsigned const *get_decisive_partner_regs(be_operand_t const *const o1, size_t const n_regs)
41 be_operand_t const *const o2 = o1->partner;
42 if (!o2 || rbitset_contains(o1->regs, o2->regs, n_regs)) {
44 } else if (rbitset_contains(o2->regs, o1->regs, n_regs)) {
51 static void pair_up_operands(be_chordal_env_t const *const env, be_insn_t *const insn)
53 /* For each out operand, try to find an in operand which can be assigned the
54 * same register as the out operand. */
55 int const n_regs = env->cls->n_regs;
56 unsigned *const bs = rbitset_alloca(n_regs);
57 be_lv_t *const lv = be_get_irg_liveness(env->irg);
58 for (int j = 0; j < insn->use_start; ++j) {
59 /* Try to find an in operand which has ... */
60 be_operand_t *smallest = NULL;
61 int smallest_n_regs = n_regs + 1;
62 be_operand_t *const out_op = &insn->ops[j];
63 for (int i = insn->use_start; i < insn->n_ops; ++i) {
64 be_operand_t *const op = &insn->ops[i];
65 if (op->partner || be_values_interfere(lv, insn->irn, op->carrier))
68 rbitset_copy(bs, op->regs, n_regs);
69 rbitset_and(bs, out_op->regs, n_regs);
70 int const n_total = rbitset_popcount(op->regs, n_regs);
71 if (!rbitset_is_empty(bs, n_regs) && n_total < smallest_n_regs) {
73 smallest_n_regs = n_total;
77 if (smallest != NULL) {
78 for (int i = insn->use_start; i < insn->n_ops; ++i) {
79 if (insn->ops[i].carrier == smallest->carrier)
80 insn->ops[i].partner = out_op;
83 out_op->partner = smallest;
84 smallest->partner = out_op;
89 static bool list_contains_irn(ir_node *const *const list, size_t const n, ir_node *const irn)
91 for (ir_node *const *i = list; i != list + n; ++i) {
98 static void handle_constraints(be_chordal_env_t *const env, ir_node *const irn)
100 void *const base = obstack_base(&env->obst);
101 be_insn_t *insn = be_scan_insn(env, irn);
103 /* Perms inserted before the constraint handling phase are considered to be
104 * correctly precolored. These Perms arise during the ABI handling phase. */
105 if (!insn || is_Phi(irn))
108 /* Prepare the constraint handling of this node.
109 * Perms are constructed and Copies are created for constrained values
110 * interfering with the instruction. */
111 ir_node *const perm = pre_process_constraints(env, &insn);
113 /* find suitable in operands to the out operands of the node. */
114 pair_up_operands(env, insn);
116 /* Look at the in/out operands and add each operand (and its possible partner)
117 * to a bipartite graph (left: nodes with partners, right: admissible colors). */
119 int const n_regs = env->cls->n_regs;
120 ir_node **const alloc_nodes = ALLOCAN(ir_node*, n_regs);
121 pmap *const partners = pmap_create();
123 hungarian_problem_t *const bp = hungarian_new(n_regs, n_regs, HUNGARIAN_MATCH_PERFECT);
125 bipartite_t *const bp = bipartite_new(n_regs, n_regs);
127 for (int i = 0; i < insn->n_ops; ++i) {
128 /* If the operand has no partner or the partner has not been marked
129 * for allocation, determine the admissible registers and mark it
130 * for allocation by associating the node and its partner with the
131 * set of admissible registers via a bipartite graph. */
132 be_operand_t *const op = &insn->ops[i];
133 if (op->partner && pmap_contains(partners, op->partner->carrier))
136 ir_node *const partner = op->partner ? op->partner->carrier : NULL;
137 pmap_insert(partners, op->carrier, partner);
139 pmap_insert(partners, partner, op->carrier);
141 /* Don't insert a node twice. */
142 if (list_contains_irn(alloc_nodes, n_alloc, op->carrier))
145 alloc_nodes[n_alloc] = op->carrier;
147 DBG((dbg, LEVEL_2, "\tassociating %+F and %+F\n", op->carrier, partner));
149 unsigned const *const bs = get_decisive_partner_regs(op, n_regs);
151 DBG((dbg, LEVEL_2, "\tallowed registers for %+F: %B\n", op->carrier, bs));
153 rbitset_foreach(bs, n_regs, col) {
155 hungarian_add(bp, n_alloc, col, 1);
157 bipartite_add(bp, n_alloc, col);
161 DBG((dbg, LEVEL_2, "\tallowed registers for %+F: none\n", op->carrier));
167 /* Put all nodes which live through the constrained instruction also to the
168 * allocation bipartite graph. They are considered unconstrained. */
170 be_lv_t *const lv = be_get_irg_liveness(env->irg);
171 foreach_out_edge(perm, edge) {
172 ir_node *const proj = get_edge_src_irn(edge);
173 assert(is_Proj(proj));
175 if (!be_values_interfere(lv, proj, irn) || pmap_contains(partners, proj))
178 /* Don't insert a node twice. */
179 if (list_contains_irn(alloc_nodes, n_alloc, proj))
182 assert(n_alloc < n_regs);
184 alloc_nodes[n_alloc] = proj;
185 pmap_insert(partners, proj, NULL);
187 bitset_foreach(env->allocatable_regs, col) {
189 hungarian_add(bp, n_alloc, col, 1);
191 bipartite_add(bp, n_alloc, col);
199 /* Compute a valid register allocation. */
200 int *const assignment = ALLOCAN(int, n_regs);
202 hungarian_prepare_cost_matrix(bp, HUNGARIAN_MODE_MAXIMIZE_UTIL);
203 int const match_res = hungarian_solve(bp, assignment, NULL, 1);
204 assert(match_res == 0 && "matching failed");
206 bipartite_matching(bp, assignment);
209 /* Assign colors obtained from the matching. */
210 for (int i = 0; i < n_alloc; ++i) {
211 assert(assignment[i] >= 0 && "there must have been a register assigned (node not register pressure faithful?)");
212 arch_register_t const *const reg = arch_register_for_index(env->cls, assignment[i]);
214 ir_node *const irn = alloc_nodes[i];
216 arch_set_irn_register(irn, reg);
217 DBG((dbg, LEVEL_2, "\tsetting %+F to register %s\n", irn, reg->name));
220 ir_node *const partner = pmap_get(ir_node, partners, alloc_nodes[i]);
221 if (partner != NULL) {
222 arch_set_irn_register(partner, reg);
223 DBG((dbg, LEVEL_2, "\tsetting %+F to register %s\n", partner, reg->name));
227 /* Allocate the non-constrained Projs of the Perm. */
229 bitset_t *const available = bitset_alloca(n_regs);
230 bitset_copy(available, env->allocatable_regs);
232 /* Put the colors of all Projs in a bitset. */
233 foreach_out_edge(perm, edge) {
234 ir_node *const proj = get_edge_src_irn(edge);
235 arch_register_t const *const reg = arch_get_irn_register(proj);
237 bitset_clear(available, reg->index);
240 /* Assign the not yet assigned Projs of the Perm a suitable color. */
241 foreach_out_edge(perm, edge) {
242 ir_node *const proj = get_edge_src_irn(edge);
243 arch_register_t const *const reg = arch_get_irn_register(proj);
245 DBG((dbg, LEVEL_2, "\tchecking reg of %+F: %s\n", proj, reg ? reg->name : "<none>"));
248 size_t const col = get_next_free_reg(available);
249 arch_register_t const *const new_reg = arch_register_for_index(env->cls, col);
250 bitset_clear(available, new_reg->index);
251 arch_set_irn_register(proj, new_reg);
252 DBG((dbg, LEVEL_2, "\tsetting %+F to register %s\n", proj, new_reg->name));
262 pmap_destroy(partners);
265 obstack_free(&env->obst, base);
269 * Handle constraint nodes in each basic block.
270 * handle_constraints() inserts Perm nodes which perm
271 * over all values live at the constrained node right in front
272 * of the constrained node. These Perms signal a constrained node.
273 * For further comments, refer to handle_constraints().
275 static void constraints(ir_node *const bl, void *const data)
277 be_chordal_env_t *const env = (be_chordal_env_t*)data;
278 sched_foreach_safe(bl, irn) {
279 handle_constraints(env, irn);
283 static void assign(ir_node *const block, void *const env_ptr)
285 be_chordal_env_t *const env = (be_chordal_env_t*)env_ptr;
286 struct list_head *const head = get_block_border_head(env, block);
287 be_lv_t *const lv = be_get_irg_liveness(env->irg);
289 DBG((dbg, LEVEL_4, "Assigning colors for block %+F\n", block));
290 DBG((dbg, LEVEL_4, "\tusedef chain for block\n"));
291 foreach_border_head(head, b) {
292 DBG((dbg, LEVEL_4, "\t%s %+F/%d\n", b->is_def ? "def" : "use",
293 b->irn, get_irn_idx(b->irn)));
296 bitset_t *const available = bitset_alloca(env->allocatable_regs->size);
297 bitset_copy(available, env->allocatable_regs);
299 /* Add initial defs for all values live in.
300 * Since their colors have already been assigned (The dominators were
301 * allocated before), we have to mark their colors as used also. */
302 be_lv_foreach_cls(lv, block, be_lv_state_in, env->cls, irn) {
303 arch_register_t const *const reg = arch_get_irn_register(irn);
305 assert(reg && "Node must have been assigned a register");
306 DBG((dbg, LEVEL_4, "%+F has reg %s\n", irn, reg->name));
308 /* Mark the color of the live in value as used. */
309 bitset_clear(available, reg->index);
312 /* Mind that the sequence of defs from back to front defines a perfect
313 * elimination order. So, coloring the definitions from first to last
315 foreach_border_head(head, b) {
316 ir_node *const irn = b->irn;
318 /* Assign a color, if it is a local def. Global defs already have a
321 /* Make the color available upon a use. */
322 arch_register_t const *const reg = arch_get_irn_register(irn);
323 assert(reg && "Register must have been assigned");
324 bitset_set(available, reg->index);
325 } else if (!be_is_live_in(lv, block, irn)) {
327 arch_register_t const *reg = arch_get_irn_register(irn);
330 assert(bitset_is_set(available, col) && "pre-colored register must be free");
332 assert(!arch_irn_is_ignore(irn));
333 col = get_next_free_reg(available);
334 reg = arch_register_for_index(env->cls, col);
335 arch_set_irn_register(irn, reg);
337 bitset_clear(available, col);
339 DBG((dbg, LEVEL_1, "\tassigning register %s(%d) to %+F\n", reg->name, col, irn));
344 static void be_ra_chordal_color(be_chordal_env_t *const chordal_env)
347 ir_graph *const irg = chordal_env->irg;
348 be_assure_live_sets(irg);
351 be_timer_push(T_CONSTR);
353 /* Handle register targeting constraints */
354 dom_tree_walk_irg(irg, constraints, NULL, chordal_env);
356 if (chordal_env->opts->dump_flags & BE_CH_DUMP_CONSTR) {
357 snprintf(buf, sizeof(buf), "%s-constr", chordal_env->cls->name);
358 dump_ir_graph(irg, buf);
361 be_timer_pop(T_CONSTR);
363 /* First, determine the pressure */
364 dom_tree_walk_irg(irg, create_borders, NULL, chordal_env);
366 /* Assign the colors */
367 dom_tree_walk_irg(irg, assign, NULL, chordal_env);
369 if (chordal_env->opts->dump_flags & BE_CH_DUMP_TREE_INTV) {
370 ir_snprintf(buf, sizeof(buf), "ifg_%s_%F.eps", chordal_env->cls->name, irg);
371 plotter_t *const plotter = new_plotter_ps(buf);
372 draw_interval_tree(&draw_chordal_def_opts, chordal_env, plotter);
373 plotter_free(plotter);
377 BE_REGISTER_MODULE_CONSTRUCTOR(be_init_chordal)
378 void be_init_chordal(void)
380 static be_ra_chordal_coloring_t coloring = {
383 FIRM_DBG_REGISTER(dbg, "firm.be.chordal");
385 be_register_chordal_coloring("default", &coloring);