GCC Middle and Back End API Reference
tree-ssa-loop-manip.c File Reference
Include dependency graph for tree-ssa-loop-manip.c:

Functions

void create_iv (tree base, tree step, tree var, struct loop *loop, gimple_stmt_iterator *incr_pos, bool after, tree *var_before, tree *var_after)
static struct loopfind_sibling_superloop ()
static void compute_live_loop_exits (bitmap live_exits, bitmap use_blocks, bitmap *loop_exits, basic_block def_bb)
static void add_exit_phi ()
static void add_exit_phis_var ()
static void add_exit_phis ()
static void get_loops_exits ()
static void find_uses_to_rename_use (basic_block bb, tree use, bitmap *use_blocks, bitmap need_phis)
static void find_uses_to_rename_stmt ()
static void find_uses_to_rename_bb ()
static void find_uses_to_rename ()
void rewrite_into_loop_closed_ssa ()
static void check_loop_closed_ssa_use ()
static void check_loop_closed_ssa_stmt ()
DEBUG_FUNCTION void verify_loop_closed_ssa ()
basic_block split_loop_exit_edge ()
basic_block ip_end_pos ()
basic_block ip_normal_pos ()
void standard_iv_increment_position (struct loop *loop, gimple_stmt_iterator *bsi, bool *insert_after)
static void copy_phi_node_args ()
bool gimple_duplicate_loop_to_header_edge (struct loop *loop, edge e, unsigned int ndupl, sbitmap wont_exit, edge orig, vec< edge > *to_remove, int flags)
bool can_unroll_loop_p (struct loop *loop, unsigned factor, struct tree_niter_desc *niter)
static void determine_exit_conditions (struct loop *loop, struct tree_niter_desc *desc, unsigned factor, tree *enter_cond, tree *exit_base, tree *exit_step, enum tree_code *exit_cmp, tree *exit_bound)
static void scale_dominated_blocks_in_loop (struct loop *loop, basic_block bb, int num, int den)
void tree_transform_and_unroll_loop (struct loop *loop, unsigned factor, edge exit, struct tree_niter_desc *desc, transform_callback transform, void *data)
void tree_unroll_loop (struct loop *loop, unsigned factor, edge exit, struct tree_niter_desc *desc)
static void rewrite_phi_with_iv (loop_p loop, gimple_stmt_iterator *psi, gimple_stmt_iterator *gsi, tree main_iv)
static void rewrite_all_phi_nodes_with_iv ()
tree canonicalize_loop_ivs ()

Variables

static bitmap_obstack loop_renamer_obstack

Function Documentation

static void add_exit_phi ( )
static
   Add a loop-closing PHI for VAR in basic block EXIT.  
     Check that at least one of the edges entering the EXIT block exits
     the loop, or a superloop of that loop, that VAR is defined in.  
static void add_exit_phis ( )
static
   Add exit phis for the names marked in NAMES_TO_RENAME.
   Exits of the loops are stored in EXITS.  Sets of blocks where the ssa
   names are used are stored in USE_BLOCKS.  

References bitmap_set_bit(), edge_def::dest, get_loop_exit_edges(), basic_block_def::index, and loop::num.

static void add_exit_phis_var ( )
static
   Add exit phis for VAR that is used in LIVEIN.
   Exits of the loops are stored in LOOP_EXITS.  
bool can_unroll_loop_p ( struct loop loop,
unsigned  factor,
struct tree_niter_desc niter 
)
   Returns true if we can unroll LOOP FACTOR times.  Number
   of iterations of the loop is returned in NITER.  
     Check whether unrolling is possible.  We only want to unroll loops
     for that we are able to determine number of iterations.  We also
     want to split the extra iterations of the loop from its end,
     therefore we require that the loop has precisely one
     exit.  
         Scalar evolutions analysis might have copy propagated
         the abnormal ssa names into these expressions, hence
         emitting the computations based on them during loop
         unrolling might create overlapping life ranges for
         them, and failures in out-of-ssa.  
     And of course, we must be able to duplicate the loop.  
     The final loop should be small enough.  

