GCC Middle and Back End API Reference
tree-ssa-propagate.c File Reference
#include "config.h"
#include "system.h"
#include "coretypes.h"
#include "tm.h"
#include "tree.h"
#include "flags.h"
#include "tm_p.h"
#include "basic-block.h"
#include "function.h"
#include "gimple-pretty-print.h"
#include "dumpfile.h"
#include "sbitmap.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.h"
#include "tree-ssa-propagate.h"
#include "langhooks.h"
#include "vec.h"
#include "value-prof.h"
#include "gt-tree-ssa-propagate.h"
Include dependency graph for tree-ssa-propagate.c:

Data Structures

struct  prop_stats_d

Macros

#define STMT_IN_SSA_EDGE_WORKLIST   GF_PLF_1

Functions

static bool cfg_blocks_empty_p ()
static void cfg_blocks_add ()
static basic_block cfg_blocks_get ()
static void add_ssa_edge ()
static void add_control_edge ()
static void simulate_stmt ()
static void process_ssa_edge_worklist ()
static void simulate_block ()
static void ssa_prop_init ()
static void ssa_prop_fini ()
bool valid_gimple_rhs_p ()
static bool valid_gimple_call_p ()
void move_ssa_defining_stmt_for_defs ()
static void finish_update_gimple_call (gimple_stmt_iterator *si_p, gimple new_stmt, gimple stmt)
bool update_gimple_call ()
bool update_call_from_tree ()
void ssa_propagate (ssa_prop_visit_stmt_fn visit_stmt, ssa_prop_visit_phi_fn visit_phi)
bool stmt_makes_single_store ()
static bool replace_uses_in ()
static void replace_phi_args_in ()
bool substitute_and_fold (ssa_prop_get_value_fn get_value_fn, ssa_prop_fold_stmt_fn fold_fn, bool do_dce)
bool may_propagate_copy ()
bool may_propagate_copy_into_stmt ()
bool may_propagate_copy_into_asm ()
static void replace_exp_1 (use_operand_p op_p, tree val, bool for_propagation)
void propagate_value ()
void replace_exp ()
void propagate_tree_value ()
void propagate_tree_value_into_stmt ()

Variables

static ssa_prop_visit_stmt_fn ssa_prop_visit_stmt
static ssa_prop_visit_phi_fn ssa_prop_visit_phi
static sbitmap executable_blocks
static vec< basic_blockcfg_blocks
static unsigned int cfg_blocks_num = 0
static int cfg_blocks_tail
static int cfg_blocks_head
static sbitmap bb_in_list
static vec< gimple, va_gc > * interesting_ssa_edges
static vec< gimple, va_gc > * varying_ssa_edges
static struct prop_stats_d prop_stats

Macro Definition Documentation

#define STMT_IN_SSA_EDGE_WORKLIST   GF_PLF_1

Keep track of statements that have been added to one of the SSA edges worklists. This flag is used to avoid visiting statements unnecessarily when draining an SSA edge worklist. If while simulating a basic block, we find a statement with STMT_IN_SSA_EDGE_WORKLIST set, we clear it to prevent SSA edge processing from visiting it again.

NOTE: users of the propagation engine are not allowed to use the GF_PLF_1 flag.


Function Documentation

static void add_control_edge ( )
static

Add edge E to the control flow worklist.

If the edge had already been executed, skip it.

 If the block is already in the list, we're done.   

Referenced by simulate_stmt().

static void add_ssa_edge ( )
static

We have just defined a new value for VAR. If IS_VARYING is true, add all immediate uses of VAR to VARYING_SSA_EDGES, otherwise add them to INTERESTING_SSA_EDGES.

Referenced by simulate_stmt().

static void cfg_blocks_add ( )
static

Add a basic block to the worklist. The block must not be already in the worklist, and it must not be the ENTRY or EXIT block.

We have to grow the array now.  Adjust to queue to occupy
the full space of the original array.  We do not need to
initialize the newly allocated portion of the array
because we keep track of CFG_BLOCKS_HEAD and
CFG_BLOCKS_HEAD.   

Minor optimization: we prefer to see blocks with more predecessors later, because there is more of a chance that the incoming edges will be executable.

