remove Cast node
[libfirm] / ir / opt / jumpthreading.c
index 8e6f9fa..34523d7 100644 (file)
  * @brief   Path-Sensitive Jump Threading
  * @date    10. Sep. 2006
  * @author  Christoph Mallon, Matthias Braun
- * @version $Id$
  */
 #include "config.h"
 
 #include "iroptimize.h"
 
 #include <assert.h>
+#include <stdbool.h>
 #include "array_t.h"
 #include "debug.h"
 #include "ircons.h"
 #include "iropt_dbg.h"
 #include "irpass.h"
 #include "vrp.h"
+#include "firmstat_t.h"
 
 #undef AVOID_PHIB
 
-DEBUG_ONLY(static firm_dbg_module_t *dbg);
+DEBUG_ONLY(static firm_dbg_module_t *dbg;)
 
 /**
  * Add the new predecessor x to node node, which is either a Block or a Phi
@@ -57,14 +58,10 @@ DEBUG_ONLY(static firm_dbg_module_t *dbg);
 static void add_pred(ir_node* node, ir_node* x)
 {
        ir_node** ins;
-       int n;
-       int i;
-
-       assert(is_Block(node) || is_Phi(node));
 
-       n = get_irn_arity(node);
+       int const n = get_Block_n_cfgpreds(node);
        NEW_ARR_A(ir_node*, ins, n + 1);
-       for (i = 0; i < n; i++)
+       for (int i = 0; i < n; i++)
                ins[i] = get_irn_n(node, i);
        ins[n] = x;
        set_irn_in(node, n + 1, ins);
@@ -149,8 +146,6 @@ static void construct_ssa(ir_node *orig_block, ir_node *orig_val,
 {
        ir_graph *irg;
        ir_mode *mode;
-       const ir_edge_t *edge;
-       const ir_edge_t *next;
 
        /* no need to do anything */
        if (orig_val == second_val)
