Implemented cf optimizations.
[libfirm] / ir / ir / ircons.c
index 057de62..a974d91 100644 (file)
@@ -9,6 +9,8 @@
 **   by Goetz Lindenmaier
 */
 
+/* $Id$ */
+
 #ifdef HAVE_CONFIG_H
 # include <config.h>
 #endif
@@ -20,7 +22,7 @@
 # include "common.h"
 # include "irvrfy.h"
 # include "irop.h"
-# include "iropt.h"
+# include "iropt_t.h"
 # include "irgmod.h"
 # include "array.h"
 /* memset belongs to string.h */
@@ -91,6 +93,11 @@ new_r_Phi (ir_graph *irg, ir_node *block, int arity, ir_node **in, ir_mode *mode
 
   res = optimize (res);
   irn_vrfy (res);
+
+  /* Memory Phis in endless loops must be kept alive.
+     As we can't distinguish these easily we keep all of them alive. */
+  if ((res->op == op_Phi) && (mode == mode_M))
+    add_End_keepalive(irg->end, res);
   return res;
 }
 
@@ -463,7 +470,6 @@ new_r_Raise (ir_graph *irg, ir_node *block, ir_node *store, ir_node *obj)
   ir_node *in[2] = {store, obj};
   ir_node *res;
   res = new_ir_node (irg, block, op_Raise, mode_T, 2, in);
-
   res = optimize (res);
   irn_vrfy (res);
   return res;
