initial checkin
[libfirm] / ir / be / becopyheur2.c
index 0cb628d..a1ecee1 100644 (file)
@@ -11,7 +11,9 @@
 #include "list.h"
 #include "pdeq.h"
 #include "bitset.h"
+
 #include "debug.h"
+#include "bitfiddle.h"
 
 #include "irphase_t.h"
 #include "irgraph_t.h"
@@ -24,7 +26,9 @@
 #include "becopyopt_t.h"
 #include "bechordal_t.h"
 
-#define INFEASIBLE(col) ((col) > (INT_MAX - 10))
+#define INFEASIBLE(cost) ((cost) == INT_MAX)
+
+static be_ifg_dump_dot_cb_t ifg_dot_cb;
 
 typedef unsigned col_t;
 
@@ -32,11 +36,13 @@ typedef struct _co2_irn_t   co2_irn_t;
 typedef struct _co2_cloud_t co2_cloud_t;
 
 typedef struct {
-       phase_t ph;
+       phase_t     ph;
        copy_opt_t *co;
-       bitset_t *ignore_regs;
-       co2_irn_t *touched;
-       int visited;
+       bitset_t   *ignore_regs;
+       co2_irn_t  *touched;
+       int         visited;
+       int         n_regs;
+       struct list_head cloud_head;
        DEBUG_ONLY(firm_dbg_module_t *dbg;)
 } co2_t;
 
