better lftr:
[libfirm] / ir / opt / cfopt.c
index 6485694..be03bfa 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 1995-2007 University of Karlsruhe.  All right reserved.
+ * Copyright (C) 1995-2008 University of Karlsruhe.  All right reserved.
  *
  * This file is part of libFirm.
  *
@@ -27,6 +27,8 @@
 # include "config.h"
 #endif
 
+#include "iroptimize.h"
+
 #include <assert.h>
 
 #include "plist.h"
@@ -51,7 +53,6 @@
 #include "irflag_t.h"
 #include "firmstat.h"
 
-#include "cfopt.h"
 #include "iropt_dbg.h"
 
 /*------------------------------------------------------------------*/
@@ -61,6 +62,8 @@
 /* is empty if it contains only a Jmp node.                         */
 /* Blocks can only be removed if they are not needed for the        */
 /* semantics of Phi nodes.                                          */
+/* Further, we NEVER remove labeled blocks (even if we could move   */
+/* the label.                                                       */
 /*------------------------------------------------------------------*/
 
 /**
@@ -114,8 +117,9 @@ static void remove_senseless_conds(ir_node *bl, void *env) {
 
 /** An environment for merge_blocks and collect nodes. */
 typedef struct _merge_env {
-       int changed;
-       plist_t *list;
+       int changed;    /**< Set if the graph was changed. */
+       int phis_moved; /**< Set if Phi nodes were moved. */
+       plist_t *list;  /**< Helper list for all found Switch Conds. */
 } merge_env;
 
 /**
@@ -183,7 +187,6 @@ static void merge_blocks(ir_node *node, void *ctx) {
        }
 }
 
-
 /**
  * Block walker removing control flow from dead block by
  * inspecting dominance info.
@@ -224,10 +227,14 @@ static void collect_nodes(ir_node *n, void *ctx) {
        ir_op *op = get_irn_op(n);
        merge_env *env = ctx;
 
-       if (op != op_Block) {
+       if (op == op_Block) {
+               /* mark the block as non-empty if it is labeled */
+               if (has_Block_label(n))
+                       mark_Block_block_visited(n);
+       } else {
                ir_node *b  = get_nodes_block(n);
 
-               if (op == op_Phi) {
+               if (op == op_Phi && get_irn_arity(n) > 0) {
                        /* Collect Phi nodes to compact ins along with block's ins. */
                        set_irn_link(n, get_irn_link(b));
                        set_irn_link(b, n);
@@ -306,7 +313,7 @@ static int test_whether_dispensable(ir_node *b, int pos) {
                }
 
                /* Seems to be empty. At least we detected this in collect_nodes. */
-               if (!get_irn_link(b)) {
+               if (get_irn_link(b) == NULL) {
                        /* There are no Phi nodes ==> all predecessors are dispensable. */
                        n_preds = get_Block_n_cfgpreds(pred);
                } else {
@@ -391,11 +398,11 @@ non_dispensable:
  * @@@ It is negotiable whether we should do this ... there might end up a copy
  * from the Phi in the loop when removing the Phis.
  */
-static void optimize_blocks(ir_node *b, void *env) {
+static void optimize_blocks(ir_node *b, void *ctx) {
        int i, j, k, n, max_preds, n_preds, p_preds = -1;
        ir_node *pred, *phi;
        ir_node **in;
-       int *changed = env;
+       merge_env *env = ctx;
 
        /* Count the number of predecessor if this block is merged with pred blocks
           that are empty. */
@@ -450,36 +457,37 @@ static void optimize_blocks(ir_node *b, void *env) {
                        exchange(phi, in[0]);
                else
                        set_irn_in(phi, p_preds, in);
-               *changed = 1;
+               env->changed = 1;
 
                phi = get_irn_link(phi);
        }
 
        /*- This happens only if merge between loop backedge and single loop entry.
-           Moreover, it is only needed if pred is the direct dominator of b, else there can be no uses
-           of the Phi's in pred ... -*/
+           Moreover, it is only needed if predb is the direct dominator of b, else there can be no uses
+           of the Phi's in predb ... -*/
        for (k = 0, n = get_Block_n_cfgpreds(b); k < n; ++k) {
-               pred = get_nodes_block(get_Block_cfgpred(b, k));
+               ir_node *predb = get_nodes_block(get_Block_cfgpred(b, k));
 
-               if (get_Block_block_visited(pred) + 1 < get_irg_block_visited(current_ir_graph)) {
+               if (get_Block_block_visited(predb) + 1 < get_irg_block_visited(current_ir_graph)) {
                        ir_node *next_phi;
 
                        /* we found a predecessor block at position k that will be removed */
-                       for (phi = get_irn_link(pred); phi; phi = next_phi) {
+                       for (phi = get_irn_link(predb); phi; phi = next_phi) {
                                int q_preds = 0;
                                next_phi = get_irn_link(phi);
 
                                assert(is_Phi(phi));
 
-                               if (get_Block_idom(b) != pred) {
-                                       /* pred is not the dominator. There can't be uses of pred's Phi nodes, kill them .*/
+                               if (get_Block_idom(b) != predb) {
+                                       /* predb is not the dominator. There can't be uses of pred's Phi nodes, kill them .*/
                                        exchange(phi, new_Bad());
                                } else {
-                                       /* pred is the direct dominator of b. There might be uses of the Phi nodes from
-                                          pred in further block, so move this phi from the predecessor into the block b */
+                                       /* predb is the direct dominator of b. There might be uses of the Phi nodes from
+                                          predb in further block, so move this phi from the predecessor into the block b */
                                        set_nodes_block(phi, b);
                                        set_irn_link(phi, get_irn_link(b));
                                        set_irn_link(b, phi);
+                                       env->phis_moved = 1;
 
                                        /* first, copy all 0..k-1 predecessors */
                                        for (i = 0; i < k; i++) {
@@ -529,7 +537,7 @@ static void optimize_blocks(ir_node *b, void *env) {
                                                exchange(phi, in[0]);
                                        else
                                                set_irn_in(phi, q_preds, in);
-                                       *changed = 1;
+                                       env->changed = 1;
 
                                        assert(q_preds <= max_preds);
                                        // assert(p_preds == q_preds && "Wrong Phi Fix");
@@ -568,7 +576,7 @@ static void optimize_blocks(ir_node *b, void *env) {
        assert(n_preds <= max_preds);
 
        set_irn_in(b, n_preds, in);
-       *changed = 1;
+       env->changed = 1;
 
        assert(get_irn_link(b) == NULL || p_preds == -1 || (n_preds == p_preds && "Wrong Phi Fix"));
        xfree(in);
@@ -578,13 +586,13 @@ static void optimize_blocks(ir_node *b, void *env) {
  * Block walker: optimize all blocks using the default optimizations.
  * This removes Blocks that with only a Jmp predecessor.
  */
-static void remove_simple_blocks(ir_node *block, void *env) {
+static void remove_simple_blocks(ir_node *block, void *ctx) {
        ir_node *new_blk = equivalent_node(block);
-       int *changed = env;
+       merge_env *env = ctx;
 
        if (new_blk != block) {
                exchange(block, new_blk);
-               *changed = 1;
+               env->changed = 1;
        }
 }
 
@@ -699,7 +707,9 @@ void optimize_cf(ir_graph *irg) {
        /* FIXME: is this still needed? */
        edges_deactivate(irg);
 
-       env.changed = 0;
+       env.changed    = 0;
+       env.phis_moved = 0;
+
        if (get_opt_optimize() && get_opt_unreachable_code()) {
                ir_node *end;
 
@@ -757,7 +767,7 @@ void optimize_cf(ir_graph *irg) {
        /* Optimize the standard code. */
        env.changed = 0;
        assure_doms(irg);
-       irg_block_walk(get_irg_end_block(irg), optimize_blocks, remove_simple_blocks, &env.changed);
+       irg_block_walk(get_irg_end_block(irg), optimize_blocks, remove_simple_blocks, &env);
 
        /* Walk all keep alives, optimize them if block, add to new in-array
           for end if useful. */
@@ -767,7 +777,7 @@ void optimize_cf(ir_graph *irg) {
 
        /* in rare cases a node may be kept alive more than once, use the visited flag to detect this */
        inc_irg_visited(irg);
-       set_using_visited(irg);
+       set_using_irn_visited(irg);
 
        /* fix the keep alive */
        for (i = j = 0; i < n; i++) {
@@ -800,7 +810,46 @@ void optimize_cf(ir_graph *irg) {
                env.changed = 1;
        }
 
-       clear_using_visited(irg);
+       clear_using_irn_visited(irg);
+
+       if (env.phis_moved) {
+               /* Bad: when we moved Phi's, we might produce dead Phi nodes
+                  that are kept-alive.
+                  Some other phases cannot copy with this, so will them.
+                */
+               n = get_End_n_keepalives(end);
+               if (n > 0) {
+                       if (env.changed) {
+                               /* Handle graph state if was changed. */
+                               set_irg_outs_inconsistent(irg);
+                       }
+                       assure_irg_outs(irg);
+
+                       for (i = j = 0; i < n; ++i) {
+                               ir_node *ka = get_End_keepalive(end, i);
+
+                               if (is_Phi(ka)) {
+                                       int k;
+
+                                       for (k = get_irn_n_outs(ka) - 1; k >= 0; --k) {
+                                               ir_node *user = get_irn_out(ka, k);
+
+                                               if (user != ka && user != end) {
+                                                       /* Is it a real user or just a self loop ? */
+                                                       break;
+                                               }
+                                       }
+                                       if (k >= 0)
+                                               in[j++] = ka;
+                               } else
+                                       in[j++] = ka;
+                       }
+                       if (j != n) {
+                               set_End_keepalives(end, j, in);
+                               env.changed = 1;
+                       }
+               }
+       }
 
        if (env.changed) {
                /* Handle graph state if was changed. */
@@ -810,6 +859,7 @@ void optimize_cf(ir_graph *irg) {
                set_irg_loopinfo_inconsistent(irg);
        }
 
+
        /* 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. */