GCC Middle and Back End API Reference
tree-cfgcleanup.c File Reference
#include "config.h"
#include "system.h"
#include "coretypes.h"
#include "tm.h"
#include "tree.h"
#include "tm_p.h"
#include "basic-block.h"
#include "diagnostic-core.h"
#include "flags.h"
#include "function.h"
#include "ggc.h"
#include "langhooks.h"
#include "gimple.h"
#include "gimple-ssa.h"
#include "tree-cfg.h"
#include "tree-phinodes.h"
#include "ssa-iterators.h"
#include "tree-ssanames.h"
#include "tree-ssa-loop-manip.h"
#include "tree-dfa.h"
#include "tree-ssa.h"
#include "tree-pass.h"
#include "except.h"
#include "cfgloop.h"
#include "hashtab.h"
#include "tree-ssa-propagate.h"
#include "tree-scalar-evolution.h"
Include dependency graph for tree-cfgcleanup.c:

Functions

static bool remove_fallthru_edge ()
static bool cleanup_control_expr_graph ()
static bool cleanup_control_flow_bb ()
static bool tree_forwarder_block_p ()
static bool phi_alternatives_equal ()
static bool remove_forwarder_block ()
bool fixup_noreturn_call ()
static bool split_bbs_on_noreturn_calls ()
static bool cleanup_tree_cfg_bb ()
static bool cleanup_tree_cfg_1 ()
static bool cleanup_tree_cfg_noloop ()
static void repair_loop_structures ()
bool cleanup_tree_cfg ()
static bool remove_forwarder_block_with_phi ()
static unsigned int merge_phi_nodes ()
static bool gate_merge_phi ()
gimple_opt_passmake_pass_merge_phi ()
static unsigned int execute_cleanup_cfg_post_optimizing ()
gimple_opt_passmake_pass_cleanup_cfg_post_optimizing ()

Variables

bitmap cfgcleanup_altered_bbs

Function Documentation

static bool cleanup_control_expr_graph ( )
static

Disconnect an unreachable block in the control expression starting at block BB.

Remove all the edges except the one that is always executed.

static bool cleanup_control_flow_bb ( )
static

Try to remove superfluous control structures in basic block BB. Returns true if anything changes.

 If the last statement of the block could throw and now cannot,
 we need to prune cfg.   
     If we had a computed goto which has a compile-time determinable
     destination, then we can eliminate the goto.   
     First look at all the outgoing edges.  Delete any outgoing
     edges which do not go to the right block.  For the one
     edge which goes to the right block, fix up its flags.   
             Turn off the EDGE_ABNORMAL flag.   
             And set EDGE_FALLTHRU.   
     Remove the GOTO_EXPR as it is not needed.  The CFG has all the
     relevant information we need.   
 Check for indirect calls that have been turned into
 noreturn calls.   
bool cleanup_tree_cfg ( void  )

Cleanup cfg and repair loop structures.

static bool cleanup_tree_cfg_1 ( )
static

Iterate the cfg cleanups, while anything changes.

 Prepare the worklists of altered blocks.   
 During forwarder block cleanup, we may redirect edges out of
 SWITCH_EXPRs, which can get expensive.  So we want to enable
 recording of edge to CASE_LABEL_EXPR.   
 Start by iterating over all basic blocks.  We cannot use FOR_EACH_BB,
 since the basic blocks may get removed.   
 Now process the altered blocks, as long as any are available.   
     Rerun split_bbs_on_noreturn_calls, in case we have altered any noreturn
     calls.   

References calculate_dominance_info(), CDI_DOMINATORS, changed, compact_blocks(), current_loops, delete_unreachable_blocks(), dom_info_available_p(), gcc_assert, LOOPS_NEED_FIXUP, loops_state_set(), timevar_pop(), timevar_push(), verify_dominators(), and verify_flow_info().

static bool cleanup_tree_cfg_bb ( )
static

Tries to cleanup cfg in basic block BB. Returns true if anything changes.

Merging the blocks may create new opportunities for folding conditional branches (due to the elimination of single-valued PHI nodes).

static bool cleanup_tree_cfg_noloop ( )
static

Remove unreachable blocks and other miscellaneous clean up work. Return true if the flowgraph was modified, false otherwise.

Iterate until there are no more cleanups left to do. If any iteration changed the flowgraph, set CHANGED to true.

