fixed output
[libfirm] / ir / opt / ldstopt.c
index 4ff2aeb..bb407e9 100644 (file)
 # include <string.h>
 #endif
 
-# include "irnode_t.h"
-# include "irgraph_t.h"
-# include "irmode_t.h"
-# include "iropt_t.h"
-# include "ircons_t.h"
-# include "irgmod.h"
-# include "irgwalk.h"
-# include "irvrfy.h"
-# include "tv_t.h"
-# include "dbginfo_t.h"
-# include "iropt_dbg.h"
-# include "irflag_t.h"
-# include "array.h"
-# include "irhooks.h"
-# include "irtools.h"
-# include "opt_polymorphy.h"
+#include "irnode_t.h"
+#include "irgraph_t.h"
+#include "irmode_t.h"
+#include "iropt_t.h"
+#include "ircons_t.h"
+#include "irgmod.h"
+#include "irgwalk.h"
+#include "irvrfy.h"
+#include "tv_t.h"
+#include "dbginfo_t.h"
+#include "iropt_dbg.h"
+#include "irflag_t.h"
+#include "array.h"
+#include "irhooks.h"
+#include "iredges.h"
+#include "irtools.h"
+#include "opt_polymorphy.h"
 
 #ifdef DO_CACHEOPT
 #include "cacheopt/cachesim.h"