static bool cfg_blocks_empty_p ( )
inlinestatic

Return true if the block worklist empty.

References bitmap_bit_p, cfg_blocks_head, cfg_blocks_tail, ENTRY_BLOCK_PTR, EXIT_BLOCK_PTR, gcc_assert, and basic_block_def::index.

static basic_block cfg_blocks_get ( )
static

Remove a block from the worklist.

static void finish_update_gimple_call ( gimple_stmt_iterator si_p,
gimple  new_stmt,
gimple  stmt 
)
static

Helper function for update_gimple_call and update_call_from_tree. A GIMPLE_CALL STMT is being replaced with GIMPLE_CALL NEW_STMT.

References CALL_EXPR_ARG, CALL_EXPR_FN, call_expr_nargs, gimple_build_call_vec(), and vNULL.

bool may_propagate_copy ( )

Return true if we may propagate ORIG into DEST, false otherwise.

 If ORIG flows in from an abnormal edge, it cannot be propagated.   
     If it is the default definition and an automatic variable then
     we can though and it is important that we do to avoid
     uninitialized regular copies.   
 If DEST is an SSA_NAME that flows from an abnormal edge, then it
 cannot be replaced.   
 Do not copy between types for which we *do* need a conversion.   
 Generally propagating virtual operands is not ok as that may
 create overlapping life-ranges.   
 Anything else is OK.   

Referenced by lookup_avail_expr(), replace_phi_args_in(), and scev_reset().

bool may_propagate_copy_into_asm ( )

Similarly, but we know that we're propagating into an ASM_EXPR.

References gimple_assign_rhs1(), gimple_assign_set_rhs_from_tree(), gimple_assign_single_p(), NULL_TREE, and propagate_tree_value().

Referenced by lookup_avail_expr().

bool may_propagate_copy_into_stmt ( )

Like may_propagate_copy, but use as the destination expression the principal expression (typically, the RHS) contained in statement DEST. This is more efficient when working with the gimple tuples representation.

If the statement is a switch or a single-rhs assignment, then the expression to be replaced by the propagation may be an SSA_NAME. Fortunately, there is an explicit tree for the expression, so we delegate to may_propagate_copy.

 In other cases, the expression is not materialized, so there
 is no destination to pass to may_propagate_copy.  On the other
 hand, the expression cannot be an SSA_NAME, so the analysis
 is much simpler.   

References replace_exp_1().

void move_ssa_defining_stmt_for_defs ( )

Make SSA names defined by OLD_STMT point to NEW_STMT as their defining statement.

Make defined SSA_NAMEs point to the new statement as their definition.

Referenced by valid_gimple_call_p().

static void process_ssa_edge_worklist ( )
static

Process an SSA edge worklist. WORKLIST is the SSA edge worklist to drain. This pops statements off the given WORKLIST and processes them until there are no more statements on WORKLIST. We take a pointer to WORKLIST because it may be reallocated when an SSA edge is added to it in simulate_stmt.

 Drain the entire worklist.   
     Pull the statement to simulate off the worklist.   
     If this statement was already visited by simulate_block, then
     we don't need to visit it again here.   
     STMT is no longer in a worklist.   
     PHI nodes are always visited, regardless of whether or not
     the destination block is executable.  Otherwise, visit the
     statement only if its block is marked executable.   
void propagate_tree_value ( )

Propagate the value VAL (assumed to be a constant or another SSA_NAME) into the tree pointed to by OP_P.

Use this version for const/copy propagation when SSA operands are not available. It will perform the additional checks to ensure validity of the const/copy propagation, but will not update any operand information. Be sure to mark the stmt as modified.

Referenced by may_propagate_copy_into_asm().

void propagate_tree_value_into_stmt ( )

Like propagate_tree_value, but use as the operand to replace the principal expression (typically, the RHS) contained in the statement referenced by iterator GSI. Note that it is not always possible to update the statement in-place, so a new statement may be created to replace the original.

void propagate_value ( )

Propagate the value VAL (assumed to be a constant or another SSA_NAME) into the operand pointed to by OP_P.

Use this version for const/copy propagation as it will perform additional checks to ensure validity of the const/copy propagation.

