cleanup
[libfirm] / ir / opt / cfopt.c
index 963a7cd..d6f3c0e 100644 (file)
@@ -56,6 +56,7 @@
 #include "irflag_t.h"
 #include "firmstat.h"
 #include "irpass.h"
+#include "irphase_t.h"
 
 #include "iropt_dbg.h"
 
 typedef struct merge_env {
        bool      changed;      /**< Set if the graph was changed. */
        bool      phis_moved;   /**< Set if Phi nodes were moved. */
-       ir_node **switch_conds; /**< Helper list for all found Switch Conds. */
 } merge_env;
 
+/** set or reset the removable property of a block. */
 static void set_Block_removable(ir_node *block, bool removable)
 {
        set_Block_mark(block, removable);
 }
 
+/** check if a block has the removable property set. */
 static bool is_Block_removable(ir_node *block)
 {
        return get_Block_mark(block);
 }
 
-static void clear_link(ir_node *node, void *ctx)
+/** checks if a given Cond node is a switch Cond. */
+static bool is_switch_Cond(ir_node *cond)
+{
+       ir_node *sel = get_Cond_selector(cond);
+       return get_irn_mode(sel) != mode_b;
+}
+
+/** Walker: clear link fields and mark all blocks as removable. */
+static void clear_link_and_mark_blocks_removable(ir_node *node, void *ctx)
 {
        (void) ctx;
        set_irn_link(node, NULL);
@@ -93,7 +103,7 @@ static void clear_link(ir_node *node, void *ctx)
  */
 static void collect_nodes(ir_node *n, void *ctx)
 {
-       merge_env *env = (merge_env*)ctx;
+       ir_node ***switch_conds = (ir_node***)ctx;
 
        if (is_Phi(n)) {
                /* Collect Phi nodes to compact ins along with block's ins. */
@@ -101,8 +111,10 @@ static void collect_nodes(ir_node *n, void *ctx)
                set_irn_link(n, get_irn_link(block));
                set_irn_link(block, n);
        } else if (is_Block(n)) {
-               if (has_Block_entity(n))
+               if (has_Block_entity(n)) {
+                       /* block with a jump label attached cannot be removed. */
                        set_Block_removable(n, false);
+               }
                return;
        } else if (!is_Jmp(n)) {  /* Check for non-empty block. */
                ir_node *block = get_nodes_block(n);
@@ -113,17 +125,14 @@ static void collect_nodes(ir_node *n, void *ctx)
                        ir_node *pred = get_Proj_pred(n);
                        set_irn_link(n, get_irn_link(pred));
                        set_irn_link(pred, n);
-               } else if (is_Cond(n)) {
-                       ir_node *sel = get_Cond_selector(n);
-                       if (get_irn_mode(sel) != mode_b) {
-                               /* found a switch-Cond, collect */
-                               ARR_APP1(ir_node*, env->switch_conds, n);
-                       }
+               } else if (is_Cond(n) && is_switch_Cond(n)) {
+                       /* found a switch-Cond, collect */
+                       ARR_APP1(ir_node*, *switch_conds, n);
                }
        }
 }
 
