Added sparse matrix impl. Used by copyopt_ilp
[libfirm] / ir / be / bephicoal.c
index 72a2866..a596553 100644 (file)
 #include "phiclass_t.h"
 #include "bephicoal_t.h"
 
-#define DEBUG_LVL SET_LEVEL_3
-#define MAX_COLORS 16
+#define DEBUG_LVL 0 //SET_LEVEL_2
+#define MAX_COLORS 32
 
 #define INITIAL_SLOTS_PINNED_GLOBAL 256
-#define INITIAL_SLOTS_FREE_NODES    128
 #define INITIAL_SLOTS_CHANGED_NODES 32
 
 /* some things for readable code */
@@ -37,7 +36,7 @@
 
 /**
  * Models conflicts between nodes. These may be life range conflicts or
- * pinning conflicts which may occur while changing colors
+ * pinning conflicts, which may occur while changing colors
  */
 typedef struct _conflict_t {
        ir_node *n1, *n2;
@@ -66,8 +65,6 @@ typedef struct _node_stat_t {
  * corresponding phi class.
  */
 typedef struct _phi_unit_t {
-       unsigned char phi_count;                        /**< the number of phi nodes in this unit */
-       /* 1 phi */
        unsigned char node_count;                       /**< size of the nodes-array */
        unsigned char conflict_count;           /**< size of the conflicts-array */
        unsigned char conflict_count_org;       /**< initial size of the conflicts-array */
@@ -79,18 +76,12 @@ typedef struct _phi_unit_t {
 static firm_dbg_module_t *dbgphi = NULL;
 
 /**
- * Contains ir_nodes of phi-classes whose colors may change unlimited times.
- * These nodes are not optimizable, so there is no need to pin their color.
- */
-static pset *free_nodes = NULL;
-
-/**
- * Contains already optimized ir_nodes of phi-classes fully processed.
+ * Contains already optimized ir_nodes of phi-units fully processed.
  * So one can perform a check not to switch them twice or more.
  */
 static pset *pinned_global = NULL;
 
-int set_cmp_node_stat_t(const void *x, const void *y, size_t size) {
+static int set_cmp_node_stat_t(const void *x, const void *y, size_t size) {
        return ((node_stat_t *)x)->irn != ((node_stat_t *)y)->irn;
 }
 
@@ -132,11 +123,20 @@ static INLINE int pu_get_new_color(phi_unit_t *pu, ir_node *irn) {
  */
 static INLINE void pu_set_new_color(phi_unit_t *pu, ir_node *irn, int color) {
        node_stat_t *found = pu_find_or_insert_node(pu, irn);
-       /* TODO Think about
-        * This is only correct if no color is set >=2 times while changing
-        * a single phi-unit-member */
        found->undo_color = found->color;
        found->color = color;
+       DBG((dbgphi, LEVEL_4, "%n %d\n", irn, color));
+}
+
+/**
+ * Sets the virtual color of a node to the color it had,
+ * before the last call to pu_set_new_color
+ */
+static INLINE void pu_undo_color(phi_unit_t *pu, ir_node *irn) {
+       node_stat_t *ns = pu_find_node(pu, irn);
+       assert(ns && "Nodes whose colors are undone must be in pu->changed_nodes");
+       ns->color = ns->undo_color;
+       DBG((dbgphi, LEVEL_3, "\t\tUndo: col(%n) := %d\n", irn, ns->undo_color));
 }
 
 /**
@@ -158,6 +158,7 @@ static INLINE int pu_is_node_removed(phi_unit_t *pu, ir_node *irn) {
 static INLINE void pu_remove_node(phi_unit_t *pu, ir_node *irn) {
        node_stat_t *found = pu_find_or_insert_node(pu, irn);
        _set_removed(found);
+       DBG((dbgphi, LEVEL_4, "%n\n", irn));
 }
 
 /**
@@ -179,6 +180,7 @@ static INLINE int pu_is_node_pinned(phi_unit_t *pu, ir_node *irn) {
 static INLINE void pu_pin_node(phi_unit_t *pu, ir_node *irn) {
        node_stat_t *found = pu_find_or_insert_node(pu, irn);
        _set_pinned(found);
+       DBG((dbgphi, LEVEL_4, "%n\n", irn));
 }
 
 /**
@@ -188,6 +190,7 @@ static INLINE void pu_pin_node(phi_unit_t *pu, ir_node *irn) {
 static INLINE void pu_add_conflict(phi_unit_t *pu, ir_node *n1, ir_node *n2) {
        int count = pu->conflict_count;
 
+       DBG((dbgphi, LEVEL_3, "\t    %n -- %n\n", n1, n2));
        assert(count != 255 && "Too much conflicts. Can hold max 255 entries");
        if ((count & 15) == 0)
                pu->conflicts = realloc(pu->conflicts, (count + 16)*sizeof(*pu->conflicts));
@@ -224,31 +227,43 @@ static INLINE int pu_are_conflicting(phi_unit_t *pu, ir_node *n1, ir_node *n2) {
        return 0;
 }
 
+/**
+ * Checks if a node is a member of a phi unit.
+ * Other nodes should not be pinned global.
+ */
+static INLINE int pu_is_global_pinnable(phi_unit_t *pu, ir_node *irn) {
+       int i;
+       for (i = 0; i < pu->node_count; ++i)
+               if (pu->nodes[i] == irn)
+                       return 1;
+       return 0;
+}
+
 /**
  * Determines a maximum independent set with respect to the conflict edges
  * in pu->conflicts and the nodes beeing all non-removed nodes of pu->nodes.
  * TODO: make this 'un-greedy'
- * TODO: be aware that phi nodes should find their way in the set.
- *       for 1 phi in greedy version this is no prob, cause is comes first at [0].
+ * ATTENTION: be aware that phi nodes find their way into the set. For 1 phi
+ *                       in greedy version this is no prob, cause it comes first at [0].
  */
-int pu_get_max_ind_set(phi_unit_t *pu, struct obstack *res) {
+static int pu_get_mis(phi_unit_t *pu, struct obstack *res) {
        int i, o, size = 0;
        ir_node **mis;
 
-       DBG((dbgphi, 1, "\t\t    Max indep set\n"));
+       DBG((dbgphi, LEVEL_2, "\t    Max indep set:\n"));
        for (i = 0; i < pu->node_count; ++i) {
                int intf_det = 0;
                if (pu_is_node_removed(pu, pu->nodes[i]))
                        continue;
                mis = (ir_node**) obstack_base(res);
                for (o = 0; o < size; ++o)
-                       if (phi_ops_interfere(pu->nodes[i], mis[o])) {
+                       if (pu_are_conflicting(pu, pu->nodes[i], mis[o])) {
                                intf_det = 1;
                                break;
                        }
 
                if (!intf_det) {
-                       DBG((dbgphi, 1, "\t\t\tAdding to mis %n\n", pu->nodes[i]));
+                       DBG((dbgphi, LEVEL_2, "\t\t%n\n", pu->nodes[i]));
                        obstack_ptr_grow(res, pu->nodes[i]);
                        size++;
                }
@@ -271,6 +286,7 @@ int pu_get_max_ind_set(phi_unit_t *pu, struct obstack *res) {
  *
  * ASSUMPTION: Assumes that a life range of a single value can't be spilt into
  *                        several smaller intervals where other values can live in between.
+ *             This should be true in SSA.
  */
 static ir_node *_pu_color_irn(phi_unit_t *pu, ir_node *irn, int col, const ir_node *trigger, struct obstack *changed_nodes) {
        ir_node *res;
@@ -278,13 +294,13 @@ static ir_node *_pu_color_irn(phi_unit_t *pu, ir_node *irn, int col, const ir_no
        ir_node **confl, *cn;
        int i, irn_col;
 
+       DBG((dbgphi, LEVEL_3, "\t\t%n \tcaused col(%n) \t%2d --> %2d\n", trigger, irn, pu_get_new_color(pu, irn), col));
        obstack_init(&confl_ob);
        irn_col = pu_get_new_color(pu, irn);
 
        if (irn_col == col)
                goto ret_save;
        if (pset_find_ptr(pinned_global, irn) || pu_is_node_pinned(pu, irn)) {
-               DBG((dbgphi, LEVEL_2, "\t\t\t%n \t~~> %n := %d: Pinned\n", trigger, irn, col));
                res = irn;
                goto ret_confl;
        }
@@ -303,32 +319,36 @@ static ir_node *_pu_color_irn(phi_unit_t *pu, ir_node *irn, int col, const ir_no
                        pset *live_ins = get_live_in(irn_bl);
                        for (n = pset_first(live_ins); n; n = pset_next(live_ins))
                                if (is_allocatable_irn(n) && n != trigger && pu_get_new_color(pu, n) == col && phi_ops_interfere(irn, n)) {
-                                       DBG((dbgphi, LEVEL_3, "\t\t\t\t\t ******************** %n %n\n", irn, n));
+                                       DBG((dbgphi, LEVEL_4, "\t\t    %n\ttroubles\n", n));
                                        obstack_ptr_grow(&confl_ob, n);
                                        pset_break(live_ins);
                                        break;
                                }
                }
 
-               /* setup the queue of blocks */
+               /* setup the queue of blocks. */
                obstack_init(&q);
                obstack_ptr_grow(&q, irn_bl);
                in = 1;
                out = 0;
 
-               /* process the queue */
+               /* process the queue. The code below checks for every block dominated
+                * by the irns one, and in which the irn is live, if there are
+                * conflicting nodes */
                while (out < in) {
                        ir_node *curr_bl, *sub_bl;
                        int i, max;
 
                        curr_bl = ((ir_node **)obstack_base(&q))[out++];
 
-                       /* Add to the result all nodes in the block which live in target color
-                        * and interfere with the irn */
+                       /* Add to the result all nodes in the block, which have
+                        * the target color and interfere with the irn */
                        for (i = 0, max = get_irn_n_outs(curr_bl); i < max; ++i) {
                                ir_node *n = get_irn_out(curr_bl, i);
-                               if (is_allocatable_irn(n) && n != trigger && pu_get_new_color(pu, n) == col && phi_ops_interfere(irn, n))
+                               if (is_allocatable_irn(n) && n != trigger && pu_get_new_color(pu, n) == col && phi_ops_interfere(irn, n)) {
+                                       DBG((dbgphi, LEVEL_4, "\t\t    %n\ttroubles\n", n));
                                        obstack_ptr_grow(&confl_ob, n);
+                               }
                        }
 
                        /* If irn lives out check i-dominated blocks where the irn lives in */
@@ -351,7 +371,6 @@ static ir_node *_pu_color_irn(phi_unit_t *pu, ir_node *irn, int col, const ir_no
                ir_node *sub_res;
 
                /* try to color the conflicting node cn with the color of the irn itself */
-               DBG((dbgphi, LEVEL_3, "\t\t\t%n \t~~> %n := %d: Subcheck\n", trigger, irn, col));
                sub_res = _pu_color_irn(pu, cn, irn_col, irn, changed_nodes);
                if (sub_res != CHANGE_SAVE) {
                        res = sub_res;
@@ -361,39 +380,54 @@ static ir_node *_pu_color_irn(phi_unit_t *pu, ir_node *irn, int col, const ir_no
        /* if we arrive here all sub changes can be applied, so it's save to change this irn */
 
 ret_save:
-       DBG((dbgphi, LEVEL_2, "\t\t\t%n \t~~> %n := %d: Save\n", trigger, irn, col));
+       DBG((dbgphi, LEVEL_3, "\t\t%n save\n", irn));
        obstack_free(&confl_ob, NULL);
        pu_set_new_color(pu, irn, col);
        obstack_ptr_grow(changed_nodes, irn);
        return CHANGE_SAVE;
 
 ret_confl:
-       DBG((dbgphi, LEVEL_2, "\t\t\t%n \t~~> %n := %d: Conflict\n", trigger, irn, col));
+       DBG((dbgphi, LEVEL_3, "\t\t%n conflicting\n", irn));
        obstack_free(&confl_ob, NULL);
        return res;
 }
 
-#define pu_color_irn(pu,irn,col,ob) _pu_color_irn(pu, irn, col, irn, ob)
+static ir_node *pu_color_irn(phi_unit_t *pu, ir_node *irn, int col) {
+       ir_node *res;
+       struct obstack ob_undo;
+
+       obstack_init(&ob_undo);
+       res = _pu_color_irn(pu, irn, col, irn, &ob_undo);
+
+       if (res != CHANGE_SAVE) { /* undo virtual changes caused by the last call */
+               int i;
+               ir_node *undo_node, **undo_nodes;
+
+               obstack_ptr_grow(&ob_undo, NULL);
+               undo_nodes = obstack_finish(&ob_undo);
+               for (i = 0, undo_node = undo_nodes[0]; undo_node; undo_node = undo_nodes[++i])
+                       pu_undo_color(pu, undo_node);
+       }
+
+       obstack_free(&ob_undo, NULL);
+       return res;
+}
 
 /**
  * Tries to set as much members of a phi unit as possible to color @p col.
  * All changes taken together are guaranteed to be conflict free.
  */
 static int pu_try_color(phi_unit_t *pu, int col, int b_size) {
-       struct obstack ob_mis, ob_undo;
+       struct obstack ob_mis;
        int i, redo, mis_size;
        ir_node **mis;
 
-       /* first init pessimistically. Just return if we can't get a better result */
-       mis_size = 0;
-
        obstack_init(&ob_mis);
-       obstack_init(&ob_undo);
        redo = 1;
        while (redo) {
                redo = 0;
                /* get a max independent set regarding current conflicts */
-               mis_size = pu_get_max_ind_set(pu, &ob_mis);
+               mis_size = pu_get_mis(pu, &ob_mis);
                mis = obstack_finish(&ob_mis);
 
                /* shortcut: if mis size is worse than best, then mis won't be better. */
@@ -405,43 +439,46 @@ static int pu_try_color(phi_unit_t *pu, int col, int b_size) {
                        ir_node *test_node, *confl_node;
 
                        test_node = mis[i];
-                       DBG((dbgphi, 1, "\t\t    Testing %n\n", test_node));
-                       confl_node = pu_color_irn(pu, test_node, col, &ob_undo);
+                       DBG((dbgphi, LEVEL_2, "\t    Testing %n\n", test_node));
+                       confl_node = pu_color_irn(pu, test_node, col);
 
                        if (confl_node == CHANGE_SAVE) {
-                               if (!pset_find_ptr(free_nodes, test_node))
-                                       pu_pin_node(pu, test_node);
-                               obstack_free(&ob_undo, obstack_finish(&ob_undo));
-                               continue;
+                               DBG((dbgphi, LEVEL_2, "\t    Save\n"));
+                               pu_pin_node(pu, test_node);
+                       } else if (confl_node == CHANGE_NYI) {
+                               DBG((dbgphi, 0, "\t    NYI\n"));
+                       } else if (confl_node == CHANGE_IMPOSSIBLE) {
+                               /* TODO: this may happen due to reg constraints --> remove from set ?? */
                        } else {
-                               int i;
-                               ir_node *undo_node, **undo_nodes;
+                               DBG((dbgphi, LEVEL_2, "\t    Conflicting\n"));
+                               assert(is_conflicting_node(confl_node));
 
-                               obstack_ptr_grow(&ob_undo, NULL);
-                               undo_nodes = obstack_finish(&ob_undo);
-                               for (i = 0, undo_node = undo_nodes[0]; undo_node; undo_node = undo_nodes[++i]) {
-                                       node_stat_t *ns = pu_find_node(pu, undo_node);
-                                       ns->color = ns->undo_color;
+                               if (pu_is_node_pinned(pu, confl_node)) {
+                                       /* changing test_node would change back a node of current phi unit */
+                                       pu_add_conflict(pu, confl_node, test_node);
+                                       redo = 1;
                                }
-                               obstack_free(&ob_undo, undo_nodes);
-
-                               if (is_conflicting_node(confl_node)) {
-                                       if (pu_is_node_pinned(pu, confl_node))
-                                               pu_add_conflict(pu, confl_node, test_node);
-                                       if (pset_find_ptr(pinned_global, confl_node))
-                                               pu_remove_node(pu, test_node);
+                               if (pset_find_ptr(pinned_global, confl_node)) {
+                                       /* changing test_node would change back a node of a prior phi unit */
+                                       pu_remove_node(pu, test_node);
+                                       redo = 1;
                                }
                        }
 
-                       /* shortcut: color not possible for phi node (phi comes first) ==> exit */
-                       if (i == 0)
-                               goto ret;
+                       if (confl_node != CHANGE_SAVE) {
+                               /* shortcut: color not possible for phi node (phi comes first) ==> exit */
+                               if (i == 0) {
+                                       mis_size = 0;
+                                       goto ret;
+                               }
+                               /* break iteration over current mis, because it will change */
+                               break;
+                       }
                }
                obstack_free(&ob_mis, mis);
        }
 
 ret:
-       obstack_free(&ob_undo, NULL);
        obstack_free(&ob_mis, NULL);
        return mis_size;
 }
@@ -450,9 +487,10 @@ ret:
  * Tries to re-allocate colors of nodes in this phi unit, to achieve a lower
  * number of copy instructions placed during phi destruction. Optimized version.
  * Works only for phi-classes/phi-units with exactly 1 phi node, which is the
- * case for approximately 80% of all phi units.
+ * case for approximately 80% of all phi classes. All other phi classes are
+ * reduced to this case.
  */
-static void pu_coalesce_1_phi(phi_unit_t *pu) {
+static void pu_coal_1_phi(phi_unit_t *pu) {
        int size, col, b_size, b_color;
        set *b_changes;
 
@@ -468,7 +506,7 @@ static void pu_coalesce_1_phi(phi_unit_t *pu) {
 
                /* did we find a better max ind. set? */
                if (size > b_size) {
-                       DBG((dbgphi, 1, "\t!! Better size: %d\n", size));
+                       DBG((dbgphi, 1, "\tBetter size: %d\n", size));
                        if (b_changes)
                                del_set(b_changes);
                        b_changes = pu->changed_nodes;
@@ -482,7 +520,7 @@ static void pu_coalesce_1_phi(phi_unit_t *pu) {
                pu->changed_nodes = new_set(set_cmp_node_stat_t, INITIAL_SLOTS_CHANGED_NODES);
                pu->conflict_count = pu->conflict_count_org;
 
-               /* shortcut: if all members can be colored we are (very) content */
+               /* shortcut: if all members can be colored we are (very) happy */
                if (b_size == pu->node_count)
                        break;
        }
@@ -490,103 +528,202 @@ static void pu_coalesce_1_phi(phi_unit_t *pu) {
        /* now apply the found optimum */
        if (b_changes) {
                node_stat_t *ns;
-               DBG((dbgphi, 1, "\tBest color: %d  Copies: %d/%d\n", b_color, pu->node_count-b_size, pu->node_count));
-               for (ns = set_first(b_changes); ns; ns = set_next(b_changes))
-                       set_irn_color(ns->irn, ns->color);
+               DBG((dbgphi, 1, "\tBest color: %d  Copies: %d/%d\n", b_color, pu->node_count-b_size, pu->node_count-1));
+               for (ns = set_first(b_changes); ns; ns = set_next(b_changes)) {
+                       /* NO_COLOR is possible, if we had an undo; so the irn stays in the
+                        * pu->changed_nodes with new color set to NO_COLOR. */
+                       if (ns->color != NO_COLOR) {
+                               DBG((dbgphi, 1, "\t    color(%n) := %d\n", ns->irn, ns->color));
+                               set_irn_color(ns->irn, ns->color);
+                               if (pu_is_global_pinnable(pu, ns->irn) && ns->color == pu_get_new_color(pu, pu->nodes[0]))
+                                       pset_insert_ptr(pinned_global, ns->irn);
+                       }
+               }
                free(b_changes);
        } else {
                DBG((dbgphi, 1, "\tBest color: none\n"));
        }
 }
 
-/**
- * Tries to re-allocate colors of nodes in this phi unit, to achieve a lower
- * number of copy instructions placed during phi destruction.
- * General purpose version.
- */
-static void pu_coalesce_n_phi(phi_unit_t *pu) {
-       DBG((dbgphi, 1, "\n"));
-       /* TODO */
-}
 
 /**
- * Prepares a phi class for further processing as a phi unit.
- * @param pc The phi class to prepare.
- * @return A so called phi unit containing some prepared informations
- *         needed by the following coalescing phase.
+ * Prepares a phi class for further processing as one or more phi units.
+ * Calls the worker-functions for all units.
+ * @param pc The phi class to process.
+ * @param root_phi In case of recursive call this is the phi node not beeing
+ *                                an argument in the phi1unit.
+ *                                Else this has to be NULL.
  */
-static phi_unit_t *new_pu(pset *pc) {
-       phi_unit_t *pu;
+static void coal_phi_class(pset *pc, ir_node *root_phi) {
+       int phi_count = 0;
        ir_node *n, *phi = NULL;
 
-       /* get the phi count of this class */
-       pu = calloc(1, sizeof(*pu));
-       for (n = pset_first(pc); n; n = pset_next(pc))
-               if (is_Phi(n)) {
-                       phi = n;
-                       pu->phi_count++;
-               }
+       /* unfortunately there _can_ be >1 phi nodes in a phi1unit,
+        * so we have an if... */
+       if (root_phi) {
+               phi = root_phi;
+               phi_count = 1;
+       } else {
+               /* get the phi count of this class. May result in phi_count == 1 */
+               for (n = pset_first(pc); n; n = pset_next(pc))
+                       if (is_Phi(n)) {
+                               phi = n;
+                               phi_count++;
+                       }
+       }
 
-       if (pu->phi_count == 1) {
+       /* the 'simple' case */
+       if (phi_count == 1) {
+               phi_unit_t *pu;
                ir_node **tmp;
-               int i, o;
                struct obstack ob;
+               int i, o;
 
                obstack_init(&ob);
 
+               DBG((dbgphi, 1, "\tPhi-1 unit:\n"));
+               pu = calloc(1, sizeof(*pu));
+
                /* build member set not containing phi interferers */
-               DBG((dbgphi, 1, "Phi-1 class:\n"));
-               pu->node_count = 1; /*for the phi*/
+               DBG((dbgphi, 1, "\t    %n\n", phi));
+               obstack_ptr_grow(&ob, phi);
+               pu->node_count = 1;
+
                for (n = pset_first(pc); n; n = pset_next(pc)) {
-                       if (is_Phi(n))
+                       if (n == phi)
                                continue;
                        if (!phi_ops_interfere(phi, n)) {
-                               DBG((dbgphi, 1, "\tAdding to members: %n\n", n));
+                               DBG((dbgphi, 1, "\t    %n\n", n));
                                obstack_ptr_grow(&ob, n);
                                pu->node_count++;
                        } else {
-                               DBG((dbgphi, 1, "\tPhi interferer: %n\n", n));
-                               pset_insert_ptr(free_nodes, n);
+                               DBG((dbgphi, 1, "\t    %n \tdropped\n", n));
                        }
                }
                tmp = obstack_finish(&ob);
                pu->nodes = malloc(pu->node_count * sizeof(*pu->nodes));
-               pu->nodes[0] = phi;
-               memcpy(&pu->nodes[1], tmp, (pu->node_count-1) * sizeof(*tmp));
+               memcpy(&pu->nodes[0], tmp, pu->node_count * sizeof(*tmp));
+               obstack_free(&ob, NULL);
 
                /* init conlict graph to life range interference */
+               DBG((dbgphi, 1, "\tInitial conflicts:\n"));
                for (i = 0; i < pu->node_count; ++i)
                        for (o = i+1; o < pu->node_count; ++o)
                                if (phi_ops_interfere(pu->nodes[i], pu->nodes[o]))
                                        pu_add_conflict(pu, pu->nodes[i], pu->nodes[o]);
                pu->conflict_count_org = pu->conflict_count;
 
+               /* init changed nodes */
                pu->changed_nodes = new_set(set_cmp_node_stat_t, INITIAL_SLOTS_CHANGED_NODES);
 
-               obstack_free(&ob, NULL);
-       } else {
-               DBG((dbgphi, 1, "Phi-n class:\n"));
-               /* TODO */
-       }
-
-       DBG((dbgphi, 1, "\n"));
-       return pu;
-}
-
+               pu_coal_1_phi(pu);
 
-/**
- * Deletes a phi unit
- */
-static void free_pu(phi_unit_t *pu) {
-       if (pu->phi_count == 1) {
                free(pu->nodes);
                free(pu->changed_nodes);
                if (pu->conflicts)
                        free(pu->conflicts);
-       } else {
-               /* TODO */
+       } else {        /* the 'not so easy' case */
+               DBG((dbgphi, 1, "\tPhi-n unit:\n"));
+
+               /* copy pc into big_pc... */
+               pset *copy = pset_new_ptr(32);
+               for (n = pset_first(pc); n; n = pset_next(pc)) {
+                       DBG((dbgphi, 1, "\t    %n\n", n));
+                       pset_insert_ptr(copy, n);
+               }
+
+               /* ... because we want to build small 'connected graphs' and
+                * delete their members from the copy */
+               while (pset_count(copy) > 0) {
+                       /* build all connected sets from the copy */
+                       int last = 0, first = 0;
+                       ir_node **queue = calloc(pset_count(copy), sizeof(*queue));
+
+                       /* pick some node out of copy, place into queue */
+                       n = pset_first(copy);
+                       pset_break(copy);
+                       pset_remove_ptr(copy, n);
+                       queue[last++] = n;
+
+                       DBG((dbgphi, 1, "\tConnected:\n"));
+                       pset *connected = pset_new_ptr(8);
+                       while (first < last) {
+                               /* pick n out of the queue into connected set */
+                               n = queue[first++];
+                               pset_insert_ptr(connected, n);
+                               DBG((dbgphi, 1, "\t    %n\n", n));
+
+
+                               /* check if pre/successors are 'connected' with n */
+                               {
+                                       ir_node *other;
+                                       int i;
+                                       /* insert all args of n, which are in the phi class to the queue */
+                                       for(i=0; i < get_irn_arity(n); ++i) {
+                                               other = get_irn_n(n, i);
+                                               if (pset_find_ptr(copy, other) && !values_interfere(n, other)) {
+                                                       queue[last++] = other;
+                                                       pset_remove_ptr(copy, other);
+                                               }
+                                       }
+                                       /* same for outs of n */
+                                       for(i=0; i < get_irn_n_outs(n); ++i) {
+                                               other = get_irn_out(n, i);
+                                               if (pset_find_ptr(copy, other) && !values_interfere(n, other)) {
+                                                       queue[last++] = other;
+                                                       pset_remove_ptr(copy, other);
+                                               }
+                                       }
+                               }
+                       }
+
+                       /* Now we have a "connected graph" build from copy==pc.
+                        * Remove 1-phi-units from the connected set for
+                        * passing to optimizer */
+                       while (pset_count(connected) > 0) {
+                               pset *phi1unit;
+                               ir_node *phi = NULL;
+                               int i;
+                               /* search a phi node */
+                               for (n = pset_first(connected); n; n = pset_next(connected))
+                                       if (is_Phi(n)) {
+                                               phi = n;
+                                               break;
+                                       }
+                               pset_break(connected);
+
+                               /* if there are only non-phi nodes left quit */
+                               if (!phi)
+                                       break;
+
+                               /* Build a 1-phi-unit with this phi */
+                               DBG((dbgphi, 1, "\t    Phi-1-unit:\n"));
+                               phi1unit = pset_new_ptr(8);
+                               pset_insert_ptr(phi1unit, phi);
+                               pset_remove_ptr(connected, phi);
+                               DBG((dbgphi, 1, "\t\t%n\n", phi));
+                               /* insert all arguments of phi, which are in the connected set
+                                * to the 1-phi-unit */
+                               for(i=0; i < get_irn_arity(phi); ++i) {
+                                       ir_node *arg = get_irn_n(phi, i);
+                                       if (pset_find_ptr(connected, arg)) {
+                                               DBG((dbgphi, 1, "\t\t%n\n", arg));
+                                               pset_insert_ptr(phi1unit, arg);
+                                               pset_remove_ptr(connected, arg);
+                                       }
+                               }
+
+                               /* finally the call for coalescing the 1-phi-unit */
+                               if (pset_count(phi1unit) > 1) /* ==1 can happen if the connected set contains only a single phi node */
+                                       coal_phi_class(phi1unit, phi);
+
+                               del_pset(phi1unit);
+                       }
+                       del_pset(connected);
+                       free(queue);
+               }
+               del_pset(copy);
        }
-       free(pu);
 }
 
 
@@ -594,18 +731,10 @@ void be_phi_coalesce(pset *all_phi_classes) {
        pset *pc;
 
        pinned_global = pset_new_ptr(INITIAL_SLOTS_PINNED_GLOBAL);
-       free_nodes = pset_new_ptr(INITIAL_SLOTS_FREE_NODES);
-
-       for (pc = pset_first(all_phi_classes); pc; pc = pset_next(all_phi_classes)) {
-               phi_unit_t *pu = new_pu(pc);
-               if (pu->phi_count == 1)
-                       pu_coalesce_1_phi(pu);
-               else
-                       pu_coalesce_n_phi(pu);
-               free_pu(pu);
-       }
 
-       del_pset(free_nodes);
+       for (pc = pset_first(all_phi_classes); pc; pc = pset_next(all_phi_classes))
+               coal_phi_class(pc, NULL);
+
        del_pset(pinned_global);
 }