Referenced by lookup_avail_expr().

void replace_exp ( )

Replace *OP_P with value VAL (assumed to be a constant or another SSA_NAME).

Use this version when not const/copy propagating values. For example, PRE uses this version when building expressions as they would appear in specific blocks taking into account actions of PHI nodes.

The statement in which an expression has been replaced should be folded using fold_stmt_inplace.

Referenced by group_case_labels().

static void replace_exp_1 ( use_operand_p  op_p,
tree  val,
bool  for_propagation 
)
static

Common code for propagate_value and replace_exp.

Replace use operand OP_P with VAL. FOR_PROPAGATION indicates if the replacement is done to propagate a value or not.

Referenced by may_propagate_copy_into_stmt().

static bool replace_uses_in ( )
static

Replace USE references in statement STMT with the values stored in PROP_VALUE. Return true if at least one reference was replaced.

static void simulate_block ( )
static

Simulate the execution of BLOCK. Evaluate the statement associated with each variable reference inside the block.

 There is nothing to do for the exit block.   
 Always simulate PHI nodes, even if we have simulated this block
 before.   
 If this is the first time we've simulated this block, then we
 must simulate each of its statements.   
     Note that we have simulated this block.   
         If this statement is already in the worklist then
         "cancel" it.  The reevaluation implied by the worklist
         entry will produce the same value we generate here and
         thus reevaluating it again from the worklist is
         pointless.   
     We can not predict when abnormal and EH edges will be executed, so
     once a block is considered executable, we consider any
     outgoing abnormal edges as executable.

     TODO: This is not exactly true.  Simplifying statement might
     prove it non-throwing and also computed goto can be handled
     when destination is known.

     At the same time, if this block has only one successor that is
     reached by non-abnormal edges, then add that successor to the
     worklist.   
static void simulate_stmt ( )
static

Simulate the execution of STMT and update the work lists accordingly.

 Don't bother visiting statements that are already
 considered varying by the propagator.   
     If the statement produced a new varying value, add the SSA
     edges coming out of OUTPUT_NAME.   
     If STMT transfers control out of its basic block, add
     all outgoing edges to the work list.   
     If the statement produced new value, add the SSA edges coming
     out of OUTPUT_NAME.   
     If we know which edge is going to be taken out of this block,
     add it to the CFG work list.   

References add_control_edge(), add_ssa_edge(), FOR_EACH_EDGE, gimple_bb(), prop_set_simulate_again(), stmt_ends_bb_p(), and basic_block_def::succs.

static void ssa_prop_fini ( )
static
static void ssa_prop_init ( )
static

Initialize local data structures and work lists.

 Worklists of SSA edges.   
 Initially assume that every edge in the CFG is not executable.
 (including the edges coming out of ENTRY_BLOCK_PTR).   
 Seed the algorithm by adding the successors of the entry block to the
 edge worklist.   

References sbitmap_free(), and vec_free().

void ssa_propagate ( ssa_prop_visit_stmt_fn  visit_stmt,
ssa_prop_visit_phi_fn  visit_phi 
)

Entry point to the propagation engine.

VISIT_STMT is called for every statement visited. VISIT_PHI is called for every PHI node visited.

 Iterate until the worklists are empty.   
         Pull the next block to simulate off the worklist.   
     In order to move things to varying as quickly as
     possible,process the VARYING_SSA_EDGES worklist first.   
     Now process the INTERESTING_SSA_EDGES worklist.   

Referenced by visit_assignment().

bool stmt_makes_single_store ( )

Return true if STMT is of the form 'mem_ref = RHS', where 'mem_ref' is a non-volatile pointer dereference, a structure reference or a reference to a single _DECL. Ignore volatile memory references because they are not interesting for the optimizers.

A call statement may have a null LHS.

bool substitute_and_fold ( ssa_prop_get_value_fn  get_value_fn,
ssa_prop_fold_stmt_fn  fold_fn,
bool  do_dce 
)

Perform final substitution and folding of propagated values.

PROP_VALUE[I] contains the single value that should be substituted at every use of SSA name N_I. If PROP_VALUE is NULL, no values are substituted.

If FOLD_FN is non-NULL the function will be invoked on all statements before propagating values for pass specific simplification.

