LLVM  3.7.0
Macros | Functions | Variables
LoopStrengthReduce.cpp File Reference
#include "llvm/Transforms/Scalar.h"
#include "llvm/ADT/DenseSet.h"
#include "llvm/ADT/Hashing.h"
#include "llvm/ADT/STLExtras.h"
#include "llvm/ADT/SetVector.h"
#include "llvm/ADT/SmallBitVector.h"
#include "llvm/Analysis/IVUsers.h"
#include "llvm/Analysis/LoopPass.h"
#include "llvm/Analysis/ScalarEvolutionExpander.h"
#include "llvm/Analysis/TargetTransformInfo.h"
#include "llvm/IR/Constants.h"
#include "llvm/IR/DerivedTypes.h"
#include "llvm/IR/Dominators.h"
#include "llvm/IR/Instructions.h"
#include "llvm/IR/IntrinsicInst.h"
#include "llvm/IR/Module.h"
#include "llvm/IR/ValueHandle.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/Debug.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/Transforms/Utils/BasicBlockUtils.h"
#include "llvm/Transforms/Utils/Local.h"
#include <algorithm>
Include dependency graph for LoopStrengthReduce.cpp:

Go to the source code of this file.

Macros

#define DEBUG_TYPE   "loop-reduce"
 

Functions

static void DoInitialMatch (const SCEV *S, Loop *L, SmallVectorImpl< const SCEV * > &Good, SmallVectorImpl< const SCEV * > &Bad, ScalarEvolution &SE)
 DoInitialMatch - Recursion helper for InitialMatch. More...
 
static bool isAddRecSExtable (const SCEVAddRecExpr *AR, ScalarEvolution &SE)
 isAddRecSExtable - Return true if the given addrec can be sign-extended without changing its value. More...
 
static bool isAddSExtable (const SCEVAddExpr *A, ScalarEvolution &SE)
 isAddSExtable - Return true if the given add can be sign-extended without changing its value. More...
 
static bool isMulSExtable (const SCEVMulExpr *M, ScalarEvolution &SE)
 isMulSExtable - Return true if the given mul can be sign-extended without changing its value. More...
 
static const SCEVgetExactSDiv (const SCEV *LHS, const SCEV *RHS, ScalarEvolution &SE, bool IgnoreSignificantBits=false)
 getExactSDiv - Return an expression for LHS /s RHS, if it can be determined and if the remainder is known to be zero, or null otherwise. More...
 
static int64_t ExtractImmediate (const SCEV *&S, ScalarEvolution &SE)
 ExtractImmediate - If S involves the addition of a constant integer value, return that integer value, and mutate S to point to a new SCEV with that value excluded. More...
 
static GlobalValueExtractSymbol (const SCEV *&S, ScalarEvolution &SE)
 ExtractSymbol - If S involves the addition of a GlobalValue address, return that symbol, and mutate S to point to a new SCEV with that value excluded. More...
 
static bool isAddressUse (Instruction *Inst, Value *OperandVal)
 isAddressUse - Returns true if the specified instruction is using the specified value as an address. More...
 
static TypegetAccessType (const Instruction *Inst)
 getAccessType - Return the type of the memory being accessed. More...
 
static bool isExistingPhi (const SCEVAddRecExpr *AR, ScalarEvolution &SE)
 isExistingPhi - Return true if this AddRec is already a phi in its loop. More...
 
static bool isHighCostExpansion (const SCEV *S, SmallPtrSetImpl< const SCEV * > &Processed, ScalarEvolution &SE)
 Check if expanding this expression is likely to incur significant cost. More...
 
static bool DeleteTriviallyDeadInstructions (SmallVectorImpl< WeakVH > &DeadInsts)
 DeleteTriviallyDeadInstructions - If any of the instructions is the specified set are trivially dead, delete them and see if this makes any of their operands subsequently dead. More...
 
static bool isAMCompletelyFolded (const TargetTransformInfo &TTI, const LSRUse &LU, const Formula &F)
 Check if the addressing mode defined by F is completely folded in LU at isel time. More...
 
