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"
57 #include "bestatevent.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 || ! 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 ir_node *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 = chordal_scan_insn(env, irn);
183 ir_node *res = insn->next_insn;
186 if (insn->pre_colored) {
188 for (i = 0; i < insn->use_start; ++i)
189 pset_insert_ptr(alloc_env->pre_colored, insn->ops[i].carrier);
193 * Perms inserted before the constraint handling phase are considered to be
194 * correctly precolored. These Perms arise during the ABI handling phase.
196 if (!insn->has_constraints || is_Phi(irn))
199 n_regs = env->cls->n_regs;
200 bs = bitset_alloca(n_regs);
201 alloc_nodes = ALLOCAN(ir_node*, n_regs);
202 //bp = hungarian_new(n_regs, n_regs, 2, HUNGARIAN_MATCH_PERFECT);
203 bp = bipartite_new(n_regs, n_regs);
204 assignment = ALLOCAN(int, n_regs);
205 partners = pmap_create();
208 * prepare the constraint handling of this node.
209 * Perms are constructed and Copies are created for constrained values
210 * interfering with the instruction.
212 perm = pre_process_constraints(alloc_env->chordal_env, &insn);
214 /* find suitable in operands to the out operands of the node. */
215 pair_up_operands(alloc_env, insn);
218 * look at the in/out operands and add each operand (and its possible partner)
219 * to a bipartite graph (left: nodes with partners, right: admissible colors).
221 for (i = 0, n_alloc = 0; i < insn->n_ops; ++i) {
222 be_operand_t *op = &insn->ops[i];
225 * If the operand has no partner or the partner has not been marked
226 * for allocation, determine the admissible registers and mark it
227 * for allocation by associating the node and its partner with the
228 * set of admissible registers via a bipartite graph.
230 if (!op->partner || !pmap_contains(partners, op->partner->carrier)) {
231 ir_node *partner = op->partner ? op->partner->carrier : NULL;
234 pmap_insert(partners, op->carrier, partner);
236 pmap_insert(partners, partner, op->carrier);
238 /* don't insert a node twice */
239 for (i = 0; i < n_alloc; ++i) {
240 if (alloc_nodes[i] == op->carrier) {
247 alloc_nodes[n_alloc] = op->carrier;
249 DBG((dbg, LEVEL_2, "\tassociating %+F and %+F\n", op->carrier,
252 bitset_clear_all(bs);
253 get_decisive_partner_regs(bs, op, op->partner);
255 DBG((dbg, LEVEL_2, "\tallowed registers for %+F: %B\n", op->carrier,
258 bitset_foreach(bs, col) {
259 //hungarian_add(bp, n_alloc, col, 1);
260 bipartite_add(bp, n_alloc, col);
268 * Put all nodes which live through the constrained instruction also to the
269 * allocation bipartite graph. They are considered unconstrained.
272 foreach_out_edge(perm, edge) {
274 ir_node *proj = get_edge_src_irn(edge);
275 be_lv_t *lv = be_get_irg_liveness(env->irg);
277 assert(is_Proj(proj));
279 if (!be_values_interfere(lv, proj, irn)
280 || pmap_contains(partners, proj))
283 /* don't insert a node twice */
284 for (i = 0; i < n_alloc; ++i) {
285 if (alloc_nodes[i] == proj) {
293 assert(n_alloc < n_regs);
295 alloc_nodes[n_alloc] = proj;
296 pmap_insert(partners, proj, NULL);
298 bitset_foreach(env->allocatable_regs, col) {
299 //hungarian_add(bp, n_alloc, col, 1);
300 bipartite_add(bp, n_alloc, col);
307 /* Compute a valid register allocation. */
309 hungarian_prepare_cost_matrix(bp, HUNGARIAN_MODE_MAXIMIZE_UTIL);
310 match_res = hungarian_solve(bp, assignment, &cost, 1);
311 assert(match_res == 0 && "matching failed");
313 /*bipartite_dump_f(stderr, bp);*/
314 bipartite_matching(bp, assignment);
317 /* Assign colors obtained from the matching. */
318 for (i = 0; i < n_alloc; ++i) {
319 const arch_register_t *reg;
322 assert(assignment[i] >= 0 && "there must have been a register assigned (node not register pressure faithful?)");
323 reg = arch_register_for_index(env->cls, assignment[i]);
325 irn = alloc_nodes[i];
327 arch_set_irn_register(irn, reg);
328 (void) pset_hinsert_ptr(alloc_env->pre_colored, irn);
329 DBG((dbg, LEVEL_2, "\tsetting %+F to register %s\n", irn, reg->name));
332 irn = pmap_get(ir_node, partners, alloc_nodes[i]);
334 arch_set_irn_register(irn, reg);
335 (void) pset_hinsert_ptr(alloc_env->pre_colored, irn);
336 DBG((dbg, LEVEL_2, "\tsetting %+F to register %s\n", irn, reg->name));
340 /* Allocate the non-constrained Projs of the Perm. */
342 bitset_clear_all(bs);
344 /* Put the colors of all Projs in a bitset. */
345 foreach_out_edge(perm, edge) {
346 ir_node *proj = get_edge_src_irn(edge);
347 const arch_register_t *reg = arch_get_irn_register(proj);
350 bitset_set(bs, reg->index);
353 /* Assign the not yet assigned Projs of the Perm a suitable color. */
354 foreach_out_edge(perm, edge) {
355 ir_node *proj = get_edge_src_irn(edge);
356 const arch_register_t *reg = arch_get_irn_register(proj);
358 DBG((dbg, LEVEL_2, "\tchecking reg of %+F: %s\n", proj, reg ? reg->name : "<none>"));
361 size_t const col = get_next_free_reg(alloc_env, bs);
362 reg = arch_register_for_index(env->cls, col);
363 bitset_set(bs, reg->index);
364 arch_set_irn_register(proj, reg);
365 pset_insert_ptr(alloc_env->pre_colored, proj);
366 DBG((dbg, LEVEL_2, "\tsetting %+F to register %s\n", proj, reg->name));
372 //hungarian_free(bp);
373 pmap_destroy(partners);
376 obstack_free(env->obst, base);
381 * Handle constraint nodes in each basic block.
382 * handle_constraints() inserts Perm nodes which perm
383 * over all values live at the constrained node right in front
384 * of the constrained node. These Perms signal a constrained node.
385 * For further comments, refer to handle_constraints().
387 static void constraints(ir_node *bl, void *data)
389 be_chordal_alloc_env_t *env = (be_chordal_alloc_env_t*)data;
392 for (irn = sched_first(bl); !sched_is_end(irn);) {
393 irn = 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 list_for_each_entry(border_t, b, head, list) {
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);
428 assert(reg && "Node must have been assigned a register");
429 col = arch_register_get_index(reg);
431 DBG((dbg, LEVEL_4, "%+F has reg %s\n", irn, reg->name));
433 /* Mark the color of the live in value as used. */
434 bitset_set(colors, col);
435 bitset_set(in_colors, col);
437 /* Mark the value live in. */
438 bitset_set(live, get_irn_idx(irn));
443 * Mind that the sequence of defs from back to front defines a perfect
444 * elimination order. So, coloring the definitions from first to last
447 list_for_each_entry_reverse(border_t, b, head, list) {
448 ir_node *irn = b->irn;
449 int nr = get_irn_idx(irn);
450 int ignore = arch_irn_is_ignore(irn);
453 * Assign a color, if it is a local def. Global defs already have a
456 if (b->is_def && !be_is_live_in(lv, block, irn)) {
457 const arch_register_t *reg;
460 if (ignore || pset_find_ptr(alloc_env->pre_colored, irn)) {
461 reg = arch_get_irn_register(irn);
463 assert(!bitset_is_set(colors, col) && "pre-colored register must be free");
465 col = get_next_free_reg(alloc_env, colors);
466 reg = arch_register_for_index(env->cls, col);
467 assert(arch_get_irn_register(irn) == NULL && "This node must not have been assigned a register yet");
470 bitset_set(colors, col);
471 arch_set_irn_register(irn, reg);
473 DBG((dbg, LEVEL_1, "\tassigning register %s(%d) to %+F\n", arch_register_get_name(reg), col, irn));
475 assert(!bitset_is_set(live, nr) && "Value's definition must not have been encountered");
476 bitset_set(live, nr);
477 } else if (!b->is_def) {
478 /* Clear the color upon a use. */
479 const arch_register_t *reg = arch_get_irn_register(irn);
482 assert(reg && "Register must have been assigned");
484 col = arch_register_get_index(reg);
486 bitset_clear(colors, col);
487 bitset_clear(live, nr);
492 void be_ra_chordal_color(be_chordal_env_t *chordal_env)
494 be_chordal_alloc_env_t env;
496 const arch_register_class_t *cls = chordal_env->cls;
498 int colors_n = arch_register_class_n_regs(cls);
499 ir_graph *irg = chordal_env->irg;
501 be_assure_live_sets(irg);
504 env.chordal_env = chordal_env;
505 env.colors_n = colors_n;
506 env.colors = bitset_alloca(colors_n);
507 env.tmp_colors = bitset_alloca(colors_n);
508 env.in_colors = bitset_alloca(colors_n);
509 env.pre_colored = pset_new_ptr_default();
511 be_timer_push(T_SPLIT);
513 if (chordal_env->opts->dump_flags & BE_CH_DUMP_SPLIT) {
514 snprintf(buf, sizeof(buf), "%s-split", chordal_env->cls->name);
515 dump_ir_graph(chordal_env->irg, buf);
518 be_timer_pop(T_SPLIT);
520 be_timer_push(T_CONSTR);
522 /* Handle register targeting constraints */
523 dom_tree_walk_irg(irg, constraints, NULL, &env);
525 if (chordal_env->opts->dump_flags & BE_CH_DUMP_CONSTR) {
526 snprintf(buf, sizeof(buf), "%s-constr", chordal_env->cls->name);
527 dump_ir_graph(chordal_env->irg, buf);
530 be_timer_pop(T_CONSTR);
532 env.live = bitset_malloc(get_irg_last_idx(chordal_env->irg));
534 /* First, determine the pressure */
535 dom_tree_walk_irg(irg, create_borders, NULL, env.chordal_env);
537 /* Assign the colors */
538 dom_tree_walk_irg(irg, assign, NULL, &env);
540 if (chordal_env->opts->dump_flags & BE_CH_DUMP_TREE_INTV) {
542 ir_snprintf(buf, sizeof(buf), "ifg_%s_%F.eps", chordal_env->cls->name, irg);
543 plotter = new_plotter_ps(buf);
544 draw_interval_tree(&draw_chordal_def_opts, chordal_env, plotter);
545 plotter_free(plotter);
548 bitset_free(env.live);
549 del_pset(env.pre_colored);
552 BE_REGISTER_MODULE_CONSTRUCTOR(be_init_chordal)
553 void be_init_chordal(void)
555 static be_ra_chordal_coloring_t coloring = {
558 FIRM_DBG_REGISTER(dbg, "firm.be.chordal");
560 be_register_chordal_coloring("default", &coloring);