changed code placement so it can work in more environments:
[libfirm] / ir / opt / cfopt.c
index 298fca1..0d553ba 100644 (file)
  */
 
 #ifdef HAVE_CONFIG_H
-# include <config.h>
+# include "config.h"
 #endif
 
 #include <assert.h>
 
+#include "xmalloc.h"
 #include "irnode_t.h"
 #include "irgraph_t.h"
 #include "irprog_t.h"
@@ -35,6 +36,7 @@
 #include "firmstat.h"
 
 #include "cfopt.h"
+#include "iropt_dbg.h"
 
 /*------------------------------------------------------------------*/
 /* Control flow optimization.                                       */
 /* semantics of Phi nodes.                                          */
 /*------------------------------------------------------------------*/
 
+/**
+ * Replace binary Conds that jumps twice into the same block
+ * by a simple Jmp.
+ * E.g.
+ * @code
+ *               Cond                     Jmp  Bad
+ *             /       \                   |   /
+ *       ProjX True   ProjX False  ==>     |  /
+ *             \       /                   | /
+ *               Block                    Block
+ * @endcode
+ *
+ * Such pattern are the result of if-conversion.
+ *
+ * Note that the simple case that Block has only these two
+ * predecessors are already handled in equivalent_node_Block().
+ */
+static void remove_senseless_conds(ir_node *bl, void *data)
+{
+       int i, j;
+       int n = get_Block_n_cfgpreds(bl);
+
+       assert(is_Block(bl));
+
+       for (i = 0; i < n; ++i) {
+               ir_node *pred_i = get_Block_cfgpred(bl, i);
+               ir_node *cond_i = skip_Proj(pred_i);
+
+               for (j = i + 1; j < n; ++j) {
+                       ir_node *pred_j = get_Block_cfgpred(bl, j);
+                       ir_node *cond_j = skip_Proj(pred_j);
+
+                       if (cond_j == cond_i
+                                       && get_irn_op(cond_i) == op_Cond
+                                       && get_irn_mode(get_Cond_selector(cond_i)) == mode_b) {
+
+                               ir_node *jmp = new_r_Jmp(current_ir_graph, get_nodes_block(cond_i));
+                               set_irn_n(bl, i, jmp);
+                               set_irn_n(bl, j, new_Bad());
+
+        DBG_OPT_IFSIM2(cond_i, jmp);
+                               break;
+                       }
+               }
+       }
+}
+
+
 /**
  * Removes Tuples from Block control flow predecessors.
  * Optimizes blocks with equivalent_node().  This is tricky,
@@ -67,55 +117,63 @@ static void merge_blocks(ir_node *n, void *env) {
       if (get_opt_normalize())
         set_Block_cfgpred(n, i, skip_Tuple(get_Block_cfgpred(n, i)));
     }
+
+    /* see below */
     new_block = equivalent_node(n);
-    if (new_block != n)
+    if (new_block != n && ! is_Bad(new_block))
       exchange (n, new_block);
 
   } else if (get_opt_optimize() && (get_irn_mode(n) == mode_X)) {
     /* We will soon visit a block.  Optimize it before visiting! */
-    ir_node *b        = get_nodes_block(n);
+    ir_node *b = get_nodes_block(skip_Proj(n));
 
     if (!is_Bad(b)) {
       new_block = equivalent_node(b);
 
       while (irn_not_visited(b) && (!is_Bad(new_block)) && (new_block != b)) {
-        /* We would have to run gigo if new is bad, so we
+        /* We would have to run gigo() if new is bad, so we
            promote it directly below. Nevertheless, we sometimes reach a block
            the first time through a dataflow node.  In this case we optimized the
            block as such and have to promote the Bad here. */
-        assert(((b == new_block) ||
-            get_opt_control_flow_straightening() ||
-            get_opt_control_flow_weak_simplification()) &&
-           ("strange flag setting"));
+        assert((get_opt_control_flow_straightening() ||
+                get_opt_control_flow_weak_simplification()) &&
+               ("strange flag setting"));
         exchange (b, new_block);
         b = new_block;
         new_block = equivalent_node(b);
       }
-      b = new_block;
-    }
 
-    /*
-     * BEWARE: do not kill floating notes here as they might be needed in
-     * valid blocks because of global CSE.
-     */
-    if (is_Bad(b) && get_opt_normalize() &&
-      get_op_pinned(get_irn_op(n)) == op_pin_state_pinned)
-      exchange(n, new_Bad());
+      /* normally, we would create a Bad block here, but this must be
+       * prevented, so just set it's cf to Bad.
+       */
+      if (is_Bad(new_block))
+        exchange(n, new_Bad());
+    }
   }
 }
 
 /**
- * Remove dead block by inspecting dominance info
+ * Remove cf from dead block by inspecting dominance info
+ * Do not replace blocks by Bad.  This optimization shall
+ * ensure, that all Bad control flow predecessors are
+ * removed, and no new other Bads are introduced.
+ *
+ * Must be run in the post walker.
  */
