X-Git-Url: http://nsz.repo.hu/git/?a=blobdiff_plain;f=ir%2Fopt%2Fcfopt.c;h=ee492eb6bceff70a9a4aff92339b9a6d3aafc873;hb=0910ef4a7bad0429f8f0f29ecc1291c5aa8f3059;hp=765f57eba2af0a07edbfaa7b5b56de8d27bf3825;hpb=8848f5a562224298a63634d7e8333e4b6df21621;p=libfirm diff --git a/ir/opt/cfopt.c b/ir/opt/cfopt.c index 765f57eba..ee492eb6b 100644 --- a/ir/opt/cfopt.c +++ b/ir/opt/cfopt.c @@ -66,22 +66,27 @@ typedef struct merge_env { bool phis_moved; /**< Set if Phi nodes were moved. */ } merge_env; +/** set or reset the removable property of a block. */ static void set_Block_removable(ir_node *block, bool removable) { set_Block_mark(block, removable); } +/** check if a block has the removable property set. */ static bool is_Block_removable(ir_node *block) { return get_Block_mark(block); } -static bool is_switch_Cond(ir_node *cond) { +/** checks if a given Cond node is a switch Cond. */ +static bool is_switch_Cond(ir_node *cond) +{ ir_node *sel = get_Cond_selector(cond); return get_irn_mode(sel) != mode_b; } -static void clear_link(ir_node *node, void *ctx) +/** 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); @@ -106,11 +111,19 @@ static void collect_nodes(ir_node *n, void *ctx) set_irn_link(n, get_irn_link(block)); set_irn_link(block, n); } else if (is_Block(n)) { - if (has_Block_entity(n)) + if (has_Block_entity(n)) { + /* 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 if (!is_Jmp(n)) { /* Check for non-empty block. */ + } else { + /* Check for non-empty block. */ ir_node *block = get_nodes_block(n); + if (is_Bad(block)) + return; + set_Block_removable(block, false); if (is_Proj(n)) { @@ -125,7 +138,7 @@ static void collect_nodes(ir_node *n, void *ctx) } } -/** Returns true if pred is predecessor of block. */ +/** Returns true if pred is predecessor of block b. */ static bool is_pred_of(ir_node *pred, ir_node *b) { int i; @@ -277,6 +290,11 @@ static void optimize_blocks(ir_node *b, void *ctx) ir_node **in; 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. */ max_preds = 0; @@ -373,12 +391,19 @@ static void optimize_blocks(ir_node *b, void *ctx) pred = get_Block_cfgpred_block(b, i); if (is_Bad(pred)) { - in[q_preds++] = 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; @@ -402,8 +427,13 @@ static void optimize_blocks(ir_node *b, void *ctx) } 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; @@ -490,7 +520,7 @@ static bool handle_switch_cond(ir_node *cond) /* handle Cond nodes with constant argument. In this case the localopt rules * should have killed all obviously impossible cases. - * So the only case left to handle here is 1 defaultProj + 1case + * So the only case left to handle here is 1 defaultProj + 1 case * (this one case should be the one taken) */ if (get_irn_link(proj2) == NULL) { ir_tarval *tv = value_of(sel); @@ -537,38 +567,9 @@ static bool handle_switch_cond(ir_node *cond) return false; } -static bool get_phase_flag(ir_phase *block_info, ir_node *block, int offset) { - return ((int)phase_get_irn_data(block_info, block)) & (1<phase,b)) return; + if (has_phis(env->phase, block)) + return; /* 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(b, i, j); + for (i = 0; i < n_preds; ++i) { + for (j = i+1; j < n_preds; ++j) { + optimize_pred_cond(block, i, j); } } } -static void remove_empty_blocks(ir_node *b, void *x) +/** + * 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(b); + int n_preds = get_Block_n_cfgpreds(block); for (i = 0; i < n_preds; ++i) { ir_node *jmp, *jmp_block, *pred, *pred_block; + int n_jpreds = 0; - jmp = get_Block_cfgpred(b, i); - if (!is_Jmp(jmp)) continue; - if (is_unknown_jump(jmp)) continue; + jmp = get_Block_cfgpred(block, i); + if (!is_Jmp(jmp)) + continue; jmp_block = get_nodes_block(jmp); - if (is_unknown_jump_target(env->phase, jmp_block)) continue; - if (has_operations(env->phase,jmp_block)) continue; - /* jmp_block is an empty block! */ + if (jmp_block == block) + continue; /* this infinite loop cannot be optimized any further */ + if (is_unknown_jump_target(env->phase, jmp_block)) + continue; /* unknown jump target must not be optimized */ + if (has_operations(env->phase,jmp_block)) + continue; /* this block contains operations and cannot be skipped */ + if (has_phis(env->phase,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 + */ + } - if (get_Block_n_cfgpreds(jmp_block) != 1) continue; - pred = get_Block_cfgpred(jmp_block, 0); - exchange(jmp, pred); - env->changed = true; + /* jmp_block is an empty block and can be optimized! */ - /* cleanup: jmp_block might have a Keep edge! */ - pred_block = get_nodes_block(pred); - exchange(jmp_block, pred_block); + 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) { + /* skip jmp block by rerouting its predecessor to block + * + * A A + * | | + * jmp_block => | + * | | + * block block + */ + pred = get_Block_cfgpred(jmp_block, 0); + exchange(jmp, pred); + + /* cleanup: jmp_block might have a Keep edge! */ + pred_block = get_nodes_block(pred); + exchange(jmp_block, pred_block); + env->changed = true; + } else if (! has_phis(env->phase, 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 = NULL; + int j; + NEW_ARR_A(ir_node *, ins, n_preds+n_jpreds); + /* 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) { + 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 ... */ + } } } /* - * Some cfg optimizations, which do not touch Phi nodes */ -static void cfgopt_ignoring_phis(ir_graph *irg) { + * All cfg optimizations, which do not touch Phi nodes. + * + * Note that this might create critical edges. + */ +static void cfgopt_ignoring_phis(ir_graph *irg) +{ ir_phase *block_info = new_phase(irg, NULL); - skip_env env = { false, block_info }; - - irg_walk_graph(irg, compute_block_info, NULL, block_info); + skip_env env = { true, block_info }; - for(;;) { + while (env.changed) { + irg_walk_graph(irg, compute_block_info, NULL, block_info); env.changed = false; - /* Conds => Jmp optimization; might produce empty blocks */ - irg_block_walk_graph(irg, optimize_conds, NULL, &env); - - /* Remove empty blocks */ + /* Remove blocks, which only consist of a Jmp */ irg_block_walk_graph(irg, remove_empty_blocks, NULL, &env); + + /* Optimize Cond->Jmp, where then- and else-block are the same. */ + irg_block_walk_graph(irg, NULL, optimize_ifs, &env); + if (env.changed) { set_irg_doms_inconsistent(irg); - /* Removing blocks might enable more Cond optimizations */ + /* clear block info, because it must be recomputed */ + irg_block_walk_graph(irg, clear_block_info, NULL, block_info); + /* Removing blocks and Conds might enable more optimizations */ continue; } else { break; @@ -711,16 +859,18 @@ void optimize_cf(ir_graph *irg) ir_node *new_end; merge_env env; + env.changed = false; + env.phis_moved = false; + assert(get_irg_phase_state(irg) != phase_building); /* 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"); - /* FIXME: control flow opt destroys block edges. So edges are deactivated - * here. Fix the edges! */ edges_deactivate(irg); + /* First the "simple" optimizations, which do not touch Phis */ cfgopt_ignoring_phis(irg); /* we use the mark flag to mark removable blocks */ @@ -730,8 +880,7 @@ void optimize_cf(ir_graph *irg) for (;;) { int length; ir_node **switch_conds = NULL; - env.changed = false; - env.phis_moved = false; + bool changed = false; assure_doms(irg); @@ -742,17 +891,18 @@ void optimize_cf(ir_graph *irg) * computations, i.e., these blocks might be removed. */ switch_conds = NEW_ARR_F(ir_node*, 0); - irg_walk(end, clear_link, collect_nodes, &switch_conds); + irg_walk(end, clear_link_and_mark_blocks_removable, collect_nodes, &switch_conds); /* handle all collected switch-Conds */ length = ARR_LEN(switch_conds); for (i = 0; i < length; ++i) { ir_node *cond = switch_conds[i]; - env.changed |= handle_switch_cond(cond); + changed |= handle_switch_cond(cond); } DEL_ARR_F(switch_conds); - if (!env.changed) break; + if (!changed) + break; set_irg_doms_inconsistent(irg); set_irg_extblk_inconsistent(irg); @@ -783,7 +933,7 @@ void optimize_cf(ir_graph *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. + Some other phases cannot copy with this, so kill them. */ n = get_End_n_keepalives(end); if (n > 0) {