beuses: Remove stale start loop test.
[libfirm] / ir / be / bechordal.c
index 78b001a..2a71e79 100644 (file)
-/**
- * Chordal register allocation.
- * @author Sebastian Hack
- * @date 8.12.2004
+/*
+ * Copyright (C) 1995-2008 University of Karlsruhe.  All right reserved.
+ *
+ * This file is part of libFirm.
+ *
+ * This file may be distributed and/or modified under the terms of the
+ * GNU General Public License version 2 as published by the Free Software
+ * Foundation and appearing in the file LICENSE.GPL included in the
+ * packaging of this file.
  *
- * Copyright (C) Universitaet Karlsruhe
- * Released under the GPL
+ * Licensees holding valid libFirm Professional Edition licenses may use
+ * this file in accordance with the libFirm Commercial License.
+ * Agreement provided with the Software.
+ *
+ * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
+ * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE.
  */
 
-#ifdef HAVE_CONFIG_H
+/**
+ * @file
+ * @brief       Chordal register allocation.
+ * @author      Sebastian Hack
+ * @date        08.12.2004
+ */
 #include "config.h"
-#endif
-
-#include <ctype.h>
-
-#include "obst.h"
-#include "pset.h"
-#include "list.h"
-#include "bitset.h"
-#include "iterator.h"
 
-#include "irmode_t.h"
-#include "irgraph_t.h"
-#include "irprintf_t.h"
-#include "irgwalk.h"
-#include "irdump.h"
-#include "irdom.h"
-#include "debug.h"
-#include "xmalloc.h"
-
-#include "beutil.h"
-#include "besched.h"
-#include "benumb_t.h"
-#include "besched_t.h"
-#include "belive_t.h"
-#include "bearch.h"
-
-#include "bechordal_t.h"
+#include "bechordal_common.h"
 #include "bechordal_draw.h"
+#include "bechordal_t.h"
+#include "beinsn_t.h"
+#include "beintlive_t.h"
+#include "beirg.h"
+#include "bemodule.h"
+#include "debug.h"
+#include "irdump.h"
 
-#define DBG_LEVEL 0 //SET_LEVEL_4
-#define NO_COLOR (-1)
-
-#undef DUMP_INTERVALS
-#undef DUMP_PRESSURE
-#undef DUMP_IFG
+#define USE_HUNGARIAN 0
 
-#if defined(DUMP_IFG) && !defined(BUILD_GRAPH)
-#error Must define BUILD_GRAPH to be able to dump it.
+#if USE_HUNGARIAN
+#include "hungarian.h"
+#else
+#include "bipartite.h"
 #endif
 
+DEBUG_ONLY(static firm_dbg_module_t *dbg = NULL;)
 
-#include "fourcc.h"
-
-/* Make a fourcc for border checking. */
-#define BORDER_FOURCC                          FOURCC('B', 'O', 'R', 'D')
-
-static firm_dbg_module_t *dbg;
-
-#ifdef BUILD_GRAPH
-
-#define IF_EDGE_HASH(e) ((e)->src)
-#define IF_NODE_HASH(n) ((n)->nnr)
-
-static int if_edge_cmp(const void *p1, const void *p2, size_t size)
+static int get_next_free_reg(bitset_t *const available)
 {
-       const if_edge_t *e1 = p1;
-       const if_edge_t *e2 = p2;
-
-       return !(e1->src == e2->src && e1->tgt == e2->tgt);
+       return bitset_next_set(available, 0);
 }
 
