1fe6060f1SDimitry Andric //===----- CodeGen/ExpandVectorPredication.cpp - Expand VP intrinsics -----===//
2fe6060f1SDimitry Andric //
3fe6060f1SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4fe6060f1SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5fe6060f1SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6fe6060f1SDimitry Andric //
7fe6060f1SDimitry Andric //===----------------------------------------------------------------------===//
8fe6060f1SDimitry Andric //
9fe6060f1SDimitry Andric // This pass implements IR expansion for vector predication intrinsics, allowing
10fe6060f1SDimitry Andric // targets to enable vector predication until just before codegen.
11fe6060f1SDimitry Andric //
12fe6060f1SDimitry Andric //===----------------------------------------------------------------------===//
13fe6060f1SDimitry Andric
14fe6060f1SDimitry Andric #include "llvm/CodeGen/ExpandVectorPredication.h"
15fe6060f1SDimitry Andric #include "llvm/ADT/Statistic.h"
16fe6060f1SDimitry Andric #include "llvm/Analysis/TargetTransformInfo.h"
17fe6060f1SDimitry Andric #include "llvm/Analysis/ValueTracking.h"
18fcaf7f86SDimitry Andric #include "llvm/Analysis/VectorUtils.h"
19fe6060f1SDimitry Andric #include "llvm/CodeGen/Passes.h"
20fe6060f1SDimitry Andric #include "llvm/IR/Constants.h"
21fe6060f1SDimitry Andric #include "llvm/IR/Function.h"
22fe6060f1SDimitry Andric #include "llvm/IR/IRBuilder.h"
23fe6060f1SDimitry Andric #include "llvm/IR/InstIterator.h"
24fe6060f1SDimitry Andric #include "llvm/IR/Instructions.h"
25fe6060f1SDimitry Andric #include "llvm/IR/IntrinsicInst.h"
26fe6060f1SDimitry Andric #include "llvm/IR/Intrinsics.h"
27fe6060f1SDimitry Andric #include "llvm/InitializePasses.h"
28fe6060f1SDimitry Andric #include "llvm/Pass.h"
29fe6060f1SDimitry Andric #include "llvm/Support/CommandLine.h"
30fe6060f1SDimitry Andric #include "llvm/Support/Compiler.h"
31fe6060f1SDimitry Andric #include "llvm/Support/Debug.h"
32bdd1243dSDimitry Andric #include <optional>
33fe6060f1SDimitry Andric
34fe6060f1SDimitry Andric using namespace llvm;
35fe6060f1SDimitry Andric
36fe6060f1SDimitry Andric using VPLegalization = TargetTransformInfo::VPLegalization;
37fe6060f1SDimitry Andric using VPTransform = TargetTransformInfo::VPLegalization::VPTransform;
38fe6060f1SDimitry Andric
39fe6060f1SDimitry Andric // Keep this in sync with TargetTransformInfo::VPLegalization.
40fe6060f1SDimitry Andric #define VPINTERNAL_VPLEGAL_CASES \
41fe6060f1SDimitry Andric VPINTERNAL_CASE(Legal) \
42fe6060f1SDimitry Andric VPINTERNAL_CASE(Discard) \
43fe6060f1SDimitry Andric VPINTERNAL_CASE(Convert)
44fe6060f1SDimitry Andric
45fe6060f1SDimitry Andric #define VPINTERNAL_CASE(X) "|" #X
46fe6060f1SDimitry Andric
47fe6060f1SDimitry Andric // Override options.
48fe6060f1SDimitry Andric static cl::opt<std::string> EVLTransformOverride(
49fe6060f1SDimitry Andric "expandvp-override-evl-transform", cl::init(""), cl::Hidden,
50fe6060f1SDimitry Andric cl::desc("Options: <empty>" VPINTERNAL_VPLEGAL_CASES
51fe6060f1SDimitry Andric ". If non-empty, ignore "
52fe6060f1SDimitry Andric "TargetTransformInfo and "
53fe6060f1SDimitry Andric "always use this transformation for the %evl parameter (Used in "
54fe6060f1SDimitry Andric "testing)."));
55fe6060f1SDimitry Andric
56fe6060f1SDimitry Andric static cl::opt<std::string> MaskTransformOverride(
57fe6060f1SDimitry Andric "expandvp-override-mask-transform", cl::init(""), cl::Hidden,
58fe6060f1SDimitry Andric cl::desc("Options: <empty>" VPINTERNAL_VPLEGAL_CASES
59fe6060f1SDimitry Andric ". If non-empty, Ignore "
60fe6060f1SDimitry Andric "TargetTransformInfo and "
61fe6060f1SDimitry Andric "always use this transformation for the %mask parameter (Used in "
62fe6060f1SDimitry Andric "testing)."));
63fe6060f1SDimitry Andric
64fe6060f1SDimitry Andric #undef VPINTERNAL_CASE
65fe6060f1SDimitry Andric #define VPINTERNAL_CASE(X) .Case(#X, VPLegalization::X)
66fe6060f1SDimitry Andric
parseOverrideOption(const std::string & TextOpt)67fe6060f1SDimitry Andric static VPTransform parseOverrideOption(const std::string &TextOpt) {
68fe6060f1SDimitry Andric return StringSwitch<VPTransform>(TextOpt) VPINTERNAL_VPLEGAL_CASES;
69fe6060f1SDimitry Andric }
70fe6060f1SDimitry Andric
71fe6060f1SDimitry Andric #undef VPINTERNAL_VPLEGAL_CASES
72fe6060f1SDimitry Andric
73fe6060f1SDimitry Andric // Whether any override options are set.
anyExpandVPOverridesSet()74fe6060f1SDimitry Andric static bool anyExpandVPOverridesSet() {
75fe6060f1SDimitry Andric return !EVLTransformOverride.empty() || !MaskTransformOverride.empty();
76fe6060f1SDimitry Andric }
77fe6060f1SDimitry Andric
78fe6060f1SDimitry Andric #define DEBUG_TYPE "expandvp"
79fe6060f1SDimitry Andric
80fe6060f1SDimitry Andric STATISTIC(NumFoldedVL, "Number of folded vector length params");
81fe6060f1SDimitry Andric STATISTIC(NumLoweredVPOps, "Number of folded vector predication operations");
82fe6060f1SDimitry Andric
83fe6060f1SDimitry Andric ///// Helpers {
84fe6060f1SDimitry Andric
85fe6060f1SDimitry Andric /// \returns Whether the vector mask \p MaskVal has all lane bits set.
isAllTrueMask(Value * MaskVal)86fe6060f1SDimitry Andric static bool isAllTrueMask(Value *MaskVal) {
87fcaf7f86SDimitry Andric if (Value *SplattedVal = getSplatValue(MaskVal))
88fcaf7f86SDimitry Andric if (auto *ConstValue = dyn_cast<Constant>(SplattedVal))
89fcaf7f86SDimitry Andric return ConstValue->isAllOnesValue();
90fcaf7f86SDimitry Andric
91fcaf7f86SDimitry Andric return false;
92fe6060f1SDimitry Andric }
93fe6060f1SDimitry Andric
94fe6060f1SDimitry Andric /// \returns A non-excepting divisor constant for this type.
getSafeDivisor(Type * DivTy)95fe6060f1SDimitry Andric static Constant *getSafeDivisor(Type *DivTy) {
96fe6060f1SDimitry Andric assert(DivTy->isIntOrIntVectorTy() && "Unsupported divisor type");
97fe6060f1SDimitry Andric return ConstantInt::get(DivTy, 1u, false);
98fe6060f1SDimitry Andric }
99fe6060f1SDimitry Andric
100fe6060f1SDimitry Andric /// Transfer operation properties from \p OldVPI to \p NewVal.
transferDecorations(Value & NewVal,VPIntrinsic & VPI)101fe6060f1SDimitry Andric static void transferDecorations(Value &NewVal, VPIntrinsic &VPI) {
102fe6060f1SDimitry Andric auto *NewInst = dyn_cast<Instruction>(&NewVal);
103fe6060f1SDimitry Andric if (!NewInst || !isa<FPMathOperator>(NewVal))
104fe6060f1SDimitry Andric return;
105fe6060f1SDimitry Andric
106fe6060f1SDimitry Andric auto *OldFMOp = dyn_cast<FPMathOperator>(&VPI);
107fe6060f1SDimitry Andric if (!OldFMOp)
108fe6060f1SDimitry Andric return;
109fe6060f1SDimitry Andric
110fe6060f1SDimitry Andric NewInst->setFastMathFlags(OldFMOp->getFastMathFlags());
111fe6060f1SDimitry Andric }
112fe6060f1SDimitry Andric
113fe6060f1SDimitry Andric /// Transfer all properties from \p OldOp to \p NewOp and replace all uses.
114fe6060f1SDimitry Andric /// OldVP gets erased.
replaceOperation(Value & NewOp,VPIntrinsic & OldOp)115fe6060f1SDimitry Andric static void replaceOperation(Value &NewOp, VPIntrinsic &OldOp) {
116fe6060f1SDimitry Andric transferDecorations(NewOp, OldOp);
117fe6060f1SDimitry Andric OldOp.replaceAllUsesWith(&NewOp);
118fe6060f1SDimitry Andric OldOp.eraseFromParent();
119fe6060f1SDimitry Andric }
120fe6060f1SDimitry Andric
maySpeculateLanes(VPIntrinsic & VPI)12181ad6265SDimitry Andric static bool maySpeculateLanes(VPIntrinsic &VPI) {
12281ad6265SDimitry Andric // The result of VP reductions depends on the mask and evl.
12381ad6265SDimitry Andric if (isa<VPReductionIntrinsic>(VPI))
12481ad6265SDimitry Andric return false;
12581ad6265SDimitry Andric // Fallback to whether the intrinsic is speculatable.
126c9157d92SDimitry Andric if (auto IntrID = VPI.getFunctionalIntrinsicID())
127c9157d92SDimitry Andric return Intrinsic::getAttributes(VPI.getContext(), *IntrID)
128c9157d92SDimitry Andric .hasFnAttr(Attribute::AttrKind::Speculatable);
129c9157d92SDimitry Andric if (auto Opc = VPI.getFunctionalOpcode())
130c9157d92SDimitry Andric return isSafeToSpeculativelyExecuteWithOpcode(*Opc, &VPI);
131c9157d92SDimitry Andric return false;
13281ad6265SDimitry Andric }
13381ad6265SDimitry Andric
134fe6060f1SDimitry Andric //// } Helpers
135fe6060f1SDimitry Andric
136fe6060f1SDimitry Andric namespace {
137fe6060f1SDimitry Andric
138fe6060f1SDimitry Andric // Expansion pass state at function scope.
139fe6060f1SDimitry Andric struct CachingVPExpander {
140fe6060f1SDimitry Andric Function &F;
141fe6060f1SDimitry Andric const TargetTransformInfo &TTI;
142fe6060f1SDimitry Andric
143fe6060f1SDimitry Andric /// \returns A (fixed length) vector with ascending integer indices
144fe6060f1SDimitry Andric /// (<0, 1, ..., NumElems-1>).
145fe6060f1SDimitry Andric /// \p Builder
146fe6060f1SDimitry Andric /// Used for instruction creation.
147fe6060f1SDimitry Andric /// \p LaneTy
148fe6060f1SDimitry Andric /// Integer element type of the result vector.
149fe6060f1SDimitry Andric /// \p NumElems
150fe6060f1SDimitry Andric /// Number of vector elements.
151fe6060f1SDimitry Andric Value *createStepVector(IRBuilder<> &Builder, Type *LaneTy,
152fe6060f1SDimitry Andric unsigned NumElems);
153fe6060f1SDimitry Andric
154fe6060f1SDimitry Andric /// \returns A bitmask that is true where the lane position is less-than \p
155fe6060f1SDimitry Andric /// EVLParam
156fe6060f1SDimitry Andric ///
157fe6060f1SDimitry Andric /// \p Builder
158fe6060f1SDimitry Andric /// Used for instruction creation.
159fe6060f1SDimitry Andric /// \p VLParam
160fe6060f1SDimitry Andric /// The explicit vector length parameter to test against the lane
161fe6060f1SDimitry Andric /// positions.
162fe6060f1SDimitry Andric /// \p ElemCount
163fe6060f1SDimitry Andric /// Static (potentially scalable) number of vector elements.
164fe6060f1SDimitry Andric Value *convertEVLToMask(IRBuilder<> &Builder, Value *EVLParam,
165fe6060f1SDimitry Andric ElementCount ElemCount);
166fe6060f1SDimitry Andric
167fe6060f1SDimitry Andric Value *foldEVLIntoMask(VPIntrinsic &VPI);
168fe6060f1SDimitry Andric
169fe6060f1SDimitry Andric /// "Remove" the %evl parameter of \p PI by setting it to the static vector
170fe6060f1SDimitry Andric /// length of the operation.
171fe6060f1SDimitry Andric void discardEVLParameter(VPIntrinsic &PI);
172fe6060f1SDimitry Andric
173bdd1243dSDimitry Andric /// Lower this VP binary operator to a unpredicated binary operator.
174fe6060f1SDimitry Andric Value *expandPredicationInBinaryOperator(IRBuilder<> &Builder,
175fe6060f1SDimitry Andric VPIntrinsic &PI);
176fe6060f1SDimitry Andric
177c9157d92SDimitry Andric /// Lower this VP int call to a unpredicated int call.
178c9157d92SDimitry Andric Value *expandPredicationToIntCall(IRBuilder<> &Builder, VPIntrinsic &PI,
179c9157d92SDimitry Andric unsigned UnpredicatedIntrinsicID);
180c9157d92SDimitry Andric
181fe013be4SDimitry Andric /// Lower this VP fp call to a unpredicated fp call.
182fe013be4SDimitry Andric Value *expandPredicationToFPCall(IRBuilder<> &Builder, VPIntrinsic &PI,
183fe013be4SDimitry Andric unsigned UnpredicatedIntrinsicID);
184fe013be4SDimitry Andric
185bdd1243dSDimitry Andric /// Lower this VP reduction to a call to an unpredicated reduction intrinsic.
186349cc55cSDimitry Andric Value *expandPredicationInReduction(IRBuilder<> &Builder,
187349cc55cSDimitry Andric VPReductionIntrinsic &PI);
188349cc55cSDimitry Andric
189c9157d92SDimitry Andric /// Lower this VP cast operation to a non-VP intrinsic.
190c9157d92SDimitry Andric Value *expandPredicationToCastIntrinsic(IRBuilder<> &Builder,
191c9157d92SDimitry Andric VPIntrinsic &VPI);
192c9157d92SDimitry Andric
193bdd1243dSDimitry Andric /// Lower this VP memory operation to a non-VP intrinsic.
194fcaf7f86SDimitry Andric Value *expandPredicationInMemoryIntrinsic(IRBuilder<> &Builder,
195fcaf7f86SDimitry Andric VPIntrinsic &VPI);
196fcaf7f86SDimitry Andric
197bdd1243dSDimitry Andric /// Lower this VP comparison to a call to an unpredicated comparison.
198bdd1243dSDimitry Andric Value *expandPredicationInComparison(IRBuilder<> &Builder,
199bdd1243dSDimitry Andric VPCmpIntrinsic &PI);
200bdd1243dSDimitry Andric
201bdd1243dSDimitry Andric /// Query TTI and expand the vector predication in \p P accordingly.
202fe6060f1SDimitry Andric Value *expandPredication(VPIntrinsic &PI);
203fe6060f1SDimitry Andric
204bdd1243dSDimitry Andric /// Determine how and whether the VPIntrinsic \p VPI shall be expanded. This
205bdd1243dSDimitry Andric /// overrides TTI with the cl::opts listed at the top of this file.
206fe6060f1SDimitry Andric VPLegalization getVPLegalizationStrategy(const VPIntrinsic &VPI) const;
207fe6060f1SDimitry Andric bool UsingTTIOverrides;
208fe6060f1SDimitry Andric
209fe6060f1SDimitry Andric public:
CachingVPExpander__anon78411b8f0111::CachingVPExpander210fe6060f1SDimitry Andric CachingVPExpander(Function &F, const TargetTransformInfo &TTI)
211fe6060f1SDimitry Andric : F(F), TTI(TTI), UsingTTIOverrides(anyExpandVPOverridesSet()) {}
212fe6060f1SDimitry Andric
213fe6060f1SDimitry Andric bool expandVectorPredication();
214fe6060f1SDimitry Andric };
215fe6060f1SDimitry Andric
216fe6060f1SDimitry Andric //// CachingVPExpander {
217fe6060f1SDimitry Andric
createStepVector(IRBuilder<> & Builder,Type * LaneTy,unsigned NumElems)218fe6060f1SDimitry Andric Value *CachingVPExpander::createStepVector(IRBuilder<> &Builder, Type *LaneTy,
219fe6060f1SDimitry Andric unsigned NumElems) {
220fe6060f1SDimitry Andric // TODO add caching
221fe6060f1SDimitry Andric SmallVector<Constant *, 16> ConstElems;
222fe6060f1SDimitry Andric
223fe6060f1SDimitry Andric for (unsigned Idx = 0; Idx < NumElems; ++Idx)
224fe6060f1SDimitry Andric ConstElems.push_back(ConstantInt::get(LaneTy, Idx, false));
225fe6060f1SDimitry Andric
226fe6060f1SDimitry Andric return ConstantVector::get(ConstElems);
227fe6060f1SDimitry Andric }
228fe6060f1SDimitry Andric
convertEVLToMask(IRBuilder<> & Builder,Value * EVLParam,ElementCount ElemCount)229fe6060f1SDimitry Andric Value *CachingVPExpander::convertEVLToMask(IRBuilder<> &Builder,
230fe6060f1SDimitry Andric Value *EVLParam,
231fe6060f1SDimitry Andric ElementCount ElemCount) {
232fe6060f1SDimitry Andric // TODO add caching
233fe6060f1SDimitry Andric // Scalable vector %evl conversion.
234fe6060f1SDimitry Andric if (ElemCount.isScalable()) {
235fe6060f1SDimitry Andric auto *M = Builder.GetInsertBlock()->getModule();
236fe6060f1SDimitry Andric Type *BoolVecTy = VectorType::get(Builder.getInt1Ty(), ElemCount);
237fe6060f1SDimitry Andric Function *ActiveMaskFunc = Intrinsic::getDeclaration(
238fe6060f1SDimitry Andric M, Intrinsic::get_active_lane_mask, {BoolVecTy, EVLParam->getType()});
239fe6060f1SDimitry Andric // `get_active_lane_mask` performs an implicit less-than comparison.
240fe6060f1SDimitry Andric Value *ConstZero = Builder.getInt32(0);
241fe6060f1SDimitry Andric return Builder.CreateCall(ActiveMaskFunc, {ConstZero, EVLParam});
242fe6060f1SDimitry Andric }
243fe6060f1SDimitry Andric
244fe6060f1SDimitry Andric // Fixed vector %evl conversion.
245fe6060f1SDimitry Andric Type *LaneTy = EVLParam->getType();
246fe6060f1SDimitry Andric unsigned NumElems = ElemCount.getFixedValue();
247fe6060f1SDimitry Andric Value *VLSplat = Builder.CreateVectorSplat(NumElems, EVLParam);
248fe6060f1SDimitry Andric Value *IdxVec = createStepVector(Builder, LaneTy, NumElems);
249fe6060f1SDimitry Andric return Builder.CreateICmp(CmpInst::ICMP_ULT, IdxVec, VLSplat);
250fe6060f1SDimitry Andric }
251fe6060f1SDimitry Andric
252fe6060f1SDimitry Andric Value *
expandPredicationInBinaryOperator(IRBuilder<> & Builder,VPIntrinsic & VPI)253fe6060f1SDimitry Andric CachingVPExpander::expandPredicationInBinaryOperator(IRBuilder<> &Builder,
254fe6060f1SDimitry Andric VPIntrinsic &VPI) {
25581ad6265SDimitry Andric assert((maySpeculateLanes(VPI) || VPI.canIgnoreVectorLengthParam()) &&
256fe6060f1SDimitry Andric "Implicitly dropping %evl in non-speculatable operator!");
257fe6060f1SDimitry Andric
258fe6060f1SDimitry Andric auto OC = static_cast<Instruction::BinaryOps>(*VPI.getFunctionalOpcode());
259fe6060f1SDimitry Andric assert(Instruction::isBinaryOp(OC));
260fe6060f1SDimitry Andric
261fe6060f1SDimitry Andric Value *Op0 = VPI.getOperand(0);
262fe6060f1SDimitry Andric Value *Op1 = VPI.getOperand(1);
263fe6060f1SDimitry Andric Value *Mask = VPI.getMaskParam();
264fe6060f1SDimitry Andric
265fe6060f1SDimitry Andric // Blend in safe operands.
266fe6060f1SDimitry Andric if (Mask && !isAllTrueMask(Mask)) {
267fe6060f1SDimitry Andric switch (OC) {
268fe6060f1SDimitry Andric default:
269fe6060f1SDimitry Andric // Can safely ignore the predicate.
270fe6060f1SDimitry Andric break;
271fe6060f1SDimitry Andric
272fe6060f1SDimitry Andric // Division operators need a safe divisor on masked-off lanes (1).
273fe6060f1SDimitry Andric case Instruction::UDiv:
274fe6060f1SDimitry Andric case Instruction::SDiv:
275fe6060f1SDimitry Andric case Instruction::URem:
276fe6060f1SDimitry Andric case Instruction::SRem:
277fe6060f1SDimitry Andric // 2nd operand must not be zero.
278fe6060f1SDimitry Andric Value *SafeDivisor = getSafeDivisor(VPI.getType());
279fe6060f1SDimitry Andric Op1 = Builder.CreateSelect(Mask, Op1, SafeDivisor);
280fe6060f1SDimitry Andric }
281fe6060f1SDimitry Andric }
282fe6060f1SDimitry Andric
283fe6060f1SDimitry Andric Value *NewBinOp = Builder.CreateBinOp(OC, Op0, Op1, VPI.getName());
284fe6060f1SDimitry Andric
285fe6060f1SDimitry Andric replaceOperation(*NewBinOp, VPI);
286fe6060f1SDimitry Andric return NewBinOp;
287fe6060f1SDimitry Andric }
288fe6060f1SDimitry Andric
expandPredicationToIntCall(IRBuilder<> & Builder,VPIntrinsic & VPI,unsigned UnpredicatedIntrinsicID)289c9157d92SDimitry Andric Value *CachingVPExpander::expandPredicationToIntCall(
290c9157d92SDimitry Andric IRBuilder<> &Builder, VPIntrinsic &VPI, unsigned UnpredicatedIntrinsicID) {
291c9157d92SDimitry Andric switch (UnpredicatedIntrinsicID) {
292c9157d92SDimitry Andric case Intrinsic::abs:
293c9157d92SDimitry Andric case Intrinsic::smax:
294c9157d92SDimitry Andric case Intrinsic::smin:
295c9157d92SDimitry Andric case Intrinsic::umax:
296c9157d92SDimitry Andric case Intrinsic::umin: {
297c9157d92SDimitry Andric Value *Op0 = VPI.getOperand(0);
298c9157d92SDimitry Andric Value *Op1 = VPI.getOperand(1);
299c9157d92SDimitry Andric Function *Fn = Intrinsic::getDeclaration(
300c9157d92SDimitry Andric VPI.getModule(), UnpredicatedIntrinsicID, {VPI.getType()});
301c9157d92SDimitry Andric Value *NewOp = Builder.CreateCall(Fn, {Op0, Op1}, VPI.getName());
302c9157d92SDimitry Andric replaceOperation(*NewOp, VPI);
303c9157d92SDimitry Andric return NewOp;
304c9157d92SDimitry Andric }
305c9157d92SDimitry Andric case Intrinsic::bswap:
306c9157d92SDimitry Andric case Intrinsic::bitreverse: {
307c9157d92SDimitry Andric Value *Op = VPI.getOperand(0);
308c9157d92SDimitry Andric Function *Fn = Intrinsic::getDeclaration(
309c9157d92SDimitry Andric VPI.getModule(), UnpredicatedIntrinsicID, {VPI.getType()});
310c9157d92SDimitry Andric Value *NewOp = Builder.CreateCall(Fn, {Op}, VPI.getName());
311c9157d92SDimitry Andric replaceOperation(*NewOp, VPI);
312c9157d92SDimitry Andric return NewOp;
313c9157d92SDimitry Andric }
314c9157d92SDimitry Andric }
315c9157d92SDimitry Andric return nullptr;
316c9157d92SDimitry Andric }
317c9157d92SDimitry Andric
expandPredicationToFPCall(IRBuilder<> & Builder,VPIntrinsic & VPI,unsigned UnpredicatedIntrinsicID)318fe013be4SDimitry Andric Value *CachingVPExpander::expandPredicationToFPCall(
319fe013be4SDimitry Andric IRBuilder<> &Builder, VPIntrinsic &VPI, unsigned UnpredicatedIntrinsicID) {
320fe013be4SDimitry Andric assert((maySpeculateLanes(VPI) || VPI.canIgnoreVectorLengthParam()) &&
321fe013be4SDimitry Andric "Implicitly dropping %evl in non-speculatable operator!");
322fe013be4SDimitry Andric
323fe013be4SDimitry Andric switch (UnpredicatedIntrinsicID) {
324fe013be4SDimitry Andric case Intrinsic::fabs:
325fe013be4SDimitry Andric case Intrinsic::sqrt: {
326fe013be4SDimitry Andric Value *Op0 = VPI.getOperand(0);
327fe013be4SDimitry Andric Function *Fn = Intrinsic::getDeclaration(
328fe013be4SDimitry Andric VPI.getModule(), UnpredicatedIntrinsicID, {VPI.getType()});
329fe013be4SDimitry Andric Value *NewOp = Builder.CreateCall(Fn, {Op0}, VPI.getName());
330fe013be4SDimitry Andric replaceOperation(*NewOp, VPI);
331fe013be4SDimitry Andric return NewOp;
332fe013be4SDimitry Andric }
333c9157d92SDimitry Andric case Intrinsic::maxnum:
334c9157d92SDimitry Andric case Intrinsic::minnum: {
335c9157d92SDimitry Andric Value *Op0 = VPI.getOperand(0);
336c9157d92SDimitry Andric Value *Op1 = VPI.getOperand(1);
337c9157d92SDimitry Andric Function *Fn = Intrinsic::getDeclaration(
338c9157d92SDimitry Andric VPI.getModule(), UnpredicatedIntrinsicID, {VPI.getType()});
339c9157d92SDimitry Andric Value *NewOp = Builder.CreateCall(Fn, {Op0, Op1}, VPI.getName());
340c9157d92SDimitry Andric replaceOperation(*NewOp, VPI);
341c9157d92SDimitry Andric return NewOp;
342c9157d92SDimitry Andric }
343fe013be4SDimitry Andric case Intrinsic::experimental_constrained_fma:
344fe013be4SDimitry Andric case Intrinsic::experimental_constrained_fmuladd: {
345fe013be4SDimitry Andric Value *Op0 = VPI.getOperand(0);
346fe013be4SDimitry Andric Value *Op1 = VPI.getOperand(1);
347fe013be4SDimitry Andric Value *Op2 = VPI.getOperand(2);
348fe013be4SDimitry Andric Function *Fn = Intrinsic::getDeclaration(
349fe013be4SDimitry Andric VPI.getModule(), UnpredicatedIntrinsicID, {VPI.getType()});
350fe013be4SDimitry Andric Value *NewOp =
351fe013be4SDimitry Andric Builder.CreateConstrainedFPCall(Fn, {Op0, Op1, Op2}, VPI.getName());
352fe013be4SDimitry Andric replaceOperation(*NewOp, VPI);
353fe013be4SDimitry Andric return NewOp;
354fe013be4SDimitry Andric }
355fe013be4SDimitry Andric }
356fe013be4SDimitry Andric
357fe013be4SDimitry Andric return nullptr;
358fe013be4SDimitry Andric }
359fe013be4SDimitry Andric
getNeutralReductionElement(const VPReductionIntrinsic & VPI,Type * EltTy)360349cc55cSDimitry Andric static Value *getNeutralReductionElement(const VPReductionIntrinsic &VPI,
361349cc55cSDimitry Andric Type *EltTy) {
362349cc55cSDimitry Andric bool Negative = false;
363349cc55cSDimitry Andric unsigned EltBits = EltTy->getScalarSizeInBits();
364349cc55cSDimitry Andric switch (VPI.getIntrinsicID()) {
365349cc55cSDimitry Andric default:
366349cc55cSDimitry Andric llvm_unreachable("Expecting a VP reduction intrinsic");
367349cc55cSDimitry Andric case Intrinsic::vp_reduce_add:
368349cc55cSDimitry Andric case Intrinsic::vp_reduce_or:
369349cc55cSDimitry Andric case Intrinsic::vp_reduce_xor:
370349cc55cSDimitry Andric case Intrinsic::vp_reduce_umax:
371349cc55cSDimitry Andric return Constant::getNullValue(EltTy);
372349cc55cSDimitry Andric case Intrinsic::vp_reduce_mul:
373349cc55cSDimitry Andric return ConstantInt::get(EltTy, 1, /*IsSigned*/ false);
374349cc55cSDimitry Andric case Intrinsic::vp_reduce_and:
375349cc55cSDimitry Andric case Intrinsic::vp_reduce_umin:
376349cc55cSDimitry Andric return ConstantInt::getAllOnesValue(EltTy);
377349cc55cSDimitry Andric case Intrinsic::vp_reduce_smin:
378349cc55cSDimitry Andric return ConstantInt::get(EltTy->getContext(),
379349cc55cSDimitry Andric APInt::getSignedMaxValue(EltBits));
380349cc55cSDimitry Andric case Intrinsic::vp_reduce_smax:
381349cc55cSDimitry Andric return ConstantInt::get(EltTy->getContext(),
382349cc55cSDimitry Andric APInt::getSignedMinValue(EltBits));
383349cc55cSDimitry Andric case Intrinsic::vp_reduce_fmax:
384349cc55cSDimitry Andric Negative = true;
385bdd1243dSDimitry Andric [[fallthrough]];
386349cc55cSDimitry Andric case Intrinsic::vp_reduce_fmin: {
387349cc55cSDimitry Andric FastMathFlags Flags = VPI.getFastMathFlags();
388349cc55cSDimitry Andric const fltSemantics &Semantics = EltTy->getFltSemantics();
389349cc55cSDimitry Andric return !Flags.noNaNs() ? ConstantFP::getQNaN(EltTy, Negative)
390349cc55cSDimitry Andric : !Flags.noInfs()
391349cc55cSDimitry Andric ? ConstantFP::getInfinity(EltTy, Negative)
392349cc55cSDimitry Andric : ConstantFP::get(EltTy,
393349cc55cSDimitry Andric APFloat::getLargest(Semantics, Negative));
394349cc55cSDimitry Andric }
395349cc55cSDimitry Andric case Intrinsic::vp_reduce_fadd:
396349cc55cSDimitry Andric return ConstantFP::getNegativeZero(EltTy);
397349cc55cSDimitry Andric case Intrinsic::vp_reduce_fmul:
398349cc55cSDimitry Andric return ConstantFP::get(EltTy, 1.0);
399349cc55cSDimitry Andric }
400349cc55cSDimitry Andric }
401349cc55cSDimitry Andric
402349cc55cSDimitry Andric Value *
expandPredicationInReduction(IRBuilder<> & Builder,VPReductionIntrinsic & VPI)403349cc55cSDimitry Andric CachingVPExpander::expandPredicationInReduction(IRBuilder<> &Builder,
404349cc55cSDimitry Andric VPReductionIntrinsic &VPI) {
40581ad6265SDimitry Andric assert((maySpeculateLanes(VPI) || VPI.canIgnoreVectorLengthParam()) &&
406349cc55cSDimitry Andric "Implicitly dropping %evl in non-speculatable operator!");
407349cc55cSDimitry Andric
408349cc55cSDimitry Andric Value *Mask = VPI.getMaskParam();
409349cc55cSDimitry Andric Value *RedOp = VPI.getOperand(VPI.getVectorParamPos());
410349cc55cSDimitry Andric
411349cc55cSDimitry Andric // Insert neutral element in masked-out positions
412349cc55cSDimitry Andric if (Mask && !isAllTrueMask(Mask)) {
413349cc55cSDimitry Andric auto *NeutralElt = getNeutralReductionElement(VPI, VPI.getType());
414349cc55cSDimitry Andric auto *NeutralVector = Builder.CreateVectorSplat(
415349cc55cSDimitry Andric cast<VectorType>(RedOp->getType())->getElementCount(), NeutralElt);
416349cc55cSDimitry Andric RedOp = Builder.CreateSelect(Mask, RedOp, NeutralVector);
417349cc55cSDimitry Andric }
418349cc55cSDimitry Andric
419349cc55cSDimitry Andric Value *Reduction;
420349cc55cSDimitry Andric Value *Start = VPI.getOperand(VPI.getStartParamPos());
421349cc55cSDimitry Andric
422349cc55cSDimitry Andric switch (VPI.getIntrinsicID()) {
423349cc55cSDimitry Andric default:
424349cc55cSDimitry Andric llvm_unreachable("Impossible reduction kind");
425349cc55cSDimitry Andric case Intrinsic::vp_reduce_add:
426349cc55cSDimitry Andric Reduction = Builder.CreateAddReduce(RedOp);
427349cc55cSDimitry Andric Reduction = Builder.CreateAdd(Reduction, Start);
428349cc55cSDimitry Andric break;
429349cc55cSDimitry Andric case Intrinsic::vp_reduce_mul:
430349cc55cSDimitry Andric Reduction = Builder.CreateMulReduce(RedOp);
431349cc55cSDimitry Andric Reduction = Builder.CreateMul(Reduction, Start);
432349cc55cSDimitry Andric break;
433349cc55cSDimitry Andric case Intrinsic::vp_reduce_and:
434349cc55cSDimitry Andric Reduction = Builder.CreateAndReduce(RedOp);
435349cc55cSDimitry Andric Reduction = Builder.CreateAnd(Reduction, Start);
436349cc55cSDimitry Andric break;
437349cc55cSDimitry Andric case Intrinsic::vp_reduce_or:
438349cc55cSDimitry Andric Reduction = Builder.CreateOrReduce(RedOp);
439349cc55cSDimitry Andric Reduction = Builder.CreateOr(Reduction, Start);
440349cc55cSDimitry Andric break;
441349cc55cSDimitry Andric case Intrinsic::vp_reduce_xor:
442349cc55cSDimitry Andric Reduction = Builder.CreateXorReduce(RedOp);
443349cc55cSDimitry Andric Reduction = Builder.CreateXor(Reduction, Start);
444349cc55cSDimitry Andric break;
445349cc55cSDimitry Andric case Intrinsic::vp_reduce_smax:
446349cc55cSDimitry Andric Reduction = Builder.CreateIntMaxReduce(RedOp, /*IsSigned*/ true);
447349cc55cSDimitry Andric Reduction =
448349cc55cSDimitry Andric Builder.CreateBinaryIntrinsic(Intrinsic::smax, Reduction, Start);
449349cc55cSDimitry Andric break;
450349cc55cSDimitry Andric case Intrinsic::vp_reduce_smin:
451349cc55cSDimitry Andric Reduction = Builder.CreateIntMinReduce(RedOp, /*IsSigned*/ true);
452349cc55cSDimitry Andric Reduction =
453349cc55cSDimitry Andric Builder.CreateBinaryIntrinsic(Intrinsic::smin, Reduction, Start);
454349cc55cSDimitry Andric break;
455349cc55cSDimitry Andric case Intrinsic::vp_reduce_umax:
456349cc55cSDimitry Andric Reduction = Builder.CreateIntMaxReduce(RedOp, /*IsSigned*/ false);
457349cc55cSDimitry Andric Reduction =
458349cc55cSDimitry Andric Builder.CreateBinaryIntrinsic(Intrinsic::umax, Reduction, Start);
459349cc55cSDimitry Andric break;
460349cc55cSDimitry Andric case Intrinsic::vp_reduce_umin:
461349cc55cSDimitry Andric Reduction = Builder.CreateIntMinReduce(RedOp, /*IsSigned*/ false);
462349cc55cSDimitry Andric Reduction =
463349cc55cSDimitry Andric Builder.CreateBinaryIntrinsic(Intrinsic::umin, Reduction, Start);
464349cc55cSDimitry Andric break;
465349cc55cSDimitry Andric case Intrinsic::vp_reduce_fmax:
466349cc55cSDimitry Andric Reduction = Builder.CreateFPMaxReduce(RedOp);
467349cc55cSDimitry Andric transferDecorations(*Reduction, VPI);
468349cc55cSDimitry Andric Reduction =
469349cc55cSDimitry Andric Builder.CreateBinaryIntrinsic(Intrinsic::maxnum, Reduction, Start);
470349cc55cSDimitry Andric break;
471349cc55cSDimitry Andric case Intrinsic::vp_reduce_fmin:
472349cc55cSDimitry Andric Reduction = Builder.CreateFPMinReduce(RedOp);
473349cc55cSDimitry Andric transferDecorations(*Reduction, VPI);
474349cc55cSDimitry Andric Reduction =
475349cc55cSDimitry Andric Builder.CreateBinaryIntrinsic(Intrinsic::minnum, Reduction, Start);
476349cc55cSDimitry Andric break;
477349cc55cSDimitry Andric case Intrinsic::vp_reduce_fadd:
478349cc55cSDimitry Andric Reduction = Builder.CreateFAddReduce(Start, RedOp);
479349cc55cSDimitry Andric break;
480349cc55cSDimitry Andric case Intrinsic::vp_reduce_fmul:
481349cc55cSDimitry Andric Reduction = Builder.CreateFMulReduce(Start, RedOp);
482349cc55cSDimitry Andric break;
483349cc55cSDimitry Andric }
484349cc55cSDimitry Andric
485349cc55cSDimitry Andric replaceOperation(*Reduction, VPI);
486349cc55cSDimitry Andric return Reduction;
487349cc55cSDimitry Andric }
488349cc55cSDimitry Andric
expandPredicationToCastIntrinsic(IRBuilder<> & Builder,VPIntrinsic & VPI)489c9157d92SDimitry Andric Value *CachingVPExpander::expandPredicationToCastIntrinsic(IRBuilder<> &Builder,
490c9157d92SDimitry Andric VPIntrinsic &VPI) {
491c9157d92SDimitry Andric Value *CastOp = nullptr;
492c9157d92SDimitry Andric switch (VPI.getIntrinsicID()) {
493c9157d92SDimitry Andric default:
494c9157d92SDimitry Andric llvm_unreachable("Not a VP cast intrinsic");
495c9157d92SDimitry Andric case Intrinsic::vp_sext:
496c9157d92SDimitry Andric CastOp =
497c9157d92SDimitry Andric Builder.CreateSExt(VPI.getOperand(0), VPI.getType(), VPI.getName());
498c9157d92SDimitry Andric break;
499c9157d92SDimitry Andric case Intrinsic::vp_zext:
500c9157d92SDimitry Andric CastOp =
501c9157d92SDimitry Andric Builder.CreateZExt(VPI.getOperand(0), VPI.getType(), VPI.getName());
502c9157d92SDimitry Andric break;
503c9157d92SDimitry Andric case Intrinsic::vp_trunc:
504c9157d92SDimitry Andric CastOp =
505c9157d92SDimitry Andric Builder.CreateTrunc(VPI.getOperand(0), VPI.getType(), VPI.getName());
506c9157d92SDimitry Andric break;
507c9157d92SDimitry Andric case Intrinsic::vp_inttoptr:
508c9157d92SDimitry Andric CastOp =
509c9157d92SDimitry Andric Builder.CreateIntToPtr(VPI.getOperand(0), VPI.getType(), VPI.getName());
510c9157d92SDimitry Andric break;
511c9157d92SDimitry Andric case Intrinsic::vp_ptrtoint:
512c9157d92SDimitry Andric CastOp =
513c9157d92SDimitry Andric Builder.CreatePtrToInt(VPI.getOperand(0), VPI.getType(), VPI.getName());
514c9157d92SDimitry Andric break;
515c9157d92SDimitry Andric case Intrinsic::vp_fptosi:
516c9157d92SDimitry Andric CastOp =
517c9157d92SDimitry Andric Builder.CreateFPToSI(VPI.getOperand(0), VPI.getType(), VPI.getName());
518c9157d92SDimitry Andric break;
519c9157d92SDimitry Andric
520c9157d92SDimitry Andric case Intrinsic::vp_fptoui:
521c9157d92SDimitry Andric CastOp =
522c9157d92SDimitry Andric Builder.CreateFPToUI(VPI.getOperand(0), VPI.getType(), VPI.getName());
523c9157d92SDimitry Andric break;
524c9157d92SDimitry Andric case Intrinsic::vp_sitofp:
525c9157d92SDimitry Andric CastOp =
526c9157d92SDimitry Andric Builder.CreateSIToFP(VPI.getOperand(0), VPI.getType(), VPI.getName());
527c9157d92SDimitry Andric break;
528c9157d92SDimitry Andric case Intrinsic::vp_uitofp:
529c9157d92SDimitry Andric CastOp =
530c9157d92SDimitry Andric Builder.CreateUIToFP(VPI.getOperand(0), VPI.getType(), VPI.getName());
531c9157d92SDimitry Andric break;
532c9157d92SDimitry Andric case Intrinsic::vp_fptrunc:
533c9157d92SDimitry Andric CastOp =
534c9157d92SDimitry Andric Builder.CreateFPTrunc(VPI.getOperand(0), VPI.getType(), VPI.getName());
535c9157d92SDimitry Andric break;
536c9157d92SDimitry Andric case Intrinsic::vp_fpext:
537c9157d92SDimitry Andric CastOp =
538c9157d92SDimitry Andric Builder.CreateFPExt(VPI.getOperand(0), VPI.getType(), VPI.getName());
539c9157d92SDimitry Andric break;
540c9157d92SDimitry Andric }
541c9157d92SDimitry Andric replaceOperation(*CastOp, VPI);
542c9157d92SDimitry Andric return CastOp;
543c9157d92SDimitry Andric }
544c9157d92SDimitry Andric
545fcaf7f86SDimitry Andric Value *
expandPredicationInMemoryIntrinsic(IRBuilder<> & Builder,VPIntrinsic & VPI)546fcaf7f86SDimitry Andric CachingVPExpander::expandPredicationInMemoryIntrinsic(IRBuilder<> &Builder,
547fcaf7f86SDimitry Andric VPIntrinsic &VPI) {
548fcaf7f86SDimitry Andric assert(VPI.canIgnoreVectorLengthParam());
549fcaf7f86SDimitry Andric
550fcaf7f86SDimitry Andric const auto &DL = F.getParent()->getDataLayout();
551fcaf7f86SDimitry Andric
552fcaf7f86SDimitry Andric Value *MaskParam = VPI.getMaskParam();
553fcaf7f86SDimitry Andric Value *PtrParam = VPI.getMemoryPointerParam();
554fcaf7f86SDimitry Andric Value *DataParam = VPI.getMemoryDataParam();
555fcaf7f86SDimitry Andric bool IsUnmasked = isAllTrueMask(MaskParam);
556fcaf7f86SDimitry Andric
557fcaf7f86SDimitry Andric MaybeAlign AlignOpt = VPI.getPointerAlignment();
558fcaf7f86SDimitry Andric
559fcaf7f86SDimitry Andric Value *NewMemoryInst = nullptr;
560fcaf7f86SDimitry Andric switch (VPI.getIntrinsicID()) {
561fcaf7f86SDimitry Andric default:
562fcaf7f86SDimitry Andric llvm_unreachable("Not a VP memory intrinsic");
563fcaf7f86SDimitry Andric case Intrinsic::vp_store:
564fcaf7f86SDimitry Andric if (IsUnmasked) {
565fcaf7f86SDimitry Andric StoreInst *NewStore =
566fcaf7f86SDimitry Andric Builder.CreateStore(DataParam, PtrParam, /*IsVolatile*/ false);
567fcaf7f86SDimitry Andric if (AlignOpt.has_value())
568bdd1243dSDimitry Andric NewStore->setAlignment(*AlignOpt);
569fcaf7f86SDimitry Andric NewMemoryInst = NewStore;
570fcaf7f86SDimitry Andric } else
571fcaf7f86SDimitry Andric NewMemoryInst = Builder.CreateMaskedStore(
572fcaf7f86SDimitry Andric DataParam, PtrParam, AlignOpt.valueOrOne(), MaskParam);
573fcaf7f86SDimitry Andric
574fcaf7f86SDimitry Andric break;
575fcaf7f86SDimitry Andric case Intrinsic::vp_load:
576fcaf7f86SDimitry Andric if (IsUnmasked) {
577fcaf7f86SDimitry Andric LoadInst *NewLoad =
578fcaf7f86SDimitry Andric Builder.CreateLoad(VPI.getType(), PtrParam, /*IsVolatile*/ false);
579fcaf7f86SDimitry Andric if (AlignOpt.has_value())
580bdd1243dSDimitry Andric NewLoad->setAlignment(*AlignOpt);
581fcaf7f86SDimitry Andric NewMemoryInst = NewLoad;
582fcaf7f86SDimitry Andric } else
583fcaf7f86SDimitry Andric NewMemoryInst = Builder.CreateMaskedLoad(
584fcaf7f86SDimitry Andric VPI.getType(), PtrParam, AlignOpt.valueOrOne(), MaskParam);
585fcaf7f86SDimitry Andric
586fcaf7f86SDimitry Andric break;
587fcaf7f86SDimitry Andric case Intrinsic::vp_scatter: {
588fcaf7f86SDimitry Andric auto *ElementType =
589fcaf7f86SDimitry Andric cast<VectorType>(DataParam->getType())->getElementType();
590fcaf7f86SDimitry Andric NewMemoryInst = Builder.CreateMaskedScatter(
591fcaf7f86SDimitry Andric DataParam, PtrParam,
592fcaf7f86SDimitry Andric AlignOpt.value_or(DL.getPrefTypeAlign(ElementType)), MaskParam);
593fcaf7f86SDimitry Andric break;
594fcaf7f86SDimitry Andric }
595fcaf7f86SDimitry Andric case Intrinsic::vp_gather: {
596fcaf7f86SDimitry Andric auto *ElementType = cast<VectorType>(VPI.getType())->getElementType();
597fcaf7f86SDimitry Andric NewMemoryInst = Builder.CreateMaskedGather(
598fcaf7f86SDimitry Andric VPI.getType(), PtrParam,
599fcaf7f86SDimitry Andric AlignOpt.value_or(DL.getPrefTypeAlign(ElementType)), MaskParam, nullptr,
600fcaf7f86SDimitry Andric VPI.getName());
601fcaf7f86SDimitry Andric break;
602fcaf7f86SDimitry Andric }
603fcaf7f86SDimitry Andric }
604fcaf7f86SDimitry Andric
605fcaf7f86SDimitry Andric assert(NewMemoryInst);
606fcaf7f86SDimitry Andric replaceOperation(*NewMemoryInst, VPI);
607fcaf7f86SDimitry Andric return NewMemoryInst;
608fcaf7f86SDimitry Andric }
609fcaf7f86SDimitry Andric
expandPredicationInComparison(IRBuilder<> & Builder,VPCmpIntrinsic & VPI)610bdd1243dSDimitry Andric Value *CachingVPExpander::expandPredicationInComparison(IRBuilder<> &Builder,
611bdd1243dSDimitry Andric VPCmpIntrinsic &VPI) {
612bdd1243dSDimitry Andric assert((maySpeculateLanes(VPI) || VPI.canIgnoreVectorLengthParam()) &&
613bdd1243dSDimitry Andric "Implicitly dropping %evl in non-speculatable operator!");
614bdd1243dSDimitry Andric
615bdd1243dSDimitry Andric assert(*VPI.getFunctionalOpcode() == Instruction::ICmp ||
616bdd1243dSDimitry Andric *VPI.getFunctionalOpcode() == Instruction::FCmp);
617bdd1243dSDimitry Andric
618bdd1243dSDimitry Andric Value *Op0 = VPI.getOperand(0);
619bdd1243dSDimitry Andric Value *Op1 = VPI.getOperand(1);
620bdd1243dSDimitry Andric auto Pred = VPI.getPredicate();
621bdd1243dSDimitry Andric
622bdd1243dSDimitry Andric auto *NewCmp = Builder.CreateCmp(Pred, Op0, Op1);
623bdd1243dSDimitry Andric
624bdd1243dSDimitry Andric replaceOperation(*NewCmp, VPI);
625bdd1243dSDimitry Andric return NewCmp;
626bdd1243dSDimitry Andric }
627bdd1243dSDimitry Andric
discardEVLParameter(VPIntrinsic & VPI)628fe6060f1SDimitry Andric void CachingVPExpander::discardEVLParameter(VPIntrinsic &VPI) {
629fe6060f1SDimitry Andric LLVM_DEBUG(dbgs() << "Discard EVL parameter in " << VPI << "\n");
630fe6060f1SDimitry Andric
631fe6060f1SDimitry Andric if (VPI.canIgnoreVectorLengthParam())
632fe6060f1SDimitry Andric return;
633fe6060f1SDimitry Andric
634fe6060f1SDimitry Andric Value *EVLParam = VPI.getVectorLengthParam();
635fe6060f1SDimitry Andric if (!EVLParam)
636fe6060f1SDimitry Andric return;
637fe6060f1SDimitry Andric
638fe6060f1SDimitry Andric ElementCount StaticElemCount = VPI.getStaticVectorLength();
639fe6060f1SDimitry Andric Value *MaxEVL = nullptr;
640fe6060f1SDimitry Andric Type *Int32Ty = Type::getInt32Ty(VPI.getContext());
641fe6060f1SDimitry Andric if (StaticElemCount.isScalable()) {
642fe6060f1SDimitry Andric // TODO add caching
643fe6060f1SDimitry Andric auto *M = VPI.getModule();
644fe6060f1SDimitry Andric Function *VScaleFunc =
645fe6060f1SDimitry Andric Intrinsic::getDeclaration(M, Intrinsic::vscale, Int32Ty);
646fe6060f1SDimitry Andric IRBuilder<> Builder(VPI.getParent(), VPI.getIterator());
647fe6060f1SDimitry Andric Value *FactorConst = Builder.getInt32(StaticElemCount.getKnownMinValue());
648fe6060f1SDimitry Andric Value *VScale = Builder.CreateCall(VScaleFunc, {}, "vscale");
649fe6060f1SDimitry Andric MaxEVL = Builder.CreateMul(VScale, FactorConst, "scalable_size",
650fe6060f1SDimitry Andric /*NUW*/ true, /*NSW*/ false);
651fe6060f1SDimitry Andric } else {
652fe6060f1SDimitry Andric MaxEVL = ConstantInt::get(Int32Ty, StaticElemCount.getFixedValue(), false);
653fe6060f1SDimitry Andric }
654fe6060f1SDimitry Andric VPI.setVectorLengthParam(MaxEVL);
655fe6060f1SDimitry Andric }
656fe6060f1SDimitry Andric
foldEVLIntoMask(VPIntrinsic & VPI)657fe6060f1SDimitry Andric Value *CachingVPExpander::foldEVLIntoMask(VPIntrinsic &VPI) {
658fe6060f1SDimitry Andric LLVM_DEBUG(dbgs() << "Folding vlen for " << VPI << '\n');
659fe6060f1SDimitry Andric
660fe6060f1SDimitry Andric IRBuilder<> Builder(&VPI);
661fe6060f1SDimitry Andric
662fe6060f1SDimitry Andric // Ineffective %evl parameter and so nothing to do here.
663fe6060f1SDimitry Andric if (VPI.canIgnoreVectorLengthParam())
664fe6060f1SDimitry Andric return &VPI;
665fe6060f1SDimitry Andric
666fe6060f1SDimitry Andric // Only VP intrinsics can have an %evl parameter.
667fe6060f1SDimitry Andric Value *OldMaskParam = VPI.getMaskParam();
668fe6060f1SDimitry Andric Value *OldEVLParam = VPI.getVectorLengthParam();
669fe6060f1SDimitry Andric assert(OldMaskParam && "no mask param to fold the vl param into");
670fe6060f1SDimitry Andric assert(OldEVLParam && "no EVL param to fold away");
671fe6060f1SDimitry Andric
672fe6060f1SDimitry Andric LLVM_DEBUG(dbgs() << "OLD evl: " << *OldEVLParam << '\n');
673fe6060f1SDimitry Andric LLVM_DEBUG(dbgs() << "OLD mask: " << *OldMaskParam << '\n');
674fe6060f1SDimitry Andric
675fe6060f1SDimitry Andric // Convert the %evl predication into vector mask predication.
676fe6060f1SDimitry Andric ElementCount ElemCount = VPI.getStaticVectorLength();
677fe6060f1SDimitry Andric Value *VLMask = convertEVLToMask(Builder, OldEVLParam, ElemCount);
678fe6060f1SDimitry Andric Value *NewMaskParam = Builder.CreateAnd(VLMask, OldMaskParam);
679fe6060f1SDimitry Andric VPI.setMaskParam(NewMaskParam);
680fe6060f1SDimitry Andric
681fe6060f1SDimitry Andric // Drop the %evl parameter.
682fe6060f1SDimitry Andric discardEVLParameter(VPI);
683fe6060f1SDimitry Andric assert(VPI.canIgnoreVectorLengthParam() &&
684fe6060f1SDimitry Andric "transformation did not render the evl param ineffective!");
685fe6060f1SDimitry Andric
686fe6060f1SDimitry Andric // Reassess the modified instruction.
687fe6060f1SDimitry Andric return &VPI;
688fe6060f1SDimitry Andric }
689fe6060f1SDimitry Andric
expandPredication(VPIntrinsic & VPI)690fe6060f1SDimitry Andric Value *CachingVPExpander::expandPredication(VPIntrinsic &VPI) {
691fe6060f1SDimitry Andric LLVM_DEBUG(dbgs() << "Lowering to unpredicated op: " << VPI << '\n');
692fe6060f1SDimitry Andric
693fe6060f1SDimitry Andric IRBuilder<> Builder(&VPI);
694fe6060f1SDimitry Andric
695fe6060f1SDimitry Andric // Try lowering to a LLVM instruction first.
696fe6060f1SDimitry Andric auto OC = VPI.getFunctionalOpcode();
697fe6060f1SDimitry Andric
698fe6060f1SDimitry Andric if (OC && Instruction::isBinaryOp(*OC))
699fe6060f1SDimitry Andric return expandPredicationInBinaryOperator(Builder, VPI);
700fe6060f1SDimitry Andric
701349cc55cSDimitry Andric if (auto *VPRI = dyn_cast<VPReductionIntrinsic>(&VPI))
702349cc55cSDimitry Andric return expandPredicationInReduction(Builder, *VPRI);
703349cc55cSDimitry Andric
704bdd1243dSDimitry Andric if (auto *VPCmp = dyn_cast<VPCmpIntrinsic>(&VPI))
705bdd1243dSDimitry Andric return expandPredicationInComparison(Builder, *VPCmp);
706bdd1243dSDimitry Andric
707c9157d92SDimitry Andric if (VPCastIntrinsic::isVPCast(VPI.getIntrinsicID())) {
708c9157d92SDimitry Andric return expandPredicationToCastIntrinsic(Builder, VPI);
709c9157d92SDimitry Andric }
710c9157d92SDimitry Andric
711fcaf7f86SDimitry Andric switch (VPI.getIntrinsicID()) {
712fcaf7f86SDimitry Andric default:
713fcaf7f86SDimitry Andric break;
714fe013be4SDimitry Andric case Intrinsic::vp_fneg: {
715fe013be4SDimitry Andric Value *NewNegOp = Builder.CreateFNeg(VPI.getOperand(0), VPI.getName());
716fe013be4SDimitry Andric replaceOperation(*NewNegOp, VPI);
717fe013be4SDimitry Andric return NewNegOp;
718fe013be4SDimitry Andric }
719c9157d92SDimitry Andric case Intrinsic::vp_abs:
720c9157d92SDimitry Andric case Intrinsic::vp_smax:
721c9157d92SDimitry Andric case Intrinsic::vp_smin:
722c9157d92SDimitry Andric case Intrinsic::vp_umax:
723c9157d92SDimitry Andric case Intrinsic::vp_umin:
724c9157d92SDimitry Andric case Intrinsic::vp_bswap:
725c9157d92SDimitry Andric case Intrinsic::vp_bitreverse:
726c9157d92SDimitry Andric return expandPredicationToIntCall(Builder, VPI,
727c9157d92SDimitry Andric VPI.getFunctionalIntrinsicID().value());
728fe013be4SDimitry Andric case Intrinsic::vp_fabs:
729fe013be4SDimitry Andric case Intrinsic::vp_sqrt:
730c9157d92SDimitry Andric case Intrinsic::vp_maxnum:
731c9157d92SDimitry Andric case Intrinsic::vp_minnum:
732*a58f00eaSDimitry Andric case Intrinsic::vp_maximum:
733*a58f00eaSDimitry Andric case Intrinsic::vp_minimum:
734c9157d92SDimitry Andric return expandPredicationToFPCall(Builder, VPI,
735c9157d92SDimitry Andric VPI.getFunctionalIntrinsicID().value());
736fcaf7f86SDimitry Andric case Intrinsic::vp_load:
737fcaf7f86SDimitry Andric case Intrinsic::vp_store:
738fcaf7f86SDimitry Andric case Intrinsic::vp_gather:
739fcaf7f86SDimitry Andric case Intrinsic::vp_scatter:
740fcaf7f86SDimitry Andric return expandPredicationInMemoryIntrinsic(Builder, VPI);
741fcaf7f86SDimitry Andric }
742fcaf7f86SDimitry Andric
743fe013be4SDimitry Andric if (auto CID = VPI.getConstrainedIntrinsicID())
744fe013be4SDimitry Andric if (Value *Call = expandPredicationToFPCall(Builder, VPI, *CID))
745fe013be4SDimitry Andric return Call;
746fe013be4SDimitry Andric
747fe6060f1SDimitry Andric return &VPI;
748fe6060f1SDimitry Andric }
749fe6060f1SDimitry Andric
750fe6060f1SDimitry Andric //// } CachingVPExpander
751fe6060f1SDimitry Andric
752fe6060f1SDimitry Andric struct TransformJob {
753fe6060f1SDimitry Andric VPIntrinsic *PI;
754fe6060f1SDimitry Andric TargetTransformInfo::VPLegalization Strategy;
TransformJob__anon78411b8f0111::TransformJob755fe6060f1SDimitry Andric TransformJob(VPIntrinsic *PI, TargetTransformInfo::VPLegalization InitStrat)
756fe6060f1SDimitry Andric : PI(PI), Strategy(InitStrat) {}
757fe6060f1SDimitry Andric
isDone__anon78411b8f0111::TransformJob758fe6060f1SDimitry Andric bool isDone() const { return Strategy.shouldDoNothing(); }
759fe6060f1SDimitry Andric };
760fe6060f1SDimitry Andric
sanitizeStrategy(VPIntrinsic & VPI,VPLegalization & LegalizeStrat)76181ad6265SDimitry Andric void sanitizeStrategy(VPIntrinsic &VPI, VPLegalization &LegalizeStrat) {
76281ad6265SDimitry Andric // Operations with speculatable lanes do not strictly need predication.
76381ad6265SDimitry Andric if (maySpeculateLanes(VPI)) {
764fe6060f1SDimitry Andric // Converting a speculatable VP intrinsic means dropping %mask and %evl.
765fe6060f1SDimitry Andric // No need to expand %evl into the %mask only to ignore that code.
766fe6060f1SDimitry Andric if (LegalizeStrat.OpStrategy == VPLegalization::Convert)
767fe6060f1SDimitry Andric LegalizeStrat.EVLParamStrategy = VPLegalization::Discard;
768fe6060f1SDimitry Andric return;
769fe6060f1SDimitry Andric }
770fe6060f1SDimitry Andric
771fe6060f1SDimitry Andric // We have to preserve the predicating effect of %evl for this
772fe6060f1SDimitry Andric // non-speculatable VP intrinsic.
773fe6060f1SDimitry Andric // 1) Never discard %evl.
774fe6060f1SDimitry Andric // 2) If this VP intrinsic will be expanded to non-VP code, make sure that
775fe6060f1SDimitry Andric // %evl gets folded into %mask.
776fe6060f1SDimitry Andric if ((LegalizeStrat.EVLParamStrategy == VPLegalization::Discard) ||
777fe6060f1SDimitry Andric (LegalizeStrat.OpStrategy == VPLegalization::Convert)) {
778fe6060f1SDimitry Andric LegalizeStrat.EVLParamStrategy = VPLegalization::Convert;
779fe6060f1SDimitry Andric }
780fe6060f1SDimitry Andric }
781fe6060f1SDimitry Andric
782fe6060f1SDimitry Andric VPLegalization
getVPLegalizationStrategy(const VPIntrinsic & VPI) const783fe6060f1SDimitry Andric CachingVPExpander::getVPLegalizationStrategy(const VPIntrinsic &VPI) const {
784fe6060f1SDimitry Andric auto VPStrat = TTI.getVPLegalizationStrategy(VPI);
785fe6060f1SDimitry Andric if (LLVM_LIKELY(!UsingTTIOverrides)) {
786fe6060f1SDimitry Andric // No overrides - we are in production.
787fe6060f1SDimitry Andric return VPStrat;
788fe6060f1SDimitry Andric }
789fe6060f1SDimitry Andric
790fe6060f1SDimitry Andric // Overrides set - we are in testing, the following does not need to be
791fe6060f1SDimitry Andric // efficient.
792fe6060f1SDimitry Andric VPStrat.EVLParamStrategy = parseOverrideOption(EVLTransformOverride);
793fe6060f1SDimitry Andric VPStrat.OpStrategy = parseOverrideOption(MaskTransformOverride);
794fe6060f1SDimitry Andric return VPStrat;
795fe6060f1SDimitry Andric }
796fe6060f1SDimitry Andric
797bdd1243dSDimitry Andric /// Expand llvm.vp.* intrinsics as requested by \p TTI.
expandVectorPredication()798fe6060f1SDimitry Andric bool CachingVPExpander::expandVectorPredication() {
799fe6060f1SDimitry Andric SmallVector<TransformJob, 16> Worklist;
800fe6060f1SDimitry Andric
801fe6060f1SDimitry Andric // Collect all VPIntrinsics that need expansion and determine their expansion
802fe6060f1SDimitry Andric // strategy.
803fe6060f1SDimitry Andric for (auto &I : instructions(F)) {
804fe6060f1SDimitry Andric auto *VPI = dyn_cast<VPIntrinsic>(&I);
805fe6060f1SDimitry Andric if (!VPI)
806fe6060f1SDimitry Andric continue;
807fe6060f1SDimitry Andric auto VPStrat = getVPLegalizationStrategy(*VPI);
80881ad6265SDimitry Andric sanitizeStrategy(*VPI, VPStrat);
809fe6060f1SDimitry Andric if (!VPStrat.shouldDoNothing())
810fe6060f1SDimitry Andric Worklist.emplace_back(VPI, VPStrat);
811fe6060f1SDimitry Andric }
812fe6060f1SDimitry Andric if (Worklist.empty())
813fe6060f1SDimitry Andric return false;
814fe6060f1SDimitry Andric
815fe6060f1SDimitry Andric // Transform all VPIntrinsics on the worklist.
816fe6060f1SDimitry Andric LLVM_DEBUG(dbgs() << "\n:::: Transforming " << Worklist.size()
817fe6060f1SDimitry Andric << " instructions ::::\n");
818fe6060f1SDimitry Andric for (TransformJob Job : Worklist) {
819fe6060f1SDimitry Andric // Transform the EVL parameter.
820fe6060f1SDimitry Andric switch (Job.Strategy.EVLParamStrategy) {
821fe6060f1SDimitry Andric case VPLegalization::Legal:
822fe6060f1SDimitry Andric break;
823fe6060f1SDimitry Andric case VPLegalization::Discard:
824fe6060f1SDimitry Andric discardEVLParameter(*Job.PI);
825fe6060f1SDimitry Andric break;
826fe6060f1SDimitry Andric case VPLegalization::Convert:
827fe6060f1SDimitry Andric if (foldEVLIntoMask(*Job.PI))
828fe6060f1SDimitry Andric ++NumFoldedVL;
829fe6060f1SDimitry Andric break;
830fe6060f1SDimitry Andric }
831fe6060f1SDimitry Andric Job.Strategy.EVLParamStrategy = VPLegalization::Legal;
832fe6060f1SDimitry Andric
833fe6060f1SDimitry Andric // Replace with a non-predicated operation.
834fe6060f1SDimitry Andric switch (Job.Strategy.OpStrategy) {
835fe6060f1SDimitry Andric case VPLegalization::Legal:
836fe6060f1SDimitry Andric break;
837fe6060f1SDimitry Andric case VPLegalization::Discard:
838fe6060f1SDimitry Andric llvm_unreachable("Invalid strategy for operators.");
839fe6060f1SDimitry Andric case VPLegalization::Convert:
840fe6060f1SDimitry Andric expandPredication(*Job.PI);
841fe6060f1SDimitry Andric ++NumLoweredVPOps;
842fe6060f1SDimitry Andric break;
843fe6060f1SDimitry Andric }
844fe6060f1SDimitry Andric Job.Strategy.OpStrategy = VPLegalization::Legal;
845fe6060f1SDimitry Andric
846fe6060f1SDimitry Andric assert(Job.isDone() && "incomplete transformation");
847fe6060f1SDimitry Andric }
848fe6060f1SDimitry Andric
849fe6060f1SDimitry Andric return true;
850fe6060f1SDimitry Andric }
851fe6060f1SDimitry Andric class ExpandVectorPredication : public FunctionPass {
852fe6060f1SDimitry Andric public:
853fe6060f1SDimitry Andric static char ID;
ExpandVectorPredication()854fe6060f1SDimitry Andric ExpandVectorPredication() : FunctionPass(ID) {
855fe6060f1SDimitry Andric initializeExpandVectorPredicationPass(*PassRegistry::getPassRegistry());
856fe6060f1SDimitry Andric }
857fe6060f1SDimitry Andric
runOnFunction(Function & F)858fe6060f1SDimitry Andric bool runOnFunction(Function &F) override {
859fe6060f1SDimitry Andric const auto *TTI = &getAnalysis<TargetTransformInfoWrapperPass>().getTTI(F);
860fe6060f1SDimitry Andric CachingVPExpander VPExpander(F, *TTI);
861fe6060f1SDimitry Andric return VPExpander.expandVectorPredication();
862fe6060f1SDimitry Andric }
863fe6060f1SDimitry Andric
getAnalysisUsage(AnalysisUsage & AU) const864fe6060f1SDimitry Andric void getAnalysisUsage(AnalysisUsage &AU) const override {
865fe6060f1SDimitry Andric AU.addRequired<TargetTransformInfoWrapperPass>();
866fe6060f1SDimitry Andric AU.setPreservesCFG();
867fe6060f1SDimitry Andric }
868fe6060f1SDimitry Andric };
869fe6060f1SDimitry Andric } // namespace
870fe6060f1SDimitry Andric
871fe6060f1SDimitry Andric char ExpandVectorPredication::ID;
872fe6060f1SDimitry Andric INITIALIZE_PASS_BEGIN(ExpandVectorPredication, "expandvp",
873fe6060f1SDimitry Andric "Expand vector predication intrinsics", false, false)
INITIALIZE_PASS_DEPENDENCY(TargetTransformInfoWrapperPass)874fe6060f1SDimitry Andric INITIALIZE_PASS_DEPENDENCY(TargetTransformInfoWrapperPass)
875fe6060f1SDimitry Andric INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
876fe6060f1SDimitry Andric INITIALIZE_PASS_END(ExpandVectorPredication, "expandvp",
877fe6060f1SDimitry Andric "Expand vector predication intrinsics", false, false)
878fe6060f1SDimitry Andric
879fe6060f1SDimitry Andric FunctionPass *llvm::createExpandVectorPredicationPass() {
880fe6060f1SDimitry Andric return new ExpandVectorPredication();
881fe6060f1SDimitry Andric }
882fe6060f1SDimitry Andric
883fe6060f1SDimitry Andric PreservedAnalyses
run(Function & F,FunctionAnalysisManager & AM)884fe6060f1SDimitry Andric ExpandVectorPredicationPass::run(Function &F, FunctionAnalysisManager &AM) {
885fe6060f1SDimitry Andric const auto &TTI = AM.getResult<TargetIRAnalysis>(F);
886fe6060f1SDimitry Andric CachingVPExpander VPExpander(F, TTI);
887fe6060f1SDimitry Andric if (!VPExpander.expandVectorPredication())
888fe6060f1SDimitry Andric return PreservedAnalyses::all();
889fe6060f1SDimitry Andric PreservedAnalyses PA;
890fe6060f1SDimitry Andric PA.preserveSet<CFGAnalyses>();
891fe6060f1SDimitry Andric return PA;
892fe6060f1SDimitry Andric }
893