ia32: Remove the ia32_x87_attr_t from ia32_asm_attr_t.
[libfirm] / ir / opt / jumpthreading.c
index 3a18401..f7b76f2 100644 (file)
@@ -28,6 +28,7 @@
 #include "iroptimize.h"
 
 #include <assert.h>
+#include <stdbool.h>
 #include "array_t.h"
 #include "debug.h"
 #include "ircons.h"
@@ -45,7 +46,7 @@
 #include "iropt_dbg.h"
 #include "irpass.h"
 #include "vrp.h"
-#include "opt_manage.h"
+#include "firmstat_t.h"
 
 #undef AVOID_PHIB
 
@@ -149,8 +150,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 +166,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);
@@ -277,8 +276,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);
@@ -286,8 +283,8 @@ 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;
@@ -301,7 +298,6 @@ static void copy_and_fix(const jumpthreading_env_t *env, ir_node *block,
                 * 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;
 
@@ -310,7 +306,7 @@ static void copy_and_fix(const jumpthreading_env_t *env, ir_node *block,
                        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);
@@ -359,6 +355,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);
+       }
 }
 
 /**
@@ -472,7 +479,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 */
@@ -537,7 +544,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 */
@@ -633,13 +640,12 @@ 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;
@@ -703,14 +709,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;
        }
 
@@ -735,7 +741,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)) {
@@ -747,13 +753,18 @@ 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;
 }
 
-static ir_graph_state_t do_jumpthread(ir_graph* irg)
+void opt_jumpthreading(ir_graph* irg)
 {
-       int changed, rerun;
-       ir_graph_state_t res = 0;
+       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");
 
@@ -761,31 +772,17 @@ static ir_graph_state_t do_jumpthread(ir_graph* 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) {
-               res |= IR_GRAPH_STATE_CONSISTENT_DOMINANCE | IR_GRAPH_STATE_CONSISTENT_ENTITY_USAGE;
-       }
-
-       return res;
-}
-
-static optdesc_t opt_jumpthread = {
-       "jumpthreading",
-       IR_GRAPH_STATE_NO_UNREACHABLE_CODE | IR_GRAPH_STATE_CONSISTENT_OUT_EDGES | IR_GRAPH_STATE_NO_CRITICAL_EDGES,
-       do_jumpthread,
-};
-
-void opt_jumpthreading(ir_graph* irg)
-{
-       perform_irg_optimization(irg, &opt_jumpthread);
+       confirm_irg_properties(irg,
+               changed ? IR_GRAPH_PROPERTIES_NONE : IR_GRAPH_PROPERTIES_ALL);
 }
 
 /* Creates an ir_graph pass for opt_jumpthreading. */