GCC Middle and Back End API Reference
loop-invariant.c File Reference

Data Structures

struct  loop_data
struct  use
struct  def
struct  invariant
struct  invariant_expr_entry
struct  invariant_expr_hasher


typedef struct invariantinvariant_p
typedef hash_table
< invariant_expr_hasher


static void check_invariant_table_size ()
static bool check_maybe_invariant ()
static struct invariantinvariant_for_use ()
static hashval_t hash_invariant_expr_1 ()
static bool invariant_expr_equal_p ()
static struct invariantfind_or_insert_inv (invariant_htab_type eq, rtx expr, enum machine_mode mode, struct invariant *inv)
static void find_identical_invariants ()
static void merge_identical_invariants ()
static void compute_always_reached (struct loop *loop, basic_block *body, bitmap may_exit, bitmap always_reached)
static void find_exits (struct loop *loop, basic_block *body, bitmap may_exit, bitmap has_exit)
static bool may_assign_reg_p ()
static void find_defs ()
static struct invariantcreate_new_invariant (struct def *def, rtx insn, bitmap depends_on, bool always_executed)
static void record_use ()
static bool check_dependency ()
static bool check_dependencies ()
static void find_invariant_insn ()
static void record_uses ()
static void find_invariants_insn ()
static void find_invariants_bb ()
static void find_invariants_body (struct loop *loop, basic_block *body, bitmap always_reached, bitmap always_executed)
static void find_invariants ()
static void free_use_list ()
static enum reg_class get_pressure_class_and_nregs ()
static void get_inv_cost ()
static int gain_for_invariant (struct invariant *inv, unsigned *regs_needed, unsigned *new_regs, unsigned regs_used, bool speed, bool call_p)
static int best_gain_for_invariant (struct invariant **best, unsigned *regs_needed, unsigned *new_regs, unsigned regs_used, bool speed, bool call_p)
static void set_move_mark ()
static void find_invariants_to_move ()
static int replace_uses ()
static bool move_invariant_reg ()
static void move_invariants ()
static void init_inv_motion_data ()
static void free_inv_motion_data ()
static void move_single_loop_invariants ()
static void free_loop_data ()
static enum reg_class get_regno_pressure_class ()
static void change_pressure ()
static void mark_regno_live ()
static void mark_regno_death ()
static void mark_reg_store (rtx reg, const_rtx setter, void *data)
static void mark_reg_clobber ()
static void mark_reg_death ()
static void mark_ref_regs ()
static void calculate_loop_reg_pressure ()
void move_loop_invariants ()


static struct loopcurr_loop
static unsigned int invariant_table_size = 0
static struct invariant ** invariant_table
static unsigned actual_stamp
static vec< invariant_pinvariants
static bitmap_head curr_regs_live
static int curr_reg_pressure [N_REG_CLASSES]
static int n_regs_set

Typedef Documentation

typedef struct invariant* invariant_p

Function Documentation

