GCC Middle and Back End API Reference
tree-ssa-loop-ivcanon.c File Reference

Data Structures

struct  loop_size

Enumerations

enum  unroll_level { UL_SINGLE_ITER, UL_NO_GROWTH, UL_ALL }

Functions

static void create_canonical_iv ()
static bool constant_after_peeling ()
static bool tree_estimate_loop_size (struct loop *loop, edge exit, edge edge_to_cancel, struct loop_size *size, int upper_bound)
static unsigned HOST_WIDE_INT estimated_unrolled_size (struct loop_size *size, unsigned HOST_WIDE_INT nunroll)
static edge loop_edge_to_cancel ()
static bool remove_exits_and_undefined_stmts ()
static bool remove_redundant_iv_tests ()
static void unloop_loops (bitmap loop_closed_ssa_invalidated, bool *irred_invalidated)
static bool try_unroll_loop_completely (struct loop *loop, edge exit, tree niter, enum unroll_level ul, HOST_WIDE_INT maxiter, location_t locus)
static bool canonicalize_loop_induction_variables (struct loop *loop, bool create_iv, enum unroll_level ul, bool try_eval)
unsigned int canonicalize_induction_variables ()
static void propagate_into_all_uses ()
static void propagate_constants_for_unrolling ()
static bool tree_unroll_loops_completely_1 (bool may_increase_size, bool unroll_outer, vec< loop_p, va_heap > &father_stack, struct loop *loop)
unsigned int tree_unroll_loops_completely ()
static unsigned int tree_ssa_loop_ivcanon ()
static bool gate_tree_ssa_loop_ivcanon ()
gimple_opt_passmake_pass_iv_canon ()
static unsigned int tree_complete_unroll ()
static bool gate_tree_complete_unroll ()
gimple_opt_passmake_pass_complete_unroll ()
static unsigned int tree_complete_unroll_inner ()
static bool gate_tree_complete_unroll_inner ()
gimple_opt_passmake_pass_complete_unrolli ()

Variables

static vec< loop_ploops_to_unloop
static vec< int > loops_to_unloop_nunroll

Enumeration Type Documentation

@verbatim 

Induction variable canonicalization and loop peeling. 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/.

   This pass detects the loops that iterate a constant number of times,
   adds a canonical induction variable (step -1, tested against 0)
   and replaces the exit test.  This enables the less powerful rtl
   level analysis to use this information.

   This might spoil the code in some cases (by increasing register pressure).
   Note that in the case the new variable is not needed, ivopts will get rid
   of it, so it might only be a problem when there are no other linear induction
   variables.  In that case the created optimization possibilities are likely
   to pay up.

   Additionally in case we detect that it is beneficial to unroll the
   loop completely, we do it right here to expose the optimization
   possibilities to the following passes.  
   Specifies types of loops that may be unrolled.  
Enumerator:
UL_SINGLE_ITER 
UL_NO_GROWTH 
UL_ALL 

Function Documentation

unsigned int canonicalize_induction_variables ( )
   The main entry point of the pass.  Adds canonical induction variables
   to the suitable loops.  
     Clean up the information about numbers of iterations, since brute force
     evaluation could reveal new information.  
static bool canonicalize_loop_induction_variables ( struct loop loop,
bool  create_iv,
enum unroll_level  ul,
bool  try_eval 
)
static
   Adds a canonical induction variable to LOOP if suitable.
   CREATE_IV is true if we may create a new iv.  UL determines
   which loops we are allowed to completely unroll.  If TRY_EVAL is true, we try
   to determine the number of iterations of a loop by direct evaluation.
   Returns true if cfg is changed.   
         If the loop has more than one exit, try checking all of them
         for # of iterations determinable through scev.  
         Finally if everything else fails, try brute force evaluation.  
     We work exceptionally hard here to estimate the bound
     by find_loop_niter_by_eval.  Be sure to keep it for future.  
     Force re-computation of loop bounds so we can remove redundant exits.  
     Remove exits that are known to be never taken based on loop bound.
     Needs to be called after compilation of max_loop_iterations_int that
     populates the loop bounds.  
static bool constant_after_peeling ( )
static
   Return true if OP in STMT will be constant after peeling LOOP.  
     We can still fold accesses to constant arrays when index is known.  
         First make fast look if we see constant array inside.  
             If so, see if we understand all the indices.  
     Induction variables are constants.  

References handled_component_p().

static void create_canonical_iv ( )
static
   Adds a canonical induction variable to LOOP iterating NITER times.  EXIT
   is the exit edge whose condition is replaced.  
     Note that we do not need to worry about overflows, since
     type of niter is always unsigned and all comparisons are
     just for equality/nonequality -- i.e. everything works
     with a modulo arithmetics.  

References dump_file, loop::num, and print_generic_expr().

