21 #ifndef LLVM_ANALYSIS_SCALAREVOLUTION_H
22 #define LLVM_ANALYSIS_SCALAREVOLUTION_H
33 #include "llvm/Support/DataTypes.h"
38 class AssumptionCache;
43 class ScalarEvolution;
45 class TargetLibraryInfo;
52 template<>
struct FoldingSetTrait<SCEV>;
66 const unsigned short SCEVType;
75 void operator=(
const SCEV &) =
delete;
133 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
148 return ID == X.FastID;
214 void deleted()
override;
215 void allUsesReplacedWith(
Value *New)
override;
261 bool WalkingBEDominatingConds;
271 ExitLimit(
const SCEV *E) : Exact(E),
Max(E) {}
273 ExitLimit(
const SCEV *E,
const SCEV *M) : Exact(E),
Max(M) {}
277 bool hasAnyInfo()
const {
278 return !isa<SCEVCouldNotCompute>(Exact) ||
279 !isa<SCEVCouldNotCompute>(
Max);
285 struct ExitNotTakenInfo {
286 AssertingVH<BasicBlock> ExitingBlock;
287 const SCEV *ExactNotTaken;
288 PointerIntPair<ExitNotTakenInfo*, 1> NextExit;
290 ExitNotTakenInfo() : ExitingBlock(nullptr), ExactNotTaken(nullptr) {}
293 bool isCompleteList()
const {
294 return NextExit.getInt() == 0;
297 void setIncomplete() { NextExit.setInt(1); }
300 ExitNotTakenInfo *getNextExit()
const {
301 return NextExit.getPointer();
304 void setNextExit(ExitNotTakenInfo *ENT) { NextExit.setPointer(ENT); }
310 class BackedgeTakenInfo {
313 ExitNotTakenInfo ExitNotTaken;
320 BackedgeTakenInfo() :
Max(nullptr) {}
324 SmallVectorImpl< std::pair<BasicBlock *, const SCEV *> > &ExitCounts,
325 bool Complete,
const SCEV *MaxCount);
330 bool hasAnyInfo()
const {
331 return ExitNotTaken.ExitingBlock || !isa<SCEVCouldNotCompute>(
Max);
359 DenseMap<const Loop*, BackedgeTakenInfo> BackedgeTakenCounts;
366 DenseMap<PHINode*, Constant*> ConstantEvolutionLoopExitValue;
371 DenseMap<
const SCEV *,
372 SmallVector<std::pair<const Loop *, const SCEV *>, 2> > ValuesAtScopes;
375 DenseMap<
const SCEV *,
376 SmallVector<PointerIntPair<const Loop *, 2, LoopDisposition>, 2>>
385 SmallVector<PointerIntPair<const BasicBlock *, 2, BlockDisposition>, 2>>
392 DenseMap<const SCEV *, ConstantRange> UnsignedRanges;
395 DenseMap<const SCEV *, ConstantRange> SignedRanges;
398 enum RangeSignHint { HINT_RANGE_UNSIGNED, HINT_RANGE_SIGNED };
401 const ConstantRange &setRange(
const SCEV *S, RangeSignHint
Hint,
402 const ConstantRange &CR) {
403 DenseMap<const SCEV *, ConstantRange> &Cache =
404 Hint == HINT_RANGE_UNSIGNED ? UnsignedRanges : SignedRanges;
406 std::pair<DenseMap<const SCEV *, ConstantRange>::iterator,
bool> Pair =
407 Cache.insert(std::make_pair(S, CR));
409 Pair.first->second = CR;
410 return Pair.first->second;
414 ConstantRange getRange(
const SCEV *S, RangeSignHint Hint);
418 const SCEV *createSCEV(Value *V);
422 const SCEV *createNodeForPHI(PHINode *PN);
426 const SCEV *createNodeForGEP(GEPOperator *
GEP);
431 const SCEV *computeSCEVAtScope(
const SCEV *S,
const Loop *L);
437 void ForgetSymbolicName(Instruction *
I,
const SCEV *SymName);
442 const BackedgeTakenInfo &getBackedgeTakenInfo(
const Loop *L);
446 BackedgeTakenInfo ComputeBackedgeTakenCount(
const Loop *L);
450 ExitLimit ComputeExitLimit(
const Loop *L,
BasicBlock *ExitingBlock);
455 ExitLimit ComputeExitLimitFromCond(
const Loop *L,
464 ExitLimit ComputeExitLimitFromICmp(
const Loop *L,
474 ComputeExitLimitFromSingleExitSwitch(
const Loop *L, SwitchInst *Switch,
480 ExitLimit ComputeLoadConstantCompareExitLimit(LoadInst *LI,
490 const SCEV *ComputeExitCountExhaustively(
const Loop *L,
497 ExitLimit HowFarToZero(
const SCEV *V,
const Loop *L,
bool IsSubExpr);
502 ExitLimit HowFarToNonZero(
const SCEV *V,
const Loop *L);
508 ExitLimit HowManyLessThans(
const SCEV *LHS,
const SCEV *RHS,
509 const Loop *L,
bool isSigned,
bool IsSubExpr);
510 ExitLimit HowManyGreaterThans(
const SCEV *LHS,
const SCEV *RHS,
511 const Loop *L,
bool isSigned,
bool IsSubExpr);
517 std::pair<BasicBlock *, BasicBlock *>
518 getPredecessorWithUniqueSuccessorForBB(
BasicBlock *BB);
523 const SCEV *LHS,
const SCEV *RHS,
524 Value *FoundCondValue,
531 const SCEV *LHS,
const SCEV *RHS,
532 const SCEV *FoundLHS,
const SCEV *FoundRHS);
538 const SCEV *LHS,
const SCEV *RHS,
539 const SCEV *FoundLHS,
540 const SCEV *FoundRHS);
547 const SCEV *LHS,
const SCEV *RHS,
548 const SCEV *FoundLHS,
549 const SCEV *FoundRHS);
555 Constant *getConstantEvolutionLoopExitValue(PHINode *PN,
const APInt& BEs,
563 const SCEV *LHS,
const SCEV *RHS);
566 void forgetMemoizedResults(
const SCEV *S);
570 bool checkValidity(
const SCEV *S)
const;
577 template<
typename ExtendOpTy>
578 bool proveNoWrapByVaryingStart(
const SCEV *Start,
const SCEV *Step,
668 bool InBounds =
false);
864 return getRange(S, HINT_RANGE_UNSIGNED);
870 return getRange(S, HINT_RANGE_SIGNED);
949 const SCEV *ElementSize)
const;
1035 const SCEV *ElementSize);
1040 const SCEV *computeBECount(
const SCEV *Delta,
const SCEV *Stride,
1046 bool doesIVOverflowOnLT(
const SCEV *RHS,
const SCEV *Stride,
1047 bool IsSigned,
bool NoWrap);
1052 bool doesIVOverflowOnGT(
const SCEV *RHS,
const SCEV *Stride,
1053 bool IsSigned,
bool NoWrap);
const SCEV * getTruncateOrNoop(const SCEV *V, Type *Ty)
getTruncateOrNoop - Return a SCEV corresponding to a conversion of the input value to the specified t...
void push_back(const T &Elt)
The SCEV properly dominates the block.
LLVMContext & getContext() const
getContext - Return a reference to the LLVMContext associated with this function. ...
#define LLVM_ATTRIBUTE_UNUSED_RESULT
bool isOne() const
isOne - Return true if the expression is a constant one.
const SCEV * getExitCount(Loop *L, BasicBlock *ExitingBlock)
const SCEV * getConstant(ConstantInt *V)
A Module instance is used to store all the information related to an LLVM module. ...
LLVMContext & getContext() const
bool isZero() const
isZero - Return true if the expression is a constant zero.
const SCEV * getMulExpr(const SCEV *Op0, const SCEV *Op1, const SCEV *Op2, SCEV::NoWrapFlags Flags=SCEV::FlagAnyWrap)
const SCEV * getPointerBase(const SCEV *V)
getPointerBase - Transitively follow the chain of pointer-type operands until reaching a SCEV that do...
ScalarEvolution - This class is the main scalar evolution driver.
bool isKnownNonNegative(const SCEV *S)
isKnownNonNegative - Test if the given expression is known to be non-negative.
const SCEV * getMulExpr(const SCEV *LHS, const SCEV *RHS, SCEV::NoWrapFlags Flags=SCEV::FlagAnyWrap)
const SCEV * getAddExpr(const SCEV *Op0, const SCEV *Op1, const SCEV *Op2, SCEV::NoWrapFlags Flags=SCEV::FlagAnyWrap)
A cache of .assume calls within a function.
bool properlyDominates(const SCEV *S, const BasicBlock *BB)
properlyDominates - Return true if elements that makes up the given SCEV properly dominate the specif...
uint32_t GetMinTrailingZeros(const SCEV *S)
GetMinTrailingZeros - Determine the minimum number of zero bits that S is guaranteed to end in (at ev...
bool isLoopInvariant(const SCEV *S, const Loop *L)
isLoopInvariant - Return true if the value of the given SCEV is unchanging in the specified loop...
bool isLoopBackedgeGuardedByCond(const Loop *L, ICmpInst::Predicate Pred, const SCEV *LHS, const SCEV *RHS)
isLoopBackedgeGuardedByCond - Test whether the backedge of the loop is protected by a conditional bet...
SCEVCouldNotCompute - An object of this class is returned by queries that could not be answered...
const SCEV * getUMaxFromMismatchedTypes(const SCEV *LHS, const SCEV *RHS)
getUMaxFromMismatchedTypes - Promote the operands to the wider of the types using zero-extension...
aarch64 collect AArch64 Collect Linker Optimization Hint(LOH)"
This file defines the MallocAllocator and BumpPtrAllocator interfaces.
bool isKnownNonPositive(const SCEV *S)
isKnownNonPositive - Test if the given expression is known to be non-positive.
The SCEV is loop-invariant.
const SCEV * getAddExpr(const SCEV *LHS, const SCEV *RHS, SCEV::NoWrapFlags Flags=SCEV::FlagAnyWrap)
const SCEV * getGEPExpr(Type *PointeeType, const SCEV *BaseExpr, const SmallVectorImpl< const SCEV * > &IndexExprs, bool InBounds=false)
Returns an expression for a GEP.
void computeAccessFunctions(const SCEV *Expr, SmallVectorImpl< const SCEV * > &Subscripts, SmallVectorImpl< const SCEV * > &Sizes)
Return in Subscripts the access functions for each dimension in Sizes.
unsigned ComputeHash() const
ComputeHash - Compute a strong hash value for this FoldingSetNodeIDRef, used to lookup the node in th...
StructType - Class to represent struct types.
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
unsigned short SubclassData
SubclassData - This field is initialized to zero and may be used in subclasses to store miscellaneous...
uint64_t getTypeSizeInBits(Type *Ty) const
getTypeSizeInBits - Return the size in bits of the specified type, for which isSCEVable must return t...
void print(raw_ostream &OS, const Module *=nullptr) const override
print - Print out the internal state of the pass.
static SCEV::NoWrapFlags LLVM_ATTRIBUTE_UNUSED_RESULT clearFlags(SCEV::NoWrapFlags Flags, SCEV::NoWrapFlags OffFlags)
bool isKnownPredicate(ICmpInst::Predicate Pred, const SCEV *LHS, const SCEV *RHS)
isKnownPredicate - Test if the given expression is known to satisfy the condition described by Pred...
const SCEV * getOffsetOfExpr(Type *IntTy, StructType *STy, unsigned FieldNo)
getOffsetOfExpr - Return an expression for offsetof on the given field with type IntTy ...
ConstantRange getSignedRange(const SCEV *S)
getSignedRange - Determine the signed range for a particular SCEV.
const SCEV * getSizeOfExpr(Type *IntTy, Type *AllocTy)
getSizeOfExpr - Return an expression for sizeof AllocTy that is type IntTy
Type * getEffectiveSCEVType(Type *Ty) const
getEffectiveSCEVType - Return a type with the same bitwidth as the given type and which represents ho...
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree...
const SCEV * getAddRecExpr(const SCEV *Start, const SCEV *Step, const Loop *L, SCEV::NoWrapFlags Flags)
getAddRecExpr - Get an add recurrence expression for the specified loop.
const SCEV * getAddRecExpr(const SmallVectorImpl< const SCEV * > &Operands, const Loop *L, SCEV::NoWrapFlags Flags)
void forgetLoopDispositions(const Loop *L)
Called when the client has changed the disposition of values in this loop.
bool isLoopEntryGuardedByCond(const Loop *L, ICmpInst::Predicate Pred, const SCEV *LHS, const SCEV *RHS)
isLoopEntryGuardedByCond - Test whether entry to the loop is protected by a conditional between LHS a...
FoldingSetNodeID - This class is used to gather all the unique data bits of a node.
const SCEV * getCouldNotCompute()
SCEVUnknown - This means that we are dealing with an entirely unknown SCEV value, and only represent ...
FoldingSetTrait - This trait class is used to define behavior of how to "profile" (in the FoldingSet ...
bool isSCEVable(Type *Ty) const
isSCEVable - Test if values of the given type are analyzable within the SCEV framework.
static bool Equals(const SCEV &X, const FoldingSetNodeID &ID, unsigned IDHash, FoldingSetNodeID &TempID)
LLVM Basic Block Representation.
The instances of the Type class are immutable: once they are created, they are never changed...
This is an important class for using LLVM in a threaded context.
Type * getType() const
getType - Return the LLVM type of this SCEV expression.
Allocate memory in an ever growing pool, as if by bump-pointer.
LoopDisposition getLoopDisposition(const SCEV *S, const Loop *L)
getLoopDisposition - Return the "disposition" of the given SCEV with respect to the given loop...
void dump() const
dump - This method is used for debugging.
const SCEV * getSMaxExpr(const SCEV *LHS, const SCEV *RHS)
const SCEV * getSCEVAtScope(const SCEV *S, const Loop *L)
getSCEVAtScope - Return a SCEV expression for the specified value at the specified scope in the progr...
Represent the analysis usage information of a pass.
bool hasOperand(const SCEV *S, const SCEV *Op) const
hasOperand - Test whether the given SCEV has Op as a direct or indirect operand.
static GCMetadataPrinterRegistry::Add< ErlangGCPrinter > X("erlang","erlang-compatible garbage collector")
const SCEV * getMinusSCEV(const SCEV *LHS, const SCEV *RHS, SCEV::NoWrapFlags Flags=SCEV::FlagAnyWrap)
getMinusSCEV - Return LHS-RHS. Minus is represented in SCEV as A+B*-1.
const SCEV * getMaxBackedgeTakenCount(const Loop *L)
getMaxBackedgeTakenCount - Similar to getBackedgeTakenCount, except return the least SCEV value that ...
Predicate
This enumeration lists the possible predicates for CmpInst subclasses.
FunctionPass class - This class is used to implement most global optimizations.
unsigned getSmallConstantTripCount(Loop *L)
Returns the maximum trip count of the loop if it is a single-exit loop and we can compute a small max...
bool isKnownNegative(const SCEV *S)
isKnownNegative - Test if the given expression is known to be negative.
void forgetValue(Value *V)
forgetValue - This method should be called by the client when it has changed a value in a way that ma...
static SCEV::NoWrapFlags LLVM_ATTRIBUTE_UNUSED_RESULT setFlags(SCEV::NoWrapFlags Flags, SCEV::NoWrapFlags OnFlags)
void findArrayDimensions(SmallVectorImpl< const SCEV * > &Terms, SmallVectorImpl< const SCEV * > &Sizes, const SCEV *ElementSize) const
Compute the array dimensions Sizes from the set of Terms extracted from the memory access function of...
const SCEV * getUMinFromMismatchedTypes(const SCEV *LHS, const SCEV *RHS)
getUMinFromMismatchedTypes - Promote the operands to the wider of the types using zero-extension...
bool isNonConstantNegative() const
isNonConstantNegative - Return true if the specified scev is negated, but not a constant.
const SCEV * getNoopOrZeroExtend(const SCEV *V, Type *Ty)
getNoopOrZeroExtend - Return a SCEV corresponding to a conversion of the input value to the specified...
FoldingSet - This template class is used to instantiate a specialized implementation of the folding s...
static unsigned ComputeHash(const SCEV &X, FoldingSetNodeID &TempID)
const SCEV * getUDivExactExpr(const SCEV *LHS, const SCEV *RHS)
getUDivExactExpr - Get a canonical unsigned division expression, or something simpler if possible...
The SCEV is loop-variant (unknown).
const SCEV * getSMinExpr(const SCEV *LHS, const SCEV *RHS)
This is the shared class of boolean and integer constants.
bool isKnownPositive(const SCEV *S)
isKnownPositive - Test if the given expression is known to be positive.
const SCEV * getTruncateExpr(const SCEV *Op, Type *Ty)
const SCEV * getTruncateOrSignExtend(const SCEV *V, Type *Ty)
getTruncateOrSignExtend - Return a SCEV corresponding to a conversion of the input value to the speci...
void releaseMemory() override
releaseMemory() - This member can be implemented by a pass if it wants to be able to release its memo...
void delinearize(const SCEV *Expr, SmallVectorImpl< const SCEV * > &Subscripts, SmallVectorImpl< const SCEV * > &Sizes, const SCEV *ElementSize)
Split this SCEVAddRecExpr into two vectors of SCEVs representing the subscripts and sizes of an array...
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small...
bool isAllOnesValue() const
isAllOnesValue - Return true if the expression is a constant all-ones value.
Provides information about what library functions are available for the current target.
The SCEV dominates the block.
unsigned getSmallConstantTripMultiple(Loop *L)
Returns the largest constant divisor of the trip count of the loop if it is a single-exit loop and we...
This class represents a range of values.
const SCEV * getNoopOrSignExtend(const SCEV *V, Type *Ty)
getNoopOrSignExtend - Return a SCEV corresponding to a conversion of the input value to the specified...
static void Profile(const SCEV &X, FoldingSetNodeID &ID)
const SCEV * getUMaxExpr(const SCEV *LHS, const SCEV *RHS)
static SCEV::NoWrapFlags LLVM_ATTRIBUTE_UNUSED_RESULT maskFlags(SCEV::NoWrapFlags Flags, int Mask)
Convenient NoWrapFlags manipulation that hides enum casts and is visible in the ScalarEvolution name ...
Node - This class is used to maintain the singly linked bucket list in a folding set.
LoopDisposition
LoopDisposition - An enum describing the relationship between a SCEV and a loop.
Class for arbitrary precision integers.
const SCEV * getSignExtendExpr(const SCEV *Op, Type *Ty)
This class uses information about analyze scalars to rewrite expressions in canonical form...
const SCEV * getAddExpr(SmallVectorImpl< const SCEV * > &Ops, SCEV::NoWrapFlags Flags=SCEV::FlagAnyWrap)
getAddExpr - Get a canonical add expression, or something simpler if possible.
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - This function should be overriden by passes that need analysis information to do t...
void collectParametricTerms(const SCEV *Expr, SmallVectorImpl< const SCEV * > &Terms)
Collect parametric terms occurring in step expressions.
The SCEV does not dominate the block.
FoldingSetNodeIDRef - This class describes a reference to an interned FoldingSetNodeID, which can be a useful to store node id data rather than using plain FoldingSetNodeIDs, since the 32-element SmallVector is often much larger than necessary, and the possibility of heap allocation means it requires a non-trivial destructor call.
void forgetLoop(const Loop *L)
forgetLoop - This method should be called by the client when it has changed a loop in a way that may ...
SCEV - This class represents an analyzed expression in the program.
SCEV(const FoldingSetNodeIDRef ID, unsigned SCEVTy)
BlockDisposition
BlockDisposition - An enum describing the relationship between a SCEV and a basic block...
bool isKnownNonZero(const SCEV *S)
isKnownNonZero - Test if the given expression is known to be non-zero.
raw_ostream & operator<<(raw_ostream &OS, const APInt &I)
ConstantRange getUnsignedRange(const SCEV *S)
getUnsignedRange - Determine the unsigned range for a particular SCEV.
const SCEV * getBackedgeTakenCount(const Loop *L)
getBackedgeTakenCount - If the specified loop has a predictable backedge-taken count, return it, otherwise return a SCEVCouldNotCompute object.
bool SimplifyICmpOperands(ICmpInst::Predicate &Pred, const SCEV *&LHS, const SCEV *&RHS, unsigned Depth=0)
SimplifyICmpOperands - Simplify LHS and RHS in a comparison with predicate Pred.
unsigned getSCEVType() const
const SCEV * getUMinExpr(const SCEV *LHS, const SCEV *RHS)
void print(raw_ostream &OS) const
print - Print out the internal representation of this scalar to the specified stream.
LLVM Value Representation.
const SCEV * getSCEV(Value *V)
getSCEV - Return a SCEV expression for the full generality of the specified expression.
void verifyAnalysis() const override
verifyAnalysis() - This member can be implemented by a analysis pass to check state of analysis infor...
DefaultFoldingSetTrait - This class provides default implementations for FoldingSetTrait implementati...
This class implements an extremely fast bulk output stream that can only output to a stream...
const SCEV * getUDivExpr(const SCEV *LHS, const SCEV *RHS)
getUDivExpr - Get a canonical unsigned division expression, or something simpler if possible...
const SCEV * getUnknown(Value *V)
The SCEV varies predictably with the loop.
bool dominates(const SCEV *S, const BasicBlock *BB)
dominates - Return true if elements that makes up the given SCEV dominate the specified basic block...
Value handle with callbacks on RAUW and destruction.
BlockDisposition getBlockDisposition(const SCEV *S, const BasicBlock *BB)
getLoopDisposition - Return the "disposition" of the given SCEV with respect to the given block...
const SCEV * getTruncateOrZeroExtend(const SCEV *V, Type *Ty)
getTruncateOrZeroExtend - Return a SCEV corresponding to a conversion of the input value to the speci...
const SCEV * getNegativeSCEV(const SCEV *V)
getNegativeSCEV - Return the SCEV object corresponding to -V.
const SCEV * getElementSize(Instruction *Inst)
Return the size of an element read or written by Inst.
const SCEV * getZeroExtendExpr(const SCEV *Op, Type *Ty)
bool hasComputableLoopEvolution(const SCEV *S, const Loop *L)
hasComputableLoopEvolution - Return true if the given SCEV changes value in a known way in the specif...
const SCEV * getNoopOrAnyExtend(const SCEV *V, Type *Ty)
getNoopOrAnyExtend - Return a SCEV corresponding to a conversion of the input value to the specified ...
NoWrapFlags
NoWrapFlags are bitfield indices into SubclassData.
friend class SCEVCallbackVH
const SCEV * getNotSCEV(const SCEV *V)
getNotSCEV - Return the SCEV object corresponding to ~V.
const SCEV * getMulExpr(SmallVectorImpl< const SCEV * > &Ops, SCEV::NoWrapFlags Flags=SCEV::FlagAnyWrap)
getMulExpr - Get a canonical multiply expression, or something simpler if possible.
bool hasLoopInvariantBackedgeTakenCount(const Loop *L)
hasLoopInvariantBackedgeTakenCount - Return true if the specified loop has an analyzable loop-invaria...
static bool classof(const SCEV *S)
Methods for support type inquiry through isa, cast, and dyn_cast:
const SCEV * getAnyExtendExpr(const SCEV *Op, Type *Ty)
getAnyExtendExpr - Return a SCEV for the given operand extended with unspecified bits out to the give...
bool runOnFunction(Function &F) override
runOnFunction - Virtual method overriden by subclasses to do the per-function processing of the pass...