beuses: Remove stale start loop test.
[libfirm] / ir / be / bechordal.c
index 12fb17b..2a71e79 100644 (file)
 
 DEBUG_ONLY(static firm_dbg_module_t *dbg = NULL;)
 
-typedef struct be_chordal_alloc_env_t {
-       be_chordal_env_t *chordal_env;
-       bitset_t         *tmp_colors;  /**< An auxiliary bitset which is as long as the number of colors in the class. */
-       bitset_t         *colors;      /**< The color mask. */
-} be_chordal_alloc_env_t;
-
-static int get_next_free_reg(be_chordal_alloc_env_t const *const alloc_env, bitset_t *const colors)
+static int get_next_free_reg(bitset_t *const available)
 {
-       bitset_t *tmp = alloc_env->tmp_colors;
-       bitset_copy(tmp, colors);
-       bitset_flip_all(tmp);
-       bitset_and(tmp, alloc_env->chordal_env->allocatable_regs);
-       return bitset_next_set(tmp, 0);
+       return bitset_next_set(available, 0);
 }
 
-static bitset_t const *get_decisive_partner_regs(be_operand_t const *const o1)
+static unsigned const *get_decisive_partner_regs(be_operand_t const *const o1, size_t const n_regs)
 {
        be_operand_t const *const o2 = o1->partner;
-       assert(!o2 || o1->req->cls == o2->req->cls);
-
-       if (!o2 || bitset_contains(o1->regs, o2->regs)) {
+       if (!o2 || rbitset_contains(o1->regs, o2->regs, n_regs)) {
                return o1->regs;
-       } else if (bitset_contains(o2->regs, o1->regs)) {
+       } else if (rbitset_contains(o2->regs, o1->regs, n_regs)) {
                return o2->regs;
        } else {
                return NULL;
@@ -79,7 +67,7 @@ static void pair_up_operands(be_chordal_env_t const *const env, be_insn_t *const
        /* For each out operand, try to find an in operand which can be assigned the
         * same register as the out operand. */
        int       const n_regs = env->cls->n_regs;
-       bitset_t *const bs     = bitset_alloca(n_regs);
+       unsigned *const bs     = rbitset_alloca(n_regs);
        be_lv_t  *const lv     = be_get_irg_liveness(env->irg);
        for (int j = 0; j < insn->use_start; ++j) {
                /* Try to find an in operand which has ... */
@@ -88,13 +76,13 @@ static void pair_up_operands(be_chordal_env_t const *const env, be_insn_t *const
                be_operand_t *const out_op          = &insn->ops[j];
                for (int i = insn->use_start; i < insn->n_ops; ++i) {
                        be_operand_t *const op = &insn->ops[i];
-                       if (op->partner || be_values_interfere(lv, op->irn, op->carrier))
+                       if (op->partner || be_values_interfere(lv, insn->irn, op->carrier))
                                continue;
 
-                       bitset_copy(bs, op->regs);
-                       bitset_and(bs, out_op->regs);
-                       int const n_total = bitset_popcount(op->regs);
-                       if (!bitset_is_empty(bs) && n_total < smallest_n_regs) {
+                       rbitset_copy(bs, op->regs, n_regs);
+                       rbitset_and(bs, out_op->regs, n_regs);
+                       int const n_total = rbitset_popcount(op->regs, n_regs);
+                       if (!rbitset_is_empty(bs, n_regs) && n_total < smallest_n_regs) {
                                smallest        = op;
                                smallest_n_regs = n_total;
                        }
@@ -121,15 +109,14 @@ static bool list_contains_irn(ir_node *const *const list, size_t const n, ir_nod
        return false;
 }
 
-static void handle_constraints(be_chordal_alloc_env_t *const alloc_env, ir_node *const irn)
+static void handle_constraints(be_chordal_env_t *const env, ir_node *const irn)
 {
-       be_chordal_env_t *const env  = alloc_env->chordal_env;
-       void             *const base = obstack_base(env->obst);
-       be_insn_t              *insn = be_scan_insn(env, irn);
+       void *const base = obstack_base(&env->obst);
+       be_insn_t  *insn = be_scan_insn(env, irn);
 
        /* Perms inserted before the constraint handling phase are considered to be
         * correctly precolored. These Perms arise during the ABI handling phase. */
-       if (!insn->has_constraints || is_Phi(irn))
+       if (!insn || is_Phi(irn))
                goto end;
 
        /* Prepare the constraint handling of this node.
@@ -173,11 +160,11 @@ static void handle_constraints(be_chordal_alloc_env_t *const alloc_env, ir_node
 
                DBG((dbg, LEVEL_2, "\tassociating %+F and %+F\n", op->carrier, partner));
 
-               bitset_t const *const bs = get_decisive_partner_regs(op);
+               unsigned const *const bs = get_decisive_partner_regs(op, n_regs);
                if (bs) {
                        DBG((dbg, LEVEL_2, "\tallowed registers for %+F: %B\n", op->carrier, bs));
 
-                       bitset_foreach(bs, col) {
+                       rbitset_foreach(bs, n_regs, col) {
 #if USE_HUNGARIAN
                                hungarian_add(bp, n_alloc, col, 1);
 #else
@@ -253,14 +240,15 @@ static void handle_constraints(be_chordal_alloc_env_t *const alloc_env, ir_node
 
        /* Allocate the non-constrained Projs of the Perm. */
        if (perm != NULL) {
-               bitset_t *const bs = bitset_alloca(n_regs);
+               bitset_t *const available = bitset_alloca(n_regs);
+               bitset_copy(available, env->allocatable_regs);
 
                /* Put the colors of all Projs in a bitset. */
                foreach_out_edge(perm, edge) {
                        ir_node               *const proj = get_edge_src_irn(edge);
                        arch_register_t const *const reg  = arch_get_irn_register(proj);
                        if (reg != NULL)
-                               bitset_set(bs, reg->index);
+                               bitset_clear(available, reg->index);
                }
 
                /* Assign the not yet assigned Projs of the Perm a suitable color. */
@@ -271,9 +259,9 @@ static void handle_constraints(be_chordal_alloc_env_t *const alloc_env, ir_node
                        DBG((dbg, LEVEL_2, "\tchecking reg of %+F: %s\n", proj, reg ? reg->name : "<none>"));
 
                        if (reg == NULL) {
-                               size_t const col = get_next_free_reg(alloc_env, bs);
+                               size_t const col = get_next_free_reg(available);
                                arch_register_t const *const new_reg = arch_register_for_index(env->cls, col);
-                               bitset_set(bs, new_reg->index);
+                               bitset_clear(available, new_reg->index);
                                arch_set_irn_register(proj, new_reg);
                                DBG((dbg, LEVEL_2, "\tsetting %+F to register %s\n", proj, new_reg->name));
                        }
@@ -288,7 +276,7 @@ static void handle_constraints(be_chordal_alloc_env_t *const alloc_env, ir_node
        pmap_destroy(partners);
 
 end:
-       obstack_free(env->obst, base);
+       obstack_free(&env->obst, base);
 }
 
 /**
@@ -300,7 +288,7 @@ end:
  */
 static void constraints(ir_node *const bl, void *const data)
 {
-       be_chordal_alloc_env_t *const env = (be_chordal_alloc_env_t*)data;
+       be_chordal_env_t *const env = (be_chordal_env_t*)data;
        for (ir_node *irn = sched_first(bl); !sched_is_end(irn);) {
                ir_node *const next = sched_next(irn);
                handle_constraints(env, irn);
@@ -310,13 +298,9 @@ static void constraints(ir_node *const bl, void *const data)
 
 static void assign(ir_node *const block, void *const env_ptr)
 {
-       be_chordal_alloc_env_t *const alloc_env = (be_chordal_alloc_env_t*)env_ptr;
-       be_chordal_env_t       *const env       = alloc_env->chordal_env;
-       bitset_t               *const colors    = alloc_env->colors;
-       struct list_head       *const head      = get_block_border_head(env, block);
-       be_lv_t                *const lv        = be_get_irg_liveness(env->irg);
-
-       bitset_clear_all(colors);
+       be_chordal_env_t *const env  = (be_chordal_env_t*)env_ptr;
+       struct list_head *const head = get_block_border_head(env, block);
+       be_lv_t          *const lv   = be_get_irg_liveness(env->irg);
 
        DBG((dbg, LEVEL_4, "Assigning colors for block %+F\n", block));
        DBG((dbg, LEVEL_4, "\tusedef chain for block\n"));
@@ -325,19 +309,20 @@ static void assign(ir_node *const block, void *const env_ptr)
                                        b->irn, get_irn_idx(b->irn)));
        }
 
+       bitset_t *const available = bitset_alloca(env->allocatable_regs->size);
+       bitset_copy(available, env->allocatable_regs);
+
        /* Add initial defs for all values live in.
         * Since their colors have already been assigned (The dominators were
         * allocated before), we have to mark their colors as used also. */
-       be_lv_foreach(lv, block, be_lv_state_in, irn) {
-               if (arch_irn_consider_in_reg_alloc(env->cls, irn)) {
-                       arch_register_t const *const reg = arch_get_irn_register(irn);
+       be_lv_foreach_cls(lv, block, be_lv_state_in, env->cls, irn) {
+               arch_register_t const *const reg = arch_get_irn_register(irn);
 
-                       assert(reg && "Node must have been assigned a register");
-                       DBG((dbg, LEVEL_4, "%+F has reg %s\n", irn, reg->name));
+               assert(reg && "Node must have been assigned a register");
+               DBG((dbg, LEVEL_4, "%+F has reg %s\n", irn, reg->name));
 
-                       /* Mark the color of the live in value as used. */
-                       bitset_set(colors, reg->index);
-               }
+               /* Mark the color of the live in value as used. */
+               bitset_clear(available, reg->index);
        }
 
        /* Mind that the sequence of defs from back to front defines a perfect
@@ -349,23 +334,23 @@ static void assign(ir_node *const block, void *const env_ptr)
                /* Assign a color, if it is a local def. Global defs already have a
                 * color. */
                if (!b->is_def) {
-                       /* Clear the color upon a use. */
+                       /* Make the color available upon a use. */
                        arch_register_t const *const reg = arch_get_irn_register(irn);
                        assert(reg && "Register must have been assigned");
-                       bitset_clear(colors, reg->index);
+                       bitset_set(available, reg->index);
                } else if (!be_is_live_in(lv, block, irn)) {
                        int                    col;
                        arch_register_t const *reg = arch_get_irn_register(irn);
                        if (reg) {
                                col = reg->index;
-                               assert(!bitset_is_set(colors, col) && "pre-colored register must be free");
+                               assert(bitset_is_set(available, col) && "pre-colored register must be free");
                        } else {
                                assert(!arch_irn_is_ignore(irn));
-                               col = get_next_free_reg(alloc_env, colors);
+                               col = get_next_free_reg(available);
                                reg = arch_register_for_index(env->cls, col);
                                arch_set_irn_register(irn, reg);
                        }
-                       bitset_set(colors, col);
+                       bitset_clear(available, col);
 
                        DBG((dbg, LEVEL_1, "\tassigning register %s(%d) to %+F\n", reg->name, col, irn));
                }
@@ -379,27 +364,13 @@ static void be_ra_chordal_color(be_chordal_env_t *const chordal_env)
        be_assure_live_sets(irg);
        assure_doms(irg);
 
-       arch_register_class_t const *const cls      = chordal_env->cls;
-       int                          const colors_n = arch_register_class_n_regs(cls);
-       be_chordal_alloc_env_t             env;
-       env.chordal_env = chordal_env;
-       env.colors      = bitset_alloca(colors_n);
-       env.tmp_colors  = bitset_alloca(colors_n);
-
-       be_timer_push(T_SPLIT);
-       if (chordal_env->opts->dump_flags & BE_CH_DUMP_SPLIT) {
-               snprintf(buf, sizeof(buf), "%s-split", cls->name);
-               dump_ir_graph(irg, buf);
-       }
-       be_timer_pop(T_SPLIT);
-
        be_timer_push(T_CONSTR);
 
        /* Handle register targeting constraints */
-       dom_tree_walk_irg(irg, constraints, NULL, &env);
+       dom_tree_walk_irg(irg, constraints, NULL, chordal_env);
 
        if (chordal_env->opts->dump_flags & BE_CH_DUMP_CONSTR) {
-               snprintf(buf, sizeof(buf), "%s-constr", cls->name);
+               snprintf(buf, sizeof(buf), "%s-constr", chordal_env->cls->name);
                dump_ir_graph(irg, buf);
        }
 
@@ -409,10 +380,10 @@ static void be_ra_chordal_color(be_chordal_env_t *const chordal_env)
        dom_tree_walk_irg(irg, create_borders, NULL, chordal_env);
 
        /* Assign the colors */
-       dom_tree_walk_irg(irg, assign, NULL, &env);
+       dom_tree_walk_irg(irg, assign, NULL, chordal_env);
 
        if (chordal_env->opts->dump_flags & BE_CH_DUMP_TREE_INTV) {
-               ir_snprintf(buf, sizeof(buf), "ifg_%s_%F.eps", cls->name, irg);
+               ir_snprintf(buf, sizeof(buf), "ifg_%s_%F.eps", chordal_env->cls->name, irg);
                plotter_t *const plotter = new_plotter_ps(buf);
                draw_interval_tree(&draw_chordal_def_opts, chordal_env, plotter);
                plotter_free(plotter);