Heavy changes. 1st attempt for 1-phi-algo implemented.
[libfirm] / ir / be / bephicoal.c
index 79c40ad..3edfa96 100644 (file)
@@ -5,11 +5,13 @@
 
 #include <stdlib.h>
 
+#include "obst.h"
+#include "pset.h"
 #include "bitset.h"
 #include "debug.h"
+
 #include "bechordal.h"
 #include "belive.h"
-
 #include "bera_t.h"
 #include "bephicongr_t.h"
 #include "bephicoal_t.h"
 #define DEBUG_LVL 1
 
 #define MAX_PHI_CLS_SIZE (1<<(sizeof(unsigned char)*8)) /* possible colors added should fit into unsigned char */
-#define MAX_COLORS 16
-
-
-static firm_dbg_module_t *dbgphi = NULL;
-
-
-typedef enum _live_status_t {
-       livein = 1,
-       liveout = 2
-} live_status_t;
+#define MAX_COLORS 32
+#define CHANGE_IMPOSSIBLE -1
+#define CHANGE_SAVE 1
 
+#define DRYRUN 1
+#define PERFORM 0
 
 typedef struct _phi_unit_t {
        unsigned char count;
        unsigned char phi_count;
-       ir_node **members;                      /* [0..phi_count-1] are phi nodes. [phi_count..count-1] the rest/arguments of the phis */
-       bitset_t **used_cols;
-       int *tgt_colors;                        /* [i] is the color to set for members[i]. [i] == -1 means dont change anything for members[i]*/
-       live_status_t *live_info;       /* [i] says how/where members[i] is live */
+
+       /* 1 phi */
+       ir_node **members;                      /**< [0] is the phi node. [1..count-1] the arguments of the phi not interfering with it */
+       int *colors;                            /**< [i] is the color to set for members[i]. [i] == NO_COLOR means dont change anything for members[i]*/
+       char *is_live_in;                       /**< [i]==1 means members[i] is live-in in (all of) its cf-pred-blocks of the phi node */
+       int size;                                       /**< size of the max independent set of members. The set is markes by colors[i] != NO_COLOR */
+       int changes;                            /**< number of re-assigned nodes belonging to phi-classes */
 } phi_unit_t;
 
 
-static pset *done;
+static firm_dbg_module_t *dbgphi = NULL;
 
+/**
+ * Contains ir_nodes of phi-classes whose colors were reassigned during coalescing.
+ * So one can check not to switch them twice or more.
+ */
+static pset *pinned_nodes = NULL;
 
-static INLINE void set_color(ir_node *irn, int col) {
-       if (!pset_find_ptr(done, irn)) {
-               set_irn_color(irn, col);
-               pset_insert_ptr(done, irn);
-       }
-}
+/**
+ * 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;
 
 
-static void coalesce(phi_unit_t *pu) {
-       int i;
-       if (pu->phi_count != 1) {
-               DBG((dbgphi, 1, "Dropped: phicount\n"));
-               return;
-       }
-#if 0
+/* TODO: ask/check if additional ir_node-space is initialized with 0000 */
+#define belongs_to_a_phi_class(n) (get_irn_phi_info(n)->phi)
 
-       done = pset_new_ptr(64);
+#define is_color_free(bl,col) (!bitset_is_set(get_ra_block_info(bl)->used_colors, col))
 
-       for (i = 0; i < pu->count; ++i)
-               if (pu->live_info[i] & livein) {
-                       DBG((dbgphi, 1, "Dropped: live-in\n"));
-                       return;
-               }
+/**
+ * Set the color of node @p n to @p col, but acts
+ * pinning aware.
+ */
+static INLINE void set_color(ir_node *n, int col) {
+       assert(!pset_find_ptr(pinned_nodes, n));
+       set_irn_color(n, col);
+       if (belongs_to_a_phi_class(n) && !pset_find_ptr(free_nodes, n))
+               pset_insert_ptr(pinned_nodes, n);
+}
 