@@ -57,7 +58,7 @@ enum changes_t {
  * walker environment
  */
 typedef struct _walk_env_t {
-  struct obstack obst;         /**< list of all stores */
+  struct obstack obst;          /**< list of all stores */
   unsigned changes;             /**< a bitmask of graph changes */
 } walk_env_t;
 
@@ -68,9 +69,7 @@ enum ldst_flags_t {
   LDST_VISITED = 1              /**< if set, this Load/Store is already visited */
 };
 
-/**
- * a Load/Store info
- */
+/** A Load/Store info. */
 typedef struct _ldst_info_t {
   ir_node  *projs[MAX_PROJ];    /**< list of Proj's of this node */
   ir_node  *exc_block;          /**< the exception block if available */
@@ -80,7 +79,7 @@ typedef struct _ldst_info_t {
 } ldst_info_t;
 
 /**
- * flags for control flow
+ * flags for control flow.
  */
 enum block_flags_t {
   BLOCK_HAS_COND = 1,      /**< Block has conditional control flow */
@@ -88,7 +87,7 @@ enum block_flags_t {
 };
 
 /**
- * a Block info
+ * a Block info.
  */
 typedef struct _block_info_t {
   unsigned flags;               /**< flags for the block */
@@ -104,8 +103,7 @@ static unsigned master_visited = 0;
 /**
  * get the Load/Store info of a node
  */
-static ldst_info_t *get_ldst_info(ir_node *node, walk_env_t *env)
-{
+static ldst_info_t *get_ldst_info(ir_node *node, walk_env_t *env) {
   ldst_info_t *info = get_irn_link(node);
 
   if (! info) {
@@ -171,8 +169,8 @@ static unsigned update_exc(ldst_info_t *info, ir_node *block, int pos)
   return 0;
 }
 
-#define get_irn_out_n(node)     (unsigned)PTR_TO_INT(get_irn_link(node))
-#define set_irn_out_n(node, n)  set_irn_link(adr, INT_TO_PTR(n))
+/** Return the number of uses of an address node */
+#define get_irn_n_uses(adr)     get_irn_n_edges(adr)
 
 /**
  * walker, collects all Load/Store/Proj nodes
@@ -200,8 +198,6 @@ static void collect_nodes(ir_node *node, void *env)
 
       if ((ldst_info->flags & LDST_VISITED) == 0) {
         adr = get_Load_ptr(pred);
-        set_irn_out_n(adr, get_irn_out_n(adr) + 1);
-
         ldst_info->flags |= LDST_VISITED;
       }
 
@@ -225,8 +221,6 @@ static void collect_nodes(ir_node *node, void *env)
 
       if ((ldst_info->flags & LDST_VISITED) == 0) {
         adr = get_Store_ptr(pred);
-        set_irn_out_n(adr, get_irn_out_n(adr) + 1);
-
         ldst_info->flags |= LDST_VISITED;
       }
 
@@ -244,7 +238,7 @@ static void collect_nodes(ir_node *node, void *env)
       }
     }
   }
-  else if (op == op_Block) { /* check, if it's an exception block */
+  else if (op == op_Block) {
     int i;
 
     for (i = get_Block_n_cfgpreds(node) - 1; i >= 0; --i) {
@@ -261,7 +255,7 @@ static void collect_nodes(ir_node *node, void *env)
       bl_info    = get_block_info(pred_block, wenv);
 
       if (is_fragile_op(pred))
-             bl_info->flags |= BLOCK_HAS_EXC;
+        bl_info->flags |= BLOCK_HAS_EXC;
       else if (is_irn_forking(pred))
         bl_info->flags |= BLOCK_HAS_COND;
 
@@ -394,6 +388,52 @@ static compound_graph_path *get_accessed_path(ir_node *ptr) {
   return rec_get_accessed_path(ptr, 0);
 }
 
+/* forward */
+static void reduce_adr_usage(ir_node *ptr);
+
+/**
+ * Update a Load that may lost it's usage.
+ */
+static void handle_load_update(ir_node *load) {
+  ldst_info_t *info = get_irn_link(load);
+
+  /* do NOT touch volatile loads for now */
+  if (get_Load_volatility(load) == volatility_is_volatile)
+    return;
+
+  if (! info->projs[pn_Load_res] && ! info->projs[pn_Load_X_except]) {
+    ir_node *ptr = get_Load_ptr(load);
+    ir_node *mem = get_Load_mem(load);
+
+    /* a Load which value is neither used nor exception checked, remove it */
+    exchange(info->projs[pn_Load_M], mem);
+    exchange(load, new_Bad());
+    reduce_adr_usage(ptr);
+  }
+}
+
+/**
+ * A Use of an address node is vanished. Check if this was a Proj
+ * node and update the counters.
+ */
+static void reduce_adr_usage(ir_node *ptr) {
+  if (is_Proj(ptr)) {
+    if (get_irn_n_edges(ptr) <= 0) {
+      /* this Proj is dead now */
+      ir_node *pred = get_Proj_pred(ptr);
+      opcode code = get_irn_opcode(pred);
+
+      if (code == iro_Load) {
+        ldst_info_t *info = get_irn_link(pred);
+        info->projs[get_Proj_proj(ptr)] = NULL;
+
+        /* this node lost it's result proj, handle that */
+        handle_load_update(pred);
+      }
+    }
+  }
+}
+
 /**
  * Follow the memory chain as long as there are only Loads
  * and try to replace current Load or Store by a previous one.
@@ -452,6 +492,8 @@ static unsigned follow_Load_chain(ir_node *load, ir_node *curr) {
         if (info->projs[pn_Load_res])
           exchange(info->projs[pn_Load_res], value);
 
+        exchange(load, new_Bad());
+        reduce_adr_usage(ptr);
         return res | DF_CHANGED;
       }
     }
@@ -496,6 +538,8 @@ static unsigned follow_Load_chain(ir_node *load, ir_node *curr) {
           res |= CF_CHANGED;
         }
 
+        exchange(load, new_Bad());
+        reduce_adr_usage(ptr);
         return res |= DF_CHANGED;
       }
     }
@@ -530,7 +574,6 @@ static unsigned follow_Load_chain(ir_node *load, ir_node *curr) {
 static unsigned optimize_load(ir_node *load)
 {
   ldst_info_t *info = get_irn_link(load);
-  ir_mode *load_mode = get_Load_mode(load);
   ir_node *mem, *ptr, *new_node;
   entity *ent;
   unsigned res = 0;
@@ -590,6 +633,8 @@ static unsigned optimize_load(ir_node *load)
     /* a Load which value is neither used nor exception checked, remove it */
     exchange(info->projs[pn_Load_M], mem);
 
+    exchange(load, new_Bad());
+    reduce_adr_usage(ptr);
     return res | DF_CHANGED;
   }
 
@@ -607,6 +652,9 @@ static unsigned optimize_load(ir_node *load)
     }
     if (info->projs[pn_Load_res])
       exchange(info->projs[pn_Load_res], new_node);
+
+    exchange(load, new_Bad());
+    reduce_adr_usage(ptr);
     return res | DF_CHANGED;
   }
 
@@ -640,7 +688,6 @@ static unsigned optimize_load(ir_node *load)
           exchange(info->projs[pn_Load_M], mem);
           res |= DF_CHANGED;
         }
-
         /* no result :-) */
         if (info->projs[pn_Load_res]) {
           if (is_atomic_entity(ent)) {
@@ -648,9 +695,12 @@ static unsigned optimize_load(ir_node *load)
 
             DBG_OPT_RC(load, c);
             exchange(info->projs[pn_Load_res], c);
-            return DF_CHANGED | res;
+            res |= DF_CHANGED;
           }
         }
+        exchange(load, new_Bad());
+        reduce_adr_usage(ptr);
+        return res;
       }
       else if (variability_constant == get_entity_variability(ent)) {
         compound_graph_path *path = get_accessed_path(ptr);
@@ -683,8 +733,11 @@ static unsigned optimize_load(ir_node *load)
           }
           if (info->projs[pn_Load_res]) {
             exchange(info->projs[pn_Load_res], copy_const_value(get_irn_dbg_info(load), c));
-            return res | DF_CHANGED;
+            res |= DF_CHANGED;
           }
+          exchange(load, new_Bad());
+          reduce_adr_usage(ptr);
+          return res;
         }
         else {
           /*  We can not determine a correct access path.  E.g., in jack, we load
@@ -702,7 +755,7 @@ static unsigned optimize_load(ir_node *load)
 
   /* Check, if the address of this load is used more than once.
    * If not, this load cannot be removed in any case. */
-  if (get_irn_out_n(ptr) <= 1)
+  if (get_irn_n_uses(ptr) <= 1)
     return res;
 
   /*
@@ -752,6 +805,8 @@ static unsigned follow_Load_chain_for_Store(ir_node *store, ir_node *curr) {
       if (get_Store_volatility(pred) != volatility_is_volatile && !pred_info->projs[pn_Store_X_except]) {
         DBG_OPT_WAW(pred, store);
         exchange( pred_info->projs[pn_Store_M], get_Store_mem(pred) );
+        exchange(pred, new_Bad());
+        reduce_adr_usage(ptr);
         return DF_CHANGED;
       }
     }
@@ -764,6 +819,8 @@ static unsigned follow_Load_chain_for_Store(ir_node *store, ir_node *curr) {
       if (! info->projs[pn_Store_X_except]) {
         DBG_OPT_WAR(store, pred);
         exchange( info->projs[pn_Store_M], mem );
+        exchange(store, new_Bad());
+        reduce_adr_usage(ptr);
         return DF_CHANGED;
       }
     }
@@ -796,7 +853,6 @@ static unsigned follow_Load_chain_for_Store(ir_node *store, ir_node *curr) {
  */
 static unsigned optimize_store(ir_node *store)
 {
-  ldst_info_t *info = get_irn_link(store);
   ir_node *ptr, *mem;
 
   if (get_Store_volatility(store) == volatility_is_volatile)
@@ -806,7 +862,7 @@ static unsigned optimize_store(ir_node *store)
 
   /* Check, if the address of this load is used more than once.
    * If not, this load cannot be removed in any case. */
-  if (get_irn_out_n(ptr) <= 1)
+  if (get_irn_n_uses(ptr) <= 1)
     return 0;
 
   mem = get_Store_mem(store);
@@ -823,10 +879,10 @@ static unsigned optimize_store(ir_node *store)
  *
  *   val1   val2   val3          val1  val2  val3
  *    |      |      |               \    |    /
- *   Str    Str    Str               \   |   /
+ *  Store  Store  Store              \   |   /
  *      \    |    /                   PhiData
  *       \   |   /                       |
- *        \  |  /                       Str
+ *        \  |  /                      Store
  *          PhiM
  *
  * @endverbatim
@@ -835,13 +891,12 @@ static unsigned optimize_store(ir_node *store)
  *
  * This is only possible if the predecessor blocks have only one successor.
  */
-static unsigned optimize_phi(ir_node *phi, void *env)
+static unsigned optimize_phi(ir_node *phi, walk_env_t *wenv)
 {
-  walk_env_t *wenv = env;
   int i, n;
-  ir_node *store, *old_store, *ptr, *block, *phiM, *phiD, *exc, *projM;
+  ir_node *store, *old_store, *ptr, *block, *phi_block, *phiM, *phiD, *exc, *projM;
   ir_mode *mode;
-  ir_node **inM, **inD;
+  ir_node **inM, **inD, **stores;
   int *idx;
   dbg_info *db = NULL;
   ldst_info_t *info;
@@ -861,13 +916,20 @@ static unsigned optimize_phi(ir_node *phi, void *env)
   if (get_irn_op(store) != op_Store)
     return 0;
 
+  block = get_nodes_block(store);
+
   /* abort on dead blocks */
-  if (is_Block_dead(get_nodes_block(store)))
+  if (is_Block_dead(block))
+    return 0;
+
+  /* check if the block is post dominated by Phi-block
+     and has no exception exit */
+  bl_info = get_irn_link(block);
+  if (bl_info->flags & BLOCK_HAS_EXC)
     return 0;
 
-  /* check if the block has only one successor */
-  bl_info = get_irn_link(get_nodes_block(store));
-  if (bl_info->flags)
+  phi_block = get_nodes_block(phi);
+  if (! block_postdominates(phi_block, block))
     return 0;
 
   /* this is the address of the store */
@@ -892,12 +954,18 @@ static unsigned optimize_phi(ir_node *phi, void *env)
       return 0;
 
     /* abort on dead blocks */
-    if (is_Block_dead(get_nodes_block(store)))
+    block = get_nodes_block(pred);
+    if (is_Block_dead(block))
       return 0;
 
-    /* check if the block has only one successor */
-    bl_info = get_irn_link(get_nodes_block(store));
-    if (bl_info->flags)
+    /* check if the block is post dominated by Phi-block
+       and has no exception exit. Note that block must be different from
+          Phi-block, else we would move a Store from end End of a block to its
+          Start... */
+    bl_info = get_irn_link(block);
+    if (bl_info->flags & BLOCK_HAS_EXC)
+      return 0;
+    if (block == phi_block || ! block_postdominates(phi_block, block))
       return 0;
   }
 
@@ -918,17 +986,41 @@ static unsigned optimize_phi(ir_node *phi, void *env)
    * Is only allowed if the predecessor blocks have only one successor.
    */
 
-  /* first step: collect all inputs */
+  NEW_ARR_A(ir_node *, stores, n);
   NEW_ARR_A(ir_node *, inM, n);
   NEW_ARR_A(ir_node *, inD, n);
   NEW_ARR_A(int, idx, n);
 
+  /* Prepare: Collect all Store nodes.  We must do this
+     first because we otherwise may loose a store when exchanging its
+     memory Proj.
+   */
+  for (i = 0; i < n; ++i)
+    stores[i] = skip_Proj(get_Phi_pred(phi, i));
+
+  /* Prepare: Skip the memory Proj: we need this in the case some stores
+     are cascaded.
+     Beware: One Store might be included more than once in the stores[]
+     list, so we must prevent to do the exchange more than once.
+   */
   for (i = 0; i < n; ++i) {
-    ir_node *pred = skip_Proj(get_Phi_pred(phi, i));
-    info = get_irn_link(pred);
+    ir_node *store = stores[i];
+    ir_node *proj_m;
+
+    info = get_irn_link(store);
+    proj_m = info->projs[pn_Store_M];
 
-    inM[i] = get_Store_mem(pred);
-    inD[i] = get_Store_value(pred);
+    if (is_Proj(proj_m) && get_Proj_pred(proj_m) == store)
+      exchange(proj_m, get_Store_mem(store));
+  }
+
+  /* first step: collect all inputs */
+  for (i = 0; i < n; ++i) {
+    ir_node *store = stores[i];
+    info = get_irn_link(store);
+
+    inM[i] = get_Store_mem(store);
+    inD[i] = get_Store_value(store);
     idx[i] = info->exc_idx;
   }
   block = get_nodes_block(phi);
@@ -993,7 +1085,7 @@ static void do_load_store_optimize(ir_node *n, void *env)
     break;
 
   case iro_Phi:
-    wenv->changes |= optimize_phi(n, env);
+    wenv->changes |= optimize_phi(n, wenv);
 
   default:
     ;
@@ -1014,6 +1106,11 @@ void optimize_load_store(ir_graph *irg)
   if (! get_opt_redundant_loadstore())
     return;
 
+  edges_assure(irg);
+
+  /* for Phi optimization post-dominators are needed ... */
+  assure_postdoms(irg);
+
   obstack_init(&env.obst);
   env.changes = 0;
 
@@ -1028,12 +1125,13 @@ void optimize_load_store(ir_graph *irg)
 
   /* Handle graph state */
   if (env.changes) {
-    if (get_irg_outs_state(current_ir_graph) == outs_consistent)
-      set_irg_outs_inconsistent(current_ir_graph);
+    if (get_irg_outs_state(irg) == outs_consistent)
+      set_irg_outs_inconsistent(irg);
   }
 
   if (env.changes & CF_CHANGED) {
-    /* is this really needed: Yes, control flow changed, block might get Bad. */
-    set_irg_doms_inconsistent(current_ir_graph);
+    /* is this really needed: Yes, control flow changed, block might
+       have Bad() predecessors. */
+    set_irg_doms_inconsistent(irg);
   }
 }