-static int if_node_cmp(const void *p1, const void *p2, size_t size)
+static unsigned const *get_decisive_partner_regs(be_operand_t const *const o1, size_t const n_regs)
 {
-       const if_node_t *n1 = p1;
-       const if_node_t *n2 = p2;
-
-       return n1->nnr != n2->nnr;
-}
-
-static INLINE if_edge_t *edge_init(if_edge_t *edge, int src, int tgt)
-{
-       /* Bring the smaller entry to src. */
-       if(src > tgt) {
-               edge->src = tgt;
-               edge->tgt = src;
+       be_operand_t const *const o2 = o1->partner;
+       if (!o2 || rbitset_contains(o1->regs, o2->regs, n_regs)) {
+               return o1->regs;
+       } else if (rbitset_contains(o2->regs, o1->regs, n_regs)) {
+               return o2->regs;
        } else {
-               edge->src = src;
-               edge->tgt = tgt;
+               return NULL;
        }
-
-       return edge;
-}
-
-static INLINE void add_if(const be_chordal_env_t *env, int src, int tgt)
-{
-       if_edge_t edge;
-       if_node_t node, *src_node, *tgt_node;
-       /* insert edge */
-       edge_init(&edge, src, tgt);
-       set_insert(env->edges, &edge, sizeof(edge), IF_EDGE_HASH(&edge));
-
-       /* insert nodes */
-       node.nnr = src;
-       node.neighb = pset_new_ptr(8);
-       src_node = set_insert(env->nodes, &node, sizeof(node), IF_NODE_HASH(&node));
-       node.nnr = tgt;
-       node.neighb = pset_new_ptr(8);
-       tgt_node = set_insert(env->nodes, &node, sizeof(node), IF_NODE_HASH(&node));
-
-       /* insert neighbors into nodes */
-       pset_insert_ptr(src_node->neighb, tgt_node);
-       pset_insert_ptr(tgt_node->neighb, src_node);
-}
-
-static INLINE int are_connected(const be_chordal_env_t *env, int src, int tgt)
-{
-       if_edge_t edge;
-       edge_init(&edge, src, tgt);
-       return set_find(env->edges, &edge, sizeof(edge), IF_EDGE_HASH(&edge)) != NULL;
-}
-
-int ifg_has_edge(const be_chordal_env_t *env, const if_node_t *n1, const if_node_t* n2) {
-       return are_connected(env, n1->nnr, n2->nnr);
 }
 
-#ifdef DUMP_IFG
-
-static void dump_ifg(const be_chordal_env_t *env)
+static void pair_up_operands(be_chordal_env_t const *const env, be_insn_t *const insn)
 {
-       FILE *f;
-       set *edges = env->edges;
-       ir_graph *irg = env->irg;
-       char filename[128];
-
-       ir_snprintf(filename, sizeof(filename), "ifg_%s_%F.dot", env->cls->name, irg);
-
-       if((f = fopen(filename, "wt")) != NULL) {
-               bitset_pos_t pos;
-               int n_edges = 0;
-               if_edge_t *edge;
-               bitset_t *bs = bitset_malloc(get_graph_node_count(irg));
-
-               ir_fprintf(f, "graph \"%F\" {\n", irg);
-               fprintf(f, "\tnode [shape=box,style=filled]\n");
-
-               for(edge = set_first(edges); edge; edge = set_next(edges)) {
-                       bitset_set(bs, edge->src);
-                       bitset_set(bs, edge->tgt);
-                       n_edges++;
+       /* 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;
+       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 ... */
+               be_operand_t       *smallest        = NULL;
+               int                 smallest_n_regs = n_regs + 1;
+               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, insn->irn, op->carrier))
+                               continue;
+
+                       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;
+                       }
                }
 
-               fprintf(f, "\tx [label=\"nodes: %u, edges: %d\"]\n", bitset_popcnt(bs), n_edges);
-
-               bitset_foreach(bs, pos) {
-                       int nr = (int) pos;
-                       ir_node *irn = get_irn_for_graph_nr(irg, nr);
-
-                       ir_fprintf(f, "\tn%d [label=\"%+F\"]\n", nr, irn);
-               }
+               if (smallest != NULL) {
+                       for (int i = insn->use_start; i < insn->n_ops; ++i) {
+                               if (insn->ops[i].carrier == smallest->carrier)
+                                       insn->ops[i].partner = out_op;
+                       }
 
-               for(edge = set_first(edges); edge; edge = set_next(edges)) {
-                       fprintf(f, "\tn%d -- n%d [len=5]\n", edge->src, edge->tgt);
+                       out_op->partner   = smallest;
+                       smallest->partner = out_op;
                }
-
-               fprintf(f, "}\n");
-               fclose(f);
-
-               bitset_free(bs);
        }
