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"
36 #include "bipartite.h"
37 #include "hungarian.h"
40 #include "irgraph_t.h"
41 #include "irprintf_t.h"
58 #include "bestatevent.h"
60 #include "beintlive_t.h"
62 #include "bechordal_t.h"
63 #include "bechordal_draw.h"
66 #include "bechordal_common.h"
68 DEBUG_ONLY(static firm_dbg_module_t *dbg = NULL;)
70 #define DUMP_INTERVALS
72 typedef struct be_chordal_alloc_env_t {
73 be_chordal_env_t *chordal_env;
75 pset *pre_colored; /**< Set of precolored nodes. */
76 bitset_t *live; /**< A liveness bitset. */
77 bitset_t *tmp_colors; /**< An auxiliary bitset which is as long as the number of colors in the class. */
78 bitset_t *colors; /**< The color mask. */
79 bitset_t *in_colors; /**< Colors used by live in values. */
80 int colors_n; /**< The number of colors. */
81 } be_chordal_alloc_env_t;
83 static int get_next_free_reg(const be_chordal_alloc_env_t *alloc_env, bitset_t *colors)
85 bitset_t *tmp = alloc_env->tmp_colors;
86 bitset_copy(tmp, colors);
88 bitset_and(tmp, alloc_env->chordal_env->allocatable_regs);
89 return bitset_next_set(tmp, 0);
92 static bitset_t *get_decisive_partner_regs(bitset_t *bs, const be_operand_t *o1, const be_operand_t *o2)
97 bitset_copy(bs, o2->regs);
102 bitset_copy(bs, o1->regs);
106 assert(o1->req->cls == o2->req->cls || ! o1->req->cls || ! o2->req->cls);
108 if (bitset_contains(o1->regs, o2->regs)) {
109 bitset_copy(bs, o1->regs);
110 } else if (bitset_contains(o2->regs, o1->regs)) {
111 bitset_copy(bs, o2->regs);
119 static void pair_up_operands(const be_chordal_alloc_env_t *alloc_env, be_insn_t *insn)
121 const be_chordal_env_t *env = alloc_env->chordal_env;
122 bitset_t *bs = bitset_alloca(env->cls->n_regs);
127 * For each out operand, try to find an in operand which can be assigned the
128 * same register as the out operand.
130 for (j = 0; j < insn->use_start; ++j) {
131 be_operand_t *smallest = NULL;
132 int smallest_n_regs = env->cls->n_regs + 1;
133 be_operand_t *out_op = &insn->ops[j];
135 /* Try to find an in operand which has ... */
136 for (i = insn->use_start; i < insn->n_ops; ++i) {
138 be_operand_t *op = &insn->ops[i];
141 if (op->partner != NULL)
143 lv = be_get_irg_liveness(env->irg);
144 if (be_values_interfere(lv, op->irn, op->carrier))
147 bitset_copy(bs, op->regs);
148 bitset_and(bs, out_op->regs);
149 n_total = bitset_popcount(op->regs);
151 if (!bitset_is_empty(bs) && n_total < smallest_n_regs) {
153 smallest_n_regs = n_total;
157 if (smallest != NULL) {
158 for (i = insn->use_start; i < insn->n_ops; ++i) {
159 if (insn->ops[i].carrier == smallest->carrier)
160 insn->ops[i].partner = out_op;
163 out_op->partner = smallest;
164 smallest->partner = out_op;
169 static ir_node *handle_constraints(be_chordal_alloc_env_t *alloc_env,
174 ir_node **alloc_nodes;
175 //hungarian_problem_t *bp;
180 const ir_edge_t *edge;
181 ir_node *perm = NULL;
182 //int match_res, cost;
183 be_chordal_env_t *env = alloc_env->chordal_env;
184 void *base = obstack_base(env->obst);
185 be_insn_t *insn = chordal_scan_insn(env, irn);
186 ir_node *res = insn->next_insn;
189 if (insn->pre_colored) {
191 for (i = 0; i < insn->use_start; ++i)
192 pset_insert_ptr(alloc_env->pre_colored, insn->ops[i].carrier);
196 * Perms inserted before the constraint handling phase are considered to be
197 * correctly precolored. These Perms arise during the ABI handling phase.
199 if (!insn->has_constraints)
202 n_regs = env->cls->n_regs;
203 bs = bitset_alloca(n_regs);
204 alloc_nodes = ALLOCAN(ir_node*, n_regs);
205 //bp = hungarian_new(n_regs, n_regs, 2, HUNGARIAN_MATCH_PERFECT);
206 bp = bipartite_new(n_regs, n_regs);
207 assignment = ALLOCAN(int, n_regs);
208 partners = pmap_create();
211 * prepare the constraint handling of this node.
212 * Perms are constructed and Copies are created for constrained values
213 * interfering with the instruction.
215 perm = pre_process_constraints(alloc_env->chordal_env, &insn);
217 /* find suitable in operands to the out operands of the node. */
218 pair_up_operands(alloc_env, insn);
221 * look at the in/out operands and add each operand (and its possible partner)
222 * to a bipartite graph (left: nodes with partners, right: admissible colors).
224 for (i = 0, n_alloc = 0; i < insn->n_ops; ++i) {
225 be_operand_t *op = &insn->ops[i];
228 * If the operand has no partner or the partner has not been marked
229 * for allocation, determine the admissible registers and mark it
230 * for allocation by associating the node and its partner with the
231 * set of admissible registers via a bipartite graph.
233 if (!op->partner || !pmap_contains(partners, op->partner->carrier)) {
234 ir_node *partner = op->partner ? op->partner->carrier : NULL;
237 pmap_insert(partners, op->carrier, partner);
239 pmap_insert(partners, partner, op->carrier);
241 /* don't insert a node twice */
242 for (i = 0; i < n_alloc; ++i) {
243 if (alloc_nodes[i] == op->carrier) {
250 alloc_nodes[n_alloc] = op->carrier;
252 DBG((dbg, LEVEL_2, "\tassociating %+F and %+F\n", op->carrier,
255 bitset_clear_all(bs);
256 get_decisive_partner_regs(bs, op, op->partner);
258 DBG((dbg, LEVEL_2, "\tallowed registers for %+F: %B\n", op->carrier,
261 bitset_foreach(bs, col) {
262 //hungarian_add(bp, n_alloc, col, 1);
263 bipartite_add(bp, n_alloc, col);
271 * Put all nodes which live through the constrained instruction also to the
272 * allocation bipartite graph. They are considered unconstrained.
275 foreach_out_edge(perm, edge) {
277 ir_node *proj = get_edge_src_irn(edge);
278 be_lv_t *lv = be_get_irg_liveness(env->irg);
280 assert(is_Proj(proj));
282 if (!be_values_interfere(lv, proj, irn)
283 || pmap_contains(partners, proj))
286 /* don't insert a node twice */
287 for (i = 0; i < n_alloc; ++i) {
288 if (alloc_nodes[i] == proj) {
296 assert(n_alloc < n_regs);
298 alloc_nodes[n_alloc] = proj;
299 pmap_insert(partners, proj, NULL);
301 bitset_foreach(env->allocatable_regs, col) {
302 //hungarian_add(bp, n_alloc, col, 1);
303 bipartite_add(bp, n_alloc, col);
310 /* Compute a valid register allocation. */
312 hungarian_prepare_cost_matrix(bp, HUNGARIAN_MODE_MAXIMIZE_UTIL);
313 match_res = hungarian_solve(bp, assignment, &cost, 1);
314 assert(match_res == 0 && "matching failed");
316 /*bipartite_dump_f(stderr, bp);*/
317 bipartite_matching(bp, assignment);
320 /* Assign colors obtained from the matching. */
321 for (i = 0; i < n_alloc; ++i) {
322 const arch_register_t *reg;
325 assert(assignment[i] >= 0 && "there must have been a register assigned (node not register pressure faithful?)");
326 reg = arch_register_for_index(env->cls, assignment[i]);
328 irn = alloc_nodes[i];
330 arch_set_irn_register(irn, reg);
331 (void) pset_hinsert_ptr(alloc_env->pre_colored, irn);
332 DBG((dbg, LEVEL_2, "\tsetting %+F to register %s\n", irn, reg->name));
335 irn = (ir_node*)pmap_get(partners, alloc_nodes[i]);
337 arch_set_irn_register(irn, reg);
338 (void) pset_hinsert_ptr(alloc_env->pre_colored, irn);
339 DBG((dbg, LEVEL_2, "\tsetting %+F to register %s\n", irn, reg->name));
343 /* Allocate the non-constrained Projs of the Perm. */
345 bitset_clear_all(bs);
347 /* Put the colors of all Projs in a bitset. */
348 foreach_out_edge(perm, edge) {
349 ir_node *proj = get_edge_src_irn(edge);
350 const arch_register_t *reg = arch_get_irn_register(proj);
353 bitset_set(bs, reg->index);
356 /* Assign the not yet assigned Projs of the Perm a suitable color. */
357 foreach_out_edge(perm, edge) {
358 ir_node *proj = get_edge_src_irn(edge);
359 const arch_register_t *reg = arch_get_irn_register(proj);
361 DBG((dbg, LEVEL_2, "\tchecking reg of %+F: %s\n", proj, reg ? reg->name : "<none>"));
364 col = get_next_free_reg(alloc_env, bs);
365 reg = arch_register_for_index(env->cls, col);
366 bitset_set(bs, reg->index);
367 arch_set_irn_register(proj, reg);
368 pset_insert_ptr(alloc_env->pre_colored, proj);
369 DBG((dbg, LEVEL_2, "\tsetting %+F to register %s\n", proj, reg->name));
375 //hungarian_free(bp);
376 pmap_destroy(partners);
379 obstack_free(env->obst, base);
383 static unsigned check_alignment_constraints(ir_node *node)
385 const arch_register_req_t *req = arch_get_irn_register_req(node);
386 return /* req->type == arch_register_req_type_aligned
387 || */ req->width > 1;
391 static ir_node *handle_split(be_chordal_alloc_env_t *alloc_env,
394 ir_node *perm = NULL;
395 be_chordal_env_t *env = alloc_env->chordal_env;
396 void *base = obstack_base(env->obst);
397 be_insn_t *insn = chordal_scan_insn(env, irn);
398 ir_node *res = insn->next_insn;
401 * Perms are inserted later before constrained instruction.
402 * So, this function does not split here.
404 if (!insn->has_constraints) {
406 unsigned has_alignment_constraints = 0;
407 const arch_register_class_t *cls = env->cls;
409 be_foreach_definition (irn, cls, value,
410 has_alignment_constraints
411 |= check_alignment_constraints(value);
414 if (has_alignment_constraints) {
416 * Make the Perm, recompute liveness and re-scan the insn since the
417 * in operands are now the Projs of the Perm.
419 perm = insert_Perm_after(env->irg, env->cls, sched_prev(insn->irn));
421 DBG((dbg, LEVEL_2, "\tsplit before %+F with node %+F\n", irn, perm));
426 obstack_free(env->obst, base);
431 * Add split points before operations that have pairing constraints.
432 * handle_split() inserts Perm nodes which perm
433 * over all values live at the constrained node right in front
434 * of the constrained node.
436 static void split(ir_node *bl, void *data)
438 be_chordal_alloc_env_t *env = (be_chordal_alloc_env_t*)data;
441 for (irn = sched_first(bl); !sched_is_end(irn);) {
442 irn = handle_split(env, irn);
447 * Handle constraint nodes in each basic block.
448 * handle_constraints() inserts Perm nodes which perm
449 * over all values live at the constrained node right in front
450 * of the constrained node. These Perms signal a constrained node.
451 * For further comments, refer to handle_constraints().
453 static void constraints(ir_node *bl, void *data)
455 be_chordal_alloc_env_t *env = (be_chordal_alloc_env_t*)data;
458 for (irn = sched_first(bl); !sched_is_end(irn);) {
459 irn = handle_constraints(env, irn);
463 static void assign(ir_node *block, void *env_ptr)
465 be_chordal_alloc_env_t *alloc_env = (be_chordal_alloc_env_t*)env_ptr;
466 be_chordal_env_t *env = alloc_env->chordal_env;
467 bitset_t *live = alloc_env->live;
468 bitset_t *colors = alloc_env->colors;
469 bitset_t *in_colors = alloc_env->in_colors;
470 struct list_head *head = get_block_border_head(env, block);
471 be_lv_t *lv = be_get_irg_liveness(env->irg);
477 bitset_clear_all(colors);
478 bitset_clear_all(live);
479 bitset_clear_all(in_colors);
481 DBG((dbg, LEVEL_4, "Assigning colors for block %+F\n", block));
482 DBG((dbg, LEVEL_4, "\tusedef chain for block\n"));
483 list_for_each_entry(border_t, b, head, list) {
484 DBG((dbg, LEVEL_4, "\t%s %+F/%d\n", b->is_def ? "def" : "use",
485 b->irn, get_irn_idx(b->irn)));
489 * Add initial defs for all values live in.
490 * Since their colors have already been assigned (The dominators were
491 * allocated before), we have to mark their colors as used also.
493 be_lv_foreach(lv, block, be_lv_state_in, idx) {
494 irn = be_lv_get_irn(lv, block, idx);
495 if (has_reg_class(env, irn)) {
496 const arch_register_t *reg = arch_get_irn_register(irn);
499 assert(reg && "Node must have been assigned a register");
500 col = arch_register_get_index(reg);
502 DBG((dbg, LEVEL_4, "%+F has reg %s\n", irn, reg->name));
504 /* Mark the color of the live in value as used. */
505 bitset_set(colors, col);
506 bitset_set(in_colors, col);
508 /* Mark the value live in. */
509 bitset_set(live, get_irn_idx(irn));
514 * Mind that the sequence of defs from back to front defines a perfect
515 * elimination order. So, coloring the definitions from first to last
518 list_for_each_entry_reverse(border_t, b, head, list) {
519 ir_node *irn = b->irn;
520 int nr = get_irn_idx(irn);
521 int ignore = arch_irn_is_ignore(irn);
524 * Assign a color, if it is a local def. Global defs already have a
527 if (b->is_def && !be_is_live_in(lv, block, irn)) {
528 const arch_register_t *reg;
531 if (ignore || pset_find_ptr(alloc_env->pre_colored, irn)) {
532 reg = arch_get_irn_register(irn);
534 assert(!bitset_is_set(colors, col) && "pre-colored register must be free");
536 col = get_next_free_reg(alloc_env, colors);
537 reg = arch_register_for_index(env->cls, col);
538 assert(arch_get_irn_register(irn) == NULL && "This node must not have been assigned a register yet");
541 bitset_set(colors, col);
542 arch_set_irn_register(irn, reg);
544 DBG((dbg, LEVEL_1, "\tassigning register %s(%d) to %+F\n", arch_register_get_name(reg), col, irn));
546 assert(!bitset_is_set(live, nr) && "Value's definition must not have been encountered");
547 bitset_set(live, nr);
548 } else if (!b->is_def) {
549 /* Clear the color upon a use. */
550 const arch_register_t *reg = arch_get_irn_register(irn);
553 assert(reg && "Register must have been assigned");
555 col = arch_register_get_index(reg);
557 bitset_clear(colors, col);
558 bitset_clear(live, nr);
563 void be_ra_chordal_color(be_chordal_env_t *chordal_env)
565 be_chordal_alloc_env_t env;
567 const arch_register_class_t *cls = chordal_env->cls;
569 int colors_n = arch_register_class_n_regs(cls);
570 ir_graph *irg = chordal_env->irg;
572 be_assure_live_sets(irg);
575 env.chordal_env = chordal_env;
576 env.colors_n = colors_n;
577 env.colors = bitset_alloca(colors_n);
578 env.tmp_colors = bitset_alloca(colors_n);
579 env.in_colors = bitset_alloca(colors_n);
580 env.pre_colored = pset_new_ptr_default();
582 be_timer_push(T_SPLIT);
584 /* Add split points before operations that have pairing constraints */
585 dom_tree_walk_irg(irg, split, NULL, &env);
587 if (chordal_env->opts->dump_flags & BE_CH_DUMP_SPLIT) {
588 snprintf(buf, sizeof(buf), "%s-split", chordal_env->cls->name);
589 dump_ir_graph(chordal_env->irg, buf);
592 be_timer_pop(T_SPLIT);
594 be_timer_push(T_CONSTR);
596 /* Handle register targeting constraints */
597 dom_tree_walk_irg(irg, constraints, NULL, &env);
599 if (chordal_env->opts->dump_flags & BE_CH_DUMP_CONSTR) {
600 snprintf(buf, sizeof(buf), "%s-constr", chordal_env->cls->name);
601 dump_ir_graph(chordal_env->irg, buf);
604 be_timer_pop(T_CONSTR);
606 env.live = bitset_malloc(get_irg_last_idx(chordal_env->irg));
608 /* First, determine the pressure */
609 dom_tree_walk_irg(irg, create_borders, NULL, env.chordal_env);
611 /* Assign the colors */
612 dom_tree_walk_irg(irg, assign, NULL, &env);
614 if (chordal_env->opts->dump_flags & BE_CH_DUMP_TREE_INTV) {
616 ir_snprintf(buf, sizeof(buf), "ifg_%s_%F.eps", chordal_env->cls->name, irg);
617 plotter = new_plotter_ps(buf);
618 draw_interval_tree(&draw_chordal_def_opts, chordal_env, plotter);
619 plotter_free(plotter);
622 bitset_free(env.live);
623 del_pset(env.pre_colored);
626 BE_REGISTER_MODULE_CONSTRUCTOR(be_init_chordal)
627 void be_init_chordal(void)
629 static be_ra_chordal_coloring_t coloring = {
632 FIRM_DBG_REGISTER(dbg, "firm.be.chordal");
634 be_register_chordal_coloring("default", &coloring);