static unsigned getScalingFactorCost (const TargetTransformInfo &TTI, const LSRUse &LU, const Formula &F)
 
static bool isAMCompletelyFolded (const TargetTransformInfo &TTI, LSRUse::KindType Kind, Type *AccessTy, GlobalValue *BaseGV, int64_t BaseOffset, bool HasBaseReg, int64_t Scale)
 
static bool isAMCompletelyFolded (const TargetTransformInfo &TTI, int64_t MinOffset, int64_t MaxOffset, LSRUse::KindType Kind, Type *AccessTy, GlobalValue *BaseGV, int64_t BaseOffset, bool HasBaseReg, int64_t Scale)
 
static bool isAMCompletelyFolded (const TargetTransformInfo &TTI, int64_t MinOffset, int64_t MaxOffset, LSRUse::KindType Kind, Type *AccessTy, const Formula &F)
 
static bool isLegalUse (const TargetTransformInfo &TTI, int64_t MinOffset, int64_t MaxOffset, LSRUse::KindType Kind, Type *AccessTy, GlobalValue *BaseGV, int64_t BaseOffset, bool HasBaseReg, int64_t Scale)
 isLegalUse - Test whether we know how to expand the current formula. More...
 
static bool isLegalUse (const TargetTransformInfo &TTI, int64_t MinOffset, int64_t MaxOffset, LSRUse::KindType Kind, Type *AccessTy, const Formula &F)
 
static bool isAlwaysFoldable (const TargetTransformInfo &TTI, LSRUse::KindType Kind, Type *AccessTy, GlobalValue *BaseGV, int64_t BaseOffset, bool HasBaseReg)
 
static bool isAlwaysFoldable (const TargetTransformInfo &TTI, ScalarEvolution &SE, int64_t MinOffset, int64_t MaxOffset, LSRUse::KindType Kind, Type *AccessTy, const SCEV *S, bool HasBaseReg)
 
static User::op_iterator findIVOperand (User::op_iterator OI, User::op_iterator OE, Loop *L, ScalarEvolution &SE)
 findIVOperand - Helper for CollectChains that finds an IV operand (computed by an AddRec in this loop) within [OI,OE) or returns OE. More...
 
static ValuegetWideOperand (Value *Oper)
 getWideOperand - IVChain logic must consistenctly peek base TruncInst operands, so wrap it in a convenient helper. More...
 
static bool isCompatibleIVType (Value *LVal, Value *RVal)
 isCompatibleIVType - Return true if we allow an IV chain to include both types. More...
 
static const SCEVgetExprBase (const SCEV *S)
 getExprBase - Return an approximation of this SCEV expression's "base", or NULL for any constant. More...
 
static bool isProfitableChain (IVChain &Chain, SmallPtrSetImpl< Instruction * > &Users, ScalarEvolution &SE, const TargetTransformInfo &TTI)
 Return true if the number of registers needed for the chain is estimated to be less than the number required for the individual IV users. More...
 
static bool canFoldIVIncExpr (const SCEV *IncExpr, Instruction *UserInst, Value *Operand, const TargetTransformInfo &TTI)
 Return true if the IVInc can be folded into an addressing mode. More...
 