@@ -167,7 +162,7 @@ static void construct_ssa(ir_node *orig_block, ir_node *orig_val,
        ssa_second_def       = second_val;
 
        /* Only fix the users of the first, i.e. the original node */
-       foreach_out_edge_safe(orig_val, edge, next) {
+       foreach_out_edge_safe(orig_val, edge) {
                ir_node *user = get_edge_src_irn(edge);
                int j = get_edge_src_pos(edge);
                ir_node *user_block = get_nodes_block(user);
@@ -194,6 +189,16 @@ static void construct_ssa(ir_node *orig_block, ir_node *orig_val,
        }
 }
 
+/**
+ * jumpthreading produces critical edges, e.g. B-C:
+ *     A         A
+ *  \ /       \  |
+ *   B    =>   B |
+ *  / \       / \|
+ *     C         C
+ *
+ * By splitting this critical edge more threadings might be possible.
+ */
 static void split_critical_edge(ir_node *block, int pos)
 {
        ir_graph *irg = get_irn_irg(block);
@@ -267,8 +272,6 @@ static ir_node *copy_and_fix_node(const jumpthreading_env_t *env,
 static void copy_and_fix(const jumpthreading_env_t *env, ir_node *block,
                          ir_node *copy_block, int j)
 {
-       const ir_edge_t *edge;
-
        /* Look at all nodes in the cond_block and copy them into pred */
        foreach_out_edge(block, edge) {
                ir_node *node = get_edge_src_irn(edge);
@@ -276,31 +279,25 @@ static void copy_and_fix(const jumpthreading_env_t *env, ir_node *block,
                ir_mode *mode;
 
                if (is_End(node)) {
-                       /* edge is a Keep edge. If the end block is unreachable via normal control flow,
-                        * we must maintain end's reachability with Keeps.
+                       /* edge is a Keep edge. If the end block is unreachable via normal
+                        * control flow, we must maintain end's reachability with Keeps.
                         */
                        keep_alive(copy_block);
                        continue;
                }
                /* ignore control flow */
                mode = get_irn_mode(node);
-               if (mode == mode_X || is_Cond(node))
+               if (mode == mode_X || is_Cond(node) || is_Switch(node))
                        continue;
 #ifdef AVOID_PHIB
                /* we may not copy mode_b nodes, because this could produce Phi with
                 * mode_bs which can't be handled in all backends. Instead we duplicate
                 * the node and move it to its users */
                if (mode == mode_b) {
-                       const ir_edge_t *edge, *next;
-                       ir_node *pred;
-                       int      pn;
+                       ir_node *const pred = get_Proj_pred(node);
+                       long     const pn   = get_Proj_proj(node);
 
-                       assert(is_Proj(node));
-
-                       pred = get_Proj_pred(node);
-                       pn   = get_Proj_proj(node);
-
-                       foreach_out_edge_safe(node, edge, next) {
+                       foreach_out_edge_safe(node, edge) {
                                ir_node *cmp_copy;
                                ir_node *user       = get_edge_src_irn(edge);
                                int pos             = get_edge_src_pos(edge);
@@ -337,7 +334,7 @@ static void copy_and_fix(const jumpthreading_env_t *env, ir_node *block,
                ir_mode *mode;
 
                mode = get_irn_mode(node);
-               if (mode == mode_X || is_Cond(node))
+               if (mode == mode_X || is_Cond(node) || is_Switch(node))
                        continue;
 #ifdef AVOID_PHIB
                if (mode == mode_b)
@@ -349,6 +346,17 @@ static void copy_and_fix(const jumpthreading_env_t *env, ir_node *block,
                copy_node = (ir_node*)get_irn_link(node);
                construct_ssa(block, node, copy_block, copy_node);
        }
+
+       /* make sure new nodes are kept alive if old nodes were */
+       ir_graph *irg = get_irn_irg(block);
+       ir_node  *end = get_irg_end(irg);
+       for (int i = 0, arity = get_End_n_keepalives(end); i < arity; ++i) {
+               ir_node *keep = get_End_keepalive(end, i);
+               if (get_irn_visited(keep) < env->visited_nr || is_Block(keep))
+                       continue;
+               ir_node *copy = get_irn_link(keep);
+               add_End_keepalive(end, copy);
+       }
 }
 
 /**
@@ -373,35 +381,6 @@ static int eval_cmp_tv(ir_relation relation, ir_tarval *tv_left,
        return 0;
 }
 
-#if 0
-/* Matze: disabled, check first if the compare still is correct */
-
-/**
- * returns whether the cmp evaluates to true or false according to vrp
- * information , or can't be evaluated!
- * 1: true, 0: false, -1: can't evaluate
- *
- * @param relation  the compare mode of the Compare
- * @param left      the left node
- * @param right     the right node
- */
-static int eval_cmp_vrp(ir_relation relation, ir_node *left, ir_node *right)
-{
-       ir_relation cmp_result = vrp_cmp(left, right);
-       /* does the compare evaluate to true? */
-       if (cmp_result == ir_relation_false)
-               return -1;
-
-       if ((cmp_result & relation) != cmp_result) {
-               if ((cmp_result & relation) != 0) {
-                       return -1;
-               }
-               return 0;
-       }
-       return 1;
-}
-#endif
-
 /**
  * returns whether the cmp evaluates to true or false, or can't be evaluated!
  * 1: true, 0: false, -1: can't evaluate
@@ -462,7 +441,7 @@ static ir_node *find_const_or_confirm(jumpthreading_env_t *env, ir_node *jump,
                DB((
                        dbg, LEVEL_1,
                        "> Found jump threading candidate %+F->%+F\n",
-                       env->true_block, block
+                       block, env->true_block
                ));
 
                /* adjust true_block to point directly towards our jump */
@@ -527,7 +506,7 @@ static ir_node *find_candidate(jumpthreading_env_t *env, ir_node *jump,
                DB((
                        dbg, LEVEL_1,
                        "> Found jump threading candidate %+F->%+F\n",
-                       env->true_block, block
+                       block, env->true_block
                ));
 
                /* adjust true_block to point directly towards our jump */
@@ -623,17 +602,17 @@ static ir_node *find_candidate(jumpthreading_env_t *env, ir_node *jump,
 static void thread_jumps(ir_node* block, void* data)
 {
        jumpthreading_env_t env;
-       int *changed = (int*)data;
+       bool *changed = (bool*)data;
        ir_node *selector;
        ir_node *projx;
        ir_node *cond;
        ir_node *copy_block;
        int      selector_evaluated;
-       const ir_edge_t *edge, *next;
        ir_graph *irg;
        ir_node *badX;
        int      cnst_pos;
 
+       /* we do not deal with Phis, so restrict this to exactly one cfgpred */
        if (get_Block_n_cfgpreds(block) != 1)
                return;
 
@@ -643,15 +622,12 @@ static void thread_jumps(ir_node* block, void* data)
        assert(get_irn_mode(projx) == mode_X);
 
        cond = get_Proj_pred(projx);
-       if (!is_Cond(cond))
-               return;
-
-       selector = get_Cond_selector(cond);
        /* TODO handle switch Conds */
-       if (get_irn_mode(selector) != mode_b)
+       if (!is_Cond(cond))
                return;
 
        /* handle cases that can be immediately evaluated */
+       selector = get_Cond_selector(cond);
        selector_evaluated = -1;
        if (is_Cmp(selector)) {
                ir_node *left  = get_Cmp_left(selector);
@@ -663,15 +639,6 @@ static void thread_jumps(ir_node* block, void* data)
 
                        selector_evaluated = eval_cmp_tv(relation, tv_left, tv_right);
                }
-#if 0
-               if (selector_evaluated < 0) {
-                       /* This is only the case if the predecessor nodes are not
-                        * constant or the comparison could not be evaluated.
-                        * Try with VRP information now.
-                        */
-                       selector_evaluated = eval_cmp_vrp(relation, left, right);
-               }
-#endif
        } else if (is_Const_or_Confirm(selector)) {
                ir_tarval *tv = get_Const_or_Confirm_tarval(selector);
                if (tv == tarval_b_true) {
@@ -695,14 +662,14 @@ static void thread_jumps(ir_node* block, void* data)
                ir_graph *irg = get_irn_irg(block);
                ir_node  *bad = new_r_Bad(irg, mode_X);
                exchange(projx, bad);
-               *changed = 1;
+               *changed = true;
                return;
        } else if (selector_evaluated == 1) {
                dbg_info *dbgi = get_irn_dbg_info(selector);
                ir_node  *jmp  = new_rd_Jmp(dbgi, get_nodes_block(projx));
                DBG_OPT_JUMPTHREADING(projx, jmp);
                exchange(projx, jmp);
-               *changed = 1;
+               *changed = true;
                return;
        }
 
@@ -716,6 +683,10 @@ static void thread_jumps(ir_node* block, void* data)
        if (copy_block == NULL)
                return;
 
+       /* We might thread the condition block of an infinite loop,
+        * such that there is no path to End anymore. */
+       keep_alive(block);
+
        /* we have to remove the edge towards the pred as the pred now
         * jumps into the true_block. We also have to shorten Phis
         * in our block because of this */
@@ -723,7 +694,7 @@ static void thread_jumps(ir_node* block, void* data)
        cnst_pos = env.cnst_pos;
 
        /* shorten Phis */
-       foreach_out_edge_safe(env.cnst_pred, edge, next) {
+       foreach_out_edge_safe(env.cnst_pred, edge) {
                ir_node *node = get_edge_src_irn(edge);
 
                if (is_Phi(node)) {
@@ -735,45 +706,40 @@ static void thread_jumps(ir_node* block, void* data)
        set_Block_cfgpred(env.cnst_pred, cnst_pos, badX);
 
        /* the graph is changed now */
-       *changed = 1;
+       *changed = true;
 }
 
 void opt_jumpthreading(ir_graph* irg)
 {
-       int changed, rerun;
+       bool changed;
+       bool rerun;
+
+       assure_irg_properties(irg,
+               IR_GRAPH_PROPERTY_NO_UNREACHABLE_CODE
+               | IR_GRAPH_PROPERTY_CONSISTENT_OUT_EDGES
+               | IR_GRAPH_PROPERTY_NO_CRITICAL_EDGES);
 
        FIRM_DBG_REGISTER(dbg, "firm.opt.jumpthreading");
 
        DB((dbg, LEVEL_1, "===> Performing jumpthreading on %+F\n", irg));
 
-       remove_critical_cf_edges(irg);
-
-       /* ugly: jump threading might get confused by garbage nodes
-        * of mode_X in copy_and_fix_node(), so remove all garbage edges. */
-       edges_deactivate(irg);
-
-       edges_assure(irg);
        ir_reserve_resources(irg, IR_RESOURCE_IRN_LINK | IR_RESOURCE_IRN_VISITED);
 
-       changed = 0;
+       changed = false;
        do {
-               rerun = 0;
+               rerun = false;
                irg_block_walk_graph(irg, thread_jumps, NULL, &rerun);
                changed |= rerun;
        } while (rerun);
 
        ir_free_resources(irg, IR_RESOURCE_IRN_LINK | IR_RESOURCE_IRN_VISITED);
 
-       if (changed) {
-               /* control flow changed, some blocks may become dead */
-               set_irg_doms_inconsistent(irg);
-               set_irg_extblk_inconsistent(irg);
-               set_irg_entity_usage_state(irg, ir_entity_usage_not_computed);
-       }
+       confirm_irg_properties(irg,
+               changed ? IR_GRAPH_PROPERTIES_NONE : IR_GRAPH_PROPERTIES_ALL);
 }
 
 /* Creates an ir_graph pass for opt_jumpthreading. */
 ir_graph_pass_t *opt_jumpthreading_pass(const char *name)
 {
        return def_graph_pass(name ? name : "jumpthreading", opt_jumpthreading);
-}  /* opt_jumpthreading_pass */
+}