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"
59 #include "bestatevent.h"
61 #include "beintlive_t.h"
63 #include "bechordal_t.h"
64 #include "bechordal_draw.h"
67 #include "bechordal_common.h"
69 DEBUG_ONLY(static firm_dbg_module_t *dbg = NULL;)
71 #define DUMP_INTERVALS
73 typedef struct be_chordal_alloc_env_t {
74 be_chordal_env_t *chordal_env;
76 pset *pre_colored; /**< Set of precolored nodes. */
77 bitset_t *live; /**< A liveness bitset. */
78 bitset_t *tmp_colors; /**< An auxiliary bitset which is as long as the number of colors in the class. */
79 bitset_t *colors; /**< The color mask. */
80 bitset_t *in_colors; /**< Colors used by live in values. */
81 int colors_n; /**< The number of colors. */
82 } be_chordal_alloc_env_t;
84 static int get_next_free_reg(const be_chordal_alloc_env_t *alloc_env, bitset_t *colors)
86 bitset_t *tmp = alloc_env->tmp_colors;
87 bitset_copy(tmp, colors);
89 bitset_and(tmp, alloc_env->chordal_env->allocatable_regs);
90 return bitset_next_set(tmp, 0);
93 static bitset_t *get_decisive_partner_regs(bitset_t *bs, const be_operand_t *o1, const be_operand_t *o2)
98 bitset_copy(bs, o2->regs);
103 bitset_copy(bs, o1->regs);
107 assert(o1->req->cls == o2->req->cls || ! o1->req->cls || ! o2->req->cls);
109 if (bitset_contains(o1->regs, o2->regs)) {
110 bitset_copy(bs, o1->regs);
111 } else if (bitset_contains(o2->regs, o1->regs)) {
112 bitset_copy(bs, o2->regs);
120 static void pair_up_operands(const be_chordal_alloc_env_t *alloc_env, be_insn_t *insn)
122 const be_chordal_env_t *env = alloc_env->chordal_env;
123 bitset_t *bs = bitset_alloca(env->cls->n_regs);
128 * For each out operand, try to find an in operand which can be assigned the
129 * same register as the out operand.
131 for (j = 0; j < insn->use_start; ++j) {
132 be_operand_t *smallest = NULL;
133 int smallest_n_regs = env->cls->n_regs + 1;
134 be_operand_t *out_op = &insn->ops[j];
136 /* Try to find an in operand which has ... */
137 for (i = insn->use_start; i < insn->n_ops; ++i) {
139 be_operand_t *op = &insn->ops[i];
142 if (op->partner != NULL)
144 lv = be_get_irg_liveness(env->irg);
145 if (be_values_interfere(lv, op->irn, op->carrier))
148 bitset_copy(bs, op->regs);
149 bitset_and(bs, out_op->regs);
150 n_total = bitset_popcount(op->regs);
152 if (!bitset_is_empty(bs) && n_total < smallest_n_regs) {
154 smallest_n_regs = n_total;
158 if (smallest != NULL) {
159 for (i = insn->use_start; i < insn->n_ops; ++i) {
160 if (insn->ops[i].carrier == smallest->carrier)
161 insn->ops[i].partner = out_op;
164 out_op->partner = smallest;
165 smallest->partner = out_op;
170 static ir_node *handle_constraints(be_chordal_alloc_env_t *alloc_env,
175 ir_node **alloc_nodes;
176 //hungarian_problem_t *bp;
181 const ir_edge_t *edge;
182 ir_node *perm = NULL;
183 //int match_res, cost;
184 be_chordal_env_t *env = alloc_env->chordal_env;
185 void *base = obstack_base(env->obst);
186 be_insn_t *insn = chordal_scan_insn(env, irn);
187 ir_node *res = insn->next_insn;
190 if (insn->pre_colored) {
192 for (i = 0; i < insn->use_start; ++i)
193 pset_insert_ptr(alloc_env->pre_colored, insn->ops[i].carrier);
197 * Perms inserted before the constraint handling phase are considered to be
198 * correctly precolored. These Perms arise during the ABI handling phase.
200 if (!insn->has_constraints)
203 n_regs = env->cls->n_regs;
204 bs = bitset_alloca(n_regs);
205 alloc_nodes = ALLOCAN(ir_node*, n_regs);
206 //bp = hungarian_new(n_regs, n_regs, 2, HUNGARIAN_MATCH_PERFECT);
207 bp = bipartite_new(n_regs, n_regs);
208 assignment = ALLOCAN(int, n_regs);
209 partners = pmap_create();
212 * prepare the constraint handling of this node.
213 * Perms are constructed and Copies are created for constrained values
214 * interfering with the instruction.
216 perm = pre_process_constraints(alloc_env->chordal_env, &insn);
218 /* find suitable in operands to the out operands of the node. */
219 pair_up_operands(alloc_env, insn);
222 * look at the in/out operands and add each operand (and its possible partner)
223 * to a bipartite graph (left: nodes with partners, right: admissible colors).
225 for (i = 0, n_alloc = 0; i < insn->n_ops; ++i) {
226 be_operand_t *op = &insn->ops[i];
229 * If the operand has no partner or the partner has not been marked
230 * for allocation, determine the admissible registers and mark it
231 * for allocation by associating the node and its partner with the
232 * set of admissible registers via a bipartite graph.
234 if (!op->partner || !pmap_contains(partners, op->partner->carrier)) {
235 ir_node *partner = op->partner ? op->partner->carrier : NULL;
238 pmap_insert(partners, op->carrier, partner);
240 pmap_insert(partners, partner, op->carrier);
242 /* don't insert a node twice */
243 for (i = 0; i < n_alloc; ++i) {
244 if (alloc_nodes[i] == op->carrier) {
251 alloc_nodes[n_alloc] = op->carrier;
253 DBG((dbg, LEVEL_2, "\tassociating %+F and %+F\n", op->carrier,
256 bitset_clear_all(bs);
257 get_decisive_partner_regs(bs, op, op->partner);
259 DBG((dbg, LEVEL_2, "\tallowed registers for %+F: %B\n", op->carrier,
262 bitset_foreach(bs, col) {
263 //hungarian_add(bp, n_alloc, col, 1);
264 bipartite_add(bp, n_alloc, col);
272 * Put all nodes which live through the constrained instruction also to the
273 * allocation bipartite graph. They are considered unconstrained.
276 foreach_out_edge(perm, edge) {
278 ir_node *proj = get_edge_src_irn(edge);
279 be_lv_t *lv = be_get_irg_liveness(env->irg);
281 assert(is_Proj(proj));
283 if (!be_values_interfere(lv, proj, irn)
284 || pmap_contains(partners, proj))
287 /* don't insert a node twice */
288 for (i = 0; i < n_alloc; ++i) {
289 if (alloc_nodes[i] == proj) {
297 assert(n_alloc < n_regs);
299 alloc_nodes[n_alloc] = proj;
300 pmap_insert(partners, proj, NULL);
302 bitset_foreach(env->allocatable_regs, col) {
303 //hungarian_add(bp, n_alloc, col, 1);
304 bipartite_add(bp, n_alloc, col);
311 /* Compute a valid register allocation. */
313 hungarian_prepare_cost_matrix(bp, HUNGARIAN_MODE_MAXIMIZE_UTIL);
314 match_res = hungarian_solve(bp, assignment, &cost, 1);
315 assert(match_res == 0 && "matching failed");
317 /*bipartite_dump_f(stderr, bp);*/
318 bipartite_matching(bp, assignment);
321 /* Assign colors obtained from the matching. */
322 for (i = 0; i < n_alloc; ++i) {
323 const arch_register_t *reg;
326 assert(assignment[i] >= 0 && "there must have been a register assigned (node not register pressure faithful?)");
327 reg = arch_register_for_index(env->cls, assignment[i]);
329 irn = alloc_nodes[i];
331 arch_set_irn_register(irn, reg);
332 (void) pset_hinsert_ptr(alloc_env->pre_colored, irn);
333 DBG((dbg, LEVEL_2, "\tsetting %+F to register %s\n", irn, reg->name));
336 irn = (ir_node*)pmap_get(partners, alloc_nodes[i]);
338 arch_set_irn_register(irn, reg);
339 (void) pset_hinsert_ptr(alloc_env->pre_colored, irn);
340 DBG((dbg, LEVEL_2, "\tsetting %+F to register %s\n", irn, reg->name));
344 /* Allocate the non-constrained Projs of the Perm. */
346 bitset_clear_all(bs);
348 /* Put the colors of all Projs in a bitset. */
349 foreach_out_edge(perm, edge) {
350 ir_node *proj = get_edge_src_irn(edge);
351 const arch_register_t *reg = arch_get_irn_register(proj);
354 bitset_set(bs, reg->index);
357 /* Assign the not yet assigned Projs of the Perm a suitable color. */
358 foreach_out_edge(perm, edge) {
359 ir_node *proj = get_edge_src_irn(edge);
360 const arch_register_t *reg = arch_get_irn_register(proj);
362 DBG((dbg, LEVEL_2, "\tchecking reg of %+F: %s\n", proj, reg ? reg->name : "<none>"));
365 col = get_next_free_reg(alloc_env, bs);
366 reg = arch_register_for_index(env->cls, col);
367 bitset_set(bs, reg->index);
368 arch_set_irn_register(proj, reg);
369 pset_insert_ptr(alloc_env->pre_colored, proj);
370 DBG((dbg, LEVEL_2, "\tsetting %+F to register %s\n", proj, reg->name));
376 //hungarian_free(bp);
377 pmap_destroy(partners);
380 obstack_free(env->obst, base);
385 * Handle constraint nodes in each basic block.
386 * handle_constraints() inserts Perm nodes which perm
387 * over all values live at the constrained node right in front
388 * of the constrained node. These Perms signal a constrained node.
389 * For further comments, refer to handle_constraints().
391 static void constraints(ir_node *bl, void *data)
393 be_chordal_alloc_env_t *env = (be_chordal_alloc_env_t*)data;
396 for (irn = sched_first(bl); !sched_is_end(irn);) {
397 irn = handle_constraints(env, irn);
401 static void assign(ir_node *block, void *env_ptr)
403 be_chordal_alloc_env_t *alloc_env = (be_chordal_alloc_env_t*)env_ptr;
404 be_chordal_env_t *env = alloc_env->chordal_env;
405 bitset_t *live = alloc_env->live;
406 bitset_t *colors = alloc_env->colors;
407 bitset_t *in_colors = alloc_env->in_colors;
408 struct list_head *head = get_block_border_head(env, block);
409 be_lv_t *lv = be_get_irg_liveness(env->irg);
415 bitset_clear_all(colors);
416 bitset_clear_all(live);
417 bitset_clear_all(in_colors);
419 DBG((dbg, LEVEL_4, "Assigning colors for block %+F\n", block));
420 DBG((dbg, LEVEL_4, "\tusedef chain for block\n"));
421 list_for_each_entry(border_t, b, head, list) {
422 DBG((dbg, LEVEL_4, "\t%s %+F/%d\n", b->is_def ? "def" : "use",
423 b->irn, get_irn_idx(b->irn)));
427 * Add initial defs for all values live in.
428 * Since their colors have already been assigned (The dominators were
429 * allocated before), we have to mark their colors as used also.
431 be_lv_foreach(lv, block, be_lv_state_in, idx) {
432 irn = be_lv_get_irn(lv, block, idx);
433 if (has_reg_class(env, irn)) {
434 const arch_register_t *reg = arch_get_irn_register(irn);
437 assert(reg && "Node must have been assigned a register");
438 col = arch_register_get_index(reg);
440 DBG((dbg, LEVEL_4, "%+F has reg %s\n", irn, reg->name));
442 /* Mark the color of the live in value as used. */
443 bitset_set(colors, col);
444 bitset_set(in_colors, col);
446 /* Mark the value live in. */
447 bitset_set(live, get_irn_idx(irn));
452 * Mind that the sequence of defs from back to front defines a perfect
453 * elimination order. So, coloring the definitions from first to last
456 list_for_each_entry_reverse(border_t, b, head, list) {
457 ir_node *irn = b->irn;
458 int nr = get_irn_idx(irn);
459 int ignore = arch_irn_is_ignore(irn);
462 * Assign a color, if it is a local def. Global defs already have a
465 if (b->is_def && !be_is_live_in(lv, block, irn)) {
466 const arch_register_t *reg;
469 if (ignore || pset_find_ptr(alloc_env->pre_colored, irn)) {
470 reg = arch_get_irn_register(irn);
472 assert(!bitset_is_set(colors, col) && "pre-colored register must be free");
474 col = get_next_free_reg(alloc_env, colors);
475 reg = arch_register_for_index(env->cls, col);
476 assert(arch_get_irn_register(irn) == NULL && "This node must not have been assigned a register yet");
479 bitset_set(colors, col);
480 arch_set_irn_register(irn, reg);
482 DBG((dbg, LEVEL_1, "\tassigning register %s(%d) to %+F\n", arch_register_get_name(reg), col, irn));
484 assert(!bitset_is_set(live, nr) && "Value's definition must not have been encountered");
485 bitset_set(live, nr);
486 } else if (!b->is_def) {
487 /* Clear the color upon a use. */
488 const arch_register_t *reg = arch_get_irn_register(irn);
491 assert(reg && "Register must have been assigned");
493 col = arch_register_get_index(reg);
495 bitset_clear(colors, col);
496 bitset_clear(live, nr);
501 void be_ra_chordal_color(be_chordal_env_t *chordal_env)
503 be_chordal_alloc_env_t env;
506 const arch_register_class_t *cls = chordal_env->cls;
508 int colors_n = arch_register_class_n_regs(cls);
509 ir_graph *irg = chordal_env->irg;
511 lv = be_assure_liveness(irg);
512 be_liveness_assure_sets(lv);
513 be_liveness_assure_chk(lv);
517 env.chordal_env = chordal_env;
518 env.colors_n = colors_n;
519 env.colors = bitset_alloca(colors_n);
520 env.tmp_colors = bitset_alloca(colors_n);
521 env.in_colors = bitset_alloca(colors_n);
522 env.pre_colored = pset_new_ptr_default();
524 be_timer_push(T_CONSTR);
526 /* Handle register targeting constraints */
527 dom_tree_walk_irg(irg, constraints, NULL, &env);
529 if (chordal_env->opts->dump_flags & BE_CH_DUMP_CONSTR) {
530 snprintf(buf, sizeof(buf), "%s-constr", chordal_env->cls->name);
531 dump_ir_graph(chordal_env->irg, buf);
534 be_timer_pop(T_CONSTR);
536 env.live = bitset_malloc(get_irg_last_idx(chordal_env->irg));
538 /* First, determine the pressure */
539 dom_tree_walk_irg(irg, create_borders, NULL, env.chordal_env);
541 /* Assign the colors */
542 dom_tree_walk_irg(irg, assign, NULL, &env);
544 if (chordal_env->opts->dump_flags & BE_CH_DUMP_TREE_INTV) {
546 ir_snprintf(buf, sizeof(buf), "ifg_%s_%F.eps", chordal_env->cls->name, irg);
547 plotter = new_plotter_ps(buf);
548 draw_interval_tree(&draw_chordal_def_opts, chordal_env, plotter);
549 plotter_free(plotter);
552 bitset_free(env.live);
553 del_pset(env.pre_colored);
556 BE_REGISTER_MODULE_CONSTRUCTOR(be_init_chordal)
557 void be_init_chordal(void)
559 static be_ra_chordal_coloring_t coloring = {
562 FIRM_DBG_REGISTER(dbg, "firm.be.chordal");
564 be_register_chordal_coloring("default", &coloring);