X-Git-Url: http://nsz.repo.hu/git/?a=blobdiff_plain;f=ir%2Fopt%2Fcfopt.c;h=d6f3c0ea715ec200c2023b9ecfce677639c05939;hb=762b472fc81c73cf7a1b0041b8cd286b7206d79d;hp=298fca1d7db4ee73b3f088333a16ae6938ed7902;hpb=0c41e492e7390943c7004f1abe4be89cb1ae2c69;p=libfirm diff --git a/ir/opt/cfopt.c b/ir/opt/cfopt.c index 298fca1d7..d6f3c0ea7 100644 --- a/ir/opt/cfopt.c +++ b/ir/opt/cfopt.c @@ -1,20 +1,41 @@ /* - * Project: libFIRM - * File name: ir/opt/cfopt.c - * Purpose: control flow optimizations - * Author: - * Created: - * CVS-ID: $Id$ - * Copyright: (c) 1998-2004 Universität Karlsruhe - * Licence: This file protected by GPL - GNU GENERAL PUBLIC LICENSE. + * Copyright (C) 1995-2008 University of Karlsruhe. All right reserved. + * + * This file is part of libFirm. + * + * This file may be distributed and/or modified under the terms of the + * GNU General Public License version 2 as published by the Free Software + * Foundation and appearing in the file LICENSE.GPL included in the + * packaging of this file. + * + * Licensees holding valid libFirm Professional Edition licenses may use + * this file in accordance with the libFirm Commercial License. + * Agreement provided with the Software. + * + * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE + * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE. */ -#ifdef HAVE_CONFIG_H -# include -#endif +/** + * @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 "xmalloc.h" #include "irnode_t.h" #include "irgraph_t.h" #include "irprog_t.h" @@ -24,145 +45,107 @@ #include "irgwalk.h" #include "irgmod.h" #include "irdump.h" -#include "irvrfy.h" +#include "irverify.h" +#include "iredges.h" -#include "array.h" +#include "array_t.h" #include "irouts.h" #include "irbackedge_t.h" #include "irflag_t.h" #include "firmstat.h" +#include "irpass.h" +#include "irphase_t.h" -#include "cfopt.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. */ -/*------------------------------------------------------------------*/ +/** An environment for merge_blocks and collect nodes. */ +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 *n, void *env) { - int i; - ir_node *new_block; - - /* clear the link field for ALL nodes first */ - set_irn_link(n, NULL); - - if (get_irn_op(n) == op_Block) { - /* Remove Tuples */ - for (i = 0; i < get_Block_n_cfgpreds(n); i++) { - /* GL @@@ : is this possible? if (get_opt_normalize()) -- added, all tests go through. - A different order of optimizations might cause problems. */ - if (get_opt_normalize()) - set_Block_cfgpred(n, i, skip_Tuple(get_Block_cfgpred(n, i))); - } - new_block = equivalent_node(n); - if (new_block != n) - exchange (n, new_block); - - } else if (get_opt_optimize() && (get_irn_mode(n) == mode_X)) { - /* We will soon visit a block. Optimize it before visiting! */ - ir_node *b = get_nodes_block(n); - - if (!is_Bad(b)) { - new_block = equivalent_node(b); - - while (irn_not_visited(b) && (!is_Bad(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(((b == new_block) || - get_opt_control_flow_straightening() || - get_opt_control_flow_weak_simplification()) && - ("strange flag setting")); - exchange (b, new_block); - b = new_block; - new_block = equivalent_node(b); - } - b = new_block; - } - - /* - * BEWARE: do not kill floating notes here as they might be needed in - * valid blocks because of global CSE. - */ - if (is_Bad(b) && get_opt_normalize() && - get_op_pinned(get_irn_op(n)) == op_pin_state_pinned) - exchange(n, new_Bad()); - } +/** set or reset the removable property of a block. */ +static void set_Block_removable(ir_node *block, bool removable) +{ + set_Block_mark(block, removable); } -/** - * Remove dead block by inspecting dominance info - */ -static void remove_dead_blocks(ir_node *block, void *env) { - /* delete dead blocks: if we have dominator information, this can easily be detected. - * Here, new Bad blocks my be introduced. - * - * BEWARE: don't kill the end block */ - if (block != get_irg_end_block(current_ir_graph) && - get_Block_dom_depth(block) == -1 && - get_opt_unreachable_code()) { - exchange (block, new_Bad()); - } +/** check if a block has the removable property set. */ +static bool is_Block_removable(ir_node *block) +{ + return get_Block_mark(block); +} + +/** 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; +} + +/** 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); } /** * Collects all Phi nodes in link list of Block. - * Marks all blocks "block_visited" if they contain a node other - * than Jmp. - * Replaces n by Bad if n is unreachable control flow. We do that - * in the post walker, so we catch all blocks. + * Marks all blocks "non_removable" if they contain a node other + * than Jmp (and Proj). + * Links all Proj nodes to their predecessors. + * Collects all switch-Conds in a list. */ -static void collect_nodes(ir_node *n, void *env) { - if (is_no_Block(n)) { - ir_node *b = get_nodes_block(n); - - /* - * BEWARE: do not kill floating notes here as they might be needed in - * valid blocks because of global CSE. - */ - if (is_Bad(b) && - get_op_pinned(get_irn_op(n)) == op_pin_state_pinned) { - /* previous merge_blocks() may have killed dead blocks */ - exchange(n, new_Bad()); - } - else if ((get_irn_op(n) == op_Phi)) { - /* 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 ((get_irn_op(n) != op_Jmp) && !is_Bad(b)) { /* Check for non empty block. */ - mark_Block_block_visited(b); - } - } +static void collect_nodes(ir_node *n, void *ctx) +{ + ir_node ***switch_conds = (ir_node***)ctx; + + if (is_Phi(n)) { + /* Collect Phi nodes to compact ins along with block's ins. */ + ir_node *block = get_nodes_block(n); + set_irn_link(n, get_irn_link(block)); + set_irn_link(block, n); + } else if (is_Block(n)) { + if (has_Block_entity(n)) { + /* block with a jump label attached cannot be removed. */ + set_Block_removable(n, false); + } + return; + } else if (!is_Jmp(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); + } else if (is_Cond(n) && is_switch_Cond(n)) { + /* found a switch-Cond, collect */ + ARR_APP1(ir_node*, *switch_conds, n); + } + } } -/** Returns true if pred is predecessor of block. */ -static int is_pred_of(ir_node *pred, ir_node *b) { - int i, n; - - for (i = 0, n = get_Block_n_cfgpreds(b); i < n; ++i) { - ir_node *b_pred = get_nodes_block(get_Block_cfgpred(b, i)); - if (b_pred == pred) return 1; - } - return 0; +/** Returns true if pred is predecessor of block b. */ +static bool is_pred_of(ir_node *pred, 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 true; + } + return false; } - -/** Test wether we can optimize away pred block pos of b. +/** Test whether we can optimize away pred block pos of b. * * @param b A block node. * @param pos The position of the predecessor block to judge about. @@ -172,76 +155,84 @@ static int is_pred_of(ir_node *pred, ir_node *b) { * The test is rather tricky. * * The situation is something like the following: - * + * @verbatim * if-block * / \ * then-b else-b * \ / * b + * @endverbatim * - * b merges the control flow of an if-then-else. We may not remove - * the 'then' _and_ the 'else' block of an 'if' if there is a Phi - * node in b, even if both are empty. The destruction of this Phi - * requires that a copy is added before the merge. We have to - * keep one of the case blocks to place the copies in. + * b merges the control flow of an if-then-else. We may not remove + * the 'then' _and_ the 'else' block of an 'if' if there is a Phi + * node in b, even if both are empty. The destruction of this Phi + * requires that a copy is added before the merge. We have to + * keep one of the case blocks to place the copies in. * - * To perform the test for pos, we must regard preds before pos - * as already removed. + * 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; - int dispensable = 1; - ir_node *cfop = get_Block_cfgpred(b, pos); - ir_node *pred = get_nodes_block(cfop); - - if (get_Block_block_visited(pred) + 1 - < get_irg_block_visited(current_ir_graph)) { - - 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_block_visited(pred, get_irg_block_visited(current_ir_graph)-1); - return 1; - } - - /* Seems to be empty. At least we detected this in collect_nodes. */ - if (!get_irn_link(b)) { - /* 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. - Work preds < pos as if they were already removed. */ - for (i = 0; i < pos; i++) { - ir_node *b_pred = get_nodes_block(get_Block_cfgpred(b, i)); - if (get_Block_block_visited(b_pred) + 1 - < get_irg_block_visited(current_ir_graph)) { - for (j = 0; j < get_Block_n_cfgpreds(b_pred); j++) { - ir_node *b_pred_pred = get_nodes_block(get_Block_cfgpred(b_pred, j)); - if (is_pred_of(b_pred_pred, pred)) dispensable = 0; - } - } else { - if (is_pred_of(b_pred, pred)) dispensable = 0; - } - } - for (i = pos +1; i < get_Block_n_cfgpreds(b); i++) { - ir_node *b_pred = get_nodes_block(get_Block_cfgpred(b, i)); - if (is_pred_of(b_pred, pred)) dispensable = 0; - } - if (!dispensable) { - set_Block_block_visited(pred, get_irg_block_visited(current_ir_graph)-1); - n_preds = 1; - } else { - n_preds = get_Block_n_cfgpreds(pred); - } - } - } - - return n_preds; +static unsigned test_whether_dispensable(ir_node *b, int pos) +{ + ir_node *pred = get_Block_cfgpred(b, pos); + ir_node *predb = get_nodes_block(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_irn_link(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 < n_cfgpreds; i++) { + ir_node *other_predb = get_Block_cfgpred_block(b, i); + if (is_pred_of(other_predb, predb)) + goto non_dispensable; + } + } + /* 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_removable(predb, false); + return 1; } - /** - * This method removed Bad cf preds from Blocks and Phis, and removes - * empty blocks. A block is empty if it only contains Phi and Jmp nodes. + * 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 @@ -249,320 +240,657 @@ static int test_whether_dispensable(ir_node *b, int pos) { * 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 precessor 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 * * then_b else_b then_b else_b * \ / \ / * \ / | / * pred_b | / - * | ____ | / + * | ____ | / ____ * | | | | | | | * | | | === optimized to ===> \ | | | * loop_b | loop_b | * | | | | | | * | |____| | |____| * | | + * @endverbatim * * 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 *env) { - int i, j, k, n, max_preds, n_preds, p_preds; - ir_node *pred, *phi; - ir_node **in; - - /* Count the number of predecessor if this block is merged with pred blocks - that are empty. */ - max_preds = 0; - for (i = 0, k = get_Block_n_cfgpreds(b); i < k; ++i) { - max_preds += test_whether_dispensable(b, i); - } - in = (ir_node **) malloc(max_preds * sizeof(ir_node *)); - -/*- - printf(" working on "); DDMN(b); - for (i = 0; i < get_Block_n_cfgpreds(b); i++) { - pred = get_nodes_block(get_Block_cfgpred(b, i)); - if (is_Bad(get_Block_cfgpred(b, i))) { - printf(" removing Bad %i\n ", i); - } else if (get_Block_block_visited(pred) +1 - < get_irg_block_visited(current_ir_graph)) { - printf(" removing pred %i ", i); DDMN(pred); - } else { printf(" Nothing to do for "); DDMN(pred); } - } - * end Debug output -*/ - - /*- Fix the Phi nodes of the current block -*/ - for (phi = get_irn_link(b); phi; ) { - assert(get_irn_op(phi) == op_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_nodes_block(get_Block_cfgpred(b, i)); - - if (is_Bad(get_Block_cfgpred(b, i))) { - /* case Phi 1: Do nothing */ - } - else if (get_Block_block_visited(pred) + 1 - < get_irg_block_visited(current_ir_graph)) { - /* case Phi 2: It's an empty block and not yet visited. */ - 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(get_irn_op(phi_pred) == op_Phi); /* Block is empty!! */ - - in[p_preds++] = get_Phi_pred(phi_pred, j); - } else { - /* case Phi 2b: */ - in[p_preds++] = phi_pred; - } - } - } - - /* The Phi_pred node is replaced now if it is a Phi. - - Somehow the removed Phi node can be used legally in loops. - Therefore we replace the old phi by the new one. - - Further we have to remove the old Phi node by replacing it - by Bad. Else it will remain in the keepalive array of End - and cause illegal situations. So if there is no loop, we should - replace it by Bad. - */ - if (get_nodes_block(phi_pred) == pred) { - /* remove the Phi as it might be kept alive. Further there - might be other users. */ - exchange(phi_pred, phi); /* geht, ist aber doch semantisch falsch! Warum?? */ - } - } else { - /* case Phi 3: */ - in[p_preds++] = get_Phi_pred(phi, i); - } - } - - /* Fix the node */ - if (p_preds == 1) - /* By removal of Bad ins the Phi might be degenerated. */ - exchange(phi, in[0]); - else - set_irn_in(phi, p_preds, in); - - phi = get_irn_link(phi); - } - - /*- This happens only if merge between loop backedge and single loop entry. - See special case above. -*/ - for (k = 0, n = get_Block_n_cfgpreds(b); k < n; ++k) { - pred = get_nodes_block(get_Block_cfgpred(b, k)); - - if (get_Block_block_visited(pred) + 1 < get_irg_block_visited(current_ir_graph)) { - /* we found a predecessor block at position k that will be removed */ - for (phi = get_irn_link(pred); phi;) { - /* - * the previous phase may already changed the phi, and even - * removed it at all, so check here if this node is still a phi - */ - if (get_irn_op(phi) == op_Phi) { - int q_preds = 0; - - /* move this phi from the predecessor into the block b */ - set_nodes_block(phi, b); - - /* first, copy all 0..k-1 predecessors */ - for (i = 0; i < k; i++) { - pred = get_nodes_block(get_Block_cfgpred(b, i)); - - if (is_Bad(get_Block_cfgpred(b, i))) { - /* Do nothing */ - } else if (get_Block_block_visited(pred) + 1 - < get_irg_block_visited(current_ir_graph)) { - /* It's an empty block and not yet visited. */ - for (j = 0; j < get_Block_n_cfgpreds(pred); j++) { - /* @@@ Hier brauche ich Schleifeninformation!!! Kontrollflusskante - muss Rueckwaertskante sein! (An allen vier in[q_preds] = phi - Anweisungen.) Trotzdem tuts bisher!! */ - if (! is_Bad(get_Block_cfgpred(pred, j))) - in[q_preds++] = phi; - } - } else { - in[q_preds++] = phi; - } - } - - /* 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); - } - - /* and now all the rest */ - for (i = k+1; i < get_Block_n_cfgpreds(b); i++) { - pred = get_nodes_block(get_Block_cfgpred(b, i)); - - if (is_Bad(get_Block_cfgpred(b, i))) { - /* Do nothing */ - } else if (get_Block_block_visited(pred) +1 - < get_irg_block_visited(current_ir_graph)) { - /* 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))) - in[q_preds++] = phi; - } - } else { - in[q_preds++] = phi; - } - } - - /* Fix the node */ - if (q_preds == 1) - exchange(phi, in[0]); - else - set_irn_in(phi, q_preds, in); - -// assert(p_preds == q_preds && "Wrong Phi Fix"); - } - phi = get_irn_link(phi); - } - } - } - - /*- Fix the block -*/ - n_preds = 0; - for (i = 0; i < get_Block_n_cfgpreds(b); i++) { - pred = get_nodes_block(get_Block_cfgpred(b, i)); - - if (is_Bad(get_Block_cfgpred(b, i))) { - /* case 1: Do nothing */ - } else if (get_Block_block_visited(pred) +1 - < get_irg_block_visited(current_ir_graph)) { - /* case 2: It's an empty block and not yet visited. */ - assert(get_Block_n_cfgpreds(b) > 1); - /* Else it should be optimized by equivalent_node. */ - for (j = 0; j < get_Block_n_cfgpreds(pred); j++) { - ir_node *pred_block = 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_block)) - in[n_preds++] = pred_block; - } - /* Remove block as it might be kept alive. */ - exchange(pred, b/*new_Bad()*/); - } else { - /* case 3: */ - in[n_preds++] = get_Block_cfgpred(b, i); - } - } - set_irn_in(b, n_preds, in); - - assert(get_irn_link(b) == NULL || (n_preds == p_preds && "Wrong Phi Fix")); - - free(in); +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 **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; + for (i = 0, k = get_Block_n_cfgpreds(b); i < k; ++i) { + max_preds += test_whether_dispensable(b, i); + } + in = XMALLOCN(ir_node*, max_preds); + + /*- Fix the Phi nodes of the current block -*/ + for (phi = (ir_node*)get_irn_link(b); phi != NULL; phi = (ir_node*)next) { + assert(is_Phi(phi)); + next = (ir_node*)get_irn_link(phi); + + /* Find the new predecessors for the Phi */ + p_preds = 0; + for (i = 0, n = get_Block_n_cfgpreds(b); i < n; ++i) { + ir_graph *irg = get_irn_irg(b); + pred = get_Block_cfgpred_block(b, i); + + if (is_Bad(pred)) { + /* case Phi 1: maintain Bads, as somebody else is responsible to remove them */ + in[p_preds++] = new_r_Bad(irg, get_irn_mode(phi)); + } else if (is_Block_removable(pred) && !Block_block_visited(pred)) { + /* case Phi 2: It's an empty block and not yet visited. */ + ir_node *phi_pred = get_Phi_pred(phi, i); + + for (j = 0, k = get_Block_n_cfgpreds(pred); j < k; j++) { + 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: */ + 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; + } + } + } else { + /* case Phi 3: */ + in[p_preds++] = get_Phi_pred(phi, i); + } + } + assert(p_preds == max_preds); + + /* Fix the node */ + if (p_preds == 1) + exchange(phi, in[0]); + else + set_irn_in(phi, p_preds, in); + 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 ... -*/ + for (k = 0, n = get_Block_n_cfgpreds(b); k < n; ++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 = (ir_node*)get_irn_link(predb); phi; phi = next_phi) { + int q_preds = 0; + next_phi = (ir_node*)get_irn_link(phi); + + assert(is_Phi(phi)); + + if (get_Block_idom(b) != predb) { + /* 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 */ + set_nodes_block(phi, b); + set_irn_link(phi, get_irn_link(b)); + set_irn_link(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_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))) { + 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; + } + } + + /* 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++) { + 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_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))) { + 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; + } + } + + /* Fix the node */ + if (q_preds == 1) + exchange(phi, in[0]); + else + set_irn_in(phi, q_preds, in); + env->changed = true; + + assert(q_preds <= max_preds); + // assert(p_preds == q_preds && "Wrong Phi Fix"); + } + } + } + } + + /*- Fix the block -*/ + n_preds = 0; + for (i = 0; i < get_Block_n_cfgpreds(b); i++) { + 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. */ + 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+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++] = pred; + } + } + assert(n_preds == max_preds); + + set_irn_in(b, n_preds, in); + env->changed = true; + + /* see if phi-fix was correct */ + assert(get_irn_link(b) == NULL || p_preds == -1 || (n_preds == p_preds)); + xfree(in); } - -/* Optimizations of the control flow that also require changes of Phi nodes. - * - * This optimization performs two passes over the graph. - * - * The first 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. +/** + * Optimize table-switch Conds. * - * The second pass performs the optimizations intended by this algorithm. - * It walks only over block nodes and adapts these and the Phi nodes in these blocks, - * which it finds in a linked list computed by the first pass. + * @param cond the switch-Cond + * @return true if the switch-Cond was optimized + */ +static bool handle_switch_cond(ir_node *cond) +{ + ir_node *sel = get_Cond_selector(cond); + ir_node *proj1 = (ir_node*)get_irn_link(cond); + ir_node *proj2 = (ir_node*)get_irn_link(proj1); + ir_node *blk = get_nodes_block(cond); + + /* exactly 1 Proj on the Cond node: must be the defaultProj */ + if (proj2 == NULL) { + ir_node *jmp = new_r_Jmp(blk); + assert(get_Cond_default_proj(cond) == get_Proj_proj(proj1)); + /* convert it into a Jmp */ + exchange(proj1, jmp); + return true; + } + + /* 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 + 1 case + * (this one case should be the one taken) */ + if (get_irn_link(proj2) == NULL) { + ir_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); + ir_graph *irg = get_irn_irg(cond); + ir_node *bad = new_r_Bad(irg, mode_X); + + if (def_num == get_Proj_proj(proj1)) { + /* first one is the defProj */ + if (num == get_Proj_proj(proj2)) { + ir_node *jmp = new_r_Jmp(blk); + exchange(proj2, jmp); + exchange(proj1, bad); + return true; + } + } else if (def_num == get_Proj_proj(proj2)) { + /* second one is the defProj */ + if (num == get_Proj_proj(proj1)) { + ir_node *jmp = new_r_Jmp(blk); + exchange(proj1, jmp); + exchange(proj2, bad); + return true; + } + } else { + /* neither: strange, Cond was not optimized so far */ + if (num == get_Proj_proj(proj1)) { + ir_node *jmp = new_r_Jmp(blk); + exchange(proj1, jmp); + exchange(proj2, bad); + return true; + } else if (num == get_Proj_proj(proj2)) { + ir_node *jmp = new_r_Jmp(blk); + exchange(proj2, jmp); + exchange(proj1, bad); + return true; + } + } + } + } + return false; +} + +/** + * Optimize boolean Conds, where true and false jump to the same block into a Jmp + * Block must contain no Phi nodes. * - * We use the block_visited flag to mark empty blocks in the first - * phase. - * @@@ It would be better to add a struct in the link field - * that keeps the Phi list and the mark. Place it on an obstack, as - * we will lose blocks and thereby generate mem leaks. + * Cond + * / \ + * projA projB => Jmp Bad + * \ / \ / + * block block + */ +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; +} + +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_phase_flag(ir_phase *block_info, ir_node *block, int flag) +{ + return PTR_TO_INT(phase_get_irn_data(block_info, block)) & flag; +} + +static void set_phase_flag(ir_phase *block_info, ir_node *block, + block_flags_t flag) +{ + int data = PTR_TO_INT(phase_get_irn_data(block_info, block)); + data |= flag; + phase_set_irn_data(block_info, block, INT_TO_PTR(data)); +} + +static bool has_operations(ir_phase *block_info, ir_node *block) +{ + return get_phase_flag(block_info, block, BF_HAS_OPERATIONS); +} + +static void set_has_operations(ir_phase *block_info, ir_node *block) +{ + set_phase_flag(block_info, block, BF_HAS_OPERATIONS); +} + +static bool has_phis(ir_phase *block_info, ir_node *block) +{ + return get_phase_flag(block_info, block, BF_HAS_PHIS); +} + +static void set_has_phis(ir_phase *block_info, ir_node *block) +{ + set_phase_flag(block_info, block, BF_HAS_PHIS); +} + +static bool is_unknown_jump_target(ir_phase *block_info, ir_node *block) +{ + return get_phase_flag(block_info, block, BF_IS_UNKNOWN_JUMP_TARGET); +} + +static void set_is_unknown_jump_target(ir_phase *block_info, ir_node *block) +{ + set_phase_flag(block_info, block, BF_IS_UNKNOWN_JUMP_TARGET); +} + +/** + * Walker: fill block info information. */ -void optimize_cf(ir_graph *irg) { - int i; - ir_node **in; - ir_node *end = get_irg_end(irg); - ir_graph *rem = current_ir_graph; - irg_dom_state dom_state = get_irg_dom_state(current_ir_graph); - current_ir_graph = irg; - - /* Handle graph state */ - assert(get_irg_phase_state(irg) != phase_building); - if (get_irg_outs_state(current_ir_graph) == outs_consistent) - set_irg_outs_inconsistent(current_ir_graph); - if (get_irg_dom_state(current_ir_graph) == dom_consistent) - set_irg_dom_inconsistent(current_ir_graph); - - if (dom_state == dom_consistent) { - /* we have dominace info, we can kill dead block */ - irg_block_walk(get_irg_end_block(irg), NULL, remove_dead_blocks, NULL); - } - - /* Use block visited flag to mark non-empty blocks. */ - inc_irg_block_visited(irg); - irg_walk(end, merge_blocks, collect_nodes, NULL); - - /* Optimize the standard code. */ - irg_block_walk(get_irg_end_block(irg), optimize_blocks, NULL, NULL); - - /* Walk all keep alives, optimize them if block, add to new in-array - for end if useful. */ - in = NEW_ARR_F (ir_node *, 1); - in[0] = get_nodes_block(end); - inc_irg_visited(current_ir_graph); - - for(i = 0; i < get_End_n_keepalives(end); i++) { - ir_node *ka = get_End_keepalive(end, i); - - if (irn_not_visited(ka)) { - if ((get_irn_op(ka) == op_Block) && Block_not_block_visited(ka)) { - set_irg_block_visited(current_ir_graph, /* Don't walk all the way to Start. */ - get_irg_block_visited(current_ir_graph)-1); - irg_block_walk(ka, optimize_blocks, NULL, NULL); - mark_irn_visited(ka); - ARR_APP1 (ir_node *, in, ka); - } else if (get_irn_op(ka) == op_Phi) { - mark_irn_visited(ka); - ARR_APP1 (ir_node *, in, ka); - } - } - } - /* DEL_ARR_F(end->in); GL @@@ tut nicht ! */ - end->in = in; - - /* 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; +static void compute_block_info(ir_node *n, void *x) +{ + ir_phase *block_info = (ir_phase *)x; + + if (is_Block(n)) { + int i, max = get_Block_n_cfgpreds(n); + for (i=0; iphase, 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(block, i, j); + } + } +} + +/** + * Pre-Block walker: remove empty blocks that are + * 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, *pred, *pred_block; + + 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 (get_Block_n_cfgpreds(jmp_block) != 1) + continue; + pred = get_Block_cfgpred(jmp_block, 0); + exchange(jmp, pred); + env->changed = true; + + /* cleanup: jmp_block might have a Keep edge! */ + pred_block = get_nodes_block(pred); + exchange(jmp_block, pred_block); + } +} + +/* + * Some cfg optimizations, which do not touch Phi nodes + */ +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); + + for (;;) { + env.changed = false; + + /* optimize useless ifs: will not touch empty blocks */ + irg_block_walk_graph(irg, NULL, optimize_ifs, &env); + + /* Remove empty blocks */ + irg_block_walk_graph(irg, remove_empty_blocks, NULL, &env); + if (env.changed) { + set_irg_doms_inconsistent(irg); + /* Removing blocks might enable more useless-if optimizations */ + continue; + } else { + break; + } + } + + phase_free(block_info); +} + +/* 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; + + 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); + + 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); + + /* The switch Cond optimization might expose unreachable code, so we loop */ + for (;;) { + int length; + ir_node **switch_conds = NULL; + bool changed = false; + + assure_doms(irg); + + /* + * 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. + */ + switch_conds = NEW_ARR_F(ir_node*, 0); + 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]; + changed |= handle_switch_cond(cond); + } + DEL_ARR_F(switch_conds); + + if (!changed) + break; + + set_irg_doms_inconsistent(irg); + set_irg_extblk_inconsistent(irg); + set_irg_entity_usage_state(irg, ir_entity_usage_not_computed); + } + + /* 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_doms(irg); + irg_block_walk_graph(irg, optimize_blocks, NULL, &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); + + 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 kill them. + */ + n = get_End_n_keepalives(end); + if (n > 0) { + NEW_ARR_A(ir_node *, in, n); + 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 = true; + } + } + } + + if (env.changed) { + /* Handle graph state if was changed. */ + set_irg_doms_inconsistent(irg); + set_irg_extblk_inconsistent(irg); + set_irg_entity_usage_state(irg, ir_entity_usage_not_computed); + } +} + +/* Creates an ir_graph pass for optimize_cf. */ +ir_graph_pass_t *optimize_cf_pass(const char *name) +{ + return def_graph_pass(name ? name : "optimize_cf", optimize_cf); }