DO_DCE is true if trivially dead stmts can be removed.

If DO_DCE is true, the statements within a BB are walked from last to first element. Otherwise we scan from first to last element.

Return TRUE when something changed.

 Substitute lattice values at definition sites.   
           Do not substitute ASSERT_EXPR rhs, this will confuse VRP.   
           Don't optimize away calls that have side-effects.   
 Propagate into all uses and fold.   
     Propagate known values into PHI nodes.   
     Propagate known values into stmts.  Do a backward walk if
     do_dce is true. In some case it exposes
     more trivially deletable stmts to walk backward.   
         Ignore ASSERT_EXPRs.  They are used by VRP to generate
         range information for names and they are discarded
         afterwards.   
         No point propagating into a stmt whose result is not used,
         but instead we might be able to remove a trivially dead stmt.
         Don't do this when called from VRP, since the SSA_NAME which
         is going to be released could be still referenced in VRP
         ranges.   
         Replace the statement with its folded version and mark it
         folded.   
         Some statements may be simplified using propagator
         specific information.  Do this before propagating
         into the stmt to not disturb pass specific information.   
         Replace real uses in the statement.   
         If we made a replacement, fold the statement.   
         Now cleanup.   
             If we cleaned up EH information from the statement,
             remove EH edges.   
             Determine what needs to be done to update the SSA form.   
bool update_call_from_tree ( )

Update a GIMPLE_CALL statement at iterator *SI_P to reflect the value of EXPR, which is expected to be the result of folding the call. This can only be done if EXPR is a CALL_EXPR with valid GIMPLE operands as arguments, or if it is a suitable RHS expression for a GIMPLE_ASSIGN. More complex expressions will require gimplification, which will introduce additional statements. In this event, no update is performed, and the function returns false. Note that we cannot mutate a GIMPLE_CALL in-place, so we always replace the statement at *SI_P with an entirely new statement. The new statement need not be a call, e.g., if the original call folded to a constant.

     The call has simplified to another call.   
     The call has simplified to an expression
     that cannot be represented as a GIMPLE_CALL.  
         A value is expected.
         Introduce a new GIMPLE_ASSIGN statement.   
         No value is expected, and EXPR has no effect.
         Replace it with an empty statement.   
         No value is expected, but EXPR has an effect,
         e.g., it could be a reference to a volatile
         variable.  Create an assignment statement
         with a dummy (unused) lhs variable.   
   The call simplified to an expression that is
   not a valid GIMPLE RHS.   

References release_defs(), and unlink_stmt_vdef().

Referenced by adjust_simduid_builtins(), fold_builtin_alloca_with_align(), and replace_phi_args_in().

bool update_gimple_call ( )

Update a GIMPLE_CALL statement at iterator *SI_P to call to FN with number of arguments NARGS, where the arguments in GIMPLE form follow NARGS argument.

Referenced by instrument_builtin_call().

static bool valid_gimple_call_p ( )
static

Return true if EXPR is a CALL_EXPR suitable for representation as a single GIMPLE_CALL statement. If the arguments require further gimplification, return false.

References gimple_block(), gimple_call_lhs(), gimple_call_set_lhs(), gimple_location(), gimple_set_block(), gimple_set_location(), gimple_set_vdef(), gimple_set_vuse(), gimple_vdef(), gimple_vuse(), gsi_replace(), move_ssa_defining_stmt_for_defs(), and NULL_TREE.

bool valid_gimple_rhs_p ( )

Return true if EXPR is an acceptable right-hand-side for a GIMPLE assignment. We validate the entire tree, not just the root node, thus catching expressions that embed complex operands that are not permitted in GIMPLE. This function is needed because the folding routines in fold-const.c may return such expressions in some cases, e.g., an array access with an embedded index addition. It may make more sense to have folding routines that are sensitive to the constraints on GIMPLE operands, rather than abandoning any any attempt to fold if the usual folding turns out to be too aggressive.

All constants are ok.

               ??? More checks needed, see the GIMPLE verifier.   

Variable Documentation

sbitmap bb_in_list
static
vec<basic_block> cfg_blocks
static

Array of control flow edges on the worklist.