References affine_iv_d::base, tree_niter_desc::bound, tree_niter_desc::cmp, tree_niter_desc::control, lower_bound_in_type(), affine_iv_d::step, tree_int_cst_sign_bit(), and upper_bound_in_type().

tree canonicalize_loop_ivs ( )
   Bases all the induction variables in LOOP on a single induction
   variable (unsigned with base 0 and step 1), whose final value is
   compared with *NIT.  When the IV type precision has to be larger
   than *NIT type precision, *NIT is converted to the larger type, the
   conversion code is inserted before the loop, and *NIT is updated to
   the new definition.  When BUMP_IN_LATCH is true, the induction
   variable is incremented in the loop latch, otherwise it is
   incremented in the loop header.  Return the induction variable that
   was created.  
     Make the loop exit if the control condition is not satisfied.  
static void check_loop_closed_ssa_stmt ( )
static
   Checks invariants of loop closed ssa form in statement STMT in BB.  
static void check_loop_closed_ssa_use ( )
static
   Check invariants of the loop closed ssa form for the USE in BB.  
static void compute_live_loop_exits ( bitmap  live_exits,
bitmap  use_blocks,
bitmap loop_exits,
basic_block  def_bb 
)
static
   DEF_BB is a basic block containing a DEF that needs rewriting into
   loop-closed SSA form.  USE_BLOCKS is the set of basic blocks containing
   uses of DEF that "escape" from the loop containing DEF_BB (i.e. blocks in
   USE_BLOCKS are dominated by DEF_BB but not in the loop father of DEF_B).
   ALL_EXITS[I] is the set of all basic blocks that exit loop I.

   Compute the subset of LOOP_EXITS that exit the loop containing DEF_BB
   or one of its loop fathers, in which DEF is live.  This set is returned
   in the bitmap LIVE_EXITS.

   Instead of computing the complete livein set of the def, we use the loop
   nesting tree as a form of poor man's structure analysis.  This greatly
   speeds up the analysis, which is important because this function may be
   called on all SSA names that need rewriting, one at a time.  
     Normally the work list size is bounded by the number of basic
     blocks in the largest loop.  We don't know this number, but we
     can be fairly sure that it will be relatively small.  
     Iterate until the worklist is empty.  
         Pull a block off the worklist.  
         Make sure we have at least enough room in the work list
         for all predecessors of this block.  
         For each predecessor block.  
             We should have met DEF_BB along the way.  
                 If we've reached DEF_LOOP, our train ends here.  
             Add PRED to the LIVEIN set.  PRED_VISITED is true if
             we had already added PRED to LIVEIN before.  
             If we have visited PRED before, don't add it to the worklist.
             If BB dominates PRED, then we're probably looking at a loop.
             We're only interested in looking up in the dominance tree
             because DEF_BB dominates all the uses.  
static void copy_phi_node_args ( )
static
   Copies phi node arguments for duplicated blocks.  The index of the first
   duplicated block is FIRST_NEW_BLOCK.  
void create_iv ( tree  base,
tree  step,
tree  var,
struct loop loop,
gimple_stmt_iterator incr_pos,
bool  after,
tree var_before,
tree var_after 
)
   Creates an induction variable with value BASE + STEP * iteration in LOOP.
   It is expected that neither BASE nor STEP are shared with other expressions
   (unless the sharing rules allow this).  Use VAR as a base var_decl for it
   (if NULL, a new temporary will be created).  The increment will occur at
   INCR_POS (after it if AFTER is true, before it otherwise).  INCR_POS and
   AFTER can be computed using standard_iv_increment_position.  The ssa versions
   of the variable before and after increment will be stored in VAR_BEFORE and
   VAR_AFTER (unless they are NULL).  
     For easier readability of the created code, produce MINUS_EXPRs
     when suitable.  
     Gimplify the step if necessary.  We put the computations in front of the
     loop (i.e. the step should be loop invariant).  