-       for (i = 0; i < pu->count; ++i) {
-               block_list_t *bl;
+/**
+ * Tries to set the color of @p n to @p col.
+ * @param  n The node to set the color for
+ * @param  col The color to set.
+ * @param  dryrun If true no colors are actually set, only testing is performed.
+ *                If false colors get set and it must be sure that no conflicts will occur.
+ * @return If setting the color is impossible CHANGE_IMPOSSIBLE is returned
+ *         else the number of nodes that need a (cascading) change is returned.
+ */
+static int color_irn(ir_node *n, int col, int dryrun) {
+       ir_node *bl;
+       int res = 0;
+       DBG((dbgphi, 1, "\t\t\t\t%n  %d\n", n, col));
+       assert(is_color(col));
+
+       if (get_irn_color(n) == col) {
+               DBG((dbgphi, 1, "\t\t\t\t\tSame\n"));
+               return 0;
+       }
 
-               if (pu->tgt_colors[i] == -1)
-                       continue;
+       if (pset_find_ptr(pinned_nodes, n)) {
+               DBG((dbgphi, 1, "\t\t\t\t\tPinned\n"));
+               if (!dryrun)
+                       assert(0 && "No prior check with dryrun=1 or buggy");
+               return CHANGE_IMPOSSIBLE;
+       }
 
-               /* TODO: if we move ahead to swapping (not just allocating new colors) this is wrong */
-               set_color(pu->members[i], pu->tgt_colors[i]);
-               if (pu->live_info[i] & liveout) {
-                       bl = get_dominated_dfs(get_nodes_block(pu->members[i]));
-               }
+       bl = get_nodes_block(n);
+       if (is_color_free(bl, col) && !is_live_out(bl, n)) {
+               DBG((dbgphi, 1, "\t\t\t\t\tFree\n"));
+               if (!dryrun)
+                       set_color(n, col);
+               return 1;
        }
 
-       del_pset(done);
+       /* for now, in the aldi-version return impossible */
+       DBG((dbgphi, 1, "\t\t\t\t\tImpossible\n"));
+       return CHANGE_IMPOSSIBLE;
 
-#endif
+       return res;
 }
 
 
-static void ana_1_phi(phi_unit_t *pu) {
-       ir_node *phi, *phi_blk;
-       int i, o, n, col, best_color, size, best_size;
-       ir_node **cand, **max_set, **best_max_set;
-
-       cand = malloc(pu->count * sizeof(ir_node*));
-       max_set = malloc(pu->count * sizeof(ir_node*));
-       best_max_set = malloc(pu->count * sizeof(ir_node*));
+/**
+ * Tries to set as much members of a phi unit as possible to color @p col.
+ */
+static void try_colors(phi_unit_t *pu, int col, int b_size, int b_changes) {
+       struct obstack ob;
+       int i, o, cand_size, mis_size, changes;
+       ir_node **cand, **mis;
 
-       phi = pu->members[0];
-       phi_blk = get_nodes_block(phi);
+       cand_size = 0;
+       obstack_init(&ob);
 
+       /* first init pessimistically, so we can just return
+        * if we see there wont be a better result */
+       pu->size = 0;
+       for (i = 0; i < pu->count; ++i)
+               pu->colors[i] = NO_COLOR;
 
-       /* fill live_info */
-       DBG((dbgphi, 1, "\tLiveness info\n"));
-       /* first the phi */
-       if (is_live_out(phi_blk, phi)) {
-               pu->live_info[0] = liveout;
-               DBG((dbgphi, 1, "\t\t%n lives out\n", phi));
+       /* For all members check if color would be possible.
+        * Does not check if color is possible in combination with
+        * other members colors being set */
+       changes = 0;
+       for (i = 0; i < pu->count; ++i) {
+               int tmp_changes = color_irn(pu->members[i], col, DRYRUN);
+               if (tmp_changes != CHANGE_IMPOSSIBLE) {
+                       DBG((dbgphi, 1, "\t\t\tAdding to cand %n\n", pu->members[i]));
+                       obstack_ptr_grow(&ob, pu->members[i]);
+                       cand_size++;
+                       changes += tmp_changes;
+               }
+               /* if color is not possible for the phi node then exit (phi comes first)*/
+               if (cand_size == 0)
+                       goto ret;
        }
-
-       /* then all args */
-       for (i = 0, n = get_irn_arity(phi); i < n; ++i) {
-               int midx;
-        ir_node *arg, *block_ith_pred;
-
-        arg = get_irn_n(phi, i);
-               block_ith_pred = get_nodes_block(get_irn_n(phi_blk, i));
-
-               /* find the arg in the members array */
-               midx = -1;
-               for (o = 0; o < pu->count; ++o)
-                       if (pu->members[o] == arg) {
-                               midx = o;
+       cand = obstack_finish(&ob);
+
+       /* shortcut: if cand is worse than best the mis wont be better. */
+       if (cand_size < b_size || (cand_size == b_size && changes >= b_changes))
+               goto ret;
+
+       /* now take the candidates cand and determine a max independent set
+        * with respect to edges representing live range interference */
+       /* TODO: make this 'un-greedy' */
+       mis_size = 0;
+       for (i = 0; i < cand_size; ++i) {
+               int intf_det = 0;
+               for (o = 0; o < mis_size; ++o) {
+                       mis = (ir_node**) obstack_base(&ob);
+                       if (values_interfere(cand[i], mis[o])) {
+                               intf_det = 1;
                                break;
                        }
-               assert(midx != -1 && "All args have to be in the members!\n");
-
-               if (is_live_in(block_ith_pred, arg)) {
-                       pu->live_info[midx] |= livein;
-                       DBG((dbgphi, 1, "\t\t%n lives in\n", arg));
                }
 
-               if (is_live_out(block_ith_pred, arg)) {
-                       pu->live_info[midx] |= liveout;
-                       DBG((dbgphi, 1, "\t\t%n lives out\n", arg));
+               if (!intf_det) {
+                       DBG((dbgphi, 1, "\t\t\tAdding to mis %n\n", cand[i]));
+                       obstack_ptr_grow(&ob, cand[i]);
+                       mis_size++;
                }
        }
+       mis = obstack_finish(&ob);
+
+       /* Now set the colors of all nodes in the mis to col.
+        * - Each change taken alone is conflict free.
+        * - If all members are not live-in in their cf-pred-blocks there will be no
+        *   conflict applying all changes
+        * - If there is a member, which is live-in in its cf-pred-block of the phi
+        *   node, it is possible that all changes together will conflict.
+        */
+       for (i = 0; i < pu->count; ++i)
+               for (o = 0; o < mis_size; ++o)
+                       if (pu->members[i] == mis[o] && get_irn_color(pu->members[i]) != col)
+                               pu->colors[i] = col;
 
-       /* find best color */
-       best_size = -1;
-       for (col = 0; col < MAX_COLORS; ++col) {                /* TODO: try phi color first */
-               DBG((dbgphi, 1, "\tTesting colors %d\n", col));
-
-               memset(cand, 0, pu->count * sizeof(ir_node*));
-               memset(max_set, 0, pu->count * sizeof(ir_node*));
-
-               /* BETTER: Alle die mit dem phi interferieren koennen eigentlich
-                * schon frueher raus, da unabhaengig von Farbe. */
-               /* for this color get all potential candidates for a max indep. set */
-               cand[0] = phi;
-               size = 1;
-               for (i = 1; i < pu->count; ++i)
-                       if ((!bitset_is_set(pu->used_cols[i], col) || get_irn_color(pu->members[i]) == col)
-                               && !phi_ops_interfere(phi, pu->members[i])) {
-                               /* color is free or already used by the node
-                                * and argument is not interfering with phi */
-                               cand[i] = pu->members[i];
-                               DBG((dbgphi, 1, "\t\tAdding candidate %n\n", cand[i]));
-                               size++;
-                       }
-               if (size <= best_size) {
-                       /* If the candidate set is smaller the max indep. set wont be larger :) */
-                       continue;
-               }
+ret:
+       obstack_free(&ob, NULL);
+}
 
-               /* determine the max indep. set */
-               /* TODO: make this 'un-greedy' */
-               size = 0;
-               for (i = 0; i < pu->count; ++i) {
-                       int intf_det = 0;
-                       if (!cand[i])
-                               continue;
 
-                       for (o = 0; o < pu->count; ++o) {
-                               if (!max_set[o])
-                                       continue;
-                               if (phi_ops_interfere(cand[i], max_set[o])) {
-                                       DBG((dbgphi, 1, "\t\t\n"));
-                                       intf_det = 1;
-                                       break;
-                               }
-                       }
+/**
+ * Sets the colors of members[i] to colors[i] as far as possible.
+ * Each single change must be conflict free (checked by try_colors).
+ * In some cases not all colors can be set.
+ */
+static void set_colors(phi_unit_t *pu) {
+       int i;
+       int change_is_save, live_in_occured = 0;
 
-                       if (!intf_det) {
-                               DBG((dbgphi, 1, "\t\tAdding to set %n\n", cand[i]));
-                               max_set[i] = cand[i];
-                               size++;
+       for (i = 0; i < pu->count; ++i)
+               if (pu->colors[i] != NO_COLOR) {
+                       if (pu->is_live_in[i])
+                               live_in_occured = 1;
+
+                       change_is_save = CHANGE_SAVE;
+                       if (live_in_occured)
+                               change_is_save = color_irn(pu->members[i], pu->colors[i], DRYRUN);
+
+                       /* HINT: Dont change to == CHANGE_SAVE */
+                       if (change_is_save != CHANGE_IMPOSSIBLE) {
+                               DBG((dbgphi, 1, "\t\tSetting %n to %d\n", pu->members[i], pu->colors[i]));
+                               color_irn(pu->members[i], pu->colors[i], PERFORM);
+                       } else {
+                               DBG((dbgphi, 1, "\t\tConflict due to a live-in: %n\n", pu->members[i]));
                        }
                }
+}
 
 
-               DBG((dbgphi, 1, "\t\tColor %d resulted in a set size of %d\n", col, size));
-
-               /* Did we find a better max set? */
-               if (size > best_size) {
-                       void *tmp;
-
-                       best_color = col;
-                       best_size = size;
+/**
+ * Tries to re-allocate colors of this phi-class, to achieve a lower number of
+ * copies placed during phi destruction. Optimized version. Works only for
+ * phi-classes/phi-units with exactly 1 phi node, which is the case for approx.
+ * 80%.
+ */
+static void coalesce_1_phi(phi_unit_t *pu) {
+       int *b_colors, b_size, b_changes, b_color;
+       int i, col;
 
-                       tmp = best_max_set;
-                       best_max_set = max_set;
-                       max_set = tmp;
+       /* init best search result */
+       b_colors = malloc(pu->count * sizeof(*b_colors));
+       for (i = 0; i < pu->count; ++i)
+               b_colors[i] = NO_COLOR;
+       b_size = 0;
+       b_changes = 0;
+       b_color = NO_COLOR;
+
+       /* find optimum of all colors */
+       for (col = MAX_COLORS-1; col >= 0; --col) {
+               DBG((dbgphi, 1, "\tTrying color %d\n", col));
+               try_colors(pu, col, b_size, b_changes);
+
+               /* did we find a better max ind. set? */
+               if (pu->size > b_size || (pu->size == b_size && pu->changes < b_changes)) {
+                       b_size = pu->size;
+                       b_changes = pu->changes;
+                       b_color = col;
+                       memcpy(b_colors, pu->colors, pu->count * sizeof(*b_colors));
+                       DBG((dbgphi, 1, "\t\tBetter! Size: %d  Changes: %d\n", b_size, b_changes));
                }
 
-               /* Is this a best possible set? */
-               /* BETTER: Find a better lower bound than pu->count, considering interferences */
-               if (best_size == pu->count)
+               /* shortcut: if all members can be colored we are content and doubt that
+                * reducing b_changes justifies all the further trying. */
+               if (b_size == pu->count)
                        break;
        }
-       DBG((dbgphi, 1, "Best color was %d. %d of %d copies needed.\n", best_color, pu->count-best_size, pu->count-1));
 
+       /* now apply the found optimum */
+       DBG((dbgphi, 1, "\tBest color: %d  Copies: %d/%d  Changes: %d\n", b_color, pu->count-b_size, pu->count, b_changes));
+       pu->size = b_size;
+       pu->changes = b_changes;
+       memcpy(pu->colors, b_colors, pu->count * sizeof(*b_colors));
+       set_colors(pu);
 
-       /* now we have the best_max_set with its best_size
-        * so set the tgt_colors */
-       for (i = 0; i < pu->count; ++i)
-               if (best_max_set[i] && get_irn_color(best_max_set[i]) != best_color)
-                       pu->tgt_colors[i] = best_color;
-               else
-                       pu->tgt_colors[i] = -1;
-
-       free(cand);
-       free(max_set);
-       free(best_max_set);
+       free(b_colors);
 }
 
+/**
+ * Tries to re-allocate colors of this phi-class, to achieve a lower number of
+ * copies placed during phi destruction. General purpose version.
+ */
+static void coalesce_n_phi(phi_unit_t *pu) {
+       DBG((dbgphi, 1, "\n"));
+       /* TODO */
+}
 
+/**
+ * Prepare a phi class for further processing as a phi unit.
+ */
 static phi_unit_t *new_phi_unit(pset *pc) {
        phi_unit_t *pu;
-       int i, o;
-       ir_node *n;
+       ir_node *n, *phi = NULL;
 
        assert(pset_count(pc) <= MAX_PHI_CLS_SIZE && "Phi class too large!");
 
-       pu = malloc(sizeof(phi_unit_t));
-       pu->count = pset_count(pc);
-       pu->members = malloc(pu->count * sizeof(*pu->members));
-       pu->used_cols = malloc(pu->count * sizeof(bitset_t*));
-       pu->tgt_colors = malloc(pu->count * sizeof(int));
-       pu->live_info = calloc(pu->count, sizeof(ir_node*));
-
-
-       /* fill the members array */
-       DBG((dbgphi, 1, "Phi class:\n"));
-       i = 0;
-       o = pu->count-1;
-       for (n = (ir_node *)pset_first(pc); n; n = (ir_node *)pset_next(pc)) {
-               DBG((dbgphi, 1, "\t%n\n", n));
-               if (is_Phi(n))
-                       pu->members[i++] = n;
-               else
-                       pu->members[o--] = n;
-       }
-       pu->phi_count = i;
-       DBG((dbgphi, 1, "\n"));
-
+       pu = malloc(sizeof(*pu));
+       pu->phi_count = 0;
+       for (n = pset_first(pc); n; n = pset_next(pc))
+               if (is_Phi(n)) {
+                       phi = n;
+                       pu->phi_count++;
+               }
 
-       /* fill used colors array */
-       for (i = 0; i < pu->count; ++i)
-               pu->used_cols[i] = get_ra_block_info(get_nodes_block(pu->members[i]))->used_colors;
+       if (pu->phi_count == 1) {
+               ir_node **tmp, *phi_block;
+               int i, max;
+               struct obstack ob;
+
+               obstack_init(&ob);
+
+               /* build member set not containing phi interferers */
+               DBG((dbgphi, 1, "Phi-1 class:\n"));
+               pu->count = 1; /*for the phi*/
+               for (n = pset_first(pc); n; n = pset_next(pc)) {
+                       if (!is_Phi(n) && !values_interfere(phi, n)) {
+                               DBG((dbgphi, 1, "\tAdding to members: %n\n", n));
+                               obstack_ptr_grow(&ob, n);
+                               pu->count++;
+                       } else {
+                               DBG((dbgphi, 1, "\tPhi interferer: %n\n", n));
+                               pset_insert_ptr(free_nodes, n);
+                       }
+               }
+               tmp = obstack_finish(&ob);
+               pu->members = malloc(pu->count * sizeof(*pu->members));
+               pu->members[0] = phi;
+               memcpy(&pu->members[1], tmp, (pu->count-1) * sizeof(*tmp));
+
+               /* init of colors array */
+               pu->colors = malloc(pu->count * sizeof(*pu->colors));
+
+               /* live-in analysis */
+               /* HINT: It is possible that a node occurs twice as arg of a phi,
+                * one time being live-in, and another time not being live-in.
+                */
+               pu->is_live_in = calloc(pu->count, sizeof(*pu->is_live_in));
+               phi_block = get_nodes_block(phi);
+               for (i = 0, max = get_irn_arity(phi); i < max; ++i) {
+                       int midx, o;
+               ir_node *arg, *block_ith_pred;
+
+               arg = get_irn_n(phi, i);
+                       block_ith_pred = get_nodes_block(get_irn_n(phi_block, i));
+
+                       /* find the arg in the members array */
+                       midx = -1;
+                       for (o = 0; o < pu->count; ++o)
+                               if (pu->members[o] == arg) {
+                                       midx = o;
+                                       break;
+                               }
+                       if (midx == -1)
+                               continue;
 
+                       if (is_live_in(block_ith_pred, arg)) {
+                               pu->is_live_in[midx] |= 1;
+                               DBG((dbgphi, 1, "\t%n is live-in in %n\n", arg, block_ith_pred));
+                       }
+               }
 
-       if (pu->phi_count == 1) {
-               ana_1_phi(pu);
+               obstack_free(&ob, NULL);
        } else {
+               DBG((dbgphi, 1, "Phi-n class:\n"));
                /* TODO */
-               // ana_n_phi(pu);
        }
 
+       DBG((dbgphi, 1, "\n"));
        return pu;
 }
 
-
+/**
+ * Deletes a phi unit
+ */
 static void free_phi_unit(phi_unit_t *pu) {
        DBG((dbgphi, 1, "\n"));
-       free(pu->members);
-       free(pu->used_cols);
-       free(pu->tgt_colors);
-       free(pu->live_info);
+       if (pu->phi_count == 1) {
+               free(pu->members);
+               free(pu->colors);
+               free(pu->is_live_in);
+       } else {
+               /* TODO */
+       }
        free(pu);
 }
 
 
 void be_phi_coalesce(pset *all_phi_classes) {
        pset *pc;
-       phi_unit_t *pu;
 
-       for (pc = (pset *)pset_first(all_phi_classes); pc; pc = (pset *)pset_next(all_phi_classes)) {
-               pu = new_phi_unit(pc);
-               coalesce(pu);
+       pinned_nodes = pset_new_ptr(256);
+       free_nodes = pset_new_ptr(64);
+
+       for (pc = pset_first(all_phi_classes); pc; pc = pset_next(all_phi_classes)) {
+               phi_unit_t *pu = new_phi_unit(pc);
+               if (pu->phi_count == 1)
+                       coalesce_1_phi(pu);
+               else
+                       coalesce_n_phi(pu);
                free_phi_unit(pu);
        }
+
+       del_pset(free_nodes);
+       del_pset(pinned_nodes);
 }