37#define VPINTERNAL_VPLEGAL_CASES \
38 VPINTERNAL_CASE(Legal) \
39 VPINTERNAL_CASE(Discard) \
40 VPINTERNAL_CASE(Convert)
42#define VPINTERNAL_CASE(X) "|" #X
48 ". If non-empty, ignore "
49 "TargetTransformInfo and "
50 "always use this transformation for the %evl parameter (Used in "
56 ". If non-empty, Ignore "
57 "TargetTransformInfo and "
58 "always use this transformation for the %mask parameter (Used in "
62#define VPINTERNAL_CASE(X) .Case(#X, VPLegalization::X)
68#undef VPINTERNAL_VPLEGAL_CASES
75#define DEBUG_TYPE "expandvp"
77STATISTIC(NumFoldedVL,
"Number of folded vector length params");
78STATISTIC(NumLoweredVPOps,
"Number of folded vector predication operations");
86 return ConstValue->isAllOnesValue();
94 return ConstantInt::get(DivTy, 1u,
false);
107 NewInst->setFastMathFlags(OldFMOp->getFastMathFlags());
140struct CachingVPExpander {
141 const TargetTransformInfo &TTI;
154 ElementCount ElemCount);
158 bool foldEVLIntoMask(VPIntrinsic &VPI);
163 bool discardEVLParameter(VPIntrinsic &PI);
166 bool expandPredicationInBinaryOperator(
IRBuilder<> &Builder, VPIntrinsic &PI);
169 bool expandPredicationInReduction(
IRBuilder<> &Builder,
170 VPReductionIntrinsic &PI);
173 bool expandPredicationInMemoryIntrinsic(
IRBuilder<> &Builder,
177 bool expandPredication(VPIntrinsic &PI);
181 VPLegalization getVPLegalizationStrategy(
const VPIntrinsic &VPI)
const;
182 bool UsingTTIOverrides;
185 CachingVPExpander(
const TargetTransformInfo &TTI)
201 Type *BoolVecTy = VectorType::get(Builder.
getInt1Ty(), ElemCount);
205 {BoolVecTy, EVLParam->
getType()},
206 {ConstZero, EVLParam});
217bool CachingVPExpander::expandPredicationInBinaryOperator(
IRBuilder<> &Builder,
220 "Implicitly dropping %evl in non-speculatable operator!");
237 case Instruction::UDiv:
238 case Instruction::SDiv:
239 case Instruction::URem:
240 case Instruction::SRem:
253static Value *getNeutralReductionElement(
const VPReductionIntrinsic &VPI,
259bool CachingVPExpander::expandPredicationInReduction(
262 "Implicitly dropping %evl in non-speculatable operator!");
269 auto *NeutralElt = getNeutralReductionElement(VPI, VPI.
getType());
272 RedOp = Builder.
CreateSelect(Mask, RedOp, NeutralVector);
281 case Intrinsic::vp_reduce_add:
282 case Intrinsic::vp_reduce_mul:
283 case Intrinsic::vp_reduce_and:
284 case Intrinsic::vp_reduce_or:
285 case Intrinsic::vp_reduce_xor: {
294 case Intrinsic::vp_reduce_smax:
295 case Intrinsic::vp_reduce_smin:
296 case Intrinsic::vp_reduce_umax:
297 case Intrinsic::vp_reduce_umin:
298 case Intrinsic::vp_reduce_fmax:
299 case Intrinsic::vp_reduce_fmin:
300 case Intrinsic::vp_reduce_fmaximum:
301 case Intrinsic::vp_reduce_fminimum: {
309 case Intrinsic::vp_reduce_fadd:
312 case Intrinsic::vp_reduce_fmul:
321bool CachingVPExpander::expandPredicationInMemoryIntrinsic(
IRBuilder<> &Builder,
334 Value *NewMemoryInst =
nullptr;
338 case Intrinsic::vp_store:
340 StoreInst *NewStore =
342 if (AlignOpt.has_value())
344 NewMemoryInst = NewStore;
347 DataParam, PtrParam, AlignOpt.
valueOrOne(), MaskParam);
350 case Intrinsic::vp_load:
354 if (AlignOpt.has_value())
356 NewMemoryInst = NewLoad;
362 case Intrinsic::vp_scatter: {
367 AlignOpt.value_or(
DL.getPrefTypeAlign(ElementType)), MaskParam);
370 case Intrinsic::vp_gather: {
374 AlignOpt.value_or(
DL.getPrefTypeAlign(ElementType)), MaskParam,
385bool CachingVPExpander::discardEVLParameter(VPIntrinsic &VPI) {
396 Value *MaxEVL =
nullptr;
403 MaxEVL = Builder.
CreateNUWMul(VScale, FactorConst,
"scalable_size");
405 MaxEVL = ConstantInt::get(Int32Ty, StaticElemCount.
getFixedValue(),
false);
411bool CachingVPExpander::foldEVLIntoMask(VPIntrinsic &VPI) {
424 "Unexpected VP intrinsic without mask operand");
429 assert(OldMaskParam &&
"no mask param to fold the vl param into");
430 assert(OldEVLParam &&
"no EVL param to fold away");
437 Value *VLMask = convertEVLToMask(Builder, OldEVLParam, ElemCount);
445 discardEVLParameter(VPI);
447 "transformation did not render the evl param ineffective!");
453bool CachingVPExpander::expandPredication(VPIntrinsic &VPI) {
454 LLVM_DEBUG(
dbgs() <<
"Lowering to unpredicated op: " << VPI <<
'\n');
462 return expandPredicationInBinaryOperator(Builder, VPI);
465 return expandPredicationInReduction(Builder, *VPRI);
470 case Intrinsic::vp_merge: {
477 case Intrinsic::vp_load:
478 case Intrinsic::vp_store:
479 case Intrinsic::vp_gather:
480 case Intrinsic::vp_scatter:
481 return expandPredicationInMemoryIntrinsic(Builder, VPI);
489void sanitizeStrategy(VPIntrinsic &VPI,
VPLegalization &LegalizeStrat) {
511CachingVPExpander::getVPLegalizationStrategy(
const VPIntrinsic &VPI)
const {
526CachingVPExpander::expandVectorPredication(VPIntrinsic &VPI) {
527 auto Strategy = getVPLegalizationStrategy(VPI);
528 sanitizeStrategy(VPI, Strategy);
533 switch (Strategy.EVLParamStrategy) {
537 if (discardEVLParameter(VPI))
538 Changed = VPExpansionDetails::IntrinsicUpdated;
541 if (foldEVLIntoMask(VPI)) {
542 Changed = VPExpansionDetails::IntrinsicUpdated;
549 switch (Strategy.OpStrategy) {
555 if (expandPredication(VPI)) {
557 Changed = VPExpansionDetails::IntrinsicReplaced;
569 return CachingVPExpander(
TTI).expandVectorPredication(VPI);
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
#define LLVM_LIKELY(EXPR)
This file contains the declarations for the subclasses of Constant, which represent the different fla...
static VPTransform parseOverrideOption(const std::string &TextOpt)
static cl::opt< std::string > MaskTransformOverride("expandvp-override-mask-transform", cl::init(""), cl::Hidden, cl::desc("Options: <empty>" VPINTERNAL_VPLEGAL_CASES ". If non-empty, Ignore " "TargetTransformInfo and " "always use this transformation for the %mask parameter (Used in " "testing)."))
static cl::opt< std::string > EVLTransformOverride("expandvp-override-evl-transform", cl::init(""), cl::Hidden, cl::desc("Options: <empty>" VPINTERNAL_VPLEGAL_CASES ". If non-empty, ignore " "TargetTransformInfo and " "always use this transformation for the %evl parameter (Used in " "testing)."))
static void replaceOperation(Value &NewOp, VPIntrinsic &OldOp)
Transfer all properties from OldOp to NewOp and replace all uses.
static bool isAllTrueMask(Value *MaskVal)
static void transferDecorations(Value &NewVal, VPIntrinsic &VPI)
Transfer operation properties from OldVPI to NewVal.
TargetTransformInfo::VPLegalization VPLegalization
TargetTransformInfo::VPLegalization::VPTransform VPTransform
static bool anyExpandVPOverridesSet()
static bool maySpeculateLanes(VPIntrinsic &VPI)
static Constant * getSafeDivisor(Type *DivTy)
#define VPINTERNAL_VPLEGAL_CASES
loop Loop Strength Reduction
This file defines the 'Statistic' class, which is designed to be an easy way to expose various metric...
#define STATISTIC(VARNAME, DESC)
LLVM_ABI bool hasAttribute(Attribute::AttrKind Kind) const
Return true if the attribute exists in this set.
Value * getArgOperand(unsigned i) const
void setArgOperand(unsigned i, Value *v)
@ ICMP_ULT
unsigned less than
This is an important base class in LLVM.
Value * CreateNUWMul(Value *LHS, Value *RHS, const Twine &Name="")
IntegerType * getInt1Ty()
Fetch the type representing a single bit.
LLVM_ABI Value * CreateVectorSplat(unsigned NumElts, Value *V, const Twine &Name="")
Return a vector value that contains.
LLVM_ABI CallInst * CreateMaskedLoad(Type *Ty, Value *Ptr, Align Alignment, Value *Mask, Value *PassThru=nullptr, const Twine &Name="")
Create a call to Masked Load intrinsic.
LLVM_ABI Value * CreateSelect(Value *C, Value *True, Value *False, const Twine &Name="", Instruction *MDFrom=nullptr)
Value * CreateVScale(Type *Ty, const Twine &Name="")
Create a call to llvm.vscale.<Ty>().
LLVM_ABI Value * CreateBinaryIntrinsic(Intrinsic::ID ID, Value *LHS, Value *RHS, FMFSource FMFSource={}, const Twine &Name="")
Create a call to intrinsic ID with 2 operands which is mangled on the first type.
ConstantInt * getInt32(uint32_t C)
Get a constant 32-bit value.
LoadInst * CreateLoad(Type *Ty, Value *Ptr, const char *Name)
Provided to resolve 'CreateLoad(Ty, Ptr, "...")' correctly, instead of converting the string to 'bool...
Value * CreateAnd(Value *LHS, Value *RHS, const Twine &Name="")
LLVM_ABI Value * CreateIntrinsic(Intrinsic::ID ID, ArrayRef< Type * > OverloadTypes, ArrayRef< Value * > Args, FMFSource FMFSource={}, const Twine &Name="", ArrayRef< OperandBundleDef > OpBundles={}, function_ref< void(CallInst *)> SetFn=[](CallInst *) {})
Variant to create a possibly constant-folded intrinsic.
StoreInst * CreateStore(Value *Val, Value *Ptr, bool isVolatile=false)
LLVM_ABI CallInst * CreateMaskedStore(Value *Val, Value *Ptr, Align Alignment, Value *Mask)
Create a call to Masked Store intrinsic.
Value * CreateBinOp(Instruction::BinaryOps Opc, Value *LHS, Value *RHS, const Twine &Name="", MDNode *FPMathTag=nullptr)
LLVM_ABI Value * CreateFAddReduce(Value *Acc, Value *Src)
Create a sequential vector fadd reduction intrinsic of the source vector.
LLVM_ABI Value * CreateFMulReduce(Value *Acc, Value *Src)
Create a sequential vector fmul reduction intrinsic of the source vector.
Value * CreateICmp(CmpInst::Predicate P, Value *LHS, Value *RHS, const Twine &Name="")
LLVM_ABI Value * CreateStepVector(Type *DstType, const Twine &Name="")
Creates a vector of type DstType with the linear sequence <0, 1, ...>
LLVM_ABI CallInst * CreateMaskedScatter(Value *Val, Value *Ptrs, Align Alignment, Value *Mask=nullptr)
Create a call to Masked Scatter intrinsic.
LLVM_ABI Value * CreateUnaryIntrinsic(Intrinsic::ID ID, Value *Op, FMFSource FMFSource={}, const Twine &Name="")
Create a call to intrinsic ID with 1 operand which is mangled on its type.
LLVM_ABI CallInst * CreateMaskedGather(Type *Ty, Value *Ptrs, Align Alignment, Value *Mask=nullptr, Value *PassThru=nullptr, const Twine &Name="")
Create a call to Masked Gather intrinsic.
This provides a uniform API for creating instructions and inserting them into a basic block: either a...
LLVM_ABI FastMathFlags getFastMathFlagsOrNone() const LLVM_READONLY
Convenience function for getting fast-math flags, or default-constructed FastMathFlags when not a FPM...
LLVM_ABI InstListType::iterator eraseFromParent()
This method unlinks 'this' from the containing basic block and deletes it.
LLVM_ABI const DataLayout & getDataLayout() const
Get the data layout of the module this instruction belongs to.
Intrinsic::ID getIntrinsicID() const
Return the intrinsic ID of this intrinsic.
void setAlignment(Align Align)
void setAlignment(Align Align)
A switch()-like statement whose cases are string literals.
The instances of the Type class are immutable: once they are created, they are never changed.
bool isIntOrIntVectorTy() const
Return true if this is an integer type or a vector of integer types.
Value * getOperand(unsigned i) const
This is the common base class for vector predication intrinsics.
std::optional< unsigned > getFunctionalIntrinsicID() const
LLVM_ABI bool canIgnoreVectorLengthParam() const
LLVM_ABI void setMaskParam(Value *)
LLVM_ABI Value * getVectorLengthParam() const
LLVM_ABI void setVectorLengthParam(Value *)
LLVM_ABI Value * getMemoryDataParam() const
LLVM_ABI Value * getMemoryPointerParam() const
LLVM_ABI MaybeAlign getPointerAlignment() const
LLVM_ABI Value * getMaskParam() const
LLVM_ABI ElementCount getStaticVectorLength() const
std::optional< unsigned > getFunctionalOpcode() const
LLVM_ABI unsigned getStartParamPos() const
LLVM_ABI unsigned getVectorParamPos() const
LLVM Value Representation.
Type * getType() const
All values are typed, get the type of this value.
LLVM_ABI void replaceAllUsesWith(Value *V)
Change all uses of this to point to a new Value.
LLVMContext & getContext() const
All values hold a context through their type.
LLVM_ABI void takeName(Value *V)
Transfer the name from V to this value.
constexpr ScalarTy getFixedValue() const
constexpr bool isScalable() const
Returns whether the quantity is scaled by a runtime quantity (vscale).
constexpr ScalarTy getKnownMinValue() const
Returns the minimum value this quantity can represent.
const ParentTy * getParent() const
self_iterator getIterator()
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
constexpr std::underlying_type_t< E > Mask()
Get a bitmask with 1s in all places up to the high-order bit of E's largest value.
LLVM_ABI AttributeSet getFnAttributes(LLVMContext &C, ID id)
Return the function attributes for an intrinsic.
initializer< Ty > init(const Ty &Val)
ElementType
The element type of an SRV or UAV resource.
This is an optimization pass for GlobalISel generic memory operations.
LLVM_ABI Intrinsic::ID getMinMaxReductionIntrinsicOp(Intrinsic::ID RdxID)
Returns the min/max intrinsic used when expanding a min/max reduction.
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
LLVM_ABI Value * getReductionIdentity(Intrinsic::ID RdxID, Type *Ty, FastMathFlags FMF)
Given information about an @llvm.vector.reduce.
LLVM_ABI unsigned getArithmeticReductionInstruction(Intrinsic::ID RdxID)
Returns the arithmetic instruction opcode used when expanding a reduction.
LLVM_ABI Value * getSplatValue(const Value *V)
Get splat value if the input is a splat vector or return nullptr.
RelativeUniformCounterPtr ValuesPtrExpr VTableAddr Value
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
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...
LLVM_ABI VPExpansionDetails expandVectorPredicationIntrinsic(VPIntrinsic &VPI, const TargetTransformInfo &TTI)
Expand a vector predication intrinsic.
LLVM_ABI bool isSafeToSpeculativelyExecuteWithOpcode(unsigned Opcode, const Instruction *Inst, const Instruction *CtxI=nullptr, AssumptionCache *AC=nullptr, const DominatorTree *DT=nullptr, const TargetLibraryInfo *TLI=nullptr, bool UseVariableInfo=true, bool IgnoreUBImplyingAttrs=true)
This returns the same result as isSafeToSpeculativelyExecute if Opcode is the actual opcode of Inst.
IRBuilder(LLVMContext &, FolderTy, InserterTy, MDNode *, ArrayRef< OperandBundleDef >) -> IRBuilder< FolderTy, InserterTy >
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
VPExpansionDetails
Represents the details the expansion of a VP intrinsic.
Align valueOrOne() const
For convenience, returns a valid alignment or 1 if undefined.