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 #define DUMP_INTERVALS
71 typedef struct be_chordal_alloc_env_t {
72 be_chordal_env_t *chordal_env;
74 pset *pre_colored; /**< Set of precolored nodes. */
75 bitset_t *live; /**< A liveness bitset. */
76 bitset_t *tmp_colors; /**< An auxiliary bitset which is as long as the number of colors in the class. */
77 bitset_t *colors; /**< The color mask. */
78 bitset_t *in_colors; /**< Colors used by live in values. */
79 int colors_n; /**< The number of colors. */
80 } be_chordal_alloc_env_t;
82 static int get_next_free_reg(const be_chordal_alloc_env_t *alloc_env, bitset_t *colors)
84 bitset_t *tmp = alloc_env->tmp_colors;
85 bitset_copy(tmp, colors);
87 bitset_and(tmp, alloc_env->chordal_env->allocatable_regs);
88 return bitset_next_set(tmp, 0);
91 static bitset_t *get_decisive_partner_regs(bitset_t *bs, const be_operand_t *o1, const be_operand_t *o2)
96 bitset_copy(bs, o2->regs);
101 bitset_copy(bs, o1->regs);
105 assert(o1->req->cls == o2->req->cls);
107 if (bitset_contains(o1->regs, o2->regs)) {
108 bitset_copy(bs, o1->regs);
109 } else if (bitset_contains(o2->regs, o1->regs)) {
110 bitset_copy(bs, o2->regs);
118 static void pair_up_operands(const be_chordal_alloc_env_t *alloc_env, be_insn_t *insn)
120 const be_chordal_env_t *env = alloc_env->chordal_env;
121 bitset_t *bs = bitset_alloca(env->cls->n_regs);
126 * For each out operand, try to find an in operand which can be assigned the
127 * same register as the out operand.
129 for (j = 0; j < insn->use_start; ++j) {
130 be_operand_t *smallest = NULL;
131 int smallest_n_regs = env->cls->n_regs + 1;
132 be_operand_t *out_op = &insn->ops[j];
134 /* Try to find an in operand which has ... */
135 for (i = insn->use_start; i < insn->n_ops; ++i) {
137 be_operand_t *op = &insn->ops[i];
140 if (op->partner != NULL)
142 lv = be_get_irg_liveness(env->irg);
143 if (be_values_interfere(lv, op->irn, op->carrier))
146 bitset_copy(bs, op->regs);
147 bitset_and(bs, out_op->regs);
148 n_total = bitset_popcount(op->regs);
150 if (!bitset_is_empty(bs) && n_total < smallest_n_regs) {
152 smallest_n_regs = n_total;
156 if (smallest != NULL) {
157 for (i = insn->use_start; i < insn->n_ops; ++i) {
158 if (insn->ops[i].carrier == smallest->carrier)
159 insn->ops[i].partner = out_op;
162 out_op->partner = smallest;
163 smallest->partner = out_op;
168 static void handle_constraints(be_chordal_alloc_env_t *alloc_env,
173 ir_node **alloc_nodes;
174 //hungarian_problem_t *bp;
178 ir_node *perm = NULL;
179 //int match_res, cost;
180 be_chordal_env_t *env = alloc_env->chordal_env;
181 void *base = obstack_base(env->obst);
182 be_insn_t *insn = be_scan_insn(env, irn);
185 if (insn->pre_colored) {
187 for (i = 0; i < insn->use_start; ++i)
188 pset_insert_ptr(alloc_env->pre_colored, insn->ops[i].carrier);
192 * Perms inserted before the constraint handling phase are considered to be
193 * correctly precolored. These Perms arise during the ABI handling phase.
195 if (!insn->has_constraints || is_Phi(irn))
198 n_regs = env->cls->n_regs;
199 bs = bitset_alloca(n_regs);
200 alloc_nodes = ALLOCAN(ir_node*, n_regs);
201 //bp = hungarian_new(n_regs, n_regs, 2, HUNGARIAN_MATCH_PERFECT);
202 bp = bipartite_new(n_regs, n_regs);
203 assignment = ALLOCAN(int, n_regs);
204 partners = pmap_create();
207 * prepare the constraint handling of this node.
208 * Perms are constructed and Copies are created for constrained values
209 * interfering with the instruction.
211 perm = pre_process_constraints(alloc_env->chordal_env, &insn);
213 /* find suitable in operands to the out operands of the node. */
214 pair_up_operands(alloc_env, insn);
217 * look at the in/out operands and add each operand (and its possible partner)
218 * to a bipartite graph (left: nodes with partners, right: admissible colors).
220 for (i = 0, n_alloc = 0; i < insn->n_ops; ++i) {
221 be_operand_t *op = &insn->ops[i];
224 * If the operand has no partner or the partner has not been marked
225 * for allocation, determine the admissible registers and mark it
226 * for allocation by associating the node and its partner with the
227 * set of admissible registers via a bipartite graph.
229 if (!op->partner || !pmap_contains(partners, op->partner->carrier)) {
230 ir_node *partner = op->partner ? op->partner->carrier : NULL;
233 pmap_insert(partners, op->carrier, partner);
235 pmap_insert(partners, partner, op->carrier);
237 /* don't insert a node twice */
238 for (i = 0; i < n_alloc; ++i) {
239 if (alloc_nodes[i] == op->carrier) {
246 alloc_nodes[n_alloc] = op->carrier;
248 DBG((dbg, LEVEL_2, "\tassociating %+F and %+F\n", op->carrier,
251 bitset_clear_all(bs);
252 get_decisive_partner_regs(bs, op, op->partner);
254 DBG((dbg, LEVEL_2, "\tallowed registers for %+F: %B\n", op->carrier,
257 bitset_foreach(bs, col) {
258 //hungarian_add(bp, n_alloc, col, 1);
259 bipartite_add(bp, n_alloc, col);
267 * Put all nodes which live through the constrained instruction also to the
268 * allocation bipartite graph. They are considered unconstrained.
271 foreach_out_edge(perm, edge) {
273 ir_node *proj = get_edge_src_irn(edge);
274 be_lv_t *lv = be_get_irg_liveness(env->irg);
276 assert(is_Proj(proj));
278 if (!be_values_interfere(lv, proj, irn)
279 || pmap_contains(partners, proj))
282 /* don't insert a node twice */
283 for (i = 0; i < n_alloc; ++i) {
284 if (alloc_nodes[i] == proj) {
292 assert(n_alloc < n_regs);
294 alloc_nodes[n_alloc] = proj;
295 pmap_insert(partners, proj, NULL);
297 bitset_foreach(env->allocatable_regs, col) {
298 //hungarian_add(bp, n_alloc, col, 1);
299 bipartite_add(bp, n_alloc, col);
306 /* Compute a valid register allocation. */
308 hungarian_prepare_cost_matrix(bp, HUNGARIAN_MODE_MAXIMIZE_UTIL);
309 match_res = hungarian_solve(bp, assignment, &cost, 1);
310 assert(match_res == 0 && "matching failed");
312 /*bipartite_dump_f(stderr, bp);*/
313 bipartite_matching(bp, assignment);
316 /* Assign colors obtained from the matching. */
317 for (i = 0; i < n_alloc; ++i) {
318 const arch_register_t *reg;
321 assert(assignment[i] >= 0 && "there must have been a register assigned (node not register pressure faithful?)");
322 reg = arch_register_for_index(env->cls, assignment[i]);
324 irn = alloc_nodes[i];
326 arch_set_irn_register(irn, reg);
327 (void) pset_hinsert_ptr(alloc_env->pre_colored, irn);
328 DBG((dbg, LEVEL_2, "\tsetting %+F to register %s\n", irn, reg->name));
331 irn = pmap_get(ir_node, partners, alloc_nodes[i]);
333 arch_set_irn_register(irn, reg);
334 (void) pset_hinsert_ptr(alloc_env->pre_colored, irn);
335 DBG((dbg, LEVEL_2, "\tsetting %+F to register %s\n", irn, reg->name));
339 /* Allocate the non-constrained Projs of the Perm. */
341 bitset_clear_all(bs);
343 /* Put the colors of all Projs in a bitset. */
344 foreach_out_edge(perm, edge) {
345 ir_node *proj = get_edge_src_irn(edge);
346 const arch_register_t *reg = arch_get_irn_register(proj);
349 bitset_set(bs, reg->index);
352 /* Assign the not yet assigned Projs of the Perm a suitable color. */
353 foreach_out_edge(perm, edge) {
354 ir_node *proj = get_edge_src_irn(edge);
355 const arch_register_t *reg = arch_get_irn_register(proj);
357 DBG((dbg, LEVEL_2, "\tchecking reg of %+F: %s\n", proj, reg ? reg->name : "<none>"));
360 size_t const col = get_next_free_reg(alloc_env, bs);
361 reg = arch_register_for_index(env->cls, col);
362 bitset_set(bs, reg->index);
363 arch_set_irn_register(proj, reg);
364 pset_insert_ptr(alloc_env->pre_colored, proj);
365 DBG((dbg, LEVEL_2, "\tsetting %+F to register %s\n", proj, reg->name));
371 //hungarian_free(bp);
372 pmap_destroy(partners);
375 obstack_free(env->obst, base);
379 * Handle constraint nodes in each basic block.
380 * handle_constraints() inserts Perm nodes which perm
381 * over all values live at the constrained node right in front
382 * of the constrained node. These Perms signal a constrained node.
383 * For further comments, refer to handle_constraints().
385 static void constraints(ir_node *bl, void *data)
387 be_chordal_alloc_env_t *env = (be_chordal_alloc_env_t*)data;
390 for (irn = sched_first(bl); !sched_is_end(irn);) {
391 ir_node *const next = sched_next(irn);
392 handle_constraints(env, irn);
397 static void assign(ir_node *block, void *env_ptr)
399 be_chordal_alloc_env_t *alloc_env = (be_chordal_alloc_env_t*)env_ptr;
400 be_chordal_env_t *env = alloc_env->chordal_env;
401 bitset_t *live = alloc_env->live;
402 bitset_t *colors = alloc_env->colors;
403 bitset_t *in_colors = alloc_env->in_colors;
404 struct list_head *head = get_block_border_head(env, block);
405 be_lv_t *lv = be_get_irg_liveness(env->irg);
407 bitset_clear_all(colors);
408 bitset_clear_all(live);
409 bitset_clear_all(in_colors);
411 DBG((dbg, LEVEL_4, "Assigning colors for block %+F\n", block));
412 DBG((dbg, LEVEL_4, "\tusedef chain for block\n"));
413 foreach_border_head(head, b) {
414 DBG((dbg, LEVEL_4, "\t%s %+F/%d\n", b->is_def ? "def" : "use",
415 b->irn, get_irn_idx(b->irn)));
419 * Add initial defs for all values live in.
420 * Since their colors have already been assigned (The dominators were
421 * allocated before), we have to mark their colors as used also.
423 be_lv_foreach(lv, block, be_lv_state_in, irn) {
424 if (has_reg_class(env, irn)) {
425 const arch_register_t *reg = arch_get_irn_register(irn);
427 assert(reg && "Node must have been assigned a register");
428 DBG((dbg, LEVEL_4, "%+F has reg %s\n", irn, reg->name));
430 /* Mark the color of the live in value as used. */
431 int const col = reg->index;
432 bitset_set(colors, col);
433 bitset_set(in_colors, col);
435 /* Mark the value live in. */
436 bitset_set(live, get_irn_idx(irn));
441 * Mind that the sequence of defs from back to front defines a perfect
442 * elimination order. So, coloring the definitions from first to last
445 foreach_border_head(head, b) {
446 ir_node *irn = b->irn;
447 int nr = get_irn_idx(irn);
448 int ignore = arch_irn_is_ignore(irn);
451 * Assign a color, if it is a local def. Global defs already have a
454 if (b->is_def && !be_is_live_in(lv, block, irn)) {
455 const arch_register_t *reg;
458 if (ignore || pset_find_ptr(alloc_env->pre_colored, irn)) {
459 reg = arch_get_irn_register(irn);
461 assert(!bitset_is_set(colors, col) && "pre-colored register must be free");
463 col = get_next_free_reg(alloc_env, colors);
464 reg = arch_register_for_index(env->cls, col);
465 assert(arch_get_irn_register(irn) == NULL && "This node must not have been assigned a register yet");
468 bitset_set(colors, col);
469 arch_set_irn_register(irn, reg);
471 DBG((dbg, LEVEL_1, "\tassigning register %s(%d) to %+F\n", reg->name, col, irn));
473 assert(!bitset_is_set(live, nr) && "Value's definition must not have been encountered");
474 bitset_set(live, nr);
475 } else if (!b->is_def) {
476 /* Clear the color upon a use. */
477 const arch_register_t *reg = arch_get_irn_register(irn);
479 assert(reg && "Register must have been assigned");
481 bitset_clear(colors, reg->index);
482 bitset_clear(live, nr);
487 static void be_ra_chordal_color(be_chordal_env_t *const chordal_env)
489 be_chordal_alloc_env_t env;
491 const arch_register_class_t *cls = chordal_env->cls;
493 int colors_n = arch_register_class_n_regs(cls);
494 ir_graph *irg = chordal_env->irg;
496 be_assure_live_sets(irg);
499 env.chordal_env = chordal_env;
500 env.colors_n = colors_n;
501 env.colors = bitset_alloca(colors_n);
502 env.tmp_colors = bitset_alloca(colors_n);
503 env.in_colors = bitset_alloca(colors_n);
504 env.pre_colored = pset_new_ptr_default();
506 be_timer_push(T_SPLIT);
508 if (chordal_env->opts->dump_flags & BE_CH_DUMP_SPLIT) {
509 snprintf(buf, sizeof(buf), "%s-split", chordal_env->cls->name);
510 dump_ir_graph(chordal_env->irg, buf);
513 be_timer_pop(T_SPLIT);
515 be_timer_push(T_CONSTR);
517 /* Handle register targeting constraints */
518 dom_tree_walk_irg(irg, constraints, NULL, &env);
520 if (chordal_env->opts->dump_flags & BE_CH_DUMP_CONSTR) {
521 snprintf(buf, sizeof(buf), "%s-constr", chordal_env->cls->name);
522 dump_ir_graph(chordal_env->irg, buf);
525 be_timer_pop(T_CONSTR);
527 env.live = bitset_malloc(get_irg_last_idx(chordal_env->irg));
529 /* First, determine the pressure */
530 dom_tree_walk_irg(irg, create_borders, NULL, env.chordal_env);
532 /* Assign the colors */
533 dom_tree_walk_irg(irg, assign, NULL, &env);
535 if (chordal_env->opts->dump_flags & BE_CH_DUMP_TREE_INTV) {
537 ir_snprintf(buf, sizeof(buf), "ifg_%s_%F.eps", chordal_env->cls->name, irg);
538 plotter = new_plotter_ps(buf);
539 draw_interval_tree(&draw_chordal_def_opts, chordal_env, plotter);
540 plotter_free(plotter);
543 bitset_free(env.live);
544 del_pset(env.pre_colored);
547 BE_REGISTER_MODULE_CONSTRUCTOR(be_init_chordal)
548 void be_init_chordal(void)
550 static be_ra_chordal_coloring_t coloring = {
553 FIRM_DBG_REGISTER(dbg, "firm.be.chordal");
555 be_register_chordal_coloring("default", &coloring);