-
 }
 
-#endif /* DUMP_IFG */
-
-#endif /* BUILD_GRAPH */
-
-static void check_border_list(struct list_head *head)
+static bool list_contains_irn(ir_node *const *const list, size_t const n, ir_node *const irn)
 {
-  border_t *x;
-  list_for_each_entry(border_t, x, head, list) {
-    assert(x->magic == BORDER_FOURCC);
-  }
+       for (ir_node *const *i = list; i != list + n; ++i) {
+               if (*i == irn)
+                       return true;
+       }
+       return false;
 }
 
-static void check_heads(be_chordal_env_t *env)
+static void handle_constraints(be_chordal_env_t *const env, ir_node *const irn)
 {
-  pmap_entry *ent;
-  for(ent = pmap_first(env->border_heads); ent; ent = pmap_next(env->border_heads)) {
-    /* ir_printf("checking border list of block %+F\n", ent->key); */
-    check_border_list(ent->value);
-  }
-}
-
+       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 || is_Phi(irn))
+               goto end;
+
+       /* Prepare the constraint handling of this node.
+        * Perms are constructed and Copies are created for constrained values
+        * interfering with the instruction. */
+       ir_node *const perm = pre_process_constraints(env, &insn);
+
+       /* find suitable in operands to the out operands of the node. */
+       pair_up_operands(env, insn);
+
+       /* Look at the in/out operands and add each operand (and its possible partner)
+        * to a bipartite graph (left: nodes with partners, right: admissible colors). */
+       int                        n_alloc     = 0;
+       int                  const n_regs      = env->cls->n_regs;
+       ir_node            **const alloc_nodes = ALLOCAN(ir_node*, n_regs);
+       pmap                *const partners    = pmap_create();
+#if USE_HUNGARIAN
+       hungarian_problem_t *const bp          = hungarian_new(n_regs, n_regs, HUNGARIAN_MATCH_PERFECT);
+#else
+       bipartite_t         *const bp          = bipartite_new(n_regs, n_regs);
+#endif
+       for (int i = 0; i < insn->n_ops; ++i) {
+               /* If the operand has no partner or the partner has not been marked
+                * for allocation, determine the admissible registers and mark it
+                * for allocation by associating the node and its partner with the
+                * set of admissible registers via a bipartite graph. */
+               be_operand_t *const op = &insn->ops[i];
+               if (op->partner && pmap_contains(partners, op->partner->carrier))
+                       continue;
+
+               ir_node *const partner = op->partner ? op->partner->carrier : NULL;
+               pmap_insert(partners, op->carrier, partner);
+               if (partner != NULL)
+                       pmap_insert(partners, partner, op->carrier);
+
+               /* Don't insert a node twice. */
+               if (list_contains_irn(alloc_nodes, n_alloc, op->carrier))
+                       continue;
+
+               alloc_nodes[n_alloc] = op->carrier;
+
+               DBG((dbg, LEVEL_2, "\tassociating %+F and %+F\n", op->carrier, partner));
+
+               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));
+
+                       rbitset_foreach(bs, n_regs, col) {
+#if USE_HUNGARIAN
+                               hungarian_add(bp, n_alloc, col, 1);
+#else
+                               bipartite_add(bp, n_alloc, col);
+#endif
+                       }
+               } else {
+                       DBG((dbg, LEVEL_2, "\tallowed registers for %+F: none\n", op->carrier));
+               }
 
-/**
- * Add an interval border to the list of a block's list
- * of interval border.
- * @note You always have to create the use before the def.
- * @param env The environment.
- * @param head The list head to enqueue the borders.
- * @param irn The node (value) the border belongs to.
- * @param pressure The pressure at this point in time.
- * @param step A time step for the border.
- * @param is_def Is the border a use or a def.
- * @return The created border.
- */
-static INLINE border_t *border_add(be_chordal_env_t *env, struct list_head *head,
-                       ir_node *irn, unsigned step, unsigned pressure,
-                       unsigned is_def, unsigned is_real)
-{
-       border_t *b;
+               n_alloc++;
+       }
 