static int best_gain_for_invariant ( struct invariant **  best,
unsigned *  regs_needed,
unsigned *  new_regs,
unsigned  regs_used,
bool  speed,
bool  call_p 
   Finds invariant with best gain for moving.  Returns the gain, stores
   the invariant in *BEST and number of registers needed for it to
   *REGS_NEEDED.  REGS_USED is the number of registers used in the loop.
   NEW_REGS is the number of new variables already added due to invariant
         Only consider the "representatives" of equivalent invariants.  

References df.

static void calculate_loop_reg_pressure ( )
   Calculate register pressure in the loops.  
             Mark any registers dead after INSN as dead now.  
             Mark any registers set in INSN as live,
             and mark them as conflicting with all other live regs.
             Clobbers are processed again, so they conflict with
             the registers that are set.  
static void change_pressure ( )
   Increase (if INCR_P) or decrease current register pressure for
   register REGNO.  
static bool check_dependencies ( )
   Finds the invariants INSN depends on and store them to the DEPENDS_ON
   bitmap.  Returns true if all dependencies of INSN are known to be
   loop invariants, false otherwise.  
static bool check_dependency ( )
   Finds the invariants USE depends on and store them to the DEPENDS_ON
   bitmap.  Returns true if all dependencies of USE are known to be
   loop invariants, false otherwise.  
         If this is the use of an uninitialized argument register that is
         likely to be spilled, do not move it lest this might extend its
         lifetime and cause reload to die.  This can occur for a call to
         a function taking complex number arguments and moving the insns
         preparing the arguments without moving the call itself wouldn't
         gain much in practice.  
     Note that in case bb == def_bb, we know that the definition
     dominates insn, because def has invariant_table[DF_REF_ID(def)]
     defined and we process the insns in the basic block bb
static void check_invariant_table_size ( )
   Check the size of the invariant table and realloc if necessary.  
static bool check_maybe_invariant ( )
   Test for possibility of invariantness of X.  
         Load/store motion is done elsewhere.  ??? Perhaps also add it here?
         It should not be hard, and might be faster than "elsewhere".  
         Just handle the most trivial case where we load from an unchanging
         location (most importantly, pic tables).  
         Don't mess with insns declared volatile.  
static void compute_always_reached ( struct loop loop,
basic_block body,
bitmap  may_exit,
bitmap  always_reached 
   Determines the basic blocks inside LOOP that are always executed and
   stores their bitmap to ALWAYS_REACHED.  MAY_EXIT is a bitmap of
   basic blocks that may either exit the loop, or contain the call that
   does not have to return.  BODY is body of the loop obtained by

References bitmap_set_bit(), edge_def::dest, find_common_loop(), flow_bb_inside_loop_p(), and basic_block_def::loop_father.

static struct invariant* create_new_invariant ( struct def def,
rtx  insn,
bitmap  depends_on,
bool  always_executed 
   Creates a new invariant for definition DEF in INSN, depending on invariants
   in DEPENDS_ON.  ALWAYS_EXECUTED is true if the insn is always executed,
   unless the program ends due to a function call.  The newly created invariant
   is returned.  
     If the set is simple, usually by moving it we move the whole store out of
     the loop.  Otherwise we save only cost of the computation.  
         ??? Try to determine cheapness of address computation.  Unfortunately
         the address cost is only a relative measure, we can't really compare
         it with any absolute number, but only with other address costs.
         But here we don't have any other addresses, so compare with a magic
         number anyway.  It has to be large enough to not regress PR33928
         (by avoiding to move reg+8,reg+16,reg+24 invariants), but small
         enough to not regress 410.bwaves either (by still moving reg+reg
         See http://gcc.gnu.org/ml/gcc-patches/2009-10/msg01210.html .  
static void find_defs ( )
   Finds definitions that may correspond to invariants in LOOP with body

References address_cost(), invariant::cheap_address, invariant::cost, set_rtx_cost(), and word_mode.

static void find_exits ( struct loop loop,
basic_block body,
bitmap  may_exit,
bitmap  has_exit 
   Finds exits out of the LOOP with body BODY.  Marks blocks in that we may
   exit the loop by cfg edge to HAS_EXIT and MAY_EXIT.  In MAY_EXIT
   additionally mark blocks that may exit due to a call.  
         Use the data stored for the subloop to decide whether we may exit
         through it.  It is sufficient to do this for header of the loop,
         as other basic blocks inside it must be dominated by it.  

References bitmap_set_bit().

static void find_identical_invariants ( )
   Finds invariants identical to INV and records the equivalence.  EQ is the
   hash table of the invariants.  

References bitmap_bit_p(), bitmap_set_bit(), CDI_DOMINATORS, and dominated_by_p().

Referenced by find_or_insert_inv().

static void find_invariant_insn ( )
   Finds invariant in INSN.  ALWAYS_REACHED is true if the insn is always
   executed.  ALWAYS_EXECUTED is true if the insn is always executed,
   unless the program ends due to a function call.  
     We can't move a CC0 setter without the user.  
     If the insn can throw exception, we cannot move it at all without changing
     We cannot make trapping insn executed, unless it was executed before.  

References invariant::def, inv(), invariant_for_use(), and record_use().

static void find_invariants ( )
   Finds invariants in LOOP.  
static void find_invariants_bb ( )
   Finds invariants in basic block BB.  ALWAYS_REACHED is true if the
   basic block is always executed.  ALWAYS_EXECUTED is true if the basic
   block is always executed, unless the program ends due to a function

References free(), and use::next.

Referenced by record_uses().

static void find_invariants_body ( struct loop loop,
basic_block body,
bitmap  always_reached,
bitmap  always_executed 
   Finds invariants in LOOP with body BODY.  ALWAYS_REACHED is the bitmap of
   basic blocks in BODY that are always executed.  ALWAYS_EXECUTED is the
   bitmap of basic blocks in BODY that are always executed unless the program
   ends due to a function call.  
static void find_invariants_insn ( )
   Finds invariants in INSN.  ALWAYS_REACHED is true if the insn is always
   executed.  ALWAYS_EXECUTED is true if the insn is always executed,
   unless the program ends due to a function call.  
static void find_invariants_to_move ( )
   Determines which invariants to move.  
       REGS_USED is actually never used when the flag is on.  
       We do not really do a good job in estimating number of
       registers used; we put some initial bound here to stand for
       induction variables etc.  that we do not detect.  
                 This is a value that is used but not changed inside loop.  

References apply_change_group(), invariant::depends_on, emit_insn_after(), invariant::eqto, gen_move_insn(), gen_reg_rtx_and_attrs(), invariant::insn, invariant::invno, loop_preheader_edge(), invariant::move, move_invariant_reg(), invariant::reg, reorder_insns(), replace_uses(), edge_def::src, and validate_change().

static struct invariant* find_or_insert_inv ( invariant_htab_type  eq,
rtx  expr,
enum machine_mode  mode,
struct invariant inv 
   Checks whether invariant with value EXPR in machine mode MODE is
   recorded in EQ.  If this is the case, return the invariant.  Otherwise
   insert INV to the table for this expression and return INV.  

References find_identical_invariants().

static void free_inv_motion_data ( )
   Frees the data allocated by invariant motion.  
static void free_loop_data ( )
   Releases the auxiliary data for LOOP.  

References regs_set.

static void free_use_list ( )
   Frees a list of uses USE.  
static int gain_for_invariant ( struct invariant inv,
unsigned *  regs_needed,
unsigned *  new_regs,
unsigned  regs_used,
bool  speed,
bool  call_p 
   Calculates gain for eliminating invariant INV.  REGS_USED is the number
   of registers used in the loop, NEW_REGS is the number of new variables
   already added due to the invariant motion.  The number of registers needed
   for it is stored in *REGS_NEEDED.  SPEED and CALL_P are flags passed
   through to estimate_reg_pressure_cost. 
           There will be register pressure excess and we want not to
           make this loop invariant motion.  All loop invariants with
           non-positive gains will be rejected in function
           find_invariants_to_move.  Therefore we return the negative
           number here.

           One could think that this rejects also expensive loop
           invariant motions and this will hurt code performance.
           However numerous experiments with different heuristics
           taking invariant cost into account did not confirm this
           assumption.  There are possible explanations for this
           o probably all expensive invariants were already moved out
             of the loop by PRE and gimple invariant motion pass.
           o expensive invariant execution will be hidden by insn
             scheduling or OOO processor hardware because usually such
             invariants have a lot of freedom to be executed
           Another reason for ignoring invariant cost vs spilling cost
           heuristics is also in difficulties to evaluate accurately
           spill cost at this stage.  

References invariant::eqto, inv(), invariant::invno, and invariant::move.

static void get_inv_cost ( )
   Calculates cost and number of registers needed for moving invariant INV
   out of the loop and stores them to *COST and *REGS_NEEDED.  
     Find the representative of the class of the equivalent invariants.  
       Hoisting constant pool constants into stack regs may cost more than
       just single register.  On x87, the balance is affected both by the
       small number of FP registers, and by its register stack organization,
       that forces us to add compensation code in and around the loop to
       shuffle the operands to the top of stack before use, and pop them
       from the stack after the loop finishes.

       To model this effect, we increase the number of registers needed for
       stack registers by two: one register push, and one register pop.
       This usually has the effect that FP constant loads from the constant
       pool are not moved out of the loop.

       Note that this also means that dependent invariants can not be moved.
       However, the primary purpose of this pass is to move loop invariant
       address arithmetic out of loops, and address arithmetic that depends
       on floating point constants is unlikely to ever occur.  
             We need to check always_executed, since if the original value of
             the invariant may be preserved, we may need to keep it in a
             separate register.  TODO check whether the register has an
             use outside of the loop.  
             If this is a single use, after moving the dependency we will not
             need a new register.  

References invariant::always_executed, invariant::def, get_pressure_class_and_nregs(), invariant::insn, use::next, and def::uses.

static enum reg_class get_pressure_class_and_nregs ( )
   Return pressure class and number of hard registers (through *NREGS)
   for destination of INSN. 
     Considered invariant insns have only one set.  

Referenced by get_inv_cost().

static enum reg_class get_regno_pressure_class ( )
   Return pressure class and number of needed hard registers (through
   *NREGS) of register REGNO.  

Referenced by mark_ref_regs().

static hashval_t hash_invariant_expr_1 ( )
   Computes hash value for invariant expression X in INSN.  
static void init_inv_motion_data ( )
   Initializes invariant motion data.  
static bool invariant_expr_equal_p ( )
   Returns true if the invariant expressions E1 and E2 used in insns INSN1
   and INSN2 have always the same value.  
     If mode of only one of the operands is VOIDmode, it is not equivalent to
     the other one.  If both are VOIDmode, we rely on the caller of this
     function to verify that their modes are the same.  
         Unhandled type of subexpression, we fail conservatively.  
static struct invariant* invariant_for_use ( )
   Returns the invariant definition for USE, or NULL if USE is not

References df_find_use(), invariant::eqto, and hash_rtx().

Referenced by find_invariant_insn().

static void mark_ref_regs ( )
   Mark occurrence of registers in X for the current loop.  

References bitmap_bit_p(), and get_regno_pressure_class().

static void mark_reg_clobber ( )
   Mark clobbering register REG.  
static void mark_reg_death ( )
   Mark register REG death.  
static void mark_reg_store ( rtx  reg,
const_rtx  setter,
void *  data 
   Mark setting register REG.  
static void mark_regno_death ( )
   Mark REGNO death.  

References loop::aux.

static void mark_regno_live ( )
   Mark REGNO birth.  
static bool may_assign_reg_p ( )
   Check whether we may assign a value to X from a register.  

References invariant::always_executed, invariant::def, and optimize_bb_for_speed_p().

static void merge_identical_invariants ( )
   Find invariants with the same value and record the equivalences.  

References bitmap_set_bit().

static bool move_invariant_reg ( )
   Move invariant INVNO out of the LOOP.  Returns true if this succeeds, false
     If this is a representative of the class of equivalent invariants,
     really move the invariant.  Otherwise just replace its use with
     the register used for the representative.  
         Move the set out of the loop.  If the set is always executed (we could
         omit this condition if we know that the register is unused outside of
         the loop, but it does not seem worth finding out) and it has no uses
         that would not be dominated by it, we may just move it (TODO).
         Otherwise we need to create a temporary register.  
         Try replacing the destination by a new pseudoregister.  
         As well as all the dominated uses.  
         And validate all the changes.  
         If there is a REG_EQUAL note on the insn we just moved, and the
         insn is in a basic block that is not always executed or the note
         contains something for which we don't know the invariant status,
         the note may no longer be valid after we move the insn.  Note that
         uses in REG_EQUAL notes are taken into account in the computation
         of invariants, so it is safe to retain the note even if it contains
         register references for which we know the invariant status.  
     If we failed, clear move flag, so that we do not try to move inv

Referenced by find_invariants_to_move().

static void move_invariants ( )
   Move selected invariant out of the LOOP.  Newly created regs are marked

References reg_allocno_class().

void move_loop_invariants ( void  )
   Move the invariants out of the loops.  
     Process the loops, innermost first.  
         move_single_loop_invariants for very large loops
         is time consuming and might need a lot of memory.  
       There is no sense to keep this info because it was most
       probably outdated by subsequent passes.  
static void move_single_loop_invariants ( )
   Move the invariants out of the LOOP.  
static void record_use ( )
   Record USE at DEF.  

Referenced by find_invariant_insn().

static void record_uses ( )
   Record registers used in INSN that have a unique invariant definition.  

References bitmap_bit_p(), find_invariants_bb(), and loop::num_nodes.

static int replace_uses ( )
   Replace the uses, reached by the definition of invariant INV, by REG.

   IN_GROUP is nonzero if this is part of a group of changes that must be
   performed as a group.  In that case, the changes will be stored.  The
   function `apply_change_group' will validate and apply the changes.  
     Replace the uses we know to be dominated.  It saves work for copy
     propagation, and also it is necessary so that dependent invariants
     are computed right.  
         If we aren't part of a larger group, apply the changes now.  

Referenced by find_invariants_to_move().

static void set_move_mark ( )
   Marks invariant INVNO and all its dependencies for moving.  
     Find the representative of the class of the equivalent invariants.  

Variable Documentation

unsigned actual_stamp
   The actual stamp for marking already visited invariants during determining
   costs of movements.  
struct loop* curr_loop
   Currently processed loop.  
int curr_reg_pressure[N_REG_CLASSES]
   Current reg pressure for each pressure class.  
bitmap_head curr_regs_live
   Registers currently living.  
struct invariant** invariant_table
unsigned int invariant_table_size = 0
   Table of invariants indexed by the df_ref uid field.  
vec<invariant_p> invariants
   The invariants.  
int n_regs_set
   Number of regs stored in the previous array.  
   Record all regs that are set in any one insn.  Communication from
   mark_reg_{store,clobber} and global_conflicts.  Asm can refer to
   all hard-registers.  

Referenced by dump_insn_info(), free_loop_data(), and get_stored_val().