@@ -629,10 +635,8 @@ ir_node *
 new_End (void)
 {
   ir_node *res;
-
   res = new_ir_node (current_ir_graph,  current_ir_graph->current_block,
                     op_End, mode_X, -1, NULL);
-
   res = optimize (res);
   irn_vrfy (res);
 
@@ -1042,6 +1046,8 @@ new_r_Phi_in (ir_graph *irg, ir_node *block, ir_mode *mode,
   known = res;
   for (i=0;  i < ins;  ++i)
   {
+       assert(in[i]);
+
     if (in[i]==res || in[i]==known || is_Bad(in[i])) continue;
 
     if (known==res)
@@ -1062,6 +1068,10 @@ new_r_Phi_in (ir_graph *irg, ir_node *block, ir_mode *mode,
   } else {
     res = optimize (res);
     irn_vrfy (res);
+    /* Memory Phis in endless loops must be kept alive.
+       As we can't distinguish these easily we keep all of the alive. */
+    if ((res->op == op_Phi) && (mode == mode_M))
+      add_End_keepalive(irg->end, res);
   }
 
   return res;
@@ -1074,17 +1084,22 @@ get_r_value_internal (ir_node *block, int pos, ir_mode *mode);
 static inline ir_node *
 phi_merge (ir_node *block, int pos, ir_mode *mode, ir_node **nin, int ins);
 
-inline ir_node **
+ir_node **
 new_frag_arr (ir_node *n) {
   ir_node **arr;
+  int opt;
   arr = NEW_ARR_D (ir_node *, current_ir_graph->obst, current_ir_graph->n_loc);
   memcpy(arr, current_ir_graph->current_block->attr.block.graph_arr,
         sizeof(ir_node *)*current_ir_graph->n_loc);
+  /* turn off optimization before allocating Proj nodes, as res isn't
+     finished yet. */
+  opt = get_optimize(); set_optimize(0);
   /* Here we rely on the fact that all frag ops have Memory as first result! */
   if (get_irn_op(n) == op_Call)
     arr[0] = new_Proj(n, mode_M, 3);
   else
     arr[0] = new_Proj(n, mode_M, 0);
+  set_optimize(opt);
   current_ir_graph->current_block->attr.block.graph_arr[current_ir_graph->n_loc-1] = n;
   return arr;
 }
@@ -1113,7 +1128,7 @@ get_r_frag_value_internal (ir_node *block, ir_node *cfOp, int pos, ir_mode *mode
   ir_node **rem;
   ir_node **frag_arr;
 
-  assert(is_fragile_op(cfOp));
+  assert(is_fragile_op(cfOp) && (get_irn_op(cfOp) != op_Bad));
 
   frag_arr = get_frag_arr(cfOp);
   res = frag_arr[pos];
@@ -1125,16 +1140,21 @@ get_r_frag_value_internal (ir_node *block, ir_node *cfOp, int pos, ir_mode *mode
        int ins = get_irn_arity(block);
        ir_node **nin;
        NEW_ARR_A (ir_node *, nin, ins);
-       phi_merge(block, pos, mode, nin, ins);
+       res = phi_merge(block, pos, mode, nin, ins);
       } else {
        res = new_r_Phi0 (current_ir_graph, block, mode);
        res->attr.phi0_pos = pos;
        res->link = block->link;
        block->link = res;
       }
-      set_frag_value(frag_arr, pos, res);
+      assert(res);
+      /* @@@ tested by Flo: set_frag_value(frag_arr, pos, res);
+        but this should be better: (remove comment if this works) */
+      /* It's a Phi, we can write this into all graph_arrs with NULL */
+      set_frag_value(block->attr.block.graph_arr, pos, res);
     } else {
       res = get_r_value_internal(block, pos, mode);
+      set_frag_value(block->attr.block.graph_arr, pos, res);
     }
   }
   return res;
@@ -1152,7 +1172,6 @@ phi_merge (ir_node *block, int pos, ir_mode *mode, ir_node **nin, int ins)
   ir_node *prevBlock, *prevCfOp, *res, *phi0;
   int i;
 
-
   /* If this block has no value at pos create a Phi0 and remember it
      in graph_arr to break recursions.
      Else we may not set graph_arr as there a later value is remembered. */
@@ -1176,11 +1195,13 @@ phi_merge (ir_node *block, int pos, ir_mode *mode, ir_node **nin, int ins)
       /* We don't need to care about exception ops in the start block.
         There are none by definition. */
       return block->attr.block.graph_arr[pos];
-    } else  {
+    } else {
       phi0 = new_r_Phi0(current_ir_graph, block, mode);
       block->attr.block.graph_arr[pos] = phi0;
 #if PRECISE_EXC_CONTEXT
-      /* Set graph_arr for fragile ops.  Also here we should break recursion. */
+      /* Set graph_arr for fragile ops.  Also here we should break recursion.
+        We could choose a cyclic path through an cfop.  But the recursion would
+        break at some point. */
       set_frag_value(block->attr.block.graph_arr, pos, phi0);
 #endif
     }
@@ -1202,9 +1223,10 @@ phi_merge (ir_node *block, int pos, ir_mode *mode, ir_node **nin, int ins)
     assert (prevBlock);
     if (!is_Bad(prevBlock)) {
 #if PRECISE_EXC_CONTEXT
-      if (is_fragile_op(prevCfOp))
+      if (is_fragile_op(prevCfOp) && (get_irn_op (prevCfOp) != op_Bad)) {
+       assert(get_r_frag_value_internal (prevBlock, prevCfOp, pos, mode));
        nin[i-1] = get_r_frag_value_internal (prevBlock, prevCfOp, pos, mode);
-      else
+      else
 #endif
        nin[i-1] = get_r_value_internal (prevBlock, pos, mode);
     } else {
@@ -1339,13 +1361,14 @@ mature_block (ir_node *block)
   ir_node *next;
 
   assert (get_irn_opcode(block) == iro_Block);
+  // assert (!get_Block_matured(block) && "Block already matured");
 
   if (!get_Block_matured(block)) {
 
-    /* turn the dynamic in-array into a static one. */
+    /* An array for building the Phi nodes. */
     ins = ARR_LEN (block->in)-1;
     NEW_ARR_A (ir_node *, nin, ins);
-    /*  @@@ something is strange here... why isn't the array copied? */
+    /* shouldn't we delete this array at the end of the procedure? @@@ memory leak? */
 
     /* Traverse a chain of Phi nodes attached to this block and mature
        these, too. **/
@@ -1362,8 +1385,9 @@ mature_block (ir_node *block)
        we can not free the node on the obstack.  Therefore we have to call
        optimize_in_place.
        Unfortunately the optimization does not change a lot, as all allocated
-       nodes refer to the unoptimized node. */
-    block = optimize_in_place(block);
+       nodes refer to the unoptimized node.
+       We can call _2, as global cse has no effect on blocks. */
+    block = optimize_in_place_2(block);
     irn_vrfy(block);
   }
 }
@@ -1457,7 +1481,9 @@ new_Quot (ir_node *memop, ir_node *op1, ir_node *op2)
   res = new_r_Quot (current_ir_graph, current_ir_graph->current_block,
                     memop, op1, op2);
 #if PRECISE_EXC_CONTEXT
-  res->attr.frag_arr = new_frag_arr(res);
+  if ((current_ir_graph->phase_state == phase_building) &&
+      (get_irn_op(res) == op_Quot))  /* Could be optimized away. */
+    res->attr.frag_arr = new_frag_arr(res);
 #endif
 
   return res;
@@ -1470,7 +1496,9 @@ new_DivMod (ir_node *memop, ir_node *op1, ir_node *op2)
   res = new_r_DivMod (current_ir_graph, current_ir_graph->current_block,
                       memop, op1, op2);
 #if PRECISE_EXC_CONTEXT
-  res->attr.frag_arr = new_frag_arr(res);
+  if ((current_ir_graph->phase_state == phase_building) &&
+      (get_irn_op(res) == op_DivMod))   /* Could be optimized away. */
+    res->attr.frag_arr = new_frag_arr(res);
 #endif
 
   return res;
@@ -1483,7 +1511,9 @@ new_Div (ir_node *memop, ir_node *op1, ir_node *op2)
   res = new_r_Div (current_ir_graph, current_ir_graph->current_block,
                    memop, op1, op2);
 #if PRECISE_EXC_CONTEXT
-  res->attr.frag_arr = new_frag_arr(res);
+  if ((current_ir_graph->phase_state == phase_building) &&
+      (get_irn_op(res) == op_Div))  /* Could be optimized away. */
+    res->attr.frag_arr = new_frag_arr(res);
 #endif
 
   return res;
@@ -1496,7 +1526,9 @@ new_Mod (ir_node *memop, ir_node *op1, ir_node *op2)
   res = new_r_Mod (current_ir_graph, current_ir_graph->current_block,
                    memop, op1, op2);
 #if PRECISE_EXC_CONTEXT
-  res->attr.frag_arr = new_frag_arr(res);
+  if ((current_ir_graph->phase_state == phase_building) &&
+      (get_irn_op(res) == op_Mod))  /* Could be optimized away. */
+    res->attr.frag_arr = new_frag_arr(res);
 #endif
 
   return res;
@@ -1592,7 +1624,9 @@ new_Call (ir_node *store, ir_node *callee, int arity, ir_node **in,
   res = new_r_Call (current_ir_graph, current_ir_graph->current_block,
                     store, callee, arity, in, type);
 #if PRECISE_EXC_CONTEXT
-  res->attr.call.frag_arr = new_frag_arr(res);
+  if ((current_ir_graph->phase_state == phase_building) &&
+      (get_irn_op(res) == op_Call))  /* Could be optimized away. */
+    res->attr.call.frag_arr = new_frag_arr(res);
 #endif
 
   return res;
@@ -1619,7 +1653,9 @@ new_Load (ir_node *store, ir_node *addr)
   res = new_r_Load (current_ir_graph, current_ir_graph->current_block,
                     store, addr);
 #if PRECISE_EXC_CONTEXT
-  res->attr.frag_arr = new_frag_arr(res);
+  if ((current_ir_graph->phase_state == phase_building) &&
+      (get_irn_op(res) == op_Load))  /* Could be optimized away. */
+    res->attr.frag_arr = new_frag_arr(res);
 #endif
 
   return res;
@@ -1632,7 +1668,9 @@ new_Store (ir_node *store, ir_node *addr, ir_node *val)
   res = new_r_Store (current_ir_graph, current_ir_graph->current_block,
                      store, addr, val);
 #if PRECISE_EXC_CONTEXT
-  res->attr.frag_arr = new_frag_arr(res);
+  if ((current_ir_graph->phase_state == phase_building) &&
+      (get_irn_op(res) == op_Store))  /* Could be optimized away. */
+    res->attr.frag_arr = new_frag_arr(res);
 #endif
 
   return res;
@@ -1646,7 +1684,9 @@ new_Alloc (ir_node *store, ir_node *size, type *alloc_type,
   res = new_r_Alloc (current_ir_graph, current_ir_graph->current_block,
                      store, size, alloc_type, where);
 #if PRECISE_EXC_CONTEXT
-  res->attr.a.frag_arr = new_frag_arr(res);
+  if ((current_ir_graph->phase_state == phase_building) &&
+      (get_irn_op(res) == op_Alloc))  /* Could be optimized away. */
+    res->attr.a.frag_arr = new_frag_arr(res);
 #endif
 
   return res;
@@ -1706,6 +1746,7 @@ new_Bad (void)
 ir_node *new_immBlock (void) {
   ir_node *res;
 
+  assert(get_irg_phase_state (current_ir_graph) == phase_building);
   /* creates a new dynamic in-array as length of in is -1 */
   res = new_ir_node (current_ir_graph, NULL, op_Block, mode_R, -1, NULL);
   current_ir_graph->current_block = res;
@@ -1750,6 +1791,7 @@ switch_block (ir_node *target)
 ir_node *
 get_value (int pos, ir_mode *mode)
 {
+  assert(get_irg_phase_state (current_ir_graph) == phase_building);
   inc_irg_visited(current_ir_graph);
   return get_r_value_internal (current_ir_graph->current_block, pos + 1, mode);
 }
@@ -1759,6 +1801,7 @@ get_value (int pos, ir_mode *mode)
 inline void
 set_value (int pos, ir_node *value)
 {
+  assert(get_irg_phase_state (current_ir_graph) == phase_building);
   current_ir_graph->current_block->attr.block.graph_arr[pos + 1] = value;
 }
 
@@ -1766,6 +1809,7 @@ set_value (int pos, ir_node *value)
 inline ir_node *
 get_store (void)
 {
+  assert(get_irg_phase_state (current_ir_graph) == phase_building);
   /* GL: one could call get_value instead */
   inc_irg_visited(current_ir_graph);
   return get_r_value_internal (current_ir_graph->current_block, 0, mode_M);
@@ -1775,10 +1819,30 @@ get_store (void)
 inline void
 set_store (ir_node *store)
 {
+  assert(get_irg_phase_state (current_ir_graph) == phase_building);
   /* GL: one could call set_value instead */
   current_ir_graph->current_block->attr.block.graph_arr[0] = store;
 }
 
+inline void
+keep_alive (ir_node *ka)
+{
+  add_End_keepalive(current_ir_graph->end, ka);
+}
+
+/** Useful access routines **/
+/* Returns the current block of the current graph.  To set the current
+   block use switch_block(). */
+ir_node *get_cur_block() {
+  return get_irg_current_block(current_ir_graph);
+}
+
+/* Returns the frame type of the current graph */
+type *get_cur_frame_type() {
+  return get_irg_frame_type(current_ir_graph);
+}
+
+
 /* ********************************************************************* */
 /* initialize */
 
@@ -1787,3 +1851,9 @@ void
 init_cons (void)
 {
 }
+
+/* call for each graph */
+void
+finalize_cons (ir_graph *irg) {
+  irg->phase_state = phase_high;
+}