BugFix: added forgotten typedef
[libfirm] / ir / opt / ldstopt.c
index 24dab80..2e9aeea 100644 (file)
@@ -76,6 +76,7 @@ typedef struct _ldst_info_t {
   ir_node  *exc_block;          /**< the exception block if available */
   int      exc_idx;             /**< predecessor index in the exception block */
   unsigned flags;               /**< flags */
+  unsigned visited;             /**< visited counter for breaking loops */
 } ldst_info_t;
 
 /**
@@ -83,7 +84,7 @@ typedef struct _ldst_info_t {
  */
 enum block_flags_t {
   BLOCK_HAS_COND = 1,      /**< Block has conditional control flow */
-  BLOCK_HAS_EXC  = 2       /**< Block has exceptionl control flow */
+  BLOCK_HAS_EXC  = 2       /**< Block has exceptional control flow */
 };
 
 /**
@@ -93,6 +94,13 @@ typedef struct _block_info_t {
   unsigned flags;               /**< flags for the block */
 } block_info_t;
 
+/** the master visited flag for loop detection. */
+static unsigned master_visited = 0;
+
+#define INC_MASTER()       ++master_visited
+#define MARK_NODE(info)    (info)->visited = master_visited
+#define NODE_VISITED(info) (info)->visited >= master_visited
+
 /**
  * get the Load/Store info of a node
  */