References add_phi_arg(), create_phi_node(), force_gimple_operand(), gimple_build_assign_with_ops(), gsi_insert_after(), gsi_insert_before(), gsi_insert_seq_on_edge_immediate(), GSI_NEW_STMT, loop::header, loop_latch_edge(), loop_preheader_edge(), make_ssa_name(), make_temp_ssa_name(), mark_addressable(), may_negate_without_overflow_p(), tree_expr_nonnegative_warnv_p(), and tree_int_cst_lt().

static void determine_exit_conditions ( struct loop loop,
struct tree_niter_desc desc,
unsigned  factor,
tree enter_cond,
tree exit_base,
tree exit_step,
enum tree_code exit_cmp,
tree exit_bound 
)
static
   Determines the conditions that control execution of LOOP unrolled FACTOR
   times.  DESC is number of iterations of LOOP.  ENTER_COND is set to
   condition that must be true if the main loop can be entered.
   EXIT_BASE, EXIT_STEP, EXIT_CMP and EXIT_BOUND are set to values describing
   how the exit from the unrolled loop should be controlled.  
     For pointers, do the arithmetics in the type of step.  
     We only need to be correct when we answer question
     "Do at least FACTOR more iterations remain?" in the unrolled loop.
     Thus, transforming BASE + STEP * i <> BOUND to
     BASE + STEP * i < BOUND is ok.  
     The main body of the loop may be entered iff:

     1) desc->may_be_zero is false.
     2) it is possible to check that there are at least FACTOR iterations
        of the loop, i.e., BOUND - step * FACTOR does not overflow.
     3) # of iterations is at least FACTOR  
     cond now may be a gimple comparison, which would be OK, but also any
     other gimple rhs (say a && b).  In this case we need to force it to
     operand.  

Referenced by scale_dominated_blocks_in_loop().

static struct loop* find_sibling_superloop ( )
staticread
   Return the innermost superloop LOOP of USE_LOOP that is a superloop of
   both DEF_LOOP and USE_LOOP.  
static void find_uses_to_rename ( )
static
   Marks names that are used outside of the loop they are defined in
   for rewrite.  Records the set of blocks in that the ssa
   names are defined to USE_BLOCKS.  If CHANGED_BBS is not NULL,
   scan only blocks in this set.  

References bitmap_empty_p(), bitmap_obstack_initialize(), cfun, LOOP_CLOSED_SSA, loops_state_set(), number_of_loops(), and update_ssa().

static void find_uses_to_rename_bb ( )
static
   Marks names that are used in BB and outside of the loop they are
   defined in for rewrite.  Records the set of blocks in that the ssa
   names are defined to USE_BLOCKS.  Record the SSA names that will
   need exit PHIs in NEED_PHIS.  
static void find_uses_to_rename_stmt ( )
static
   For uses in STMT, mark names that are used outside of the loop they are
   defined to rewrite.  Record the set of blocks in that the ssa
   names are defined to USE_BLOCKS and the ssa names themselves to
   NEED_PHIS.  

References find_uses_to_rename_use(), gimple_phi_result(), gsi_stmt(), and virtual_operand_p().

static void find_uses_to_rename_use ( basic_block  bb,
tree  use,
bitmap use_blocks,
bitmap  need_phis 
)
static
   For USE in BB, if it is used outside of the loop it is defined in,
   mark it for rewrite.  Record basic block BB where it is used
   to USE_BLOCKS.  Record the ssa name index to NEED_PHIS bitmap.  
     If the definition is not inside a loop, it is not interesting.  
     If the use is not outside of the loop it is defined in, it is not
     interesting.  
     If we're seeing VER for the first time, we still have to allocate
     a bitmap for its uses.  

Referenced by find_uses_to_rename_stmt().

static void get_loops_exits ( )
static
   Fill the array of bitmaps LOOP_EXITS with all loop exit edge targets.  

References gimple_bb(), basic_block_def::loop_father, and loop_outer().