If dominance information is available, there cannot be any unreachable blocks.

static unsigned int execute_cleanup_cfg_post_optimizing ( )
static

Pass: cleanup the CFG just before expanding trees to RTL. This is just a round of label cleanups and case node grouping because after the tree optimizers have run such cleanups may be necessary.

bool fixup_noreturn_call ( )

STMT is a call that has been discovered noreturn. Fixup the CFG and remove LHS. Return true if something changed.

 First split basic block if stmt is not last.   
 If there is LHS, remove it.   
     We need to remove SSA name to avoid checking errors.
     All uses are dominated by the noreturn and thus will
     be removed afterwards.
     We proactively remove affected non-PHI statements to avoid
     fixup_cfg from trying to update them and crashing.   
 Similarly remove VDEF if there is any.   

References BASIC_BLOCK, cfun, changed, gimple_bb(), gimple_call_noreturn_p(), basic_block_def::index, last_basic_block, MODIFIED_NORETURN_CALLS, NULL, NUM_FIXED_BLOCKS, and vec_safe_length().

static bool gate_merge_phi ( )
static
gimple_opt_pass* make_pass_cleanup_cfg_post_optimizing ( )
gimple_opt_pass* make_pass_merge_phi ( )
static unsigned int merge_phi_nodes ( )
static

This pass merges PHI nodes if one feeds into another. For example, suppose we have the following:

goto <bb 9> (<L9>);

<L8>:; tem_17 = foo ();

tem_6 = PHI <tem_17(8), tem_23(7)>;

<L9>:;

tem_3 = PHI <tem_6(9), tem_2(5)>;

<L10>:;

Then we merge the first PHI node into the second one like so:

goto <bb 9> (<L10>);

<L8>:; tem_17 = foo ();

tem_3 = PHI <tem_23(7), tem_2(5), tem_17(8)>;

<L10>:;

 Find all PHI nodes that we may be able to merge.   
     Look for a forwarder block with PHI nodes.   
     We have to feed into another basic block with PHI
     nodes.   
         We don't want to deal with a basic block with
         abnormal edges.   
         If BB does not dominate DEST, then the PHI nodes at
         DEST must be the only users of the results of the PHI
         nodes at BB.   
         BB dominates DEST.  There may be many users of the PHI
         nodes in BB.  However, there is still a trivial case we
         can handle.  If the result of every PHI in BB is used
         only by a PHI in DEST, then we can trivially merge the
         PHI nodes from BB into DEST.   
             If the PHI's result is never used, then we can just
             ignore it.   
             Get the single use of the result of this PHI node.   
         If the loop above iterated through all the PHI nodes
         in BB, then we can merge the PHIs from BB into DEST.   
 Now let's drain WORKLIST.   
 Removing forwarder blocks can cause formerly irreducible loops
 to become reducible if we merged two entry blocks.   

References remove_forwarder_block_with_phi().

static bool phi_alternatives_equal ( )
static

If all the PHI nodes in DEST have alternatives for E1 and E2 and those alternatives are equal in each of the PHI nodes, then return true, else return false.

References DECL_NONLOCAL, edge_def::dest, EH_LANDING_PAD_NR, first_stmt(), gimple_label_label(), and single_succ_edge().

Referenced by remove_forwarder_block().

static bool remove_fallthru_edge ( )
static

Remove any fallthru edge from EV. Return true if an edge was removed.

References EDGE_COMPLEX, edge_def::flags, and remove_edge_and_dominated_blocks().

static bool remove_forwarder_block ( )
static

Removes forwarder block BB. Returns false if this failed.

 We check for infinite loops already in tree_forwarder_block_p.
 However it may happen that the infinite loop is created
 afterwards due to removal of forwarders.   
 If the destination block consists of a nonlocal label or is a
 EH landing pad, do not merge it.   
 If there is an abnormal edge to basic block BB, but not into
 dest, problems might occur during removal of the phi node at out
 of ssa due to overlapping live ranges of registers.

 If there is an abnormal edge in DEST, the problems would occur
 anyway since cleanup_dead_labels would then merge the labels for
 two different eh regions, and rest of exception handling code
 does not like it.

 So if there is an abnormal edge to BB, proceed only if there is
 no abnormal edge to DEST and there are no phi nodes in DEST.   
 If there are phi nodes in DEST, and some of the blocks that are
 predecessors of BB are also predecessors of DEST, check that the
 phi node arguments match.   
 Redirect the edges.   
         If there is an abnormal edge, redirect it anyway, and
         move the labels to the new block to make it legal.   
         Create arguments for the phi nodes, since the edge was not
         here before.   
 Move nonlocal labels and computed goto targets as well as user
 defined labels and labels with an EH landing pad number to the
 new block, so that the redirection of the abnormal edges works,
 jump targets end up in a sane place and debug information for
 labels is retained.   
 Move debug statements if the destination has a single predecessor.   
 Update the dominators.   
         Shortcut to avoid calling (relatively expensive)
         nearest_common_dominator unless necessary.   
 And kill the forwarder block.   