int cfg_blocks_head
static

Referenced by cfg_blocks_empty_p().

unsigned int cfg_blocks_num = 0
static
int cfg_blocks_tail
static

Referenced by cfg_blocks_empty_p().

sbitmap executable_blocks
static

A bitmap to keep track of executable blocks in the CFG.

vec<gimple, va_gc>* interesting_ssa_edges
static

Worklist of SSA edges which will need reexamination as their definition has changed. SSA edges are def-use edges in the SSA web. For each D-U edge, we store the target statement or PHI node U.

struct prop_stats_d prop_stats
static

Referenced by replace_phi_args_in().

ssa_prop_visit_phi_fn ssa_prop_visit_phi
static
ssa_prop_visit_stmt_fn ssa_prop_visit_stmt
static

Generic SSA value propagation engine. Copyright (C) 2004-2013 Free Software Foundation, Inc. Contributed by Diego Novillo dnovi.nosp@m.llo@.nosp@m.redha.nosp@m.t.co.nosp@m.m

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/. This file implements a generic value propagation engine based on the same propagation used by the SSA-CCP algorithm [1].

Propagation is performed by simulating the execution of every statement that produces the value being propagated. Simulation proceeds as follows:

1- Initially, all edges of the CFG are marked not executable and the CFG worklist is seeded with all the statements in the entry basic block (block 0).

2- Every statement S is simulated with a call to the call-back function SSA_PROP_VISIT_STMT. This evaluation may produce 3 results:

SSA_PROP_NOT_INTERESTING: Statement S produces nothing of interest and does not affect any of the work lists.

SSA_PROP_VARYING: The value produced by S cannot be determined at compile time. Further simulation of S is not required. If S is a conditional jump, all the outgoing edges for the block are considered executable and added to the work list.

SSA_PROP_INTERESTING: S produces a value that can be computed at compile time. Its result can be propagated into the statements that feed from S. Furthermore, if S is a conditional jump, only the edge known to be taken is added to the work list. Edges that are known not to execute are never simulated.

3- PHI nodes are simulated with a call to SSA_PROP_VISIT_PHI. The return value from SSA_PROP_VISIT_PHI has the same semantics as described in #2.

4- Three work lists are kept. Statements are only added to these lists if they produce one of SSA_PROP_INTERESTING or SSA_PROP_VARYING.

CFG_BLOCKS contains the list of blocks to be simulated. Blocks are added to this list if their incoming edges are found executable.

VARYING_SSA_EDGES contains the list of statements that feed from statements that produce an SSA_PROP_VARYING result. These are simulated first to speed up processing.

INTERESTING_SSA_EDGES contains the list of statements that feed from statements that produce an SSA_PROP_INTERESTING result.

5- Simulation terminates when all three work lists are drained.

Before calling ssa_propagate, it is important to clear prop_simulate_again_p for all the statements in the program that should be simulated. This initialization allows an implementation to specify which statements should never be simulated.

It is also important to compute def-use information before calling ssa_propagate.

References:

[1] Constant propagation with conditional branches, Wegman and Zadeck, ACM TOPLAS 13(2):181-210.

[2] Building an Optimizing Compiler, Robert Morgan, Butterworth-Heinemann, 1998, Section 8.9.

[3] Advanced Compiler Design and Implementation, Steven Muchnick, Morgan Kaufmann, 1997, Section 12.6 Function pointers used to parameterize the propagation engine.

vec<gimple, va_gc>* varying_ssa_edges
static

Identical to INTERESTING_SSA_EDGES. For performance reasons, the list of SSA edges is split into two. One contains all SSA edges who need to be reexamined because their lattice value changed to varying (this worklist), and the other contains all other SSA edges to be reexamined (INTERESTING_SSA_EDGES).

Since most values in the program are VARYING, the ideal situation is to move them to that lattice value as quickly as possible. Thus, it doesn't make sense to process any other type of lattice value until all VARYING values are propagated fully, which is one thing using the VARYING worklist achieves. In addition, if we don't use a separate worklist for VARYING edges, we end up with situations where lattice values move from UNDEFINED->INTERESTING->VARYING instead of UNDEFINED->VARYING.