1 //===- LoopVectorizationLegality.cpp --------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 // This file provides loop vectorization legality analysis. Original code
10 // resided in LoopVectorize.cpp for a long time.
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).
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"
25 #define LV_NAME "loop-vectorize"
26 #define DEBUG_TYPE LV_NAME
28 extern cl::opt
<bool> EnableVPlanPredication
;
31 EnableIfConversion("enable-if-conversion", cl::init(true), cl::Hidden
,
32 cl::desc("Enable if-conversion during vectorization."));
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."));
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."));
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"));
48 /// Maximum vectorization interleave count.
49 static const unsigned MaxInterleaveFactor
= 16;
53 bool LoopVectorizeHints::Hint::validate(unsigned Val
) {
56 return isPowerOf2_32(Val
) && Val
<= VectorizerParams::MaxVectorWidth
;
58 return isPowerOf2_32(Val
) && Val
<= MaxInterleaveFactor
;
63 return (Val
== 0 || Val
== 1);
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", 0, HK_PREDICATE
), TheLoop(L
),
77 // Populate values with existing loop metadata.
78 getHintsFromMetadata();
80 // force-vector-interleave overrides DisableInterleaving.
81 if (VectorizerParams::isInterleaveForced())
82 Interleave
.Value
= VectorizerParams::VectorizationInterleave
;
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");
93 void LoopVectorizeHints::setAlreadyVectorized() {
94 LLVMContext
&Context
= TheLoop
->getHeader()->getContext();
96 MDNode
*IsVectorizedMD
= MDNode::get(
98 {MDString::get(Context
, "llvm.loop.isvectorized"),
99 ConstantAsMetadata::get(ConstantInt::get(Context
, APInt(32, 1)))});
100 MDNode
*LoopID
= TheLoop
->getLoopID();
102 makePostTransformationMetadata(Context
, LoopID
,
103 {Twine(Prefix(), "vectorize.").str(),
104 Twine(Prefix(), "interleave.").str()},
106 TheLoop
->setLoopID(NewLoopID
);
108 // Update internal cache.
109 IsVectorized
.Value
= 1;
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();
120 if (VectorizeOnlyWhenForced
&& getForce() != LoopVectorizeHints::FK_Enabled
) {
121 LLVM_DEBUG(dbgs() << "LV: Not vectorizing: No #pragma vectorize enable.\n");
122 emitRemarkWithHints();
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.
132 return OptimizationRemarkAnalysis(vectorizeAnalysisPassName(),
133 "AllDisabled", L
->getStartLoc(),
135 << "loop not vectorized: vectorization and interleaving are "
136 "explicitly disabled, or the loop has already been "
145 void LoopVectorizeHints::emitRemarkWithHints() const {
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";
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
);
171 const char *LoopVectorizeHints::vectorizeAnalysisPassName() const {
174 if (getForce() == LoopVectorizeHints::FK_Disabled
)
176 if (getForce() == LoopVectorizeHints::FK_Undefined
&& getWidth() == 0)
178 return OptimizationRemarkAnalysis::AlwaysPrint
;
181 void LoopVectorizeHints::getHintsFromMetadata() {
182 MDNode
*LoopID
= TheLoop
->getLoopID();
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");
190 for (unsigned i
= 1, ie
= LoopID
->getNumOperands(); i
< ie
; ++i
) {
191 const MDString
*S
= nullptr;
192 SmallVector
<Metadata
*, 4> Args
;
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)
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
));
203 S
= dyn_cast
<MDString
>(LoopID
->getOperand(i
));
204 assert(Args
.size() == 0 && "too many arguments for MDString");
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]);
217 void LoopVectorizeHints::setHint(StringRef Name
, Metadata
*Arg
) {
218 if (!Name
.startswith(Prefix()))
220 Name
= Name
.substr(Prefix().size(), StringRef::npos
);
222 const ConstantInt
*C
= mdconst::dyn_extract
<ConstantInt
>(Arg
);
225 unsigned Val
= C
->getZExtValue();
227 Hint
*Hints
[] = {&Width
, &Interleave
, &Force
, &IsVectorized
, &Predicate
};
228 for (auto H
: Hints
) {
229 if (Name
== H
->Name
) {
230 if (H
->validate(Val
))
233 LLVM_DEBUG(dbgs() << "LV: ignoring invalid hint '" << Name
<< "'\n");
239 bool LoopVectorizationRequirements::doesNotMeet(
240 Function
*F
, Loop
*L
, const LoopVectorizeHints
&Hints
) {
241 const char *PassName
= Hints
.vectorizeAnalysisPassName();
243 if (UnsafeAlgebraInst
&& !Hints
.allowReordering()) {
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";
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
) {
262 return OptimizationRemarkAnalysisAliasing(PassName
, "CantReorderMemOps",
265 << "loop not vectorized: cannot prove it is safe to reorder "
268 LLVM_DEBUG(dbgs() << "LV: Too many memory checks needed.\n");
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.
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.");
301 // If Lp is the outer loop, it's uniform by definition.
304 assert(OuterLp
->contains(Lp
) && "OuterLp must contain Lp.");
307 PHINode
*IV
= Lp
->getCanonicalInductionVariable();
309 LLVM_DEBUG(dbgs() << "LV: Canonical IV not found.\n");
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");
322 auto *LatchCmp
= dyn_cast
<CmpInst
>(LatchBr
->getCondition());
325 dbgs() << "LV: Loop latch condition is not a compare instruction.\n");
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");
341 // Return true if \p Lp and all its nested loops are uniform with regard to \p
343 static bool isUniformLoopNest(Loop
*Lp
, Loop
*OuterLp
) {
344 if (!isUniformLoop(Lp
, OuterLp
))
347 // Check if nested loops are uniform.
348 for (Loop
*SubLp
: *Lp
)
349 if (!isUniformLoopNest(SubLp
, OuterLp
))
355 /// Check whether it is safe to if-convert this phi node.
357 /// Phi nodes with constant expressions that can trap are not safe to if
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
))
369 static Type
*convertPointerToIntegerType(const DataLayout
&DL
, Type
*Ty
) {
370 if (Ty
->isPointerTy())
371 return DL
.getIntPtrType(Ty
);
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());
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())
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');
408 int LoopVectorizationLegality::isConsecutivePtr(Value
*Ptr
) {
409 const ValueToValueMap
&Strides
=
410 getSymbolicStrides() ? *getSymbolicStrides() : ValueToValueMap();
412 int Stride
= getPtrStride(PSE
, Ptr
, TheLoop
, Strides
, true, false);
413 if (Stride
== 1 || Stride
== -1)
418 bool LoopVectorizationLegality::isUniform(Value
*V
) {
419 return LAI
->isUniform(V
);
422 bool LoopVectorizationLegality::canVectorizeOuterLoop() {
423 assert(!TheLoop
->empty() && "We are not vectorizing an outer loop.");
424 // Store the result and return it at the end instead of exiting early, in case
425 // allowExtraAnalysis is used to report multiple reasons for not vectorizing.
427 bool DoExtraAnalysis
= ORE
->allowExtraAnalysis(DEBUG_TYPE
);
429 for (BasicBlock
*BB
: TheLoop
->blocks()) {
430 // Check whether the BB terminator is a BranchInst. Any other terminator is
431 // not supported yet.
432 auto *Br
= dyn_cast
<BranchInst
>(BB
->getTerminator());
434 reportVectorizationFailure("Unsupported basic block terminator",
435 "loop control flow is not understood by vectorizer",
436 "CFGNotUnderstood", ORE
, TheLoop
);
443 // Check whether the BranchInst is a supported one. Only unconditional
444 // branches, conditional branches with an outer loop invariant condition or
445 // backedges are supported.
446 // FIXME: We skip these checks when VPlan predication is enabled as we
447 // want to allow divergent branches. This whole check will be removed
448 // once VPlan predication is on by default.
449 if (!EnableVPlanPredication
&& Br
&& Br
->isConditional() &&
450 !TheLoop
->isLoopInvariant(Br
->getCondition()) &&
451 !LI
->isLoopHeader(Br
->getSuccessor(0)) &&
452 !LI
->isLoopHeader(Br
->getSuccessor(1))) {
453 reportVectorizationFailure("Unsupported conditional branch",
454 "loop control flow is not understood by vectorizer",
455 "CFGNotUnderstood", ORE
, TheLoop
);
463 // Check whether inner loops are uniform. At this point, we only support
464 // simple outer loops scenarios with uniform nested loops.
465 if (!isUniformLoopNest(TheLoop
/*loop nest*/,
466 TheLoop
/*context outer loop*/)) {
467 reportVectorizationFailure("Outer loop contains divergent loops",
468 "loop control flow is not understood by vectorizer",
469 "CFGNotUnderstood", ORE
, TheLoop
);
476 // Check whether we are able to set up outer loop induction.
477 if (!setupOuterLoopInductions()) {
478 reportVectorizationFailure("Unsupported outer loop Phi(s)",
479 "Unsupported outer loop Phi(s)",
480 "UnsupportedPhi", ORE
, TheLoop
);
490 void LoopVectorizationLegality::addInductionPhi(
491 PHINode
*Phi
, const InductionDescriptor
&ID
,
492 SmallPtrSetImpl
<Value
*> &AllowedExit
) {
493 Inductions
[Phi
] = ID
;
495 // In case this induction also comes with casts that we know we can ignore
496 // in the vectorized loop body, record them here. All casts could be recorded
497 // here for ignoring, but suffices to record only the first (as it is the
498 // only one that may bw used outside the cast sequence).
499 const SmallVectorImpl
<Instruction
*> &Casts
= ID
.getCastInsts();
501 InductionCastsToIgnore
.insert(*Casts
.begin());
503 Type
*PhiTy
= Phi
->getType();
504 const DataLayout
&DL
= Phi
->getModule()->getDataLayout();
506 // Get the widest type.
507 if (!PhiTy
->isFloatingPointTy()) {
509 WidestIndTy
= convertPointerToIntegerType(DL
, PhiTy
);
511 WidestIndTy
= getWiderType(DL
, PhiTy
, WidestIndTy
);
514 // Int inductions are special because we only allow one IV.
515 if (ID
.getKind() == InductionDescriptor::IK_IntInduction
&&
516 ID
.getConstIntStepValue() && ID
.getConstIntStepValue()->isOne() &&
517 isa
<Constant
>(ID
.getStartValue()) &&
518 cast
<Constant
>(ID
.getStartValue())->isNullValue()) {
520 // Use the phi node with the widest type as induction. Use the last
521 // one if there are multiple (no good reason for doing this other
522 // than it is expedient). We've checked that it begins at zero and
523 // steps by one, so this is a canonical induction variable.
524 if (!PrimaryInduction
|| PhiTy
== WidestIndTy
)
525 PrimaryInduction
= Phi
;
528 // Both the PHI node itself, and the "post-increment" value feeding
529 // back into the PHI node may have external users.
530 // We can allow those uses, except if the SCEVs we have for them rely
531 // on predicates that only hold within the loop, since allowing the exit
532 // currently means re-using this SCEV outside the loop (see PR33706 for more
534 if (PSE
.getUnionPredicate().isAlwaysTrue()) {
535 AllowedExit
.insert(Phi
);
536 AllowedExit
.insert(Phi
->getIncomingValueForBlock(TheLoop
->getLoopLatch()));
539 LLVM_DEBUG(dbgs() << "LV: Found an induction variable.\n");
542 bool LoopVectorizationLegality::setupOuterLoopInductions() {
543 BasicBlock
*Header
= TheLoop
->getHeader();
545 // Returns true if a given Phi is a supported induction.
546 auto isSupportedPhi
= [&](PHINode
&Phi
) -> bool {
547 InductionDescriptor ID
;
548 if (InductionDescriptor::isInductionPHI(&Phi
, TheLoop
, PSE
, ID
) &&
549 ID
.getKind() == InductionDescriptor::IK_IntInduction
) {
550 addInductionPhi(&Phi
, ID
, AllowedExit
);
553 // Bail out for any Phi in the outer loop header that is not a supported
557 << "LV: Found unsupported PHI for outer loop vectorization.\n");
562 if (llvm::all_of(Header
->phis(), isSupportedPhi
))
568 bool LoopVectorizationLegality::canVectorizeInstrs() {
569 BasicBlock
*Header
= TheLoop
->getHeader();
571 // Look for the attribute signaling the absence of NaNs.
572 Function
&F
= *Header
->getParent();
574 F
.getFnAttribute("no-nans-fp-math").getValueAsString() == "true";
576 // For each block in the loop.
577 for (BasicBlock
*BB
: TheLoop
->blocks()) {
578 // Scan the instructions in the block and look for hazards.
579 for (Instruction
&I
: *BB
) {
580 if (auto *Phi
= dyn_cast
<PHINode
>(&I
)) {
581 Type
*PhiTy
= Phi
->getType();
582 // Check that this PHI type is allowed.
583 if (!PhiTy
->isIntegerTy() && !PhiTy
->isFloatingPointTy() &&
584 !PhiTy
->isPointerTy()) {
585 reportVectorizationFailure("Found a non-int non-pointer PHI",
586 "loop control flow is not understood by vectorizer",
587 "CFGNotUnderstood", ORE
, TheLoop
);
591 // If this PHINode is not in the header block, then we know that we
592 // can convert it to select during if-conversion. No need to check if
593 // the PHIs in this block are induction or reduction variables.
595 // Non-header phi nodes that have outside uses can be vectorized. Add
596 // them to the list of allowed exits.
597 // Unsafe cyclic dependencies with header phis are identified during
598 // legalization for reduction, induction and first order
600 AllowedExit
.insert(&I
);
604 // We only allow if-converted PHIs with exactly two incoming values.
605 if (Phi
->getNumIncomingValues() != 2) {
606 reportVectorizationFailure("Found an invalid PHI",
607 "loop control flow is not understood by vectorizer",
608 "CFGNotUnderstood", ORE
, TheLoop
, Phi
);
612 RecurrenceDescriptor RedDes
;
613 if (RecurrenceDescriptor::isReductionPHI(Phi
, TheLoop
, RedDes
, DB
, AC
,
615 if (RedDes
.hasUnsafeAlgebra())
616 Requirements
->addUnsafeAlgebraInst(RedDes
.getUnsafeAlgebraInst());
617 AllowedExit
.insert(RedDes
.getLoopExitInstr());
618 Reductions
[Phi
] = RedDes
;
622 // TODO: Instead of recording the AllowedExit, it would be good to record the
623 // complementary set: NotAllowedExit. These include (but may not be
625 // 1. Reduction phis as they represent the one-before-last value, which
626 // is not available when vectorized
627 // 2. Induction phis and increment when SCEV predicates cannot be used
628 // outside the loop - see addInductionPhi
629 // 3. Non-Phis with outside uses when SCEV predicates cannot be used
630 // outside the loop - see call to hasOutsideLoopUser in the non-phi
632 // 4. FirstOrderRecurrence phis that can possibly be handled by
634 // By recording these, we can then reason about ways to vectorize each
635 // of these NotAllowedExit.
636 InductionDescriptor ID
;
637 if (InductionDescriptor::isInductionPHI(Phi
, TheLoop
, PSE
, ID
)) {
638 addInductionPhi(Phi
, ID
, AllowedExit
);
639 if (ID
.hasUnsafeAlgebra() && !HasFunNoNaNAttr
)
640 Requirements
->addUnsafeAlgebraInst(ID
.getUnsafeAlgebraInst());
644 if (RecurrenceDescriptor::isFirstOrderRecurrence(Phi
, TheLoop
,
646 FirstOrderRecurrences
.insert(Phi
);
650 // As a last resort, coerce the PHI to a AddRec expression
651 // and re-try classifying it a an induction PHI.
652 if (InductionDescriptor::isInductionPHI(Phi
, TheLoop
, PSE
, ID
, true)) {
653 addInductionPhi(Phi
, ID
, AllowedExit
);
657 reportVectorizationFailure("Found an unidentified PHI",
658 "value that could not be identified as "
659 "reduction is used outside the loop",
660 "NonReductionValueUsedOutsideLoop", ORE
, TheLoop
, Phi
);
662 } // end of PHI handling
664 // We handle calls that:
665 // * Are debug info intrinsics.
666 // * Have a mapping to an IR intrinsic.
667 // * Have a vector version available.
668 auto *CI
= dyn_cast
<CallInst
>(&I
);
669 if (CI
&& !getVectorIntrinsicIDForCall(CI
, TLI
) &&
670 !isa
<DbgInfoIntrinsic
>(CI
) &&
671 !(CI
->getCalledFunction() && TLI
&&
672 TLI
->isFunctionVectorizable(CI
->getCalledFunction()->getName()))) {
673 // If the call is a recognized math libary call, it is likely that
674 // we can vectorize it given loosened floating-point constraints.
677 TLI
&& CI
->getCalledFunction() &&
678 CI
->getType()->isFloatingPointTy() &&
679 TLI
->getLibFunc(CI
->getCalledFunction()->getName(), Func
) &&
680 TLI
->hasOptimizedCodeGen(Func
);
683 // TODO: Ideally, we should not use clang-specific language here,
684 // but it's hard to provide meaningful yet generic advice.
685 // Also, should this be guarded by allowExtraAnalysis() and/or be part
686 // of the returned info from isFunctionVectorizable()?
687 reportVectorizationFailure("Found a non-intrinsic callsite",
688 "library call cannot be vectorized. "
689 "Try compiling with -fno-math-errno, -ffast-math, "
691 "CantVectorizeLibcall", ORE
, TheLoop
, CI
);
693 reportVectorizationFailure("Found a non-intrinsic callsite",
694 "call instruction cannot be vectorized",
695 "CantVectorizeLibcall", ORE
, TheLoop
, CI
);
700 // Some intrinsics have scalar arguments and should be same in order for
701 // them to be vectorized (i.e. loop invariant).
703 auto *SE
= PSE
.getSE();
704 Intrinsic::ID IntrinID
= getVectorIntrinsicIDForCall(CI
, TLI
);
705 for (unsigned i
= 0, e
= CI
->getNumArgOperands(); i
!= e
; ++i
)
706 if (hasVectorInstrinsicScalarOpd(IntrinID
, i
)) {
707 if (!SE
->isLoopInvariant(PSE
.getSCEV(CI
->getOperand(i
)), TheLoop
)) {
708 reportVectorizationFailure("Found unvectorizable intrinsic",
709 "intrinsic instruction cannot be vectorized",
710 "CantVectorizeIntrinsic", ORE
, TheLoop
, CI
);
716 // Check that the instruction return type is vectorizable.
717 // Also, we can't vectorize extractelement instructions.
718 if ((!VectorType::isValidElementType(I
.getType()) &&
719 !I
.getType()->isVoidTy()) ||
720 isa
<ExtractElementInst
>(I
)) {
721 reportVectorizationFailure("Found unvectorizable type",
722 "instruction return type cannot be vectorized",
723 "CantVectorizeInstructionReturnType", ORE
, TheLoop
, &I
);
727 // Check that the stored type is vectorizable.
728 if (auto *ST
= dyn_cast
<StoreInst
>(&I
)) {
729 Type
*T
= ST
->getValueOperand()->getType();
730 if (!VectorType::isValidElementType(T
)) {
731 reportVectorizationFailure("Store instruction cannot be vectorized",
732 "store instruction cannot be vectorized",
733 "CantVectorizeStore", ORE
, TheLoop
, ST
);
737 // For nontemporal stores, check that a nontemporal vector version is
738 // supported on the target.
739 if (ST
->getMetadata(LLVMContext::MD_nontemporal
)) {
740 // Arbitrarily try a vector of 2 elements.
741 Type
*VecTy
= VectorType::get(T
, /*NumElements=*/2);
742 assert(VecTy
&& "did not find vectorized version of stored type");
743 unsigned Alignment
= getLoadStoreAlignment(ST
);
744 assert(Alignment
&& "Alignment should be set");
745 if (!TTI
->isLegalNTStore(VecTy
, llvm::Align(Alignment
))) {
746 reportVectorizationFailure(
747 "nontemporal store instruction cannot be vectorized",
748 "nontemporal store instruction cannot be vectorized",
749 "CantVectorizeNontemporalStore", ORE
, TheLoop
, ST
);
754 } else if (auto *LD
= dyn_cast
<LoadInst
>(&I
)) {
755 if (LD
->getMetadata(LLVMContext::MD_nontemporal
)) {
756 // For nontemporal loads, check that a nontemporal vector version is
757 // supported on the target (arbitrarily try a vector of 2 elements).
758 Type
*VecTy
= VectorType::get(I
.getType(), /*NumElements=*/2);
759 assert(VecTy
&& "did not find vectorized version of load type");
760 unsigned Alignment
= getLoadStoreAlignment(LD
);
761 assert(Alignment
&& "Alignment should be set");
762 if (!TTI
->isLegalNTLoad(VecTy
, llvm::Align(Alignment
))) {
763 reportVectorizationFailure(
764 "nontemporal load instruction cannot be vectorized",
765 "nontemporal load instruction cannot be vectorized",
766 "CantVectorizeNontemporalLoad", ORE
, TheLoop
, LD
);
771 // FP instructions can allow unsafe algebra, thus vectorizable by
772 // non-IEEE-754 compliant SIMD units.
773 // This applies to floating-point math operations and calls, not memory
774 // operations, shuffles, or casts, as they don't change precision or
776 } else if (I
.getType()->isFloatingPointTy() && (CI
|| I
.isBinaryOp()) &&
778 LLVM_DEBUG(dbgs() << "LV: Found FP op with unsafe algebra.\n");
779 Hints
->setPotentiallyUnsafe();
782 // Reduction instructions are allowed to have exit users.
783 // All other instructions must not have external users.
784 if (hasOutsideLoopUser(TheLoop
, &I
, AllowedExit
)) {
785 // We can safely vectorize loops where instructions within the loop are
786 // used outside the loop only if the SCEV predicates within the loop is
787 // same as outside the loop. Allowing the exit means reusing the SCEV
789 if (PSE
.getUnionPredicate().isAlwaysTrue()) {
790 AllowedExit
.insert(&I
);
793 reportVectorizationFailure("Value cannot be used outside the loop",
794 "value cannot be used outside the loop",
795 "ValueUsedOutsideLoop", ORE
, TheLoop
, &I
);
801 if (!PrimaryInduction
) {
802 if (Inductions
.empty()) {
803 reportVectorizationFailure("Did not find one integer induction var",
804 "loop induction variable could not be identified",
805 "NoInductionVariable", ORE
, TheLoop
);
807 } else if (!WidestIndTy
) {
808 reportVectorizationFailure("Did not find one integer induction var",
809 "integer loop induction variable could not be identified",
810 "NoIntegerInductionVariable", ORE
, TheLoop
);
813 LLVM_DEBUG(dbgs() << "LV: Did not find one integer induction var.\n");
817 // Now we know the widest induction type, check if our found induction
818 // is the same size. If it's not, unset it here and InnerLoopVectorizer
819 // will create another.
820 if (PrimaryInduction
&& WidestIndTy
!= PrimaryInduction
->getType())
821 PrimaryInduction
= nullptr;
826 bool LoopVectorizationLegality::canVectorizeMemory() {
827 LAI
= &(*GetLAA
)(*TheLoop
);
828 const OptimizationRemarkAnalysis
*LAR
= LAI
->getReport();
831 return OptimizationRemarkAnalysis(Hints
->vectorizeAnalysisPassName(),
832 "loop not vectorized: ", *LAR
);
835 if (!LAI
->canVectorizeMemory())
838 if (LAI
->hasDependenceInvolvingLoopInvariantAddress()) {
839 reportVectorizationFailure("Stores to a uniform address",
840 "write to a loop invariant address could not be vectorized",
841 "CantVectorizeStoreToLoopInvariantAddress", ORE
, TheLoop
);
844 Requirements
->addRuntimePointerChecks(LAI
->getNumRuntimePointerChecks());
845 PSE
.addPredicate(LAI
->getPSE().getUnionPredicate());
850 bool LoopVectorizationLegality::isInductionPhi(const Value
*V
) {
851 Value
*In0
= const_cast<Value
*>(V
);
852 PHINode
*PN
= dyn_cast_or_null
<PHINode
>(In0
);
856 return Inductions
.count(PN
);
859 bool LoopVectorizationLegality::isCastedInductionVariable(const Value
*V
) {
860 auto *Inst
= dyn_cast
<Instruction
>(V
);
861 return (Inst
&& InductionCastsToIgnore
.count(Inst
));
864 bool LoopVectorizationLegality::isInductionVariable(const Value
*V
) {
865 return isInductionPhi(V
) || isCastedInductionVariable(V
);
868 bool LoopVectorizationLegality::isFirstOrderRecurrence(const PHINode
*Phi
) {
869 return FirstOrderRecurrences
.count(Phi
);
872 bool LoopVectorizationLegality::blockNeedsPredication(BasicBlock
*BB
) {
873 return LoopAccessInfo::blockNeedsPredication(BB
, TheLoop
, DT
);
876 bool LoopVectorizationLegality::blockCanBePredicated(
877 BasicBlock
*BB
, SmallPtrSetImpl
<Value
*> &SafePtrs
, bool PreserveGuards
) {
878 const bool IsAnnotatedParallel
= TheLoop
->isAnnotatedParallel();
880 for (Instruction
&I
: *BB
) {
881 // Check that we don't have a constant expression that can trap as operand.
882 for (Value
*Operand
: I
.operands()) {
883 if (auto *C
= dyn_cast
<Constant
>(Operand
))
887 // We might be able to hoist the load.
888 if (I
.mayReadFromMemory()) {
889 auto *LI
= dyn_cast
<LoadInst
>(&I
);
892 if (!SafePtrs
.count(LI
->getPointerOperand())) {
893 // !llvm.mem.parallel_loop_access implies if-conversion safety.
894 // Otherwise, record that the load needs (real or emulated) masking
895 // and let the cost model decide.
896 if (!IsAnnotatedParallel
|| PreserveGuards
)
902 if (I
.mayWriteToMemory()) {
903 auto *SI
= dyn_cast
<StoreInst
>(&I
);
906 // Predicated store requires some form of masking:
907 // 1) masked store HW instruction,
908 // 2) emulation via load-blend-store (only if safe and legal to do so,
909 // be aware on the race conditions), or
910 // 3) element-by-element predicate check and scalar store.
921 bool LoopVectorizationLegality::canVectorizeWithIfConvert() {
922 if (!EnableIfConversion
) {
923 reportVectorizationFailure("If-conversion is disabled",
924 "if-conversion is disabled",
925 "IfConversionDisabled",
930 assert(TheLoop
->getNumBlocks() > 1 && "Single block loops are vectorizable");
932 // A list of pointers which are known to be dereferenceable within scope of
933 // the loop body for each iteration of the loop which executes. That is,
934 // the memory pointed to can be dereferenced (with the access size implied by
935 // the value's type) unconditionally within the loop header without
936 // introducing a new fault.
937 SmallPtrSet
<Value
*, 8> SafePointes
;
939 // Collect safe addresses.
940 for (BasicBlock
*BB
: TheLoop
->blocks()) {
941 if (!blockNeedsPredication(BB
)) {
942 for (Instruction
&I
: *BB
)
943 if (auto *Ptr
= getLoadStorePointerOperand(&I
))
944 SafePointes
.insert(Ptr
);
948 // For a block which requires predication, a address may be safe to access
949 // in the loop w/o predication if we can prove dereferenceability facts
950 // sufficient to ensure it'll never fault within the loop. For the moment,
951 // we restrict this to loads; stores are more complicated due to
952 // concurrency restrictions.
953 ScalarEvolution
&SE
= *PSE
.getSE();
954 for (Instruction
&I
: *BB
) {
955 LoadInst
*LI
= dyn_cast
<LoadInst
>(&I
);
956 if (LI
&& !mustSuppressSpeculation(*LI
) &&
957 isDereferenceableAndAlignedInLoop(LI
, TheLoop
, SE
, *DT
))
958 SafePointes
.insert(LI
->getPointerOperand());
962 // Collect the blocks that need predication.
963 BasicBlock
*Header
= TheLoop
->getHeader();
964 for (BasicBlock
*BB
: TheLoop
->blocks()) {
965 // We don't support switch statements inside loops.
966 if (!isa
<BranchInst
>(BB
->getTerminator())) {
967 reportVectorizationFailure("Loop contains a switch statement",
968 "loop contains a switch statement",
969 "LoopContainsSwitch", ORE
, TheLoop
,
970 BB
->getTerminator());
974 // We must be able to predicate all blocks that need to be predicated.
975 if (blockNeedsPredication(BB
)) {
976 if (!blockCanBePredicated(BB
, SafePointes
)) {
977 reportVectorizationFailure(
978 "Control flow cannot be substituted for a select",
979 "control flow cannot be substituted for a select",
980 "NoCFGForSelect", ORE
, TheLoop
,
981 BB
->getTerminator());
984 } else if (BB
!= Header
&& !canIfConvertPHINodes(BB
)) {
985 reportVectorizationFailure(
986 "Control flow cannot be substituted for a select",
987 "control flow cannot be substituted for a select",
988 "NoCFGForSelect", ORE
, TheLoop
,
989 BB
->getTerminator());
994 // We can if-convert this loop.
998 // Helper function to canVectorizeLoopNestCFG.
999 bool LoopVectorizationLegality::canVectorizeLoopCFG(Loop
*Lp
,
1000 bool UseVPlanNativePath
) {
1001 assert((UseVPlanNativePath
|| Lp
->empty()) &&
1002 "VPlan-native path is not enabled.");
1004 // TODO: ORE should be improved to show more accurate information when an
1005 // outer loop can't be vectorized because a nested loop is not understood or
1006 // legal. Something like: "outer_loop_location: loop not vectorized:
1007 // (inner_loop_location) loop control flow is not understood by vectorizer".
1009 // Store the result and return it at the end instead of exiting early, in case
1010 // allowExtraAnalysis is used to report multiple reasons for not vectorizing.
1012 bool DoExtraAnalysis
= ORE
->allowExtraAnalysis(DEBUG_TYPE
);
1014 // We must have a loop in canonical form. Loops with indirectbr in them cannot
1015 // be canonicalized.
1016 if (!Lp
->getLoopPreheader()) {
1017 reportVectorizationFailure("Loop doesn't have a legal pre-header",
1018 "loop control flow is not understood by vectorizer",
1019 "CFGNotUnderstood", ORE
, TheLoop
);
1020 if (DoExtraAnalysis
)
1026 // We must have a single backedge.
1027 if (Lp
->getNumBackEdges() != 1) {
1028 reportVectorizationFailure("The loop must have a single backedge",
1029 "loop control flow is not understood by vectorizer",
1030 "CFGNotUnderstood", ORE
, TheLoop
);
1031 if (DoExtraAnalysis
)
1037 // We must have a single exiting block.
1038 if (!Lp
->getExitingBlock()) {
1039 reportVectorizationFailure("The loop must have an exiting block",
1040 "loop control flow is not understood by vectorizer",
1041 "CFGNotUnderstood", ORE
, TheLoop
);
1042 if (DoExtraAnalysis
)
1048 // We only handle bottom-tested loops, i.e. loop in which the condition is
1049 // checked at the end of each iteration. With that we can assume that all
1050 // instructions in the loop are executed the same number of times.
1051 if (Lp
->getExitingBlock() != Lp
->getLoopLatch()) {
1052 reportVectorizationFailure("The exiting block is not the loop latch",
1053 "loop control flow is not understood by vectorizer",
1054 "CFGNotUnderstood", ORE
, TheLoop
);
1055 if (DoExtraAnalysis
)
1064 bool LoopVectorizationLegality::canVectorizeLoopNestCFG(
1065 Loop
*Lp
, bool UseVPlanNativePath
) {
1066 // Store the result and return it at the end instead of exiting early, in case
1067 // allowExtraAnalysis is used to report multiple reasons for not vectorizing.
1069 bool DoExtraAnalysis
= ORE
->allowExtraAnalysis(DEBUG_TYPE
);
1070 if (!canVectorizeLoopCFG(Lp
, UseVPlanNativePath
)) {
1071 if (DoExtraAnalysis
)
1077 // Recursively check whether the loop control flow of nested loops is
1079 for (Loop
*SubLp
: *Lp
)
1080 if (!canVectorizeLoopNestCFG(SubLp
, UseVPlanNativePath
)) {
1081 if (DoExtraAnalysis
)
1090 bool LoopVectorizationLegality::canVectorize(bool UseVPlanNativePath
) {
1091 // Store the result and return it at the end instead of exiting early, in case
1092 // allowExtraAnalysis is used to report multiple reasons for not vectorizing.
1095 bool DoExtraAnalysis
= ORE
->allowExtraAnalysis(DEBUG_TYPE
);
1096 // Check whether the loop-related control flow in the loop nest is expected by
1098 if (!canVectorizeLoopNestCFG(TheLoop
, UseVPlanNativePath
)) {
1099 if (DoExtraAnalysis
)
1105 // We need to have a loop header.
1106 LLVM_DEBUG(dbgs() << "LV: Found a loop: " << TheLoop
->getHeader()->getName()
1109 // Specific checks for outer loops. We skip the remaining legal checks at this
1110 // point because they don't support outer loops.
1111 if (!TheLoop
->empty()) {
1112 assert(UseVPlanNativePath
&& "VPlan-native path is not enabled.");
1114 if (!canVectorizeOuterLoop()) {
1115 reportVectorizationFailure("Unsupported outer loop",
1116 "unsupported outer loop",
1117 "UnsupportedOuterLoop",
1119 // TODO: Implement DoExtraAnalysis when subsequent legal checks support
1124 LLVM_DEBUG(dbgs() << "LV: We can vectorize this outer loop!\n");
1128 assert(TheLoop
->empty() && "Inner loop expected.");
1129 // Check if we can if-convert non-single-bb loops.
1130 unsigned NumBlocks
= TheLoop
->getNumBlocks();
1131 if (NumBlocks
!= 1 && !canVectorizeWithIfConvert()) {
1132 LLVM_DEBUG(dbgs() << "LV: Can't if-convert the loop.\n");
1133 if (DoExtraAnalysis
)
1139 // Check if we can vectorize the instructions and CFG in this loop.
1140 if (!canVectorizeInstrs()) {
1141 LLVM_DEBUG(dbgs() << "LV: Can't vectorize the instructions or CFG\n");
1142 if (DoExtraAnalysis
)
1148 // Go over each instruction and look at memory deps.
1149 if (!canVectorizeMemory()) {
1150 LLVM_DEBUG(dbgs() << "LV: Can't vectorize due to memory conflicts\n");
1151 if (DoExtraAnalysis
)
1157 LLVM_DEBUG(dbgs() << "LV: We can vectorize this loop"
1158 << (LAI
->getRuntimePointerChecking()->Need
1159 ? " (with a runtime bound check)"
1163 unsigned SCEVThreshold
= VectorizeSCEVCheckThreshold
;
1164 if (Hints
->getForce() == LoopVectorizeHints::FK_Enabled
)
1165 SCEVThreshold
= PragmaVectorizeSCEVCheckThreshold
;
1167 if (PSE
.getUnionPredicate().getComplexity() > SCEVThreshold
) {
1168 reportVectorizationFailure("Too many SCEV checks needed",
1169 "Too many SCEV assumptions need to be made and checked at runtime",
1170 "TooManySCEVRunTimeChecks", ORE
, TheLoop
);
1171 if (DoExtraAnalysis
)
1177 // Okay! We've done all the tests. If any have failed, return false. Otherwise
1178 // we can vectorize, and at this point we don't have any other mem analysis
1179 // which may limit our maximum vectorization factor, so just return true with
1184 bool LoopVectorizationLegality::prepareToFoldTailByMasking() {
1186 LLVM_DEBUG(dbgs() << "LV: checking if tail can be folded by masking.\n");
1188 if (!PrimaryInduction
) {
1189 reportVectorizationFailure(
1190 "No primary induction, cannot fold tail by masking",
1191 "Missing a primary induction variable in the loop, which is "
1192 "needed in order to fold tail by masking as required.",
1193 "NoPrimaryInduction", ORE
, TheLoop
);
1197 SmallPtrSet
<const Value
*, 8> ReductionLiveOuts
;
1199 for (auto &Reduction
: *getReductionVars())
1200 ReductionLiveOuts
.insert(Reduction
.second
.getLoopExitInstr());
1202 // TODO: handle non-reduction outside users when tail is folded by masking.
1203 for (auto *AE
: AllowedExit
) {
1204 // Check that all users of allowed exit values are inside the loop or
1205 // are the live-out of a reduction.
1206 if (ReductionLiveOuts
.count(AE
))
1208 for (User
*U
: AE
->users()) {
1209 Instruction
*UI
= cast
<Instruction
>(U
);
1210 if (TheLoop
->contains(UI
))
1212 reportVectorizationFailure(
1213 "Cannot fold tail by masking, loop has an outside user for",
1214 "Cannot fold tail by masking in the presence of live outs.",
1215 "LiveOutFoldingTailByMasking", ORE
, TheLoop
, UI
);
1220 // The list of pointers that we can safely read and write to remains empty.
1221 SmallPtrSet
<Value
*, 8> SafePointers
;
1223 // Check and mark all blocks for predication, including those that ordinarily
1224 // do not need predication such as the header block.
1225 for (BasicBlock
*BB
: TheLoop
->blocks()) {
1226 if (!blockCanBePredicated(BB
, SafePointers
, /* MaskAllLoads= */ true)) {
1227 reportVectorizationFailure(
1228 "Cannot fold tail by masking as required",
1229 "control flow cannot be substituted for a select",
1230 "NoCFGForSelect", ORE
, TheLoop
,
1231 BB
->getTerminator());
1236 LLVM_DEBUG(dbgs() << "LV: can fold tail by masking.\n");