References find_edge(), FOR_EACH_EDGE, phi_alternatives_equal(), basic_block_def::preds, and edge_def::src.

static bool remove_forwarder_block_with_phi ( )
static

Tries to merge the PHI nodes at BB into those at BB's sole successor. Returns true if successful.

 We check for infinite loops already in tree_forwarder_block_p.
 However it may happen that the infinite loop is created
 afterwards due to removal of forwarders.   
 If the destination block consists of a nonlocal label, do not
 merge it.   
 Redirect each incoming edge to BB to DEST.   
         We already have an edge S from E->src to DEST.  If S and
         E->dest's sole successor edge have the same PHI arguments
         at DEST, redirect S to DEST.   
         PHI arguments are different.  Create a forwarder block by
         splitting E so that we can merge PHI arguments on E to
         DEST.   
     redirect_edge_and_branch must not create a new edge.   
     Add to the PHI nodes at DEST each PHI argument removed at the
     destination of E.   
             If DEF is one of the results of PHI nodes removed during
             redirection, replace it with the PHI argument that used
             to be on E.   
 Update the dominators.   
     Shortcut to avoid calling (relatively expensive)
     nearest_common_dominator unless necessary.   
 Remove BB since all of BB's incoming edges have been redirected
 to DEST.   

References FOR_EACH_VEC_SAFE_ELT, redirect_edge_var_map_def(), redirect_edge_var_map_location(), redirect_edge_var_map_result(), and redirect_edge_var_map_vector().

Referenced by merge_phi_nodes().

static void repair_loop_structures ( )
static

Repairs loop structures.

This usually does nothing. But sometimes parts of cfg that originally were inside a loop get out of it due to edge removal (since they become unreachable by back edges from latch). Also a former irreducible loop can become reducible - in this case force a full rewrite into loop-closed SSA form.

static bool split_bbs_on_noreturn_calls ( )
static

Split basic blocks on calls in the middle of a basic block that are now known not to return, and remove the unreachable code.

Detect cases where a mid-block call is now known not to return.

       BB might be deleted at this point, so verify first
       BB is present in the cfg.   

References merge_blocks(), and single_succ().

static bool tree_forwarder_block_p ( )
static

Return true if basic block BB does nothing except pass control flow to another block and that we can safely insert a label at the start of the successor block.

As a precondition, we require that BB be not equal to ENTRY_BLOCK_PTR.

 BB must have a single outgoing edge.   
     If PHI_WANTED is false, BB must not have any PHI nodes.
     Otherwise, BB must have PHI nodes.   
     BB may not be a predecessor of EXIT_BLOCK_PTR.   
     Nor should this be an infinite loop.   
     BB may not have an abnormal outgoing edge.   
 There should not be an edge coming from entry, or an EH edge.   
     If goto_locus of any of the edges differs, prevent removing
     the forwarder block for -O0.   
 Now walk through the statements backward.  We can ignore labels,
 anything else means this is not a forwarder block.   
         ??? For now, hope there's a corresponding debug
         assignment at the destination.   
     Protect loop latches, headers and preheaders.   

Variable Documentation

bitmap cfgcleanup_altered_bbs

CFG cleanup for trees. Copyright (C) 2001-2013 Free Software Foundation, Inc.

This file is part of GCC.

GCC is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 3, or (at your option) any later version.

GCC is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with GCC; see the file COPYING3. If not see http://www.gnu.org/licenses/. The set of blocks in that at least one of the following changes happened: – the statement at the end of the block was changed – the block was newly created – the set of the predecessors of the block changed – the set of the successors of the block changed ??? Maybe we could track these changes separately, since they determine what cleanups it makes sense to try on the block.