GCC Middle and Back End API Reference
loop-invariant.c File Reference
#include "config.h"
#include "system.h"
#include "coretypes.h"
#include "tm.h"
#include "hard-reg-set.h"
#include "rtl.h"
#include "tm_p.h"
#include "obstack.h"
#include "basic-block.h"
#include "cfgloop.h"
#include "expr.h"
#include "recog.h"
#include "target.h"
#include "function.h"
#include "flags.h"
#include "df.h"
#include "hash-table.h"
#include "except.h"
#include "params.h"
#include "regs.h"
#include "ira.h"
#include "dumpfile.h"
Include dependency graph for loop-invariant.c:

Data Structures

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

Macros

#define LOOP_DATA(LOOP)   ((struct loop_data *) (LOOP)->aux)

Typedefs

typedef struct invariantinvariant_p
typedef hash_table
< invariant_expr_hasher
invariant_htab_type

Functions

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 ()

Variables

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 rtx regs_set [(FIRST_PSEUDO_REGISTER > MAX_RECOG_OPERANDS?FIRST_PSEUDO_REGISTER:MAX_RECOG_OPERANDS)*2]
static int n_regs_set

Macro Definition Documentation

#define LOOP_DATA (   LOOP)    ((struct loop_data *) (LOOP)->aux)

Referenced by mark_ref_regs().


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 
)
static

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 motion.

Only consider the "representatives" of equivalent invariants.

References df, DF_REG_SIZE, DF_REGNO_FIRST_DEF, and DF_REGNO_LAST_USE.

static void calculate_loop_reg_pressure ( )
static

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 ( )
static

Increase (if INCR_P) or decrease current register pressure for register REGNO.

static bool check_dependencies ( )
static

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 ( )
static

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 sequentially.

References BLOCK_FOR_INSN, DF_INSN_INFO_EQ_USES, DF_INSN_INFO_GET, and DF_INSN_INFO_USES.

static void check_invariant_table_size ( )
static

Check the size of the invariant table and realloc if necessary.

static bool check_maybe_invariant ( )
static

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 
)
static

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 get_loop_body_in_dom_order.

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 
)
staticread

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
     invariants).
     See http://gcc.gnu.org/ml/gcc-patches/2009-10/msg01210.html .   
static void find_defs ( )
static

Finds definitions that may correspond to invariants in LOOP with body BODY.

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

static void find_exits ( struct loop loop,
basic_block body,
bitmap  may_exit,
bitmap  has_exit 
)
static

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 ( )
static

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 ( )
static

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.

If the insn can throw exception, we cannot move it at all without changing cfg.

 We cannot make trapping insn executed, unless it was executed before.   

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

static void find_invariants ( )
static

Finds invariants in LOOP.

static void find_invariants_bb ( )
static

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 call.

References use::next.

Referenced by record_uses().

static void find_invariants_body ( struct loop loop,
basic_block body,
bitmap  always_reached,
bitmap  always_executed 
)
static

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 ( )
static

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 ( )
static

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(), BB_END, invariant::depends_on, emit_insn_after(), invariant::eqto, EXECUTE_IF_SET_IN_BITMAP, gen_move_insn(), gen_reg_rtx_and_attrs(), GET_CODE, invariant::insn, invariant::invno, loop_preheader_edge(), invariant::move, move_invariant_reg(), invariant::reg, REG_P, REGNO, reorder_insns(), replace_uses(), SET_DEST, single_set, edge_def::src, SUBREG_REG, and validate_change().

static struct invariant* find_or_insert_inv ( invariant_htab_type  eq,
rtx  expr,
enum machine_mode  mode,
struct invariant inv 
)
staticread

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 ( )
static

Frees the data allocated by invariant motion.

static void free_loop_data ( )
static

Releases the auxiliary data for LOOP.

References GET_CODE, REG_P, regs_set, and SUBREG_REG.

static void free_use_list ( )
static

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 
)
static

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 result: 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 out-of-order. 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, ira_pressure_classes, ira_pressure_classes_num, and invariant::move.

static void get_inv_cost ( )
static

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.   
         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, ira_pressure_classes, ira_pressure_classes_num, use::next, and def::uses.

static enum reg_class get_pressure_class_and_nregs ( )
static

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 ( )
static

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 ( )
static

Computes hash value for invariant expression X in INSN.

static void init_inv_motion_data ( )
static

Initializes invariant motion data.

static bool invariant_expr_equal_p ( )
static

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 ( )
staticread

Returns the invariant definition for USE, or NULL if USE is not invariant.

References CASE_CONST_ANY, df_find_use(), invariant::eqto, gcc_assert, GET_MODE, hash_rtx(), and NULL.

Referenced by find_invariant_insn().

static void mark_ref_regs ( )
static

Mark occurrence of registers in X for the current loop.

References bitmap_bit_p, EXECUTE_IF_SET_IN_BITMAP, get_regno_pressure_class(), and LOOP_DATA.

static void mark_reg_clobber ( )
static

Mark clobbering register REG.

static void mark_reg_death ( )
static

Mark register REG death.

static void mark_reg_store ( rtx  reg,
const_rtx  setter,
void *  data 
)
static

Mark setting register REG.

static void mark_regno_death ( )
static

Mark REGNO death.

References loop::aux.

static void mark_regno_live ( )
static

Mark REGNO birth.

static bool may_assign_reg_p ( )
static

Check whether we may assign a value to X from a register.

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

static void merge_identical_invariants ( )
static

Find invariants with the same value and record the equivalences.

References bitmap_set_bit, CALL_P, FOR_BB_INSNS, RTL_CONST_OR_PURE_CALL_P, and RTL_LOOPING_CONST_OR_PURE_CALL_P.

static bool move_invariant_reg ( )
static

Move invariant INVNO out of the LOOP. Returns true if this succeeds, false otherwise.

 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
 again.   

Referenced by find_invariants_to_move().

static void move_invariants ( )
static

Move selected invariant out of the LOOP. Newly created regs are marked in TEMPORARY_REGS.

References ira_pressure_class_translate, and 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 ( )
static

Move the invariants out of the LOOP.

static void record_use ( )
static

Record USE at DEF.

Referenced by find_invariant_insn().

static void record_uses ( )
static

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 ( )
static

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 ( )
static

Marks invariant INVNO and all its dependencies for moving.

Find the representative of the class of the equivalent invariants.


Variable Documentation

unsigned actual_stamp
static

The actual stamp for marking already visited invariants during determining costs of movements.

struct loop* curr_loop
static

Currently processed loop.

int curr_reg_pressure[N_REG_CLASSES]
static

Current reg pressure for each pressure class.

bitmap_head curr_regs_live
static

Registers currently living.

struct invariant** invariant_table
static
unsigned int invariant_table_size = 0
static

Table of invariants indexed by the df_ref uid field.

vec<invariant_p> invariants
static

The invariants.

int n_regs_set
static

Number of regs stored in the previous array.

rtx regs_set[(FIRST_PSEUDO_REGISTER > MAX_RECOG_OPERANDS?FIRST_PSEUDO_REGISTER:MAX_RECOG_OPERANDS)*2]
static

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().