-static void remove_dead_blocks(ir_node *block, void *env) {
-  /* delete dead blocks: if we have dominator information, this can easily be detected.
-   * Here, new Bad blocks my be introduced.
-   *
-   * BEWARE: don't kill the end block */
-  if (block != get_irg_end_block(current_ir_graph) &&
-      get_Block_dom_depth(block) == -1 &&
-      get_opt_unreachable_code()) {
-    exchange (block, new_Bad());
+static void remove_dead_block_cf(ir_node *block, void *env)
+{
+  int i, n;
+
+  /* check block predecessors and turn control flow into bad */
+  for (i = 0, n = get_Block_n_cfgpreds(block); i < n; ++i) {
+    ir_node *pred_X = get_Block_cfgpred(block, i);
+
+    if (! is_Bad(pred_X)) {
+      ir_node *pred_bl = get_nodes_block(skip_Proj(pred_X));
+
+      if (is_Bad(pred_bl) || (get_Block_dom_depth(pred_bl) == -1))
+        exchange (pred_X, new_Bad());
+    }
   }
 }
 
@@ -130,16 +188,7 @@ static void collect_nodes(ir_node *n, void *env) {
   if (is_no_Block(n)) {
     ir_node *b = get_nodes_block(n);
 
-    /*
-     * BEWARE: do not kill floating notes here as they might be needed in
-     * valid blocks because of global CSE.
-     */
-    if (is_Bad(b) &&
-      get_op_pinned(get_irn_op(n)) == op_pin_state_pinned) {
-      /* previous merge_blocks() may have killed dead blocks */
-      exchange(n, new_Bad());
-    }
-    else if ((get_irn_op(n) == op_Phi)) {
+    if ((get_irn_op(n) == op_Phi)) {
       /* Collect Phi nodes to compact ins along with block's ins. */
       set_irn_link(n, get_irn_link(b));
       set_irn_link(b, n);
@@ -162,7 +211,7 @@ static int is_pred_of(ir_node *pred, ir_node *b) {
 }
 
 
-/** Test wether we can optimize away pred block pos of b.
+/** Test whether we can optimize away pred block pos of b.
  *
  *  @param  b    A block node.
  *  @param  pos  The position of the predecessor block to judge about.
@@ -185,7 +234,7 @@ static int is_pred_of(ir_node *pred, ir_node *b) {
  *     requires that a copy is added before the merge.  We have to
  *     keep one of the case blocks to place the copies in.
  *
- *     To perform the test for pos, we must regard preds before pos
+ *     To perform the test for pos, we must regard predecessors before pos
  *     as already removed.
  **/
 static int test_whether_dispensable(ir_node *b, int pos) {
@@ -194,6 +243,10 @@ static int test_whether_dispensable(ir_node *b, int pos) {
   ir_node *cfop = get_Block_cfgpred(b, pos);
   ir_node *pred = get_nodes_block(cfop);
 
+  /* Bad blocks will be optimized away, so we don't need space for them */
+  if (is_Bad(pred))
+    return 0;
+
   if (get_Block_block_visited(pred) + 1
       < get_irg_block_visited(current_ir_graph)) {
 
@@ -240,7 +293,7 @@ static int test_whether_dispensable(ir_node *b, int pos) {
 
 
 /**
- * This method removed Bad cf preds from Blocks and Phis, and removes
+ * This method removed Bad cf predecessors from Blocks and Phis, and removes
  * empty blocks.  A block is empty if it only contains Phi and Jmp nodes.
  *
  * We first adapt Phi nodes, then Block nodes, as we need the old ins
@@ -257,7 +310,7 @@ static int test_whether_dispensable(ir_node *b, int pos) {
  * For Phi nodes f we have to check the conditions at the Block of f.
  * For cases 1 and 3 we proceed as for Blocks.  For case 2 we can have two
  * cases:
- *  2a: The old precessor of the Phi f is a Phi pred_f IN THE BLOCK REMOVED.  In this
+ *  2a: The old predecessor of the Phi f is a Phi pred_f IN THE BLOCK REMOVED.  In this
  *      case we proceed as for blocks. We remove pred_f.  All
  *      predecessors of pred_f now are predecessors of f.
  *  2b: The old predecessor of f is NOT in the block removed. It might be a Phi, too.
@@ -295,7 +348,7 @@ static void optimize_blocks(ir_node *b, void *env) {
   for (i = 0, k = get_Block_n_cfgpreds(b); i < k; ++i) {
     max_preds += test_whether_dispensable(b, i);
   }
-  in = (ir_node **) malloc(max_preds * sizeof(ir_node *));
+  in = xmalloc(max_preds * sizeof(*in));
 
 /*-
   printf(" working on "); DDMN(b);
@@ -349,7 +402,7 @@ static void optimize_blocks(ir_node *b, void *env) {
            Therefore we replace the old phi by the new one.
 
            Further we have to remove the old Phi node by replacing it
-           by Bad.  Else it will remain in the keepalive array of End
+           by Bad.  Else it will remain in the keep alive array of End
            and cause illegal situations.  So if there is no loop, we should
            replace it by Bad.
         */
@@ -363,6 +416,7 @@ static void optimize_blocks(ir_node *b, void *env) {
         in[p_preds++] = get_Phi_pred(phi, i);
       }
     }
+    assert(p_preds <= max_preds);
 
     /* Fix the node */
     if (p_preds == 1)
@@ -444,7 +498,8 @@ static void optimize_blocks(ir_node *b, void *env) {
           else
             set_irn_in(phi, q_preds, in);
 
-//          assert(p_preds == q_preds && "Wrong Phi Fix");
+          assert(q_preds <= max_preds);
+//        assert(p_preds == q_preds && "Wrong Phi Fix");
         }
         phi = get_irn_link(phi);
       }
@@ -478,11 +533,13 @@ static void optimize_blocks(ir_node *b, void *env) {
       in[n_preds++] = get_Block_cfgpred(b, i);
     }
   }
+  assert(n_preds <= max_preds);
+
   set_irn_in(b, n_preds, in);
 
   assert(get_irn_link(b) == NULL || (n_preds == p_preds && "Wrong Phi Fix"));
 
-  free(in);
+  xfree(in);
 }
 
 
@@ -503,10 +560,10 @@ static void optimize_blocks(ir_node *b, void *env) {
  * phase.
  * @@@ It would be better to add a struct in the link field
  * that keeps the Phi list and the mark.  Place it on an obstack, as
- * we will lose blocks and thereby generate mem leaks.
+ * we will lose blocks and thereby generate memory leaks.
  */
 void optimize_cf(ir_graph *irg) {
-  int i;
+  int i, n;
   ir_node **in;
   ir_node *end = get_irg_end(irg);
   ir_graph *rem = current_ir_graph;
@@ -520,11 +577,25 @@ void optimize_cf(ir_graph *irg) {
   if (get_irg_dom_state(current_ir_graph) == dom_consistent)
     set_irg_dom_inconsistent(current_ir_graph);
 
-  if (dom_state == dom_consistent) {
-    /* we have dominace info, we can kill dead block */
-    irg_block_walk(get_irg_end_block(irg), NULL, remove_dead_blocks, NULL);
+  if (dom_state == dom_consistent && get_opt_optimize() && get_opt_unreachable_code()) {
+    ir_node *end = get_irg_end(irg);
+
+    /* we have dominance info, we can kill dead block */
+    irg_block_walk_graph(irg, NULL, remove_dead_block_cf, NULL);
+
+    /* fix the keep-alives */
+    for (i = 0, n = get_End_n_keepalives(end); i < n; ++i) {
+      ir_node *ka = get_End_keepalive(end, i);
+
+      if (is_Block(ka) && (get_Block_dom_depth(ka) == -1))
+        set_End_keepalive(end, i, new_Bad());
+      if (is_Phi(ka) && (get_Block_dom_depth(get_nodes_block(ka)) == -1))
+        set_End_keepalive(end, i, new_Bad());
+    }
   }
 
+  irg_block_walk_graph(current_ir_graph, NULL, remove_senseless_conds, NULL);
+
   /* Use block visited flag to mark non-empty blocks. */
   inc_irg_block_visited(irg);
   irg_walk(end, merge_blocks, collect_nodes, NULL);
@@ -538,7 +609,7 @@ void optimize_cf(ir_graph *irg) {
   in[0] = get_nodes_block(end);
   inc_irg_visited(current_ir_graph);
 
-  for(i = 0; i < get_End_n_keepalives(end); i++) {
+  for (i = 0; i < get_End_n_keepalives(end); i++) {
     ir_node *ka = get_End_keepalive(end, i);
 
     if (irn_not_visited(ka)) {
@@ -557,11 +628,15 @@ void optimize_cf(ir_graph *irg) {
   /* DEL_ARR_F(end->in);   GL @@@ tut nicht ! */
   end->in = in;
 
-  /* after optimize_cf(), only Bad data flow may remain. */
-  if (irg_vrfy_bads(irg, BAD_DF | BAD_BLOCK | TUPLE)) {
-    dump_ir_block_graph(irg, "-vrfy-cf");
-    dump_ir_graph(irg, "-vrfy-cf");
-    fprintf(stderr, "VRFY_BAD in optimize_cf()\n");
+
+  /* the verifier doesn't work yet with floating nodes */
+  if (get_irg_pinned(irg) == op_pin_state_pinned) {
+    /* after optimize_cf(), only Bad data flow may remain. */
+    if (irg_vrfy_bads(irg, BAD_DF | BAD_BLOCK | TUPLE)) {
+      dump_ir_block_graph(irg, "-vrfy-cf");
+      dump_ir_graph(irg, "-vrfy-cf");
+      fprintf(stderr, "VRFY_BAD in optimize_cf()\n");
+    }
   }
 
   current_ir_graph = rem;