static const SCEVCollectSubexprs (const SCEV *S, const SCEVConstant *C, SmallVectorImpl< const SCEV * > &Ops, const Loop *L, ScalarEvolution &SE, unsigned Depth=0)
 CollectSubexprs - Split S into subexpressions which can be pulled out into separate registers. More...
 
 INITIALIZE_PASS_BEGIN (LoopStrengthReduce,"loop-reduce","Loop Strength Reduction", false, false) INITIALIZE_PASS_END(LoopStrengthReduce
 

Variables

static const unsigned MaxIVUsers = 200
 MaxIVUsers is an arbitrary threshold that provides an early opportunitiy for bail out. More...
 
static cl::opt< boolEnablePhiElim ("enable-lsr-phielim", cl::Hidden, cl::init(true), cl::desc("Enable LSR phi elimination"))
 
static cl::opt< boolStressIVChain ("stress-ivchain", cl::Hidden, cl::init(false), cl::desc("Stress test LSR IV chains"))
 
static const size_t ComplexityLimit = UINT16_MAX
 
loop reduce
 
loop Loop Strength Reduction
 
loop Loop Strength false
 

Macro Definition Documentation

#define DEBUG_TYPE   "loop-reduce"

Definition at line 81 of file LoopStrengthReduce.cpp.

Function Documentation

static bool canFoldIVIncExpr ( const SCEV IncExpr,
Instruction UserInst,
Value Operand,
const TargetTransformInfo TTI 
)
static
static const SCEV* CollectSubexprs ( const SCEV S,
const SCEVConstant C,
SmallVectorImpl< const SCEV * > &  Ops,
const Loop L,
ScalarEvolution SE,
unsigned  Depth = 0 
)
static

CollectSubexprs - Split S into subexpressions which can be pulled out into separate registers.

If C is non-null, multiply each subexpression by C.

Return remainder expression after factoring the subexpressions captured by Ops. If Ops is complete, return NULL.

Definition at line 3173 of file LoopStrengthReduce.cpp.

References llvm::CallingConv::C, llvm::SCEVAddRecExpr::getLoop(), llvm::ScalarEvolution::getMulExpr(), llvm::SCEVAddRecExpr::getStart(), llvm::SCEVAddRecExpr::getStepRecurrence(), llvm::SCEVNAryExpr::getType(), llvm::SCEV::isZero(), and llvm::SmallVectorTemplateBase< T, isPodLike< T >::value >::push_back().

static bool DeleteTriviallyDeadInstructions ( SmallVectorImpl< WeakVH > &  DeadInsts)
static

DeleteTriviallyDeadInstructions - If any of the instructions is the specified set are trivially dead, delete them and see if this makes any of their operands subsequently dead.

Definition at line 800 of file LoopStrengthReduce.cpp.

References llvm::SmallVectorImpl< T >::emplace_back(), llvm::SmallVectorBase::empty(), llvm::Instruction::eraseFromParent(), llvm::isInstructionTriviallyDead(), llvm::User::operands(), and llvm::SmallVectorImpl< T >::pop_back_val().

static void DoInitialMatch ( const SCEV S,
Loop L,
SmallVectorImpl< const SCEV * > &  Good,
SmallVectorImpl< const SCEV * > &  Bad,
ScalarEvolution SE 
)
static
static int64_t ExtractImmediate ( const SCEV *&  S,
ScalarEvolution SE 
)
static

ExtractImmediate - If S involves the addition of a constant integer value, return that integer value, and mutate S to point to a new SCEV with that value excluded.

Definition at line 609 of file LoopStrengthReduce.cpp.

References llvm::SCEV::FlagAnyWrap, llvm::ScalarEvolution::getAddExpr(), llvm::ScalarEvolution::getAddRecExpr(), and llvm::ScalarEvolution::getConstant().

Referenced by isAlwaysFoldable().

static GlobalValue* ExtractSymbol ( const SCEV *&  S,
ScalarEvolution SE 
)
static

ExtractSymbol - If S involves the addition of a GlobalValue address, return that symbol, and mutate S to point to a new SCEV with that value excluded.

Definition at line 636 of file LoopStrengthReduce.cpp.

References llvm::SCEV::FlagAnyWrap, llvm::ScalarEvolution::getAddExpr(), llvm::ScalarEvolution::getAddRecExpr(), and llvm::ScalarEvolution::getConstant().

Referenced by isAlwaysFoldable().

static User::op_iterator findIVOperand ( User::op_iterator  OI,
User::op_iterator  OE,
Loop L,
ScalarEvolution SE 
)
static

findIVOperand - Helper for CollectChains that finds an IV operand (computed by an AddRec in this loop) within [OI,OE) or returns OE.

If IVUsers mapped Instructions to IVStrideUses, we could partially skip this.

Definition at line 2422 of file LoopStrengthReduce.cpp.

References llvm::SCEVAddRecExpr::getLoop(), llvm::ScalarEvolution::getSCEV(), and llvm::ScalarEvolution::isSCEVable().

static Type* getAccessType ( const Instruction Inst)
static

getAccessType - Return the type of the memory being accessed.

Definition at line 686 of file LoopStrengthReduce.cpp.

References llvm::IntegerType::get(), llvm::PointerType::get(), llvm::Value::getType(), and SI.

Referenced by canFoldIVIncExpr().

static const SCEV* getExactSDiv ( const SCEV LHS,
const SCEV RHS,
ScalarEvolution SE,
bool  IgnoreSignificantBits = false 
)
static

getExactSDiv - Return an expression for LHS /s RHS, if it can be determined and if the remainder is known to be zero, or null otherwise.

If IgnoreSignificantBits is true, expressions like (X * Y) /s Y are simplified to Y, ignoring that the multiplication may overflow, which is useful when the result will be used in a context where the most significant bits are ignored.

Definition at line 521 of file LoopStrengthReduce.cpp.

References llvm::dyn_cast(), llvm::SCEV::FlagAnyWrap, Found(), llvm::ScalarEvolution::getAddExpr(), llvm::ScalarEvolution::getConstant(), llvm::ScalarEvolution::getMulExpr(), llvm::SCEV::getType(), llvm::SCEVConstant::getValue(), llvm::ConstantInt::getValue(), isAddRecSExtable(), isAddSExtable(), llvm::APInt::isAllOnesValue(), isMulSExtable(), llvm::SmallVectorTemplateBase< T, isPodLike< T >::value >::push_back(), llvm::APInt::sdiv(), and llvm::APInt::srem().

static const SCEV* getExprBase ( const SCEV S)
static

getExprBase - Return an approximation of this SCEV expression's "base", or NULL for any constant.

Returning the expression itself is conservative. Returning a deeper subexpression is more precise and valid as long as it isn't less complex than another subexpression. For expressions involving multiple unscaled values, we need to return the pointer-type SCEVUnknown. This avoids forming chains across objects, such as: PrevOper==a[i], IVOper==b[i], IVInc==b-a.

Since SCEVUnknown is the rightmost type, and pointers are the rightmost SCEVUnknown, we simply return the rightmost SCEV operand.

Definition at line 2465 of file LoopStrengthReduce.cpp.

References llvm::SCEV::getSCEVType(), I, llvm::SCEVNAryExpr::op_begin(), llvm::SCEVNAryExpr::op_end(), llvm::scAddExpr, llvm::scAddRecExpr, llvm::scConstant, llvm::scMulExpr, llvm::scSignExtend, llvm::scTruncate, and llvm::scZeroExtend.

static unsigned getScalingFactorCost ( const TargetTransformInfo TTI,
const LSRUse &  LU,
const Formula &  F 
)
static
static Value* getWideOperand ( Value Oper)
static

getWideOperand - IVChain logic must consistenctly peek base TruncInst operands, so wrap it in a convenient helper.

Definition at line 2441 of file LoopStrengthReduce.cpp.

INITIALIZE_PASS_BEGIN ( LoopStrengthReduce  ,
"loop-reduce ,
"Loop Strength Reduction ,
false  ,
false   
)
static bool isAddRecSExtable ( const SCEVAddRecExpr AR,
ScalarEvolution SE 
)
static

isAddRecSExtable - Return true if the given addrec can be sign-extended without changing its value.

Definition at line 492 of file LoopStrengthReduce.cpp.

References llvm::IntegerType::get(), llvm::ScalarEvolution::getContext(), llvm::ScalarEvolution::getSignExtendExpr(), llvm::SCEVNAryExpr::getType(), and llvm::ScalarEvolution::getTypeSizeInBits().

Referenced by getExactSDiv().

static bool isAddressUse ( Instruction Inst,
Value OperandVal 
)
static

isAddressUse - Returns true if the specified instruction is using the specified value as an address.

Definition at line 662 of file LoopStrengthReduce.cpp.

References prefetch, and SI.

Referenced by canFoldIVIncExpr().

static bool isAddSExtable ( const SCEVAddExpr A,
ScalarEvolution SE 
)
static

isAddSExtable - Return true if the given add can be sign-extended without changing its value.

Definition at line 500 of file LoopStrengthReduce.cpp.

References llvm::IntegerType::get(), llvm::ScalarEvolution::getContext(), llvm::ScalarEvolution::getSignExtendExpr(), llvm::SCEVAddExpr::getType(), and llvm::ScalarEvolution::getTypeSizeInBits().

Referenced by getExactSDiv().

static bool isAlwaysFoldable ( const TargetTransformInfo TTI,
LSRUse::KindType  Kind,
Type AccessTy,
GlobalValue BaseGV,
int64_t  BaseOffset,
bool  HasBaseReg 
)
static

Definition at line 1517 of file LoopStrengthReduce.cpp.

References isAMCompletelyFolded().

Referenced by canFoldIVIncExpr().

static bool isAlwaysFoldable ( const TargetTransformInfo TTI,
ScalarEvolution SE,
int64_t  MinOffset,
int64_t  MaxOffset,
LSRUse::KindType  Kind,
Type AccessTy,
const SCEV S,
bool  HasBaseReg 
)
static
static bool isAMCompletelyFolded ( const TargetTransformInfo TTI,
const LSRUse &  LU,
const Formula &  F 
)
static

Check if the addressing mode defined by F is completely folded in LU at isel time.

This includes address-mode folding and special icmp tricks. This function returns true if LU can accommodate what F defines and up to 1 base + 1 scaled + offset. In other words, if F has several base registers, this function may still return true. Therefore, users still need to account for additional base registers and/or unfolded offsets to derive an accurate cost model.

Definition at line 1472 of file LoopStrengthReduce.cpp.

Referenced by getScalingFactorCost(), isAlwaysFoldable(), isAMCompletelyFolded(), and isLegalUse().

static bool isAMCompletelyFolded ( const TargetTransformInfo TTI,
LSRUse::KindType  Kind,
Type AccessTy,
GlobalValue BaseGV,
int64_t  BaseOffset,
bool  HasBaseReg,
int64_t  Scale 
)
static
static bool isAMCompletelyFolded ( const TargetTransformInfo TTI,
int64_t  MinOffset,
int64_t  MaxOffset,
LSRUse::KindType  Kind,
Type AccessTy,
GlobalValue BaseGV,
int64_t  BaseOffset,
bool  HasBaseReg,
int64_t  Scale 
)
static

Definition at line 1413 of file LoopStrengthReduce.cpp.

References isAMCompletelyFolded().

static bool isAMCompletelyFolded ( const TargetTransformInfo TTI,
int64_t  MinOffset,
int64_t  MaxOffset,
LSRUse::KindType  Kind,
Type AccessTy,
const Formula &  F 
)
static

Definition at line 1434 of file LoopStrengthReduce.cpp.

References isAMCompletelyFolded().

static bool isCompatibleIVType ( Value LVal,
Value RVal 
)
static

isCompatibleIVType - Return true if we allow an IV chain to include both types.

Definition at line 2449 of file LoopStrengthReduce.cpp.

References llvm::Value::getType(), and llvm::Type::isPointerTy().

static bool isExistingPhi ( const SCEVAddRecExpr AR,
ScalarEvolution SE 
)
static
static bool isHighCostExpansion ( const SCEV S,
SmallPtrSetImpl< const SCEV * > &  Processed,
ScalarEvolution SE 
)
static

Check if expanding this expression is likely to incur significant cost.

This is tricky because SCEV doesn't track which expressions are actually computed by the current IR.

We currently allow expansion of IV increments that involve adds, multiplication by constants, and AddRecs from existing phis.

TODO: Allow UDivExpr if we can find an existing IV increment that is an obvious multiple of the UDivExpr.

Definition at line 735 of file LoopStrengthReduce.cpp.

References llvm::dyn_cast(), llvm::Instruction::getOpcode(), llvm::ScalarEvolution::getSCEV(), llvm::SCEV::getSCEVType(), llvm::Value::getType(), llvm::SmallPtrSetImpl< PtrType >::insert(), isExistingPhi(), llvm::ScalarEvolution::isSCEVable(), llvm::scConstant, llvm::scSignExtend, llvm::scTruncate, llvm::scUnknown, llvm::scZeroExtend, and llvm::Value::users().

static bool isLegalUse ( const TargetTransformInfo TTI,
int64_t  MinOffset,
int64_t  MaxOffset,
LSRUse::KindType  Kind,
Type AccessTy,
GlobalValue BaseGV,
int64_t  BaseOffset,
bool  HasBaseReg,
int64_t  Scale 
)
static

isLegalUse - Test whether we know how to expand the current formula.

Definition at line 1451 of file LoopStrengthReduce.cpp.

References isAMCompletelyFolded().

Referenced by isLegalUse().

static bool isLegalUse ( const TargetTransformInfo TTI,
int64_t  MinOffset,
int64_t  MaxOffset,
LSRUse::KindType  Kind,
Type AccessTy,
const Formula &  F 
)
static

Definition at line 1465 of file LoopStrengthReduce.cpp.

References isLegalUse().

static bool isMulSExtable ( const SCEVMulExpr M,
ScalarEvolution SE 
)
static

isMulSExtable - Return true if the given mul can be sign-extended without changing its value.

Definition at line 508 of file LoopStrengthReduce.cpp.

References llvm::IntegerType::get(), llvm::ScalarEvolution::getContext(), llvm::SCEVNAryExpr::getNumOperands(), llvm::ScalarEvolution::getSignExtendExpr(), llvm::SCEVNAryExpr::getType(), and llvm::ScalarEvolution::getTypeSizeInBits().

Referenced by getExactSDiv().

static bool isProfitableChain ( IVChain &  Chain,
SmallPtrSetImpl< Instruction * > &  Users,
ScalarEvolution SE,
const TargetTransformInfo TTI 
)
static

Return true if the number of registers needed for the chain is estimated to be less than the number required for the individual IV users.

First prohibit any IV users that keep the IV live across increments (the Users set should be empty). Next count the number and type of increments in the chain.

Chaining IVs can lead to considerable code bloat if ISEL doesn't effectively use postinc addressing modes. Only consider it profitable it the increments can be computed in fewer registers when chained.

TODO: Consider IVInc free if it's already used in another chains.

Definition at line 2533 of file LoopStrengthReduce.cpp.

References cost, llvm::dbgs(), DEBUG, llvm::SmallPtrSetImplBase::empty(), for(), llvm::ScalarEvolution::getSCEV(), and StressIVChain.

Variable Documentation

const size_t ComplexityLimit = UINT16_MAX
static

Definition at line 3960 of file LoopStrengthReduce.cpp.

cl::opt<bool> EnablePhiElim("enable-lsr-phielim", cl::Hidden, cl::init(true), cl::desc("Enable LSR phi elimination"))
static
loop Loop Strength false

Definition at line 4963 of file LoopStrengthReduce.cpp.

const unsigned MaxIVUsers = 200
static

MaxIVUsers is an arbitrary threshold that provides an early opportunitiy for bail out.

This threshold is far beyond the number of users that LSR can conceivably solve, so it should not affect generated code, but catches the worst cases before LSR burns too much compile time and stack space.

Definition at line 87 of file LoopStrengthReduce.cpp.

loop reduce

Definition at line 4963 of file LoopStrengthReduce.cpp.

loop Loop Strength Reduction

Definition at line 4963 of file LoopStrengthReduce.cpp.

cl::opt<bool> StressIVChain("stress-ivchain", cl::Hidden, cl::init(false), cl::desc("Stress test LSR IV chains"))
static

Referenced by isProfitableChain().