- implemented apply phase
[libfirm] / ir / opt / gvn_pre.c
index a9498d8..798aa36 100644 (file)
@@ -25,9 +25,7 @@
  * @version $Id$
  * @summary
  */
-#ifdef HAVE_CONFIG_H
-# include "config.h"
-#endif
+#include "config.h"
 
 #include "irflag.h"
 #include "irdom.h"
@@ -40,6 +38,7 @@
 #include "irnodemap.h"
 #include "irnodeset.h"
 #include "iredges.h"
+#include "iropt_dbg.h"
 #include "debug.h"
 
 #include "irgraph_t.h"
@@ -53,8 +52,9 @@ typedef struct block_info {
        ir_valueset_t     *antic_in;  /**< The Antic_in set for a block. */
        ir_valueset_t     *new_set;   /**< The set of all new values for a block. */
        ir_node           *avail;     /**< The get_map(avail, block) result. */
-       int               not_found;  /**< Non-zero, if avail was not found in this block. */
+       ir_node           *block;     /**< The Block of the block info. */
        struct block_info *next;      /**< Links all entries, so we can recover the sets easily. */
+       int               not_found;  /**< Non-zero, if avail was not found in this block. */
 } block_info;
 
 /**
@@ -66,6 +66,7 @@ typedef struct elim_pair {
        ir_node *old_node;      /**< The old node that will be replaced. */
        ir_node *new_node;      /**< The new node. */
        struct elim_pair *next; /**< Links all entries in a list. */
+       int     reason;         /**< The reason for the replacement. */
 } elim_pair;
 
 /** The environment for the GVN-PRE algorithm */
@@ -75,6 +76,7 @@ typedef struct pre_env {
        ir_node *end_block;     /**< The end block of the current graph */
        block_info *list;       /**< Links all block info entires for easier recovery. */
        elim_pair *pairs;       /**< A list of node pairs that must be eliminated. */
+       unsigned last_idx;      /**< last node index of "old" nodes, all higher indexes are newly created once. */
        char changes;           /**< Non-zero, if calculation of Antic_in has changed. */
        char first_iter;        /**< non-zero for first iteration */
 } pre_env;
