1 //===- LoopVectorizationLegality.cpp --------------------------------------===//
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 file provides loop vectorization legality analysis. Original code
10 // resided in LoopVectorize.cpp for a long time.
11 //
12 // At this point, it is implemented as a utility class, not as an analysis
13 // pass. It should be easy to create an analysis pass around it if there
14 // is a need (but D45420 needs to happen first).
15 //
16 #include "llvm/Transforms/Vectorize/LoopVectorize.h"
17 #include "llvm/Transforms/Vectorize/LoopVectorizationLegality.h"
18 #include "llvm/Analysis/Loads.h"
19 #include "llvm/Analysis/ValueTracking.h"
20 #include "llvm/Analysis/VectorUtils.h"
21 #include "llvm/IR/IntrinsicInst.h"
22 
23 using namespace llvm;
24 
25 #define LV_NAME "loop-vectorize"
26 #define DEBUG_TYPE LV_NAME
27 
28 extern cl::opt<bool> EnableVPlanPredication;
29 
30 static cl::opt<bool>
31     EnableIfConversion("enable-if-conversion", cl::init(true), cl::Hidden,
32                        cl::desc("Enable if-conversion during vectorization."));
33 
34 static cl::opt<unsigned> PragmaVectorizeMemoryCheckThreshold(
35     "pragma-vectorize-memory-check-threshold", cl::init(128), cl::Hidden,
36     cl::desc("The maximum allowed number of runtime memory checks with a "
37              "vectorize(enable) pragma."));
38 
39 static cl::opt<unsigned> VectorizeSCEVCheckThreshold(
40     "vectorize-scev-check-threshold", cl::init(16), cl::Hidden,
41     cl::desc("The maximum number of SCEV checks allowed."));
42 
43 static cl::opt<unsigned> PragmaVectorizeSCEVCheckThreshold(
44     "pragma-vectorize-scev-check-threshold", cl::init(128), cl::Hidden,
45     cl::desc("The maximum number of SCEV checks allowed with a "
46              "vectorize(enable) pragma"));
47 
48 /// Maximum vectorization interleave count.
49 static const unsigned MaxInterleaveFactor = 16;
50 
51 namespace llvm {
52 
53 bool LoopVectorizeHints::Hint::validate(unsigned Val) {
54   switch (Kind) {
55   case HK_WIDTH:
56     return isPowerOf2_32(Val) && Val <= VectorizerParams::MaxVectorWidth;
57   case HK_UNROLL:
58     return isPowerOf2_32(Val) && Val <= MaxInterleaveFactor;
59   case HK_FORCE:
60     return (Val <= 1);
61   case HK_ISVECTORIZED:
62   case HK_PREDICATE:
63     return (Val == 0 || Val == 1);
64   }
65   return false;
66 }
67 
68 LoopVectorizeHints::LoopVectorizeHints(const Loop *L,
69                                        bool InterleaveOnlyWhenForced,
70                                        OptimizationRemarkEmitter &ORE)
71     : Width("vectorize.width", VectorizerParams::VectorizationFactor, HK_WIDTH),
72       Interleave("interleave.count", InterleaveOnlyWhenForced, HK_UNROLL),
73       Force("vectorize.enable", FK_Undefined, HK_FORCE),
74       IsVectorized("isvectorized", 0, HK_ISVECTORIZED),
75       Predicate("vectorize.predicate.enable", FK_Undefined, HK_PREDICATE), TheLoop(L),
76       ORE(ORE) {
77   // Populate values with existing loop metadata.
78   getHintsFromMetadata();
79 
80   // force-vector-interleave overrides DisableInterleaving.
81   if (VectorizerParams::isInterleaveForced())
82     Interleave.Value = VectorizerParams::VectorizationInterleave;
83 
84   if (IsVectorized.Value != 1)
85     // If the vectorization width and interleaving count are both 1 then
86     // consider the loop to have been already vectorized because there's
87     // nothing more that we can do.
88     IsVectorized.Value = Width.Value == 1 && Interleave.Value == 1;
89   LLVM_DEBUG(if (InterleaveOnlyWhenForced && Interleave.Value == 1) dbgs()
90              << "LV: Interleaving disabled by the pass manager\n");
91 }
92 
93 void LoopVectorizeHints::setAlreadyVectorized() {
94   LLVMContext &Context = TheLoop->getHeader()->getContext();
95 
96   MDNode *IsVectorizedMD = MDNode::get(
97       Context,
98       {MDString::get(Context, "llvm.loop.isvectorized"),
99        ConstantAsMetadata::get(ConstantInt::get(Context, APInt(32, 1)))});
100   MDNode *LoopID = TheLoop->getLoopID();
101   MDNode *NewLoopID =
102       makePostTransformationMetadata(Context, LoopID,
103                                      {Twine(Prefix(), "vectorize.").str(),
104                                       Twine(Prefix(), "interleave.").str()},
105                                      {IsVectorizedMD});
106   TheLoop->setLoopID(NewLoopID);
107 
108   // Update internal cache.
109   IsVectorized.Value = 1;
110 }
111 
112 bool LoopVectorizeHints::allowVectorization(
113     Function *F, Loop *L, bool VectorizeOnlyWhenForced) const {
114   if (getForce() == LoopVectorizeHints::FK_Disabled) {
115     LLVM_DEBUG(dbgs() << "LV: Not vectorizing: #pragma vectorize disable.\n");
116     emitRemarkWithHints();
117     return false;
118   }
119 
120   if (VectorizeOnlyWhenForced && getForce() != LoopVectorizeHints::FK_Enabled) {
121     LLVM_DEBUG(dbgs() << "LV: Not vectorizing: No #pragma vectorize enable.\n");
122     emitRemarkWithHints();
123     return false;
124   }
125 
126   if (getIsVectorized() == 1) {
127     LLVM_DEBUG(dbgs() << "LV: Not vectorizing: Disabled/already vectorized.\n");
128     // FIXME: Add interleave.disable metadata. This will allow
129     // vectorize.disable to be used without disabling the pass and errors
130     // to differentiate between disabled vectorization and a width of 1.
131     ORE.emit([&]() {
132       return OptimizationRemarkAnalysis(vectorizeAnalysisPassName(),
133                                         "AllDisabled", L->getStartLoc(),
134                                         L->getHeader())
135              << "loop not vectorized: vectorization and interleaving are "
136                 "explicitly disabled, or the loop has already been "
137                 "vectorized";
138     });
139     return false;
140   }
141 
142   return true;
143 }
144 
145 void LoopVectorizeHints::emitRemarkWithHints() const {
146   using namespace ore;
147 
148   ORE.emit([&]() {
149     if (Force.Value == LoopVectorizeHints::FK_Disabled)
150       return OptimizationRemarkMissed(LV_NAME, "MissedExplicitlyDisabled",
151                                       TheLoop->getStartLoc(),
152                                       TheLoop->getHeader())
153              << "loop not vectorized: vectorization is explicitly disabled";
154     else {
155       OptimizationRemarkMissed R(LV_NAME, "MissedDetails",
156                                  TheLoop->getStartLoc(), TheLoop->getHeader());
157       R << "loop not vectorized";
158       if (Force.Value == LoopVectorizeHints::FK_Enabled) {
159         R << " (Force=" << NV("Force", true);
160         if (Width.Value != 0)
161           R << ", Vector Width=" << NV("VectorWidth", Width.Value);
162         if (Interleave.Value != 0)
163           R << ", Interleave Count=" << NV("InterleaveCount", Interleave.Value);
164         R << ")";
165       }
166       return R;
167     }
168   });
169 }
170 
171 const char *LoopVectorizeHints::vectorizeAnalysisPassName() const {
172   if (getWidth() == 1)
173     return LV_NAME;
174   if (getForce() == LoopVectorizeHints::FK_Disabled)
175     return LV_NAME;
176   if (getForce() == LoopVectorizeHints::FK_Undefined && getWidth() == 0)
177     return LV_NAME;
178   return OptimizationRemarkAnalysis::AlwaysPrint;
179 }
180 
181 void LoopVectorizeHints::getHintsFromMetadata() {
182   MDNode *LoopID = TheLoop->getLoopID();
183   if (!LoopID)
184     return;
185 
186   // First operand should refer to the loop id itself.
187   assert(LoopID->getNumOperands() > 0 && "requires at least one operand");
188   assert(LoopID->getOperand(0) == LoopID && "invalid loop id");
189 
190   for (unsigned i = 1, ie = LoopID->getNumOperands(); i < ie; ++i) {
191     const MDString *S = nullptr;
192     SmallVector<Metadata *, 4> Args;
193 
194     // The expected hint is either a MDString or a MDNode with the first
195     // operand a MDString.
196     if (const MDNode *MD = dyn_cast<MDNode>(LoopID->getOperand(i))) {
197       if (!MD || MD->getNumOperands() == 0)
198         continue;
199       S = dyn_cast<MDString>(MD->getOperand(0));
200       for (unsigned i = 1, ie = MD->getNumOperands(); i < ie; ++i)
201         Args.push_back(MD->getOperand(i));
202     } else {
203       S = dyn_cast<MDString>(LoopID->getOperand(i));
204       assert(Args.size() == 0 && "too many arguments for MDString");
205     }
206 
207     if (!S)
208       continue;
209 
210     // Check if the hint starts with the loop metadata prefix.
211     StringRef Name = S->getString();
212     if (Args.size() == 1)
213       setHint(Name, Args[0]);
214   }
215 }
216 
217 void LoopVectorizeHints::setHint(StringRef Name, Metadata *Arg) {
218   if (!Name.startswith(Prefix()))
219     return;
220   Name = Name.substr(Prefix().size(), StringRef::npos);
221 
222   const ConstantInt *C = mdconst::dyn_extract<ConstantInt>(Arg);
223   if (!C)
224     return;
225   unsigned Val = C->getZExtValue();
226 
227   Hint *Hints[] = {&Width, &Interleave, &Force, &IsVectorized, &Predicate};
228   for (auto H : Hints) {
229     if (Name == H->Name) {
230       if (H->validate(Val))
231         H->Value = Val;
232       else
233         LLVM_DEBUG(dbgs() << "LV: ignoring invalid hint '" << Name << "'\n");
234       break;
235     }
236   }
237 }
238 
239 bool LoopVectorizationRequirements::doesNotMeet(
240     Function *F, Loop *L, const LoopVectorizeHints &Hints) {
241   const char *PassName = Hints.vectorizeAnalysisPassName();
242   bool Failed = false;
243   if (UnsafeAlgebraInst && !Hints.allowReordering()) {
244     ORE.emit([&]() {
245       return OptimizationRemarkAnalysisFPCommute(
246                  PassName, "CantReorderFPOps", UnsafeAlgebraInst->getDebugLoc(),
247                  UnsafeAlgebraInst->getParent())
248              << "loop not vectorized: cannot prove it is safe to reorder "
249                 "floating-point operations";
250     });
251     Failed = true;
252   }
253 
254   // Test if runtime memcheck thresholds are exceeded.
255   bool PragmaThresholdReached =
256       NumRuntimePointerChecks > PragmaVectorizeMemoryCheckThreshold;
257   bool ThresholdReached =
258       NumRuntimePointerChecks > VectorizerParams::RuntimeMemoryCheckThreshold;
259   if ((ThresholdReached && !Hints.allowReordering()) ||
260       PragmaThresholdReached) {
261     ORE.emit([&]() {
262       return OptimizationRemarkAnalysisAliasing(PassName, "CantReorderMemOps",
263                                                 L->getStartLoc(),
264                                                 L->getHeader())
265              << "loop not vectorized: cannot prove it is safe to reorder "
266                 "memory operations";
267     });
268     LLVM_DEBUG(dbgs() << "LV: Too many memory checks needed.\n");
269     Failed = true;
270   }
271 
272   return Failed;
273 }
274 
275 // Return true if the inner loop \p Lp is uniform with regard to the outer loop
276 // \p OuterLp (i.e., if the outer loop is vectorized, all the vector lanes
277 // executing the inner loop will execute the same iterations). This check is
278 // very constrained for now but it will be relaxed in the future. \p Lp is
279 // considered uniform if it meets all the following conditions:
280 //   1) it has a canonical IV (starting from 0 and with stride 1),
281 //   2) its latch terminator is a conditional branch and,
282 //   3) its latch condition is a compare instruction whose operands are the
283 //      canonical IV and an OuterLp invariant.
284 // This check doesn't take into account the uniformity of other conditions not
285 // related to the loop latch because they don't affect the loop uniformity.
286 //
287 // NOTE: We decided to keep all these checks and its associated documentation
288 // together so that we can easily have a picture of the current supported loop
289 // nests. However, some of the current checks don't depend on \p OuterLp and
290 // would be redundantly executed for each \p Lp if we invoked this function for
291 // different candidate outer loops. This is not the case for now because we
292 // don't currently have the infrastructure to evaluate multiple candidate outer
293 // loops and \p OuterLp will be a fixed parameter while we only support explicit
294 // outer loop vectorization. It's also very likely that these checks go away
295 // before introducing the aforementioned infrastructure. However, if this is not
296 // the case, we should move the \p OuterLp independent checks to a separate
297 // function that is only executed once for each \p Lp.
298 static bool isUniformLoop(Loop *Lp, Loop *OuterLp) {
299   assert(Lp->getLoopLatch() && "Expected loop with a single latch.");
300 
301   // If Lp is the outer loop, it's uniform by definition.
302   if (Lp == OuterLp)
303     return true;
304   assert(OuterLp->contains(Lp) && "OuterLp must contain Lp.");
305 
306   // 1.
307   PHINode *IV = Lp->getCanonicalInductionVariable();
308   if (!IV) {
309     LLVM_DEBUG(dbgs() << "LV: Canonical IV not found.\n");
310     return false;
311   }
312 
313   // 2.
314   BasicBlock *Latch = Lp->getLoopLatch();
315   auto *LatchBr = dyn_cast<BranchInst>(Latch->getTerminator());
316   if (!LatchBr || LatchBr->isUnconditional()) {
317     LLVM_DEBUG(dbgs() << "LV: Unsupported loop latch branch.\n");
318     return false;
319   }
320 
321   // 3.
322   auto *LatchCmp = dyn_cast<CmpInst>(LatchBr->getCondition());
323   if (!LatchCmp) {
324     LLVM_DEBUG(
325         dbgs() << "LV: Loop latch condition is not a compare instruction.\n");
326     return false;
327   }
328 
329   Value *CondOp0 = LatchCmp->getOperand(0);
330   Value *CondOp1 = LatchCmp->getOperand(1);
331   Value *IVUpdate = IV->getIncomingValueForBlock(Latch);
332   if (!(CondOp0 == IVUpdate && OuterLp->isLoopInvariant(CondOp1)) &&
333       !(CondOp1 == IVUpdate && OuterLp->isLoopInvariant(CondOp0))) {
334     LLVM_DEBUG(dbgs() << "LV: Loop latch condition is not uniform.\n");
335     return false;
336   }
337 
338   return true;
339 }
340 
341 // Return true if \p Lp and all its nested loops are uniform with regard to \p
342 // OuterLp.
343 static bool isUniformLoopNest(Loop *Lp, Loop *OuterLp) {
344   if (!isUniformLoop(Lp, OuterLp))
345     return false;
346 
347   // Check if nested loops are uniform.
348   for (Loop *SubLp : *Lp)
349     if (!isUniformLoopNest(SubLp, OuterLp))
350       return false;
351 
352   return true;
353 }
354 
355 /// Check whether it is safe to if-convert this phi node.
356 ///
357 /// Phi nodes with constant expressions that can trap are not safe to if
358 /// convert.
359 static bool canIfConvertPHINodes(BasicBlock *BB) {
360   for (PHINode &Phi : BB->phis()) {
361     for (Value *V : Phi.incoming_values())
362       if (auto *C = dyn_cast<Constant>(V))
363         if (C->canTrap())
364           return false;
365   }
366   return true;
367 }
368 
369 static Type *convertPointerToIntegerType(const DataLayout &DL, Type *Ty) {
370   if (Ty->isPointerTy())
371     return DL.getIntPtrType(Ty);
372 
373   // It is possible that char's or short's overflow when we ask for the loop's
374   // trip count, work around this by changing the type size.
375   if (Ty->getScalarSizeInBits() < 32)
376     return Type::getInt32Ty(Ty->getContext());
377 
378   return Ty;
379 }
380 
381 static Type *getWiderType(const DataLayout &DL, Type *Ty0, Type *Ty1) {
382   Ty0 = convertPointerToIntegerType(DL, Ty0);
383   Ty1 = convertPointerToIntegerType(DL, Ty1);
384   if (Ty0->getScalarSizeInBits() > Ty1->getScalarSizeInBits())
385     return Ty0;
386   return Ty1;
387 }
388 
389 /// Check that the instruction has outside loop users and is not an
390 /// identified reduction variable.
391 static bool hasOutsideLoopUser(const Loop *TheLoop, Instruction *Inst,
392                                SmallPtrSetImpl<Value *> &AllowedExit) {
393   // Reductions, Inductions and non-header phis are allowed to have exit users. All
394   // other instructions must not have external users.
395   if (!AllowedExit.count(Inst))
396     // Check that all of the users of the loop are inside the BB.
397     for (User *U : Inst->users()) {
398       Instruction *UI = cast<Instruction>(U);
399       // This user may be a reduction exit value.
400       if (!TheLoop->contains(UI)) {
401         LLVM_DEBUG(dbgs() << "LV: Found an outside user for : " << *UI << '\n');
402         return true;
403       }
404     }
405   return false;
406 }
407 
408 int LoopVectorizationLegality::isConsecutivePtr(Value *Ptr) {
409   const ValueToValueMap &Strides =
410       getSymbolicStrides() ? *getSymbolicStrides() : ValueToValueMap();
411 
412   bool CanAddPredicate = !TheLoop->getHeader()->getParent()->hasOptSize();
413   int Stride = getPtrStride(PSE, Ptr, TheLoop, Strides, CanAddPredicate, false);
414   if (Stride == 1 || Stride == -1)
415     return Stride;
416   return 0;
417 }
418 
419 bool LoopVectorizationLegality::isUniform(Value *V) {
420   return LAI->isUniform(V);
421 }
422 
423 bool LoopVectorizationLegality::canVectorizeOuterLoop() {
424   assert(!TheLoop->empty() && "We are not vectorizing an outer loop.");
425   // Store the result and return it at the end instead of exiting early, in case
426   // allowExtraAnalysis is used to report multiple reasons for not vectorizing.
427   bool Result = true;
428   bool DoExtraAnalysis = ORE->allowExtraAnalysis(DEBUG_TYPE);
429 
430   for (BasicBlock *BB : TheLoop->blocks()) {
431     // Check whether the BB terminator is a BranchInst. Any other terminator is
432     // not supported yet.
433     auto *Br = dyn_cast<BranchInst>(BB->getTerminator());
434     if (!Br) {
435       reportVectorizationFailure("Unsupported basic block terminator",
436           "loop control flow is not understood by vectorizer",
437           "CFGNotUnderstood", ORE, TheLoop);
438       if (DoExtraAnalysis)
439         Result = false;
440       else
441         return false;
442     }
443 
444     // Check whether the BranchInst is a supported one. Only unconditional
445     // branches, conditional branches with an outer loop invariant condition or
446     // backedges are supported.
447     // FIXME: We skip these checks when VPlan predication is enabled as we
448     // want to allow divergent branches. This whole check will be removed
449     // once VPlan predication is on by default.
450     if (!EnableVPlanPredication && Br && Br->isConditional() &&
451         !TheLoop->isLoopInvariant(Br->getCondition()) &&
452         !LI->isLoopHeader(Br->getSuccessor(0)) &&
453         !LI->isLoopHeader(Br->getSuccessor(1))) {
454       reportVectorizationFailure("Unsupported conditional branch",
455           "loop control flow is not understood by vectorizer",
456           "CFGNotUnderstood", ORE, TheLoop);
457       if (DoExtraAnalysis)
458         Result = false;
459       else
460         return false;
461     }
462   }
463 
464   // Check whether inner loops are uniform. At this point, we only support
465   // simple outer loops scenarios with uniform nested loops.
466   if (!isUniformLoopNest(TheLoop /*loop nest*/,
467                          TheLoop /*context outer loop*/)) {
468     reportVectorizationFailure("Outer loop contains divergent loops",
469         "loop control flow is not understood by vectorizer",
470         "CFGNotUnderstood", ORE, TheLoop);
471     if (DoExtraAnalysis)
472       Result = false;
473     else
474       return false;
475   }
476 
477   // Check whether we are able to set up outer loop induction.
478   if (!setupOuterLoopInductions()) {
479     reportVectorizationFailure("Unsupported outer loop Phi(s)",
480                                "Unsupported outer loop Phi(s)",
481                                "UnsupportedPhi", ORE, TheLoop);
482     if (DoExtraAnalysis)
483       Result = false;
484     else
485       return false;
486   }
487 
488   return Result;
489 }
490 
491 void LoopVectorizationLegality::addInductionPhi(
492     PHINode *Phi, const InductionDescriptor &ID,
493     SmallPtrSetImpl<Value *> &AllowedExit) {
494   Inductions[Phi] = ID;
495 
496   // In case this induction also comes with casts that we know we can ignore
497   // in the vectorized loop body, record them here. All casts could be recorded
498   // here for ignoring, but suffices to record only the first (as it is the
499   // only one that may bw used outside the cast sequence).
500   const SmallVectorImpl<Instruction *> &Casts = ID.getCastInsts();
501   if (!Casts.empty())
502     InductionCastsToIgnore.insert(*Casts.begin());
503 
504   Type *PhiTy = Phi->getType();
505   const DataLayout &DL = Phi->getModule()->getDataLayout();
506 
507   // Get the widest type.
508   if (!PhiTy->isFloatingPointTy()) {
509     if (!WidestIndTy)
510       WidestIndTy = convertPointerToIntegerType(DL, PhiTy);
511     else
512       WidestIndTy = getWiderType(DL, PhiTy, WidestIndTy);
513   }
514 
515   // Int inductions are special because we only allow one IV.
516   if (ID.getKind() == InductionDescriptor::IK_IntInduction &&
517       ID.getConstIntStepValue() && ID.getConstIntStepValue()->isOne() &&
518       isa<Constant>(ID.getStartValue()) &&
519       cast<Constant>(ID.getStartValue())->isNullValue()) {
520 
521     // Use the phi node with the widest type as induction. Use the last
522     // one if there are multiple (no good reason for doing this other
523     // than it is expedient). We've checked that it begins at zero and
524     // steps by one, so this is a canonical induction variable.
525     if (!PrimaryInduction || PhiTy == WidestIndTy)
526       PrimaryInduction = Phi;
527   }
528 
529   // Both the PHI node itself, and the "post-increment" value feeding
530   // back into the PHI node may have external users.
531   // We can allow those uses, except if the SCEVs we have for them rely
532   // on predicates that only hold within the loop, since allowing the exit
533   // currently means re-using this SCEV outside the loop (see PR33706 for more
534   // details).
535   if (PSE.getUnionPredicate().isAlwaysTrue()) {
536     AllowedExit.insert(Phi);
537     AllowedExit.insert(Phi->getIncomingValueForBlock(TheLoop->getLoopLatch()));
538   }
539 
540   LLVM_DEBUG(dbgs() << "LV: Found an induction variable.\n");
541 }
542 
543 bool LoopVectorizationLegality::setupOuterLoopInductions() {
544   BasicBlock *Header = TheLoop->getHeader();
545 
546   // Returns true if a given Phi is a supported induction.
547   auto isSupportedPhi = [&](PHINode &Phi) -> bool {
548     InductionDescriptor ID;
549     if (InductionDescriptor::isInductionPHI(&Phi, TheLoop, PSE, ID) &&
550         ID.getKind() == InductionDescriptor::IK_IntInduction) {
551       addInductionPhi(&Phi, ID, AllowedExit);
552       return true;
553     } else {
554       // Bail out for any Phi in the outer loop header that is not a supported
555       // induction.
556       LLVM_DEBUG(
557           dbgs()
558           << "LV: Found unsupported PHI for outer loop vectorization.\n");
559       return false;
560     }
561   };
562 
563   if (llvm::all_of(Header->phis(), isSupportedPhi))
564     return true;
565   else
566     return false;
567 }
568 
569 bool LoopVectorizationLegality::canVectorizeInstrs() {
570   BasicBlock *Header = TheLoop->getHeader();
571 
572   // Look for the attribute signaling the absence of NaNs.
573   Function &F = *Header->getParent();
574   HasFunNoNaNAttr =
575       F.getFnAttribute("no-nans-fp-math").getValueAsString() == "true";
576 
577   // For each block in the loop.
578   for (BasicBlock *BB : TheLoop->blocks()) {
579     // Scan the instructions in the block and look for hazards.
580     for (Instruction &I : *BB) {
581       if (auto *Phi = dyn_cast<PHINode>(&I)) {
582         Type *PhiTy = Phi->getType();
583         // Check that this PHI type is allowed.
584         if (!PhiTy->isIntegerTy() && !PhiTy->isFloatingPointTy() &&
585             !PhiTy->isPointerTy()) {
586           reportVectorizationFailure("Found a non-int non-pointer PHI",
587                                      "loop control flow is not understood by vectorizer",
588                                      "CFGNotUnderstood", ORE, TheLoop);
589           return false;
590         }
591 
592         // If this PHINode is not in the header block, then we know that we
593         // can convert it to select during if-conversion. No need to check if
594         // the PHIs in this block are induction or reduction variables.
595         if (BB != Header) {
596           // Non-header phi nodes that have outside uses can be vectorized. Add
597           // them to the list of allowed exits.
598           // Unsafe cyclic dependencies with header phis are identified during
599           // legalization for reduction, induction and first order
600           // recurrences.
601           AllowedExit.insert(&I);
602           continue;
603         }
604 
605         // We only allow if-converted PHIs with exactly two incoming values.
606         if (Phi->getNumIncomingValues() != 2) {
607           reportVectorizationFailure("Found an invalid PHI",
608               "loop control flow is not understood by vectorizer",
609               "CFGNotUnderstood", ORE, TheLoop, Phi);
610           return false;
611         }
612 
613         RecurrenceDescriptor RedDes;
614         if (RecurrenceDescriptor::isReductionPHI(Phi, TheLoop, RedDes, DB, AC,
615                                                  DT)) {
616           if (RedDes.hasUnsafeAlgebra())
617             Requirements->addUnsafeAlgebraInst(RedDes.getUnsafeAlgebraInst());
618           AllowedExit.insert(RedDes.getLoopExitInstr());
619           Reductions[Phi] = RedDes;
620           continue;
621         }
622 
623         // TODO: Instead of recording the AllowedExit, it would be good to record the
624         // complementary set: NotAllowedExit. These include (but may not be
625         // limited to):
626         // 1. Reduction phis as they represent the one-before-last value, which
627         // is not available when vectorized
628         // 2. Induction phis and increment when SCEV predicates cannot be used
629         // outside the loop - see addInductionPhi
630         // 3. Non-Phis with outside uses when SCEV predicates cannot be used
631         // outside the loop - see call to hasOutsideLoopUser in the non-phi
632         // handling below
633         // 4. FirstOrderRecurrence phis that can possibly be handled by
634         // extraction.
635         // By recording these, we can then reason about ways to vectorize each
636         // of these NotAllowedExit.
637         InductionDescriptor ID;
638         if (InductionDescriptor::isInductionPHI(Phi, TheLoop, PSE, ID)) {
639           addInductionPhi(Phi, ID, AllowedExit);
640           if (ID.hasUnsafeAlgebra() && !HasFunNoNaNAttr)
641             Requirements->addUnsafeAlgebraInst(ID.getUnsafeAlgebraInst());
642           continue;
643         }
644 
645         if (RecurrenceDescriptor::isFirstOrderRecurrence(Phi, TheLoop,
646                                                          SinkAfter, DT)) {
647           FirstOrderRecurrences.insert(Phi);
648           continue;
649         }
650 
651         // As a last resort, coerce the PHI to a AddRec expression
652         // and re-try classifying it a an induction PHI.
653         if (InductionDescriptor::isInductionPHI(Phi, TheLoop, PSE, ID, true)) {
654           addInductionPhi(Phi, ID, AllowedExit);
655           continue;
656         }
657 
658         reportVectorizationFailure("Found an unidentified PHI",
659             "value that could not be identified as "
660             "reduction is used outside the loop",
661             "NonReductionValueUsedOutsideLoop", ORE, TheLoop, Phi);
662         return false;
663       } // end of PHI handling
664 
665       // We handle calls that:
666       //   * Are debug info intrinsics.
667       //   * Have a mapping to an IR intrinsic.
668       //   * Have a vector version available.
669       auto *CI = dyn_cast<CallInst>(&I);
670       if (CI && !getVectorIntrinsicIDForCall(CI, TLI) &&
671           !isa<DbgInfoIntrinsic>(CI) &&
672           !(CI->getCalledFunction() && TLI &&
673             !VFDatabase::getMappings(*CI).empty())) {
674         // If the call is a recognized math libary call, it is likely that
675         // we can vectorize it given loosened floating-point constraints.
676         LibFunc Func;
677         bool IsMathLibCall =
678             TLI && CI->getCalledFunction() &&
679             CI->getType()->isFloatingPointTy() &&
680             TLI->getLibFunc(CI->getCalledFunction()->getName(), Func) &&
681             TLI->hasOptimizedCodeGen(Func);
682 
683         if (IsMathLibCall) {
684           // TODO: Ideally, we should not use clang-specific language here,
685           // but it's hard to provide meaningful yet generic advice.
686           // Also, should this be guarded by allowExtraAnalysis() and/or be part
687           // of the returned info from isFunctionVectorizable()?
688           reportVectorizationFailure(
689               "Found a non-intrinsic callsite",
690               "library call cannot be vectorized. "
691               "Try compiling with -fno-math-errno, -ffast-math, "
692               "or similar flags",
693               "CantVectorizeLibcall", ORE, TheLoop, CI);
694         } else {
695           reportVectorizationFailure("Found a non-intrinsic callsite",
696                                      "call instruction cannot be vectorized",
697                                      "CantVectorizeLibcall", ORE, TheLoop, CI);
698         }
699         return false;
700       }
701 
702       // Some intrinsics have scalar arguments and should be same in order for
703       // them to be vectorized (i.e. loop invariant).
704       if (CI) {
705         auto *SE = PSE.getSE();
706         Intrinsic::ID IntrinID = getVectorIntrinsicIDForCall(CI, TLI);
707         for (unsigned i = 0, e = CI->getNumArgOperands(); i != e; ++i)
708           if (hasVectorInstrinsicScalarOpd(IntrinID, i)) {
709             if (!SE->isLoopInvariant(PSE.getSCEV(CI->getOperand(i)), TheLoop)) {
710               reportVectorizationFailure("Found unvectorizable intrinsic",
711                   "intrinsic instruction cannot be vectorized",
712                   "CantVectorizeIntrinsic", ORE, TheLoop, CI);
713               return false;
714             }
715           }
716       }
717 
718       // Check that the instruction return type is vectorizable.
719       // Also, we can't vectorize extractelement instructions.
720       if ((!VectorType::isValidElementType(I.getType()) &&
721            !I.getType()->isVoidTy()) ||
722           isa<ExtractElementInst>(I)) {
723         reportVectorizationFailure("Found unvectorizable type",
724             "instruction return type cannot be vectorized",
725             "CantVectorizeInstructionReturnType", ORE, TheLoop, &I);
726         return false;
727       }
728 
729       // Check that the stored type is vectorizable.
730       if (auto *ST = dyn_cast<StoreInst>(&I)) {
731         Type *T = ST->getValueOperand()->getType();
732         if (!VectorType::isValidElementType(T)) {
733           reportVectorizationFailure("Store instruction cannot be vectorized",
734                                      "store instruction cannot be vectorized",
735                                      "CantVectorizeStore", ORE, TheLoop, ST);
736           return false;
737         }
738 
739         // For nontemporal stores, check that a nontemporal vector version is
740         // supported on the target.
741         if (ST->getMetadata(LLVMContext::MD_nontemporal)) {
742           // Arbitrarily try a vector of 2 elements.
743           Type *VecTy = VectorType::get(T, /*NumElements=*/2);
744           assert(VecTy && "did not find vectorized version of stored type");
745           const MaybeAlign Alignment = getLoadStoreAlignment(ST);
746           assert(Alignment && "Alignment should be set");
747           if (!TTI->isLegalNTStore(VecTy, *Alignment)) {
748             reportVectorizationFailure(
749                 "nontemporal store instruction cannot be vectorized",
750                 "nontemporal store instruction cannot be vectorized",
751                 "CantVectorizeNontemporalStore", ORE, TheLoop, ST);
752             return false;
753           }
754         }
755 
756       } else if (auto *LD = dyn_cast<LoadInst>(&I)) {
757         if (LD->getMetadata(LLVMContext::MD_nontemporal)) {
758           // For nontemporal loads, check that a nontemporal vector version is
759           // supported on the target (arbitrarily try a vector of 2 elements).
760           Type *VecTy = VectorType::get(I.getType(), /*NumElements=*/2);
761           assert(VecTy && "did not find vectorized version of load type");
762           const MaybeAlign Alignment = getLoadStoreAlignment(LD);
763           assert(Alignment && "Alignment should be set");
764           if (!TTI->isLegalNTLoad(VecTy, *Alignment)) {
765             reportVectorizationFailure(
766                 "nontemporal load instruction cannot be vectorized",
767                 "nontemporal load instruction cannot be vectorized",
768                 "CantVectorizeNontemporalLoad", ORE, TheLoop, LD);
769             return false;
770           }
771         }
772 
773         // FP instructions can allow unsafe algebra, thus vectorizable by
774         // non-IEEE-754 compliant SIMD units.
775         // This applies to floating-point math operations and calls, not memory
776         // operations, shuffles, or casts, as they don't change precision or
777         // semantics.
778       } else if (I.getType()->isFloatingPointTy() && (CI || I.isBinaryOp()) &&
779                  !I.isFast()) {
780         LLVM_DEBUG(dbgs() << "LV: Found FP op with unsafe algebra.\n");
781         Hints->setPotentiallyUnsafe();
782       }
783 
784       // Reduction instructions are allowed to have exit users.
785       // All other instructions must not have external users.
786       if (hasOutsideLoopUser(TheLoop, &I, AllowedExit)) {
787         // We can safely vectorize loops where instructions within the loop are
788         // used outside the loop only if the SCEV predicates within the loop is
789         // same as outside the loop. Allowing the exit means reusing the SCEV
790         // outside the loop.
791         if (PSE.getUnionPredicate().isAlwaysTrue()) {
792           AllowedExit.insert(&I);
793           continue;
794         }
795         reportVectorizationFailure("Value cannot be used outside the loop",
796                                    "value cannot be used outside the loop",
797                                    "ValueUsedOutsideLoop", ORE, TheLoop, &I);
798         return false;
799       }
800     } // next instr.
801   }
802 
803   if (!PrimaryInduction) {
804     if (Inductions.empty()) {
805       reportVectorizationFailure("Did not find one integer induction var",
806           "loop induction variable could not be identified",
807           "NoInductionVariable", ORE, TheLoop);
808       return false;
809     } else if (!WidestIndTy) {
810       reportVectorizationFailure("Did not find one integer induction var",
811           "integer loop induction variable could not be identified",
812           "NoIntegerInductionVariable", ORE, TheLoop);
813       return false;
814     } else {
815       LLVM_DEBUG(dbgs() << "LV: Did not find one integer induction var.\n");
816     }
817   }
818 
819   // For first order recurrences, we use the previous value (incoming value from
820   // the latch) to check if it dominates all users of the recurrence. Bail out
821   // if we have to sink such an instruction for another recurrence, as the
822   // dominance requirement may not hold after sinking.
823   BasicBlock *LoopLatch = TheLoop->getLoopLatch();
824   if (any_of(FirstOrderRecurrences, [LoopLatch, this](const PHINode *Phi) {
825         Instruction *V =
826             cast<Instruction>(Phi->getIncomingValueForBlock(LoopLatch));
827         return SinkAfter.find(V) != SinkAfter.end();
828       }))
829     return false;
830 
831   // Now we know the widest induction type, check if our found induction
832   // is the same size. If it's not, unset it here and InnerLoopVectorizer
833   // will create another.
834   if (PrimaryInduction && WidestIndTy != PrimaryInduction->getType())
835     PrimaryInduction = nullptr;
836 
837   return true;
838 }
839 
840 bool LoopVectorizationLegality::canVectorizeMemory() {
841   LAI = &(*GetLAA)(*TheLoop);
842   const OptimizationRemarkAnalysis *LAR = LAI->getReport();
843   if (LAR) {
844     ORE->emit([&]() {
845       return OptimizationRemarkAnalysis(Hints->vectorizeAnalysisPassName(),
846                                         "loop not vectorized: ", *LAR);
847     });
848   }
849   if (!LAI->canVectorizeMemory())
850     return false;
851 
852   if (LAI->hasDependenceInvolvingLoopInvariantAddress()) {
853     reportVectorizationFailure("Stores to a uniform address",
854         "write to a loop invariant address could not be vectorized",
855         "CantVectorizeStoreToLoopInvariantAddress", ORE, TheLoop);
856     return false;
857   }
858   Requirements->addRuntimePointerChecks(LAI->getNumRuntimePointerChecks());
859   PSE.addPredicate(LAI->getPSE().getUnionPredicate());
860 
861   return true;
862 }
863 
864 bool LoopVectorizationLegality::isInductionPhi(const Value *V) {
865   Value *In0 = const_cast<Value *>(V);
866   PHINode *PN = dyn_cast_or_null<PHINode>(In0);
867   if (!PN)
868     return false;
869 
870   return Inductions.count(PN);
871 }
872 
873 bool LoopVectorizationLegality::isCastedInductionVariable(const Value *V) {
874   auto *Inst = dyn_cast<Instruction>(V);
875   return (Inst && InductionCastsToIgnore.count(Inst));
876 }
877 
878 bool LoopVectorizationLegality::isInductionVariable(const Value *V) {
879   return isInductionPhi(V) || isCastedInductionVariable(V);
880 }
881 
882 bool LoopVectorizationLegality::isFirstOrderRecurrence(const PHINode *Phi) {
883   return FirstOrderRecurrences.count(Phi);
884 }
885 
886 bool LoopVectorizationLegality::blockNeedsPredication(BasicBlock *BB) {
887   return LoopAccessInfo::blockNeedsPredication(BB, TheLoop, DT);
888 }
889 
890 bool LoopVectorizationLegality::blockCanBePredicated(
891     BasicBlock *BB, SmallPtrSetImpl<Value *> &SafePtrs, bool PreserveGuards) {
892   const bool IsAnnotatedParallel = TheLoop->isAnnotatedParallel();
893 
894   for (Instruction &I : *BB) {
895     // Check that we don't have a constant expression that can trap as operand.
896     for (Value *Operand : I.operands()) {
897       if (auto *C = dyn_cast<Constant>(Operand))
898         if (C->canTrap())
899           return false;
900     }
901     // We might be able to hoist the load.
902     if (I.mayReadFromMemory()) {
903       auto *LI = dyn_cast<LoadInst>(&I);
904       if (!LI)
905         return false;
906       if (!SafePtrs.count(LI->getPointerOperand())) {
907         // !llvm.mem.parallel_loop_access implies if-conversion safety.
908         // Otherwise, record that the load needs (real or emulated) masking
909         // and let the cost model decide.
910         if (!IsAnnotatedParallel || PreserveGuards)
911           MaskedOp.insert(LI);
912         continue;
913       }
914     }
915 
916     if (I.mayWriteToMemory()) {
917       auto *SI = dyn_cast<StoreInst>(&I);
918       if (!SI)
919         return false;
920       // Predicated store requires some form of masking:
921       // 1) masked store HW instruction,
922       // 2) emulation via load-blend-store (only if safe and legal to do so,
923       //    be aware on the race conditions), or
924       // 3) element-by-element predicate check and scalar store.
925       MaskedOp.insert(SI);
926       continue;
927     }
928     if (I.mayThrow())
929       return false;
930   }
931 
932   return true;
933 }
934 
935 bool LoopVectorizationLegality::canVectorizeWithIfConvert() {
936   if (!EnableIfConversion) {
937     reportVectorizationFailure("If-conversion is disabled",
938                                "if-conversion is disabled",
939                                "IfConversionDisabled",
940                                ORE, TheLoop);
941     return false;
942   }
943 
944   assert(TheLoop->getNumBlocks() > 1 && "Single block loops are vectorizable");
945 
946   // A list of pointers which are known to be dereferenceable within scope of
947   // the loop body for each iteration of the loop which executes.  That is,
948   // the memory pointed to can be dereferenced (with the access size implied by
949   // the value's type) unconditionally within the loop header without
950   // introducing a new fault.
951   SmallPtrSet<Value *, 8> SafePointes;
952 
953   // Collect safe addresses.
954   for (BasicBlock *BB : TheLoop->blocks()) {
955     if (!blockNeedsPredication(BB)) {
956       for (Instruction &I : *BB)
957         if (auto *Ptr = getLoadStorePointerOperand(&I))
958           SafePointes.insert(Ptr);
959       continue;
960     }
961 
962     // For a block which requires predication, a address may be safe to access
963     // in the loop w/o predication if we can prove dereferenceability facts
964     // sufficient to ensure it'll never fault within the loop. For the moment,
965     // we restrict this to loads; stores are more complicated due to
966     // concurrency restrictions.
967     ScalarEvolution &SE = *PSE.getSE();
968     for (Instruction &I : *BB) {
969       LoadInst *LI = dyn_cast<LoadInst>(&I);
970       if (LI && !mustSuppressSpeculation(*LI) &&
971           isDereferenceableAndAlignedInLoop(LI, TheLoop, SE, *DT))
972         SafePointes.insert(LI->getPointerOperand());
973     }
974   }
975 
976   // Collect the blocks that need predication.
977   BasicBlock *Header = TheLoop->getHeader();
978   for (BasicBlock *BB : TheLoop->blocks()) {
979     // We don't support switch statements inside loops.
980     if (!isa<BranchInst>(BB->getTerminator())) {
981       reportVectorizationFailure("Loop contains a switch statement",
982                                  "loop contains a switch statement",
983                                  "LoopContainsSwitch", ORE, TheLoop,
984                                  BB->getTerminator());
985       return false;
986     }
987 
988     // We must be able to predicate all blocks that need to be predicated.
989     if (blockNeedsPredication(BB)) {
990       if (!blockCanBePredicated(BB, SafePointes)) {
991         reportVectorizationFailure(
992             "Control flow cannot be substituted for a select",
993             "control flow cannot be substituted for a select",
994             "NoCFGForSelect", ORE, TheLoop,
995             BB->getTerminator());
996         return false;
997       }
998     } else if (BB != Header && !canIfConvertPHINodes(BB)) {
999       reportVectorizationFailure(
1000           "Control flow cannot be substituted for a select",
1001           "control flow cannot be substituted for a select",
1002           "NoCFGForSelect", ORE, TheLoop,
1003           BB->getTerminator());
1004       return false;
1005     }
1006   }
1007 
1008   // We can if-convert this loop.
1009   return true;
1010 }
1011 
1012 // Helper function to canVectorizeLoopNestCFG.
1013 bool LoopVectorizationLegality::canVectorizeLoopCFG(Loop *Lp,
1014                                                     bool UseVPlanNativePath) {
1015   assert((UseVPlanNativePath || Lp->empty()) &&
1016          "VPlan-native path is not enabled.");
1017 
1018   // TODO: ORE should be improved to show more accurate information when an
1019   // outer loop can't be vectorized because a nested loop is not understood or
1020   // legal. Something like: "outer_loop_location: loop not vectorized:
1021   // (inner_loop_location) loop control flow is not understood by vectorizer".
1022 
1023   // Store the result and return it at the end instead of exiting early, in case
1024   // allowExtraAnalysis is used to report multiple reasons for not vectorizing.
1025   bool Result = true;
1026   bool DoExtraAnalysis = ORE->allowExtraAnalysis(DEBUG_TYPE);
1027 
1028   // We must have a loop in canonical form. Loops with indirectbr in them cannot
1029   // be canonicalized.
1030   if (!Lp->getLoopPreheader()) {
1031     reportVectorizationFailure("Loop doesn't have a legal pre-header",
1032         "loop control flow is not understood by vectorizer",
1033         "CFGNotUnderstood", ORE, TheLoop);
1034     if (DoExtraAnalysis)
1035       Result = false;
1036     else
1037       return false;
1038   }
1039 
1040   // We must have a single backedge.
1041   if (Lp->getNumBackEdges() != 1) {
1042     reportVectorizationFailure("The loop must have a single backedge",
1043         "loop control flow is not understood by vectorizer",
1044         "CFGNotUnderstood", ORE, TheLoop);
1045     if (DoExtraAnalysis)
1046       Result = false;
1047     else
1048       return false;
1049   }
1050 
1051   // We must have a single exiting block.
1052   if (!Lp->getExitingBlock()) {
1053     reportVectorizationFailure("The loop must have an exiting block",
1054         "loop control flow is not understood by vectorizer",
1055         "CFGNotUnderstood", ORE, TheLoop);
1056     if (DoExtraAnalysis)
1057       Result = false;
1058     else
1059       return false;
1060   }
1061 
1062   // We only handle bottom-tested loops, i.e. loop in which the condition is
1063   // checked at the end of each iteration. With that we can assume that all
1064   // instructions in the loop are executed the same number of times.
1065   if (Lp->getExitingBlock() != Lp->getLoopLatch()) {
1066     reportVectorizationFailure("The exiting block is not the loop latch",
1067         "loop control flow is not understood by vectorizer",
1068         "CFGNotUnderstood", ORE, TheLoop);
1069     if (DoExtraAnalysis)
1070       Result = false;
1071     else
1072       return false;
1073   }
1074 
1075   return Result;
1076 }
1077 
1078 bool LoopVectorizationLegality::canVectorizeLoopNestCFG(
1079     Loop *Lp, bool UseVPlanNativePath) {
1080   // Store the result and return it at the end instead of exiting early, in case
1081   // allowExtraAnalysis is used to report multiple reasons for not vectorizing.
1082   bool Result = true;
1083   bool DoExtraAnalysis = ORE->allowExtraAnalysis(DEBUG_TYPE);
1084   if (!canVectorizeLoopCFG(Lp, UseVPlanNativePath)) {
1085     if (DoExtraAnalysis)
1086       Result = false;
1087     else
1088       return false;
1089   }
1090 
1091   // Recursively check whether the loop control flow of nested loops is
1092   // understood.
1093   for (Loop *SubLp : *Lp)
1094     if (!canVectorizeLoopNestCFG(SubLp, UseVPlanNativePath)) {
1095       if (DoExtraAnalysis)
1096         Result = false;
1097       else
1098         return false;
1099     }
1100 
1101   return Result;
1102 }
1103 
1104 bool LoopVectorizationLegality::canVectorize(bool UseVPlanNativePath) {
1105   // Store the result and return it at the end instead of exiting early, in case
1106   // allowExtraAnalysis is used to report multiple reasons for not vectorizing.
1107   bool Result = true;
1108 
1109   bool DoExtraAnalysis = ORE->allowExtraAnalysis(DEBUG_TYPE);
1110   // Check whether the loop-related control flow in the loop nest is expected by
1111   // vectorizer.
1112   if (!canVectorizeLoopNestCFG(TheLoop, UseVPlanNativePath)) {
1113     if (DoExtraAnalysis)
1114       Result = false;
1115     else
1116       return false;
1117   }
1118 
1119   // We need to have a loop header.
1120   LLVM_DEBUG(dbgs() << "LV: Found a loop: " << TheLoop->getHeader()->getName()
1121                     << '\n');
1122 
1123   // Specific checks for outer loops. We skip the remaining legal checks at this
1124   // point because they don't support outer loops.
1125   if (!TheLoop->empty()) {
1126     assert(UseVPlanNativePath && "VPlan-native path is not enabled.");
1127 
1128     if (!canVectorizeOuterLoop()) {
1129       reportVectorizationFailure("Unsupported outer loop",
1130                                  "unsupported outer loop",
1131                                  "UnsupportedOuterLoop",
1132                                  ORE, TheLoop);
1133       // TODO: Implement DoExtraAnalysis when subsequent legal checks support
1134       // outer loops.
1135       return false;
1136     }
1137 
1138     LLVM_DEBUG(dbgs() << "LV: We can vectorize this outer loop!\n");
1139     return Result;
1140   }
1141 
1142   assert(TheLoop->empty() && "Inner loop expected.");
1143   // Check if we can if-convert non-single-bb loops.
1144   unsigned NumBlocks = TheLoop->getNumBlocks();
1145   if (NumBlocks != 1 && !canVectorizeWithIfConvert()) {
1146     LLVM_DEBUG(dbgs() << "LV: Can't if-convert the loop.\n");
1147     if (DoExtraAnalysis)
1148       Result = false;
1149     else
1150       return false;
1151   }
1152 
1153   // Check if we can vectorize the instructions and CFG in this loop.
1154   if (!canVectorizeInstrs()) {
1155     LLVM_DEBUG(dbgs() << "LV: Can't vectorize the instructions or CFG\n");
1156     if (DoExtraAnalysis)
1157       Result = false;
1158     else
1159       return false;
1160   }
1161 
1162   // Go over each instruction and look at memory deps.
1163   if (!canVectorizeMemory()) {
1164     LLVM_DEBUG(dbgs() << "LV: Can't vectorize due to memory conflicts\n");
1165     if (DoExtraAnalysis)
1166       Result = false;
1167     else
1168       return false;
1169   }
1170 
1171   LLVM_DEBUG(dbgs() << "LV: We can vectorize this loop"
1172                     << (LAI->getRuntimePointerChecking()->Need
1173                             ? " (with a runtime bound check)"
1174                             : "")
1175                     << "!\n");
1176 
1177   unsigned SCEVThreshold = VectorizeSCEVCheckThreshold;
1178   if (Hints->getForce() == LoopVectorizeHints::FK_Enabled)
1179     SCEVThreshold = PragmaVectorizeSCEVCheckThreshold;
1180 
1181   if (PSE.getUnionPredicate().getComplexity() > SCEVThreshold) {
1182     reportVectorizationFailure("Too many SCEV checks needed",
1183         "Too many SCEV assumptions need to be made and checked at runtime",
1184         "TooManySCEVRunTimeChecks", ORE, TheLoop);
1185     if (DoExtraAnalysis)
1186       Result = false;
1187     else
1188       return false;
1189   }
1190 
1191   // Okay! We've done all the tests. If any have failed, return false. Otherwise
1192   // we can vectorize, and at this point we don't have any other mem analysis
1193   // which may limit our maximum vectorization factor, so just return true with
1194   // no restrictions.
1195   return Result;
1196 }
1197 
1198 bool LoopVectorizationLegality::prepareToFoldTailByMasking() {
1199 
1200   LLVM_DEBUG(dbgs() << "LV: checking if tail can be folded by masking.\n");
1201 
1202   if (!PrimaryInduction) {
1203     reportVectorizationFailure(
1204         "No primary induction, cannot fold tail by masking",
1205         "Missing a primary induction variable in the loop, which is "
1206         "needed in order to fold tail by masking as required.",
1207         "NoPrimaryInduction", ORE, TheLoop);
1208     return false;
1209   }
1210 
1211   SmallPtrSet<const Value *, 8> ReductionLiveOuts;
1212 
1213   for (auto &Reduction : *getReductionVars())
1214     ReductionLiveOuts.insert(Reduction.second.getLoopExitInstr());
1215 
1216   // TODO: handle non-reduction outside users when tail is folded by masking.
1217   for (auto *AE : AllowedExit) {
1218     // Check that all users of allowed exit values are inside the loop or
1219     // are the live-out of a reduction.
1220     if (ReductionLiveOuts.count(AE))
1221       continue;
1222     for (User *U : AE->users()) {
1223       Instruction *UI = cast<Instruction>(U);
1224       if (TheLoop->contains(UI))
1225         continue;
1226       reportVectorizationFailure(
1227           "Cannot fold tail by masking, loop has an outside user for",
1228           "Cannot fold tail by masking in the presence of live outs.",
1229           "LiveOutFoldingTailByMasking", ORE, TheLoop, UI);
1230       return false;
1231     }
1232   }
1233 
1234   // The list of pointers that we can safely read and write to remains empty.
1235   SmallPtrSet<Value *, 8> SafePointers;
1236 
1237   // Check and mark all blocks for predication, including those that ordinarily
1238   // do not need predication such as the header block.
1239   for (BasicBlock *BB : TheLoop->blocks()) {
1240     if (!blockCanBePredicated(BB, SafePointers, /* MaskAllLoads= */ true)) {
1241       reportVectorizationFailure(
1242           "Cannot fold tail by masking as required",
1243           "control flow cannot be substituted for a select",
1244           "NoCFGForSelect", ORE, TheLoop,
1245           BB->getTerminator());
1246       return false;
1247     }
1248   }
1249 
1250   LLVM_DEBUG(dbgs() << "LV: can fold tail by masking.\n");
1251   return true;
1252 }
1253 
1254 } // namespace llvm
1255