bool gimple_duplicate_loop_to_header_edge ( struct loop loop,
edge  e,
unsigned int  ndupl,
sbitmap  wont_exit,
edge  orig,
vec< edge > *  to_remove,
int  flags 
)
   The same as cfgloopmanip.c:duplicate_loop_to_header_edge, but also
   updates the PHI nodes at start of the copied region.  In order to
   achieve this, only loops whose exits all lead to the same location
   are handled.

   Notice that we do not completely update the SSA web after
   duplication.  The caller is responsible for calling update_ssa
   after the loop has been duplicated.  
     ???  This forces needless update_ssa calls after processing each
     loop instead of just once after processing all loops.  We should
     instead verify that loop-closed SSA form is up-to-date for LOOP
     only (and possibly SSA form).  For now just skip verifying if
     there are to-be renamed variables.  
     Readd the removed phi args for e.  
     Copy the phi node arguments.  
basic_block ip_end_pos ( )
   Returns the basic block in that statements should be emitted for induction
   variables incremented at the end of the LOOP.  

References gsi_last_bb(), ip_normal_pos(), last_stmt(), and loop::latch.

basic_block ip_normal_pos ( )
   Returns the basic block in that statements should be emitted for induction
   variables incremented just before exit condition of a LOOP.  

References gsi_last_bb().

Referenced by ip_end_pos().

static void rewrite_all_phi_nodes_with_iv ( )
static
   Rewrite all the phi nodes of LOOP in function of the main induction
   variable MAIN_IV.  
void rewrite_into_loop_closed_ssa ( )
   Rewrites the program into a loop closed ssa form -- i.e. inserts extra
   phi nodes to ensure that no variable is used outside the loop it is
   defined in.

   This strengthening of the basic ssa form has several advantages:

   1) Updating it during unrolling/peeling/versioning is trivial, since
      we do not need to care about the uses outside of the loop.
      The same applies to virtual operands which are also rewritten into
      loop closed SSA form.  Note that virtual operands are always live
      until function exit.
   2) The behavior of all uses of an induction variable is the same.
      Without this, you need to distinguish the case when the variable
      is used outside of the loop it is defined in, for example

      for (i = 0; i < 100; i++)
        {
          for (j = 0; j < 100; j++)
            {
              k = i + j;
              use1 (k);
            }
          use2 (k);
        }

      Looking from the outer loop with the normal SSA form, the first use of k
      is not well-behaved, while the second one is an induction variable with
      base 99 and step 1.

      If CHANGED_BBS is not NULL, we look for uses outside loops only in
      the basic blocks in this set.

      UPDATE_FLAG is used in the call to update_ssa.  See
      TODO_update_ssa* for documentation.  
     If the pass has caused the SSA form to be out-of-date, update it
     now.  
     Uses of names to rename.  We don't have to initialize this array,
     because we know that we will only have entries for the SSA names
     in NAMES_TO_RENAME.  
     Find the uses outside loops.  
         An array of bitmaps where LOOP_EXITS[I] is the set of basic blocks
         that are the destination of an edge exiting loop number I.  
         Add the PHI nodes on exits of the loops for the names we need to
         rewrite.  
         Fix up all the names found to be used outside their original
         loops.  

Referenced by canonicalize_loop_closed_ssa().

static void rewrite_phi_with_iv ( loop_p  loop,
gimple_stmt_iterator psi,
gimple_stmt_iterator gsi,
tree  main_iv 
)
static
   Rewrite the phi node at position PSI in function of the main
   induction variable MAIN_IV and insert the generated code at GSI.  
static void scale_dominated_blocks_in_loop ( struct loop loop,
basic_block  bb,
int  num,
int  den 
)
static
   Scales the frequencies of all basic blocks in LOOP that are strictly
   dominated by BB by NUM/DEN.  

References determine_exit_conditions(), expected_loop_iterations(), edge_def::flags, integer_nonzerop(), loop_preheader_edge(), loop_version(), loop::next, prob, update_ssa(), and vNULL.