@@ -49,7 +55,8 @@ struct _co2_irn_t {
        col_t            tmp_col;
        col_t            orig_col;
        int              visited;
-       int              rank;
+       int              fixed_index;
+       int                              last_color_change;
        unsigned         fixed     : 1;
        unsigned         tmp_fixed : 1;
        struct list_head changed_list;
@@ -57,14 +64,17 @@ struct _co2_irn_t {
 };
 
 struct _co2_cloud_t {
-       int costs;
-       int inevit;
-       int best_costs;
-       int n_memb;
-       int max_degree;
-       co2_irn_t *master;
+       co2_t      *env;
+       int         costs;
+       int         inevit;
+       int         best_costs;
+       int         n_memb;
+       int         max_degree;
+       int                     ticks;
+       co2_irn_t  *master;
        co2_irn_t **seq;
        col_t      *best_cols;
+       int       **failrure_matrix;
        struct list_head members_head;
        struct list_head list;
 };
@@ -82,10 +92,10 @@ typedef struct {
 
 #define get_co2_irn(co2, irn)   ((co2_irn_t *) phase_get_or_set_irn_data(&co2->ph, irn))
 
-static void co2_irn_init(phase_t *ph, ir_node *irn, void *data)
+static void *co2_irn_init(phase_t *ph, ir_node *irn, void *data)
 {
        co2_t *env    = (co2_t *) ph;
-       co2_irn_t *ci = data;
+       co2_irn_t *ci = data ? data : phase_alloc(ph, sizeof(ci[0]));
 
        memset(ci, 0, sizeof(ci[0]));
        INIT_LIST_HEAD(&ci->changed_list);
@@ -93,8 +103,9 @@ static void co2_irn_init(phase_t *ph, ir_node *irn, void *data)
        ci->irn          = irn;
        ci->touched_next = env->touched;
        ci->orig_col     = get_irn_col(env->co, irn);
-       ci->aff          = get_affinity_info(env->co, irn);
+       ci->aff          = get_affinity_info(env->co, (ir_node *)irn);
        env->touched     = ci;
+       return ci;
 }
 
 
@@ -112,16 +123,6 @@ static int cmp_clouds(const void *a, const void *b)
        return (*q)->costs - (*p)->costs;
 }
 
-static co2_cloud_t *new_cloud(co2_t *env)
-{
-       co2_cloud_t *cloud = phase_alloc(&env->ph, sizeof(cloud[0]));
-       memset(cloud, 0, sizeof(cloud[0]));
-       INIT_LIST_HEAD(&cloud->members_head);
-       INIT_LIST_HEAD(&cloud->list);
-       cloud->best_costs = INT_MAX;
-       return cloud;
-}
-
 /**
  * An order on color/costs pairs.
  * If the costs are equal, we use the color as a kind of normalization.
@@ -133,12 +134,7 @@ static int col_cost_pair_lt(const void *a, const void *b)
        int c = p->costs;
        int d = q->costs;
 
-       if(c > d)
-               return 1;
-       if(c < d)
-               return -1;
-
-       return 0;
+       return (c > d) - (c < d);
 }
 
 const char *flag_str(unsigned int fl)
@@ -159,7 +155,7 @@ static col_t get_col(co2_t *env, ir_node *irn)
        return ci->tmp_fixed ? ci->tmp_col : ci->orig_col;
 }
 
-static INLINE color_is_fix(co2_t *env, ir_node *irn)
+static INLINE int color_is_fix(co2_t *env, ir_node *irn)
 {
        co2_irn_t *ci = get_co2_irn(env, irn);
        return ci->fixed || ci->tmp_fixed;
@@ -201,7 +197,7 @@ static void incur_constraint_costs(co2_t *env, ir_node *irn, col_cost_pair_t *co
                req.limited(req.limited_env, aux);
                n_constr = bitset_popcnt(aux);
                bitset_foreach(aux, elm) {
-                       col_costs[elm].costs += costs / n_constr;
+                       col_costs[elm].costs  = add_saturated(col_costs[elm].costs, costs / n_constr);
                        col_costs[elm].flags |= NEIGHBOR_CONSTR;
                }
        }
@@ -230,9 +226,11 @@ static void determine_color_costs(co2_t *env, co2_irn_t *ci, col_cost_pair_t *co
        void *it;
        int i;
 
+#if 0
        if(get_irn_node_nr(irn) == 2040) {
                printf("Hallo");
        }
+#endif
 
        /* Put all forbidden colors into the aux bitset. */
        admissible_colors(env, ci, forb);
@@ -250,7 +248,7 @@ static void determine_color_costs(co2_t *env, co2_irn_t *ci, col_cost_pair_t *co
                co_gs_foreach_neighb(a, n) {
                        if(color_is_fix(env, n->irn)) {
                                col_t col = get_col(env, n->irn);
-                               col_costs[col].costs -= 100 * n->costs;
+                               col_costs[col].costs = add_saturated(col_costs[col].costs, -n->costs * 128);
                        }
 
                        incur_constraint_costs(env, n->irn, col_costs, -n->costs);
@@ -266,7 +264,7 @@ static void determine_color_costs(co2_t *env, co2_irn_t *ci, col_cost_pair_t *co
                }
                else {
                        incur_constraint_costs(env, pos, col_costs, INT_MAX);
-                       col_costs[col].costs += 10 * be_ifg_degree(ifg, pos);
+                       col_costs[col].costs = add_saturated(col_costs[col].costs, 8 * be_ifg_degree(ifg, pos));
                }
        }
 
@@ -384,18 +382,18 @@ static int recolor(co2_t *env, ir_node *irn, col_cost_pair_t *col_list, struct l
                ir_node *n;
                void *it;
 
-               DBG((env->dbg, LEVEL_3, "\t\t%2Ntrying color %d(%d) on %+F\n", depth, tgt_col, costs, irn));
+               DBG((env->dbg, LEVEL_3, "\t\t%2{firm:indent}trying color %d(%d) on %+F\n", depth, tgt_col, costs, irn));
 
                /* If the costs for that color (and all successive) are infinite, bail out we won't make it anyway. */
                if(INFEASIBLE(costs)) {
-                       DB((env->dbg, LEVEL_4, "\t\t%2Ncolor %d infeasible due to %s\n", depth, tgt_col, flag_str(col_list[i].flags)));
+                       DB((env->dbg, LEVEL_4, "\t\t%2{firm:indent}color %d infeasible due to %s\n", depth, tgt_col, flag_str(col_list[i].flags)));
                        ci->tmp_fixed = 0;
                        return 0;
                }
 
                /* Set the new color of the node and mark the node as temporarily fixed. */
-               ci->tmp_col   = tgt_col;
-               ci->tmp_fixed = 1;
+               ci->tmp_col     = tgt_col;
+               ci->tmp_fixed   = 1;
 
                /*
                If that color has costs > 0, there's at least one neighbor having that color,
@@ -430,7 +428,7 @@ static int recolor(co2_t *env, ir_node *irn, col_cost_pair_t *col_list, struct l
                of the current node, every thing was ok and we can return safely.
                */
                if(neigh_ok) {
-                       DBG((env->dbg, LEVEL_3, "\t\t%2Ncolor %d(%d) was ok\n", depth, tgt_col, costs));
+                       DBG((env->dbg, LEVEL_3, "\t\t%2{firm:indent}color %d(%d) was ok\n", depth, tgt_col, costs));
                        list_splice(&changed, parent_changed);
                        res = 1;
                        break;
@@ -453,13 +451,13 @@ static int change_color_not(co2_t *env, ir_node *irn, col_t not_col, struct list
        int res       = 0;
        col_t col     = get_col(env, irn);
 
-       DBG((env->dbg, LEVEL_3, "\t\t%2Nclearing %+F(%d) of color %d\n", depth, irn, col, not_col));
+       DBG((env->dbg, LEVEL_3, "\t\t%2{firm:indent}clearing %+F(%d) of color %d\n", depth, irn, col, not_col));
 
        /* the node does not have to forbidden color. That's fine, mark it as visited and return. */
        if(col != not_col) {
                if(!ci->tmp_fixed) {
-                       ci->tmp_col   = col;
-                       ci->tmp_fixed = 1;
+                       ci->tmp_col     = col;
+                       ci->tmp_fixed   = 1;
                }
 
                list_add(&ci->changed_list, parent_changed);
@@ -494,25 +492,21 @@ static int change_color_single(co2_t *env, ir_node *irn, col_t tgt_col, struct l
        col_t col     = get_col(env, irn);
        int res       = 0;
 
-       DBG((env->dbg, LEVEL_3, "\t\t%2Ntrying to set %+F(%d) to color %d\n", depth, irn, col, tgt_col));
-
-       /* If the color is already fix, bail out. */
-       if(color_is_fix(env, irn))
-               return 0;
+       DBG((env->dbg, LEVEL_3, "\t\t%2{firm:indent}trying to set %+F(%d) to color %d\n", depth, irn, col, tgt_col));
 
        /* the node has the wanted color. That's fine, mark it as visited and return. */
        if(col == tgt_col) {
                if(!ci->tmp_fixed) {
-                       ci->tmp_col   = col;
-                       ci->tmp_fixed = 1;
+                       ci->tmp_col     = col;
+                       ci->tmp_fixed   = 1;
+                       list_add(&ci->changed_list, parent_changed);
                }
 
-               list_add(&ci->changed_list, parent_changed);
-               DB((env->dbg, LEVEL_3, "\t\tok\n"));
+               DB((env->dbg, LEVEL_3, "\t\t%2{firm:indent}ok\n", depth));
                return 1;
        }
 
-       else {
+       if(!color_is_fix(env, irn)) {
                int n_regs           = env->co->cls->n_regs;
                col_cost_pair_t *seq = alloca(n_regs * sizeof(seq[0]));
 
@@ -522,174 +516,12 @@ static int change_color_single(co2_t *env, ir_node *irn, col_t tgt_col, struct l
                /* Try recoloring the node using the color list. */
                res = recolor(env, irn, seq, parent_changed, depth);
 
-               DB((env->dbg, LEVEL_3, "\t\tcolor %d %s for %+F\n", tgt_col, res ? "was ok" : "failed", irn));
+               DB((env->dbg, LEVEL_3, "\t\t%2{firm:indent}color %d %s for %+F\n", depth, tgt_col, res ? "was ok" : "failed", irn));
        }
 
        return res;
 }
 
-
-#if 0
-static void try_color(co2_t *env, co2_irn_t *ci, col_t col, struct list_head *parent_changed)
-{
-       be_ifg_t *ifg            = env->co->cenv->ifg;
-       int n_regs               = env->co->cls->n_regs;
-       col_cost_pair_t *col_seq = alloca(n_regs * sizeof(col_seq[0]));
-       affinity_node_t *a       = get_affinity_info(env->co, ci->irn);
-       co2_irn_t **nbs          = alloca(a->degree * sizeof(nbs[0]));
-       int ok = 0;
-
-       col_t new_col;
-       neighb_t *n;
-
-       assert(a != NULL && "This node must be an affinity node");
-
-       /* If that node has already been fixed, leave it alone. */
-       if(color_is_fix(env, ci->irn) || !is_color_admissible(env, ci, col)) {
-               // DB((env->dbg, LEVEL_2, "\t-> color is already fix: %d\n", get_col(env, ci->irn)));
-               return;
-       }
-
-       DB((env->dbg, LEVEL_1, "\taffinity node %+F cost %d trying color %d\n", ci->irn, ci->costs, col));
-
-       single_color_cost(env, col, col_seq);
-       recolor(env, ci->irn, col_seq, parent_changed, 0);
-       new_col = get_col(env, ci->irn);
-
-       ci->tmp_fixed = 1;
-       ci->tmp_col   = new_col;
-
-       DB((env->dbg, LEVEL_2, "\t-> has color %d now. %d wanted\n", new_col, col));
-
-       i = 0;
-       co_gs_foreach_neighb(a, n)
-               nbs[i++] = get_co2_irn(env, n->irn);
-
-       co_gs_foreach_neighb(a, n) {
-               co2_irn_t *ni = get_co2_irn(env, n->irn);
-               col_t tgt_col = be_ifg_connected(ifg, ci->irn, ni->irn) ? get_col(env, ni->irn) : new_col;
-               try_color(env, ni, tgt_col, parent_changed);
-       }
-}
-
-
-static void process_cloud(co2_t *env, co2_cloud_t *cloud)
-{
-       int n_regs            = env->co->cls->n_regs;
-       col_cost_pair_t *cols = alloca(n_regs * sizeof(cols[0]));
-       int best_costs        = cloud_costs(env, cloud);
-       int best_col          = 0;
-
-       struct list_head changed;
-       co2_irn_t *ci;
-       int i;
-
-
-       i = 0;
-       DB((env->dbg, LEVEL_2, "processing cloud with costs %d and master %+F containing:\n", cloud->costs, cloud->master->irn));
-       list_for_each_entry(co2_irn_t, ci, &cloud->members_head, cloud_list) {
-               DB((env->dbg, LEVEL_2, "\t%+F %d\n", ci->irn, ci->costs));
-       }
-
-       determine_color_costs(env, cloud->master, cols);
-       qsort(cols, n_regs, sizeof(cols[0]), col_cost_pair_lt);
-
-       best_col = cols[0].col;
-       for(i = 0; i < n_regs; ++i) {
-               col_t col  = cols[i].col;
-               int reject = 1;
-               int costs;
-
-               INIT_LIST_HEAD(&changed);
-               DBG((env->dbg, LEVEL_2, "\n\ttrying color %d. current costs: %d\n", col, best_costs));
-
-               /* try to recolor all the cloud members. */
-               try_color(env, cloud->master, col, &changed);
-
-               /* recoloring of all nodes did succeed. measure the costs and decide if the coloring shall be kept. */
-               costs = cloud_costs(env, cloud);
-
-               /* materialize the new coloring. */
-               if(costs < best_costs) {
-                       materialize_coloring(&changed);
-                       best_costs = costs;
-                       best_col   = col;
-                       reject     = 0;
-               }
-
-               /* We won't get the cloud any better so stop it. */
-               if(costs == 0)
-                       break;
-
-               if(reject)
-                       reject_coloring(&changed);
-       }
-
-       DB((env->dbg, LEVEL_2, "\tfinished cloud with costs %d\n", best_costs));
-
-       /* fix all cloud members */
-       list_for_each_entry(co2_irn_t, ci, &cloud->members_head, cloud_list) {
-               ci->fixed = 1;
-       }
-
-}
-
-static void try_affinity_node(co2_t *env, co2_irn_t *ci, col_t preferred, struct list_head *parent_changed)
-{
-       ir_node *irn = ci->irn;
-
-       if(!color_is_fix(env, irn)) {
-               int n_regs      = env->co->cls->n_regs;
-               bitset_t *tried = bitset_alloca(n_regs);
-               bitset_t *adm   = bitset_alloca(n_regs);
-               col_cost_pair_t *seq = alloca(n_regs * sizeof(seq[0]));
-
-               affinity_node_t *a = get_affinity_info(env->co, irn);
-               int best_costs  = cloud_costs(env, ci->cloud);
-               int best_col    = get_col(env, ci->irn);
-
-               int i;
-
-               determine_color_costs(env, ci, seq);
-               if(!INFEASIBLE(seq[preferred].costs))
-                       seq[preferred].costs = INT_MIN;
-
-               qsort(seq, n_regs, sizeof(seq[0]), col_cost_pair_lt);
-
-               for(i = 0; i < n_regs; ++i) {
-                       col_t col = seq[i].col;
-
-                       struct list_head changed;
-                       int ok, costs;
-
-                       INIT_LIST_HEAD(&changed);
-                       ok  = change_color_single(env, irn, col, &changed, 0);
-                       col = get_col(env, irn);
-
-                       if(!bitset_is_set(tried, col)) {
-                               neighb_t *n;
-
-                               if(!ci->tmp_col) {
-                                       ci->tmp_col   = col;
-                                       ci->tmp_fixed = 1;
-                                       list_add(&ci->changed_list, &changed);
-                               }
-
-                               co_gs_foreach_neighb(a, n) {
-                                       co2_irn_t *ni = get_co2_irn(env, n->irn);
-                                       try_affinity_node(env, ni, col, &changed);
-                               }
-
-                               examine_coloring(env, ci->cloud);
-                               reject_coloring(&changed);
-
-                               bitset_set(tried, col);
-                       }
-               }
-       }
-}
-#endif
-
 static void examine_cloud_coloring(co2_t *env, co2_cloud_t *cloud)
 {
        int costs = cloud_costs(env, cloud);
@@ -704,43 +536,73 @@ static void examine_cloud_coloring(co2_t *env, co2_cloud_t *cloud)
        }
 }
 
-static int color_change_balance(co2_t *env, co2_irn_t *ci, bitset_t *tried_colors)
+static int color_change_balance(co2_t *env, co2_irn_t *ci, bitset_t *tried_colors, int depth)
 {
-       col_t col = get_col(env, ci->irn);
-       neighb_t *n;
+       col_t col   = get_col(env, ci->irn);
        int balance = 0;
+       neighb_t *n;
 
+       DBG((env->dbg, LEVEL_3, "\t\t%2{firm:indent}node %+F has color %d\n", depth, ci->irn, col));
        co_gs_foreach_neighb(ci->aff, n) {
                col_t nc  = get_col(env, n->irn);
                int fixed = color_is_fix(env, n->irn);
 
-               if(nc == col)
+#if 0
+               if(fixed) {
+                       if(nc == col)
+                               balance -= n->costs;
+                       else if(!bitset_is_set(tried_colors, nc))
+                               balance += n->costs;
+               }
+
+               else
+                       balance += n->costs;
+
+#else
+               if(nc == col) {
                        balance -= n->costs;
-               else if(!fixed || !bitset_is_set(tried_colors, nc))
+                       DBG((env->dbg, LEVEL_3, "\t\t%2{firm:indent}neighbor %+F has the same color, bonus %d\n", depth, n->irn, -n->costs));
+               }
+               else if(!fixed) {
                        balance += n->costs;
+                       DBG((env->dbg, LEVEL_3, "\t\t%2{firm:indent}non fixed neighbor %+F has different color %d, malus %d\n", depth, n->irn, nc, n->costs));
+               }
+#endif
        }
 
-       DBG((env->dbg, LEVEL_4, "\t\tbalance for changing %+F color %d\n", ci->irn, balance));
        return balance;
 }
 
-static void keep_sensible_colors(co2_t *env, co2_irn_t *ci, col_cost_pair_t *seq)
+static void adjust_start_colors(co2_t *env, co2_cloud_t *cloud, col_cost_pair_t *seq)
 {
-       bitset_t *fixed_cols = bitset_alloca(env->co->cls->n_regs);
-       int all_fixed = 1;
-       neighb_t *n;
+       int n_regs    = env->co->cls->n_regs;
+       bitset_t *adm = bitset_alloca(n_regs);
+       bitset_pos_t col;
+       int i;
 
-       co_gs_foreach_neighb(ci->aff, n) {
-               all_fixed &= color_is_fix(env, n->irn);
-               bitset_set(fixed_cols, get_col(env, n->irn));
-       }
+       for(i = 0; i < cloud->n_memb; ++i) {
+               co2_irn_t *ci = cloud->seq[i];
+               int n_constr;
+
+               /* Prefer precolored neighbors. */
+               bitset_clear_all(adm);
+               admissible_colors(env, ci, adm);
+               n_constr = bitset_popcnt(adm);
+
+               bitset_foreach(adm, col) {
+                       seq[col].costs = add_saturated(seq[col].costs, -128 * (n_regs - n_constr));
+               }
 
-       if(all_fixed) {
-               bitset_pos_t i;
-               bitset_flip_all(fixed_cols);
-               bitset_foreach(fixed_cols, i)
-                       seq[i].costs = INT_MAX;
+               bitset_foreach_clear(adm, col) {
+                       seq[col].costs = add_saturated(seq[col].costs, 128);
+               }
        }
+
+       admissible_colors(env, cloud->master, adm);
+       bitset_flip_all(adm);
+
+       bitset_foreach(adm, col)
+               seq[col].costs = INT_MAX;
 }
 
 static int process_node(co2_t *env, co2_cloud_t *cloud, int index)
@@ -754,34 +616,34 @@ static int process_node(co2_t *env, co2_cloud_t *cloud, int index)
                col_cost_pair_t *seq    = alloca(n_regs * sizeof(seq[0]));
                bitset_t *cols_tried    = bitset_alloca(n_regs);
                int done                = 0;
-               //col_cost_pair_t *single = alloca(n_regs * sizeof(seq[0]));
+               int n_new_fixed         = 0;
+               int n_fixed             = 0;
 
+               neighb_t *n;
                int i;
 
-               determine_color_costs(env, ci, seq);
-
-               if(index == 0) {
-                       col_t col     = get_col(env, ci->irn);
-                       int min_costs = INT_MAX;
-                       int i;
-
-                       for(i = 0; i < n_regs; ++i)
-                               min_costs = MIN(min_costs, seq[i].costs);
+               /*
+               Investigate if one of the fixed neighbors of this node has changed
+               its color since this node changed its color. If so we will
+               consider changing this node's color again.
+               */
 
-                       seq[col].costs = min_costs - 1;
+               co_gs_foreach_neighb(ci->aff, n) {
+                       co2_irn_t *ni = get_co2_irn(env, n->irn);
+                       n_fixed += ni->fixed;
+                       n_new_fixed += ni->fixed && ni->last_color_change > ci->last_color_change;
                }
 
-               //keep_sensible_colors(env, ci, seq);
-               qsort(seq, n_regs, sizeof(seq[0]), col_cost_pair_lt);
+               if(n_fixed > 0 && n_new_fixed == 0)
+                       return process_node(env, cloud, index + 1);
 
-#if 0
-               if(index == cloud->n_memb - 1) {
-                       for(i = 0; i < n_regs; ++i)
-                               if(seq[i].costs >= 0)
-                                       seq[i].costs = INT_MAX;
-               }
-#endif
+               determine_color_costs(env, ci, seq);
+
+               /* If this is the first node, adjust the sequence of the coloring. */
+               if(index == 0)
+                       adjust_start_colors(env, cloud, seq);
 
+               qsort(seq, n_regs, sizeof(seq[0]), col_cost_pair_lt);
 
                for(i = 0; i < n_regs && !done; ++i) {
                        col_t col = seq[i].col;
@@ -794,21 +656,26 @@ static int process_node(co2_t *env, co2_cloud_t *cloud, int index)
                                all other colors do no good.
                        */
 
-                       DB((env->dbg, LEVEL_2, "\t%2Ntrying %+F index %d for color %d\n", index, ci->irn, index, col));
+                       DB((env->dbg, LEVEL_2, "\t%2{firm:indent}trying %+F index %d for color %d\n", index, ci->irn, index, col));
                        if(INFEASIBLE(costs)) {
-                               DBG((env->dbg, LEVEL_2, "\t%2N-> color is infeasible due to %s\n", index, flag_str(seq[i].flags)));
+                               DBG((env->dbg, LEVEL_2, "\t%2{firm:indent}-> color is infeasible due to %s\n", index, flag_str(seq[i].flags)));
                                break;
                        }
 
                        bitset_set(cols_tried, col);
                        INIT_LIST_HEAD(&changed);
-                       ok = change_color_single(env, ci->irn, col, &changed, 0);
-                       DB((env->dbg, LEVEL_2, "\t%2N-> %s\n", index, ok ? "ok" : "failed"));
+                       cloud->ticks++;
+                       ok = change_color_single(env, ci->irn, col, &changed, index);
+                       DB((env->dbg, LEVEL_2, "\t%2{firm:indent}-> %s\n", index, ok ? "ok" : "failed"));
 
                        /* if we succeeded changing the color, we will figure out the next node. */
                        if(ok) {
+                               int balance;
                                int finish;
 
+                               /* Mark the time of the last color change */
+                               ci->last_color_change = cloud->ticks;
+
                                /* materialize the coloring and fix the node's color. */
                                ci->fixed = 1;
 
@@ -818,23 +685,24 @@ static int process_node(co2_t *env, co2_cloud_t *cloud, int index)
                                /* if this is the last node in the coloring sequence, examine the coloring */
                                if(index == cloud->n_memb - 1) {
                                        examine_cloud_coloring(env, cloud);
-                                       DB((env->dbg, LEVEL_2, "\t%2N-> current best coloring %d\n", index, cloud->best_costs));
-                                       if(cloud->best_costs == cloud->inevit) {
-                                               done = 1;
-                                               res  = 1;
-                                       }
+                                       DB((env->dbg, LEVEL_2, "\t%2{firm:indent}-> current best coloring %d\n", index, cloud->best_costs));
+                                       if(cloud->best_costs == cloud->inevit)
+                                               finish = 1;
                                }
 
+                               /* Compute the recoloring balance for this solution. */
+                               balance = color_change_balance(env, ci, cols_tried, index);
+                               DBG((env->dbg, LEVEL_3, "\t%2{firm:indent}balance for further color changing: %d\n", index, balance));
+
                                /* unfix the node. */
                                reject_coloring(&changed);
                                ci->fixed = 0;
 
-                               if(finish || color_change_balance(env, ci, cols_tried) <= 0) {
+                               if(finish || balance <= 0) {
                                        res  = finish;
                                        done = 1;
                                }
                        }
-
                }
        }
 
@@ -929,19 +797,39 @@ static void populate_cloud(co2_t *env, co2_cloud_t *cloud, affinity_node_t *a, i
        }
 }
 
-static void init_cloud(co2_t *env, co2_cloud_t *cloud, affinity_node_t *a)
+static co2_cloud_t *new_cloud(co2_t *env, affinity_node_t *a)
 {
+       co2_cloud_t *cloud = phase_alloc(&env->ph, sizeof(cloud[0]));
+       memset(cloud, 0, sizeof(cloud[0]));
+       INIT_LIST_HEAD(&cloud->members_head);
+       INIT_LIST_HEAD(&cloud->list);
+       list_add(&cloud->list, &env->cloud_head);
+       cloud->best_costs = INT_MAX;
+       cloud->env = env;
        env->visited++;
        populate_cloud(env, cloud, a, 0);
 
+       /* Allocate space for the best colors array, where the best coloring is saved. */
        cloud->best_cols = phase_alloc(&env->ph, cloud->n_memb * sizeof(cloud->best_cols[0]));
-       cloud->seq       = phase_alloc(&env->ph, cloud->n_memb * sizeof(cloud->seq[0]));
-       env->visited++;
+
+       /* Also allocate space for the node sequence and compute that sequence. */
+       cloud->seq    = phase_alloc(&env->ph, cloud->n_memb * sizeof(cloud->seq[0]));
        cloud->seq[0] = cloud->master;
+       env->visited++;
        determine_coloring_sequence(env, cloud);
+
+#if 0
+       /* Allocate the failure matrix. */
+       cloud->failrure_matrix = phase_alloc(&env->ph, cloud->n_memb * sizeof(cloud->failrure_matrix[0]));
+       for(i = 0; i < env->n_regs; ++i) {
+               cloud->failrure_matrix[i] = phase_alloc(&env->ph, env->n_regs * sizeof(cloud->failrure_matrix[i][0]));
+               memset(cloud->failrure_matrix[i], 0, env->n_regs * sizeof(cloud->failrure_matrix[i][0]));
+       }
+#endif
+       return cloud;
 }
 
-static void process_cloud(co2_t *env, co2_cloud_t *cloud)
+static void process_cloud(co2_t *env, co2_cloud_t *cloud, int nr)
 {
        struct list_head changed;
        int i;
@@ -984,12 +872,23 @@ static void process_cloud(co2_t *env, co2_cloud_t *cloud)
                }
                assert(!some_fixed);
        }
+
+       {
+               char buf[256];
+               FILE *f;
+
+               ir_snprintf(buf, sizeof(buf), "ifg_%F_%s_cloud_%d.dot", env->co->irg, env->co->cls->name, nr);
+               if(f = fopen(buf, "wt")) {
+                       be_ifg_dump_dot(env->co->cenv->ifg, env->co->irg, f, &ifg_dot_cb, env);
+                       fclose(f);
+               }
+       }
+
 }
 
 static void process(co2_t *env)
 {
        affinity_node_t *a;
-       struct list_head cloud_head;
        co2_cloud_t *pos;
        co2_cloud_t **clouds;
        int n_clouds;
@@ -998,31 +897,25 @@ static void process(co2_t *env)
        int all_costs   = 0;
        int final_costs = 0;
 
-
-       INIT_LIST_HEAD(&cloud_head);
-
        n_clouds = 0;
        co_gs_foreach_aff_node(env->co, a) {
                co2_irn_t *ci = get_co2_irn(env, a->irn);
 
                if(!ci->cloud) {
-                       co2_cloud_t *cloud = new_cloud(env);
-
-                       init_cloud(env, cloud, a);
-                       list_add(&cloud->list, &cloud_head);
+                       co2_cloud_t *cloud = new_cloud(env, a);
                        n_clouds++;
                }
        }
 
        i = 0;
        clouds = xmalloc(n_clouds * sizeof(clouds[0]));
-       list_for_each_entry(co2_cloud_t, pos, &cloud_head, list)
+       list_for_each_entry(co2_cloud_t, pos, &env->cloud_head, list)
                clouds[i++] = pos;
        qsort(clouds, n_clouds, sizeof(clouds[0]), cmp_clouds);
 
        for(i = 0; i < n_clouds; ++i) {
                init_costs  += cloud_costs(env, clouds[i]);
-               process_cloud(env, clouds[i]);
+               process_cloud(env, clouds[i], i);
                all_costs   += clouds[i]->costs;
                final_costs += clouds[i]->best_costs;
        }
@@ -1043,11 +936,127 @@ static void writeback_colors(co2_t *env)
        }
 }
 
+/*
+  ___ _____ ____   ____   ___ _____   ____                        _
+ |_ _|  ___/ ___| |  _ \ / _ \_   _| |  _ \ _   _ _ __ ___  _ __ (_)_ __   __ _
+  | || |_ | |  _  | | | | | | || |   | | | | | | | '_ ` _ \| '_ \| | '_ \ / _` |
+  | ||  _|| |_| | | |_| | |_| || |   | |_| | |_| | | | | | | |_) | | | | | (_| |
+ |___|_|   \____| |____/ \___/ |_|   |____/ \__,_|_| |_| |_| .__/|_|_| |_|\__, |
+                                                           |_|            |___/
+*/
+
+static const char *get_dot_color_name(int col)
+{
+       static const char *names[] = {
+               "blue",
+               "red",
+               "green",
+               "yellow",
+               "cyan",
+               "magenta",
+               "orange",
+               "chocolate",
+               "beige",
+               "navy",
+               "darkgreen",
+               "darkred",
+               "lightPink",
+               "chartreuse",
+               "lightskyblue",
+               "linen",
+               "pink",
+               "lightslateblue",
+               "mintcream",
+               "red",
+               "darkolivegreen",
+               "mediumblue",
+               "mistyrose",
+               "salmon",
+               "darkseagreen",
+               "mediumslateblue"
+               "moccasin",
+               "tomato",
+               "forestgreen",
+               "darkturquoise",
+               "palevioletred"
+       };
+
+       return col < sizeof(names)/sizeof(names[0]) ? names[col] : "white";
+}
+
+static const char *get_dot_shape_name(co2_t *env, co2_irn_t *ci)
+{
+       arch_register_req_t req;
+
+       arch_get_register_req(env->co->aenv, &req, ci->irn, BE_OUT_POS(0));
+       if(arch_register_req_is(&req, limited))
+               return "diamond";
+
+       if(ci->fixed)
+               return "rectangle";
+
+       if(ci->tmp_fixed)
+               return "hexagon";
+
+       return "ellipse";
+}
+
+static void ifg_dump_graph_attr(FILE *f, void *self)
+{
+       fprintf(f, "overlay=false");
+}
+
+static int ifg_is_dump_node(void *self, ir_node *irn)
+{
+       co2_t *env = self;
+       return !arch_irn_is(env->co->aenv, irn, ignore);
+}
+
+static void ifg_dump_node_attr(FILE *f, void *self, ir_node *irn)
+{
+       co2_t *env    = self;
+       co2_irn_t *ci = get_co2_irn(env, irn);
+
+       ir_fprintf(f, "label=\"%+F,%d\" style=filled color=%s shape=%s", irn, ci->costs,
+               get_dot_color_name(get_col(env, irn)), get_dot_shape_name(env, ci));
+}
+
+static void ifg_dump_at_end(FILE *file, void *self)
+{
+       co2_t *env = self;
+       affinity_node_t *a;
+
+       co_gs_foreach_aff_node(env->co, a) {
+               int idx = get_irn_idx(a->irn);
+               neighb_t *n;
+
+               co_gs_foreach_neighb(a, n) {
+                       int nidx = get_irn_idx(n->irn);
+
+                       if(idx < nidx) {
+                               const char *style = get_col(env, a->irn) == get_col(env, n->irn) ? "dashed" : "dotted";
+                               fprintf(file, "\tn%d -- n%d [label=\"%d\" style=%s weight=0.01];\n", idx, nidx, n->costs, style);
+                       }
+               }
+       }
+
+}
+
+static be_ifg_dump_dot_cb_t ifg_dot_cb = {
+       ifg_is_dump_node,
+       ifg_dump_graph_attr,
+       ifg_dump_node_attr,
+       NULL,
+       NULL,
+       ifg_dump_at_end
+};
+
 void co_solve_heuristic_new(copy_opt_t *co)
 {
        co2_t env;
+       FILE *f;
 
-       phase_init(&env.ph, "co2", co->cenv->birg->irg, sizeof(co2_irn_t), PHASE_DEFAULT_GROWTH, co2_irn_init);
+       phase_init(&env.ph, "co2", co->cenv->birg->irg, PHASE_DEFAULT_GROWTH, co2_irn_init);
        env.touched     = NULL;
        env.visited     = 0;
        env.co          = co;
@@ -1056,8 +1065,20 @@ void co_solve_heuristic_new(copy_opt_t *co)
        bitset_flip_all(env.ignore_regs);
        be_abi_put_ignore_regs(co->cenv->birg->abi, co->cls, env.ignore_regs);
        FIRM_DBG_REGISTER(env.dbg, "firm.be.co2");
+       INIT_LIST_HEAD(&env.cloud_head);
+
+       if(f = be_chordal_open(co->cenv, "ifg_before_", "dot")) {
+               be_ifg_dump_dot(co->cenv->ifg, co->irg, f, &ifg_dot_cb, &env);
+               fclose(f);
+       }
 
        process(&env);
+
+       if(f = be_chordal_open(co->cenv, "ifg_after_", "dot")) {
+               be_ifg_dump_dot(co->cenv->ifg, co->irg, f, &ifg_dot_cb, &env);
+               fclose(f);
+       }
+
        writeback_colors(&env);
        phase_free(&env.ph);
 }