rename tarval to ir_tarval
[libfirm] / ir / opt / cfopt.c
index 972703d..68b1221 100644 (file)
@@ -40,7 +40,7 @@
 #include "irgwalk.h"
 #include "irgmod.h"
 #include "irdump.h"
-#include "irvrfy.h"
+#include "irverify.h"
 #include "iredges.h"
 
 #include "array_t.h"
@@ -50,6 +50,7 @@
 
 #include "irflag_t.h"
 #include "firmstat.h"
+#include "irpass.h"
 
 #include "iropt_dbg.h"
 
@@ -85,7 +86,8 @@
  * Note that the simple case that Block has only these two
  * predecessors are already handled in equivalent_node_Block().
  */
-static int remove_senseless_conds(ir_node *bl) {
+static int remove_senseless_conds(ir_node *bl)
+{
        int i, j;
        int n = get_Block_n_cfgpreds(bl);
        int changed = 0;
@@ -102,9 +104,10 @@ static int remove_senseless_conds(ir_node *bl) {
                                ir_node *cond_j = skip_Proj(pred_j);
 
                                if (cond_j == cond_i) {
-                                       ir_node *jmp = new_r_Jmp(current_ir_graph, get_nodes_block(cond_i));
+                                       ir_graph *irg = get_irn_irg(bl);
+                                       ir_node  *jmp = new_r_Jmp(get_nodes_block(cond_i));
                                        set_irn_n(bl, i, jmp);
-                                       set_irn_n(bl, j, new_Bad());
+                                       set_irn_n(bl, j, new_r_Bad(irg));
 
                                        DBG_OPT_IFSIM2(cond_i, jmp);
                                        changed = 1;
@@ -117,7 +120,7 @@ static int remove_senseless_conds(ir_node *bl) {
 }
 
 /** An environment for merge_blocks and collect nodes. */
-typedef struct _merge_env {
+typedef struct merge_env {
        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. */
@@ -130,7 +133,8 @@ typedef struct _merge_env {
  * Therefore we also optimize at control flow operations, depending
  * how we first reach the Block.
  */
-static void merge_blocks(ir_node *node, void *ctx) {
+static void merge_blocks(ir_node *node, void *ctx)
+{
        int i;
        ir_node *new_block;
        merge_env *env = ctx;
@@ -168,9 +172,6 @@ static void merge_blocks(ir_node *node, void *ctx) {
                                   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((get_opt_control_flow_straightening() ||
-                                       get_opt_control_flow_weak_simplification()) &&
-                                       ("strange flag setting"));
                                exchange(b, new_block);
                                env->changed = 1;
                                b = new_block;
@@ -181,7 +182,8 @@ static void merge_blocks(ir_node *node, void *ctx) {
                         * prevented, so just set it's cf to Bad.
                         */
                        if (is_Block_dead(new_block)) {
-                               exchange(node, new_Bad());
+                               ir_graph *irg = get_irn_irg(node);
+                               exchange(node, new_r_Bad(irg));
                                env->changed = 1;
                        }
                }
@@ -198,7 +200,8 @@ static void merge_blocks(ir_node *node, void *ctx) {
  *
  * Must be run in the post walker.
  */
-static void remove_unreachable_blocks_and_conds(ir_node *block, void *env) {
+static void remove_unreachable_blocks_and_conds(ir_node *block, void *env)
+{
        int i;
        int *changed = env;
 
@@ -212,8 +215,9 @@ static void remove_unreachable_blocks_and_conds(ir_node *block, void *env) {
                        ir_node *pred_bl = get_nodes_block(skip_Proj(skipped));
 
                        if (is_Block_dead(pred_bl) || (get_Block_dom_depth(pred_bl) < 0)) {
+                               ir_graph *irg = get_irn_irg(block);
                                set_Block_dead(pred_bl);
-                               exchange(pred_X, new_Bad());
+                               exchange(pred_X, new_r_Bad(irg));
                                *changed = 1;
                        } else if (skipped != pred_X) {
                                set_Block_cfgpred(block, i, skipped);
@@ -225,7 +229,7 @@ static void remove_unreachable_blocks_and_conds(ir_node *block, void *env) {
        *changed |= remove_senseless_conds(block);
 
        /* clear the block mark of all non labeled blocks */
-       if (has_Block_label(block))
+       if (has_Block_entity(block))
                set_Block_non_removable(block);
        else
                set_Block_removable(block);
@@ -238,13 +242,14 @@ static void remove_unreachable_blocks_and_conds(ir_node *block, void *env) {
  * Links all Proj nodes to their predecessors.
  * Collects all switch-Conds in a list.
  */
-static void collect_nodes(ir_node *n, void *ctx) {
+static void collect_nodes(ir_node *n, void *ctx)
+{
        ir_opcode code = get_irn_opcode(n);
        merge_env *env = ctx;
 
        if (code == iro_Block) {
                /* mark the block as non-removable if it is labeled */
-               if (has_Block_label(n))
+               if (has_Block_entity(n))
                        set_Block_non_removable(n);
        } else {
                ir_node *b = get_nodes_block(n);
@@ -273,7 +278,8 @@ static void collect_nodes(ir_node *n, void *ctx) {
 }
 
 /** Returns true if pred is predecessor of block. */
-static int is_pred_of(ir_node *pred, ir_node *b) {
+static int is_pred_of(ir_node *pred, ir_node *b)
+{
        int i;
 
        for (i = get_Block_n_cfgpreds(b) - 1; i >= 0; --i) {
@@ -311,7 +317,8 @@ static int is_pred_of(ir_node *pred, ir_node *b) {
  *  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) {
+static int test_whether_dispensable(ir_node *b, int pos)
+{
        int i, j, n_preds = 1;
        ir_node *pred = get_Block_cfgpred_block(b, pos);
 
@@ -320,12 +327,6 @@ static int test_whether_dispensable(ir_node *b, int pos) {
                return 0;
 
        if (is_Block_removable(pred)) {
-               if (!get_opt_optimize() || !get_opt_control_flow_strong_simplification()) {
-                       /* Mark block so that is will not be removed: optimization is turned off. */
-                       set_Block_non_removable(pred);
-                       return 1;
-               }
-
                /* Seems to be empty. At least we detected this in collect_nodes. */
                if (get_irn_link(b) == NULL) {
                        /* There are no Phi nodes ==> all predecessors are dispensable. */
@@ -410,7 +411,8 @@ 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 *ctx) {
+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, *next;
        ir_node **in;
@@ -489,7 +491,8 @@ static void optimize_blocks(ir_node *b, void *ctx) {
 
                                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());
+                                       ir_graph *irg = get_irn_irg(b);
+                                       exchange(phi, new_r_Bad(irg));
                                } else {
                                        /* 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 */
@@ -562,7 +565,7 @@ static void optimize_blocks(ir_node *b, void *ctx) {
                        /* case 1: Do nothing */
                } else if (is_Block_removable(pred) && !Block_block_visited(pred)) {
                        /* case 2: It's an empty block and not yet visited. */
-                       assert(get_Block_n_cfgpreds(b) > 1 || has_Block_label(b));
+                       assert(get_Block_n_cfgpreds(b) > 1 || has_Block_entity(b));
                        /* Else it should be optimized by equivalent_node. */
                        for (j = 0; j < get_Block_n_cfgpreds(pred); j++) {
                                ir_node *pred_X = get_Block_cfgpred(pred, j);
@@ -573,7 +576,7 @@ static void optimize_blocks(ir_node *b, void *ctx) {
                                        in[n_preds++] = pred_X;
                        }
                        /* Remove block as it might be kept alive. */
-                       exchange(pred, b/*new_Bad()*/);
+                       exchange(pred, b/*new_r_Bad(irg)*/);
                } else {
                        /* case 3: */
                        in[n_preds++] = get_Block_cfgpred(b, i);
@@ -592,7 +595,8 @@ static void optimize_blocks(ir_node *b, void *ctx) {
  * 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 *ctx) {
+static void remove_simple_blocks(ir_node *block, void *ctx)
+{
        ir_node *new_blk = equivalent_node(block);
        merge_env *env = ctx;
 
@@ -614,7 +618,8 @@ static void remove_simple_blocks(ir_node *block, void *ctx) {
  *
  * Expects all Proj's linked to the cond node
  */
-static int handle_switch_cond(ir_node *cond) {
+static int handle_switch_cond(ir_node *cond)
+{
        ir_node *sel = get_Cond_selector(cond);
 
        ir_node *proj1 = get_irn_link(cond);
@@ -625,48 +630,49 @@ static int handle_switch_cond(ir_node *cond) {
 
        if (proj2 == NULL) {
                /* this Cond has only one Proj: must be the defProj */
-               assert(get_Cond_defaultProj(cond) == get_Proj_proj(proj1));
+               assert(get_Cond_default_proj(cond) == get_Proj_proj(proj1));
                /* convert it into a Jmp */
-               jmp = new_r_Jmp(current_ir_graph, blk);
+               jmp = new_r_Jmp(blk);
                exchange(proj1, jmp);
                return 1;
        } else if (get_irn_link(proj2) == NULL) {
                /* We have two Proj's here. Check if the Cond has
                   a constant argument */
-               tarval *tv = value_of(sel);
+               ir_tarval *tv = value_of(sel);
 
                if (tv != tarval_bad) {
                        /* we have a constant switch */
-                       long num     = get_tarval_long(tv);
-                       long def_num = get_Cond_defaultProj(cond);
+                       long      num     = get_tarval_long(tv);
+                       long      def_num = get_Cond_default_proj(cond);
+                       ir_graph *irg     = get_irn_irg(cond);
 
                        if (def_num == get_Proj_proj(proj1)) {
                                /* first one is the defProj */
                                if (num == get_Proj_proj(proj2)) {
-                                       jmp = new_r_Jmp(current_ir_graph, blk);
+                                       jmp = new_r_Jmp(blk);
                                        exchange(proj2, jmp);
-                                       exchange(proj1, new_Bad());
+                                       exchange(proj1, new_r_Bad(irg));
                                        return 1;
                                }
                        } else if (def_num == get_Proj_proj(proj2)) {
                                /* second one is the defProj */
                                if (num == get_Proj_proj(proj1)) {
-                                       jmp = new_r_Jmp(current_ir_graph, blk);
+                                       jmp = new_r_Jmp(blk);
                                        exchange(proj1, jmp);
-                                       exchange(proj2, new_Bad());
+                                       exchange(proj2, new_r_Bad(irg));
                                        return 1;
                                }
                        } else {
                                /* neither: strange, Cond was not optimized so far */
                                if (num == get_Proj_proj(proj1)) {
-                                       jmp = new_r_Jmp(current_ir_graph, blk);
+                                       jmp = new_r_Jmp(blk);
                                        exchange(proj1, jmp);
-                                       exchange(proj2, new_Bad());
+                                       exchange(proj2, new_r_Bad(irg));
                                        return 1;
                                } else if (num == get_Proj_proj(proj2)) {
-                                       jmp = new_r_Jmp(current_ir_graph, blk);
+                                       jmp = new_r_Jmp(blk);
                                        exchange(proj2, jmp);
-                                       exchange(proj1, new_Bad());
+                                       exchange(proj1, new_r_Bad(irg));
                                        return 1;
                                }
                        }
@@ -691,11 +697,11 @@ static int handle_switch_cond(ir_node *cond) {
  * We use the mark flag to mark removable blocks in the first
  * phase.
  */
-void optimize_cf(ir_graph *irg) {
+void optimize_cf(ir_graph *irg)
+{
        int i, j, n, changed;
        ir_node **in = NULL;
        ir_node *cond, *end = get_irg_end(irg);
-       ir_graph *rem = current_ir_graph;
        plist_element_t *el;
        merge_env env;
 
@@ -705,8 +711,6 @@ void optimize_cf(ir_graph *irg) {
        assert(get_irg_pinned(irg) != op_pin_state_floats &&
               "Control flow optimization need a pinned graph");
 
-       current_ir_graph = irg;
-
        /* FIXME: control flow opt destroys block edges. So edges are deactivated here. Fix the edges! */
        edges_deactivate(irg);
 
@@ -729,7 +733,7 @@ restart:
                if (is_Block(ka)) {
                        /* do NOT keep dead blocks */
                        if (is_Block_dead(ka) || get_Block_dom_depth(ka) < 0) {
-                               set_End_keepalive(end, i, new_Bad());
+                               set_End_keepalive(end, i, new_r_Bad(irg));
                                changed = 1;
                        }
                } else {
@@ -737,7 +741,7 @@ restart:
 
                        if (is_Bad(block) || is_Block_dead(block) || get_Block_dom_depth(block) < 0) {
                                /* do NOT keep nodes in dead blocks */
-                               set_End_keepalive(end, i, new_Bad());
+                               set_End_keepalive(end, i, new_r_Bad(irg));
                                changed = 1;
                        }
                }
@@ -791,7 +795,7 @@ restart:
                if (is_Block(ka)) {
                        /* do NOT keep dead blocks */
                        if (is_Block_dead(ka) || get_Block_dom_depth(ka) < 0) {
-                               set_End_keepalive(end, i, new_Bad());
+                               set_End_keepalive(end, i, new_r_Bad(irg));
                                changed = 1;
                        }
                } else {
@@ -799,7 +803,7 @@ restart:
 
                        if (is_Bad(block) || is_Block_dead(block) || get_Block_dom_depth(block) < 0) {
                                /* do NOT keep nodes in dead blocks */
-                               set_End_keepalive(end, i, new_Bad());
+                               set_End_keepalive(end, i, new_r_Bad(irg));
                                changed = 1;
                        }
                }
@@ -864,12 +868,15 @@ restart:
        /* 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");
+               if (irg_verify_bads(irg, BAD_DF | BAD_BLOCK | TUPLE)) {
+                       dump_ir_graph(irg, "-verify-cf");
+                       fprintf(stderr, "VERIFY_BAD in optimize_cf()\n");
                }
        }
-
-       current_ir_graph = rem;
 }
+
+/* Creates an ir_graph pass for optimize_cf. */
+ir_graph_pass_t *optimize_cf_pass(const char *name)
+{
+       return def_graph_pass(name ? name : "optimize_cf", optimize_cf);
+}  /* optimize_cf_pass */