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
35 #include "raw_bitset.h"
37 #include "bipartite.h"
38 #include "hungarian.h"
41 #include "irgraph_t.h"
42 #include "irprintf_t.h"
60 #include "bestatevent.h"
62 #include "beintlive_t.h"
64 #include "bechordal_t.h"
65 #include "bechordal_draw.h"
68 #include "bechordal_common.h"
70 DEBUG_ONLY(static firm_dbg_module_t *dbg = NULL;)
74 #define DUMP_INTERVALS
76 typedef struct be_chordal_alloc_env_t {
77 be_chordal_env_t *chordal_env;
79 pset *pre_colored; /**< Set of precolored nodes. */
80 bitset_t *live; /**< A liveness bitset. */
81 bitset_t *tmp_colors; /**< An auxiliary bitset which is as long as the number of colors in the class. */
82 bitset_t *colors; /**< The color mask. */
83 bitset_t *in_colors; /**< Colors used by live in values. */
84 int colors_n; /**< The number of colors. */
85 } be_chordal_alloc_env_t;
87 static int get_next_free_reg(const be_chordal_alloc_env_t *alloc_env, bitset_t *colors)
89 bitset_t *tmp = alloc_env->tmp_colors;
90 bitset_copy(tmp, colors);
92 bitset_and(tmp, alloc_env->chordal_env->allocatable_regs);
93 return bitset_next_set(tmp, 0);
96 static bitset_t *get_decisive_partner_regs(bitset_t *bs, const be_operand_t *o1, const be_operand_t *o2)
101 bitset_copy(bs, o2->regs);
106 bitset_copy(bs, o1->regs);
110 assert(o1->req->cls == o2->req->cls || ! o1->req->cls || ! o2->req->cls);
112 if (bitset_contains(o1->regs, o2->regs)) {
113 bitset_copy(bs, o1->regs);
114 } else if (bitset_contains(o2->regs, o1->regs)) {
115 bitset_copy(bs, o2->regs);
123 static void pair_up_operands(const be_chordal_alloc_env_t *alloc_env, be_insn_t *insn)
125 const be_chordal_env_t *env = alloc_env->chordal_env;
126 bitset_t *bs = bitset_alloca(env->cls->n_regs);
131 * For each out operand, try to find an in operand which can be assigned the
132 * same register as the out operand.
134 for (j = 0; j < insn->use_start; ++j) {
135 be_operand_t *smallest = NULL;
136 int smallest_n_regs = env->cls->n_regs + 1;
137 be_operand_t *out_op = &insn->ops[j];
139 /* Try to find an in operand which has ... */
140 for (i = insn->use_start; i < insn->n_ops; ++i) {
142 be_operand_t *op = &insn->ops[i];
145 if (op->partner != NULL)
147 lv = be_get_irg_liveness(env->irg);
148 if (be_values_interfere(lv, op->irn, op->carrier))
151 bitset_copy(bs, op->regs);
152 bitset_and(bs, out_op->regs);
153 n_total = bitset_popcount(op->regs);
155 if (!bitset_is_empty(bs) && n_total < smallest_n_regs) {
157 smallest_n_regs = n_total;
161 if (smallest != NULL) {
162 for (i = insn->use_start; i < insn->n_ops; ++i) {
163 if (insn->ops[i].carrier == smallest->carrier)
164 insn->ops[i].partner = out_op;
167 out_op->partner = smallest;
168 smallest->partner = out_op;
173 static ir_node *handle_constraints(be_chordal_alloc_env_t *alloc_env,
174 ir_node *irn, int *silent)
178 ir_node **alloc_nodes;
179 //hungarian_problem_t *bp;
184 const ir_edge_t *edge;
185 ir_node *perm = NULL;
186 //int match_res, cost;
187 be_chordal_env_t *env = alloc_env->chordal_env;
188 void *base = obstack_base(env->obst);
189 be_insn_t *insn = chordal_scan_insn(env, irn);
190 ir_node *res = insn->next_insn;
191 int be_silent = *silent;
194 if (insn->pre_colored) {
196 for (i = 0; i < insn->use_start; ++i)
197 pset_insert_ptr(alloc_env->pre_colored, insn->ops[i].carrier);
201 * If the current node is a barrier toggle the silent flag.
202 * If we are in the start block, we are ought to be silent at the beginning,
203 * so the toggling activates the constraint handling but skips the barrier.
204 * If we are in the end block we handle the in requirements of the barrier
205 * and set the rest to silent.
207 if (be_is_Barrier(irn))
214 * Perms inserted before the constraint handling phase are considered to be
215 * correctly precolored. These Perms arise during the ABI handling phase.
217 if (!insn->has_constraints)
220 n_regs = env->cls->n_regs;
221 bs = bitset_alloca(n_regs);
222 alloc_nodes = ALLOCAN(ir_node*, n_regs);
223 //bp = hungarian_new(n_regs, n_regs, 2, HUNGARIAN_MATCH_PERFECT);
224 bp = bipartite_new(n_regs, n_regs);
225 assignment = ALLOCAN(int, n_regs);
226 partners = pmap_create();
229 * prepare the constraint handling of this node.
230 * Perms are constructed and Copies are created for constrained values
231 * interfering with the instruction.
233 perm = pre_process_constraints(alloc_env->chordal_env, &insn);
235 /* find suitable in operands to the out operands of the node. */
236 pair_up_operands(alloc_env, insn);
239 * look at the in/out operands and add each operand (and its possible partner)
240 * to a bipartite graph (left: nodes with partners, right: admissible colors).
242 for (i = 0, n_alloc = 0; i < insn->n_ops; ++i) {
243 be_operand_t *op = &insn->ops[i];
246 * If the operand has no partner or the partner has not been marked
247 * for allocation, determine the admissible registers and mark it
248 * for allocation by associating the node and its partner with the
249 * set of admissible registers via a bipartite graph.
251 if (!op->partner || !pmap_contains(partners, op->partner->carrier)) {
252 ir_node *partner = op->partner ? op->partner->carrier : NULL;
255 pmap_insert(partners, op->carrier, partner);
257 pmap_insert(partners, partner, op->carrier);
259 /* don't insert a node twice */
260 for (i = 0; i < n_alloc; ++i) {
261 if (alloc_nodes[i] == op->carrier) {
268 alloc_nodes[n_alloc] = op->carrier;
270 DBG((dbg, LEVEL_2, "\tassociating %+F and %+F\n", op->carrier,
273 bitset_clear_all(bs);
274 get_decisive_partner_regs(bs, op, op->partner);
276 DBG((dbg, LEVEL_2, "\tallowed registers for %+F: %B\n", op->carrier,
279 bitset_foreach(bs, col) {
280 //hungarian_add(bp, n_alloc, col, 1);
281 bipartite_add(bp, n_alloc, col);
289 * Put all nodes which live through the constrained instruction also to the
290 * allocation bipartite graph. They are considered unconstrained.
293 foreach_out_edge(perm, edge) {
295 ir_node *proj = get_edge_src_irn(edge);
296 be_lv_t *lv = be_get_irg_liveness(env->irg);
298 assert(is_Proj(proj));
300 if (!be_values_interfere(lv, proj, irn)
301 || pmap_contains(partners, proj))
304 /* don't insert a node twice */
305 for (i = 0; i < n_alloc; ++i) {
306 if (alloc_nodes[i] == proj) {
314 assert(n_alloc < n_regs);
316 alloc_nodes[n_alloc] = proj;
317 pmap_insert(partners, proj, NULL);
319 bitset_foreach(env->allocatable_regs, col) {
320 //hungarian_add(bp, n_alloc, col, 1);
321 bipartite_add(bp, n_alloc, col);
328 /* Compute a valid register allocation. */
330 hungarian_prepare_cost_matrix(bp, HUNGARIAN_MODE_MAXIMIZE_UTIL);
331 match_res = hungarian_solve(bp, assignment, &cost, 1);
332 assert(match_res == 0 && "matching failed");
334 /*bipartite_dump_f(stderr, bp);*/
335 bipartite_matching(bp, assignment);
338 /* Assign colors obtained from the matching. */
339 for (i = 0; i < n_alloc; ++i) {
340 const arch_register_t *reg;
343 assert(assignment[i] >= 0 && "there must have been a register assigned (node not register pressure faithful?)");
344 reg = arch_register_for_index(env->cls, assignment[i]);
346 irn = alloc_nodes[i];
348 arch_set_irn_register(irn, reg);
349 (void) pset_hinsert_ptr(alloc_env->pre_colored, irn);
350 DBG((dbg, LEVEL_2, "\tsetting %+F to register %s\n", irn, reg->name));
353 irn = pmap_get(partners, alloc_nodes[i]);
355 arch_set_irn_register(irn, reg);
356 (void) pset_hinsert_ptr(alloc_env->pre_colored, irn);
357 DBG((dbg, LEVEL_2, "\tsetting %+F to register %s\n", irn, reg->name));
361 /* Allocate the non-constrained Projs of the Perm. */
363 bitset_clear_all(bs);
365 /* Put the colors of all Projs in a bitset. */
366 foreach_out_edge(perm, edge) {
367 ir_node *proj = get_edge_src_irn(edge);
368 const arch_register_t *reg = arch_get_irn_register(proj);
371 bitset_set(bs, reg->index);
374 /* Assign the not yet assigned Projs of the Perm a suitable color. */
375 foreach_out_edge(perm, edge) {
376 ir_node *proj = get_edge_src_irn(edge);
377 const arch_register_t *reg = arch_get_irn_register(proj);
379 DBG((dbg, LEVEL_2, "\tchecking reg of %+F: %s\n", proj, reg ? reg->name : "<none>"));
382 col = get_next_free_reg(alloc_env, bs);
383 reg = arch_register_for_index(env->cls, col);
384 bitset_set(bs, reg->index);
385 arch_set_irn_register(proj, reg);
386 pset_insert_ptr(alloc_env->pre_colored, proj);
387 DBG((dbg, LEVEL_2, "\tsetting %+F to register %s\n", proj, reg->name));
393 //hungarian_free(bp);
394 pmap_destroy(partners);
397 obstack_free(env->obst, base);
402 * Handle constraint nodes in each basic block.
403 * handle_constraints() inserts Perm nodes which perm
404 * over all values live at the constrained node right in front
405 * of the constrained node. These Perms signal a constrained node.
406 * For further comments, refer to handle_constraints().
408 static void constraints(ir_node *bl, void *data)
411 * Start silent in the start block.
412 * The silence remains until the first barrier is seen.
413 * Each other block is begun loud.
415 int silent = bl == get_irg_start_block(get_irn_irg(bl));
416 be_chordal_alloc_env_t *env = data;
420 * If the block is the start block search the barrier and
421 * start handling constraints from there.
423 for (irn = sched_first(bl); !sched_is_end(irn);) {
424 irn = handle_constraints(env, irn, &silent);
428 static void assign(ir_node *block, void *env_ptr)
430 be_chordal_alloc_env_t *alloc_env = env_ptr;
431 be_chordal_env_t *env = alloc_env->chordal_env;
432 bitset_t *live = alloc_env->live;
433 bitset_t *colors = alloc_env->colors;
434 bitset_t *in_colors = alloc_env->in_colors;
435 struct list_head *head = get_block_border_head(env, block);
436 be_lv_t *lv = be_get_irg_liveness(env->irg);
442 bitset_clear_all(colors);
443 bitset_clear_all(live);
444 bitset_clear_all(in_colors);
446 DBG((dbg, LEVEL_4, "Assigning colors for block %+F\n", block));
447 DBG((dbg, LEVEL_4, "\tusedef chain for block\n"));
448 list_for_each_entry(border_t, b, head, list) {
449 DBG((dbg, LEVEL_4, "\t%s %+F/%d\n", b->is_def ? "def" : "use",
450 b->irn, get_irn_idx(b->irn)));
454 * Add initial defs for all values live in.
455 * Since their colors have already been assigned (The dominators were
456 * allocated before), we have to mark their colors as used also.
458 be_lv_foreach(lv, block, be_lv_state_in, idx) {
459 irn = be_lv_get_irn(lv, block, idx);
460 if (has_reg_class(env, irn)) {
461 const arch_register_t *reg = arch_get_irn_register(irn);
464 assert(reg && "Node must have been assigned a register");
465 col = arch_register_get_index(reg);
467 DBG((dbg, LEVEL_4, "%+F has reg %s\n", irn, reg->name));
469 /* Mark the color of the live in value as used. */
470 bitset_set(colors, col);
471 bitset_set(in_colors, col);
473 /* Mark the value live in. */
474 bitset_set(live, get_irn_idx(irn));
479 * Mind that the sequence of defs from back to front defines a perfect
480 * elimination order. So, coloring the definitions from first to last
483 list_for_each_entry_reverse(border_t, b, head, list) {
484 ir_node *irn = b->irn;
485 int nr = get_irn_idx(irn);
486 int ignore = arch_irn_is_ignore(irn);
489 * Assign a color, if it is a local def. Global defs already have a
492 if (b->is_def && !be_is_live_in(lv, block, irn)) {
493 const arch_register_t *reg;
496 if (ignore || pset_find_ptr(alloc_env->pre_colored, irn)) {
497 reg = arch_get_irn_register(irn);
499 assert(!bitset_is_set(colors, col) && "pre-colored register must be free");
501 col = get_next_free_reg(alloc_env, colors);
502 reg = arch_register_for_index(env->cls, col);
503 assert(arch_get_irn_register(irn) == NULL && "This node must not have been assigned a register yet");
506 bitset_set(colors, col);
507 arch_set_irn_register(irn, reg);
509 DBG((dbg, LEVEL_1, "\tassigning register %s(%d) to %+F\n", arch_register_get_name(reg), col, irn));
511 assert(!bitset_is_set(live, nr) && "Value's definition must not have been encountered");
512 bitset_set(live, nr);
513 } else if (!b->is_def) {
514 /* Clear the color upon a use. */
515 const arch_register_t *reg = arch_get_irn_register(irn);
518 assert(reg && "Register must have been assigned");
520 col = arch_register_get_index(reg);
522 bitset_clear(colors, col);
523 bitset_clear(live, nr);
528 void be_ra_chordal_color(be_chordal_env_t *chordal_env)
530 be_chordal_alloc_env_t env;
533 const arch_register_class_t *cls = chordal_env->cls;
535 int colors_n = arch_register_class_n_regs(cls);
536 ir_graph *irg = chordal_env->irg;
538 lv = be_assure_liveness(irg);
539 be_liveness_assure_sets(lv);
540 be_liveness_assure_chk(lv);
544 env.chordal_env = chordal_env;
545 env.colors_n = colors_n;
546 env.colors = bitset_alloca(colors_n);
547 env.tmp_colors = bitset_alloca(colors_n);
548 env.in_colors = bitset_alloca(colors_n);
549 env.pre_colored = pset_new_ptr_default();
551 be_timer_push(T_CONSTR);
553 /* Handle register targeting constraints */
554 dom_tree_walk_irg(irg, constraints, NULL, &env);
556 if (chordal_env->opts->dump_flags & BE_CH_DUMP_CONSTR) {
557 snprintf(buf, sizeof(buf), "%s-constr", chordal_env->cls->name);
558 dump_ir_graph(chordal_env->irg, buf);
561 be_timer_pop(T_CONSTR);
563 env.live = bitset_malloc(get_irg_last_idx(chordal_env->irg));
565 /* First, determine the pressure */
566 dom_tree_walk_irg(irg, create_borders, NULL, env.chordal_env);
568 /* Assign the colors */
569 dom_tree_walk_irg(irg, assign, NULL, &env);
571 if (chordal_env->opts->dump_flags & BE_CH_DUMP_TREE_INTV) {
573 ir_snprintf(buf, sizeof(buf), "ifg_%s_%F.eps", chordal_env->cls->name, irg);
574 plotter = new_plotter_ps(buf);
575 draw_interval_tree(&draw_chordal_def_opts, chordal_env, plotter);
576 plotter_free(plotter);
579 bitset_free(env.live);
580 del_pset(env.pre_colored);
583 BE_REGISTER_MODULE_CONSTRUCTOR(be_init_chordal);
584 void be_init_chordal(void)
586 static be_ra_chordal_coloring_t coloring = {
589 FIRM_DBG_REGISTER(dbg, "firm.be.chordal");
591 be_register_chordal_coloring("default", &coloring);