LLVM 22.0.0git
LoopVectorize.cpp
Go to the documentation of this file.
1//===- LoopVectorize.cpp - A Loop Vectorizer ------------------------------===//
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 is the LLVM loop vectorizer. This pass modifies 'vectorizable' loops
10// and generates target-independent LLVM-IR.
11// The vectorizer uses the TargetTransformInfo analysis to estimate the costs
12// of instructions in order to estimate the profitability of vectorization.
13//
14// The loop vectorizer combines consecutive loop iterations into a single
15// 'wide' iteration. After this transformation the index is incremented
16// by the SIMD vector width, and not by one.
17//
18// This pass has three parts:
19// 1. The main loop pass that drives the different parts.
20// 2. LoopVectorizationLegality - A unit that checks for the legality
21// of the vectorization.
22// 3. InnerLoopVectorizer - A unit that performs the actual
23// widening of instructions.
24// 4. LoopVectorizationCostModel - A unit that checks for the profitability
25// of vectorization. It decides on the optimal vector width, which
26// can be one, if vectorization is not profitable.
27//
28// There is a development effort going on to migrate loop vectorizer to the
29// VPlan infrastructure and to introduce outer loop vectorization support (see
30// docs/VectorizationPlan.rst and
31// http://lists.llvm.org/pipermail/llvm-dev/2017-December/119523.html). For this
32// purpose, we temporarily introduced the VPlan-native vectorization path: an
33// alternative vectorization path that is natively implemented on top of the
34// VPlan infrastructure. See EnableVPlanNativePath for enabling.
35//
36//===----------------------------------------------------------------------===//
37//
38// The reduction-variable vectorization is based on the paper:
39// D. Nuzman and R. Henderson. Multi-platform Auto-vectorization.
40//
41// Variable uniformity checks are inspired by:
42// Karrenberg, R. and Hack, S. Whole Function Vectorization.
43//
44// The interleaved access vectorization is based on the paper:
45// Dorit Nuzman, Ira Rosen and Ayal Zaks. Auto-Vectorization of Interleaved
46// Data for SIMD
47//
48// Other ideas/concepts are from:
49// A. Zaks and D. Nuzman. Autovectorization in GCC-two years later.
50//
51// S. Maleki, Y. Gao, M. Garzaran, T. Wong and D. Padua. An Evaluation of
52// Vectorizing Compilers.
53//
54//===----------------------------------------------------------------------===//
55
58#include "VPRecipeBuilder.h"
59#include "VPlan.h"
60#include "VPlanAnalysis.h"
61#include "VPlanCFG.h"
62#include "VPlanHelpers.h"
63#include "VPlanPatternMatch.h"
64#include "VPlanTransforms.h"
65#include "VPlanUtils.h"
66#include "VPlanVerifier.h"
67#include "llvm/ADT/APInt.h"
68#include "llvm/ADT/ArrayRef.h"
69#include "llvm/ADT/DenseMap.h"
71#include "llvm/ADT/Hashing.h"
72#include "llvm/ADT/MapVector.h"
73#include "llvm/ADT/STLExtras.h"
76#include "llvm/ADT/Statistic.h"
77#include "llvm/ADT/StringRef.h"
78#include "llvm/ADT/Twine.h"
79#include "llvm/ADT/TypeSwitch.h"
84#include "llvm/Analysis/CFG.h"
101#include "llvm/IR/Attributes.h"
102#include "llvm/IR/BasicBlock.h"
103#include "llvm/IR/CFG.h"
104#include "llvm/IR/Constant.h"
105#include "llvm/IR/Constants.h"
106#include "llvm/IR/DataLayout.h"
107#include "llvm/IR/DebugInfo.h"
108#include "llvm/IR/DebugLoc.h"
109#include "llvm/IR/DerivedTypes.h"
111#include "llvm/IR/Dominators.h"
112#include "llvm/IR/Function.h"
113#include "llvm/IR/IRBuilder.h"
114#include "llvm/IR/InstrTypes.h"
115#include "llvm/IR/Instruction.h"
116#include "llvm/IR/Instructions.h"
118#include "llvm/IR/Intrinsics.h"
119#include "llvm/IR/MDBuilder.h"
120#include "llvm/IR/Metadata.h"
121#include "llvm/IR/Module.h"
122#include "llvm/IR/Operator.h"
123#include "llvm/IR/PatternMatch.h"
125#include "llvm/IR/Type.h"
126#include "llvm/IR/Use.h"
127#include "llvm/IR/User.h"
128#include "llvm/IR/Value.h"
129#include "llvm/IR/Verifier.h"
130#include "llvm/Support/Casting.h"
132#include "llvm/Support/Debug.h"
147#include <algorithm>
148#include <cassert>
149#include <cstdint>
150#include <functional>
151#include <iterator>
152#include <limits>
153#include <memory>
154#include <string>
155#include <tuple>
156#include <utility>
157
158using namespace llvm;
159using namespace SCEVPatternMatch;
160
161#define LV_NAME "loop-vectorize"
162#define DEBUG_TYPE LV_NAME
163
164#ifndef NDEBUG
165const char VerboseDebug[] = DEBUG_TYPE "-verbose";
166#endif
167
168STATISTIC(LoopsVectorized, "Number of loops vectorized");
169STATISTIC(LoopsAnalyzed, "Number of loops analyzed for vectorization");
170STATISTIC(LoopsEpilogueVectorized, "Number of epilogues vectorized");
171STATISTIC(LoopsEarlyExitVectorized, "Number of early exit loops vectorized");
172
174 "enable-epilogue-vectorization", cl::init(true), cl::Hidden,
175 cl::desc("Enable vectorization of epilogue loops."));
176
178 "epilogue-vectorization-force-VF", cl::init(1), cl::Hidden,
179 cl::desc("When epilogue vectorization is enabled, and a value greater than "
180 "1 is specified, forces the given VF for all applicable epilogue "
181 "loops."));
182
184 "epilogue-vectorization-minimum-VF", cl::Hidden,
185 cl::desc("Only loops with vectorization factor equal to or larger than "
186 "the specified value are considered for epilogue vectorization."));
187
188/// Loops with a known constant trip count below this number are vectorized only
189/// if no scalar iteration overheads are incurred.
191 "vectorizer-min-trip-count", cl::init(16), cl::Hidden,
192 cl::desc("Loops with a constant trip count that is smaller than this "
193 "value are vectorized only if no scalar iteration overheads "
194 "are incurred."));
195
197 "vectorize-memory-check-threshold", cl::init(128), cl::Hidden,
198 cl::desc("The maximum allowed number of runtime memory checks"));
199
200// Option prefer-predicate-over-epilogue indicates that an epilogue is undesired,
201// that predication is preferred, and this lists all options. I.e., the
202// vectorizer will try to fold the tail-loop (epilogue) into the vector body
203// and predicate the instructions accordingly. If tail-folding fails, there are
204// different fallback strategies depending on these values:
211} // namespace PreferPredicateTy
212
214 "prefer-predicate-over-epilogue",
217 cl::desc("Tail-folding and predication preferences over creating a scalar "
218 "epilogue loop."),
220 "scalar-epilogue",
221 "Don't tail-predicate loops, create scalar epilogue"),
223 "predicate-else-scalar-epilogue",
224 "prefer tail-folding, create scalar epilogue if tail "
225 "folding fails."),
227 "predicate-dont-vectorize",
228 "prefers tail-folding, don't attempt vectorization if "
229 "tail-folding fails.")));
230
232 "force-tail-folding-style", cl::desc("Force the tail folding style"),
235 clEnumValN(TailFoldingStyle::None, "none", "Disable tail folding"),
238 "Create lane mask for data only, using active.lane.mask intrinsic"),
240 "data-without-lane-mask",
241 "Create lane mask with compare/stepvector"),
243 "Create lane mask using active.lane.mask intrinsic, and use "
244 "it for both data and control flow"),
246 "data-and-control-without-rt-check",
247 "Similar to data-and-control, but remove the runtime check"),
249 "Use predicated EVL instructions for tail folding. If EVL "
250 "is unsupported, fallback to data-without-lane-mask.")));
251
253 "vectorizer-maximize-bandwidth", cl::init(false), cl::Hidden,
254 cl::desc("Maximize bandwidth when selecting vectorization factor which "
255 "will be determined by the smallest type in loop."));
256
258 "enable-interleaved-mem-accesses", cl::init(false), cl::Hidden,
259 cl::desc("Enable vectorization on interleaved memory accesses in a loop"));
260
261/// An interleave-group may need masking if it resides in a block that needs
262/// predication, or in order to mask away gaps.
264 "enable-masked-interleaved-mem-accesses", cl::init(false), cl::Hidden,
265 cl::desc("Enable vectorization on masked interleaved memory accesses in a loop"));
266
268 "force-target-num-scalar-regs", cl::init(0), cl::Hidden,
269 cl::desc("A flag that overrides the target's number of scalar registers."));
270
272 "force-target-num-vector-regs", cl::init(0), cl::Hidden,
273 cl::desc("A flag that overrides the target's number of vector registers."));
274
276 "force-target-max-scalar-interleave", cl::init(0), cl::Hidden,
277 cl::desc("A flag that overrides the target's max interleave factor for "
278 "scalar loops."));
279
281 "force-target-max-vector-interleave", cl::init(0), cl::Hidden,
282 cl::desc("A flag that overrides the target's max interleave factor for "
283 "vectorized loops."));
284
286 "force-target-instruction-cost", cl::init(0), cl::Hidden,
287 cl::desc("A flag that overrides the target's expected cost for "
288 "an instruction to a single constant value. Mostly "
289 "useful for getting consistent testing."));
290
292 "force-target-supports-scalable-vectors", cl::init(false), cl::Hidden,
293 cl::desc(
294 "Pretend that scalable vectors are supported, even if the target does "
295 "not support them. This flag should only be used for testing."));
296
298 "small-loop-cost", cl::init(20), cl::Hidden,
299 cl::desc(
300 "The cost of a loop that is considered 'small' by the interleaver."));
301
303 "loop-vectorize-with-block-frequency", cl::init(true), cl::Hidden,
304 cl::desc("Enable the use of the block frequency analysis to access PGO "
305 "heuristics minimizing code growth in cold regions and being more "
306 "aggressive in hot regions."));
307
308// Runtime interleave loops for load/store throughput.
310 "enable-loadstore-runtime-interleave", cl::init(true), cl::Hidden,
311 cl::desc(
312 "Enable runtime interleaving until load/store ports are saturated"));
313
314/// The number of stores in a loop that are allowed to need predication.
316 "vectorize-num-stores-pred", cl::init(1), cl::Hidden,
317 cl::desc("Max number of stores to be predicated behind an if."));
318
320 "enable-ind-var-reg-heur", cl::init(true), cl::Hidden,
321 cl::desc("Count the induction variable only once when interleaving"));
322
324 "enable-cond-stores-vec", cl::init(true), cl::Hidden,
325 cl::desc("Enable if predication of stores during vectorization."));
326
328 "max-nested-scalar-reduction-interleave", cl::init(2), cl::Hidden,
329 cl::desc("The maximum interleave count to use when interleaving a scalar "
330 "reduction in a nested loop."));
331
332static cl::opt<bool>
333 PreferInLoopReductions("prefer-inloop-reductions", cl::init(false),
335 cl::desc("Prefer in-loop vector reductions, "
336 "overriding the targets preference."));
337
339 "force-ordered-reductions", cl::init(false), cl::Hidden,
340 cl::desc("Enable the vectorisation of loops with in-order (strict) "
341 "FP reductions"));
342
344 "prefer-predicated-reduction-select", cl::init(false), cl::Hidden,
345 cl::desc(
346 "Prefer predicating a reduction operation over an after loop select."));
347
349 "enable-vplan-native-path", cl::Hidden,
350 cl::desc("Enable VPlan-native vectorization path with "
351 "support for outer loop vectorization."));
352
354 llvm::VerifyEachVPlan("vplan-verify-each",
355#ifdef EXPENSIVE_CHECKS
356 cl::init(true),
357#else
358 cl::init(false),
359#endif
361 cl::desc("Verfiy VPlans after VPlan transforms."));
362
363// This flag enables the stress testing of the VPlan H-CFG construction in the
364// VPlan-native vectorization path. It must be used in conjuction with
365// -enable-vplan-native-path. -vplan-verify-hcfg can also be used to enable the
366// verification of the H-CFGs built.
368 "vplan-build-stress-test", cl::init(false), cl::Hidden,
369 cl::desc(
370 "Build VPlan for every supported loop nest in the function and bail "
371 "out right after the build (stress test the VPlan H-CFG construction "
372 "in the VPlan-native vectorization path)."));
373
375 "interleave-loops", cl::init(true), cl::Hidden,
376 cl::desc("Enable loop interleaving in Loop vectorization passes"));
378 "vectorize-loops", cl::init(true), cl::Hidden,
379 cl::desc("Run the Loop vectorization passes"));
380
382 "force-widen-divrem-via-safe-divisor", cl::Hidden,
383 cl::desc(
384 "Override cost based safe divisor widening for div/rem instructions"));
385
387 "vectorizer-maximize-bandwidth-for-vector-calls", cl::init(true),
389 cl::desc("Try wider VFs if they enable the use of vector variants"));
390
392 "enable-early-exit-vectorization", cl::init(true), cl::Hidden,
393 cl::desc(
394 "Enable vectorization of early exit loops with uncountable exits."));
395
397 "vectorizer-consider-reg-pressure", cl::init(false), cl::Hidden,
398 cl::desc("Discard VFs if their register pressure is too high."));
399
400// Likelyhood of bypassing the vectorized loop because there are zero trips left
401// after prolog. See `emitIterationCountCheck`.
402static constexpr uint32_t MinItersBypassWeights[] = {1, 127};
403
404/// A helper function that returns true if the given type is irregular. The
405/// type is irregular if its allocated size doesn't equal the store size of an
406/// element of the corresponding vector type.
407static bool hasIrregularType(Type *Ty, const DataLayout &DL) {
408 // Determine if an array of N elements of type Ty is "bitcast compatible"
409 // with a <N x Ty> vector.
410 // This is only true if there is no padding between the array elements.
411 return DL.getTypeAllocSizeInBits(Ty) != DL.getTypeSizeInBits(Ty);
412}
413
414/// A version of ScalarEvolution::getSmallConstantTripCount that returns an
415/// ElementCount to include loops whose trip count is a function of vscale.
417 const Loop *L) {
418 if (unsigned ExpectedTC = SE->getSmallConstantTripCount(L))
419 return ElementCount::getFixed(ExpectedTC);
420
421 const SCEV *BTC = SE->getBackedgeTakenCount(L);
423 return ElementCount::getFixed(0);
424
425 const SCEV *ExitCount = SE->getTripCountFromExitCount(BTC, BTC->getType(), L);
426 if (isa<SCEVVScale>(ExitCount))
428
429 const APInt *Scale;
430 if (match(ExitCount, m_scev_Mul(m_scev_APInt(Scale), m_SCEVVScale())))
431 if (cast<SCEVMulExpr>(ExitCount)->hasNoUnsignedWrap())
432 if (Scale->getActiveBits() <= 32)
434
435 return ElementCount::getFixed(0);
436}
437
438/// Returns "best known" trip count, which is either a valid positive trip count
439/// or std::nullopt when an estimate cannot be made (including when the trip
440/// count would overflow), for the specified loop \p L as defined by the
441/// following procedure:
442/// 1) Returns exact trip count if it is known.
443/// 2) Returns expected trip count according to profile data if any.
444/// 3) Returns upper bound estimate if known, and if \p CanUseConstantMax.
445/// 4) Returns std::nullopt if all of the above failed.
446static std::optional<ElementCount>
448 bool CanUseConstantMax = true) {
449 // Check if exact trip count is known.
450 if (auto ExpectedTC = getSmallConstantTripCount(PSE.getSE(), L))
451 return ExpectedTC;
452
453 // Check if there is an expected trip count available from profile data.
455 if (auto EstimatedTC = getLoopEstimatedTripCount(L))
456 return ElementCount::getFixed(*EstimatedTC);
457
458 if (!CanUseConstantMax)
459 return std::nullopt;
460
461 // Check if upper bound estimate is known.
462 if (unsigned ExpectedTC = PSE.getSmallConstantMaxTripCount())
463 return ElementCount::getFixed(ExpectedTC);
464
465 return std::nullopt;
466}
467
468namespace {
469// Forward declare GeneratedRTChecks.
470class GeneratedRTChecks;
471
472using SCEV2ValueTy = DenseMap<const SCEV *, Value *>;
473} // namespace
474
475namespace llvm {
476
478
479/// InnerLoopVectorizer vectorizes loops which contain only one basic
480/// block to a specified vectorization factor (VF).
481/// This class performs the widening of scalars into vectors, or multiple
482/// scalars. This class also implements the following features:
483/// * It inserts an epilogue loop for handling loops that don't have iteration
484/// counts that are known to be a multiple of the vectorization factor.
485/// * It handles the code generation for reduction variables.
486/// * Scalarization (implementation using scalars) of un-vectorizable
487/// instructions.
488/// InnerLoopVectorizer does not perform any vectorization-legality
489/// checks, and relies on the caller to check for the different legality
490/// aspects. The InnerLoopVectorizer relies on the
491/// LoopVectorizationLegality class to provide information about the induction
492/// and reduction variables that were found to a given vectorization factor.
494public:
498 ElementCount VecWidth, unsigned UnrollFactor,
500 ProfileSummaryInfo *PSI, GeneratedRTChecks &RTChecks,
501 VPlan &Plan)
502 : OrigLoop(OrigLoop), PSE(PSE), LI(LI), DT(DT), TTI(TTI), AC(AC),
503 VF(VecWidth), UF(UnrollFactor), Builder(PSE.getSE()->getContext()),
506 Plan.getVectorLoopRegion()->getSinglePredecessor())) {}
507
508 virtual ~InnerLoopVectorizer() = default;
509
510 /// Creates a basic block for the scalar preheader. Both
511 /// EpilogueVectorizerMainLoop and EpilogueVectorizerEpilogueLoop overwrite
512 /// the method to create additional blocks and checks needed for epilogue
513 /// vectorization.
515
516 /// Fix the vectorized code, taking care of header phi's, and more.
518
519 /// Fix the non-induction PHIs in \p Plan.
521
522 /// Returns the original loop trip count.
523 Value *getTripCount() const { return TripCount; }
524
525 /// Used to set the trip count after ILV's construction and after the
526 /// preheader block has been executed. Note that this always holds the trip
527 /// count of the original loop for both main loop and epilogue vectorization.
528 void setTripCount(Value *TC) { TripCount = TC; }
529
530protected:
532
533 /// Create and return a new IR basic block for the scalar preheader whose name
534 /// is prefixed with \p Prefix.
536
537 /// Allow subclasses to override and print debug traces before/after vplan
538 /// execution, when trace information is requested.
539 virtual void printDebugTracesAtStart() {}
540 virtual void printDebugTracesAtEnd() {}
541
542 /// The original loop.
544
545 /// A wrapper around ScalarEvolution used to add runtime SCEV checks. Applies
546 /// dynamic knowledge to simplify SCEV expressions and converts them to a
547 /// more usable form.
549
550 /// Loop Info.
552
553 /// Dominator Tree.
555
556 /// Target Transform Info.
558
559 /// Assumption Cache.
561
562 /// The vectorization SIMD factor to use. Each vector will have this many
563 /// vector elements.
565
566 /// The vectorization unroll factor to use. Each scalar is vectorized to this
567 /// many different vector instructions.
568 unsigned UF;
569
570 /// The builder that we use
572
573 // --- Vectorization state ---
574
575 /// Trip count of the original loop.
576 Value *TripCount = nullptr;
577
578 /// The profitablity analysis.
580
581 /// BFI and PSI are used to check for profile guided size optimizations.
584
585 /// Structure to hold information about generated runtime checks, responsible
586 /// for cleaning the checks, if vectorization turns out unprofitable.
587 GeneratedRTChecks &RTChecks;
588
590
591 /// The vector preheader block of \p Plan, used as target for check blocks
592 /// introduced during skeleton creation.
594};
595
596/// Encapsulate information regarding vectorization of a loop and its epilogue.
597/// This information is meant to be updated and used across two stages of
598/// epilogue vectorization.
601 unsigned MainLoopUF = 0;
603 unsigned EpilogueUF = 0;
606 Value *TripCount = nullptr;
609
611 ElementCount EVF, unsigned EUF,
613 : MainLoopVF(MVF), MainLoopUF(MUF), EpilogueVF(EVF), EpilogueUF(EUF),
615 assert(EUF == 1 &&
616 "A high UF for the epilogue loop is likely not beneficial.");
617 }
618};
619
620/// An extension of the inner loop vectorizer that creates a skeleton for a
621/// vectorized loop that has its epilogue (residual) also vectorized.
622/// The idea is to run the vplan on a given loop twice, firstly to setup the
623/// skeleton and vectorize the main loop, and secondly to complete the skeleton
624/// from the first step and vectorize the epilogue. This is achieved by
625/// deriving two concrete strategy classes from this base class and invoking
626/// them in succession from the loop vectorizer planner.
628public:
639
640 /// Holds and updates state information required to vectorize the main loop
641 /// and its epilogue in two separate passes. This setup helps us avoid
642 /// regenerating and recomputing runtime safety checks. It also helps us to
643 /// shorten the iteration-count-check path length for the cases where the
644 /// iteration count of the loop is so small that the main vector loop is
645 /// completely skipped.
647
648protected:
650};
651
652/// A specialized derived class of inner loop vectorizer that performs
653/// vectorization of *main* loops in the process of vectorizing loops and their
654/// epilogues.
656public:
668 /// Implements the interface for creating a vectorized skeleton using the
669 /// *main loop* strategy (i.e., the first pass of VPlan execution).
671
672protected:
673 /// Introduces a new VPIRBasicBlock for \p CheckIRBB to Plan between the
674 /// vector preheader and its predecessor, also connecting the new block to the
675 /// scalar preheader.
676 void introduceCheckBlockInVPlan(BasicBlock *CheckIRBB);
677
678 // Create a check to see if the main vector loop should be executed
680 unsigned UF) const;
681
682 /// Emits an iteration count bypass check once for the main loop (when \p
683 /// ForEpilogue is false) and once for the epilogue loop (when \p
684 /// ForEpilogue is true).
686 bool ForEpilogue);
687 void printDebugTracesAtStart() override;
688 void printDebugTracesAtEnd() override;
689};
690
691// A specialized derived class of inner loop vectorizer that performs
692// vectorization of *epilogue* loops in the process of vectorizing loops and
693// their epilogues.
695public:
705 /// Implements the interface for creating a vectorized skeleton using the
706 /// *epilogue loop* strategy (i.e., the second pass of VPlan execution).
708
709protected:
710 void printDebugTracesAtStart() override;
711 void printDebugTracesAtEnd() override;
712};
713} // end namespace llvm
714
715/// Look for a meaningful debug location on the instruction or its operands.
717 if (!I)
718 return DebugLoc::getUnknown();
719
721 if (I->getDebugLoc() != Empty)
722 return I->getDebugLoc();
723
724 for (Use &Op : I->operands()) {
725 if (Instruction *OpInst = dyn_cast<Instruction>(Op))
726 if (OpInst->getDebugLoc() != Empty)
727 return OpInst->getDebugLoc();
728 }
729
730 return I->getDebugLoc();
731}
732
733/// Write a \p DebugMsg about vectorization to the debug output stream. If \p I
734/// is passed, the message relates to that particular instruction.
735#ifndef NDEBUG
736static void debugVectorizationMessage(const StringRef Prefix,
737 const StringRef DebugMsg,
738 Instruction *I) {
739 dbgs() << "LV: " << Prefix << DebugMsg;
740 if (I != nullptr)
741 dbgs() << " " << *I;
742 else
743 dbgs() << '.';
744 dbgs() << '\n';
745}
746#endif
747
748/// Create an analysis remark that explains why vectorization failed
749///
750/// \p PassName is the name of the pass (e.g. can be AlwaysPrint). \p
751/// RemarkName is the identifier for the remark. If \p I is passed it is an
752/// instruction that prevents vectorization. Otherwise \p TheLoop is used for
753/// the location of the remark. If \p DL is passed, use it as debug location for
754/// the remark. \return the remark object that can be streamed to.
755static OptimizationRemarkAnalysis
756createLVAnalysis(const char *PassName, StringRef RemarkName, Loop *TheLoop,
757 Instruction *I, DebugLoc DL = {}) {
758 BasicBlock *CodeRegion = I ? I->getParent() : TheLoop->getHeader();
759 // If debug location is attached to the instruction, use it. Otherwise if DL
760 // was not provided, use the loop's.
761 if (I && I->getDebugLoc())
762 DL = I->getDebugLoc();
763 else if (!DL)
764 DL = TheLoop->getStartLoc();
765
766 return OptimizationRemarkAnalysis(PassName, RemarkName, DL, CodeRegion);
767}
768
769namespace llvm {
770
771/// Return a value for Step multiplied by VF.
773 int64_t Step) {
774 assert(Ty->isIntegerTy() && "Expected an integer step");
775 ElementCount VFxStep = VF.multiplyCoefficientBy(Step);
776 assert(isPowerOf2_64(VF.getKnownMinValue()) && "must pass power-of-2 VF");
777 if (VF.isScalable() && isPowerOf2_64(Step)) {
778 return B.CreateShl(
779 B.CreateVScale(Ty),
780 ConstantInt::get(Ty, Log2_64(VFxStep.getKnownMinValue())), "", true);
781 }
782 return B.CreateElementCount(Ty, VFxStep);
783}
784
785/// Return the runtime value for VF.
787 return B.CreateElementCount(Ty, VF);
788}
789
791 const StringRef OREMsg, const StringRef ORETag,
792 OptimizationRemarkEmitter *ORE, Loop *TheLoop,
793 Instruction *I) {
794 LLVM_DEBUG(debugVectorizationMessage("Not vectorizing: ", DebugMsg, I));
795 LoopVectorizeHints Hints(TheLoop, true /* doesn't matter */, *ORE);
796 ORE->emit(
797 createLVAnalysis(Hints.vectorizeAnalysisPassName(), ORETag, TheLoop, I)
798 << "loop not vectorized: " << OREMsg);
799}
800
801/// Reports an informative message: print \p Msg for debugging purposes as well
802/// as an optimization remark. Uses either \p I as location of the remark, or
803/// otherwise \p TheLoop. If \p DL is passed, use it as debug location for the
804/// remark. If \p DL is passed, use it as debug location for the remark.
805static void reportVectorizationInfo(const StringRef Msg, const StringRef ORETag,
807 Loop *TheLoop, Instruction *I = nullptr,
808 DebugLoc DL = {}) {
810 LoopVectorizeHints Hints(TheLoop, true /* doesn't matter */, *ORE);
811 ORE->emit(createLVAnalysis(Hints.vectorizeAnalysisPassName(), ORETag, TheLoop,
812 I, DL)
813 << Msg);
814}
815
816/// Report successful vectorization of the loop. In case an outer loop is
817/// vectorized, prepend "outer" to the vectorization remark.
819 VectorizationFactor VF, unsigned IC) {
821 "Vectorizing: ", TheLoop->isInnermost() ? "innermost loop" : "outer loop",
822 nullptr));
823 StringRef LoopType = TheLoop->isInnermost() ? "" : "outer ";
824 ORE->emit([&]() {
825 return OptimizationRemark(LV_NAME, "Vectorized", TheLoop->getStartLoc(),
826 TheLoop->getHeader())
827 << "vectorized " << LoopType << "loop (vectorization width: "
828 << ore::NV("VectorizationFactor", VF.Width)
829 << ", interleaved count: " << ore::NV("InterleaveCount", IC) << ")";
830 });
831}
832
833} // end namespace llvm
834
835namespace llvm {
836
837// Loop vectorization cost-model hints how the scalar epilogue loop should be
838// lowered.
840
841 // The default: allowing scalar epilogues.
843
844 // Vectorization with OptForSize: don't allow epilogues.
846
847 // A special case of vectorisation with OptForSize: loops with a very small
848 // trip count are considered for vectorization under OptForSize, thereby
849 // making sure the cost of their loop body is dominant, free of runtime
850 // guards and scalar iteration overheads.
852
853 // Loop hint predicate indicating an epilogue is undesired.
855
856 // Directive indicating we must either tail fold or not vectorize
858};
859
860/// LoopVectorizationCostModel - estimates the expected speedups due to
861/// vectorization.
862/// In many cases vectorization is not profitable. This can happen because of
863/// a number of reasons. In this class we mainly attempt to predict the
864/// expected speedup/slowdowns due to the supported instruction set. We use the
865/// TargetTransformInfo to query the different backends for the cost of
866/// different operations.
869
870public:
881 : ScalarEpilogueStatus(SEL), TheLoop(L), PSE(PSE), LI(LI), Legal(Legal),
882 TTI(TTI), TLI(TLI), DB(DB), AC(AC), ORE(ORE), TheFunction(F),
883 Hints(Hints), InterleaveInfo(IAI) {
884 if (TTI.supportsScalableVectors() || ForceTargetSupportsScalableVectors)
885 initializeVScaleForTuning();
887 // Query this against the original loop and save it here because the profile
888 // of the original loop header may change as the transformation happens.
889 OptForSize = llvm::shouldOptimizeForSize(L->getHeader(), PSI, BFI,
891 }
892
893 /// \return An upper bound for the vectorization factors (both fixed and
894 /// scalable). If the factors are 0, vectorization and interleaving should be
895 /// avoided up front.
896 FixedScalableVFPair computeMaxVF(ElementCount UserVF, unsigned UserIC);
897
898 /// \return True if runtime checks are required for vectorization, and false
899 /// otherwise.
900 bool runtimeChecksRequired();
901
902 /// Setup cost-based decisions for user vectorization factor.
903 /// \return true if the UserVF is a feasible VF to be chosen.
906 return expectedCost(UserVF).isValid();
907 }
908
909 /// \return True if maximizing vector bandwidth is enabled by the target or
910 /// user options, for the given register kind.
911 bool useMaxBandwidth(TargetTransformInfo::RegisterKind RegKind);
912
913 /// \return True if register pressure should be considered for the given VF.
914 bool shouldConsiderRegPressureForVF(ElementCount VF);
915
916 /// \return The size (in bits) of the smallest and widest types in the code
917 /// that needs to be vectorized. We ignore values that remain scalar such as
918 /// 64 bit loop indices.
919 std::pair<unsigned, unsigned> getSmallestAndWidestTypes();
920
921 /// Memory access instruction may be vectorized in more than one way.
922 /// Form of instruction after vectorization depends on cost.
923 /// This function takes cost-based decisions for Load/Store instructions
924 /// and collects them in a map. This decisions map is used for building
925 /// the lists of loop-uniform and loop-scalar instructions.
926 /// The calculated cost is saved with widening decision in order to
927 /// avoid redundant calculations.
928 void setCostBasedWideningDecision(ElementCount VF);
929
930 /// A call may be vectorized in different ways depending on whether we have
931 /// vectorized variants available and whether the target supports masking.
932 /// This function analyzes all calls in the function at the supplied VF,
933 /// makes a decision based on the costs of available options, and stores that
934 /// decision in a map for use in planning and plan execution.
935 void setVectorizedCallDecision(ElementCount VF);
936
937 /// Collect values we want to ignore in the cost model.
938 void collectValuesToIgnore();
939
940 /// Collect all element types in the loop for which widening is needed.
941 void collectElementTypesForWidening();
942
943 /// Split reductions into those that happen in the loop, and those that happen
944 /// outside. In loop reductions are collected into InLoopReductions.
945 void collectInLoopReductions();
946
947 /// Returns true if we should use strict in-order reductions for the given
948 /// RdxDesc. This is true if the -enable-strict-reductions flag is passed,
949 /// the IsOrdered flag of RdxDesc is set and we do not allow reordering
950 /// of FP operations.
951 bool useOrderedReductions(const RecurrenceDescriptor &RdxDesc) const {
952 return !Hints->allowReordering() && RdxDesc.isOrdered();
953 }
954
955 /// \returns The smallest bitwidth each instruction can be represented with.
956 /// The vector equivalents of these instructions should be truncated to this
957 /// type.
959 return MinBWs;
960 }
961
962 /// \returns True if it is more profitable to scalarize instruction \p I for
963 /// vectorization factor \p VF.
965 assert(VF.isVector() &&
966 "Profitable to scalarize relevant only for VF > 1.");
967 assert(
968 TheLoop->isInnermost() &&
969 "cost-model should not be used for outer loops (in VPlan-native path)");
970
971 auto Scalars = InstsToScalarize.find(VF);
972 assert(Scalars != InstsToScalarize.end() &&
973 "VF not yet analyzed for scalarization profitability");
974 return Scalars->second.contains(I);
975 }
976
977 /// Returns true if \p I is known to be uniform after vectorization.
979 assert(
980 TheLoop->isInnermost() &&
981 "cost-model should not be used for outer loops (in VPlan-native path)");
982 // Pseudo probe needs to be duplicated for each unrolled iteration and
983 // vector lane so that profiled loop trip count can be accurately
984 // accumulated instead of being under counted.
986 return false;
987
988 if (VF.isScalar())
989 return true;
990
991 auto UniformsPerVF = Uniforms.find(VF);
992 assert(UniformsPerVF != Uniforms.end() &&
993 "VF not yet analyzed for uniformity");
994 return UniformsPerVF->second.count(I);
995 }
996
997 /// Returns true if \p I is known to be scalar after vectorization.
999 assert(
1000 TheLoop->isInnermost() &&
1001 "cost-model should not be used for outer loops (in VPlan-native path)");
1002 if (VF.isScalar())
1003 return true;
1004
1005 auto ScalarsPerVF = Scalars.find(VF);
1006 assert(ScalarsPerVF != Scalars.end() &&
1007 "Scalar values are not calculated for VF");
1008 return ScalarsPerVF->second.count(I);
1009 }
1010
1011 /// \returns True if instruction \p I can be truncated to a smaller bitwidth
1012 /// for vectorization factor \p VF.
1014 // Truncs must truncate at most to their destination type.
1015 if (isa_and_nonnull<TruncInst>(I) && MinBWs.contains(I) &&
1016 I->getType()->getScalarSizeInBits() < MinBWs.lookup(I))
1017 return false;
1018 return VF.isVector() && MinBWs.contains(I) &&
1019 !isProfitableToScalarize(I, VF) &&
1021 }
1022
1023 /// Decision that was taken during cost calculation for memory instruction.
1026 CM_Widen, // For consecutive accesses with stride +1.
1027 CM_Widen_Reverse, // For consecutive accesses with stride -1.
1033 };
1034
1035 /// Save vectorization decision \p W and \p Cost taken by the cost model for
1036 /// instruction \p I and vector width \p VF.
1039 assert(VF.isVector() && "Expected VF >=2");
1040 WideningDecisions[{I, VF}] = {W, Cost};
1041 }
1042
1043 /// Save vectorization decision \p W and \p Cost taken by the cost model for
1044 /// interleaving group \p Grp and vector width \p VF.
1048 assert(VF.isVector() && "Expected VF >=2");
1049 /// Broadcast this decicion to all instructions inside the group.
1050 /// When interleaving, the cost will only be assigned one instruction, the
1051 /// insert position. For other cases, add the appropriate fraction of the
1052 /// total cost to each instruction. This ensures accurate costs are used,
1053 /// even if the insert position instruction is not used.
1054 InstructionCost InsertPosCost = Cost;
1055 InstructionCost OtherMemberCost = 0;
1056 if (W != CM_Interleave)
1057 OtherMemberCost = InsertPosCost = Cost / Grp->getNumMembers();
1058 ;
1059 for (unsigned Idx = 0; Idx < Grp->getFactor(); ++Idx) {
1060 if (auto *I = Grp->getMember(Idx)) {
1061 if (Grp->getInsertPos() == I)
1062 WideningDecisions[{I, VF}] = {W, InsertPosCost};
1063 else
1064 WideningDecisions[{I, VF}] = {W, OtherMemberCost};
1065 }
1066 }
1067 }
1068
1069 /// Return the cost model decision for the given instruction \p I and vector
1070 /// width \p VF. Return CM_Unknown if this instruction did not pass
1071 /// through the cost modeling.
1073 assert(VF.isVector() && "Expected VF to be a vector VF");
1074 assert(
1075 TheLoop->isInnermost() &&
1076 "cost-model should not be used for outer loops (in VPlan-native path)");
1077
1078 std::pair<Instruction *, ElementCount> InstOnVF(I, VF);
1079 auto Itr = WideningDecisions.find(InstOnVF);
1080 if (Itr == WideningDecisions.end())
1081 return CM_Unknown;
1082 return Itr->second.first;
1083 }
1084
1085 /// Return the vectorization cost for the given instruction \p I and vector
1086 /// width \p VF.
1088 assert(VF.isVector() && "Expected VF >=2");
1089 std::pair<Instruction *, ElementCount> InstOnVF(I, VF);
1090 assert(WideningDecisions.contains(InstOnVF) &&
1091 "The cost is not calculated");
1092 return WideningDecisions[InstOnVF].second;
1093 }
1094
1102
1104 Function *Variant, Intrinsic::ID IID,
1105 std::optional<unsigned> MaskPos,
1107 assert(!VF.isScalar() && "Expected vector VF");
1108 CallWideningDecisions[{CI, VF}] = {Kind, Variant, IID, MaskPos, Cost};
1109 }
1110
1112 ElementCount VF) const {
1113 assert(!VF.isScalar() && "Expected vector VF");
1114 auto I = CallWideningDecisions.find({CI, VF});
1115 if (I == CallWideningDecisions.end())
1116 return {CM_Unknown, nullptr, Intrinsic::not_intrinsic, std::nullopt, 0};
1117 return I->second;
1118 }
1119
1120 /// Return True if instruction \p I is an optimizable truncate whose operand
1121 /// is an induction variable. Such a truncate will be removed by adding a new
1122 /// induction variable with the destination type.
1124 // If the instruction is not a truncate, return false.
1125 auto *Trunc = dyn_cast<TruncInst>(I);
1126 if (!Trunc)
1127 return false;
1128
1129 // Get the source and destination types of the truncate.
1130 Type *SrcTy = toVectorTy(Trunc->getSrcTy(), VF);
1131 Type *DestTy = toVectorTy(Trunc->getDestTy(), VF);
1132
1133 // If the truncate is free for the given types, return false. Replacing a
1134 // free truncate with an induction variable would add an induction variable
1135 // update instruction to each iteration of the loop. We exclude from this
1136 // check the primary induction variable since it will need an update
1137 // instruction regardless.
1138 Value *Op = Trunc->getOperand(0);
1139 if (Op != Legal->getPrimaryInduction() && TTI.isTruncateFree(SrcTy, DestTy))
1140 return false;
1141
1142 // If the truncated value is not an induction variable, return false.
1143 return Legal->isInductionPhi(Op);
1144 }
1145
1146 /// Collects the instructions to scalarize for each predicated instruction in
1147 /// the loop.
1148 void collectInstsToScalarize(ElementCount VF);
1149
1150 /// Collect values that will not be widened, including Uniforms, Scalars, and
1151 /// Instructions to Scalarize for the given \p VF.
1152 /// The sets depend on CM decision for Load/Store instructions
1153 /// that may be vectorized as interleave, gather-scatter or scalarized.
1154 /// Also make a decision on what to do about call instructions in the loop
1155 /// at that VF -- scalarize, call a known vector routine, or call a
1156 /// vector intrinsic.
1158 // Do the analysis once.
1159 if (VF.isScalar() || Uniforms.contains(VF))
1160 return;
1162 collectLoopUniforms(VF);
1164 collectLoopScalars(VF);
1166 }
1167
1168 /// Returns true if the target machine supports masked store operation
1169 /// for the given \p DataType and kind of access to \p Ptr.
1170 bool isLegalMaskedStore(Type *DataType, Value *Ptr, Align Alignment,
1171 unsigned AddressSpace) const {
1172 return Legal->isConsecutivePtr(DataType, Ptr) &&
1173 TTI.isLegalMaskedStore(DataType, Alignment, AddressSpace);
1174 }
1175
1176 /// Returns true if the target machine supports masked load operation
1177 /// for the given \p DataType and kind of access to \p Ptr.
1178 bool isLegalMaskedLoad(Type *DataType, Value *Ptr, Align Alignment,
1179 unsigned AddressSpace) const {
1180 return Legal->isConsecutivePtr(DataType, Ptr) &&
1181 TTI.isLegalMaskedLoad(DataType, Alignment, AddressSpace);
1182 }
1183
1184 /// Returns true if the target machine can represent \p V as a masked gather
1185 /// or scatter operation.
1187 bool LI = isa<LoadInst>(V);
1188 bool SI = isa<StoreInst>(V);
1189 if (!LI && !SI)
1190 return false;
1191 auto *Ty = getLoadStoreType(V);
1193 if (VF.isVector())
1194 Ty = VectorType::get(Ty, VF);
1195 return (LI && TTI.isLegalMaskedGather(Ty, Align)) ||
1196 (SI && TTI.isLegalMaskedScatter(Ty, Align));
1197 }
1198
1199 /// Returns true if the target machine supports all of the reduction
1200 /// variables found for the given VF.
1202 return (all_of(Legal->getReductionVars(), [&](auto &Reduction) -> bool {
1203 const RecurrenceDescriptor &RdxDesc = Reduction.second;
1204 return TTI.isLegalToVectorizeReduction(RdxDesc, VF);
1205 }));
1206 }
1207
1208 /// Given costs for both strategies, return true if the scalar predication
1209 /// lowering should be used for div/rem. This incorporates an override
1210 /// option so it is not simply a cost comparison.
1212 InstructionCost SafeDivisorCost) const {
1213 switch (ForceSafeDivisor) {
1214 case cl::BOU_UNSET:
1215 return ScalarCost < SafeDivisorCost;
1216 case cl::BOU_TRUE:
1217 return false;
1218 case cl::BOU_FALSE:
1219 return true;
1220 }
1221 llvm_unreachable("impossible case value");
1222 }
1223
1224 /// Returns true if \p I is an instruction which requires predication and
1225 /// for which our chosen predication strategy is scalarization (i.e. we
1226 /// don't have an alternate strategy such as masking available).
1227 /// \p VF is the vectorization factor that will be used to vectorize \p I.
1228 bool isScalarWithPredication(Instruction *I, ElementCount VF) const;
1229
1230 /// Returns true if \p I is an instruction that needs to be predicated
1231 /// at runtime. The result is independent of the predication mechanism.
1232 /// Superset of instructions that return true for isScalarWithPredication.
1233 bool isPredicatedInst(Instruction *I) const;
1234
1235 /// Return the costs for our two available strategies for lowering a
1236 /// div/rem operation which requires speculating at least one lane.
1237 /// First result is for scalarization (will be invalid for scalable
1238 /// vectors); second is for the safe-divisor strategy.
1239 std::pair<InstructionCost, InstructionCost>
1240 getDivRemSpeculationCost(Instruction *I,
1241 ElementCount VF) const;
1242
1243 /// Returns true if \p I is a memory instruction with consecutive memory
1244 /// access that can be widened.
1245 bool memoryInstructionCanBeWidened(Instruction *I, ElementCount VF);
1246
1247 /// Returns true if \p I is a memory instruction in an interleaved-group
1248 /// of memory accesses that can be vectorized with wide vector loads/stores
1249 /// and shuffles.
1250 bool interleavedAccessCanBeWidened(Instruction *I, ElementCount VF) const;
1251
1252 /// Check if \p Instr belongs to any interleaved access group.
1254 return InterleaveInfo.isInterleaved(Instr);
1255 }
1256
1257 /// Get the interleaved access group that \p Instr belongs to.
1260 return InterleaveInfo.getInterleaveGroup(Instr);
1261 }
1262
1263 /// Returns true if we're required to use a scalar epilogue for at least
1264 /// the final iteration of the original loop.
1265 bool requiresScalarEpilogue(bool IsVectorizing) const {
1266 if (!isScalarEpilogueAllowed()) {
1267 LLVM_DEBUG(dbgs() << "LV: Loop does not require scalar epilogue\n");
1268 return false;
1269 }
1270 // If we might exit from anywhere but the latch and early exit vectorization
1271 // is disabled, we must run the exiting iteration in scalar form.
1272 if (TheLoop->getExitingBlock() != TheLoop->getLoopLatch() &&
1273 !(EnableEarlyExitVectorization && Legal->hasUncountableEarlyExit())) {
1274 LLVM_DEBUG(dbgs() << "LV: Loop requires scalar epilogue: not exiting "
1275 "from latch block\n");
1276 return true;
1277 }
1278 if (IsVectorizing && InterleaveInfo.requiresScalarEpilogue()) {
1279 LLVM_DEBUG(dbgs() << "LV: Loop requires scalar epilogue: "
1280 "interleaved group requires scalar epilogue\n");
1281 return true;
1282 }
1283 LLVM_DEBUG(dbgs() << "LV: Loop does not require scalar epilogue\n");
1284 return false;
1285 }
1286
1287 /// Returns true if a scalar epilogue is not allowed due to optsize or a
1288 /// loop hint annotation.
1290 return ScalarEpilogueStatus == CM_ScalarEpilogueAllowed;
1291 }
1292
1293 /// Returns the TailFoldingStyle that is best for the current loop.
1294 TailFoldingStyle getTailFoldingStyle(bool IVUpdateMayOverflow = true) const {
1295 if (!ChosenTailFoldingStyle)
1297 return IVUpdateMayOverflow ? ChosenTailFoldingStyle->first
1298 : ChosenTailFoldingStyle->second;
1299 }
1300
1301 /// Selects and saves TailFoldingStyle for 2 options - if IV update may
1302 /// overflow or not.
1303 /// \param IsScalableVF true if scalable vector factors enabled.
1304 /// \param UserIC User specific interleave count.
1305 void setTailFoldingStyles(bool IsScalableVF, unsigned UserIC) {
1306 assert(!ChosenTailFoldingStyle && "Tail folding must not be selected yet.");
1307 if (!Legal->canFoldTailByMasking()) {
1308 ChosenTailFoldingStyle = {TailFoldingStyle::None, TailFoldingStyle::None};
1309 return;
1310 }
1311
1312 // Default to TTI preference, but allow command line override.
1313 ChosenTailFoldingStyle = {
1314 TTI.getPreferredTailFoldingStyle(/*IVUpdateMayOverflow=*/true),
1315 TTI.getPreferredTailFoldingStyle(/*IVUpdateMayOverflow=*/false)};
1316 if (ForceTailFoldingStyle.getNumOccurrences())
1317 ChosenTailFoldingStyle = {ForceTailFoldingStyle.getValue(),
1318 ForceTailFoldingStyle.getValue()};
1319
1320 if (ChosenTailFoldingStyle->first != TailFoldingStyle::DataWithEVL &&
1321 ChosenTailFoldingStyle->second != TailFoldingStyle::DataWithEVL)
1322 return;
1323 // Override EVL styles if needed.
1324 // FIXME: Investigate opportunity for fixed vector factor.
1325 bool EVLIsLegal = UserIC <= 1 && IsScalableVF &&
1326 TTI.hasActiveVectorLength() && !EnableVPlanNativePath;
1327 if (EVLIsLegal)
1328 return;
1329 // If for some reason EVL mode is unsupported, fallback to a scalar epilogue
1330 // if it's allowed, or DataWithoutLaneMask otherwise.
1331 if (ScalarEpilogueStatus == CM_ScalarEpilogueAllowed ||
1332 ScalarEpilogueStatus == CM_ScalarEpilogueNotNeededUsePredicate)
1333 ChosenTailFoldingStyle = {TailFoldingStyle::None, TailFoldingStyle::None};
1334 else
1335 ChosenTailFoldingStyle = {TailFoldingStyle::DataWithoutLaneMask,
1337
1338 LLVM_DEBUG(
1339 dbgs() << "LV: Preference for VP intrinsics indicated. Will "
1340 "not try to generate VP Intrinsics "
1341 << (UserIC > 1
1342 ? "since interleave count specified is greater than 1.\n"
1343 : "due to non-interleaving reasons.\n"));
1344 }
1345
1346 /// Returns true if all loop blocks should be masked to fold tail loop.
1347 bool foldTailByMasking() const {
1348 // TODO: check if it is possible to check for None style independent of
1349 // IVUpdateMayOverflow flag in getTailFoldingStyle.
1351 }
1352
1353 /// Return maximum safe number of elements to be processed per vector
1354 /// iteration, which do not prevent store-load forwarding and are safe with
1355 /// regard to the memory dependencies. Required for EVL-based VPlans to
1356 /// correctly calculate AVL (application vector length) as min(remaining AVL,
1357 /// MaxSafeElements).
1358 /// TODO: need to consider adjusting cost model to use this value as a
1359 /// vectorization factor for EVL-based vectorization.
1360 std::optional<unsigned> getMaxSafeElements() const { return MaxSafeElements; }
1361
1362 /// Returns true if the instructions in this block requires predication
1363 /// for any reason, e.g. because tail folding now requires a predicate
1364 /// or because the block in the original loop was predicated.
1366 return foldTailByMasking() || Legal->blockNeedsPredication(BB);
1367 }
1368
1369 /// Returns true if VP intrinsics with explicit vector length support should
1370 /// be generated in the tail folded loop.
1374
1375 /// Returns true if the Phi is part of an inloop reduction.
1376 bool isInLoopReduction(PHINode *Phi) const {
1377 return InLoopReductions.contains(Phi);
1378 }
1379
1380 /// Returns true if the predicated reduction select should be used to set the
1381 /// incoming value for the reduction phi.
1383 // Force to use predicated reduction select since the EVL of the
1384 // second-to-last iteration might not be VF*UF.
1385 if (foldTailWithEVL())
1386 return true;
1388 TTI.preferPredicatedReductionSelect();
1389 }
1390
1391 /// Estimate cost of an intrinsic call instruction CI if it were vectorized
1392 /// with factor VF. Return the cost of the instruction, including
1393 /// scalarization overhead if it's needed.
1394 InstructionCost getVectorIntrinsicCost(CallInst *CI, ElementCount VF) const;
1395
1396 /// Estimate cost of a call instruction CI if it were vectorized with factor
1397 /// VF. Return the cost of the instruction, including scalarization overhead
1398 /// if it's needed.
1399 InstructionCost getVectorCallCost(CallInst *CI, ElementCount VF) const;
1400
1401 /// Invalidates decisions already taken by the cost model.
1403 WideningDecisions.clear();
1404 CallWideningDecisions.clear();
1405 Uniforms.clear();
1406 Scalars.clear();
1407 }
1408
1409 /// Returns the expected execution cost. The unit of the cost does
1410 /// not matter because we use the 'cost' units to compare different
1411 /// vector widths. The cost that is returned is *not* normalized by
1412 /// the factor width.
1413 InstructionCost expectedCost(ElementCount VF);
1414
1415 bool hasPredStores() const { return NumPredStores > 0; }
1416
1417 /// Returns true if epilogue vectorization is considered profitable, and
1418 /// false otherwise.
1419 /// \p VF is the vectorization factor chosen for the original loop.
1420 /// \p Multiplier is an aditional scaling factor applied to VF before
1421 /// comparing to EpilogueVectorizationMinVF.
1422 bool isEpilogueVectorizationProfitable(const ElementCount VF,
1423 const unsigned IC) const;
1424
1425 /// Returns the execution time cost of an instruction for a given vector
1426 /// width. Vector width of one means scalar.
1427 InstructionCost getInstructionCost(Instruction *I, ElementCount VF);
1428
1429 /// Return the cost of instructions in an inloop reduction pattern, if I is
1430 /// part of that pattern.
1431 std::optional<InstructionCost> getReductionPatternCost(Instruction *I,
1432 ElementCount VF,
1433 Type *VectorTy) const;
1434
1435 /// Returns true if \p Op should be considered invariant and if it is
1436 /// trivially hoistable.
1437 bool shouldConsiderInvariant(Value *Op);
1438
1439 /// Return the value of vscale used for tuning the cost model.
1440 std::optional<unsigned> getVScaleForTuning() const { return VScaleForTuning; }
1441
1442private:
1443 unsigned NumPredStores = 0;
1444
1445 /// Used to store the value of vscale used for tuning the cost model. It is
1446 /// initialized during object construction.
1447 std::optional<unsigned> VScaleForTuning;
1448
1449 /// Initializes the value of vscale used for tuning the cost model. If
1450 /// vscale_range.min == vscale_range.max then return vscale_range.max, else
1451 /// return the value returned by the corresponding TTI method.
1452 void initializeVScaleForTuning() {
1453 const Function *Fn = TheLoop->getHeader()->getParent();
1454 if (Fn->hasFnAttribute(Attribute::VScaleRange)) {
1455 auto Attr = Fn->getFnAttribute(Attribute::VScaleRange);
1456 auto Min = Attr.getVScaleRangeMin();
1457 auto Max = Attr.getVScaleRangeMax();
1458 if (Max && Min == Max) {
1459 VScaleForTuning = Max;
1460 return;
1461 }
1462 }
1463
1464 VScaleForTuning = TTI.getVScaleForTuning();
1465 }
1466
1467 /// \return An upper bound for the vectorization factors for both
1468 /// fixed and scalable vectorization, where the minimum-known number of
1469 /// elements is a power-of-2 larger than zero. If scalable vectorization is
1470 /// disabled or unsupported, then the scalable part will be equal to
1471 /// ElementCount::getScalable(0).
1472 FixedScalableVFPair computeFeasibleMaxVF(unsigned MaxTripCount,
1473 ElementCount UserVF,
1474 bool FoldTailByMasking);
1475
1476 /// If \p VF > MaxTripcount, clamps it to the next lower VF that is <=
1477 /// MaxTripCount.
1478 ElementCount clampVFByMaxTripCount(ElementCount VF, unsigned MaxTripCount,
1479 bool FoldTailByMasking) const;
1480
1481 /// \return the maximized element count based on the targets vector
1482 /// registers and the loop trip-count, but limited to a maximum safe VF.
1483 /// This is a helper function of computeFeasibleMaxVF.
1484 ElementCount getMaximizedVFForTarget(unsigned MaxTripCount,
1485 unsigned SmallestType,
1486 unsigned WidestType,
1487 ElementCount MaxSafeVF,
1488 bool FoldTailByMasking);
1489
1490 /// Checks if scalable vectorization is supported and enabled. Caches the
1491 /// result to avoid repeated debug dumps for repeated queries.
1492 bool isScalableVectorizationAllowed();
1493
1494 /// \return the maximum legal scalable VF, based on the safe max number
1495 /// of elements.
1496 ElementCount getMaxLegalScalableVF(unsigned MaxSafeElements);
1497
1498 /// Calculate vectorization cost of memory instruction \p I.
1499 InstructionCost getMemoryInstructionCost(Instruction *I, ElementCount VF);
1500
1501 /// The cost computation for scalarized memory instruction.
1502 InstructionCost getMemInstScalarizationCost(Instruction *I, ElementCount VF);
1503
1504 /// The cost computation for interleaving group of memory instructions.
1505 InstructionCost getInterleaveGroupCost(Instruction *I, ElementCount VF);
1506
1507 /// The cost computation for Gather/Scatter instruction.
1508 InstructionCost getGatherScatterCost(Instruction *I, ElementCount VF);
1509
1510 /// The cost computation for widening instruction \p I with consecutive
1511 /// memory access.
1512 InstructionCost getConsecutiveMemOpCost(Instruction *I, ElementCount VF);
1513
1514 /// The cost calculation for Load/Store instruction \p I with uniform pointer -
1515 /// Load: scalar load + broadcast.
1516 /// Store: scalar store + (loop invariant value stored? 0 : extract of last
1517 /// element)
1518 InstructionCost getUniformMemOpCost(Instruction *I, ElementCount VF);
1519
1520 /// Estimate the overhead of scalarizing an instruction. This is a
1521 /// convenience wrapper for the type-based getScalarizationOverhead API.
1523 ElementCount VF) const;
1524
1525 /// Returns true if an artificially high cost for emulated masked memrefs
1526 /// should be used.
1527 bool useEmulatedMaskMemRefHack(Instruction *I, ElementCount VF);
1528
1529 /// Map of scalar integer values to the smallest bitwidth they can be legally
1530 /// represented as. The vector equivalents of these values should be truncated
1531 /// to this type.
1532 MapVector<Instruction *, uint64_t> MinBWs;
1533
1534 /// A type representing the costs for instructions if they were to be
1535 /// scalarized rather than vectorized. The entries are Instruction-Cost
1536 /// pairs.
1537 using ScalarCostsTy = MapVector<Instruction *, InstructionCost>;
1538
1539 /// A set containing all BasicBlocks that are known to present after
1540 /// vectorization as a predicated block.
1541 DenseMap<ElementCount, SmallPtrSet<BasicBlock *, 4>>
1542 PredicatedBBsAfterVectorization;
1543
1544 /// Records whether it is allowed to have the original scalar loop execute at
1545 /// least once. This may be needed as a fallback loop in case runtime
1546 /// aliasing/dependence checks fail, or to handle the tail/remainder
1547 /// iterations when the trip count is unknown or doesn't divide by the VF,
1548 /// or as a peel-loop to handle gaps in interleave-groups.
1549 /// Under optsize and when the trip count is very small we don't allow any
1550 /// iterations to execute in the scalar loop.
1551 ScalarEpilogueLowering ScalarEpilogueStatus = CM_ScalarEpilogueAllowed;
1552
1553 /// Control finally chosen tail folding style. The first element is used if
1554 /// the IV update may overflow, the second element - if it does not.
1555 std::optional<std::pair<TailFoldingStyle, TailFoldingStyle>>
1556 ChosenTailFoldingStyle;
1557
1558 /// true if scalable vectorization is supported and enabled.
1559 std::optional<bool> IsScalableVectorizationAllowed;
1560
1561 /// Maximum safe number of elements to be processed per vector iteration,
1562 /// which do not prevent store-load forwarding and are safe with regard to the
1563 /// memory dependencies. Required for EVL-based veectorization, where this
1564 /// value is used as the upper bound of the safe AVL.
1565 std::optional<unsigned> MaxSafeElements;
1566
1567 /// A map holding scalar costs for different vectorization factors. The
1568 /// presence of a cost for an instruction in the mapping indicates that the
1569 /// instruction will be scalarized when vectorizing with the associated
1570 /// vectorization factor. The entries are VF-ScalarCostTy pairs.
1571 MapVector<ElementCount, ScalarCostsTy> InstsToScalarize;
1572
1573 /// Holds the instructions known to be uniform after vectorization.
1574 /// The data is collected per VF.
1575 DenseMap<ElementCount, SmallPtrSet<Instruction *, 4>> Uniforms;
1576
1577 /// Holds the instructions known to be scalar after vectorization.
1578 /// The data is collected per VF.
1579 DenseMap<ElementCount, SmallPtrSet<Instruction *, 4>> Scalars;
1580
1581 /// Holds the instructions (address computations) that are forced to be
1582 /// scalarized.
1583 DenseMap<ElementCount, SmallPtrSet<Instruction *, 4>> ForcedScalars;
1584
1585 /// PHINodes of the reductions that should be expanded in-loop.
1586 SmallPtrSet<PHINode *, 4> InLoopReductions;
1587
1588 /// A Map of inloop reduction operations and their immediate chain operand.
1589 /// FIXME: This can be removed once reductions can be costed correctly in
1590 /// VPlan. This was added to allow quick lookup of the inloop operations.
1591 DenseMap<Instruction *, Instruction *> InLoopReductionImmediateChains;
1592
1593 /// Returns the expected difference in cost from scalarizing the expression
1594 /// feeding a predicated instruction \p PredInst. The instructions to
1595 /// scalarize and their scalar costs are collected in \p ScalarCosts. A
1596 /// non-negative return value implies the expression will be scalarized.
1597 /// Currently, only single-use chains are considered for scalarization.
1598 InstructionCost computePredInstDiscount(Instruction *PredInst,
1599 ScalarCostsTy &ScalarCosts,
1600 ElementCount VF);
1601
1602 /// Collect the instructions that are uniform after vectorization. An
1603 /// instruction is uniform if we represent it with a single scalar value in
1604 /// the vectorized loop corresponding to each vector iteration. Examples of
1605 /// uniform instructions include pointer operands of consecutive or
1606 /// interleaved memory accesses. Note that although uniformity implies an
1607 /// instruction will be scalar, the reverse is not true. In general, a
1608 /// scalarized instruction will be represented by VF scalar values in the
1609 /// vectorized loop, each corresponding to an iteration of the original
1610 /// scalar loop.
1611 void collectLoopUniforms(ElementCount VF);
1612
1613 /// Collect the instructions that are scalar after vectorization. An
1614 /// instruction is scalar if it is known to be uniform or will be scalarized
1615 /// during vectorization. collectLoopScalars should only add non-uniform nodes
1616 /// to the list if they are used by a load/store instruction that is marked as
1617 /// CM_Scalarize. Non-uniform scalarized instructions will be represented by
1618 /// VF values in the vectorized loop, each corresponding to an iteration of
1619 /// the original scalar loop.
1620 void collectLoopScalars(ElementCount VF);
1621
1622 /// Keeps cost model vectorization decision and cost for instructions.
1623 /// Right now it is used for memory instructions only.
1624 using DecisionList = DenseMap<std::pair<Instruction *, ElementCount>,
1625 std::pair<InstWidening, InstructionCost>>;
1626
1627 DecisionList WideningDecisions;
1628
1629 using CallDecisionList =
1630 DenseMap<std::pair<CallInst *, ElementCount>, CallWideningDecision>;
1631
1632 CallDecisionList CallWideningDecisions;
1633
1634 /// Returns true if \p V is expected to be vectorized and it needs to be
1635 /// extracted.
1636 bool needsExtract(Value *V, ElementCount VF) const {
1638 if (VF.isScalar() || !I || !TheLoop->contains(I) ||
1639 TheLoop->isLoopInvariant(I) ||
1640 getWideningDecision(I, VF) == CM_Scalarize ||
1641 (isa<CallInst>(I) &&
1642 getCallWideningDecision(cast<CallInst>(I), VF).Kind == CM_Scalarize))
1643 return false;
1644
1645 // Assume we can vectorize V (and hence we need extraction) if the
1646 // scalars are not computed yet. This can happen, because it is called
1647 // via getScalarizationOverhead from setCostBasedWideningDecision, before
1648 // the scalars are collected. That should be a safe assumption in most
1649 // cases, because we check if the operands have vectorizable types
1650 // beforehand in LoopVectorizationLegality.
1651 return !Scalars.contains(VF) || !isScalarAfterVectorization(I, VF);
1652 };
1653
1654 /// Returns a range containing only operands needing to be extracted.
1655 SmallVector<Value *, 4> filterExtractingOperands(Instruction::op_range Ops,
1656 ElementCount VF) const {
1657
1658 SmallPtrSet<const Value *, 4> UniqueOperands;
1660 for (Value *Op : Ops) {
1661 if (isa<Constant>(Op) || !UniqueOperands.insert(Op).second ||
1662 !needsExtract(Op, VF))
1663 continue;
1664 Res.push_back(Op);
1665 }
1666 return Res;
1667 }
1668
1669public:
1670 /// The loop that we evaluate.
1672
1673 /// Predicated scalar evolution analysis.
1675
1676 /// Loop Info analysis.
1678
1679 /// Vectorization legality.
1681
1682 /// Vector target information.
1684
1685 /// Target Library Info.
1687
1688 /// Demanded bits analysis.
1690
1691 /// Assumption cache.
1693
1694 /// Interface to emit optimization remarks.
1696
1698
1699 /// Loop Vectorize Hint.
1701
1702 /// The interleave access information contains groups of interleaved accesses
1703 /// with the same stride and close to each other.
1705
1706 /// Values to ignore in the cost model.
1708
1709 /// Values to ignore in the cost model when VF > 1.
1711
1712 /// All element types found in the loop.
1714
1715 /// The kind of cost that we are calculating
1717
1718 /// Whether this loop should be optimized for size based on function attribute
1719 /// or profile information.
1721
1722 /// The highest VF possible for this loop, without using MaxBandwidth.
1724};
1725} // end namespace llvm
1726
1727namespace {
1728/// Helper struct to manage generating runtime checks for vectorization.
1729///
1730/// The runtime checks are created up-front in temporary blocks to allow better
1731/// estimating the cost and un-linked from the existing IR. After deciding to
1732/// vectorize, the checks are moved back. If deciding not to vectorize, the
1733/// temporary blocks are completely removed.
1734class GeneratedRTChecks {
1735 /// Basic block which contains the generated SCEV checks, if any.
1736 BasicBlock *SCEVCheckBlock = nullptr;
1737
1738 /// The value representing the result of the generated SCEV checks. If it is
1739 /// nullptr no SCEV checks have been generated.
1740 Value *SCEVCheckCond = nullptr;
1741
1742 /// Basic block which contains the generated memory runtime checks, if any.
1743 BasicBlock *MemCheckBlock = nullptr;
1744
1745 /// The value representing the result of the generated memory runtime checks.
1746 /// If it is nullptr no memory runtime checks have been generated.
1747 Value *MemRuntimeCheckCond = nullptr;
1748
1749 DominatorTree *DT;
1750 LoopInfo *LI;
1752
1753 SCEVExpander SCEVExp;
1754 SCEVExpander MemCheckExp;
1755
1756 bool CostTooHigh = false;
1757
1758 Loop *OuterLoop = nullptr;
1759
1761
1762 /// The kind of cost that we are calculating
1764
1765public:
1766 GeneratedRTChecks(PredicatedScalarEvolution &PSE, DominatorTree *DT,
1769 : DT(DT), LI(LI), TTI(TTI),
1770 SCEVExp(*PSE.getSE(), DL, "scev.check", /*PreserveLCSSA=*/false),
1771 MemCheckExp(*PSE.getSE(), DL, "scev.check", /*PreserveLCSSA=*/false),
1772 PSE(PSE), CostKind(CostKind) {}
1773
1774 /// Generate runtime checks in SCEVCheckBlock and MemCheckBlock, so we can
1775 /// accurately estimate the cost of the runtime checks. The blocks are
1776 /// un-linked from the IR and are added back during vector code generation. If
1777 /// there is no vector code generation, the check blocks are removed
1778 /// completely.
1779 void create(Loop *L, const LoopAccessInfo &LAI,
1780 const SCEVPredicate &UnionPred, ElementCount VF, unsigned IC) {
1781
1782 // Hard cutoff to limit compile-time increase in case a very large number of
1783 // runtime checks needs to be generated.
1784 // TODO: Skip cutoff if the loop is guaranteed to execute, e.g. due to
1785 // profile info.
1786 CostTooHigh =
1788 if (CostTooHigh)
1789 return;
1790
1791 BasicBlock *LoopHeader = L->getHeader();
1792 BasicBlock *Preheader = L->getLoopPreheader();
1793
1794 // Use SplitBlock to create blocks for SCEV & memory runtime checks to
1795 // ensure the blocks are properly added to LoopInfo & DominatorTree. Those
1796 // may be used by SCEVExpander. The blocks will be un-linked from their
1797 // predecessors and removed from LI & DT at the end of the function.
1798 if (!UnionPred.isAlwaysTrue()) {
1799 SCEVCheckBlock = SplitBlock(Preheader, Preheader->getTerminator(), DT, LI,
1800 nullptr, "vector.scevcheck");
1801
1802 SCEVCheckCond = SCEVExp.expandCodeForPredicate(
1803 &UnionPred, SCEVCheckBlock->getTerminator());
1804 if (isa<Constant>(SCEVCheckCond)) {
1805 // Clean up directly after expanding the predicate to a constant, to
1806 // avoid further expansions re-using anything left over from SCEVExp.
1807 SCEVExpanderCleaner SCEVCleaner(SCEVExp);
1808 SCEVCleaner.cleanup();
1809 }
1810 }
1811
1812 const auto &RtPtrChecking = *LAI.getRuntimePointerChecking();
1813 if (RtPtrChecking.Need) {
1814 auto *Pred = SCEVCheckBlock ? SCEVCheckBlock : Preheader;
1815 MemCheckBlock = SplitBlock(Pred, Pred->getTerminator(), DT, LI, nullptr,
1816 "vector.memcheck");
1817
1818 auto DiffChecks = RtPtrChecking.getDiffChecks();
1819 if (DiffChecks) {
1820 Value *RuntimeVF = nullptr;
1821 MemRuntimeCheckCond = addDiffRuntimeChecks(
1822 MemCheckBlock->getTerminator(), *DiffChecks, MemCheckExp,
1823 [VF, &RuntimeVF](IRBuilderBase &B, unsigned Bits) {
1824 if (!RuntimeVF)
1825 RuntimeVF = getRuntimeVF(B, B.getIntNTy(Bits), VF);
1826 return RuntimeVF;
1827 },
1828 IC);
1829 } else {
1830 MemRuntimeCheckCond = addRuntimeChecks(
1831 MemCheckBlock->getTerminator(), L, RtPtrChecking.getChecks(),
1833 }
1834 assert(MemRuntimeCheckCond &&
1835 "no RT checks generated although RtPtrChecking "
1836 "claimed checks are required");
1837 }
1838
1839 SCEVExp.eraseDeadInstructions(SCEVCheckCond);
1840
1841 if (!MemCheckBlock && !SCEVCheckBlock)
1842 return;
1843
1844 // Unhook the temporary block with the checks, update various places
1845 // accordingly.
1846 if (SCEVCheckBlock)
1847 SCEVCheckBlock->replaceAllUsesWith(Preheader);
1848 if (MemCheckBlock)
1849 MemCheckBlock->replaceAllUsesWith(Preheader);
1850
1851 if (SCEVCheckBlock) {
1852 SCEVCheckBlock->getTerminator()->moveBefore(
1853 Preheader->getTerminator()->getIterator());
1854 auto *UI = new UnreachableInst(Preheader->getContext(), SCEVCheckBlock);
1855 UI->setDebugLoc(DebugLoc::getTemporary());
1856 Preheader->getTerminator()->eraseFromParent();
1857 }
1858 if (MemCheckBlock) {
1859 MemCheckBlock->getTerminator()->moveBefore(
1860 Preheader->getTerminator()->getIterator());
1861 auto *UI = new UnreachableInst(Preheader->getContext(), MemCheckBlock);
1862 UI->setDebugLoc(DebugLoc::getTemporary());
1863 Preheader->getTerminator()->eraseFromParent();
1864 }
1865
1866 DT->changeImmediateDominator(LoopHeader, Preheader);
1867 if (MemCheckBlock) {
1868 DT->eraseNode(MemCheckBlock);
1869 LI->removeBlock(MemCheckBlock);
1870 }
1871 if (SCEVCheckBlock) {
1872 DT->eraseNode(SCEVCheckBlock);
1873 LI->removeBlock(SCEVCheckBlock);
1874 }
1875
1876 // Outer loop is used as part of the later cost calculations.
1877 OuterLoop = L->getParentLoop();
1878 }
1879
1881 if (SCEVCheckBlock || MemCheckBlock)
1882 LLVM_DEBUG(dbgs() << "Calculating cost of runtime checks:\n");
1883
1884 if (CostTooHigh) {
1886 Cost.setInvalid();
1887 LLVM_DEBUG(dbgs() << " number of checks exceeded threshold\n");
1888 return Cost;
1889 }
1890
1891 InstructionCost RTCheckCost = 0;
1892 if (SCEVCheckBlock)
1893 for (Instruction &I : *SCEVCheckBlock) {
1894 if (SCEVCheckBlock->getTerminator() == &I)
1895 continue;
1897 LLVM_DEBUG(dbgs() << " " << C << " for " << I << "\n");
1898 RTCheckCost += C;
1899 }
1900 if (MemCheckBlock) {
1901 InstructionCost MemCheckCost = 0;
1902 for (Instruction &I : *MemCheckBlock) {
1903 if (MemCheckBlock->getTerminator() == &I)
1904 continue;
1906 LLVM_DEBUG(dbgs() << " " << C << " for " << I << "\n");
1907 MemCheckCost += C;
1908 }
1909
1910 // If the runtime memory checks are being created inside an outer loop
1911 // we should find out if these checks are outer loop invariant. If so,
1912 // the checks will likely be hoisted out and so the effective cost will
1913 // reduce according to the outer loop trip count.
1914 if (OuterLoop) {
1915 ScalarEvolution *SE = MemCheckExp.getSE();
1916 // TODO: If profitable, we could refine this further by analysing every
1917 // individual memory check, since there could be a mixture of loop
1918 // variant and invariant checks that mean the final condition is
1919 // variant.
1920 const SCEV *Cond = SE->getSCEV(MemRuntimeCheckCond);
1921 if (SE->isLoopInvariant(Cond, OuterLoop)) {
1922 // It seems reasonable to assume that we can reduce the effective
1923 // cost of the checks even when we know nothing about the trip
1924 // count. Assume that the outer loop executes at least twice.
1925 unsigned BestTripCount = 2;
1926
1927 // Get the best known TC estimate.
1928 if (auto EstimatedTC = getSmallBestKnownTC(
1929 PSE, OuterLoop, /* CanUseConstantMax = */ false))
1930 if (EstimatedTC->isFixed())
1931 BestTripCount = EstimatedTC->getFixedValue();
1932
1933 InstructionCost NewMemCheckCost = MemCheckCost / BestTripCount;
1934
1935 // Let's ensure the cost is always at least 1.
1936 NewMemCheckCost = std::max(NewMemCheckCost.getValue(),
1937 (InstructionCost::CostType)1);
1938
1939 if (BestTripCount > 1)
1941 << "We expect runtime memory checks to be hoisted "
1942 << "out of the outer loop. Cost reduced from "
1943 << MemCheckCost << " to " << NewMemCheckCost << '\n');
1944
1945 MemCheckCost = NewMemCheckCost;
1946 }
1947 }
1948
1949 RTCheckCost += MemCheckCost;
1950 }
1951
1952 if (SCEVCheckBlock || MemCheckBlock)
1953 LLVM_DEBUG(dbgs() << "Total cost of runtime checks: " << RTCheckCost
1954 << "\n");
1955
1956 return RTCheckCost;
1957 }
1958
1959 /// Remove the created SCEV & memory runtime check blocks & instructions, if
1960 /// unused.
1961 ~GeneratedRTChecks() {
1962 SCEVExpanderCleaner SCEVCleaner(SCEVExp);
1963 SCEVExpanderCleaner MemCheckCleaner(MemCheckExp);
1964 bool SCEVChecksUsed = !SCEVCheckBlock || !pred_empty(SCEVCheckBlock);
1965 bool MemChecksUsed = !MemCheckBlock || !pred_empty(MemCheckBlock);
1966 if (SCEVChecksUsed)
1967 SCEVCleaner.markResultUsed();
1968
1969 if (MemChecksUsed) {
1970 MemCheckCleaner.markResultUsed();
1971 } else {
1972 auto &SE = *MemCheckExp.getSE();
1973 // Memory runtime check generation creates compares that use expanded
1974 // values. Remove them before running the SCEVExpanderCleaners.
1975 for (auto &I : make_early_inc_range(reverse(*MemCheckBlock))) {
1976 if (MemCheckExp.isInsertedInstruction(&I))
1977 continue;
1978 SE.forgetValue(&I);
1979 I.eraseFromParent();
1980 }
1981 }
1982 MemCheckCleaner.cleanup();
1983 SCEVCleaner.cleanup();
1984
1985 if (!SCEVChecksUsed)
1986 SCEVCheckBlock->eraseFromParent();
1987 if (!MemChecksUsed)
1988 MemCheckBlock->eraseFromParent();
1989 }
1990
1991 /// Retrieves the SCEVCheckCond and SCEVCheckBlock that were generated as IR
1992 /// outside VPlan.
1993 std::pair<Value *, BasicBlock *> getSCEVChecks() const {
1994 using namespace llvm::PatternMatch;
1995 if (!SCEVCheckCond || match(SCEVCheckCond, m_ZeroInt()))
1996 return {nullptr, nullptr};
1997
1998 return {SCEVCheckCond, SCEVCheckBlock};
1999 }
2000
2001 /// Retrieves the MemCheckCond and MemCheckBlock that were generated as IR
2002 /// outside VPlan.
2003 std::pair<Value *, BasicBlock *> getMemRuntimeChecks() const {
2004 using namespace llvm::PatternMatch;
2005 if (MemRuntimeCheckCond && match(MemRuntimeCheckCond, m_ZeroInt()))
2006 return {nullptr, nullptr};
2007 return {MemRuntimeCheckCond, MemCheckBlock};
2008 }
2009
2010 /// Return true if any runtime checks have been added
2011 bool hasChecks() const {
2012 return getSCEVChecks().first || getMemRuntimeChecks().first;
2013 }
2014};
2015} // namespace
2016
2022
2027
2028// Return true if \p OuterLp is an outer loop annotated with hints for explicit
2029// vectorization. The loop needs to be annotated with #pragma omp simd
2030// simdlen(#) or #pragma clang vectorize(enable) vectorize_width(#). If the
2031// vector length information is not provided, vectorization is not considered
2032// explicit. Interleave hints are not allowed either. These limitations will be
2033// relaxed in the future.
2034// Please, note that we are currently forced to abuse the pragma 'clang
2035// vectorize' semantics. This pragma provides *auto-vectorization hints*
2036// (i.e., LV must check that vectorization is legal) whereas pragma 'omp simd'
2037// provides *explicit vectorization hints* (LV can bypass legal checks and
2038// assume that vectorization is legal). However, both hints are implemented
2039// using the same metadata (llvm.loop.vectorize, processed by
2040// LoopVectorizeHints). This will be fixed in the future when the native IR
2041// representation for pragma 'omp simd' is introduced.
2042static bool isExplicitVecOuterLoop(Loop *OuterLp,
2044 assert(!OuterLp->isInnermost() && "This is not an outer loop");
2045 LoopVectorizeHints Hints(OuterLp, true /*DisableInterleaving*/, *ORE);
2046
2047 // Only outer loops with an explicit vectorization hint are supported.
2048 // Unannotated outer loops are ignored.
2050 return false;
2051
2052 Function *Fn = OuterLp->getHeader()->getParent();
2053 if (!Hints.allowVectorization(Fn, OuterLp,
2054 true /*VectorizeOnlyWhenForced*/)) {
2055 LLVM_DEBUG(dbgs() << "LV: Loop hints prevent outer loop vectorization.\n");
2056 return false;
2057 }
2058
2059 if (Hints.getInterleave() > 1) {
2060 // TODO: Interleave support is future work.
2061 LLVM_DEBUG(dbgs() << "LV: Not vectorizing: Interleave is not supported for "
2062 "outer loops.\n");
2063 Hints.emitRemarkWithHints();
2064 return false;
2065 }
2066
2067 return true;
2068}
2069
2073 // Collect inner loops and outer loops without irreducible control flow. For
2074 // now, only collect outer loops that have explicit vectorization hints. If we
2075 // are stress testing the VPlan H-CFG construction, we collect the outermost
2076 // loop of every loop nest.
2077 if (L.isInnermost() || VPlanBuildStressTest ||
2079 LoopBlocksRPO RPOT(&L);
2080 RPOT.perform(LI);
2082 V.push_back(&L);
2083 // TODO: Collect inner loops inside marked outer loops in case
2084 // vectorization fails for the outer loop. Do not invoke
2085 // 'containsIrreducibleCFG' again for inner loops when the outer loop is
2086 // already known to be reducible. We can use an inherited attribute for
2087 // that.
2088 return;
2089 }
2090 }
2091 for (Loop *InnerL : L)
2092 collectSupportedLoops(*InnerL, LI, ORE, V);
2093}
2094
2095//===----------------------------------------------------------------------===//
2096// Implementation of LoopVectorizationLegality, InnerLoopVectorizer and
2097// LoopVectorizationCostModel and LoopVectorizationPlanner.
2098//===----------------------------------------------------------------------===//
2099
2100/// Compute the transformed value of Index at offset StartValue using step
2101/// StepValue.
2102/// For integer induction, returns StartValue + Index * StepValue.
2103/// For pointer induction, returns StartValue[Index * StepValue].
2104/// FIXME: The newly created binary instructions should contain nsw/nuw
2105/// flags, which can be found from the original scalar operations.
2106static Value *
2108 Value *Step,
2110 const BinaryOperator *InductionBinOp) {
2111 using namespace llvm::PatternMatch;
2112 Type *StepTy = Step->getType();
2113 Value *CastedIndex = StepTy->isIntegerTy()
2114 ? B.CreateSExtOrTrunc(Index, StepTy)
2115 : B.CreateCast(Instruction::SIToFP, Index, StepTy);
2116 if (CastedIndex != Index) {
2117 CastedIndex->setName(CastedIndex->getName() + ".cast");
2118 Index = CastedIndex;
2119 }
2120
2121 // Note: the IR at this point is broken. We cannot use SE to create any new
2122 // SCEV and then expand it, hoping that SCEV's simplification will give us
2123 // a more optimal code. Unfortunately, attempt of doing so on invalid IR may
2124 // lead to various SCEV crashes. So all we can do is to use builder and rely
2125 // on InstCombine for future simplifications. Here we handle some trivial
2126 // cases only.
2127 auto CreateAdd = [&B](Value *X, Value *Y) {
2128 assert(X->getType() == Y->getType() && "Types don't match!");
2129 if (match(X, m_ZeroInt()))
2130 return Y;
2131 if (match(Y, m_ZeroInt()))
2132 return X;
2133 return B.CreateAdd(X, Y);
2134 };
2135
2136 // We allow X to be a vector type, in which case Y will potentially be
2137 // splatted into a vector with the same element count.
2138 auto CreateMul = [&B](Value *X, Value *Y) {
2139 assert(X->getType()->getScalarType() == Y->getType() &&
2140 "Types don't match!");
2141 if (match(X, m_One()))
2142 return Y;
2143 if (match(Y, m_One()))
2144 return X;
2145 VectorType *XVTy = dyn_cast<VectorType>(X->getType());
2146 if (XVTy && !isa<VectorType>(Y->getType()))
2147 Y = B.CreateVectorSplat(XVTy->getElementCount(), Y);
2148 return B.CreateMul(X, Y);
2149 };
2150
2151 switch (InductionKind) {
2153 assert(!isa<VectorType>(Index->getType()) &&
2154 "Vector indices not supported for integer inductions yet");
2155 assert(Index->getType() == StartValue->getType() &&
2156 "Index type does not match StartValue type");
2157 if (isa<ConstantInt>(Step) && cast<ConstantInt>(Step)->isMinusOne())
2158 return B.CreateSub(StartValue, Index);
2159 auto *Offset = CreateMul(Index, Step);
2160 return CreateAdd(StartValue, Offset);
2161 }
2163 return B.CreatePtrAdd(StartValue, CreateMul(Index, Step));
2165 assert(!isa<VectorType>(Index->getType()) &&
2166 "Vector indices not supported for FP inductions yet");
2167 assert(Step->getType()->isFloatingPointTy() && "Expected FP Step value");
2168 assert(InductionBinOp &&
2169 (InductionBinOp->getOpcode() == Instruction::FAdd ||
2170 InductionBinOp->getOpcode() == Instruction::FSub) &&
2171 "Original bin op should be defined for FP induction");
2172
2173 Value *MulExp = B.CreateFMul(Step, Index);
2174 return B.CreateBinOp(InductionBinOp->getOpcode(), StartValue, MulExp,
2175 "induction");
2176 }
2178 return nullptr;
2179 }
2180 llvm_unreachable("invalid enum");
2181}
2182
2183static std::optional<unsigned> getMaxVScale(const Function &F,
2184 const TargetTransformInfo &TTI) {
2185 if (std::optional<unsigned> MaxVScale = TTI.getMaxVScale())
2186 return MaxVScale;
2187
2188 if (F.hasFnAttribute(Attribute::VScaleRange))
2189 return F.getFnAttribute(Attribute::VScaleRange).getVScaleRangeMax();
2190
2191 return std::nullopt;
2192}
2193
2194/// For the given VF and UF and maximum trip count computed for the loop, return
2195/// whether the induction variable might overflow in the vectorized loop. If not,
2196/// then we know a runtime overflow check always evaluates to false and can be
2197/// removed.
2199 const LoopVectorizationCostModel *Cost,
2200 ElementCount VF, std::optional<unsigned> UF = std::nullopt) {
2201 // Always be conservative if we don't know the exact unroll factor.
2202 unsigned MaxUF = UF ? *UF : Cost->TTI.getMaxInterleaveFactor(VF);
2203
2204 IntegerType *IdxTy = Cost->Legal->getWidestInductionType();
2205 APInt MaxUIntTripCount = IdxTy->getMask();
2206
2207 // We know the runtime overflow check is known false iff the (max) trip-count
2208 // is known and (max) trip-count + (VF * UF) does not overflow in the type of
2209 // the vector loop induction variable.
2210 if (unsigned TC = Cost->PSE.getSmallConstantMaxTripCount()) {
2211 uint64_t MaxVF = VF.getKnownMinValue();
2212 if (VF.isScalable()) {
2213 std::optional<unsigned> MaxVScale =
2214 getMaxVScale(*Cost->TheFunction, Cost->TTI);
2215 if (!MaxVScale)
2216 return false;
2217 MaxVF *= *MaxVScale;
2218 }
2219
2220 return (MaxUIntTripCount - TC).ugt(MaxVF * MaxUF);
2221 }
2222
2223 return false;
2224}
2225
2226// Return whether we allow using masked interleave-groups (for dealing with
2227// strided loads/stores that reside in predicated blocks, or for dealing
2228// with gaps).
2230 // If an override option has been passed in for interleaved accesses, use it.
2231 if (EnableMaskedInterleavedMemAccesses.getNumOccurrences() > 0)
2233
2234 return TTI.enableMaskedInterleavedAccessVectorization();
2235}
2236
2238 BasicBlock *CheckIRBB) {
2239 // Note: The block with the minimum trip-count check is already connected
2240 // during earlier VPlan construction.
2241 VPBlockBase *ScalarPH = Plan.getScalarPreheader();
2242 VPBlockBase *PreVectorPH = VectorPHVPBB->getSinglePredecessor();
2243 assert(PreVectorPH->getNumSuccessors() == 2 && "Expected 2 successors");
2244 assert(PreVectorPH->getSuccessors()[0] == ScalarPH && "Unexpected successor");
2245 VPIRBasicBlock *CheckVPIRBB = Plan.createVPIRBasicBlock(CheckIRBB);
2246 VPBlockUtils::insertOnEdge(PreVectorPH, VectorPHVPBB, CheckVPIRBB);
2247 PreVectorPH = CheckVPIRBB;
2248 VPBlockUtils::connectBlocks(PreVectorPH, ScalarPH);
2249 PreVectorPH->swapSuccessors();
2250
2251 // We just connected a new block to the scalar preheader. Update all
2252 // VPPhis by adding an incoming value for it, replicating the last value.
2253 unsigned NumPredecessors = ScalarPH->getNumPredecessors();
2254 for (VPRecipeBase &R : cast<VPBasicBlock>(ScalarPH)->phis()) {
2255 assert(isa<VPPhi>(&R) && "Phi expected to be VPPhi");
2256 assert(cast<VPPhi>(&R)->getNumIncoming() == NumPredecessors - 1 &&
2257 "must have incoming values for all operands");
2258 R.addOperand(R.getOperand(NumPredecessors - 2));
2259 }
2260}
2261
2263 BasicBlock *VectorPH, ElementCount VF, unsigned UF) const {
2264 // Generate code to check if the loop's trip count is less than VF * UF, or
2265 // equal to it in case a scalar epilogue is required; this implies that the
2266 // vector trip count is zero. This check also covers the case where adding one
2267 // to the backedge-taken count overflowed leading to an incorrect trip count
2268 // of zero. In this case we will also jump to the scalar loop.
2269 auto P = Cost->requiresScalarEpilogue(VF.isVector()) ? ICmpInst::ICMP_ULE
2271
2272 // Reuse existing vector loop preheader for TC checks.
2273 // Note that new preheader block is generated for vector loop.
2274 BasicBlock *const TCCheckBlock = VectorPH;
2276 TCCheckBlock->getContext(),
2277 InstSimplifyFolder(TCCheckBlock->getDataLayout()));
2278 Builder.SetInsertPoint(TCCheckBlock->getTerminator());
2279
2280 // If tail is to be folded, vector loop takes care of all iterations.
2282 Type *CountTy = Count->getType();
2283 Value *CheckMinIters = Builder.getFalse();
2284 auto CreateStep = [&]() -> Value * {
2285 // Create step with max(MinProTripCount, UF * VF).
2286 if (UF * VF.getKnownMinValue() >= MinProfitableTripCount.getKnownMinValue())
2287 return createStepForVF(Builder, CountTy, VF, UF);
2288
2289 Value *MinProfTC =
2290 Builder.CreateElementCount(CountTy, MinProfitableTripCount);
2291 if (!VF.isScalable())
2292 return MinProfTC;
2293 return Builder.CreateBinaryIntrinsic(
2294 Intrinsic::umax, MinProfTC, createStepForVF(Builder, CountTy, VF, UF));
2295 };
2296
2297 TailFoldingStyle Style = Cost->getTailFoldingStyle();
2298 if (Style == TailFoldingStyle::None) {
2299 Value *Step = CreateStep();
2300 ScalarEvolution &SE = *PSE.getSE();
2301 // TODO: Emit unconditional branch to vector preheader instead of
2302 // conditional branch with known condition.
2303 const SCEV *TripCountSCEV = SE.applyLoopGuards(SE.getSCEV(Count), OrigLoop);
2304 // Check if the trip count is < the step.
2305 if (SE.isKnownPredicate(P, TripCountSCEV, SE.getSCEV(Step))) {
2306 // TODO: Ensure step is at most the trip count when determining max VF and
2307 // UF, w/o tail folding.
2308 CheckMinIters = Builder.getTrue();
2310 TripCountSCEV, SE.getSCEV(Step))) {
2311 // Generate the minimum iteration check only if we cannot prove the
2312 // check is known to be true, or known to be false.
2313 CheckMinIters = Builder.CreateICmp(P, Count, Step, "min.iters.check");
2314 } // else step known to be < trip count, use CheckMinIters preset to false.
2315 } else if (VF.isScalable() && !TTI->isVScaleKnownToBeAPowerOfTwo() &&
2318 // vscale is not necessarily a power-of-2, which means we cannot guarantee
2319 // an overflow to zero when updating induction variables and so an
2320 // additional overflow check is required before entering the vector loop.
2321
2322 // Get the maximum unsigned value for the type.
2323 Value *MaxUIntTripCount =
2324 ConstantInt::get(CountTy, cast<IntegerType>(CountTy)->getMask());
2325 Value *LHS = Builder.CreateSub(MaxUIntTripCount, Count);
2326
2327 // Don't execute the vector loop if (UMax - n) < (VF * UF).
2328 CheckMinIters = Builder.CreateICmp(ICmpInst::ICMP_ULT, LHS, CreateStep());
2329 }
2330 return CheckMinIters;
2331}
2332
2333/// Replace \p VPBB with a VPIRBasicBlock wrapping \p IRBB. All recipes from \p
2334/// VPBB are moved to the end of the newly created VPIRBasicBlock. All
2335/// predecessors and successors of VPBB, if any, are rewired to the new
2336/// VPIRBasicBlock. If \p VPBB may be unreachable, \p Plan must be passed.
2338 BasicBlock *IRBB,
2339 VPlan *Plan = nullptr) {
2340 if (!Plan)
2341 Plan = VPBB->getPlan();
2342 VPIRBasicBlock *IRVPBB = Plan->createVPIRBasicBlock(IRBB);
2343 auto IP = IRVPBB->begin();
2344 for (auto &R : make_early_inc_range(VPBB->phis()))
2345 R.moveBefore(*IRVPBB, IP);
2346
2347 for (auto &R :
2349 R.moveBefore(*IRVPBB, IRVPBB->end());
2350
2351 VPBlockUtils::reassociateBlocks(VPBB, IRVPBB);
2352 // VPBB is now dead and will be cleaned up when the plan gets destroyed.
2353 return IRVPBB;
2354}
2355
2357 BasicBlock *VectorPH = OrigLoop->getLoopPreheader();
2358 assert(VectorPH && "Invalid loop structure");
2359 assert((OrigLoop->getUniqueLatchExitBlock() ||
2360 Cost->requiresScalarEpilogue(VF.isVector())) &&
2361 "loops not exiting via the latch without required epilogue?");
2362
2363 // NOTE: The Plan's scalar preheader VPBB isn't replaced with a VPIRBasicBlock
2364 // wrapping the newly created scalar preheader here at the moment, because the
2365 // Plan's scalar preheader may be unreachable at this point. Instead it is
2366 // replaced in executePlan.
2367 return SplitBlock(VectorPH, VectorPH->getTerminator(), DT, LI, nullptr,
2368 Twine(Prefix) + "scalar.ph");
2369}
2370
2371/// Return the expanded step for \p ID using \p ExpandedSCEVs to look up SCEV
2372/// expansion results.
2374 const SCEV2ValueTy &ExpandedSCEVs) {
2375 const SCEV *Step = ID.getStep();
2376 if (auto *C = dyn_cast<SCEVConstant>(Step))
2377 return C->getValue();
2378 if (auto *U = dyn_cast<SCEVUnknown>(Step))
2379 return U->getValue();
2380 Value *V = ExpandedSCEVs.lookup(Step);
2381 assert(V && "SCEV must be expanded at this point");
2382 return V;
2383}
2384
2385/// Knowing that loop \p L executes a single vector iteration, add instructions
2386/// that will get simplified and thus should not have any cost to \p
2387/// InstsToIgnore.
2390 SmallPtrSetImpl<Instruction *> &InstsToIgnore) {
2391 auto *Cmp = L->getLatchCmpInst();
2392 if (Cmp)
2393 InstsToIgnore.insert(Cmp);
2394 for (const auto &KV : IL) {
2395 // Extract the key by hand so that it can be used in the lambda below. Note
2396 // that captured structured bindings are a C++20 extension.
2397 const PHINode *IV = KV.first;
2398
2399 // Get next iteration value of the induction variable.
2400 Instruction *IVInst =
2401 cast<Instruction>(IV->getIncomingValueForBlock(L->getLoopLatch()));
2402 if (all_of(IVInst->users(),
2403 [&](const User *U) { return U == IV || U == Cmp; }))
2404 InstsToIgnore.insert(IVInst);
2405 }
2406}
2407
2409 // Create a new IR basic block for the scalar preheader.
2410 BasicBlock *ScalarPH = createScalarPreheader("");
2411 return ScalarPH->getSinglePredecessor();
2412}
2413
2414namespace {
2415
2416struct CSEDenseMapInfo {
2417 static bool canHandle(const Instruction *I) {
2420 }
2421
2422 static inline Instruction *getEmptyKey() {
2424 }
2425
2426 static inline Instruction *getTombstoneKey() {
2427 return DenseMapInfo<Instruction *>::getTombstoneKey();
2428 }
2429
2430 static unsigned getHashValue(const Instruction *I) {
2431 assert(canHandle(I) && "Unknown instruction!");
2432 return hash_combine(I->getOpcode(),
2433 hash_combine_range(I->operand_values()));
2434 }
2435
2436 static bool isEqual(const Instruction *LHS, const Instruction *RHS) {
2437 if (LHS == getEmptyKey() || RHS == getEmptyKey() ||
2438 LHS == getTombstoneKey() || RHS == getTombstoneKey())
2439 return LHS == RHS;
2440 return LHS->isIdenticalTo(RHS);
2441 }
2442};
2443
2444} // end anonymous namespace
2445
2446/// FIXME: This legacy common-subexpression-elimination routine is scheduled for
2447/// removal, in favor of the VPlan-based one.
2448static void legacyCSE(BasicBlock *BB) {
2449 // Perform simple cse.
2451 for (Instruction &In : llvm::make_early_inc_range(*BB)) {
2452 if (!CSEDenseMapInfo::canHandle(&In))
2453 continue;
2454
2455 // Check if we can replace this instruction with any of the
2456 // visited instructions.
2457 if (Instruction *V = CSEMap.lookup(&In)) {
2458 In.replaceAllUsesWith(V);
2459 In.eraseFromParent();
2460 continue;
2461 }
2462
2463 CSEMap[&In] = &In;
2464 }
2465}
2466
2467/// This function attempts to return a value that represents the ElementCount
2468/// at runtime. For fixed-width VFs we know this precisely at compile
2469/// time, but for scalable VFs we calculate it based on an estimate of the
2470/// vscale value.
2472 std::optional<unsigned> VScale) {
2473 unsigned EstimatedVF = VF.getKnownMinValue();
2474 if (VF.isScalable())
2475 if (VScale)
2476 EstimatedVF *= *VScale;
2477 assert(EstimatedVF >= 1 && "Estimated VF shouldn't be less than 1");
2478 return EstimatedVF;
2479}
2480
2483 ElementCount VF) const {
2484 // We only need to calculate a cost if the VF is scalar; for actual vectors
2485 // we should already have a pre-calculated cost at each VF.
2486 if (!VF.isScalar())
2487 return getCallWideningDecision(CI, VF).Cost;
2488
2489 Type *RetTy = CI->getType();
2491 if (auto RedCost = getReductionPatternCost(CI, VF, RetTy))
2492 return *RedCost;
2493
2495 for (auto &ArgOp : CI->args())
2496 Tys.push_back(ArgOp->getType());
2497
2498 InstructionCost ScalarCallCost =
2499 TTI.getCallInstrCost(CI->getCalledFunction(), RetTy, Tys, CostKind);
2500
2501 // If this is an intrinsic we may have a lower cost for it.
2504 return std::min(ScalarCallCost, IntrinsicCost);
2505 }
2506 return ScalarCallCost;
2507}
2508
2510 if (VF.isScalar() || !canVectorizeTy(Ty))
2511 return Ty;
2512 return toVectorizedTy(Ty, VF);
2513}
2514
2517 ElementCount VF) const {
2519 assert(ID && "Expected intrinsic call!");
2520 Type *RetTy = maybeVectorizeType(CI->getType(), VF);
2521 FastMathFlags FMF;
2522 if (auto *FPMO = dyn_cast<FPMathOperator>(CI))
2523 FMF = FPMO->getFastMathFlags();
2524
2527 SmallVector<Type *> ParamTys;
2528 std::transform(FTy->param_begin(), FTy->param_end(),
2529 std::back_inserter(ParamTys),
2530 [&](Type *Ty) { return maybeVectorizeType(Ty, VF); });
2531
2532 IntrinsicCostAttributes CostAttrs(ID, RetTy, Arguments, ParamTys, FMF,
2535 return TTI.getIntrinsicInstrCost(CostAttrs, CostKind);
2536}
2537
2539 // Fix widened non-induction PHIs by setting up the PHI operands.
2540 fixNonInductionPHIs(State);
2541
2542 // Don't apply optimizations below when no (vector) loop remains, as they all
2543 // require one at the moment.
2544 VPBasicBlock *HeaderVPBB =
2545 vputils::getFirstLoopHeader(*State.Plan, State.VPDT);
2546 if (!HeaderVPBB)
2547 return;
2548
2549 BasicBlock *HeaderBB = State.CFG.VPBB2IRBB[HeaderVPBB];
2550
2551 // Remove redundant induction instructions.
2552 legacyCSE(HeaderBB);
2553}
2554
2556 auto Iter = vp_depth_first_shallow(Plan.getEntry());
2558 for (VPRecipeBase &P : VPBB->phis()) {
2560 if (!VPPhi)
2561 continue;
2562 PHINode *NewPhi = cast<PHINode>(State.get(VPPhi));
2563 // Make sure the builder has a valid insert point.
2564 Builder.SetInsertPoint(NewPhi);
2565 for (const auto &[Inc, VPBB] : VPPhi->incoming_values_and_blocks())
2566 NewPhi->addIncoming(State.get(Inc), State.CFG.VPBB2IRBB[VPBB]);
2567 }
2568 }
2569}
2570
2571void LoopVectorizationCostModel::collectLoopScalars(ElementCount VF) {
2572 // We should not collect Scalars more than once per VF. Right now, this
2573 // function is called from collectUniformsAndScalars(), which already does
2574 // this check. Collecting Scalars for VF=1 does not make any sense.
2575 assert(VF.isVector() && !Scalars.contains(VF) &&
2576 "This function should not be visited twice for the same VF");
2577
2578 // This avoids any chances of creating a REPLICATE recipe during planning
2579 // since that would result in generation of scalarized code during execution,
2580 // which is not supported for scalable vectors.
2581 if (VF.isScalable()) {
2582 Scalars[VF].insert_range(Uniforms[VF]);
2583 return;
2584 }
2585
2587
2588 // These sets are used to seed the analysis with pointers used by memory
2589 // accesses that will remain scalar.
2591 SmallPtrSet<Instruction *, 8> PossibleNonScalarPtrs;
2592 auto *Latch = TheLoop->getLoopLatch();
2593
2594 // A helper that returns true if the use of Ptr by MemAccess will be scalar.
2595 // The pointer operands of loads and stores will be scalar as long as the
2596 // memory access is not a gather or scatter operation. The value operand of a
2597 // store will remain scalar if the store is scalarized.
2598 auto IsScalarUse = [&](Instruction *MemAccess, Value *Ptr) {
2599 InstWidening WideningDecision = getWideningDecision(MemAccess, VF);
2600 assert(WideningDecision != CM_Unknown &&
2601 "Widening decision should be ready at this moment");
2602 if (auto *Store = dyn_cast<StoreInst>(MemAccess))
2603 if (Ptr == Store->getValueOperand())
2604 return WideningDecision == CM_Scalarize;
2605 assert(Ptr == getLoadStorePointerOperand(MemAccess) &&
2606 "Ptr is neither a value or pointer operand");
2607 return WideningDecision != CM_GatherScatter;
2608 };
2609
2610 // A helper that returns true if the given value is a getelementptr
2611 // instruction contained in the loop.
2612 auto IsLoopVaryingGEP = [&](Value *V) {
2613 return isa<GetElementPtrInst>(V) && !TheLoop->isLoopInvariant(V);
2614 };
2615
2616 // A helper that evaluates a memory access's use of a pointer. If the use will
2617 // be a scalar use and the pointer is only used by memory accesses, we place
2618 // the pointer in ScalarPtrs. Otherwise, the pointer is placed in
2619 // PossibleNonScalarPtrs.
2620 auto EvaluatePtrUse = [&](Instruction *MemAccess, Value *Ptr) {
2621 // We only care about bitcast and getelementptr instructions contained in
2622 // the loop.
2623 if (!IsLoopVaryingGEP(Ptr))
2624 return;
2625
2626 // If the pointer has already been identified as scalar (e.g., if it was
2627 // also identified as uniform), there's nothing to do.
2628 auto *I = cast<Instruction>(Ptr);
2629 if (Worklist.count(I))
2630 return;
2631
2632 // If the use of the pointer will be a scalar use, and all users of the
2633 // pointer are memory accesses, place the pointer in ScalarPtrs. Otherwise,
2634 // place the pointer in PossibleNonScalarPtrs.
2635 if (IsScalarUse(MemAccess, Ptr) &&
2637 ScalarPtrs.insert(I);
2638 else
2639 PossibleNonScalarPtrs.insert(I);
2640 };
2641
2642 // We seed the scalars analysis with three classes of instructions: (1)
2643 // instructions marked uniform-after-vectorization and (2) bitcast,
2644 // getelementptr and (pointer) phi instructions used by memory accesses
2645 // requiring a scalar use.
2646 //
2647 // (1) Add to the worklist all instructions that have been identified as
2648 // uniform-after-vectorization.
2649 Worklist.insert_range(Uniforms[VF]);
2650
2651 // (2) Add to the worklist all bitcast and getelementptr instructions used by
2652 // memory accesses requiring a scalar use. The pointer operands of loads and
2653 // stores will be scalar unless the operation is a gather or scatter.
2654 // The value operand of a store will remain scalar if the store is scalarized.
2655 for (auto *BB : TheLoop->blocks())
2656 for (auto &I : *BB) {
2657 if (auto *Load = dyn_cast<LoadInst>(&I)) {
2658 EvaluatePtrUse(Load, Load->getPointerOperand());
2659 } else if (auto *Store = dyn_cast<StoreInst>(&I)) {
2660 EvaluatePtrUse(Store, Store->getPointerOperand());
2661 EvaluatePtrUse(Store, Store->getValueOperand());
2662 }
2663 }
2664 for (auto *I : ScalarPtrs)
2665 if (!PossibleNonScalarPtrs.count(I)) {
2666 LLVM_DEBUG(dbgs() << "LV: Found scalar instruction: " << *I << "\n");
2667 Worklist.insert(I);
2668 }
2669
2670 // Insert the forced scalars.
2671 // FIXME: Currently VPWidenPHIRecipe() often creates a dead vector
2672 // induction variable when the PHI user is scalarized.
2673 auto ForcedScalar = ForcedScalars.find(VF);
2674 if (ForcedScalar != ForcedScalars.end())
2675 for (auto *I : ForcedScalar->second) {
2676 LLVM_DEBUG(dbgs() << "LV: Found (forced) scalar instruction: " << *I << "\n");
2677 Worklist.insert(I);
2678 }
2679
2680 // Expand the worklist by looking through any bitcasts and getelementptr
2681 // instructions we've already identified as scalar. This is similar to the
2682 // expansion step in collectLoopUniforms(); however, here we're only
2683 // expanding to include additional bitcasts and getelementptr instructions.
2684 unsigned Idx = 0;
2685 while (Idx != Worklist.size()) {
2686 Instruction *Dst = Worklist[Idx++];
2687 if (!IsLoopVaryingGEP(Dst->getOperand(0)))
2688 continue;
2689 auto *Src = cast<Instruction>(Dst->getOperand(0));
2690 if (llvm::all_of(Src->users(), [&](User *U) -> bool {
2691 auto *J = cast<Instruction>(U);
2692 return !TheLoop->contains(J) || Worklist.count(J) ||
2693 ((isa<LoadInst>(J) || isa<StoreInst>(J)) &&
2694 IsScalarUse(J, Src));
2695 })) {
2696 Worklist.insert(Src);
2697 LLVM_DEBUG(dbgs() << "LV: Found scalar instruction: " << *Src << "\n");
2698 }
2699 }
2700
2701 // An induction variable will remain scalar if all users of the induction
2702 // variable and induction variable update remain scalar.
2703 for (const auto &Induction : Legal->getInductionVars()) {
2704 auto *Ind = Induction.first;
2705 auto *IndUpdate = cast<Instruction>(Ind->getIncomingValueForBlock(Latch));
2706
2707 // If tail-folding is applied, the primary induction variable will be used
2708 // to feed a vector compare.
2709 if (Ind == Legal->getPrimaryInduction() && foldTailByMasking())
2710 continue;
2711
2712 // Returns true if \p Indvar is a pointer induction that is used directly by
2713 // load/store instruction \p I.
2714 auto IsDirectLoadStoreFromPtrIndvar = [&](Instruction *Indvar,
2715 Instruction *I) {
2716 return Induction.second.getKind() ==
2719 Indvar == getLoadStorePointerOperand(I) && IsScalarUse(I, Indvar);
2720 };
2721
2722 // Determine if all users of the induction variable are scalar after
2723 // vectorization.
2724 bool ScalarInd = all_of(Ind->users(), [&](User *U) -> bool {
2725 auto *I = cast<Instruction>(U);
2726 return I == IndUpdate || !TheLoop->contains(I) || Worklist.count(I) ||
2727 IsDirectLoadStoreFromPtrIndvar(Ind, I);
2728 });
2729 if (!ScalarInd)
2730 continue;
2731
2732 // If the induction variable update is a fixed-order recurrence, neither the
2733 // induction variable or its update should be marked scalar after
2734 // vectorization.
2735 auto *IndUpdatePhi = dyn_cast<PHINode>(IndUpdate);
2736 if (IndUpdatePhi && Legal->isFixedOrderRecurrence(IndUpdatePhi))
2737 continue;
2738
2739 // Determine if all users of the induction variable update instruction are
2740 // scalar after vectorization.
2741 bool ScalarIndUpdate = all_of(IndUpdate->users(), [&](User *U) -> bool {
2742 auto *I = cast<Instruction>(U);
2743 return I == Ind || !TheLoop->contains(I) || Worklist.count(I) ||
2744 IsDirectLoadStoreFromPtrIndvar(IndUpdate, I);
2745 });
2746 if (!ScalarIndUpdate)
2747 continue;
2748
2749 // The induction variable and its update instruction will remain scalar.
2750 Worklist.insert(Ind);
2751 Worklist.insert(IndUpdate);
2752 LLVM_DEBUG(dbgs() << "LV: Found scalar instruction: " << *Ind << "\n");
2753 LLVM_DEBUG(dbgs() << "LV: Found scalar instruction: " << *IndUpdate
2754 << "\n");
2755 }
2756
2757 Scalars[VF].insert_range(Worklist);
2758}
2759
2761 Instruction *I, ElementCount VF) const {
2762 if (!isPredicatedInst(I))
2763 return false;
2764
2765 // Do we have a non-scalar lowering for this predicated
2766 // instruction? No - it is scalar with predication.
2767 switch(I->getOpcode()) {
2768 default:
2769 return true;
2770 case Instruction::Call:
2771 if (VF.isScalar())
2772 return true;
2774 case Instruction::Load:
2775 case Instruction::Store: {
2777 auto *Ty = getLoadStoreType(I);
2778 unsigned AS = getLoadStoreAddressSpace(I);
2779 Type *VTy = Ty;
2780 if (VF.isVector())
2781 VTy = VectorType::get(Ty, VF);
2782 const Align Alignment = getLoadStoreAlignment(I);
2783 return isa<LoadInst>(I) ? !(isLegalMaskedLoad(Ty, Ptr, Alignment, AS) ||
2784 TTI.isLegalMaskedGather(VTy, Alignment))
2785 : !(isLegalMaskedStore(Ty, Ptr, Alignment, AS) ||
2786 TTI.isLegalMaskedScatter(VTy, Alignment));
2787 }
2788 case Instruction::UDiv:
2789 case Instruction::SDiv:
2790 case Instruction::SRem:
2791 case Instruction::URem: {
2792 // We have the option to use the safe-divisor idiom to avoid predication.
2793 // The cost based decision here will always select safe-divisor for
2794 // scalable vectors as scalarization isn't legal.
2795 const auto [ScalarCost, SafeDivisorCost] = getDivRemSpeculationCost(I, VF);
2796 return isDivRemScalarWithPredication(ScalarCost, SafeDivisorCost);
2797 }
2798 }
2799}
2800
2801// TODO: Fold into LoopVectorizationLegality::isMaskRequired.
2803 // TODO: We can use the loop-preheader as context point here and get
2804 // context sensitive reasoning for isSafeToSpeculativelyExecute.
2806 (isa<LoadInst, StoreInst, CallInst>(I) && !Legal->isMaskRequired(I)) ||
2808 return false;
2809
2810 // If the instruction was executed conditionally in the original scalar loop,
2811 // predication is needed with a mask whose lanes are all possibly inactive.
2812 if (Legal->blockNeedsPredication(I->getParent()))
2813 return true;
2814
2815 // If we're not folding the tail by masking, predication is unnecessary.
2816 if (!foldTailByMasking())
2817 return false;
2818
2819 // All that remain are instructions with side-effects originally executed in
2820 // the loop unconditionally, but now execute under a tail-fold mask (only)
2821 // having at least one active lane (the first). If the side-effects of the
2822 // instruction are invariant, executing it w/o (the tail-folding) mask is safe
2823 // - it will cause the same side-effects as when masked.
2824 switch(I->getOpcode()) {
2825 default:
2827 "instruction should have been considered by earlier checks");
2828 case Instruction::Call:
2829 // Side-effects of a Call are assumed to be non-invariant, needing a
2830 // (fold-tail) mask.
2831 assert(Legal->isMaskRequired(I) &&
2832 "should have returned earlier for calls not needing a mask");
2833 return true;
2834 case Instruction::Load:
2835 // If the address is loop invariant no predication is needed.
2836 return !Legal->isInvariant(getLoadStorePointerOperand(I));
2837 case Instruction::Store: {
2838 // For stores, we need to prove both speculation safety (which follows from
2839 // the same argument as loads), but also must prove the value being stored
2840 // is correct. The easiest form of the later is to require that all values
2841 // stored are the same.
2842 return !(Legal->isInvariant(getLoadStorePointerOperand(I)) &&
2843 TheLoop->isLoopInvariant(cast<StoreInst>(I)->getValueOperand()));
2844 }
2845 case Instruction::UDiv:
2846 case Instruction::SDiv:
2847 case Instruction::SRem:
2848 case Instruction::URem:
2849 // If the divisor is loop-invariant no predication is needed.
2850 return !Legal->isInvariant(I->getOperand(1));
2851 }
2852}
2853
2854std::pair<InstructionCost, InstructionCost>
2856 ElementCount VF) const {
2857 assert(I->getOpcode() == Instruction::UDiv ||
2858 I->getOpcode() == Instruction::SDiv ||
2859 I->getOpcode() == Instruction::SRem ||
2860 I->getOpcode() == Instruction::URem);
2862
2863 // Scalarization isn't legal for scalable vector types
2864 InstructionCost ScalarizationCost = InstructionCost::getInvalid();
2865 if (!VF.isScalable()) {
2866 // Get the scalarization cost and scale this amount by the probability of
2867 // executing the predicated block. If the instruction is not predicated,
2868 // we fall through to the next case.
2869 ScalarizationCost = 0;
2870
2871 // These instructions have a non-void type, so account for the phi nodes
2872 // that we will create. This cost is likely to be zero. The phi node
2873 // cost, if any, should be scaled by the block probability because it
2874 // models a copy at the end of each predicated block.
2875 ScalarizationCost +=
2876 VF.getFixedValue() * TTI.getCFInstrCost(Instruction::PHI, CostKind);
2877
2878 // The cost of the non-predicated instruction.
2879 ScalarizationCost +=
2880 VF.getFixedValue() *
2881 TTI.getArithmeticInstrCost(I->getOpcode(), I->getType(), CostKind);
2882
2883 // The cost of insertelement and extractelement instructions needed for
2884 // scalarization.
2885 ScalarizationCost += getScalarizationOverhead(I, VF);
2886
2887 // Scale the cost by the probability of executing the predicated blocks.
2888 // This assumes the predicated block for each vector lane is equally
2889 // likely.
2890 ScalarizationCost = ScalarizationCost / getPredBlockCostDivisor(CostKind);
2891 }
2892
2893 InstructionCost SafeDivisorCost = 0;
2894 auto *VecTy = toVectorTy(I->getType(), VF);
2895 // The cost of the select guard to ensure all lanes are well defined
2896 // after we speculate above any internal control flow.
2897 SafeDivisorCost +=
2898 TTI.getCmpSelInstrCost(Instruction::Select, VecTy,
2899 toVectorTy(Type::getInt1Ty(I->getContext()), VF),
2901
2902 SmallVector<const Value *, 4> Operands(I->operand_values());
2903 SafeDivisorCost += TTI.getArithmeticInstrCost(
2904 I->getOpcode(), VecTy, CostKind,
2905 {TargetTransformInfo::OK_AnyValue, TargetTransformInfo::OP_None},
2906 {TargetTransformInfo::OK_AnyValue, TargetTransformInfo::OP_None},
2907 Operands, I);
2908 return {ScalarizationCost, SafeDivisorCost};
2909}
2910
2912 Instruction *I, ElementCount VF) const {
2913 assert(isAccessInterleaved(I) && "Expecting interleaved access.");
2915 "Decision should not be set yet.");
2916 auto *Group = getInterleavedAccessGroup(I);
2917 assert(Group && "Must have a group.");
2918 unsigned InterleaveFactor = Group->getFactor();
2919
2920 // If the instruction's allocated size doesn't equal its type size, it
2921 // requires padding and will be scalarized.
2922 auto &DL = I->getDataLayout();
2923 auto *ScalarTy = getLoadStoreType(I);
2924 if (hasIrregularType(ScalarTy, DL))
2925 return false;
2926
2927 // For scalable vectors, the interleave factors must be <= 8 since we require
2928 // the (de)interleaveN intrinsics instead of shufflevectors.
2929 if (VF.isScalable() && InterleaveFactor > 8)
2930 return false;
2931
2932 // If the group involves a non-integral pointer, we may not be able to
2933 // losslessly cast all values to a common type.
2934 bool ScalarNI = DL.isNonIntegralPointerType(ScalarTy);
2935 for (unsigned Idx = 0; Idx < InterleaveFactor; Idx++) {
2936 Instruction *Member = Group->getMember(Idx);
2937 if (!Member)
2938 continue;
2939 auto *MemberTy = getLoadStoreType(Member);
2940 bool MemberNI = DL.isNonIntegralPointerType(MemberTy);
2941 // Don't coerce non-integral pointers to integers or vice versa.
2942 if (MemberNI != ScalarNI)
2943 // TODO: Consider adding special nullptr value case here
2944 return false;
2945 if (MemberNI && ScalarNI &&
2946 ScalarTy->getPointerAddressSpace() !=
2947 MemberTy->getPointerAddressSpace())
2948 return false;
2949 }
2950
2951 // Check if masking is required.
2952 // A Group may need masking for one of two reasons: it resides in a block that
2953 // needs predication, or it was decided to use masking to deal with gaps
2954 // (either a gap at the end of a load-access that may result in a speculative
2955 // load, or any gaps in a store-access).
2956 bool PredicatedAccessRequiresMasking =
2957 blockNeedsPredicationForAnyReason(I->getParent()) &&
2958 Legal->isMaskRequired(I);
2959 bool LoadAccessWithGapsRequiresEpilogMasking =
2960 isa<LoadInst>(I) && Group->requiresScalarEpilogue() &&
2962 bool StoreAccessWithGapsRequiresMasking =
2963 isa<StoreInst>(I) && !Group->isFull();
2964 if (!PredicatedAccessRequiresMasking &&
2965 !LoadAccessWithGapsRequiresEpilogMasking &&
2966 !StoreAccessWithGapsRequiresMasking)
2967 return true;
2968
2969 // If masked interleaving is required, we expect that the user/target had
2970 // enabled it, because otherwise it either wouldn't have been created or
2971 // it should have been invalidated by the CostModel.
2973 "Masked interleave-groups for predicated accesses are not enabled.");
2974
2975 if (Group->isReverse())
2976 return false;
2977
2978 // TODO: Support interleaved access that requires a gap mask for scalable VFs.
2979 bool NeedsMaskForGaps = LoadAccessWithGapsRequiresEpilogMasking ||
2980 StoreAccessWithGapsRequiresMasking;
2981 if (VF.isScalable() && NeedsMaskForGaps)
2982 return false;
2983
2984 auto *Ty = getLoadStoreType(I);
2985 const Align Alignment = getLoadStoreAlignment(I);
2986 unsigned AS = getLoadStoreAddressSpace(I);
2987 return isa<LoadInst>(I) ? TTI.isLegalMaskedLoad(Ty, Alignment, AS)
2988 : TTI.isLegalMaskedStore(Ty, Alignment, AS);
2989}
2990
2992 Instruction *I, ElementCount VF) {
2993 // Get and ensure we have a valid memory instruction.
2994 assert((isa<LoadInst, StoreInst>(I)) && "Invalid memory instruction");
2995
2997 auto *ScalarTy = getLoadStoreType(I);
2998
2999 // In order to be widened, the pointer should be consecutive, first of all.
3000 if (!Legal->isConsecutivePtr(ScalarTy, Ptr))
3001 return false;
3002
3003 // If the instruction is a store located in a predicated block, it will be
3004 // scalarized.
3005 if (isScalarWithPredication(I, VF))
3006 return false;
3007
3008 // If the instruction's allocated size doesn't equal it's type size, it
3009 // requires padding and will be scalarized.
3010 auto &DL = I->getDataLayout();
3011 if (hasIrregularType(ScalarTy, DL))
3012 return false;
3013
3014 return true;
3015}
3016
3017void LoopVectorizationCostModel::collectLoopUniforms(ElementCount VF) {
3018 // We should not collect Uniforms more than once per VF. Right now,
3019 // this function is called from collectUniformsAndScalars(), which
3020 // already does this check. Collecting Uniforms for VF=1 does not make any
3021 // sense.
3022
3023 assert(VF.isVector() && !Uniforms.contains(VF) &&
3024 "This function should not be visited twice for the same VF");
3025
3026 // Visit the list of Uniforms. If we find no uniform value, we won't
3027 // analyze again. Uniforms.count(VF) will return 1.
3028 Uniforms[VF].clear();
3029
3030 // Now we know that the loop is vectorizable!
3031 // Collect instructions inside the loop that will remain uniform after
3032 // vectorization.
3033
3034 // Global values, params and instructions outside of current loop are out of
3035 // scope.
3036 auto IsOutOfScope = [&](Value *V) -> bool {
3038 return (!I || !TheLoop->contains(I));
3039 };
3040
3041 // Worklist containing uniform instructions demanding lane 0.
3042 SetVector<Instruction *> Worklist;
3043
3044 // Add uniform instructions demanding lane 0 to the worklist. Instructions
3045 // that require predication must not be considered uniform after
3046 // vectorization, because that would create an erroneous replicating region
3047 // where only a single instance out of VF should be formed.
3048 auto AddToWorklistIfAllowed = [&](Instruction *I) -> void {
3049 if (IsOutOfScope(I)) {
3050 LLVM_DEBUG(dbgs() << "LV: Found not uniform due to scope: "
3051 << *I << "\n");
3052 return;
3053 }
3054 if (isPredicatedInst(I)) {
3055 LLVM_DEBUG(
3056 dbgs() << "LV: Found not uniform due to requiring predication: " << *I
3057 << "\n");
3058 return;
3059 }
3060 LLVM_DEBUG(dbgs() << "LV: Found uniform instruction: " << *I << "\n");
3061 Worklist.insert(I);
3062 };
3063
3064 // Start with the conditional branches exiting the loop. If the branch
3065 // condition is an instruction contained in the loop that is only used by the
3066 // branch, it is uniform. Note conditions from uncountable early exits are not
3067 // uniform.
3069 TheLoop->getExitingBlocks(Exiting);
3070 for (BasicBlock *E : Exiting) {
3071 if (Legal->hasUncountableEarlyExit() && TheLoop->getLoopLatch() != E)
3072 continue;
3073 auto *Cmp = dyn_cast<Instruction>(E->getTerminator()->getOperand(0));
3074 if (Cmp && TheLoop->contains(Cmp) && Cmp->hasOneUse())
3075 AddToWorklistIfAllowed(Cmp);
3076 }
3077
3078 auto PrevVF = VF.divideCoefficientBy(2);
3079 // Return true if all lanes perform the same memory operation, and we can
3080 // thus choose to execute only one.
3081 auto IsUniformMemOpUse = [&](Instruction *I) {
3082 // If the value was already known to not be uniform for the previous
3083 // (smaller VF), it cannot be uniform for the larger VF.
3084 if (PrevVF.isVector()) {
3085 auto Iter = Uniforms.find(PrevVF);
3086 if (Iter != Uniforms.end() && !Iter->second.contains(I))
3087 return false;
3088 }
3089 if (!Legal->isUniformMemOp(*I, VF))
3090 return false;
3091 if (isa<LoadInst>(I))
3092 // Loading the same address always produces the same result - at least
3093 // assuming aliasing and ordering which have already been checked.
3094 return true;
3095 // Storing the same value on every iteration.
3096 return TheLoop->isLoopInvariant(cast<StoreInst>(I)->getValueOperand());
3097 };
3098
3099 auto IsUniformDecision = [&](Instruction *I, ElementCount VF) {
3100 InstWidening WideningDecision = getWideningDecision(I, VF);
3101 assert(WideningDecision != CM_Unknown &&
3102 "Widening decision should be ready at this moment");
3103
3104 if (IsUniformMemOpUse(I))
3105 return true;
3106
3107 return (WideningDecision == CM_Widen ||
3108 WideningDecision == CM_Widen_Reverse ||
3109 WideningDecision == CM_Interleave);
3110 };
3111
3112 // Returns true if Ptr is the pointer operand of a memory access instruction
3113 // I, I is known to not require scalarization, and the pointer is not also
3114 // stored.
3115 auto IsVectorizedMemAccessUse = [&](Instruction *I, Value *Ptr) -> bool {
3116 if (isa<StoreInst>(I) && I->getOperand(0) == Ptr)
3117 return false;
3118 return getLoadStorePointerOperand(I) == Ptr &&
3119 (IsUniformDecision(I, VF) || Legal->isInvariant(Ptr));
3120 };
3121
3122 // Holds a list of values which are known to have at least one uniform use.
3123 // Note that there may be other uses which aren't uniform. A "uniform use"
3124 // here is something which only demands lane 0 of the unrolled iterations;
3125 // it does not imply that all lanes produce the same value (e.g. this is not
3126 // the usual meaning of uniform)
3127 SetVector<Value *> HasUniformUse;
3128
3129 // Scan the loop for instructions which are either a) known to have only
3130 // lane 0 demanded or b) are uses which demand only lane 0 of their operand.
3131 for (auto *BB : TheLoop->blocks())
3132 for (auto &I : *BB) {
3133 if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(&I)) {
3134 switch (II->getIntrinsicID()) {
3135 case Intrinsic::sideeffect:
3136 case Intrinsic::experimental_noalias_scope_decl:
3137 case Intrinsic::assume:
3138 case Intrinsic::lifetime_start:
3139 case Intrinsic::lifetime_end:
3140 if (TheLoop->hasLoopInvariantOperands(&I))
3141 AddToWorklistIfAllowed(&I);
3142 break;
3143 default:
3144 break;
3145 }
3146 }
3147
3148 if (auto *EVI = dyn_cast<ExtractValueInst>(&I)) {
3149 if (IsOutOfScope(EVI->getAggregateOperand())) {
3150 AddToWorklistIfAllowed(EVI);
3151 continue;
3152 }
3153 // Only ExtractValue instructions where the aggregate value comes from a
3154 // call are allowed to be non-uniform.
3155 assert(isa<CallInst>(EVI->getAggregateOperand()) &&
3156 "Expected aggregate value to be call return value");
3157 }
3158
3159 // If there's no pointer operand, there's nothing to do.
3161 if (!Ptr)
3162 continue;
3163
3164 // If the pointer can be proven to be uniform, always add it to the
3165 // worklist.
3166 if (isa<Instruction>(Ptr) && Legal->isUniform(Ptr, VF))
3167 AddToWorklistIfAllowed(cast<Instruction>(Ptr));
3168
3169 if (IsUniformMemOpUse(&I))
3170 AddToWorklistIfAllowed(&I);
3171
3172 if (IsVectorizedMemAccessUse(&I, Ptr))
3173 HasUniformUse.insert(Ptr);
3174 }
3175
3176 // Add to the worklist any operands which have *only* uniform (e.g. lane 0
3177 // demanding) users. Since loops are assumed to be in LCSSA form, this
3178 // disallows uses outside the loop as well.
3179 for (auto *V : HasUniformUse) {
3180 if (IsOutOfScope(V))
3181 continue;
3182 auto *I = cast<Instruction>(V);
3183 bool UsersAreMemAccesses = all_of(I->users(), [&](User *U) -> bool {
3184 auto *UI = cast<Instruction>(U);
3185 return TheLoop->contains(UI) && IsVectorizedMemAccessUse(UI, V);
3186 });
3187 if (UsersAreMemAccesses)
3188 AddToWorklistIfAllowed(I);
3189 }
3190
3191 // Expand Worklist in topological order: whenever a new instruction
3192 // is added , its users should be already inside Worklist. It ensures
3193 // a uniform instruction will only be used by uniform instructions.
3194 unsigned Idx = 0;
3195 while (Idx != Worklist.size()) {
3196 Instruction *I = Worklist[Idx++];
3197
3198 for (auto *OV : I->operand_values()) {
3199 // isOutOfScope operands cannot be uniform instructions.
3200 if (IsOutOfScope(OV))
3201 continue;
3202 // First order recurrence Phi's should typically be considered
3203 // non-uniform.
3204 auto *OP = dyn_cast<PHINode>(OV);
3205 if (OP && Legal->isFixedOrderRecurrence(OP))
3206 continue;
3207 // If all the users of the operand are uniform, then add the
3208 // operand into the uniform worklist.
3209 auto *OI = cast<Instruction>(OV);
3210 if (llvm::all_of(OI->users(), [&](User *U) -> bool {
3211 auto *J = cast<Instruction>(U);
3212 return Worklist.count(J) || IsVectorizedMemAccessUse(J, OI);
3213 }))
3214 AddToWorklistIfAllowed(OI);
3215 }
3216 }
3217
3218 // For an instruction to be added into Worklist above, all its users inside
3219 // the loop should also be in Worklist. However, this condition cannot be
3220 // true for phi nodes that form a cyclic dependence. We must process phi
3221 // nodes separately. An induction variable will remain uniform if all users
3222 // of the induction variable and induction variable update remain uniform.
3223 // The code below handles both pointer and non-pointer induction variables.
3224 BasicBlock *Latch = TheLoop->getLoopLatch();
3225 for (const auto &Induction : Legal->getInductionVars()) {
3226 auto *Ind = Induction.first;
3227 auto *IndUpdate = cast<Instruction>(Ind->getIncomingValueForBlock(Latch));
3228
3229 // Determine if all users of the induction variable are uniform after
3230 // vectorization.
3231 bool UniformInd = all_of(Ind->users(), [&](User *U) -> bool {
3232 auto *I = cast<Instruction>(U);
3233 return I == IndUpdate || !TheLoop->contains(I) || Worklist.count(I) ||
3234 IsVectorizedMemAccessUse(I, Ind);
3235 });
3236 if (!UniformInd)
3237 continue;
3238
3239 // Determine if all users of the induction variable update instruction are
3240 // uniform after vectorization.
3241 bool UniformIndUpdate = all_of(IndUpdate->users(), [&](User *U) -> bool {
3242 auto *I = cast<Instruction>(U);
3243 return I == Ind || Worklist.count(I) ||
3244 IsVectorizedMemAccessUse(I, IndUpdate);
3245 });
3246 if (!UniformIndUpdate)
3247 continue;
3248
3249 // The induction variable and its update instruction will remain uniform.
3250 AddToWorklistIfAllowed(Ind);
3251 AddToWorklistIfAllowed(IndUpdate);
3252 }
3253
3254 Uniforms[VF].insert_range(Worklist);
3255}
3256
3258 LLVM_DEBUG(dbgs() << "LV: Performing code size checks.\n");
3259
3260 if (Legal->getRuntimePointerChecking()->Need) {
3261 reportVectorizationFailure("Runtime ptr check is required with -Os/-Oz",
3262 "runtime pointer checks needed. Enable vectorization of this "
3263 "loop with '#pragma clang loop vectorize(enable)' when "
3264 "compiling with -Os/-Oz",
3265 "CantVersionLoopWithOptForSize", ORE, TheLoop);
3266 return true;
3267 }
3268
3269 if (!PSE.getPredicate().isAlwaysTrue()) {
3270 reportVectorizationFailure("Runtime SCEV check is required with -Os/-Oz",
3271 "runtime SCEV checks needed. Enable vectorization of this "
3272 "loop with '#pragma clang loop vectorize(enable)' when "
3273 "compiling with -Os/-Oz",
3274 "CantVersionLoopWithOptForSize", ORE, TheLoop);
3275 return true;
3276 }
3277
3278 // FIXME: Avoid specializing for stride==1 instead of bailing out.
3279 if (!Legal->getLAI()->getSymbolicStrides().empty()) {
3280 reportVectorizationFailure("Runtime stride check for small trip count",
3281 "runtime stride == 1 checks needed. Enable vectorization of "
3282 "this loop without such check by compiling with -Os/-Oz",
3283 "CantVersionLoopWithOptForSize", ORE, TheLoop);
3284 return true;
3285 }
3286
3287 return false;
3288}
3289
3290bool LoopVectorizationCostModel::isScalableVectorizationAllowed() {
3291 if (IsScalableVectorizationAllowed)
3292 return *IsScalableVectorizationAllowed;
3293
3294 IsScalableVectorizationAllowed = false;
3295 if (!TTI.supportsScalableVectors() && !ForceTargetSupportsScalableVectors)
3296 return false;
3297
3298 if (Hints->isScalableVectorizationDisabled()) {
3299 reportVectorizationInfo("Scalable vectorization is explicitly disabled",
3300 "ScalableVectorizationDisabled", ORE, TheLoop);
3301 return false;
3302 }
3303
3304 LLVM_DEBUG(dbgs() << "LV: Scalable vectorization is available\n");
3305
3306 auto MaxScalableVF = ElementCount::getScalable(
3307 std::numeric_limits<ElementCount::ScalarTy>::max());
3308
3309 // Test that the loop-vectorizer can legalize all operations for this MaxVF.
3310 // FIXME: While for scalable vectors this is currently sufficient, this should
3311 // be replaced by a more detailed mechanism that filters out specific VFs,
3312 // instead of invalidating vectorization for a whole set of VFs based on the
3313 // MaxVF.
3314
3315 // Disable scalable vectorization if the loop contains unsupported reductions.
3316 if (!canVectorizeReductions(MaxScalableVF)) {
3318 "Scalable vectorization not supported for the reduction "
3319 "operations found in this loop.",
3320 "ScalableVFUnfeasible", ORE, TheLoop);
3321 return false;
3322 }
3323
3324 // Disable scalable vectorization if the loop contains any instructions
3325 // with element types not supported for scalable vectors.
3326 if (any_of(ElementTypesInLoop, [&](Type *Ty) {
3327 return !Ty->isVoidTy() &&
3329 })) {
3330 reportVectorizationInfo("Scalable vectorization is not supported "
3331 "for all element types found in this loop.",
3332 "ScalableVFUnfeasible", ORE, TheLoop);
3333 return false;
3334 }
3335
3336 if (!Legal->isSafeForAnyVectorWidth() && !getMaxVScale(*TheFunction, TTI)) {
3337 reportVectorizationInfo("The target does not provide maximum vscale value "
3338 "for safe distance analysis.",
3339 "ScalableVFUnfeasible", ORE, TheLoop);
3340 return false;
3341 }
3342
3343 IsScalableVectorizationAllowed = true;
3344 return true;
3345}
3346
3347ElementCount
3348LoopVectorizationCostModel::getMaxLegalScalableVF(unsigned MaxSafeElements) {
3349 if (!isScalableVectorizationAllowed())
3350 return ElementCount::getScalable(0);
3351
3352 auto MaxScalableVF = ElementCount::getScalable(
3353 std::numeric_limits<ElementCount::ScalarTy>::max());
3354 if (Legal->isSafeForAnyVectorWidth())
3355 return MaxScalableVF;
3356
3357 std::optional<unsigned> MaxVScale = getMaxVScale(*TheFunction, TTI);
3358 // Limit MaxScalableVF by the maximum safe dependence distance.
3359 MaxScalableVF = ElementCount::getScalable(MaxSafeElements / *MaxVScale);
3360
3361 if (!MaxScalableVF)
3363 "Max legal vector width too small, scalable vectorization "
3364 "unfeasible.",
3365 "ScalableVFUnfeasible", ORE, TheLoop);
3366
3367 return MaxScalableVF;
3368}
3369
3370FixedScalableVFPair LoopVectorizationCostModel::computeFeasibleMaxVF(
3371 unsigned MaxTripCount, ElementCount UserVF, bool FoldTailByMasking) {
3372 MinBWs = computeMinimumValueSizes(TheLoop->getBlocks(), *DB, &TTI);
3373 unsigned SmallestType, WidestType;
3374 std::tie(SmallestType, WidestType) = getSmallestAndWidestTypes();
3375
3376 // Get the maximum safe dependence distance in bits computed by LAA.
3377 // It is computed by MaxVF * sizeOf(type) * 8, where type is taken from
3378 // the memory accesses that is most restrictive (involved in the smallest
3379 // dependence distance).
3380 unsigned MaxSafeElementsPowerOf2 =
3381 bit_floor(Legal->getMaxSafeVectorWidthInBits() / WidestType);
3382 if (!Legal->isSafeForAnyStoreLoadForwardDistances()) {
3383 unsigned SLDist = Legal->getMaxStoreLoadForwardSafeDistanceInBits();
3384 MaxSafeElementsPowerOf2 =
3385 std::min(MaxSafeElementsPowerOf2, SLDist / WidestType);
3386 }
3387 auto MaxSafeFixedVF = ElementCount::getFixed(MaxSafeElementsPowerOf2);
3388 auto MaxSafeScalableVF = getMaxLegalScalableVF(MaxSafeElementsPowerOf2);
3389
3390 if (!Legal->isSafeForAnyVectorWidth())
3391 this->MaxSafeElements = MaxSafeElementsPowerOf2;
3392
3393 LLVM_DEBUG(dbgs() << "LV: The max safe fixed VF is: " << MaxSafeFixedVF
3394 << ".\n");
3395 LLVM_DEBUG(dbgs() << "LV: The max safe scalable VF is: " << MaxSafeScalableVF
3396 << ".\n");
3397
3398 // First analyze the UserVF, fall back if the UserVF should be ignored.
3399 if (UserVF) {
3400 auto MaxSafeUserVF =
3401 UserVF.isScalable() ? MaxSafeScalableVF : MaxSafeFixedVF;
3402
3403 if (ElementCount::isKnownLE(UserVF, MaxSafeUserVF)) {
3404 // If `VF=vscale x N` is safe, then so is `VF=N`
3405 if (UserVF.isScalable())
3406 return FixedScalableVFPair(
3407 ElementCount::getFixed(UserVF.getKnownMinValue()), UserVF);
3408
3409 return UserVF;
3410 }
3411
3412 assert(ElementCount::isKnownGT(UserVF, MaxSafeUserVF));
3413
3414 // Only clamp if the UserVF is not scalable. If the UserVF is scalable, it
3415 // is better to ignore the hint and let the compiler choose a suitable VF.
3416 if (!UserVF.isScalable()) {
3417 LLVM_DEBUG(dbgs() << "LV: User VF=" << UserVF
3418 << " is unsafe, clamping to max safe VF="
3419 << MaxSafeFixedVF << ".\n");
3420 ORE->emit([&]() {
3421 return OptimizationRemarkAnalysis(DEBUG_TYPE, "VectorizationFactor",
3422 TheLoop->getStartLoc(),
3423 TheLoop->getHeader())
3424 << "User-specified vectorization factor "
3425 << ore::NV("UserVectorizationFactor", UserVF)
3426 << " is unsafe, clamping to maximum safe vectorization factor "
3427 << ore::NV("VectorizationFactor", MaxSafeFixedVF);
3428 });
3429 return MaxSafeFixedVF;
3430 }
3431
3433 LLVM_DEBUG(dbgs() << "LV: User VF=" << UserVF
3434 << " is ignored because scalable vectors are not "
3435 "available.\n");
3436 ORE->emit([&]() {
3437 return OptimizationRemarkAnalysis(DEBUG_TYPE, "VectorizationFactor",
3438 TheLoop->getStartLoc(),
3439 TheLoop->getHeader())
3440 << "User-specified vectorization factor "
3441 << ore::NV("UserVectorizationFactor", UserVF)
3442 << " is ignored because the target does not support scalable "
3443 "vectors. The compiler will pick a more suitable value.";
3444 });
3445 } else {
3446 LLVM_DEBUG(dbgs() << "LV: User VF=" << UserVF
3447 << " is unsafe. Ignoring scalable UserVF.\n");
3448 ORE->emit([&]() {
3449 return OptimizationRemarkAnalysis(DEBUG_TYPE, "VectorizationFactor",
3450 TheLoop->getStartLoc(),
3451 TheLoop->getHeader())
3452 << "User-specified vectorization factor "
3453 << ore::NV("UserVectorizationFactor", UserVF)
3454 << " is unsafe. Ignoring the hint to let the compiler pick a "
3455 "more suitable value.";
3456 });
3457 }
3458 }
3459
3460 LLVM_DEBUG(dbgs() << "LV: The Smallest and Widest types: " << SmallestType
3461 << " / " << WidestType << " bits.\n");
3462
3463 FixedScalableVFPair Result(ElementCount::getFixed(1),
3465 if (auto MaxVF =
3466 getMaximizedVFForTarget(MaxTripCount, SmallestType, WidestType,
3467 MaxSafeFixedVF, FoldTailByMasking))
3468 Result.FixedVF = MaxVF;
3469
3470 if (auto MaxVF =
3471 getMaximizedVFForTarget(MaxTripCount, SmallestType, WidestType,
3472 MaxSafeScalableVF, FoldTailByMasking))
3473 if (MaxVF.isScalable()) {
3474 Result.ScalableVF = MaxVF;
3475 LLVM_DEBUG(dbgs() << "LV: Found feasible scalable VF = " << MaxVF
3476 << "\n");
3477 }
3478
3479 return Result;
3480}
3481
3482FixedScalableVFPair
3484 if (Legal->getRuntimePointerChecking()->Need && TTI.hasBranchDivergence()) {
3485 // TODO: It may be useful to do since it's still likely to be dynamically
3486 // uniform if the target can skip.
3488 "Not inserting runtime ptr check for divergent target",
3489 "runtime pointer checks needed. Not enabled for divergent target",
3490 "CantVersionLoopWithDivergentTarget", ORE, TheLoop);
3492 }
3493
3494 ScalarEvolution *SE = PSE.getSE();
3496 unsigned MaxTC = PSE.getSmallConstantMaxTripCount();
3497 LLVM_DEBUG(dbgs() << "LV: Found trip count: " << TC << '\n');
3498 if (TC != ElementCount::getFixed(MaxTC))
3499 LLVM_DEBUG(dbgs() << "LV: Found maximum trip count: " << MaxTC << '\n');
3500 if (TC.isScalar()) {
3501 reportVectorizationFailure("Single iteration (non) loop",
3502 "loop trip count is one, irrelevant for vectorization",
3503 "SingleIterationLoop", ORE, TheLoop);
3505 }
3506
3507 // If BTC matches the widest induction type and is -1 then the trip count
3508 // computation will wrap to 0 and the vector trip count will be 0. Do not try
3509 // to vectorize.
3510 const SCEV *BTC = SE->getBackedgeTakenCount(TheLoop);
3511 if (!isa<SCEVCouldNotCompute>(BTC) &&
3512 BTC->getType()->getScalarSizeInBits() >=
3513 Legal->getWidestInductionType()->getScalarSizeInBits() &&
3515 SE->getMinusOne(BTC->getType()))) {
3517 "Trip count computation wrapped",
3518 "backedge-taken count is -1, loop trip count wrapped to 0",
3519 "TripCountWrapped", ORE, TheLoop);
3521 }
3522
3523 switch (ScalarEpilogueStatus) {
3525 return computeFeasibleMaxVF(MaxTC, UserVF, false);
3527 [[fallthrough]];
3529 LLVM_DEBUG(
3530 dbgs() << "LV: vector predicate hint/switch found.\n"
3531 << "LV: Not allowing scalar epilogue, creating predicated "
3532 << "vector loop.\n");
3533 break;
3535 // fallthrough as a special case of OptForSize
3537 if (ScalarEpilogueStatus == CM_ScalarEpilogueNotAllowedOptSize)
3538 LLVM_DEBUG(
3539 dbgs() << "LV: Not allowing scalar epilogue due to -Os/-Oz.\n");
3540 else
3541 LLVM_DEBUG(dbgs() << "LV: Not allowing scalar epilogue due to low trip "
3542 << "count.\n");
3543
3544 // Bail if runtime checks are required, which are not good when optimising
3545 // for size.
3548
3549 break;
3550 }
3551
3552 // Now try the tail folding
3553
3554 // Invalidate interleave groups that require an epilogue if we can't mask
3555 // the interleave-group.
3557 assert(WideningDecisions.empty() && Uniforms.empty() && Scalars.empty() &&
3558 "No decisions should have been taken at this point");
3559 // Note: There is no need to invalidate any cost modeling decisions here, as
3560 // none were taken so far.
3561 InterleaveInfo.invalidateGroupsRequiringScalarEpilogue();
3562 }
3563
3564 FixedScalableVFPair MaxFactors = computeFeasibleMaxVF(MaxTC, UserVF, true);
3565
3566 // Avoid tail folding if the trip count is known to be a multiple of any VF
3567 // we choose.
3568 std::optional<unsigned> MaxPowerOf2RuntimeVF =
3569 MaxFactors.FixedVF.getFixedValue();
3570 if (MaxFactors.ScalableVF) {
3571 std::optional<unsigned> MaxVScale = getMaxVScale(*TheFunction, TTI);
3572 if (MaxVScale && TTI.isVScaleKnownToBeAPowerOfTwo()) {
3573 MaxPowerOf2RuntimeVF = std::max<unsigned>(
3574 *MaxPowerOf2RuntimeVF,
3575 *MaxVScale * MaxFactors.ScalableVF.getKnownMinValue());
3576 } else
3577 MaxPowerOf2RuntimeVF = std::nullopt; // Stick with tail-folding for now.
3578 }
3579
3580 auto NoScalarEpilogueNeeded = [this, &UserIC](unsigned MaxVF) {
3581 // Return false if the loop is neither a single-latch-exit loop nor an
3582 // early-exit loop as tail-folding is not supported in that case.
3583 if (TheLoop->getExitingBlock() != TheLoop->getLoopLatch() &&
3584 !Legal->hasUncountableEarlyExit())
3585 return false;
3586 unsigned MaxVFtimesIC = UserIC ? MaxVF * UserIC : MaxVF;
3587 ScalarEvolution *SE = PSE.getSE();
3588 // Calling getSymbolicMaxBackedgeTakenCount enables support for loops
3589 // with uncountable exits. For countable loops, the symbolic maximum must
3590 // remain identical to the known back-edge taken count.
3591 const SCEV *BackedgeTakenCount = PSE.getSymbolicMaxBackedgeTakenCount();
3592 assert((Legal->hasUncountableEarlyExit() ||
3593 BackedgeTakenCount == PSE.getBackedgeTakenCount()) &&
3594 "Invalid loop count");
3595 const SCEV *ExitCount = SE->getAddExpr(
3596 BackedgeTakenCount, SE->getOne(BackedgeTakenCount->getType()));
3597 const SCEV *Rem = SE->getURemExpr(
3598 SE->applyLoopGuards(ExitCount, TheLoop),
3599 SE->getConstant(BackedgeTakenCount->getType(), MaxVFtimesIC));
3600 return Rem->isZero();
3601 };
3602
3603 if (MaxPowerOf2RuntimeVF > 0u) {
3604 assert((UserVF.isNonZero() || isPowerOf2_32(*MaxPowerOf2RuntimeVF)) &&
3605 "MaxFixedVF must be a power of 2");
3606 if (NoScalarEpilogueNeeded(*MaxPowerOf2RuntimeVF)) {
3607 // Accept MaxFixedVF if we do not have a tail.
3608 LLVM_DEBUG(dbgs() << "LV: No tail will remain for any chosen VF.\n");
3609 return MaxFactors;
3610 }
3611 }
3612
3613 auto ExpectedTC = getSmallBestKnownTC(PSE, TheLoop);
3614 if (ExpectedTC && ExpectedTC->isFixed() &&
3615 ExpectedTC->getFixedValue() <=
3616 TTI.getMinTripCountTailFoldingThreshold()) {
3617 if (MaxPowerOf2RuntimeVF > 0u) {
3618 // If we have a low-trip-count, and the fixed-width VF is known to divide
3619 // the trip count but the scalable factor does not, use the fixed-width
3620 // factor in preference to allow the generation of a non-predicated loop.
3621 if (ScalarEpilogueStatus == CM_ScalarEpilogueNotAllowedLowTripLoop &&
3622 NoScalarEpilogueNeeded(MaxFactors.FixedVF.getFixedValue())) {
3623 LLVM_DEBUG(dbgs() << "LV: Picking a fixed-width so that no tail will "
3624 "remain for any chosen VF.\n");
3625 MaxFactors.ScalableVF = ElementCount::getScalable(0);
3626 return MaxFactors;
3627 }
3628 }
3629
3631 "The trip count is below the minial threshold value.",
3632 "loop trip count is too low, avoiding vectorization", "LowTripCount",
3633 ORE, TheLoop);
3635 }
3636
3637 // If we don't know the precise trip count, or if the trip count that we
3638 // found modulo the vectorization factor is not zero, try to fold the tail
3639 // by masking.
3640 // FIXME: look for a smaller MaxVF that does divide TC rather than masking.
3641 bool ContainsScalableVF = MaxFactors.ScalableVF.isNonZero();
3642 setTailFoldingStyles(ContainsScalableVF, UserIC);
3643 if (foldTailByMasking()) {
3645 LLVM_DEBUG(
3646 dbgs()
3647 << "LV: tail is folded with EVL, forcing unroll factor to be 1. Will "
3648 "try to generate VP Intrinsics with scalable vector "
3649 "factors only.\n");
3650 // Tail folded loop using VP intrinsics restricts the VF to be scalable
3651 // for now.
3652 // TODO: extend it for fixed vectors, if required.
3653 assert(ContainsScalableVF && "Expected scalable vector factor.");
3654
3655 MaxFactors.FixedVF = ElementCount::getFixed(1);
3656 }
3657 return MaxFactors;
3658 }
3659
3660 // If there was a tail-folding hint/switch, but we can't fold the tail by
3661 // masking, fallback to a vectorization with a scalar epilogue.
3662 if (ScalarEpilogueStatus == CM_ScalarEpilogueNotNeededUsePredicate) {
3663 LLVM_DEBUG(dbgs() << "LV: Cannot fold tail by masking: vectorize with a "
3664 "scalar epilogue instead.\n");
3665 ScalarEpilogueStatus = CM_ScalarEpilogueAllowed;
3666 return MaxFactors;
3667 }
3668
3669 if (ScalarEpilogueStatus == CM_ScalarEpilogueNotAllowedUsePredicate) {
3670 LLVM_DEBUG(dbgs() << "LV: Can't fold tail by masking: don't vectorize\n");
3672 }
3673
3674 if (TC.isZero()) {
3676 "unable to calculate the loop count due to complex control flow",
3677 "UnknownLoopCountComplexCFG", ORE, TheLoop);
3679 }
3680
3682 "Cannot optimize for size and vectorize at the same time.",
3683 "cannot optimize for size and vectorize at the same time. "
3684 "Enable vectorization of this loop with '#pragma clang loop "
3685 "vectorize(enable)' when compiling with -Os/-Oz",
3686 "NoTailLoopWithOptForSize", ORE, TheLoop);
3688}
3689
3691 ElementCount VF) {
3692 if (ConsiderRegPressure.getNumOccurrences())
3693 return ConsiderRegPressure;
3694
3695 // TODO: We should eventually consider register pressure for all targets. The
3696 // TTI hook is temporary whilst target-specific issues are being fixed.
3697 if (TTI.shouldConsiderVectorizationRegPressure())
3698 return true;
3699
3700 if (!useMaxBandwidth(VF.isScalable()
3703 return false;
3704 // Only calculate register pressure for VFs enabled by MaxBandwidth.
3706 VF, VF.isScalable() ? MaxPermissibleVFWithoutMaxBW.ScalableVF
3708}
3709
3712 return MaximizeBandwidth || (MaximizeBandwidth.getNumOccurrences() == 0 &&
3713 (TTI.shouldMaximizeVectorBandwidth(RegKind) ||
3715 Legal->hasVectorCallVariants())));
3716}
3717
3718ElementCount LoopVectorizationCostModel::clampVFByMaxTripCount(
3719 ElementCount VF, unsigned MaxTripCount, bool FoldTailByMasking) const {
3720 unsigned EstimatedVF = VF.getKnownMinValue();
3721 if (VF.isScalable() && TheFunction->hasFnAttribute(Attribute::VScaleRange)) {
3722 auto Attr = TheFunction->getFnAttribute(Attribute::VScaleRange);
3723 auto Min = Attr.getVScaleRangeMin();
3724 EstimatedVF *= Min;
3725 }
3726
3727 // When a scalar epilogue is required, at least one iteration of the scalar
3728 // loop has to execute. Adjust MaxTripCount accordingly to avoid picking a
3729 // max VF that results in a dead vector loop.
3730 if (MaxTripCount > 0 && requiresScalarEpilogue(true))
3731 MaxTripCount -= 1;
3732
3733 if (MaxTripCount && MaxTripCount <= EstimatedVF &&
3734 (!FoldTailByMasking || isPowerOf2_32(MaxTripCount))) {
3735 // If upper bound loop trip count (TC) is known at compile time there is no
3736 // point in choosing VF greater than TC (as done in the loop below). Select
3737 // maximum power of two which doesn't exceed TC. If VF is
3738 // scalable, we only fall back on a fixed VF when the TC is less than or
3739 // equal to the known number of lanes.
3740 auto ClampedUpperTripCount = llvm::bit_floor(MaxTripCount);
3741 LLVM_DEBUG(dbgs() << "LV: Clamping the MaxVF to maximum power of two not "
3742 "exceeding the constant trip count: "
3743 << ClampedUpperTripCount << "\n");
3744 return ElementCount::get(ClampedUpperTripCount,
3745 FoldTailByMasking ? VF.isScalable() : false);
3746 }
3747 return VF;
3748}
3749
3750ElementCount LoopVectorizationCostModel::getMaximizedVFForTarget(
3751 unsigned MaxTripCount, unsigned SmallestType, unsigned WidestType,
3752 ElementCount MaxSafeVF, bool FoldTailByMasking) {
3753 bool ComputeScalableMaxVF = MaxSafeVF.isScalable();
3754 const TypeSize WidestRegister = TTI.getRegisterBitWidth(
3755 ComputeScalableMaxVF ? TargetTransformInfo::RGK_ScalableVector
3757
3758 // Convenience function to return the minimum of two ElementCounts.
3759 auto MinVF = [](const ElementCount &LHS, const ElementCount &RHS) {
3760 assert((LHS.isScalable() == RHS.isScalable()) &&
3761 "Scalable flags must match");
3762 return ElementCount::isKnownLT(LHS, RHS) ? LHS : RHS;
3763 };
3764
3765 // Ensure MaxVF is a power of 2; the dependence distance bound may not be.
3766 // Note that both WidestRegister and WidestType may not be a powers of 2.
3767 auto MaxVectorElementCount = ElementCount::get(
3768 llvm::bit_floor(WidestRegister.getKnownMinValue() / WidestType),
3769 ComputeScalableMaxVF);
3770 MaxVectorElementCount = MinVF(MaxVectorElementCount, MaxSafeVF);
3771 LLVM_DEBUG(dbgs() << "LV: The Widest register safe to use is: "
3772 << (MaxVectorElementCount * WidestType) << " bits.\n");
3773
3774 if (!MaxVectorElementCount) {
3775 LLVM_DEBUG(dbgs() << "LV: The target has no "
3776 << (ComputeScalableMaxVF ? "scalable" : "fixed")
3777 << " vector registers.\n");
3778 return ElementCount::getFixed(1);
3779 }
3780
3781 ElementCount MaxVF = clampVFByMaxTripCount(MaxVectorElementCount,
3782 MaxTripCount, FoldTailByMasking);
3783 // If the MaxVF was already clamped, there's no point in trying to pick a
3784 // larger one.
3785 if (MaxVF != MaxVectorElementCount)
3786 return MaxVF;
3787
3789 ComputeScalableMaxVF ? TargetTransformInfo::RGK_ScalableVector
3791
3792 if (MaxVF.isScalable())
3793 MaxPermissibleVFWithoutMaxBW.ScalableVF = MaxVF;
3794 else
3795 MaxPermissibleVFWithoutMaxBW.FixedVF = MaxVF;
3796
3797 if (useMaxBandwidth(RegKind)) {
3798 auto MaxVectorElementCountMaxBW = ElementCount::get(
3799 llvm::bit_floor(WidestRegister.getKnownMinValue() / SmallestType),
3800 ComputeScalableMaxVF);
3801 MaxVF = MinVF(MaxVectorElementCountMaxBW, MaxSafeVF);
3802
3803 if (ElementCount MinVF =
3804 TTI.getMinimumVF(SmallestType, ComputeScalableMaxVF)) {
3805 if (ElementCount::isKnownLT(MaxVF, MinVF)) {
3806 LLVM_DEBUG(dbgs() << "LV: Overriding calculated MaxVF(" << MaxVF
3807 << ") with target's minimum: " << MinVF << '\n');
3808 MaxVF = MinVF;
3809 }
3810 }
3811
3812 MaxVF = clampVFByMaxTripCount(MaxVF, MaxTripCount, FoldTailByMasking);
3813
3814 if (MaxVectorElementCount != MaxVF) {
3815 // Invalidate any widening decisions we might have made, in case the loop
3816 // requires prediction (decided later), but we have already made some
3817 // load/store widening decisions.
3818 invalidateCostModelingDecisions();
3819 }
3820 }
3821 return MaxVF;
3822}
3823
3824bool LoopVectorizationPlanner::isMoreProfitable(const VectorizationFactor &A,
3825 const VectorizationFactor &B,
3826 const unsigned MaxTripCount,
3827 bool HasTail,
3828 bool IsEpilogue) const {
3829 InstructionCost CostA = A.Cost;
3830 InstructionCost CostB = B.Cost;
3831
3832 // Improve estimate for the vector width if it is scalable.
3833 unsigned EstimatedWidthA = A.Width.getKnownMinValue();
3834 unsigned EstimatedWidthB = B.Width.getKnownMinValue();
3835 if (std::optional<unsigned> VScale = CM.getVScaleForTuning()) {
3836 if (A.Width.isScalable())
3837 EstimatedWidthA *= *VScale;
3838 if (B.Width.isScalable())
3839 EstimatedWidthB *= *VScale;
3840 }
3841
3842 // When optimizing for size choose whichever is smallest, which will be the
3843 // one with the smallest cost for the whole loop. On a tie pick the larger
3844 // vector width, on the assumption that throughput will be greater.
3845 if (CM.CostKind == TTI::TCK_CodeSize)
3846 return CostA < CostB ||
3847 (CostA == CostB && EstimatedWidthA > EstimatedWidthB);
3848
3849 // Assume vscale may be larger than 1 (or the value being tuned for),
3850 // so that scalable vectorization is slightly favorable over fixed-width
3851 // vectorization.
3852 bool PreferScalable = !TTI.preferFixedOverScalableIfEqualCost(IsEpilogue) &&
3853 A.Width.isScalable() && !B.Width.isScalable();
3854
3855 auto CmpFn = [PreferScalable](const InstructionCost &LHS,
3856 const InstructionCost &RHS) {
3857 return PreferScalable ? LHS <= RHS : LHS < RHS;
3858 };
3859
3860 // To avoid the need for FP division:
3861 // (CostA / EstimatedWidthA) < (CostB / EstimatedWidthB)
3862 // <=> (CostA * EstimatedWidthB) < (CostB * EstimatedWidthA)
3863 if (!MaxTripCount)
3864 return CmpFn(CostA * EstimatedWidthB, CostB * EstimatedWidthA);
3865
3866 auto GetCostForTC = [MaxTripCount, HasTail](unsigned VF,
3867 InstructionCost VectorCost,
3868 InstructionCost ScalarCost) {
3869 // If the trip count is a known (possibly small) constant, the trip count
3870 // will be rounded up to an integer number of iterations under
3871 // FoldTailByMasking. The total cost in that case will be
3872 // VecCost*ceil(TripCount/VF). When not folding the tail, the total
3873 // cost will be VecCost*floor(TC/VF) + ScalarCost*(TC%VF). There will be
3874 // some extra overheads, but for the purpose of comparing the costs of
3875 // different VFs we can use this to compare the total loop-body cost
3876 // expected after vectorization.
3877 if (HasTail)
3878 return VectorCost * (MaxTripCount / VF) +
3879 ScalarCost * (MaxTripCount % VF);
3880 return VectorCost * divideCeil(MaxTripCount, VF);
3881 };
3882
3883 auto RTCostA = GetCostForTC(EstimatedWidthA, CostA, A.ScalarCost);
3884 auto RTCostB = GetCostForTC(EstimatedWidthB, CostB, B.ScalarCost);
3885 return CmpFn(RTCostA, RTCostB);
3886}
3887
3888bool LoopVectorizationPlanner::isMoreProfitable(const VectorizationFactor &A,
3889 const VectorizationFactor &B,
3890 bool HasTail,
3891 bool IsEpilogue) const {
3892 const unsigned MaxTripCount = PSE.getSmallConstantMaxTripCount();
3893 return LoopVectorizationPlanner::isMoreProfitable(A, B, MaxTripCount, HasTail,
3894 IsEpilogue);
3895}
3896
3899 using RecipeVFPair = std::pair<VPRecipeBase *, ElementCount>;
3900 SmallVector<RecipeVFPair> InvalidCosts;
3901 for (const auto &Plan : VPlans) {
3902 for (ElementCount VF : Plan->vectorFactors()) {
3903 // The VPlan-based cost model is designed for computing vector cost.
3904 // Querying VPlan-based cost model with a scarlar VF will cause some
3905 // errors because we expect the VF is vector for most of the widen
3906 // recipes.
3907 if (VF.isScalar())
3908 continue;
3909
3910 VPCostContext CostCtx(CM.TTI, *CM.TLI, *Plan, CM, CM.CostKind,
3911 *CM.PSE.getSE(), OrigLoop);
3912 precomputeCosts(*Plan, VF, CostCtx);
3913 auto Iter = vp_depth_first_deep(Plan->getVectorLoopRegion()->getEntry());
3915 for (auto &R : *VPBB) {
3916 if (!R.cost(VF, CostCtx).isValid())
3917 InvalidCosts.emplace_back(&R, VF);
3918 }
3919 }
3920 }
3921 }
3922 if (InvalidCosts.empty())
3923 return;
3924
3925 // Emit a report of VFs with invalid costs in the loop.
3926
3927 // Group the remarks per recipe, keeping the recipe order from InvalidCosts.
3929 unsigned I = 0;
3930 for (auto &Pair : InvalidCosts)
3931 if (Numbering.try_emplace(Pair.first, I).second)
3932 ++I;
3933
3934 // Sort the list, first on recipe(number) then on VF.
3935 sort(InvalidCosts, [&Numbering](RecipeVFPair &A, RecipeVFPair &B) {
3936 unsigned NA = Numbering[A.first];
3937 unsigned NB = Numbering[B.first];
3938 if (NA != NB)
3939 return NA < NB;
3940 return ElementCount::isKnownLT(A.second, B.second);
3941 });
3942
3943 // For a list of ordered recipe-VF pairs:
3944 // [(load, VF1), (load, VF2), (store, VF1)]
3945 // group the recipes together to emit separate remarks for:
3946 // load (VF1, VF2)
3947 // store (VF1)
3948 auto Tail = ArrayRef<RecipeVFPair>(InvalidCosts);
3949 auto Subset = ArrayRef<RecipeVFPair>();
3950 do {
3951 if (Subset.empty())
3952 Subset = Tail.take_front(1);
3953
3954 VPRecipeBase *R = Subset.front().first;
3955
3956 unsigned Opcode =
3959 [](const auto *R) { return Instruction::PHI; })
3960 .Case<VPWidenSelectRecipe>(
3961 [](const auto *R) { return Instruction::Select; })
3962 .Case<VPWidenStoreRecipe>(
3963 [](const auto *R) { return Instruction::Store; })
3964 .Case<VPWidenLoadRecipe>(
3965 [](const auto *R) { return Instruction::Load; })
3966 .Case<VPWidenCallRecipe, VPWidenIntrinsicRecipe>(
3967 [](const auto *R) { return Instruction::Call; })
3970 [](const auto *R) { return R->getOpcode(); })
3971 .Case<VPInterleaveRecipe>([](const VPInterleaveRecipe *R) {
3972 return R->getStoredValues().empty() ? Instruction::Load
3973 : Instruction::Store;
3974 });
3975
3976 // If the next recipe is different, or if there are no other pairs,
3977 // emit a remark for the collated subset. e.g.
3978 // [(load, VF1), (load, VF2))]
3979 // to emit:
3980 // remark: invalid costs for 'load' at VF=(VF1, VF2)
3981 if (Subset == Tail || Tail[Subset.size()].first != R) {
3982 std::string OutString;
3983 raw_string_ostream OS(OutString);
3984 assert(!Subset.empty() && "Unexpected empty range");
3985 OS << "Recipe with invalid costs prevented vectorization at VF=(";
3986 for (const auto &Pair : Subset)
3987 OS << (Pair.second == Subset.front().second ? "" : ", ") << Pair.second;
3988 OS << "):";
3989 if (Opcode == Instruction::Call) {
3990 StringRef Name = "";
3991 if (auto *Int = dyn_cast<VPWidenIntrinsicRecipe>(R)) {
3992 Name = Int->getIntrinsicName();
3993 } else {
3994 auto *WidenCall = dyn_cast<VPWidenCallRecipe>(R);
3995 Function *CalledFn =
3996 WidenCall ? WidenCall->getCalledScalarFunction()
3997 : cast<Function>(R->getOperand(R->getNumOperands() - 1)
3998 ->getLiveInIRValue());
3999 Name = CalledFn->getName();
4000 }
4001 OS << " call to " << Name;
4002 } else
4003 OS << " " << Instruction::getOpcodeName(Opcode);
4004 reportVectorizationInfo(OutString, "InvalidCost", ORE, OrigLoop, nullptr,
4005 R->getDebugLoc());
4006 Tail = Tail.drop_front(Subset.size());
4007 Subset = {};
4008 } else
4009 // Grow the subset by one element
4010 Subset = Tail.take_front(Subset.size() + 1);
4011 } while (!Tail.empty());
4012}
4013
4014/// Check if any recipe of \p Plan will generate a vector value, which will be
4015/// assigned a vector register.
4017 const TargetTransformInfo &TTI) {
4018 assert(VF.isVector() && "Checking a scalar VF?");
4019 VPTypeAnalysis TypeInfo(Plan);
4020 DenseSet<VPRecipeBase *> EphemeralRecipes;
4021 collectEphemeralRecipesForVPlan(Plan, EphemeralRecipes);
4022 // Set of already visited types.
4023 DenseSet<Type *> Visited;
4026 for (VPRecipeBase &R : *VPBB) {
4027 if (EphemeralRecipes.contains(&R))
4028 continue;
4029 // Continue early if the recipe is considered to not produce a vector
4030 // result. Note that this includes VPInstruction where some opcodes may
4031 // produce a vector, to preserve existing behavior as VPInstructions model
4032 // aspects not directly mapped to existing IR instructions.
4033 switch (R.getVPDefID()) {
4034 case VPDef::VPDerivedIVSC:
4035 case VPDef::VPScalarIVStepsSC:
4036 case VPDef::VPReplicateSC:
4037 case VPDef::VPInstructionSC:
4038 case VPDef::VPCanonicalIVPHISC:
4039 case VPDef::VPVectorPointerSC:
4040 case VPDef::VPVectorEndPointerSC:
4041 case VPDef::VPExpandSCEVSC:
4042 case VPDef::VPEVLBasedIVPHISC:
4043 case VPDef::VPPredInstPHISC:
4044 case VPDef::VPBranchOnMaskSC:
4045 continue;
4046 case VPDef::VPReductionSC:
4047 case VPDef::VPActiveLaneMaskPHISC:
4048 case VPDef::VPWidenCallSC:
4049 case VPDef::VPWidenCanonicalIVSC:
4050 case VPDef::VPWidenCastSC:
4051 case VPDef::VPWidenGEPSC:
4052 case VPDef::VPWidenIntrinsicSC:
4053 case VPDef::VPWidenSC:
4054 case VPDef::VPWidenSelectSC:
4055 case VPDef::VPBlendSC:
4056 case VPDef::VPFirstOrderRecurrencePHISC:
4057 case VPDef::VPHistogramSC:
4058 case VPDef::VPWidenPHISC:
4059 case VPDef::VPWidenIntOrFpInductionSC:
4060 case VPDef::VPWidenPointerInductionSC:
4061 case VPDef::VPReductionPHISC:
4062 case VPDef::VPInterleaveEVLSC:
4063 case VPDef::VPInterleaveSC:
4064 case VPDef::VPWidenLoadEVLSC:
4065 case VPDef::VPWidenLoadSC:
4066 case VPDef::VPWidenStoreEVLSC:
4067 case VPDef::VPWidenStoreSC:
4068 break;
4069 default:
4070 llvm_unreachable("unhandled recipe");
4071 }
4072
4073 auto WillGenerateTargetVectors = [&TTI, VF](Type *VectorTy) {
4074 unsigned NumLegalParts = TTI.getNumberOfParts(VectorTy);
4075 if (!NumLegalParts)
4076 return false;
4077 if (VF.isScalable()) {
4078 // <vscale x 1 x iN> is assumed to be profitable over iN because
4079 // scalable registers are a distinct register class from scalar
4080 // ones. If we ever find a target which wants to lower scalable
4081 // vectors back to scalars, we'll need to update this code to
4082 // explicitly ask TTI about the register class uses for each part.
4083 return NumLegalParts <= VF.getKnownMinValue();
4084 }
4085 // Two or more elements that share a register - are vectorized.
4086 return NumLegalParts < VF.getFixedValue();
4087 };
4088
4089 // If no def nor is a store, e.g., branches, continue - no value to check.
4090 if (R.getNumDefinedValues() == 0 &&
4092 continue;
4093 // For multi-def recipes, currently only interleaved loads, suffice to
4094 // check first def only.
4095 // For stores check their stored value; for interleaved stores suffice
4096 // the check first stored value only. In all cases this is the second
4097 // operand.
4098 VPValue *ToCheck =
4099 R.getNumDefinedValues() >= 1 ? R.getVPValue(0) : R.getOperand(1);
4100 Type *ScalarTy = TypeInfo.inferScalarType(ToCheck);
4101 if (!Visited.insert({ScalarTy}).second)
4102 continue;
4103 Type *WideTy = toVectorizedTy(ScalarTy, VF);
4104 if (any_of(getContainedTypes(WideTy), WillGenerateTargetVectors))
4105 return true;
4106 }
4107 }
4108
4109 return false;
4110}
4111
4112static bool hasReplicatorRegion(VPlan &Plan) {
4114 Plan.getVectorLoopRegion()->getEntry())),
4115 [](auto *VPRB) { return VPRB->isReplicator(); });
4116}
4117
4118#ifndef NDEBUG
4119VectorizationFactor LoopVectorizationPlanner::selectVectorizationFactor() {
4120 InstructionCost ExpectedCost = CM.expectedCost(ElementCount::getFixed(1));
4121 LLVM_DEBUG(dbgs() << "LV: Scalar loop costs: " << ExpectedCost << ".\n");
4122 assert(ExpectedCost.isValid() && "Unexpected invalid cost for scalar loop");
4123 assert(
4124 any_of(VPlans,
4125 [](std::unique_ptr<VPlan> &P) { return P->hasScalarVFOnly(); }) &&
4126 "Expected Scalar VF to be a candidate");
4127
4128 const VectorizationFactor ScalarCost(ElementCount::getFixed(1), ExpectedCost,
4129 ExpectedCost);
4130 VectorizationFactor ChosenFactor = ScalarCost;
4131
4132 bool ForceVectorization = Hints.getForce() == LoopVectorizeHints::FK_Enabled;
4133 if (ForceVectorization &&
4134 (VPlans.size() > 1 || !VPlans[0]->hasScalarVFOnly())) {
4135 // Ignore scalar width, because the user explicitly wants vectorization.
4136 // Initialize cost to max so that VF = 2 is, at least, chosen during cost
4137 // evaluation.
4138 ChosenFactor.Cost = InstructionCost::getMax();
4139 }
4140
4141 for (auto &P : VPlans) {
4142 ArrayRef<ElementCount> VFs(P->vectorFactors().begin(),
4143 P->vectorFactors().end());
4144
4146 if (any_of(VFs, [this](ElementCount VF) {
4147 return CM.shouldConsiderRegPressureForVF(VF);
4148 }))
4149 RUs = calculateRegisterUsageForPlan(*P, VFs, TTI, CM.ValuesToIgnore);
4150
4151 for (unsigned I = 0; I < VFs.size(); I++) {
4152 ElementCount VF = VFs[I];
4153 // The cost for scalar VF=1 is already calculated, so ignore it.
4154 if (VF.isScalar())
4155 continue;
4156
4157 /// If the register pressure needs to be considered for VF,
4158 /// don't consider the VF as valid if it exceeds the number
4159 /// of registers for the target.
4160 if (CM.shouldConsiderRegPressureForVF(VF) &&
4161 RUs[I].exceedsMaxNumRegs(TTI, ForceTargetNumVectorRegs))
4162 continue;
4163
4164 InstructionCost C = CM.expectedCost(VF);
4165
4166 // Add on other costs that are modelled in VPlan, but not in the legacy
4167 // cost model.
4168 VPCostContext CostCtx(CM.TTI, *CM.TLI, *P, CM, CM.CostKind,
4169 *CM.PSE.getSE(), OrigLoop);
4170 VPRegionBlock *VectorRegion = P->getVectorLoopRegion();
4171 assert(VectorRegion && "Expected to have a vector region!");
4172 for (VPBasicBlock *VPBB : VPBlockUtils::blocksOnly<VPBasicBlock>(
4173 vp_depth_first_shallow(VectorRegion->getEntry()))) {
4174 for (VPRecipeBase &R : *VPBB) {
4175 auto *VPI = dyn_cast<VPInstruction>(&R);
4176 if (!VPI)
4177 continue;
4178 switch (VPI->getOpcode()) {
4179 // Selects are only modelled in the legacy cost model for safe
4180 // divisors.
4181 case Instruction::Select: {
4182 VPValue *VPV = VPI->getVPSingleValue();
4183 if (VPV->getNumUsers() == 1) {
4184 if (auto *WR = dyn_cast<VPWidenRecipe>(*VPV->user_begin())) {
4185 switch (WR->getOpcode()) {
4186 case Instruction::UDiv:
4187 case Instruction::SDiv:
4188 case Instruction::URem:
4189 case Instruction::SRem:
4190 continue;
4191 default:
4192 break;
4193 }
4194 }
4195 }
4196 C += VPI->cost(VF, CostCtx);
4197 break;
4198 }
4200 unsigned Multiplier =
4201 cast<ConstantInt>(VPI->getOperand(2)->getLiveInIRValue())
4202 ->getZExtValue();
4203 C += VPI->cost(VF * Multiplier, CostCtx);
4204 break;
4205 }
4207 C += VPI->cost(VF, CostCtx);
4208 break;
4209 default:
4210 break;
4211 }
4212 }
4213 }
4214
4215 VectorizationFactor Candidate(VF, C, ScalarCost.ScalarCost);
4216 unsigned Width =
4217 estimateElementCount(Candidate.Width, CM.getVScaleForTuning());
4218 LLVM_DEBUG(dbgs() << "LV: Vector loop of width " << VF
4219 << " costs: " << (Candidate.Cost / Width));
4220 if (VF.isScalable())
4221 LLVM_DEBUG(dbgs() << " (assuming a minimum vscale of "
4222 << CM.getVScaleForTuning().value_or(1) << ")");
4223 LLVM_DEBUG(dbgs() << ".\n");
4224
4225 if (!ForceVectorization && !willGenerateVectors(*P, VF, TTI)) {
4226 LLVM_DEBUG(
4227 dbgs()
4228 << "LV: Not considering vector loop of width " << VF
4229 << " because it will not generate any vector instructions.\n");
4230 continue;
4231 }
4232
4233 if (CM.OptForSize && !ForceVectorization && hasReplicatorRegion(*P)) {
4234 LLVM_DEBUG(
4235 dbgs()
4236 << "LV: Not considering vector loop of width " << VF
4237 << " because it would cause replicated blocks to be generated,"
4238 << " which isn't allowed when optimizing for size.\n");
4239 continue;
4240 }
4241
4242 if (isMoreProfitable(Candidate, ChosenFactor, P->hasScalarTail()))
4243 ChosenFactor = Candidate;
4244 }
4245 }
4246
4247 if (!EnableCondStoresVectorization && CM.hasPredStores()) {
4249 "There are conditional stores.",
4250 "store that is conditionally executed prevents vectorization",
4251 "ConditionalStore", ORE, OrigLoop);
4252 ChosenFactor = ScalarCost;
4253 }
4254
4255 LLVM_DEBUG(if (ForceVectorization && !ChosenFactor.Width.isScalar() &&
4256 !isMoreProfitable(ChosenFactor, ScalarCost,
4257 !CM.foldTailByMasking())) dbgs()
4258 << "LV: Vectorization seems to be not beneficial, "
4259 << "but was forced by a user.\n");
4260 return ChosenFactor;
4261}
4262#endif
4263
4264bool LoopVectorizationPlanner::isCandidateForEpilogueVectorization(
4265 ElementCount VF) const {
4266 // Cross iteration phis such as fixed-order recurrences and FMaxNum/FMinNum
4267 // reductions need special handling and are currently unsupported.
4268 if (any_of(OrigLoop->getHeader()->phis(), [&](PHINode &Phi) {
4269 if (!Legal->isReductionVariable(&Phi))
4270 return Legal->isFixedOrderRecurrence(&Phi);
4271 return RecurrenceDescriptor::isFPMinMaxNumRecurrenceKind(
4272 Legal->getRecurrenceDescriptor(&Phi).getRecurrenceKind());
4273 }))
4274 return false;
4275
4276 // Phis with uses outside of the loop require special handling and are
4277 // currently unsupported.
4278 for (const auto &Entry : Legal->getInductionVars()) {
4279 // Look for uses of the value of the induction at the last iteration.
4280 Value *PostInc =
4281 Entry.first->getIncomingValueForBlock(OrigLoop->getLoopLatch());
4282 for (User *U : PostInc->users())
4283 if (!OrigLoop->contains(cast<Instruction>(U)))
4284 return false;
4285 // Look for uses of penultimate value of the induction.
4286 for (User *U : Entry.first->users())
4287 if (!OrigLoop->contains(cast<Instruction>(U)))
4288 return false;
4289 }
4290
4291 // Epilogue vectorization code has not been auditted to ensure it handles
4292 // non-latch exits properly. It may be fine, but it needs auditted and
4293 // tested.
4294 // TODO: Add support for loops with an early exit.
4295 if (OrigLoop->getExitingBlock() != OrigLoop->getLoopLatch())
4296 return false;
4297
4298 return true;
4299}
4300
4302 const ElementCount VF, const unsigned IC) const {
4303 // FIXME: We need a much better cost-model to take different parameters such
4304 // as register pressure, code size increase and cost of extra branches into
4305 // account. For now we apply a very crude heuristic and only consider loops
4306 // with vectorization factors larger than a certain value.
4307
4308 // Allow the target to opt out entirely.
4309 if (!TTI.preferEpilogueVectorization())
4310 return false;
4311
4312 // We also consider epilogue vectorization unprofitable for targets that don't
4313 // consider interleaving beneficial (eg. MVE).
4314 if (TTI.getMaxInterleaveFactor(VF) <= 1)
4315 return false;
4316
4317 unsigned MinVFThreshold = EpilogueVectorizationMinVF.getNumOccurrences() > 0
4319 : TTI.getEpilogueVectorizationMinVF();
4320 return estimateElementCount(VF * IC, VScaleForTuning) >= MinVFThreshold;
4321}
4322
4324 const ElementCount MainLoopVF, unsigned IC) {
4327 LLVM_DEBUG(dbgs() << "LEV: Epilogue vectorization is disabled.\n");
4328 return Result;
4329 }
4330
4331 if (!CM.isScalarEpilogueAllowed()) {
4332 LLVM_DEBUG(dbgs() << "LEV: Unable to vectorize epilogue because no "
4333 "epilogue is allowed.\n");
4334 return Result;
4335 }
4336
4337 // Not really a cost consideration, but check for unsupported cases here to
4338 // simplify the logic.
4339 if (!isCandidateForEpilogueVectorization(MainLoopVF)) {
4340 LLVM_DEBUG(dbgs() << "LEV: Unable to vectorize epilogue because the loop "
4341 "is not a supported candidate.\n");
4342 return Result;
4343 }
4344
4346 LLVM_DEBUG(dbgs() << "LEV: Epilogue vectorization factor is forced.\n");
4348 if (hasPlanWithVF(ForcedEC))
4349 return {ForcedEC, 0, 0};
4350
4351 LLVM_DEBUG(dbgs() << "LEV: Epilogue vectorization forced factor is not "
4352 "viable.\n");
4353 return Result;
4354 }
4355
4356 if (OrigLoop->getHeader()->getParent()->hasOptSize()) {
4357 LLVM_DEBUG(
4358 dbgs() << "LEV: Epilogue vectorization skipped due to opt for size.\n");
4359 return Result;
4360 }
4361
4362 if (!CM.isEpilogueVectorizationProfitable(MainLoopVF, IC)) {
4363 LLVM_DEBUG(dbgs() << "LEV: Epilogue vectorization is not profitable for "
4364 "this loop\n");
4365 return Result;
4366 }
4367
4368 // If MainLoopVF = vscale x 2, and vscale is expected to be 4, then we know
4369 // the main loop handles 8 lanes per iteration. We could still benefit from
4370 // vectorizing the epilogue loop with VF=4.
4371 ElementCount EstimatedRuntimeVF = ElementCount::getFixed(
4372 estimateElementCount(MainLoopVF, CM.getVScaleForTuning()));
4373
4374 ScalarEvolution &SE = *PSE.getSE();
4375 Type *TCType = Legal->getWidestInductionType();
4376 const SCEV *RemainingIterations = nullptr;
4377 unsigned MaxTripCount = 0;
4378 const SCEV *TC =
4379 vputils::getSCEVExprForVPValue(getPlanFor(MainLoopVF).getTripCount(), SE);
4380 assert(!isa<SCEVCouldNotCompute>(TC) && "Trip count SCEV must be computable");
4381 const SCEV *KnownMinTC;
4382 bool ScalableTC = match(TC, m_scev_c_Mul(m_SCEV(KnownMinTC), m_SCEVVScale()));
4383 // Use versions of TC and VF in which both are either scalable or fixed.
4384 if (ScalableTC == MainLoopVF.isScalable())
4385 RemainingIterations =
4386 SE.getURemExpr(TC, SE.getElementCount(TCType, MainLoopVF * IC));
4387 else if (ScalableTC) {
4388 const SCEV *EstimatedTC = SE.getMulExpr(
4389 KnownMinTC,
4390 SE.getConstant(TCType, CM.getVScaleForTuning().value_or(1)));
4391 RemainingIterations = SE.getURemExpr(
4392 EstimatedTC, SE.getElementCount(TCType, MainLoopVF * IC));
4393 } else
4394 RemainingIterations =
4395 SE.getURemExpr(TC, SE.getElementCount(TCType, EstimatedRuntimeVF * IC));
4396
4397 // No iterations left to process in the epilogue.
4398 if (RemainingIterations->isZero())
4399 return Result;
4400
4401 if (MainLoopVF.isFixed()) {
4402 MaxTripCount = MainLoopVF.getFixedValue() * IC - 1;
4403 if (SE.isKnownPredicate(CmpInst::ICMP_ULT, RemainingIterations,
4404 SE.getConstant(TCType, MaxTripCount))) {
4405 MaxTripCount = SE.getUnsignedRangeMax(RemainingIterations).getZExtValue();
4406 }
4407 LLVM_DEBUG(dbgs() << "LEV: Maximum Trip Count for Epilogue: "
4408 << MaxTripCount << "\n");
4409 }
4410
4411 for (auto &NextVF : ProfitableVFs) {
4412 // Skip candidate VFs without a corresponding VPlan.
4413 if (!hasPlanWithVF(NextVF.Width))
4414 continue;
4415
4416 // Skip candidate VFs with widths >= the (estimated) runtime VF (scalable
4417 // vectors) or > the VF of the main loop (fixed vectors).
4418 if ((!NextVF.Width.isScalable() && MainLoopVF.isScalable() &&
4419 ElementCount::isKnownGE(NextVF.Width, EstimatedRuntimeVF)) ||
4420 (NextVF.Width.isScalable() &&
4421 ElementCount::isKnownGE(NextVF.Width, MainLoopVF)) ||
4422 (!NextVF.Width.isScalable() && !MainLoopVF.isScalable() &&
4423 ElementCount::isKnownGT(NextVF.Width, MainLoopVF)))
4424 continue;
4425
4426 // If NextVF is greater than the number of remaining iterations, the
4427 // epilogue loop would be dead. Skip such factors.
4428 if (RemainingIterations && !NextVF.Width.isScalable()) {
4429 if (SE.isKnownPredicate(
4431 SE.getConstant(TCType, NextVF.Width.getFixedValue()),
4432 RemainingIterations))
4433 continue;
4434 }
4435
4436 if (Result.Width.isScalar() ||
4437 isMoreProfitable(NextVF, Result, MaxTripCount, !CM.foldTailByMasking(),
4438 /*IsEpilogue*/ true))
4439 Result = NextVF;
4440 }
4441
4442 if (Result != VectorizationFactor::Disabled())
4443 LLVM_DEBUG(dbgs() << "LEV: Vectorizing epilogue loop with VF = "
4444 << Result.Width << "\n");
4445 return Result;
4446}
4447
4448std::pair<unsigned, unsigned>
4450 unsigned MinWidth = -1U;
4451 unsigned MaxWidth = 8;
4452 const DataLayout &DL = TheFunction->getDataLayout();
4453 // For in-loop reductions, no element types are added to ElementTypesInLoop
4454 // if there are no loads/stores in the loop. In this case, check through the
4455 // reduction variables to determine the maximum width.
4456 if (ElementTypesInLoop.empty() && !Legal->getReductionVars().empty()) {
4457 for (const auto &PhiDescriptorPair : Legal->getReductionVars()) {
4458 const RecurrenceDescriptor &RdxDesc = PhiDescriptorPair.second;
4459 // When finding the min width used by the recurrence we need to account
4460 // for casts on the input operands of the recurrence.
4461 MinWidth = std::min(
4462 MinWidth,
4463 std::min(RdxDesc.getMinWidthCastToRecurrenceTypeInBits(),
4465 MaxWidth = std::max(MaxWidth,
4467 }
4468 } else {
4469 for (Type *T : ElementTypesInLoop) {
4470 MinWidth = std::min<unsigned>(
4471 MinWidth, DL.getTypeSizeInBits(T->getScalarType()).getFixedValue());
4472 MaxWidth = std::max<unsigned>(
4473 MaxWidth, DL.getTypeSizeInBits(T->getScalarType()).getFixedValue());
4474 }
4475 }
4476 return {MinWidth, MaxWidth};
4477}
4478
4480 ElementTypesInLoop.clear();
4481 // For each block.
4482 for (BasicBlock *BB : TheLoop->blocks()) {
4483 // For each instruction in the loop.
4484 for (Instruction &I : BB->instructionsWithoutDebug()) {
4485 Type *T = I.getType();
4486
4487 // Skip ignored values.
4488 if (ValuesToIgnore.count(&I))
4489 continue;
4490
4491 // Only examine Loads, Stores and PHINodes.
4492 if (!isa<LoadInst>(I) && !isa<StoreInst>(I) && !isa<PHINode>(I))
4493 continue;
4494
4495 // Examine PHI nodes that are reduction variables. Update the type to
4496 // account for the recurrence type.
4497 if (auto *PN = dyn_cast<PHINode>(&I)) {
4498 if (!Legal->isReductionVariable(PN))
4499 continue;
4500 const RecurrenceDescriptor &RdxDesc =
4501 Legal->getRecurrenceDescriptor(PN);
4503 TTI.preferInLoopReduction(RdxDesc.getRecurrenceKind(),
4504 RdxDesc.getRecurrenceType()))
4505 continue;
4506 T = RdxDesc.getRecurrenceType();
4507 }
4508
4509 // Examine the stored values.
4510 if (auto *ST = dyn_cast<StoreInst>(&I))
4511 T = ST->getValueOperand()->getType();
4512
4513 assert(T->isSized() &&
4514 "Expected the load/store/recurrence type to be sized");
4515
4516 ElementTypesInLoop.insert(T);
4517 }
4518 }
4519}
4520
4521unsigned
4523 InstructionCost LoopCost) {
4524 // -- The interleave heuristics --
4525 // We interleave the loop in order to expose ILP and reduce the loop overhead.
4526 // There are many micro-architectural considerations that we can't predict
4527 // at this level. For example, frontend pressure (on decode or fetch) due to
4528 // code size, or the number and capabilities of the execution ports.
4529 //
4530 // We use the following heuristics to select the interleave count:
4531 // 1. If the code has reductions, then we interleave to break the cross
4532 // iteration dependency.
4533 // 2. If the loop is really small, then we interleave to reduce the loop
4534 // overhead.
4535 // 3. We don't interleave if we think that we will spill registers to memory
4536 // due to the increased register pressure.
4537
4538 if (!CM.isScalarEpilogueAllowed())
4539 return 1;
4540
4543 LLVM_DEBUG(dbgs() << "LV: Preference for VP intrinsics indicated. "
4544 "Unroll factor forced to be 1.\n");
4545 return 1;
4546 }
4547
4548 // We used the distance for the interleave count.
4549 if (!Legal->isSafeForAnyVectorWidth())
4550 return 1;
4551
4552 // We don't attempt to perform interleaving for loops with uncountable early
4553 // exits because the VPInstruction::AnyOf code cannot currently handle
4554 // multiple parts.
4555 if (Plan.hasEarlyExit())
4556 return 1;
4557
4558 const bool HasReductions =
4561
4562 // If we did not calculate the cost for VF (because the user selected the VF)
4563 // then we calculate the cost of VF here.
4564 if (LoopCost == 0) {
4565 if (VF.isScalar())
4566 LoopCost = CM.expectedCost(VF);
4567 else
4568 LoopCost = cost(Plan, VF);
4569 assert(LoopCost.isValid() && "Expected to have chosen a VF with valid cost");
4570
4571 // Loop body is free and there is no need for interleaving.
4572 if (LoopCost == 0)
4573 return 1;
4574 }
4575
4576 VPRegisterUsage R =
4577 calculateRegisterUsageForPlan(Plan, {VF}, TTI, CM.ValuesToIgnore)[0];
4578 // We divide by these constants so assume that we have at least one
4579 // instruction that uses at least one register.
4580 for (auto &Pair : R.MaxLocalUsers) {
4581 Pair.second = std::max(Pair.second, 1U);
4582 }
4583
4584 // We calculate the interleave count using the following formula.
4585 // Subtract the number of loop invariants from the number of available
4586 // registers. These registers are used by all of the interleaved instances.
4587 // Next, divide the remaining registers by the number of registers that is
4588 // required by the loop, in order to estimate how many parallel instances
4589 // fit without causing spills. All of this is rounded down if necessary to be
4590 // a power of two. We want power of two interleave count to simplify any
4591 // addressing operations or alignment considerations.
4592 // We also want power of two interleave counts to ensure that the induction
4593 // variable of the vector loop wraps to zero, when tail is folded by masking;
4594 // this currently happens when OptForSize, in which case IC is set to 1 above.
4595 unsigned IC = UINT_MAX;
4596
4597 for (const auto &Pair : R.MaxLocalUsers) {
4598 unsigned TargetNumRegisters = TTI.getNumberOfRegisters(Pair.first);
4599 LLVM_DEBUG(dbgs() << "LV: The target has " << TargetNumRegisters
4600 << " registers of "
4601 << TTI.getRegisterClassName(Pair.first)
4602 << " register class\n");
4603 if (VF.isScalar()) {
4604 if (ForceTargetNumScalarRegs.getNumOccurrences() > 0)
4605 TargetNumRegisters = ForceTargetNumScalarRegs;
4606 } else {
4607 if (ForceTargetNumVectorRegs.getNumOccurrences() > 0)
4608 TargetNumRegisters = ForceTargetNumVectorRegs;
4609 }
4610 unsigned MaxLocalUsers = Pair.second;
4611 unsigned LoopInvariantRegs = 0;
4612 if (R.LoopInvariantRegs.contains(Pair.first))
4613 LoopInvariantRegs = R.LoopInvariantRegs[Pair.first];
4614
4615 unsigned TmpIC = llvm::bit_floor((TargetNumRegisters - LoopInvariantRegs) /
4616 MaxLocalUsers);
4617 // Don't count the induction variable as interleaved.
4619 TmpIC = llvm::bit_floor((TargetNumRegisters - LoopInvariantRegs - 1) /
4620 std::max(1U, (MaxLocalUsers - 1)));
4621 }
4622
4623 IC = std::min(IC, TmpIC);
4624 }
4625
4626 // Clamp the interleave ranges to reasonable counts.
4627 unsigned MaxInterleaveCount = TTI.getMaxInterleaveFactor(VF);
4628
4629 // Check if the user has overridden the max.
4630 if (VF.isScalar()) {
4631 if (ForceTargetMaxScalarInterleaveFactor.getNumOccurrences() > 0)
4632 MaxInterleaveCount = ForceTargetMaxScalarInterleaveFactor;
4633 } else {
4634 if (ForceTargetMaxVectorInterleaveFactor.getNumOccurrences() > 0)
4635 MaxInterleaveCount = ForceTargetMaxVectorInterleaveFactor;
4636 }
4637
4638 // Try to get the exact trip count, or an estimate based on profiling data or
4639 // ConstantMax from PSE, failing that.
4640 auto BestKnownTC = getSmallBestKnownTC(PSE, OrigLoop);
4641
4642 // For fixed length VFs treat a scalable trip count as unknown.
4643 if (BestKnownTC && (BestKnownTC->isFixed() || VF.isScalable())) {
4644 // Re-evaluate trip counts and VFs to be in the same numerical space.
4645 unsigned AvailableTC =
4646 estimateElementCount(*BestKnownTC, CM.getVScaleForTuning());
4647 unsigned EstimatedVF = estimateElementCount(VF, CM.getVScaleForTuning());
4648
4649 // At least one iteration must be scalar when this constraint holds. So the
4650 // maximum available iterations for interleaving is one less.
4651 if (CM.requiresScalarEpilogue(VF.isVector()))
4652 --AvailableTC;
4653
4654 unsigned InterleaveCountLB = bit_floor(std::max(
4655 1u, std::min(AvailableTC / (EstimatedVF * 2), MaxInterleaveCount)));
4656
4657 if (getSmallConstantTripCount(PSE.getSE(), OrigLoop).isNonZero()) {
4658 // If the best known trip count is exact, we select between two
4659 // prospective ICs, where
4660 //
4661 // 1) the aggressive IC is capped by the trip count divided by VF
4662 // 2) the conservative IC is capped by the trip count divided by (VF * 2)
4663 //
4664 // The final IC is selected in a way that the epilogue loop trip count is
4665 // minimized while maximizing the IC itself, so that we either run the
4666 // vector loop at least once if it generates a small epilogue loop, or
4667 // else we run the vector loop at least twice.
4668
4669 unsigned InterleaveCountUB = bit_floor(std::max(
4670 1u, std::min(AvailableTC / EstimatedVF, MaxInterleaveCount)));
4671 MaxInterleaveCount = InterleaveCountLB;
4672
4673 if (InterleaveCountUB != InterleaveCountLB) {
4674 unsigned TailTripCountUB =
4675 (AvailableTC % (EstimatedVF * InterleaveCountUB));
4676 unsigned TailTripCountLB =
4677 (AvailableTC % (EstimatedVF * InterleaveCountLB));
4678 // If both produce same scalar tail, maximize the IC to do the same work
4679 // in fewer vector loop iterations
4680 if (TailTripCountUB == TailTripCountLB)
4681 MaxInterleaveCount = InterleaveCountUB;
4682 }
4683 } else {
4684 // If trip count is an estimated compile time constant, limit the
4685 // IC to be capped by the trip count divided by VF * 2, such that the
4686 // vector loop runs at least twice to make interleaving seem profitable
4687 // when there is an epilogue loop present. Since exact Trip count is not
4688 // known we choose to be conservative in our IC estimate.
4689 MaxInterleaveCount = InterleaveCountLB;
4690 }
4691 }
4692
4693 assert(MaxInterleaveCount > 0 &&
4694 "Maximum interleave count must be greater than 0");
4695
4696 // Clamp the calculated IC to be between the 1 and the max interleave count
4697 // that the target and trip count allows.
4698 if (IC > MaxInterleaveCount)
4699 IC = MaxInterleaveCount;
4700 else
4701 // Make sure IC is greater than 0.
4702 IC = std::max(1u, IC);
4703
4704 assert(IC > 0 && "Interleave count must be greater than 0.");
4705
4706 // Interleave if we vectorized this loop and there is a reduction that could
4707 // benefit from interleaving.
4708 if (VF.isVector() && HasReductions) {
4709 LLVM_DEBUG(dbgs() << "LV: Interleaving because of reductions.\n");
4710 return IC;
4711 }
4712
4713 // For any scalar loop that either requires runtime checks or predication we
4714 // are better off leaving this to the unroller. Note that if we've already
4715 // vectorized the loop we will have done the runtime check and so interleaving
4716 // won't require further checks.
4717 bool ScalarInterleavingRequiresPredication =
4718 (VF.isScalar() && any_of(OrigLoop->blocks(), [this](BasicBlock *BB) {
4719 return Legal->blockNeedsPredication(BB);
4720 }));
4721 bool ScalarInterleavingRequiresRuntimePointerCheck =
4722 (VF.isScalar() && Legal->getRuntimePointerChecking()->Need);
4723
4724 // We want to interleave small loops in order to reduce the loop overhead and
4725 // potentially expose ILP opportunities.
4726 LLVM_DEBUG(dbgs() << "LV: Loop cost is " << LoopCost << '\n'
4727 << "LV: IC is " << IC << '\n'
4728 << "LV: VF is " << VF << '\n');
4729 const bool AggressivelyInterleaveReductions =
4730 TTI.enableAggressiveInterleaving(HasReductions);
4731 if (!ScalarInterleavingRequiresRuntimePointerCheck &&
4732 !ScalarInterleavingRequiresPredication && LoopCost < SmallLoopCost) {
4733 // We assume that the cost overhead is 1 and we use the cost model
4734 // to estimate the cost of the loop and interleave until the cost of the
4735 // loop overhead is about 5% of the cost of the loop.
4736 unsigned SmallIC = std::min(IC, (unsigned)llvm::bit_floor<uint64_t>(
4737 SmallLoopCost / LoopCost.getValue()));
4738
4739 // Interleave until store/load ports (estimated by max interleave count) are
4740 // saturated.
4741 unsigned NumStores = 0;
4742 unsigned NumLoads = 0;
4745 for (VPRecipeBase &R : *VPBB) {
4747 NumLoads++;
4748 continue;
4749 }
4751 NumStores++;
4752 continue;
4753 }
4754
4755 if (auto *InterleaveR = dyn_cast<VPInterleaveRecipe>(&R)) {
4756 if (unsigned StoreOps = InterleaveR->getNumStoreOperands())
4757 NumStores += StoreOps;
4758 else
4759 NumLoads += InterleaveR->getNumDefinedValues();
4760 continue;
4761 }
4762 if (auto *RepR = dyn_cast<VPReplicateRecipe>(&R)) {
4763 NumLoads += isa<LoadInst>(RepR->getUnderlyingInstr());
4764 NumStores += isa<StoreInst>(RepR->getUnderlyingInstr());
4765 continue;
4766 }
4767 if (isa<VPHistogramRecipe>(&R)) {
4768 NumLoads++;
4769 NumStores++;
4770 continue;
4771 }
4772 }
4773 }
4774 unsigned StoresIC = IC / (NumStores ? NumStores : 1);
4775 unsigned LoadsIC = IC / (NumLoads ? NumLoads : 1);
4776
4777 // There is little point in interleaving for reductions containing selects
4778 // and compares when VF=1 since it may just create more overhead than it's
4779 // worth for loops with small trip counts. This is because we still have to
4780 // do the final reduction after the loop.
4781 bool HasSelectCmpReductions =
4782 HasReductions &&
4784 [](VPRecipeBase &R) {
4785 auto *RedR = dyn_cast<VPReductionPHIRecipe>(&R);
4786 return RedR && (RecurrenceDescriptor::isAnyOfRecurrenceKind(
4787 RedR->getRecurrenceKind()) ||
4788 RecurrenceDescriptor::isFindIVRecurrenceKind(
4789 RedR->getRecurrenceKind()));
4790 });
4791 if (HasSelectCmpReductions) {
4792 LLVM_DEBUG(dbgs() << "LV: Not interleaving select-cmp reductions.\n");
4793 return 1;
4794 }
4795
4796 // If we have a scalar reduction (vector reductions are already dealt with
4797 // by this point), we can increase the critical path length if the loop
4798 // we're interleaving is inside another loop. For tree-wise reductions
4799 // set the limit to 2, and for ordered reductions it's best to disable
4800 // interleaving entirely.
4801 if (HasReductions && OrigLoop->getLoopDepth() > 1) {
4802 bool HasOrderedReductions =
4804 [](VPRecipeBase &R) {
4805 auto *RedR = dyn_cast<VPReductionPHIRecipe>(&R);
4806
4807 return RedR && RedR->isOrdered();
4808 });
4809 if (HasOrderedReductions) {
4810 LLVM_DEBUG(
4811 dbgs() << "LV: Not interleaving scalar ordered reductions.\n");
4812 return 1;
4813 }
4814
4815 unsigned F = MaxNestedScalarReductionIC;
4816 SmallIC = std::min(SmallIC, F);
4817 StoresIC = std::min(StoresIC, F);
4818 LoadsIC = std::min(LoadsIC, F);
4819 }
4820
4822 std::max(StoresIC, LoadsIC) > SmallIC) {
4823 LLVM_DEBUG(
4824 dbgs() << "LV: Interleaving to saturate store or load ports.\n");
4825 return std::max(StoresIC, LoadsIC);
4826 }
4827
4828 // If there are scalar reductions and TTI has enabled aggressive
4829 // interleaving for reductions, we will interleave to expose ILP.
4830 if (VF.isScalar() && AggressivelyInterleaveReductions) {
4831 LLVM_DEBUG(dbgs() << "LV: Interleaving to expose ILP.\n");
4832 // Interleave no less than SmallIC but not as aggressive as the normal IC
4833 // to satisfy the rare situation when resources are too limited.
4834 return std::max(IC / 2, SmallIC);
4835 }
4836
4837 LLVM_DEBUG(dbgs() << "LV: Interleaving to reduce branch cost.\n");
4838 return SmallIC;
4839 }
4840
4841 // Interleave if this is a large loop (small loops are already dealt with by
4842 // this point) that could benefit from interleaving.
4843 if (AggressivelyInterleaveReductions) {
4844 LLVM_DEBUG(dbgs() << "LV: Interleaving to expose ILP.\n");
4845 return IC;
4846 }
4847
4848 LLVM_DEBUG(dbgs() << "LV: Not Interleaving.\n");
4849 return 1;
4850}
4851
4852bool LoopVectorizationCostModel::useEmulatedMaskMemRefHack(Instruction *I,
4853 ElementCount VF) {
4854 // TODO: Cost model for emulated masked load/store is completely
4855 // broken. This hack guides the cost model to use an artificially
4856 // high enough value to practically disable vectorization with such
4857 // operations, except where previously deployed legality hack allowed
4858 // using very low cost values. This is to avoid regressions coming simply
4859 // from moving "masked load/store" check from legality to cost model.
4860 // Masked Load/Gather emulation was previously never allowed.
4861 // Limited number of Masked Store/Scatter emulation was allowed.
4862 assert((isPredicatedInst(I)) &&
4863 "Expecting a scalar emulated instruction");
4864 return isa<LoadInst>(I) ||
4865 (isa<StoreInst>(I) &&
4866 NumPredStores > NumberOfStoresToPredicate);
4867}
4868
4870 assert(VF.isVector() && "Expected VF >= 2");
4871
4872 // If we've already collected the instructions to scalarize or the predicated
4873 // BBs after vectorization, there's nothing to do. Collection may already have
4874 // occurred if we have a user-selected VF and are now computing the expected
4875 // cost for interleaving.
4876 if (InstsToScalarize.contains(VF) ||
4877 PredicatedBBsAfterVectorization.contains(VF))
4878 return;
4879
4880 // Initialize a mapping for VF in InstsToScalalarize. If we find that it's
4881 // not profitable to scalarize any instructions, the presence of VF in the
4882 // map will indicate that we've analyzed it already.
4883 ScalarCostsTy &ScalarCostsVF = InstsToScalarize[VF];
4884
4885 // Find all the instructions that are scalar with predication in the loop and
4886 // determine if it would be better to not if-convert the blocks they are in.
4887 // If so, we also record the instructions to scalarize.
4888 for (BasicBlock *BB : TheLoop->blocks()) {
4890 continue;
4891 for (Instruction &I : *BB)
4892 if (isScalarWithPredication(&I, VF)) {
4893 ScalarCostsTy ScalarCosts;
4894 // Do not apply discount logic for:
4895 // 1. Scalars after vectorization, as there will only be a single copy
4896 // of the instruction.
4897 // 2. Scalable VF, as that would lead to invalid scalarization costs.
4898 // 3. Emulated masked memrefs, if a hacked cost is needed.
4899 if (!isScalarAfterVectorization(&I, VF) && !VF.isScalable() &&
4900 !useEmulatedMaskMemRefHack(&I, VF) &&
4901 computePredInstDiscount(&I, ScalarCosts, VF) >= 0) {
4902 for (const auto &[I, IC] : ScalarCosts)
4903 ScalarCostsVF.insert({I, IC});
4904 // Check if we decided to scalarize a call. If so, update the widening
4905 // decision of the call to CM_Scalarize with the computed scalar cost.
4906 for (const auto &[I, Cost] : ScalarCosts) {
4907 auto *CI = dyn_cast<CallInst>(I);
4908 if (!CI || !CallWideningDecisions.contains({CI, VF}))
4909 continue;
4910 CallWideningDecisions[{CI, VF}].Kind = CM_Scalarize;
4911 CallWideningDecisions[{CI, VF}].Cost = Cost;
4912 }
4913 }
4914 // Remember that BB will remain after vectorization.
4915 PredicatedBBsAfterVectorization[VF].insert(BB);
4916 for (auto *Pred : predecessors(BB)) {
4917 if (Pred->getSingleSuccessor() == BB)
4918 PredicatedBBsAfterVectorization[VF].insert(Pred);
4919 }
4920 }
4921 }
4922}
4923
4924InstructionCost LoopVectorizationCostModel::computePredInstDiscount(
4925 Instruction *PredInst, ScalarCostsTy &ScalarCosts, ElementCount VF) {
4926 assert(!isUniformAfterVectorization(PredInst, VF) &&
4927 "Instruction marked uniform-after-vectorization will be predicated");
4928
4929 // Initialize the discount to zero, meaning that the scalar version and the
4930 // vector version cost the same.
4931 InstructionCost Discount = 0;
4932
4933 // Holds instructions to analyze. The instructions we visit are mapped in
4934 // ScalarCosts. Those instructions are the ones that would be scalarized if
4935 // we find that the scalar version costs less.
4937
4938 // Returns true if the given instruction can be scalarized.
4939 auto CanBeScalarized = [&](Instruction *I) -> bool {
4940 // We only attempt to scalarize instructions forming a single-use chain
4941 // from the original predicated block that would otherwise be vectorized.
4942 // Although not strictly necessary, we give up on instructions we know will
4943 // already be scalar to avoid traversing chains that are unlikely to be
4944 // beneficial.
4945 if (!I->hasOneUse() || PredInst->getParent() != I->getParent() ||
4946 isScalarAfterVectorization(I, VF))
4947 return false;
4948
4949 // If the instruction is scalar with predication, it will be analyzed
4950 // separately. We ignore it within the context of PredInst.
4951 if (isScalarWithPredication(I, VF))
4952 return false;
4953
4954 // If any of the instruction's operands are uniform after vectorization,
4955 // the instruction cannot be scalarized. This prevents, for example, a
4956 // masked load from being scalarized.
4957 //
4958 // We assume we will only emit a value for lane zero of an instruction
4959 // marked uniform after vectorization, rather than VF identical values.
4960 // Thus, if we scalarize an instruction that uses a uniform, we would
4961 // create uses of values corresponding to the lanes we aren't emitting code
4962 // for. This behavior can be changed by allowing getScalarValue to clone
4963 // the lane zero values for uniforms rather than asserting.
4964 for (Use &U : I->operands())
4965 if (auto *J = dyn_cast<Instruction>(U.get()))
4966 if (isUniformAfterVectorization(J, VF))
4967 return false;
4968
4969 // Otherwise, we can scalarize the instruction.
4970 return true;
4971 };
4972
4973 // Compute the expected cost discount from scalarizing the entire expression
4974 // feeding the predicated instruction. We currently only consider expressions
4975 // that are single-use instruction chains.
4976 Worklist.push_back(PredInst);
4977 while (!Worklist.empty()) {
4978 Instruction *I = Worklist.pop_back_val();
4979
4980 // If we've already analyzed the instruction, there's nothing to do.
4981 if (ScalarCosts.contains(I))
4982 continue;
4983
4984 // Cannot scalarize fixed-order recurrence phis at the moment.
4985 if (isa<PHINode>(I) && Legal->isFixedOrderRecurrence(cast<PHINode>(I)))
4986 continue;
4987
4988 // Compute the cost of the vector instruction. Note that this cost already
4989 // includes the scalarization overhead of the predicated instruction.
4990 InstructionCost VectorCost = getInstructionCost(I, VF);
4991
4992 // Compute the cost of the scalarized instruction. This cost is the cost of
4993 // the instruction as if it wasn't if-converted and instead remained in the
4994 // predicated block. We will scale this cost by block probability after
4995 // computing the scalarization overhead.
4996 InstructionCost ScalarCost =
4997 VF.getFixedValue() * getInstructionCost(I, ElementCount::getFixed(1));
4998
4999 // Compute the scalarization overhead of needed insertelement instructions
5000 // and phi nodes.
5001 if (isScalarWithPredication(I, VF) && !I->getType()->isVoidTy()) {
5002 Type *WideTy = toVectorizedTy(I->getType(), VF);
5003 for (Type *VectorTy : getContainedTypes(WideTy)) {
5004 ScalarCost += TTI.getScalarizationOverhead(
5006 /*Insert=*/true,
5007 /*Extract=*/false, CostKind);
5008 }
5009 ScalarCost +=
5010 VF.getFixedValue() * TTI.getCFInstrCost(Instruction::PHI, CostKind);
5011 }
5012
5013 // Compute the scalarization overhead of needed extractelement
5014 // instructions. For each of the instruction's operands, if the operand can
5015 // be scalarized, add it to the worklist; otherwise, account for the
5016 // overhead.
5017 for (Use &U : I->operands())
5018 if (auto *J = dyn_cast<Instruction>(U.get())) {
5019 assert(canVectorizeTy(J->getType()) &&
5020 "Instruction has non-scalar type");
5021 if (CanBeScalarized(J))
5022 Worklist.push_back(J);
5023 else if (needsExtract(J, VF)) {
5024 Type *WideTy = toVectorizedTy(J->getType(), VF);
5025 for (Type *VectorTy : getContainedTypes(WideTy)) {
5026 ScalarCost += TTI.getScalarizationOverhead(
5027 cast<VectorType>(VectorTy),
5028 APInt::getAllOnes(VF.getFixedValue()), /*Insert*/ false,
5029 /*Extract*/ true, CostKind);
5030 }
5031 }
5032 }
5033
5034 // Scale the total scalar cost by block probability.
5035 ScalarCost /= getPredBlockCostDivisor(CostKind);
5036
5037 // Compute the discount. A non-negative discount means the vector version
5038 // of the instruction costs more, and scalarizing would be beneficial.
5039 Discount += VectorCost - ScalarCost;
5040 ScalarCosts[I] = ScalarCost;
5041 }
5042
5043 return Discount;
5044}
5045
5048
5049 // If the vector loop gets executed exactly once with the given VF, ignore the
5050 // costs of comparison and induction instructions, as they'll get simplified
5051 // away.
5052 SmallPtrSet<Instruction *, 2> ValuesToIgnoreForVF;
5053 auto TC = getSmallConstantTripCount(PSE.getSE(), TheLoop);
5054 if (TC == VF && !foldTailByMasking())
5056 ValuesToIgnoreForVF);
5057
5058 // For each block.
5059 for (BasicBlock *BB : TheLoop->blocks()) {
5060 InstructionCost BlockCost;
5061
5062 // For each instruction in the old loop.
5063 for (Instruction &I : BB->instructionsWithoutDebug()) {
5064 // Skip ignored values.
5065 if (ValuesToIgnore.count(&I) || ValuesToIgnoreForVF.count(&I) ||
5066 (VF.isVector() && VecValuesToIgnore.count(&I)))
5067 continue;
5068
5070
5071 // Check if we should override the cost.
5072 if (C.isValid() && ForceTargetInstructionCost.getNumOccurrences() > 0)
5074
5075 BlockCost += C;
5076 LLVM_DEBUG(dbgs() << "LV: Found an estimated cost of " << C << " for VF "
5077 << VF << " For instruction: " << I << '\n');
5078 }
5079
5080 // If we are vectorizing a predicated block, it will have been
5081 // if-converted. This means that the block's instructions (aside from
5082 // stores and instructions that may divide by zero) will now be
5083 // unconditionally executed. For the scalar case, we may not always execute
5084 // the predicated block, if it is an if-else block. Thus, scale the block's
5085 // cost by the probability of executing it. blockNeedsPredication from
5086 // Legal is used so as to not include all blocks in tail folded loops.
5087 if (VF.isScalar() && Legal->blockNeedsPredication(BB))
5088 BlockCost /= getPredBlockCostDivisor(CostKind);
5089
5090 Cost += BlockCost;
5091 }
5092
5093 return Cost;
5094}
5095
5096/// Gets Address Access SCEV after verifying that the access pattern
5097/// is loop invariant except the induction variable dependence.
5098///
5099/// This SCEV can be sent to the Target in order to estimate the address
5100/// calculation cost.
5102 Value *Ptr,
5105 const Loop *TheLoop) {
5106
5107 auto *Gep = dyn_cast<GetElementPtrInst>(Ptr);
5108 if (!Gep)
5109 return nullptr;
5110
5111 // We are looking for a gep with all loop invariant indices except for one
5112 // which should be an induction variable.
5113 auto *SE = PSE.getSE();
5114 unsigned NumOperands = Gep->getNumOperands();
5115 for (unsigned Idx = 1; Idx < NumOperands; ++Idx) {
5116 Value *Opd = Gep->getOperand(Idx);
5117 if (!SE->isLoopInvariant(SE->getSCEV(Opd), TheLoop) &&
5118 !Legal->isInductionVariable(Opd))
5119 return nullptr;
5120 }
5121
5122 // Now we know we have a GEP ptr, %inv, %ind, %inv. return the Ptr SCEV.
5123 return PSE.getSCEV(Ptr);
5124}
5125
5127LoopVectorizationCostModel::getMemInstScalarizationCost(Instruction *I,
5128 ElementCount VF) {
5129 assert(VF.isVector() &&
5130 "Scalarization cost of instruction implies vectorization.");
5131 if (VF.isScalable())
5132 return InstructionCost::getInvalid();
5133
5134 Type *ValTy = getLoadStoreType(I);
5135 auto *SE = PSE.getSE();
5136
5137 unsigned AS = getLoadStoreAddressSpace(I);
5139 Type *PtrTy = toVectorTy(Ptr->getType(), VF);
5140 // NOTE: PtrTy is a vector to signal `TTI::getAddressComputationCost`
5141 // that it is being called from this specific place.
5142
5143 // Figure out whether the access is strided and get the stride value
5144 // if it's known in compile time
5145 const SCEV *PtrSCEV = getAddressAccessSCEV(Ptr, Legal, PSE, TheLoop);
5146
5147 // Get the cost of the scalar memory instruction and address computation.
5149 PtrTy, SE, PtrSCEV, CostKind);
5150
5151 // Don't pass *I here, since it is scalar but will actually be part of a
5152 // vectorized loop where the user of it is a vectorized instruction.
5153 const Align Alignment = getLoadStoreAlignment(I);
5154 TTI::OperandValueInfo OpInfo = TTI::getOperandInfo(I->getOperand(0));
5155 Cost += VF.getFixedValue() *
5156 TTI.getMemoryOpCost(I->getOpcode(), ValTy->getScalarType(), Alignment,
5157 AS, CostKind, OpInfo);
5158
5159 // Get the overhead of the extractelement and insertelement instructions
5160 // we might create due to scalarization.
5162
5163 // If we have a predicated load/store, it will need extra i1 extracts and
5164 // conditional branches, but may not be executed for each vector lane. Scale
5165 // the cost by the probability of executing the predicated block.
5166 if (isPredicatedInst(I)) {
5168
5169 // Add the cost of an i1 extract and a branch
5170 auto *VecI1Ty =
5171 VectorType::get(IntegerType::getInt1Ty(ValTy->getContext()), VF);
5173 VecI1Ty, APInt::getAllOnes(VF.getFixedValue()),
5174 /*Insert=*/false, /*Extract=*/true, CostKind);
5175 Cost += TTI.getCFInstrCost(Instruction::Br, CostKind);
5176
5177 if (useEmulatedMaskMemRefHack(I, VF))
5178 // Artificially setting to a high enough value to practically disable
5179 // vectorization with such operations.
5180 Cost = 3000000;
5181 }
5182
5183 return Cost;
5184}
5185
5187LoopVectorizationCostModel::getConsecutiveMemOpCost(Instruction *I,
5188 ElementCount VF) {
5189 Type *ValTy = getLoadStoreType(I);
5190 auto *VectorTy = cast<VectorType>(toVectorTy(ValTy, VF));
5192 unsigned AS = getLoadStoreAddressSpace(I);
5193 int ConsecutiveStride = Legal->isConsecutivePtr(ValTy, Ptr);
5194
5195 assert((ConsecutiveStride == 1 || ConsecutiveStride == -1) &&
5196 "Stride should be 1 or -1 for consecutive memory access");
5197 const Align Alignment = getLoadStoreAlignment(I);
5199 if (Legal->isMaskRequired(I)) {
5200 Cost += TTI.getMaskedMemoryOpCost(I->getOpcode(), VectorTy, Alignment, AS,
5201 CostKind);
5202 } else {
5203 TTI::OperandValueInfo OpInfo = TTI::getOperandInfo(I->getOperand(0));
5204 Cost += TTI.getMemoryOpCost(I->getOpcode(), VectorTy, Alignment, AS,
5205 CostKind, OpInfo, I);
5206 }
5207
5208 bool Reverse = ConsecutiveStride < 0;
5209 if (Reverse)
5211 VectorTy, {}, CostKind, 0);
5212 return Cost;
5213}
5214
5216LoopVectorizationCostModel::getUniformMemOpCost(Instruction *I,
5217 ElementCount VF) {
5218 assert(Legal->isUniformMemOp(*I, VF));
5219
5220 Type *ValTy = getLoadStoreType(I);
5222 auto *VectorTy = cast<VectorType>(toVectorTy(ValTy, VF));
5223 const Align Alignment = getLoadStoreAlignment(I);
5224 unsigned AS = getLoadStoreAddressSpace(I);
5225 if (isa<LoadInst>(I)) {
5226 return TTI.getAddressComputationCost(PtrTy, nullptr, nullptr, CostKind) +
5227 TTI.getMemoryOpCost(Instruction::Load, ValTy, Alignment, AS,
5228 CostKind) +
5230 VectorTy, {}, CostKind);
5231 }
5232 StoreInst *SI = cast<StoreInst>(I);
5233
5234 bool IsLoopInvariantStoreValue = Legal->isInvariant(SI->getValueOperand());
5235 // TODO: We have existing tests that request the cost of extracting element
5236 // VF.getKnownMinValue() - 1 from a scalable vector. This does not represent
5237 // the actual generated code, which involves extracting the last element of
5238 // a scalable vector where the lane to extract is unknown at compile time.
5240 TTI.getAddressComputationCost(PtrTy, nullptr, nullptr, CostKind) +
5241 TTI.getMemoryOpCost(Instruction::Store, ValTy, Alignment, AS, CostKind);
5242 if (!IsLoopInvariantStoreValue)
5243 Cost += TTI.getIndexedVectorInstrCostFromEnd(Instruction::ExtractElement,
5244 VectorTy, CostKind, 0);
5245 return Cost;
5246}
5247
5249LoopVectorizationCostModel::getGatherScatterCost(Instruction *I,
5250 ElementCount VF) {
5251 Type *ValTy = getLoadStoreType(I);
5252 auto *VectorTy = cast<VectorType>(toVectorTy(ValTy, VF));
5253 const Align Alignment = getLoadStoreAlignment(I);
5255 Type *PtrTy = Ptr->getType();
5256
5257 if (!Legal->isUniform(Ptr, VF))
5258 PtrTy = toVectorTy(PtrTy, VF);
5259
5260 return TTI.getAddressComputationCost(PtrTy, nullptr, nullptr, CostKind) +
5261 TTI.getGatherScatterOpCost(I->getOpcode(), VectorTy, Ptr,
5262 Legal->isMaskRequired(I), Alignment,
5263 CostKind, I);
5264}
5265
5267LoopVectorizationCostModel::getInterleaveGroupCost(Instruction *I,
5268 ElementCount VF) {
5269 const auto *Group = getInterleavedAccessGroup(I);
5270 assert(Group && "Fail to get an interleaved access group.");
5271
5272 Instruction *InsertPos = Group->getInsertPos();
5273 Type *ValTy = getLoadStoreType(InsertPos);
5274 auto *VectorTy = cast<VectorType>(toVectorTy(ValTy, VF));
5275 unsigned AS = getLoadStoreAddressSpace(InsertPos);
5276
5277 unsigned InterleaveFactor = Group->getFactor();
5278 auto *WideVecTy = VectorType::get(ValTy, VF * InterleaveFactor);
5279
5280 // Holds the indices of existing members in the interleaved group.
5281 SmallVector<unsigned, 4> Indices;
5282 for (unsigned IF = 0; IF < InterleaveFactor; IF++)
5283 if (Group->getMember(IF))
5284 Indices.push_back(IF);
5285
5286 // Calculate the cost of the whole interleaved group.
5287 bool UseMaskForGaps =
5288 (Group->requiresScalarEpilogue() && !isScalarEpilogueAllowed()) ||
5289 (isa<StoreInst>(I) && !Group->isFull());
5291 InsertPos->getOpcode(), WideVecTy, Group->getFactor(), Indices,
5292 Group->getAlign(), AS, CostKind, Legal->isMaskRequired(I),
5293 UseMaskForGaps);
5294
5295 if (Group->isReverse()) {
5296 // TODO: Add support for reversed masked interleaved access.
5297 assert(!Legal->isMaskRequired(I) &&
5298 "Reverse masked interleaved access not supported.");
5299 Cost += Group->getNumMembers() *
5301 VectorTy, {}, CostKind, 0);
5302 }
5303 return Cost;
5304}
5305
5306std::optional<InstructionCost>
5308 ElementCount VF,
5309 Type *Ty) const {
5310 using namespace llvm::PatternMatch;
5311 // Early exit for no inloop reductions
5312 if (InLoopReductions.empty() || VF.isScalar() || !isa<VectorType>(Ty))
5313 return std::nullopt;
5314 auto *VectorTy = cast<VectorType>(Ty);
5315
5316 // We are looking for a pattern of, and finding the minimal acceptable cost:
5317 // reduce(mul(ext(A), ext(B))) or
5318 // reduce(mul(A, B)) or
5319 // reduce(ext(A)) or
5320 // reduce(A).
5321 // The basic idea is that we walk down the tree to do that, finding the root
5322 // reduction instruction in InLoopReductionImmediateChains. From there we find
5323 // the pattern of mul/ext and test the cost of the entire pattern vs the cost
5324 // of the components. If the reduction cost is lower then we return it for the
5325 // reduction instruction and 0 for the other instructions in the pattern. If
5326 // it is not we return an invalid cost specifying the orignal cost method
5327 // should be used.
5328 Instruction *RetI = I;
5329 if (match(RetI, m_ZExtOrSExt(m_Value()))) {
5330 if (!RetI->hasOneUser())
5331 return std::nullopt;
5332 RetI = RetI->user_back();
5333 }
5334
5335 if (match(RetI, m_OneUse(m_Mul(m_Value(), m_Value()))) &&
5336 RetI->user_back()->getOpcode() == Instruction::Add) {
5337 RetI = RetI->user_back();
5338 }
5339
5340 // Test if the found instruction is a reduction, and if not return an invalid
5341 // cost specifying the parent to use the original cost modelling.
5342 Instruction *LastChain = InLoopReductionImmediateChains.lookup(RetI);
5343 if (!LastChain)
5344 return std::nullopt;
5345
5346 // Find the reduction this chain is a part of and calculate the basic cost of
5347 // the reduction on its own.
5348 Instruction *ReductionPhi = LastChain;
5349 while (!isa<PHINode>(ReductionPhi))
5350 ReductionPhi = InLoopReductionImmediateChains.at(ReductionPhi);
5351
5352 const RecurrenceDescriptor &RdxDesc =
5353 Legal->getRecurrenceDescriptor(cast<PHINode>(ReductionPhi));
5354
5355 InstructionCost BaseCost;
5356 RecurKind RK = RdxDesc.getRecurrenceKind();
5359 BaseCost = TTI.getMinMaxReductionCost(MinMaxID, VectorTy,
5360 RdxDesc.getFastMathFlags(), CostKind);
5361 } else {
5362 BaseCost = TTI.getArithmeticReductionCost(
5363 RdxDesc.getOpcode(), VectorTy, RdxDesc.getFastMathFlags(), CostKind);
5364 }
5365
5366 // For a call to the llvm.fmuladd intrinsic we need to add the cost of a
5367 // normal fmul instruction to the cost of the fadd reduction.
5368 if (RK == RecurKind::FMulAdd)
5369 BaseCost +=
5370 TTI.getArithmeticInstrCost(Instruction::FMul, VectorTy, CostKind);
5371
5372 // If we're using ordered reductions then we can just return the base cost
5373 // here, since getArithmeticReductionCost calculates the full ordered
5374 // reduction cost when FP reassociation is not allowed.
5375 if (useOrderedReductions(RdxDesc))
5376 return BaseCost;
5377
5378 // Get the operand that was not the reduction chain and match it to one of the
5379 // patterns, returning the better cost if it is found.
5380 Instruction *RedOp = RetI->getOperand(1) == LastChain
5383
5384 VectorTy = VectorType::get(I->getOperand(0)->getType(), VectorTy);
5385
5386 Instruction *Op0, *Op1;
5387 if (RedOp && RdxDesc.getOpcode() == Instruction::Add &&
5388 match(RedOp,
5390 match(Op0, m_ZExtOrSExt(m_Value())) &&
5391 Op0->getOpcode() == Op1->getOpcode() &&
5392 Op0->getOperand(0)->getType() == Op1->getOperand(0)->getType() &&
5393 !TheLoop->isLoopInvariant(Op0) && !TheLoop->isLoopInvariant(Op1) &&
5394 (Op0->getOpcode() == RedOp->getOpcode() || Op0 == Op1)) {
5395
5396 // Matched reduce.add(ext(mul(ext(A), ext(B)))
5397 // Note that the extend opcodes need to all match, or if A==B they will have
5398 // been converted to zext(mul(sext(A), sext(A))) as it is known positive,
5399 // which is equally fine.
5400 bool IsUnsigned = isa<ZExtInst>(Op0);
5401 auto *ExtType = VectorType::get(Op0->getOperand(0)->getType(), VectorTy);
5402 auto *MulType = VectorType::get(Op0->getType(), VectorTy);
5403
5404 InstructionCost ExtCost =
5405 TTI.getCastInstrCost(Op0->getOpcode(), MulType, ExtType,
5407 InstructionCost MulCost =
5408 TTI.getArithmeticInstrCost(Instruction::Mul, MulType, CostKind);
5409 InstructionCost Ext2Cost =
5410 TTI.getCastInstrCost(RedOp->getOpcode(), VectorTy, MulType,
5412
5413 InstructionCost RedCost = TTI.getMulAccReductionCost(
5414 IsUnsigned, RdxDesc.getOpcode(), RdxDesc.getRecurrenceType(), ExtType,
5415 CostKind);
5416
5417 if (RedCost.isValid() &&
5418 RedCost < ExtCost * 2 + MulCost + Ext2Cost + BaseCost)
5419 return I == RetI ? RedCost : 0;
5420 } else if (RedOp && match(RedOp, m_ZExtOrSExt(m_Value())) &&
5421 !TheLoop->isLoopInvariant(RedOp)) {
5422 // Matched reduce(ext(A))
5423 bool IsUnsigned = isa<ZExtInst>(RedOp);
5424 auto *ExtType = VectorType::get(RedOp->getOperand(0)->getType(), VectorTy);
5425 InstructionCost RedCost = TTI.getExtendedReductionCost(
5426 RdxDesc.getOpcode(), IsUnsigned, RdxDesc.getRecurrenceType(), ExtType,
5427 RdxDesc.getFastMathFlags(), CostKind);
5428
5429 InstructionCost ExtCost =
5430 TTI.getCastInstrCost(RedOp->getOpcode(), VectorTy, ExtType,
5432 if (RedCost.isValid() && RedCost < BaseCost + ExtCost)
5433 return I == RetI ? RedCost : 0;
5434 } else if (RedOp && RdxDesc.getOpcode() == Instruction::Add &&
5435 match(RedOp, m_Mul(m_Instruction(Op0), m_Instruction(Op1)))) {
5436 if (match(Op0, m_ZExtOrSExt(m_Value())) &&
5437 Op0->getOpcode() == Op1->getOpcode() &&
5438 !TheLoop->isLoopInvariant(Op0) && !TheLoop->isLoopInvariant(Op1)) {
5439 bool IsUnsigned = isa<ZExtInst>(Op0);
5440 Type *Op0Ty = Op0->getOperand(0)->getType();
5441 Type *Op1Ty = Op1->getOperand(0)->getType();
5442 Type *LargestOpTy =
5443 Op0Ty->getIntegerBitWidth() < Op1Ty->getIntegerBitWidth() ? Op1Ty
5444 : Op0Ty;
5445 auto *ExtType = VectorType::get(LargestOpTy, VectorTy);
5446
5447 // Matched reduce.add(mul(ext(A), ext(B))), where the two ext may be of
5448 // different sizes. We take the largest type as the ext to reduce, and add
5449 // the remaining cost as, for example reduce(mul(ext(ext(A)), ext(B))).
5450 InstructionCost ExtCost0 = TTI.getCastInstrCost(
5451 Op0->getOpcode(), VectorTy, VectorType::get(Op0Ty, VectorTy),
5453 InstructionCost ExtCost1 = TTI.getCastInstrCost(
5454 Op1->getOpcode(), VectorTy, VectorType::get(Op1Ty, VectorTy),
5456 InstructionCost MulCost =
5457 TTI.getArithmeticInstrCost(Instruction::Mul, VectorTy, CostKind);
5458
5459 InstructionCost RedCost = TTI.getMulAccReductionCost(
5460 IsUnsigned, RdxDesc.getOpcode(), RdxDesc.getRecurrenceType(), ExtType,
5461 CostKind);
5462 InstructionCost ExtraExtCost = 0;
5463 if (Op0Ty != LargestOpTy || Op1Ty != LargestOpTy) {
5464 Instruction *ExtraExtOp = (Op0Ty != LargestOpTy) ? Op0 : Op1;
5465 ExtraExtCost = TTI.getCastInstrCost(
5466 ExtraExtOp->getOpcode(), ExtType,
5467 VectorType::get(ExtraExtOp->getOperand(0)->getType(), VectorTy),
5469 }
5470
5471 if (RedCost.isValid() &&
5472 (RedCost + ExtraExtCost) < (ExtCost0 + ExtCost1 + MulCost + BaseCost))
5473 return I == RetI ? RedCost : 0;
5474 } else if (!match(I, m_ZExtOrSExt(m_Value()))) {
5475 // Matched reduce.add(mul())
5476 InstructionCost MulCost =
5477 TTI.getArithmeticInstrCost(Instruction::Mul, VectorTy, CostKind);
5478
5479 InstructionCost RedCost = TTI.getMulAccReductionCost(
5480 true, RdxDesc.getOpcode(), RdxDesc.getRecurrenceType(), VectorTy,
5481 CostKind);
5482
5483 if (RedCost.isValid() && RedCost < MulCost + BaseCost)
5484 return I == RetI ? RedCost : 0;
5485 }
5486 }
5487
5488 return I == RetI ? std::optional<InstructionCost>(BaseCost) : std::nullopt;
5489}
5490
5492LoopVectorizationCostModel::getMemoryInstructionCost(Instruction *I,
5493 ElementCount VF) {
5494 // Calculate scalar cost only. Vectorization cost should be ready at this
5495 // moment.
5496 if (VF.isScalar()) {
5497 Type *ValTy = getLoadStoreType(I);
5499 const Align Alignment = getLoadStoreAlignment(I);
5500 unsigned AS = getLoadStoreAddressSpace(I);
5501
5502 TTI::OperandValueInfo OpInfo = TTI::getOperandInfo(I->getOperand(0));
5503 return TTI.getAddressComputationCost(PtrTy, nullptr, nullptr, CostKind) +
5504 TTI.getMemoryOpCost(I->getOpcode(), ValTy, Alignment, AS, CostKind,
5505 OpInfo, I);
5506 }
5507 return getWideningCost(I, VF);
5508}
5509
5511LoopVectorizationCostModel::getScalarizationOverhead(Instruction *I,
5512 ElementCount VF) const {
5513
5514 // There is no mechanism yet to create a scalable scalarization loop,
5515 // so this is currently Invalid.
5516 if (VF.isScalable())
5517 return InstructionCost::getInvalid();
5518
5519 if (VF.isScalar())
5520 return 0;
5521
5523 Type *RetTy = toVectorizedTy(I->getType(), VF);
5524 if (!RetTy->isVoidTy() &&
5526
5527 for (Type *VectorTy : getContainedTypes(RetTy)) {
5530 /*Insert=*/true,
5531 /*Extract=*/false, CostKind);
5532 }
5533 }
5534
5535 // Some targets keep addresses scalar.
5537 return Cost;
5538
5539 // Some targets support efficient element stores.
5541 return Cost;
5542
5543 // Collect operands to consider.
5544 CallInst *CI = dyn_cast<CallInst>(I);
5545 Instruction::op_range Ops = CI ? CI->args() : I->operands();
5546
5547 // Skip operands that do not require extraction/scalarization and do not incur
5548 // any overhead.
5550 for (auto *V : filterExtractingOperands(Ops, VF))
5551 Tys.push_back(maybeVectorizeType(V->getType(), VF));
5553}
5554
5556 if (VF.isScalar())
5557 return;
5558 NumPredStores = 0;
5559 for (BasicBlock *BB : TheLoop->blocks()) {
5560 // For each instruction in the old loop.
5561 for (Instruction &I : *BB) {
5563 if (!Ptr)
5564 continue;
5565
5566 // TODO: We should generate better code and update the cost model for
5567 // predicated uniform stores. Today they are treated as any other
5568 // predicated store (see added test cases in
5569 // invariant-store-vectorization.ll).
5571 NumPredStores++;
5572
5573 if (Legal->isUniformMemOp(I, VF)) {
5574 auto IsLegalToScalarize = [&]() {
5575 if (!VF.isScalable())
5576 // Scalarization of fixed length vectors "just works".
5577 return true;
5578
5579 // We have dedicated lowering for unpredicated uniform loads and
5580 // stores. Note that even with tail folding we know that at least
5581 // one lane is active (i.e. generalized predication is not possible
5582 // here), and the logic below depends on this fact.
5583 if (!foldTailByMasking())
5584 return true;
5585
5586 // For scalable vectors, a uniform memop load is always
5587 // uniform-by-parts and we know how to scalarize that.
5588 if (isa<LoadInst>(I))
5589 return true;
5590
5591 // A uniform store isn't neccessarily uniform-by-part
5592 // and we can't assume scalarization.
5593 auto &SI = cast<StoreInst>(I);
5594 return TheLoop->isLoopInvariant(SI.getValueOperand());
5595 };
5596
5597 const InstructionCost GatherScatterCost =
5599 getGatherScatterCost(&I, VF) : InstructionCost::getInvalid();
5600
5601 // Load: Scalar load + broadcast
5602 // Store: Scalar store + isLoopInvariantStoreValue ? 0 : extract
5603 // FIXME: This cost is a significant under-estimate for tail folded
5604 // memory ops.
5605 const InstructionCost ScalarizationCost =
5606 IsLegalToScalarize() ? getUniformMemOpCost(&I, VF)
5608
5609 // Choose better solution for the current VF, Note that Invalid
5610 // costs compare as maximumal large. If both are invalid, we get
5611 // scalable invalid which signals a failure and a vectorization abort.
5612 if (GatherScatterCost < ScalarizationCost)
5613 setWideningDecision(&I, VF, CM_GatherScatter, GatherScatterCost);
5614 else
5615 setWideningDecision(&I, VF, CM_Scalarize, ScalarizationCost);
5616 continue;
5617 }
5618
5619 // We assume that widening is the best solution when possible.
5620 if (memoryInstructionCanBeWidened(&I, VF)) {
5621 InstructionCost Cost = getConsecutiveMemOpCost(&I, VF);
5622 int ConsecutiveStride = Legal->isConsecutivePtr(
5624 assert((ConsecutiveStride == 1 || ConsecutiveStride == -1) &&
5625 "Expected consecutive stride.");
5626 InstWidening Decision =
5627 ConsecutiveStride == 1 ? CM_Widen : CM_Widen_Reverse;
5628 setWideningDecision(&I, VF, Decision, Cost);
5629 continue;
5630 }
5631
5632 // Choose between Interleaving, Gather/Scatter or Scalarization.
5634 unsigned NumAccesses = 1;
5635 if (isAccessInterleaved(&I)) {
5636 const auto *Group = getInterleavedAccessGroup(&I);
5637 assert(Group && "Fail to get an interleaved access group.");
5638
5639 // Make one decision for the whole group.
5640 if (getWideningDecision(&I, VF) != CM_Unknown)
5641 continue;
5642
5643 NumAccesses = Group->getNumMembers();
5645 InterleaveCost = getInterleaveGroupCost(&I, VF);
5646 }
5647
5648 InstructionCost GatherScatterCost =
5650 ? getGatherScatterCost(&I, VF) * NumAccesses
5652
5653 InstructionCost ScalarizationCost =
5654 getMemInstScalarizationCost(&I, VF) * NumAccesses;
5655
5656 // Choose better solution for the current VF,
5657 // write down this decision and use it during vectorization.
5659 InstWidening Decision;
5660 if (InterleaveCost <= GatherScatterCost &&
5661 InterleaveCost < ScalarizationCost) {
5662 Decision = CM_Interleave;
5663 Cost = InterleaveCost;
5664 } else if (GatherScatterCost < ScalarizationCost) {
5665 Decision = CM_GatherScatter;
5666 Cost = GatherScatterCost;
5667 } else {
5668 Decision = CM_Scalarize;
5669 Cost = ScalarizationCost;
5670 }
5671 // If the instructions belongs to an interleave group, the whole group
5672 // receives the same decision. The whole group receives the cost, but
5673 // the cost will actually be assigned to one instruction.
5674 if (const auto *Group = getInterleavedAccessGroup(&I)) {
5675 if (Decision == CM_Scalarize) {
5676 for (unsigned Idx = 0; Idx < Group->getFactor(); ++Idx) {
5677 if (auto *I = Group->getMember(Idx)) {
5678 setWideningDecision(I, VF, Decision,
5679 getMemInstScalarizationCost(I, VF));
5680 }
5681 }
5682 } else {
5683 setWideningDecision(Group, VF, Decision, Cost);
5684 }
5685 } else
5686 setWideningDecision(&I, VF, Decision, Cost);
5687 }
5688 }
5689
5690 // Make sure that any load of address and any other address computation
5691 // remains scalar unless there is gather/scatter support. This avoids
5692 // inevitable extracts into address registers, and also has the benefit of
5693 // activating LSR more, since that pass can't optimize vectorized
5694 // addresses.
5695 if (TTI.prefersVectorizedAddressing())
5696 return;
5697
5698 // Start with all scalar pointer uses.
5700 for (BasicBlock *BB : TheLoop->blocks())
5701 for (Instruction &I : *BB) {
5702 Instruction *PtrDef =
5704 if (PtrDef && TheLoop->contains(PtrDef) &&
5706 AddrDefs.insert(PtrDef);
5707 }
5708
5709 // Add all instructions used to generate the addresses.
5711 append_range(Worklist, AddrDefs);
5712 while (!Worklist.empty()) {
5713 Instruction *I = Worklist.pop_back_val();
5714 for (auto &Op : I->operands())
5715 if (auto *InstOp = dyn_cast<Instruction>(Op))
5716 if (TheLoop->contains(InstOp) && !isa<PHINode>(InstOp) &&
5717 AddrDefs.insert(InstOp).second)
5718 Worklist.push_back(InstOp);
5719 }
5720
5721 auto UpdateMemOpUserCost = [this, VF](LoadInst *LI) {
5722 // If there are direct memory op users of the newly scalarized load,
5723 // their cost may have changed because there's no scalarization
5724 // overhead for the operand. Update it.
5725 for (User *U : LI->users()) {
5727 continue;
5729 continue;
5732 getMemInstScalarizationCost(cast<Instruction>(U), VF));
5733 }
5734 };
5735 for (auto *I : AddrDefs) {
5736 if (isa<LoadInst>(I)) {
5737 // Setting the desired widening decision should ideally be handled in
5738 // by cost functions, but since this involves the task of finding out
5739 // if the loaded register is involved in an address computation, it is
5740 // instead changed here when we know this is the case.
5741 InstWidening Decision = getWideningDecision(I, VF);
5742 if (Decision == CM_Widen || Decision == CM_Widen_Reverse ||
5743 (!isPredicatedInst(I) && !Legal->isUniformMemOp(*I, VF) &&
5744 Decision == CM_Scalarize)) {
5745 // Scalarize a widened load of address or update the cost of a scalar
5746 // load of an address.
5748 I, VF, CM_Scalarize,
5749 (VF.getKnownMinValue() *
5750 getMemoryInstructionCost(I, ElementCount::getFixed(1))));
5751 UpdateMemOpUserCost(cast<LoadInst>(I));
5752 } else if (const auto *Group = getInterleavedAccessGroup(I)) {
5753 // Scalarize all members of this interleaved group when any member
5754 // is used as an address. The address-used load skips scalarization
5755 // overhead, other members include it.
5756 for (unsigned Idx = 0; Idx < Group->getFactor(); ++Idx) {
5757 if (Instruction *Member = Group->getMember(Idx)) {
5759 AddrDefs.contains(Member)
5760 ? (VF.getKnownMinValue() *
5761 getMemoryInstructionCost(Member,
5763 : getMemInstScalarizationCost(Member, VF);
5765 UpdateMemOpUserCost(cast<LoadInst>(Member));
5766 }
5767 }
5768 }
5769 } else {
5770 // Cannot scalarize fixed-order recurrence phis at the moment.
5771 if (isa<PHINode>(I) && Legal->isFixedOrderRecurrence(cast<PHINode>(I)))
5772 continue;
5773
5774 // Make sure I gets scalarized and a cost estimate without
5775 // scalarization overhead.
5776 ForcedScalars[VF].insert(I);
5777 }
5778 }
5779}
5780
5782 assert(!VF.isScalar() &&
5783 "Trying to set a vectorization decision for a scalar VF");
5784
5785 auto ForcedScalar = ForcedScalars.find(VF);
5786 for (BasicBlock *BB : TheLoop->blocks()) {
5787 // For each instruction in the old loop.
5788 for (Instruction &I : *BB) {
5790
5791 if (!CI)
5792 continue;
5793
5797 Function *ScalarFunc = CI->getCalledFunction();
5798 Type *ScalarRetTy = CI->getType();
5799 SmallVector<Type *, 4> Tys, ScalarTys;
5800 for (auto &ArgOp : CI->args())
5801 ScalarTys.push_back(ArgOp->getType());
5802
5803 // Estimate cost of scalarized vector call. The source operands are
5804 // assumed to be vectors, so we need to extract individual elements from
5805 // there, execute VF scalar calls, and then gather the result into the
5806 // vector return value.
5807 if (VF.isFixed()) {
5808 InstructionCost ScalarCallCost =
5809 TTI.getCallInstrCost(ScalarFunc, ScalarRetTy, ScalarTys, CostKind);
5810
5811 // Compute costs of unpacking argument values for the scalar calls and
5812 // packing the return values to a vector.
5813 InstructionCost ScalarizationCost = getScalarizationOverhead(CI, VF);
5814 ScalarCost = ScalarCallCost * VF.getKnownMinValue() + ScalarizationCost;
5815 } else {
5816 // There is no point attempting to calculate the scalar cost for a
5817 // scalable VF as we know it will be Invalid.
5819 "Unexpected valid cost for scalarizing scalable vectors");
5820 ScalarCost = InstructionCost::getInvalid();
5821 }
5822
5823 // Honor ForcedScalars and UniformAfterVectorization decisions.
5824 // TODO: For calls, it might still be more profitable to widen. Use
5825 // VPlan-based cost model to compare different options.
5826 if (VF.isVector() && ((ForcedScalar != ForcedScalars.end() &&
5827 ForcedScalar->second.contains(CI)) ||
5828 isUniformAfterVectorization(CI, VF))) {
5829 setCallWideningDecision(CI, VF, CM_Scalarize, nullptr,
5830 Intrinsic::not_intrinsic, std::nullopt,
5831 ScalarCost);
5832 continue;
5833 }
5834
5835 bool MaskRequired = Legal->isMaskRequired(CI);
5836 // Compute corresponding vector type for return value and arguments.
5837 Type *RetTy = toVectorizedTy(ScalarRetTy, VF);
5838 for (Type *ScalarTy : ScalarTys)
5839 Tys.push_back(toVectorizedTy(ScalarTy, VF));
5840
5841 // An in-loop reduction using an fmuladd intrinsic is a special case;
5842 // we don't want the normal cost for that intrinsic.
5844 if (auto RedCost = getReductionPatternCost(CI, VF, RetTy)) {
5847 std::nullopt, *RedCost);
5848 continue;
5849 }
5850
5851 // Find the cost of vectorizing the call, if we can find a suitable
5852 // vector variant of the function.
5853 VFInfo FuncInfo;
5854 Function *VecFunc = nullptr;
5855 // Search through any available variants for one we can use at this VF.
5856 for (VFInfo &Info : VFDatabase::getMappings(*CI)) {
5857 // Must match requested VF.
5858 if (Info.Shape.VF != VF)
5859 continue;
5860
5861 // Must take a mask argument if one is required
5862 if (MaskRequired && !Info.isMasked())
5863 continue;
5864
5865 // Check that all parameter kinds are supported
5866 bool ParamsOk = true;
5867 for (VFParameter Param : Info.Shape.Parameters) {
5868 switch (Param.ParamKind) {
5870 break;
5872 Value *ScalarParam = CI->getArgOperand(Param.ParamPos);
5873 // Make sure the scalar parameter in the loop is invariant.
5874 if (!PSE.getSE()->isLoopInvariant(PSE.getSCEV(ScalarParam),
5875 TheLoop))
5876 ParamsOk = false;
5877 break;
5878 }
5880 Value *ScalarParam = CI->getArgOperand(Param.ParamPos);
5881 // Find the stride for the scalar parameter in this loop and see if
5882 // it matches the stride for the variant.
5883 // TODO: do we need to figure out the cost of an extract to get the
5884 // first lane? Or do we hope that it will be folded away?
5885 ScalarEvolution *SE = PSE.getSE();
5886 if (!match(SE->getSCEV(ScalarParam),
5888 m_SCEV(), m_scev_SpecificSInt(Param.LinearStepOrPos),
5890 ParamsOk = false;
5891 break;
5892 }
5894 break;
5895 default:
5896 ParamsOk = false;
5897 break;
5898 }
5899 }
5900
5901 if (!ParamsOk)
5902 continue;
5903
5904 // Found a suitable candidate, stop here.
5905 VecFunc = CI->getModule()->getFunction(Info.VectorName);
5906 FuncInfo = Info;
5907 break;
5908 }
5909
5910 if (TLI && VecFunc && !CI->isNoBuiltin())
5911 VectorCost = TTI.getCallInstrCost(nullptr, RetTy, Tys, CostKind);
5912
5913 // Find the cost of an intrinsic; some targets may have instructions that
5914 // perform the operation without needing an actual call.
5916 if (IID != Intrinsic::not_intrinsic)
5918
5919 InstructionCost Cost = ScalarCost;
5920 InstWidening Decision = CM_Scalarize;
5921
5922 if (VectorCost <= Cost) {
5923 Cost = VectorCost;
5924 Decision = CM_VectorCall;
5925 }
5926
5927 if (IntrinsicCost <= Cost) {
5929 Decision = CM_IntrinsicCall;
5930 }
5931
5932 setCallWideningDecision(CI, VF, Decision, VecFunc, IID,
5934 }
5935 }
5936}
5937
5939 if (!Legal->isInvariant(Op))
5940 return false;
5941 // Consider Op invariant, if it or its operands aren't predicated
5942 // instruction in the loop. In that case, it is not trivially hoistable.
5943 auto *OpI = dyn_cast<Instruction>(Op);
5944 return !OpI || !TheLoop->contains(OpI) ||
5945 (!isPredicatedInst(OpI) &&
5946 (!isa<PHINode>(OpI) || OpI->getParent() != TheLoop->getHeader()) &&
5947 all_of(OpI->operands(),
5948 [this](Value *Op) { return shouldConsiderInvariant(Op); }));
5949}
5950
5953 ElementCount VF) {
5954 // If we know that this instruction will remain uniform, check the cost of
5955 // the scalar version.
5957 VF = ElementCount::getFixed(1);
5958
5959 if (VF.isVector() && isProfitableToScalarize(I, VF))
5960 return InstsToScalarize[VF][I];
5961
5962 // Forced scalars do not have any scalarization overhead.
5963 auto ForcedScalar = ForcedScalars.find(VF);
5964 if (VF.isVector() && ForcedScalar != ForcedScalars.end()) {
5965 auto InstSet = ForcedScalar->second;
5966 if (InstSet.count(I))
5968 VF.getKnownMinValue();
5969 }
5970
5971 Type *RetTy = I->getType();
5973 RetTy = IntegerType::get(RetTy->getContext(), MinBWs[I]);
5974 auto *SE = PSE.getSE();
5975
5976 Type *VectorTy;
5977 if (isScalarAfterVectorization(I, VF)) {
5978 [[maybe_unused]] auto HasSingleCopyAfterVectorization =
5979 [this](Instruction *I, ElementCount VF) -> bool {
5980 if (VF.isScalar())
5981 return true;
5982
5983 auto Scalarized = InstsToScalarize.find(VF);
5984 assert(Scalarized != InstsToScalarize.end() &&
5985 "VF not yet analyzed for scalarization profitability");
5986 return !Scalarized->second.count(I) &&
5987 llvm::all_of(I->users(), [&](User *U) {
5988 auto *UI = cast<Instruction>(U);
5989 return !Scalarized->second.count(UI);
5990 });
5991 };
5992
5993 // With the exception of GEPs and PHIs, after scalarization there should
5994 // only be one copy of the instruction generated in the loop. This is
5995 // because the VF is either 1, or any instructions that need scalarizing
5996 // have already been dealt with by the time we get here. As a result,
5997 // it means we don't have to multiply the instruction cost by VF.
5998 assert(I->getOpcode() == Instruction::GetElementPtr ||
5999 I->getOpcode() == Instruction::PHI ||
6000 (I->getOpcode() == Instruction::BitCast &&
6001 I->getType()->isPointerTy()) ||
6002 HasSingleCopyAfterVectorization(I, VF));
6003 VectorTy = RetTy;
6004 } else
6005 VectorTy = toVectorizedTy(RetTy, VF);
6006
6007 if (VF.isVector() && VectorTy->isVectorTy() &&
6008 !TTI.getNumberOfParts(VectorTy))
6010
6011 // TODO: We need to estimate the cost of intrinsic calls.
6012 switch (I->getOpcode()) {
6013 case Instruction::GetElementPtr:
6014 // We mark this instruction as zero-cost because the cost of GEPs in
6015 // vectorized code depends on whether the corresponding memory instruction
6016 // is scalarized or not. Therefore, we handle GEPs with the memory
6017 // instruction cost.
6018 return 0;
6019 case Instruction::Br: {
6020 // In cases of scalarized and predicated instructions, there will be VF
6021 // predicated blocks in the vectorized loop. Each branch around these
6022 // blocks requires also an extract of its vector compare i1 element.
6023 // Note that the conditional branch from the loop latch will be replaced by
6024 // a single branch controlling the loop, so there is no extra overhead from
6025 // scalarization.
6026 bool ScalarPredicatedBB = false;
6028 if (VF.isVector() && BI->isConditional() &&
6029 (PredicatedBBsAfterVectorization[VF].count(BI->getSuccessor(0)) ||
6030 PredicatedBBsAfterVectorization[VF].count(BI->getSuccessor(1))) &&
6031 BI->getParent() != TheLoop->getLoopLatch())
6032 ScalarPredicatedBB = true;
6033
6034 if (ScalarPredicatedBB) {
6035 // Not possible to scalarize scalable vector with predicated instructions.
6036 if (VF.isScalable())
6038 // Return cost for branches around scalarized and predicated blocks.
6039 auto *VecI1Ty =
6041 return (
6042 TTI.getScalarizationOverhead(
6043 VecI1Ty, APInt::getAllOnes(VF.getFixedValue()),
6044 /*Insert*/ false, /*Extract*/ true, CostKind) +
6045 (TTI.getCFInstrCost(Instruction::Br, CostKind) * VF.getFixedValue()));
6046 }
6047
6048 if (I->getParent() == TheLoop->getLoopLatch() || VF.isScalar())
6049 // The back-edge branch will remain, as will all scalar branches.
6050 return TTI.getCFInstrCost(Instruction::Br, CostKind);
6051
6052 // This branch will be eliminated by if-conversion.
6053 return 0;
6054 // Note: We currently assume zero cost for an unconditional branch inside
6055 // a predicated block since it will become a fall-through, although we
6056 // may decide in the future to call TTI for all branches.
6057 }
6058 case Instruction::Switch: {
6059 if (VF.isScalar())
6060 return TTI.getCFInstrCost(Instruction::Switch, CostKind);
6061 auto *Switch = cast<SwitchInst>(I);
6062 return Switch->getNumCases() *
6063 TTI.getCmpSelInstrCost(
6064 Instruction::ICmp,
6065 toVectorTy(Switch->getCondition()->getType(), VF),
6066 toVectorTy(Type::getInt1Ty(I->getContext()), VF),
6068 }
6069 case Instruction::PHI: {
6070 auto *Phi = cast<PHINode>(I);
6071
6072 // First-order recurrences are replaced by vector shuffles inside the loop.
6073 if (VF.isVector() && Legal->isFixedOrderRecurrence(Phi)) {
6075 std::iota(Mask.begin(), Mask.end(), VF.getKnownMinValue() - 1);
6076 return TTI.getShuffleCost(TargetTransformInfo::SK_Splice,
6077 cast<VectorType>(VectorTy),
6078 cast<VectorType>(VectorTy), Mask, CostKind,
6079 VF.getKnownMinValue() - 1);
6080 }
6081
6082 // Phi nodes in non-header blocks (not inductions, reductions, etc.) are
6083 // converted into select instructions. We require N - 1 selects per phi
6084 // node, where N is the number of incoming values.
6085 if (VF.isVector() && Phi->getParent() != TheLoop->getHeader()) {
6086 Type *ResultTy = Phi->getType();
6087
6088 // All instructions in an Any-of reduction chain are narrowed to bool.
6089 // Check if that is the case for this phi node.
6090 auto *HeaderUser = cast_if_present<PHINode>(
6091 find_singleton<User>(Phi->users(), [this](User *U, bool) -> User * {
6092 auto *Phi = dyn_cast<PHINode>(U);
6093 if (Phi && Phi->getParent() == TheLoop->getHeader())
6094 return Phi;
6095 return nullptr;
6096 }));
6097 if (HeaderUser) {
6098 auto &ReductionVars = Legal->getReductionVars();
6099 auto Iter = ReductionVars.find(HeaderUser);
6100 if (Iter != ReductionVars.end() &&
6102 Iter->second.getRecurrenceKind()))
6103 ResultTy = Type::getInt1Ty(Phi->getContext());
6104 }
6105 return (Phi->getNumIncomingValues() - 1) *
6106 TTI.getCmpSelInstrCost(
6107 Instruction::Select, toVectorTy(ResultTy, VF),
6108 toVectorTy(Type::getInt1Ty(Phi->getContext()), VF),
6110 }
6111
6112 // When tail folding with EVL, if the phi is part of an out of loop
6113 // reduction then it will be transformed into a wide vp_merge.
6114 if (VF.isVector() && foldTailWithEVL() &&
6115 Legal->getReductionVars().contains(Phi) && !isInLoopReduction(Phi)) {
6117 Intrinsic::vp_merge, toVectorTy(Phi->getType(), VF),
6118 {toVectorTy(Type::getInt1Ty(Phi->getContext()), VF)});
6119 return TTI.getIntrinsicInstrCost(ICA, CostKind);
6120 }
6121
6122 return TTI.getCFInstrCost(Instruction::PHI, CostKind);
6123 }
6124 case Instruction::UDiv:
6125 case Instruction::SDiv:
6126 case Instruction::URem:
6127 case Instruction::SRem:
6128 if (VF.isVector() && isPredicatedInst(I)) {
6129 const auto [ScalarCost, SafeDivisorCost] = getDivRemSpeculationCost(I, VF);
6130 return isDivRemScalarWithPredication(ScalarCost, SafeDivisorCost) ?
6131 ScalarCost : SafeDivisorCost;
6132 }
6133 // We've proven all lanes safe to speculate, fall through.
6134 [[fallthrough]];
6135 case Instruction::Add:
6136 case Instruction::Sub: {
6137 auto Info = Legal->getHistogramInfo(I);
6138 if (Info && VF.isVector()) {
6139 const HistogramInfo *HGram = Info.value();
6140 // Assume that a non-constant update value (or a constant != 1) requires
6141 // a multiply, and add that into the cost.
6143 ConstantInt *RHS = dyn_cast<ConstantInt>(I->getOperand(1));
6144 if (!RHS || RHS->getZExtValue() != 1)
6145 MulCost =
6146 TTI.getArithmeticInstrCost(Instruction::Mul, VectorTy, CostKind);
6147
6148 // Find the cost of the histogram operation itself.
6149 Type *PtrTy = VectorType::get(HGram->Load->getPointerOperandType(), VF);
6150 Type *ScalarTy = I->getType();
6151 Type *MaskTy = VectorType::get(Type::getInt1Ty(I->getContext()), VF);
6152 IntrinsicCostAttributes ICA(Intrinsic::experimental_vector_histogram_add,
6153 Type::getVoidTy(I->getContext()),
6154 {PtrTy, ScalarTy, MaskTy});
6155
6156 // Add the costs together with the add/sub operation.
6157 return TTI.getIntrinsicInstrCost(ICA, CostKind) + MulCost +
6158 TTI.getArithmeticInstrCost(I->getOpcode(), VectorTy, CostKind);
6159 }
6160 [[fallthrough]];
6161 }
6162 case Instruction::FAdd:
6163 case Instruction::FSub:
6164 case Instruction::Mul:
6165 case Instruction::FMul:
6166 case Instruction::FDiv:
6167 case Instruction::FRem:
6168 case Instruction::Shl:
6169 case Instruction::LShr:
6170 case Instruction::AShr:
6171 case Instruction::And:
6172 case Instruction::Or:
6173 case Instruction::Xor: {
6174 // If we're speculating on the stride being 1, the multiplication may
6175 // fold away. We can generalize this for all operations using the notion
6176 // of neutral elements. (TODO)
6177 if (I->getOpcode() == Instruction::Mul &&
6178 ((TheLoop->isLoopInvariant(I->getOperand(0)) &&
6179 PSE.getSCEV(I->getOperand(0))->isOne()) ||
6180 (TheLoop->isLoopInvariant(I->getOperand(1)) &&
6181 PSE.getSCEV(I->getOperand(1))->isOne())))
6182 return 0;
6183
6184 // Detect reduction patterns
6185 if (auto RedCost = getReductionPatternCost(I, VF, VectorTy))
6186 return *RedCost;
6187
6188 // Certain instructions can be cheaper to vectorize if they have a constant
6189 // second vector operand. One example of this are shifts on x86.
6190 Value *Op2 = I->getOperand(1);
6191 if (!isa<Constant>(Op2) && TheLoop->isLoopInvariant(Op2) &&
6192 PSE.getSE()->isSCEVable(Op2->getType()) &&
6193 isa<SCEVConstant>(PSE.getSCEV(Op2))) {
6194 Op2 = cast<SCEVConstant>(PSE.getSCEV(Op2))->getValue();
6195 }
6196 auto Op2Info = TTI.getOperandInfo(Op2);
6197 if (Op2Info.Kind == TargetTransformInfo::OK_AnyValue &&
6200
6201 SmallVector<const Value *, 4> Operands(I->operand_values());
6202 return TTI.getArithmeticInstrCost(
6203 I->getOpcode(), VectorTy, CostKind,
6204 {TargetTransformInfo::OK_AnyValue, TargetTransformInfo::OP_None},
6205 Op2Info, Operands, I, TLI);
6206 }
6207 case Instruction::FNeg: {
6208 return TTI.getArithmeticInstrCost(
6209 I->getOpcode(), VectorTy, CostKind,
6210 {TargetTransformInfo::OK_AnyValue, TargetTransformInfo::OP_None},
6211 {TargetTransformInfo::OK_AnyValue, TargetTransformInfo::OP_None},
6212 I->getOperand(0), I);
6213 }
6214 case Instruction::Select: {
6216 const SCEV *CondSCEV = SE->getSCEV(SI->getCondition());
6217 bool ScalarCond = (SE->isLoopInvariant(CondSCEV, TheLoop));
6218
6219 const Value *Op0, *Op1;
6220 using namespace llvm::PatternMatch;
6221 if (!ScalarCond && (match(I, m_LogicalAnd(m_Value(Op0), m_Value(Op1))) ||
6222 match(I, m_LogicalOr(m_Value(Op0), m_Value(Op1))))) {
6223 // select x, y, false --> x & y
6224 // select x, true, y --> x | y
6225 const auto [Op1VK, Op1VP] = TTI::getOperandInfo(Op0);
6226 const auto [Op2VK, Op2VP] = TTI::getOperandInfo(Op1);
6227 assert(Op0->getType()->getScalarSizeInBits() == 1 &&
6228 Op1->getType()->getScalarSizeInBits() == 1);
6229
6230 return TTI.getArithmeticInstrCost(
6231 match(I, m_LogicalOr()) ? Instruction::Or : Instruction::And,
6232 VectorTy, CostKind, {Op1VK, Op1VP}, {Op2VK, Op2VP}, {Op0, Op1}, I);
6233 }
6234
6235 Type *CondTy = SI->getCondition()->getType();
6236 if (!ScalarCond)
6237 CondTy = VectorType::get(CondTy, VF);
6238
6240 if (auto *Cmp = dyn_cast<CmpInst>(SI->getCondition()))
6241 Pred = Cmp->getPredicate();
6242 return TTI.getCmpSelInstrCost(I->getOpcode(), VectorTy, CondTy, Pred,
6243 CostKind, {TTI::OK_AnyValue, TTI::OP_None},
6244 {TTI::OK_AnyValue, TTI::OP_None}, I);
6245 }
6246 case Instruction::ICmp:
6247 case Instruction::FCmp: {
6248 Type *ValTy = I->getOperand(0)->getType();
6249
6251 [[maybe_unused]] Instruction *Op0AsInstruction =
6252 dyn_cast<Instruction>(I->getOperand(0));
6253 assert((!canTruncateToMinimalBitwidth(Op0AsInstruction, VF) ||
6254 MinBWs[I] == MinBWs[Op0AsInstruction]) &&
6255 "if both the operand and the compare are marked for "
6256 "truncation, they must have the same bitwidth");
6257 ValTy = IntegerType::get(ValTy->getContext(), MinBWs[I]);
6258 }
6259
6260 VectorTy = toVectorTy(ValTy, VF);
6261 return TTI.getCmpSelInstrCost(
6262 I->getOpcode(), VectorTy, CmpInst::makeCmpResultType(VectorTy),
6263 cast<CmpInst>(I)->getPredicate(), CostKind,
6264 {TTI::OK_AnyValue, TTI::OP_None}, {TTI::OK_AnyValue, TTI::OP_None}, I);
6265 }
6266 case Instruction::Store:
6267 case Instruction::Load: {
6268 ElementCount Width = VF;
6269 if (Width.isVector()) {
6270 InstWidening Decision = getWideningDecision(I, Width);
6271 assert(Decision != CM_Unknown &&
6272 "CM decision should be taken at this point");
6275 if (Decision == CM_Scalarize)
6276 Width = ElementCount::getFixed(1);
6277 }
6278 VectorTy = toVectorTy(getLoadStoreType(I), Width);
6279 return getMemoryInstructionCost(I, VF);
6280 }
6281 case Instruction::BitCast:
6282 if (I->getType()->isPointerTy())
6283 return 0;
6284 [[fallthrough]];
6285 case Instruction::ZExt:
6286 case Instruction::SExt:
6287 case Instruction::FPToUI:
6288 case Instruction::FPToSI:
6289 case Instruction::FPExt:
6290 case Instruction::PtrToInt:
6291 case Instruction::IntToPtr:
6292 case Instruction::SIToFP:
6293 case Instruction::UIToFP:
6294 case Instruction::Trunc:
6295 case Instruction::FPTrunc: {
6296 // Computes the CastContextHint from a Load/Store instruction.
6297 auto ComputeCCH = [&](Instruction *I) -> TTI::CastContextHint {
6299 "Expected a load or a store!");
6300
6301 if (VF.isScalar() || !TheLoop->contains(I))
6303
6304 switch (getWideningDecision(I, VF)) {
6316 llvm_unreachable("Instr did not go through cost modelling?");
6319 llvm_unreachable_internal("Instr has invalid widening decision");
6320 }
6321
6322 llvm_unreachable("Unhandled case!");
6323 };
6324
6325 unsigned Opcode = I->getOpcode();
6327 // For Trunc, the context is the only user, which must be a StoreInst.
6328 if (Opcode == Instruction::Trunc || Opcode == Instruction::FPTrunc) {
6329 if (I->hasOneUse())
6330 if (StoreInst *Store = dyn_cast<StoreInst>(*I->user_begin()))
6331 CCH = ComputeCCH(Store);
6332 }
6333 // For Z/Sext, the context is the operand, which must be a LoadInst.
6334 else if (Opcode == Instruction::ZExt || Opcode == Instruction::SExt ||
6335 Opcode == Instruction::FPExt) {
6336 if (LoadInst *Load = dyn_cast<LoadInst>(I->getOperand(0)))
6337 CCH = ComputeCCH(Load);
6338 }
6339
6340 // We optimize the truncation of induction variables having constant
6341 // integer steps. The cost of these truncations is the same as the scalar
6342 // operation.
6343 if (isOptimizableIVTruncate(I, VF)) {
6344 auto *Trunc = cast<TruncInst>(I);
6345 return TTI.getCastInstrCost(Instruction::Trunc, Trunc->getDestTy(),
6346 Trunc->getSrcTy(), CCH, CostKind, Trunc);
6347 }
6348
6349 // Detect reduction patterns
6350 if (auto RedCost = getReductionPatternCost(I, VF, VectorTy))
6351 return *RedCost;
6352
6353 Type *SrcScalarTy = I->getOperand(0)->getType();
6354 Instruction *Op0AsInstruction = dyn_cast<Instruction>(I->getOperand(0));
6355 if (canTruncateToMinimalBitwidth(Op0AsInstruction, VF))
6356 SrcScalarTy =
6357 IntegerType::get(SrcScalarTy->getContext(), MinBWs[Op0AsInstruction]);
6358 Type *SrcVecTy =
6359 VectorTy->isVectorTy() ? toVectorTy(SrcScalarTy, VF) : SrcScalarTy;
6360
6362 // If the result type is <= the source type, there will be no extend
6363 // after truncating the users to the minimal required bitwidth.
6364 if (VectorTy->getScalarSizeInBits() <= SrcVecTy->getScalarSizeInBits() &&
6365 (I->getOpcode() == Instruction::ZExt ||
6366 I->getOpcode() == Instruction::SExt))
6367 return 0;
6368 }
6369
6370 return TTI.getCastInstrCost(Opcode, VectorTy, SrcVecTy, CCH, CostKind, I);
6371 }
6372 case Instruction::Call:
6373 return getVectorCallCost(cast<CallInst>(I), VF);
6374 case Instruction::ExtractValue:
6375 return TTI.getInstructionCost(I, CostKind);
6376 case Instruction::Alloca:
6377 // We cannot easily widen alloca to a scalable alloca, as
6378 // the result would need to be a vector of pointers.
6379 if (VF.isScalable())
6381 [[fallthrough]];
6382 default:
6383 // This opcode is unknown. Assume that it is the same as 'mul'.
6384 return TTI.getArithmeticInstrCost(Instruction::Mul, VectorTy, CostKind);
6385 } // end of switch.
6386}
6387
6389 // Ignore ephemeral values.
6391
6392 SmallVector<Value *, 4> DeadInterleavePointerOps;
6394
6395 // If a scalar epilogue is required, users outside the loop won't use
6396 // live-outs from the vector loop but from the scalar epilogue. Ignore them if
6397 // that is the case.
6398 bool RequiresScalarEpilogue = requiresScalarEpilogue(true);
6399 auto IsLiveOutDead = [this, RequiresScalarEpilogue](User *U) {
6400 return RequiresScalarEpilogue &&
6401 !TheLoop->contains(cast<Instruction>(U)->getParent());
6402 };
6403
6405 DFS.perform(LI);
6406 for (BasicBlock *BB : reverse(make_range(DFS.beginRPO(), DFS.endRPO())))
6407 for (Instruction &I : reverse(*BB)) {
6408 if (VecValuesToIgnore.contains(&I) || ValuesToIgnore.contains(&I))
6409 continue;
6410
6411 // Add instructions that would be trivially dead and are only used by
6412 // values already ignored to DeadOps to seed worklist.
6414 all_of(I.users(), [this, IsLiveOutDead](User *U) {
6415 return VecValuesToIgnore.contains(U) ||
6416 ValuesToIgnore.contains(U) || IsLiveOutDead(U);
6417 }))
6418 DeadOps.push_back(&I);
6419
6420 // For interleave groups, we only create a pointer for the start of the
6421 // interleave group. Queue up addresses of group members except the insert
6422 // position for further processing.
6423 if (isAccessInterleaved(&I)) {
6424 auto *Group = getInterleavedAccessGroup(&I);
6425 if (Group->getInsertPos() == &I)
6426 continue;
6427 Value *PointerOp = getLoadStorePointerOperand(&I);
6428 DeadInterleavePointerOps.push_back(PointerOp);
6429 }
6430
6431 // Queue branches for analysis. They are dead, if their successors only
6432 // contain dead instructions.
6433 if (auto *Br = dyn_cast<BranchInst>(&I)) {
6434 if (Br->isConditional())
6435 DeadOps.push_back(&I);
6436 }
6437 }
6438
6439 // Mark ops feeding interleave group members as free, if they are only used
6440 // by other dead computations.
6441 for (unsigned I = 0; I != DeadInterleavePointerOps.size(); ++I) {
6442 auto *Op = dyn_cast<Instruction>(DeadInterleavePointerOps[I]);
6443 if (!Op || !TheLoop->contains(Op) || any_of(Op->users(), [this](User *U) {
6444 Instruction *UI = cast<Instruction>(U);
6445 return !VecValuesToIgnore.contains(U) &&
6446 (!isAccessInterleaved(UI) ||
6447 getInterleavedAccessGroup(UI)->getInsertPos() == UI);
6448 }))
6449 continue;
6450 VecValuesToIgnore.insert(Op);
6451 append_range(DeadInterleavePointerOps, Op->operands());
6452 }
6453
6454 // Mark ops that would be trivially dead and are only used by ignored
6455 // instructions as free.
6456 BasicBlock *Header = TheLoop->getHeader();
6457
6458 // Returns true if the block contains only dead instructions. Such blocks will
6459 // be removed by VPlan-to-VPlan transforms and won't be considered by the
6460 // VPlan-based cost model, so skip them in the legacy cost-model as well.
6461 auto IsEmptyBlock = [this](BasicBlock *BB) {
6462 return all_of(*BB, [this](Instruction &I) {
6463 return ValuesToIgnore.contains(&I) || VecValuesToIgnore.contains(&I) ||
6464 (isa<BranchInst>(&I) && !cast<BranchInst>(&I)->isConditional());
6465 });
6466 };
6467 for (unsigned I = 0; I != DeadOps.size(); ++I) {
6468 auto *Op = dyn_cast<Instruction>(DeadOps[I]);
6469
6470 // Check if the branch should be considered dead.
6471 if (auto *Br = dyn_cast_or_null<BranchInst>(Op)) {
6472 BasicBlock *ThenBB = Br->getSuccessor(0);
6473 BasicBlock *ElseBB = Br->getSuccessor(1);
6474 // Don't considers branches leaving the loop for simplification.
6475 if (!TheLoop->contains(ThenBB) || !TheLoop->contains(ElseBB))
6476 continue;
6477 bool ThenEmpty = IsEmptyBlock(ThenBB);
6478 bool ElseEmpty = IsEmptyBlock(ElseBB);
6479 if ((ThenEmpty && ElseEmpty) ||
6480 (ThenEmpty && ThenBB->getSingleSuccessor() == ElseBB &&
6481 ElseBB->phis().empty()) ||
6482 (ElseEmpty && ElseBB->getSingleSuccessor() == ThenBB &&
6483 ThenBB->phis().empty())) {
6484 VecValuesToIgnore.insert(Br);
6485 DeadOps.push_back(Br->getCondition());
6486 }
6487 continue;
6488 }
6489
6490 // Skip any op that shouldn't be considered dead.
6491 if (!Op || !TheLoop->contains(Op) ||
6492 (isa<PHINode>(Op) && Op->getParent() == Header) ||
6494 any_of(Op->users(), [this, IsLiveOutDead](User *U) {
6495 return !VecValuesToIgnore.contains(U) &&
6496 !ValuesToIgnore.contains(U) && !IsLiveOutDead(U);
6497 }))
6498 continue;
6499
6500 // If all of Op's users are in ValuesToIgnore, add it to ValuesToIgnore
6501 // which applies for both scalar and vector versions. Otherwise it is only
6502 // dead in vector versions, so only add it to VecValuesToIgnore.
6503 if (all_of(Op->users(),
6504 [this](User *U) { return ValuesToIgnore.contains(U); }))
6505 ValuesToIgnore.insert(Op);
6506
6507 VecValuesToIgnore.insert(Op);
6508 append_range(DeadOps, Op->operands());
6509 }
6510
6511 // Ignore type-promoting instructions we identified during reduction
6512 // detection.
6513 for (const auto &Reduction : Legal->getReductionVars()) {
6514 const RecurrenceDescriptor &RedDes = Reduction.second;
6515 const SmallPtrSetImpl<Instruction *> &Casts = RedDes.getCastInsts();
6516 VecValuesToIgnore.insert_range(Casts);
6517 }
6518 // Ignore type-casting instructions we identified during induction
6519 // detection.
6520 for (const auto &Induction : Legal->getInductionVars()) {
6521 const InductionDescriptor &IndDes = Induction.second;
6522 const SmallVectorImpl<Instruction *> &Casts = IndDes.getCastInsts();
6523 VecValuesToIgnore.insert_range(Casts);
6524 }
6525}
6526
6528 // Avoid duplicating work finding in-loop reductions.
6529 if (!InLoopReductions.empty())
6530 return;
6531
6532 for (const auto &Reduction : Legal->getReductionVars()) {
6533 PHINode *Phi = Reduction.first;
6534 const RecurrenceDescriptor &RdxDesc = Reduction.second;
6535
6536 // We don't collect reductions that are type promoted (yet).
6537 if (RdxDesc.getRecurrenceType() != Phi->getType())
6538 continue;
6539
6540 // If the target would prefer this reduction to happen "in-loop", then we
6541 // want to record it as such.
6542 RecurKind Kind = RdxDesc.getRecurrenceKind();
6543 if (!PreferInLoopReductions && !useOrderedReductions(RdxDesc) &&
6544 !TTI.preferInLoopReduction(Kind, Phi->getType()))
6545 continue;
6546
6547 // Check that we can correctly put the reductions into the loop, by
6548 // finding the chain of operations that leads from the phi to the loop
6549 // exit value.
6550 SmallVector<Instruction *, 4> ReductionOperations =
6551 RdxDesc.getReductionOpChain(Phi, TheLoop);
6552 bool InLoop = !ReductionOperations.empty();
6553
6554 if (InLoop) {
6555 InLoopReductions.insert(Phi);
6556 // Add the elements to InLoopReductionImmediateChains for cost modelling.
6557 Instruction *LastChain = Phi;
6558 for (auto *I : ReductionOperations) {
6559 InLoopReductionImmediateChains[I] = LastChain;
6560 LastChain = I;
6561 }
6562 }
6563 LLVM_DEBUG(dbgs() << "LV: Using " << (InLoop ? "inloop" : "out of loop")
6564 << " reduction for phi: " << *Phi << "\n");
6565 }
6566}
6567
6568// This function will select a scalable VF if the target supports scalable
6569// vectors and a fixed one otherwise.
6570// TODO: we could return a pair of values that specify the max VF and
6571// min VF, to be used in `buildVPlans(MinVF, MaxVF)` instead of
6572// `buildVPlans(VF, VF)`. We cannot do it because VPLAN at the moment
6573// doesn't have a cost model that can choose which plan to execute if
6574// more than one is generated.
6577 unsigned WidestType;
6578 std::tie(std::ignore, WidestType) = CM.getSmallestAndWidestTypes();
6579
6581 TTI.enableScalableVectorization()
6584
6585 TypeSize RegSize = TTI.getRegisterBitWidth(RegKind);
6586 unsigned N = RegSize.getKnownMinValue() / WidestType;
6587 return ElementCount::get(N, RegSize.isScalable());
6588}
6589
6592 ElementCount VF = UserVF;
6593 // Outer loop handling: They may require CFG and instruction level
6594 // transformations before even evaluating whether vectorization is profitable.
6595 // Since we cannot modify the incoming IR, we need to build VPlan upfront in
6596 // the vectorization pipeline.
6597 if (!OrigLoop->isInnermost()) {
6598 // If the user doesn't provide a vectorization factor, determine a
6599 // reasonable one.
6600 if (UserVF.isZero()) {
6601 VF = determineVPlanVF(TTI, CM);
6602 LLVM_DEBUG(dbgs() << "LV: VPlan computed VF " << VF << ".\n");
6603
6604 // Make sure we have a VF > 1 for stress testing.
6605 if (VPlanBuildStressTest && (VF.isScalar() || VF.isZero())) {
6606 LLVM_DEBUG(dbgs() << "LV: VPlan stress testing: "
6607 << "overriding computed VF.\n");
6608 VF = ElementCount::getFixed(4);
6609 }
6610 } else if (UserVF.isScalable() && !TTI.supportsScalableVectors() &&
6612 LLVM_DEBUG(dbgs() << "LV: Not vectorizing. Scalable VF requested, but "
6613 << "not supported by the target.\n");
6615 "Scalable vectorization requested but not supported by the target",
6616 "the scalable user-specified vectorization width for outer-loop "
6617 "vectorization cannot be used because the target does not support "
6618 "scalable vectors.",
6619 "ScalableVFUnfeasible", ORE, OrigLoop);
6621 }
6622 assert(EnableVPlanNativePath && "VPlan-native path is not enabled.");
6624 "VF needs to be a power of two");
6625 LLVM_DEBUG(dbgs() << "LV: Using " << (!UserVF.isZero() ? "user " : "")
6626 << "VF " << VF << " to build VPlans.\n");
6627 buildVPlans(VF, VF);
6628
6629 if (VPlans.empty())
6631
6632 // For VPlan build stress testing, we bail out after VPlan construction.
6635
6636 return {VF, 0 /*Cost*/, 0 /* ScalarCost */};
6637 }
6638
6639 LLVM_DEBUG(
6640 dbgs() << "LV: Not vectorizing. Inner loops aren't supported in the "
6641 "VPlan-native path.\n");
6643}
6644
6645void LoopVectorizationPlanner::plan(ElementCount UserVF, unsigned UserIC) {
6646 assert(OrigLoop->isInnermost() && "Inner loop expected.");
6647 CM.collectValuesToIgnore();
6648 CM.collectElementTypesForWidening();
6649
6650 FixedScalableVFPair MaxFactors = CM.computeMaxVF(UserVF, UserIC);
6651 if (!MaxFactors) // Cases that should not to be vectorized nor interleaved.
6652 return;
6653
6654 // Invalidate interleave groups if all blocks of loop will be predicated.
6655 if (CM.blockNeedsPredicationForAnyReason(OrigLoop->getHeader()) &&
6657 LLVM_DEBUG(
6658 dbgs()
6659 << "LV: Invalidate all interleaved groups due to fold-tail by masking "
6660 "which requires masked-interleaved support.\n");
6661 if (CM.InterleaveInfo.invalidateGroups())
6662 // Invalidating interleave groups also requires invalidating all decisions
6663 // based on them, which includes widening decisions and uniform and scalar
6664 // values.
6665 CM.invalidateCostModelingDecisions();
6666 }
6667
6668 if (CM.foldTailByMasking())
6669 Legal->prepareToFoldTailByMasking();
6670
6671 ElementCount MaxUserVF =
6672 UserVF.isScalable() ? MaxFactors.ScalableVF : MaxFactors.FixedVF;
6673 if (UserVF) {
6674 if (!ElementCount::isKnownLE(UserVF, MaxUserVF)) {
6676 "UserVF ignored because it may be larger than the maximal safe VF",
6677 "InvalidUserVF", ORE, OrigLoop);
6678 } else {
6680 "VF needs to be a power of two");
6681 // Collect the instructions (and their associated costs) that will be more
6682 // profitable to scalarize.
6683 CM.collectInLoopReductions();
6684 if (CM.selectUserVectorizationFactor(UserVF)) {
6685 LLVM_DEBUG(dbgs() << "LV: Using user VF " << UserVF << ".\n");
6686 buildVPlansWithVPRecipes(UserVF, UserVF);
6688 return;
6689 }
6690 reportVectorizationInfo("UserVF ignored because of invalid costs.",
6691 "InvalidCost", ORE, OrigLoop);
6692 }
6693 }
6694
6695 // Collect the Vectorization Factor Candidates.
6696 SmallVector<ElementCount> VFCandidates;
6697 for (auto VF = ElementCount::getFixed(1);
6698 ElementCount::isKnownLE(VF, MaxFactors.FixedVF); VF *= 2)
6699 VFCandidates.push_back(VF);
6700 for (auto VF = ElementCount::getScalable(1);
6701 ElementCount::isKnownLE(VF, MaxFactors.ScalableVF); VF *= 2)
6702 VFCandidates.push_back(VF);
6703
6704 CM.collectInLoopReductions();
6705 for (const auto &VF : VFCandidates) {
6706 // Collect Uniform and Scalar instructions after vectorization with VF.
6707 CM.collectNonVectorizedAndSetWideningDecisions(VF);
6708 }
6709
6710 buildVPlansWithVPRecipes(ElementCount::getFixed(1), MaxFactors.FixedVF);
6711 buildVPlansWithVPRecipes(ElementCount::getScalable(1), MaxFactors.ScalableVF);
6712
6714}
6715
6717 ElementCount VF) const {
6718 InstructionCost Cost = CM.getInstructionCost(UI, VF);
6719 if (Cost.isValid() && ForceTargetInstructionCost.getNumOccurrences())
6721 return Cost;
6722}
6723
6725 ElementCount VF) const {
6726 return CM.isUniformAfterVectorization(I, VF);
6727}
6728
6729bool VPCostContext::skipCostComputation(Instruction *UI, bool IsVector) const {
6730 return CM.ValuesToIgnore.contains(UI) ||
6731 (IsVector && CM.VecValuesToIgnore.contains(UI)) ||
6732 SkipCostComputation.contains(UI);
6733}
6734
6736LoopVectorizationPlanner::precomputeCosts(VPlan &Plan, ElementCount VF,
6737 VPCostContext &CostCtx) const {
6739 // Cost modeling for inductions is inaccurate in the legacy cost model
6740 // compared to the recipes that are generated. To match here initially during
6741 // VPlan cost model bring up directly use the induction costs from the legacy
6742 // cost model. Note that we do this as pre-processing; the VPlan may not have
6743 // any recipes associated with the original induction increment instruction
6744 // and may replace truncates with VPWidenIntOrFpInductionRecipe. We precompute
6745 // the cost of induction phis and increments (both that are represented by
6746 // recipes and those that are not), to avoid distinguishing between them here,
6747 // and skip all recipes that represent induction phis and increments (the
6748 // former case) later on, if they exist, to avoid counting them twice.
6749 // Similarly we pre-compute the cost of any optimized truncates.
6750 // TODO: Switch to more accurate costing based on VPlan.
6751 for (const auto &[IV, IndDesc] : Legal->getInductionVars()) {
6753 IV->getIncomingValueForBlock(OrigLoop->getLoopLatch()));
6754 SmallVector<Instruction *> IVInsts = {IVInc};
6755 for (unsigned I = 0; I != IVInsts.size(); I++) {
6756 for (Value *Op : IVInsts[I]->operands()) {
6757 auto *OpI = dyn_cast<Instruction>(Op);
6758 if (Op == IV || !OpI || !OrigLoop->contains(OpI) || !Op->hasOneUse())
6759 continue;
6760 IVInsts.push_back(OpI);
6761 }
6762 }
6763 IVInsts.push_back(IV);
6764 for (User *U : IV->users()) {
6765 auto *CI = cast<Instruction>(U);
6766 if (!CostCtx.CM.isOptimizableIVTruncate(CI, VF))
6767 continue;
6768 IVInsts.push_back(CI);
6769 }
6770
6771 // If the vector loop gets executed exactly once with the given VF, ignore
6772 // the costs of comparison and induction instructions, as they'll get
6773 // simplified away.
6774 // TODO: Remove this code after stepping away from the legacy cost model and
6775 // adding code to simplify VPlans before calculating their costs.
6776 auto TC = getSmallConstantTripCount(PSE.getSE(), OrigLoop);
6777 if (TC == VF && !CM.foldTailByMasking())
6778 addFullyUnrolledInstructionsToIgnore(OrigLoop, Legal->getInductionVars(),
6779 CostCtx.SkipCostComputation);
6780
6781 for (Instruction *IVInst : IVInsts) {
6782 if (CostCtx.skipCostComputation(IVInst, VF.isVector()))
6783 continue;
6784 InstructionCost InductionCost = CostCtx.getLegacyCost(IVInst, VF);
6785 LLVM_DEBUG({
6786 dbgs() << "Cost of " << InductionCost << " for VF " << VF
6787 << ": induction instruction " << *IVInst << "\n";
6788 });
6789 Cost += InductionCost;
6790 CostCtx.SkipCostComputation.insert(IVInst);
6791 }
6792 }
6793
6794 /// Compute the cost of all exiting conditions of the loop using the legacy
6795 /// cost model. This is to match the legacy behavior, which adds the cost of
6796 /// all exit conditions. Note that this over-estimates the cost, as there will
6797 /// be a single condition to control the vector loop.
6799 CM.TheLoop->getExitingBlocks(Exiting);
6800 SetVector<Instruction *> ExitInstrs;
6801 // Collect all exit conditions.
6802 for (BasicBlock *EB : Exiting) {
6803 auto *Term = dyn_cast<BranchInst>(EB->getTerminator());
6804 if (!Term || CostCtx.skipCostComputation(Term, VF.isVector()))
6805 continue;
6806 if (auto *CondI = dyn_cast<Instruction>(Term->getOperand(0))) {
6807 ExitInstrs.insert(CondI);
6808 }
6809 }
6810 // Compute the cost of all instructions only feeding the exit conditions.
6811 for (unsigned I = 0; I != ExitInstrs.size(); ++I) {
6812 Instruction *CondI = ExitInstrs[I];
6813 if (!OrigLoop->contains(CondI) ||
6814 !CostCtx.SkipCostComputation.insert(CondI).second)
6815 continue;
6816 InstructionCost CondICost = CostCtx.getLegacyCost(CondI, VF);
6817 LLVM_DEBUG({
6818 dbgs() << "Cost of " << CondICost << " for VF " << VF
6819 << ": exit condition instruction " << *CondI << "\n";
6820 });
6821 Cost += CondICost;
6822 for (Value *Op : CondI->operands()) {
6823 auto *OpI = dyn_cast<Instruction>(Op);
6824 if (!OpI || CostCtx.skipCostComputation(OpI, VF.isVector()) ||
6825 any_of(OpI->users(), [&ExitInstrs, this](User *U) {
6826 return OrigLoop->contains(cast<Instruction>(U)->getParent()) &&
6827 !ExitInstrs.contains(cast<Instruction>(U));
6828 }))
6829 continue;
6830 ExitInstrs.insert(OpI);
6831 }
6832 }
6833
6834 // Pre-compute the costs for branches except for the backedge, as the number
6835 // of replicate regions in a VPlan may not directly match the number of
6836 // branches, which would lead to different decisions.
6837 // TODO: Compute cost of branches for each replicate region in the VPlan,
6838 // which is more accurate than the legacy cost model.
6839 for (BasicBlock *BB : OrigLoop->blocks()) {
6840 if (CostCtx.skipCostComputation(BB->getTerminator(), VF.isVector()))
6841 continue;
6842 CostCtx.SkipCostComputation.insert(BB->getTerminator());
6843 if (BB == OrigLoop->getLoopLatch())
6844 continue;
6845 auto BranchCost = CostCtx.getLegacyCost(BB->getTerminator(), VF);
6846 Cost += BranchCost;
6847 }
6848
6849 // Pre-compute costs for instructions that are forced-scalar or profitable to
6850 // scalarize. Their costs will be computed separately in the legacy cost
6851 // model.
6852 for (Instruction *ForcedScalar : CM.ForcedScalars[VF]) {
6853 if (CostCtx.skipCostComputation(ForcedScalar, VF.isVector()))
6854 continue;
6855 CostCtx.SkipCostComputation.insert(ForcedScalar);
6856 InstructionCost ForcedCost = CostCtx.getLegacyCost(ForcedScalar, VF);
6857 LLVM_DEBUG({
6858 dbgs() << "Cost of " << ForcedCost << " for VF " << VF
6859 << ": forced scalar " << *ForcedScalar << "\n";
6860 });
6861 Cost += ForcedCost;
6862 }
6863 for (const auto &[Scalarized, ScalarCost] : CM.InstsToScalarize[VF]) {
6864 if (CostCtx.skipCostComputation(Scalarized, VF.isVector()))
6865 continue;
6866 CostCtx.SkipCostComputation.insert(Scalarized);
6867 LLVM_DEBUG({
6868 dbgs() << "Cost of " << ScalarCost << " for VF " << VF
6869 << ": profitable to scalarize " << *Scalarized << "\n";
6870 });
6871 Cost += ScalarCost;
6872 }
6873
6874 return Cost;
6875}
6876
6877InstructionCost LoopVectorizationPlanner::cost(VPlan &Plan,
6878 ElementCount VF) const {
6879 VPCostContext CostCtx(CM.TTI, *CM.TLI, Plan, CM, CM.CostKind, *PSE.getSE(),
6880 OrigLoop);
6881 InstructionCost Cost = precomputeCosts(Plan, VF, CostCtx);
6882
6883 // Now compute and add the VPlan-based cost.
6884 Cost += Plan.cost(VF, CostCtx);
6885#ifndef NDEBUG
6886 unsigned EstimatedWidth = estimateElementCount(VF, CM.getVScaleForTuning());
6887 LLVM_DEBUG(dbgs() << "Cost for VF " << VF << ": " << Cost
6888 << " (Estimated cost per lane: ");
6889 if (Cost.isValid()) {
6890 double CostPerLane = double(Cost.getValue()) / EstimatedWidth;
6891 LLVM_DEBUG(dbgs() << format("%.1f", CostPerLane));
6892 } else /* No point dividing an invalid cost - it will still be invalid */
6893 LLVM_DEBUG(dbgs() << "Invalid");
6894 LLVM_DEBUG(dbgs() << ")\n");
6895#endif
6896 return Cost;
6897}
6898
6899#ifndef NDEBUG
6900/// Return true if the original loop \ TheLoop contains any instructions that do
6901/// not have corresponding recipes in \p Plan and are not marked to be ignored
6902/// in \p CostCtx. This means the VPlan contains simplification that the legacy
6903/// cost-model did not account for.
6905 VPCostContext &CostCtx,
6906 Loop *TheLoop,
6907 ElementCount VF) {
6908 // First collect all instructions for the recipes in Plan.
6909 auto GetInstructionForCost = [](const VPRecipeBase *R) -> Instruction * {
6910 if (auto *S = dyn_cast<VPSingleDefRecipe>(R))
6911 return dyn_cast_or_null<Instruction>(S->getUnderlyingValue());
6912 if (auto *WidenMem = dyn_cast<VPWidenMemoryRecipe>(R))
6913 return &WidenMem->getIngredient();
6914 return nullptr;
6915 };
6916
6917 // Check if a select for a safe divisor was hoisted to the pre-header. If so,
6918 // the select doesn't need to be considered for the vector loop cost; go with
6919 // the more accurate VPlan-based cost model.
6920 for (VPRecipeBase &R : *Plan.getVectorPreheader()) {
6921 auto *VPI = dyn_cast<VPInstruction>(&R);
6922 if (!VPI || VPI->getOpcode() != Instruction::Select ||
6923 VPI->getNumUsers() != 1)
6924 continue;
6925
6926 if (auto *WR = dyn_cast<VPWidenRecipe>(*VPI->user_begin())) {
6927 switch (WR->getOpcode()) {
6928 case Instruction::UDiv:
6929 case Instruction::SDiv:
6930 case Instruction::URem:
6931 case Instruction::SRem:
6932 return true;
6933 default:
6934 break;
6935 }
6936 }
6937 }
6938
6939 DenseSet<Instruction *> SeenInstrs;
6940 auto Iter = vp_depth_first_deep(Plan.getVectorLoopRegion()->getEntry());
6942 for (VPRecipeBase &R : *VPBB) {
6943 if (auto *IR = dyn_cast<VPInterleaveRecipe>(&R)) {
6944 auto *IG = IR->getInterleaveGroup();
6945 unsigned NumMembers = IG->getNumMembers();
6946 for (unsigned I = 0; I != NumMembers; ++I) {
6947 if (Instruction *M = IG->getMember(I))
6948 SeenInstrs.insert(M);
6949 }
6950 continue;
6951 }
6952 // Unused FOR splices are removed by VPlan transforms, so the VPlan-based
6953 // cost model won't cost it whilst the legacy will.
6954 if (auto *FOR = dyn_cast<VPFirstOrderRecurrencePHIRecipe>(&R)) {
6955 using namespace VPlanPatternMatch;
6956 if (none_of(FOR->users(),
6957 match_fn(m_VPInstruction<
6959 return true;
6960 }
6961 // The VPlan-based cost model is more accurate for partial reduction and
6962 // comparing against the legacy cost isn't desirable.
6964 return true;
6965
6966 // The VPlan-based cost model can analyze if recipes are scalar
6967 // recursively, but the legacy cost model cannot.
6968 if (auto *WidenMemR = dyn_cast<VPWidenMemoryRecipe>(&R)) {
6969 auto *AddrI = dyn_cast<Instruction>(
6970 getLoadStorePointerOperand(&WidenMemR->getIngredient()));
6971 if (AddrI && vputils::isSingleScalar(WidenMemR->getAddr()) !=
6972 CostCtx.isLegacyUniformAfterVectorization(AddrI, VF))
6973 return true;
6974 }
6975
6976 /// If a VPlan transform folded a recipe to one producing a single-scalar,
6977 /// but the original instruction wasn't uniform-after-vectorization in the
6978 /// legacy cost model, the legacy cost overestimates the actual cost.
6979 if (auto *RepR = dyn_cast<VPReplicateRecipe>(&R)) {
6980 if (RepR->isSingleScalar() &&
6982 RepR->getUnderlyingInstr(), VF))
6983 return true;
6984 }
6985 if (Instruction *UI = GetInstructionForCost(&R)) {
6986 // If we adjusted the predicate of the recipe, the cost in the legacy
6987 // cost model may be different.
6988 using namespace VPlanPatternMatch;
6989 CmpPredicate Pred;
6990 if (match(&R, m_Cmp(Pred, m_VPValue(), m_VPValue())) &&
6991 cast<VPRecipeWithIRFlags>(R).getPredicate() !=
6992 cast<CmpInst>(UI)->getPredicate())
6993 return true;
6994 SeenInstrs.insert(UI);
6995 }
6996 }
6997 }
6998
6999 // Return true if the loop contains any instructions that are not also part of
7000 // the VPlan or are skipped for VPlan-based cost computations. This indicates
7001 // that the VPlan contains extra simplifications.
7002 return any_of(TheLoop->blocks(), [&SeenInstrs, &CostCtx,
7003 TheLoop](BasicBlock *BB) {
7004 return any_of(*BB, [&SeenInstrs, &CostCtx, TheLoop, BB](Instruction &I) {
7005 // Skip induction phis when checking for simplifications, as they may not
7006 // be lowered directly be lowered to a corresponding PHI recipe.
7007 if (isa<PHINode>(&I) && BB == TheLoop->getHeader() &&
7008 CostCtx.CM.Legal->isInductionPhi(cast<PHINode>(&I)))
7009 return false;
7010 return !SeenInstrs.contains(&I) && !CostCtx.skipCostComputation(&I, true);
7011 });
7012 });
7013}
7014#endif
7015
7017 if (VPlans.empty())
7019 // If there is a single VPlan with a single VF, return it directly.
7020 VPlan &FirstPlan = *VPlans[0];
7021 if (VPlans.size() == 1 && size(FirstPlan.vectorFactors()) == 1)
7022 return {*FirstPlan.vectorFactors().begin(), 0, 0};
7023
7024 LLVM_DEBUG(dbgs() << "LV: Computing best VF using cost kind: "
7025 << (CM.CostKind == TTI::TCK_RecipThroughput
7026 ? "Reciprocal Throughput\n"
7027 : CM.CostKind == TTI::TCK_Latency
7028 ? "Instruction Latency\n"
7029 : CM.CostKind == TTI::TCK_CodeSize ? "Code Size\n"
7030 : CM.CostKind == TTI::TCK_SizeAndLatency
7031 ? "Code Size and Latency\n"
7032 : "Unknown\n"));
7033
7035 assert(hasPlanWithVF(ScalarVF) &&
7036 "More than a single plan/VF w/o any plan having scalar VF");
7037
7038 // TODO: Compute scalar cost using VPlan-based cost model.
7039 InstructionCost ScalarCost = CM.expectedCost(ScalarVF);
7040 LLVM_DEBUG(dbgs() << "LV: Scalar loop costs: " << ScalarCost << ".\n");
7041 VectorizationFactor ScalarFactor(ScalarVF, ScalarCost, ScalarCost);
7042 VectorizationFactor BestFactor = ScalarFactor;
7043
7044 bool ForceVectorization = Hints.getForce() == LoopVectorizeHints::FK_Enabled;
7045 if (ForceVectorization) {
7046 // Ignore scalar width, because the user explicitly wants vectorization.
7047 // Initialize cost to max so that VF = 2 is, at least, chosen during cost
7048 // evaluation.
7049 BestFactor.Cost = InstructionCost::getMax();
7050 }
7051
7052 for (auto &P : VPlans) {
7053 ArrayRef<ElementCount> VFs(P->vectorFactors().begin(),
7054 P->vectorFactors().end());
7055
7057 if (any_of(VFs, [this](ElementCount VF) {
7058 return CM.shouldConsiderRegPressureForVF(VF);
7059 }))
7060 RUs = calculateRegisterUsageForPlan(*P, VFs, TTI, CM.ValuesToIgnore);
7061
7062 for (unsigned I = 0; I < VFs.size(); I++) {
7063 ElementCount VF = VFs[I];
7064 if (VF.isScalar())
7065 continue;
7066 if (!ForceVectorization && !willGenerateVectors(*P, VF, TTI)) {
7067 LLVM_DEBUG(
7068 dbgs()
7069 << "LV: Not considering vector loop of width " << VF
7070 << " because it will not generate any vector instructions.\n");
7071 continue;
7072 }
7073 if (CM.OptForSize && !ForceVectorization && hasReplicatorRegion(*P)) {
7074 LLVM_DEBUG(
7075 dbgs()
7076 << "LV: Not considering vector loop of width " << VF
7077 << " because it would cause replicated blocks to be generated,"
7078 << " which isn't allowed when optimizing for size.\n");
7079 continue;
7080 }
7081
7082 InstructionCost Cost = cost(*P, VF);
7083 VectorizationFactor CurrentFactor(VF, Cost, ScalarCost);
7084
7085 if (CM.shouldConsiderRegPressureForVF(VF) &&
7086 RUs[I].exceedsMaxNumRegs(TTI, ForceTargetNumVectorRegs)) {
7087 LLVM_DEBUG(dbgs() << "LV(REG): Not considering vector loop of width "
7088 << VF << " because it uses too many registers\n");
7089 continue;
7090 }
7091
7092 if (isMoreProfitable(CurrentFactor, BestFactor, P->hasScalarTail()))
7093 BestFactor = CurrentFactor;
7094
7095 // If profitable add it to ProfitableVF list.
7096 if (isMoreProfitable(CurrentFactor, ScalarFactor, P->hasScalarTail()))
7097 ProfitableVFs.push_back(CurrentFactor);
7098 }
7099 }
7100
7101#ifndef NDEBUG
7102 // Select the optimal vectorization factor according to the legacy cost-model.
7103 // This is now only used to verify the decisions by the new VPlan-based
7104 // cost-model and will be retired once the VPlan-based cost-model is
7105 // stabilized.
7106 VectorizationFactor LegacyVF = selectVectorizationFactor();
7107 VPlan &BestPlan = getPlanFor(BestFactor.Width);
7108
7109 // Pre-compute the cost and use it to check if BestPlan contains any
7110 // simplifications not accounted for in the legacy cost model. If that's the
7111 // case, don't trigger the assertion, as the extra simplifications may cause a
7112 // different VF to be picked by the VPlan-based cost model.
7113 VPCostContext CostCtx(CM.TTI, *CM.TLI, BestPlan, CM, CM.CostKind,
7114 *CM.PSE.getSE(), OrigLoop);
7115 precomputeCosts(BestPlan, BestFactor.Width, CostCtx);
7116 // Verify that the VPlan-based and legacy cost models agree, except for VPlans
7117 // with early exits and plans with additional VPlan simplifications. The
7118 // legacy cost model doesn't properly model costs for such loops.
7119 assert((BestFactor.Width == LegacyVF.Width || BestPlan.hasEarlyExit() ||
7120 !Legal->getLAI()->getSymbolicStrides().empty() ||
7122 CostCtx, OrigLoop,
7123 BestFactor.Width) ||
7125 getPlanFor(LegacyVF.Width), CostCtx, OrigLoop, LegacyVF.Width)) &&
7126 " VPlan cost model and legacy cost model disagreed");
7127 assert((BestFactor.Width.isScalar() || BestFactor.ScalarCost > 0) &&
7128 "when vectorizing, the scalar cost must be computed.");
7129#endif
7130
7131 LLVM_DEBUG(dbgs() << "LV: Selecting VF: " << BestFactor.Width << ".\n");
7132 return BestFactor;
7133}
7134
7136 using namespace VPlanPatternMatch;
7138 "RdxResult must be ComputeFindIVResult");
7139 VPValue *StartVPV = RdxResult->getOperand(1);
7140 match(StartVPV, m_Freeze(m_VPValue(StartVPV)));
7141 return StartVPV->getLiveInIRValue();
7142}
7143
7144// If \p EpiResumePhiR is resume VPPhi for a reduction when vectorizing the
7145// epilog loop, fix the reduction's scalar PHI node by adding the incoming value
7146// from the main vector loop.
7148 VPPhi *EpiResumePhiR, PHINode &EpiResumePhi, BasicBlock *BypassBlock) {
7149 // Get the VPInstruction computing the reduction result in the middle block.
7150 // The first operand may not be from the middle block if it is not connected
7151 // to the scalar preheader. In that case, there's nothing to fix.
7152 VPValue *Incoming = EpiResumePhiR->getOperand(0);
7155 auto *EpiRedResult = dyn_cast<VPInstruction>(Incoming);
7156 if (!EpiRedResult ||
7157 (EpiRedResult->getOpcode() != VPInstruction::ComputeAnyOfResult &&
7158 EpiRedResult->getOpcode() != VPInstruction::ComputeReductionResult &&
7159 EpiRedResult->getOpcode() != VPInstruction::ComputeFindIVResult))
7160 return;
7161
7162 auto *EpiRedHeaderPhi =
7163 cast<VPReductionPHIRecipe>(EpiRedResult->getOperand(0));
7164 RecurKind Kind = EpiRedHeaderPhi->getRecurrenceKind();
7165 Value *MainResumeValue;
7166 if (auto *VPI = dyn_cast<VPInstruction>(EpiRedHeaderPhi->getStartValue())) {
7167 assert((VPI->getOpcode() == VPInstruction::Broadcast ||
7168 VPI->getOpcode() == VPInstruction::ReductionStartVector) &&
7169 "unexpected start recipe");
7170 MainResumeValue = VPI->getOperand(0)->getUnderlyingValue();
7171 } else
7172 MainResumeValue = EpiRedHeaderPhi->getStartValue()->getUnderlyingValue();
7174 [[maybe_unused]] Value *StartV =
7175 EpiRedResult->getOperand(1)->getLiveInIRValue();
7176 auto *Cmp = cast<ICmpInst>(MainResumeValue);
7177 assert(Cmp->getPredicate() == CmpInst::ICMP_NE &&
7178 "AnyOf expected to start with ICMP_NE");
7179 assert(Cmp->getOperand(1) == StartV &&
7180 "AnyOf expected to start by comparing main resume value to original "
7181 "start value");
7182 MainResumeValue = Cmp->getOperand(0);
7184 Value *StartV = getStartValueFromReductionResult(EpiRedResult);
7185 Value *SentinelV = EpiRedResult->getOperand(2)->getLiveInIRValue();
7186 using namespace llvm::PatternMatch;
7187 Value *Cmp, *OrigResumeV, *CmpOp;
7188 [[maybe_unused]] bool IsExpectedPattern =
7189 match(MainResumeValue,
7190 m_Select(m_OneUse(m_Value(Cmp)), m_Specific(SentinelV),
7191 m_Value(OrigResumeV))) &&
7193 m_Value(CmpOp))) &&
7194 ((CmpOp == StartV && isGuaranteedNotToBeUndefOrPoison(CmpOp))));
7195 assert(IsExpectedPattern && "Unexpected reduction resume pattern");
7196 MainResumeValue = OrigResumeV;
7197 }
7198 PHINode *MainResumePhi = cast<PHINode>(MainResumeValue);
7199
7200 // When fixing reductions in the epilogue loop we should already have
7201 // created a bc.merge.rdx Phi after the main vector body. Ensure that we carry
7202 // over the incoming values correctly.
7203 EpiResumePhi.setIncomingValueForBlock(
7204 BypassBlock, MainResumePhi->getIncomingValueForBlock(BypassBlock));
7205}
7206
7208 ElementCount BestVF, unsigned BestUF, VPlan &BestVPlan,
7209 InnerLoopVectorizer &ILV, DominatorTree *DT, bool VectorizingEpilogue) {
7210 assert(BestVPlan.hasVF(BestVF) &&
7211 "Trying to execute plan with unsupported VF");
7212 assert(BestVPlan.hasUF(BestUF) &&
7213 "Trying to execute plan with unsupported UF");
7214 if (BestVPlan.hasEarlyExit())
7215 ++LoopsEarlyExitVectorized;
7216 // TODO: Move to VPlan transform stage once the transition to the VPlan-based
7217 // cost model is complete for better cost estimates.
7220 BestVPlan);
7223 bool HasBranchWeights =
7224 hasBranchWeightMD(*OrigLoop->getLoopLatch()->getTerminator());
7225 if (HasBranchWeights) {
7226 std::optional<unsigned> VScale = CM.getVScaleForTuning();
7228 BestVPlan, BestVF, VScale);
7229 }
7230
7231 // Checks are the same for all VPlans, added to BestVPlan only for
7232 // compactness.
7233 attachRuntimeChecks(BestVPlan, ILV.RTChecks, HasBranchWeights);
7234
7235 // Retrieving VectorPH now when it's easier while VPlan still has Regions.
7236 VPBasicBlock *VectorPH = cast<VPBasicBlock>(BestVPlan.getVectorPreheader());
7237
7238 VPlanTransforms::optimizeForVFAndUF(BestVPlan, BestVF, BestUF, PSE);
7241 if (BestVPlan.getEntry()->getSingleSuccessor() ==
7242 BestVPlan.getScalarPreheader()) {
7243 // TODO: The vector loop would be dead, should not even try to vectorize.
7244 ORE->emit([&]() {
7245 return OptimizationRemarkAnalysis(DEBUG_TYPE, "VectorizationDead",
7246 OrigLoop->getStartLoc(),
7247 OrigLoop->getHeader())
7248 << "Created vector loop never executes due to insufficient trip "
7249 "count.";
7250 });
7252 }
7253
7255 BestVPlan, BestVF,
7256 TTI.getRegisterBitWidth(TargetTransformInfo::RGK_FixedWidthVector));
7258
7260 // Regions are dissolved after optimizing for VF and UF, which completely
7261 // removes unneeded loop regions first.
7263 // Canonicalize EVL loops after regions are dissolved.
7267 BestVPlan, VectorPH, CM.foldTailByMasking(),
7268 CM.requiresScalarEpilogue(BestVF.isVector()));
7269 VPlanTransforms::materializeVFAndVFxUF(BestVPlan, VectorPH, BestVF);
7270 VPlanTransforms::cse(BestVPlan);
7272
7273 // 0. Generate SCEV-dependent code in the entry, including TripCount, before
7274 // making any changes to the CFG.
7275 DenseMap<const SCEV *, Value *> ExpandedSCEVs =
7276 VPlanTransforms::expandSCEVs(BestVPlan, *PSE.getSE());
7277 if (!ILV.getTripCount())
7278 ILV.setTripCount(BestVPlan.getTripCount()->getLiveInIRValue());
7279 else
7280 assert(VectorizingEpilogue && "should only re-use the existing trip "
7281 "count during epilogue vectorization");
7282
7283 // Perform the actual loop transformation.
7284 VPTransformState State(&TTI, BestVF, LI, DT, ILV.AC, ILV.Builder, &BestVPlan,
7285 OrigLoop->getParentLoop(),
7286 Legal->getWidestInductionType());
7287
7288#ifdef EXPENSIVE_CHECKS
7289 assert(DT->verify(DominatorTree::VerificationLevel::Fast));
7290#endif
7291
7292 // 1. Set up the skeleton for vectorization, including vector pre-header and
7293 // middle block. The vector loop is created during VPlan execution.
7294 State.CFG.PrevBB = ILV.createVectorizedLoopSkeleton();
7296 State.CFG.PrevBB->getSingleSuccessor(), &BestVPlan);
7298
7299 assert(verifyVPlanIsValid(BestVPlan, true /*VerifyLate*/) &&
7300 "final VPlan is invalid");
7301
7302 // After vectorization, the exit blocks of the original loop will have
7303 // additional predecessors. Invalidate SCEVs for the exit phis in case SE
7304 // looked through single-entry phis.
7305 ScalarEvolution &SE = *PSE.getSE();
7306 for (VPIRBasicBlock *Exit : BestVPlan.getExitBlocks()) {
7307 if (!Exit->hasPredecessors())
7308 continue;
7309 for (VPRecipeBase &PhiR : Exit->phis())
7311 &cast<VPIRPhi>(PhiR).getIRPhi());
7312 }
7313 // Forget the original loop and block dispositions.
7314 SE.forgetLoop(OrigLoop);
7316
7318
7319 //===------------------------------------------------===//
7320 //
7321 // Notice: any optimization or new instruction that go
7322 // into the code below should also be implemented in
7323 // the cost-model.
7324 //
7325 //===------------------------------------------------===//
7326
7327 // Retrieve loop information before executing the plan, which may remove the
7328 // original loop, if it becomes unreachable.
7329 MDNode *LID = OrigLoop->getLoopID();
7330 unsigned OrigLoopInvocationWeight = 0;
7331 std::optional<unsigned> OrigAverageTripCount =
7332 getLoopEstimatedTripCount(OrigLoop, &OrigLoopInvocationWeight);
7333
7334 BestVPlan.execute(&State);
7335
7336 // 2.6. Maintain Loop Hints
7337 // Keep all loop hints from the original loop on the vector loop (we'll
7338 // replace the vectorizer-specific hints below).
7339 VPBasicBlock *HeaderVPBB = vputils::getFirstLoopHeader(BestVPlan, State.VPDT);
7340 // Add metadata to disable runtime unrolling a scalar loop when there
7341 // are no runtime checks about strides and memory. A scalar loop that is
7342 // rarely used is not worth unrolling.
7343 bool DisableRuntimeUnroll = !ILV.RTChecks.hasChecks() && !BestVF.isScalar();
7345 HeaderVPBB ? LI->getLoopFor(State.CFG.VPBB2IRBB.lookup(HeaderVPBB))
7346 : nullptr,
7347 HeaderVPBB, BestVPlan, VectorizingEpilogue, LID, OrigAverageTripCount,
7348 OrigLoopInvocationWeight,
7349 estimateElementCount(BestVF * BestUF, CM.getVScaleForTuning()),
7350 DisableRuntimeUnroll);
7351
7352 // 3. Fix the vectorized code: take care of header phi's, live-outs,
7353 // predication, updating analyses.
7354 ILV.fixVectorizedLoop(State);
7355
7357
7358 return ExpandedSCEVs;
7359}
7360
7361//===--------------------------------------------------------------------===//
7362// EpilogueVectorizerMainLoop
7363//===--------------------------------------------------------------------===//
7364
7365/// This function is partially responsible for generating the control flow
7366/// depicted in https://llvm.org/docs/Vectorizers.html#epilogue-vectorization.
7368 BasicBlock *ScalarPH = createScalarPreheader("");
7369 BasicBlock *VectorPH = ScalarPH->getSinglePredecessor();
7370
7371 // Generate the code to check the minimum iteration count of the vector
7372 // epilogue (see below).
7373 EPI.EpilogueIterationCountCheck =
7374 emitIterationCountCheck(VectorPH, ScalarPH, true);
7375 EPI.EpilogueIterationCountCheck->setName("iter.check");
7376
7377 VectorPH = cast<BranchInst>(EPI.EpilogueIterationCountCheck->getTerminator())
7378 ->getSuccessor(1);
7379 // Generate the iteration count check for the main loop, *after* the check
7380 // for the epilogue loop, so that the path-length is shorter for the case
7381 // that goes directly through the vector epilogue. The longer-path length for
7382 // the main loop is compensated for, by the gain from vectorizing the larger
7383 // trip count. Note: the branch will get updated later on when we vectorize
7384 // the epilogue.
7385 EPI.MainLoopIterationCountCheck =
7386 emitIterationCountCheck(VectorPH, ScalarPH, false);
7387
7388 return cast<BranchInst>(EPI.MainLoopIterationCountCheck->getTerminator())
7389 ->getSuccessor(1);
7390}
7391
7393 LLVM_DEBUG({
7394 dbgs() << "Create Skeleton for epilogue vectorized loop (first pass)\n"
7395 << "Main Loop VF:" << EPI.MainLoopVF
7396 << ", Main Loop UF:" << EPI.MainLoopUF
7397 << ", Epilogue Loop VF:" << EPI.EpilogueVF
7398 << ", Epilogue Loop UF:" << EPI.EpilogueUF << "\n";
7399 });
7400}
7401
7404 dbgs() << "intermediate fn:\n"
7405 << *OrigLoop->getHeader()->getParent() << "\n";
7406 });
7407}
7408
7410 BasicBlock *VectorPH, BasicBlock *Bypass, bool ForEpilogue) {
7411 assert(Bypass && "Expected valid bypass basic block.");
7414 Value *CheckMinIters = createIterationCountCheck(
7415 VectorPH, ForEpilogue ? EPI.EpilogueVF : EPI.MainLoopVF,
7416 ForEpilogue ? EPI.EpilogueUF : EPI.MainLoopUF);
7417
7418 BasicBlock *const TCCheckBlock = VectorPH;
7419 if (!ForEpilogue)
7420 TCCheckBlock->setName("vector.main.loop.iter.check");
7421
7422 // Create new preheader for vector loop.
7423 VectorPH = SplitBlock(TCCheckBlock, TCCheckBlock->getTerminator(),
7424 static_cast<DominatorTree *>(nullptr), LI, nullptr,
7425 "vector.ph");
7426 if (ForEpilogue) {
7427 // Save the trip count so we don't have to regenerate it in the
7428 // vec.epilog.iter.check. This is safe to do because the trip count
7429 // generated here dominates the vector epilog iter check.
7430 EPI.TripCount = Count;
7431 } else {
7433 }
7434
7435 BranchInst &BI = *BranchInst::Create(Bypass, VectorPH, CheckMinIters);
7436 if (hasBranchWeightMD(*OrigLoop->getLoopLatch()->getTerminator()))
7437 setBranchWeights(BI, MinItersBypassWeights, /*IsExpected=*/false);
7438 ReplaceInstWithInst(TCCheckBlock->getTerminator(), &BI);
7439
7440 // When vectorizing the main loop, its trip-count check is placed in a new
7441 // block, whereas the overall trip-count check is placed in the VPlan entry
7442 // block. When vectorizing the epilogue loop, its trip-count check is placed
7443 // in the VPlan entry block.
7444 if (!ForEpilogue)
7445 introduceCheckBlockInVPlan(TCCheckBlock);
7446 return TCCheckBlock;
7447}
7448
7449//===--------------------------------------------------------------------===//
7450// EpilogueVectorizerEpilogueLoop
7451//===--------------------------------------------------------------------===//
7452
7453/// This function creates a new scalar preheader, using the previous one as
7454/// entry block to the epilogue VPlan. The minimum iteration check is being
7455/// represented in VPlan.
7457 BasicBlock *NewScalarPH = createScalarPreheader("vec.epilog.");
7458 BasicBlock *OriginalScalarPH = NewScalarPH->getSinglePredecessor();
7459 OriginalScalarPH->setName("vec.epilog.iter.check");
7460 VPIRBasicBlock *NewEntry = Plan.createVPIRBasicBlock(OriginalScalarPH);
7461 VPBasicBlock *OldEntry = Plan.getEntry();
7462 for (auto &R : make_early_inc_range(*OldEntry)) {
7463 // Skip moving VPIRInstructions (including VPIRPhis), which are unmovable by
7464 // defining.
7465 if (isa<VPIRInstruction>(&R))
7466 continue;
7467 R.moveBefore(*NewEntry, NewEntry->end());
7468 }
7469
7470 VPBlockUtils::reassociateBlocks(OldEntry, NewEntry);
7471 Plan.setEntry(NewEntry);
7472 // OldEntry is now dead and will be cleaned up when the plan gets destroyed.
7473
7474 return OriginalScalarPH;
7475}
7476
7478 LLVM_DEBUG({
7479 dbgs() << "Create Skeleton for epilogue vectorized loop (second pass)\n"
7480 << "Epilogue Loop VF:" << EPI.EpilogueVF
7481 << ", Epilogue Loop UF:" << EPI.EpilogueUF << "\n";
7482 });
7483}
7484
7487 dbgs() << "final fn:\n" << *OrigLoop->getHeader()->getParent() << "\n";
7488 });
7489}
7490
7492VPRecipeBuilder::tryToWidenMemory(Instruction *I, ArrayRef<VPValue *> Operands,
7493 VFRange &Range) {
7495 "Must be called with either a load or store");
7496
7497 auto WillWiden = [&](ElementCount VF) -> bool {
7499 CM.getWideningDecision(I, VF);
7501 "CM decision should be taken at this point.");
7503 return true;
7504 if (CM.isScalarAfterVectorization(I, VF) ||
7505 CM.isProfitableToScalarize(I, VF))
7506 return false;
7508 };
7509
7511 return nullptr;
7512
7513 VPValue *Mask = nullptr;
7514 if (Legal->isMaskRequired(I))
7515 Mask = getBlockInMask(Builder.getInsertBlock());
7516
7517 // Determine if the pointer operand of the access is either consecutive or
7518 // reverse consecutive.
7520 CM.getWideningDecision(I, Range.Start);
7522 bool Consecutive =
7524
7525 VPValue *Ptr = isa<LoadInst>(I) ? Operands[0] : Operands[1];
7526 if (Consecutive) {
7528 Ptr->getUnderlyingValue()->stripPointerCasts());
7529 VPSingleDefRecipe *VectorPtr;
7530 if (Reverse) {
7531 // When folding the tail, we may compute an address that we don't in the
7532 // original scalar loop: drop the GEP no-wrap flags in this case.
7533 // Otherwise preserve existing flags without no-unsigned-wrap, as we will
7534 // emit negative indices.
7535 GEPNoWrapFlags Flags =
7536 CM.foldTailByMasking() || !GEP
7538 : GEP->getNoWrapFlags().withoutNoUnsignedWrap();
7539 VectorPtr =
7541 /*Stride*/ -1, Flags, I->getDebugLoc());
7542 } else {
7543 VectorPtr = new VPVectorPointerRecipe(Ptr, getLoadStoreType(I),
7544 GEP ? GEP->getNoWrapFlags()
7546 I->getDebugLoc());
7547 }
7548 Builder.insert(VectorPtr);
7549 Ptr = VectorPtr;
7550 }
7551 if (LoadInst *Load = dyn_cast<LoadInst>(I))
7552 return new VPWidenLoadRecipe(*Load, Ptr, Mask, Consecutive, Reverse,
7553 Load->getAlign(), VPIRMetadata(*Load, LVer),
7554 I->getDebugLoc());
7555
7556 StoreInst *Store = cast<StoreInst>(I);
7557 return new VPWidenStoreRecipe(*Store, Ptr, Operands[0], Mask, Consecutive,
7558 Reverse, Store->getAlign(),
7559 VPIRMetadata(*Store, LVer), I->getDebugLoc());
7560}
7561
7562/// Creates a VPWidenIntOrFpInductionRecpipe for \p Phi. If needed, it will also
7563/// insert a recipe to expand the step for the induction recipe.
7564static VPWidenIntOrFpInductionRecipe *
7566 VPValue *Start, const InductionDescriptor &IndDesc,
7567 VPlan &Plan, ScalarEvolution &SE, Loop &OrigLoop) {
7568 assert(IndDesc.getStartValue() ==
7569 Phi->getIncomingValueForBlock(OrigLoop.getLoopPreheader()));
7570 assert(SE.isLoopInvariant(IndDesc.getStep(), &OrigLoop) &&
7571 "step must be loop invariant");
7572
7573 VPValue *Step =
7575 if (auto *TruncI = dyn_cast<TruncInst>(PhiOrTrunc)) {
7576 return new VPWidenIntOrFpInductionRecipe(Phi, Start, Step, &Plan.getVF(),
7577 IndDesc, TruncI,
7578 TruncI->getDebugLoc());
7579 }
7580 assert(isa<PHINode>(PhiOrTrunc) && "must be a phi node here");
7581 return new VPWidenIntOrFpInductionRecipe(Phi, Start, Step, &Plan.getVF(),
7582 IndDesc, Phi->getDebugLoc());
7583}
7584
7585VPHeaderPHIRecipe *VPRecipeBuilder::tryToOptimizeInductionPHI(
7586 PHINode *Phi, ArrayRef<VPValue *> Operands, VFRange &Range) {
7587
7588 // Check if this is an integer or fp induction. If so, build the recipe that
7589 // produces its scalar and vector values.
7590 if (auto *II = Legal->getIntOrFpInductionDescriptor(Phi))
7591 return createWidenInductionRecipes(Phi, Phi, Operands[0], *II, Plan,
7592 *PSE.getSE(), *OrigLoop);
7593
7594 // Check if this is pointer induction. If so, build the recipe for it.
7595 if (auto *II = Legal->getPointerInductionDescriptor(Phi)) {
7596 VPValue *Step = vputils::getOrCreateVPValueForSCEVExpr(Plan, II->getStep());
7597 return new VPWidenPointerInductionRecipe(
7598 Phi, Operands[0], Step, &Plan.getVFxUF(), *II,
7600 [&](ElementCount VF) {
7601 return CM.isScalarAfterVectorization(Phi, VF);
7602 },
7603 Range),
7604 Phi->getDebugLoc());
7605 }
7606 return nullptr;
7607}
7608
7609VPWidenIntOrFpInductionRecipe *VPRecipeBuilder::tryToOptimizeInductionTruncate(
7610 TruncInst *I, ArrayRef<VPValue *> Operands, VFRange &Range) {
7611 // Optimize the special case where the source is a constant integer
7612 // induction variable. Notice that we can only optimize the 'trunc' case
7613 // because (a) FP conversions lose precision, (b) sext/zext may wrap, and
7614 // (c) other casts depend on pointer size.
7615
7616 // Determine whether \p K is a truncation based on an induction variable that
7617 // can be optimized.
7618 auto IsOptimizableIVTruncate =
7619 [&](Instruction *K) -> std::function<bool(ElementCount)> {
7620 return [=](ElementCount VF) -> bool {
7621 return CM.isOptimizableIVTruncate(K, VF);
7622 };
7623 };
7624
7626 IsOptimizableIVTruncate(I), Range)) {
7627
7628 auto *Phi = cast<PHINode>(I->getOperand(0));
7629 const InductionDescriptor &II = *Legal->getIntOrFpInductionDescriptor(Phi);
7630 VPValue *Start = Plan.getOrAddLiveIn(II.getStartValue());
7631 return createWidenInductionRecipes(Phi, I, Start, II, Plan, *PSE.getSE(),
7632 *OrigLoop);
7633 }
7634 return nullptr;
7635}
7636
7637VPSingleDefRecipe *VPRecipeBuilder::tryToWidenCall(CallInst *CI,
7638 ArrayRef<VPValue *> Operands,
7639 VFRange &Range) {
7641 [this, CI](ElementCount VF) {
7642 return CM.isScalarWithPredication(CI, VF);
7643 },
7644 Range);
7645
7646 if (IsPredicated)
7647 return nullptr;
7648
7650 if (ID && (ID == Intrinsic::assume || ID == Intrinsic::lifetime_end ||
7651 ID == Intrinsic::lifetime_start || ID == Intrinsic::sideeffect ||
7652 ID == Intrinsic::pseudoprobe ||
7653 ID == Intrinsic::experimental_noalias_scope_decl))
7654 return nullptr;
7655
7657
7658 // Is it beneficial to perform intrinsic call compared to lib call?
7659 bool ShouldUseVectorIntrinsic =
7661 [&](ElementCount VF) -> bool {
7662 return CM.getCallWideningDecision(CI, VF).Kind ==
7664 },
7665 Range);
7666 if (ShouldUseVectorIntrinsic)
7667 return new VPWidenIntrinsicRecipe(*CI, ID, Ops, CI->getType(),
7668 CI->getDebugLoc());
7669
7670 Function *Variant = nullptr;
7671 std::optional<unsigned> MaskPos;
7672 // Is better to call a vectorized version of the function than to to scalarize
7673 // the call?
7674 auto ShouldUseVectorCall = LoopVectorizationPlanner::getDecisionAndClampRange(
7675 [&](ElementCount VF) -> bool {
7676 // The following case may be scalarized depending on the VF.
7677 // The flag shows whether we can use a usual Call for vectorized
7678 // version of the instruction.
7679
7680 // If we've found a variant at a previous VF, then stop looking. A
7681 // vectorized variant of a function expects input in a certain shape
7682 // -- basically the number of input registers, the number of lanes
7683 // per register, and whether there's a mask required.
7684 // We store a pointer to the variant in the VPWidenCallRecipe, so
7685 // once we have an appropriate variant it's only valid for that VF.
7686 // This will force a different vplan to be generated for each VF that
7687 // finds a valid variant.
7688 if (Variant)
7689 return false;
7690 LoopVectorizationCostModel::CallWideningDecision Decision =
7691 CM.getCallWideningDecision(CI, VF);
7693 Variant = Decision.Variant;
7694 MaskPos = Decision.MaskPos;
7695 return true;
7696 }
7697
7698 return false;
7699 },
7700 Range);
7701 if (ShouldUseVectorCall) {
7702 if (MaskPos.has_value()) {
7703 // We have 2 cases that would require a mask:
7704 // 1) The block needs to be predicated, either due to a conditional
7705 // in the scalar loop or use of an active lane mask with
7706 // tail-folding, and we use the appropriate mask for the block.
7707 // 2) No mask is required for the block, but the only available
7708 // vector variant at this VF requires a mask, so we synthesize an
7709 // all-true mask.
7710 VPValue *Mask = nullptr;
7711 if (Legal->isMaskRequired(CI))
7712 Mask = getBlockInMask(Builder.getInsertBlock());
7713 else
7714 Mask = Plan.getOrAddLiveIn(
7715 ConstantInt::getTrue(IntegerType::getInt1Ty(CI->getContext())));
7716
7717 Ops.insert(Ops.begin() + *MaskPos, Mask);
7718 }
7719
7720 Ops.push_back(Operands.back());
7721 return new VPWidenCallRecipe(CI, Variant, Ops, CI->getDebugLoc());
7722 }
7723
7724 return nullptr;
7725}
7726
7727bool VPRecipeBuilder::shouldWiden(Instruction *I, VFRange &Range) const {
7729 !isa<StoreInst>(I) && "Instruction should have been handled earlier");
7730 // Instruction should be widened, unless it is scalar after vectorization,
7731 // scalarization is profitable or it is predicated.
7732 auto WillScalarize = [this, I](ElementCount VF) -> bool {
7733 return CM.isScalarAfterVectorization(I, VF) ||
7734 CM.isProfitableToScalarize(I, VF) ||
7735 CM.isScalarWithPredication(I, VF);
7736 };
7738 Range);
7739}
7740
7741VPWidenRecipe *VPRecipeBuilder::tryToWiden(Instruction *I,
7742 ArrayRef<VPValue *> Operands) {
7743 switch (I->getOpcode()) {
7744 default:
7745 return nullptr;
7746 case Instruction::SDiv:
7747 case Instruction::UDiv:
7748 case Instruction::SRem:
7749 case Instruction::URem: {
7750 // If not provably safe, use a select to form a safe divisor before widening the
7751 // div/rem operation itself. Otherwise fall through to general handling below.
7752 if (CM.isPredicatedInst(I)) {
7753 SmallVector<VPValue *> Ops(Operands);
7754 VPValue *Mask = getBlockInMask(Builder.getInsertBlock());
7755 VPValue *One = Plan.getConstantInt(I->getType(), 1u);
7756 auto *SafeRHS = Builder.createSelect(Mask, Ops[1], One, I->getDebugLoc());
7757 Ops[1] = SafeRHS;
7758 return new VPWidenRecipe(*I, Ops);
7759 }
7760 [[fallthrough]];
7761 }
7762 case Instruction::Add:
7763 case Instruction::And:
7764 case Instruction::AShr:
7765 case Instruction::FAdd:
7766 case Instruction::FCmp:
7767 case Instruction::FDiv:
7768 case Instruction::FMul:
7769 case Instruction::FNeg:
7770 case Instruction::FRem:
7771 case Instruction::FSub:
7772 case Instruction::ICmp:
7773 case Instruction::LShr:
7774 case Instruction::Mul:
7775 case Instruction::Or:
7776 case Instruction::Select:
7777 case Instruction::Shl:
7778 case Instruction::Sub:
7779 case Instruction::Xor:
7780 case Instruction::Freeze: {
7781 SmallVector<VPValue *> NewOps(Operands);
7782 if (Instruction::isBinaryOp(I->getOpcode())) {
7783 // The legacy cost model uses SCEV to check if some of the operands are
7784 // constants. To match the legacy cost model's behavior, use SCEV to try
7785 // to replace operands with constants.
7786 ScalarEvolution &SE = *PSE.getSE();
7787 auto GetConstantViaSCEV = [this, &SE](VPValue *Op) {
7788 if (!Op->isLiveIn())
7789 return Op;
7790 Value *V = Op->getUnderlyingValue();
7791 if (isa<Constant>(V) || !SE.isSCEVable(V->getType()))
7792 return Op;
7793 auto *C = dyn_cast<SCEVConstant>(SE.getSCEV(V));
7794 if (!C)
7795 return Op;
7796 return Plan.getOrAddLiveIn(C->getValue());
7797 };
7798 // For Mul, the legacy cost model checks both operands.
7799 if (I->getOpcode() == Instruction::Mul)
7800 NewOps[0] = GetConstantViaSCEV(NewOps[0]);
7801 // For other binops, the legacy cost model only checks the second operand.
7802 NewOps[1] = GetConstantViaSCEV(NewOps[1]);
7803 }
7804 return new VPWidenRecipe(*I, NewOps);
7805 }
7806 case Instruction::ExtractValue: {
7807 SmallVector<VPValue *> NewOps(Operands);
7808 auto *EVI = cast<ExtractValueInst>(I);
7809 assert(EVI->getNumIndices() == 1 && "Expected one extractvalue index");
7810 unsigned Idx = EVI->getIndices()[0];
7811 NewOps.push_back(Plan.getConstantInt(32, Idx));
7812 return new VPWidenRecipe(*I, NewOps);
7813 }
7814 };
7815}
7816
7817VPHistogramRecipe *
7818VPRecipeBuilder::tryToWidenHistogram(const HistogramInfo *HI,
7819 ArrayRef<VPValue *> Operands) {
7820 // FIXME: Support other operations.
7821 unsigned Opcode = HI->Update->getOpcode();
7822 assert((Opcode == Instruction::Add || Opcode == Instruction::Sub) &&
7823 "Histogram update operation must be an Add or Sub");
7824
7826 // Bucket address.
7827 HGramOps.push_back(Operands[1]);
7828 // Increment value.
7829 HGramOps.push_back(getVPValueOrAddLiveIn(HI->Update->getOperand(1)));
7830
7831 // In case of predicated execution (due to tail-folding, or conditional
7832 // execution, or both), pass the relevant mask.
7833 if (Legal->isMaskRequired(HI->Store))
7834 HGramOps.push_back(getBlockInMask(Builder.getInsertBlock()));
7835
7836 return new VPHistogramRecipe(Opcode, HGramOps, HI->Store->getDebugLoc());
7837}
7838
7839VPReplicateRecipe *
7841 VFRange &Range) {
7843 [&](ElementCount VF) { return CM.isUniformAfterVectorization(I, VF); },
7844 Range);
7845
7846 bool IsPredicated = CM.isPredicatedInst(I);
7847
7848 // Even if the instruction is not marked as uniform, there are certain
7849 // intrinsic calls that can be effectively treated as such, so we check for
7850 // them here. Conservatively, we only do this for scalable vectors, since
7851 // for fixed-width VFs we can always fall back on full scalarization.
7852 if (!IsUniform && Range.Start.isScalable() && isa<IntrinsicInst>(I)) {
7853 switch (cast<IntrinsicInst>(I)->getIntrinsicID()) {
7854 case Intrinsic::assume:
7855 case Intrinsic::lifetime_start:
7856 case Intrinsic::lifetime_end:
7857 // For scalable vectors if one of the operands is variant then we still
7858 // want to mark as uniform, which will generate one instruction for just
7859 // the first lane of the vector. We can't scalarize the call in the same
7860 // way as for fixed-width vectors because we don't know how many lanes
7861 // there are.
7862 //
7863 // The reasons for doing it this way for scalable vectors are:
7864 // 1. For the assume intrinsic generating the instruction for the first
7865 // lane is still be better than not generating any at all. For
7866 // example, the input may be a splat across all lanes.
7867 // 2. For the lifetime start/end intrinsics the pointer operand only
7868 // does anything useful when the input comes from a stack object,
7869 // which suggests it should always be uniform. For non-stack objects
7870 // the effect is to poison the object, which still allows us to
7871 // remove the call.
7872 IsUniform = true;
7873 break;
7874 default:
7875 break;
7876 }
7877 }
7878 VPValue *BlockInMask = nullptr;
7879 if (!IsPredicated) {
7880 // Finalize the recipe for Instr, first if it is not predicated.
7881 LLVM_DEBUG(dbgs() << "LV: Scalarizing:" << *I << "\n");
7882 } else {
7883 LLVM_DEBUG(dbgs() << "LV: Scalarizing and predicating:" << *I << "\n");
7884 // Instructions marked for predication are replicated and a mask operand is
7885 // added initially. Masked replicate recipes will later be placed under an
7886 // if-then construct to prevent side-effects. Generate recipes to compute
7887 // the block mask for this region.
7888 BlockInMask = getBlockInMask(Builder.getInsertBlock());
7889 }
7890
7891 // Note that there is some custom logic to mark some intrinsics as uniform
7892 // manually above for scalable vectors, which this assert needs to account for
7893 // as well.
7894 assert((Range.Start.isScalar() || !IsUniform || !IsPredicated ||
7895 (Range.Start.isScalable() && isa<IntrinsicInst>(I))) &&
7896 "Should not predicate a uniform recipe");
7897 auto *Recipe = new VPReplicateRecipe(I, Operands, IsUniform, BlockInMask,
7898 VPIRMetadata(*I, LVer));
7899 return Recipe;
7900}
7901
7902/// Find all possible partial reductions in the loop and track all of those that
7903/// are valid so recipes can be formed later.
7905 // Find all possible partial reductions.
7907 PartialReductionChains;
7908 for (const auto &[Phi, RdxDesc] : Legal->getReductionVars()) {
7909 getScaledReductions(Phi, RdxDesc.getLoopExitInstr(), Range,
7910 PartialReductionChains);
7911 }
7912
7913 // A partial reduction is invalid if any of its extends are used by
7914 // something that isn't another partial reduction. This is because the
7915 // extends are intended to be lowered along with the reduction itself.
7916
7917 // Build up a set of partial reduction ops for efficient use checking.
7918 SmallPtrSet<User *, 4> PartialReductionOps;
7919 for (const auto &[PartialRdx, _] : PartialReductionChains)
7920 PartialReductionOps.insert(PartialRdx.ExtendUser);
7921
7922 auto ExtendIsOnlyUsedByPartialReductions =
7923 [&PartialReductionOps](Instruction *Extend) {
7924 return all_of(Extend->users(), [&](const User *U) {
7925 return PartialReductionOps.contains(U);
7926 });
7927 };
7928
7929 // Check if each use of a chain's two extends is a partial reduction
7930 // and only add those that don't have non-partial reduction users.
7931 for (auto Pair : PartialReductionChains) {
7932 PartialReductionChain Chain = Pair.first;
7933 if (ExtendIsOnlyUsedByPartialReductions(Chain.ExtendA) &&
7934 (!Chain.ExtendB || ExtendIsOnlyUsedByPartialReductions(Chain.ExtendB)))
7935 ScaledReductionMap.try_emplace(Chain.Reduction, Pair.second);
7936 }
7937}
7938
7939bool VPRecipeBuilder::getScaledReductions(
7940 Instruction *PHI, Instruction *RdxExitInstr, VFRange &Range,
7941 SmallVectorImpl<std::pair<PartialReductionChain, unsigned>> &Chains) {
7942 if (!CM.TheLoop->contains(RdxExitInstr))
7943 return false;
7944
7945 auto *Update = dyn_cast<BinaryOperator>(RdxExitInstr);
7946 if (!Update)
7947 return false;
7948
7949 Value *Op = Update->getOperand(0);
7950 Value *PhiOp = Update->getOperand(1);
7951 if (Op == PHI)
7952 std::swap(Op, PhiOp);
7953
7954 // Try and get a scaled reduction from the first non-phi operand.
7955 // If one is found, we use the discovered reduction instruction in
7956 // place of the accumulator for costing.
7957 if (auto *OpInst = dyn_cast<Instruction>(Op)) {
7958 if (getScaledReductions(PHI, OpInst, Range, Chains)) {
7959 PHI = Chains.rbegin()->first.Reduction;
7960
7961 Op = Update->getOperand(0);
7962 PhiOp = Update->getOperand(1);
7963 if (Op == PHI)
7964 std::swap(Op, PhiOp);
7965 }
7966 }
7967 if (PhiOp != PHI)
7968 return false;
7969
7970 using namespace llvm::PatternMatch;
7971
7972 // If the update is a binary operator, check both of its operands to see if
7973 // they are extends. Otherwise, see if the update comes directly from an
7974 // extend.
7975 Instruction *Exts[2] = {nullptr};
7976 BinaryOperator *ExtendUser = dyn_cast<BinaryOperator>(Op);
7977 std::optional<unsigned> BinOpc;
7978 Type *ExtOpTypes[2] = {nullptr};
7980
7981 auto CollectExtInfo = [this, &Exts, &ExtOpTypes,
7982 &ExtKinds](SmallVectorImpl<Value *> &Ops) -> bool {
7983 for (const auto &[I, OpI] : enumerate(Ops)) {
7984 const APInt *C;
7985 if (I > 0 && match(OpI, m_APInt(C)) &&
7986 canConstantBeExtended(C, ExtOpTypes[0], ExtKinds[0])) {
7987 ExtOpTypes[I] = ExtOpTypes[0];
7988 ExtKinds[I] = ExtKinds[0];
7989 continue;
7990 }
7991 Value *ExtOp;
7992 if (!match(OpI, m_ZExtOrSExt(m_Value(ExtOp))))
7993 return false;
7994 Exts[I] = cast<Instruction>(OpI);
7995
7996 // TODO: We should be able to support live-ins.
7997 if (!CM.TheLoop->contains(Exts[I]))
7998 return false;
7999
8000 ExtOpTypes[I] = ExtOp->getType();
8001 ExtKinds[I] = TTI::getPartialReductionExtendKind(Exts[I]);
8002 }
8003 return true;
8004 };
8005
8006 if (ExtendUser) {
8007 if (!ExtendUser->hasOneUse())
8008 return false;
8009
8010 // Use the side-effect of match to replace BinOp only if the pattern is
8011 // matched, we don't care at this point whether it actually matched.
8012 match(ExtendUser, m_Neg(m_BinOp(ExtendUser)));
8013
8014 SmallVector<Value *> Ops(ExtendUser->operands());
8015 if (!CollectExtInfo(Ops))
8016 return false;
8017
8018 BinOpc = std::make_optional(ExtendUser->getOpcode());
8019 } else if (match(Update, m_Add(m_Value(), m_Value()))) {
8020 // We already know the operands for Update are Op and PhiOp.
8022 if (!CollectExtInfo(Ops))
8023 return false;
8024
8025 ExtendUser = Update;
8026 BinOpc = std::nullopt;
8027 } else
8028 return false;
8029
8030 PartialReductionChain Chain(RdxExitInstr, Exts[0], Exts[1], ExtendUser);
8031
8032 TypeSize PHISize = PHI->getType()->getPrimitiveSizeInBits();
8033 TypeSize ASize = ExtOpTypes[0]->getPrimitiveSizeInBits();
8034 if (!PHISize.hasKnownScalarFactor(ASize))
8035 return false;
8036 unsigned TargetScaleFactor = PHISize.getKnownScalarFactor(ASize);
8037
8039 [&](ElementCount VF) {
8041 Update->getOpcode(), ExtOpTypes[0], ExtOpTypes[1],
8042 PHI->getType(), VF, ExtKinds[0], ExtKinds[1], BinOpc,
8043 CM.CostKind);
8044 return Cost.isValid();
8045 },
8046 Range)) {
8047 Chains.emplace_back(Chain, TargetScaleFactor);
8048 return true;
8049 }
8050
8051 return false;
8052}
8053
8055 VFRange &Range) {
8056 // First, check for specific widening recipes that deal with inductions, Phi
8057 // nodes, calls and memory operations.
8058 VPRecipeBase *Recipe;
8059 Instruction *Instr = R->getUnderlyingInstr();
8060 SmallVector<VPValue *, 4> Operands(R->operands());
8061 if (auto *PhiR = dyn_cast<VPPhi>(R)) {
8062 VPBasicBlock *Parent = PhiR->getParent();
8063 [[maybe_unused]] VPRegionBlock *LoopRegionOf =
8064 Parent->getEnclosingLoopRegion();
8065 assert(LoopRegionOf && LoopRegionOf->getEntry() == Parent &&
8066 "Non-header phis should have been handled during predication");
8067 auto *Phi = cast<PHINode>(R->getUnderlyingInstr());
8068 assert(Operands.size() == 2 && "Must have 2 operands for header phis");
8069 if ((Recipe = tryToOptimizeInductionPHI(Phi, Operands, Range)))
8070 return Recipe;
8071
8072 VPHeaderPHIRecipe *PhiRecipe = nullptr;
8073 assert((Legal->isReductionVariable(Phi) ||
8074 Legal->isFixedOrderRecurrence(Phi)) &&
8075 "can only widen reductions and fixed-order recurrences here");
8076 VPValue *StartV = Operands[0];
8077 if (Legal->isReductionVariable(Phi)) {
8078 const RecurrenceDescriptor &RdxDesc = Legal->getRecurrenceDescriptor(Phi);
8079 assert(RdxDesc.getRecurrenceStartValue() ==
8080 Phi->getIncomingValueForBlock(OrigLoop->getLoopPreheader()));
8081
8082 // If the PHI is used by a partial reduction, set the scale factor.
8083 unsigned ScaleFactor =
8084 getScalingForReduction(RdxDesc.getLoopExitInstr()).value_or(1);
8085 PhiRecipe = new VPReductionPHIRecipe(
8086 Phi, RdxDesc.getRecurrenceKind(), *StartV, CM.isInLoopReduction(Phi),
8087 CM.useOrderedReductions(RdxDesc), ScaleFactor);
8088 } else {
8089 // TODO: Currently fixed-order recurrences are modeled as chains of
8090 // first-order recurrences. If there are no users of the intermediate
8091 // recurrences in the chain, the fixed order recurrence should be modeled
8092 // directly, enabling more efficient codegen.
8093 PhiRecipe = new VPFirstOrderRecurrencePHIRecipe(Phi, *StartV);
8094 }
8095 // Add backedge value.
8096 PhiRecipe->addOperand(Operands[1]);
8097 return PhiRecipe;
8098 }
8099 assert(!R->isPhi() && "only VPPhi nodes expected at this point");
8100
8101 if (isa<TruncInst>(Instr) && (Recipe = tryToOptimizeInductionTruncate(
8102 cast<TruncInst>(Instr), Operands, Range)))
8103 return Recipe;
8104
8105 // All widen recipes below deal only with VF > 1.
8107 [&](ElementCount VF) { return VF.isScalar(); }, Range))
8108 return nullptr;
8109
8110 if (auto *CI = dyn_cast<CallInst>(Instr))
8111 return tryToWidenCall(CI, Operands, Range);
8112
8113 if (StoreInst *SI = dyn_cast<StoreInst>(Instr))
8114 if (auto HistInfo = Legal->getHistogramInfo(SI))
8115 return tryToWidenHistogram(*HistInfo, Operands);
8116
8117 if (isa<LoadInst>(Instr) || isa<StoreInst>(Instr))
8118 return tryToWidenMemory(Instr, Operands, Range);
8119
8120 if (std::optional<unsigned> ScaleFactor = getScalingForReduction(Instr)) {
8121 if (auto PartialRed =
8122 tryToCreatePartialReduction(Instr, Operands, ScaleFactor.value()))
8123 return PartialRed;
8124 }
8125
8126 if (!shouldWiden(Instr, Range))
8127 return nullptr;
8128
8129 if (auto *GEP = dyn_cast<GetElementPtrInst>(Instr))
8130 return new VPWidenGEPRecipe(GEP, Operands);
8131
8132 if (auto *SI = dyn_cast<SelectInst>(Instr)) {
8133 return new VPWidenSelectRecipe(*SI, Operands);
8134 }
8135
8136 if (auto *CI = dyn_cast<CastInst>(Instr)) {
8137 return new VPWidenCastRecipe(CI->getOpcode(), Operands[0], CI->getType(),
8138 *CI);
8139 }
8140
8141 return tryToWiden(Instr, Operands);
8142}
8143
8146 ArrayRef<VPValue *> Operands,
8147 unsigned ScaleFactor) {
8148 assert(Operands.size() == 2 &&
8149 "Unexpected number of operands for partial reduction");
8150
8151 VPValue *BinOp = Operands[0];
8152 VPValue *Accumulator = Operands[1];
8153 VPRecipeBase *BinOpRecipe = BinOp->getDefiningRecipe();
8154 if (isa<VPReductionPHIRecipe>(BinOpRecipe) ||
8155 isa<VPPartialReductionRecipe>(BinOpRecipe))
8156 std::swap(BinOp, Accumulator);
8157
8158 if (ScaleFactor !=
8159 vputils::getVFScaleFactor(Accumulator->getDefiningRecipe()))
8160 return nullptr;
8161
8162 unsigned ReductionOpcode = Reduction->getOpcode();
8163 if (ReductionOpcode == Instruction::Sub) {
8164 auto *const Zero = ConstantInt::get(Reduction->getType(), 0);
8166 Ops.push_back(Plan.getOrAddLiveIn(Zero));
8167 Ops.push_back(BinOp);
8168 BinOp = new VPWidenRecipe(*Reduction, Ops);
8169 Builder.insert(BinOp->getDefiningRecipe());
8170 ReductionOpcode = Instruction::Add;
8171 }
8172
8173 VPValue *Cond = nullptr;
8174 if (CM.blockNeedsPredicationForAnyReason(Reduction->getParent())) {
8175 assert((ReductionOpcode == Instruction::Add ||
8176 ReductionOpcode == Instruction::Sub) &&
8177 "Expected an ADD or SUB operation for predicated partial "
8178 "reductions (because the neutral element in the mask is zero)!");
8179 Cond = getBlockInMask(Builder.getInsertBlock());
8180 VPValue *Zero = Plan.getConstantInt(Reduction->getType(), 0);
8181 BinOp = Builder.createSelect(Cond, BinOp, Zero, Reduction->getDebugLoc());
8182 }
8183 return new VPPartialReductionRecipe(ReductionOpcode, Accumulator, BinOp, Cond,
8184 ScaleFactor, Reduction);
8185}
8186
8187void LoopVectorizationPlanner::buildVPlansWithVPRecipes(ElementCount MinVF,
8188 ElementCount MaxVF) {
8189 if (ElementCount::isKnownGT(MinVF, MaxVF))
8190 return;
8191
8192 assert(OrigLoop->isInnermost() && "Inner loop expected.");
8193
8194 const LoopAccessInfo *LAI = Legal->getLAI();
8196 OrigLoop, LI, DT, PSE.getSE());
8197 if (!LAI->getRuntimePointerChecking()->getChecks().empty() &&
8199 // Only use noalias metadata when using memory checks guaranteeing no
8200 // overlap across all iterations.
8201 LVer.prepareNoAliasMetadata();
8202 }
8203
8204 // Create initial base VPlan0, to serve as common starting point for all
8205 // candidates built later for specific VF ranges.
8206 auto VPlan0 = VPlanTransforms::buildVPlan0(
8207 OrigLoop, *LI, Legal->getWidestInductionType(),
8208 getDebugLocFromInstOrOperands(Legal->getPrimaryInduction()), PSE);
8209
8210 auto MaxVFTimes2 = MaxVF * 2;
8211 for (ElementCount VF = MinVF; ElementCount::isKnownLT(VF, MaxVFTimes2);) {
8212 VFRange SubRange = {VF, MaxVFTimes2};
8213 if (auto Plan = tryToBuildVPlanWithVPRecipes(
8214 std::unique_ptr<VPlan>(VPlan0->duplicate()), SubRange, &LVer)) {
8215 // Now optimize the initial VPlan.
8217 *Plan, CM.getMinimalBitwidths());
8219 // TODO: try to put it close to addActiveLaneMask().
8220 if (CM.foldTailWithEVL())
8222 *Plan, CM.getMaxSafeElements());
8223 assert(verifyVPlanIsValid(*Plan) && "VPlan is invalid");
8224 VPlans.push_back(std::move(Plan));
8225 }
8226 VF = SubRange.End;
8227 }
8228}
8229
8230VPlanPtr LoopVectorizationPlanner::tryToBuildVPlanWithVPRecipes(
8231 VPlanPtr Plan, VFRange &Range, LoopVersioning *LVer) {
8232
8233 using namespace llvm::VPlanPatternMatch;
8234 SmallPtrSet<const InterleaveGroup<Instruction> *, 1> InterleaveGroups;
8235
8236 // ---------------------------------------------------------------------------
8237 // Build initial VPlan: Scan the body of the loop in a topological order to
8238 // visit each basic block after having visited its predecessor basic blocks.
8239 // ---------------------------------------------------------------------------
8240
8241 bool RequiresScalarEpilogueCheck =
8243 [this](ElementCount VF) {
8244 return !CM.requiresScalarEpilogue(VF.isVector());
8245 },
8246 Range);
8247 VPlanTransforms::handleEarlyExits(*Plan, Legal->hasUncountableEarlyExit());
8248 VPlanTransforms::addMiddleCheck(*Plan, RequiresScalarEpilogueCheck,
8249 CM.foldTailByMasking());
8250
8252
8253 // Don't use getDecisionAndClampRange here, because we don't know the UF
8254 // so this function is better to be conservative, rather than to split
8255 // it up into different VPlans.
8256 // TODO: Consider using getDecisionAndClampRange here to split up VPlans.
8257 bool IVUpdateMayOverflow = false;
8258 for (ElementCount VF : Range)
8259 IVUpdateMayOverflow |= !isIndvarOverflowCheckKnownFalse(&CM, VF);
8260
8261 TailFoldingStyle Style = CM.getTailFoldingStyle(IVUpdateMayOverflow);
8262 // Use NUW for the induction increment if we proved that it won't overflow in
8263 // the vector loop or when not folding the tail. In the later case, we know
8264 // that the canonical induction increment will not overflow as the vector trip
8265 // count is >= increment and a multiple of the increment.
8266 VPRegionBlock *LoopRegion = Plan->getVectorLoopRegion();
8267 bool HasNUW = !IVUpdateMayOverflow || Style == TailFoldingStyle::None;
8268 if (!HasNUW) {
8269 auto *IVInc =
8270 LoopRegion->getExitingBasicBlock()->getTerminator()->getOperand(0);
8271 assert(match(IVInc,
8272 m_VPInstruction<Instruction::Add>(
8273 m_Specific(LoopRegion->getCanonicalIV()), m_VPValue())) &&
8274 "Did not find the canonical IV increment");
8275 cast<VPRecipeWithIRFlags>(IVInc)->dropPoisonGeneratingFlags();
8276 }
8277
8278 // ---------------------------------------------------------------------------
8279 // Pre-construction: record ingredients whose recipes we'll need to further
8280 // process after constructing the initial VPlan.
8281 // ---------------------------------------------------------------------------
8282
8283 // For each interleave group which is relevant for this (possibly trimmed)
8284 // Range, add it to the set of groups to be later applied to the VPlan and add
8285 // placeholders for its members' Recipes which we'll be replacing with a
8286 // single VPInterleaveRecipe.
8287 for (InterleaveGroup<Instruction> *IG : IAI.getInterleaveGroups()) {
8288 auto ApplyIG = [IG, this](ElementCount VF) -> bool {
8289 bool Result = (VF.isVector() && // Query is illegal for VF == 1
8290 CM.getWideningDecision(IG->getInsertPos(), VF) ==
8292 // For scalable vectors, the interleave factors must be <= 8 since we
8293 // require the (de)interleaveN intrinsics instead of shufflevectors.
8294 assert((!Result || !VF.isScalable() || IG->getFactor() <= 8) &&
8295 "Unsupported interleave factor for scalable vectors");
8296 return Result;
8297 };
8298 if (!getDecisionAndClampRange(ApplyIG, Range))
8299 continue;
8300 InterleaveGroups.insert(IG);
8301 }
8302
8303 // ---------------------------------------------------------------------------
8304 // Predicate and linearize the top-level loop region.
8305 // ---------------------------------------------------------------------------
8306 auto BlockMaskCache = VPlanTransforms::introduceMasksAndLinearize(
8307 *Plan, CM.foldTailByMasking());
8308
8309 // ---------------------------------------------------------------------------
8310 // Construct wide recipes and apply predication for original scalar
8311 // VPInstructions in the loop.
8312 // ---------------------------------------------------------------------------
8313 VPRecipeBuilder RecipeBuilder(*Plan, OrigLoop, TLI, &TTI, Legal, CM, PSE,
8314 Builder, BlockMaskCache, LVer);
8315 RecipeBuilder.collectScaledReductions(Range);
8316
8317 // Scan the body of the loop in a topological order to visit each basic block
8318 // after having visited its predecessor basic blocks.
8319 VPBasicBlock *HeaderVPBB = LoopRegion->getEntryBasicBlock();
8320 ReversePostOrderTraversal<VPBlockShallowTraversalWrapper<VPBlockBase *>> RPOT(
8321 HeaderVPBB);
8322
8323 auto *MiddleVPBB = Plan->getMiddleBlock();
8324 VPBasicBlock::iterator MBIP = MiddleVPBB->getFirstNonPhi();
8325 // Mapping from VPValues in the initial plan to their widened VPValues. Needed
8326 // temporarily to update created block masks.
8327 DenseMap<VPValue *, VPValue *> Old2New;
8328 for (VPBasicBlock *VPBB : VPBlockUtils::blocksOnly<VPBasicBlock>(RPOT)) {
8329 // Convert input VPInstructions to widened recipes.
8330 for (VPRecipeBase &R : make_early_inc_range(*VPBB)) {
8331 auto *SingleDef = cast<VPSingleDefRecipe>(&R);
8332 auto *UnderlyingValue = SingleDef->getUnderlyingValue();
8333 // Skip recipes that do not need transforming, including canonical IV,
8334 // wide canonical IV and VPInstructions without underlying values. The
8335 // latter are added above for masking.
8336 // FIXME: Migrate code relying on the underlying instruction from VPlan0
8337 // to construct recipes below to not use the underlying instruction.
8339 &R) ||
8340 (isa<VPInstruction>(&R) && !UnderlyingValue))
8341 continue;
8342 assert(isa<VPInstruction>(&R) && UnderlyingValue && "unsupported recipe");
8343
8344 // TODO: Gradually replace uses of underlying instruction by analyses on
8345 // VPlan.
8346 Instruction *Instr = cast<Instruction>(UnderlyingValue);
8347 Builder.setInsertPoint(SingleDef);
8348
8349 // The stores with invariant address inside the loop will be deleted, and
8350 // in the exit block, a uniform store recipe will be created for the final
8351 // invariant store of the reduction.
8352 StoreInst *SI;
8353 if ((SI = dyn_cast<StoreInst>(Instr)) &&
8354 Legal->isInvariantAddressOfReduction(SI->getPointerOperand())) {
8355 // Only create recipe for the final invariant store of the reduction.
8356 if (Legal->isInvariantStoreOfReduction(SI)) {
8357 auto *Recipe =
8358 new VPReplicateRecipe(SI, R.operands(), true /* IsUniform */,
8359 nullptr /*Mask*/, VPIRMetadata(*SI, LVer));
8360 Recipe->insertBefore(*MiddleVPBB, MBIP);
8361 }
8362 R.eraseFromParent();
8363 continue;
8364 }
8365
8366 VPRecipeBase *Recipe =
8367 RecipeBuilder.tryToCreateWidenRecipe(SingleDef, Range);
8368 if (!Recipe)
8369 Recipe = RecipeBuilder.handleReplication(Instr, R.operands(), Range);
8370
8371 RecipeBuilder.setRecipe(Instr, Recipe);
8372 if (isa<VPWidenIntOrFpInductionRecipe>(Recipe) && isa<TruncInst>(Instr)) {
8373 // Optimized a truncate to VPWidenIntOrFpInductionRecipe. It needs to be
8374 // moved to the phi section in the header.
8375 Recipe->insertBefore(*HeaderVPBB, HeaderVPBB->getFirstNonPhi());
8376 } else {
8377 Builder.insert(Recipe);
8378 }
8379 if (Recipe->getNumDefinedValues() == 1) {
8380 SingleDef->replaceAllUsesWith(Recipe->getVPSingleValue());
8381 Old2New[SingleDef] = Recipe->getVPSingleValue();
8382 } else {
8383 assert(Recipe->getNumDefinedValues() == 0 &&
8384 "Unexpected multidef recipe");
8385 R.eraseFromParent();
8386 }
8387 }
8388 }
8389
8390 // replaceAllUsesWith above may invalidate the block masks. Update them here.
8391 // TODO: Include the masks as operands in the predicated VPlan directly
8392 // to remove the need to keep a map of masks beyond the predication
8393 // transform.
8394 RecipeBuilder.updateBlockMaskCache(Old2New);
8395 for (VPValue *Old : Old2New.keys())
8396 Old->getDefiningRecipe()->eraseFromParent();
8397
8398 assert(isa<VPRegionBlock>(LoopRegion) &&
8399 !LoopRegion->getEntryBasicBlock()->empty() &&
8400 "entry block must be set to a VPRegionBlock having a non-empty entry "
8401 "VPBasicBlock");
8402
8403 // Update wide induction increments to use the same step as the corresponding
8404 // wide induction. This enables detecting induction increments directly in
8405 // VPlan and removes redundant splats.
8406 for (const auto &[Phi, ID] : Legal->getInductionVars()) {
8407 auto *IVInc = cast<Instruction>(
8408 Phi->getIncomingValueForBlock(OrigLoop->getLoopLatch()));
8409 if (IVInc->getOperand(0) != Phi || IVInc->getOpcode() != Instruction::Add)
8410 continue;
8411 VPWidenInductionRecipe *WideIV =
8412 cast<VPWidenInductionRecipe>(RecipeBuilder.getRecipe(Phi));
8413 VPRecipeBase *R = RecipeBuilder.getRecipe(IVInc);
8414 R->setOperand(1, WideIV->getStepValue());
8415 }
8416
8417 // TODO: We can't call runPass on these transforms yet, due to verifier
8418 // failures.
8420 DenseMap<VPValue *, VPValue *> IVEndValues;
8421 VPlanTransforms::addScalarResumePhis(*Plan, RecipeBuilder, IVEndValues);
8422
8423 // ---------------------------------------------------------------------------
8424 // Transform initial VPlan: Apply previously taken decisions, in order, to
8425 // bring the VPlan to its final state.
8426 // ---------------------------------------------------------------------------
8427
8428 // Adjust the recipes for any inloop reductions.
8429 adjustRecipesForReductions(Plan, RecipeBuilder, Range.Start);
8430
8431 // Apply mandatory transformation to handle FP maxnum/minnum reduction with
8432 // NaNs if possible, bail out otherwise.
8434 *Plan))
8435 return nullptr;
8436
8437 // Transform recipes to abstract recipes if it is legal and beneficial and
8438 // clamp the range for better cost estimation.
8439 // TODO: Enable following transform when the EVL-version of extended-reduction
8440 // and mulacc-reduction are implemented.
8441 if (!CM.foldTailWithEVL()) {
8442 VPCostContext CostCtx(CM.TTI, *CM.TLI, *Plan, CM, CM.CostKind,
8443 *CM.PSE.getSE(), OrigLoop);
8445 CostCtx, Range);
8446 }
8447
8448 for (ElementCount VF : Range)
8449 Plan->addVF(VF);
8450 Plan->setName("Initial VPlan");
8451
8452 // Interleave memory: for each Interleave Group we marked earlier as relevant
8453 // for this VPlan, replace the Recipes widening its memory instructions with a
8454 // single VPInterleaveRecipe at its insertion point.
8456 InterleaveGroups, RecipeBuilder,
8457 CM.isScalarEpilogueAllowed());
8458
8459 // Replace VPValues for known constant strides.
8461 Legal->getLAI()->getSymbolicStrides());
8462
8463 auto BlockNeedsPredication = [this](BasicBlock *BB) {
8464 return Legal->blockNeedsPredication(BB);
8465 };
8467 BlockNeedsPredication);
8468
8469 // Sink users of fixed-order recurrence past the recipe defining the previous
8470 // value and introduce FirstOrderRecurrenceSplice VPInstructions.
8472 *Plan, Builder))
8473 return nullptr;
8474
8475 if (useActiveLaneMask(Style)) {
8476 // TODO: Move checks to VPlanTransforms::addActiveLaneMask once
8477 // TailFoldingStyle is visible there.
8478 bool ForControlFlow = useActiveLaneMaskForControlFlow(Style);
8479 bool WithoutRuntimeCheck =
8480 Style == TailFoldingStyle::DataAndControlFlowWithoutRuntimeCheck;
8481 VPlanTransforms::addActiveLaneMask(*Plan, ForControlFlow,
8482 WithoutRuntimeCheck);
8483 }
8484 VPlanTransforms::optimizeInductionExitUsers(*Plan, IVEndValues, *PSE.getSE());
8485
8486 assert(verifyVPlanIsValid(*Plan) && "VPlan is invalid");
8487 return Plan;
8488}
8489
8490VPlanPtr LoopVectorizationPlanner::tryToBuildVPlan(VFRange &Range) {
8491 // Outer loop handling: They may require CFG and instruction level
8492 // transformations before even evaluating whether vectorization is profitable.
8493 // Since we cannot modify the incoming IR, we need to build VPlan upfront in
8494 // the vectorization pipeline.
8495 assert(!OrigLoop->isInnermost());
8496 assert(EnableVPlanNativePath && "VPlan-native path is not enabled.");
8497
8498 auto Plan = VPlanTransforms::buildVPlan0(
8499 OrigLoop, *LI, Legal->getWidestInductionType(),
8500 getDebugLocFromInstOrOperands(Legal->getPrimaryInduction()), PSE);
8502 /*HasUncountableExit*/ false);
8503 VPlanTransforms::addMiddleCheck(*Plan, /*RequiresScalarEpilogue*/ true,
8504 /*TailFolded*/ false);
8505
8507
8508 for (ElementCount VF : Range)
8509 Plan->addVF(VF);
8510
8512 *Plan,
8513 [this](PHINode *P) {
8514 return Legal->getIntOrFpInductionDescriptor(P);
8515 },
8516 *TLI))
8517 return nullptr;
8518
8519 // Collect mapping of IR header phis to header phi recipes, to be used in
8520 // addScalarResumePhis.
8521 DenseMap<VPBasicBlock *, VPValue *> BlockMaskCache;
8522 VPRecipeBuilder RecipeBuilder(*Plan, OrigLoop, TLI, &TTI, Legal, CM, PSE,
8523 Builder, BlockMaskCache, nullptr /*LVer*/);
8524 for (auto &R : Plan->getVectorLoopRegion()->getEntryBasicBlock()->phis()) {
8526 continue;
8527 auto *HeaderR = cast<VPHeaderPHIRecipe>(&R);
8528 RecipeBuilder.setRecipe(HeaderR->getUnderlyingInstr(), HeaderR);
8529 }
8530 DenseMap<VPValue *, VPValue *> IVEndValues;
8531 // TODO: IVEndValues are not used yet in the native path, to optimize exit
8532 // values.
8533 // TODO: We can't call runPass on the transform yet, due to verifier
8534 // failures.
8535 VPlanTransforms::addScalarResumePhis(*Plan, RecipeBuilder, IVEndValues);
8536
8537 assert(verifyVPlanIsValid(*Plan) && "VPlan is invalid");
8538 return Plan;
8539}
8540
8541// Adjust the recipes for reductions. For in-loop reductions the chain of
8542// instructions leading from the loop exit instr to the phi need to be converted
8543// to reductions, with one operand being vector and the other being the scalar
8544// reduction chain. For other reductions, a select is introduced between the phi
8545// and users outside the vector region when folding the tail.
8546//
8547// A ComputeReductionResult recipe is added to the middle block, also for
8548// in-loop reductions which compute their result in-loop, because generating
8549// the subsequent bc.merge.rdx phi is driven by ComputeReductionResult recipes.
8550//
8551// Adjust AnyOf reductions; replace the reduction phi for the selected value
8552// with a boolean reduction phi node to check if the condition is true in any
8553// iteration. The final value is selected by the final ComputeReductionResult.
8554void LoopVectorizationPlanner::adjustRecipesForReductions(
8555 VPlanPtr &Plan, VPRecipeBuilder &RecipeBuilder, ElementCount MinVF) {
8556 using namespace VPlanPatternMatch;
8557 VPRegionBlock *VectorLoopRegion = Plan->getVectorLoopRegion();
8558 VPBasicBlock *Header = VectorLoopRegion->getEntryBasicBlock();
8559 VPBasicBlock *MiddleVPBB = Plan->getMiddleBlock();
8561
8562 for (VPRecipeBase &R : Header->phis()) {
8563 auto *PhiR = dyn_cast<VPReductionPHIRecipe>(&R);
8564 if (!PhiR || !PhiR->isInLoop() || (MinVF.isScalar() && !PhiR->isOrdered()))
8565 continue;
8566
8567 RecurKind Kind = PhiR->getRecurrenceKind();
8568 assert(
8571 "AnyOf and FindIV reductions are not allowed for in-loop reductions");
8572
8573 // Collect the chain of "link" recipes for the reduction starting at PhiR.
8574 SetVector<VPSingleDefRecipe *> Worklist;
8575 Worklist.insert(PhiR);
8576 for (unsigned I = 0; I != Worklist.size(); ++I) {
8577 VPSingleDefRecipe *Cur = Worklist[I];
8578 for (VPUser *U : Cur->users()) {
8579 auto *UserRecipe = cast<VPSingleDefRecipe>(U);
8580 if (!UserRecipe->getParent()->getEnclosingLoopRegion()) {
8581 assert((UserRecipe->getParent() == MiddleVPBB ||
8582 UserRecipe->getParent() == Plan->getScalarPreheader()) &&
8583 "U must be either in the loop region, the middle block or the "
8584 "scalar preheader.");
8585 continue;
8586 }
8587 Worklist.insert(UserRecipe);
8588 }
8589 }
8590
8591 // Visit operation "Links" along the reduction chain top-down starting from
8592 // the phi until LoopExitValue. We keep track of the previous item
8593 // (PreviousLink) to tell which of the two operands of a Link will remain
8594 // scalar and which will be reduced. For minmax by select(cmp), Link will be
8595 // the select instructions. Blend recipes of in-loop reduction phi's will
8596 // get folded to their non-phi operand, as the reduction recipe handles the
8597 // condition directly.
8598 VPSingleDefRecipe *PreviousLink = PhiR; // Aka Worklist[0].
8599 for (VPSingleDefRecipe *CurrentLink : drop_begin(Worklist)) {
8600 if (auto *Blend = dyn_cast<VPBlendRecipe>(CurrentLink)) {
8601 assert(Blend->getNumIncomingValues() == 2 &&
8602 "Blend must have 2 incoming values");
8603 if (Blend->getIncomingValue(0) == PhiR) {
8604 Blend->replaceAllUsesWith(Blend->getIncomingValue(1));
8605 } else {
8606 assert(Blend->getIncomingValue(1) == PhiR &&
8607 "PhiR must be an operand of the blend");
8608 Blend->replaceAllUsesWith(Blend->getIncomingValue(0));
8609 }
8610 continue;
8611 }
8612
8613 Instruction *CurrentLinkI = CurrentLink->getUnderlyingInstr();
8614
8615 // Index of the first operand which holds a non-mask vector operand.
8616 unsigned IndexOfFirstOperand;
8617 // Recognize a call to the llvm.fmuladd intrinsic.
8618 bool IsFMulAdd = (Kind == RecurKind::FMulAdd);
8619 VPValue *VecOp;
8620 VPBasicBlock *LinkVPBB = CurrentLink->getParent();
8621 if (IsFMulAdd) {
8622 assert(
8624 "Expected instruction to be a call to the llvm.fmuladd intrinsic");
8625 assert(((MinVF.isScalar() && isa<VPReplicateRecipe>(CurrentLink)) ||
8626 isa<VPWidenIntrinsicRecipe>(CurrentLink)) &&
8627 CurrentLink->getOperand(2) == PreviousLink &&
8628 "expected a call where the previous link is the added operand");
8629
8630 // If the instruction is a call to the llvm.fmuladd intrinsic then we
8631 // need to create an fmul recipe (multiplying the first two operands of
8632 // the fmuladd together) to use as the vector operand for the fadd
8633 // reduction.
8634 VPInstruction *FMulRecipe = new VPInstruction(
8635 Instruction::FMul,
8636 {CurrentLink->getOperand(0), CurrentLink->getOperand(1)},
8637 CurrentLinkI->getFastMathFlags());
8638 LinkVPBB->insert(FMulRecipe, CurrentLink->getIterator());
8639 VecOp = FMulRecipe;
8640 } else if (PhiR->isInLoop() && Kind == RecurKind::AddChainWithSubs &&
8641 CurrentLinkI->getOpcode() == Instruction::Sub) {
8642 Type *PhiTy = PhiR->getUnderlyingValue()->getType();
8643 auto *Zero = Plan->getConstantInt(PhiTy, 0);
8644 VPWidenRecipe *Sub = new VPWidenRecipe(
8645 Instruction::Sub, {Zero, CurrentLink->getOperand(1)}, {},
8646 VPIRMetadata(), CurrentLinkI->getDebugLoc());
8647 Sub->setUnderlyingValue(CurrentLinkI);
8648 LinkVPBB->insert(Sub, CurrentLink->getIterator());
8649 VecOp = Sub;
8650 } else {
8652 if (isa<VPWidenRecipe>(CurrentLink)) {
8653 assert(isa<CmpInst>(CurrentLinkI) &&
8654 "need to have the compare of the select");
8655 continue;
8656 }
8657 assert(isa<VPWidenSelectRecipe>(CurrentLink) &&
8658 "must be a select recipe");
8659 IndexOfFirstOperand = 1;
8660 } else {
8661 assert((MinVF.isScalar() || isa<VPWidenRecipe>(CurrentLink)) &&
8662 "Expected to replace a VPWidenSC");
8663 IndexOfFirstOperand = 0;
8664 }
8665 // Note that for non-commutable operands (cmp-selects), the semantics of
8666 // the cmp-select are captured in the recurrence kind.
8667 unsigned VecOpId =
8668 CurrentLink->getOperand(IndexOfFirstOperand) == PreviousLink
8669 ? IndexOfFirstOperand + 1
8670 : IndexOfFirstOperand;
8671 VecOp = CurrentLink->getOperand(VecOpId);
8672 assert(VecOp != PreviousLink &&
8673 CurrentLink->getOperand(CurrentLink->getNumOperands() - 1 -
8674 (VecOpId - IndexOfFirstOperand)) ==
8675 PreviousLink &&
8676 "PreviousLink must be the operand other than VecOp");
8677 }
8678
8679 VPValue *CondOp = nullptr;
8680 if (CM.blockNeedsPredicationForAnyReason(CurrentLinkI->getParent()))
8681 CondOp = RecipeBuilder.getBlockInMask(CurrentLink->getParent());
8682
8683 // TODO: Retrieve FMFs from recipes directly.
8684 RecurrenceDescriptor RdxDesc = Legal->getRecurrenceDescriptor(
8685 cast<PHINode>(PhiR->getUnderlyingInstr()));
8686 // Non-FP RdxDescs will have all fast math flags set, so clear them.
8687 FastMathFlags FMFs = isa<FPMathOperator>(CurrentLinkI)
8688 ? RdxDesc.getFastMathFlags()
8689 : FastMathFlags();
8690 auto *RedRecipe = new VPReductionRecipe(
8691 Kind, FMFs, CurrentLinkI, PreviousLink, VecOp, CondOp,
8692 PhiR->isOrdered(), CurrentLinkI->getDebugLoc());
8693 // Append the recipe to the end of the VPBasicBlock because we need to
8694 // ensure that it comes after all of it's inputs, including CondOp.
8695 // Delete CurrentLink as it will be invalid if its operand is replaced
8696 // with a reduction defined at the bottom of the block in the next link.
8697 if (LinkVPBB->getNumSuccessors() == 0)
8698 RedRecipe->insertBefore(&*std::prev(std::prev(LinkVPBB->end())));
8699 else
8700 LinkVPBB->appendRecipe(RedRecipe);
8701
8702 CurrentLink->replaceAllUsesWith(RedRecipe);
8703 ToDelete.push_back(CurrentLink);
8704 PreviousLink = RedRecipe;
8705 }
8706 }
8707 VPBasicBlock *LatchVPBB = VectorLoopRegion->getExitingBasicBlock();
8708 Builder.setInsertPoint(&*std::prev(std::prev(LatchVPBB->end())));
8709 VPBasicBlock::iterator IP = MiddleVPBB->getFirstNonPhi();
8710 for (VPRecipeBase &R :
8711 Plan->getVectorLoopRegion()->getEntryBasicBlock()->phis()) {
8712 VPReductionPHIRecipe *PhiR = dyn_cast<VPReductionPHIRecipe>(&R);
8713 if (!PhiR)
8714 continue;
8715
8716 const RecurrenceDescriptor &RdxDesc = Legal->getRecurrenceDescriptor(
8718 Type *PhiTy = PhiR->getUnderlyingValue()->getType();
8719 // If tail is folded by masking, introduce selects between the phi
8720 // and the users outside the vector region of each reduction, at the
8721 // beginning of the dedicated latch block.
8722 auto *OrigExitingVPV = PhiR->getBackedgeValue();
8723 auto *NewExitingVPV = PhiR->getBackedgeValue();
8724 // Don't output selects for partial reductions because they have an output
8725 // with fewer lanes than the VF. So the operands of the select would have
8726 // different numbers of lanes. Partial reductions mask the input instead.
8727 if (!PhiR->isInLoop() && CM.foldTailByMasking() &&
8728 !isa<VPPartialReductionRecipe>(OrigExitingVPV->getDefiningRecipe())) {
8729 VPValue *Cond = RecipeBuilder.getBlockInMask(PhiR->getParent());
8730 std::optional<FastMathFlags> FMFs =
8731 PhiTy->isFloatingPointTy()
8732 ? std::make_optional(RdxDesc.getFastMathFlags())
8733 : std::nullopt;
8734 NewExitingVPV =
8735 Builder.createSelect(Cond, OrigExitingVPV, PhiR, {}, "", FMFs);
8736 OrigExitingVPV->replaceUsesWithIf(NewExitingVPV, [](VPUser &U, unsigned) {
8737 return isa<VPInstruction>(&U) &&
8738 (cast<VPInstruction>(&U)->getOpcode() ==
8740 cast<VPInstruction>(&U)->getOpcode() ==
8742 cast<VPInstruction>(&U)->getOpcode() ==
8744 });
8745 if (CM.usePredicatedReductionSelect())
8746 PhiR->setOperand(1, NewExitingVPV);
8747 }
8748
8749 // We want code in the middle block to appear to execute on the location of
8750 // the scalar loop's latch terminator because: (a) it is all compiler
8751 // generated, (b) these instructions are always executed after evaluating
8752 // the latch conditional branch, and (c) other passes may add new
8753 // predecessors which terminate on this line. This is the easiest way to
8754 // ensure we don't accidentally cause an extra step back into the loop while
8755 // debugging.
8756 DebugLoc ExitDL = OrigLoop->getLoopLatch()->getTerminator()->getDebugLoc();
8757
8758 // TODO: At the moment ComputeReductionResult also drives creation of the
8759 // bc.merge.rdx phi nodes, hence it needs to be created unconditionally here
8760 // even for in-loop reductions, until the reduction resume value handling is
8761 // also modeled in VPlan.
8762 VPInstruction *FinalReductionResult;
8763 VPBuilder::InsertPointGuard Guard(Builder);
8764 Builder.setInsertPoint(MiddleVPBB, IP);
8765 RecurKind RecurrenceKind = PhiR->getRecurrenceKind();
8767 VPValue *Start = PhiR->getStartValue();
8768 VPValue *Sentinel = Plan->getOrAddLiveIn(RdxDesc.getSentinelValue());
8769 FinalReductionResult =
8770 Builder.createNaryOp(VPInstruction::ComputeFindIVResult,
8771 {PhiR, Start, Sentinel, NewExitingVPV}, ExitDL);
8772 } else if (RecurrenceDescriptor::isAnyOfRecurrenceKind(RecurrenceKind)) {
8773 VPValue *Start = PhiR->getStartValue();
8774 FinalReductionResult =
8775 Builder.createNaryOp(VPInstruction::ComputeAnyOfResult,
8776 {PhiR, Start, NewExitingVPV}, ExitDL);
8777 } else {
8778 VPIRFlags Flags =
8780 ? VPIRFlags(RdxDesc.getFastMathFlags())
8781 : VPIRFlags();
8782 FinalReductionResult =
8783 Builder.createNaryOp(VPInstruction::ComputeReductionResult,
8784 {PhiR, NewExitingVPV}, Flags, ExitDL);
8785 }
8786 // If the vector reduction can be performed in a smaller type, we truncate
8787 // then extend the loop exit value to enable InstCombine to evaluate the
8788 // entire expression in the smaller type.
8789 if (MinVF.isVector() && PhiTy != RdxDesc.getRecurrenceType() &&
8791 assert(!PhiR->isInLoop() && "Unexpected truncated inloop reduction!");
8793 "Unexpected truncated min-max recurrence!");
8794 Type *RdxTy = RdxDesc.getRecurrenceType();
8795 VPWidenCastRecipe *Trunc;
8796 Instruction::CastOps ExtendOpc =
8797 RdxDesc.isSigned() ? Instruction::SExt : Instruction::ZExt;
8798 VPWidenCastRecipe *Extnd;
8799 {
8800 VPBuilder::InsertPointGuard Guard(Builder);
8801 Builder.setInsertPoint(
8802 NewExitingVPV->getDefiningRecipe()->getParent(),
8803 std::next(NewExitingVPV->getDefiningRecipe()->getIterator()));
8804 Trunc =
8805 Builder.createWidenCast(Instruction::Trunc, NewExitingVPV, RdxTy);
8806 Extnd = Builder.createWidenCast(ExtendOpc, Trunc, PhiTy);
8807 }
8808 if (PhiR->getOperand(1) == NewExitingVPV)
8809 PhiR->setOperand(1, Extnd->getVPSingleValue());
8810
8811 // Update ComputeReductionResult with the truncated exiting value and
8812 // extend its result.
8813 FinalReductionResult->setOperand(1, Trunc);
8814 FinalReductionResult =
8815 Builder.createScalarCast(ExtendOpc, FinalReductionResult, PhiTy, {});
8816 }
8817
8818 // Update all users outside the vector region. Also replace redundant
8819 // ExtractLastElement.
8820 for (auto *U : to_vector(OrigExitingVPV->users())) {
8821 auto *Parent = cast<VPRecipeBase>(U)->getParent();
8822 if (FinalReductionResult == U || Parent->getParent())
8823 continue;
8824 U->replaceUsesOfWith(OrigExitingVPV, FinalReductionResult);
8826 cast<VPInstruction>(U)->replaceAllUsesWith(FinalReductionResult);
8827 }
8828
8829 // Adjust AnyOf reductions; replace the reduction phi for the selected value
8830 // with a boolean reduction phi node to check if the condition is true in
8831 // any iteration. The final value is selected by the final
8832 // ComputeReductionResult.
8833 if (RecurrenceDescriptor::isAnyOfRecurrenceKind(RecurrenceKind)) {
8834 auto *Select = cast<VPRecipeBase>(*find_if(PhiR->users(), [](VPUser *U) {
8835 return isa<VPWidenSelectRecipe>(U) ||
8836 (isa<VPReplicateRecipe>(U) &&
8837 cast<VPReplicateRecipe>(U)->getUnderlyingInstr()->getOpcode() ==
8838 Instruction::Select);
8839 }));
8840 VPValue *Cmp = Select->getOperand(0);
8841 // If the compare is checking the reduction PHI node, adjust it to check
8842 // the start value.
8843 if (VPRecipeBase *CmpR = Cmp->getDefiningRecipe())
8844 CmpR->replaceUsesOfWith(PhiR, PhiR->getStartValue());
8845 Builder.setInsertPoint(Select);
8846
8847 // If the true value of the select is the reduction phi, the new value is
8848 // selected if the negated condition is true in any iteration.
8849 if (Select->getOperand(1) == PhiR)
8850 Cmp = Builder.createNot(Cmp);
8851 VPValue *Or = Builder.createOr(PhiR, Cmp);
8852 Select->getVPSingleValue()->replaceAllUsesWith(Or);
8853 // Delete Select now that it has invalid types.
8854 ToDelete.push_back(Select);
8855
8856 // Convert the reduction phi to operate on bools.
8857 PhiR->setOperand(0, Plan->getFalse());
8858 continue;
8859 }
8860
8862 RdxDesc.getRecurrenceKind())) {
8863 // Adjust the start value for FindFirstIV/FindLastIV recurrences to use
8864 // the sentinel value after generating the ResumePhi recipe, which uses
8865 // the original start value.
8866 PhiR->setOperand(0, Plan->getOrAddLiveIn(RdxDesc.getSentinelValue()));
8867 }
8868 RecurKind RK = RdxDesc.getRecurrenceKind();
8872 VPBuilder PHBuilder(Plan->getVectorPreheader());
8873 VPValue *Iden = Plan->getOrAddLiveIn(
8874 getRecurrenceIdentity(RK, PhiTy, RdxDesc.getFastMathFlags()));
8875 // If the PHI is used by a partial reduction, set the scale factor.
8876 unsigned ScaleFactor =
8877 RecipeBuilder.getScalingForReduction(RdxDesc.getLoopExitInstr())
8878 .value_or(1);
8879 auto *ScaleFactorVPV = Plan->getConstantInt(32, ScaleFactor);
8880 VPValue *StartV = PHBuilder.createNaryOp(
8882 {PhiR->getStartValue(), Iden, ScaleFactorVPV},
8883 PhiTy->isFloatingPointTy() ? RdxDesc.getFastMathFlags()
8884 : FastMathFlags());
8885 PhiR->setOperand(0, StartV);
8886 }
8887 }
8888 for (VPRecipeBase *R : ToDelete)
8889 R->eraseFromParent();
8890
8892}
8893
8894void LoopVectorizationPlanner::attachRuntimeChecks(
8895 VPlan &Plan, GeneratedRTChecks &RTChecks, bool HasBranchWeights) const {
8896 const auto &[SCEVCheckCond, SCEVCheckBlock] = RTChecks.getSCEVChecks();
8897 if (SCEVCheckBlock && SCEVCheckBlock->hasNPredecessors(0)) {
8898 assert((!CM.OptForSize ||
8899 CM.Hints->getForce() == LoopVectorizeHints::FK_Enabled) &&
8900 "Cannot SCEV check stride or overflow when optimizing for size");
8901 VPlanTransforms::attachCheckBlock(Plan, SCEVCheckCond, SCEVCheckBlock,
8902 HasBranchWeights);
8903 }
8904 const auto &[MemCheckCond, MemCheckBlock] = RTChecks.getMemRuntimeChecks();
8905 if (MemCheckBlock && MemCheckBlock->hasNPredecessors(0)) {
8906 // VPlan-native path does not do any analysis for runtime checks
8907 // currently.
8908 assert((!EnableVPlanNativePath || OrigLoop->isInnermost()) &&
8909 "Runtime checks are not supported for outer loops yet");
8910
8911 if (CM.OptForSize) {
8912 assert(
8913 CM.Hints->getForce() == LoopVectorizeHints::FK_Enabled &&
8914 "Cannot emit memory checks when optimizing for size, unless forced "
8915 "to vectorize.");
8916 ORE->emit([&]() {
8917 return OptimizationRemarkAnalysis(DEBUG_TYPE, "VectorizationCodeSize",
8918 OrigLoop->getStartLoc(),
8919 OrigLoop->getHeader())
8920 << "Code-size may be reduced by not forcing "
8921 "vectorization, or by source-code modifications "
8922 "eliminating the need for runtime checks "
8923 "(e.g., adding 'restrict').";
8924 });
8925 }
8926 VPlanTransforms::attachCheckBlock(Plan, MemCheckCond, MemCheckBlock,
8927 HasBranchWeights);
8928 }
8929}
8930
8932 VPlan &Plan, ElementCount VF, unsigned UF,
8933 ElementCount MinProfitableTripCount) const {
8934 // vscale is not necessarily a power-of-2, which means we cannot guarantee
8935 // an overflow to zero when updating induction variables and so an
8936 // additional overflow check is required before entering the vector loop.
8937 bool IsIndvarOverflowCheckNeededForVF =
8938 VF.isScalable() && !TTI.isVScaleKnownToBeAPowerOfTwo() &&
8939 !isIndvarOverflowCheckKnownFalse(&CM, VF, UF) &&
8940 CM.getTailFoldingStyle() !=
8942 const uint32_t *BranchWeigths =
8943 hasBranchWeightMD(*OrigLoop->getLoopLatch()->getTerminator())
8945 : nullptr;
8947 Plan, VF, UF, MinProfitableTripCount,
8948 CM.requiresScalarEpilogue(VF.isVector()), CM.foldTailByMasking(),
8949 IsIndvarOverflowCheckNeededForVF, OrigLoop, BranchWeigths,
8950 OrigLoop->getLoopPredecessor()->getTerminator()->getDebugLoc(),
8951 *PSE.getSE());
8952}
8953
8955 assert(!State.Lane && "VPDerivedIVRecipe being replicated.");
8956
8957 // Fast-math-flags propagate from the original induction instruction.
8958 IRBuilder<>::FastMathFlagGuard FMFG(State.Builder);
8959 if (FPBinOp)
8960 State.Builder.setFastMathFlags(FPBinOp->getFastMathFlags());
8961
8962 Value *Step = State.get(getStepValue(), VPLane(0));
8963 Value *Index = State.get(getOperand(1), VPLane(0));
8964 Value *DerivedIV = emitTransformedIndex(
8965 State.Builder, Index, getStartValue()->getLiveInIRValue(), Step, Kind,
8967 DerivedIV->setName(Name);
8968 State.set(this, DerivedIV, VPLane(0));
8969}
8970
8971// Determine how to lower the scalar epilogue, which depends on 1) optimising
8972// for minimum code-size, 2) predicate compiler options, 3) loop hints forcing
8973// predication, and 4) a TTI hook that analyses whether the loop is suitable
8974// for predication.
8979 // 1) OptSize takes precedence over all other options, i.e. if this is set,
8980 // don't look at hints or options, and don't request a scalar epilogue.
8981 // (For PGSO, as shouldOptimizeForSize isn't currently accessible from
8982 // LoopAccessInfo (due to code dependency and not being able to reliably get
8983 // PSI/BFI from a loop analysis under NPM), we cannot suppress the collection
8984 // of strides in LoopAccessInfo::analyzeLoop() and vectorize without
8985 // versioning when the vectorization is forced, unlike hasOptSize. So revert
8986 // back to the old way and vectorize with versioning when forced. See D81345.)
8987 if (F->hasOptSize() || (llvm::shouldOptimizeForSize(L->getHeader(), PSI, BFI,
8991
8992 // 2) If set, obey the directives
8993 if (PreferPredicateOverEpilogue.getNumOccurrences()) {
9001 };
9002 }
9003
9004 // 3) If set, obey the hints
9005 switch (Hints.getPredicate()) {
9010 };
9011
9012 // 4) if the TTI hook indicates this is profitable, request predication.
9013 TailFoldingInfo TFI(TLI, &LVL, IAI);
9014 if (TTI->preferPredicateOverEpilogue(&TFI))
9016
9018}
9019
9020// Process the loop in the VPlan-native vectorization path. This path builds
9021// VPlan upfront in the vectorization pipeline, which allows to apply
9022// VPlan-to-VPlan transformations from the very beginning without modifying the
9023// input LLVM IR.
9030 LoopVectorizationRequirements &Requirements) {
9031
9033 LLVM_DEBUG(dbgs() << "LV: cannot compute the outer-loop trip count\n");
9034 return false;
9035 }
9036 assert(EnableVPlanNativePath && "VPlan-native path is disabled.");
9037 Function *F = L->getHeader()->getParent();
9038 InterleavedAccessInfo IAI(PSE, L, DT, LI, LVL->getLAI());
9039
9041 getScalarEpilogueLowering(F, L, Hints, PSI, BFI, TTI, TLI, *LVL, &IAI);
9042
9043 LoopVectorizationCostModel CM(SEL, L, PSE, LI, LVL, *TTI, TLI, DB, AC, ORE, F,
9044 &Hints, IAI, PSI, BFI);
9045 // Use the planner for outer loop vectorization.
9046 // TODO: CM is not used at this point inside the planner. Turn CM into an
9047 // optional argument if we don't need it in the future.
9048 LoopVectorizationPlanner LVP(L, LI, DT, TLI, *TTI, LVL, CM, IAI, PSE, Hints,
9049 ORE);
9050
9051 // Get user vectorization factor.
9052 ElementCount UserVF = Hints.getWidth();
9053
9055
9056 // Plan how to best vectorize, return the best VF and its cost.
9057 const VectorizationFactor VF = LVP.planInVPlanNativePath(UserVF);
9058
9059 // If we are stress testing VPlan builds, do not attempt to generate vector
9060 // code. Masked vector code generation support will follow soon.
9061 // Also, do not attempt to vectorize if no vector code will be produced.
9063 return false;
9064
9065 VPlan &BestPlan = LVP.getPlanFor(VF.Width);
9066
9067 {
9068 GeneratedRTChecks Checks(PSE, DT, LI, TTI, F->getDataLayout(), CM.CostKind);
9069 InnerLoopVectorizer LB(L, PSE, LI, DT, TTI, AC, VF.Width, /*UF=*/1, &CM,
9070 BFI, PSI, Checks, BestPlan);
9071 LLVM_DEBUG(dbgs() << "Vectorizing outer loop in \""
9072 << L->getHeader()->getParent()->getName() << "\"\n");
9073 LVP.addMinimumIterationCheck(BestPlan, VF.Width, /*UF=*/1,
9075
9076 LVP.executePlan(VF.Width, /*UF=*/1, BestPlan, LB, DT, false);
9077 }
9078
9079 reportVectorization(ORE, L, VF, 1);
9080
9081 assert(!verifyFunction(*L->getHeader()->getParent(), &dbgs()));
9082 return true;
9083}
9084
9085// Emit a remark if there are stores to floats that required a floating point
9086// extension. If the vectorized loop was generated with floating point there
9087// will be a performance penalty from the conversion overhead and the change in
9088// the vector width.
9091 for (BasicBlock *BB : L->getBlocks()) {
9092 for (Instruction &Inst : *BB) {
9093 if (auto *S = dyn_cast<StoreInst>(&Inst)) {
9094 if (S->getValueOperand()->getType()->isFloatTy())
9095 Worklist.push_back(S);
9096 }
9097 }
9098 }
9099
9100 // Traverse the floating point stores upwards searching, for floating point
9101 // conversions.
9104 while (!Worklist.empty()) {
9105 auto *I = Worklist.pop_back_val();
9106 if (!L->contains(I))
9107 continue;
9108 if (!Visited.insert(I).second)
9109 continue;
9110
9111 // Emit a remark if the floating point store required a floating
9112 // point conversion.
9113 // TODO: More work could be done to identify the root cause such as a
9114 // constant or a function return type and point the user to it.
9115 if (isa<FPExtInst>(I) && EmittedRemark.insert(I).second)
9116 ORE->emit([&]() {
9117 return OptimizationRemarkAnalysis(LV_NAME, "VectorMixedPrecision",
9118 I->getDebugLoc(), L->getHeader())
9119 << "floating point conversion changes vector width. "
9120 << "Mixed floating point precision requires an up/down "
9121 << "cast that will negatively impact performance.";
9122 });
9123
9124 for (Use &Op : I->operands())
9125 if (auto *OpI = dyn_cast<Instruction>(Op))
9126 Worklist.push_back(OpI);
9127 }
9128}
9129
9130/// For loops with uncountable early exits, find the cost of doing work when
9131/// exiting the loop early, such as calculating the final exit values of
9132/// variables used outside the loop.
9133/// TODO: This is currently overly pessimistic because the loop may not take
9134/// the early exit, but better to keep this conservative for now. In future,
9135/// it might be possible to relax this by using branch probabilities.
9137 VPlan &Plan, ElementCount VF) {
9138 InstructionCost Cost = 0;
9139 for (auto *ExitVPBB : Plan.getExitBlocks()) {
9140 for (auto *PredVPBB : ExitVPBB->getPredecessors()) {
9141 // If the predecessor is not the middle.block, then it must be the
9142 // vector.early.exit block, which may contain work to calculate the exit
9143 // values of variables used outside the loop.
9144 if (PredVPBB != Plan.getMiddleBlock()) {
9145 LLVM_DEBUG(dbgs() << "Calculating cost of work in exit block "
9146 << PredVPBB->getName() << ":\n");
9147 Cost += PredVPBB->cost(VF, CostCtx);
9148 }
9149 }
9150 }
9151 return Cost;
9152}
9153
9154/// This function determines whether or not it's still profitable to vectorize
9155/// the loop given the extra work we have to do outside of the loop:
9156/// 1. Perform the runtime checks before entering the loop to ensure it's safe
9157/// to vectorize.
9158/// 2. In the case of loops with uncountable early exits, we may have to do
9159/// extra work when exiting the loop early, such as calculating the final
9160/// exit values of variables used outside the loop.
9161static bool isOutsideLoopWorkProfitable(GeneratedRTChecks &Checks,
9162 VectorizationFactor &VF, Loop *L,
9164 VPCostContext &CostCtx, VPlan &Plan,
9166 std::optional<unsigned> VScale) {
9167 InstructionCost TotalCost = Checks.getCost();
9168 if (!TotalCost.isValid())
9169 return false;
9170
9171 // Add on the cost of any work required in the vector early exit block, if
9172 // one exists.
9173 TotalCost += calculateEarlyExitCost(CostCtx, Plan, VF.Width);
9174
9175 // When interleaving only scalar and vector cost will be equal, which in turn
9176 // would lead to a divide by 0. Fall back to hard threshold.
9177 if (VF.Width.isScalar()) {
9178 // TODO: Should we rename VectorizeMemoryCheckThreshold?
9179 if (TotalCost > VectorizeMemoryCheckThreshold) {
9180 LLVM_DEBUG(
9181 dbgs()
9182 << "LV: Interleaving only is not profitable due to runtime checks\n");
9183 return false;
9184 }
9185 return true;
9186 }
9187
9188 // The scalar cost should only be 0 when vectorizing with a user specified
9189 // VF/IC. In those cases, runtime checks should always be generated.
9190 uint64_t ScalarC = VF.ScalarCost.getValue();
9191 if (ScalarC == 0)
9192 return true;
9193
9194 // First, compute the minimum iteration count required so that the vector
9195 // loop outperforms the scalar loop.
9196 // The total cost of the scalar loop is
9197 // ScalarC * TC
9198 // where
9199 // * TC is the actual trip count of the loop.
9200 // * ScalarC is the cost of a single scalar iteration.
9201 //
9202 // The total cost of the vector loop is
9203 // RtC + VecC * (TC / VF) + EpiC
9204 // where
9205 // * RtC is the cost of the generated runtime checks plus the cost of
9206 // performing any additional work in the vector.early.exit block for loops
9207 // with uncountable early exits.
9208 // * VecC is the cost of a single vector iteration.
9209 // * TC is the actual trip count of the loop
9210 // * VF is the vectorization factor
9211 // * EpiCost is the cost of the generated epilogue, including the cost
9212 // of the remaining scalar operations.
9213 //
9214 // Vectorization is profitable once the total vector cost is less than the
9215 // total scalar cost:
9216 // RtC + VecC * (TC / VF) + EpiC < ScalarC * TC
9217 //
9218 // Now we can compute the minimum required trip count TC as
9219 // VF * (RtC + EpiC) / (ScalarC * VF - VecC) < TC
9220 //
9221 // For now we assume the epilogue cost EpiC = 0 for simplicity. Note that
9222 // the computations are performed on doubles, not integers and the result
9223 // is rounded up, hence we get an upper estimate of the TC.
9224 unsigned IntVF = estimateElementCount(VF.Width, VScale);
9225 uint64_t RtC = TotalCost.getValue();
9226 uint64_t Div = ScalarC * IntVF - VF.Cost.getValue();
9227 uint64_t MinTC1 = Div == 0 ? 0 : divideCeil(RtC * IntVF, Div);
9228
9229 // Second, compute a minimum iteration count so that the cost of the
9230 // runtime checks is only a fraction of the total scalar loop cost. This
9231 // adds a loop-dependent bound on the overhead incurred if the runtime
9232 // checks fail. In case the runtime checks fail, the cost is RtC + ScalarC
9233 // * TC. To bound the runtime check to be a fraction 1/X of the scalar
9234 // cost, compute
9235 // RtC < ScalarC * TC * (1 / X) ==> RtC * X / ScalarC < TC
9236 uint64_t MinTC2 = divideCeil(RtC * 10, ScalarC);
9237
9238 // Now pick the larger minimum. If it is not a multiple of VF and a scalar
9239 // epilogue is allowed, choose the next closest multiple of VF. This should
9240 // partly compensate for ignoring the epilogue cost.
9241 uint64_t MinTC = std::max(MinTC1, MinTC2);
9242 if (SEL == CM_ScalarEpilogueAllowed)
9243 MinTC = alignTo(MinTC, IntVF);
9245
9246 LLVM_DEBUG(
9247 dbgs() << "LV: Minimum required TC for runtime checks to be profitable:"
9248 << VF.MinProfitableTripCount << "\n");
9249
9250 // Skip vectorization if the expected trip count is less than the minimum
9251 // required trip count.
9252 if (auto ExpectedTC = getSmallBestKnownTC(PSE, L)) {
9253 if (ElementCount::isKnownLT(*ExpectedTC, VF.MinProfitableTripCount)) {
9254 LLVM_DEBUG(dbgs() << "LV: Vectorization is not beneficial: expected "
9255 "trip count < minimum profitable VF ("
9256 << *ExpectedTC << " < " << VF.MinProfitableTripCount
9257 << ")\n");
9258
9259 return false;
9260 }
9261 }
9262 return true;
9263}
9264
9266 : InterleaveOnlyWhenForced(Opts.InterleaveOnlyWhenForced ||
9268 VectorizeOnlyWhenForced(Opts.VectorizeOnlyWhenForced ||
9270
9271/// Prepare \p MainPlan for vectorizing the main vector loop during epilogue
9272/// vectorization. Remove ResumePhis from \p MainPlan for inductions that
9273/// don't have a corresponding wide induction in \p EpiPlan.
9274static void preparePlanForMainVectorLoop(VPlan &MainPlan, VPlan &EpiPlan) {
9275 // Collect PHI nodes of widened phis in the VPlan for the epilogue. Those
9276 // will need their resume-values computed in the main vector loop. Others
9277 // can be removed from the main VPlan.
9278 SmallPtrSet<PHINode *, 2> EpiWidenedPhis;
9279 for (VPRecipeBase &R :
9282 continue;
9283 EpiWidenedPhis.insert(
9284 cast<PHINode>(R.getVPSingleValue()->getUnderlyingValue()));
9285 }
9286 for (VPRecipeBase &R :
9287 make_early_inc_range(MainPlan.getScalarHeader()->phis())) {
9288 auto *VPIRInst = cast<VPIRPhi>(&R);
9289 if (EpiWidenedPhis.contains(&VPIRInst->getIRPhi()))
9290 continue;
9291 // There is no corresponding wide induction in the epilogue plan that would
9292 // need a resume value. Remove the VPIRInst wrapping the scalar header phi
9293 // together with the corresponding ResumePhi. The resume values for the
9294 // scalar loop will be created during execution of EpiPlan.
9295 VPRecipeBase *ResumePhi = VPIRInst->getOperand(0)->getDefiningRecipe();
9296 VPIRInst->eraseFromParent();
9297 ResumePhi->eraseFromParent();
9298 }
9300
9301 using namespace VPlanPatternMatch;
9302 // When vectorizing the epilogue, FindFirstIV & FindLastIV reductions can
9303 // introduce multiple uses of undef/poison. If the reduction start value may
9304 // be undef or poison it needs to be frozen and the frozen start has to be
9305 // used when computing the reduction result. We also need to use the frozen
9306 // value in the resume phi generated by the main vector loop, as this is also
9307 // used to compute the reduction result after the epilogue vector loop.
9308 auto AddFreezeForFindLastIVReductions = [](VPlan &Plan,
9309 bool UpdateResumePhis) {
9310 VPBuilder Builder(Plan.getEntry());
9311 for (VPRecipeBase &R : *Plan.getMiddleBlock()) {
9312 auto *VPI = dyn_cast<VPInstruction>(&R);
9313 if (!VPI || VPI->getOpcode() != VPInstruction::ComputeFindIVResult)
9314 continue;
9315 VPValue *OrigStart = VPI->getOperand(1);
9317 continue;
9318 VPInstruction *Freeze =
9319 Builder.createNaryOp(Instruction::Freeze, {OrigStart}, {}, "fr");
9320 VPI->setOperand(1, Freeze);
9321 if (UpdateResumePhis)
9322 OrigStart->replaceUsesWithIf(Freeze, [Freeze](VPUser &U, unsigned) {
9323 return Freeze != &U && isa<VPPhi>(&U);
9324 });
9325 }
9326 };
9327 AddFreezeForFindLastIVReductions(MainPlan, true);
9328 AddFreezeForFindLastIVReductions(EpiPlan, false);
9329
9330 VPBasicBlock *MainScalarPH = MainPlan.getScalarPreheader();
9331 VPValue *VectorTC = &MainPlan.getVectorTripCount();
9332 // If there is a suitable resume value for the canonical induction in the
9333 // scalar (which will become vector) epilogue loop, use it and move it to the
9334 // beginning of the scalar preheader. Otherwise create it below.
9335 auto ResumePhiIter =
9336 find_if(MainScalarPH->phis(), [VectorTC](VPRecipeBase &R) {
9337 return match(&R, m_VPInstruction<Instruction::PHI>(m_Specific(VectorTC),
9338 m_ZeroInt()));
9339 });
9340 VPPhi *ResumePhi = nullptr;
9341 if (ResumePhiIter == MainScalarPH->phis().end()) {
9342 VPBuilder ScalarPHBuilder(MainScalarPH, MainScalarPH->begin());
9343 ResumePhi = ScalarPHBuilder.createScalarPhi(
9344 {VectorTC,
9346 {}, "vec.epilog.resume.val");
9347 } else {
9348 ResumePhi = cast<VPPhi>(&*ResumePhiIter);
9349 if (MainScalarPH->begin() == MainScalarPH->end())
9350 ResumePhi->moveBefore(*MainScalarPH, MainScalarPH->end());
9351 else if (&*MainScalarPH->begin() != ResumePhi)
9352 ResumePhi->moveBefore(*MainScalarPH, MainScalarPH->begin());
9353 }
9354 // Add a user to to make sure the resume phi won't get removed.
9355 VPBuilder(MainScalarPH)
9357}
9358
9359/// Prepare \p Plan for vectorizing the epilogue loop. That is, re-use expanded
9360/// SCEVs from \p ExpandedSCEVs and set resume values for header recipes. Some
9361/// reductions require creating new instructions to compute the resume values.
9362/// They are collected in a vector and returned. They must be moved to the
9363/// preheader of the vector epilogue loop, after created by the execution of \p
9364/// Plan.
9366 VPlan &Plan, Loop *L, const SCEV2ValueTy &ExpandedSCEVs,
9368 ScalarEvolution &SE) {
9369 VPRegionBlock *VectorLoop = Plan.getVectorLoopRegion();
9370 VPBasicBlock *Header = VectorLoop->getEntryBasicBlock();
9371 Header->setName("vec.epilog.vector.body");
9372
9373 VPCanonicalIVPHIRecipe *IV = VectorLoop->getCanonicalIV();
9374 // When vectorizing the epilogue loop, the canonical induction needs to be
9375 // adjusted by the value after the main vector loop. Find the resume value
9376 // created during execution of the main VPlan. It must be the first phi in the
9377 // loop preheader. Use the value to increment the canonical IV, and update all
9378 // users in the loop region to use the adjusted value.
9379 // FIXME: Improve modeling for canonical IV start values in the epilogue
9380 // loop.
9381 using namespace llvm::PatternMatch;
9382 PHINode *EPResumeVal = &*L->getLoopPreheader()->phis().begin();
9383 for (Value *Inc : EPResumeVal->incoming_values()) {
9384 if (match(Inc, m_SpecificInt(0)))
9385 continue;
9386 assert(!EPI.VectorTripCount &&
9387 "Must only have a single non-zero incoming value");
9388 EPI.VectorTripCount = Inc;
9389 }
9390 // If we didn't find a non-zero vector trip count, all incoming values
9391 // must be zero, which also means the vector trip count is zero. Pick the
9392 // first zero as vector trip count.
9393 // TODO: We should not choose VF * UF so the main vector loop is known to
9394 // be dead.
9395 if (!EPI.VectorTripCount) {
9396 assert(EPResumeVal->getNumIncomingValues() > 0 &&
9397 all_of(EPResumeVal->incoming_values(),
9398 [](Value *Inc) { return match(Inc, m_SpecificInt(0)); }) &&
9399 "all incoming values must be 0");
9400 EPI.VectorTripCount = EPResumeVal->getOperand(0);
9401 }
9402 VPValue *VPV = Plan.getOrAddLiveIn(EPResumeVal);
9403 assert(all_of(IV->users(),
9404 [](const VPUser *U) {
9405 return isa<VPScalarIVStepsRecipe>(U) ||
9406 isa<VPDerivedIVRecipe>(U) ||
9407 cast<VPRecipeBase>(U)->isScalarCast() ||
9408 cast<VPInstruction>(U)->getOpcode() ==
9409 Instruction::Add;
9410 }) &&
9411 "the canonical IV should only be used by its increment or "
9412 "ScalarIVSteps when resetting the start value");
9413 VPBuilder Builder(Header, Header->getFirstNonPhi());
9414 VPInstruction *Add = Builder.createNaryOp(Instruction::Add, {IV, VPV});
9415 IV->replaceAllUsesWith(Add);
9416 Add->setOperand(0, IV);
9417
9419 SmallVector<Instruction *> InstsToMove;
9420 // Ensure that the start values for all header phi recipes are updated before
9421 // vectorizing the epilogue loop. Skip the canonical IV, which has been
9422 // handled above.
9423 for (VPRecipeBase &R : drop_begin(Header->phis())) {
9424 Value *ResumeV = nullptr;
9425 // TODO: Move setting of resume values to prepareToExecute.
9426 if (auto *ReductionPhi = dyn_cast<VPReductionPHIRecipe>(&R)) {
9427 auto *RdxResult =
9428 cast<VPInstruction>(*find_if(ReductionPhi->users(), [](VPUser *U) {
9429 auto *VPI = dyn_cast<VPInstruction>(U);
9430 return VPI &&
9431 (VPI->getOpcode() == VPInstruction::ComputeAnyOfResult ||
9432 VPI->getOpcode() == VPInstruction::ComputeReductionResult ||
9433 VPI->getOpcode() == VPInstruction::ComputeFindIVResult);
9434 }));
9435 ResumeV = cast<PHINode>(ReductionPhi->getUnderlyingInstr())
9436 ->getIncomingValueForBlock(L->getLoopPreheader());
9437 RecurKind RK = ReductionPhi->getRecurrenceKind();
9439 Value *StartV = RdxResult->getOperand(1)->getLiveInIRValue();
9440 // VPReductionPHIRecipes for AnyOf reductions expect a boolean as
9441 // start value; compare the final value from the main vector loop
9442 // to the start value.
9443 BasicBlock *PBB = cast<Instruction>(ResumeV)->getParent();
9444 IRBuilder<> Builder(PBB, PBB->getFirstNonPHIIt());
9445 ResumeV = Builder.CreateICmpNE(ResumeV, StartV);
9446 if (auto *I = dyn_cast<Instruction>(ResumeV))
9447 InstsToMove.push_back(I);
9449 Value *StartV = getStartValueFromReductionResult(RdxResult);
9450 ToFrozen[StartV] = cast<PHINode>(ResumeV)->getIncomingValueForBlock(
9452
9453 // VPReductionPHIRecipe for FindFirstIV/FindLastIV reductions requires
9454 // an adjustment to the resume value. The resume value is adjusted to
9455 // the sentinel value when the final value from the main vector loop
9456 // equals the start value. This ensures correctness when the start value
9457 // might not be less than the minimum value of a monotonically
9458 // increasing induction variable.
9459 BasicBlock *ResumeBB = cast<Instruction>(ResumeV)->getParent();
9460 IRBuilder<> Builder(ResumeBB, ResumeBB->getFirstNonPHIIt());
9461 Value *Cmp = Builder.CreateICmpEQ(ResumeV, ToFrozen[StartV]);
9462 if (auto *I = dyn_cast<Instruction>(Cmp))
9463 InstsToMove.push_back(I);
9464 Value *Sentinel = RdxResult->getOperand(2)->getLiveInIRValue();
9465 ResumeV = Builder.CreateSelect(Cmp, Sentinel, ResumeV);
9466 if (auto *I = dyn_cast<Instruction>(ResumeV))
9467 InstsToMove.push_back(I);
9468 } else {
9469 VPValue *StartVal = Plan.getOrAddLiveIn(ResumeV);
9470 auto *PhiR = dyn_cast<VPReductionPHIRecipe>(&R);
9471 if (auto *VPI = dyn_cast<VPInstruction>(PhiR->getStartValue())) {
9472 assert(VPI->getOpcode() == VPInstruction::ReductionStartVector &&
9473 "unexpected start value");
9474 VPI->setOperand(0, StartVal);
9475 continue;
9476 }
9477 }
9478 } else {
9479 // Retrieve the induction resume values for wide inductions from
9480 // their original phi nodes in the scalar loop.
9481 PHINode *IndPhi = cast<VPWidenInductionRecipe>(&R)->getPHINode();
9482 // Hook up to the PHINode generated by a ResumePhi recipe of main
9483 // loop VPlan, which feeds the scalar loop.
9484 ResumeV = IndPhi->getIncomingValueForBlock(L->getLoopPreheader());
9485 }
9486 assert(ResumeV && "Must have a resume value");
9487 VPValue *StartVal = Plan.getOrAddLiveIn(ResumeV);
9488 cast<VPHeaderPHIRecipe>(&R)->setStartValue(StartVal);
9489 }
9490
9491 // For some VPValues in the epilogue plan we must re-use the generated IR
9492 // values from the main plan. Replace them with live-in VPValues.
9493 // TODO: This is a workaround needed for epilogue vectorization and it
9494 // should be removed once induction resume value creation is done
9495 // directly in VPlan.
9496 for (auto &R : make_early_inc_range(*Plan.getEntry())) {
9497 // Re-use frozen values from the main plan for Freeze VPInstructions in the
9498 // epilogue plan. This ensures all users use the same frozen value.
9499 auto *VPI = dyn_cast<VPInstruction>(&R);
9500 if (VPI && VPI->getOpcode() == Instruction::Freeze) {
9501 VPI->replaceAllUsesWith(Plan.getOrAddLiveIn(
9502 ToFrozen.lookup(VPI->getOperand(0)->getLiveInIRValue())));
9503 continue;
9504 }
9505
9506 // Re-use the trip count and steps expanded for the main loop, as
9507 // skeleton creation needs it as a value that dominates both the scalar
9508 // and vector epilogue loops
9509 auto *ExpandR = dyn_cast<VPExpandSCEVRecipe>(&R);
9510 if (!ExpandR)
9511 continue;
9512 VPValue *ExpandedVal =
9513 Plan.getOrAddLiveIn(ExpandedSCEVs.lookup(ExpandR->getSCEV()));
9514 ExpandR->replaceAllUsesWith(ExpandedVal);
9515 if (Plan.getTripCount() == ExpandR)
9516 Plan.resetTripCount(ExpandedVal);
9517 ExpandR->eraseFromParent();
9518 }
9519
9520 auto VScale = CM.getVScaleForTuning();
9521 unsigned MainLoopStep =
9522 estimateElementCount(EPI.MainLoopVF * EPI.MainLoopUF, VScale);
9523 unsigned EpilogueLoopStep =
9524 estimateElementCount(EPI.EpilogueVF * EPI.EpilogueUF, VScale);
9526 Plan, EPI.TripCount, EPI.VectorTripCount,
9528 EPI.EpilogueUF, MainLoopStep, EpilogueLoopStep, SE);
9529
9530 return InstsToMove;
9531}
9532
9533// Generate bypass values from the additional bypass block. Note that when the
9534// vectorized epilogue is skipped due to iteration count check, then the
9535// resume value for the induction variable comes from the trip count of the
9536// main vector loop, passed as the second argument.
9538 PHINode *OrigPhi, const InductionDescriptor &II, IRBuilder<> &BypassBuilder,
9539 const SCEV2ValueTy &ExpandedSCEVs, Value *MainVectorTripCount,
9540 Instruction *OldInduction) {
9541 Value *Step = getExpandedStep(II, ExpandedSCEVs);
9542 // For the primary induction the additional bypass end value is known.
9543 // Otherwise it is computed.
9544 Value *EndValueFromAdditionalBypass = MainVectorTripCount;
9545 if (OrigPhi != OldInduction) {
9546 auto *BinOp = II.getInductionBinOp();
9547 // Fast-math-flags propagate from the original induction instruction.
9549 BypassBuilder.setFastMathFlags(BinOp->getFastMathFlags());
9550
9551 // Compute the end value for the additional bypass.
9552 EndValueFromAdditionalBypass =
9553 emitTransformedIndex(BypassBuilder, MainVectorTripCount,
9554 II.getStartValue(), Step, II.getKind(), BinOp);
9555 EndValueFromAdditionalBypass->setName("ind.end");
9556 }
9557 return EndValueFromAdditionalBypass;
9558}
9559
9561 VPlan &BestEpiPlan,
9563 const SCEV2ValueTy &ExpandedSCEVs,
9564 Value *MainVectorTripCount) {
9565 // Fix reduction resume values from the additional bypass block.
9566 BasicBlock *PH = L->getLoopPreheader();
9567 for (auto *Pred : predecessors(PH)) {
9568 for (PHINode &Phi : PH->phis()) {
9569 if (Phi.getBasicBlockIndex(Pred) != -1)
9570 continue;
9571 Phi.addIncoming(Phi.getIncomingValueForBlock(BypassBlock), Pred);
9572 }
9573 }
9574 auto *ScalarPH = cast<VPIRBasicBlock>(BestEpiPlan.getScalarPreheader());
9575 if (ScalarPH->hasPredecessors()) {
9576 // If ScalarPH has predecessors, we may need to update its reduction
9577 // resume values.
9578 for (const auto &[R, IRPhi] :
9579 zip(ScalarPH->phis(), ScalarPH->getIRBasicBlock()->phis())) {
9581 BypassBlock);
9582 }
9583 }
9584
9585 // Fix induction resume values from the additional bypass block.
9586 IRBuilder<> BypassBuilder(BypassBlock, BypassBlock->getFirstInsertionPt());
9587 for (const auto &[IVPhi, II] : LVL.getInductionVars()) {
9588 auto *Inc = cast<PHINode>(IVPhi->getIncomingValueForBlock(PH));
9590 IVPhi, II, BypassBuilder, ExpandedSCEVs, MainVectorTripCount,
9591 LVL.getPrimaryInduction());
9592 // TODO: Directly add as extra operand to the VPResumePHI recipe.
9593 Inc->setIncomingValueForBlock(BypassBlock, V);
9594 }
9595}
9596
9597/// Connect the epilogue vector loop generated for \p EpiPlan to the main vector
9598// loop, after both plans have executed, updating branches from the iteration
9599// and runtime checks of the main loop, as well as updating various phis. \p
9600// InstsToMove contains instructions that need to be moved to the preheader of
9601// the epilogue vector loop.
9603 VPlan &EpiPlan, Loop *L, EpilogueLoopVectorizationInfo &EPI,
9605 DenseMap<const SCEV *, Value *> &ExpandedSCEVs, GeneratedRTChecks &Checks,
9606 ArrayRef<Instruction *> InstsToMove) {
9607 BasicBlock *VecEpilogueIterationCountCheck =
9608 cast<VPIRBasicBlock>(EpiPlan.getEntry())->getIRBasicBlock();
9609
9610 BasicBlock *VecEpiloguePreHeader =
9611 cast<BranchInst>(VecEpilogueIterationCountCheck->getTerminator())
9612 ->getSuccessor(1);
9613 // Adjust the control flow taking the state info from the main loop
9614 // vectorization into account.
9616 "expected this to be saved from the previous pass.");
9617 DomTreeUpdater DTU(DT, DomTreeUpdater::UpdateStrategy::Eager);
9619 VecEpilogueIterationCountCheck, VecEpiloguePreHeader);
9620
9622 VecEpilogueIterationCountCheck},
9624 VecEpiloguePreHeader}});
9625
9626 BasicBlock *ScalarPH =
9627 cast<VPIRBasicBlock>(EpiPlan.getScalarPreheader())->getIRBasicBlock();
9629 VecEpilogueIterationCountCheck, ScalarPH);
9630 DTU.applyUpdates(
9632 VecEpilogueIterationCountCheck},
9634
9635 // Adjust the terminators of runtime check blocks and phis using them.
9636 BasicBlock *SCEVCheckBlock = Checks.getSCEVChecks().second;
9637 BasicBlock *MemCheckBlock = Checks.getMemRuntimeChecks().second;
9638 if (SCEVCheckBlock) {
9639 SCEVCheckBlock->getTerminator()->replaceUsesOfWith(
9640 VecEpilogueIterationCountCheck, ScalarPH);
9641 DTU.applyUpdates({{DominatorTree::Delete, SCEVCheckBlock,
9642 VecEpilogueIterationCountCheck},
9643 {DominatorTree::Insert, SCEVCheckBlock, ScalarPH}});
9644 }
9645 if (MemCheckBlock) {
9646 MemCheckBlock->getTerminator()->replaceUsesOfWith(
9647 VecEpilogueIterationCountCheck, ScalarPH);
9648 DTU.applyUpdates(
9649 {{DominatorTree::Delete, MemCheckBlock, VecEpilogueIterationCountCheck},
9650 {DominatorTree::Insert, MemCheckBlock, ScalarPH}});
9651 }
9652
9653 // The vec.epilog.iter.check block may contain Phi nodes from inductions
9654 // or reductions which merge control-flow from the latch block and the
9655 // middle block. Update the incoming values here and move the Phi into the
9656 // preheader.
9657 SmallVector<PHINode *, 4> PhisInBlock(
9658 llvm::make_pointer_range(VecEpilogueIterationCountCheck->phis()));
9659
9660 for (PHINode *Phi : PhisInBlock) {
9661 Phi->moveBefore(VecEpiloguePreHeader->getFirstNonPHIIt());
9662 Phi->replaceIncomingBlockWith(
9663 VecEpilogueIterationCountCheck->getSinglePredecessor(),
9664 VecEpilogueIterationCountCheck);
9665
9666 // If the phi doesn't have an incoming value from the
9667 // EpilogueIterationCountCheck, we are done. Otherwise remove the
9668 // incoming value and also those from other check blocks. This is needed
9669 // for reduction phis only.
9670 if (none_of(Phi->blocks(), [&](BasicBlock *IncB) {
9671 return EPI.EpilogueIterationCountCheck == IncB;
9672 }))
9673 continue;
9674 Phi->removeIncomingValue(EPI.EpilogueIterationCountCheck);
9675 if (SCEVCheckBlock)
9676 Phi->removeIncomingValue(SCEVCheckBlock);
9677 if (MemCheckBlock)
9678 Phi->removeIncomingValue(MemCheckBlock);
9679 }
9680
9681 auto IP = VecEpiloguePreHeader->getFirstNonPHIIt();
9682 for (auto *I : InstsToMove)
9683 I->moveBefore(IP);
9684
9685 // VecEpilogueIterationCountCheck conditionally skips over the epilogue loop
9686 // after executing the main loop. We need to update the resume values of
9687 // inductions and reductions during epilogue vectorization.
9688 fixScalarResumeValuesFromBypass(VecEpilogueIterationCountCheck, L, EpiPlan,
9689 LVL, ExpandedSCEVs, EPI.VectorTripCount);
9690}
9691
9693 assert((EnableVPlanNativePath || L->isInnermost()) &&
9694 "VPlan-native path is not enabled. Only process inner loops.");
9695
9696 LLVM_DEBUG(dbgs() << "\nLV: Checking a loop in '"
9697 << L->getHeader()->getParent()->getName() << "' from "
9698 << L->getLocStr() << "\n");
9699
9700 LoopVectorizeHints Hints(L, InterleaveOnlyWhenForced, *ORE, TTI);
9701
9702 LLVM_DEBUG(
9703 dbgs() << "LV: Loop hints:"
9704 << " force="
9706 ? "disabled"
9708 ? "enabled"
9709 : "?"))
9710 << " width=" << Hints.getWidth()
9711 << " interleave=" << Hints.getInterleave() << "\n");
9712
9713 // Function containing loop
9714 Function *F = L->getHeader()->getParent();
9715
9716 // Looking at the diagnostic output is the only way to determine if a loop
9717 // was vectorized (other than looking at the IR or machine code), so it
9718 // is important to generate an optimization remark for each loop. Most of
9719 // these messages are generated as OptimizationRemarkAnalysis. Remarks
9720 // generated as OptimizationRemark and OptimizationRemarkMissed are
9721 // less verbose reporting vectorized loops and unvectorized loops that may
9722 // benefit from vectorization, respectively.
9723
9724 if (!Hints.allowVectorization(F, L, VectorizeOnlyWhenForced)) {
9725 LLVM_DEBUG(dbgs() << "LV: Loop hints prevent vectorization.\n");
9726 return false;
9727 }
9728
9729 PredicatedScalarEvolution PSE(*SE, *L);
9730
9731 // Check if it is legal to vectorize the loop.
9732 LoopVectorizationRequirements Requirements;
9733 LoopVectorizationLegality LVL(L, PSE, DT, TTI, TLI, F, *LAIs, LI, ORE,
9734 &Requirements, &Hints, DB, AC, BFI, PSI, AA);
9736 LLVM_DEBUG(dbgs() << "LV: Not vectorizing: Cannot prove legality.\n");
9737 Hints.emitRemarkWithHints();
9738 return false;
9739 }
9740
9742 reportVectorizationFailure("Auto-vectorization of loops with uncountable "
9743 "early exit is not enabled",
9744 "UncountableEarlyExitLoopsDisabled", ORE, L);
9745 return false;
9746 }
9747
9748 if (!LVL.getPotentiallyFaultingLoads().empty()) {
9749 reportVectorizationFailure("Auto-vectorization of loops with potentially "
9750 "faulting load is not supported",
9751 "PotentiallyFaultingLoadsNotSupported", ORE, L);
9752 return false;
9753 }
9754
9755 // Entrance to the VPlan-native vectorization path. Outer loops are processed
9756 // here. They may require CFG and instruction level transformations before
9757 // even evaluating whether vectorization is profitable. Since we cannot modify
9758 // the incoming IR, we need to build VPlan upfront in the vectorization
9759 // pipeline.
9760 if (!L->isInnermost())
9761 return processLoopInVPlanNativePath(L, PSE, LI, DT, &LVL, TTI, TLI, DB, AC,
9762 ORE, BFI, PSI, Hints, Requirements);
9763
9764 assert(L->isInnermost() && "Inner loop expected.");
9765
9766 InterleavedAccessInfo IAI(PSE, L, DT, LI, LVL.getLAI());
9767 bool UseInterleaved = TTI->enableInterleavedAccessVectorization();
9768
9769 // If an override option has been passed in for interleaved accesses, use it.
9770 if (EnableInterleavedMemAccesses.getNumOccurrences() > 0)
9771 UseInterleaved = EnableInterleavedMemAccesses;
9772
9773 // Analyze interleaved memory accesses.
9774 if (UseInterleaved)
9776
9777 if (LVL.hasUncountableEarlyExit()) {
9778 BasicBlock *LoopLatch = L->getLoopLatch();
9779 if (IAI.requiresScalarEpilogue() ||
9781 [LoopLatch](BasicBlock *BB) { return BB != LoopLatch; })) {
9782 reportVectorizationFailure("Auto-vectorization of early exit loops "
9783 "requiring a scalar epilogue is unsupported",
9784 "UncountableEarlyExitUnsupported", ORE, L);
9785 return false;
9786 }
9787 }
9788
9789 // Check the function attributes and profiles to find out if this function
9790 // should be optimized for size.
9792 getScalarEpilogueLowering(F, L, Hints, PSI, BFI, TTI, TLI, LVL, &IAI);
9793
9794 // Check the loop for a trip count threshold: vectorize loops with a tiny trip
9795 // count by optimizing for size, to minimize overheads.
9796 auto ExpectedTC = getSmallBestKnownTC(PSE, L);
9797 if (ExpectedTC && ExpectedTC->isFixed() &&
9798 ExpectedTC->getFixedValue() < TinyTripCountVectorThreshold) {
9799 LLVM_DEBUG(dbgs() << "LV: Found a loop with a very small trip count. "
9800 << "This loop is worth vectorizing only if no scalar "
9801 << "iteration overheads are incurred.");
9803 LLVM_DEBUG(dbgs() << " But vectorizing was explicitly forced.\n");
9804 else {
9805 LLVM_DEBUG(dbgs() << "\n");
9806 // Predicate tail-folded loops are efficient even when the loop
9807 // iteration count is low. However, setting the epilogue policy to
9808 // `CM_ScalarEpilogueNotAllowedLowTripLoop` prevents vectorizing loops
9809 // with runtime checks. It's more effective to let
9810 // `isOutsideLoopWorkProfitable` determine if vectorization is
9811 // beneficial for the loop.
9814 }
9815 }
9816
9817 // Check the function attributes to see if implicit floats or vectors are
9818 // allowed.
9819 if (F->hasFnAttribute(Attribute::NoImplicitFloat)) {
9821 "Can't vectorize when the NoImplicitFloat attribute is used",
9822 "loop not vectorized due to NoImplicitFloat attribute",
9823 "NoImplicitFloat", ORE, L);
9824 Hints.emitRemarkWithHints();
9825 return false;
9826 }
9827
9828 // Check if the target supports potentially unsafe FP vectorization.
9829 // FIXME: Add a check for the type of safety issue (denormal, signaling)
9830 // for the target we're vectorizing for, to make sure none of the
9831 // additional fp-math flags can help.
9832 if (Hints.isPotentiallyUnsafe() &&
9833 TTI->isFPVectorizationPotentiallyUnsafe()) {
9835 "Potentially unsafe FP op prevents vectorization",
9836 "loop not vectorized due to unsafe FP support.",
9837 "UnsafeFP", ORE, L);
9838 Hints.emitRemarkWithHints();
9839 return false;
9840 }
9841
9842 bool AllowOrderedReductions;
9843 // If the flag is set, use that instead and override the TTI behaviour.
9844 if (ForceOrderedReductions.getNumOccurrences() > 0)
9845 AllowOrderedReductions = ForceOrderedReductions;
9846 else
9847 AllowOrderedReductions = TTI->enableOrderedReductions();
9848 if (!LVL.canVectorizeFPMath(AllowOrderedReductions)) {
9849 ORE->emit([&]() {
9850 auto *ExactFPMathInst = Requirements.getExactFPInst();
9851 return OptimizationRemarkAnalysisFPCommute(DEBUG_TYPE, "CantReorderFPOps",
9852 ExactFPMathInst->getDebugLoc(),
9853 ExactFPMathInst->getParent())
9854 << "loop not vectorized: cannot prove it is safe to reorder "
9855 "floating-point operations";
9856 });
9857 LLVM_DEBUG(dbgs() << "LV: loop not vectorized: cannot prove it is safe to "
9858 "reorder floating-point operations\n");
9859 Hints.emitRemarkWithHints();
9860 return false;
9861 }
9862
9863 // Use the cost model.
9864 LoopVectorizationCostModel CM(SEL, L, PSE, LI, &LVL, *TTI, TLI, DB, AC, ORE,
9865 F, &Hints, IAI, PSI, BFI);
9866 // Use the planner for vectorization.
9867 LoopVectorizationPlanner LVP(L, LI, DT, TLI, *TTI, &LVL, CM, IAI, PSE, Hints,
9868 ORE);
9869
9870 // Get user vectorization factor and interleave count.
9871 ElementCount UserVF = Hints.getWidth();
9872 unsigned UserIC = Hints.getInterleave();
9873 if (UserIC > 1 && !LVL.isSafeForAnyVectorWidth())
9874 UserIC = 1;
9875
9876 // Plan how to best vectorize.
9877 LVP.plan(UserVF, UserIC);
9879 unsigned IC = 1;
9880
9881 if (ORE->allowExtraAnalysis(LV_NAME))
9883
9884 GeneratedRTChecks Checks(PSE, DT, LI, TTI, F->getDataLayout(), CM.CostKind);
9885 if (LVP.hasPlanWithVF(VF.Width)) {
9886 // Select the interleave count.
9887 IC = LVP.selectInterleaveCount(LVP.getPlanFor(VF.Width), VF.Width, VF.Cost);
9888
9889 unsigned SelectedIC = std::max(IC, UserIC);
9890 // Optimistically generate runtime checks if they are needed. Drop them if
9891 // they turn out to not be profitable.
9892 if (VF.Width.isVector() || SelectedIC > 1) {
9893 Checks.create(L, *LVL.getLAI(), PSE.getPredicate(), VF.Width, SelectedIC);
9894
9895 // Bail out early if either the SCEV or memory runtime checks are known to
9896 // fail. In that case, the vector loop would never execute.
9897 using namespace llvm::PatternMatch;
9898 if (Checks.getSCEVChecks().first &&
9899 match(Checks.getSCEVChecks().first, m_One()))
9900 return false;
9901 if (Checks.getMemRuntimeChecks().first &&
9902 match(Checks.getMemRuntimeChecks().first, m_One()))
9903 return false;
9904 }
9905
9906 // Check if it is profitable to vectorize with runtime checks.
9907 bool ForceVectorization =
9909 VPCostContext CostCtx(CM.TTI, *CM.TLI, LVP.getPlanFor(VF.Width), CM,
9910 CM.CostKind, *CM.PSE.getSE(), L);
9911 if (!ForceVectorization &&
9912 !isOutsideLoopWorkProfitable(Checks, VF, L, PSE, CostCtx,
9913 LVP.getPlanFor(VF.Width), SEL,
9914 CM.getVScaleForTuning())) {
9915 ORE->emit([&]() {
9917 DEBUG_TYPE, "CantReorderMemOps", L->getStartLoc(),
9918 L->getHeader())
9919 << "loop not vectorized: cannot prove it is safe to reorder "
9920 "memory operations";
9921 });
9922 LLVM_DEBUG(dbgs() << "LV: Too many memory checks needed.\n");
9923 Hints.emitRemarkWithHints();
9924 return false;
9925 }
9926 }
9927
9928 // Identify the diagnostic messages that should be produced.
9929 std::pair<StringRef, std::string> VecDiagMsg, IntDiagMsg;
9930 bool VectorizeLoop = true, InterleaveLoop = true;
9931 if (VF.Width.isScalar()) {
9932 LLVM_DEBUG(dbgs() << "LV: Vectorization is possible but not beneficial.\n");
9933 VecDiagMsg = {
9934 "VectorizationNotBeneficial",
9935 "the cost-model indicates that vectorization is not beneficial"};
9936 VectorizeLoop = false;
9937 }
9938
9939 if (UserIC == 1 && Hints.getInterleave() > 1) {
9941 "UserIC should only be ignored due to unsafe dependencies");
9942 LLVM_DEBUG(dbgs() << "LV: Ignoring user-specified interleave count.\n");
9943 IntDiagMsg = {"InterleavingUnsafe",
9944 "Ignoring user-specified interleave count due to possibly "
9945 "unsafe dependencies in the loop."};
9946 InterleaveLoop = false;
9947 } else if (!LVP.hasPlanWithVF(VF.Width) && UserIC > 1) {
9948 // Tell the user interleaving was avoided up-front, despite being explicitly
9949 // requested.
9950 LLVM_DEBUG(dbgs() << "LV: Ignoring UserIC, because vectorization and "
9951 "interleaving should be avoided up front\n");
9952 IntDiagMsg = {"InterleavingAvoided",
9953 "Ignoring UserIC, because interleaving was avoided up front"};
9954 InterleaveLoop = false;
9955 } else if (IC == 1 && UserIC <= 1) {
9956 // Tell the user interleaving is not beneficial.
9957 LLVM_DEBUG(dbgs() << "LV: Interleaving is not beneficial.\n");
9958 IntDiagMsg = {
9959 "InterleavingNotBeneficial",
9960 "the cost-model indicates that interleaving is not beneficial"};
9961 InterleaveLoop = false;
9962 if (UserIC == 1) {
9963 IntDiagMsg.first = "InterleavingNotBeneficialAndDisabled";
9964 IntDiagMsg.second +=
9965 " and is explicitly disabled or interleave count is set to 1";
9966 }
9967 } else if (IC > 1 && UserIC == 1) {
9968 // Tell the user interleaving is beneficial, but it explicitly disabled.
9969 LLVM_DEBUG(dbgs() << "LV: Interleaving is beneficial but is explicitly "
9970 "disabled.\n");
9971 IntDiagMsg = {"InterleavingBeneficialButDisabled",
9972 "the cost-model indicates that interleaving is beneficial "
9973 "but is explicitly disabled or interleave count is set to 1"};
9974 InterleaveLoop = false;
9975 }
9976
9977 // If there is a histogram in the loop, do not just interleave without
9978 // vectorizing. The order of operations will be incorrect without the
9979 // histogram intrinsics, which are only used for recipes with VF > 1.
9980 if (!VectorizeLoop && InterleaveLoop && LVL.hasHistograms()) {
9981 LLVM_DEBUG(dbgs() << "LV: Not interleaving without vectorization due "
9982 << "to histogram operations.\n");
9983 IntDiagMsg = {
9984 "HistogramPreventsScalarInterleaving",
9985 "Unable to interleave without vectorization due to constraints on "
9986 "the order of histogram operations"};
9987 InterleaveLoop = false;
9988 }
9989
9990 // Override IC if user provided an interleave count.
9991 IC = UserIC > 0 ? UserIC : IC;
9992
9993 // Emit diagnostic messages, if any.
9994 const char *VAPassName = Hints.vectorizeAnalysisPassName();
9995 if (!VectorizeLoop && !InterleaveLoop) {
9996 // Do not vectorize or interleaving the loop.
9997 ORE->emit([&]() {
9998 return OptimizationRemarkMissed(VAPassName, VecDiagMsg.first,
9999 L->getStartLoc(), L->getHeader())
10000 << VecDiagMsg.second;
10001 });
10002 ORE->emit([&]() {
10003 return OptimizationRemarkMissed(LV_NAME, IntDiagMsg.first,
10004 L->getStartLoc(), L->getHeader())
10005 << IntDiagMsg.second;
10006 });
10007 return false;
10008 }
10009
10010 if (!VectorizeLoop && InterleaveLoop) {
10011 LLVM_DEBUG(dbgs() << "LV: Interleave Count is " << IC << '\n');
10012 ORE->emit([&]() {
10013 return OptimizationRemarkAnalysis(VAPassName, VecDiagMsg.first,
10014 L->getStartLoc(), L->getHeader())
10015 << VecDiagMsg.second;
10016 });
10017 } else if (VectorizeLoop && !InterleaveLoop) {
10018 LLVM_DEBUG(dbgs() << "LV: Found a vectorizable loop (" << VF.Width
10019 << ") in " << L->getLocStr() << '\n');
10020 ORE->emit([&]() {
10021 return OptimizationRemarkAnalysis(LV_NAME, IntDiagMsg.first,
10022 L->getStartLoc(), L->getHeader())
10023 << IntDiagMsg.second;
10024 });
10025 } else if (VectorizeLoop && InterleaveLoop) {
10026 LLVM_DEBUG(dbgs() << "LV: Found a vectorizable loop (" << VF.Width
10027 << ") in " << L->getLocStr() << '\n');
10028 LLVM_DEBUG(dbgs() << "LV: Interleave Count is " << IC << '\n');
10029 }
10030
10031 // Report the vectorization decision.
10032 if (VF.Width.isScalar()) {
10033 using namespace ore;
10034 assert(IC > 1);
10035 ORE->emit([&]() {
10036 return OptimizationRemark(LV_NAME, "Interleaved", L->getStartLoc(),
10037 L->getHeader())
10038 << "interleaved loop (interleaved count: "
10039 << NV("InterleaveCount", IC) << ")";
10040 });
10041 } else {
10042 // Report the vectorization decision.
10043 reportVectorization(ORE, L, VF, IC);
10044 }
10045 if (ORE->allowExtraAnalysis(LV_NAME))
10047
10048 // If we decided that it is *legal* to interleave or vectorize the loop, then
10049 // do it.
10050
10051 VPlan &BestPlan = LVP.getPlanFor(VF.Width);
10052 // Consider vectorizing the epilogue too if it's profitable.
10053 VectorizationFactor EpilogueVF =
10055 if (EpilogueVF.Width.isVector()) {
10056 std::unique_ptr<VPlan> BestMainPlan(BestPlan.duplicate());
10057
10058 // The first pass vectorizes the main loop and creates a scalar epilogue
10059 // to be vectorized by executing the plan (potentially with a different
10060 // factor) again shortly afterwards.
10061 VPlan &BestEpiPlan = LVP.getPlanFor(EpilogueVF.Width);
10062 BestEpiPlan.getMiddleBlock()->setName("vec.epilog.middle.block");
10063 BestEpiPlan.getVectorPreheader()->setName("vec.epilog.ph");
10064 preparePlanForMainVectorLoop(*BestMainPlan, BestEpiPlan);
10065 EpilogueLoopVectorizationInfo EPI(VF.Width, IC, EpilogueVF.Width, 1,
10066 BestEpiPlan);
10067 EpilogueVectorizerMainLoop MainILV(L, PSE, LI, DT, TTI, AC, EPI, &CM, BFI,
10068 PSI, Checks, *BestMainPlan);
10069 auto ExpandedSCEVs = LVP.executePlan(EPI.MainLoopVF, EPI.MainLoopUF,
10070 *BestMainPlan, MainILV, DT, false);
10071 ++LoopsVectorized;
10072
10073 // Second pass vectorizes the epilogue and adjusts the control flow
10074 // edges from the first pass.
10075 EpilogueVectorizerEpilogueLoop EpilogILV(L, PSE, LI, DT, TTI, AC, EPI, &CM,
10076 BFI, PSI, Checks, BestEpiPlan);
10078 BestEpiPlan, L, ExpandedSCEVs, EPI, CM, *PSE.getSE());
10079 LVP.executePlan(EPI.EpilogueVF, EPI.EpilogueUF, BestEpiPlan, EpilogILV, DT,
10080 true);
10081 connectEpilogueVectorLoop(BestEpiPlan, L, EPI, DT, LVL, ExpandedSCEVs,
10082 Checks, InstsToMove);
10083 ++LoopsEpilogueVectorized;
10084 } else {
10085 InnerLoopVectorizer LB(L, PSE, LI, DT, TTI, AC, VF.Width, IC, &CM, BFI, PSI,
10086 Checks, BestPlan);
10087 // TODO: Move to general VPlan pipeline once epilogue loops are also
10088 // supported.
10091 IC, PSE);
10092 LVP.addMinimumIterationCheck(BestPlan, VF.Width, IC,
10094
10095 LVP.executePlan(VF.Width, IC, BestPlan, LB, DT, false);
10096 ++LoopsVectorized;
10097 }
10098
10099 assert(DT->verify(DominatorTree::VerificationLevel::Fast) &&
10100 "DT not preserved correctly");
10101 assert(!verifyFunction(*F, &dbgs()));
10102
10103 return true;
10104}
10105
10107
10108 // Don't attempt if
10109 // 1. the target claims to have no vector registers, and
10110 // 2. interleaving won't help ILP.
10111 //
10112 // The second condition is necessary because, even if the target has no
10113 // vector registers, loop vectorization may still enable scalar
10114 // interleaving.
10115 if (!TTI->getNumberOfRegisters(TTI->getRegisterClassForType(true)) &&
10116 TTI->getMaxInterleaveFactor(ElementCount::getFixed(1)) < 2)
10117 return LoopVectorizeResult(false, false);
10118
10119 bool Changed = false, CFGChanged = false;
10120
10121 // The vectorizer requires loops to be in simplified form.
10122 // Since simplification may add new inner loops, it has to run before the
10123 // legality and profitability checks. This means running the loop vectorizer
10124 // will simplify all loops, regardless of whether anything end up being
10125 // vectorized.
10126 for (const auto &L : *LI)
10127 Changed |= CFGChanged |=
10128 simplifyLoop(L, DT, LI, SE, AC, nullptr, false /* PreserveLCSSA */);
10129
10130 // Build up a worklist of inner-loops to vectorize. This is necessary as
10131 // the act of vectorizing or partially unrolling a loop creates new loops
10132 // and can invalidate iterators across the loops.
10133 SmallVector<Loop *, 8> Worklist;
10134
10135 for (Loop *L : *LI)
10136 collectSupportedLoops(*L, LI, ORE, Worklist);
10137
10138 LoopsAnalyzed += Worklist.size();
10139
10140 // Now walk the identified inner loops.
10141 while (!Worklist.empty()) {
10142 Loop *L = Worklist.pop_back_val();
10143
10144 // For the inner loops we actually process, form LCSSA to simplify the
10145 // transform.
10146 Changed |= formLCSSARecursively(*L, *DT, LI, SE);
10147
10148 Changed |= CFGChanged |= processLoop(L);
10149
10150 if (Changed) {
10151 LAIs->clear();
10152
10153#ifndef NDEBUG
10154 if (VerifySCEV)
10155 SE->verify();
10156#endif
10157 }
10158 }
10159
10160 // Process each loop nest in the function.
10161 return LoopVectorizeResult(Changed, CFGChanged);
10162}
10163
10166 LI = &AM.getResult<LoopAnalysis>(F);
10167 // There are no loops in the function. Return before computing other
10168 // expensive analyses.
10169 if (LI->empty())
10170 return PreservedAnalyses::all();
10179 AA = &AM.getResult<AAManager>(F);
10180
10181 auto &MAMProxy = AM.getResult<ModuleAnalysisManagerFunctionProxy>(F);
10182 PSI = MAMProxy.getCachedResult<ProfileSummaryAnalysis>(*F.getParent());
10183 BFI = nullptr;
10184 if (PSI && PSI->hasProfileSummary())
10186 LoopVectorizeResult Result = runImpl(F);
10187 if (!Result.MadeAnyChange)
10188 return PreservedAnalyses::all();
10190
10191 if (isAssignmentTrackingEnabled(*F.getParent())) {
10192 for (auto &BB : F)
10194 }
10195
10196 PA.preserve<LoopAnalysis>();
10200
10201 if (Result.MadeCFGChange) {
10202 // Making CFG changes likely means a loop got vectorized. Indicate that
10203 // extra simplification passes should be run.
10204 // TODO: MadeCFGChanges is not a prefect proxy. Extra passes should only
10205 // be run if runtime checks have been added.
10208 } else {
10210 }
10211 return PA;
10212}
10213
10215 raw_ostream &OS, function_ref<StringRef(StringRef)> MapClassName2PassName) {
10216 static_cast<PassInfoMixin<LoopVectorizePass> *>(this)->printPipeline(
10217 OS, MapClassName2PassName);
10218
10219 OS << '<';
10220 OS << (InterleaveOnlyWhenForced ? "" : "no-") << "interleave-forced-only;";
10221 OS << (VectorizeOnlyWhenForced ? "" : "no-") << "vectorize-forced-only;";
10222 OS << '>';
10223}
for(const MachineOperand &MO :llvm::drop_begin(OldMI.operands(), Desc.getNumOperands()))
static unsigned getIntrinsicID(const SDNode *N)
unsigned RegSize
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
aarch64 promote const
AMDGPU Lower Kernel Arguments
AMDGPU Register Bank Select
Rewrite undef for PHI
This file implements a class to represent arbitrary precision integral constant values and operations...
@ PostInc
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
static bool isEqual(const Function &Caller, const Function &Callee)
This file contains the simple types necessary to represent the attributes associated with functions a...
static const Function * getParent(const Value *V)
This is the interface for LLVM's primary stateless and local alias analysis.
static bool IsEmptyBlock(MachineBasicBlock *MBB)
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
#define clEnumValN(ENUMVAL, FLAGNAME, DESC)
This file contains the declarations for the subclasses of Constant, which represent the different fla...
static cl::opt< OutputCostKind > CostKind("cost-kind", cl::desc("Target cost kind"), cl::init(OutputCostKind::RecipThroughput), cl::values(clEnumValN(OutputCostKind::RecipThroughput, "throughput", "Reciprocal throughput"), clEnumValN(OutputCostKind::Latency, "latency", "Instruction latency"), clEnumValN(OutputCostKind::CodeSize, "code-size", "Code size"), clEnumValN(OutputCostKind::SizeAndLatency, "size-latency", "Code size and latency"), clEnumValN(OutputCostKind::All, "all", "Print all cost kinds")))
static cl::opt< IntrinsicCostStrategy > IntrinsicCost("intrinsic-cost-strategy", cl::desc("Costing strategy for intrinsic instructions"), cl::init(IntrinsicCostStrategy::InstructionCost), cl::values(clEnumValN(IntrinsicCostStrategy::InstructionCost, "instruction-cost", "Use TargetTransformInfo::getInstructionCost"), clEnumValN(IntrinsicCostStrategy::IntrinsicCost, "intrinsic-cost", "Use TargetTransformInfo::getIntrinsicInstrCost"), clEnumValN(IntrinsicCostStrategy::TypeBasedIntrinsicCost, "type-based-intrinsic-cost", "Calculate the intrinsic cost based only on argument types")))
static InstructionCost getCost(Instruction &Inst, TTI::TargetCostKind CostKind, TargetTransformInfo &TTI, TargetLibraryInfo &TLI)
Definition CostModel.cpp:74
This file defines DenseMapInfo traits for DenseMap.
This file defines the DenseMap class.
#define DEBUG_TYPE
This is the interface for a simple mod/ref and alias analysis over globals.
Hexagon Common GEP
#define _
This file provides various utilities for inspecting and working with the control flow graph in LLVM I...
Module.h This file contains the declarations for the Module class.
This defines the Use class.
static bool hasNoUnsignedWrap(BinaryOperator &I)
This file defines an InstructionCost class that is used when calculating the cost of an instruction,...
static std::pair< Value *, APInt > getMask(Value *WideMask, unsigned Factor, ElementCount LeafValueEC)
const AbstractManglingParser< Derived, Alloc >::OperatorInfo AbstractManglingParser< Derived, Alloc >::Ops[]
Legalize the Machine IR a function s Machine IR
Definition Legalizer.cpp:80
static cl::opt< unsigned, true > VectorizationFactor("force-vector-width", cl::Hidden, cl::desc("Sets the SIMD width. Zero is autoselect."), cl::location(VectorizerParams::VectorizationFactor))
This header provides classes for managing per-loop analyses.
static const char * VerboseDebug
#define LV_NAME
This file defines the LoopVectorizationLegality class.
This file provides a LoopVectorizationPlanner class.
static void collectSupportedLoops(Loop &L, LoopInfo *LI, OptimizationRemarkEmitter *ORE, SmallVectorImpl< Loop * > &V)
static cl::opt< unsigned > EpilogueVectorizationMinVF("epilogue-vectorization-minimum-VF", cl::Hidden, cl::desc("Only loops with vectorization factor equal to or larger than " "the specified value are considered for epilogue vectorization."))
static cl::opt< unsigned > EpilogueVectorizationForceVF("epilogue-vectorization-force-VF", cl::init(1), cl::Hidden, cl::desc("When epilogue vectorization is enabled, and a value greater than " "1 is specified, forces the given VF for all applicable epilogue " "loops."))
static Type * maybeVectorizeType(Type *Ty, ElementCount VF)
static ElementCount determineVPlanVF(const TargetTransformInfo &TTI, LoopVectorizationCostModel &CM)
static ElementCount getSmallConstantTripCount(ScalarEvolution *SE, const Loop *L)
A version of ScalarEvolution::getSmallConstantTripCount that returns an ElementCount to include loops...
static cl::opt< unsigned > VectorizeMemoryCheckThreshold("vectorize-memory-check-threshold", cl::init(128), cl::Hidden, cl::desc("The maximum allowed number of runtime memory checks"))
static void preparePlanForMainVectorLoop(VPlan &MainPlan, VPlan &EpiPlan)
Prepare MainPlan for vectorizing the main vector loop during epilogue vectorization.
static cl::opt< unsigned > TinyTripCountVectorThreshold("vectorizer-min-trip-count", cl::init(16), cl::Hidden, cl::desc("Loops with a constant trip count that is smaller than this " "value are vectorized only if no scalar iteration overheads " "are incurred."))
Loops with a known constant trip count below this number are vectorized only if no scalar iteration o...
static void debugVectorizationMessage(const StringRef Prefix, const StringRef DebugMsg, Instruction *I)
Write a DebugMsg about vectorization to the debug output stream.
static cl::opt< bool > EnableCondStoresVectorization("enable-cond-stores-vec", cl::init(true), cl::Hidden, cl::desc("Enable if predication of stores during vectorization."))
static void legacyCSE(BasicBlock *BB)
FIXME: This legacy common-subexpression-elimination routine is scheduled for removal,...
static VPIRBasicBlock * replaceVPBBWithIRVPBB(VPBasicBlock *VPBB, BasicBlock *IRBB, VPlan *Plan=nullptr)
Replace VPBB with a VPIRBasicBlock wrapping IRBB.
static Value * emitTransformedIndex(IRBuilderBase &B, Value *Index, Value *StartValue, Value *Step, InductionDescriptor::InductionKind InductionKind, const BinaryOperator *InductionBinOp)
Compute the transformed value of Index at offset StartValue using step StepValue.
static DebugLoc getDebugLocFromInstOrOperands(Instruction *I)
Look for a meaningful debug location on the instruction or its operands.
static Value * createInductionAdditionalBypassValues(PHINode *OrigPhi, const InductionDescriptor &II, IRBuilder<> &BypassBuilder, const SCEV2ValueTy &ExpandedSCEVs, Value *MainVectorTripCount, Instruction *OldInduction)
static void fixReductionScalarResumeWhenVectorizingEpilog(VPPhi *EpiResumePhiR, PHINode &EpiResumePhi, BasicBlock *BypassBlock)
static Value * getStartValueFromReductionResult(VPInstruction *RdxResult)
static cl::opt< bool > ForceTargetSupportsScalableVectors("force-target-supports-scalable-vectors", cl::init(false), cl::Hidden, cl::desc("Pretend that scalable vectors are supported, even if the target does " "not support them. This flag should only be used for testing."))
static bool useActiveLaneMaskForControlFlow(TailFoldingStyle Style)
static cl::opt< bool > EnableEarlyExitVectorization("enable-early-exit-vectorization", cl::init(true), cl::Hidden, cl::desc("Enable vectorization of early exit loops with uncountable exits."))
static cl::opt< bool > ConsiderRegPressure("vectorizer-consider-reg-pressure", cl::init(false), cl::Hidden, cl::desc("Discard VFs if their register pressure is too high."))
static unsigned estimateElementCount(ElementCount VF, std::optional< unsigned > VScale)
This function attempts to return a value that represents the ElementCount at runtime.
static constexpr uint32_t MinItersBypassWeights[]
static cl::opt< unsigned > ForceTargetNumScalarRegs("force-target-num-scalar-regs", cl::init(0), cl::Hidden, cl::desc("A flag that overrides the target's number of scalar registers."))
static cl::opt< bool > UseWiderVFIfCallVariantsPresent("vectorizer-maximize-bandwidth-for-vector-calls", cl::init(true), cl::Hidden, cl::desc("Try wider VFs if they enable the use of vector variants"))
static std::optional< unsigned > getMaxVScale(const Function &F, const TargetTransformInfo &TTI)
static cl::opt< unsigned > SmallLoopCost("small-loop-cost", cl::init(20), cl::Hidden, cl::desc("The cost of a loop that is considered 'small' by the interleaver."))
static void connectEpilogueVectorLoop(VPlan &EpiPlan, Loop *L, EpilogueLoopVectorizationInfo &EPI, DominatorTree *DT, LoopVectorizationLegality &LVL, DenseMap< const SCEV *, Value * > &ExpandedSCEVs, GeneratedRTChecks &Checks, ArrayRef< Instruction * > InstsToMove)
Connect the epilogue vector loop generated for EpiPlan to the main vector.
static bool planContainsAdditionalSimplifications(VPlan &Plan, VPCostContext &CostCtx, Loop *TheLoop, ElementCount VF)
Return true if the original loop \ TheLoop contains any instructions that do not have corresponding r...
static cl::opt< unsigned > ForceTargetNumVectorRegs("force-target-num-vector-regs", cl::init(0), cl::Hidden, cl::desc("A flag that overrides the target's number of vector registers."))
static bool isExplicitVecOuterLoop(Loop *OuterLp, OptimizationRemarkEmitter *ORE)
static cl::opt< bool > EnableIndVarRegisterHeur("enable-ind-var-reg-heur", cl::init(true), cl::Hidden, cl::desc("Count the induction variable only once when interleaving"))
static cl::opt< TailFoldingStyle > ForceTailFoldingStyle("force-tail-folding-style", cl::desc("Force the tail folding style"), cl::init(TailFoldingStyle::None), cl::values(clEnumValN(TailFoldingStyle::None, "none", "Disable tail folding"), clEnumValN(TailFoldingStyle::Data, "data", "Create lane mask for data only, using active.lane.mask intrinsic"), clEnumValN(TailFoldingStyle::DataWithoutLaneMask, "data-without-lane-mask", "Create lane mask with compare/stepvector"), clEnumValN(TailFoldingStyle::DataAndControlFlow, "data-and-control", "Create lane mask using active.lane.mask intrinsic, and use " "it for both data and control flow"), clEnumValN(TailFoldingStyle::DataAndControlFlowWithoutRuntimeCheck, "data-and-control-without-rt-check", "Similar to data-and-control, but remove the runtime check"), clEnumValN(TailFoldingStyle::DataWithEVL, "data-with-evl", "Use predicated EVL instructions for tail folding. If EVL " "is unsupported, fallback to data-without-lane-mask.")))
static cl::opt< bool > EnableEpilogueVectorization("enable-epilogue-vectorization", cl::init(true), cl::Hidden, cl::desc("Enable vectorization of epilogue loops."))
static ScalarEpilogueLowering getScalarEpilogueLowering(Function *F, Loop *L, LoopVectorizeHints &Hints, ProfileSummaryInfo *PSI, BlockFrequencyInfo *BFI, TargetTransformInfo *TTI, TargetLibraryInfo *TLI, LoopVectorizationLegality &LVL, InterleavedAccessInfo *IAI)
static cl::opt< bool > PreferPredicatedReductionSelect("prefer-predicated-reduction-select", cl::init(false), cl::Hidden, cl::desc("Prefer predicating a reduction operation over an after loop select."))
static VPWidenIntOrFpInductionRecipe * createWidenInductionRecipes(PHINode *Phi, Instruction *PhiOrTrunc, VPValue *Start, const InductionDescriptor &IndDesc, VPlan &Plan, ScalarEvolution &SE, Loop &OrigLoop)
Creates a VPWidenIntOrFpInductionRecpipe for Phi.
static cl::opt< bool > PreferInLoopReductions("prefer-inloop-reductions", cl::init(false), cl::Hidden, cl::desc("Prefer in-loop vector reductions, " "overriding the targets preference."))
static SmallVector< Instruction * > preparePlanForEpilogueVectorLoop(VPlan &Plan, Loop *L, const SCEV2ValueTy &ExpandedSCEVs, EpilogueLoopVectorizationInfo &EPI, LoopVectorizationCostModel &CM, ScalarEvolution &SE)
Prepare Plan for vectorizing the epilogue loop.
static cl::opt< bool > EnableLoadStoreRuntimeInterleave("enable-loadstore-runtime-interleave", cl::init(true), cl::Hidden, cl::desc("Enable runtime interleaving until load/store ports are saturated"))
static cl::opt< bool > VPlanBuildStressTest("vplan-build-stress-test", cl::init(false), cl::Hidden, cl::desc("Build VPlan for every supported loop nest in the function and bail " "out right after the build (stress test the VPlan H-CFG construction " "in the VPlan-native vectorization path)."))
static bool hasIrregularType(Type *Ty, const DataLayout &DL)
A helper function that returns true if the given type is irregular.
static cl::opt< bool > LoopVectorizeWithBlockFrequency("loop-vectorize-with-block-frequency", cl::init(true), cl::Hidden, cl::desc("Enable the use of the block frequency analysis to access PGO " "heuristics minimizing code growth in cold regions and being more " "aggressive in hot regions."))
static std::optional< ElementCount > getSmallBestKnownTC(PredicatedScalarEvolution &PSE, Loop *L, bool CanUseConstantMax=true)
Returns "best known" trip count, which is either a valid positive trip count or std::nullopt when an ...
static Value * getExpandedStep(const InductionDescriptor &ID, const SCEV2ValueTy &ExpandedSCEVs)
Return the expanded step for ID using ExpandedSCEVs to look up SCEV expansion results.
static bool useActiveLaneMask(TailFoldingStyle Style)
static bool hasReplicatorRegion(VPlan &Plan)
static bool isIndvarOverflowCheckKnownFalse(const LoopVectorizationCostModel *Cost, ElementCount VF, std::optional< unsigned > UF=std::nullopt)
For the given VF and UF and maximum trip count computed for the loop, return whether the induction va...
static void addFullyUnrolledInstructionsToIgnore(Loop *L, const LoopVectorizationLegality::InductionList &IL, SmallPtrSetImpl< Instruction * > &InstsToIgnore)
Knowing that loop L executes a single vector iteration, add instructions that will get simplified and...
static cl::opt< PreferPredicateTy::Option > PreferPredicateOverEpilogue("prefer-predicate-over-epilogue", cl::init(PreferPredicateTy::ScalarEpilogue), cl::Hidden, cl::desc("Tail-folding and predication preferences over creating a scalar " "epilogue loop."), cl::values(clEnumValN(PreferPredicateTy::ScalarEpilogue, "scalar-epilogue", "Don't tail-predicate loops, create scalar epilogue"), clEnumValN(PreferPredicateTy::PredicateElseScalarEpilogue, "predicate-else-scalar-epilogue", "prefer tail-folding, create scalar epilogue if tail " "folding fails."), clEnumValN(PreferPredicateTy::PredicateOrDontVectorize, "predicate-dont-vectorize", "prefers tail-folding, don't attempt vectorization if " "tail-folding fails.")))
static cl::opt< bool > EnableInterleavedMemAccesses("enable-interleaved-mem-accesses", cl::init(false), cl::Hidden, cl::desc("Enable vectorization on interleaved memory accesses in a loop"))
static cl::opt< bool > EnableMaskedInterleavedMemAccesses("enable-masked-interleaved-mem-accesses", cl::init(false), cl::Hidden, cl::desc("Enable vectorization on masked interleaved memory accesses in a loop"))
An interleave-group may need masking if it resides in a block that needs predication,...
static cl::opt< bool > ForceOrderedReductions("force-ordered-reductions", cl::init(false), cl::Hidden, cl::desc("Enable the vectorisation of loops with in-order (strict) " "FP reductions"))
static const SCEV * getAddressAccessSCEV(Value *Ptr, LoopVectorizationLegality *Legal, PredicatedScalarEvolution &PSE, const Loop *TheLoop)
Gets Address Access SCEV after verifying that the access pattern is loop invariant except the inducti...
static cl::opt< cl::boolOrDefault > ForceSafeDivisor("force-widen-divrem-via-safe-divisor", cl::Hidden, cl::desc("Override cost based safe divisor widening for div/rem instructions"))
static InstructionCost calculateEarlyExitCost(VPCostContext &CostCtx, VPlan &Plan, ElementCount VF)
For loops with uncountable early exits, find the cost of doing work when exiting the loop early,...
static cl::opt< unsigned > ForceTargetMaxVectorInterleaveFactor("force-target-max-vector-interleave", cl::init(0), cl::Hidden, cl::desc("A flag that overrides the target's max interleave factor for " "vectorized loops."))
static bool processLoopInVPlanNativePath(Loop *L, PredicatedScalarEvolution &PSE, LoopInfo *LI, DominatorTree *DT, LoopVectorizationLegality *LVL, TargetTransformInfo *TTI, TargetLibraryInfo *TLI, DemandedBits *DB, AssumptionCache *AC, OptimizationRemarkEmitter *ORE, BlockFrequencyInfo *BFI, ProfileSummaryInfo *PSI, LoopVectorizeHints &Hints, LoopVectorizationRequirements &Requirements)
static bool useMaskedInterleavedAccesses(const TargetTransformInfo &TTI)
static cl::opt< unsigned > NumberOfStoresToPredicate("vectorize-num-stores-pred", cl::init(1), cl::Hidden, cl::desc("Max number of stores to be predicated behind an if."))
The number of stores in a loop that are allowed to need predication.
static cl::opt< unsigned > MaxNestedScalarReductionIC("max-nested-scalar-reduction-interleave", cl::init(2), cl::Hidden, cl::desc("The maximum interleave count to use when interleaving a scalar " "reduction in a nested loop."))
static cl::opt< unsigned > ForceTargetMaxScalarInterleaveFactor("force-target-max-scalar-interleave", cl::init(0), cl::Hidden, cl::desc("A flag that overrides the target's max interleave factor for " "scalar loops."))
static void checkMixedPrecision(Loop *L, OptimizationRemarkEmitter *ORE)
static bool willGenerateVectors(VPlan &Plan, ElementCount VF, const TargetTransformInfo &TTI)
Check if any recipe of Plan will generate a vector value, which will be assigned a vector register.
static bool isOutsideLoopWorkProfitable(GeneratedRTChecks &Checks, VectorizationFactor &VF, Loop *L, PredicatedScalarEvolution &PSE, VPCostContext &CostCtx, VPlan &Plan, ScalarEpilogueLowering SEL, std::optional< unsigned > VScale)
This function determines whether or not it's still profitable to vectorize the loop given the extra w...
static void fixScalarResumeValuesFromBypass(BasicBlock *BypassBlock, Loop *L, VPlan &BestEpiPlan, LoopVectorizationLegality &LVL, const SCEV2ValueTy &ExpandedSCEVs, Value *MainVectorTripCount)
static cl::opt< bool > MaximizeBandwidth("vectorizer-maximize-bandwidth", cl::init(false), cl::Hidden, cl::desc("Maximize bandwidth when selecting vectorization factor which " "will be determined by the smallest type in loop."))
static OptimizationRemarkAnalysis createLVAnalysis(const char *PassName, StringRef RemarkName, Loop *TheLoop, Instruction *I, DebugLoc DL={})
Create an analysis remark that explains why vectorization failed.
#define F(x, y, z)
Definition MD5.cpp:55
#define I(x, y, z)
Definition MD5.cpp:58
This file implements a map that provides insertion order iteration.
This file contains the declarations for metadata subclasses.
#define T
ConstantRange Range(APInt(BitWidth, Low), APInt(BitWidth, High))
uint64_t IntrinsicInst * II
#define P(N)
This file contains the declarations for profiling metadata utility functions.
const SmallVectorImpl< MachineOperand > & Cond
static BinaryOperator * CreateMul(Value *S1, Value *S2, const Twine &Name, BasicBlock::iterator InsertBefore, Value *FlagsOp)
static BinaryOperator * CreateAdd(Value *S1, Value *S2, const Twine &Name, BasicBlock::iterator InsertBefore, Value *FlagsOp)
static bool isValid(const char C)
Returns true if C is a valid mangled character: <0-9a-zA-Z_>.
static InstructionCost getScalarizationOverhead(const TargetTransformInfo &TTI, Type *ScalarTy, VectorType *Ty, const APInt &DemandedElts, bool Insert, bool Extract, TTI::TargetCostKind CostKind, bool ForPoisonSrc=true, ArrayRef< Value * > VL={})
This is similar to TargetTransformInfo::getScalarizationOverhead, but if ScalarTy is a FixedVectorTyp...
This file contains some templates that are useful if you are working with the STL at all.
#define OP(OPC)
Definition Instruction.h:46
This file defines the SmallPtrSet class.
This file defines the SmallVector class.
This file defines the 'Statistic' class, which is designed to be an easy way to expose various metric...
#define STATISTIC(VARNAME, DESC)
Definition Statistic.h:171
#define LLVM_DEBUG(...)
Definition Debug.h:114
#define DEBUG_WITH_TYPE(TYPE,...)
DEBUG_WITH_TYPE macro - This macro should be used by passes to emit debug information.
Definition Debug.h:72
static TableGen::Emitter::Opt Y("gen-skeleton-entry", EmitSkeleton, "Generate example skeleton entry")
static TableGen::Emitter::OptClass< SkeletonEmitter > X("gen-skeleton-class", "Generate example skeleton class")
This pass exposes codegen information to IR-level passes.
LocallyHashedType DenseMapInfo< LocallyHashedType >::Empty
This file implements the TypeSwitch template, which mimics a switch() statement whose cases are type ...
This file contains the declarations of different VPlan-related auxiliary helpers.
This file provides utility VPlan to VPlan transformations.
This file declares the class VPlanVerifier, which contains utility functions to check the consistency...
This file contains the declarations of the Vectorization Plan base classes:
static const char PassName[]
Value * RHS
Value * LHS
static const uint32_t IV[8]
Definition blake3_impl.h:83
A manager for alias analyses.
Class for arbitrary precision integers.
Definition APInt.h:78
static APInt getAllOnes(unsigned numBits)
Return an APInt of a specified width with all bits set.
Definition APInt.h:234
uint64_t getZExtValue() const
Get zero extended value.
Definition APInt.h:1540
unsigned getActiveBits() const
Compute the number of active bits in the value.
Definition APInt.h:1512
PassT::Result & getResult(IRUnitT &IR, ExtraArgTs... ExtraArgs)
Get the result of an analysis pass for a given IR unit.
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:41
const T & back() const
back - Get the last element.
Definition ArrayRef.h:152
ArrayRef< T > take_front(size_t N=1) const
Return a copy of *this with only the first N elements.
Definition ArrayRef.h:220
size_t size() const
size - Get the array size.
Definition ArrayRef.h:143
A function analysis which provides an AssumptionCache.
A cache of @llvm.assume calls within a function.
LLVM_ABI unsigned getVScaleRangeMin() const
Returns the minimum value for the vscale_range attribute.
LLVM Basic Block Representation.
Definition BasicBlock.h:62
iterator_range< const_phi_iterator > phis() const
Returns a range that iterates over the phis in the basic block.
Definition BasicBlock.h:528
LLVM_ABI const_iterator getFirstInsertionPt() const
Returns an iterator to the first instruction in this block that is suitable for inserting a non-PHI i...
const Function * getParent() const
Return the enclosing method, or null if none.
Definition BasicBlock.h:213
LLVM_ABI InstListType::const_iterator getFirstNonPHIIt() const
Returns an iterator to the first instruction in this block that is not a PHINode instruction.
LLVM_ABI const BasicBlock * getSinglePredecessor() const
Return the predecessor of this block if it has a single predecessor block.
LLVM_ABI const BasicBlock * getSingleSuccessor() const
Return the successor of this block if it has a single successor.
LLVM_ABI const DataLayout & getDataLayout() const
Get the data layout of the module this basic block belongs to.
LLVM_ABI LLVMContext & getContext() const
Get the context in which this basic block lives.
const Instruction * getTerminator() const LLVM_READONLY
Returns the terminator instruction if the block is well formed or null if the block is not well forme...
Definition BasicBlock.h:233
BinaryOps getOpcode() const
Definition InstrTypes.h:374
Analysis pass which computes BlockFrequencyInfo.
BlockFrequencyInfo pass uses BlockFrequencyInfoImpl implementation to estimate IR basic block frequen...
Conditional or Unconditional Branch instruction.
bool isConditional() const
static BranchInst * Create(BasicBlock *IfTrue, InsertPosition InsertBefore=nullptr)
BasicBlock * getSuccessor(unsigned i) const
Represents analyses that only rely on functions' control flow.
Definition Analysis.h:73
bool isNoBuiltin() const
Return true if the call should not be treated as a call to a builtin.
Function * getCalledFunction() const
Returns the function called, or null if this is an indirect function invocation or the function signa...
Value * getArgOperand(unsigned i) const
iterator_range< User::op_iterator > args()
Iteration adapter for range-for loops.
unsigned arg_size() const
This class represents a function call, abstracting a target machine's calling convention.
static Type * makeCmpResultType(Type *opnd_type)
Create a result type for fcmp/icmp.
Definition InstrTypes.h:982
Predicate
This enumeration lists the possible predicates for CmpInst subclasses.
Definition InstrTypes.h:676
@ ICMP_UGT
unsigned greater than
Definition InstrTypes.h:699
@ ICMP_ULT
unsigned less than
Definition InstrTypes.h:701
@ ICMP_NE
not equal
Definition InstrTypes.h:698
@ ICMP_ULE
unsigned less or equal
Definition InstrTypes.h:702
Predicate getInversePredicate() const
For example, EQ -> NE, UGT -> ULE, SLT -> SGE, OEQ -> UNE, UGT -> OLE, OLT -> UGE,...
Definition InstrTypes.h:789
An abstraction over a floating-point predicate, and a pack of an integer predicate with samesign info...
This is the shared class of boolean and integer constants.
Definition Constants.h:87
static LLVM_ABI ConstantInt * getTrue(LLVMContext &Context)
A parsed version of the target data layout string in and methods for querying it.
Definition DataLayout.h:63
A debug info location.
Definition DebugLoc.h:124
static DebugLoc getTemporary()
Definition DebugLoc.h:161
static DebugLoc getUnknown()
Definition DebugLoc.h:162
An analysis that produces DemandedBits for a function.
ValueT lookup(const_arg_type_t< KeyT > Val) const
lookup - Return the entry for the specified key, or a default constructed value if no such entry exis...
Definition DenseMap.h:205
iterator find(const_arg_type_t< KeyT > Val)
Definition DenseMap.h:178
std::pair< iterator, bool > try_emplace(KeyT &&Key, Ts &&...Args)
Definition DenseMap.h:248
iterator end()
Definition DenseMap.h:81
bool contains(const_arg_type_t< KeyT > Val) const
Return true if the specified key is in the map, false otherwise.
Definition DenseMap.h:169
void insert_range(Range &&R)
Inserts range of 'std::pair<KeyT, ValueT>' values into the map.
Definition DenseMap.h:286
Implements a dense probed hash-table based set.
Definition DenseSet.h:279
Analysis pass which computes a DominatorTree.
Definition Dominators.h:284
void changeImmediateDominator(DomTreeNodeBase< NodeT > *N, DomTreeNodeBase< NodeT > *NewIDom)
changeImmediateDominator - This method is used to update the dominator tree information when a node's...
void eraseNode(NodeT *BB)
eraseNode - Removes a node from the dominator tree.
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree.
Definition Dominators.h:165
constexpr bool isVector() const
One or more elements.
Definition TypeSize.h:325
static constexpr ElementCount getScalable(ScalarTy MinVal)
Definition TypeSize.h:313
static constexpr ElementCount getFixed(ScalarTy MinVal)
Definition TypeSize.h:310
static constexpr ElementCount get(ScalarTy MinVal, bool Scalable)
Definition TypeSize.h:316
constexpr bool isScalar() const
Exactly one element.
Definition TypeSize.h:321
BasicBlock * createVectorizedLoopSkeleton() final
Implements the interface for creating a vectorized skeleton using the epilogue loop strategy (i....
EpilogueVectorizerEpilogueLoop(Loop *OrigLoop, PredicatedScalarEvolution &PSE, LoopInfo *LI, DominatorTree *DT, const TargetTransformInfo *TTI, AssumptionCache *AC, EpilogueLoopVectorizationInfo &EPI, LoopVectorizationCostModel *CM, BlockFrequencyInfo *BFI, ProfileSummaryInfo *PSI, GeneratedRTChecks &Checks, VPlan &Plan)
void printDebugTracesAtStart() override
Allow subclasses to override and print debug traces before/after vplan execution, when trace informat...
A specialized derived class of inner loop vectorizer that performs vectorization of main loops in the...
void introduceCheckBlockInVPlan(BasicBlock *CheckIRBB)
Introduces a new VPIRBasicBlock for CheckIRBB to Plan between the vector preheader and its predecesso...
BasicBlock * emitIterationCountCheck(BasicBlock *VectorPH, BasicBlock *Bypass, bool ForEpilogue)
Emits an iteration count bypass check once for the main loop (when ForEpilogue is false) and once for...
EpilogueVectorizerMainLoop(Loop *OrigLoop, PredicatedScalarEvolution &PSE, LoopInfo *LI, DominatorTree *DT, const TargetTransformInfo *TTI, AssumptionCache *AC, EpilogueLoopVectorizationInfo &EPI, LoopVectorizationCostModel *CM, BlockFrequencyInfo *BFI, ProfileSummaryInfo *PSI, GeneratedRTChecks &Check, VPlan &Plan)
Value * createIterationCountCheck(BasicBlock *VectorPH, ElementCount VF, unsigned UF) const
void printDebugTracesAtStart() override
Allow subclasses to override and print debug traces before/after vplan execution, when trace informat...
BasicBlock * createVectorizedLoopSkeleton() final
Implements the interface for creating a vectorized skeleton using the main loop strategy (i....
Convenience struct for specifying and reasoning about fast-math flags.
Definition FMF.h:22
Class to represent function types.
param_iterator param_begin() const
param_iterator param_end() const
FunctionType * getFunctionType() const
Returns the FunctionType for me.
Definition Function.h:209
Attribute getFnAttribute(Attribute::AttrKind Kind) const
Return the attribute for the given attribute kind.
Definition Function.cpp:762
bool hasFnAttribute(Attribute::AttrKind Kind) const
Return true if the function has the attribute.
Definition Function.cpp:727
Represents flags for the getelementptr instruction/expression.
static GEPNoWrapFlags none()
void applyUpdates(ArrayRef< UpdateT > Updates)
Submit updates to all available trees.
Common base class shared among various IRBuilders.
Definition IRBuilder.h:114
void setFastMathFlags(FastMathFlags NewFMF)
Set the fast-math flags to be used with generated fp-math operators.
Definition IRBuilder.h:345
This provides a uniform API for creating instructions and inserting them into a basic block: either a...
Definition IRBuilder.h:2788
A struct for saving information about induction variables.
const SCEV * getStep() const
InductionKind
This enum represents the kinds of inductions that we support.
@ IK_NoInduction
Not an induction variable.
@ IK_FpInduction
Floating point induction variable.
@ IK_PtrInduction
Pointer induction var. Step = C.
@ IK_IntInduction
Integer induction variable. Step = C.
const SmallVectorImpl< Instruction * > & getCastInsts() const
Returns a reference to the type cast instructions in the induction update chain, that are redundant w...
Value * getStartValue() const
InnerLoopAndEpilogueVectorizer(Loop *OrigLoop, PredicatedScalarEvolution &PSE, LoopInfo *LI, DominatorTree *DT, const TargetTransformInfo *TTI, AssumptionCache *AC, EpilogueLoopVectorizationInfo &EPI, LoopVectorizationCostModel *CM, BlockFrequencyInfo *BFI, ProfileSummaryInfo *PSI, GeneratedRTChecks &Checks, VPlan &Plan, ElementCount VecWidth, ElementCount MinProfitableTripCount, unsigned UnrollFactor)
EpilogueLoopVectorizationInfo & EPI
Holds and updates state information required to vectorize the main loop and its epilogue in two separ...
InnerLoopVectorizer vectorizes loops which contain only one basic block to a specified vectorization ...
virtual void printDebugTracesAtStart()
Allow subclasses to override and print debug traces before/after vplan execution, when trace informat...
Value * TripCount
Trip count of the original loop.
const TargetTransformInfo * TTI
Target Transform Info.
LoopVectorizationCostModel * Cost
The profitablity analysis.
BlockFrequencyInfo * BFI
BFI and PSI are used to check for profile guided size optimizations.
Value * getTripCount() const
Returns the original loop trip count.
friend class LoopVectorizationPlanner
PredicatedScalarEvolution & PSE
A wrapper around ScalarEvolution used to add runtime SCEV checks.
LoopInfo * LI
Loop Info.
ProfileSummaryInfo * PSI
DominatorTree * DT
Dominator Tree.
void setTripCount(Value *TC)
Used to set the trip count after ILV's construction and after the preheader block has been executed.
void fixVectorizedLoop(VPTransformState &State)
Fix the vectorized code, taking care of header phi's, and more.
virtual BasicBlock * createVectorizedLoopSkeleton()
Creates a basic block for the scalar preheader.
virtual void printDebugTracesAtEnd()
AssumptionCache * AC
Assumption Cache.
InnerLoopVectorizer(Loop *OrigLoop, PredicatedScalarEvolution &PSE, LoopInfo *LI, DominatorTree *DT, const TargetTransformInfo *TTI, AssumptionCache *AC, ElementCount VecWidth, unsigned UnrollFactor, LoopVectorizationCostModel *CM, BlockFrequencyInfo *BFI, ProfileSummaryInfo *PSI, GeneratedRTChecks &RTChecks, VPlan &Plan)
IRBuilder Builder
The builder that we use.
void fixNonInductionPHIs(VPTransformState &State)
Fix the non-induction PHIs in Plan.
VPBasicBlock * VectorPHVPBB
The vector preheader block of Plan, used as target for check blocks introduced during skeleton creati...
unsigned UF
The vectorization unroll factor to use.
GeneratedRTChecks & RTChecks
Structure to hold information about generated runtime checks, responsible for cleaning the checks,...
virtual ~InnerLoopVectorizer()=default
ElementCount VF
The vectorization SIMD factor to use.
Loop * OrigLoop
The original loop.
BasicBlock * createScalarPreheader(StringRef Prefix)
Create and return a new IR basic block for the scalar preheader whose name is prefixed with Prefix.
InstSimplifyFolder - Use InstructionSimplify to fold operations to existing values.
static InstructionCost getInvalid(CostType Val=0)
static InstructionCost getMax()
CostType getValue() const
This function is intended to be used as sparingly as possible, since the class provides the full rang...
const DebugLoc & getDebugLoc() const
Return the debug location for this node as a DebugLoc.
LLVM_ABI const Module * getModule() const
Return the module owning the function this instruction belongs to or nullptr it the function does not...
LLVM_ABI void moveBefore(InstListType::iterator InsertPos)
Unlink this instruction from its current basic block and insert it into the basic block that MovePos ...
bool isBinaryOp() const
LLVM_ABI InstListType::iterator eraseFromParent()
This method unlinks 'this' from the containing basic block and deletes it.
Instruction * user_back()
Specialize the methods defined in Value, as we know that an instruction can only be used by other ins...
LLVM_ABI FastMathFlags getFastMathFlags() const LLVM_READONLY
Convenience function for getting all the fast-math flags, which must be an operator which supports th...
const char * getOpcodeName() const
unsigned getOpcode() const
Returns a member of one of the enums like Instruction::Add.
Class to represent integer types.
static LLVM_ABI IntegerType * get(LLVMContext &C, unsigned NumBits)
This static method is the primary way of constructing an IntegerType.
Definition Type.cpp:319
LLVM_ABI APInt getMask() const
For example, this is 0xFF for an 8 bit integer, 0xFFFF for i16, etc.
Definition Type.cpp:343
The group of interleaved loads/stores sharing the same stride and close to each other.
uint32_t getFactor() const
InstTy * getMember(uint32_t Index) const
Get the member with the given index Index.
InstTy * getInsertPos() const
uint32_t getNumMembers() const
Drive the analysis of interleaved memory accesses in the loop.
bool requiresScalarEpilogue() const
Returns true if an interleaved group that may access memory out-of-bounds requires a scalar epilogue ...
LLVM_ABI void analyzeInterleaving(bool EnableMaskedInterleavedGroup)
Analyze the interleaved accesses and collect them in interleave groups.
An instruction for reading from memory.
Type * getPointerOperandType() const
This analysis provides dependence information for the memory accesses of a loop.
Drive the analysis of memory accesses in the loop.
const RuntimePointerChecking * getRuntimePointerChecking() const
unsigned getNumRuntimePointerChecks() const
Number of memchecks required to prove independence of otherwise may-alias pointers.
Analysis pass that exposes the LoopInfo for a function.
Definition LoopInfo.h:569
bool contains(const LoopT *L) const
Return true if the specified loop is contained within in this loop.
BlockT * getLoopLatch() const
If there is a single latch block for this loop, return it.
bool isInnermost() const
Return true if the loop does not contain any (natural) loops.
void getExitingBlocks(SmallVectorImpl< BlockT * > &ExitingBlocks) const
Return all blocks inside the loop that have successors outside of the loop.
BlockT * getHeader() const
iterator_range< block_iterator > blocks() const
BlockT * getLoopPreheader() const
If there is a preheader for this loop, return it.
ArrayRef< BlockT * > getBlocks() const
Get a list of the basic blocks which make up this loop.
Store the result of a depth first search within basic blocks contained by a single loop.
RPOIterator beginRPO() const
Reverse iterate over the cached postorder blocks.
void perform(const LoopInfo *LI)
Traverse the loop blocks and store the DFS result.
RPOIterator endRPO() const
Wrapper class to LoopBlocksDFS that provides a standard begin()/end() interface for the DFS reverse p...
void perform(const LoopInfo *LI)
Traverse the loop blocks and store the DFS result.
void removeBlock(BlockT *BB)
This method completely removes BB from all data structures, including all of the Loop objects it is n...
LoopVectorizationCostModel - estimates the expected speedups due to vectorization.
SmallPtrSet< Type *, 16 > ElementTypesInLoop
All element types found in the loop.
bool isLegalMaskedLoad(Type *DataType, Value *Ptr, Align Alignment, unsigned AddressSpace) const
Returns true if the target machine supports masked load operation for the given DataType and kind of ...
LoopVectorizationCostModel(ScalarEpilogueLowering SEL, Loop *L, PredicatedScalarEvolution &PSE, LoopInfo *LI, LoopVectorizationLegality *Legal, const TargetTransformInfo &TTI, const TargetLibraryInfo *TLI, DemandedBits *DB, AssumptionCache *AC, OptimizationRemarkEmitter *ORE, const Function *F, const LoopVectorizeHints *Hints, InterleavedAccessInfo &IAI, ProfileSummaryInfo *PSI, BlockFrequencyInfo *BFI)
void collectElementTypesForWidening()
Collect all element types in the loop for which widening is needed.
bool canVectorizeReductions(ElementCount VF) const
Returns true if the target machine supports all of the reduction variables found for the given VF.
bool isLegalMaskedStore(Type *DataType, Value *Ptr, Align Alignment, unsigned AddressSpace) const
Returns true if the target machine supports masked store operation for the given DataType and kind of...
bool isEpilogueVectorizationProfitable(const ElementCount VF, const unsigned IC) const
Returns true if epilogue vectorization is considered profitable, and false otherwise.
bool isPredicatedInst(Instruction *I) const
Returns true if I is an instruction that needs to be predicated at runtime.
void collectValuesToIgnore()
Collect values we want to ignore in the cost model.
void collectInLoopReductions()
Split reductions into those that happen in the loop, and those that happen outside.
std::pair< unsigned, unsigned > getSmallestAndWidestTypes()
bool isUniformAfterVectorization(Instruction *I, ElementCount VF) const
Returns true if I is known to be uniform after vectorization.
void collectNonVectorizedAndSetWideningDecisions(ElementCount VF)
Collect values that will not be widened, including Uniforms, Scalars, and Instructions to Scalarize f...
PredicatedScalarEvolution & PSE
Predicated scalar evolution analysis.
const LoopVectorizeHints * Hints
Loop Vectorize Hint.
std::optional< unsigned > getMaxSafeElements() const
Return maximum safe number of elements to be processed per vector iteration, which do not prevent sto...
const TargetTransformInfo & TTI
Vector target information.
LoopVectorizationLegality * Legal
Vectorization legality.
std::optional< InstructionCost > getReductionPatternCost(Instruction *I, ElementCount VF, Type *VectorTy) const
Return the cost of instructions in an inloop reduction pattern, if I is part of that pattern.
InstructionCost getInstructionCost(Instruction *I, ElementCount VF)
Returns the execution time cost of an instruction for a given vector width.
DemandedBits * DB
Demanded bits analysis.
bool interleavedAccessCanBeWidened(Instruction *I, ElementCount VF) const
Returns true if I is a memory instruction in an interleaved-group of memory accesses that can be vect...
const TargetLibraryInfo * TLI
Target Library Info.
bool memoryInstructionCanBeWidened(Instruction *I, ElementCount VF)
Returns true if I is a memory instruction with consecutive memory access that can be widened.
const InterleaveGroup< Instruction > * getInterleavedAccessGroup(Instruction *Instr) const
Get the interleaved access group that Instr belongs to.
InstructionCost getVectorIntrinsicCost(CallInst *CI, ElementCount VF) const
Estimate cost of an intrinsic call instruction CI if it were vectorized with factor VF.
bool OptForSize
Whether this loop should be optimized for size based on function attribute or profile information.
bool useMaxBandwidth(TargetTransformInfo::RegisterKind RegKind)
bool isScalarAfterVectorization(Instruction *I, ElementCount VF) const
Returns true if I is known to be scalar after vectorization.
bool isOptimizableIVTruncate(Instruction *I, ElementCount VF)
Return True if instruction I is an optimizable truncate whose operand is an induction variable.
FixedScalableVFPair computeMaxVF(ElementCount UserVF, unsigned UserIC)
bool shouldConsiderRegPressureForVF(ElementCount VF)
Loop * TheLoop
The loop that we evaluate.
TTI::TargetCostKind CostKind
The kind of cost that we are calculating.
TailFoldingStyle getTailFoldingStyle(bool IVUpdateMayOverflow=true) const
Returns the TailFoldingStyle that is best for the current loop.
InterleavedAccessInfo & InterleaveInfo
The interleave access information contains groups of interleaved accesses with the same stride and cl...
SmallPtrSet< const Value *, 16 > ValuesToIgnore
Values to ignore in the cost model.
void setVectorizedCallDecision(ElementCount VF)
A call may be vectorized in different ways depending on whether we have vectorized variants available...
void invalidateCostModelingDecisions()
Invalidates decisions already taken by the cost model.
bool isAccessInterleaved(Instruction *Instr) const
Check if Instr belongs to any interleaved access group.
bool selectUserVectorizationFactor(ElementCount UserVF)
Setup cost-based decisions for user vectorization factor.
std::optional< unsigned > getVScaleForTuning() const
Return the value of vscale used for tuning the cost model.
OptimizationRemarkEmitter * ORE
Interface to emit optimization remarks.
LoopInfo * LI
Loop Info analysis.
bool requiresScalarEpilogue(bool IsVectorizing) const
Returns true if we're required to use a scalar epilogue for at least the final iteration of the origi...
SmallPtrSet< const Value *, 16 > VecValuesToIgnore
Values to ignore in the cost model when VF > 1.
bool isInLoopReduction(PHINode *Phi) const
Returns true if the Phi is part of an inloop reduction.
bool isProfitableToScalarize(Instruction *I, ElementCount VF) const
void setWideningDecision(const InterleaveGroup< Instruction > *Grp, ElementCount VF, InstWidening W, InstructionCost Cost)
Save vectorization decision W and Cost taken by the cost model for interleaving group Grp and vector ...
const MapVector< Instruction *, uint64_t > & getMinimalBitwidths() const
CallWideningDecision getCallWideningDecision(CallInst *CI, ElementCount VF) const
bool isLegalGatherOrScatter(Value *V, ElementCount VF)
Returns true if the target machine can represent V as a masked gather or scatter operation.
bool canTruncateToMinimalBitwidth(Instruction *I, ElementCount VF) const
bool shouldConsiderInvariant(Value *Op)
Returns true if Op should be considered invariant and if it is trivially hoistable.
bool foldTailByMasking() const
Returns true if all loop blocks should be masked to fold tail loop.
bool foldTailWithEVL() const
Returns true if VP intrinsics with explicit vector length support should be generated in the tail fol...
bool usePredicatedReductionSelect() const
Returns true if the predicated reduction select should be used to set the incoming value for the redu...
bool blockNeedsPredicationForAnyReason(BasicBlock *BB) const
Returns true if the instructions in this block requires predication for any reason,...
void setCallWideningDecision(CallInst *CI, ElementCount VF, InstWidening Kind, Function *Variant, Intrinsic::ID IID, std::optional< unsigned > MaskPos, InstructionCost Cost)
void setTailFoldingStyles(bool IsScalableVF, unsigned UserIC)
Selects and saves TailFoldingStyle for 2 options - if IV update may overflow or not.
AssumptionCache * AC
Assumption cache.
void setWideningDecision(Instruction *I, ElementCount VF, InstWidening W, InstructionCost Cost)
Save vectorization decision W and Cost taken by the cost model for instruction I and vector width VF.
InstWidening
Decision that was taken during cost calculation for memory instruction.
bool isScalarWithPredication(Instruction *I, ElementCount VF) const
Returns true if I is an instruction which requires predication and for which our chosen predication s...
InstructionCost getVectorCallCost(CallInst *CI, ElementCount VF) const
Estimate cost of a call instruction CI if it were vectorized with factor VF.
bool useOrderedReductions(const RecurrenceDescriptor &RdxDesc) const
Returns true if we should use strict in-order reductions for the given RdxDesc.
std::pair< InstructionCost, InstructionCost > getDivRemSpeculationCost(Instruction *I, ElementCount VF) const
Return the costs for our two available strategies for lowering a div/rem operation which requires spe...
bool isDivRemScalarWithPredication(InstructionCost ScalarCost, InstructionCost SafeDivisorCost) const
Given costs for both strategies, return true if the scalar predication lowering should be used for di...
InstructionCost expectedCost(ElementCount VF)
Returns the expected execution cost.
void setCostBasedWideningDecision(ElementCount VF)
Memory access instruction may be vectorized in more than one way.
InstWidening getWideningDecision(Instruction *I, ElementCount VF) const
Return the cost model decision for the given instruction I and vector width VF.
FixedScalableVFPair MaxPermissibleVFWithoutMaxBW
The highest VF possible for this loop, without using MaxBandwidth.
bool isScalarEpilogueAllowed() const
Returns true if a scalar epilogue is not allowed due to optsize or a loop hint annotation.
InstructionCost getWideningCost(Instruction *I, ElementCount VF)
Return the vectorization cost for the given instruction I and vector width VF.
void collectInstsToScalarize(ElementCount VF)
Collects the instructions to scalarize for each predicated instruction in the loop.
LoopVectorizationLegality checks if it is legal to vectorize a loop, and to what vectorization factor...
MapVector< PHINode *, InductionDescriptor > InductionList
InductionList saves induction variables and maps them to the induction descriptor.
const SmallPtrSetImpl< const Instruction * > & getPotentiallyFaultingLoads() const
Returns potentially faulting loads.
bool canVectorize(bool UseVPlanNativePath)
Returns true if it is legal to vectorize this loop.
bool canVectorizeFPMath(bool EnableStrictReductions)
Returns true if it is legal to vectorize the FP math operations in this loop.
PHINode * getPrimaryInduction()
Returns the primary induction variable.
const SmallVector< BasicBlock *, 4 > & getCountableExitingBlocks() const
Returns all exiting blocks with a countable exit, i.e.
const InductionList & getInductionVars() const
Returns the induction variables found in the loop.
bool hasUncountableEarlyExit() const
Returns true if the loop has exactly one uncountable early exit, i.e.
bool hasHistograms() const
Returns a list of all known histogram operations in the loop.
const LoopAccessInfo * getLAI() const
Planner drives the vectorization process after having passed Legality checks.
VectorizationFactor selectEpilogueVectorizationFactor(const ElementCount MaxVF, unsigned IC)
VPlan & getPlanFor(ElementCount VF) const
Return the VPlan for VF.
Definition VPlan.cpp:1592
VectorizationFactor planInVPlanNativePath(ElementCount UserVF)
Use the VPlan-native path to plan how to best vectorize, return the best VF and its cost.
void updateLoopMetadataAndProfileInfo(Loop *VectorLoop, VPBasicBlock *HeaderVPBB, const VPlan &Plan, bool VectorizingEpilogue, MDNode *OrigLoopID, std::optional< unsigned > OrigAverageTripCount, unsigned OrigLoopInvocationWeight, unsigned EstimatedVFxUF, bool DisableRuntimeUnroll)
Update loop metadata and profile info for both the scalar remainder loop and VectorLoop,...
Definition VPlan.cpp:1643
void buildVPlans(ElementCount MinVF, ElementCount MaxVF)
Build VPlans for power-of-2 VF's between MinVF and MaxVF inclusive, according to the information gath...
Definition VPlan.cpp:1576
VectorizationFactor computeBestVF()
Compute and return the most profitable vectorization factor.
DenseMap< const SCEV *, Value * > executePlan(ElementCount VF, unsigned UF, VPlan &BestPlan, InnerLoopVectorizer &LB, DominatorTree *DT, bool VectorizingEpilogue)
Generate the IR code for the vectorized loop captured in VPlan BestPlan according to the best selecte...
unsigned selectInterleaveCount(VPlan &Plan, ElementCount VF, InstructionCost LoopCost)
void emitInvalidCostRemarks(OptimizationRemarkEmitter *ORE)
Emit remarks for recipes with invalid costs in the available VPlans.
static bool getDecisionAndClampRange(const std::function< bool(ElementCount)> &Predicate, VFRange &Range)
Test a Predicate on a Range of VF's.
Definition VPlan.cpp:1557
void printPlans(raw_ostream &O)
Definition VPlan.cpp:1721
void plan(ElementCount UserVF, unsigned UserIC)
Build VPlans for the specified UserVF and UserIC if they are non-zero or all applicable candidate VFs...
void addMinimumIterationCheck(VPlan &Plan, ElementCount VF, unsigned UF, ElementCount MinProfitableTripCount) const
Create a check to Plan to see if the vector loop should be executed based on its trip count.
bool hasPlanWithVF(ElementCount VF) const
Look through the existing plans and return true if we have one with vectorization factor VF.
This holds vectorization requirements that must be verified late in the process.
Utility class for getting and setting loop vectorizer hints in the form of loop metadata.
bool allowVectorization(Function *F, Loop *L, bool VectorizeOnlyWhenForced) const
void emitRemarkWithHints() const
Dumps all the hint information.
const char * vectorizeAnalysisPassName() const
If hints are provided that force vectorization, use the AlwaysPrint pass name to force the frontend t...
This class emits a version of the loop where run-time checks ensure that may-alias pointers can't ove...
Represents a single loop in the control flow graph.
Definition LoopInfo.h:40
bool hasLoopInvariantOperands(const Instruction *I) const
Return true if all the operands of the specified instruction are loop invariant.
Definition LoopInfo.cpp:67
DebugLoc getStartLoc() const
Return the debug location of the start of this loop.
Definition LoopInfo.cpp:632
bool isLoopInvariant(const Value *V) const
Return true if the specified value is loop invariant.
Definition LoopInfo.cpp:61
Metadata node.
Definition Metadata.h:1078
This class implements a map that also provides access to all stored values in a deterministic order.
Definition MapVector.h:36
std::pair< iterator, bool > insert(const std::pair< KeyT, ValueT > &KV)
Definition MapVector.h:119
Function * getFunction(StringRef Name) const
Look up the specified function in the module symbol table.
Definition Module.cpp:230
Diagnostic information for optimization analysis remarks related to pointer aliasing.
Diagnostic information for optimization analysis remarks related to floating-point non-commutativity.
Diagnostic information for optimization analysis remarks.
The optimization diagnostic interface.
LLVM_ABI void emit(DiagnosticInfoOptimizationBase &OptDiag)
Output the remark via the diagnostic handler and to the optimization record file.
Diagnostic information for missed-optimization remarks.
Diagnostic information for applied optimization remarks.
void addIncoming(Value *V, BasicBlock *BB)
Add an incoming value to the end of the PHI list.
op_range incoming_values()
void setIncomingValueForBlock(const BasicBlock *BB, Value *V)
Set every incoming value(s) for block BB to V.
Value * getIncomingValueForBlock(const BasicBlock *BB) const
unsigned getNumIncomingValues() const
Return the number of incoming edges.
An interface layer with SCEV used to manage how we see SCEV expressions for values in the context of ...
ScalarEvolution * getSE() const
Returns the ScalarEvolution analysis used.
LLVM_ABI const SCEVPredicate & getPredicate() const
LLVM_ABI unsigned getSmallConstantMaxTripCount()
Returns the upper bound of the loop trip count as a normal unsigned value, or 0 if the trip count is ...
LLVM_ABI const SCEV * getBackedgeTakenCount()
Get the (predicated) backedge count for the analyzed loop.
LLVM_ABI const SCEV * getSCEV(Value *V)
Returns the SCEV expression of V, in the context of the current SCEV predicate.
A set of analyses that are preserved following a run of a transformation pass.
Definition Analysis.h:112
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition Analysis.h:118
PreservedAnalyses & preserveSet()
Mark an analysis set as preserved.
Definition Analysis.h:151
PreservedAnalyses & preserve()
Mark an analysis as preserved.
Definition Analysis.h:132
An analysis pass based on the new PM to deliver ProfileSummaryInfo.
Analysis providing profile information.
The RecurrenceDescriptor is used to identify recurrences variables in a loop.
static bool isFMulAddIntrinsic(Instruction *I)
Returns true if the instruction is a call to the llvm.fmuladd intrinsic.
FastMathFlags getFastMathFlags() const
Instruction * getLoopExitInstr() const
static LLVM_ABI unsigned getOpcode(RecurKind Kind)
Returns the opcode corresponding to the RecurrenceKind.
Type * getRecurrenceType() const
Returns the type of the recurrence.
const SmallPtrSet< Instruction *, 8 > & getCastInsts() const
Returns a reference to the instructions used for type-promoting the recurrence.
unsigned getMinWidthCastToRecurrenceTypeInBits() const
Returns the minimum width used by the recurrence in bits.
TrackingVH< Value > getRecurrenceStartValue() const
LLVM_ABI SmallVector< Instruction *, 4 > getReductionOpChain(PHINode *Phi, Loop *L) const
Attempts to find a chain of operations from Phi to LoopExitInst that can be treated as a set of reduc...
static bool isAnyOfRecurrenceKind(RecurKind Kind)
Returns true if the recurrence kind is of the form select(cmp(),x,y) where one of (x,...
bool isSigned() const
Returns true if all source operands of the recurrence are SExtInsts.
RecurKind getRecurrenceKind() const
bool isOrdered() const
Expose an ordered FP reduction to the instance users.
static LLVM_ABI bool isFloatingPointRecurrenceKind(RecurKind Kind)
Returns true if the recurrence kind is a floating point kind.
static bool isFindIVRecurrenceKind(RecurKind Kind)
Returns true if the recurrence kind is of the form select(cmp(),x,y) where one of (x,...
Value * getSentinelValue() const
Returns the sentinel value for FindFirstIV & FindLastIV recurrences to replace the start value.
static bool isMinMaxRecurrenceKind(RecurKind Kind)
Returns true if the recurrence kind is any min/max kind.
std::optional< ArrayRef< PointerDiffInfo > > getDiffChecks() const
const SmallVectorImpl< RuntimePointerCheck > & getChecks() const
Returns the checks that generateChecks created.
This class uses information about analyze scalars to rewrite expressions in canonical form.
ScalarEvolution * getSE()
bool isInsertedInstruction(Instruction *I) const
Return true if the specified instruction was inserted by the code rewriter.
LLVM_ABI Value * expandCodeForPredicate(const SCEVPredicate *Pred, Instruction *Loc)
Generates a code sequence that evaluates this predicate.
void eraseDeadInstructions(Value *Root)
Remove inserted instructions that are dead, e.g.
virtual bool isAlwaysTrue() const =0
Returns true if the predicate is always true.
This class represents an analyzed expression in the program.
LLVM_ABI bool isZero() const
Return true if the expression is a constant zero.
LLVM_ABI Type * getType() const
Return the LLVM type of this SCEV expression.
Analysis pass that exposes the ScalarEvolution for a function.
The main scalar evolution driver.
LLVM_ABI const SCEV * getURemExpr(const SCEV *LHS, const SCEV *RHS)
Represents an unsigned remainder expression based on unsigned division.
LLVM_ABI const SCEV * getBackedgeTakenCount(const Loop *L, ExitCountKind Kind=Exact)
If the specified loop has a predictable backedge-taken count, return it, otherwise return a SCEVCould...
LLVM_ABI const SCEV * getConstant(ConstantInt *V)
LLVM_ABI const SCEV * getSCEV(Value *V)
Return a SCEV expression for the full generality of the specified expression.
LLVM_ABI const SCEV * getTripCountFromExitCount(const SCEV *ExitCount)
A version of getTripCountFromExitCount below which always picks an evaluation type which can not resu...
const SCEV * getOne(Type *Ty)
Return a SCEV for the constant 1 of a specific type.
LLVM_ABI void forgetLoop(const Loop *L)
This method should be called by the client when it has changed a loop in a way that may effect Scalar...
LLVM_ABI bool isLoopInvariant(const SCEV *S, const Loop *L)
Return true if the value of the given SCEV is unchanging in the specified loop.
LLVM_ABI bool isSCEVable(Type *Ty) const
Test if values of the given type are analyzable within the SCEV framework.
LLVM_ABI const SCEV * getElementCount(Type *Ty, ElementCount EC, SCEV::NoWrapFlags Flags=SCEV::FlagAnyWrap)
LLVM_ABI void forgetValue(Value *V)
This method should be called by the client when it has changed a value in a way that may effect its v...
LLVM_ABI void forgetBlockAndLoopDispositions(Value *V=nullptr)
Called when the client has changed the disposition of values in a loop or block.
const SCEV * getMinusOne(Type *Ty)
Return a SCEV for the constant -1 of a specific type.
LLVM_ABI void forgetLcssaPhiWithNewPredecessor(Loop *L, PHINode *V)
Forget LCSSA phi node V of loop L to which a new predecessor was added, such that it may no longer be...
LLVM_ABI unsigned getSmallConstantTripCount(const Loop *L)
Returns the exact trip count of the loop if we can compute it, and the result is a small constant.
APInt getUnsignedRangeMax(const SCEV *S)
Determine the max of the unsigned range for a particular SCEV.
LLVM_ABI const SCEV * applyLoopGuards(const SCEV *Expr, const Loop *L)
Try to apply information from loop guards for L to Expr.
LLVM_ABI const SCEV * getMulExpr(SmallVectorImpl< const SCEV * > &Ops, SCEV::NoWrapFlags Flags=SCEV::FlagAnyWrap, unsigned Depth=0)
Get a canonical multiply expression, or something simpler if possible.
LLVM_ABI const SCEV * getAddExpr(SmallVectorImpl< const SCEV * > &Ops, SCEV::NoWrapFlags Flags=SCEV::FlagAnyWrap, unsigned Depth=0)
Get a canonical add expression, or something simpler if possible.
LLVM_ABI bool isKnownPredicate(CmpPredicate Pred, const SCEV *LHS, const SCEV *RHS)
Test if the given expression is known to satisfy the condition described by Pred, LHS,...
This class represents the LLVM 'select' instruction.
A vector that has set insertion semantics.
Definition SetVector.h:59
size_type size() const
Determine the number of elements in the SetVector.
Definition SetVector.h:102
void insert_range(Range &&R)
Definition SetVector.h:175
size_type count(const key_type &key) const
Count the number of elements of a given key in the SetVector.
Definition SetVector.h:261
bool insert(const value_type &X)
Insert a new element into the SetVector.
Definition SetVector.h:150
A templated base class for SmallPtrSet which provides the typesafe interface that is common across al...
size_type count(ConstPtrType Ptr) const
count - Return 1 if the specified pointer is in the set, 0 otherwise.
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
bool contains(ConstPtrType Ptr) const
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements.
A SetVector that performs no allocations if smaller than a certain size.
Definition SetVector.h:338
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
reference emplace_back(ArgTypes &&... Args)
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
An instruction for storing to memory.
StringRef - Represent a constant reference to a string, i.e.
Definition StringRef.h:55
Analysis pass providing the TargetTransformInfo.
Analysis pass providing the TargetLibraryInfo.
Provides information about what library functions are available for the current target.
This pass provides access to the codegen interfaces that are needed for IR-level transformations.
LLVM_ABI std::optional< unsigned > getVScaleForTuning() const
LLVM_ABI InstructionCost getScalarizationOverhead(VectorType *Ty, const APInt &DemandedElts, bool Insert, bool Extract, TTI::TargetCostKind CostKind, bool ForPoisonSrc=true, ArrayRef< Value * > VL={}) const
Estimate the overhead of scalarizing an instruction.
LLVM_ABI bool supportsEfficientVectorElementLoadStore() const
If target has efficient vector element load/store instructions, it can return true here so that inser...
LLVM_ABI bool prefersVectorizedAddressing() const
Return true if target doesn't mind addresses in vectors.
LLVM_ABI TypeSize getRegisterBitWidth(RegisterKind K) const
LLVM_ABI bool preferFixedOverScalableIfEqualCost(bool IsEpilogue) const
LLVM_ABI InstructionCost getMemoryOpCost(unsigned Opcode, Type *Src, Align Alignment, unsigned AddressSpace, TTI::TargetCostKind CostKind=TTI::TCK_RecipThroughput, OperandValueInfo OpdInfo={OK_AnyValue, OP_None}, const Instruction *I=nullptr) const
LLVM_ABI InstructionCost getInterleavedMemoryOpCost(unsigned Opcode, Type *VecTy, unsigned Factor, ArrayRef< unsigned > Indices, Align Alignment, unsigned AddressSpace, TTI::TargetCostKind CostKind=TTI::TCK_RecipThroughput, bool UseMaskForCond=false, bool UseMaskForGaps=false) const
LLVM_ABI InstructionCost getShuffleCost(ShuffleKind Kind, VectorType *DstTy, VectorType *SrcTy, ArrayRef< int > Mask={}, TTI::TargetCostKind CostKind=TTI::TCK_RecipThroughput, int Index=0, VectorType *SubTp=nullptr, ArrayRef< const Value * > Args={}, const Instruction *CxtI=nullptr) const
static LLVM_ABI PartialReductionExtendKind getPartialReductionExtendKind(Instruction *I)
Get the kind of extension that an instruction represents.
static LLVM_ABI OperandValueInfo getOperandInfo(const Value *V)
Collect properties of V used in cost analysis, e.g. OP_PowerOf2.
LLVM_ABI bool isElementTypeLegalForScalableVector(Type *Ty) const
LLVM_ABI ElementCount getMinimumVF(unsigned ElemWidth, bool IsScalable) const
TargetCostKind
The kind of cost model.
@ TCK_RecipThroughput
Reciprocal throughput.
@ TCK_CodeSize
Instruction code size.
@ TCK_SizeAndLatency
The weighted sum of size and latency.
@ TCK_Latency
The latency of instruction.
LLVM_ABI InstructionCost getMaskedMemoryOpCost(unsigned Opcode, Type *Src, Align Alignment, unsigned AddressSpace, TTI::TargetCostKind CostKind=TTI::TCK_RecipThroughput) const
LLVM_ABI InstructionCost getAddressComputationCost(Type *PtrTy, ScalarEvolution *SE, const SCEV *Ptr, TTI::TargetCostKind CostKind) const
LLVM_ABI InstructionCost getPartialReductionCost(unsigned Opcode, Type *InputTypeA, Type *InputTypeB, Type *AccumType, ElementCount VF, PartialReductionExtendKind OpAExtend, PartialReductionExtendKind OpBExtend, std::optional< unsigned > BinOp, TTI::TargetCostKind CostKind) const
LLVM_ABI InstructionCost getGatherScatterOpCost(unsigned Opcode, Type *DataTy, const Value *Ptr, bool VariableMask, Align Alignment, TTI::TargetCostKind CostKind=TTI::TCK_RecipThroughput, const Instruction *I=nullptr) const
LLVM_ABI bool supportsScalableVectors() const
@ TCC_Free
Expected to fold away in lowering.
LLVM_ABI InstructionCost getInstructionCost(const User *U, ArrayRef< const Value * > Operands, TargetCostKind CostKind) const
Estimate the cost of a given IR user when lowered.
LLVM_ABI InstructionCost getIndexedVectorInstrCostFromEnd(unsigned Opcode, Type *Val, TTI::TargetCostKind CostKind, unsigned Index) const
LLVM_ABI InstructionCost getOperandsScalarizationOverhead(ArrayRef< Type * > Tys, TTI::TargetCostKind CostKind) const
Estimate the overhead of scalarizing operands with the given types.
@ SK_Splice
Concatenates elements from the first input vector with elements of the second input vector.
@ SK_Broadcast
Broadcast element 0 to all other elements.
@ SK_Reverse
Reverse the order of the vector.
LLVM_ABI InstructionCost getCFInstrCost(unsigned Opcode, TTI::TargetCostKind CostKind=TTI::TCK_SizeAndLatency, const Instruction *I=nullptr) const
CastContextHint
Represents a hint about the context in which a cast is used.
@ Reversed
The cast is used with a reversed load/store.
@ Masked
The cast is used with a masked load/store.
@ None
The cast is not used with a load/store of any kind.
@ Normal
The cast is used with a normal load/store.
@ Interleave
The cast is used with an interleaved load/store.
@ GatherScatter
The cast is used with a gather/scatter.
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition Twine.h:82
This class implements a switch-like dispatch statement for a value of 'T' using dyn_cast functionalit...
Definition TypeSwitch.h:88
TypeSwitch< T, ResultT > & Case(CallableT &&caseFn)
Add a case on the given type.
Definition TypeSwitch.h:97
The instances of the Type class are immutable: once they are created, they are never changed.
Definition Type.h:45
LLVM_ABI unsigned getIntegerBitWidth() const
bool isVectorTy() const
True if this is an instance of VectorType.
Definition Type.h:273
static LLVM_ABI Type * getVoidTy(LLVMContext &C)
Definition Type.cpp:281
Type * getScalarType() const
If this is a vector type, return the element type, otherwise return 'this'.
Definition Type.h:352
LLVM_ABI TypeSize getPrimitiveSizeInBits() const LLVM_READONLY
Return the basic size of this type if it is a primitive type.
Definition Type.cpp:198
LLVMContext & getContext() const
Return the LLVMContext in which this type was uniqued.
Definition Type.h:128
LLVM_ABI unsigned getScalarSizeInBits() const LLVM_READONLY
If this is a vector type, return the getPrimitiveSizeInBits value for the element type.
Definition Type.cpp:231
static LLVM_ABI IntegerType * getInt1Ty(LLVMContext &C)
Definition Type.cpp:294
bool isFloatingPointTy() const
Return true if this is one of the floating-point types.
Definition Type.h:184
bool isIntegerTy() const
True if this is an instance of IntegerType.
Definition Type.h:240
bool isVoidTy() const
Return true if this is 'void'.
Definition Type.h:139
A Use represents the edge between a Value definition and its users.
Definition Use.h:35
op_range operands()
Definition User.h:292
LLVM_ABI bool replaceUsesOfWith(Value *From, Value *To)
Replace uses of one Value with another.
Definition User.cpp:21
Value * getOperand(unsigned i) const
Definition User.h:232
static SmallVector< VFInfo, 8 > getMappings(const CallInst &CI)
Retrieve all the VFInfo instances associated to the CallInst CI.
Definition VectorUtils.h:74
VPBasicBlock serves as the leaf of the Hierarchical Control-Flow Graph.
Definition VPlan.h:3815
void appendRecipe(VPRecipeBase *Recipe)
Augment the existing recipes of a VPBasicBlock with an additional Recipe as the last recipe.
Definition VPlan.h:3890
RecipeListTy::iterator iterator
Instruction iterators...
Definition VPlan.h:3842
iterator end()
Definition VPlan.h:3852
iterator begin()
Recipe iterator methods.
Definition VPlan.h:3850
iterator_range< iterator > phis()
Returns an iterator range over the PHI-like recipes in the block.
Definition VPlan.h:3903
iterator getFirstNonPhi()
Return the position of the first non-phi node recipe in the block.
Definition VPlan.cpp:220
VPRegionBlock * getEnclosingLoopRegion()
Definition VPlan.cpp:593
VPRecipeBase * getTerminator()
If the block has multiple successors, return the branch recipe terminating the block.
Definition VPlan.cpp:638
void insert(VPRecipeBase *Recipe, iterator InsertPt)
Definition VPlan.h:3881
bool empty() const
Definition VPlan.h:3861
VPBlockBase is the building block of the Hierarchical Control-Flow Graph.
Definition VPlan.h:80
VPRegionBlock * getParent()
Definition VPlan.h:172
const VPBasicBlock * getExitingBasicBlock() const
Definition VPlan.cpp:190
void setName(const Twine &newName)
Definition VPlan.h:165
size_t getNumSuccessors() const
Definition VPlan.h:218
void swapSuccessors()
Swap successors of the block. The block must have exactly 2 successors.
Definition VPlan.h:321
size_t getNumPredecessors() const
Definition VPlan.h:219
VPlan * getPlan()
Definition VPlan.cpp:165
const VPBasicBlock * getEntryBasicBlock() const
Definition VPlan.cpp:170
VPBlockBase * getSingleSuccessor() const
Definition VPlan.h:208
const VPBlocksTy & getSuccessors() const
Definition VPlan.h:197
static auto blocksOnly(const T &Range)
Return an iterator range over Range which only includes BlockTy blocks.
Definition VPlanUtils.h:187
static void insertOnEdge(VPBlockBase *From, VPBlockBase *To, VPBlockBase *BlockPtr)
Inserts BlockPtr on the edge between From and To.
Definition VPlanUtils.h:208
static void connectBlocks(VPBlockBase *From, VPBlockBase *To, unsigned PredIdx=-1u, unsigned SuccIdx=-1u)
Connect VPBlockBases From and To bi-directionally.
Definition VPlanUtils.h:146
static void reassociateBlocks(VPBlockBase *Old, VPBlockBase *New)
Reassociate all the blocks connected to Old so that they now point to New.
Definition VPlanUtils.h:173
VPlan-based builder utility analogous to IRBuilder.
VPPhi * createScalarPhi(ArrayRef< VPValue * > IncomingValues, DebugLoc DL, const Twine &Name="")
VPInstruction * createNaryOp(unsigned Opcode, ArrayRef< VPValue * > Operands, Instruction *Inst=nullptr, const Twine &Name="")
Create an N-ary operation with Opcode, Operands and set Inst as its underlying Instruction.
Canonical scalar induction phi of the vector loop.
Definition VPlan.h:3471
unsigned getNumDefinedValues() const
Returns the number of values defined by the VPDef.
Definition VPlanValue.h:424
VPValue * getVPSingleValue()
Returns the only VPValue defined by the VPDef.
Definition VPlanValue.h:397
void execute(VPTransformState &State) override
Generate the transformed value of the induction at offset StartValue (1.
VPValue * getStepValue() const
Definition VPlan.h:3692
VPValue * getStartValue() const
Definition VPlan.h:3691
A pure virtual base class for all recipes modeling header phis, including phis for first order recurr...
Definition VPlan.h:1981
virtual VPValue * getBackedgeValue()
Returns the incoming value from the loop backedge.
Definition VPlan.h:2029
VPValue * getStartValue()
Returns the start value of the phi, if one is set.
Definition VPlan.h:2018
A special type of VPBasicBlock that wraps an existing IR basic block.
Definition VPlan.h:3968
Helper to manage IR metadata for recipes.
Definition VPlan.h:938
This is a concrete Recipe that models a single VPlan-level instruction.
Definition VPlan.h:979
@ ComputeAnyOfResult
Compute the final result of a AnyOf reduction with select(cmp(),x,y), where one of (x,...
Definition VPlan.h:1017
@ ResumeForEpilogue
Explicit user for the resume phi of the canonical induction in the main VPlan, used by the epilogue v...
Definition VPlan.h:1066
@ FirstOrderRecurrenceSplice
Definition VPlan.h:985
@ ReductionStartVector
Start vector for reductions with 3 operands: the original start value, the identity value for the red...
Definition VPlan.h:1057
unsigned getOpcode() const
Definition VPlan.h:1123
VPInterleaveRecipe is a recipe for transforming an interleave group of load or stores into one wide l...
Definition VPlan.h:2579
In what follows, the term "input IR" refers to code that is fed into the vectorizer whereas the term ...
A recipe for forming partial reductions.
Definition VPlan.h:2766
detail::zippy< llvm::detail::zip_first, VPUser::const_operand_range, const_incoming_blocks_range > incoming_values_and_blocks() const
Returns an iterator range over pairs of incoming values and corresponding incoming blocks.
Definition VPlan.h:1294
VPRecipeBase is a base class modeling a sequence of one or more output IR instructions.
Definition VPlan.h:386
VPBasicBlock * getParent()
Definition VPlan.h:407
void moveBefore(VPBasicBlock &BB, iplist< VPRecipeBase >::iterator I)
Unlink this recipe and insert into BB before I.
void insertBefore(VPRecipeBase *InsertPos)
Insert an unlinked recipe into a basic block immediately before the specified recipe.
iplist< VPRecipeBase >::iterator eraseFromParent()
This method unlinks 'this' from the containing basic block and deletes it.
VPRecipeBase * tryToCreateWidenRecipe(VPSingleDefRecipe *R, VFRange &Range)
Create and return a widened recipe for R if one can be created within the given VF Range.
VPValue * getBlockInMask(VPBasicBlock *VPBB) const
Returns the entry mask for block VPBB or null if the mask is all-true.
std::optional< unsigned > getScalingForReduction(const Instruction *ExitInst)
void collectScaledReductions(VFRange &Range)
Find all possible partial reductions in the loop and track all of those that are valid so recipes can...
VPReplicateRecipe * handleReplication(Instruction *I, ArrayRef< VPValue * > Operands, VFRange &Range)
Build a VPReplicationRecipe for I using Operands.
VPRecipeBase * tryToCreatePartialReduction(Instruction *Reduction, ArrayRef< VPValue * > Operands, unsigned ScaleFactor)
Create and return a partial reduction recipe for a reduction instruction along with binary operation ...
A recipe for handling reduction phis.
Definition VPlan.h:2334
bool isInLoop() const
Returns true, if the phi is part of an in-loop reduction.
Definition VPlan.h:2394
RecurKind getRecurrenceKind() const
Returns the recurrence kind of the reduction.
Definition VPlan.h:2388
VPRegionBlock represents a collection of VPBasicBlocks and VPRegionBlocks which form a Single-Entry-S...
Definition VPlan.h:4003
const VPBlockBase * getEntry() const
Definition VPlan.h:4039
VPCanonicalIVPHIRecipe * getCanonicalIV()
Returns the canonical induction recipe of the region.
Definition VPlan.h:4101
VPReplicateRecipe replicates a given instruction producing multiple scalar copies of the original sca...
Definition VPlan.h:2872
VPSingleDef is a base class for recipes for modeling a sequence of one or more output IR that define ...
Definition VPlan.h:517
Instruction * getUnderlyingInstr()
Returns the underlying instruction.
Definition VPlan.h:582
An analysis for type-inference for VPValues.
Type * inferScalarType(const VPValue *V)
Infer the type of V. Returns the scalar type of V.
This class augments VPValue with operands which provide the inverse def-use edges from VPValue's user...
Definition VPlanValue.h:199
void setOperand(unsigned I, VPValue *New)
Definition VPlanValue.h:243
VPValue * getOperand(unsigned N) const
Definition VPlanValue.h:238
void addOperand(VPValue *Operand)
Definition VPlanValue.h:232
This is the base class of the VPlan Def/Use graph, used for modeling the data flow into,...
Definition VPlanValue.h:48
VPRecipeBase * getDefiningRecipe()
Returns the recipe defining this VPValue or nullptr if it is not defined by a recipe,...
Definition VPlan.cpp:135
Value * getLiveInIRValue() const
Returns the underlying IR value, if this VPValue is defined outside the scope of VPlan.
Definition VPlanValue.h:176
Value * getUnderlyingValue() const
Return the underlying Value attached to this VPValue.
Definition VPlanValue.h:85
void replaceAllUsesWith(VPValue *New)
Definition VPlan.cpp:1393
user_iterator user_begin()
Definition VPlanValue.h:130
unsigned getNumUsers() const
Definition VPlanValue.h:113
void replaceUsesWithIf(VPValue *New, llvm::function_ref< bool(VPUser &U, unsigned Idx)> ShouldReplace)
Go through the uses list for this VPValue and make each use point to New if the callback ShouldReplac...
Definition VPlan.cpp:1397
user_range users()
Definition VPlanValue.h:134
A recipe to compute a pointer to the last element of each part of a widened memory access for widened...
Definition VPlan.h:1845
VPWidenCastRecipe is a recipe to create vector cast instructions.
Definition VPlan.h:1486
A recipe for handling GEP instructions.
Definition VPlan.h:1773
VPValue * getStepValue()
Returns the step value of the induction.
Definition VPlan.h:2074
A recipe for handling phi nodes of integer and floating-point inductions, producing their vector valu...
Definition VPlan.h:2121
A common base class for widening memory operations.
Definition VPlan.h:3178
A recipe for widened phis.
Definition VPlan.h:2257
VPWidenRecipe is a recipe for producing a widened instruction using the opcode and operands of the re...
Definition VPlan.h:1443
VPlan models a candidate for vectorization, encoding various decisions take to produce efficient outp...
Definition VPlan.h:4133
bool hasVF(ElementCount VF) const
Definition VPlan.h:4339
VPBasicBlock * getEntry()
Definition VPlan.h:4227
VPValue & getVectorTripCount()
The vector trip count.
Definition VPlan.h:4318
VPValue & getVFxUF()
Returns VF * UF of the vector loop region.
Definition VPlan.h:4325
VPValue & getVF()
Returns the VF of the vector loop region.
Definition VPlan.h:4321
VPValue * getTripCount() const
The trip count of the original loop.
Definition VPlan.h:4289
iterator_range< SmallSetVector< ElementCount, 2 >::iterator > vectorFactors() const
Returns an iterator range over all VFs of the plan.
Definition VPlan.h:4346
bool hasUF(unsigned UF) const
Definition VPlan.h:4357
ArrayRef< VPIRBasicBlock * > getExitBlocks() const
Return an ArrayRef containing VPIRBasicBlocks wrapping the exit blocks of the original scalar loop.
Definition VPlan.h:4279
VPValue * getConstantInt(Type *Ty, uint64_t Val, bool IsSigned=false)
Return a VPValue wrapping a ConstantInt with the given type and value.
Definition VPlan.h:4402
LLVM_ABI_FOR_TEST VPRegionBlock * getVectorLoopRegion()
Returns the VPRegionBlock of the vector loop.
Definition VPlan.cpp:1027
bool hasEarlyExit() const
Returns true if the VPlan is based on a loop with an early exit.
Definition VPlan.h:4495
InstructionCost cost(ElementCount VF, VPCostContext &Ctx)
Return the cost of this plan.
Definition VPlan.cpp:1009
void resetTripCount(VPValue *NewTripCount)
Resets the trip count for the VPlan.
Definition VPlan.h:4303
VPBasicBlock * getMiddleBlock()
Returns the 'middle' block of the plan, that is the block that selects whether to execute the scalar ...
Definition VPlan.h:4252
VPValue * getOrAddLiveIn(Value *V)
Gets the live-in VPValue for V or adds a new live-in (if none exists yet) for V.
Definition VPlan.h:4381
VPBasicBlock * getScalarPreheader() const
Return the VPBasicBlock for the preheader of the scalar loop.
Definition VPlan.h:4270
void execute(VPTransformState *State)
Generate the IR code for this VPlan.
Definition VPlan.cpp:921
VPIRBasicBlock * getScalarHeader() const
Return the VPIRBasicBlock wrapping the header of the scalar loop.
Definition VPlan.h:4275
VPBasicBlock * getVectorPreheader()
Returns the preheader of the vector loop region, if one exists, or null otherwise.
Definition VPlan.h:4232
VPlan * duplicate()
Clone the current VPlan, update all VPValues of the new VPlan and cloned recipes to refer to the clon...
Definition VPlan.cpp:1169
LLVM Value Representation.
Definition Value.h:75
Type * getType() const
All values are typed, get the type of this value.
Definition Value.h:256
LLVM_ABI bool hasOneUser() const
Return true if there is exactly one user of this value.
Definition Value.cpp:166
LLVM_ABI void setName(const Twine &Name)
Change the name of the value.
Definition Value.cpp:390
bool hasOneUse() const
Return true if there is exactly one use of this value.
Definition Value.h:439
LLVM_ABI void replaceAllUsesWith(Value *V)
Change all uses of this to point to a new Value.
Definition Value.cpp:546
iterator_range< user_iterator > users()
Definition Value.h:426
LLVM_ABI LLVMContext & getContext() const
All values hold a context through their type.
Definition Value.cpp:1099
LLVM_ABI StringRef getName() const
Return a constant reference to the value's name.
Definition Value.cpp:322
static LLVM_ABI VectorType * get(Type *ElementType, ElementCount EC)
This static method is the primary way to construct an VectorType.
std::pair< iterator, bool > insert(const ValueT &V)
Definition DenseSet.h:202
bool contains(const_arg_type_t< ValueT > V) const
Check if the set contains the given element.
Definition DenseSet.h:175
constexpr bool hasKnownScalarFactor(const FixedOrScalableQuantity &RHS) const
Returns true if there exists a value X where RHS.multiplyCoefficientBy(X) will result in a value whos...
Definition TypeSize.h:270
constexpr ScalarTy getFixedValue() const
Definition TypeSize.h:201
static constexpr bool isKnownLE(const FixedOrScalableQuantity &LHS, const FixedOrScalableQuantity &RHS)
Definition TypeSize.h:231
constexpr bool isNonZero() const
Definition TypeSize.h:156
constexpr ScalarTy getKnownScalarFactor(const FixedOrScalableQuantity &RHS) const
Returns a value X where RHS.multiplyCoefficientBy(X) will result in a value whose quantity matches ou...
Definition TypeSize.h:278
static constexpr bool isKnownLT(const FixedOrScalableQuantity &LHS, const FixedOrScalableQuantity &RHS)
Definition TypeSize.h:217
constexpr bool isScalable() const
Returns whether the quantity is scaled by a runtime quantity (vscale).
Definition TypeSize.h:169
constexpr LeafTy multiplyCoefficientBy(ScalarTy RHS) const
Definition TypeSize.h:257
constexpr bool isFixed() const
Returns true if the quantity is not scaled by vscale.
Definition TypeSize.h:172
constexpr ScalarTy getKnownMinValue() const
Returns the minimum value this quantity can represent.
Definition TypeSize.h:166
constexpr bool isZero() const
Definition TypeSize.h:154
static constexpr bool isKnownGT(const FixedOrScalableQuantity &LHS, const FixedOrScalableQuantity &RHS)
Definition TypeSize.h:224
constexpr LeafTy divideCoefficientBy(ScalarTy RHS) const
We do not provide the '/' operator here because division for polynomial types does not work in the sa...
Definition TypeSize.h:253
static constexpr bool isKnownGE(const FixedOrScalableQuantity &LHS, const FixedOrScalableQuantity &RHS)
Definition TypeSize.h:238
An efficient, type-erasing, non-owning reference to a callable.
const ParentTy * getParent() const
Definition ilist_node.h:34
self_iterator getIterator()
Definition ilist_node.h:123
IteratorT end() const
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition raw_ostream.h:53
A raw_ostream that writes to an std::string.
Changed
This provides a very simple, boring adaptor for a begin and end iterator into a range type.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
constexpr char Align[]
Key for Kernel::Arg::Metadata::mAlign.
constexpr std::underlying_type_t< E > Mask()
Get a bitmask with 1s in all places up to the high-order bit of E's largest value.
@ Entry
Definition COFF.h:862
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
Definition CallingConv.h:24
@ Tail
Attemps to make calls as fast as possible while guaranteeing that tail call optimization can always b...
Definition CallingConv.h:76
@ C
The default llvm calling convention, compatible with C.
Definition CallingConv.h:34
@ BasicBlock
Various leaf nodes.
Definition ISDOpcodes.h:81
std::variant< std::monostate, Loc::Single, Loc::Multi, Loc::MMI, Loc::EntryValue > Variant
Alias for the std::variant specialization base class of DbgVariable.
Definition DwarfDebug.h:189
SpecificConstantMatch m_ZeroInt()
Convenience matchers for specific integer values.
BinaryOp_match< SpecificConstantMatch, SrcTy, TargetOpcode::G_SUB > m_Neg(const SrcTy &&Src)
Matches a register negated by a G_SUB.
OneUse_match< SubPat > m_OneUse(const SubPat &SP)
BinaryOp_match< LHS, RHS, Instruction::Add > m_Add(const LHS &L, const RHS &R)
class_match< BinaryOperator > m_BinOp()
Match an arbitrary binary operation and ignore it.
OneOps_match< OpTy, Instruction::Freeze > m_Freeze(const OpTy &Op)
Matches FreezeInst.
ap_match< APInt > m_APInt(const APInt *&Res)
Match a ConstantInt or splatted ConstantVector, binding the specified pointer to the contained APInt.
specific_intval< false > m_SpecificInt(const APInt &V)
Match a specific integer value or vector with all elements equal to the value.
bool match(Val *V, const Pattern &P)
bind_ty< Instruction > m_Instruction(Instruction *&I)
Match an instruction, capturing it if we match.
specificval_ty m_Specific(const Value *V)
Match if we have a specific specified value.
cst_pred_ty< is_one > m_One()
Match an integer 1 or a vector with all elements equal to 1.
ThreeOps_match< Cond, LHS, RHS, Instruction::Select > m_Select(const Cond &C, const LHS &L, const RHS &R)
Matches SelectInst.
BinaryOp_match< LHS, RHS, Instruction::Mul > m_Mul(const LHS &L, const RHS &R)
auto m_LogicalOr()
Matches L || R where L and R are arbitrary values.
SpecificCmpClass_match< LHS, RHS, ICmpInst > m_SpecificICmp(CmpPredicate MatchPred, const LHS &L, const RHS &R)
class_match< CmpInst > m_Cmp()
Matches any compare instruction and ignore it.
class_match< Value > m_Value()
Match an arbitrary value and ignore it.
match_combine_or< CastInst_match< OpTy, ZExtInst >, CastInst_match< OpTy, SExtInst > > m_ZExtOrSExt(const OpTy &Op)
auto m_LogicalAnd()
Matches L && R where L and R are arbitrary values.
MatchFunctor< Val, Pattern > match_fn(const Pattern &P)
A match functor that can be used as a UnaryPredicate in functional algorithms like all_of.
class_match< const SCEVVScale > m_SCEVVScale()
bind_cst_ty m_scev_APInt(const APInt *&C)
Match an SCEV constant and bind it to an APInt.
specificloop_ty m_SpecificLoop(const Loop *L)
cst_pred_ty< is_specific_signed_cst > m_scev_SpecificSInt(int64_t V)
Match an SCEV constant with a plain signed integer (sign-extended value will be matched)
SCEVAffineAddRec_match< Op0_t, Op1_t, class_match< const Loop > > m_scev_AffineAddRec(const Op0_t &Op0, const Op1_t &Op1)
bind_ty< const SCEVMulExpr > m_scev_Mul(const SCEVMulExpr *&V)
bool match(const SCEV *S, const Pattern &P)
SCEVBinaryExpr_match< SCEVMulExpr, Op0_t, Op1_t, SCEV::FlagAnyWrap, true > m_scev_c_Mul(const Op0_t &Op0, const Op1_t &Op1)
class_match< const SCEV > m_SCEV()
match_combine_or< AllRecipe_match< Instruction::ZExt, Op0_t >, AllRecipe_match< Instruction::SExt, Op0_t > > m_ZExtOrSExt(const Op0_t &Op0)
VPInstruction_match< VPInstruction::ExtractLastElement, Op0_t > m_ExtractLastElement(const Op0_t &Op0)
class_match< VPValue > m_VPValue()
Match an arbitrary VPValue and ignore it.
ValuesClass values(OptsTy... Options)
Helper to build a ValuesClass by forwarding a variable number of arguments as an initializer list to ...
initializer< Ty > init(const Ty &Val)
Add a small namespace to avoid name clashes with the classes used in the streaming interface.
DiagnosticInfoOptimizationBase::Argument NV
NodeAddr< InstrNode * > Instr
Definition RDFGraph.h:389
NodeAddr< PhiNode * > Phi
Definition RDFGraph.h:390
friend class Instruction
Iterator for Instructions in a `BasicBlock.
Definition BasicBlock.h:73
bool isSingleScalar(const VPValue *VPV)
Returns true if VPV is a single scalar, either because it produces the same value for all lanes or on...
VPValue * getOrCreateVPValueForSCEVExpr(VPlan &Plan, const SCEV *Expr)
Get or create a VPValue that corresponds to the expansion of Expr.
VPBasicBlock * getFirstLoopHeader(VPlan &Plan, VPDominatorTree &VPDT)
Returns the header block of the first, top-level loop, or null if none exist.
unsigned getVFScaleFactor(VPRecipeBase *R)
Get the VF scaling factor applied to the recipe's output, if the recipe has one.
const SCEV * getSCEVExprForVPValue(const VPValue *V, ScalarEvolution &SE, const Loop *L=nullptr)
Return the SCEV expression for V.
This is an optimization pass for GlobalISel generic memory operations.
LLVM_ABI bool simplifyLoop(Loop *L, DominatorTree *DT, LoopInfo *LI, ScalarEvolution *SE, AssumptionCache *AC, MemorySSAUpdater *MSSAU, bool PreserveLCSSA)
Simplify each loop in a loop nest recursively.
LLVM_ABI void ReplaceInstWithInst(BasicBlock *BB, BasicBlock::iterator &BI, Instruction *I)
Replace the instruction specified by BI with the instruction specified by I.
auto drop_begin(T &&RangeOrContainer, size_t N=1)
Return a range covering RangeOrContainer with the first N elements excluded.
Definition STLExtras.h:316
@ Offset
Definition DWP.cpp:477
detail::zippy< detail::zip_shortest, T, U, Args... > zip(T &&t, U &&u, Args &&...args)
zip iterator for two or more iteratable types.
Definition STLExtras.h:829
FunctionAddr VTableAddr Value
Definition InstrProf.h:137
LLVM_ABI Value * addRuntimeChecks(Instruction *Loc, Loop *TheLoop, const SmallVectorImpl< RuntimePointerCheck > &PointerChecks, SCEVExpander &Expander, bool HoistRuntimeChecks=false)
Add code that checks at runtime if the accessed arrays in PointerChecks overlap.
auto cast_if_present(const Y &Val)
cast_if_present<X> - Functionally identical to cast, except that a null value is accepted.
Definition Casting.h:683
LLVM_ABI bool RemoveRedundantDbgInstrs(BasicBlock *BB)
Try to remove redundant dbg.value instructions from given basic block.
cl::opt< bool > VerifyEachVPlan
LLVM_ABI std::optional< unsigned > getLoopEstimatedTripCount(Loop *L, unsigned *EstimatedLoopInvocationWeight=nullptr)
Return either:
static void reportVectorization(OptimizationRemarkEmitter *ORE, Loop *TheLoop, VectorizationFactor VF, unsigned IC)
Report successful vectorization of the loop.
bool all_of(R &&range, UnaryPredicate P)
Provide wrappers to std::all_of which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1725
unsigned getLoadStoreAddressSpace(const Value *I)
A helper function that returns the address space of the pointer operand of load or store instruction.
LLVM_ABI Intrinsic::ID getMinMaxReductionIntrinsicOp(Intrinsic::ID RdxID)
Returns the min/max intrinsic used when expanding a min/max reduction.
auto size(R &&Range, std::enable_if_t< std::is_base_of< std::random_access_iterator_tag, typename std::iterator_traits< decltype(Range.begin())>::iterator_category >::value, void > *=nullptr)
Get the size of a range.
Definition STLExtras.h:1655
LLVM_ABI_FOR_TEST bool verifyVPlanIsValid(const VPlan &Plan, bool VerifyLate=false)
Verify invariants for general VPlans.
LLVM_ABI Intrinsic::ID getVectorIntrinsicIDForCall(const CallInst *CI, const TargetLibraryInfo *TLI)
Returns intrinsic ID for call.
InstructionCost Cost
auto enumerate(FirstRange &&First, RestRanges &&...Rest)
Given two or more input ranges, returns a new range whose values are tuples (A, B,...
Definition STLExtras.h:2472
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:643
LLVM_ABI bool verifyFunction(const Function &F, raw_ostream *OS=nullptr)
Check a function for errors, useful for use when debugging a pass.
const Value * getLoadStorePointerOperand(const Value *V)
A helper function that returns the pointer operand of a load or store instruction.
OuterAnalysisManagerProxy< ModuleAnalysisManager, Function > ModuleAnalysisManagerFunctionProxy
Provide the ModuleAnalysisManager to Function proxy.
Value * getRuntimeVF(IRBuilderBase &B, Type *Ty, ElementCount VF)
Return the runtime value for VF.
LLVM_ABI bool formLCSSARecursively(Loop &L, const DominatorTree &DT, const LoopInfo *LI, ScalarEvolution *SE)
Put a loop nest into LCSSA form.
Definition LCSSA.cpp:449
iterator_range< T > make_range(T x, T y)
Convenience function for iterating over sub-ranges.
void append_range(Container &C, Range &&R)
Wrapper function to append range R to container C.
Definition STLExtras.h:2136
LLVM_ABI bool shouldOptimizeForSize(const MachineFunction *MF, ProfileSummaryInfo *PSI, const MachineBlockFrequencyInfo *BFI, PGSOQueryType QueryType=PGSOQueryType::Other)
Returns true if machine function MF is suggested to be size-optimized based on the profile.
iterator_range< early_inc_iterator_impl< detail::IterOfRange< RangeT > > > make_early_inc_range(RangeT &&Range)
Make a range that does early increment to allow mutation of the underlying range without disrupting i...
Definition STLExtras.h:632
constexpr bool isPowerOf2_64(uint64_t Value)
Return true if the argument is a power of two > 0 (64 bit edition.)
Definition MathExtras.h:284
Align getLoadStoreAlignment(const Value *I)
A helper function that returns the alignment of load or store instruction.
iterator_range< df_iterator< VPBlockShallowTraversalWrapper< VPBlockBase * > > > vp_depth_first_shallow(VPBlockBase *G)
Returns an iterator range to traverse the graph starting at G in depth-first order.
Definition VPlanCFG.h:216
LLVM_ABI bool VerifySCEV
LLVM_ABI bool isSafeToSpeculativelyExecute(const Instruction *I, const Instruction *CtxI=nullptr, AssumptionCache *AC=nullptr, const DominatorTree *DT=nullptr, const TargetLibraryInfo *TLI=nullptr, bool UseVariableInfo=true, bool IgnoreUBImplyingAttrs=true)
Return true if the instruction does not have any effects besides calculating the result and does not ...
bool isa_and_nonnull(const Y &Val)
Definition Casting.h:676
iterator_range< df_iterator< VPBlockDeepTraversalWrapper< VPBlockBase * > > > vp_depth_first_deep(VPBlockBase *G)
Returns an iterator range to traverse the graph starting at G in depth-first order while traversing t...
Definition VPlanCFG.h:243
SmallVector< VPRegisterUsage, 8 > calculateRegisterUsageForPlan(VPlan &Plan, ArrayRef< ElementCount > VFs, const TargetTransformInfo &TTI, const SmallPtrSetImpl< const Value * > &ValuesToIgnore)
Estimate the register usage for Plan and vectorization factors in VFs by calculating the highest numb...
unsigned Log2_64(uint64_t Value)
Return the floor log base 2 of the specified value, -1 if the value is zero.
Definition MathExtras.h:337
LLVM_ABI void setBranchWeights(Instruction &I, ArrayRef< uint32_t > Weights, bool IsExpected, bool ElideAllZero=false)
Create a new branch_weights metadata node and add or overwrite a prof metadata reference to instructi...
auto dyn_cast_or_null(const Y &Val)
Definition Casting.h:753
bool any_of(R &&range, UnaryPredicate P)
Provide wrappers to std::any_of which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1732
void collectEphemeralRecipesForVPlan(VPlan &Plan, DenseSet< VPRecipeBase * > &EphRecipes)
auto reverse(ContainerTy &&C)
Definition STLExtras.h:406
bool containsIrreducibleCFG(RPOTraversalT &RPOTraversal, const LoopInfoT &LI)
Return true if the control flow in RPOTraversal is irreducible.
Definition CFG.h:149
constexpr bool isPowerOf2_32(uint32_t Value)
Return true if the argument is a power of two > 0.
Definition MathExtras.h:279
void sort(IteratorTy Start, IteratorTy End)
Definition STLExtras.h:1622
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition Debug.cpp:207
bool none_of(R &&Range, UnaryPredicate P)
Provide wrappers to std::none_of which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1739
LLVM_ABI cl::opt< bool > EnableLoopVectorization
LLVM_ABI bool wouldInstructionBeTriviallyDead(const Instruction *I, const TargetLibraryInfo *TLI=nullptr)
Return true if the result produced by the instruction would have no side effects if it was not used.
Definition Local.cpp:421
FunctionAddr VTableAddr Count
Definition InstrProf.h:139
SmallVector< ValueTypeFromRangeType< R >, Size > to_vector(R &&Range)
Given a range of type R, iterate the entire range and return a SmallVector with elements of the vecto...
Type * toVectorizedTy(Type *Ty, ElementCount EC)
A helper for converting to vectorized types.
LLVM_ABI void llvm_unreachable_internal(const char *msg=nullptr, const char *file=nullptr, unsigned line=0)
This function calls abort(), and prints the optional message to stderr.
bool canConstantBeExtended(const APInt *C, Type *NarrowType, TTI::PartialReductionExtendKind ExtKind)
Check if a constant CI can be safely treated as having been extended from a narrower type with the gi...
Definition VPlan.cpp:1734
T * find_singleton(R &&Range, Predicate P, bool AllowRepeats=false)
Return the single value in Range that satisfies P(<member of Range> *, AllowRepeats)->T * returning n...
Definition STLExtras.h:1787
class LLVM_GSL_OWNER SmallVector
Forward declaration of SmallVector so that calculateSmallVectorDefaultInlinedElements can reference s...
cl::opt< unsigned > ForceTargetInstructionCost
bool isa(const From &Val)
isa<X> - Return true if the parameter to the template is an instance of one of the template type argu...
Definition Casting.h:547
format_object< Ts... > format(const char *Fmt, const Ts &... Vals)
These are helper functions used to produce formatted output.
Definition Format.h:129
constexpr T divideCeil(U Numerator, V Denominator)
Returns the integer ceil(Numerator / Denominator).
Definition MathExtras.h:394
bool canVectorizeTy(Type *Ty)
Returns true if Ty is a valid vector element type, void, or an unpacked literal struct where all elem...
TargetTransformInfo TTI
static void reportVectorizationInfo(const StringRef Msg, const StringRef ORETag, OptimizationRemarkEmitter *ORE, Loop *TheLoop, Instruction *I=nullptr, DebugLoc DL={})
Reports an informative message: print Msg for debugging purposes as well as an optimization remark.
LLVM_ABI bool isAssignmentTrackingEnabled(const Module &M)
Return true if assignment tracking is enabled for module M.
RecurKind
These are the kinds of recurrences that we support.
@ Or
Bitwise or logical OR of integers.
@ FMulAdd
Sum of float products with llvm.fmuladd(a * b + sum).
@ Sub
Subtraction of integers.
@ Add
Sum of integers.
LLVM_ABI Value * getRecurrenceIdentity(RecurKind K, Type *Tp, FastMathFlags FMF)
Given information about an recurrence kind, return the identity for the @llvm.vector....
uint64_t alignTo(uint64_t Size, Align A)
Returns a multiple of A needed to store Size bytes.
Definition Alignment.h:144
LLVM_ABI void reportVectorizationFailure(const StringRef DebugMsg, const StringRef OREMsg, const StringRef ORETag, OptimizationRemarkEmitter *ORE, Loop *TheLoop, Instruction *I=nullptr)
Reports a vectorization failure: print DebugMsg for debugging purposes along with the corresponding o...
DWARFExpression::Operation Op
ScalarEpilogueLowering
@ CM_ScalarEpilogueNotAllowedLowTripLoop
@ CM_ScalarEpilogueNotNeededUsePredicate
@ CM_ScalarEpilogueNotAllowedOptSize
@ CM_ScalarEpilogueAllowed
@ CM_ScalarEpilogueNotAllowedUsePredicate
LLVM_ABI bool isGuaranteedNotToBeUndefOrPoison(const Value *V, AssumptionCache *AC=nullptr, const Instruction *CtxI=nullptr, const DominatorTree *DT=nullptr, unsigned Depth=0)
Return true if this function can prove that V does not have undef bits and is never poison.
ArrayRef(const T &OneElt) -> ArrayRef< T >
Value * createStepForVF(IRBuilderBase &B, Type *Ty, ElementCount VF, int64_t Step)
Return a value for Step multiplied by VF.
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:559
LLVM_ABI BasicBlock * SplitBlock(BasicBlock *Old, BasicBlock::iterator SplitPt, DominatorTree *DT, LoopInfo *LI=nullptr, MemorySSAUpdater *MSSAU=nullptr, const Twine &BBName="", bool Before=false)
Split the specified block at the specified instruction.
auto find_if(R &&Range, UnaryPredicate P)
Provide wrappers to std::find_if which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1758
auto predecessors(const MachineBasicBlock *BB)
iterator_range< pointer_iterator< WrappedIteratorT > > make_pointer_range(RangeT &&Range)
Definition iterator.h:363
cl::opt< bool > EnableVPlanNativePath
Definition VPlan.cpp:56
Type * getLoadStoreType(const Value *I)
A helper function that returns the type of a load or store instruction.
ArrayRef< Type * > getContainedTypes(Type *const &Ty)
Returns the types contained in Ty.
LLVM_ABI Value * addDiffRuntimeChecks(Instruction *Loc, ArrayRef< PointerDiffInfo > Checks, SCEVExpander &Expander, function_ref< Value *(IRBuilderBase &, unsigned)> GetVF, unsigned IC)
bool pred_empty(const BasicBlock *BB)
Definition CFG.h:119
@ DataAndControlFlowWithoutRuntimeCheck
Use predicate to control both data and control flow, but modify the trip count so that a runtime over...
@ None
Don't use tail folding.
@ DataWithEVL
Use predicated EVL instructions for tail-folding.
@ DataAndControlFlow
Use predicate to control both data and control flow.
@ DataWithoutLaneMask
Same as Data, but avoids using the get.active.lane.mask intrinsic to calculate the mask and instead i...
@ Data
Use predicate only to mask operations on data in the loop.
unsigned getPredBlockCostDivisor(TargetTransformInfo::TargetCostKind CostKind)
A helper function that returns how much we should divide the cost of a predicated block by.
AnalysisManager< Function > FunctionAnalysisManager
Convenience typedef for the Function analysis manager.
LLVM_ABI bool hasBranchWeightMD(const Instruction &I)
Checks if an instructions has Branch Weight Metadata.
hash_code hash_combine(const Ts &...args)
Combine values into a single hash_code.
Definition Hashing.h:592
T bit_floor(T Value)
Returns the largest integral power of two no greater than Value if Value is nonzero.
Definition bit.h:330
Type * toVectorTy(Type *Scalar, ElementCount EC)
A helper function for converting Scalar types to vector types.
std::unique_ptr< VPlan > VPlanPtr
Definition VPlan.h:76
constexpr detail::IsaCheckPredicate< Types... > IsaPred
Function object wrapper for the llvm::isa type check.
Definition Casting.h:830
LLVM_ABI MapVector< Instruction *, uint64_t > computeMinimumValueSizes(ArrayRef< BasicBlock * > Blocks, DemandedBits &DB, const TargetTransformInfo *TTI=nullptr)
Compute a map of integer instructions to their minimum legal type size.
hash_code hash_combine_range(InputIteratorT first, InputIteratorT last)
Compute a hash_code for a sequence of values.
Definition Hashing.h:466
LLVM_ABI cl::opt< bool > EnableLoopInterleaving
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
Definition BitVector.h:869
#define N
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition Alignment.h:39
A special type used by analysis passes to provide an address that identifies that particular analysis...
Definition Analysis.h:29
static LLVM_ABI void collectEphemeralValues(const Loop *L, AssumptionCache *AC, SmallPtrSetImpl< const Value * > &EphValues)
Collect a loop's ephemeral values (those used only by an assume or similar intrinsics in the loop).
An information struct used to provide DenseMap with the various necessary components for a given valu...
Encapsulate information regarding vectorization of a loop and its epilogue.
EpilogueLoopVectorizationInfo(ElementCount MVF, unsigned MUF, ElementCount EVF, unsigned EUF, VPlan &EpiloguePlan)
A class that represents two vectorization factors (initialized with 0 by default).
static FixedScalableVFPair getNone()
This holds details about a histogram operation – a load -> update -> store sequence where each lane i...
Incoming for lane maks phi as machine instruction, incoming register Reg and incoming block Block are...
TargetLibraryInfo * TLI
LLVM_ABI LoopVectorizeResult runImpl(Function &F)
LLVM_ABI bool processLoop(Loop *L)
ProfileSummaryInfo * PSI
LoopAccessInfoManager * LAIs
LLVM_ABI void printPipeline(raw_ostream &OS, function_ref< StringRef(StringRef)> MapClassName2PassName)
LLVM_ABI LoopVectorizePass(LoopVectorizeOptions Opts={})
BlockFrequencyInfo * BFI
ScalarEvolution * SE
AssumptionCache * AC
LLVM_ABI PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM)
OptimizationRemarkEmitter * ORE
TargetTransformInfo * TTI
Storage for information about made changes.
A chain of instructions that form a partial reduction.
Instruction * Reduction
The top-level binary operation that forms the reduction to a scalar after the loop body.
Instruction * ExtendA
The extension of each of the inner binary operation's operands.
A CRTP mix-in to automatically provide informational APIs needed for passes.
Definition PassManager.h:70
A marker analysis to determine if extra passes should be run after loop vectorization.
static LLVM_ABI AnalysisKey Key
Holds the VFShape for a specific scalar to vector function mapping.
std::optional< unsigned > getParamIndexForOptionalMask() const
Instruction Set Architecture.
Encapsulates information needed to describe a parameter.
A range of powers-of-2 vectorization factors with fixed start and adjustable end.
ElementCount End
Struct to hold various analysis needed for cost computations.
LoopVectorizationCostModel & CM
bool isLegacyUniformAfterVectorization(Instruction *I, ElementCount VF) const
Return true if I is considered uniform-after-vectorization in the legacy cost model for VF.
bool skipCostComputation(Instruction *UI, bool IsVector) const
Return true if the cost for UI shouldn't be computed, e.g.
InstructionCost getLegacyCost(Instruction *UI, ElementCount VF) const
Return the cost for UI with VF using the legacy cost model as fallback until computing the cost of al...
SmallPtrSet< Instruction *, 8 > SkipCostComputation
A recipe for handling first-order recurrence phis.
Definition VPlan.h:2299
A struct that represents some properties of the register usage of a loop.
VPTransformState holds information passed down when "executing" a VPlan, needed for generating the ou...
A recipe for widening select instructions.
Definition VPlan.h:1727
static void materializeBroadcasts(VPlan &Plan)
Add explicit broadcasts for live-ins and VPValues defined in Plan's entry block if they are used as v...
static void materializePacksAndUnpacks(VPlan &Plan)
Add explicit Build[Struct]Vector recipes to Pack multiple scalar values into vectors and Unpack recip...
static void materializeBackedgeTakenCount(VPlan &Plan, VPBasicBlock *VectorPH)
Materialize the backedge-taken count to be computed explicitly using VPInstructions.
static LLVM_ABI_FOR_TEST std::unique_ptr< VPlan > buildVPlan0(Loop *TheLoop, LoopInfo &LI, Type *InductionTy, DebugLoc IVDL, PredicatedScalarEvolution &PSE)
Create a base VPlan0, serving as the common starting point for all later candidates.
static void optimizeInductionExitUsers(VPlan &Plan, DenseMap< VPValue *, VPValue * > &EndValues, ScalarEvolution &SE)
If there's a single exit block, optimize its phi recipes that use exiting IV values by feeding them p...
static LLVM_ABI_FOR_TEST void handleEarlyExits(VPlan &Plan, bool HasUncountableExit)
Update Plan to account for all early exits.
static void addScalarResumePhis(VPlan &Plan, VPRecipeBuilder &Builder, DenseMap< VPValue *, VPValue * > &IVEndValues)
Create resume phis in the scalar preheader for first-order recurrences, reductions and inductions,...
static void canonicalizeEVLLoops(VPlan &Plan)
Transform EVL loops to use variable-length stepping after region dissolution.
static void dropPoisonGeneratingRecipes(VPlan &Plan, const std::function< bool(BasicBlock *)> &BlockNeedsPredication)
Drop poison flags from recipes that may generate a poison value that is used after vectorization,...
static void createInterleaveGroups(VPlan &Plan, const SmallPtrSetImpl< const InterleaveGroup< Instruction > * > &InterleaveGroups, VPRecipeBuilder &RecipeBuilder, const bool &ScalarEpilogueAllowed)
static bool runPass(bool(*Transform)(VPlan &, ArgsTy...), VPlan &Plan, typename std::remove_reference< ArgsTy >::type &...Args)
Helper to run a VPlan transform Transform on VPlan, forwarding extra arguments to the transform.
static void addBranchWeightToMiddleTerminator(VPlan &Plan, ElementCount VF, std::optional< unsigned > VScaleForTuning)
Add branch weight metadata, if the Plan's middle block is terminated by a BranchOnCond recipe.
static void unrollByUF(VPlan &Plan, unsigned UF)
Explicitly unroll Plan by UF.
static DenseMap< const SCEV *, Value * > expandSCEVs(VPlan &Plan, ScalarEvolution &SE)
Expand VPExpandSCEVRecipes in Plan's entry block.
static void convertToConcreteRecipes(VPlan &Plan)
Lower abstract recipes to concrete ones, that can be codegen'd.
static void addMinimumIterationCheck(VPlan &Plan, ElementCount VF, unsigned UF, ElementCount MinProfitableTripCount, bool RequiresScalarEpilogue, bool TailFolded, bool CheckNeededWithTailFolding, Loop *OrigLoop, const uint32_t *MinItersBypassWeights, DebugLoc DL, ScalarEvolution &SE)
static void convertToAbstractRecipes(VPlan &Plan, VPCostContext &Ctx, VFRange &Range)
This function converts initial recipes to the abstract recipes and clamps Range based on cost model f...
static void materializeConstantVectorTripCount(VPlan &Plan, ElementCount BestVF, unsigned BestUF, PredicatedScalarEvolution &PSE)
static LLVM_ABI_FOR_TEST bool tryToConvertVPInstructionsToVPRecipes(VPlan &Plan, function_ref< const InductionDescriptor *(PHINode *)> GetIntOrFpInductionDescriptor, const TargetLibraryInfo &TLI)
Replaces the VPInstructions in Plan with corresponding widen recipes.
static void addExitUsersForFirstOrderRecurrences(VPlan &Plan, VFRange &Range)
Handle users in the exit block for first order reductions in the original exit block.
static DenseMap< VPBasicBlock *, VPValue * > introduceMasksAndLinearize(VPlan &Plan, bool FoldTail)
Predicate and linearize the control-flow in the only loop region of Plan.
static void addExplicitVectorLength(VPlan &Plan, const std::optional< unsigned > &MaxEVLSafeElements)
Add a VPEVLBasedIVPHIRecipe and related recipes to Plan and replaces all uses except the canonical IV...
static void replaceSymbolicStrides(VPlan &Plan, PredicatedScalarEvolution &PSE, const DenseMap< Value *, const SCEV * > &StridesMap)
Replace symbolic strides from StridesMap in Plan with constants when possible.
static bool handleMaxMinNumReductions(VPlan &Plan)
Check if Plan contains any FMaxNum or FMinNum reductions.
static void removeBranchOnConst(VPlan &Plan)
Remove BranchOnCond recipes with true or false conditions together with removing dead edges to their ...
static LLVM_ABI_FOR_TEST void createLoopRegions(VPlan &Plan)
Replace loops in Plan's flat CFG with VPRegionBlocks, turning Plan's flat CFG into a hierarchical CFG...
static void removeDeadRecipes(VPlan &Plan)
Remove dead recipes from Plan.
static void attachCheckBlock(VPlan &Plan, Value *Cond, BasicBlock *CheckBlock, bool AddBranchWeights)
Wrap runtime check block CheckBlock in a VPIRBB and Cond in a VPValue and connect the block to Plan,...
static void materializeVectorTripCount(VPlan &Plan, VPBasicBlock *VectorPHVPBB, bool TailByMasking, bool RequiresScalarEpilogue)
Materialize vector trip count computations to a set of VPInstructions.
static void simplifyRecipes(VPlan &Plan)
Perform instcombine-like simplifications on recipes in Plan.
static void replicateByVF(VPlan &Plan, ElementCount VF)
Replace each replicating VPReplicateRecipe and VPInstruction outside of any replicate region in Plan ...
static void clearReductionWrapFlags(VPlan &Plan)
Clear NSW/NUW flags from reduction instructions if necessary.
static void cse(VPlan &Plan)
Perform common-subexpression-elimination on Plan.
static void addActiveLaneMask(VPlan &Plan, bool UseActiveLaneMaskForControlFlow, bool DataAndControlFlowWithoutRuntimeCheck)
Replace (ICMP_ULE, wide canonical IV, backedge-taken-count) checks with an (active-lane-mask recipe,...
static void optimize(VPlan &Plan)
Apply VPlan-to-VPlan optimizations to Plan, including induction recipe optimizations,...
static void dissolveLoopRegions(VPlan &Plan)
Replace loop regions with explicit CFG.
static void narrowInterleaveGroups(VPlan &Plan, ElementCount VF, unsigned VectorRegWidth)
Try to convert a plan with interleave groups with VF elements to a plan with the interleave groups re...
static void truncateToMinimalBitwidths(VPlan &Plan, const MapVector< Instruction *, uint64_t > &MinBWs)
Insert truncates and extends for any truncated recipe.
static bool adjustFixedOrderRecurrences(VPlan &Plan, VPBuilder &Builder)
Try to have all users of fixed-order recurrences appear after the recipe defining their previous valu...
static void optimizeForVFAndUF(VPlan &Plan, ElementCount BestVF, unsigned BestUF, PredicatedScalarEvolution &PSE)
Optimize Plan based on BestVF and BestUF.
static void materializeVFAndVFxUF(VPlan &Plan, VPBasicBlock *VectorPH, ElementCount VF)
Materialize VF and VFxUF to be computed explicitly using VPInstructions.
static void addMinimumVectorEpilogueIterationCheck(VPlan &Plan, Value *TripCount, Value *VectorTripCount, bool RequiresScalarEpilogue, ElementCount EpilogueVF, unsigned EpilogueUF, unsigned MainLoopStep, unsigned EpilogueLoopStep, ScalarEvolution &SE)
Add a check to Plan to see if the epilogue vector loop should be executed.
static LLVM_ABI_FOR_TEST void addMiddleCheck(VPlan &Plan, bool RequiresScalarEpilogueCheck, bool TailFolded)
If a check is needed to guard executing the scalar epilogue loop, it will be added to the middle bloc...
TODO: The following VectorizationFactor was pulled out of LoopVectorizationCostModel class.
InstructionCost Cost
Cost of the loop with that width.
ElementCount MinProfitableTripCount
The minimum trip count required to make vectorization profitable, e.g.
ElementCount Width
Vector width with best cost.
InstructionCost ScalarCost
Cost of the scalar loop.
static VectorizationFactor Disabled()
Width 1 means no vectorization, cost 0 means uncomputed cost.
static LLVM_ABI bool HoistRuntimeChecks