1 //===----- CodeGen/ExpandVectorPredication.cpp - Expand VP intrinsics -----===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This pass implements IR expansion for vector predication intrinsics, allowing
10 // targets to enable vector predication until just before codegen.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "llvm/CodeGen/ExpandVectorPredication.h"
15 #include "llvm/ADT/Statistic.h"
16 #include "llvm/Analysis/TargetTransformInfo.h"
17 #include "llvm/Analysis/ValueTracking.h"
18 #include "llvm/Analysis/VectorUtils.h"
19 #include "llvm/CodeGen/Passes.h"
20 #include "llvm/IR/Constants.h"
21 #include "llvm/IR/Function.h"
22 #include "llvm/IR/IRBuilder.h"
23 #include "llvm/IR/InstIterator.h"
24 #include "llvm/IR/Instructions.h"
25 #include "llvm/IR/IntrinsicInst.h"
26 #include "llvm/IR/Intrinsics.h"
27 #include "llvm/InitializePasses.h"
28 #include "llvm/Pass.h"
29 #include "llvm/Support/CommandLine.h"
30 #include "llvm/Support/Compiler.h"
31 #include "llvm/Support/Debug.h"
32 
33 using namespace llvm;
34 
35 using VPLegalization = TargetTransformInfo::VPLegalization;
36 using VPTransform = TargetTransformInfo::VPLegalization::VPTransform;
37 
38 // Keep this in sync with TargetTransformInfo::VPLegalization.
39 #define VPINTERNAL_VPLEGAL_CASES                                               \
40   VPINTERNAL_CASE(Legal)                                                       \
41   VPINTERNAL_CASE(Discard)                                                     \
42   VPINTERNAL_CASE(Convert)
43 
44 #define VPINTERNAL_CASE(X) "|" #X
45 
46 // Override options.
47 static cl::opt<std::string> EVLTransformOverride(
48     "expandvp-override-evl-transform", cl::init(""), cl::Hidden,
49     cl::desc("Options: <empty>" VPINTERNAL_VPLEGAL_CASES
50              ". If non-empty, ignore "
51              "TargetTransformInfo and "
52              "always use this transformation for the %evl parameter (Used in "
53              "testing)."));
54 
55 static cl::opt<std::string> MaskTransformOverride(
56     "expandvp-override-mask-transform", cl::init(""), cl::Hidden,
57     cl::desc("Options: <empty>" VPINTERNAL_VPLEGAL_CASES
58              ". If non-empty, Ignore "
59              "TargetTransformInfo and "
60              "always use this transformation for the %mask parameter (Used in "
61              "testing)."));
62 
63 #undef VPINTERNAL_CASE
64 #define VPINTERNAL_CASE(X) .Case(#X, VPLegalization::X)
65 
66 static VPTransform parseOverrideOption(const std::string &TextOpt) {
67   return StringSwitch<VPTransform>(TextOpt) VPINTERNAL_VPLEGAL_CASES;
68 }
69 
70 #undef VPINTERNAL_VPLEGAL_CASES
71 
72 // Whether any override options are set.
73 static bool anyExpandVPOverridesSet() {
74   return !EVLTransformOverride.empty() || !MaskTransformOverride.empty();
75 }
76 
77 #define DEBUG_TYPE "expandvp"
78 
79 STATISTIC(NumFoldedVL, "Number of folded vector length params");
80 STATISTIC(NumLoweredVPOps, "Number of folded vector predication operations");
81 
82 ///// Helpers {
83 
84 /// \returns Whether the vector mask \p MaskVal has all lane bits set.
85 static bool isAllTrueMask(Value *MaskVal) {
86   if (Value *SplattedVal = getSplatValue(MaskVal))
87     if (auto *ConstValue = dyn_cast<Constant>(SplattedVal))
88       return ConstValue->isAllOnesValue();
89 
90   return false;
91 }
92 
93 /// \returns A non-excepting divisor constant for this type.
94 static Constant *getSafeDivisor(Type *DivTy) {
95   assert(DivTy->isIntOrIntVectorTy() && "Unsupported divisor type");
96   return ConstantInt::get(DivTy, 1u, false);
97 }
98 
99 /// Transfer operation properties from \p OldVPI to \p NewVal.
100 static void transferDecorations(Value &NewVal, VPIntrinsic &VPI) {
101   auto *NewInst = dyn_cast<Instruction>(&NewVal);
102   if (!NewInst || !isa<FPMathOperator>(NewVal))
103     return;
104 
105   auto *OldFMOp = dyn_cast<FPMathOperator>(&VPI);
106   if (!OldFMOp)
107     return;
108 
109   NewInst->setFastMathFlags(OldFMOp->getFastMathFlags());
110 }
111 
112 /// Transfer all properties from \p OldOp to \p NewOp and replace all uses.
113 /// OldVP gets erased.
114 static void replaceOperation(Value &NewOp, VPIntrinsic &OldOp) {
115   transferDecorations(NewOp, OldOp);
116   OldOp.replaceAllUsesWith(&NewOp);
117   OldOp.eraseFromParent();
118 }
119 
120 static bool maySpeculateLanes(VPIntrinsic &VPI) {
121   // The result of VP reductions depends on the mask and evl.
122   if (isa<VPReductionIntrinsic>(VPI))
123     return false;
124   // Fallback to whether the intrinsic is speculatable.
125   Optional<unsigned> OpcOpt = VPI.getFunctionalOpcode();
126   unsigned FunctionalOpc = OpcOpt.value_or((unsigned)Instruction::Call);
127   return isSafeToSpeculativelyExecuteWithOpcode(FunctionalOpc, &VPI);
128 }
129 
130 //// } Helpers
131 
132 namespace {
133 
134 // Expansion pass state at function scope.
135 struct CachingVPExpander {
136   Function &F;
137   const TargetTransformInfo &TTI;
138 
139   /// \returns A (fixed length) vector with ascending integer indices
140   /// (<0, 1, ..., NumElems-1>).
141   /// \p Builder
142   ///    Used for instruction creation.
143   /// \p LaneTy
144   ///    Integer element type of the result vector.
145   /// \p NumElems
146   ///    Number of vector elements.
147   Value *createStepVector(IRBuilder<> &Builder, Type *LaneTy,
148                           unsigned NumElems);
149 
150   /// \returns A bitmask that is true where the lane position is less-than \p
151   /// EVLParam
152   ///
153   /// \p Builder
154   ///    Used for instruction creation.
155   /// \p VLParam
156   ///    The explicit vector length parameter to test against the lane
157   ///    positions.
158   /// \p ElemCount
159   ///    Static (potentially scalable) number of vector elements.
160   Value *convertEVLToMask(IRBuilder<> &Builder, Value *EVLParam,
161                           ElementCount ElemCount);
162 
163   Value *foldEVLIntoMask(VPIntrinsic &VPI);
164 
165   /// "Remove" the %evl parameter of \p PI by setting it to the static vector
166   /// length of the operation.
167   void discardEVLParameter(VPIntrinsic &PI);
168 
169   /// \brief Lower this VP binary operator to a unpredicated binary operator.
170   Value *expandPredicationInBinaryOperator(IRBuilder<> &Builder,
171                                            VPIntrinsic &PI);
172 
173   /// \brief Lower this VP reduction to a call to an unpredicated reduction
174   /// intrinsic.
175   Value *expandPredicationInReduction(IRBuilder<> &Builder,
176                                       VPReductionIntrinsic &PI);
177 
178   /// \brief Lower this VP memory operation to a non-VP intrinsic.
179   Value *expandPredicationInMemoryIntrinsic(IRBuilder<> &Builder,
180                                             VPIntrinsic &VPI);
181 
182   /// \brief Query TTI and expand the vector predication in \p P accordingly.
183   Value *expandPredication(VPIntrinsic &PI);
184 
185   /// \brief  Determine how and whether the VPIntrinsic \p VPI shall be
186   /// expanded. This overrides TTI with the cl::opts listed at the top of this
187   /// file.
188   VPLegalization getVPLegalizationStrategy(const VPIntrinsic &VPI) const;
189   bool UsingTTIOverrides;
190 
191 public:
192   CachingVPExpander(Function &F, const TargetTransformInfo &TTI)
193       : F(F), TTI(TTI), UsingTTIOverrides(anyExpandVPOverridesSet()) {}
194 
195   bool expandVectorPredication();
196 };
197 
198 //// CachingVPExpander {
199 
200 Value *CachingVPExpander::createStepVector(IRBuilder<> &Builder, Type *LaneTy,
201                                            unsigned NumElems) {
202   // TODO add caching
203   SmallVector<Constant *, 16> ConstElems;
204 
205   for (unsigned Idx = 0; Idx < NumElems; ++Idx)
206     ConstElems.push_back(ConstantInt::get(LaneTy, Idx, false));
207 
208   return ConstantVector::get(ConstElems);
209 }
210 
211 Value *CachingVPExpander::convertEVLToMask(IRBuilder<> &Builder,
212                                            Value *EVLParam,
213                                            ElementCount ElemCount) {
214   // TODO add caching
215   // Scalable vector %evl conversion.
216   if (ElemCount.isScalable()) {
217     auto *M = Builder.GetInsertBlock()->getModule();
218     Type *BoolVecTy = VectorType::get(Builder.getInt1Ty(), ElemCount);
219     Function *ActiveMaskFunc = Intrinsic::getDeclaration(
220         M, Intrinsic::get_active_lane_mask, {BoolVecTy, EVLParam->getType()});
221     // `get_active_lane_mask` performs an implicit less-than comparison.
222     Value *ConstZero = Builder.getInt32(0);
223     return Builder.CreateCall(ActiveMaskFunc, {ConstZero, EVLParam});
224   }
225 
226   // Fixed vector %evl conversion.
227   Type *LaneTy = EVLParam->getType();
228   unsigned NumElems = ElemCount.getFixedValue();
229   Value *VLSplat = Builder.CreateVectorSplat(NumElems, EVLParam);
230   Value *IdxVec = createStepVector(Builder, LaneTy, NumElems);
231   return Builder.CreateICmp(CmpInst::ICMP_ULT, IdxVec, VLSplat);
232 }
233 
234 Value *
235 CachingVPExpander::expandPredicationInBinaryOperator(IRBuilder<> &Builder,
236                                                      VPIntrinsic &VPI) {
237   assert((maySpeculateLanes(VPI) || VPI.canIgnoreVectorLengthParam()) &&
238          "Implicitly dropping %evl in non-speculatable operator!");
239 
240   auto OC = static_cast<Instruction::BinaryOps>(*VPI.getFunctionalOpcode());
241   assert(Instruction::isBinaryOp(OC));
242 
243   Value *Op0 = VPI.getOperand(0);
244   Value *Op1 = VPI.getOperand(1);
245   Value *Mask = VPI.getMaskParam();
246 
247   // Blend in safe operands.
248   if (Mask && !isAllTrueMask(Mask)) {
249     switch (OC) {
250     default:
251       // Can safely ignore the predicate.
252       break;
253 
254     // Division operators need a safe divisor on masked-off lanes (1).
255     case Instruction::UDiv:
256     case Instruction::SDiv:
257     case Instruction::URem:
258     case Instruction::SRem:
259       // 2nd operand must not be zero.
260       Value *SafeDivisor = getSafeDivisor(VPI.getType());
261       Op1 = Builder.CreateSelect(Mask, Op1, SafeDivisor);
262     }
263   }
264 
265   Value *NewBinOp = Builder.CreateBinOp(OC, Op0, Op1, VPI.getName());
266 
267   replaceOperation(*NewBinOp, VPI);
268   return NewBinOp;
269 }
270 
271 static Value *getNeutralReductionElement(const VPReductionIntrinsic &VPI,
272                                          Type *EltTy) {
273   bool Negative = false;
274   unsigned EltBits = EltTy->getScalarSizeInBits();
275   switch (VPI.getIntrinsicID()) {
276   default:
277     llvm_unreachable("Expecting a VP reduction intrinsic");
278   case Intrinsic::vp_reduce_add:
279   case Intrinsic::vp_reduce_or:
280   case Intrinsic::vp_reduce_xor:
281   case Intrinsic::vp_reduce_umax:
282     return Constant::getNullValue(EltTy);
283   case Intrinsic::vp_reduce_mul:
284     return ConstantInt::get(EltTy, 1, /*IsSigned*/ false);
285   case Intrinsic::vp_reduce_and:
286   case Intrinsic::vp_reduce_umin:
287     return ConstantInt::getAllOnesValue(EltTy);
288   case Intrinsic::vp_reduce_smin:
289     return ConstantInt::get(EltTy->getContext(),
290                             APInt::getSignedMaxValue(EltBits));
291   case Intrinsic::vp_reduce_smax:
292     return ConstantInt::get(EltTy->getContext(),
293                             APInt::getSignedMinValue(EltBits));
294   case Intrinsic::vp_reduce_fmax:
295     Negative = true;
296     LLVM_FALLTHROUGH;
297   case Intrinsic::vp_reduce_fmin: {
298     FastMathFlags Flags = VPI.getFastMathFlags();
299     const fltSemantics &Semantics = EltTy->getFltSemantics();
300     return !Flags.noNaNs() ? ConstantFP::getQNaN(EltTy, Negative)
301            : !Flags.noInfs()
302                ? ConstantFP::getInfinity(EltTy, Negative)
303                : ConstantFP::get(EltTy,
304                                  APFloat::getLargest(Semantics, Negative));
305   }
306   case Intrinsic::vp_reduce_fadd:
307     return ConstantFP::getNegativeZero(EltTy);
308   case Intrinsic::vp_reduce_fmul:
309     return ConstantFP::get(EltTy, 1.0);
310   }
311 }
312 
313 Value *
314 CachingVPExpander::expandPredicationInReduction(IRBuilder<> &Builder,
315                                                 VPReductionIntrinsic &VPI) {
316   assert((maySpeculateLanes(VPI) || VPI.canIgnoreVectorLengthParam()) &&
317          "Implicitly dropping %evl in non-speculatable operator!");
318 
319   Value *Mask = VPI.getMaskParam();
320   Value *RedOp = VPI.getOperand(VPI.getVectorParamPos());
321 
322   // Insert neutral element in masked-out positions
323   if (Mask && !isAllTrueMask(Mask)) {
324     auto *NeutralElt = getNeutralReductionElement(VPI, VPI.getType());
325     auto *NeutralVector = Builder.CreateVectorSplat(
326         cast<VectorType>(RedOp->getType())->getElementCount(), NeutralElt);
327     RedOp = Builder.CreateSelect(Mask, RedOp, NeutralVector);
328   }
329 
330   Value *Reduction;
331   Value *Start = VPI.getOperand(VPI.getStartParamPos());
332 
333   switch (VPI.getIntrinsicID()) {
334   default:
335     llvm_unreachable("Impossible reduction kind");
336   case Intrinsic::vp_reduce_add:
337     Reduction = Builder.CreateAddReduce(RedOp);
338     Reduction = Builder.CreateAdd(Reduction, Start);
339     break;
340   case Intrinsic::vp_reduce_mul:
341     Reduction = Builder.CreateMulReduce(RedOp);
342     Reduction = Builder.CreateMul(Reduction, Start);
343     break;
344   case Intrinsic::vp_reduce_and:
345     Reduction = Builder.CreateAndReduce(RedOp);
346     Reduction = Builder.CreateAnd(Reduction, Start);
347     break;
348   case Intrinsic::vp_reduce_or:
349     Reduction = Builder.CreateOrReduce(RedOp);
350     Reduction = Builder.CreateOr(Reduction, Start);
351     break;
352   case Intrinsic::vp_reduce_xor:
353     Reduction = Builder.CreateXorReduce(RedOp);
354     Reduction = Builder.CreateXor(Reduction, Start);
355     break;
356   case Intrinsic::vp_reduce_smax:
357     Reduction = Builder.CreateIntMaxReduce(RedOp, /*IsSigned*/ true);
358     Reduction =
359         Builder.CreateBinaryIntrinsic(Intrinsic::smax, Reduction, Start);
360     break;
361   case Intrinsic::vp_reduce_smin:
362     Reduction = Builder.CreateIntMinReduce(RedOp, /*IsSigned*/ true);
363     Reduction =
364         Builder.CreateBinaryIntrinsic(Intrinsic::smin, Reduction, Start);
365     break;
366   case Intrinsic::vp_reduce_umax:
367     Reduction = Builder.CreateIntMaxReduce(RedOp, /*IsSigned*/ false);
368     Reduction =
369         Builder.CreateBinaryIntrinsic(Intrinsic::umax, Reduction, Start);
370     break;
371   case Intrinsic::vp_reduce_umin:
372     Reduction = Builder.CreateIntMinReduce(RedOp, /*IsSigned*/ false);
373     Reduction =
374         Builder.CreateBinaryIntrinsic(Intrinsic::umin, Reduction, Start);
375     break;
376   case Intrinsic::vp_reduce_fmax:
377     Reduction = Builder.CreateFPMaxReduce(RedOp);
378     transferDecorations(*Reduction, VPI);
379     Reduction =
380         Builder.CreateBinaryIntrinsic(Intrinsic::maxnum, Reduction, Start);
381     break;
382   case Intrinsic::vp_reduce_fmin:
383     Reduction = Builder.CreateFPMinReduce(RedOp);
384     transferDecorations(*Reduction, VPI);
385     Reduction =
386         Builder.CreateBinaryIntrinsic(Intrinsic::minnum, Reduction, Start);
387     break;
388   case Intrinsic::vp_reduce_fadd:
389     Reduction = Builder.CreateFAddReduce(Start, RedOp);
390     break;
391   case Intrinsic::vp_reduce_fmul:
392     Reduction = Builder.CreateFMulReduce(Start, RedOp);
393     break;
394   }
395 
396   replaceOperation(*Reduction, VPI);
397   return Reduction;
398 }
399 
400 Value *
401 CachingVPExpander::expandPredicationInMemoryIntrinsic(IRBuilder<> &Builder,
402                                                       VPIntrinsic &VPI) {
403   assert(VPI.canIgnoreVectorLengthParam());
404 
405   Value *MaskParam = VPI.getMaskParam();
406   Value *PtrParam = VPI.getMemoryPointerParam();
407   Value *DataParam = VPI.getMemoryDataParam();
408   bool IsUnmasked = isAllTrueMask(MaskParam);
409 
410   MaybeAlign AlignOpt = VPI.getPointerAlignment();
411 
412   Value *NewMemoryInst = nullptr;
413   switch (VPI.getIntrinsicID()) {
414   default:
415     llvm_unreachable("Not a VP memory intrinsic");
416   case Intrinsic::vp_store:
417     if (IsUnmasked) {
418       StoreInst *NewStore =
419           Builder.CreateStore(DataParam, PtrParam, /*IsVolatile*/ false);
420       if (AlignOpt.hasValue())
421         NewStore->setAlignment(AlignOpt.getValue());
422       NewMemoryInst = NewStore;
423     } else
424       NewMemoryInst = Builder.CreateMaskedStore(
425           DataParam, PtrParam, AlignOpt.valueOrOne(), MaskParam);
426 
427     break;
428   case Intrinsic::vp_load:
429     if (IsUnmasked) {
430       LoadInst *NewLoad =
431           Builder.CreateLoad(VPI.getType(), PtrParam, /*IsVolatile*/ false);
432       if (AlignOpt.hasValue())
433         NewLoad->setAlignment(AlignOpt.getValue());
434       NewMemoryInst = NewLoad;
435     } else
436       NewMemoryInst = Builder.CreateMaskedLoad(
437           VPI.getType(), PtrParam, AlignOpt.valueOrOne(), MaskParam);
438 
439     break;
440   }
441 
442   assert(NewMemoryInst);
443   replaceOperation(*NewMemoryInst, VPI);
444   return NewMemoryInst;
445 }
446 
447 void CachingVPExpander::discardEVLParameter(VPIntrinsic &VPI) {
448   LLVM_DEBUG(dbgs() << "Discard EVL parameter in " << VPI << "\n");
449 
450   if (VPI.canIgnoreVectorLengthParam())
451     return;
452 
453   Value *EVLParam = VPI.getVectorLengthParam();
454   if (!EVLParam)
455     return;
456 
457   ElementCount StaticElemCount = VPI.getStaticVectorLength();
458   Value *MaxEVL = nullptr;
459   Type *Int32Ty = Type::getInt32Ty(VPI.getContext());
460   if (StaticElemCount.isScalable()) {
461     // TODO add caching
462     auto *M = VPI.getModule();
463     Function *VScaleFunc =
464         Intrinsic::getDeclaration(M, Intrinsic::vscale, Int32Ty);
465     IRBuilder<> Builder(VPI.getParent(), VPI.getIterator());
466     Value *FactorConst = Builder.getInt32(StaticElemCount.getKnownMinValue());
467     Value *VScale = Builder.CreateCall(VScaleFunc, {}, "vscale");
468     MaxEVL = Builder.CreateMul(VScale, FactorConst, "scalable_size",
469                                /*NUW*/ true, /*NSW*/ false);
470   } else {
471     MaxEVL = ConstantInt::get(Int32Ty, StaticElemCount.getFixedValue(), false);
472   }
473   VPI.setVectorLengthParam(MaxEVL);
474 }
475 
476 Value *CachingVPExpander::foldEVLIntoMask(VPIntrinsic &VPI) {
477   LLVM_DEBUG(dbgs() << "Folding vlen for " << VPI << '\n');
478 
479   IRBuilder<> Builder(&VPI);
480 
481   // Ineffective %evl parameter and so nothing to do here.
482   if (VPI.canIgnoreVectorLengthParam())
483     return &VPI;
484 
485   // Only VP intrinsics can have an %evl parameter.
486   Value *OldMaskParam = VPI.getMaskParam();
487   Value *OldEVLParam = VPI.getVectorLengthParam();
488   assert(OldMaskParam && "no mask param to fold the vl param into");
489   assert(OldEVLParam && "no EVL param to fold away");
490 
491   LLVM_DEBUG(dbgs() << "OLD evl: " << *OldEVLParam << '\n');
492   LLVM_DEBUG(dbgs() << "OLD mask: " << *OldMaskParam << '\n');
493 
494   // Convert the %evl predication into vector mask predication.
495   ElementCount ElemCount = VPI.getStaticVectorLength();
496   Value *VLMask = convertEVLToMask(Builder, OldEVLParam, ElemCount);
497   Value *NewMaskParam = Builder.CreateAnd(VLMask, OldMaskParam);
498   VPI.setMaskParam(NewMaskParam);
499 
500   // Drop the %evl parameter.
501   discardEVLParameter(VPI);
502   assert(VPI.canIgnoreVectorLengthParam() &&
503          "transformation did not render the evl param ineffective!");
504 
505   // Reassess the modified instruction.
506   return &VPI;
507 }
508 
509 Value *CachingVPExpander::expandPredication(VPIntrinsic &VPI) {
510   LLVM_DEBUG(dbgs() << "Lowering to unpredicated op: " << VPI << '\n');
511 
512   IRBuilder<> Builder(&VPI);
513 
514   // Try lowering to a LLVM instruction first.
515   auto OC = VPI.getFunctionalOpcode();
516 
517   if (OC && Instruction::isBinaryOp(*OC))
518     return expandPredicationInBinaryOperator(Builder, VPI);
519 
520   if (auto *VPRI = dyn_cast<VPReductionIntrinsic>(&VPI))
521     return expandPredicationInReduction(Builder, *VPRI);
522 
523   switch (VPI.getIntrinsicID()) {
524   default:
525     break;
526   case Intrinsic::vp_load:
527   case Intrinsic::vp_store:
528     return expandPredicationInMemoryIntrinsic(Builder, VPI);
529   }
530 
531   return &VPI;
532 }
533 
534 //// } CachingVPExpander
535 
536 struct TransformJob {
537   VPIntrinsic *PI;
538   TargetTransformInfo::VPLegalization Strategy;
539   TransformJob(VPIntrinsic *PI, TargetTransformInfo::VPLegalization InitStrat)
540       : PI(PI), Strategy(InitStrat) {}
541 
542   bool isDone() const { return Strategy.shouldDoNothing(); }
543 };
544 
545 void sanitizeStrategy(VPIntrinsic &VPI, VPLegalization &LegalizeStrat) {
546   // Operations with speculatable lanes do not strictly need predication.
547   if (maySpeculateLanes(VPI)) {
548     // Converting a speculatable VP intrinsic means dropping %mask and %evl.
549     // No need to expand %evl into the %mask only to ignore that code.
550     if (LegalizeStrat.OpStrategy == VPLegalization::Convert)
551       LegalizeStrat.EVLParamStrategy = VPLegalization::Discard;
552     return;
553   }
554 
555   // We have to preserve the predicating effect of %evl for this
556   // non-speculatable VP intrinsic.
557   // 1) Never discard %evl.
558   // 2) If this VP intrinsic will be expanded to non-VP code, make sure that
559   //    %evl gets folded into %mask.
560   if ((LegalizeStrat.EVLParamStrategy == VPLegalization::Discard) ||
561       (LegalizeStrat.OpStrategy == VPLegalization::Convert)) {
562     LegalizeStrat.EVLParamStrategy = VPLegalization::Convert;
563   }
564 }
565 
566 VPLegalization
567 CachingVPExpander::getVPLegalizationStrategy(const VPIntrinsic &VPI) const {
568   auto VPStrat = TTI.getVPLegalizationStrategy(VPI);
569   if (LLVM_LIKELY(!UsingTTIOverrides)) {
570     // No overrides - we are in production.
571     return VPStrat;
572   }
573 
574   // Overrides set - we are in testing, the following does not need to be
575   // efficient.
576   VPStrat.EVLParamStrategy = parseOverrideOption(EVLTransformOverride);
577   VPStrat.OpStrategy = parseOverrideOption(MaskTransformOverride);
578   return VPStrat;
579 }
580 
581 /// \brief Expand llvm.vp.* intrinsics as requested by \p TTI.
582 bool CachingVPExpander::expandVectorPredication() {
583   SmallVector<TransformJob, 16> Worklist;
584 
585   // Collect all VPIntrinsics that need expansion and determine their expansion
586   // strategy.
587   for (auto &I : instructions(F)) {
588     auto *VPI = dyn_cast<VPIntrinsic>(&I);
589     if (!VPI)
590       continue;
591     auto VPStrat = getVPLegalizationStrategy(*VPI);
592     sanitizeStrategy(*VPI, VPStrat);
593     if (!VPStrat.shouldDoNothing())
594       Worklist.emplace_back(VPI, VPStrat);
595   }
596   if (Worklist.empty())
597     return false;
598 
599   // Transform all VPIntrinsics on the worklist.
600   LLVM_DEBUG(dbgs() << "\n:::: Transforming " << Worklist.size()
601                     << " instructions ::::\n");
602   for (TransformJob Job : Worklist) {
603     // Transform the EVL parameter.
604     switch (Job.Strategy.EVLParamStrategy) {
605     case VPLegalization::Legal:
606       break;
607     case VPLegalization::Discard:
608       discardEVLParameter(*Job.PI);
609       break;
610     case VPLegalization::Convert:
611       if (foldEVLIntoMask(*Job.PI))
612         ++NumFoldedVL;
613       break;
614     }
615     Job.Strategy.EVLParamStrategy = VPLegalization::Legal;
616 
617     // Replace with a non-predicated operation.
618     switch (Job.Strategy.OpStrategy) {
619     case VPLegalization::Legal:
620       break;
621     case VPLegalization::Discard:
622       llvm_unreachable("Invalid strategy for operators.");
623     case VPLegalization::Convert:
624       expandPredication(*Job.PI);
625       ++NumLoweredVPOps;
626       break;
627     }
628     Job.Strategy.OpStrategy = VPLegalization::Legal;
629 
630     assert(Job.isDone() && "incomplete transformation");
631   }
632 
633   return true;
634 }
635 class ExpandVectorPredication : public FunctionPass {
636 public:
637   static char ID;
638   ExpandVectorPredication() : FunctionPass(ID) {
639     initializeExpandVectorPredicationPass(*PassRegistry::getPassRegistry());
640   }
641 
642   bool runOnFunction(Function &F) override {
643     const auto *TTI = &getAnalysis<TargetTransformInfoWrapperPass>().getTTI(F);
644     CachingVPExpander VPExpander(F, *TTI);
645     return VPExpander.expandVectorPredication();
646   }
647 
648   void getAnalysisUsage(AnalysisUsage &AU) const override {
649     AU.addRequired<TargetTransformInfoWrapperPass>();
650     AU.setPreservesCFG();
651   }
652 };
653 } // namespace
654 
655 char ExpandVectorPredication::ID;
656 INITIALIZE_PASS_BEGIN(ExpandVectorPredication, "expandvp",
657                       "Expand vector predication intrinsics", false, false)
658 INITIALIZE_PASS_DEPENDENCY(TargetTransformInfoWrapperPass)
659 INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
660 INITIALIZE_PASS_END(ExpandVectorPredication, "expandvp",
661                     "Expand vector predication intrinsics", false, false)
662 
663 FunctionPass *llvm::createExpandVectorPredicationPass() {
664   return new ExpandVectorPredication();
665 }
666 
667 PreservedAnalyses
668 ExpandVectorPredicationPass::run(Function &F, FunctionAnalysisManager &AM) {
669   const auto &TTI = AM.getResult<TargetIRAnalysis>(F);
670   CachingVPExpander VPExpander(F, TTI);
671   if (!VPExpander.expandVectorPredication())
672     return PreservedAnalyses::all();
673   PreservedAnalyses PA;
674   PA.preserveSet<CFGAnalyses>();
675   return PA;
676 }
677