X-Git-Url: http://nsz.repo.hu/git/?a=blobdiff_plain;f=ir%2Fopt%2Fcfopt.c;h=6fa73c7bfa20c2ae983c0e85166d16ab5796ba13;hb=fe4dd08ecc09bf5c9e63cc2d2cb756295af7b423;hp=6485694091b52e11e5e211d32b5a532361106993;hpb=73c710d43962a445f4ecd84818b85d7403f9919d;p=libfirm diff --git a/ir/opt/cfopt.c b/ir/opt/cfopt.c index 648569409..6fa73c7bf 100644 --- a/ir/opt/cfopt.c +++ b/ir/opt/cfopt.c @@ -27,6 +27,8 @@ # include "config.h" #endif +#include "iroptimize.h" + #include #include "plist.h" @@ -51,7 +53,6 @@ #include "irflag_t.h" #include "firmstat.h" -#include "cfopt.h" #include "iropt_dbg.h" /*------------------------------------------------------------------*/ @@ -114,8 +115,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; /** @@ -391,11 +393,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 +452,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 +532,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 +571,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 +581,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 +702,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 +762,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. */ @@ -802,6 +807,45 @@ void optimize_cf(ir_graph *irg) { clear_using_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. */ set_irg_outs_inconsistent(irg); @@ -810,6 +854,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. */