X-Git-Url: http://nsz.repo.hu/git/?a=blobdiff_plain;f=ir%2Fopt%2Fcfopt.c;h=43be0a2f04ee136c4fb9ba469ef0f05fb3221f79;hb=847c1b62a1eae1e8055645996049f7f5db0a0d8b;hp=df3700402084518f259f46f2bde306042e9c9f25;hpb=f8b8a445d2c65da173ad640978a5687761a3a620;p=libfirm diff --git a/ir/opt/cfopt.c b/ir/opt/cfopt.c index df3700402..43be0a2f0 100644 --- a/ir/opt/cfopt.c +++ b/ir/opt/cfopt.c @@ -21,15 +21,19 @@ * @file * @brief Control flow optimizations. * @author Goetz Lindenmaier, Michael Beck, Sebastian Hack - * @version $Id$ + * + * Removes Bad control flow predecessors and empty blocks. A block is empty + * if it contains only a Jmp node. Blocks can only be removed if they are not + * needed for the semantics of Phi nodes. Further, we NEVER remove labeled + * blocks (even if we could move the label). */ #include "config.h" #include "iroptimize.h" #include +#include -#include "plist.h" #include "xmalloc.h" #include "irnode_t.h" #include "irgraph_t.h" @@ -40,7 +44,7 @@ #include "irgwalk.h" #include "irgmod.h" #include "irdump.h" -#include "irvrfy.h" +#include "irverify.h" #include "iredges.h" #include "array_t.h" @@ -50,186 +54,48 @@ #include "irflag_t.h" #include "firmstat.h" +#include "irpass.h" +#include "irnodehashmap.h" #include "irtools.h" #include "iropt_dbg.h" -/*------------------------------------------------------------------*/ -/* Control flow optimization. */ -/* */ -/* Removes Bad control flow predecessors and empty blocks. A block */ -/* is empty if it contains only a Jmp node. */ -/* Blocks can only be removed if they are not needed for the */ -/* semantics of Phi nodes. */ -/* Further, we NEVER remove labeled blocks (even if we could move */ -/* the label. */ -/*------------------------------------------------------------------*/ - -#define set_Block_removable(block) set_Block_mark(block, 1) -#define set_Block_non_removable(block) set_Block_mark(block, 0) -#define is_Block_removable(block) (get_Block_mark(block) != 0) - -/** - * Replace binary Conds that jumps twice into the same block - * by a simple Jmp. - * E.g. - * @verbatim - * Cond Jmp Bad - * / \ | / - * ProjX True ProjX False ==> | / - * \ / | / - * Block Block - * @endverbatim - * - * Such pattern are the result of if-conversion. - * - * 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) { - int i, j; - int n = get_Block_n_cfgpreds(bl); - int changed = 0; - - for (i = 0; i < n; ++i) { - ir_node *pred_i = get_Block_cfgpred(bl, i); - ir_node *cond_i = skip_Proj(pred_i); - - /* binary Cond */ - if (is_Cond(cond_i) && get_irn_mode(get_Cond_selector(cond_i)) == mode_b) { - - for (j = i + 1; j < n; ++j) { - ir_node *pred_j = get_Block_cfgpred(bl, j); - ir_node *cond_j = skip_Proj(pred_j); - - if (cond_j == cond_i) { - ir_node *jmp = new_r_Jmp(get_nodes_block(cond_i)); - set_irn_n(bl, i, jmp); - set_irn_n(bl, j, new_Bad()); - - DBG_OPT_IFSIM2(cond_i, jmp); - changed = 1; - break; - } - } - } - } - return changed; -} - /** An environment for merge_blocks and collect nodes. */ -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. */ +typedef struct merge_env { + bool changed; /**< Set if the graph was changed. */ + bool phis_moved; /**< Set if Phi nodes were moved. */ } merge_env; -/** - * Removes Tuples from Block control flow predecessors. - * Optimizes blocks with equivalent_node(). This is tricky, - * as we want to avoid nodes that have as block predecessor Bads. - * Therefore we also optimize at control flow operations, depending - * how we first reach the Block. - */ -static void merge_blocks(ir_node *node, void *ctx) { - int i; - ir_node *new_block; - merge_env *env = ctx; - - /* clear the link field for ALL nodes first */ - set_irn_link(node, NULL); - - if (is_Block(node)) { - /* Remove Tuples */ - for (i = get_Block_n_cfgpreds(node) - 1; i >= 0; --i) { - ir_node *pred = get_Block_cfgpred(node, i); - ir_node *skipped = skip_Tuple(pred); - if (pred != skipped) { - set_Block_cfgpred(node, i, skipped); - env->changed = 1; - } - } - - /* see below */ - new_block = equivalent_node(node); - if (new_block != node && ! is_Block_dead(new_block)) { - exchange(node, new_block); - env->changed = 1; - } +/** set or reset the removable property of a block. */ +static void set_Block_removable(ir_node *block, bool removable) +{ + set_Block_mark(block, removable); +} - } else if (get_opt_optimize() && (get_irn_mode(node) == mode_X)) { - /* We will soon visit a block. Optimize it before visiting! */ - ir_node *b = get_nodes_block(skip_Proj(node)); - - if (!is_Block_dead(b)) { - new_block = equivalent_node(b); - - while (!irn_visited(b) && !is_Block_dead(new_block) && new_block != b) { - /* We would have to run gigo() if new is bad, so we - 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; - new_block = equivalent_node(b); - } +/** check if a block has the removable property set. */ +static bool is_Block_removable(const ir_node *block) +{ + return get_Block_mark(block); +} - /* normally, we would create a Bad block here, but this must be - * prevented, so just set it's cf to Bad. - */ - if (is_Block_dead(new_block)) { - exchange(node, new_Bad()); - env->changed = 1; - } - } - } +/** checks if a given Cond node is a switch Cond. */ +static bool is_switch_Cond(const ir_node *cond) +{ + ir_node *sel = get_Cond_selector(cond); + return get_irn_mode(sel) != mode_b; } -/** - * Block walker removing control flow from dead block by - * inspecting dominance info. - * Do not replace blocks by Bad. This optimization shall - * ensure, that all Bad control flow predecessors are - * removed, and no new other Bads are introduced. - * Further removed useless Conds and clear the mark of all blocks. - * - * Must be run in the post walker. - */ -static void remove_unreachable_blocks_and_conds(ir_node *block, void *env) { - int i; - int *changed = env; - - /* Check block predecessors and turn control flow into bad. - Beware of Tuple, kill them. */ - for (i = get_Block_n_cfgpreds(block) - 1; i >= 0; --i) { - ir_node *pred_X = get_Block_cfgpred(block, i); - ir_node *skipped = skip_Tuple(pred_X); - - if (! is_Bad(skipped)) { - ir_node *pred_bl = get_nodes_block(skip_Proj(skipped)); - - if (is_Block_dead(pred_bl) || (get_Block_dom_depth(pred_bl) < 0)) { - set_Block_dead(pred_bl); - exchange(pred_X, new_Bad()); - *changed = 1; - } else if (skipped != pred_X) { - set_Block_cfgpred(block, i, skipped); - *changed = 1; - } - } +/** 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); + if (is_Block(node)) { + set_Block_removable(node, true); + set_Block_phis(node, NULL); + } else if (is_Phi(node)) { + set_Phi_next(node, NULL); } - - *changed |= remove_senseless_conds(block); - - /* clear the block mark of all non labeled blocks */ - if (has_Block_entity(block)) - set_Block_non_removable(block); - else - set_Block_removable(block); } /** @@ -239,50 +105,47 @@ 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) { - 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_entity(n)) - set_Block_non_removable(n); +static void collect_nodes(ir_node *n, void *ctx) +{ + (void) ctx; + if (is_Phi(n)) { + /* Collect Phi nodes to compact ins along with block's ins. */ + ir_node *block = get_nodes_block(n); + add_Block_phi(block, n); + } else if (is_Block(n)) { + if (get_Block_entity(n) != NULL) { + /* block with a jump label attached cannot be removed. */ + set_Block_removable(n, false); + } + } else if (is_Bad(n) || is_Jmp(n)) { + /* ignore these */ + return; } else { - ir_node *b = get_nodes_block(n); - - if (code == iro_Phi && get_irn_arity(n) > 0) { - /* Collect Phi nodes to compact ins along with block's ins. */ - set_irn_link(n, get_irn_link(b)); - set_irn_link(b, n); - } else if (code != iro_Jmp && !is_Bad(b)) { /* Check for non-empty block. */ - set_Block_non_removable(b); - - if (code == iro_Proj) { /* link Proj nodes */ - ir_node *pred = get_Proj_pred(n); - - set_irn_link(n, get_irn_link(pred)); - set_irn_link(pred, n); - } else if (code == iro_Cond) { - ir_node *sel = get_Cond_selector(n); - if (mode_is_int(get_irn_mode(sel))) { - /* found a switch-Cond, collect */ - plist_insert_back(env->list, n); - } - } + /* Check for non-empty block. */ + ir_node *block = get_nodes_block(n); + + set_Block_removable(block, false); + + if (is_Proj(n)) { + /* link Proj nodes */ + ir_node *pred = get_Proj_pred(n); + set_irn_link(n, get_irn_link(pred)); + set_irn_link(pred, n); } } } -/** Returns true if pred is predecessor of block. */ -static int is_pred_of(ir_node *pred, ir_node *b) { +/** Returns true if pred is predecessor of block b. */ +static bool is_pred_of(const ir_node *pred, const ir_node *b) +{ int i; for (i = get_Block_n_cfgpreds(b) - 1; i >= 0; --i) { ir_node *b_pred = get_Block_cfgpred_block(b, i); if (b_pred == pred) - return 1; + return true; } - return 0; + return false; } /** Test whether we can optimize away pred block pos of b. @@ -312,61 +175,87 @@ 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) { - int i, j, n_preds = 1; - ir_node *pred = get_Block_cfgpred_block(b, pos); - - /* Bad blocks will be optimized away, so we don't need space for them */ - if (is_Block_dead(pred)) - 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; - } +static unsigned test_whether_dispensable(const ir_node *b, int pos) +{ + ir_node *pred = get_Block_cfgpred(b, pos); + ir_node *predb = get_nodes_block(pred); - /* 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. */ - n_preds = get_Block_n_cfgpreds(pred); - } else { - /* b's pred blocks and pred's pred blocks must be pairwise disjunct. - Handle all pred blocks with preds < pos as if they were already removed. */ - for (i = 0; i < pos; i++) { - ir_node *b_pred = get_Block_cfgpred_block(b, i); - if (! is_Block_dead(b_pred) && is_Block_removable(b_pred)) { - for (j = get_Block_n_cfgpreds(b_pred) - 1; j >= 0; --j) { - ir_node *b_pred_pred = get_Block_cfgpred_block(b_pred, j); - if (is_pred_of(b_pred_pred, pred)) - goto non_dispensable; - } - } else { - if (is_pred_of(b_pred, pred)) + if (is_Bad(pred) || !is_Block_removable(predb)) + return 1; + + /* can't remove self-loops */ + if (predb == b) + goto non_dispensable; + if (is_unknown_jump(pred)) + goto non_dispensable; + + /* Seems to be empty. At least we detected this in collect_nodes. */ + if (get_Block_phis(b) != NULL) { + int n_cfgpreds = get_Block_n_cfgpreds(b); + int i; + /* there are Phi nodes */ + + /* b's pred blocks and pred's pred blocks must be pairwise disjunct. + * Handle all pred blocks with preds < pos as if they were already + * removed. */ + for (i = 0; i < pos; i++) { + ir_node *other_pred = get_Block_cfgpred(b, i); + ir_node *other_predb = get_nodes_block(other_pred); + if (is_Bad(other_pred)) + continue; + if (is_Block_removable(other_predb) + && !Block_block_visited(other_predb)) { + int j; + for (j = get_Block_n_cfgpreds(other_predb) - 1; j >= 0; --j) { + ir_node *other_predpred + = get_Block_cfgpred_block(other_predb, j); + if (is_pred_of(other_predpred, predb)) goto non_dispensable; } + } else if (is_pred_of(other_predb, predb)) { + goto non_dispensable; } - for (i = pos +1; i < get_Block_n_cfgpreds(b); i++) { - ir_node *b_pred = get_Block_cfgpred_block(b, i); - if (is_pred_of(b_pred, pred)) - goto non_dispensable; - } - /* if we get here, the block is dispensable */ - n_preds = get_Block_n_cfgpreds(pred); + } + for (i = pos+1; i < n_cfgpreds; i++) { + ir_node *other_predb = get_Block_cfgpred_block(b, i); + if (is_pred_of(other_predb, predb)) + goto non_dispensable; } } - - return n_preds; + /* we will not dispense already visited blocks */ + if (Block_block_visited(predb)) + return 1; + /* if we get here, the block is dispensable, count useful preds */ + return get_irn_arity(predb); non_dispensable: - set_Block_non_removable(pred); + set_Block_removable(predb, false); return 1; } /** - * This method removed Bad cf predecessors from Blocks and Phis, and removes - * empty blocks. A block is empty if it only contains Phi and Jmp nodes. + * This method merges blocks. A block is applicable to be merged, if it + * has only one predecessor with an unconditional jump to this block; + * and if this block does not contain any phis. + */ +static void merge_blocks(ir_node *b, void *env) +{ + (void) env; + + if (get_Block_n_cfgpreds(b) == 1) { + ir_node* pred = get_Block_cfgpred(b, 0); + if (is_Jmp(pred)) { + ir_node* pred_block = get_nodes_block(pred); + if (get_Block_phis(b) == NULL) { + exchange(b, pred_block); + } + } + } +} + +/** + * This method removes empty blocks. A block is empty if it only contains Phi + * and Jmp nodes. * * We first adapt Phi nodes, then Block nodes, as we need the old ins * of the Block to adapt the Phi nodes. We do this by computing new @@ -374,20 +263,22 @@ non_dispensable: * for all nodes, not regarding whether there is a possibility for optimization. * * For each predecessor p of a Block b there are three cases: - * -1. The predecessor p is a Bad node: just skip it. The in array of b shrinks by one. - * -2. The predecessor p is empty. Remove p. All predecessors of p are now - * predecessors of b. - * -3. The predecessor p is a block containing useful code. Just keep p as is. + * - The predecessor p is a Bad node: just skip it. The in array of b shrinks + * by one. + * - The predecessor p is empty. Remove p. All predecessors of p are now + * predecessors of b. + * - The predecessor p is a block containing useful code. Just keep p as is. * * For Phi nodes f we have to check the conditions at the Block of f. * For cases 1 and 3 we proceed as for Blocks. For case 2 we can have two * cases: - * -2a: The old predecessor of the Phi f is a Phi pred_f IN THE BLOCK REMOVED. In this - * case we proceed as for blocks. We remove pred_f. All - * predecessors of pred_f now are predecessors of f. - * -2b: The old predecessor of f is NOT in the block removed. It might be a Phi, too. - * We have to replicate f for each predecessor of the removed block. Or, with - * other words, the removed predecessor block has exactly one predecessor. + * -2a: The old predecessor of the Phi f is a Phi pred_f IN THE BLOCK REMOVED. + * In this case we proceed as for blocks. We remove pred_f. All + * predecessors of pred_f now are predecessors of f. + * -2b: The old predecessor of f is NOT in the block removed. It might be a Phi + * too. We have to replicate f for each predecessor of the removed block. + * Or, with other words, the removed predecessor block has exactly one + * predecessor. * * Further there is a special case for self referencing blocks: * @verbatim @@ -408,14 +299,19 @@ non_dispensable: * If there is a Phi in pred_b, but we remove pred_b, we have to generate a * Phi in loop_b, that has the ins of the Phi in pred_b and a self referencing * backedge. - * @@@ 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 *phi; + ir_node *next; ir_node **in; - merge_env *env = ctx; + 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. */ @@ -426,34 +322,43 @@ static void optimize_blocks(ir_node *b, void *ctx) { in = XMALLOCN(ir_node*, max_preds); /*- Fix the Phi nodes of the current block -*/ - for (phi = get_irn_link(b); phi != NULL; phi = next) { - assert(is_Phi(phi)); - next = get_irn_link(phi); + for (phi = get_Block_phis(b); phi != NULL; phi = next) { + next = get_Phi_next(phi); /* Find the new predecessors for the Phi */ p_preds = 0; for (i = 0, n = get_Block_n_cfgpreds(b); i < n; ++i) { - pred = get_Block_cfgpred_block(b, i); + ir_graph *irg = get_irn_irg(b); + ir_node *predx = get_Block_cfgpred(b, i); + ir_node *pred; + + /* case Phi 1: maintain Bads, as somebody else is responsible to + * remove them */ + if (is_Bad(predx)) { + in[p_preds++] = new_r_Bad(irg, get_irn_mode(phi)); + continue; + } - if (is_Block_dead(pred)) { - /* case Phi 1: Do nothing */ - } else if (is_Block_removable(pred) && !Block_block_visited(pred)) { - /* case Phi 2: It's an empty block and not yet visited. */ + pred = get_nodes_block(predx); + + /* case Phi 2: It's an empty block and not yet visited. */ + if (is_Block_removable(pred) && !Block_block_visited(pred)) { ir_node *phi_pred = get_Phi_pred(phi, i); for (j = 0, k = get_Block_n_cfgpreds(pred); j < k; j++) { - /* because of breaking loops, not all predecessors are Bad-clean, - * so we must check this here again */ - if (! is_Bad(get_Block_cfgpred(pred, j))) { - if (get_nodes_block(phi_pred) == pred) { - /* case Phi 2a: */ - assert(is_Phi(phi_pred)); /* Block is empty!! */ - - in[p_preds++] = get_Phi_pred(phi_pred, j); - } else { - /* case Phi 2b: */ - in[p_preds++] = phi_pred; - } + ir_node *pred_pred = get_Block_cfgpred(pred, j); + + if (is_Bad(pred_pred)) { + in[p_preds++] = new_r_Bad(irg, get_irn_mode(phi)); + continue; + } + + if (get_nodes_block(phi_pred) == pred) { + /* case Phi 2a: */ + in[p_preds++] = get_Phi_pred(phi_pred, j); + } else { + /* case Phi 2b: */ + in[p_preds++] = phi_pred; } } } else { @@ -461,55 +366,73 @@ static void optimize_blocks(ir_node *b, void *ctx) { in[p_preds++] = get_Phi_pred(phi, i); } } - assert(p_preds <= max_preds); + assert(p_preds == max_preds); /* Fix the node */ - if (p_preds == 1) - /* By removal of Bad ins the Phi might be degenerated. */ + if (p_preds == 1) { exchange(phi, in[0]); - else + } else { set_irn_in(phi, p_preds, in); - env->changed = 1; + } + env->changed = true; } /*- This happens only if merge between loop backedge and single loop entry. - 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 ... -*/ + 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) { - ir_node *predb = get_nodes_block(get_Block_cfgpred(b, k)); + ir_node *pred = get_Block_cfgpred(b, k); + ir_node *predb = get_nodes_block(pred); + if (is_Bad(pred)) + continue; if (is_Block_removable(predb) && !Block_block_visited(predb)) { ir_node *next_phi; /* we found a predecessor block at position k that will be removed */ - for (phi = get_irn_link(predb); phi; phi = next_phi) { + for (phi = get_Block_phis(predb); phi != NULL; phi = next_phi) { int q_preds = 0; - next_phi = get_irn_link(phi); - - assert(is_Phi(phi)); + next_phi = get_Phi_next(phi); 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()); + /* predb is not the dominator. There can't be uses of + * pred's Phi nodes, kill them .*/ + ir_graph *irg = get_irn_irg(b); + ir_mode *mode = get_irn_mode(phi); + exchange(phi, new_r_Bad(irg, mode)); } 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 */ + /* 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; + set_Phi_next(phi, get_Block_phis(b)); + set_Block_phis(b, phi); + env->phis_moved = true; /* first, copy all 0..k-1 predecessors */ for (i = 0; i < k; i++) { - pred = get_Block_cfgpred_block(b, i); - - if (is_Block_dead(pred)) { - /* Do nothing */ - } else if (is_Block_removable(pred) && !Block_block_visited(pred)) { + ir_node *predx = get_Block_cfgpred(b, i); + ir_node *pred_block; + + if (is_Bad(predx)) { + ir_graph *irg = get_irn_irg(b); + ir_mode *mode = get_irn_mode(phi); + in[q_preds++] = new_r_Bad(irg, mode); + continue; + } + pred_block = get_nodes_block(predx); + if (is_Block_removable(pred_block) + && !Block_block_visited(pred_block)) { + int n_cfgpreds = get_Block_n_cfgpreds(pred_block); /* 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))) + for (j = 0; j < n_cfgpreds; j++) { + if (!is_Bad(get_Block_cfgpred(pred_block, 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; @@ -519,21 +442,27 @@ static void optimize_blocks(ir_node *b, void *ctx) { /* now we are at k, copy the phi predecessors */ pred = get_nodes_block(get_Block_cfgpred(b, k)); for (i = 0; i < get_Phi_n_preds(phi); i++) { - if (! is_Bad(get_Block_cfgpred(pred, i))) - in[q_preds++] = get_Phi_pred(phi, i); + in[q_preds++] = get_Phi_pred(phi, i); } /* and now all the rest */ for (i = k+1; i < get_Block_n_cfgpreds(b); i++) { pred = get_Block_cfgpred_block(b, i); - if (is_Block_dead(pred)) { - /* Do nothing */ + if (is_Bad(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; @@ -545,7 +474,7 @@ static void optimize_blocks(ir_node *b, void *ctx) { exchange(phi, in[0]); else set_irn_in(phi, q_preds, in); - env->changed = 1; + env->changed = true; assert(q_preds <= max_preds); // assert(p_preds == q_preds && "Wrong Phi Fix"); @@ -557,273 +486,403 @@ static void optimize_blocks(ir_node *b, void *ctx) { /*- Fix the block -*/ n_preds = 0; for (i = 0; i < get_Block_n_cfgpreds(b); i++) { - pred = get_Block_cfgpred_block(b, i); - - if (is_Block_dead(pred)) { - /* case 1: Do nothing */ - } else if (is_Block_removable(pred) && !Block_block_visited(pred)) { + ir_node *pred = get_Block_cfgpred(b, i); + ir_node *predb = get_nodes_block(pred); + ir_graph *irg = get_irn_irg(pred); + + /* case 1: Bad predecessor */ + if (is_Bad(pred)) { + in[n_preds++] = new_r_Bad(irg, mode_X); + continue; + } + if (is_Block_removable(predb) && !Block_block_visited(predb)) { /* case 2: It's an empty block and not yet visited. */ - 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); - - /* because of breaking loops, not all predecessors are Bad-clean, - * so we must check this here again */ - if (! is_Bad(pred_X)) - in[n_preds++] = pred_X; + for (j = 0; j < get_Block_n_cfgpreds(predb); j++) { + ir_node *predpred = get_Block_cfgpred(predb, j); + + if (is_Bad(predpred)) { + in[n_preds++] = new_r_Bad(irg, mode_X); + continue; + } + + in[n_preds++] = predpred; } - /* Remove block as it might be kept alive. */ - exchange(pred, b/*new_Bad()*/); + /* Remove block+jump as it might be kept alive. */ + exchange(pred, new_r_Bad(get_irn_irg(b), mode_X)); + exchange(predb, new_r_Bad(get_irn_irg(b), mode_BB)); } else { /* case 3: */ - in[n_preds++] = get_Block_cfgpred(b, i); + in[n_preds++] = pred; } } - assert(n_preds <= max_preds); + assert(n_preds == max_preds); set_irn_in(b, n_preds, in); - env->changed = 1; + env->changed = true; - assert(get_irn_link(b) == NULL || p_preds == -1 || (n_preds == p_preds && "Wrong Phi Fix")); + /* see if phi-fix was correct */ + assert(get_Block_phis(b) == NULL || p_preds == -1 || (n_preds == p_preds)); xfree(in); } /** - * Block walker: optimize all blocks using the default optimizations. - * This removes Blocks that with only a Jmp predecessor. + * Optimize boolean Conds, where true and false jump to the same block into a Jmp + * Block must contain no Phi nodes. + * + * Cond + * / \ + * projA projB => Jmp Bad + * \ / \ / + * block block */ -static void remove_simple_blocks(ir_node *block, void *ctx) { - ir_node *new_blk = equivalent_node(block); - merge_env *env = ctx; +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; +} - if (new_blk != block) { - exchange(block, new_blk); - env->changed = 1; - } +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_block_flag(const ir_nodehashmap_t *infos, const ir_node *block, + int flag) +{ + return PTR_TO_INT(ir_nodehashmap_get(void, infos, block)) & flag; } -/** - * Handle pre-optimized table switch Cond's. - * During iropt, all Projs from a switch-Cond are already removed except - * the defProj and maybe the taken one. - * The defProj cannot be removed WITHOUT looking backwards, so we do this here. - * - * @param cond the switch-Cond - * - * @return non-zero if a switch-Cond was optimized - * - * Expects all Proj's linked to the cond node - */ -static int handle_switch_cond(ir_node *cond) { - ir_node *sel = get_Cond_selector(cond); +static void set_block_flag(ir_nodehashmap_t *infos, ir_node *block, + block_flags_t flag) +{ + int data = PTR_TO_INT(ir_nodehashmap_get(void, infos, block)); + data |= flag; + ir_nodehashmap_insert(infos, block, INT_TO_PTR(data)); +} - ir_node *proj1 = get_irn_link(cond); - ir_node *proj2 = get_irn_link(proj1); - ir_node *jmp, *blk; +static void clear_block_flag(ir_nodehashmap_t *infos, const ir_node *block) +{ + ir_nodehashmap_remove(infos, block); +} - blk = get_nodes_block(cond); +static bool has_operations(ir_nodehashmap_t *infos, const ir_node *block) +{ + return get_block_flag(infos, block, BF_HAS_OPERATIONS); +} - if (proj2 == NULL) { - /* this Cond has only one Proj: must be the defProj */ - assert(get_Cond_default_proj(cond) == get_Proj_proj(proj1)); - /* convert it into a Jmp */ - 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); - - if (tv != tarval_bad) { - /* we have a constant switch */ - long num = get_tarval_long(tv); - long def_num = get_Cond_default_proj(cond); - - if (def_num == get_Proj_proj(proj1)) { - /* first one is the defProj */ - if (num == get_Proj_proj(proj2)) { - jmp = new_r_Jmp(blk); - exchange(proj2, jmp); - exchange(proj1, new_Bad()); - 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(blk); - exchange(proj1, jmp); - exchange(proj2, new_Bad()); - return 1; - } - } else { - /* neither: strange, Cond was not optimized so far */ - if (num == get_Proj_proj(proj1)) { - jmp = new_r_Jmp(blk); - exchange(proj1, jmp); - exchange(proj2, new_Bad()); - return 1; - } else if (num == get_Proj_proj(proj2)) { - jmp = new_r_Jmp(blk); - exchange(proj2, jmp); - exchange(proj1, new_Bad()); - return 1; - } +static void set_has_operations(ir_nodehashmap_t *infos, ir_node *block) +{ + set_block_flag(infos, block, BF_HAS_OPERATIONS); +} + +static bool has_phis(ir_nodehashmap_t *infos, const ir_node *block) +{ + return get_block_flag(infos, block, BF_HAS_PHIS); +} + +static void set_has_phis(ir_nodehashmap_t *infos, ir_node *block) +{ + set_block_flag(infos, block, BF_HAS_PHIS); +} + +static bool is_unknown_jump_target(ir_nodehashmap_t *infos, const ir_node *block) +{ + return get_block_flag(infos, block, BF_IS_UNKNOWN_JUMP_TARGET); +} + +static void set_is_unknown_jump_target(ir_nodehashmap_t *infos, ir_node *block) +{ + set_block_flag(infos, block, BF_IS_UNKNOWN_JUMP_TARGET); +} + +/** + * Pre-Walker: fill block info information. + */ +static void compute_block_info(ir_node *n, void *x) +{ + ir_nodehashmap_t *infos = (ir_nodehashmap_t*)x; + + if (is_Block(n)) { + int i, max = get_Block_n_cfgpreds(n); + for (i=0; iblock_infos, block)) + return; - /* if the graph is not pinned, we cannot determine empty blocks */ - assert(get_irg_pinned(irg) != op_pin_state_floats && - "Control flow optimization need a pinned graph"); + /* 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); + } + } +} - current_ir_graph = irg; +/** + * Pre-Block walker: remove empty blocks (only contain a Jmp) + * that are control flow 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; + int n_jpreds = 0; + + jmp = get_Block_cfgpred(block, i); + if (!is_Jmp(jmp)) + continue; + jmp_block = get_nodes_block(jmp); + if (jmp_block == block) + continue; /* this infinite loop cannot be optimized any further */ + if (is_unknown_jump_target(&env->block_infos, jmp_block)) + continue; /* unknown jump target must not be optimized */ + if (has_phis(&env->block_infos,jmp_block)) + continue; /* this block contains Phis and is not skipped */ + if (Block_block_visited(jmp_block)) { + continue; + /* otherwise we could break the walker, + * if block was reached via + * KeepAlive edge -> jmp_block -> A ---> block, + * because the walker cannot handle Id nodes. + * + * A B + * \ / + * jmp_block + * / \ + * block End + */ + } - /* FIXME: control flow opt destroys block edges. So edges are deactivated here. Fix the edges! */ - edges_deactivate(irg); + /* jmp_block is an empty block and can be optimized! */ - /* we use the mark flag to mark removable blocks */ - ir_reserve_resources(irg, IR_RESOURCE_BLOCK_MARK); -restart: - env.changed = 0; - env.phis_moved = 0; - - /* ALWAYS kill unreachable control flow. Backend cannot handle it anyway. - Use dominator info to kill blocks. Also optimize useless Conds. */ - assure_doms(irg); - irg_block_walk_graph(irg, NULL, remove_unreachable_blocks_and_conds, &env.changed); - - /* fix the keep-alives */ - changed = 0; - for (i = 0, n = get_End_n_keepalives(end); i < n; ++i) { - ir_node *ka = get_End_keepalive(end, i); - - 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()); - changed = 1; + n_jpreds = get_Block_n_cfgpreds(jmp_block); + /** + * If the jmp block has only one predecessor this is straightforward. + * However, if there are more predecessors, we only handle this, + * if block has no Phis. + */ + if (n_jpreds == 1) { + ir_node *pred = get_Block_cfgpred(jmp_block, 0); + ir_node *pred_block = get_nodes_block(pred); + if (has_operations(&env->block_infos,jmp_block)) { + if (get_irg_start_block(get_irn_irg(pred_block)) == pred_block) + continue; /* must not merge operations into start block */ + if (!is_Jmp(pred)) + continue; /* must not create partially dead code, especially when it is mode_M */ } - } else { - ir_node *block = get_nodes_block(ka); - 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()); - changed = 1; + /* skip jmp block by rerouting its predecessor to block + * + * A A + * | | + * jmp_block => | + * | | + * block block + */ + exchange(jmp, pred); + + /* cleanup: jmp_block might have a Keep edge! */ + exchange(jmp_block, pred_block); + env->changed = true; + } else if ( !has_phis(&env->block_infos, block) && + !has_operations(&env->block_infos,jmp_block)) + { + /* all predecessors can skip the jmp block, so block gets some new + * predecessors + * + * A B A B + * \ / | | + * jmp_block C => Bad C | | + * \ / \ | | / + * block block + */ + ir_node **ins = ALLOCAN(ir_node*, n_preds+n_jpreds); + int j; + /* first copy the old predecessors, because the outer loop (i) + * still walks over them */ + for (j = 0; j < n_preds; ++j) { + ins[j] = get_Block_cfgpred(block, j); + } + /* now append the new predecessors */ + for (j = 0; j < n_jpreds; ++j) { + ir_node *pred = get_Block_cfgpred(jmp_block, j); + ins[n_preds+j] = pred; } + set_irn_in(block, n_preds+n_jpreds, ins); + /* convert the jmp_block to Bad */ + ir_graph *irg = get_irn_irg(block); + exchange(jmp_block, new_r_Bad(irg, mode_BB)); + exchange(jmp, new_r_Bad(irg, mode_X)); + /* let the outer loop walk over the new predecessors as well */ + n_preds += n_jpreds; + env->changed = true; + // TODO What if jmp_block had a KeepAlive edge? + } else { + /* This would involve Phis ... */ } } - env.changed |= changed; - - ir_reserve_resources(irg, IR_RESOURCE_IRN_LINK); +} - env.list = plist_new(); - irg_walk(end, merge_blocks, collect_nodes, &env); +/* + * All cfg optimizations, which do not touch Phi nodes. + * + * Note that this might create critical edges. + */ +static void cfgopt_ignoring_phis(ir_graph *irg) +{ + skip_env env; - ir_free_resources(irg, IR_RESOURCE_IRN_LINK); + env.changed = true; + ir_nodehashmap_init(&env.block_infos); - if (env.changed) { - /* Handle graph state if was changed. */ - set_irg_outs_inconsistent(irg); - set_irg_doms_inconsistent(irg); - set_irg_extblk_inconsistent(irg); - set_irg_loopinfo_inconsistent(irg); - set_irg_entity_usage_state(irg, ir_entity_usage_not_computed); - env.changed = 0; - } + while (env.changed) { + irg_walk_graph(irg, compute_block_info, NULL, &env.block_infos); + env.changed = false; - /* handle all collected switch-Conds */ - foreach_plist(env.list, el) { - cond = plist_element_get_value(el); - env.changed |= handle_switch_cond(cond); - } - plist_free(env.list); + /* Remove blocks, which only consist of a Jmp */ + irg_block_walk_graph(irg, remove_empty_blocks, NULL, &env); - if (env.changed) { - /* The Cond optimization might generate unreachable code, so restart if - it happens. */ - goto restart; - } + /* Optimize Cond->Jmp, where then- and else-block are the same. */ + irg_block_walk_graph(irg, NULL, optimize_ifs, &env); - /* Optimize the standard code. */ - env.changed = 0; - assure_doms(irg); - irg_block_walk_graph(irg, optimize_blocks, remove_simple_blocks, &env); - - /* in rare cases a node may be kept alive more than once, use the visited flag to detect this */ - ir_reserve_resources(irg, IR_RESOURCE_IRN_VISITED); - inc_irg_visited(irg); - - /* fix the keep-alives again */ - changed = 0; - for (i = 0, n = get_End_n_keepalives(end); i < n; ++i) { - ir_node *ka = get_End_keepalive(end, i); - - 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()); - changed = 1; - } + if (env.changed) { + confirm_irg_properties(irg, IR_GRAPH_PROPERTIES_NONE); + /* clear block info, because it must be recomputed */ + irg_block_walk_graph(irg, clear_block_info, NULL, &env.block_infos); + /* Removing blocks and Conds might enable more optimizations */ + continue; } else { - ir_node *block = get_nodes_block(ka); - - 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()); - changed = 1; - } + confirm_irg_properties(irg, IR_GRAPH_PROPERTIES_ALL); + break; } } - env.changed |= changed; - remove_End_Bads_and_doublets(end); + ir_nodehashmap_destroy(&env.block_infos); +} + +/* Optimizations of the control flow that also require changes of Phi nodes. */ +void optimize_cf(ir_graph *irg) +{ + int i, j, n; + ir_node **in = NULL; + ir_node *end = get_irg_end(irg); + ir_node *new_end; + merge_env env; + + env.changed = false; + env.phis_moved = false; + + /* if the graph is not pinned, we cannot determine empty blocks */ + assert(get_irg_pinned(irg) != op_pin_state_floats && + "Control flow optimization need a pinned graph"); + assure_irg_properties(irg, IR_GRAPH_PROPERTY_NO_UNREACHABLE_CODE); - ir_free_resources(irg, IR_RESOURCE_BLOCK_MARK | IR_RESOURCE_IRN_VISITED); + /* First the "simple" optimizations, which do not touch Phis */ + 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 + | IR_RESOURCE_PHI_LIST); + + /* + * 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. + */ + irg_walk(end, clear_link_and_mark_blocks_removable, collect_nodes, NULL); + + /* assert due to collect_nodes: + * 1. removable blocks are now marked as such + * 2. phi lists are up to date + */ + + /* 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_irg_properties(irg, IR_GRAPH_PROPERTY_CONSISTENT_DOMINANCE); + irg_block_walk_graph(irg, optimize_blocks, merge_blocks, &env); + + new_end = optimize_in_place(end); + if (new_end != end) { + set_irg_end(irg, new_end); + end = new_end; + } + remove_End_Bads_and_doublets(end); + + ir_free_resources(irg, IR_RESOURCE_BLOCK_MARK | IR_RESOURCE_IRN_LINK + | IR_RESOURCE_PHI_LIST); 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) { NEW_ARR_A(ir_node *, in, n); - 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) { @@ -847,36 +906,17 @@ restart: } if (j != n) { set_End_keepalives(end, j, in); - env.changed = 1; + env.changed = true; } } } - if (env.changed) { - /* Handle graph state if was changed. */ - set_irg_outs_inconsistent(irg); - set_irg_doms_inconsistent(irg); - set_irg_extblk_inconsistent(irg); - set_irg_loopinfo_inconsistent(irg); - set_irg_entity_usage_state(irg, ir_entity_usage_not_computed); - } - - - /* 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"); - } - } - - current_ir_graph = rem; + confirm_irg_properties(irg, + env.changed ? IR_GRAPH_PROPERTIES_NONE : IR_GRAPH_PROPERTIES_ALL); } /* Creates an ir_graph pass for optimize_cf. */ -ir_graph_pass_t *optimize_cf_pass(const char *name, int verify, int dump) +ir_graph_pass_t *optimize_cf_pass(const char *name) { - return def_graph_pass(name ? name : "optimize_cf", verify, dump, optimize_cf); -} /* optimize_cf_pass */ + return def_graph_pass(name ? name : "optimize_cf", optimize_cf); +}