26#ifndef LLVM_TRANSFORMS_VECTORIZE_LOOPVECTORIZATIONLEGALITY_H
27#define LLVM_TRANSFORMS_VECTORIZE_LOOPVECTORIZATIONLEGALITY_H
51struct VectorizeOptions;
63 enum HintKind { HK_WIDTH, HK_INTERLEAVE, HK_ISVECTORIZED };
71 Hint(
const char *Name,
unsigned Value, HintKind Kind)
74 LLVM_ABI bool validate(
unsigned Val);
77 const VectorizeOptions &Opts;
100 static StringRef Prefix() {
return "llvm.loop."; }
103 bool PotentiallyUnsafe =
false;
128 bool InterleaveOnlyWhenForced,
147 if (Interleave.Value)
148 return Interleave.Value;
195 void getHintsFromMetadata();
210 void reportDisallowedVectorization(
const StringRef DebugMsg,
213 const Loop *L)
const;
232 if (
I && !ExactFPMathInst)
293 : Opts(Opts), TheLoop(L), LI(LI), PSE(PSE), TTI(TTI), TLI(TLI), DT(DT),
294 LAIs(LAIs), ORE(ORE), Requirements(R), Hints(
H), DB(DB), AC(AC),
295 AllowRuntimeSCEVChecks(AllowRuntimeSCEVChecks), AA(AA) {}
346 "only reductions have recurrence descriptors");
347 return Reductions.find(PN)->second;
355 return ConditionalInductions;
359 return !ConditionalInductions.empty();
417 std::optional<ElementCount> VF)
const;
421 return LAI->getRuntimePointerChecking();
427 return LAI->getDepChecker().isSafeForAnyVectorWidth() &&
428 LAI->getDepChecker().isSafeForAnyStoreLoadForwardDistances();
432 return LAI->getDepChecker().getMaxSafeVectorWidthInBits();
439 return UncountableExitType;
463 return LAI->getDepChecker().isSafeForAnyStoreLoadForwardDistances();
469 return LAI->getDepChecker().getStoreLoadForwardSafeDistanceInBits();
477 return TailFoldedMaskedOp.contains(
I);
478 return ConditionallyExecutedOps.contains(
I);
490 if (HGram.Load ==
I || HGram.Update ==
I || HGram.Store ==
I)
502 auto It = CompressedPtrs.find(Ptr);
503 if (It != CompressedPtrs.end())
534 return CountableExitingBlocks;
545 bool canVectorizeLoopNestCFG(
Loop *Lp,
bool UseVPlanNativePath);
551 bool setupOuterLoopInductions();
556 bool canDetermineWidestInductionType();
564 bool canVectorizeLoopCFG(
Loop *Lp,
bool UseVPlanNativePath)
const;
569 bool canVectorizeInstrs();
578 bool canVectorizeMemory();
583 bool canVectorizeIndirectUnsafeDependences();
587 bool canVectorizeWithIfConvert();
591 bool canVectorizeOuterLoop();
611 bool isVectorizableEarlyExitLoop();
676 bool canUncountableExitConditionLoadBeMoved(
BasicBlock *ExitingBlock);
678 struct SafeAccessInfo {
684 using SafeAccessesTy = SmallDenseMap<Value *, SafeAccessInfo, 8>;
695 blockCanBePredicated(BasicBlock *BB, SafeAccessesTy &SafeAccesses,
696 SmallPtrSetImpl<const Instruction *> &MaskedOp)
const;
701 void addInductionPhi(PHINode *Phi,
const InductionDescriptor &ID);
706 bool addConditionalInduction(PHINode *Phi,
707 const ConditionalInductionDescriptor &CondID);
709 const VectorizeOptions &Opts;
722 PredicatedScalarEvolution &PSE;
725 TargetTransformInfo *
TTI;
728 TargetLibraryInfo *
TLI;
734 LoopAccessInfoManager &LAIs;
736 const LoopAccessInfo *LAI =
nullptr;
739 OptimizationRemarkEmitter *ORE;
745 PHINode *PrimaryInduction =
nullptr;
762 IntegerType *WidestIndTy =
nullptr;
765 LoopVectorizationRequirements *Requirements;
768 LoopVectorizeHints *Hints;
780 SmallPtrSet<const Instruction *, 8> ConditionallyExecutedOps;
782 SmallPtrSet<const Instruction *, 8> TailFoldedMaskedOp;
792 SmallDenseMap<const Value *, CompressedPtrInfo> CompressedPtrs;
795 bool AllowRuntimeSCEVChecks;
805 bool VecCallVariantsFound =
false;
const TargetLowering & TLI
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
This file defines the DenseMap class.
This file implements a map that provides insertion order iteration.
SmallVector< MachineOperand, 4 > Cond
A cache of @llvm.assume calls within a function.
LLVM Basic Block Representation.
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree.
static constexpr ElementCount get(ScalarTy MinVal, bool Scalable)
Class to represent integer types.
An instruction for reading from memory.
Drive the analysis of memory accesses in the loop.
bool isCompressedLoadOrStore(Instruction *I)
Returns true for I if it corresponds to a compressed load or store (which can map to an llvm....
MapVector< PHINode *, InductionDescriptor > InductionList
InductionList saves induction variables and maps them to the induction descriptor.
LoopInfo * getLoopInfo() const
LLVM_ABI bool isInvariantStoreOfReduction(StoreInst *SI)
Returns True if given store is a final invariant store of one of the reductions found in the loop.
bool hasVectorCallVariants() const
Returns true if there is at least one function call in the loop which has a vectorized variant availa...
uint64_t getMaxSafeVectorWidthInBits() const
LLVM_ABI void collectUnitStridePredicates() const
Add unit stride predicates for memory accesses to PSE, if runtime checks are allowed and an inner loo...
const ConditionalInductionList & getConditionalInductions() const
Returns the conditional inductions found in the loop.
const RecurrenceDescriptor & getRecurrenceDescriptor(PHINode *PN) const
Returns the recurrence descriptor associated with a given phi node PN, expecting one to exist.
RecurrenceSet & getFixedOrderRecurrences()
Return the fixed-order recurrences found in the loop.
uint64_t getMaxStoreLoadForwardSafeDistanceInBits() const
Return safe power-of-2 number of elements, which do not prevent store-load forwarding and safe to ope...
LLVM_ABI bool isInvariantAddressOfReduction(Value *V)
Returns True if given address is invariant and is used to store recurrent expression.
DominatorTree * getDominatorTree() const
LLVM_ABI bool canVectorize(bool UseVPlanNativePath)
Returns true if it is legal to vectorize this loop.
PredicatedScalarEvolution * getPredicatedScalarEvolution() const
LLVM_ABI bool blockNeedsPredication(const BasicBlock *BB) const
Return true if the block BB needs to be predicated in order for the loop to be vectorized.
LLVM_ABI int isConsecutivePtr(Type *AccessTy, Value *Ptr) const
Check if this pointer is consecutive when vectorizing.
AssumptionCache * getAssumptionCache() const
SmallPtrSet< const PHINode *, 8 > RecurrenceSet
RecurrenceSet contains the phi nodes that are recurrences other than inductions and reductions.
std::optional< CompressedPtrInfo > getCompressedPtrInfo(Value *Ptr) const
Returns the CompressedPtrInfo for Ptr if the pointer is defined via a conditional induction PHI,...
bool hasUncountableExitWithSideEffects() const
Returns true if this is an early exit loop with state-changing or potentially-faulting operations and...
LLVM_ABI bool canVectorizeFPMath(bool EnableStrictReductions)
Returns true if it is legal to vectorize the FP math operations in this loop.
bool isReductionVariable(PHINode *PN) const
Returns True if PN is a reduction variable in this loop.
LLVM_ABI bool isFixedOrderRecurrence(const PHINode *Phi) const
Returns True if Phi is a fixed-order recurrence in this loop.
IntegerType * getWidestInductionType()
Returns the widest induction type.
LLVM_ABI bool isInductionPhi(const Value *V) const
Returns True if V is a Phi node of an induction variable in this loop.
PHINode * getPrimaryInduction()
Returns the primary induction variable.
UncountableExitTrait getUncountableExitTrait() const
Returns information about whether this loop contains at least one uncountable early exit,...
const SmallVector< BasicBlock *, 4 > & getCountableExitingBlocks() const
Returns all exiting blocks with a countable exit, i.e.
AAResults * getAA() const
LLVM_ABI Instruction * findCountableComparisonInCombinedCondition(Value *Cond) const
If Cond is a combined exit condition featuring uncountable and countable comparisons,...
bool hasConditionalInductions() const
const HistogramInfo * getHistogramInfo(Instruction *I) const
Returns a HistogramInfo* for the given instruction if it was determined to be part of a load -> updat...
const InductionList & getInductionVars() const
Returns the induction variables found in the loop.
LLVM_ABI bool isInvariant(Value *V) const
Returns true if V is invariant across all loop iterations according to SCEV.
const ReductionList & getReductionVars() const
Returns the reduction variables found in the loop.
bool isSafeForAnyStoreLoadForwardDistances() const
Return true if there is store-load forwarding dependencies.
bool isSafeForAnyVectorWidth() const
LLVM_ABI bool canFoldTailByMasking() const
Return true if we can vectorize this loop while folding its tail by masking.
LLVM_ABI void prepareToFoldTailByMasking()
Mark all respective loads/stores for masking.
bool hasUncountableEarlyExit() const
Returns true if the loop has uncountable early exits, i.e.
LLVM_ABI bool isUniformMemOp(Instruction &I, std::optional< ElementCount > VF) const
A uniform memory op is a load or store which accesses the same memory location on all VF lanes,...
bool hasHistograms() const
Returns a list of all known histogram operations in the loop.
const LoopAccessInfo * getLAI() const
MapVector< PHINode *, RecurrenceDescriptor > ReductionList
ReductionList contains the reduction descriptors for all of the reductions that were found in the loo...
ScalarEvolution * getScalarEvolution() const
MapVector< PHINode *, ConditionalInductionDescriptor > ConditionalInductionList
ConditionalInductionList saves conditional inductions and maps them to their descriptors.
bool isMaskRequired(const Instruction *I, bool TailFolded) const
Returns true if instruction I requires a mask for vectorization.
LoopVectorizationLegality(const VectorizeOptions &Opts, Loop *L, PredicatedScalarEvolution &PSE, DominatorTree *DT, TargetTransformInfo *TTI, TargetLibraryInfo *TLI, Function *F, LoopAccessInfoManager &LAIs, LoopInfo *LI, OptimizationRemarkEmitter *ORE, LoopVectorizationRequirements *R, LoopVectorizeHints *H, DemandedBits *DB, AssumptionCache *AC, bool AllowRuntimeSCEVChecks, AAResults *AA)
LLVM_ABI bool isUniform(Value *V, std::optional< ElementCount > VF) const
Returns true if value V is uniform across VF lanes, when VF is provided, and otherwise if V is invari...
const RuntimePointerChecking * getRuntimePointerChecking() const
Returns the information that we collected about runtime memory check.
This holds vectorization requirements that must be verified late in the process.
Instruction * getExactFPInst()
void addExactFPMathInst(Instruction *I)
Track the 1st floating-point instruction that can not be reassociated.
Utility class for getting and setting loop vectorizer hints in the form of loop metadata.
@ SK_PreferScalable
Vectorize loops using scalable vectors or fixed-width vectors, but favor scalable vectors when the co...
@ SK_AlwaysScalable
Always vectorize loops using scalable vectors if feasible (i.e.
@ SK_Unspecified
Not selected.
@ SK_FixedWidthOnly
Disables vectorization with scalable vectors.
bool isScalableVectorizationDisabled() const
enum ForceKind getForce() const
LLVM_ABI bool allowReordering() const
When enabling loop hints are provided we allow the vectorizer to change the order of operations that ...
LLVM_ABI LoopVectorizeHints(const VectorizeOptions &Opts, const Loop *L, bool InterleaveOnlyWhenForced, OptimizationRemarkEmitter &ORE, const TargetTransformInfo *TTI=nullptr)
LLVM_ABI void emitRemarkWithHints() const
Dumps all the hint information.
LLVM_ABI bool allowVectorization(Loop *L, bool VectorizeOnlyWhenForced) const
bool isScalableVectorizationAlwaysPreferred() const
bool isPotentiallyUnsafe() const
ElementCount getWidth() const
@ FK_Enabled
Forcing enabled.
@ FK_Undefined
Not selected.
@ FK_Disabled
Forcing disabled.
unsigned getPredicate() const
LLVM_ABI void setAlreadyVectorized()
Mark the loop L as already vectorized by setting the width to 1.
unsigned getInterleave() const
unsigned getIsVectorized() const
void setPotentiallyUnsafe()
Represents a single loop in the control flow graph.
This class implements a map that also provides access to all stored values in a deterministic order.
An interface layer with SCEV used to manage how we see SCEV expressions for values in the context of ...
The RecurrenceDescriptor is used to identify recurrences variables in a loop.
Holds information about the memory runtime legality checks to verify that a group of pointers do not ...
This node represents a polynomial recurrence on the trip count of the specified loop.
The main scalar evolution driver.
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements.
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
An instruction for storing to memory.
Represent a constant reference to a string, i.e.
Provides information about what library functions are available for the current target.
The instances of the Type class are immutable: once they are created, they are never changed.
LLVM Value Representation.
This is an optimization pass for GlobalISel generic memory operations.
const Value * getLoadStorePointerOperand(const Value *V)
A helper function that returns the pointer operand of a load or store instruction.
HistogramUpdateKind
The kind of update operation performed on the buckets of a histogram, i.e.
UncountableExitTrait
Indicates the characteristics of a loop with an uncountable exit.
RelativeUniformCounterPtr ValuesPtrExpr VTableAddr Value
LLVM_ABI bool hasDisableAllTransformsHint(const Loop *L)
Look for the loop attribute that disables all transformation heuristic.
LLVM_ABI TransformationMode hasUnrollTransformation(const Loop *L)
class LLVM_GSL_OWNER SmallVector
Forward declaration of SmallVector so that calculateSmallVectorDefaultInlinedElements can reference s...
bool isa(const From &Val)
isa<X> - Return true if the parameter to the template is an instance of one of the template type argu...
@ TM_Disable
The transformation should not be applied.
This struct is a compact representation of a valid (non-zero power of two) alignment.
Holds details about a "compressed" pointer: the conditional induction PHI used to derive the pointer ...
PHINode * ConditionalInductionPHI
const SCEVAddRecExpr * PtrSCEV
This holds details about a histogram operation – a load -> update -> store sequence where each lane i...
HistogramInfo(LoadInst *Load, Instruction *Update, StoreInst *Store, HistogramUpdateKind UpdateKind)
HistogramUpdateKind UpdateKind
The update operation applied to the histogram buckets.