@@ -148,7 +156,11 @@ static unsigned update_projs(ldst_info_t *info, ir_node *proj)
 }
 
 /**
- * update the exception block info for a Load/Store
+ * update the exception block info for a Load/Store node.
+ *
+ * @param info   the load/store info struct
+ * @param block  the exception handler block for this load/store
+ * @param pos    the control flow input of the block
  */
 static unsigned update_exc(ldst_info_t *info, ir_node *block, int pos)
 {
@@ -165,7 +177,7 @@ static unsigned update_exc(ldst_info_t *info, ir_node *block, int pos)
 /**
  * walker, collects all Load/Store/Proj nodes
  *
- * walks form Start -> End
+ * walks from Start -> End
  */
 static void collect_nodes(ir_node *node, void *env)
 {
@@ -233,9 +245,9 @@ static void collect_nodes(ir_node *node, void *env)
     }
   }
   else if (op == op_Block) { /* check, if it's an exception block */
-    int i, n;
+    int i;
 
-    for (i = 0, n = get_Block_n_cfgpreds(node); i < n; ++i) {
+    for (i = get_Block_n_cfgpreds(node) - 1; i >= 0; --i) {
       ir_node      *pred_block;
       block_info_t *bl_info;
 
@@ -243,15 +255,15 @@ static void collect_nodes(ir_node *node, void *env)
 
       /* ignore Bad predecessors, they will be removed later */
       if (is_Bad(pred))
-       continue;
+        continue;
 
       pred_block = get_nodes_block(pred);
       bl_info    = get_block_info(pred_block, wenv);
 
       if (is_fragile_op(pred))
-       bl_info->flags |= BLOCK_HAS_EXC;
-      else if (is_forking_op(pred))
-       bl_info->flags |= BLOCK_HAS_COND;
+             bl_info->flags |= BLOCK_HAS_EXC;
+      else if (is_irn_forking(pred))
+        bl_info->flags |= BLOCK_HAS_COND;
 
       if (get_irn_op(pred) == op_Load || get_irn_op(pred) == op_Store) {
         ldst_info = get_ldst_info(pred, wenv);
@@ -274,10 +286,10 @@ static entity *find_constant_entity(ir_node *ptr)
       return get_SymConst_entity(ptr);
     }
     else if (op == op_Sel) {
-      entity *ent = get_Sel_entity(ptr);
-      type *tp    = get_entity_owner(ent);
+      entity  *ent = get_Sel_entity(ptr);
+      ir_type *tp  = get_entity_owner(ent);
 
-      /* Do not fiddle about polymorphy. */
+      /* Do not fiddle with polymorphism. */
       if (is_Class_type(get_entity_owner(ent)) &&
           ((get_entity_n_overwrites(ent)    != 0) ||
            (get_entity_n_overwrittenby(ent) != 0)   ) )
@@ -409,16 +421,16 @@ static unsigned optimize_load(ir_node *load)
    * like x = new O; x->t;
    */
   if (info->projs[pn_Load_X_except]) {
-    if (get_irn_op(ptr) == op_Sel) {
+    if (is_Sel(ptr)) {
       ir_node *mem = get_Sel_mem(ptr);
 
       if (get_irn_op(skip_Proj(mem)) == op_Alloc) {
         /* ok, check the types */
-        entity *ent  = get_Sel_entity(ptr);
-        type *s_type = get_entity_type(ent);
-        type *a_type = get_Alloc_type(mem);
+        entity  *ent    = get_Sel_entity(ptr);
+        ir_type *s_type = get_entity_type(ent);
+        ir_type *a_type = get_Alloc_type(mem);
 
-        if (is_subclass_of(s_type, a_type)) {
+        if (is_SubClass_of(s_type, a_type)) {
           /* ok, condition met: there can't be an exception because
            * Alloc guarantees that enough memory was allocated */
 
@@ -452,7 +464,7 @@ static unsigned optimize_load(ir_node *load)
   }
 
   /* Load from a constant polymorphic field, where we can resolve
-     polymorphy. */
+     polymorphism. */
   new_node = transform_node_Load(load);
   if (new_node != load) {
     if (info->projs[pn_Load_M]) {
@@ -502,7 +514,7 @@ static unsigned optimize_load(ir_node *load)
         /* no result :-) */
         if (info->projs[pn_Load_res]) {
           if (is_atomic_entity(ent)) {
-            ir_node *c = copy_const_value(get_atomic_ent_value(ent));
+            ir_node *c = copy_const_value(get_irn_dbg_info(load), get_atomic_ent_value(ent));
 
             DBG_OPT_RC(load, c);
             exchange(info->projs[pn_Load_res], c);
@@ -540,7 +552,7 @@ static unsigned optimize_load(ir_node *load)
             res |= DF_CHANGED;
           }
           if (info->projs[pn_Load_res]) {
-            exchange(info->projs[pn_Load_res], copy_const_value(c));
+            exchange(info->projs[pn_Load_res], copy_const_value(get_irn_dbg_info(load), c));
             return res | DF_CHANGED;
           }
         }
@@ -563,21 +575,26 @@ static unsigned optimize_load(ir_node *load)
   if (get_irn_out_n(ptr) <= 1)
     return res;
 
-  /* follow the memory chain as long as there are only Loads
-   * and try to replace current Load or Store by a previous one
+  /*
+   * follow the memory chain as long as there are only Loads
+   * and try to replace current Load or Store by a previous one.
+   * Note that in unreachable loops it might happen that we reach
+   * load again, as well as we can fall into a cycle.
+   * We break such cycles using a special visited flag.
    */
-  for (pred = skip_Proj(mem); ; pred = skip_Proj(get_Load_mem(pred))) {
+  INC_MASTER();
+  for (pred = skip_Proj(mem); load != pred; pred = skip_Proj(get_Load_mem(pred))) {
+    ldst_info_t *pred_info = get_irn_link(pred);
+
     /*
      * BEWARE: one might think that checking the modes is useless, because
      * if the pointers are identical, they refer to the same object.
      * This is only true in strong typed languages, not in C were the following
-     * is possible a = *(type1 *)p; b = *(type2 *)p ...
+     * is possible a = *(ir_type1 *)p; b = *(ir_type2 *)p ...
      */
 
     if (get_irn_op(pred) == op_Store && get_Store_ptr(pred) == ptr &&
         get_irn_mode(get_Store_value(pred)) == load_mode) {
-      ldst_info_t *pred_info = get_irn_link(pred);
-
       /*
        * a Load immediately after a Store -- a read after write.
        * We may remove the Load, if both Load & Store does not have an exception handler
@@ -622,8 +639,6 @@ static unsigned optimize_load(ir_node *load)
        * hander because they would have exact the same exception...
        */
       if (! info->projs[pn_Load_X_except] || get_nodes_block(load) == get_nodes_block(pred)) {
-        ldst_info_t *pred_info = get_irn_link(pred);
-
         DBG_OPT_RAR(load, pred);
 
         if (pred_info->projs[pn_Load_res]) {
@@ -660,7 +675,12 @@ static unsigned optimize_load(ir_node *load)
     /* follow only Load chains */
     if (get_irn_op(pred) != op_Load)
       break;
-   }
+
+    /* check for cycles */
+    if (NODE_VISITED(pred_info))
+      break;
+    MARK_NODE(pred_info);
+  }
   return res;
 }
 
@@ -681,7 +701,7 @@ static unsigned optimize_store(ir_node *store)
    * BEWARE: one might think that checking the modes is useless, because
    * if the pointers are identical, they refer to the same object.
    * This is only true in strong typed languages, not is C were the following
-   * is possible *(type1 *)p = a; *(type2 *)p = b ...
+   * is possible *(ir_type1 *)p = a; *(ir_type2 *)p = b ...
    */
 
   ptr   = get_Store_ptr(store);
@@ -697,7 +717,8 @@ static unsigned optimize_store(ir_node *store)
   mode  = get_irn_mode(value);
 
   /* follow the memory chain as long as there are only Loads */
-  for (pred = skip_Proj(mem); ; pred = skip_Proj(get_Load_mem(pred))) {
+  INC_MASTER();
+  for (pred = skip_Proj(mem); pred != store; pred = skip_Proj(get_Load_mem(pred))) {
     ldst_info_t *pred_info = get_irn_link(pred);
 
     if (get_irn_op(pred) == op_Store && get_Store_ptr(pred) == ptr &&
@@ -730,26 +751,33 @@ static unsigned optimize_store(ir_node *store)
     /* follow only Load chains */
     if (get_irn_op(pred) != op_Load)
       break;
+
+    /* check for cycles */
+    if (NODE_VISITED(pred_info))
+      break;
+    MARK_NODE(pred_info);
   }
   return res;
 }
 
 /**
- * walker, optimizes Phi after Stores:
+ * walker, optimizes Phi after Stores to identical places:
  * Does the following optimization:
+ * @verbatim
  *
  *   val1   val2   val3          val1  val2  val3
  *    |      |      |               \    |    /
  *   Str    Str    Str               \   |   /
- *      \    |    /                     Phi
+ *      \    |    /                   PhiData
  *       \   |   /                       |
  *        \  |  /                       Str
- *          Phi
+ *          PhiM
  *
- * This removes the number of stores and allows for predicated execution.
- * Moves Stores back to the end of a function which may be bad
+ * @endverbatim
+ * This reduces the number of stores and allows for predicated execution.
+ * Moves Stores back to the end of a function which may be bad.
  *
- * Is only allowed if the predecessor blocks have only one successor.
+ * This is only possible if the predecessor blocks have only one successor.
  */
 static unsigned optimize_phi(ir_node *phi, void *env)
 {
@@ -781,7 +809,7 @@ static unsigned optimize_phi(ir_node *phi, void *env)
   if (is_Block_dead(get_nodes_block(store)))
     return 0;
 
-  /* check if the block has only one output */
+  /* check if the block has only one successor */
   bl_info = get_irn_link(get_nodes_block(store));
   if (bl_info->flags)
     return 0;
@@ -798,7 +826,7 @@ static unsigned optimize_phi(ir_node *phi, void *env)
     if (get_irn_op(pred) != op_Store)
       return 0;
 
-    if (mode != get_irn_mode(get_Store_value(pred)) || ptr != get_Store_ptr(pred))
+    if (ptr != get_Store_ptr(pred) || mode != get_irn_mode(get_Store_value(pred)))
       return 0;
 
     info = get_irn_link(pred);
@@ -811,7 +839,7 @@ static unsigned optimize_phi(ir_node *phi, void *env)
     if (is_Block_dead(get_nodes_block(store)))
       return 0;
 
-    /* check if the block has only one output */
+    /* check if the block has only one successor */
     bl_info = get_irn_link(get_nodes_block(store));
     if (bl_info->flags)
       return 0;
@@ -819,17 +847,17 @@ static unsigned optimize_phi(ir_node *phi, void *env)
 
   /*
    * ok, when we are here, we found all predecessors of a Phi that
-   * are Stores to the same address. That means whatever we do before
-   * we enter the block of the Phi, we do a Store.
-   * So, we can move the store to the current block:
+   * are Stores to the same address and size. That means whatever
+   * we do before we enter the block of the Phi, we do a Store.
+   * So, we can move the Store to the current block:
    *
    *   val1    val2    val3          val1  val2  val3
    *    |       |       |               \    |    /
    * | Str | | Str | | Str |             \   |   /
-   *      \     |     /                     Phi
+   *      \     |     /                   PhiData
    *       \    |    /                       |
    *        \   |   /                       Str
-   *           Phi
+   *           PhiM
    *
    * Is only allowed if the predecessor blocks have only one successor.
    */
@@ -885,14 +913,14 @@ static unsigned optimize_phi(ir_node *phi, void *env)
     res |= CF_CHANGED;
   }
 
-  /* sixt step: replace old Phi */
+  /* sixth step: replace old Phi */
   exchange(phi, projM);
 
   return res | DF_CHANGED;
 }
 
 /**
- * walker, collects all Load/Store/Proj nodes
+ * walker, do the optimizations
  */
 static void do_load_store_optimize(ir_node *n, void *env)
 {
@@ -927,13 +955,14 @@ void optimize_load_store(ir_graph *irg)
   assert(get_irg_pinned(irg) != op_pin_state_floats &&
     "LoadStore optimization needs pinned graph");
 
-  if (!get_opt_redundant_LoadStore())
+  if (! get_opt_redundant_loadstore())
     return;
 
   obstack_init(&env.obst);
   env.changes = 0;
 
   /* init the links, then collect Loads/Stores/Proj's in lists */
+  master_visited = 0;
   irg_walk_graph(irg, firm_clear_link, collect_nodes, &env);
 
   /* now we have collected enough information, optimize */
@@ -949,7 +978,6 @@ void optimize_load_store(ir_graph *irg)
 
   if (env.changes & CF_CHANGED) {
     /* is this really needed: Yes, control flow changed, block might get Bad. */
-    if (get_irg_dom_state(current_ir_graph) == dom_consistent)
-      set_irg_dom_inconsistent(current_ir_graph);
+    set_irg_doms_inconsistent(current_ir_graph);
   }
 }