-       if(!is_def) {
-               border_t *def;
+       /* Put all nodes which live through the constrained instruction also to the
+        * allocation bipartite graph. They are considered unconstrained. */
+       if (perm != NULL) {
+               be_lv_t *const lv = be_get_irg_liveness(env->irg);
+               foreach_out_edge(perm, edge) {
+                       ir_node *const proj = get_edge_src_irn(edge);
+                       assert(is_Proj(proj));
 
-               b = obstack_alloc(&env->obst, sizeof(*b));
+                       if (!be_values_interfere(lv, proj, irn) || pmap_contains(partners, proj))
+                               continue;
 
-               /* also allocate the def and tie it to the use. */
-               def = obstack_alloc(&env->obst, sizeof(*def));
-    memset(def, 0, sizeof(*def));
-               b->other_end = def;
-               def->other_end = b;
+                       /* Don't insert a node twice. */
+                       if (list_contains_irn(alloc_nodes, n_alloc, proj))
+                               continue;
 
-               /*
-                * Set the link field of the irn to the def.
-                * This strongly relies on the fact, that the use is always
-                * made before the def.
-                */
-               set_irn_link(irn, def);
+                       assert(n_alloc < n_regs);
 
-               b->magic = BORDER_FOURCC;
-               def->magic = BORDER_FOURCC;
-       }
+                       alloc_nodes[n_alloc] = proj;
+                       pmap_insert(partners, proj, NULL);
 
-       /*
-        * If the def is encountered, the use was made and so was the
-        * the def node (see the code above). It was placed into the
-        * link field of the irn, so we can get it there.
-        */
-       else {
-               b = get_irn_link(irn);
+                       bitset_foreach(env->allocatable_regs, col) {
+#if USE_HUNGARIAN
+                               hungarian_add(bp, n_alloc, col, 1);
+#else
+                               bipartite_add(bp, n_alloc, col);
+#endif
+                       }
 
-               assert(b && b->magic == BORDER_FOURCC && "Illegal border encountered");
+                       n_alloc++;
+               }
        }
 
-       b->pressure = pressure;
-       b->is_def = is_def;
-       b->is_real = is_real;
-       b->irn = irn;
-       b->step = step;
-  check_heads(env);
-       list_add_tail(&b->list, head);
-  check_heads(env);
-       DBG((dbg, LEVEL_5, "\t\t%s adding %+F, step: %d\n",
-                               is_def ? "def" : "use", irn, step));
-
+       /* Compute a valid register allocation. */
+       int *const assignment = ALLOCAN(int, n_regs);
+#if USE_HUNGARIAN
+       hungarian_prepare_cost_matrix(bp, HUNGARIAN_MODE_MAXIMIZE_UTIL);
+       int const match_res = hungarian_solve(bp, assignment, NULL, 1);
+       assert(match_res == 0 && "matching failed");
+#else
+       bipartite_matching(bp, assignment);
+#endif
 
-       return b;
-}
+       /* Assign colors obtained from the matching. */
+       for (int i = 0; i < n_alloc; ++i) {
+               assert(assignment[i] >= 0 && "there must have been a register assigned (node not register pressure faithful?)");
+               arch_register_t const *const reg = arch_register_for_index(env->cls, assignment[i]);
 
-static INLINE int has_reg_class(const be_chordal_env_t *env, const ir_node *irn)
-{
-  return arch_irn_has_reg_class(env->session_env->main_env->arch_env, irn, arch_pos_make_out(0), env->cls);
-}
+               ir_node *const irn = alloc_nodes[i];
+               if (irn != NULL) {
+                       arch_set_irn_register(irn, reg);
+                       DBG((dbg, LEVEL_2, "\tsetting %+F to register %s\n", irn, reg->name));
+               }
 
-/**
- * Annotate the register pressure to the nodes and compute
- * the liveness intervals.
- * @param block The block to do it for.
- * @param env_ptr The environment.
- */
-static void pressure(ir_node *block, void *env_ptr)
-{
-/* Convenience macro for a def */
-#define border_def(irn, step, real) \
-       border_add(env, head, irn, step, pressure--, 1, real)
-
-/* Convenience macro for a use */
-#define border_use(irn, step, real) \
-       border_add(env, head, irn, step, ++pressure, 0, real)
-
-       be_chordal_env_t *env = env_ptr;
-       bitset_t *live = env->live;
-       ir_node *irn;
-
-       int i, n;
-       unsigned step = 0;
-       unsigned pressure = 0;
-       struct list_head *head;
-       pset *live_in = put_live_in(block, pset_new_ptr_default());
-       pset *live_end = put_live_end(block, pset_new_ptr_default());
-
-       DBG((dbg, LEVEL_1, "Computing pressure in block %+F\n", block));
-       bitset_clear_all(live);
-
-       /* Set up the border list in the block info */
-       head = obstack_alloc(&env->obst, sizeof(*head));
-       INIT_LIST_HEAD(head);
-  assert(pmap_get(env->border_heads, block) == NULL);
-       pmap_insert(env->border_heads, block, head);
-
-       /*
-        * Make final uses of all values live out of the block.
-        * They are necessary to build up real intervals.
-        */
-       for(irn = pset_first(live_end); irn; irn = pset_next(live_end)) {
-               DBG((dbg, LEVEL_3, "\tMaking live: %+F/%d\n", irn, get_irn_graph_nr(irn)));
-               bitset_set(live, get_irn_graph_nr(irn));
-               if(has_reg_class(env, irn))
-                       border_use(irn, step, 0);
-       }
-       ++step;
-
-       /*
-        * Determine the last uses of a value inside the block, since they are
-        * relevant for the interval borders.
-        */
-       sched_foreach_reverse(block, irn) {
-               DBG((dbg, LEVEL_1, "\tinsn: %+F, pressure: %d\n", irn, pressure));
-               DBG((dbg, LEVEL_2, "\tlive: %b\n", live));
-
-           /*
-            * If the node defines some value, which can put into a
-            * register of the current class, make a border for it.
-            */
-               if(has_reg_class(env, irn)) {
-                       bitset_pos_t elm;
-                       int nr = get_irn_graph_nr(irn);
-
-                       bitset_clear(live, nr);
-                       border_def(irn, step, 1);
-
-#ifdef BUILD_GRAPH
-                       bitset_foreach(live, elm)
-                               add_if(env, nr, (int) elm);
-#endif
+               ir_node *const partner = pmap_get(ir_node, partners, alloc_nodes[i]);
+               if (partner != NULL) {
+                       arch_set_irn_register(partner, reg);
+                       DBG((dbg, LEVEL_2, "\tsetting %+F to register %s\n", partner, reg->name));
                }
+       }
 
-               /*
-                * If the node is no phi node we can examine the uses.
-                */
-               if(!is_Phi(irn)) {
-                       for(i = 0, n = get_irn_arity(irn); i < n; ++i) {
-                               ir_node *op = get_irn_n(irn, i);
+       /* Allocate the non-constrained Projs of the Perm. */
+       if (perm != NULL) {
+               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_clear(available, reg->index);
+               }
 
-                               if(has_reg_class(env, op)) {
-                                       int nr = get_irn_graph_nr(op);
+               /* Assign the not yet assigned Projs of the Perm a suitable color. */
+               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);
 
-                                       DBG((dbg, LEVEL_4, "\t\tpos: %d, use: %+F\n", i, op));
+                       DBG((dbg, LEVEL_2, "\tchecking reg of %+F: %s\n", proj, reg ? reg->name : "<none>"));
 
-                                       if(!bitset_is_set(live, nr)) {
-                                               border_use(op, step, 1);
-                                               bitset_set(live, nr);
-                                       }
-                               }
+                       if (reg == NULL) {
+                               size_t const col = get_next_free_reg(available);
+                               arch_register_t const *const new_reg = arch_register_for_index(env->cls, col);
+                               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));
                        }
                }
-               ++step;
        }
 