static unsigned HOST_WIDE_INT estimated_unrolled_size ( struct loop_size size,
unsigned HOST_WIDE_INT  nunroll 
)
static
   Estimate number of insns of completely unrolled loop.
   It is (NUNROLL + 1) * size of loop body with taking into account
   the fact that in last copy everything after exit conditional
   is dead and that some instructions will be eliminated after
   peeling.

   Loop body is likely going to simplify further, this is difficult
   to guess, we just decrease the result by 1/3.  

References edge_def::flags, get_loop_exit_edges(), loop::latch, basic_block_def::preds, edge_def::src, and basic_block_def::succs.

static bool gate_tree_complete_unroll ( )
static
static bool gate_tree_complete_unroll_inner ( )
static
static bool gate_tree_ssa_loop_ivcanon ( )
static
static edge loop_edge_to_cancel ( )
static
   Loop LOOP is known to not loop.  See if there is an edge in the loop
   body that can be remove to make the loop to always exit and at
   the same time it does not make any code potentially executed 
   during the last iteration dead.  

   After complette unrolling we still may get rid of the conditional
   on the exit in the last copy even if we have no idea what it does.
   This is quite common case for loops of form

     int a[5];
     for (i=0;i<b;i++)
       a[i]=0;

   Here we prove the loop to iterate 5 times but we do not know
   it from induction variable.

   For now we handle only simple case where there is exit condition
   just before the latch block and the latch block contains no statements
   with side effect that may otherwise terminate the execution of loop
   (such as by EH or by terminating the program or longjmp).

   In the general case we may want to cancel the paths leading to statements
   loop-niter identified as having undefined effect in the last iteration.
   The other cases are hopefully rare and will be cleaned up later.  
     We want only one predecestor of the loop.  
          Find the other edge than the loop exit
          leaving the conditoinal.  
         We only can handle conditionals.  
         We should never have conditionals in the loop latch. 
         Check that it leads to loop latch.  
         Verify that the code in loop latch does nothing that may end program
         execution without really reaching the exit.  This may include
         non-pure/const function calls, EH statements, volatile ASMs etc.  
gimple_opt_pass* make_pass_complete_unroll ( )
gimple_opt_pass* make_pass_complete_unrolli ( )
gimple_opt_pass* make_pass_iv_canon ( )
static void propagate_constants_for_unrolling ( )
static
   Propagate constant SSA_NAMEs defined in basic block BB.  
     Look for degenerate PHI nodes with constant argument.  
     Look for assignments to SSA names with constant RHS.  
static void propagate_into_all_uses ( )
static
   Propagate VAL into all uses of SSA_NAME.  
static bool remove_exits_and_undefined_stmts ( )
static
   Remove all tests for exits that are known to be taken after LOOP was
   peeled NPEELED times. Put gcc_unreachable before every statement
   known to not be executed.  
         If statement is known to be undefined after peeling, turn it
         into unreachable (or trap when debugging experience is supposed
         to be good).  
         If we know the exit will be taken after peeling, update.  

References dump_file, print_gimple_stmt(), and nb_iter_bound::stmt.

static bool remove_redundant_iv_tests ( )
static
   Remove all exits that are known to be never taken because of the loop bound
   discovered.  
         Exit is pointless if it won't be taken before loop reaches
         upper bound.  
             Only when we know the actual number of iterations, not
             just a bound, we can remove the exit.  
static unsigned int tree_complete_unroll ( )
static
   Complete unrolling of loops.  
static unsigned int tree_complete_unroll_inner ( )
static
   Complete unrolling of inner loops.  

Referenced by gate_tree_complete_unroll().

static bool tree_estimate_loop_size ( struct loop loop,
edge  exit,
edge  edge_to_cancel,
struct loop_size size,
int  upper_bound 
)
static
   Computes an estimated number of insns in LOOP.
   EXIT (if non-NULL) is an exite edge that will be eliminated in all but last
   iteration of the loop.
   EDGE_TO_CANCEL (if non-NULL) is an non-exit edge eliminated in the last iteration
   of loop.
   Return results in SIZE, estimate benefits for complete unrolling exiting by EXIT. 
   Stop estimating after UPPER_BOUND is met.  Return true in this case.  
             Look for reasons why we might optimize this stmt away. 
             Exit conditional.  
             Sets of IV variables  
             Assignments of IV variables.  
             Conditionals.  
static unsigned int tree_ssa_loop_ivcanon ( )
static
   Canonical induction variable creation pass.  
unsigned int tree_unroll_loops_completely ( )
   Unroll LOOPS completely if they iterate just few times.  Unless
   MAY_INCREASE_SIZE is true, perform the unrolling only if the
   size of the code does not increase.  
             Be sure to skip unlooped loops while procesing father_stack
             array.  
             We can not use TODO_update_ssa_no_phi because VOPS gets confused.  
             Propagate the constants within the new basic blocks.  
             This will take care of removing completely unrolled loops
             from the loop structures so we can continue unrolling now
             innermost loops.  
             Clean up the information about numbers of iterations, since
             complete unrolling might have invalidated it.  