basic_block split_loop_exit_edge ( )
   Split loop exit edge EXIT.  The things are a bit complicated by a need to
   preserve the loop closed ssa form.  The newly created block is returned.  
         If the argument of the PHI node is a constant, we do not need
         to keep it inside loop.  
         Otherwise create an auxiliary phi node that will copy the value
         of the SSA name out of the loop.  

References loop::latch.

void standard_iv_increment_position ( struct loop loop,
gimple_stmt_iterator bsi,
bool *  insert_after 
)
   Stores the standard position for induction variable increment in LOOP
   (just before the exit condition if it is available and latch block is empty,
   end of the latch block otherwise) to BSI.  INSERT_AFTER is set to true if
   the increment should be inserted after *BSI.  

References cfun, LOOP_CLOSED_SSA, LOOPS_HAVE_PREHEADERS, LOOPS_HAVE_SIMPLE_LATCHES, loops_state_satisfies_p(), and need_ssa_update_p().

void tree_transform_and_unroll_loop ( struct loop loop,
unsigned  factor,
edge  exit,
struct tree_niter_desc desc,
transform_callback  transform,
void *  data 
)
     Let us assume that the unrolled loop is quite likely to be entered.  
     The values for scales should keep profile consistent, and somewhat close
     to correct.

     TODO: The current value of SCALE_REST makes it appear that the loop that
     is created by splitting the remaining iterations of the unrolled loop is
     executed the same number of times as the original loop, and with the same
     frequencies, which is obviously wrong.  This does not appear to cause
     problems, so we do not bother with fixing it for now.  To make the profile
     correct, we would need to change the probability of the exit edge of the
     loop, and recompute the distribution of frequencies in its body because
     of this change (scale the frequencies of blocks before and after the exit
     by appropriate factors).  
     Determine the probability of the exit edge of the unrolled loop.  
     Without profile feedback, loops for that we do not know a better estimate
     are assumed to roll 10 times.  When we unroll such loop, it appears to
     roll too little, and it may even seem to be cold.  To avoid this, we
     ensure that the created loop appears to roll at least 5 times (but at
     most as many times as before unrolling).  
     Prepare the cfg and update the phi nodes.  Move the loop exit to the
     loop latch (and make its condition dummy, for the moment).  
     Since the exit edge will be removed, the frequency of all the blocks
     in the loop that are dominated by it must be scaled by
     1 / (1 - exit->probability).  
     Set the probability of new exit to the same of the old one.  Fix
     the frequency of the latch block, by scaling it back by
     1 - exit->probability.  
         Prefer using original variable as a base for the new ssa name.
         This is necessary for virtual ops, and useful in order to avoid
         losing debug info for real ops.  
     Transform the loop.  
     Unroll the loop and remove the exits in all iterations except for the
     last one.  
     Ensure that the frequencies in the loop match the new estimated
     number of iterations, and change the probability of the new
     exit edge.  
     Finally create the new counter for number of iterations and add the new
     exit instruction.  
void tree_unroll_loop ( struct loop loop,
unsigned  factor,
edge  exit,
struct tree_niter_desc desc 
)
   Wrapper over tree_transform_and_unroll_loop for case we do not
   want to transform the loop before unrolling.  The meaning
   of the arguments is the same as for tree_transform_and_unroll_loop.  

Referenced by insn_to_prefetch_ratio_too_small_p().

DEBUG_FUNCTION void verify_loop_closed_ssa ( )
   Checks that invariants of the loop closed ssa form are preserved.
   Call verify_ssa when VERIFY_SSA_P is true.  

References add_phi_arg(), create_phi_node(), duplicate_ssa_name(), gimple_phi_arg_location_from_edge(), gsi_end_p(), gsi_next(), gsi_start_phis(), gsi_stmt(), single_succ_edge(), and split_edge().

Referenced by canonicalize_loop_closed_ssa(), and close_phi_written_to_memory().


Variable Documentation

bitmap_obstack loop_renamer_obstack
static
@verbatim 

High-level loop manipulation functions. Copyright (C) 2004-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/.

   All bitmaps for rewriting into loop-closed SSA go on this obstack,
   so that we can free them all at once.