-       /*
-        * Add initial defs for all values live in.
-        */
-       for(irn = pset_first(live_in); irn; irn = pset_next(live_in)) {
-               if(has_reg_class(env, irn)) {
+#if USE_HUNGARIAN
+       hungarian_free(bp);
+#else
+       bipartite_free(bp);
+#endif
+       pmap_destroy(partners);
 
-                       /* Mark the value live in. */
-                       bitset_set(live, get_irn_graph_nr(irn));
+end:
+       obstack_free(&env->obst, base);
+}
 
-                       /* Add the def */
-                       border_def(irn, step, 0);
-               }
+/**
+ * Handle constraint nodes in each basic block.
+ * handle_constraints() inserts Perm nodes which perm
+ * over all values live at the constrained node right in front
+ * of the constrained node. These Perms signal a constrained node.
+ * For further comments, refer to handle_constraints().
+ */
+static void constraints(ir_node *const bl, void *const 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);
+               irn = next;
        }
-
-  check_heads(env);
-
-  del_pset(live_in);
-  del_pset(live_end);
 }
 
-static void assign(ir_node *block, void *env_ptr)
+static void assign(ir_node *const block, void *const env_ptr)
 {
-       be_chordal_env_t *env = env_ptr;
-       bitset_t *live = env->live;
-       bitset_t *colors = env->colors;
-       bitset_t *in_colors = env->in_colors;
-  const arch_env_t *arch_env = env->session_env->main_env->arch_env;
-
-       const ir_node *irn;
-       border_t *b;
-       struct list_head *head = get_block_border_head(env, block);
-       pset *live_in = put_live_in(block, pset_new_ptr_default());
-
-  check_heads(env);
-
-
-       bitset_clear_all(live);
-       bitset_clear_all(colors);
-       bitset_clear_all(in_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"));
-       list_for_each_entry(border_t, b, head, list) {
+       foreach_border_head(head, b) {
                DBG((dbg, LEVEL_4, "\t%s %+F/%d\n", b->is_def ? "def" : "use",
-                                       b->irn, get_irn_graph_nr(b->irn)));
+                                       b->irn, get_irn_idx(b->irn)));
        }
 
-       /*
-        * 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.
-        */
-       for(irn = pset_first(live_in); irn; irn = pset_next(live_in)) {
-               if(has_reg_class(env, irn)) {
-      const arch_register_t *reg = arch_get_irn_register(arch_env, irn, 0);
-      int col;
-
-      assert(reg && "Node must have been assigned a register");
-                       col = arch_register_get_index(reg);
-
-                       /* Mark the color of the live in value as used. */
-                       bitset_set(colors, col);
-                       bitset_set(in_colors, col);
-
-                       /* Mark the value live in. */
-                       bitset_set(live, get_irn_graph_nr(irn));
-               }
-       }
+       bitset_t *const available = bitset_alloca(env->allocatable_regs->size);
+       bitset_copy(available, env->allocatable_regs);
 
-       /*
-        * Mind that the sequence of defs from back to front defines a perfect
-        * elimination order. So, coloring the definitions from first to last
-        * will work.
-        */
-       list_for_each_entry_reverse(border_t, b, head, list) {
-               ir_node *irn = b->irn;
-               int nr = get_irn_graph_nr(irn);
-
-               /*
-                * Assign a color, if it is a local def. Global defs already have a
-                * color.
-                */
-               if(b->is_def && !is_live_in(block, irn)) {
-      const arch_register_t *reg;
-                       int col = NO_COLOR;
-
-                       DBG((dbg, LEVEL_4, "\tcolors in use: %b\n", colors));
-
-      col = bitset_next_clear(colors, 0);
-      reg = arch_register_for_index(env->cls, col);
-
-      assert(arch_get_irn_register(arch_env, irn, 0) == NULL
-          && "This node must not have been assigned a register yet");
-                       assert(!bitset_is_set(live, nr) && "Value's definition must not have been encountered");
-
-                       bitset_set(colors, col);
-                       bitset_set(live, nr);
-
-                       arch_set_irn_register(arch_env, irn, 0, reg);
-                       DBG((dbg, LEVEL_1, "\tassigning register %s(%d) to %+F\n",
-            arch_register_get_name(reg), col, irn));
-               }
+       /* 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_cls(lv, block, be_lv_state_in, env->cls, irn) {
+               arch_register_t const *const reg = arch_get_irn_register(irn);
 
-               /* Clear the color upon a use. */
-               else if(!b->is_def) {
-      const arch_register_t *reg = arch_get_irn_register(arch_env, irn, 0);
-                       int col;
+               assert(reg && "Node must have been assigned a register");
+               DBG((dbg, LEVEL_4, "%+F has reg %s\n", irn, reg->name));
 
-      assert(reg && "Register must have been assigned");
+               /* Mark the color of the live in value as used. */
+               bitset_clear(available, reg->index);
+       }
 
-      col = arch_register_get_index(reg);
-                       assert(bitset_is_set(live, nr) && "Cannot have a non live use");
+       /* Mind that the sequence of defs from back to front defines a perfect
+        * elimination order. So, coloring the definitions from first to last
+        * will work. */
+       foreach_border_head(head, b) {
+               ir_node *const irn = b->irn;
+
+               /* Assign a color, if it is a local def. Global defs already have a
+                * color. */
+               if (!b->is_def) {
+                       /* 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_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(available, col) && "pre-colored register must be free");
+                       } else {
+                               assert(!arch_irn_is_ignore(irn));
+                               col = get_next_free_reg(available);
+                               reg = arch_register_for_index(env->cls, col);
+                               arch_set_irn_register(irn, reg);
+                       }
+                       bitset_clear(available, col);
 
-                       bitset_clear(colors, col);
-                       bitset_clear(live, nr);
+                       DBG((dbg, LEVEL_1, "\tassigning register %s(%d) to %+F\n", reg->name, col, irn));
                }
        }
-
-  del_pset(live_in);
-}
-
-void be_ra_chordal_init(void)
-{
-       dbg = firm_dbg_register(DBG_CHORDAL);
-       firm_dbg_set_mask(dbg, DBG_LEVEL);
 }
 
-be_chordal_env_t *be_ra_chordal(
-    const be_main_session_env_t *session,
-    const arch_register_class_t *cls)
+static void be_ra_chordal_color(be_chordal_env_t *const chordal_env)
 {
-  ir_graph *irg = session->irg;
-       int node_count = get_graph_node_count(irg);
-       int colors_n = arch_register_class_n_regs(cls);
-       be_chordal_env_t *env = malloc(sizeof(*env));
+       char            buf[256];
+       ir_graph *const irg = chordal_env->irg;
+       be_assure_live_sets(irg);
+       assure_doms(irg);
 
-       if(get_irg_dom_state(irg) != dom_consistent)
-               compute_doms(irg);
+       be_timer_push(T_CONSTR);
 
-       obstack_init(&env->obst);
+       /* Handle register targeting constraints */
+       dom_tree_walk_irg(irg, constraints, NULL, chordal_env);
 
-#ifdef BUILD_GRAPH
-       env->edges = new_set(if_edge_cmp, node_count);
-       env->nodes = new_set(if_node_cmp, node_count);
-#endif
+       if (chordal_env->opts->dump_flags & BE_CH_DUMP_CONSTR) {
+               snprintf(buf, sizeof(buf), "%s-constr", chordal_env->cls->name);
+               dump_ir_graph(irg, buf);
+       }
 
-  env->session_env = session;
-       env->live = bitset_obstack_alloc(&env->obst, node_count);
-       env->colors = bitset_obstack_alloc(&env->obst, colors_n);
-       env->in_colors = bitset_obstack_alloc(&env->obst, colors_n);
-       env->colors_n = colors_n;
-       env->cls = cls;
-       env->border_heads = pmap_create();
+       be_timer_pop(T_CONSTR);
 
        /* First, determine the pressure */
-       dom_tree_walk_irg(irg, pressure, NULL, env);
-
-       /* Insert probable spills */
-       be_ra_chordal_spill(env);
+       dom_tree_walk_irg(irg, create_borders, NULL, chordal_env);
 
        /* Assign the colors */
-       dom_tree_walk_irg(irg, assign, NULL, env);
-
-#ifdef DUMP_IFG
-       dump_ifg(env);
-#endif
-
-#ifdef DUMP_INTERVALS
-       {
-               char buf[128];
-       plotter_t *plotter;
-
-               ir_snprintf(buf, sizeof(buf), "ifg_%s_%F.eps", cls->name, irg);
-       plotter = new_plotter_ps(buf);
+       dom_tree_walk_irg(irg, assign, NULL, chordal_env);
 
-       draw_interval_tree(&draw_chordal_def_opts, env, plotter, arch_env, cls);
-       plotter_free(plotter);
+       if (chordal_env->opts->dump_flags & BE_CH_DUMP_TREE_INTV) {
+               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);
        }
-#endif
-       return env;
 }
 
-void be_ra_chordal_check(be_chordal_env_t *chordal_env) {
-       const arch_env_t *arch_env = chordal_env->session_env->main_env->arch_env;
-       struct obstack ob;
-       pmap_entry *pme;
-       ir_node **nodes, *n1, *n2;
-       int i, o;
-
-       /* Collect all irns */
-       obstack_init(&ob);
-       pmap_foreach(chordal_env->border_heads, pme) {
-               border_t *curr;
-               struct list_head *head = pme->value;
-               list_for_each_entry(border_t, curr, head, list)
-                       if (curr->is_def && curr->is_real)
-                               if (arch_get_irn_reg_class(arch_env, curr->irn, arch_pos_make_out(0)) == chordal_env->cls)
-                                       obstack_ptr_grow(&ob, curr->irn);
-       }
-       obstack_ptr_grow(&ob, NULL);
-       nodes = (ir_node **) obstack_finish(&ob);
-
-       /* Check them */
-       for (i = 0, n1 = nodes[i]; n1; n1 = nodes[++i]) {
-               const arch_register_t *n1_reg, *n2_reg;
-
-               n1_reg = arch_get_irn_register(arch_env, n1, 0);
-               if (!arch_reg_is_allocatable(arch_env, n1, arch_pos_make_out(0), n1_reg)) {
-                       DBG((dbg, 0, "Register assigned to %+F is not allowed\n", n1));
-                       assert(0 && "Register constraint does not hold");
-               }
-               for (o = i+1, n2 = nodes[o]; n2; n2 = nodes[++o]) {
-                       n2_reg = arch_get_irn_register(arch_env, n2, 0);
-                       if (nodes_interfere(chordal_env, n1, n2) && n1_reg == n2_reg) {
-                               DBG((dbg, 0, "Values %+F and %+F interfere and have the same regiseter assigned\n", n1, n2));
-                               assert(0 && "Interfering values have the same color!");
-                       }
-               }
-       }
-       obstack_free(&ob, NULL);
-}
-
-/* BETTER #ifdef BUILD_GRAPH --> faster version of checker with edges */
-
-void be_ra_chordal_done(be_chordal_env_t *env)
+BE_REGISTER_MODULE_CONSTRUCTOR(be_init_chordal)
+void be_init_chordal(void)
 {
-#ifdef BUILD_GRAPH
-       {
-               if_node_t *ifn;
-               for(ifn = set_first(env->nodes); ifn; ifn = set_next(env->nodes))
-                       free(ifn->neighb);
-               free(env->nodes);
-               free(env->edges);
-       }
-#endif
+       static be_ra_chordal_coloring_t coloring = {
+               be_ra_chordal_color
+       };
+       FIRM_DBG_REGISTER(dbg, "firm.be.chordal");
 
-  pmap_destroy(env->border_heads);
-       obstack_free(&env->obst, NULL);
-       free(env);
+       be_register_chordal_coloring("default", &coloring);
 }
-
-
-
-int nodes_interfere(const be_chordal_env_t *env, const ir_node *a, const ir_node *b)
-{
-#ifdef BUILD_GRAPH
-       return are_connected(env, get_irn_graph_nr(a), get_irn_graph_nr(b));
-#else
-       return values_interfere(a, b);
-#endif /* BUILD_GRAPH */
-}
-
-#ifdef BUILD_GRAPH
-
-set *be_ra_get_ifg_edges(const be_chordal_env_t *env) {
-       return env->edges;
-}
-
-set *be_ra_get_ifg_nodes(const be_chordal_env_t *env) {
-       return env->nodes;
-}
-
-#endif