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
34 #include "raw_bitset.h"
35 #include "bipartite.h"
36 #include "hungarian.h"
39 #include "irgraph_t.h"
40 #include "irprintf_t.h"
59 #include "beintlive_t.h"
61 #include "bechordal_t.h"
62 #include "bechordal_draw.h"
65 #include "bechordal_common.h"
67 DEBUG_ONLY(static firm_dbg_module_t *dbg = NULL;)
69 typedef struct be_chordal_alloc_env_t {
70 be_chordal_env_t *chordal_env;
72 bitset_t *live; /**< A liveness bitset. */
73 bitset_t *tmp_colors; /**< An auxiliary bitset which is as long as the number of colors in the class. */
74 bitset_t *colors; /**< The color mask. */
75 } be_chordal_alloc_env_t;
77 static int get_next_free_reg(const be_chordal_alloc_env_t *alloc_env, bitset_t *colors)
79 bitset_t *tmp = alloc_env->tmp_colors;
80 bitset_copy(tmp, colors);
82 bitset_and(tmp, alloc_env->chordal_env->allocatable_regs);
83 return bitset_next_set(tmp, 0);
86 static bitset_t const *get_decisive_partner_regs(be_operand_t const *const o1)
88 be_operand_t const *const o2 = o1->partner;
89 assert(!o2 || o1->req->cls == o2->req->cls);
91 if (!o2 || bitset_contains(o1->regs, o2->regs)) {
93 } else if (bitset_contains(o2->regs, o1->regs)) {
100 static void pair_up_operands(const be_chordal_alloc_env_t *alloc_env, be_insn_t *insn)
102 const be_chordal_env_t *env = alloc_env->chordal_env;
103 bitset_t *bs = bitset_alloca(env->cls->n_regs);
108 * For each out operand, try to find an in operand which can be assigned the
109 * same register as the out operand.
111 for (j = 0; j < insn->use_start; ++j) {
112 be_operand_t *smallest = NULL;
113 int smallest_n_regs = env->cls->n_regs + 1;
114 be_operand_t *out_op = &insn->ops[j];
116 /* Try to find an in operand which has ... */
117 for (i = insn->use_start; i < insn->n_ops; ++i) {
119 be_operand_t *op = &insn->ops[i];
122 if (op->partner != NULL)
124 lv = be_get_irg_liveness(env->irg);
125 if (be_values_interfere(lv, op->irn, op->carrier))
128 bitset_copy(bs, op->regs);
129 bitset_and(bs, out_op->regs);
130 n_total = bitset_popcount(op->regs);
132 if (!bitset_is_empty(bs) && n_total < smallest_n_regs) {
134 smallest_n_regs = n_total;
138 if (smallest != NULL) {
139 for (i = insn->use_start; i < insn->n_ops; ++i) {
140 if (insn->ops[i].carrier == smallest->carrier)
141 insn->ops[i].partner = out_op;
144 out_op->partner = smallest;
145 smallest->partner = out_op;
150 static void handle_constraints(be_chordal_alloc_env_t *alloc_env,
154 ir_node **alloc_nodes;
155 //hungarian_problem_t *bp;
159 ir_node *perm = NULL;
160 //int match_res, cost;
161 be_chordal_env_t *env = alloc_env->chordal_env;
162 void *base = obstack_base(env->obst);
163 be_insn_t *insn = be_scan_insn(env, irn);
167 * Perms inserted before the constraint handling phase are considered to be
168 * correctly precolored. These Perms arise during the ABI handling phase.
170 if (!insn->has_constraints || is_Phi(irn))
173 n_regs = env->cls->n_regs;
174 alloc_nodes = ALLOCAN(ir_node*, n_regs);
175 //bp = hungarian_new(n_regs, n_regs, 2, HUNGARIAN_MATCH_PERFECT);
176 bp = bipartite_new(n_regs, n_regs);
177 assignment = ALLOCAN(int, n_regs);
178 partners = pmap_create();
181 * prepare the constraint handling of this node.
182 * Perms are constructed and Copies are created for constrained values
183 * interfering with the instruction.
185 perm = pre_process_constraints(alloc_env->chordal_env, &insn);
187 /* find suitable in operands to the out operands of the node. */
188 pair_up_operands(alloc_env, insn);
191 * look at the in/out operands and add each operand (and its possible partner)
192 * to a bipartite graph (left: nodes with partners, right: admissible colors).
194 for (i = 0, n_alloc = 0; i < insn->n_ops; ++i) {
195 be_operand_t *op = &insn->ops[i];
198 * If the operand has no partner or the partner has not been marked
199 * for allocation, determine the admissible registers and mark it
200 * for allocation by associating the node and its partner with the
201 * set of admissible registers via a bipartite graph.
203 if (!op->partner || !pmap_contains(partners, op->partner->carrier)) {
204 ir_node *partner = op->partner ? op->partner->carrier : NULL;
207 pmap_insert(partners, op->carrier, partner);
209 pmap_insert(partners, partner, op->carrier);
211 /* don't insert a node twice */
212 for (i = 0; i < n_alloc; ++i) {
213 if (alloc_nodes[i] == op->carrier) {
220 alloc_nodes[n_alloc] = op->carrier;
222 DBG((dbg, LEVEL_2, "\tassociating %+F and %+F\n", op->carrier,
225 bitset_t const *const bs = get_decisive_partner_regs(op);
227 DBG((dbg, LEVEL_2, "\tallowed registers for %+F: %B\n", op->carrier, bs));
229 bitset_foreach(bs, col) {
230 //hungarian_add(bp, n_alloc, col, 1);
231 bipartite_add(bp, n_alloc, col);
234 DBG((dbg, LEVEL_2, "\tallowed registers for %+F: none\n", op->carrier));
242 * Put all nodes which live through the constrained instruction also to the
243 * allocation bipartite graph. They are considered unconstrained.
246 foreach_out_edge(perm, edge) {
248 ir_node *proj = get_edge_src_irn(edge);
249 be_lv_t *lv = be_get_irg_liveness(env->irg);
251 assert(is_Proj(proj));
253 if (!be_values_interfere(lv, proj, irn)
254 || pmap_contains(partners, proj))
257 /* don't insert a node twice */
258 for (i = 0; i < n_alloc; ++i) {
259 if (alloc_nodes[i] == proj) {
267 assert(n_alloc < n_regs);
269 alloc_nodes[n_alloc] = proj;
270 pmap_insert(partners, proj, NULL);
272 bitset_foreach(env->allocatable_regs, col) {
273 //hungarian_add(bp, n_alloc, col, 1);
274 bipartite_add(bp, n_alloc, col);
281 /* Compute a valid register allocation. */
283 hungarian_prepare_cost_matrix(bp, HUNGARIAN_MODE_MAXIMIZE_UTIL);
284 match_res = hungarian_solve(bp, assignment, &cost, 1);
285 assert(match_res == 0 && "matching failed");
287 /*bipartite_dump_f(stderr, bp);*/
288 bipartite_matching(bp, assignment);
291 /* Assign colors obtained from the matching. */
292 for (i = 0; i < n_alloc; ++i) {
293 const arch_register_t *reg;
296 assert(assignment[i] >= 0 && "there must have been a register assigned (node not register pressure faithful?)");
297 reg = arch_register_for_index(env->cls, assignment[i]);
299 irn = alloc_nodes[i];
301 arch_set_irn_register(irn, reg);
302 DBG((dbg, LEVEL_2, "\tsetting %+F to register %s\n", irn, reg->name));
305 irn = pmap_get(ir_node, partners, alloc_nodes[i]);
307 arch_set_irn_register(irn, reg);
308 DBG((dbg, LEVEL_2, "\tsetting %+F to register %s\n", irn, reg->name));
312 /* Allocate the non-constrained Projs of the Perm. */
314 /* Put the colors of all Projs in a bitset. */
315 bitset_t *const bs = bitset_alloca(n_regs);
316 foreach_out_edge(perm, edge) {
317 ir_node *proj = get_edge_src_irn(edge);
318 const arch_register_t *reg = arch_get_irn_register(proj);
321 bitset_set(bs, reg->index);
324 /* Assign the not yet assigned Projs of the Perm a suitable color. */
325 foreach_out_edge(perm, edge) {
326 ir_node *proj = get_edge_src_irn(edge);
327 const arch_register_t *reg = arch_get_irn_register(proj);
329 DBG((dbg, LEVEL_2, "\tchecking reg of %+F: %s\n", proj, reg ? reg->name : "<none>"));
332 size_t const col = get_next_free_reg(alloc_env, bs);
333 reg = arch_register_for_index(env->cls, col);
334 bitset_set(bs, reg->index);
335 arch_set_irn_register(proj, reg);
336 DBG((dbg, LEVEL_2, "\tsetting %+F to register %s\n", proj, reg->name));
342 //hungarian_free(bp);
343 pmap_destroy(partners);
346 obstack_free(env->obst, base);
350 * Handle constraint nodes in each basic block.
351 * handle_constraints() inserts Perm nodes which perm
352 * over all values live at the constrained node right in front
353 * of the constrained node. These Perms signal a constrained node.
354 * For further comments, refer to handle_constraints().
356 static void constraints(ir_node *bl, void *data)
358 be_chordal_alloc_env_t *env = (be_chordal_alloc_env_t*)data;
361 for (irn = sched_first(bl); !sched_is_end(irn);) {
362 ir_node *const next = sched_next(irn);
363 handle_constraints(env, irn);
368 static void assign(ir_node *block, void *env_ptr)
370 be_chordal_alloc_env_t *alloc_env = (be_chordal_alloc_env_t*)env_ptr;
371 be_chordal_env_t *env = alloc_env->chordal_env;
372 bitset_t *live = alloc_env->live;
373 bitset_t *colors = alloc_env->colors;
374 struct list_head *head = get_block_border_head(env, block);
375 be_lv_t *lv = be_get_irg_liveness(env->irg);
377 bitset_clear_all(colors);
378 bitset_clear_all(live);
380 DBG((dbg, LEVEL_4, "Assigning colors for block %+F\n", block));
381 DBG((dbg, LEVEL_4, "\tusedef chain for block\n"));
382 foreach_border_head(head, b) {
383 DBG((dbg, LEVEL_4, "\t%s %+F/%d\n", b->is_def ? "def" : "use",
384 b->irn, get_irn_idx(b->irn)));
388 * Add initial defs for all values live in.
389 * Since their colors have already been assigned (The dominators were
390 * allocated before), we have to mark their colors as used also.
392 be_lv_foreach(lv, block, be_lv_state_in, irn) {
393 if (arch_irn_consider_in_reg_alloc(env->cls, irn)) {
394 const arch_register_t *reg = arch_get_irn_register(irn);
396 assert(reg && "Node must have been assigned a register");
397 DBG((dbg, LEVEL_4, "%+F has reg %s\n", irn, reg->name));
399 /* Mark the color of the live in value as used. */
400 int const col = reg->index;
401 bitset_set(colors, col);
403 /* Mark the value live in. */
404 bitset_set(live, get_irn_idx(irn));
409 * Mind that the sequence of defs from back to front defines a perfect
410 * elimination order. So, coloring the definitions from first to last
413 foreach_border_head(head, b) {
414 ir_node *irn = b->irn;
415 int nr = get_irn_idx(irn);
418 * Assign a color, if it is a local def. Global defs already have a
421 if (b->is_def && !be_is_live_in(lv, block, irn)) {
423 arch_register_t const *reg = arch_get_irn_register(irn);
426 assert(!bitset_is_set(colors, col) && "pre-colored register must be free");
428 assert(!arch_irn_is_ignore(irn));
429 col = get_next_free_reg(alloc_env, colors);
430 reg = arch_register_for_index(env->cls, col);
431 arch_set_irn_register(irn, reg);
433 bitset_set(colors, col);
435 DBG((dbg, LEVEL_1, "\tassigning register %s(%d) to %+F\n", reg->name, col, irn));
437 assert(!bitset_is_set(live, nr) && "Value's definition must not have been encountered");
438 bitset_set(live, nr);
439 } else if (!b->is_def) {
440 /* Clear the color upon a use. */
441 const arch_register_t *reg = arch_get_irn_register(irn);
443 assert(reg && "Register must have been assigned");
445 bitset_clear(colors, reg->index);
446 bitset_clear(live, nr);
451 static void be_ra_chordal_color(be_chordal_env_t *const chordal_env)
453 be_chordal_alloc_env_t env;
455 const arch_register_class_t *cls = chordal_env->cls;
457 int colors_n = arch_register_class_n_regs(cls);
458 ir_graph *irg = chordal_env->irg;
460 be_assure_live_sets(irg);
463 env.chordal_env = chordal_env;
464 env.colors = bitset_alloca(colors_n);
465 env.tmp_colors = bitset_alloca(colors_n);
467 be_timer_push(T_SPLIT);
469 if (chordal_env->opts->dump_flags & BE_CH_DUMP_SPLIT) {
470 snprintf(buf, sizeof(buf), "%s-split", chordal_env->cls->name);
471 dump_ir_graph(chordal_env->irg, buf);
474 be_timer_pop(T_SPLIT);
476 be_timer_push(T_CONSTR);
478 /* Handle register targeting constraints */
479 dom_tree_walk_irg(irg, constraints, NULL, &env);
481 if (chordal_env->opts->dump_flags & BE_CH_DUMP_CONSTR) {
482 snprintf(buf, sizeof(buf), "%s-constr", chordal_env->cls->name);
483 dump_ir_graph(chordal_env->irg, buf);
486 be_timer_pop(T_CONSTR);
488 env.live = bitset_malloc(get_irg_last_idx(chordal_env->irg));
490 /* First, determine the pressure */
491 dom_tree_walk_irg(irg, create_borders, NULL, env.chordal_env);
493 /* Assign the colors */
494 dom_tree_walk_irg(irg, assign, NULL, &env);
496 if (chordal_env->opts->dump_flags & BE_CH_DUMP_TREE_INTV) {
498 ir_snprintf(buf, sizeof(buf), "ifg_%s_%F.eps", chordal_env->cls->name, irg);
499 plotter = new_plotter_ps(buf);
500 draw_interval_tree(&draw_chordal_def_opts, chordal_env, plotter);
501 plotter_free(plotter);
504 bitset_free(env.live);
507 BE_REGISTER_MODULE_CONSTRUCTOR(be_init_chordal)
508 void be_init_chordal(void)
510 static be_ra_chordal_coloring_t coloring = {
513 FIRM_DBG_REGISTER(dbg, "firm.be.chordal");
515 be_register_chordal_coloring("default", &coloring);