@@ -102,7 +104,12 @@ static void value_union(ir_valueset_t *dst, ir_valueset_t *src)
 /* ----------  Functions for Values ---------- */
 
 /**
- * Add a node node representing the value value to the set.
+ * Add a node e representing the value v to the set.
+ *
+ * @param e  a node representing an expression
+ * @param v  a node representing a value
+ *
+ * @return the final value for the expression e
  */
 static ir_node *add(ir_node *e, ir_node *v)
 {
@@ -118,10 +125,15 @@ static ir_node *add(ir_node *e, ir_node *v)
        v = identify_remember(value_table, v);
        ir_nodemap_insert(&value_map, e, v);
        return v;
-}
+}  /* add */
 
 /**
  * Lookup a value in a value set.
+ *
+ * @param e  a node representing an expression
+ *
+ * @return a node representing the value or NULL if
+ *         the given expression is not available
  */
 static ir_node *lookup(ir_node *e)
 {
@@ -129,17 +141,22 @@ static ir_node *lookup(ir_node *e)
        if (value != NULL)
                return identify_remember(value_table, value);
        return NULL;
-}
+}  /* lookup */
 
 /**
- * Return the block info of a block
+ * Return the block info of a block.
+ *
+ * @param block  the block
  */
 static block_info *get_block_info(ir_node *block) {
        return get_irn_link(block);
-}
+}  /* get_block_info */
 
 /**
- * allocate a block info
+ * Allocate a block info for a block.
+ *
+ * @param block   the block
+ * @param env     the environment
  */
 static void alloc_blk_info(ir_node *block, pre_env *env) {
        block_info *info = obstack_alloc(env->obst, sizeof(*info));
@@ -150,13 +167,16 @@ static void alloc_blk_info(ir_node *block, pre_env *env) {
        info->antic_in  = ir_valueset_new(16);
        info->new_set   = NULL;
        info->avail     = NULL;
-       info->not_found = 0;
+       info->block     = block;
        info->next      = env->list;
        env->list       = info;
-}
+       info->not_found = 0;
+}  /* alloc_blk_info */
 
 /**
  * Returns non-zero if a node is movable and a possible candidate for PRE.
+ *
+ * @param n  the node
  */
 static int is_nice_value(ir_node *n) {
        ir_mode *mode;
@@ -178,6 +198,10 @@ static int is_nice_value(ir_node *n) {
 #ifdef DEBUG_libfirm
 /**
  * Dump a value set.
+ *
+ * @param set    the set to dump
+ * @param txt    a text to describe the set
+ * @param block  the owner block of the set
  */
 static void dump_value_set(ir_valueset_t *set, char *txt, ir_node *block) {
        ir_valueset_iterator_t iter;
@@ -245,6 +269,9 @@ static void topo_walker(ir_node *irn, void *ctx) {
  * Precondition:
  *  This function must be called in the top-down dominance order:
  *  Then, it computes Leader(Nodes(block)) instead of Nodes(block) !
+ *
+ * @param block   the block
+ * @param ctx     walker context
  */
 static void compute_avail_top_down(ir_node *block, void *ctx)
 {
@@ -274,52 +301,60 @@ static void compute_avail_top_down(ir_node *block, void *ctx)
        value_union(info->avail_out, info->exp_gen);
 
        dump_value_set(info->avail_out, "Avail_out", block);
-}
+}  /* compute_avail_top_down */
 
 /**
  * check if a node n is clean in block block.
+ *
+ * @param n      the node
+ * @param block  the block
+ * @param set    a value set, containing the already processed predecessors
  */
-static int _is_clean(ir_node *n, ir_node *block)
-{
+static int is_clean_in_block(ir_node *n, ir_node *block, ir_valueset_t *set) {
        int i;
 
-       if (get_nodes_block(n) != block)
-               return 1;
        if (is_Phi(n))
                return 1;
 
-       if (irn_visited(n))
+       if (! is_nice_value(n))
                return 0;
 
-       if (! is_nice_value(n))
-               goto bad;
        for (i = get_irn_arity(n) - 1; i >= 0; --i) {
                ir_node *pred = get_irn_n(n, i);
-               if (! _is_clean(pred, block))
-                       goto bad;
+               ir_node *value;
+
+               if (get_nodes_block(pred) != block)
+                       continue;
+
+               if (is_Phi(pred))
+                       continue;
+
+               if (! is_nice_value(pred))
+                       return 0;
+
+               value = lookup(pred);
+               if (! value)
+                       return 0;
+               if (! ir_valueset_lookup(set, value))
+                       return 0;
        }
        return 1;
-bad:
-       mark_irn_visited(n);
-       return 0;
-}
-
-/**
- * check if a node n is clean.
- */
-static int is_clean(ir_node *n) {
-       int res = _is_clean(n, get_nodes_block(n));
-       return res;
-}
+}  /* is_clean_in_block */
 
 /**
- * Implements phi_translate.
+ * Implements phi_translate. Translate a expression above a Phi.
+ *
+ * @param node        the node
+ * @param block       the block in which the node is translated
+ * @param pos         the input number of the destination block
+ * @param translated  the valueset containing the other already translated nodes
+ *
+ * @return a node representing the translated value
  */
-static ir_node *phi_translate(ir_node *node, ir_node *block, int pos, pre_env *env)
+static ir_node *phi_translate(ir_node *node, ir_node *block, int pos, ir_valueset_t *translated)
 {
-       ir_node *nn;
-       int i, arity;
-       struct obstack *old;
+       ir_node        *nn;
+       int            i, arity;
 
        if (is_Phi(node)) {
                if (get_nodes_block(node) == block) {
@@ -336,21 +371,19 @@ static ir_node *phi_translate(ir_node *node, ir_node *block, int pos, pre_env *e
        for (i = 0; i < arity; ++i) {
                ir_node *pred    = get_irn_intra_n(node, i);
                ir_node *leader  = lookup(pred);
+               ir_node *trans;
 
                leader = leader != NULL ? leader : pred;
-               if (is_Phi(leader) && get_nodes_block(pred) == block)
+               trans  = ir_valueset_lookup(translated, leader);
+
+               if ((trans != NULL && trans != leader) || (is_Phi(leader) && get_nodes_block(leader) == block))
                        break;
        }
        if (i >= arity) {
-               /* no Phi in the predecessors */
+               /* no translation needed */
                return node;
        }
 
-       /* Create a copy of the node in the pos'th predecessor block.
-          Use our environmental obstack, as these nodes are always
-          temporary. */
-       old = current_ir_graph->obst;
-       current_ir_graph->obst = env->obst;
        nn = new_ir_node(
                get_irn_dbg_info(node),
                current_ir_graph,
@@ -367,19 +400,27 @@ static ir_node *phi_translate(ir_node *node, ir_node *block, int pos, pre_env *e
        for (i = 0; i < arity; ++i) {
                ir_node *pred    = get_irn_intra_n(node, i);
                ir_node *leader  = lookup(pred);
+               ir_node *trans;
 
                leader = leader != NULL ? leader : pred;
-               if (is_Phi(leader) && get_irn_intra_n(pred, -1) == block)
-                       set_irn_n(nn, i, get_Phi_pred(leader, pos));
+               trans  = ir_valueset_lookup(translated, leader);
+               if (trans == NULL)
+                       trans = leader;
+
+               if (is_Phi(trans) && get_nodes_block(trans) == block)
+                       set_irn_n(nn, i, get_Phi_pred(trans, pos));
                else
-                       set_irn_n(nn, i, leader);
+                       set_irn_n(nn, i, trans);
        }
-       current_ir_graph->obst = old;
+       nn = optimize_node(nn);
        return nn;
 }  /* phi_translate */
 
 /**
- * computes Antic_in(block):
+ * Block-walker, computes Antic_in(block).
+ *
+ * @param block  the block
+ * @param ctx    the walker environment
  */
 static void compute_antic(ir_node *block, void *ctx) {
        pre_env    *env = ctx;
@@ -429,9 +470,9 @@ static void compute_antic(ir_node *block, void *ctx) {
                        /* translate into list: we cannot insert into a set we iterate
                         * and succ might be equal to block for endless loops */
                        foreach_valueset(succ_info->antic_in, value, expr, iter) {
-                               ir_node *trans = phi_translate(expr, succ, pos, env);
+                               ir_node *trans = phi_translate(expr, succ, pos, info->antic_in);
 
-                               if (is_clean(trans))
+                               if (is_clean_in_block(trans, block, info->antic_in))
                                        ir_valueset_insert(info->antic_in, value, trans);
                        }
                } else { /* n_succ > 1 */
@@ -458,7 +499,8 @@ static void compute_antic(ir_node *block, void *ctx) {
                                if (i >= n_succ) {
                                        /* we found a value that is common in all Antic_in(succ(b)),
                                            put it in Antic_in(b) if the value is NOT already represented. */
-                                       ir_valueset_insert(info->antic_in, value, expr);
+                                       if (is_clean_in_block(expr, block, info->antic_in))
+                                               ir_valueset_insert(info->antic_in, value, expr);
                                }
                        }
                }
@@ -486,6 +528,9 @@ static void compute_antic(ir_node *block, void *ctx) {
  *     2c. Insert a new Phi merging the values of the predecessors.
  *     2d. Insert the new Phi, and the new expressions, into the
  *         NEW_SETS set.
+ *
+ * @param block  the block
+ * @param ctx    the walker environment
  */
 static void insert_nodes(ir_node *block, void *ctx)
 {
@@ -550,7 +595,7 @@ static void insert_nodes(ir_node *block, void *ctx)
                        if (is_Bad(pred_blk))
                                continue;
 
-                       e_prime = phi_translate(expr, block, pos, env);
+                       e_prime = phi_translate(expr, block, pos, curr_info->avail_out);
                        v_prime = lookup(e_prime);
                        if (v_prime == NULL)
                                v_prime = value;
@@ -580,11 +625,11 @@ static void insert_nodes(ir_node *block, void *ctx)
                /* If it's not the same value already existing along every predecessor, and
                   it's defined by some predecessor, it is partially redundant. */
                if (! all_same && by_some) {
-                       ir_node *phi, **in;
+                       ir_node *phi, *l, **in;
 
                        DB((dbg, LEVEL_1, "Partial redundant %+F from block %+F found\n", expr, block));
 
-                       in = xmalloc(arity * sizeof(*in));
+                       in = XMALLOCN(ir_node*, arity);
                        /* for all predecessor blocks */
                        for (pos = 0; pos < arity; ++pos) {
                                ir_node *pred_blk = get_Block_cfgpred_block(block, pos);
@@ -614,7 +659,7 @@ static void insert_nodes(ir_node *block, void *ctx)
                                                                get_irn_in(pred) + 1);
                                                        copy_node_attr(pred, nn);
 
-                                                       DB((dbg, LEVEL_2, "New node %+F in block %+F created\n", nn, pred_blk));
+                                                       DB((dbg, LEVEL_1, "New node %+F in block %+F created\n", nn, pred_blk));
                                                        proj_pred = nn;
                                                }
                                                mode = get_irn_mode(e_prime);
@@ -630,19 +675,27 @@ static void insert_nodes(ir_node *block, void *ctx)
                                                        set_Proj_pred(nn, proj_pred);
                                                }
 
-                                               DB((dbg, LEVEL_2, "New node %+F in block %+F created\n", nn, pred_blk));
-                                               ir_valueset_insert(pred_info->avail_out, add(nn, lookup(expr)), nn);
+                                               DB((dbg, LEVEL_1, "New node %+F in block %+F created\n", nn, pred_blk));
+                                               l = lookup(expr);
+                                               if (l == NULL) {
+                                                       l = add(expr, value);
+                                               }
+                                               ir_valueset_insert(pred_info->avail_out, add(nn, l), nn);
                                                pred_info->avail = nn;
                                        }
                                }
                                in[pos] = pred_info->avail;
                        }  /* for */
                        phi = new_r_Phi(current_ir_graph, block, arity, in, mode);
-                       value = add(phi, lookup(expr));
+                       l = lookup(expr);
+                       if (l == NULL) {
+                               l = add(expr, value);
+                       }
+                       value = add(phi, l);
                        ir_valueset_replace(curr_info->avail_out, value, phi);
                        ir_valueset_insert(curr_info->new_set, value, phi);
                        free(in);
-                       DB((dbg, LEVEL_2, "New %+F for redundant %+F created\n", phi, expr));
+                       DB((dbg, LEVEL_1, "New %+F for redundant %+F created\n", phi, expr));
                        ir_valueset_remove_iterator(curr_info->antic_in, &iter);
                        env->changes |= 1;
                }  /* if */
@@ -654,6 +707,9 @@ static void insert_nodes(ir_node *block, void *ctx)
  *
  * We cannot do the changes right here, as this would change
  * the hash values of the nodes in the avail_out set!
+ *
+ * @param irn  the node
+ * @param ctx  the walker environment
  */
 static void eliminate(ir_node *irn, void *ctx) {
        pre_env *env = ctx;
@@ -672,6 +728,7 @@ static void eliminate(ir_node *irn, void *ctx) {
                                p->old_node = irn;
                                p->new_node = expr;
                                p->next     = env->pairs;
+                               p->reason   = get_irn_idx(expr) >= env->last_idx ? FS_OPT_GVN_PARTLY : FS_OPT_GVN_FULLY;
                                env->pairs  = p;
                        }
                }
@@ -681,11 +738,16 @@ static void eliminate(ir_node *irn, void *ctx) {
 /**
  * Do all the recorded changes and optimize
  * newly created Phi's.
+ *
+ * @param pairs  list of elimination pairs
  */
 static void eliminate_nodes(elim_pair *pairs) {
        elim_pair *p;
 
        for (p = pairs; p != NULL; p = p->next) {
+               /* might be already changed */
+               p->new_node = skip_Id(p->new_node);
+
                DB((dbg, LEVEL_2, "Replacing %+F by %+F\n", p->old_node, p->new_node));
                /*
                 * PRE tends to create Phi(self, self, ... , x, self, self, ...)
@@ -706,9 +768,12 @@ static void eliminate_nodes(elim_pair *pairs) {
                                        res = pred;
                                }
                        }
-                       if (res)
+                       if (res) {
+                               exchange(p->new_node, res);
                                p->new_node = res;
+                       }
                }
+               DBG_OPT_GVN_PRE(p->old_node, p->new_node, p->reason);
                exchange(p->old_node, p->new_node);
        }
 }  /* eliminate_nodes */
@@ -723,15 +788,16 @@ static void eliminate_nodes(elim_pair *pairs) {
  */
 void do_gvn_pre(ir_graph *irg)
 {
-       struct obstack obst;
-       pre_env a_env;
+       struct obstack       obst;
+       pre_env              a_env;
        optimization_state_t state;
-       block_info *bl_info;
-       unsigned antic_iter, insert_iter;
+       block_info           *bl_info;
+       unsigned             antic_iter, insert_iter;
+       ir_node              *value, *expr;
 
        /* register a debug mask */
        FIRM_DBG_REGISTER(dbg, "firm.opt.gvn_pre");
-       firm_dbg_set_mask(dbg, SET_LEVEL_2);
+       firm_dbg_set_mask(dbg, 3);
 
        /* edges will crash if enabled due to our allocate on other obstack trick */
        edges_deactivate(irg);
@@ -761,16 +827,26 @@ void do_gvn_pre(ir_graph *irg)
 
        /*
         * Switch on GCSE. We need it to correctly compute
-        * the leader of a node by hashing.
+        * the value of a node, which is independent from
+        * its block.
         */
        save_optimization_state(&state);
        set_opt_global_cse(1);
 
-       DB((dbg, LEVEL_1, "Doing GVN-PRE for %e\n", get_irg_entity(irg)));
+       DB((dbg, LEVEL_1, "Doing GVN-PRE for %+F\n", irg));
 
        /* allocate block info for all blocks */
-       irg_walk_blkwise_graph(irg, NULL, topo_walker, &a_env);
+       irg_walk_blkwise_dom_top_down(irg, NULL, topo_walker, &a_env);
+
+       /* clean the exp_gen set. Doing this here saves the cleanup in the iteration. */
+       for (bl_info = a_env.list; bl_info != NULL; bl_info = bl_info->next) {
+               ir_valueset_iterator_t iter;
 
+               foreach_valueset(bl_info->exp_gen, value, expr, iter) {
+                       if (!is_clean_in_block(expr, bl_info->block, bl_info->exp_gen))
+                               ir_valueset_remove_iterator(bl_info->exp_gen, &iter);
+               }
+       }
        /* compute the available value sets for all blocks */
        dom_tree_walk_irg(irg, compute_avail_top_down, NULL, &a_env);
 
@@ -783,7 +859,6 @@ void do_gvn_pre(ir_graph *irg)
        do {
                DB((dbg, LEVEL_1, "Antic_in Iteration %d starts ...\n", ++antic_iter));
                a_env.changes = 0;
-               //irg_block_walk_graph(irg, compute_antic, NULL, &a_env);
                postdom_tree_walk_irg(irg, compute_antic, NULL, &a_env);
                a_env.first_iter = 0;
                DB((dbg, LEVEL_1, "------------------------\n"));
@@ -791,6 +866,7 @@ void do_gvn_pre(ir_graph *irg)
 
        /* compute redundant expressions */
        insert_iter = 0;
+       a_env.last_idx = get_irg_last_idx(irg);
        do {
                DB((dbg, LEVEL_1, "Insert Iteration %d starts ...\n", ++insert_iter));
                a_env.changes = 0;
@@ -822,6 +898,5 @@ void do_gvn_pre(ir_graph *irg)
        if (a_env.pairs) {
                set_irg_outs_inconsistent(irg);
                set_irg_loopinfo_inconsistent(irg);
-
        }
 }  /* do_gvn_pre */