static bool tree_unroll_loops_completely_1 ( bool  may_increase_size,
bool  unroll_outer,
vec< loop_p, va_heap > &  father_stack,
struct loop loop 
)
static
   Process loops from innermost to outer, stopping at the innermost
   loop we unrolled.  
     Process inner loops first.  
     If we changed an inner loop we cannot process outer loops in this
     iteration because SSA form is not up-to-date.  Continue with
     siblings of outer loops instead.  
     Don't unroll #pragma omp simd loops until the vectorizer
     attempts to vectorize those.  
     Try to unroll this loop.  
         Unroll outermost loops only if asked to do so or they do
         not cause code growth.  
         If we'll continue unrolling, we need to propagate constants
         within the new basic blocks to fold away induction variable
         computations; otherwise, the size might blow up before the
         iteration is complete and the IR eventually cleaned up.  
static bool try_unroll_loop_completely ( struct loop loop,
edge  exit,
tree  niter,
enum unroll_level  ul,
HOST_WIDE_INT  maxiter,
location_t  locus 
)
static
   Tries to unroll LOOP completely, i.e. NITER times.
   UL determines which loops we are allowed to unroll.
   EXIT is the exit of the loop that should be eliminated.
   MAXITER specfy bound on number of iterations, -1 if it is
   not known or too large for HOST_WIDE_INT.  The location
   LOCUS corresponding to the loop is used when emitting
   a summary of the unroll to the dump file.  
     See if we proved number of iterations to be low constant.

     EXIT is an edge that will be removed in all but last iteration of 
     the loop.

     EDGE_TO_CACNEL is an edge that will be removed from the last iteration
     of the unrolled sequence and is expected to make the final loop not
     rolling. 

     If the number of execution of loop is determined by standard induction
     variable test, then EXIT and EDGE_TO_CANCEL are the two edges leaving
     from the iv test.  
     We do not know the number of iterations and thus we can not eliminate
     the EXIT edge.  
     See if we can improve our estimate by using recorded loop bounds.  
         Loop terminates before the IV variable test, so we can not
         remove it in the last iteration.  
         If the code is going to shrink, we don't need to be extra cautious
         on guessing if the unrolling is going to be profitable.  
             If there is IV variable that will become constant, we save
             one instruction in the loop prologue we do not account
             otherwise.  
         We unroll only inner loops, because we do not consider it profitable
         otheriwse.  We still can cancel loopback edge of not rolling loop;
         this is always a good idea.  
         Outer loops tend to be less interesting candidates for complette
         unrolling unless we can do a lot of propagation into the inner loop
         body.  For now we disable outer loop unrolling when the code would
         grow.  
         If there is call on a hot path through the loop, then
         there is most probably not much to optimize.  
         If there is pure/const call in the function, then we
         can still optimize the unrolled loop body if it contains
         some other interesting code than the calls and code
         storing or cumulating the return value.  
                  One IV increment, one test, one ivtmp store
                  and one useful stmt.  That is about minimal loop
                  doing pure call.  
         Complette unrolling is major win when control flow is removed and
         one big basic block is created.  If the loop contains control flow
         the optimization may still be a win because of eliminating the loop
         overhead but it also may blow the branch predictor tables.
         Limit number of branches on the hot path through the peeled
         sequence.  
     Remove the conditional from the last copy of the loop.  
         Do not remove the path. Doing so may remove outer loop
         and confuse bookkeeping code in tree_unroll_loops_completelly.  
     Store the loop for later unlooping and exit removal.  
static void unloop_loops ( bitmap  loop_closed_ssa_invalidated,
bool *  irred_invalidated 
)
static
   Cancel all fully unrolled loops by putting __builtin_unreachable
   on the latch edge.  
   We do it after all unrolling since unlooping moves basic blocks
   across loop boundaries trashing loop closed SSA form as well
   as SCEV info needed to be intact during unrolling. 

   IRRED_INVALIDATED is used to bookkeep if information about
   irreducible regions may become invalid as a result
   of the transformation.  
   LOOP_CLOSED_SSA_INVALIDATED is used to bookkepp the case
   when we need to go into loop closed SSA form.  
         Unloop destroys the latch edge.  
         Create new basic block for the latch edge destination and wire
         it in.  

References host_integerp(), HOST_WIDE_INT, loop::ninsns, edge_def::src, and tree_low_cst().


Variable Documentation

vec<loop_p> loops_to_unloop
static
   Stores loops that will be unlooped after we process whole loop tree. 
vec<int> loops_to_unloop_nunroll
static