-/** Returns true if pred is predecessor of block. */
+/** Returns true if pred is predecessor of block b. */
 static bool is_pred_of(ir_node *pred, ir_node *b)
 {
        int i;
@@ -275,6 +284,11 @@ static void optimize_blocks(ir_node *b, void *ctx)
        ir_node **in;
        merge_env *env = (merge_env*)ctx;
 
+       if (get_Block_dom_depth(b) < 0) {
+               /* ignore unreachable blocks */
+               return;
+       }
+
        /* Count the number of predecessor if this block is merged with pred blocks
           that are empty. */
        max_preds = 0;
@@ -371,12 +385,19 @@ static void optimize_blocks(ir_node *b, void *ctx)
                                                pred = get_Block_cfgpred_block(b, i);
 
                                                if (is_Bad(pred)) {
-                                                       in[q_preds++] = pred;
+                                                       ir_graph *irg  = get_irn_irg(b);
+                                                       ir_mode  *mode = get_irn_mode(phi);
+                                                       in[q_preds++] = new_r_Bad(irg, mode);
                                                } else if (is_Block_removable(pred) && !Block_block_visited(pred)) {
                                                        /* It's an empty block and not yet visited. */
                                                        for (j = 0; j < get_Block_n_cfgpreds(pred); j++) {
-                                                               if (! is_Bad(get_Block_cfgpred(pred, j)))
+                                                               if (! is_Bad(get_Block_cfgpred(pred, j))) {
                                                                        in[q_preds++] = phi;
+                                                               } else {
+                                                                       ir_graph *irg  = get_irn_irg(b);
+                                                                       ir_mode  *mode = get_irn_mode(phi);
+                                                                       in[q_preds++] = new_r_Bad(irg, mode);
+                                                               }
                                                        }
                                                } else {
                                                        in[q_preds++] = phi;
@@ -400,8 +421,13 @@ static void optimize_blocks(ir_node *b, void *ctx)
                                                } else if (is_Block_removable(pred) && !Block_block_visited(pred)) {
                                                        /* It's an empty block and not yet visited. */
                                                        for (j = 0; j < get_Block_n_cfgpreds(pred); j++) {
-                                                               if (! is_Bad(get_Block_cfgpred(pred, j)))
+                                                               if (! is_Bad(get_Block_cfgpred(pred, j))) {
                                                                        in[q_preds++] = phi;
+                                                               } else {
+                                                                       ir_graph *irg  = get_irn_irg(b);
+                                                                       ir_mode  *mode = get_irn_mode(phi);
+                                                                       in[q_preds++] = new_r_Bad(irg, mode);
+                                                               }
                                                        }
                                                } else {
                                                        in[q_preds++] = phi;
@@ -488,7 +514,7 @@ static bool handle_switch_cond(ir_node *cond)
 
        /* handle Cond nodes with constant argument. In this case the localopt rules
         * should have killed all obviously impossible cases.
-        * So the only case left to handle here is 1 defaultProj + 1case
+        * So the only case left to handle here is 1 defaultProj + 1 case
         * (this one case should be the one taken) */
        if (get_irn_link(proj2) == NULL) {
                ir_tarval *tv = value_of(sel);
@@ -535,21 +561,210 @@ static bool handle_switch_cond(ir_node *cond)
        return false;
 }
 
-/* Optimizations of the control flow that also require changes of Phi nodes.
- *
- * This optimization performs two passes over the graph.
- *
- * The first pass collects all Phi nodes in a link list in the block
- * nodes.  Further it performs simple control flow optimizations.
- * Finally it marks all blocks that do not contain useful
- * computations, i.e., these blocks might be removed.
- *
- * The second pass performs the optimizations intended by this algorithm.
- * It walks only over block nodes and adapts these and the Phi nodes in these
- * blocks, which it finds in a linked list computed by the first pass.
+/**
+ * Optimize boolean Conds, where true and false jump to the same block into a Jmp
+ * Block must contain no Phi nodes.
  *
- * We use the mark flag to mark removable blocks in the first phase.
+ *        Cond
+ *       /    \
+ *  projA      projB   =>   Jmp     Bad
+ *       \    /                \   /
+ *       block                 block
+ */
+static bool optimize_pred_cond(ir_node *block, int i, int j)
+{
+       ir_node *projA, *projB, *cond, *pred_block, *jmp, *bad;
+       assert(i != j);
+
+       projA = get_Block_cfgpred(block, i);
+       if (!is_Proj(projA)) return false;
+       projB = get_Block_cfgpred(block, j);
+       if (!is_Proj(projB)) return false;
+       cond  = get_Proj_pred(projA);
+       if (!is_Cond(cond))  return false;
+
+       if (cond != get_Proj_pred(projB)) return false;
+       if (is_switch_Cond(cond)) return false;
+
+       /* cond should actually be a Jmp */
+       pred_block = get_nodes_block(cond);
+       jmp = new_r_Jmp(pred_block);
+       bad = new_r_Bad(get_irn_irg(block), mode_X);
+
+       assert(projA != projB);
+       exchange(projA, jmp);
+       exchange(projB, bad);
+       return true;
+}
+
+typedef enum block_flags_t {
+       BF_HAS_OPERATIONS         = 1 << 0,
+       BF_HAS_PHIS               = 1 << 1,
+       BF_IS_UNKNOWN_JUMP_TARGET = 1 << 2,
+} block_flags_t;
+
+static bool get_phase_flag(ir_phase *block_info, ir_node *block, int flag)
+{
+       return PTR_TO_INT(phase_get_irn_data(block_info, block)) & flag;
+}
+
+static void set_phase_flag(ir_phase *block_info, ir_node *block,
+                           block_flags_t flag)
+{
+       int data = PTR_TO_INT(phase_get_irn_data(block_info, block));
+       data |= flag;
+       phase_set_irn_data(block_info, block, INT_TO_PTR(data));
+}
+
+static bool has_operations(ir_phase *block_info, ir_node *block)
+{
+       return get_phase_flag(block_info, block, BF_HAS_OPERATIONS);
+}
+
+static void set_has_operations(ir_phase *block_info, ir_node *block)
+{
+       set_phase_flag(block_info, block, BF_HAS_OPERATIONS);
+}
+
+static bool has_phis(ir_phase *block_info, ir_node *block)
+{
+       return get_phase_flag(block_info, block, BF_HAS_PHIS);
+}
+
+static void set_has_phis(ir_phase *block_info, ir_node *block)
+{
+       set_phase_flag(block_info, block, BF_HAS_PHIS);
+}
+
+static bool is_unknown_jump_target(ir_phase *block_info, ir_node *block)
+{
+       return get_phase_flag(block_info, block, BF_IS_UNKNOWN_JUMP_TARGET);
+}
+
+static void set_is_unknown_jump_target(ir_phase *block_info, ir_node *block)
+{
+       set_phase_flag(block_info, block, BF_IS_UNKNOWN_JUMP_TARGET);
+}
+
+/**
+ * Walker: fill block info information.
  */
+static void compute_block_info(ir_node *n, void *x)
+{
+       ir_phase *block_info = (ir_phase *)x;
+
+       if (is_Block(n)) {
+               int i, max = get_Block_n_cfgpreds(n);
+               for (i=0; i<max; i++) {
+                       ir_node *pred = get_Block_cfgpred(n,i);
+                       if (is_unknown_jump(pred)) {
+                               set_is_unknown_jump_target(block_info, n);
+                       }
+               }
+       } else if (is_Phi(n)) {
+               ir_node *block = get_nodes_block(n);
+               set_has_phis(block_info, block);
+       } else if (is_Jmp(n) || is_Cond(n) || is_Cmp(n) || is_Proj(n)) {
+               /* ignore */
+       } else {
+               ir_node *block = get_nodes_block(n);
+               set_has_operations(block_info, block);
+       }
+}
+
+typedef struct skip_env {
+       bool changed;
+       ir_phase *phase;
+} skip_env;
+
+/**
+ * Post-Block-walker: Optimize useless if's (boolean Cond nodes
+ * with same true/false target)
+ * away.
+ */
+static void optimize_ifs(ir_node *block, void *x)
+{
+       skip_env *env = (skip_env*)x;
+       int i, j;
+       int n_preds = get_Block_n_cfgpreds(block);
+
+       if (has_phis(env->phase, block))
+               return;
+
+       /* optimize Cond predecessors (might produce Bad predecessors) */
+       for (i = 0; i < n_preds; ++i) {
+               for (j = i+1; j < n_preds; ++j) {
+                       optimize_pred_cond(block, i, j);
+               }
+       }
+}
+
+/**
+ * Pre-Block walker: remove empty blocks that are
+ * predecessors of the current block.
+ */
+static void remove_empty_blocks(ir_node *block, void *x)
+{
+       skip_env *env = (skip_env*)x;
+       int i;
+       int n_preds = get_Block_n_cfgpreds(block);
+
+       for (i = 0; i < n_preds; ++i) {
+               ir_node *jmp, *jmp_block, *pred, *pred_block;
+
+               jmp = get_Block_cfgpred(block, i);
+               if (!is_Jmp(jmp))
+                       continue;
+               jmp_block = get_nodes_block(jmp);
+               if (is_unknown_jump_target(env->phase, jmp_block))
+                       continue;
+               if (has_operations(env->phase,jmp_block))
+                       continue;
+               /* jmp_block is an empty block! */
+
+               if (get_Block_n_cfgpreds(jmp_block) != 1)
+                       continue;
+               pred = get_Block_cfgpred(jmp_block, 0);
+               exchange(jmp, pred);
+               env->changed = true;
+
+               /* cleanup: jmp_block might have a Keep edge! */
+               pred_block = get_nodes_block(pred);
+               exchange(jmp_block, pred_block);
+       }
+}
+
+/*
+ * Some cfg optimizations, which do not touch Phi nodes
+ */
+static void cfgopt_ignoring_phis(ir_graph *irg)
+{
+       ir_phase *block_info = new_phase(irg, NULL);
+       skip_env env = { false, block_info };
+
+       irg_walk_graph(irg, compute_block_info, NULL, block_info);
+
+       for (;;) {
+               env.changed = false;
+
+               /* optimize useless ifs: will not touch empty blocks */
+               irg_block_walk_graph(irg, NULL, optimize_ifs, &env);
+
+               /* Remove empty blocks */
+               irg_block_walk_graph(irg, remove_empty_blocks, NULL, &env);
+               if (env.changed) {
+                       set_irg_doms_inconsistent(irg);
+                       /* Removing blocks might enable more useless-if optimizations */
+                       continue;
+               } else {
+                       break;
+               }
+       }
+
+       phase_free(block_info);
+}
+
+/* Optimizations of the control flow that also require changes of Phi nodes.  */
 void optimize_cf(ir_graph *irg)
 {
        int i, j, n;
@@ -558,6 +773,9 @@ void optimize_cf(ir_graph *irg)
        ir_node *new_end;
        merge_env env;
 
+       env.changed    = false;
+       env.phis_moved = false;
+
        assert(get_irg_phase_state(irg) != phase_building);
 
        /* if the graph is not pinned, we cannot determine empty blocks */
@@ -568,29 +786,38 @@ void optimize_cf(ir_graph *irg)
         * here. Fix the edges! */
        edges_deactivate(irg);
 
+       cfgopt_ignoring_phis(irg);
+
        /* we use the mark flag to mark removable blocks */
        ir_reserve_resources(irg, IR_RESOURCE_BLOCK_MARK | IR_RESOURCE_IRN_LINK);
 
-       /* The Cond optimization might expose unreachable code, so we loop */
+       /* The switch Cond optimization might expose unreachable code, so we loop */
        for (;;) {
                int length;
-               env.changed    = false;
-               env.phis_moved = false;
+               ir_node **switch_conds = NULL;
+               bool changed = false;
 
                assure_doms(irg);
 
-               env.switch_conds = NEW_ARR_F(ir_node*, 0);
-               irg_walk(end, clear_link, collect_nodes, &env);
+               /*
+                * This pass collects all Phi nodes in a link list in the block
+                * nodes.  Further it performs simple control flow optimizations.
+                * Finally it marks all blocks that do not contain useful
+                * computations, i.e., these blocks might be removed.
+                */
+               switch_conds = NEW_ARR_F(ir_node*, 0);
+               irg_walk(end, clear_link_and_mark_blocks_removable, collect_nodes, &switch_conds);
 
                /* handle all collected switch-Conds */
-               length = ARR_LEN(env.switch_conds);
+               length = ARR_LEN(switch_conds);
                for (i = 0; i < length; ++i) {
-                       ir_node *cond = env.switch_conds[i];
-                       env.changed |= handle_switch_cond(cond);
+                       ir_node *cond = switch_conds[i];
+                       changed |= handle_switch_cond(cond);
                }
-               DEL_ARR_F(env.switch_conds);
+               DEL_ARR_F(switch_conds);
 
-               if (!env.changed) break;
+               if (!changed)
+                       break;
 
                set_irg_doms_inconsistent(irg);
                set_irg_extblk_inconsistent(irg);
@@ -602,7 +829,10 @@ void optimize_cf(ir_graph *irg)
         * 2. phi lists are up to date
         */
 
-       /* Optimize the standard code. */
+       /* Optimize the standard code.
+        * It walks only over block nodes and adapts these and the Phi nodes in these
+        * blocks, which it finds in a linked list computed before.
+        * */
        assure_doms(irg);
        irg_block_walk_graph(irg, optimize_blocks, NULL, &env);
 
@@ -618,7 +848,7 @@ void optimize_cf(ir_graph *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.
+                  Some other phases cannot copy with this, so kill them.
                 */
                n = get_End_n_keepalives(end);
                if (n > 0) {