2 * Copyright (C) 1995-2008 University of Karlsruhe. All right reserved.
4 * This file is part of libFirm.
6 * This file may be distributed and/or modified under the terms of the
7 * GNU General Public License version 2 as published by the Free Software
8 * Foundation and appearing in the file LICENSE.GPL included in the
9 * packaging of this file.
11 * Licensees holding valid libFirm Professional Edition licenses may use
12 * this file in accordance with the libFirm Commercial License.
13 * Agreement provided with the Software.
15 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
16 * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * @brief Chordal register allocation.
23 * @author Sebastian Hack
28 #include "bechordal_common.h"
29 #include "bechordal_draw.h"
30 #include "bechordal_t.h"
32 #include "beintlive_t.h"
38 #define USE_HUNGARIAN 0
41 #include "hungarian.h"
43 #include "bipartite.h"
46 DEBUG_ONLY(static firm_dbg_module_t *dbg = NULL;)
48 static int get_next_free_reg(bitset_t *const available)
50 return bitset_next_set(available, 0);
53 static unsigned const *get_decisive_partner_regs(be_operand_t const *const o1, size_t const n_regs)
55 be_operand_t const *const o2 = o1->partner;
56 if (!o2 || rbitset_contains(o1->regs, o2->regs, n_regs)) {
58 } else if (rbitset_contains(o2->regs, o1->regs, n_regs)) {
65 static void pair_up_operands(be_chordal_env_t const *const env, be_insn_t *const insn)
67 /* For each out operand, try to find an in operand which can be assigned the
68 * same register as the out operand. */
69 int const n_regs = env->cls->n_regs;
70 unsigned *const bs = rbitset_alloca(n_regs);
71 be_lv_t *const lv = be_get_irg_liveness(env->irg);
72 for (int j = 0; j < insn->use_start; ++j) {
73 /* Try to find an in operand which has ... */
74 be_operand_t *smallest = NULL;
75 int smallest_n_regs = n_regs + 1;
76 be_operand_t *const out_op = &insn->ops[j];
77 for (int i = insn->use_start; i < insn->n_ops; ++i) {
78 be_operand_t *const op = &insn->ops[i];
79 if (op->partner || be_values_interfere(lv, insn->irn, op->carrier))
82 rbitset_copy(bs, op->regs, n_regs);
83 rbitset_and(bs, out_op->regs, n_regs);
84 int const n_total = rbitset_popcount(op->regs, n_regs);
85 if (!rbitset_is_empty(bs, n_regs) && n_total < smallest_n_regs) {
87 smallest_n_regs = n_total;
91 if (smallest != NULL) {
92 for (int i = insn->use_start; i < insn->n_ops; ++i) {
93 if (insn->ops[i].carrier == smallest->carrier)
94 insn->ops[i].partner = out_op;
97 out_op->partner = smallest;
98 smallest->partner = out_op;
103 static bool list_contains_irn(ir_node *const *const list, size_t const n, ir_node *const irn)
105 for (ir_node *const *i = list; i != list + n; ++i) {
112 static void handle_constraints(be_chordal_env_t *const env, ir_node *const irn)
114 void *const base = obstack_base(&env->obst);
115 be_insn_t *insn = be_scan_insn(env, irn);
117 /* Perms inserted before the constraint handling phase are considered to be
118 * correctly precolored. These Perms arise during the ABI handling phase. */
119 if (!insn || is_Phi(irn))
122 /* Prepare the constraint handling of this node.
123 * Perms are constructed and Copies are created for constrained values
124 * interfering with the instruction. */
125 ir_node *const perm = pre_process_constraints(env, &insn);
127 /* find suitable in operands to the out operands of the node. */
128 pair_up_operands(env, insn);
130 /* Look at the in/out operands and add each operand (and its possible partner)
131 * to a bipartite graph (left: nodes with partners, right: admissible colors). */
133 int const n_regs = env->cls->n_regs;
134 ir_node **const alloc_nodes = ALLOCAN(ir_node*, n_regs);
135 pmap *const partners = pmap_create();
137 hungarian_problem_t *const bp = hungarian_new(n_regs, n_regs, HUNGARIAN_MATCH_PERFECT);
139 bipartite_t *const bp = bipartite_new(n_regs, n_regs);
141 for (int i = 0; i < insn->n_ops; ++i) {
142 /* If the operand has no partner or the partner has not been marked
143 * for allocation, determine the admissible registers and mark it
144 * for allocation by associating the node and its partner with the
145 * set of admissible registers via a bipartite graph. */
146 be_operand_t *const op = &insn->ops[i];
147 if (op->partner && pmap_contains(partners, op->partner->carrier))
150 ir_node *const partner = op->partner ? op->partner->carrier : NULL;
151 pmap_insert(partners, op->carrier, partner);
153 pmap_insert(partners, partner, op->carrier);
155 /* Don't insert a node twice. */
156 if (list_contains_irn(alloc_nodes, n_alloc, op->carrier))
159 alloc_nodes[n_alloc] = op->carrier;
161 DBG((dbg, LEVEL_2, "\tassociating %+F and %+F\n", op->carrier, partner));
163 unsigned const *const bs = get_decisive_partner_regs(op, n_regs);
165 DBG((dbg, LEVEL_2, "\tallowed registers for %+F: %B\n", op->carrier, bs));
167 rbitset_foreach(bs, n_regs, col) {
169 hungarian_add(bp, n_alloc, col, 1);
171 bipartite_add(bp, n_alloc, col);
175 DBG((dbg, LEVEL_2, "\tallowed registers for %+F: none\n", op->carrier));
181 /* Put all nodes which live through the constrained instruction also to the
182 * allocation bipartite graph. They are considered unconstrained. */
184 be_lv_t *const lv = be_get_irg_liveness(env->irg);
185 foreach_out_edge(perm, edge) {
186 ir_node *const proj = get_edge_src_irn(edge);
187 assert(is_Proj(proj));
189 if (!be_values_interfere(lv, proj, irn) || pmap_contains(partners, proj))
192 /* Don't insert a node twice. */
193 if (list_contains_irn(alloc_nodes, n_alloc, proj))
196 assert(n_alloc < n_regs);
198 alloc_nodes[n_alloc] = proj;
199 pmap_insert(partners, proj, NULL);
201 bitset_foreach(env->allocatable_regs, col) {
203 hungarian_add(bp, n_alloc, col, 1);
205 bipartite_add(bp, n_alloc, col);
213 /* Compute a valid register allocation. */
214 int *const assignment = ALLOCAN(int, n_regs);
216 hungarian_prepare_cost_matrix(bp, HUNGARIAN_MODE_MAXIMIZE_UTIL);
217 int const match_res = hungarian_solve(bp, assignment, NULL, 1);
218 assert(match_res == 0 && "matching failed");
220 bipartite_matching(bp, assignment);
223 /* Assign colors obtained from the matching. */
224 for (int i = 0; i < n_alloc; ++i) {
225 assert(assignment[i] >= 0 && "there must have been a register assigned (node not register pressure faithful?)");
226 arch_register_t const *const reg = arch_register_for_index(env->cls, assignment[i]);
228 ir_node *const irn = alloc_nodes[i];
230 arch_set_irn_register(irn, reg);
231 DBG((dbg, LEVEL_2, "\tsetting %+F to register %s\n", irn, reg->name));
234 ir_node *const partner = pmap_get(ir_node, partners, alloc_nodes[i]);
235 if (partner != NULL) {
236 arch_set_irn_register(partner, reg);
237 DBG((dbg, LEVEL_2, "\tsetting %+F to register %s\n", partner, reg->name));
241 /* Allocate the non-constrained Projs of the Perm. */
243 bitset_t *const available = bitset_alloca(n_regs);
244 bitset_copy(available, env->allocatable_regs);
246 /* Put the colors of all Projs in a bitset. */
247 foreach_out_edge(perm, edge) {
248 ir_node *const proj = get_edge_src_irn(edge);
249 arch_register_t const *const reg = arch_get_irn_register(proj);
251 bitset_clear(available, reg->index);
254 /* Assign the not yet assigned Projs of the Perm a suitable color. */
255 foreach_out_edge(perm, edge) {
256 ir_node *const proj = get_edge_src_irn(edge);
257 arch_register_t const *const reg = arch_get_irn_register(proj);
259 DBG((dbg, LEVEL_2, "\tchecking reg of %+F: %s\n", proj, reg ? reg->name : "<none>"));
262 size_t const col = get_next_free_reg(available);
263 arch_register_t const *const new_reg = arch_register_for_index(env->cls, col);
264 bitset_clear(available, new_reg->index);
265 arch_set_irn_register(proj, new_reg);
266 DBG((dbg, LEVEL_2, "\tsetting %+F to register %s\n", proj, new_reg->name));
276 pmap_destroy(partners);
279 obstack_free(&env->obst, base);
283 * Handle constraint nodes in each basic block.
284 * handle_constraints() inserts Perm nodes which perm
285 * over all values live at the constrained node right in front
286 * of the constrained node. These Perms signal a constrained node.
287 * For further comments, refer to handle_constraints().
289 static void constraints(ir_node *const bl, void *const data)
291 be_chordal_env_t *const env = (be_chordal_env_t*)data;
292 for (ir_node *irn = sched_first(bl); !sched_is_end(irn);) {
293 ir_node *const next = sched_next(irn);
294 handle_constraints(env, irn);
299 static void assign(ir_node *const block, void *const env_ptr)
301 be_chordal_env_t *const env = (be_chordal_env_t*)env_ptr;
302 struct list_head *const head = get_block_border_head(env, block);
303 be_lv_t *const lv = be_get_irg_liveness(env->irg);
305 DBG((dbg, LEVEL_4, "Assigning colors for block %+F\n", block));
306 DBG((dbg, LEVEL_4, "\tusedef chain for block\n"));
307 foreach_border_head(head, b) {
308 DBG((dbg, LEVEL_4, "\t%s %+F/%d\n", b->is_def ? "def" : "use",
309 b->irn, get_irn_idx(b->irn)));
312 bitset_t *const available = bitset_alloca(env->allocatable_regs->size);
313 bitset_copy(available, env->allocatable_regs);
315 /* Add initial defs for all values live in.
316 * Since their colors have already been assigned (The dominators were
317 * allocated before), we have to mark their colors as used also. */
318 be_lv_foreach_cls(lv, block, be_lv_state_in, env->cls, irn) {
319 arch_register_t const *const reg = arch_get_irn_register(irn);
321 assert(reg && "Node must have been assigned a register");
322 DBG((dbg, LEVEL_4, "%+F has reg %s\n", irn, reg->name));
324 /* Mark the color of the live in value as used. */
325 bitset_clear(available, reg->index);
328 /* Mind that the sequence of defs from back to front defines a perfect
329 * elimination order. So, coloring the definitions from first to last
331 foreach_border_head(head, b) {
332 ir_node *const irn = b->irn;
334 /* Assign a color, if it is a local def. Global defs already have a
337 /* Make the color available upon a use. */
338 arch_register_t const *const reg = arch_get_irn_register(irn);
339 assert(reg && "Register must have been assigned");
340 bitset_set(available, reg->index);
341 } else if (!be_is_live_in(lv, block, irn)) {
343 arch_register_t const *reg = arch_get_irn_register(irn);
346 assert(bitset_is_set(available, col) && "pre-colored register must be free");
348 assert(!arch_irn_is_ignore(irn));
349 col = get_next_free_reg(available);
350 reg = arch_register_for_index(env->cls, col);
351 arch_set_irn_register(irn, reg);
353 bitset_clear(available, col);
355 DBG((dbg, LEVEL_1, "\tassigning register %s(%d) to %+F\n", reg->name, col, irn));
360 static void be_ra_chordal_color(be_chordal_env_t *const chordal_env)
363 ir_graph *const irg = chordal_env->irg;
364 be_assure_live_sets(irg);
367 be_timer_push(T_CONSTR);
369 /* Handle register targeting constraints */
370 dom_tree_walk_irg(irg, constraints, NULL, chordal_env);
372 if (chordal_env->opts->dump_flags & BE_CH_DUMP_CONSTR) {
373 snprintf(buf, sizeof(buf), "%s-constr", chordal_env->cls->name);
374 dump_ir_graph(irg, buf);
377 be_timer_pop(T_CONSTR);
379 /* First, determine the pressure */
380 dom_tree_walk_irg(irg, create_borders, NULL, chordal_env);
382 /* Assign the colors */
383 dom_tree_walk_irg(irg, assign, NULL, chordal_env);
385 if (chordal_env->opts->dump_flags & BE_CH_DUMP_TREE_INTV) {
386 ir_snprintf(buf, sizeof(buf), "ifg_%s_%F.eps", chordal_env->cls->name, irg);
387 plotter_t *const plotter = new_plotter_ps(buf);
388 draw_interval_tree(&draw_chordal_def_opts, chordal_env, plotter);
389 plotter_free(plotter);
393 BE_REGISTER_MODULE_CONSTRUCTOR(be_init_chordal)
394 void be_init_chordal(void)
396 static be_ra_chordal_coloring_t coloring = {
399 FIRM_DBG_REGISTER(dbg, "firm.be.chordal");
401 be_register_chordal_coloring("default", &coloring);