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/Proposal/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 
56 #include "llvm/Transforms/Vectorize/LoopVectorize.h"
57 #include "LoopVectorizationPlanner.h"
58 #include "VPRecipeBuilder.h"
59 #include "VPlan.h"
60 #include "VPlanHCFGBuilder.h"
61 #include "VPlanPredicator.h"
62 #include "VPlanTransforms.h"
63 #include "llvm/ADT/APInt.h"
64 #include "llvm/ADT/ArrayRef.h"
65 #include "llvm/ADT/DenseMap.h"
66 #include "llvm/ADT/DenseMapInfo.h"
67 #include "llvm/ADT/Hashing.h"
68 #include "llvm/ADT/MapVector.h"
69 #include "llvm/ADT/None.h"
70 #include "llvm/ADT/Optional.h"
71 #include "llvm/ADT/STLExtras.h"
72 #include "llvm/ADT/SmallPtrSet.h"
73 #include "llvm/ADT/SmallSet.h"
74 #include "llvm/ADT/SmallVector.h"
75 #include "llvm/ADT/Statistic.h"
76 #include "llvm/ADT/StringRef.h"
77 #include "llvm/ADT/Twine.h"
78 #include "llvm/ADT/iterator_range.h"
79 #include "llvm/Analysis/AssumptionCache.h"
80 #include "llvm/Analysis/BasicAliasAnalysis.h"
81 #include "llvm/Analysis/BlockFrequencyInfo.h"
82 #include "llvm/Analysis/CFG.h"
83 #include "llvm/Analysis/CodeMetrics.h"
84 #include "llvm/Analysis/DemandedBits.h"
85 #include "llvm/Analysis/GlobalsModRef.h"
86 #include "llvm/Analysis/LoopAccessAnalysis.h"
87 #include "llvm/Analysis/LoopAnalysisManager.h"
88 #include "llvm/Analysis/LoopInfo.h"
89 #include "llvm/Analysis/LoopIterator.h"
90 #include "llvm/Analysis/OptimizationRemarkEmitter.h"
91 #include "llvm/Analysis/ProfileSummaryInfo.h"
92 #include "llvm/Analysis/ScalarEvolution.h"
93 #include "llvm/Analysis/ScalarEvolutionExpressions.h"
94 #include "llvm/Analysis/TargetLibraryInfo.h"
95 #include "llvm/Analysis/TargetTransformInfo.h"
96 #include "llvm/Analysis/VectorUtils.h"
97 #include "llvm/IR/Attributes.h"
98 #include "llvm/IR/BasicBlock.h"
99 #include "llvm/IR/CFG.h"
100 #include "llvm/IR/Constant.h"
101 #include "llvm/IR/Constants.h"
102 #include "llvm/IR/DataLayout.h"
103 #include "llvm/IR/DebugInfoMetadata.h"
104 #include "llvm/IR/DebugLoc.h"
105 #include "llvm/IR/DerivedTypes.h"
106 #include "llvm/IR/DiagnosticInfo.h"
107 #include "llvm/IR/Dominators.h"
108 #include "llvm/IR/Function.h"
109 #include "llvm/IR/IRBuilder.h"
110 #include "llvm/IR/InstrTypes.h"
111 #include "llvm/IR/Instruction.h"
112 #include "llvm/IR/Instructions.h"
113 #include "llvm/IR/IntrinsicInst.h"
114 #include "llvm/IR/Intrinsics.h"
115 #include "llvm/IR/LLVMContext.h"
116 #include "llvm/IR/Metadata.h"
117 #include "llvm/IR/Module.h"
118 #include "llvm/IR/Operator.h"
119 #include "llvm/IR/PatternMatch.h"
120 #include "llvm/IR/Type.h"
121 #include "llvm/IR/Use.h"
122 #include "llvm/IR/User.h"
123 #include "llvm/IR/Value.h"
124 #include "llvm/IR/ValueHandle.h"
125 #include "llvm/IR/Verifier.h"
126 #include "llvm/InitializePasses.h"
127 #include "llvm/Pass.h"
128 #include "llvm/Support/Casting.h"
129 #include "llvm/Support/CommandLine.h"
130 #include "llvm/Support/Compiler.h"
131 #include "llvm/Support/Debug.h"
132 #include "llvm/Support/ErrorHandling.h"
133 #include "llvm/Support/InstructionCost.h"
134 #include "llvm/Support/MathExtras.h"
135 #include "llvm/Support/raw_ostream.h"
136 #include "llvm/Transforms/Utils/BasicBlockUtils.h"
137 #include "llvm/Transforms/Utils/InjectTLIMappings.h"
138 #include "llvm/Transforms/Utils/LoopSimplify.h"
139 #include "llvm/Transforms/Utils/LoopUtils.h"
140 #include "llvm/Transforms/Utils/LoopVersioning.h"
141 #include "llvm/Transforms/Utils/ScalarEvolutionExpander.h"
142 #include "llvm/Transforms/Utils/SizeOpts.h"
143 #include "llvm/Transforms/Vectorize/LoopVectorizationLegality.h"
144 #include <algorithm>
145 #include <cassert>
146 #include <cstdint>
147 #include <cstdlib>
148 #include <functional>
149 #include <iterator>
150 #include <limits>
151 #include <memory>
152 #include <string>
153 #include <tuple>
154 #include <utility>
155 
156 using namespace llvm;
157 
158 #define LV_NAME "loop-vectorize"
159 #define DEBUG_TYPE LV_NAME
160 
161 #ifndef NDEBUG
162 const char VerboseDebug[] = DEBUG_TYPE "-verbose";
163 #endif
164 
165 /// @{
166 /// Metadata attribute names
167 const char LLVMLoopVectorizeFollowupAll[] = "llvm.loop.vectorize.followup_all";
168 const char LLVMLoopVectorizeFollowupVectorized[] =
169     "llvm.loop.vectorize.followup_vectorized";
170 const char LLVMLoopVectorizeFollowupEpilogue[] =
171     "llvm.loop.vectorize.followup_epilogue";
172 /// @}
173 
174 STATISTIC(LoopsVectorized, "Number of loops vectorized");
175 STATISTIC(LoopsAnalyzed, "Number of loops analyzed for vectorization");
176 STATISTIC(LoopsEpilogueVectorized, "Number of epilogues vectorized");
177 
178 static cl::opt<bool> EnableEpilogueVectorization(
179     "enable-epilogue-vectorization", cl::init(true), cl::Hidden,
180     cl::desc("Enable vectorization of epilogue loops."));
181 
182 static cl::opt<unsigned> EpilogueVectorizationForceVF(
183     "epilogue-vectorization-force-VF", cl::init(1), cl::Hidden,
184     cl::desc("When epilogue vectorization is enabled, and a value greater than "
185              "1 is specified, forces the given VF for all applicable epilogue "
186              "loops."));
187 
188 static cl::opt<unsigned> EpilogueVectorizationMinVF(
189     "epilogue-vectorization-minimum-VF", cl::init(16), cl::Hidden,
190     cl::desc("Only loops with vectorization factor equal to or larger than "
191              "the specified value are considered for epilogue vectorization."));
192 
193 /// Loops with a known constant trip count below this number are vectorized only
194 /// if no scalar iteration overheads are incurred.
195 static cl::opt<unsigned> TinyTripCountVectorThreshold(
196     "vectorizer-min-trip-count", cl::init(16), cl::Hidden,
197     cl::desc("Loops with a constant trip count that is smaller than this "
198              "value are vectorized only if no scalar iteration overheads "
199              "are incurred."));
200 
201 static cl::opt<unsigned> PragmaVectorizeMemoryCheckThreshold(
202     "pragma-vectorize-memory-check-threshold", cl::init(128), cl::Hidden,
203     cl::desc("The maximum allowed number of runtime memory checks with a "
204              "vectorize(enable) pragma."));
205 
206 // Option prefer-predicate-over-epilogue indicates that an epilogue is undesired,
207 // that predication is preferred, and this lists all options. I.e., the
208 // vectorizer will try to fold the tail-loop (epilogue) into the vector body
209 // and predicate the instructions accordingly. If tail-folding fails, there are
210 // different fallback strategies depending on these values:
211 namespace PreferPredicateTy {
212   enum Option {
213     ScalarEpilogue = 0,
214     PredicateElseScalarEpilogue,
215     PredicateOrDontVectorize
216   };
217 } // namespace PreferPredicateTy
218 
219 static cl::opt<PreferPredicateTy::Option> PreferPredicateOverEpilogue(
220     "prefer-predicate-over-epilogue",
221     cl::init(PreferPredicateTy::ScalarEpilogue),
222     cl::Hidden,
223     cl::desc("Tail-folding and predication preferences over creating a scalar "
224              "epilogue loop."),
225     cl::values(clEnumValN(PreferPredicateTy::ScalarEpilogue,
226                          "scalar-epilogue",
227                          "Don't tail-predicate loops, create scalar epilogue"),
228               clEnumValN(PreferPredicateTy::PredicateElseScalarEpilogue,
229                          "predicate-else-scalar-epilogue",
230                          "prefer tail-folding, create scalar epilogue if tail "
231                          "folding fails."),
232               clEnumValN(PreferPredicateTy::PredicateOrDontVectorize,
233                          "predicate-dont-vectorize",
234                          "prefers tail-folding, don't attempt vectorization if "
235                          "tail-folding fails.")));
236 
237 static cl::opt<bool> MaximizeBandwidth(
238     "vectorizer-maximize-bandwidth", cl::init(false), cl::Hidden,
239     cl::desc("Maximize bandwidth when selecting vectorization factor which "
240              "will be determined by the smallest type in loop."));
241 
242 static cl::opt<bool> EnableInterleavedMemAccesses(
243     "enable-interleaved-mem-accesses", cl::init(false), cl::Hidden,
244     cl::desc("Enable vectorization on interleaved memory accesses in a loop"));
245 
246 /// An interleave-group may need masking if it resides in a block that needs
247 /// predication, or in order to mask away gaps.
248 static cl::opt<bool> EnableMaskedInterleavedMemAccesses(
249     "enable-masked-interleaved-mem-accesses", cl::init(false), cl::Hidden,
250     cl::desc("Enable vectorization on masked interleaved memory accesses in a loop"));
251 
252 static cl::opt<unsigned> TinyTripCountInterleaveThreshold(
253     "tiny-trip-count-interleave-threshold", cl::init(128), cl::Hidden,
254     cl::desc("We don't interleave loops with a estimated constant trip count "
255              "below this number"));
256 
257 static cl::opt<unsigned> ForceTargetNumScalarRegs(
258     "force-target-num-scalar-regs", cl::init(0), cl::Hidden,
259     cl::desc("A flag that overrides the target's number of scalar registers."));
260 
261 static cl::opt<unsigned> ForceTargetNumVectorRegs(
262     "force-target-num-vector-regs", cl::init(0), cl::Hidden,
263     cl::desc("A flag that overrides the target's number of vector registers."));
264 
265 static cl::opt<unsigned> ForceTargetMaxScalarInterleaveFactor(
266     "force-target-max-scalar-interleave", cl::init(0), cl::Hidden,
267     cl::desc("A flag that overrides the target's max interleave factor for "
268              "scalar loops."));
269 
270 static cl::opt<unsigned> ForceTargetMaxVectorInterleaveFactor(
271     "force-target-max-vector-interleave", cl::init(0), cl::Hidden,
272     cl::desc("A flag that overrides the target's max interleave factor for "
273              "vectorized loops."));
274 
275 static cl::opt<unsigned> ForceTargetInstructionCost(
276     "force-target-instruction-cost", cl::init(0), cl::Hidden,
277     cl::desc("A flag that overrides the target's expected cost for "
278              "an instruction to a single constant value. Mostly "
279              "useful for getting consistent testing."));
280 
281 static cl::opt<bool> ForceTargetSupportsScalableVectors(
282     "force-target-supports-scalable-vectors", cl::init(false), cl::Hidden,
283     cl::desc(
284         "Pretend that scalable vectors are supported, even if the target does "
285         "not support them. This flag should only be used for testing."));
286 
287 static cl::opt<unsigned> SmallLoopCost(
288     "small-loop-cost", cl::init(20), cl::Hidden,
289     cl::desc(
290         "The cost of a loop that is considered 'small' by the interleaver."));
291 
292 static cl::opt<bool> LoopVectorizeWithBlockFrequency(
293     "loop-vectorize-with-block-frequency", cl::init(true), cl::Hidden,
294     cl::desc("Enable the use of the block frequency analysis to access PGO "
295              "heuristics minimizing code growth in cold regions and being more "
296              "aggressive in hot regions."));
297 
298 // Runtime interleave loops for load/store throughput.
299 static cl::opt<bool> EnableLoadStoreRuntimeInterleave(
300     "enable-loadstore-runtime-interleave", cl::init(true), cl::Hidden,
301     cl::desc(
302         "Enable runtime interleaving until load/store ports are saturated"));
303 
304 /// Interleave small loops with scalar reductions.
305 static cl::opt<bool> InterleaveSmallLoopScalarReduction(
306     "interleave-small-loop-scalar-reduction", cl::init(false), cl::Hidden,
307     cl::desc("Enable interleaving for loops with small iteration counts that "
308              "contain scalar reductions to expose ILP."));
309 
310 /// The number of stores in a loop that are allowed to need predication.
311 static cl::opt<unsigned> NumberOfStoresToPredicate(
312     "vectorize-num-stores-pred", cl::init(1), cl::Hidden,
313     cl::desc("Max number of stores to be predicated behind an if."));
314 
315 static cl::opt<bool> EnableIndVarRegisterHeur(
316     "enable-ind-var-reg-heur", cl::init(true), cl::Hidden,
317     cl::desc("Count the induction variable only once when interleaving"));
318 
319 static cl::opt<bool> EnableCondStoresVectorization(
320     "enable-cond-stores-vec", cl::init(true), cl::Hidden,
321     cl::desc("Enable if predication of stores during vectorization."));
322 
323 static cl::opt<unsigned> MaxNestedScalarReductionIC(
324     "max-nested-scalar-reduction-interleave", cl::init(2), cl::Hidden,
325     cl::desc("The maximum interleave count to use when interleaving a scalar "
326              "reduction in a nested loop."));
327 
328 static cl::opt<bool>
329     PreferInLoopReductions("prefer-inloop-reductions", cl::init(false),
330                            cl::Hidden,
331                            cl::desc("Prefer in-loop vector reductions, "
332                                     "overriding the targets preference."));
333 
334 static cl::opt<bool> ForceOrderedReductions(
335     "force-ordered-reductions", cl::init(false), cl::Hidden,
336     cl::desc("Enable the vectorisation of loops with in-order (strict) "
337              "FP reductions"));
338 
339 static cl::opt<bool> PreferPredicatedReductionSelect(
340     "prefer-predicated-reduction-select", cl::init(false), cl::Hidden,
341     cl::desc(
342         "Prefer predicating a reduction operation over an after loop select."));
343 
344 cl::opt<bool> EnableVPlanNativePath(
345     "enable-vplan-native-path", cl::init(false), cl::Hidden,
346     cl::desc("Enable VPlan-native vectorization path with "
347              "support for outer loop vectorization."));
348 
349 // FIXME: Remove this switch once we have divergence analysis. Currently we
350 // assume divergent non-backedge branches when this switch is true.
351 cl::opt<bool> EnableVPlanPredication(
352     "enable-vplan-predication", cl::init(false), cl::Hidden,
353     cl::desc("Enable VPlan-native vectorization path predicator with "
354              "support for outer loop vectorization."));
355 
356 // This flag enables the stress testing of the VPlan H-CFG construction in the
357 // VPlan-native vectorization path. It must be used in conjuction with
358 // -enable-vplan-native-path. -vplan-verify-hcfg can also be used to enable the
359 // verification of the H-CFGs built.
360 static cl::opt<bool> VPlanBuildStressTest(
361     "vplan-build-stress-test", cl::init(false), cl::Hidden,
362     cl::desc(
363         "Build VPlan for every supported loop nest in the function and bail "
364         "out right after the build (stress test the VPlan H-CFG construction "
365         "in the VPlan-native vectorization path)."));
366 
367 cl::opt<bool> llvm::EnableLoopInterleaving(
368     "interleave-loops", cl::init(true), cl::Hidden,
369     cl::desc("Enable loop interleaving in Loop vectorization passes"));
370 cl::opt<bool> llvm::EnableLoopVectorization(
371     "vectorize-loops", cl::init(true), cl::Hidden,
372     cl::desc("Run the Loop vectorization passes"));
373 
374 cl::opt<bool> PrintVPlansInDotFormat(
375     "vplan-print-in-dot-format", cl::init(false), cl::Hidden,
376     cl::desc("Use dot format instead of plain text when dumping VPlans"));
377 
378 /// A helper function that returns true if the given type is irregular. The
379 /// type is irregular if its allocated size doesn't equal the store size of an
380 /// element of the corresponding vector type.
381 static bool hasIrregularType(Type *Ty, const DataLayout &DL) {
382   // Determine if an array of N elements of type Ty is "bitcast compatible"
383   // with a <N x Ty> vector.
384   // This is only true if there is no padding between the array elements.
385   return DL.getTypeAllocSizeInBits(Ty) != DL.getTypeSizeInBits(Ty);
386 }
387 
388 /// A helper function that returns the reciprocal of the block probability of
389 /// predicated blocks. If we return X, we are assuming the predicated block
390 /// will execute once for every X iterations of the loop header.
391 ///
392 /// TODO: We should use actual block probability here, if available. Currently,
393 ///       we always assume predicated blocks have a 50% chance of executing.
394 static unsigned getReciprocalPredBlockProb() { return 2; }
395 
396 /// A helper function that returns an integer or floating-point constant with
397 /// value C.
398 static Constant *getSignedIntOrFpConstant(Type *Ty, int64_t C) {
399   return Ty->isIntegerTy() ? ConstantInt::getSigned(Ty, C)
400                            : ConstantFP::get(Ty, C);
401 }
402 
403 /// Returns "best known" trip count for the specified loop \p L as defined by
404 /// the following procedure:
405 ///   1) Returns exact trip count if it is known.
406 ///   2) Returns expected trip count according to profile data if any.
407 ///   3) Returns upper bound estimate if it is known.
408 ///   4) Returns None if all of the above failed.
409 static Optional<unsigned> getSmallBestKnownTC(ScalarEvolution &SE, Loop *L) {
410   // Check if exact trip count is known.
411   if (unsigned ExpectedTC = SE.getSmallConstantTripCount(L))
412     return ExpectedTC;
413 
414   // Check if there is an expected trip count available from profile data.
415   if (LoopVectorizeWithBlockFrequency)
416     if (auto EstimatedTC = getLoopEstimatedTripCount(L))
417       return EstimatedTC;
418 
419   // Check if upper bound estimate is known.
420   if (unsigned ExpectedTC = SE.getSmallConstantMaxTripCount(L))
421     return ExpectedTC;
422 
423   return None;
424 }
425 
426 // Forward declare GeneratedRTChecks.
427 class GeneratedRTChecks;
428 
429 namespace llvm {
430 
431 /// InnerLoopVectorizer vectorizes loops which contain only one basic
432 /// block to a specified vectorization factor (VF).
433 /// This class performs the widening of scalars into vectors, or multiple
434 /// scalars. This class also implements the following features:
435 /// * It inserts an epilogue loop for handling loops that don't have iteration
436 ///   counts that are known to be a multiple of the vectorization factor.
437 /// * It handles the code generation for reduction variables.
438 /// * Scalarization (implementation using scalars) of un-vectorizable
439 ///   instructions.
440 /// InnerLoopVectorizer does not perform any vectorization-legality
441 /// checks, and relies on the caller to check for the different legality
442 /// aspects. The InnerLoopVectorizer relies on the
443 /// LoopVectorizationLegality class to provide information about the induction
444 /// and reduction variables that were found to a given vectorization factor.
445 class InnerLoopVectorizer {
446 public:
447   InnerLoopVectorizer(Loop *OrigLoop, PredicatedScalarEvolution &PSE,
448                       LoopInfo *LI, DominatorTree *DT,
449                       const TargetLibraryInfo *TLI,
450                       const TargetTransformInfo *TTI, AssumptionCache *AC,
451                       OptimizationRemarkEmitter *ORE, ElementCount VecWidth,
452                       unsigned UnrollFactor, LoopVectorizationLegality *LVL,
453                       LoopVectorizationCostModel *CM, BlockFrequencyInfo *BFI,
454                       ProfileSummaryInfo *PSI, GeneratedRTChecks &RTChecks)
455       : OrigLoop(OrigLoop), PSE(PSE), LI(LI), DT(DT), TLI(TLI), TTI(TTI),
456         AC(AC), ORE(ORE), VF(VecWidth), UF(UnrollFactor),
457         Builder(PSE.getSE()->getContext()), Legal(LVL), Cost(CM), BFI(BFI),
458         PSI(PSI), RTChecks(RTChecks) {
459     // Query this against the original loop and save it here because the profile
460     // of the original loop header may change as the transformation happens.
461     OptForSizeBasedOnProfile = llvm::shouldOptimizeForSize(
462         OrigLoop->getHeader(), PSI, BFI, PGSOQueryType::IRPass);
463   }
464 
465   virtual ~InnerLoopVectorizer() = default;
466 
467   /// Create a new empty loop that will contain vectorized instructions later
468   /// on, while the old loop will be used as the scalar remainder. Control flow
469   /// is generated around the vectorized (and scalar epilogue) loops consisting
470   /// of various checks and bypasses. Return the pre-header block of the new
471   /// loop.
472   /// In the case of epilogue vectorization, this function is overriden to
473   /// handle the more complex control flow around the loops.
474   virtual BasicBlock *createVectorizedLoopSkeleton();
475 
476   /// Widen a single instruction within the innermost loop.
477   void widenInstruction(Instruction &I, VPValue *Def, VPUser &Operands,
478                         VPTransformState &State);
479 
480   /// Widen a single call instruction within the innermost loop.
481   void widenCallInstruction(CallInst &I, VPValue *Def, VPUser &ArgOperands,
482                             VPTransformState &State);
483 
484   /// Widen a single select instruction within the innermost loop.
485   void widenSelectInstruction(SelectInst &I, VPValue *VPDef, VPUser &Operands,
486                               bool InvariantCond, VPTransformState &State);
487 
488   /// Fix the vectorized code, taking care of header phi's, live-outs, and more.
489   void fixVectorizedLoop(VPTransformState &State);
490 
491   // Return true if any runtime check is added.
492   bool areSafetyChecksAdded() { return AddedSafetyChecks; }
493 
494   /// A type for vectorized values in the new loop. Each value from the
495   /// original loop, when vectorized, is represented by UF vector values in the
496   /// new unrolled loop, where UF is the unroll factor.
497   using VectorParts = SmallVector<Value *, 2>;
498 
499   /// Vectorize a single GetElementPtrInst based on information gathered and
500   /// decisions taken during planning.
501   void widenGEP(GetElementPtrInst *GEP, VPValue *VPDef, VPUser &Indices,
502                 unsigned UF, ElementCount VF, bool IsPtrLoopInvariant,
503                 SmallBitVector &IsIndexLoopInvariant, VPTransformState &State);
504 
505   /// Vectorize a single first-order recurrence or pointer induction PHINode in
506   /// a block. This method handles the induction variable canonicalization. It
507   /// supports both VF = 1 for unrolled loops and arbitrary length vectors.
508   void widenPHIInstruction(Instruction *PN, VPWidenPHIRecipe *PhiR,
509                            VPTransformState &State);
510 
511   /// A helper function to scalarize a single Instruction in the innermost loop.
512   /// Generates a sequence of scalar instances for each lane between \p MinLane
513   /// and \p MaxLane, times each part between \p MinPart and \p MaxPart,
514   /// inclusive. Uses the VPValue operands from \p Operands instead of \p
515   /// Instr's operands.
516   void scalarizeInstruction(Instruction *Instr, VPValue *Def, VPUser &Operands,
517                             const VPIteration &Instance, bool IfPredicateInstr,
518                             VPTransformState &State);
519 
520   /// Widen an integer or floating-point induction variable \p IV. If \p Trunc
521   /// is provided, the integer induction variable will first be truncated to
522   /// the corresponding type.
523   void widenIntOrFpInduction(PHINode *IV, Value *Start, TruncInst *Trunc,
524                              VPValue *Def, VPValue *CastDef,
525                              VPTransformState &State);
526 
527   /// Construct the vector value of a scalarized value \p V one lane at a time.
528   void packScalarIntoVectorValue(VPValue *Def, const VPIteration &Instance,
529                                  VPTransformState &State);
530 
531   /// Try to vectorize interleaved access group \p Group with the base address
532   /// given in \p Addr, optionally masking the vector operations if \p
533   /// BlockInMask is non-null. Use \p State to translate given VPValues to IR
534   /// values in the vectorized loop.
535   void vectorizeInterleaveGroup(const InterleaveGroup<Instruction> *Group,
536                                 ArrayRef<VPValue *> VPDefs,
537                                 VPTransformState &State, VPValue *Addr,
538                                 ArrayRef<VPValue *> StoredValues,
539                                 VPValue *BlockInMask = nullptr);
540 
541   /// Vectorize Load and Store instructions with the base address given in \p
542   /// Addr, optionally masking the vector operations if \p BlockInMask is
543   /// non-null. Use \p State to translate given VPValues to IR values in the
544   /// vectorized loop.
545   void vectorizeMemoryInstruction(Instruction *Instr, VPTransformState &State,
546                                   VPValue *Def, VPValue *Addr,
547                                   VPValue *StoredValue, VPValue *BlockInMask,
548                                   bool ConsecutiveStride, bool Reverse);
549 
550   /// Set the debug location in the builder \p Ptr using the debug location in
551   /// \p V. If \p Ptr is None then it uses the class member's Builder.
552   void setDebugLocFromInst(const Value *V,
553                            Optional<IRBuilder<> *> CustomBuilder = None);
554 
555   /// Fix the non-induction PHIs in the OrigPHIsToFix vector.
556   void fixNonInductionPHIs(VPTransformState &State);
557 
558   /// Returns true if the reordering of FP operations is not allowed, but we are
559   /// able to vectorize with strict in-order reductions for the given RdxDesc.
560   bool useOrderedReductions(RecurrenceDescriptor &RdxDesc);
561 
562   /// Create a broadcast instruction. This method generates a broadcast
563   /// instruction (shuffle) for loop invariant values and for the induction
564   /// value. If this is the induction variable then we extend it to N, N+1, ...
565   /// this is needed because each iteration in the loop corresponds to a SIMD
566   /// element.
567   virtual Value *getBroadcastInstrs(Value *V);
568 
569 protected:
570   friend class LoopVectorizationPlanner;
571 
572   /// A small list of PHINodes.
573   using PhiVector = SmallVector<PHINode *, 4>;
574 
575   /// A type for scalarized values in the new loop. Each value from the
576   /// original loop, when scalarized, is represented by UF x VF scalar values
577   /// in the new unrolled loop, where UF is the unroll factor and VF is the
578   /// vectorization factor.
579   using ScalarParts = SmallVector<SmallVector<Value *, 4>, 2>;
580 
581   /// Set up the values of the IVs correctly when exiting the vector loop.
582   void fixupIVUsers(PHINode *OrigPhi, const InductionDescriptor &II,
583                     Value *CountRoundDown, Value *EndValue,
584                     BasicBlock *MiddleBlock);
585 
586   /// Create a new induction variable inside L.
587   PHINode *createInductionVariable(Loop *L, Value *Start, Value *End,
588                                    Value *Step, Instruction *DL);
589 
590   /// Handle all cross-iteration phis in the header.
591   void fixCrossIterationPHIs(VPTransformState &State);
592 
593   /// Create the exit value of first order recurrences in the middle block and
594   /// update their users.
595   void fixFirstOrderRecurrence(VPWidenPHIRecipe *PhiR, VPTransformState &State);
596 
597   /// Create code for the loop exit value of the reduction.
598   void fixReduction(VPReductionPHIRecipe *Phi, VPTransformState &State);
599 
600   /// Clear NSW/NUW flags from reduction instructions if necessary.
601   void clearReductionWrapFlags(const RecurrenceDescriptor &RdxDesc,
602                                VPTransformState &State);
603 
604   /// Fixup the LCSSA phi nodes in the unique exit block.  This simply
605   /// means we need to add the appropriate incoming value from the middle
606   /// block as exiting edges from the scalar epilogue loop (if present) are
607   /// already in place, and we exit the vector loop exclusively to the middle
608   /// block.
609   void fixLCSSAPHIs(VPTransformState &State);
610 
611   /// Iteratively sink the scalarized operands of a predicated instruction into
612   /// the block that was created for it.
613   void sinkScalarOperands(Instruction *PredInst);
614 
615   /// Shrinks vector element sizes to the smallest bitwidth they can be legally
616   /// represented as.
617   void truncateToMinimalBitwidths(VPTransformState &State);
618 
619   /// This function adds
620   /// (StartIdx * Step, (StartIdx + 1) * Step, (StartIdx + 2) * Step, ...)
621   /// to each vector element of Val. The sequence starts at StartIndex.
622   /// \p Opcode is relevant for FP induction variable.
623   virtual Value *
624   getStepVector(Value *Val, Value *StartIdx, Value *Step,
625                 Instruction::BinaryOps Opcode = Instruction::BinaryOpsEnd);
626 
627   /// Compute scalar induction steps. \p ScalarIV is the scalar induction
628   /// variable on which to base the steps, \p Step is the size of the step, and
629   /// \p EntryVal is the value from the original loop that maps to the steps.
630   /// Note that \p EntryVal doesn't have to be an induction variable - it
631   /// can also be a truncate instruction.
632   void buildScalarSteps(Value *ScalarIV, Value *Step, Instruction *EntryVal,
633                         const InductionDescriptor &ID, VPValue *Def,
634                         VPValue *CastDef, VPTransformState &State);
635 
636   /// Create a vector induction phi node based on an existing scalar one. \p
637   /// EntryVal is the value from the original loop that maps to the vector phi
638   /// node, and \p Step is the loop-invariant step. If \p EntryVal is a
639   /// truncate instruction, instead of widening the original IV, we widen a
640   /// version of the IV truncated to \p EntryVal's type.
641   void createVectorIntOrFpInductionPHI(const InductionDescriptor &II,
642                                        Value *Step, Value *Start,
643                                        Instruction *EntryVal, VPValue *Def,
644                                        VPValue *CastDef,
645                                        VPTransformState &State);
646 
647   /// Returns true if an instruction \p I should be scalarized instead of
648   /// vectorized for the chosen vectorization factor.
649   bool shouldScalarizeInstruction(Instruction *I) const;
650 
651   /// Returns true if we should generate a scalar version of \p IV.
652   bool needsScalarInduction(Instruction *IV) const;
653 
654   /// If there is a cast involved in the induction variable \p ID, which should
655   /// be ignored in the vectorized loop body, this function records the
656   /// VectorLoopValue of the respective Phi also as the VectorLoopValue of the
657   /// cast. We had already proved that the casted Phi is equal to the uncasted
658   /// Phi in the vectorized loop (under a runtime guard), and therefore
659   /// there is no need to vectorize the cast - the same value can be used in the
660   /// vector loop for both the Phi and the cast.
661   /// If \p VectorLoopValue is a scalarized value, \p Lane is also specified,
662   /// Otherwise, \p VectorLoopValue is a widened/vectorized value.
663   ///
664   /// \p EntryVal is the value from the original loop that maps to the vector
665   /// phi node and is used to distinguish what is the IV currently being
666   /// processed - original one (if \p EntryVal is a phi corresponding to the
667   /// original IV) or the "newly-created" one based on the proof mentioned above
668   /// (see also buildScalarSteps() and createVectorIntOrFPInductionPHI()). In the
669   /// latter case \p EntryVal is a TruncInst and we must not record anything for
670   /// that IV, but it's error-prone to expect callers of this routine to care
671   /// about that, hence this explicit parameter.
672   void recordVectorLoopValueForInductionCast(
673       const InductionDescriptor &ID, const Instruction *EntryVal,
674       Value *VectorLoopValue, VPValue *CastDef, VPTransformState &State,
675       unsigned Part, unsigned Lane = UINT_MAX);
676 
677   /// Generate a shuffle sequence that will reverse the vector Vec.
678   virtual Value *reverseVector(Value *Vec);
679 
680   /// Returns (and creates if needed) the original loop trip count.
681   Value *getOrCreateTripCount(Loop *NewLoop);
682 
683   /// Returns (and creates if needed) the trip count of the widened loop.
684   Value *getOrCreateVectorTripCount(Loop *NewLoop);
685 
686   /// Returns a bitcasted value to the requested vector type.
687   /// Also handles bitcasts of vector<float> <-> vector<pointer> types.
688   Value *createBitOrPointerCast(Value *V, VectorType *DstVTy,
689                                 const DataLayout &DL);
690 
691   /// Emit a bypass check to see if the vector trip count is zero, including if
692   /// it overflows.
693   void emitMinimumIterationCountCheck(Loop *L, BasicBlock *Bypass);
694 
695   /// Emit a bypass check to see if all of the SCEV assumptions we've
696   /// had to make are correct. Returns the block containing the checks or
697   /// nullptr if no checks have been added.
698   BasicBlock *emitSCEVChecks(Loop *L, BasicBlock *Bypass);
699 
700   /// Emit bypass checks to check any memory assumptions we may have made.
701   /// Returns the block containing the checks or nullptr if no checks have been
702   /// added.
703   BasicBlock *emitMemRuntimeChecks(Loop *L, BasicBlock *Bypass);
704 
705   /// Compute the transformed value of Index at offset StartValue using step
706   /// StepValue.
707   /// For integer induction, returns StartValue + Index * StepValue.
708   /// For pointer induction, returns StartValue[Index * StepValue].
709   /// FIXME: The newly created binary instructions should contain nsw/nuw
710   /// flags, which can be found from the original scalar operations.
711   Value *emitTransformedIndex(IRBuilder<> &B, Value *Index, ScalarEvolution *SE,
712                               const DataLayout &DL,
713                               const InductionDescriptor &ID) const;
714 
715   /// Emit basic blocks (prefixed with \p Prefix) for the iteration check,
716   /// vector loop preheader, middle block and scalar preheader. Also
717   /// allocate a loop object for the new vector loop and return it.
718   Loop *createVectorLoopSkeleton(StringRef Prefix);
719 
720   /// Create new phi nodes for the induction variables to resume iteration count
721   /// in the scalar epilogue, from where the vectorized loop left off (given by
722   /// \p VectorTripCount).
723   /// In cases where the loop skeleton is more complicated (eg. epilogue
724   /// vectorization) and the resume values can come from an additional bypass
725   /// block, the \p AdditionalBypass pair provides information about the bypass
726   /// block and the end value on the edge from bypass to this loop.
727   void createInductionResumeValues(
728       Loop *L, Value *VectorTripCount,
729       std::pair<BasicBlock *, Value *> AdditionalBypass = {nullptr, nullptr});
730 
731   /// Complete the loop skeleton by adding debug MDs, creating appropriate
732   /// conditional branches in the middle block, preparing the builder and
733   /// running the verifier. Take in the vector loop \p L as argument, and return
734   /// the preheader of the completed vector loop.
735   BasicBlock *completeLoopSkeleton(Loop *L, MDNode *OrigLoopID);
736 
737   /// Add additional metadata to \p To that was not present on \p Orig.
738   ///
739   /// Currently this is used to add the noalias annotations based on the
740   /// inserted memchecks.  Use this for instructions that are *cloned* into the
741   /// vector loop.
742   void addNewMetadata(Instruction *To, const Instruction *Orig);
743 
744   /// Add metadata from one instruction to another.
745   ///
746   /// This includes both the original MDs from \p From and additional ones (\see
747   /// addNewMetadata).  Use this for *newly created* instructions in the vector
748   /// loop.
749   void addMetadata(Instruction *To, Instruction *From);
750 
751   /// Similar to the previous function but it adds the metadata to a
752   /// vector of instructions.
753   void addMetadata(ArrayRef<Value *> To, Instruction *From);
754 
755   /// Allow subclasses to override and print debug traces before/after vplan
756   /// execution, when trace information is requested.
757   virtual void printDebugTracesAtStart(){};
758   virtual void printDebugTracesAtEnd(){};
759 
760   /// The original loop.
761   Loop *OrigLoop;
762 
763   /// A wrapper around ScalarEvolution used to add runtime SCEV checks. Applies
764   /// dynamic knowledge to simplify SCEV expressions and converts them to a
765   /// more usable form.
766   PredicatedScalarEvolution &PSE;
767 
768   /// Loop Info.
769   LoopInfo *LI;
770 
771   /// Dominator Tree.
772   DominatorTree *DT;
773 
774   /// Alias Analysis.
775   AAResults *AA;
776 
777   /// Target Library Info.
778   const TargetLibraryInfo *TLI;
779 
780   /// Target Transform Info.
781   const TargetTransformInfo *TTI;
782 
783   /// Assumption Cache.
784   AssumptionCache *AC;
785 
786   /// Interface to emit optimization remarks.
787   OptimizationRemarkEmitter *ORE;
788 
789   /// LoopVersioning.  It's only set up (non-null) if memchecks were
790   /// used.
791   ///
792   /// This is currently only used to add no-alias metadata based on the
793   /// memchecks.  The actually versioning is performed manually.
794   std::unique_ptr<LoopVersioning> LVer;
795 
796   /// The vectorization SIMD factor to use. Each vector will have this many
797   /// vector elements.
798   ElementCount VF;
799 
800   /// The vectorization unroll factor to use. Each scalar is vectorized to this
801   /// many different vector instructions.
802   unsigned UF;
803 
804   /// The builder that we use
805   IRBuilder<> Builder;
806 
807   // --- Vectorization state ---
808 
809   /// The vector-loop preheader.
810   BasicBlock *LoopVectorPreHeader;
811 
812   /// The scalar-loop preheader.
813   BasicBlock *LoopScalarPreHeader;
814 
815   /// Middle Block between the vector and the scalar.
816   BasicBlock *LoopMiddleBlock;
817 
818   /// The unique ExitBlock of the scalar loop if one exists.  Note that
819   /// there can be multiple exiting edges reaching this block.
820   BasicBlock *LoopExitBlock;
821 
822   /// The vector loop body.
823   BasicBlock *LoopVectorBody;
824 
825   /// The scalar loop body.
826   BasicBlock *LoopScalarBody;
827 
828   /// A list of all bypass blocks. The first block is the entry of the loop.
829   SmallVector<BasicBlock *, 4> LoopBypassBlocks;
830 
831   /// The new Induction variable which was added to the new block.
832   PHINode *Induction = nullptr;
833 
834   /// The induction variable of the old basic block.
835   PHINode *OldInduction = nullptr;
836 
837   /// Store instructions that were predicated.
838   SmallVector<Instruction *, 4> PredicatedInstructions;
839 
840   /// Trip count of the original loop.
841   Value *TripCount = nullptr;
842 
843   /// Trip count of the widened loop (TripCount - TripCount % (VF*UF))
844   Value *VectorTripCount = nullptr;
845 
846   /// The legality analysis.
847   LoopVectorizationLegality *Legal;
848 
849   /// The profitablity analysis.
850   LoopVectorizationCostModel *Cost;
851 
852   // Record whether runtime checks are added.
853   bool AddedSafetyChecks = false;
854 
855   // Holds the end values for each induction variable. We save the end values
856   // so we can later fix-up the external users of the induction variables.
857   DenseMap<PHINode *, Value *> IVEndValues;
858 
859   // Vector of original scalar PHIs whose corresponding widened PHIs need to be
860   // fixed up at the end of vector code generation.
861   SmallVector<PHINode *, 8> OrigPHIsToFix;
862 
863   /// BFI and PSI are used to check for profile guided size optimizations.
864   BlockFrequencyInfo *BFI;
865   ProfileSummaryInfo *PSI;
866 
867   // Whether this loop should be optimized for size based on profile guided size
868   // optimizatios.
869   bool OptForSizeBasedOnProfile;
870 
871   /// Structure to hold information about generated runtime checks, responsible
872   /// for cleaning the checks, if vectorization turns out unprofitable.
873   GeneratedRTChecks &RTChecks;
874 };
875 
876 class InnerLoopUnroller : public InnerLoopVectorizer {
877 public:
878   InnerLoopUnroller(Loop *OrigLoop, PredicatedScalarEvolution &PSE,
879                     LoopInfo *LI, DominatorTree *DT,
880                     const TargetLibraryInfo *TLI,
881                     const TargetTransformInfo *TTI, AssumptionCache *AC,
882                     OptimizationRemarkEmitter *ORE, unsigned UnrollFactor,
883                     LoopVectorizationLegality *LVL,
884                     LoopVectorizationCostModel *CM, BlockFrequencyInfo *BFI,
885                     ProfileSummaryInfo *PSI, GeneratedRTChecks &Check)
886       : InnerLoopVectorizer(OrigLoop, PSE, LI, DT, TLI, TTI, AC, ORE,
887                             ElementCount::getFixed(1), UnrollFactor, LVL, CM,
888                             BFI, PSI, Check) {}
889 
890 private:
891   Value *getBroadcastInstrs(Value *V) override;
892   Value *getStepVector(
893       Value *Val, Value *StartIdx, Value *Step,
894       Instruction::BinaryOps Opcode = Instruction::BinaryOpsEnd) override;
895   Value *reverseVector(Value *Vec) override;
896 };
897 
898 /// Encapsulate information regarding vectorization of a loop and its epilogue.
899 /// This information is meant to be updated and used across two stages of
900 /// epilogue vectorization.
901 struct EpilogueLoopVectorizationInfo {
902   ElementCount MainLoopVF = ElementCount::getFixed(0);
903   unsigned MainLoopUF = 0;
904   ElementCount EpilogueVF = ElementCount::getFixed(0);
905   unsigned EpilogueUF = 0;
906   BasicBlock *MainLoopIterationCountCheck = nullptr;
907   BasicBlock *EpilogueIterationCountCheck = nullptr;
908   BasicBlock *SCEVSafetyCheck = nullptr;
909   BasicBlock *MemSafetyCheck = nullptr;
910   Value *TripCount = nullptr;
911   Value *VectorTripCount = nullptr;
912 
913   EpilogueLoopVectorizationInfo(ElementCount MVF, unsigned MUF,
914                                 ElementCount EVF, unsigned EUF)
915       : MainLoopVF(MVF), MainLoopUF(MUF), EpilogueVF(EVF), EpilogueUF(EUF) {
916     assert(EUF == 1 &&
917            "A high UF for the epilogue loop is likely not beneficial.");
918   }
919 };
920 
921 /// An extension of the inner loop vectorizer that creates a skeleton for a
922 /// vectorized loop that has its epilogue (residual) also vectorized.
923 /// The idea is to run the vplan on a given loop twice, firstly to setup the
924 /// skeleton and vectorize the main loop, and secondly to complete the skeleton
925 /// from the first step and vectorize the epilogue.  This is achieved by
926 /// deriving two concrete strategy classes from this base class and invoking
927 /// them in succession from the loop vectorizer planner.
928 class InnerLoopAndEpilogueVectorizer : public InnerLoopVectorizer {
929 public:
930   InnerLoopAndEpilogueVectorizer(
931       Loop *OrigLoop, PredicatedScalarEvolution &PSE, LoopInfo *LI,
932       DominatorTree *DT, const TargetLibraryInfo *TLI,
933       const TargetTransformInfo *TTI, AssumptionCache *AC,
934       OptimizationRemarkEmitter *ORE, EpilogueLoopVectorizationInfo &EPI,
935       LoopVectorizationLegality *LVL, llvm::LoopVectorizationCostModel *CM,
936       BlockFrequencyInfo *BFI, ProfileSummaryInfo *PSI,
937       GeneratedRTChecks &Checks)
938       : InnerLoopVectorizer(OrigLoop, PSE, LI, DT, TLI, TTI, AC, ORE,
939                             EPI.MainLoopVF, EPI.MainLoopUF, LVL, CM, BFI, PSI,
940                             Checks),
941         EPI(EPI) {}
942 
943   // Override this function to handle the more complex control flow around the
944   // three loops.
945   BasicBlock *createVectorizedLoopSkeleton() final override {
946     return createEpilogueVectorizedLoopSkeleton();
947   }
948 
949   /// The interface for creating a vectorized skeleton using one of two
950   /// different strategies, each corresponding to one execution of the vplan
951   /// as described above.
952   virtual BasicBlock *createEpilogueVectorizedLoopSkeleton() = 0;
953 
954   /// Holds and updates state information required to vectorize the main loop
955   /// and its epilogue in two separate passes. This setup helps us avoid
956   /// regenerating and recomputing runtime safety checks. It also helps us to
957   /// shorten the iteration-count-check path length for the cases where the
958   /// iteration count of the loop is so small that the main vector loop is
959   /// completely skipped.
960   EpilogueLoopVectorizationInfo &EPI;
961 };
962 
963 /// A specialized derived class of inner loop vectorizer that performs
964 /// vectorization of *main* loops in the process of vectorizing loops and their
965 /// epilogues.
966 class EpilogueVectorizerMainLoop : public InnerLoopAndEpilogueVectorizer {
967 public:
968   EpilogueVectorizerMainLoop(
969       Loop *OrigLoop, PredicatedScalarEvolution &PSE, LoopInfo *LI,
970       DominatorTree *DT, const TargetLibraryInfo *TLI,
971       const TargetTransformInfo *TTI, AssumptionCache *AC,
972       OptimizationRemarkEmitter *ORE, EpilogueLoopVectorizationInfo &EPI,
973       LoopVectorizationLegality *LVL, llvm::LoopVectorizationCostModel *CM,
974       BlockFrequencyInfo *BFI, ProfileSummaryInfo *PSI,
975       GeneratedRTChecks &Check)
976       : InnerLoopAndEpilogueVectorizer(OrigLoop, PSE, LI, DT, TLI, TTI, AC, ORE,
977                                        EPI, LVL, CM, BFI, PSI, Check) {}
978   /// Implements the interface for creating a vectorized skeleton using the
979   /// *main loop* strategy (ie the first pass of vplan execution).
980   BasicBlock *createEpilogueVectorizedLoopSkeleton() final override;
981 
982 protected:
983   /// Emits an iteration count bypass check once for the main loop (when \p
984   /// ForEpilogue is false) and once for the epilogue loop (when \p
985   /// ForEpilogue is true).
986   BasicBlock *emitMinimumIterationCountCheck(Loop *L, BasicBlock *Bypass,
987                                              bool ForEpilogue);
988   void printDebugTracesAtStart() override;
989   void printDebugTracesAtEnd() override;
990 };
991 
992 // A specialized derived class of inner loop vectorizer that performs
993 // vectorization of *epilogue* loops in the process of vectorizing loops and
994 // their epilogues.
995 class EpilogueVectorizerEpilogueLoop : public InnerLoopAndEpilogueVectorizer {
996 public:
997   EpilogueVectorizerEpilogueLoop(
998       Loop *OrigLoop, PredicatedScalarEvolution &PSE, LoopInfo *LI,
999       DominatorTree *DT, const TargetLibraryInfo *TLI,
1000       const TargetTransformInfo *TTI, AssumptionCache *AC,
1001       OptimizationRemarkEmitter *ORE, EpilogueLoopVectorizationInfo &EPI,
1002       LoopVectorizationLegality *LVL, llvm::LoopVectorizationCostModel *CM,
1003       BlockFrequencyInfo *BFI, ProfileSummaryInfo *PSI,
1004       GeneratedRTChecks &Checks)
1005       : InnerLoopAndEpilogueVectorizer(OrigLoop, PSE, LI, DT, TLI, TTI, AC, ORE,
1006                                        EPI, LVL, CM, BFI, PSI, Checks) {}
1007   /// Implements the interface for creating a vectorized skeleton using the
1008   /// *epilogue loop* strategy (ie the second pass of vplan execution).
1009   BasicBlock *createEpilogueVectorizedLoopSkeleton() final override;
1010 
1011 protected:
1012   /// Emits an iteration count bypass check after the main vector loop has
1013   /// finished to see if there are any iterations left to execute by either
1014   /// the vector epilogue or the scalar epilogue.
1015   BasicBlock *emitMinimumVectorEpilogueIterCountCheck(Loop *L,
1016                                                       BasicBlock *Bypass,
1017                                                       BasicBlock *Insert);
1018   void printDebugTracesAtStart() override;
1019   void printDebugTracesAtEnd() override;
1020 };
1021 } // end namespace llvm
1022 
1023 /// Look for a meaningful debug location on the instruction or it's
1024 /// operands.
1025 static Instruction *getDebugLocFromInstOrOperands(Instruction *I) {
1026   if (!I)
1027     return I;
1028 
1029   DebugLoc Empty;
1030   if (I->getDebugLoc() != Empty)
1031     return I;
1032 
1033   for (Use &Op : I->operands()) {
1034     if (Instruction *OpInst = dyn_cast<Instruction>(Op))
1035       if (OpInst->getDebugLoc() != Empty)
1036         return OpInst;
1037   }
1038 
1039   return I;
1040 }
1041 
1042 void InnerLoopVectorizer::setDebugLocFromInst(
1043     const Value *V, Optional<IRBuilder<> *> CustomBuilder) {
1044   IRBuilder<> *B = (CustomBuilder == None) ? &Builder : *CustomBuilder;
1045   if (const Instruction *Inst = dyn_cast_or_null<Instruction>(V)) {
1046     const DILocation *DIL = Inst->getDebugLoc();
1047 
1048     // When a FSDiscriminator is enabled, we don't need to add the multiply
1049     // factors to the discriminators.
1050     if (DIL && Inst->getFunction()->isDebugInfoForProfiling() &&
1051         !isa<DbgInfoIntrinsic>(Inst) && !EnableFSDiscriminator) {
1052       // FIXME: For scalable vectors, assume vscale=1.
1053       auto NewDIL =
1054           DIL->cloneByMultiplyingDuplicationFactor(UF * VF.getKnownMinValue());
1055       if (NewDIL)
1056         B->SetCurrentDebugLocation(NewDIL.getValue());
1057       else
1058         LLVM_DEBUG(dbgs()
1059                    << "Failed to create new discriminator: "
1060                    << DIL->getFilename() << " Line: " << DIL->getLine());
1061     } else
1062       B->SetCurrentDebugLocation(DIL);
1063   } else
1064     B->SetCurrentDebugLocation(DebugLoc());
1065 }
1066 
1067 /// Write a \p DebugMsg about vectorization to the debug output stream. If \p I
1068 /// is passed, the message relates to that particular instruction.
1069 #ifndef NDEBUG
1070 static void debugVectorizationMessage(const StringRef Prefix,
1071                                       const StringRef DebugMsg,
1072                                       Instruction *I) {
1073   dbgs() << "LV: " << Prefix << DebugMsg;
1074   if (I != nullptr)
1075     dbgs() << " " << *I;
1076   else
1077     dbgs() << '.';
1078   dbgs() << '\n';
1079 }
1080 #endif
1081 
1082 /// Create an analysis remark that explains why vectorization failed
1083 ///
1084 /// \p PassName is the name of the pass (e.g. can be AlwaysPrint).  \p
1085 /// RemarkName is the identifier for the remark.  If \p I is passed it is an
1086 /// instruction that prevents vectorization.  Otherwise \p TheLoop is used for
1087 /// the location of the remark.  \return the remark object that can be
1088 /// streamed to.
1089 static OptimizationRemarkAnalysis createLVAnalysis(const char *PassName,
1090     StringRef RemarkName, Loop *TheLoop, Instruction *I) {
1091   Value *CodeRegion = TheLoop->getHeader();
1092   DebugLoc DL = TheLoop->getStartLoc();
1093 
1094   if (I) {
1095     CodeRegion = I->getParent();
1096     // If there is no debug location attached to the instruction, revert back to
1097     // using the loop's.
1098     if (I->getDebugLoc())
1099       DL = I->getDebugLoc();
1100   }
1101 
1102   return OptimizationRemarkAnalysis(PassName, RemarkName, DL, CodeRegion);
1103 }
1104 
1105 /// Return a value for Step multiplied by VF.
1106 static Value *createStepForVF(IRBuilder<> &B, Constant *Step, ElementCount VF) {
1107   assert(isa<ConstantInt>(Step) && "Expected an integer step");
1108   Constant *StepVal = ConstantInt::get(
1109       Step->getType(),
1110       cast<ConstantInt>(Step)->getSExtValue() * VF.getKnownMinValue());
1111   return VF.isScalable() ? B.CreateVScale(StepVal) : StepVal;
1112 }
1113 
1114 namespace llvm {
1115 
1116 /// Return the runtime value for VF.
1117 Value *getRuntimeVF(IRBuilder<> &B, Type *Ty, ElementCount VF) {
1118   Constant *EC = ConstantInt::get(Ty, VF.getKnownMinValue());
1119   return VF.isScalable() ? B.CreateVScale(EC) : EC;
1120 }
1121 
1122 static Value *getRuntimeVFAsFloat(IRBuilder<> &B, Type *FTy, ElementCount VF) {
1123   assert(FTy->isFloatingPointTy() && "Expected floating point type!");
1124   Type *IntTy = IntegerType::get(FTy->getContext(), FTy->getScalarSizeInBits());
1125   Value *RuntimeVF = getRuntimeVF(B, IntTy, VF);
1126   return B.CreateUIToFP(RuntimeVF, FTy);
1127 }
1128 
1129 void reportVectorizationFailure(const StringRef DebugMsg,
1130                                 const StringRef OREMsg, const StringRef ORETag,
1131                                 OptimizationRemarkEmitter *ORE, Loop *TheLoop,
1132                                 Instruction *I) {
1133   LLVM_DEBUG(debugVectorizationMessage("Not vectorizing: ", DebugMsg, I));
1134   LoopVectorizeHints Hints(TheLoop, true /* doesn't matter */, *ORE);
1135   ORE->emit(
1136       createLVAnalysis(Hints.vectorizeAnalysisPassName(), ORETag, TheLoop, I)
1137       << "loop not vectorized: " << OREMsg);
1138 }
1139 
1140 void reportVectorizationInfo(const StringRef Msg, const StringRef ORETag,
1141                              OptimizationRemarkEmitter *ORE, Loop *TheLoop,
1142                              Instruction *I) {
1143   LLVM_DEBUG(debugVectorizationMessage("", Msg, I));
1144   LoopVectorizeHints Hints(TheLoop, true /* doesn't matter */, *ORE);
1145   ORE->emit(
1146       createLVAnalysis(Hints.vectorizeAnalysisPassName(), ORETag, TheLoop, I)
1147       << Msg);
1148 }
1149 
1150 } // end namespace llvm
1151 
1152 #ifndef NDEBUG
1153 /// \return string containing a file name and a line # for the given loop.
1154 static std::string getDebugLocString(const Loop *L) {
1155   std::string Result;
1156   if (L) {
1157     raw_string_ostream OS(Result);
1158     if (const DebugLoc LoopDbgLoc = L->getStartLoc())
1159       LoopDbgLoc.print(OS);
1160     else
1161       // Just print the module name.
1162       OS << L->getHeader()->getParent()->getParent()->getModuleIdentifier();
1163     OS.flush();
1164   }
1165   return Result;
1166 }
1167 #endif
1168 
1169 void InnerLoopVectorizer::addNewMetadata(Instruction *To,
1170                                          const Instruction *Orig) {
1171   // If the loop was versioned with memchecks, add the corresponding no-alias
1172   // metadata.
1173   if (LVer && (isa<LoadInst>(Orig) || isa<StoreInst>(Orig)))
1174     LVer->annotateInstWithNoAlias(To, Orig);
1175 }
1176 
1177 void InnerLoopVectorizer::addMetadata(Instruction *To,
1178                                       Instruction *From) {
1179   propagateMetadata(To, From);
1180   addNewMetadata(To, From);
1181 }
1182 
1183 void InnerLoopVectorizer::addMetadata(ArrayRef<Value *> To,
1184                                       Instruction *From) {
1185   for (Value *V : To) {
1186     if (Instruction *I = dyn_cast<Instruction>(V))
1187       addMetadata(I, From);
1188   }
1189 }
1190 
1191 namespace llvm {
1192 
1193 // Loop vectorization cost-model hints how the scalar epilogue loop should be
1194 // lowered.
1195 enum ScalarEpilogueLowering {
1196 
1197   // The default: allowing scalar epilogues.
1198   CM_ScalarEpilogueAllowed,
1199 
1200   // Vectorization with OptForSize: don't allow epilogues.
1201   CM_ScalarEpilogueNotAllowedOptSize,
1202 
1203   // A special case of vectorisation with OptForSize: loops with a very small
1204   // trip count are considered for vectorization under OptForSize, thereby
1205   // making sure the cost of their loop body is dominant, free of runtime
1206   // guards and scalar iteration overheads.
1207   CM_ScalarEpilogueNotAllowedLowTripLoop,
1208 
1209   // Loop hint predicate indicating an epilogue is undesired.
1210   CM_ScalarEpilogueNotNeededUsePredicate,
1211 
1212   // Directive indicating we must either tail fold or not vectorize
1213   CM_ScalarEpilogueNotAllowedUsePredicate
1214 };
1215 
1216 /// ElementCountComparator creates a total ordering for ElementCount
1217 /// for the purposes of using it in a set structure.
1218 struct ElementCountComparator {
1219   bool operator()(const ElementCount &LHS, const ElementCount &RHS) const {
1220     return std::make_tuple(LHS.isScalable(), LHS.getKnownMinValue()) <
1221            std::make_tuple(RHS.isScalable(), RHS.getKnownMinValue());
1222   }
1223 };
1224 using ElementCountSet = SmallSet<ElementCount, 16, ElementCountComparator>;
1225 
1226 /// LoopVectorizationCostModel - estimates the expected speedups due to
1227 /// vectorization.
1228 /// In many cases vectorization is not profitable. This can happen because of
1229 /// a number of reasons. In this class we mainly attempt to predict the
1230 /// expected speedup/slowdowns due to the supported instruction set. We use the
1231 /// TargetTransformInfo to query the different backends for the cost of
1232 /// different operations.
1233 class LoopVectorizationCostModel {
1234 public:
1235   LoopVectorizationCostModel(ScalarEpilogueLowering SEL, Loop *L,
1236                              PredicatedScalarEvolution &PSE, LoopInfo *LI,
1237                              LoopVectorizationLegality *Legal,
1238                              const TargetTransformInfo &TTI,
1239                              const TargetLibraryInfo *TLI, DemandedBits *DB,
1240                              AssumptionCache *AC,
1241                              OptimizationRemarkEmitter *ORE, const Function *F,
1242                              const LoopVectorizeHints *Hints,
1243                              InterleavedAccessInfo &IAI)
1244       : ScalarEpilogueStatus(SEL), TheLoop(L), PSE(PSE), LI(LI), Legal(Legal),
1245         TTI(TTI), TLI(TLI), DB(DB), AC(AC), ORE(ORE), TheFunction(F),
1246         Hints(Hints), InterleaveInfo(IAI) {}
1247 
1248   /// \return An upper bound for the vectorization factors (both fixed and
1249   /// scalable). If the factors are 0, vectorization and interleaving should be
1250   /// avoided up front.
1251   FixedScalableVFPair computeMaxVF(ElementCount UserVF, unsigned UserIC);
1252 
1253   /// \return True if runtime checks are required for vectorization, and false
1254   /// otherwise.
1255   bool runtimeChecksRequired();
1256 
1257   /// \return The most profitable vectorization factor and the cost of that VF.
1258   /// This method checks every VF in \p CandidateVFs. If UserVF is not ZERO
1259   /// then this vectorization factor will be selected if vectorization is
1260   /// possible.
1261   VectorizationFactor
1262   selectVectorizationFactor(const ElementCountSet &CandidateVFs);
1263 
1264   VectorizationFactor
1265   selectEpilogueVectorizationFactor(const ElementCount MaxVF,
1266                                     const LoopVectorizationPlanner &LVP);
1267 
1268   /// Setup cost-based decisions for user vectorization factor.
1269   /// \return true if the UserVF is a feasible VF to be chosen.
1270   bool selectUserVectorizationFactor(ElementCount UserVF) {
1271     collectUniformsAndScalars(UserVF);
1272     collectInstsToScalarize(UserVF);
1273     return expectedCost(UserVF).first.isValid();
1274   }
1275 
1276   /// \return The size (in bits) of the smallest and widest types in the code
1277   /// that needs to be vectorized. We ignore values that remain scalar such as
1278   /// 64 bit loop indices.
1279   std::pair<unsigned, unsigned> getSmallestAndWidestTypes();
1280 
1281   /// \return The desired interleave count.
1282   /// If interleave count has been specified by metadata it will be returned.
1283   /// Otherwise, the interleave count is computed and returned. VF and LoopCost
1284   /// are the selected vectorization factor and the cost of the selected VF.
1285   unsigned selectInterleaveCount(ElementCount VF, unsigned LoopCost);
1286 
1287   /// Memory access instruction may be vectorized in more than one way.
1288   /// Form of instruction after vectorization depends on cost.
1289   /// This function takes cost-based decisions for Load/Store instructions
1290   /// and collects them in a map. This decisions map is used for building
1291   /// the lists of loop-uniform and loop-scalar instructions.
1292   /// The calculated cost is saved with widening decision in order to
1293   /// avoid redundant calculations.
1294   void setCostBasedWideningDecision(ElementCount VF);
1295 
1296   /// A struct that represents some properties of the register usage
1297   /// of a loop.
1298   struct RegisterUsage {
1299     /// Holds the number of loop invariant values that are used in the loop.
1300     /// The key is ClassID of target-provided register class.
1301     SmallMapVector<unsigned, unsigned, 4> LoopInvariantRegs;
1302     /// Holds the maximum number of concurrent live intervals in the loop.
1303     /// The key is ClassID of target-provided register class.
1304     SmallMapVector<unsigned, unsigned, 4> MaxLocalUsers;
1305   };
1306 
1307   /// \return Returns information about the register usages of the loop for the
1308   /// given vectorization factors.
1309   SmallVector<RegisterUsage, 8>
1310   calculateRegisterUsage(ArrayRef<ElementCount> VFs);
1311 
1312   /// Collect values we want to ignore in the cost model.
1313   void collectValuesToIgnore();
1314 
1315   /// Collect all element types in the loop for which widening is needed.
1316   void collectElementTypesForWidening();
1317 
1318   /// Split reductions into those that happen in the loop, and those that happen
1319   /// outside. In loop reductions are collected into InLoopReductionChains.
1320   void collectInLoopReductions();
1321 
1322   /// Returns true if we should use strict in-order reductions for the given
1323   /// RdxDesc. This is true if the -enable-strict-reductions flag is passed,
1324   /// the IsOrdered flag of RdxDesc is set and we do not allow reordering
1325   /// of FP operations.
1326   bool useOrderedReductions(const RecurrenceDescriptor &RdxDesc) {
1327     return !Hints->allowReordering() && RdxDesc.isOrdered();
1328   }
1329 
1330   /// \returns The smallest bitwidth each instruction can be represented with.
1331   /// The vector equivalents of these instructions should be truncated to this
1332   /// type.
1333   const MapVector<Instruction *, uint64_t> &getMinimalBitwidths() const {
1334     return MinBWs;
1335   }
1336 
1337   /// \returns True if it is more profitable to scalarize instruction \p I for
1338   /// vectorization factor \p VF.
1339   bool isProfitableToScalarize(Instruction *I, ElementCount VF) const {
1340     assert(VF.isVector() &&
1341            "Profitable to scalarize relevant only for VF > 1.");
1342 
1343     // Cost model is not run in the VPlan-native path - return conservative
1344     // result until this changes.
1345     if (EnableVPlanNativePath)
1346       return false;
1347 
1348     auto Scalars = InstsToScalarize.find(VF);
1349     assert(Scalars != InstsToScalarize.end() &&
1350            "VF not yet analyzed for scalarization profitability");
1351     return Scalars->second.find(I) != Scalars->second.end();
1352   }
1353 
1354   /// Returns true if \p I is known to be uniform after vectorization.
1355   bool isUniformAfterVectorization(Instruction *I, ElementCount VF) const {
1356     if (VF.isScalar())
1357       return true;
1358 
1359     // Cost model is not run in the VPlan-native path - return conservative
1360     // result until this changes.
1361     if (EnableVPlanNativePath)
1362       return false;
1363 
1364     auto UniformsPerVF = Uniforms.find(VF);
1365     assert(UniformsPerVF != Uniforms.end() &&
1366            "VF not yet analyzed for uniformity");
1367     return UniformsPerVF->second.count(I);
1368   }
1369 
1370   /// Returns true if \p I is known to be scalar after vectorization.
1371   bool isScalarAfterVectorization(Instruction *I, ElementCount VF) const {
1372     if (VF.isScalar())
1373       return true;
1374 
1375     // Cost model is not run in the VPlan-native path - return conservative
1376     // result until this changes.
1377     if (EnableVPlanNativePath)
1378       return false;
1379 
1380     auto ScalarsPerVF = Scalars.find(VF);
1381     assert(ScalarsPerVF != Scalars.end() &&
1382            "Scalar values are not calculated for VF");
1383     return ScalarsPerVF->second.count(I);
1384   }
1385 
1386   /// \returns True if instruction \p I can be truncated to a smaller bitwidth
1387   /// for vectorization factor \p VF.
1388   bool canTruncateToMinimalBitwidth(Instruction *I, ElementCount VF) const {
1389     return VF.isVector() && MinBWs.find(I) != MinBWs.end() &&
1390            !isProfitableToScalarize(I, VF) &&
1391            !isScalarAfterVectorization(I, VF);
1392   }
1393 
1394   /// Decision that was taken during cost calculation for memory instruction.
1395   enum InstWidening {
1396     CM_Unknown,
1397     CM_Widen,         // For consecutive accesses with stride +1.
1398     CM_Widen_Reverse, // For consecutive accesses with stride -1.
1399     CM_Interleave,
1400     CM_GatherScatter,
1401     CM_Scalarize
1402   };
1403 
1404   /// Save vectorization decision \p W and \p Cost taken by the cost model for
1405   /// instruction \p I and vector width \p VF.
1406   void setWideningDecision(Instruction *I, ElementCount VF, InstWidening W,
1407                            InstructionCost Cost) {
1408     assert(VF.isVector() && "Expected VF >=2");
1409     WideningDecisions[std::make_pair(I, VF)] = std::make_pair(W, Cost);
1410   }
1411 
1412   /// Save vectorization decision \p W and \p Cost taken by the cost model for
1413   /// interleaving group \p Grp and vector width \p VF.
1414   void setWideningDecision(const InterleaveGroup<Instruction> *Grp,
1415                            ElementCount VF, InstWidening W,
1416                            InstructionCost Cost) {
1417     assert(VF.isVector() && "Expected VF >=2");
1418     /// Broadcast this decicion to all instructions inside the group.
1419     /// But the cost will be assigned to one instruction only.
1420     for (unsigned i = 0; i < Grp->getFactor(); ++i) {
1421       if (auto *I = Grp->getMember(i)) {
1422         if (Grp->getInsertPos() == I)
1423           WideningDecisions[std::make_pair(I, VF)] = std::make_pair(W, Cost);
1424         else
1425           WideningDecisions[std::make_pair(I, VF)] = std::make_pair(W, 0);
1426       }
1427     }
1428   }
1429 
1430   /// Return the cost model decision for the given instruction \p I and vector
1431   /// width \p VF. Return CM_Unknown if this instruction did not pass
1432   /// through the cost modeling.
1433   InstWidening getWideningDecision(Instruction *I, ElementCount VF) const {
1434     assert(VF.isVector() && "Expected VF to be a vector VF");
1435     // Cost model is not run in the VPlan-native path - return conservative
1436     // result until this changes.
1437     if (EnableVPlanNativePath)
1438       return CM_GatherScatter;
1439 
1440     std::pair<Instruction *, ElementCount> InstOnVF = std::make_pair(I, VF);
1441     auto Itr = WideningDecisions.find(InstOnVF);
1442     if (Itr == WideningDecisions.end())
1443       return CM_Unknown;
1444     return Itr->second.first;
1445   }
1446 
1447   /// Return the vectorization cost for the given instruction \p I and vector
1448   /// width \p VF.
1449   InstructionCost getWideningCost(Instruction *I, ElementCount VF) {
1450     assert(VF.isVector() && "Expected VF >=2");
1451     std::pair<Instruction *, ElementCount> InstOnVF = std::make_pair(I, VF);
1452     assert(WideningDecisions.find(InstOnVF) != WideningDecisions.end() &&
1453            "The cost is not calculated");
1454     return WideningDecisions[InstOnVF].second;
1455   }
1456 
1457   /// Return True if instruction \p I is an optimizable truncate whose operand
1458   /// is an induction variable. Such a truncate will be removed by adding a new
1459   /// induction variable with the destination type.
1460   bool isOptimizableIVTruncate(Instruction *I, ElementCount VF) {
1461     // If the instruction is not a truncate, return false.
1462     auto *Trunc = dyn_cast<TruncInst>(I);
1463     if (!Trunc)
1464       return false;
1465 
1466     // Get the source and destination types of the truncate.
1467     Type *SrcTy = ToVectorTy(cast<CastInst>(I)->getSrcTy(), VF);
1468     Type *DestTy = ToVectorTy(cast<CastInst>(I)->getDestTy(), VF);
1469 
1470     // If the truncate is free for the given types, return false. Replacing a
1471     // free truncate with an induction variable would add an induction variable
1472     // update instruction to each iteration of the loop. We exclude from this
1473     // check the primary induction variable since it will need an update
1474     // instruction regardless.
1475     Value *Op = Trunc->getOperand(0);
1476     if (Op != Legal->getPrimaryInduction() && TTI.isTruncateFree(SrcTy, DestTy))
1477       return false;
1478 
1479     // If the truncated value is not an induction variable, return false.
1480     return Legal->isInductionPhi(Op);
1481   }
1482 
1483   /// Collects the instructions to scalarize for each predicated instruction in
1484   /// the loop.
1485   void collectInstsToScalarize(ElementCount VF);
1486 
1487   /// Collect Uniform and Scalar values for the given \p VF.
1488   /// The sets depend on CM decision for Load/Store instructions
1489   /// that may be vectorized as interleave, gather-scatter or scalarized.
1490   void collectUniformsAndScalars(ElementCount VF) {
1491     // Do the analysis once.
1492     if (VF.isScalar() || Uniforms.find(VF) != Uniforms.end())
1493       return;
1494     setCostBasedWideningDecision(VF);
1495     collectLoopUniforms(VF);
1496     collectLoopScalars(VF);
1497   }
1498 
1499   /// Returns true if the target machine supports masked store operation
1500   /// for the given \p DataType and kind of access to \p Ptr.
1501   bool isLegalMaskedStore(Type *DataType, Value *Ptr, Align Alignment) const {
1502     return Legal->isConsecutivePtr(DataType, Ptr) &&
1503            TTI.isLegalMaskedStore(DataType, Alignment);
1504   }
1505 
1506   /// Returns true if the target machine supports masked load operation
1507   /// for the given \p DataType and kind of access to \p Ptr.
1508   bool isLegalMaskedLoad(Type *DataType, Value *Ptr, Align Alignment) const {
1509     return Legal->isConsecutivePtr(DataType, Ptr) &&
1510            TTI.isLegalMaskedLoad(DataType, Alignment);
1511   }
1512 
1513   /// Returns true if the target machine can represent \p V as a masked gather
1514   /// or scatter operation.
1515   bool isLegalGatherOrScatter(Value *V) {
1516     bool LI = isa<LoadInst>(V);
1517     bool SI = isa<StoreInst>(V);
1518     if (!LI && !SI)
1519       return false;
1520     auto *Ty = getLoadStoreType(V);
1521     Align Align = getLoadStoreAlignment(V);
1522     return (LI && TTI.isLegalMaskedGather(Ty, Align)) ||
1523            (SI && TTI.isLegalMaskedScatter(Ty, Align));
1524   }
1525 
1526   /// Returns true if the target machine supports all of the reduction
1527   /// variables found for the given VF.
1528   bool canVectorizeReductions(ElementCount VF) const {
1529     return (all_of(Legal->getReductionVars(), [&](auto &Reduction) -> bool {
1530       const RecurrenceDescriptor &RdxDesc = Reduction.second;
1531       return TTI.isLegalToVectorizeReduction(RdxDesc, VF);
1532     }));
1533   }
1534 
1535   /// Returns true if \p I is an instruction that will be scalarized with
1536   /// predication. Such instructions include conditional stores and
1537   /// instructions that may divide by zero.
1538   /// If a non-zero VF has been calculated, we check if I will be scalarized
1539   /// predication for that VF.
1540   bool isScalarWithPredication(Instruction *I) const;
1541 
1542   // Returns true if \p I is an instruction that will be predicated either
1543   // through scalar predication or masked load/store or masked gather/scatter.
1544   // Superset of instructions that return true for isScalarWithPredication.
1545   bool isPredicatedInst(Instruction *I) {
1546     if (!blockNeedsPredication(I->getParent()))
1547       return false;
1548     // Loads and stores that need some form of masked operation are predicated
1549     // instructions.
1550     if (isa<LoadInst>(I) || isa<StoreInst>(I))
1551       return Legal->isMaskRequired(I);
1552     return isScalarWithPredication(I);
1553   }
1554 
1555   /// Returns true if \p I is a memory instruction with consecutive memory
1556   /// access that can be widened.
1557   bool
1558   memoryInstructionCanBeWidened(Instruction *I,
1559                                 ElementCount VF = ElementCount::getFixed(1));
1560 
1561   /// Returns true if \p I is a memory instruction in an interleaved-group
1562   /// of memory accesses that can be vectorized with wide vector loads/stores
1563   /// and shuffles.
1564   bool
1565   interleavedAccessCanBeWidened(Instruction *I,
1566                                 ElementCount VF = ElementCount::getFixed(1));
1567 
1568   /// Check if \p Instr belongs to any interleaved access group.
1569   bool isAccessInterleaved(Instruction *Instr) {
1570     return InterleaveInfo.isInterleaved(Instr);
1571   }
1572 
1573   /// Get the interleaved access group that \p Instr belongs to.
1574   const InterleaveGroup<Instruction> *
1575   getInterleavedAccessGroup(Instruction *Instr) {
1576     return InterleaveInfo.getInterleaveGroup(Instr);
1577   }
1578 
1579   /// Returns true if we're required to use a scalar epilogue for at least
1580   /// the final iteration of the original loop.
1581   bool requiresScalarEpilogue(ElementCount VF) const {
1582     if (!isScalarEpilogueAllowed())
1583       return false;
1584     // If we might exit from anywhere but the latch, must run the exiting
1585     // iteration in scalar form.
1586     if (TheLoop->getExitingBlock() != TheLoop->getLoopLatch())
1587       return true;
1588     return VF.isVector() && InterleaveInfo.requiresScalarEpilogue();
1589   }
1590 
1591   /// Returns true if a scalar epilogue is not allowed due to optsize or a
1592   /// loop hint annotation.
1593   bool isScalarEpilogueAllowed() const {
1594     return ScalarEpilogueStatus == CM_ScalarEpilogueAllowed;
1595   }
1596 
1597   /// Returns true if all loop blocks should be masked to fold tail loop.
1598   bool foldTailByMasking() const { return FoldTailByMasking; }
1599 
1600   bool blockNeedsPredication(BasicBlock *BB) const {
1601     return foldTailByMasking() || Legal->blockNeedsPredication(BB);
1602   }
1603 
1604   /// A SmallMapVector to store the InLoop reduction op chains, mapping phi
1605   /// nodes to the chain of instructions representing the reductions. Uses a
1606   /// MapVector to ensure deterministic iteration order.
1607   using ReductionChainMap =
1608       SmallMapVector<PHINode *, SmallVector<Instruction *, 4>, 4>;
1609 
1610   /// Return the chain of instructions representing an inloop reduction.
1611   const ReductionChainMap &getInLoopReductionChains() const {
1612     return InLoopReductionChains;
1613   }
1614 
1615   /// Returns true if the Phi is part of an inloop reduction.
1616   bool isInLoopReduction(PHINode *Phi) const {
1617     return InLoopReductionChains.count(Phi);
1618   }
1619 
1620   /// Estimate cost of an intrinsic call instruction CI if it were vectorized
1621   /// with factor VF.  Return the cost of the instruction, including
1622   /// scalarization overhead if it's needed.
1623   InstructionCost getVectorIntrinsicCost(CallInst *CI, ElementCount VF) const;
1624 
1625   /// Estimate cost of a call instruction CI if it were vectorized with factor
1626   /// VF. Return the cost of the instruction, including scalarization overhead
1627   /// if it's needed. The flag NeedToScalarize shows if the call needs to be
1628   /// scalarized -
1629   /// i.e. either vector version isn't available, or is too expensive.
1630   InstructionCost getVectorCallCost(CallInst *CI, ElementCount VF,
1631                                     bool &NeedToScalarize) const;
1632 
1633   /// Returns true if the per-lane cost of VectorizationFactor A is lower than
1634   /// that of B.
1635   bool isMoreProfitable(const VectorizationFactor &A,
1636                         const VectorizationFactor &B) const;
1637 
1638   /// Invalidates decisions already taken by the cost model.
1639   void invalidateCostModelingDecisions() {
1640     WideningDecisions.clear();
1641     Uniforms.clear();
1642     Scalars.clear();
1643   }
1644 
1645 private:
1646   unsigned NumPredStores = 0;
1647 
1648   /// \return An upper bound for the vectorization factors for both
1649   /// fixed and scalable vectorization, where the minimum-known number of
1650   /// elements is a power-of-2 larger than zero. If scalable vectorization is
1651   /// disabled or unsupported, then the scalable part will be equal to
1652   /// ElementCount::getScalable(0).
1653   FixedScalableVFPair computeFeasibleMaxVF(unsigned ConstTripCount,
1654                                            ElementCount UserVF);
1655 
1656   /// \return the maximized element count based on the targets vector
1657   /// registers and the loop trip-count, but limited to a maximum safe VF.
1658   /// This is a helper function of computeFeasibleMaxVF.
1659   /// FIXME: MaxSafeVF is currently passed by reference to avoid some obscure
1660   /// issue that occurred on one of the buildbots which cannot be reproduced
1661   /// without having access to the properietary compiler (see comments on
1662   /// D98509). The issue is currently under investigation and this workaround
1663   /// will be removed as soon as possible.
1664   ElementCount getMaximizedVFForTarget(unsigned ConstTripCount,
1665                                        unsigned SmallestType,
1666                                        unsigned WidestType,
1667                                        const ElementCount &MaxSafeVF);
1668 
1669   /// \return the maximum legal scalable VF, based on the safe max number
1670   /// of elements.
1671   ElementCount getMaxLegalScalableVF(unsigned MaxSafeElements);
1672 
1673   /// The vectorization cost is a combination of the cost itself and a boolean
1674   /// indicating whether any of the contributing operations will actually
1675   /// operate on vector values after type legalization in the backend. If this
1676   /// latter value is false, then all operations will be scalarized (i.e. no
1677   /// vectorization has actually taken place).
1678   using VectorizationCostTy = std::pair<InstructionCost, bool>;
1679 
1680   /// Returns the expected execution cost. The unit of the cost does
1681   /// not matter because we use the 'cost' units to compare different
1682   /// vector widths. The cost that is returned is *not* normalized by
1683   /// the factor width. If \p Invalid is not nullptr, this function
1684   /// will add a pair(Instruction*, ElementCount) to \p Invalid for
1685   /// each instruction that has an Invalid cost for the given VF.
1686   using InstructionVFPair = std::pair<Instruction *, ElementCount>;
1687   VectorizationCostTy
1688   expectedCost(ElementCount VF,
1689                SmallVectorImpl<InstructionVFPair> *Invalid = nullptr);
1690 
1691   /// Returns the execution time cost of an instruction for a given vector
1692   /// width. Vector width of one means scalar.
1693   VectorizationCostTy getInstructionCost(Instruction *I, ElementCount VF);
1694 
1695   /// The cost-computation logic from getInstructionCost which provides
1696   /// the vector type as an output parameter.
1697   InstructionCost getInstructionCost(Instruction *I, ElementCount VF,
1698                                      Type *&VectorTy);
1699 
1700   /// Return the cost of instructions in an inloop reduction pattern, if I is
1701   /// part of that pattern.
1702   Optional<InstructionCost>
1703   getReductionPatternCost(Instruction *I, ElementCount VF, Type *VectorTy,
1704                           TTI::TargetCostKind CostKind);
1705 
1706   /// Calculate vectorization cost of memory instruction \p I.
1707   InstructionCost getMemoryInstructionCost(Instruction *I, ElementCount VF);
1708 
1709   /// The cost computation for scalarized memory instruction.
1710   InstructionCost getMemInstScalarizationCost(Instruction *I, ElementCount VF);
1711 
1712   /// The cost computation for interleaving group of memory instructions.
1713   InstructionCost getInterleaveGroupCost(Instruction *I, ElementCount VF);
1714 
1715   /// The cost computation for Gather/Scatter instruction.
1716   InstructionCost getGatherScatterCost(Instruction *I, ElementCount VF);
1717 
1718   /// The cost computation for widening instruction \p I with consecutive
1719   /// memory access.
1720   InstructionCost getConsecutiveMemOpCost(Instruction *I, ElementCount VF);
1721 
1722   /// The cost calculation for Load/Store instruction \p I with uniform pointer -
1723   /// Load: scalar load + broadcast.
1724   /// Store: scalar store + (loop invariant value stored? 0 : extract of last
1725   /// element)
1726   InstructionCost getUniformMemOpCost(Instruction *I, ElementCount VF);
1727 
1728   /// Estimate the overhead of scalarizing an instruction. This is a
1729   /// convenience wrapper for the type-based getScalarizationOverhead API.
1730   InstructionCost getScalarizationOverhead(Instruction *I,
1731                                            ElementCount VF) const;
1732 
1733   /// Returns whether the instruction is a load or store and will be a emitted
1734   /// as a vector operation.
1735   bool isConsecutiveLoadOrStore(Instruction *I);
1736 
1737   /// Returns true if an artificially high cost for emulated masked memrefs
1738   /// should be used.
1739   bool useEmulatedMaskMemRefHack(Instruction *I);
1740 
1741   /// Map of scalar integer values to the smallest bitwidth they can be legally
1742   /// represented as. The vector equivalents of these values should be truncated
1743   /// to this type.
1744   MapVector<Instruction *, uint64_t> MinBWs;
1745 
1746   /// A type representing the costs for instructions if they were to be
1747   /// scalarized rather than vectorized. The entries are Instruction-Cost
1748   /// pairs.
1749   using ScalarCostsTy = DenseMap<Instruction *, InstructionCost>;
1750 
1751   /// A set containing all BasicBlocks that are known to present after
1752   /// vectorization as a predicated block.
1753   SmallPtrSet<BasicBlock *, 4> PredicatedBBsAfterVectorization;
1754 
1755   /// Records whether it is allowed to have the original scalar loop execute at
1756   /// least once. This may be needed as a fallback loop in case runtime
1757   /// aliasing/dependence checks fail, or to handle the tail/remainder
1758   /// iterations when the trip count is unknown or doesn't divide by the VF,
1759   /// or as a peel-loop to handle gaps in interleave-groups.
1760   /// Under optsize and when the trip count is very small we don't allow any
1761   /// iterations to execute in the scalar loop.
1762   ScalarEpilogueLowering ScalarEpilogueStatus = CM_ScalarEpilogueAllowed;
1763 
1764   /// All blocks of loop are to be masked to fold tail of scalar iterations.
1765   bool FoldTailByMasking = false;
1766 
1767   /// A map holding scalar costs for different vectorization factors. The
1768   /// presence of a cost for an instruction in the mapping indicates that the
1769   /// instruction will be scalarized when vectorizing with the associated
1770   /// vectorization factor. The entries are VF-ScalarCostTy pairs.
1771   DenseMap<ElementCount, ScalarCostsTy> InstsToScalarize;
1772 
1773   /// Holds the instructions known to be uniform after vectorization.
1774   /// The data is collected per VF.
1775   DenseMap<ElementCount, SmallPtrSet<Instruction *, 4>> Uniforms;
1776 
1777   /// Holds the instructions known to be scalar after vectorization.
1778   /// The data is collected per VF.
1779   DenseMap<ElementCount, SmallPtrSet<Instruction *, 4>> Scalars;
1780 
1781   /// Holds the instructions (address computations) that are forced to be
1782   /// scalarized.
1783   DenseMap<ElementCount, SmallPtrSet<Instruction *, 4>> ForcedScalars;
1784 
1785   /// PHINodes of the reductions that should be expanded in-loop along with
1786   /// their associated chains of reduction operations, in program order from top
1787   /// (PHI) to bottom
1788   ReductionChainMap InLoopReductionChains;
1789 
1790   /// A Map of inloop reduction operations and their immediate chain operand.
1791   /// FIXME: This can be removed once reductions can be costed correctly in
1792   /// vplan. This was added to allow quick lookup to the inloop operations,
1793   /// without having to loop through InLoopReductionChains.
1794   DenseMap<Instruction *, Instruction *> InLoopReductionImmediateChains;
1795 
1796   /// Returns the expected difference in cost from scalarizing the expression
1797   /// feeding a predicated instruction \p PredInst. The instructions to
1798   /// scalarize and their scalar costs are collected in \p ScalarCosts. A
1799   /// non-negative return value implies the expression will be scalarized.
1800   /// Currently, only single-use chains are considered for scalarization.
1801   int computePredInstDiscount(Instruction *PredInst, ScalarCostsTy &ScalarCosts,
1802                               ElementCount VF);
1803 
1804   /// Collect the instructions that are uniform after vectorization. An
1805   /// instruction is uniform if we represent it with a single scalar value in
1806   /// the vectorized loop corresponding to each vector iteration. Examples of
1807   /// uniform instructions include pointer operands of consecutive or
1808   /// interleaved memory accesses. Note that although uniformity implies an
1809   /// instruction will be scalar, the reverse is not true. In general, a
1810   /// scalarized instruction will be represented by VF scalar values in the
1811   /// vectorized loop, each corresponding to an iteration of the original
1812   /// scalar loop.
1813   void collectLoopUniforms(ElementCount VF);
1814 
1815   /// Collect the instructions that are scalar after vectorization. An
1816   /// instruction is scalar if it is known to be uniform or will be scalarized
1817   /// during vectorization. Non-uniform scalarized instructions will be
1818   /// represented by VF values in the vectorized loop, each corresponding to an
1819   /// iteration of the original scalar loop.
1820   void collectLoopScalars(ElementCount VF);
1821 
1822   /// Keeps cost model vectorization decision and cost for instructions.
1823   /// Right now it is used for memory instructions only.
1824   using DecisionList = DenseMap<std::pair<Instruction *, ElementCount>,
1825                                 std::pair<InstWidening, InstructionCost>>;
1826 
1827   DecisionList WideningDecisions;
1828 
1829   /// Returns true if \p V is expected to be vectorized and it needs to be
1830   /// extracted.
1831   bool needsExtract(Value *V, ElementCount VF) const {
1832     Instruction *I = dyn_cast<Instruction>(V);
1833     if (VF.isScalar() || !I || !TheLoop->contains(I) ||
1834         TheLoop->isLoopInvariant(I))
1835       return false;
1836 
1837     // Assume we can vectorize V (and hence we need extraction) if the
1838     // scalars are not computed yet. This can happen, because it is called
1839     // via getScalarizationOverhead from setCostBasedWideningDecision, before
1840     // the scalars are collected. That should be a safe assumption in most
1841     // cases, because we check if the operands have vectorizable types
1842     // beforehand in LoopVectorizationLegality.
1843     return Scalars.find(VF) == Scalars.end() ||
1844            !isScalarAfterVectorization(I, VF);
1845   };
1846 
1847   /// Returns a range containing only operands needing to be extracted.
1848   SmallVector<Value *, 4> filterExtractingOperands(Instruction::op_range Ops,
1849                                                    ElementCount VF) const {
1850     return SmallVector<Value *, 4>(make_filter_range(
1851         Ops, [this, VF](Value *V) { return this->needsExtract(V, VF); }));
1852   }
1853 
1854   /// Determines if we have the infrastructure to vectorize loop \p L and its
1855   /// epilogue, assuming the main loop is vectorized by \p VF.
1856   bool isCandidateForEpilogueVectorization(const Loop &L,
1857                                            const ElementCount VF) const;
1858 
1859   /// Returns true if epilogue vectorization is considered profitable, and
1860   /// false otherwise.
1861   /// \p VF is the vectorization factor chosen for the original loop.
1862   bool isEpilogueVectorizationProfitable(const ElementCount VF) const;
1863 
1864 public:
1865   /// The loop that we evaluate.
1866   Loop *TheLoop;
1867 
1868   /// Predicated scalar evolution analysis.
1869   PredicatedScalarEvolution &PSE;
1870 
1871   /// Loop Info analysis.
1872   LoopInfo *LI;
1873 
1874   /// Vectorization legality.
1875   LoopVectorizationLegality *Legal;
1876 
1877   /// Vector target information.
1878   const TargetTransformInfo &TTI;
1879 
1880   /// Target Library Info.
1881   const TargetLibraryInfo *TLI;
1882 
1883   /// Demanded bits analysis.
1884   DemandedBits *DB;
1885 
1886   /// Assumption cache.
1887   AssumptionCache *AC;
1888 
1889   /// Interface to emit optimization remarks.
1890   OptimizationRemarkEmitter *ORE;
1891 
1892   const Function *TheFunction;
1893 
1894   /// Loop Vectorize Hint.
1895   const LoopVectorizeHints *Hints;
1896 
1897   /// The interleave access information contains groups of interleaved accesses
1898   /// with the same stride and close to each other.
1899   InterleavedAccessInfo &InterleaveInfo;
1900 
1901   /// Values to ignore in the cost model.
1902   SmallPtrSet<const Value *, 16> ValuesToIgnore;
1903 
1904   /// Values to ignore in the cost model when VF > 1.
1905   SmallPtrSet<const Value *, 16> VecValuesToIgnore;
1906 
1907   /// All element types found in the loop.
1908   SmallPtrSet<Type *, 16> ElementTypesInLoop;
1909 
1910   /// Profitable vector factors.
1911   SmallVector<VectorizationFactor, 8> ProfitableVFs;
1912 };
1913 } // end namespace llvm
1914 
1915 /// Helper struct to manage generating runtime checks for vectorization.
1916 ///
1917 /// The runtime checks are created up-front in temporary blocks to allow better
1918 /// estimating the cost and un-linked from the existing IR. After deciding to
1919 /// vectorize, the checks are moved back. If deciding not to vectorize, the
1920 /// temporary blocks are completely removed.
1921 class GeneratedRTChecks {
1922   /// Basic block which contains the generated SCEV checks, if any.
1923   BasicBlock *SCEVCheckBlock = nullptr;
1924 
1925   /// The value representing the result of the generated SCEV checks. If it is
1926   /// nullptr, either no SCEV checks have been generated or they have been used.
1927   Value *SCEVCheckCond = nullptr;
1928 
1929   /// Basic block which contains the generated memory runtime checks, if any.
1930   BasicBlock *MemCheckBlock = nullptr;
1931 
1932   /// The value representing the result of the generated memory runtime checks.
1933   /// If it is nullptr, either no memory runtime checks have been generated or
1934   /// they have been used.
1935   Value *MemRuntimeCheckCond = nullptr;
1936 
1937   DominatorTree *DT;
1938   LoopInfo *LI;
1939 
1940   SCEVExpander SCEVExp;
1941   SCEVExpander MemCheckExp;
1942 
1943 public:
1944   GeneratedRTChecks(ScalarEvolution &SE, DominatorTree *DT, LoopInfo *LI,
1945                     const DataLayout &DL)
1946       : DT(DT), LI(LI), SCEVExp(SE, DL, "scev.check"),
1947         MemCheckExp(SE, DL, "scev.check") {}
1948 
1949   /// Generate runtime checks in SCEVCheckBlock and MemCheckBlock, so we can
1950   /// accurately estimate the cost of the runtime checks. The blocks are
1951   /// un-linked from the IR and is added back during vector code generation. If
1952   /// there is no vector code generation, the check blocks are removed
1953   /// completely.
1954   void Create(Loop *L, const LoopAccessInfo &LAI,
1955               const SCEVUnionPredicate &UnionPred) {
1956 
1957     BasicBlock *LoopHeader = L->getHeader();
1958     BasicBlock *Preheader = L->getLoopPreheader();
1959 
1960     // Use SplitBlock to create blocks for SCEV & memory runtime checks to
1961     // ensure the blocks are properly added to LoopInfo & DominatorTree. Those
1962     // may be used by SCEVExpander. The blocks will be un-linked from their
1963     // predecessors and removed from LI & DT at the end of the function.
1964     if (!UnionPred.isAlwaysTrue()) {
1965       SCEVCheckBlock = SplitBlock(Preheader, Preheader->getTerminator(), DT, LI,
1966                                   nullptr, "vector.scevcheck");
1967 
1968       SCEVCheckCond = SCEVExp.expandCodeForPredicate(
1969           &UnionPred, SCEVCheckBlock->getTerminator());
1970     }
1971 
1972     const auto &RtPtrChecking = *LAI.getRuntimePointerChecking();
1973     if (RtPtrChecking.Need) {
1974       auto *Pred = SCEVCheckBlock ? SCEVCheckBlock : Preheader;
1975       MemCheckBlock = SplitBlock(Pred, Pred->getTerminator(), DT, LI, nullptr,
1976                                  "vector.memcheck");
1977 
1978       MemRuntimeCheckCond =
1979           addRuntimeChecks(MemCheckBlock->getTerminator(), L,
1980                            RtPtrChecking.getChecks(), MemCheckExp);
1981       assert(MemRuntimeCheckCond &&
1982              "no RT checks generated although RtPtrChecking "
1983              "claimed checks are required");
1984     }
1985 
1986     if (!MemCheckBlock && !SCEVCheckBlock)
1987       return;
1988 
1989     // Unhook the temporary block with the checks, update various places
1990     // accordingly.
1991     if (SCEVCheckBlock)
1992       SCEVCheckBlock->replaceAllUsesWith(Preheader);
1993     if (MemCheckBlock)
1994       MemCheckBlock->replaceAllUsesWith(Preheader);
1995 
1996     if (SCEVCheckBlock) {
1997       SCEVCheckBlock->getTerminator()->moveBefore(Preheader->getTerminator());
1998       new UnreachableInst(Preheader->getContext(), SCEVCheckBlock);
1999       Preheader->getTerminator()->eraseFromParent();
2000     }
2001     if (MemCheckBlock) {
2002       MemCheckBlock->getTerminator()->moveBefore(Preheader->getTerminator());
2003       new UnreachableInst(Preheader->getContext(), MemCheckBlock);
2004       Preheader->getTerminator()->eraseFromParent();
2005     }
2006 
2007     DT->changeImmediateDominator(LoopHeader, Preheader);
2008     if (MemCheckBlock) {
2009       DT->eraseNode(MemCheckBlock);
2010       LI->removeBlock(MemCheckBlock);
2011     }
2012     if (SCEVCheckBlock) {
2013       DT->eraseNode(SCEVCheckBlock);
2014       LI->removeBlock(SCEVCheckBlock);
2015     }
2016   }
2017 
2018   /// Remove the created SCEV & memory runtime check blocks & instructions, if
2019   /// unused.
2020   ~GeneratedRTChecks() {
2021     SCEVExpanderCleaner SCEVCleaner(SCEVExp, *DT);
2022     SCEVExpanderCleaner MemCheckCleaner(MemCheckExp, *DT);
2023     if (!SCEVCheckCond)
2024       SCEVCleaner.markResultUsed();
2025 
2026     if (!MemRuntimeCheckCond)
2027       MemCheckCleaner.markResultUsed();
2028 
2029     if (MemRuntimeCheckCond) {
2030       auto &SE = *MemCheckExp.getSE();
2031       // Memory runtime check generation creates compares that use expanded
2032       // values. Remove them before running the SCEVExpanderCleaners.
2033       for (auto &I : make_early_inc_range(reverse(*MemCheckBlock))) {
2034         if (MemCheckExp.isInsertedInstruction(&I))
2035           continue;
2036         SE.forgetValue(&I);
2037         SE.eraseValueFromMap(&I);
2038         I.eraseFromParent();
2039       }
2040     }
2041     MemCheckCleaner.cleanup();
2042     SCEVCleaner.cleanup();
2043 
2044     if (SCEVCheckCond)
2045       SCEVCheckBlock->eraseFromParent();
2046     if (MemRuntimeCheckCond)
2047       MemCheckBlock->eraseFromParent();
2048   }
2049 
2050   /// Adds the generated SCEVCheckBlock before \p LoopVectorPreHeader and
2051   /// adjusts the branches to branch to the vector preheader or \p Bypass,
2052   /// depending on the generated condition.
2053   BasicBlock *emitSCEVChecks(Loop *L, BasicBlock *Bypass,
2054                              BasicBlock *LoopVectorPreHeader,
2055                              BasicBlock *LoopExitBlock) {
2056     if (!SCEVCheckCond)
2057       return nullptr;
2058     if (auto *C = dyn_cast<ConstantInt>(SCEVCheckCond))
2059       if (C->isZero())
2060         return nullptr;
2061 
2062     auto *Pred = LoopVectorPreHeader->getSinglePredecessor();
2063 
2064     BranchInst::Create(LoopVectorPreHeader, SCEVCheckBlock);
2065     // Create new preheader for vector loop.
2066     if (auto *PL = LI->getLoopFor(LoopVectorPreHeader))
2067       PL->addBasicBlockToLoop(SCEVCheckBlock, *LI);
2068 
2069     SCEVCheckBlock->getTerminator()->eraseFromParent();
2070     SCEVCheckBlock->moveBefore(LoopVectorPreHeader);
2071     Pred->getTerminator()->replaceSuccessorWith(LoopVectorPreHeader,
2072                                                 SCEVCheckBlock);
2073 
2074     DT->addNewBlock(SCEVCheckBlock, Pred);
2075     DT->changeImmediateDominator(LoopVectorPreHeader, SCEVCheckBlock);
2076 
2077     ReplaceInstWithInst(
2078         SCEVCheckBlock->getTerminator(),
2079         BranchInst::Create(Bypass, LoopVectorPreHeader, SCEVCheckCond));
2080     // Mark the check as used, to prevent it from being removed during cleanup.
2081     SCEVCheckCond = nullptr;
2082     return SCEVCheckBlock;
2083   }
2084 
2085   /// Adds the generated MemCheckBlock before \p LoopVectorPreHeader and adjusts
2086   /// the branches to branch to the vector preheader or \p Bypass, depending on
2087   /// the generated condition.
2088   BasicBlock *emitMemRuntimeChecks(Loop *L, BasicBlock *Bypass,
2089                                    BasicBlock *LoopVectorPreHeader) {
2090     // Check if we generated code that checks in runtime if arrays overlap.
2091     if (!MemRuntimeCheckCond)
2092       return nullptr;
2093 
2094     auto *Pred = LoopVectorPreHeader->getSinglePredecessor();
2095     Pred->getTerminator()->replaceSuccessorWith(LoopVectorPreHeader,
2096                                                 MemCheckBlock);
2097 
2098     DT->addNewBlock(MemCheckBlock, Pred);
2099     DT->changeImmediateDominator(LoopVectorPreHeader, MemCheckBlock);
2100     MemCheckBlock->moveBefore(LoopVectorPreHeader);
2101 
2102     if (auto *PL = LI->getLoopFor(LoopVectorPreHeader))
2103       PL->addBasicBlockToLoop(MemCheckBlock, *LI);
2104 
2105     ReplaceInstWithInst(
2106         MemCheckBlock->getTerminator(),
2107         BranchInst::Create(Bypass, LoopVectorPreHeader, MemRuntimeCheckCond));
2108     MemCheckBlock->getTerminator()->setDebugLoc(
2109         Pred->getTerminator()->getDebugLoc());
2110 
2111     // Mark the check as used, to prevent it from being removed during cleanup.
2112     MemRuntimeCheckCond = nullptr;
2113     return MemCheckBlock;
2114   }
2115 };
2116 
2117 // Return true if \p OuterLp is an outer loop annotated with hints for explicit
2118 // vectorization. The loop needs to be annotated with #pragma omp simd
2119 // simdlen(#) or #pragma clang vectorize(enable) vectorize_width(#). If the
2120 // vector length information is not provided, vectorization is not considered
2121 // explicit. Interleave hints are not allowed either. These limitations will be
2122 // relaxed in the future.
2123 // Please, note that we are currently forced to abuse the pragma 'clang
2124 // vectorize' semantics. This pragma provides *auto-vectorization hints*
2125 // (i.e., LV must check that vectorization is legal) whereas pragma 'omp simd'
2126 // provides *explicit vectorization hints* (LV can bypass legal checks and
2127 // assume that vectorization is legal). However, both hints are implemented
2128 // using the same metadata (llvm.loop.vectorize, processed by
2129 // LoopVectorizeHints). This will be fixed in the future when the native IR
2130 // representation for pragma 'omp simd' is introduced.
2131 static bool isExplicitVecOuterLoop(Loop *OuterLp,
2132                                    OptimizationRemarkEmitter *ORE) {
2133   assert(!OuterLp->isInnermost() && "This is not an outer loop");
2134   LoopVectorizeHints Hints(OuterLp, true /*DisableInterleaving*/, *ORE);
2135 
2136   // Only outer loops with an explicit vectorization hint are supported.
2137   // Unannotated outer loops are ignored.
2138   if (Hints.getForce() == LoopVectorizeHints::FK_Undefined)
2139     return false;
2140 
2141   Function *Fn = OuterLp->getHeader()->getParent();
2142   if (!Hints.allowVectorization(Fn, OuterLp,
2143                                 true /*VectorizeOnlyWhenForced*/)) {
2144     LLVM_DEBUG(dbgs() << "LV: Loop hints prevent outer loop vectorization.\n");
2145     return false;
2146   }
2147 
2148   if (Hints.getInterleave() > 1) {
2149     // TODO: Interleave support is future work.
2150     LLVM_DEBUG(dbgs() << "LV: Not vectorizing: Interleave is not supported for "
2151                          "outer loops.\n");
2152     Hints.emitRemarkWithHints();
2153     return false;
2154   }
2155 
2156   return true;
2157 }
2158 
2159 static void collectSupportedLoops(Loop &L, LoopInfo *LI,
2160                                   OptimizationRemarkEmitter *ORE,
2161                                   SmallVectorImpl<Loop *> &V) {
2162   // Collect inner loops and outer loops without irreducible control flow. For
2163   // now, only collect outer loops that have explicit vectorization hints. If we
2164   // are stress testing the VPlan H-CFG construction, we collect the outermost
2165   // loop of every loop nest.
2166   if (L.isInnermost() || VPlanBuildStressTest ||
2167       (EnableVPlanNativePath && isExplicitVecOuterLoop(&L, ORE))) {
2168     LoopBlocksRPO RPOT(&L);
2169     RPOT.perform(LI);
2170     if (!containsIrreducibleCFG<const BasicBlock *>(RPOT, *LI)) {
2171       V.push_back(&L);
2172       // TODO: Collect inner loops inside marked outer loops in case
2173       // vectorization fails for the outer loop. Do not invoke
2174       // 'containsIrreducibleCFG' again for inner loops when the outer loop is
2175       // already known to be reducible. We can use an inherited attribute for
2176       // that.
2177       return;
2178     }
2179   }
2180   for (Loop *InnerL : L)
2181     collectSupportedLoops(*InnerL, LI, ORE, V);
2182 }
2183 
2184 namespace {
2185 
2186 /// The LoopVectorize Pass.
2187 struct LoopVectorize : public FunctionPass {
2188   /// Pass identification, replacement for typeid
2189   static char ID;
2190 
2191   LoopVectorizePass Impl;
2192 
2193   explicit LoopVectorize(bool InterleaveOnlyWhenForced = false,
2194                          bool VectorizeOnlyWhenForced = false)
2195       : FunctionPass(ID),
2196         Impl({InterleaveOnlyWhenForced, VectorizeOnlyWhenForced}) {
2197     initializeLoopVectorizePass(*PassRegistry::getPassRegistry());
2198   }
2199 
2200   bool runOnFunction(Function &F) override {
2201     if (skipFunction(F))
2202       return false;
2203 
2204     auto *SE = &getAnalysis<ScalarEvolutionWrapperPass>().getSE();
2205     auto *LI = &getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
2206     auto *TTI = &getAnalysis<TargetTransformInfoWrapperPass>().getTTI(F);
2207     auto *DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree();
2208     auto *BFI = &getAnalysis<BlockFrequencyInfoWrapperPass>().getBFI();
2209     auto *TLIP = getAnalysisIfAvailable<TargetLibraryInfoWrapperPass>();
2210     auto *TLI = TLIP ? &TLIP->getTLI(F) : nullptr;
2211     auto *AA = &getAnalysis<AAResultsWrapperPass>().getAAResults();
2212     auto *AC = &getAnalysis<AssumptionCacheTracker>().getAssumptionCache(F);
2213     auto *LAA = &getAnalysis<LoopAccessLegacyAnalysis>();
2214     auto *DB = &getAnalysis<DemandedBitsWrapperPass>().getDemandedBits();
2215     auto *ORE = &getAnalysis<OptimizationRemarkEmitterWrapperPass>().getORE();
2216     auto *PSI = &getAnalysis<ProfileSummaryInfoWrapperPass>().getPSI();
2217 
2218     std::function<const LoopAccessInfo &(Loop &)> GetLAA =
2219         [&](Loop &L) -> const LoopAccessInfo & { return LAA->getInfo(&L); };
2220 
2221     return Impl.runImpl(F, *SE, *LI, *TTI, *DT, *BFI, TLI, *DB, *AA, *AC,
2222                         GetLAA, *ORE, PSI).MadeAnyChange;
2223   }
2224 
2225   void getAnalysisUsage(AnalysisUsage &AU) const override {
2226     AU.addRequired<AssumptionCacheTracker>();
2227     AU.addRequired<BlockFrequencyInfoWrapperPass>();
2228     AU.addRequired<DominatorTreeWrapperPass>();
2229     AU.addRequired<LoopInfoWrapperPass>();
2230     AU.addRequired<ScalarEvolutionWrapperPass>();
2231     AU.addRequired<TargetTransformInfoWrapperPass>();
2232     AU.addRequired<AAResultsWrapperPass>();
2233     AU.addRequired<LoopAccessLegacyAnalysis>();
2234     AU.addRequired<DemandedBitsWrapperPass>();
2235     AU.addRequired<OptimizationRemarkEmitterWrapperPass>();
2236     AU.addRequired<InjectTLIMappingsLegacy>();
2237 
2238     // We currently do not preserve loopinfo/dominator analyses with outer loop
2239     // vectorization. Until this is addressed, mark these analyses as preserved
2240     // only for non-VPlan-native path.
2241     // TODO: Preserve Loop and Dominator analyses for VPlan-native path.
2242     if (!EnableVPlanNativePath) {
2243       AU.addPreserved<LoopInfoWrapperPass>();
2244       AU.addPreserved<DominatorTreeWrapperPass>();
2245     }
2246 
2247     AU.addPreserved<BasicAAWrapperPass>();
2248     AU.addPreserved<GlobalsAAWrapperPass>();
2249     AU.addRequired<ProfileSummaryInfoWrapperPass>();
2250   }
2251 };
2252 
2253 } // end anonymous namespace
2254 
2255 //===----------------------------------------------------------------------===//
2256 // Implementation of LoopVectorizationLegality, InnerLoopVectorizer and
2257 // LoopVectorizationCostModel and LoopVectorizationPlanner.
2258 //===----------------------------------------------------------------------===//
2259 
2260 Value *InnerLoopVectorizer::getBroadcastInstrs(Value *V) {
2261   // We need to place the broadcast of invariant variables outside the loop,
2262   // but only if it's proven safe to do so. Else, broadcast will be inside
2263   // vector loop body.
2264   Instruction *Instr = dyn_cast<Instruction>(V);
2265   bool SafeToHoist = OrigLoop->isLoopInvariant(V) &&
2266                      (!Instr ||
2267                       DT->dominates(Instr->getParent(), LoopVectorPreHeader));
2268   // Place the code for broadcasting invariant variables in the new preheader.
2269   IRBuilder<>::InsertPointGuard Guard(Builder);
2270   if (SafeToHoist)
2271     Builder.SetInsertPoint(LoopVectorPreHeader->getTerminator());
2272 
2273   // Broadcast the scalar into all locations in the vector.
2274   Value *Shuf = Builder.CreateVectorSplat(VF, V, "broadcast");
2275 
2276   return Shuf;
2277 }
2278 
2279 void InnerLoopVectorizer::createVectorIntOrFpInductionPHI(
2280     const InductionDescriptor &II, Value *Step, Value *Start,
2281     Instruction *EntryVal, VPValue *Def, VPValue *CastDef,
2282     VPTransformState &State) {
2283   assert((isa<PHINode>(EntryVal) || isa<TruncInst>(EntryVal)) &&
2284          "Expected either an induction phi-node or a truncate of it!");
2285 
2286   // Construct the initial value of the vector IV in the vector loop preheader
2287   auto CurrIP = Builder.saveIP();
2288   Builder.SetInsertPoint(LoopVectorPreHeader->getTerminator());
2289   if (isa<TruncInst>(EntryVal)) {
2290     assert(Start->getType()->isIntegerTy() &&
2291            "Truncation requires an integer type");
2292     auto *TruncType = cast<IntegerType>(EntryVal->getType());
2293     Step = Builder.CreateTrunc(Step, TruncType);
2294     Start = Builder.CreateCast(Instruction::Trunc, Start, TruncType);
2295   }
2296 
2297   Value *Zero = getSignedIntOrFpConstant(Start->getType(), 0);
2298   Value *SplatStart = Builder.CreateVectorSplat(VF, Start);
2299   Value *SteppedStart =
2300       getStepVector(SplatStart, Zero, Step, II.getInductionOpcode());
2301 
2302   // We create vector phi nodes for both integer and floating-point induction
2303   // variables. Here, we determine the kind of arithmetic we will perform.
2304   Instruction::BinaryOps AddOp;
2305   Instruction::BinaryOps MulOp;
2306   if (Step->getType()->isIntegerTy()) {
2307     AddOp = Instruction::Add;
2308     MulOp = Instruction::Mul;
2309   } else {
2310     AddOp = II.getInductionOpcode();
2311     MulOp = Instruction::FMul;
2312   }
2313 
2314   // Multiply the vectorization factor by the step using integer or
2315   // floating-point arithmetic as appropriate.
2316   Type *StepType = Step->getType();
2317   Value *RuntimeVF;
2318   if (Step->getType()->isFloatingPointTy())
2319     RuntimeVF = getRuntimeVFAsFloat(Builder, StepType, VF);
2320   else
2321     RuntimeVF = getRuntimeVF(Builder, StepType, VF);
2322   Value *Mul = Builder.CreateBinOp(MulOp, Step, RuntimeVF);
2323 
2324   // Create a vector splat to use in the induction update.
2325   //
2326   // FIXME: If the step is non-constant, we create the vector splat with
2327   //        IRBuilder. IRBuilder can constant-fold the multiply, but it doesn't
2328   //        handle a constant vector splat.
2329   Value *SplatVF = isa<Constant>(Mul)
2330                        ? ConstantVector::getSplat(VF, cast<Constant>(Mul))
2331                        : Builder.CreateVectorSplat(VF, Mul);
2332   Builder.restoreIP(CurrIP);
2333 
2334   // We may need to add the step a number of times, depending on the unroll
2335   // factor. The last of those goes into the PHI.
2336   PHINode *VecInd = PHINode::Create(SteppedStart->getType(), 2, "vec.ind",
2337                                     &*LoopVectorBody->getFirstInsertionPt());
2338   VecInd->setDebugLoc(EntryVal->getDebugLoc());
2339   Instruction *LastInduction = VecInd;
2340   for (unsigned Part = 0; Part < UF; ++Part) {
2341     State.set(Def, LastInduction, Part);
2342 
2343     if (isa<TruncInst>(EntryVal))
2344       addMetadata(LastInduction, EntryVal);
2345     recordVectorLoopValueForInductionCast(II, EntryVal, LastInduction, CastDef,
2346                                           State, Part);
2347 
2348     LastInduction = cast<Instruction>(
2349         Builder.CreateBinOp(AddOp, LastInduction, SplatVF, "step.add"));
2350     LastInduction->setDebugLoc(EntryVal->getDebugLoc());
2351   }
2352 
2353   // Move the last step to the end of the latch block. This ensures consistent
2354   // placement of all induction updates.
2355   auto *LoopVectorLatch = LI->getLoopFor(LoopVectorBody)->getLoopLatch();
2356   auto *Br = cast<BranchInst>(LoopVectorLatch->getTerminator());
2357   auto *ICmp = cast<Instruction>(Br->getCondition());
2358   LastInduction->moveBefore(ICmp);
2359   LastInduction->setName("vec.ind.next");
2360 
2361   VecInd->addIncoming(SteppedStart, LoopVectorPreHeader);
2362   VecInd->addIncoming(LastInduction, LoopVectorLatch);
2363 }
2364 
2365 bool InnerLoopVectorizer::shouldScalarizeInstruction(Instruction *I) const {
2366   return Cost->isScalarAfterVectorization(I, VF) ||
2367          Cost->isProfitableToScalarize(I, VF);
2368 }
2369 
2370 bool InnerLoopVectorizer::needsScalarInduction(Instruction *IV) const {
2371   if (shouldScalarizeInstruction(IV))
2372     return true;
2373   auto isScalarInst = [&](User *U) -> bool {
2374     auto *I = cast<Instruction>(U);
2375     return (OrigLoop->contains(I) && shouldScalarizeInstruction(I));
2376   };
2377   return llvm::any_of(IV->users(), isScalarInst);
2378 }
2379 
2380 void InnerLoopVectorizer::recordVectorLoopValueForInductionCast(
2381     const InductionDescriptor &ID, const Instruction *EntryVal,
2382     Value *VectorLoopVal, VPValue *CastDef, VPTransformState &State,
2383     unsigned Part, unsigned Lane) {
2384   assert((isa<PHINode>(EntryVal) || isa<TruncInst>(EntryVal)) &&
2385          "Expected either an induction phi-node or a truncate of it!");
2386 
2387   // This induction variable is not the phi from the original loop but the
2388   // newly-created IV based on the proof that casted Phi is equal to the
2389   // uncasted Phi in the vectorized loop (under a runtime guard possibly). It
2390   // re-uses the same InductionDescriptor that original IV uses but we don't
2391   // have to do any recording in this case - that is done when original IV is
2392   // processed.
2393   if (isa<TruncInst>(EntryVal))
2394     return;
2395 
2396   const SmallVectorImpl<Instruction *> &Casts = ID.getCastInsts();
2397   if (Casts.empty())
2398     return;
2399   // Only the first Cast instruction in the Casts vector is of interest.
2400   // The rest of the Casts (if exist) have no uses outside the
2401   // induction update chain itself.
2402   if (Lane < UINT_MAX)
2403     State.set(CastDef, VectorLoopVal, VPIteration(Part, Lane));
2404   else
2405     State.set(CastDef, VectorLoopVal, Part);
2406 }
2407 
2408 void InnerLoopVectorizer::widenIntOrFpInduction(PHINode *IV, Value *Start,
2409                                                 TruncInst *Trunc, VPValue *Def,
2410                                                 VPValue *CastDef,
2411                                                 VPTransformState &State) {
2412   assert((IV->getType()->isIntegerTy() || IV != OldInduction) &&
2413          "Primary induction variable must have an integer type");
2414 
2415   auto II = Legal->getInductionVars().find(IV);
2416   assert(II != Legal->getInductionVars().end() && "IV is not an induction");
2417 
2418   auto ID = II->second;
2419   assert(IV->getType() == ID.getStartValue()->getType() && "Types must match");
2420 
2421   // The value from the original loop to which we are mapping the new induction
2422   // variable.
2423   Instruction *EntryVal = Trunc ? cast<Instruction>(Trunc) : IV;
2424 
2425   auto &DL = OrigLoop->getHeader()->getModule()->getDataLayout();
2426 
2427   // Generate code for the induction step. Note that induction steps are
2428   // required to be loop-invariant
2429   auto CreateStepValue = [&](const SCEV *Step) -> Value * {
2430     assert(PSE.getSE()->isLoopInvariant(Step, OrigLoop) &&
2431            "Induction step should be loop invariant");
2432     if (PSE.getSE()->isSCEVable(IV->getType())) {
2433       SCEVExpander Exp(*PSE.getSE(), DL, "induction");
2434       return Exp.expandCodeFor(Step, Step->getType(),
2435                                LoopVectorPreHeader->getTerminator());
2436     }
2437     return cast<SCEVUnknown>(Step)->getValue();
2438   };
2439 
2440   // The scalar value to broadcast. This is derived from the canonical
2441   // induction variable. If a truncation type is given, truncate the canonical
2442   // induction variable and step. Otherwise, derive these values from the
2443   // induction descriptor.
2444   auto CreateScalarIV = [&](Value *&Step) -> Value * {
2445     Value *ScalarIV = Induction;
2446     if (IV != OldInduction) {
2447       ScalarIV = IV->getType()->isIntegerTy()
2448                      ? Builder.CreateSExtOrTrunc(Induction, IV->getType())
2449                      : Builder.CreateCast(Instruction::SIToFP, Induction,
2450                                           IV->getType());
2451       ScalarIV = emitTransformedIndex(Builder, ScalarIV, PSE.getSE(), DL, ID);
2452       ScalarIV->setName("offset.idx");
2453     }
2454     if (Trunc) {
2455       auto *TruncType = cast<IntegerType>(Trunc->getType());
2456       assert(Step->getType()->isIntegerTy() &&
2457              "Truncation requires an integer step");
2458       ScalarIV = Builder.CreateTrunc(ScalarIV, TruncType);
2459       Step = Builder.CreateTrunc(Step, TruncType);
2460     }
2461     return ScalarIV;
2462   };
2463 
2464   // Create the vector values from the scalar IV, in the absence of creating a
2465   // vector IV.
2466   auto CreateSplatIV = [&](Value *ScalarIV, Value *Step) {
2467     Value *Broadcasted = getBroadcastInstrs(ScalarIV);
2468     for (unsigned Part = 0; Part < UF; ++Part) {
2469       assert(!VF.isScalable() && "scalable vectors not yet supported.");
2470       Value *StartIdx;
2471       if (Step->getType()->isFloatingPointTy())
2472         StartIdx = getRuntimeVFAsFloat(Builder, Step->getType(), VF * Part);
2473       else
2474         StartIdx = getRuntimeVF(Builder, Step->getType(), VF * Part);
2475 
2476       Value *EntryPart =
2477           getStepVector(Broadcasted, StartIdx, Step, ID.getInductionOpcode());
2478       State.set(Def, EntryPart, Part);
2479       if (Trunc)
2480         addMetadata(EntryPart, Trunc);
2481       recordVectorLoopValueForInductionCast(ID, EntryVal, EntryPart, CastDef,
2482                                             State, Part);
2483     }
2484   };
2485 
2486   // Fast-math-flags propagate from the original induction instruction.
2487   IRBuilder<>::FastMathFlagGuard FMFG(Builder);
2488   if (ID.getInductionBinOp() && isa<FPMathOperator>(ID.getInductionBinOp()))
2489     Builder.setFastMathFlags(ID.getInductionBinOp()->getFastMathFlags());
2490 
2491   // Now do the actual transformations, and start with creating the step value.
2492   Value *Step = CreateStepValue(ID.getStep());
2493   if (VF.isZero() || VF.isScalar()) {
2494     Value *ScalarIV = CreateScalarIV(Step);
2495     CreateSplatIV(ScalarIV, Step);
2496     return;
2497   }
2498 
2499   // Determine if we want a scalar version of the induction variable. This is
2500   // true if the induction variable itself is not widened, or if it has at
2501   // least one user in the loop that is not widened.
2502   auto NeedsScalarIV = needsScalarInduction(EntryVal);
2503   if (!NeedsScalarIV) {
2504     createVectorIntOrFpInductionPHI(ID, Step, Start, EntryVal, Def, CastDef,
2505                                     State);
2506     return;
2507   }
2508 
2509   // Try to create a new independent vector induction variable. If we can't
2510   // create the phi node, we will splat the scalar induction variable in each
2511   // loop iteration.
2512   if (!shouldScalarizeInstruction(EntryVal)) {
2513     createVectorIntOrFpInductionPHI(ID, Step, Start, EntryVal, Def, CastDef,
2514                                     State);
2515     Value *ScalarIV = CreateScalarIV(Step);
2516     // Create scalar steps that can be used by instructions we will later
2517     // scalarize. Note that the addition of the scalar steps will not increase
2518     // the number of instructions in the loop in the common case prior to
2519     // InstCombine. We will be trading one vector extract for each scalar step.
2520     buildScalarSteps(ScalarIV, Step, EntryVal, ID, Def, CastDef, State);
2521     return;
2522   }
2523 
2524   // All IV users are scalar instructions, so only emit a scalar IV, not a
2525   // vectorised IV. Except when we tail-fold, then the splat IV feeds the
2526   // predicate used by the masked loads/stores.
2527   Value *ScalarIV = CreateScalarIV(Step);
2528   if (!Cost->isScalarEpilogueAllowed())
2529     CreateSplatIV(ScalarIV, Step);
2530   buildScalarSteps(ScalarIV, Step, EntryVal, ID, Def, CastDef, State);
2531 }
2532 
2533 Value *InnerLoopVectorizer::getStepVector(Value *Val, Value *StartIdx,
2534                                           Value *Step,
2535                                           Instruction::BinaryOps BinOp) {
2536   // Create and check the types.
2537   auto *ValVTy = cast<VectorType>(Val->getType());
2538   ElementCount VLen = ValVTy->getElementCount();
2539 
2540   Type *STy = Val->getType()->getScalarType();
2541   assert((STy->isIntegerTy() || STy->isFloatingPointTy()) &&
2542          "Induction Step must be an integer or FP");
2543   assert(Step->getType() == STy && "Step has wrong type");
2544 
2545   SmallVector<Constant *, 8> Indices;
2546 
2547   // Create a vector of consecutive numbers from zero to VF.
2548   VectorType *InitVecValVTy = ValVTy;
2549   Type *InitVecValSTy = STy;
2550   if (STy->isFloatingPointTy()) {
2551     InitVecValSTy =
2552         IntegerType::get(STy->getContext(), STy->getScalarSizeInBits());
2553     InitVecValVTy = VectorType::get(InitVecValSTy, VLen);
2554   }
2555   Value *InitVec = Builder.CreateStepVector(InitVecValVTy);
2556 
2557   // Splat the StartIdx
2558   Value *StartIdxSplat = Builder.CreateVectorSplat(VLen, StartIdx);
2559 
2560   if (STy->isIntegerTy()) {
2561     InitVec = Builder.CreateAdd(InitVec, StartIdxSplat);
2562     Step = Builder.CreateVectorSplat(VLen, Step);
2563     assert(Step->getType() == Val->getType() && "Invalid step vec");
2564     // FIXME: The newly created binary instructions should contain nsw/nuw flags,
2565     // which can be found from the original scalar operations.
2566     Step = Builder.CreateMul(InitVec, Step);
2567     return Builder.CreateAdd(Val, Step, "induction");
2568   }
2569 
2570   // Floating point induction.
2571   assert((BinOp == Instruction::FAdd || BinOp == Instruction::FSub) &&
2572          "Binary Opcode should be specified for FP induction");
2573   InitVec = Builder.CreateUIToFP(InitVec, ValVTy);
2574   InitVec = Builder.CreateFAdd(InitVec, StartIdxSplat);
2575 
2576   Step = Builder.CreateVectorSplat(VLen, Step);
2577   Value *MulOp = Builder.CreateFMul(InitVec, Step);
2578   return Builder.CreateBinOp(BinOp, Val, MulOp, "induction");
2579 }
2580 
2581 void InnerLoopVectorizer::buildScalarSteps(Value *ScalarIV, Value *Step,
2582                                            Instruction *EntryVal,
2583                                            const InductionDescriptor &ID,
2584                                            VPValue *Def, VPValue *CastDef,
2585                                            VPTransformState &State) {
2586   // We shouldn't have to build scalar steps if we aren't vectorizing.
2587   assert(VF.isVector() && "VF should be greater than one");
2588   // Get the value type and ensure it and the step have the same integer type.
2589   Type *ScalarIVTy = ScalarIV->getType()->getScalarType();
2590   assert(ScalarIVTy == Step->getType() &&
2591          "Val and Step should have the same type");
2592 
2593   // We build scalar steps for both integer and floating-point induction
2594   // variables. Here, we determine the kind of arithmetic we will perform.
2595   Instruction::BinaryOps AddOp;
2596   Instruction::BinaryOps MulOp;
2597   if (ScalarIVTy->isIntegerTy()) {
2598     AddOp = Instruction::Add;
2599     MulOp = Instruction::Mul;
2600   } else {
2601     AddOp = ID.getInductionOpcode();
2602     MulOp = Instruction::FMul;
2603   }
2604 
2605   // Determine the number of scalars we need to generate for each unroll
2606   // iteration. If EntryVal is uniform, we only need to generate the first
2607   // lane. Otherwise, we generate all VF values.
2608   bool IsUniform =
2609       Cost->isUniformAfterVectorization(cast<Instruction>(EntryVal), VF);
2610   unsigned Lanes = IsUniform ? 1 : VF.getKnownMinValue();
2611   // Compute the scalar steps and save the results in State.
2612   Type *IntStepTy = IntegerType::get(ScalarIVTy->getContext(),
2613                                      ScalarIVTy->getScalarSizeInBits());
2614   Type *VecIVTy = nullptr;
2615   Value *UnitStepVec = nullptr, *SplatStep = nullptr, *SplatIV = nullptr;
2616   if (!IsUniform && VF.isScalable()) {
2617     VecIVTy = VectorType::get(ScalarIVTy, VF);
2618     UnitStepVec = Builder.CreateStepVector(VectorType::get(IntStepTy, VF));
2619     SplatStep = Builder.CreateVectorSplat(VF, Step);
2620     SplatIV = Builder.CreateVectorSplat(VF, ScalarIV);
2621   }
2622 
2623   for (unsigned Part = 0; Part < UF; ++Part) {
2624     Value *StartIdx0 =
2625         createStepForVF(Builder, ConstantInt::get(IntStepTy, Part), VF);
2626 
2627     if (!IsUniform && VF.isScalable()) {
2628       auto *SplatStartIdx = Builder.CreateVectorSplat(VF, StartIdx0);
2629       auto *InitVec = Builder.CreateAdd(SplatStartIdx, UnitStepVec);
2630       if (ScalarIVTy->isFloatingPointTy())
2631         InitVec = Builder.CreateSIToFP(InitVec, VecIVTy);
2632       auto *Mul = Builder.CreateBinOp(MulOp, InitVec, SplatStep);
2633       auto *Add = Builder.CreateBinOp(AddOp, SplatIV, Mul);
2634       State.set(Def, Add, Part);
2635       recordVectorLoopValueForInductionCast(ID, EntryVal, Add, CastDef, State,
2636                                             Part);
2637       // It's useful to record the lane values too for the known minimum number
2638       // of elements so we do those below. This improves the code quality when
2639       // trying to extract the first element, for example.
2640     }
2641 
2642     if (ScalarIVTy->isFloatingPointTy())
2643       StartIdx0 = Builder.CreateSIToFP(StartIdx0, ScalarIVTy);
2644 
2645     for (unsigned Lane = 0; Lane < Lanes; ++Lane) {
2646       Value *StartIdx = Builder.CreateBinOp(
2647           AddOp, StartIdx0, getSignedIntOrFpConstant(ScalarIVTy, Lane));
2648       // The step returned by `createStepForVF` is a runtime-evaluated value
2649       // when VF is scalable. Otherwise, it should be folded into a Constant.
2650       assert((VF.isScalable() || isa<Constant>(StartIdx)) &&
2651              "Expected StartIdx to be folded to a constant when VF is not "
2652              "scalable");
2653       auto *Mul = Builder.CreateBinOp(MulOp, StartIdx, Step);
2654       auto *Add = Builder.CreateBinOp(AddOp, ScalarIV, Mul);
2655       State.set(Def, Add, VPIteration(Part, Lane));
2656       recordVectorLoopValueForInductionCast(ID, EntryVal, Add, CastDef, State,
2657                                             Part, Lane);
2658     }
2659   }
2660 }
2661 
2662 void InnerLoopVectorizer::packScalarIntoVectorValue(VPValue *Def,
2663                                                     const VPIteration &Instance,
2664                                                     VPTransformState &State) {
2665   Value *ScalarInst = State.get(Def, Instance);
2666   Value *VectorValue = State.get(Def, Instance.Part);
2667   VectorValue = Builder.CreateInsertElement(
2668       VectorValue, ScalarInst,
2669       Instance.Lane.getAsRuntimeExpr(State.Builder, VF));
2670   State.set(Def, VectorValue, Instance.Part);
2671 }
2672 
2673 Value *InnerLoopVectorizer::reverseVector(Value *Vec) {
2674   assert(Vec->getType()->isVectorTy() && "Invalid type");
2675   return Builder.CreateVectorReverse(Vec, "reverse");
2676 }
2677 
2678 // Return whether we allow using masked interleave-groups (for dealing with
2679 // strided loads/stores that reside in predicated blocks, or for dealing
2680 // with gaps).
2681 static bool useMaskedInterleavedAccesses(const TargetTransformInfo &TTI) {
2682   // If an override option has been passed in for interleaved accesses, use it.
2683   if (EnableMaskedInterleavedMemAccesses.getNumOccurrences() > 0)
2684     return EnableMaskedInterleavedMemAccesses;
2685 
2686   return TTI.enableMaskedInterleavedAccessVectorization();
2687 }
2688 
2689 // Try to vectorize the interleave group that \p Instr belongs to.
2690 //
2691 // E.g. Translate following interleaved load group (factor = 3):
2692 //   for (i = 0; i < N; i+=3) {
2693 //     R = Pic[i];             // Member of index 0
2694 //     G = Pic[i+1];           // Member of index 1
2695 //     B = Pic[i+2];           // Member of index 2
2696 //     ... // do something to R, G, B
2697 //   }
2698 // To:
2699 //   %wide.vec = load <12 x i32>                       ; Read 4 tuples of R,G,B
2700 //   %R.vec = shuffle %wide.vec, poison, <0, 3, 6, 9>   ; R elements
2701 //   %G.vec = shuffle %wide.vec, poison, <1, 4, 7, 10>  ; G elements
2702 //   %B.vec = shuffle %wide.vec, poison, <2, 5, 8, 11>  ; B elements
2703 //
2704 // Or translate following interleaved store group (factor = 3):
2705 //   for (i = 0; i < N; i+=3) {
2706 //     ... do something to R, G, B
2707 //     Pic[i]   = R;           // Member of index 0
2708 //     Pic[i+1] = G;           // Member of index 1
2709 //     Pic[i+2] = B;           // Member of index 2
2710 //   }
2711 // To:
2712 //   %R_G.vec = shuffle %R.vec, %G.vec, <0, 1, 2, ..., 7>
2713 //   %B_U.vec = shuffle %B.vec, poison, <0, 1, 2, 3, u, u, u, u>
2714 //   %interleaved.vec = shuffle %R_G.vec, %B_U.vec,
2715 //        <0, 4, 8, 1, 5, 9, 2, 6, 10, 3, 7, 11>    ; Interleave R,G,B elements
2716 //   store <12 x i32> %interleaved.vec              ; Write 4 tuples of R,G,B
2717 void InnerLoopVectorizer::vectorizeInterleaveGroup(
2718     const InterleaveGroup<Instruction> *Group, ArrayRef<VPValue *> VPDefs,
2719     VPTransformState &State, VPValue *Addr, ArrayRef<VPValue *> StoredValues,
2720     VPValue *BlockInMask) {
2721   Instruction *Instr = Group->getInsertPos();
2722   const DataLayout &DL = Instr->getModule()->getDataLayout();
2723 
2724   // Prepare for the vector type of the interleaved load/store.
2725   Type *ScalarTy = getLoadStoreType(Instr);
2726   unsigned InterleaveFactor = Group->getFactor();
2727   assert(!VF.isScalable() && "scalable vectors not yet supported.");
2728   auto *VecTy = VectorType::get(ScalarTy, VF * InterleaveFactor);
2729 
2730   // Prepare for the new pointers.
2731   SmallVector<Value *, 2> AddrParts;
2732   unsigned Index = Group->getIndex(Instr);
2733 
2734   // TODO: extend the masked interleaved-group support to reversed access.
2735   assert((!BlockInMask || !Group->isReverse()) &&
2736          "Reversed masked interleave-group not supported.");
2737 
2738   // If the group is reverse, adjust the index to refer to the last vector lane
2739   // instead of the first. We adjust the index from the first vector lane,
2740   // rather than directly getting the pointer for lane VF - 1, because the
2741   // pointer operand of the interleaved access is supposed to be uniform. For
2742   // uniform instructions, we're only required to generate a value for the
2743   // first vector lane in each unroll iteration.
2744   if (Group->isReverse())
2745     Index += (VF.getKnownMinValue() - 1) * Group->getFactor();
2746 
2747   for (unsigned Part = 0; Part < UF; Part++) {
2748     Value *AddrPart = State.get(Addr, VPIteration(Part, 0));
2749     setDebugLocFromInst(AddrPart);
2750 
2751     // Notice current instruction could be any index. Need to adjust the address
2752     // to the member of index 0.
2753     //
2754     // E.g.  a = A[i+1];     // Member of index 1 (Current instruction)
2755     //       b = A[i];       // Member of index 0
2756     // Current pointer is pointed to A[i+1], adjust it to A[i].
2757     //
2758     // E.g.  A[i+1] = a;     // Member of index 1
2759     //       A[i]   = b;     // Member of index 0
2760     //       A[i+2] = c;     // Member of index 2 (Current instruction)
2761     // Current pointer is pointed to A[i+2], adjust it to A[i].
2762 
2763     bool InBounds = false;
2764     if (auto *gep = dyn_cast<GetElementPtrInst>(AddrPart->stripPointerCasts()))
2765       InBounds = gep->isInBounds();
2766     AddrPart = Builder.CreateGEP(ScalarTy, AddrPart, Builder.getInt32(-Index));
2767     cast<GetElementPtrInst>(AddrPart)->setIsInBounds(InBounds);
2768 
2769     // Cast to the vector pointer type.
2770     unsigned AddressSpace = AddrPart->getType()->getPointerAddressSpace();
2771     Type *PtrTy = VecTy->getPointerTo(AddressSpace);
2772     AddrParts.push_back(Builder.CreateBitCast(AddrPart, PtrTy));
2773   }
2774 
2775   setDebugLocFromInst(Instr);
2776   Value *PoisonVec = PoisonValue::get(VecTy);
2777 
2778   Value *MaskForGaps = nullptr;
2779   if (Group->requiresScalarEpilogue() && !Cost->isScalarEpilogueAllowed()) {
2780     MaskForGaps = createBitMaskForGaps(Builder, VF.getKnownMinValue(), *Group);
2781     assert(MaskForGaps && "Mask for Gaps is required but it is null");
2782   }
2783 
2784   // Vectorize the interleaved load group.
2785   if (isa<LoadInst>(Instr)) {
2786     // For each unroll part, create a wide load for the group.
2787     SmallVector<Value *, 2> NewLoads;
2788     for (unsigned Part = 0; Part < UF; Part++) {
2789       Instruction *NewLoad;
2790       if (BlockInMask || MaskForGaps) {
2791         assert(useMaskedInterleavedAccesses(*TTI) &&
2792                "masked interleaved groups are not allowed.");
2793         Value *GroupMask = MaskForGaps;
2794         if (BlockInMask) {
2795           Value *BlockInMaskPart = State.get(BlockInMask, Part);
2796           Value *ShuffledMask = Builder.CreateShuffleVector(
2797               BlockInMaskPart,
2798               createReplicatedMask(InterleaveFactor, VF.getKnownMinValue()),
2799               "interleaved.mask");
2800           GroupMask = MaskForGaps
2801                           ? Builder.CreateBinOp(Instruction::And, ShuffledMask,
2802                                                 MaskForGaps)
2803                           : ShuffledMask;
2804         }
2805         NewLoad =
2806             Builder.CreateMaskedLoad(VecTy, AddrParts[Part], Group->getAlign(),
2807                                      GroupMask, PoisonVec, "wide.masked.vec");
2808       }
2809       else
2810         NewLoad = Builder.CreateAlignedLoad(VecTy, AddrParts[Part],
2811                                             Group->getAlign(), "wide.vec");
2812       Group->addMetadata(NewLoad);
2813       NewLoads.push_back(NewLoad);
2814     }
2815 
2816     // For each member in the group, shuffle out the appropriate data from the
2817     // wide loads.
2818     unsigned J = 0;
2819     for (unsigned I = 0; I < InterleaveFactor; ++I) {
2820       Instruction *Member = Group->getMember(I);
2821 
2822       // Skip the gaps in the group.
2823       if (!Member)
2824         continue;
2825 
2826       auto StrideMask =
2827           createStrideMask(I, InterleaveFactor, VF.getKnownMinValue());
2828       for (unsigned Part = 0; Part < UF; Part++) {
2829         Value *StridedVec = Builder.CreateShuffleVector(
2830             NewLoads[Part], StrideMask, "strided.vec");
2831 
2832         // If this member has different type, cast the result type.
2833         if (Member->getType() != ScalarTy) {
2834           assert(!VF.isScalable() && "VF is assumed to be non scalable.");
2835           VectorType *OtherVTy = VectorType::get(Member->getType(), VF);
2836           StridedVec = createBitOrPointerCast(StridedVec, OtherVTy, DL);
2837         }
2838 
2839         if (Group->isReverse())
2840           StridedVec = reverseVector(StridedVec);
2841 
2842         State.set(VPDefs[J], StridedVec, Part);
2843       }
2844       ++J;
2845     }
2846     return;
2847   }
2848 
2849   // The sub vector type for current instruction.
2850   auto *SubVT = VectorType::get(ScalarTy, VF);
2851 
2852   // Vectorize the interleaved store group.
2853   MaskForGaps = createBitMaskForGaps(Builder, VF.getKnownMinValue(), *Group);
2854   assert((!MaskForGaps || useMaskedInterleavedAccesses(*TTI)) &&
2855          "masked interleaved groups are not allowed.");
2856   assert((!MaskForGaps || !VF.isScalable()) &&
2857          "masking gaps for scalable vectors is not yet supported.");
2858   for (unsigned Part = 0; Part < UF; Part++) {
2859     // Collect the stored vector from each member.
2860     SmallVector<Value *, 4> StoredVecs;
2861     for (unsigned i = 0; i < InterleaveFactor; i++) {
2862       assert((Group->getMember(i) || MaskForGaps) &&
2863              "Fail to get a member from an interleaved store group");
2864       Instruction *Member = Group->getMember(i);
2865 
2866       // Skip the gaps in the group.
2867       if (!Member) {
2868         Value *Undef = PoisonValue::get(SubVT);
2869         StoredVecs.push_back(Undef);
2870         continue;
2871       }
2872 
2873       Value *StoredVec = State.get(StoredValues[i], Part);
2874 
2875       if (Group->isReverse())
2876         StoredVec = reverseVector(StoredVec);
2877 
2878       // If this member has different type, cast it to a unified type.
2879 
2880       if (StoredVec->getType() != SubVT)
2881         StoredVec = createBitOrPointerCast(StoredVec, SubVT, DL);
2882 
2883       StoredVecs.push_back(StoredVec);
2884     }
2885 
2886     // Concatenate all vectors into a wide vector.
2887     Value *WideVec = concatenateVectors(Builder, StoredVecs);
2888 
2889     // Interleave the elements in the wide vector.
2890     Value *IVec = Builder.CreateShuffleVector(
2891         WideVec, createInterleaveMask(VF.getKnownMinValue(), InterleaveFactor),
2892         "interleaved.vec");
2893 
2894     Instruction *NewStoreInstr;
2895     if (BlockInMask || MaskForGaps) {
2896       Value *GroupMask = MaskForGaps;
2897       if (BlockInMask) {
2898         Value *BlockInMaskPart = State.get(BlockInMask, Part);
2899         Value *ShuffledMask = Builder.CreateShuffleVector(
2900             BlockInMaskPart,
2901             createReplicatedMask(InterleaveFactor, VF.getKnownMinValue()),
2902             "interleaved.mask");
2903         GroupMask = MaskForGaps ? Builder.CreateBinOp(Instruction::And,
2904                                                       ShuffledMask, MaskForGaps)
2905                                 : ShuffledMask;
2906       }
2907       NewStoreInstr = Builder.CreateMaskedStore(IVec, AddrParts[Part],
2908                                                 Group->getAlign(), GroupMask);
2909     } else
2910       NewStoreInstr =
2911           Builder.CreateAlignedStore(IVec, AddrParts[Part], Group->getAlign());
2912 
2913     Group->addMetadata(NewStoreInstr);
2914   }
2915 }
2916 
2917 void InnerLoopVectorizer::vectorizeMemoryInstruction(
2918     Instruction *Instr, VPTransformState &State, VPValue *Def, VPValue *Addr,
2919     VPValue *StoredValue, VPValue *BlockInMask, bool ConsecutiveStride,
2920     bool Reverse) {
2921   // Attempt to issue a wide load.
2922   LoadInst *LI = dyn_cast<LoadInst>(Instr);
2923   StoreInst *SI = dyn_cast<StoreInst>(Instr);
2924 
2925   assert((LI || SI) && "Invalid Load/Store instruction");
2926   assert((!SI || StoredValue) && "No stored value provided for widened store");
2927   assert((!LI || !StoredValue) && "Stored value provided for widened load");
2928 
2929   Type *ScalarDataTy = getLoadStoreType(Instr);
2930 
2931   auto *DataTy = VectorType::get(ScalarDataTy, VF);
2932   const Align Alignment = getLoadStoreAlignment(Instr);
2933   bool CreateGatherScatter = !ConsecutiveStride;
2934 
2935   VectorParts BlockInMaskParts(UF);
2936   bool isMaskRequired = BlockInMask;
2937   if (isMaskRequired)
2938     for (unsigned Part = 0; Part < UF; ++Part)
2939       BlockInMaskParts[Part] = State.get(BlockInMask, Part);
2940 
2941   const auto CreateVecPtr = [&](unsigned Part, Value *Ptr) -> Value * {
2942     // Calculate the pointer for the specific unroll-part.
2943     GetElementPtrInst *PartPtr = nullptr;
2944 
2945     bool InBounds = false;
2946     if (auto *gep = dyn_cast<GetElementPtrInst>(Ptr->stripPointerCasts()))
2947       InBounds = gep->isInBounds();
2948     if (Reverse) {
2949       // If the address is consecutive but reversed, then the
2950       // wide store needs to start at the last vector element.
2951       // RunTimeVF =  VScale * VF.getKnownMinValue()
2952       // For fixed-width VScale is 1, then RunTimeVF = VF.getKnownMinValue()
2953       Value *RunTimeVF = getRuntimeVF(Builder, Builder.getInt32Ty(), VF);
2954       // NumElt = -Part * RunTimeVF
2955       Value *NumElt = Builder.CreateMul(Builder.getInt32(-Part), RunTimeVF);
2956       // LastLane = 1 - RunTimeVF
2957       Value *LastLane = Builder.CreateSub(Builder.getInt32(1), RunTimeVF);
2958       PartPtr =
2959           cast<GetElementPtrInst>(Builder.CreateGEP(ScalarDataTy, Ptr, NumElt));
2960       PartPtr->setIsInBounds(InBounds);
2961       PartPtr = cast<GetElementPtrInst>(
2962           Builder.CreateGEP(ScalarDataTy, PartPtr, LastLane));
2963       PartPtr->setIsInBounds(InBounds);
2964       if (isMaskRequired) // Reverse of a null all-one mask is a null mask.
2965         BlockInMaskParts[Part] = reverseVector(BlockInMaskParts[Part]);
2966     } else {
2967       Value *Increment = createStepForVF(Builder, Builder.getInt32(Part), VF);
2968       PartPtr = cast<GetElementPtrInst>(
2969           Builder.CreateGEP(ScalarDataTy, Ptr, Increment));
2970       PartPtr->setIsInBounds(InBounds);
2971     }
2972 
2973     unsigned AddressSpace = Ptr->getType()->getPointerAddressSpace();
2974     return Builder.CreateBitCast(PartPtr, DataTy->getPointerTo(AddressSpace));
2975   };
2976 
2977   // Handle Stores:
2978   if (SI) {
2979     setDebugLocFromInst(SI);
2980 
2981     for (unsigned Part = 0; Part < UF; ++Part) {
2982       Instruction *NewSI = nullptr;
2983       Value *StoredVal = State.get(StoredValue, Part);
2984       if (CreateGatherScatter) {
2985         Value *MaskPart = isMaskRequired ? BlockInMaskParts[Part] : nullptr;
2986         Value *VectorGep = State.get(Addr, Part);
2987         NewSI = Builder.CreateMaskedScatter(StoredVal, VectorGep, Alignment,
2988                                             MaskPart);
2989       } else {
2990         if (Reverse) {
2991           // If we store to reverse consecutive memory locations, then we need
2992           // to reverse the order of elements in the stored value.
2993           StoredVal = reverseVector(StoredVal);
2994           // We don't want to update the value in the map as it might be used in
2995           // another expression. So don't call resetVectorValue(StoredVal).
2996         }
2997         auto *VecPtr = CreateVecPtr(Part, State.get(Addr, VPIteration(0, 0)));
2998         if (isMaskRequired)
2999           NewSI = Builder.CreateMaskedStore(StoredVal, VecPtr, Alignment,
3000                                             BlockInMaskParts[Part]);
3001         else
3002           NewSI = Builder.CreateAlignedStore(StoredVal, VecPtr, Alignment);
3003       }
3004       addMetadata(NewSI, SI);
3005     }
3006     return;
3007   }
3008 
3009   // Handle loads.
3010   assert(LI && "Must have a load instruction");
3011   setDebugLocFromInst(LI);
3012   for (unsigned Part = 0; Part < UF; ++Part) {
3013     Value *NewLI;
3014     if (CreateGatherScatter) {
3015       Value *MaskPart = isMaskRequired ? BlockInMaskParts[Part] : nullptr;
3016       Value *VectorGep = State.get(Addr, Part);
3017       NewLI = Builder.CreateMaskedGather(DataTy, VectorGep, Alignment, MaskPart,
3018                                          nullptr, "wide.masked.gather");
3019       addMetadata(NewLI, LI);
3020     } else {
3021       auto *VecPtr = CreateVecPtr(Part, State.get(Addr, VPIteration(0, 0)));
3022       if (isMaskRequired)
3023         NewLI = Builder.CreateMaskedLoad(
3024             DataTy, VecPtr, Alignment, BlockInMaskParts[Part],
3025             PoisonValue::get(DataTy), "wide.masked.load");
3026       else
3027         NewLI =
3028             Builder.CreateAlignedLoad(DataTy, VecPtr, Alignment, "wide.load");
3029 
3030       // Add metadata to the load, but setVectorValue to the reverse shuffle.
3031       addMetadata(NewLI, LI);
3032       if (Reverse)
3033         NewLI = reverseVector(NewLI);
3034     }
3035 
3036     State.set(Def, NewLI, Part);
3037   }
3038 }
3039 
3040 void InnerLoopVectorizer::scalarizeInstruction(Instruction *Instr, VPValue *Def,
3041                                                VPUser &User,
3042                                                const VPIteration &Instance,
3043                                                bool IfPredicateInstr,
3044                                                VPTransformState &State) {
3045   assert(!Instr->getType()->isAggregateType() && "Can't handle vectors");
3046 
3047   // llvm.experimental.noalias.scope.decl intrinsics must only be duplicated for
3048   // the first lane and part.
3049   if (isa<NoAliasScopeDeclInst>(Instr))
3050     if (!Instance.isFirstIteration())
3051       return;
3052 
3053   setDebugLocFromInst(Instr);
3054 
3055   // Does this instruction return a value ?
3056   bool IsVoidRetTy = Instr->getType()->isVoidTy();
3057 
3058   Instruction *Cloned = Instr->clone();
3059   if (!IsVoidRetTy)
3060     Cloned->setName(Instr->getName() + ".cloned");
3061 
3062   State.Builder.SetInsertPoint(Builder.GetInsertBlock(),
3063                                Builder.GetInsertPoint());
3064   // Replace the operands of the cloned instructions with their scalar
3065   // equivalents in the new loop.
3066   for (unsigned op = 0, e = User.getNumOperands(); op != e; ++op) {
3067     auto *Operand = dyn_cast<Instruction>(Instr->getOperand(op));
3068     auto InputInstance = Instance;
3069     if (!Operand || !OrigLoop->contains(Operand) ||
3070         (Cost->isUniformAfterVectorization(Operand, State.VF)))
3071       InputInstance.Lane = VPLane::getFirstLane();
3072     auto *NewOp = State.get(User.getOperand(op), InputInstance);
3073     Cloned->setOperand(op, NewOp);
3074   }
3075   addNewMetadata(Cloned, Instr);
3076 
3077   // Place the cloned scalar in the new loop.
3078   Builder.Insert(Cloned);
3079 
3080   State.set(Def, Cloned, Instance);
3081 
3082   // If we just cloned a new assumption, add it the assumption cache.
3083   if (auto *II = dyn_cast<AssumeInst>(Cloned))
3084     AC->registerAssumption(II);
3085 
3086   // End if-block.
3087   if (IfPredicateInstr)
3088     PredicatedInstructions.push_back(Cloned);
3089 }
3090 
3091 PHINode *InnerLoopVectorizer::createInductionVariable(Loop *L, Value *Start,
3092                                                       Value *End, Value *Step,
3093                                                       Instruction *DL) {
3094   BasicBlock *Header = L->getHeader();
3095   BasicBlock *Latch = L->getLoopLatch();
3096   // As we're just creating this loop, it's possible no latch exists
3097   // yet. If so, use the header as this will be a single block loop.
3098   if (!Latch)
3099     Latch = Header;
3100 
3101   IRBuilder<> B(&*Header->getFirstInsertionPt());
3102   Instruction *OldInst = getDebugLocFromInstOrOperands(OldInduction);
3103   setDebugLocFromInst(OldInst, &B);
3104   auto *Induction = B.CreatePHI(Start->getType(), 2, "index");
3105 
3106   B.SetInsertPoint(Latch->getTerminator());
3107   setDebugLocFromInst(OldInst, &B);
3108 
3109   // Create i+1 and fill the PHINode.
3110   //
3111   // If the tail is not folded, we know that End - Start >= Step (either
3112   // statically or through the minimum iteration checks). We also know that both
3113   // Start % Step == 0 and End % Step == 0. We exit the vector loop if %IV +
3114   // %Step == %End. Hence we must exit the loop before %IV + %Step unsigned
3115   // overflows and we can mark the induction increment as NUW.
3116   Value *Next = B.CreateAdd(Induction, Step, "index.next",
3117                             /*NUW=*/!Cost->foldTailByMasking(), /*NSW=*/false);
3118   Induction->addIncoming(Start, L->getLoopPreheader());
3119   Induction->addIncoming(Next, Latch);
3120   // Create the compare.
3121   Value *ICmp = B.CreateICmpEQ(Next, End);
3122   B.CreateCondBr(ICmp, L->getUniqueExitBlock(), Header);
3123 
3124   // Now we have two terminators. Remove the old one from the block.
3125   Latch->getTerminator()->eraseFromParent();
3126 
3127   return Induction;
3128 }
3129 
3130 Value *InnerLoopVectorizer::getOrCreateTripCount(Loop *L) {
3131   if (TripCount)
3132     return TripCount;
3133 
3134   assert(L && "Create Trip Count for null loop.");
3135   IRBuilder<> Builder(L->getLoopPreheader()->getTerminator());
3136   // Find the loop boundaries.
3137   ScalarEvolution *SE = PSE.getSE();
3138   const SCEV *BackedgeTakenCount = PSE.getBackedgeTakenCount();
3139   assert(!isa<SCEVCouldNotCompute>(BackedgeTakenCount) &&
3140          "Invalid loop count");
3141 
3142   Type *IdxTy = Legal->getWidestInductionType();
3143   assert(IdxTy && "No type for induction");
3144 
3145   // The exit count might have the type of i64 while the phi is i32. This can
3146   // happen if we have an induction variable that is sign extended before the
3147   // compare. The only way that we get a backedge taken count is that the
3148   // induction variable was signed and as such will not overflow. In such a case
3149   // truncation is legal.
3150   if (SE->getTypeSizeInBits(BackedgeTakenCount->getType()) >
3151       IdxTy->getPrimitiveSizeInBits())
3152     BackedgeTakenCount = SE->getTruncateOrNoop(BackedgeTakenCount, IdxTy);
3153   BackedgeTakenCount = SE->getNoopOrZeroExtend(BackedgeTakenCount, IdxTy);
3154 
3155   // Get the total trip count from the count by adding 1.
3156   const SCEV *ExitCount = SE->getAddExpr(
3157       BackedgeTakenCount, SE->getOne(BackedgeTakenCount->getType()));
3158 
3159   const DataLayout &DL = L->getHeader()->getModule()->getDataLayout();
3160 
3161   // Expand the trip count and place the new instructions in the preheader.
3162   // Notice that the pre-header does not change, only the loop body.
3163   SCEVExpander Exp(*SE, DL, "induction");
3164 
3165   // Count holds the overall loop count (N).
3166   TripCount = Exp.expandCodeFor(ExitCount, ExitCount->getType(),
3167                                 L->getLoopPreheader()->getTerminator());
3168 
3169   if (TripCount->getType()->isPointerTy())
3170     TripCount =
3171         CastInst::CreatePointerCast(TripCount, IdxTy, "exitcount.ptrcnt.to.int",
3172                                     L->getLoopPreheader()->getTerminator());
3173 
3174   return TripCount;
3175 }
3176 
3177 Value *InnerLoopVectorizer::getOrCreateVectorTripCount(Loop *L) {
3178   if (VectorTripCount)
3179     return VectorTripCount;
3180 
3181   Value *TC = getOrCreateTripCount(L);
3182   IRBuilder<> Builder(L->getLoopPreheader()->getTerminator());
3183 
3184   Type *Ty = TC->getType();
3185   // This is where we can make the step a runtime constant.
3186   Value *Step = createStepForVF(Builder, ConstantInt::get(Ty, UF), VF);
3187 
3188   // If the tail is to be folded by masking, round the number of iterations N
3189   // up to a multiple of Step instead of rounding down. This is done by first
3190   // adding Step-1 and then rounding down. Note that it's ok if this addition
3191   // overflows: the vector induction variable will eventually wrap to zero given
3192   // that it starts at zero and its Step is a power of two; the loop will then
3193   // exit, with the last early-exit vector comparison also producing all-true.
3194   if (Cost->foldTailByMasking()) {
3195     assert(isPowerOf2_32(VF.getKnownMinValue() * UF) &&
3196            "VF*UF must be a power of 2 when folding tail by masking");
3197     assert(!VF.isScalable() &&
3198            "Tail folding not yet supported for scalable vectors");
3199     TC = Builder.CreateAdd(
3200         TC, ConstantInt::get(Ty, VF.getKnownMinValue() * UF - 1), "n.rnd.up");
3201   }
3202 
3203   // Now we need to generate the expression for the part of the loop that the
3204   // vectorized body will execute. This is equal to N - (N % Step) if scalar
3205   // iterations are not required for correctness, or N - Step, otherwise. Step
3206   // is equal to the vectorization factor (number of SIMD elements) times the
3207   // unroll factor (number of SIMD instructions).
3208   Value *R = Builder.CreateURem(TC, Step, "n.mod.vf");
3209 
3210   // There are cases where we *must* run at least one iteration in the remainder
3211   // loop.  See the cost model for when this can happen.  If the step evenly
3212   // divides the trip count, we set the remainder to be equal to the step. If
3213   // the step does not evenly divide the trip count, no adjustment is necessary
3214   // since there will already be scalar iterations. Note that the minimum
3215   // iterations check ensures that N >= Step.
3216   if (Cost->requiresScalarEpilogue(VF)) {
3217     auto *IsZero = Builder.CreateICmpEQ(R, ConstantInt::get(R->getType(), 0));
3218     R = Builder.CreateSelect(IsZero, Step, R);
3219   }
3220 
3221   VectorTripCount = Builder.CreateSub(TC, R, "n.vec");
3222 
3223   return VectorTripCount;
3224 }
3225 
3226 Value *InnerLoopVectorizer::createBitOrPointerCast(Value *V, VectorType *DstVTy,
3227                                                    const DataLayout &DL) {
3228   // Verify that V is a vector type with same number of elements as DstVTy.
3229   auto *DstFVTy = cast<FixedVectorType>(DstVTy);
3230   unsigned VF = DstFVTy->getNumElements();
3231   auto *SrcVecTy = cast<FixedVectorType>(V->getType());
3232   assert((VF == SrcVecTy->getNumElements()) && "Vector dimensions do not match");
3233   Type *SrcElemTy = SrcVecTy->getElementType();
3234   Type *DstElemTy = DstFVTy->getElementType();
3235   assert((DL.getTypeSizeInBits(SrcElemTy) == DL.getTypeSizeInBits(DstElemTy)) &&
3236          "Vector elements must have same size");
3237 
3238   // Do a direct cast if element types are castable.
3239   if (CastInst::isBitOrNoopPointerCastable(SrcElemTy, DstElemTy, DL)) {
3240     return Builder.CreateBitOrPointerCast(V, DstFVTy);
3241   }
3242   // V cannot be directly casted to desired vector type.
3243   // May happen when V is a floating point vector but DstVTy is a vector of
3244   // pointers or vice-versa. Handle this using a two-step bitcast using an
3245   // intermediate Integer type for the bitcast i.e. Ptr <-> Int <-> Float.
3246   assert((DstElemTy->isPointerTy() != SrcElemTy->isPointerTy()) &&
3247          "Only one type should be a pointer type");
3248   assert((DstElemTy->isFloatingPointTy() != SrcElemTy->isFloatingPointTy()) &&
3249          "Only one type should be a floating point type");
3250   Type *IntTy =
3251       IntegerType::getIntNTy(V->getContext(), DL.getTypeSizeInBits(SrcElemTy));
3252   auto *VecIntTy = FixedVectorType::get(IntTy, VF);
3253   Value *CastVal = Builder.CreateBitOrPointerCast(V, VecIntTy);
3254   return Builder.CreateBitOrPointerCast(CastVal, DstFVTy);
3255 }
3256 
3257 void InnerLoopVectorizer::emitMinimumIterationCountCheck(Loop *L,
3258                                                          BasicBlock *Bypass) {
3259   Value *Count = getOrCreateTripCount(L);
3260   // Reuse existing vector loop preheader for TC checks.
3261   // Note that new preheader block is generated for vector loop.
3262   BasicBlock *const TCCheckBlock = LoopVectorPreHeader;
3263   IRBuilder<> Builder(TCCheckBlock->getTerminator());
3264 
3265   // Generate code to check if the loop's trip count is less than VF * UF, or
3266   // equal to it in case a scalar epilogue is required; this implies that the
3267   // vector trip count is zero. This check also covers the case where adding one
3268   // to the backedge-taken count overflowed leading to an incorrect trip count
3269   // of zero. In this case we will also jump to the scalar loop.
3270   auto P = Cost->requiresScalarEpilogue(VF) ? ICmpInst::ICMP_ULE
3271                                             : ICmpInst::ICMP_ULT;
3272 
3273   // If tail is to be folded, vector loop takes care of all iterations.
3274   Value *CheckMinIters = Builder.getFalse();
3275   if (!Cost->foldTailByMasking()) {
3276     Value *Step =
3277         createStepForVF(Builder, ConstantInt::get(Count->getType(), UF), VF);
3278     CheckMinIters = Builder.CreateICmp(P, Count, Step, "min.iters.check");
3279   }
3280   // Create new preheader for vector loop.
3281   LoopVectorPreHeader =
3282       SplitBlock(TCCheckBlock, TCCheckBlock->getTerminator(), DT, LI, nullptr,
3283                  "vector.ph");
3284 
3285   assert(DT->properlyDominates(DT->getNode(TCCheckBlock),
3286                                DT->getNode(Bypass)->getIDom()) &&
3287          "TC check is expected to dominate Bypass");
3288 
3289   // Update dominator for Bypass & LoopExit (if needed).
3290   DT->changeImmediateDominator(Bypass, TCCheckBlock);
3291   if (!Cost->requiresScalarEpilogue(VF))
3292     // If there is an epilogue which must run, there's no edge from the
3293     // middle block to exit blocks  and thus no need to update the immediate
3294     // dominator of the exit blocks.
3295     DT->changeImmediateDominator(LoopExitBlock, TCCheckBlock);
3296 
3297   ReplaceInstWithInst(
3298       TCCheckBlock->getTerminator(),
3299       BranchInst::Create(Bypass, LoopVectorPreHeader, CheckMinIters));
3300   LoopBypassBlocks.push_back(TCCheckBlock);
3301 }
3302 
3303 BasicBlock *InnerLoopVectorizer::emitSCEVChecks(Loop *L, BasicBlock *Bypass) {
3304 
3305   BasicBlock *const SCEVCheckBlock =
3306       RTChecks.emitSCEVChecks(L, Bypass, LoopVectorPreHeader, LoopExitBlock);
3307   if (!SCEVCheckBlock)
3308     return nullptr;
3309 
3310   assert(!(SCEVCheckBlock->getParent()->hasOptSize() ||
3311            (OptForSizeBasedOnProfile &&
3312             Cost->Hints->getForce() != LoopVectorizeHints::FK_Enabled)) &&
3313          "Cannot SCEV check stride or overflow when optimizing for size");
3314 
3315 
3316   // Update dominator only if this is first RT check.
3317   if (LoopBypassBlocks.empty()) {
3318     DT->changeImmediateDominator(Bypass, SCEVCheckBlock);
3319     if (!Cost->requiresScalarEpilogue(VF))
3320       // If there is an epilogue which must run, there's no edge from the
3321       // middle block to exit blocks  and thus no need to update the immediate
3322       // dominator of the exit blocks.
3323       DT->changeImmediateDominator(LoopExitBlock, SCEVCheckBlock);
3324   }
3325 
3326   LoopBypassBlocks.push_back(SCEVCheckBlock);
3327   AddedSafetyChecks = true;
3328   return SCEVCheckBlock;
3329 }
3330 
3331 BasicBlock *InnerLoopVectorizer::emitMemRuntimeChecks(Loop *L,
3332                                                       BasicBlock *Bypass) {
3333   // VPlan-native path does not do any analysis for runtime checks currently.
3334   if (EnableVPlanNativePath)
3335     return nullptr;
3336 
3337   BasicBlock *const MemCheckBlock =
3338       RTChecks.emitMemRuntimeChecks(L, Bypass, LoopVectorPreHeader);
3339 
3340   // Check if we generated code that checks in runtime if arrays overlap. We put
3341   // the checks into a separate block to make the more common case of few
3342   // elements faster.
3343   if (!MemCheckBlock)
3344     return nullptr;
3345 
3346   if (MemCheckBlock->getParent()->hasOptSize() || OptForSizeBasedOnProfile) {
3347     assert(Cost->Hints->getForce() == LoopVectorizeHints::FK_Enabled &&
3348            "Cannot emit memory checks when optimizing for size, unless forced "
3349            "to vectorize.");
3350     ORE->emit([&]() {
3351       return OptimizationRemarkAnalysis(DEBUG_TYPE, "VectorizationCodeSize",
3352                                         L->getStartLoc(), L->getHeader())
3353              << "Code-size may be reduced by not forcing "
3354                 "vectorization, or by source-code modifications "
3355                 "eliminating the need for runtime checks "
3356                 "(e.g., adding 'restrict').";
3357     });
3358   }
3359 
3360   LoopBypassBlocks.push_back(MemCheckBlock);
3361 
3362   AddedSafetyChecks = true;
3363 
3364   // We currently don't use LoopVersioning for the actual loop cloning but we
3365   // still use it to add the noalias metadata.
3366   LVer = std::make_unique<LoopVersioning>(
3367       *Legal->getLAI(),
3368       Legal->getLAI()->getRuntimePointerChecking()->getChecks(), OrigLoop, LI,
3369       DT, PSE.getSE());
3370   LVer->prepareNoAliasMetadata();
3371   return MemCheckBlock;
3372 }
3373 
3374 Value *InnerLoopVectorizer::emitTransformedIndex(
3375     IRBuilder<> &B, Value *Index, ScalarEvolution *SE, const DataLayout &DL,
3376     const InductionDescriptor &ID) const {
3377 
3378   SCEVExpander Exp(*SE, DL, "induction");
3379   auto Step = ID.getStep();
3380   auto StartValue = ID.getStartValue();
3381   assert(Index->getType()->getScalarType() == Step->getType() &&
3382          "Index scalar type does not match StepValue type");
3383 
3384   // Note: the IR at this point is broken. We cannot use SE to create any new
3385   // SCEV and then expand it, hoping that SCEV's simplification will give us
3386   // a more optimal code. Unfortunately, attempt of doing so on invalid IR may
3387   // lead to various SCEV crashes. So all we can do is to use builder and rely
3388   // on InstCombine for future simplifications. Here we handle some trivial
3389   // cases only.
3390   auto CreateAdd = [&B](Value *X, Value *Y) {
3391     assert(X->getType() == Y->getType() && "Types don't match!");
3392     if (auto *CX = dyn_cast<ConstantInt>(X))
3393       if (CX->isZero())
3394         return Y;
3395     if (auto *CY = dyn_cast<ConstantInt>(Y))
3396       if (CY->isZero())
3397         return X;
3398     return B.CreateAdd(X, Y);
3399   };
3400 
3401   // We allow X to be a vector type, in which case Y will potentially be
3402   // splatted into a vector with the same element count.
3403   auto CreateMul = [&B](Value *X, Value *Y) {
3404     assert(X->getType()->getScalarType() == Y->getType() &&
3405            "Types don't match!");
3406     if (auto *CX = dyn_cast<ConstantInt>(X))
3407       if (CX->isOne())
3408         return Y;
3409     if (auto *CY = dyn_cast<ConstantInt>(Y))
3410       if (CY->isOne())
3411         return X;
3412     VectorType *XVTy = dyn_cast<VectorType>(X->getType());
3413     if (XVTy && !isa<VectorType>(Y->getType()))
3414       Y = B.CreateVectorSplat(XVTy->getElementCount(), Y);
3415     return B.CreateMul(X, Y);
3416   };
3417 
3418   // Get a suitable insert point for SCEV expansion. For blocks in the vector
3419   // loop, choose the end of the vector loop header (=LoopVectorBody), because
3420   // the DomTree is not kept up-to-date for additional blocks generated in the
3421   // vector loop. By using the header as insertion point, we guarantee that the
3422   // expanded instructions dominate all their uses.
3423   auto GetInsertPoint = [this, &B]() {
3424     BasicBlock *InsertBB = B.GetInsertPoint()->getParent();
3425     if (InsertBB != LoopVectorBody &&
3426         LI->getLoopFor(LoopVectorBody) == LI->getLoopFor(InsertBB))
3427       return LoopVectorBody->getTerminator();
3428     return &*B.GetInsertPoint();
3429   };
3430 
3431   switch (ID.getKind()) {
3432   case InductionDescriptor::IK_IntInduction: {
3433     assert(!isa<VectorType>(Index->getType()) &&
3434            "Vector indices not supported for integer inductions yet");
3435     assert(Index->getType() == StartValue->getType() &&
3436            "Index type does not match StartValue type");
3437     if (ID.getConstIntStepValue() && ID.getConstIntStepValue()->isMinusOne())
3438       return B.CreateSub(StartValue, Index);
3439     auto *Offset = CreateMul(
3440         Index, Exp.expandCodeFor(Step, Index->getType(), GetInsertPoint()));
3441     return CreateAdd(StartValue, Offset);
3442   }
3443   case InductionDescriptor::IK_PtrInduction: {
3444     assert(isa<SCEVConstant>(Step) &&
3445            "Expected constant step for pointer induction");
3446     return B.CreateGEP(
3447         ID.getElementType(), StartValue,
3448         CreateMul(Index,
3449                   Exp.expandCodeFor(Step, Index->getType()->getScalarType(),
3450                                     GetInsertPoint())));
3451   }
3452   case InductionDescriptor::IK_FpInduction: {
3453     assert(!isa<VectorType>(Index->getType()) &&
3454            "Vector indices not supported for FP inductions yet");
3455     assert(Step->getType()->isFloatingPointTy() && "Expected FP Step value");
3456     auto InductionBinOp = ID.getInductionBinOp();
3457     assert(InductionBinOp &&
3458            (InductionBinOp->getOpcode() == Instruction::FAdd ||
3459             InductionBinOp->getOpcode() == Instruction::FSub) &&
3460            "Original bin op should be defined for FP induction");
3461 
3462     Value *StepValue = cast<SCEVUnknown>(Step)->getValue();
3463     Value *MulExp = B.CreateFMul(StepValue, Index);
3464     return B.CreateBinOp(InductionBinOp->getOpcode(), StartValue, MulExp,
3465                          "induction");
3466   }
3467   case InductionDescriptor::IK_NoInduction:
3468     return nullptr;
3469   }
3470   llvm_unreachable("invalid enum");
3471 }
3472 
3473 Loop *InnerLoopVectorizer::createVectorLoopSkeleton(StringRef Prefix) {
3474   LoopScalarBody = OrigLoop->getHeader();
3475   LoopVectorPreHeader = OrigLoop->getLoopPreheader();
3476   assert(LoopVectorPreHeader && "Invalid loop structure");
3477   LoopExitBlock = OrigLoop->getUniqueExitBlock(); // may be nullptr
3478   assert((LoopExitBlock || Cost->requiresScalarEpilogue(VF)) &&
3479          "multiple exit loop without required epilogue?");
3480 
3481   LoopMiddleBlock =
3482       SplitBlock(LoopVectorPreHeader, LoopVectorPreHeader->getTerminator(), DT,
3483                  LI, nullptr, Twine(Prefix) + "middle.block");
3484   LoopScalarPreHeader =
3485       SplitBlock(LoopMiddleBlock, LoopMiddleBlock->getTerminator(), DT, LI,
3486                  nullptr, Twine(Prefix) + "scalar.ph");
3487 
3488   auto *ScalarLatchTerm = OrigLoop->getLoopLatch()->getTerminator();
3489 
3490   // Set up the middle block terminator.  Two cases:
3491   // 1) If we know that we must execute the scalar epilogue, emit an
3492   //    unconditional branch.
3493   // 2) Otherwise, we must have a single unique exit block (due to how we
3494   //    implement the multiple exit case).  In this case, set up a conditonal
3495   //    branch from the middle block to the loop scalar preheader, and the
3496   //    exit block.  completeLoopSkeleton will update the condition to use an
3497   //    iteration check, if required to decide whether to execute the remainder.
3498   BranchInst *BrInst = Cost->requiresScalarEpilogue(VF) ?
3499     BranchInst::Create(LoopScalarPreHeader) :
3500     BranchInst::Create(LoopExitBlock, LoopScalarPreHeader,
3501                        Builder.getTrue());
3502   BrInst->setDebugLoc(ScalarLatchTerm->getDebugLoc());
3503   ReplaceInstWithInst(LoopMiddleBlock->getTerminator(), BrInst);
3504 
3505   // We intentionally don't let SplitBlock to update LoopInfo since
3506   // LoopVectorBody should belong to another loop than LoopVectorPreHeader.
3507   // LoopVectorBody is explicitly added to the correct place few lines later.
3508   LoopVectorBody =
3509       SplitBlock(LoopVectorPreHeader, LoopVectorPreHeader->getTerminator(), DT,
3510                  nullptr, nullptr, Twine(Prefix) + "vector.body");
3511 
3512   // Update dominator for loop exit.
3513   if (!Cost->requiresScalarEpilogue(VF))
3514     // If there is an epilogue which must run, there's no edge from the
3515     // middle block to exit blocks  and thus no need to update the immediate
3516     // dominator of the exit blocks.
3517     DT->changeImmediateDominator(LoopExitBlock, LoopMiddleBlock);
3518 
3519   // Create and register the new vector loop.
3520   Loop *Lp = LI->AllocateLoop();
3521   Loop *ParentLoop = OrigLoop->getParentLoop();
3522 
3523   // Insert the new loop into the loop nest and register the new basic blocks
3524   // before calling any utilities such as SCEV that require valid LoopInfo.
3525   if (ParentLoop) {
3526     ParentLoop->addChildLoop(Lp);
3527   } else {
3528     LI->addTopLevelLoop(Lp);
3529   }
3530   Lp->addBasicBlockToLoop(LoopVectorBody, *LI);
3531   return Lp;
3532 }
3533 
3534 void InnerLoopVectorizer::createInductionResumeValues(
3535     Loop *L, Value *VectorTripCount,
3536     std::pair<BasicBlock *, Value *> AdditionalBypass) {
3537   assert(VectorTripCount && L && "Expected valid arguments");
3538   assert(((AdditionalBypass.first && AdditionalBypass.second) ||
3539           (!AdditionalBypass.first && !AdditionalBypass.second)) &&
3540          "Inconsistent information about additional bypass.");
3541   // We are going to resume the execution of the scalar loop.
3542   // Go over all of the induction variables that we found and fix the
3543   // PHIs that are left in the scalar version of the loop.
3544   // The starting values of PHI nodes depend on the counter of the last
3545   // iteration in the vectorized loop.
3546   // If we come from a bypass edge then we need to start from the original
3547   // start value.
3548   for (auto &InductionEntry : Legal->getInductionVars()) {
3549     PHINode *OrigPhi = InductionEntry.first;
3550     InductionDescriptor II = InductionEntry.second;
3551 
3552     // Create phi nodes to merge from the  backedge-taken check block.
3553     PHINode *BCResumeVal =
3554         PHINode::Create(OrigPhi->getType(), 3, "bc.resume.val",
3555                         LoopScalarPreHeader->getTerminator());
3556     // Copy original phi DL over to the new one.
3557     BCResumeVal->setDebugLoc(OrigPhi->getDebugLoc());
3558     Value *&EndValue = IVEndValues[OrigPhi];
3559     Value *EndValueFromAdditionalBypass = AdditionalBypass.second;
3560     if (OrigPhi == OldInduction) {
3561       // We know what the end value is.
3562       EndValue = VectorTripCount;
3563     } else {
3564       IRBuilder<> B(L->getLoopPreheader()->getTerminator());
3565 
3566       // Fast-math-flags propagate from the original induction instruction.
3567       if (II.getInductionBinOp() && isa<FPMathOperator>(II.getInductionBinOp()))
3568         B.setFastMathFlags(II.getInductionBinOp()->getFastMathFlags());
3569 
3570       Type *StepType = II.getStep()->getType();
3571       Instruction::CastOps CastOp =
3572           CastInst::getCastOpcode(VectorTripCount, true, StepType, true);
3573       Value *CRD = B.CreateCast(CastOp, VectorTripCount, StepType, "cast.crd");
3574       const DataLayout &DL = LoopScalarBody->getModule()->getDataLayout();
3575       EndValue = emitTransformedIndex(B, CRD, PSE.getSE(), DL, II);
3576       EndValue->setName("ind.end");
3577 
3578       // Compute the end value for the additional bypass (if applicable).
3579       if (AdditionalBypass.first) {
3580         B.SetInsertPoint(&(*AdditionalBypass.first->getFirstInsertionPt()));
3581         CastOp = CastInst::getCastOpcode(AdditionalBypass.second, true,
3582                                          StepType, true);
3583         CRD =
3584             B.CreateCast(CastOp, AdditionalBypass.second, StepType, "cast.crd");
3585         EndValueFromAdditionalBypass =
3586             emitTransformedIndex(B, CRD, PSE.getSE(), DL, II);
3587         EndValueFromAdditionalBypass->setName("ind.end");
3588       }
3589     }
3590     // The new PHI merges the original incoming value, in case of a bypass,
3591     // or the value at the end of the vectorized loop.
3592     BCResumeVal->addIncoming(EndValue, LoopMiddleBlock);
3593 
3594     // Fix the scalar body counter (PHI node).
3595     // The old induction's phi node in the scalar body needs the truncated
3596     // value.
3597     for (BasicBlock *BB : LoopBypassBlocks)
3598       BCResumeVal->addIncoming(II.getStartValue(), BB);
3599 
3600     if (AdditionalBypass.first)
3601       BCResumeVal->setIncomingValueForBlock(AdditionalBypass.first,
3602                                             EndValueFromAdditionalBypass);
3603 
3604     OrigPhi->setIncomingValueForBlock(LoopScalarPreHeader, BCResumeVal);
3605   }
3606 }
3607 
3608 BasicBlock *InnerLoopVectorizer::completeLoopSkeleton(Loop *L,
3609                                                       MDNode *OrigLoopID) {
3610   assert(L && "Expected valid loop.");
3611 
3612   // The trip counts should be cached by now.
3613   Value *Count = getOrCreateTripCount(L);
3614   Value *VectorTripCount = getOrCreateVectorTripCount(L);
3615 
3616   auto *ScalarLatchTerm = OrigLoop->getLoopLatch()->getTerminator();
3617 
3618   // Add a check in the middle block to see if we have completed
3619   // all of the iterations in the first vector loop.  Three cases:
3620   // 1) If we require a scalar epilogue, there is no conditional branch as
3621   //    we unconditionally branch to the scalar preheader.  Do nothing.
3622   // 2) If (N - N%VF) == N, then we *don't* need to run the remainder.
3623   //    Thus if tail is to be folded, we know we don't need to run the
3624   //    remainder and we can use the previous value for the condition (true).
3625   // 3) Otherwise, construct a runtime check.
3626   if (!Cost->requiresScalarEpilogue(VF) && !Cost->foldTailByMasking()) {
3627     Instruction *CmpN = CmpInst::Create(Instruction::ICmp, CmpInst::ICMP_EQ,
3628                                         Count, VectorTripCount, "cmp.n",
3629                                         LoopMiddleBlock->getTerminator());
3630 
3631     // Here we use the same DebugLoc as the scalar loop latch terminator instead
3632     // of the corresponding compare because they may have ended up with
3633     // different line numbers and we want to avoid awkward line stepping while
3634     // debugging. Eg. if the compare has got a line number inside the loop.
3635     CmpN->setDebugLoc(ScalarLatchTerm->getDebugLoc());
3636     cast<BranchInst>(LoopMiddleBlock->getTerminator())->setCondition(CmpN);
3637   }
3638 
3639   // Get ready to start creating new instructions into the vectorized body.
3640   assert(LoopVectorPreHeader == L->getLoopPreheader() &&
3641          "Inconsistent vector loop preheader");
3642   Builder.SetInsertPoint(&*LoopVectorBody->getFirstInsertionPt());
3643 
3644   Optional<MDNode *> VectorizedLoopID =
3645       makeFollowupLoopID(OrigLoopID, {LLVMLoopVectorizeFollowupAll,
3646                                       LLVMLoopVectorizeFollowupVectorized});
3647   if (VectorizedLoopID.hasValue()) {
3648     L->setLoopID(VectorizedLoopID.getValue());
3649 
3650     // Do not setAlreadyVectorized if loop attributes have been defined
3651     // explicitly.
3652     return LoopVectorPreHeader;
3653   }
3654 
3655   // Keep all loop hints from the original loop on the vector loop (we'll
3656   // replace the vectorizer-specific hints below).
3657   if (MDNode *LID = OrigLoop->getLoopID())
3658     L->setLoopID(LID);
3659 
3660   LoopVectorizeHints Hints(L, true, *ORE);
3661   Hints.setAlreadyVectorized();
3662 
3663 #ifdef EXPENSIVE_CHECKS
3664   assert(DT->verify(DominatorTree::VerificationLevel::Fast));
3665   LI->verify(*DT);
3666 #endif
3667 
3668   return LoopVectorPreHeader;
3669 }
3670 
3671 BasicBlock *InnerLoopVectorizer::createVectorizedLoopSkeleton() {
3672   /*
3673    In this function we generate a new loop. The new loop will contain
3674    the vectorized instructions while the old loop will continue to run the
3675    scalar remainder.
3676 
3677        [ ] <-- loop iteration number check.
3678     /   |
3679    /    v
3680   |    [ ] <-- vector loop bypass (may consist of multiple blocks).
3681   |  /  |
3682   | /   v
3683   ||   [ ]     <-- vector pre header.
3684   |/    |
3685   |     v
3686   |    [  ] \
3687   |    [  ]_|   <-- vector loop.
3688   |     |
3689   |     v
3690   \   -[ ]   <--- middle-block.
3691    \/   |
3692    /\   v
3693    | ->[ ]     <--- new preheader.
3694    |    |
3695  (opt)  v      <-- edge from middle to exit iff epilogue is not required.
3696    |   [ ] \
3697    |   [ ]_|   <-- old scalar loop to handle remainder (scalar epilogue).
3698     \   |
3699      \  v
3700       >[ ]     <-- exit block(s).
3701    ...
3702    */
3703 
3704   // Get the metadata of the original loop before it gets modified.
3705   MDNode *OrigLoopID = OrigLoop->getLoopID();
3706 
3707   // Workaround!  Compute the trip count of the original loop and cache it
3708   // before we start modifying the CFG.  This code has a systemic problem
3709   // wherein it tries to run analysis over partially constructed IR; this is
3710   // wrong, and not simply for SCEV.  The trip count of the original loop
3711   // simply happens to be prone to hitting this in practice.  In theory, we
3712   // can hit the same issue for any SCEV, or ValueTracking query done during
3713   // mutation.  See PR49900.
3714   getOrCreateTripCount(OrigLoop);
3715 
3716   // Create an empty vector loop, and prepare basic blocks for the runtime
3717   // checks.
3718   Loop *Lp = createVectorLoopSkeleton("");
3719 
3720   // Now, compare the new count to zero. If it is zero skip the vector loop and
3721   // jump to the scalar loop. This check also covers the case where the
3722   // backedge-taken count is uint##_max: adding one to it will overflow leading
3723   // to an incorrect trip count of zero. In this (rare) case we will also jump
3724   // to the scalar loop.
3725   emitMinimumIterationCountCheck(Lp, LoopScalarPreHeader);
3726 
3727   // Generate the code to check any assumptions that we've made for SCEV
3728   // expressions.
3729   emitSCEVChecks(Lp, LoopScalarPreHeader);
3730 
3731   // Generate the code that checks in runtime if arrays overlap. We put the
3732   // checks into a separate block to make the more common case of few elements
3733   // faster.
3734   emitMemRuntimeChecks(Lp, LoopScalarPreHeader);
3735 
3736   // Some loops have a single integer induction variable, while other loops
3737   // don't. One example is c++ iterators that often have multiple pointer
3738   // induction variables. In the code below we also support a case where we
3739   // don't have a single induction variable.
3740   //
3741   // We try to obtain an induction variable from the original loop as hard
3742   // as possible. However if we don't find one that:
3743   //   - is an integer
3744   //   - counts from zero, stepping by one
3745   //   - is the size of the widest induction variable type
3746   // then we create a new one.
3747   OldInduction = Legal->getPrimaryInduction();
3748   Type *IdxTy = Legal->getWidestInductionType();
3749   Value *StartIdx = ConstantInt::get(IdxTy, 0);
3750   // The loop step is equal to the vectorization factor (num of SIMD elements)
3751   // times the unroll factor (num of SIMD instructions).
3752   Builder.SetInsertPoint(&*Lp->getHeader()->getFirstInsertionPt());
3753   Value *Step = createStepForVF(Builder, ConstantInt::get(IdxTy, UF), VF);
3754   Value *CountRoundDown = getOrCreateVectorTripCount(Lp);
3755   Induction =
3756       createInductionVariable(Lp, StartIdx, CountRoundDown, Step,
3757                               getDebugLocFromInstOrOperands(OldInduction));
3758 
3759   // Emit phis for the new starting index of the scalar loop.
3760   createInductionResumeValues(Lp, CountRoundDown);
3761 
3762   return completeLoopSkeleton(Lp, OrigLoopID);
3763 }
3764 
3765 // Fix up external users of the induction variable. At this point, we are
3766 // in LCSSA form, with all external PHIs that use the IV having one input value,
3767 // coming from the remainder loop. We need those PHIs to also have a correct
3768 // value for the IV when arriving directly from the middle block.
3769 void InnerLoopVectorizer::fixupIVUsers(PHINode *OrigPhi,
3770                                        const InductionDescriptor &II,
3771                                        Value *CountRoundDown, Value *EndValue,
3772                                        BasicBlock *MiddleBlock) {
3773   // There are two kinds of external IV usages - those that use the value
3774   // computed in the last iteration (the PHI) and those that use the penultimate
3775   // value (the value that feeds into the phi from the loop latch).
3776   // We allow both, but they, obviously, have different values.
3777 
3778   assert(OrigLoop->getUniqueExitBlock() && "Expected a single exit block");
3779 
3780   DenseMap<Value *, Value *> MissingVals;
3781 
3782   // An external user of the last iteration's value should see the value that
3783   // the remainder loop uses to initialize its own IV.
3784   Value *PostInc = OrigPhi->getIncomingValueForBlock(OrigLoop->getLoopLatch());
3785   for (User *U : PostInc->users()) {
3786     Instruction *UI = cast<Instruction>(U);
3787     if (!OrigLoop->contains(UI)) {
3788       assert(isa<PHINode>(UI) && "Expected LCSSA form");
3789       MissingVals[UI] = EndValue;
3790     }
3791   }
3792 
3793   // An external user of the penultimate value need to see EndValue - Step.
3794   // The simplest way to get this is to recompute it from the constituent SCEVs,
3795   // that is Start + (Step * (CRD - 1)).
3796   for (User *U : OrigPhi->users()) {
3797     auto *UI = cast<Instruction>(U);
3798     if (!OrigLoop->contains(UI)) {
3799       const DataLayout &DL =
3800           OrigLoop->getHeader()->getModule()->getDataLayout();
3801       assert(isa<PHINode>(UI) && "Expected LCSSA form");
3802 
3803       IRBuilder<> B(MiddleBlock->getTerminator());
3804 
3805       // Fast-math-flags propagate from the original induction instruction.
3806       if (II.getInductionBinOp() && isa<FPMathOperator>(II.getInductionBinOp()))
3807         B.setFastMathFlags(II.getInductionBinOp()->getFastMathFlags());
3808 
3809       Value *CountMinusOne = B.CreateSub(
3810           CountRoundDown, ConstantInt::get(CountRoundDown->getType(), 1));
3811       Value *CMO =
3812           !II.getStep()->getType()->isIntegerTy()
3813               ? B.CreateCast(Instruction::SIToFP, CountMinusOne,
3814                              II.getStep()->getType())
3815               : B.CreateSExtOrTrunc(CountMinusOne, II.getStep()->getType());
3816       CMO->setName("cast.cmo");
3817       Value *Escape = emitTransformedIndex(B, CMO, PSE.getSE(), DL, II);
3818       Escape->setName("ind.escape");
3819       MissingVals[UI] = Escape;
3820     }
3821   }
3822 
3823   for (auto &I : MissingVals) {
3824     PHINode *PHI = cast<PHINode>(I.first);
3825     // One corner case we have to handle is two IVs "chasing" each-other,
3826     // that is %IV2 = phi [...], [ %IV1, %latch ]
3827     // In this case, if IV1 has an external use, we need to avoid adding both
3828     // "last value of IV1" and "penultimate value of IV2". So, verify that we
3829     // don't already have an incoming value for the middle block.
3830     if (PHI->getBasicBlockIndex(MiddleBlock) == -1)
3831       PHI->addIncoming(I.second, MiddleBlock);
3832   }
3833 }
3834 
3835 namespace {
3836 
3837 struct CSEDenseMapInfo {
3838   static bool canHandle(const Instruction *I) {
3839     return isa<InsertElementInst>(I) || isa<ExtractElementInst>(I) ||
3840            isa<ShuffleVectorInst>(I) || isa<GetElementPtrInst>(I);
3841   }
3842 
3843   static inline Instruction *getEmptyKey() {
3844     return DenseMapInfo<Instruction *>::getEmptyKey();
3845   }
3846 
3847   static inline Instruction *getTombstoneKey() {
3848     return DenseMapInfo<Instruction *>::getTombstoneKey();
3849   }
3850 
3851   static unsigned getHashValue(const Instruction *I) {
3852     assert(canHandle(I) && "Unknown instruction!");
3853     return hash_combine(I->getOpcode(), hash_combine_range(I->value_op_begin(),
3854                                                            I->value_op_end()));
3855   }
3856 
3857   static bool isEqual(const Instruction *LHS, const Instruction *RHS) {
3858     if (LHS == getEmptyKey() || RHS == getEmptyKey() ||
3859         LHS == getTombstoneKey() || RHS == getTombstoneKey())
3860       return LHS == RHS;
3861     return LHS->isIdenticalTo(RHS);
3862   }
3863 };
3864 
3865 } // end anonymous namespace
3866 
3867 ///Perform cse of induction variable instructions.
3868 static void cse(BasicBlock *BB) {
3869   // Perform simple cse.
3870   SmallDenseMap<Instruction *, Instruction *, 4, CSEDenseMapInfo> CSEMap;
3871   for (Instruction &In : llvm::make_early_inc_range(*BB)) {
3872     if (!CSEDenseMapInfo::canHandle(&In))
3873       continue;
3874 
3875     // Check if we can replace this instruction with any of the
3876     // visited instructions.
3877     if (Instruction *V = CSEMap.lookup(&In)) {
3878       In.replaceAllUsesWith(V);
3879       In.eraseFromParent();
3880       continue;
3881     }
3882 
3883     CSEMap[&In] = &In;
3884   }
3885 }
3886 
3887 InstructionCost
3888 LoopVectorizationCostModel::getVectorCallCost(CallInst *CI, ElementCount VF,
3889                                               bool &NeedToScalarize) const {
3890   Function *F = CI->getCalledFunction();
3891   Type *ScalarRetTy = CI->getType();
3892   SmallVector<Type *, 4> Tys, ScalarTys;
3893   for (auto &ArgOp : CI->args())
3894     ScalarTys.push_back(ArgOp->getType());
3895 
3896   // Estimate cost of scalarized vector call. The source operands are assumed
3897   // to be vectors, so we need to extract individual elements from there,
3898   // execute VF scalar calls, and then gather the result into the vector return
3899   // value.
3900   InstructionCost ScalarCallCost =
3901       TTI.getCallInstrCost(F, ScalarRetTy, ScalarTys, TTI::TCK_RecipThroughput);
3902   if (VF.isScalar())
3903     return ScalarCallCost;
3904 
3905   // Compute corresponding vector type for return value and arguments.
3906   Type *RetTy = ToVectorTy(ScalarRetTy, VF);
3907   for (Type *ScalarTy : ScalarTys)
3908     Tys.push_back(ToVectorTy(ScalarTy, VF));
3909 
3910   // Compute costs of unpacking argument values for the scalar calls and
3911   // packing the return values to a vector.
3912   InstructionCost ScalarizationCost = getScalarizationOverhead(CI, VF);
3913 
3914   InstructionCost Cost =
3915       ScalarCallCost * VF.getKnownMinValue() + ScalarizationCost;
3916 
3917   // If we can't emit a vector call for this function, then the currently found
3918   // cost is the cost we need to return.
3919   NeedToScalarize = true;
3920   VFShape Shape = VFShape::get(*CI, VF, false /*HasGlobalPred*/);
3921   Function *VecFunc = VFDatabase(*CI).getVectorizedFunction(Shape);
3922 
3923   if (!TLI || CI->isNoBuiltin() || !VecFunc)
3924     return Cost;
3925 
3926   // If the corresponding vector cost is cheaper, return its cost.
3927   InstructionCost VectorCallCost =
3928       TTI.getCallInstrCost(nullptr, RetTy, Tys, TTI::TCK_RecipThroughput);
3929   if (VectorCallCost < Cost) {
3930     NeedToScalarize = false;
3931     Cost = VectorCallCost;
3932   }
3933   return Cost;
3934 }
3935 
3936 static Type *MaybeVectorizeType(Type *Elt, ElementCount VF) {
3937   if (VF.isScalar() || (!Elt->isIntOrPtrTy() && !Elt->isFloatingPointTy()))
3938     return Elt;
3939   return VectorType::get(Elt, VF);
3940 }
3941 
3942 InstructionCost
3943 LoopVectorizationCostModel::getVectorIntrinsicCost(CallInst *CI,
3944                                                    ElementCount VF) const {
3945   Intrinsic::ID ID = getVectorIntrinsicIDForCall(CI, TLI);
3946   assert(ID && "Expected intrinsic call!");
3947   Type *RetTy = MaybeVectorizeType(CI->getType(), VF);
3948   FastMathFlags FMF;
3949   if (auto *FPMO = dyn_cast<FPMathOperator>(CI))
3950     FMF = FPMO->getFastMathFlags();
3951 
3952   SmallVector<const Value *> Arguments(CI->args());
3953   FunctionType *FTy = CI->getCalledFunction()->getFunctionType();
3954   SmallVector<Type *> ParamTys;
3955   std::transform(FTy->param_begin(), FTy->param_end(),
3956                  std::back_inserter(ParamTys),
3957                  [&](Type *Ty) { return MaybeVectorizeType(Ty, VF); });
3958 
3959   IntrinsicCostAttributes CostAttrs(ID, RetTy, Arguments, ParamTys, FMF,
3960                                     dyn_cast<IntrinsicInst>(CI));
3961   return TTI.getIntrinsicInstrCost(CostAttrs,
3962                                    TargetTransformInfo::TCK_RecipThroughput);
3963 }
3964 
3965 static Type *smallestIntegerVectorType(Type *T1, Type *T2) {
3966   auto *I1 = cast<IntegerType>(cast<VectorType>(T1)->getElementType());
3967   auto *I2 = cast<IntegerType>(cast<VectorType>(T2)->getElementType());
3968   return I1->getBitWidth() < I2->getBitWidth() ? T1 : T2;
3969 }
3970 
3971 static Type *largestIntegerVectorType(Type *T1, Type *T2) {
3972   auto *I1 = cast<IntegerType>(cast<VectorType>(T1)->getElementType());
3973   auto *I2 = cast<IntegerType>(cast<VectorType>(T2)->getElementType());
3974   return I1->getBitWidth() > I2->getBitWidth() ? T1 : T2;
3975 }
3976 
3977 void InnerLoopVectorizer::truncateToMinimalBitwidths(VPTransformState &State) {
3978   // For every instruction `I` in MinBWs, truncate the operands, create a
3979   // truncated version of `I` and reextend its result. InstCombine runs
3980   // later and will remove any ext/trunc pairs.
3981   SmallPtrSet<Value *, 4> Erased;
3982   for (const auto &KV : Cost->getMinimalBitwidths()) {
3983     // If the value wasn't vectorized, we must maintain the original scalar
3984     // type. The absence of the value from State indicates that it
3985     // wasn't vectorized.
3986     // FIXME: Should not rely on getVPValue at this point.
3987     VPValue *Def = State.Plan->getVPValue(KV.first, true);
3988     if (!State.hasAnyVectorValue(Def))
3989       continue;
3990     for (unsigned Part = 0; Part < UF; ++Part) {
3991       Value *I = State.get(Def, Part);
3992       if (Erased.count(I) || I->use_empty() || !isa<Instruction>(I))
3993         continue;
3994       Type *OriginalTy = I->getType();
3995       Type *ScalarTruncatedTy =
3996           IntegerType::get(OriginalTy->getContext(), KV.second);
3997       auto *TruncatedTy = VectorType::get(
3998           ScalarTruncatedTy, cast<VectorType>(OriginalTy)->getElementCount());
3999       if (TruncatedTy == OriginalTy)
4000         continue;
4001 
4002       IRBuilder<> B(cast<Instruction>(I));
4003       auto ShrinkOperand = [&](Value *V) -> Value * {
4004         if (auto *ZI = dyn_cast<ZExtInst>(V))
4005           if (ZI->getSrcTy() == TruncatedTy)
4006             return ZI->getOperand(0);
4007         return B.CreateZExtOrTrunc(V, TruncatedTy);
4008       };
4009 
4010       // The actual instruction modification depends on the instruction type,
4011       // unfortunately.
4012       Value *NewI = nullptr;
4013       if (auto *BO = dyn_cast<BinaryOperator>(I)) {
4014         NewI = B.CreateBinOp(BO->getOpcode(), ShrinkOperand(BO->getOperand(0)),
4015                              ShrinkOperand(BO->getOperand(1)));
4016 
4017         // Any wrapping introduced by shrinking this operation shouldn't be
4018         // considered undefined behavior. So, we can't unconditionally copy
4019         // arithmetic wrapping flags to NewI.
4020         cast<BinaryOperator>(NewI)->copyIRFlags(I, /*IncludeWrapFlags=*/false);
4021       } else if (auto *CI = dyn_cast<ICmpInst>(I)) {
4022         NewI =
4023             B.CreateICmp(CI->getPredicate(), ShrinkOperand(CI->getOperand(0)),
4024                          ShrinkOperand(CI->getOperand(1)));
4025       } else if (auto *SI = dyn_cast<SelectInst>(I)) {
4026         NewI = B.CreateSelect(SI->getCondition(),
4027                               ShrinkOperand(SI->getTrueValue()),
4028                               ShrinkOperand(SI->getFalseValue()));
4029       } else if (auto *CI = dyn_cast<CastInst>(I)) {
4030         switch (CI->getOpcode()) {
4031         default:
4032           llvm_unreachable("Unhandled cast!");
4033         case Instruction::Trunc:
4034           NewI = ShrinkOperand(CI->getOperand(0));
4035           break;
4036         case Instruction::SExt:
4037           NewI = B.CreateSExtOrTrunc(
4038               CI->getOperand(0),
4039               smallestIntegerVectorType(OriginalTy, TruncatedTy));
4040           break;
4041         case Instruction::ZExt:
4042           NewI = B.CreateZExtOrTrunc(
4043               CI->getOperand(0),
4044               smallestIntegerVectorType(OriginalTy, TruncatedTy));
4045           break;
4046         }
4047       } else if (auto *SI = dyn_cast<ShuffleVectorInst>(I)) {
4048         auto Elements0 =
4049             cast<VectorType>(SI->getOperand(0)->getType())->getElementCount();
4050         auto *O0 = B.CreateZExtOrTrunc(
4051             SI->getOperand(0), VectorType::get(ScalarTruncatedTy, Elements0));
4052         auto Elements1 =
4053             cast<VectorType>(SI->getOperand(1)->getType())->getElementCount();
4054         auto *O1 = B.CreateZExtOrTrunc(
4055             SI->getOperand(1), VectorType::get(ScalarTruncatedTy, Elements1));
4056 
4057         NewI = B.CreateShuffleVector(O0, O1, SI->getShuffleMask());
4058       } else if (isa<LoadInst>(I) || isa<PHINode>(I)) {
4059         // Don't do anything with the operands, just extend the result.
4060         continue;
4061       } else if (auto *IE = dyn_cast<InsertElementInst>(I)) {
4062         auto Elements =
4063             cast<VectorType>(IE->getOperand(0)->getType())->getElementCount();
4064         auto *O0 = B.CreateZExtOrTrunc(
4065             IE->getOperand(0), VectorType::get(ScalarTruncatedTy, Elements));
4066         auto *O1 = B.CreateZExtOrTrunc(IE->getOperand(1), ScalarTruncatedTy);
4067         NewI = B.CreateInsertElement(O0, O1, IE->getOperand(2));
4068       } else if (auto *EE = dyn_cast<ExtractElementInst>(I)) {
4069         auto Elements =
4070             cast<VectorType>(EE->getOperand(0)->getType())->getElementCount();
4071         auto *O0 = B.CreateZExtOrTrunc(
4072             EE->getOperand(0), VectorType::get(ScalarTruncatedTy, Elements));
4073         NewI = B.CreateExtractElement(O0, EE->getOperand(2));
4074       } else {
4075         // If we don't know what to do, be conservative and don't do anything.
4076         continue;
4077       }
4078 
4079       // Lastly, extend the result.
4080       NewI->takeName(cast<Instruction>(I));
4081       Value *Res = B.CreateZExtOrTrunc(NewI, OriginalTy);
4082       I->replaceAllUsesWith(Res);
4083       cast<Instruction>(I)->eraseFromParent();
4084       Erased.insert(I);
4085       State.reset(Def, Res, Part);
4086     }
4087   }
4088 
4089   // We'll have created a bunch of ZExts that are now parentless. Clean up.
4090   for (const auto &KV : Cost->getMinimalBitwidths()) {
4091     // If the value wasn't vectorized, we must maintain the original scalar
4092     // type. The absence of the value from State indicates that it
4093     // wasn't vectorized.
4094     // FIXME: Should not rely on getVPValue at this point.
4095     VPValue *Def = State.Plan->getVPValue(KV.first, true);
4096     if (!State.hasAnyVectorValue(Def))
4097       continue;
4098     for (unsigned Part = 0; Part < UF; ++Part) {
4099       Value *I = State.get(Def, Part);
4100       ZExtInst *Inst = dyn_cast<ZExtInst>(I);
4101       if (Inst && Inst->use_empty()) {
4102         Value *NewI = Inst->getOperand(0);
4103         Inst->eraseFromParent();
4104         State.reset(Def, NewI, Part);
4105       }
4106     }
4107   }
4108 }
4109 
4110 void InnerLoopVectorizer::fixVectorizedLoop(VPTransformState &State) {
4111   // Insert truncates and extends for any truncated instructions as hints to
4112   // InstCombine.
4113   if (VF.isVector())
4114     truncateToMinimalBitwidths(State);
4115 
4116   // Fix widened non-induction PHIs by setting up the PHI operands.
4117   if (OrigPHIsToFix.size()) {
4118     assert(EnableVPlanNativePath &&
4119            "Unexpected non-induction PHIs for fixup in non VPlan-native path");
4120     fixNonInductionPHIs(State);
4121   }
4122 
4123   // At this point every instruction in the original loop is widened to a
4124   // vector form. Now we need to fix the recurrences in the loop. These PHI
4125   // nodes are currently empty because we did not want to introduce cycles.
4126   // This is the second stage of vectorizing recurrences.
4127   fixCrossIterationPHIs(State);
4128 
4129   // Forget the original basic block.
4130   PSE.getSE()->forgetLoop(OrigLoop);
4131 
4132   // If we inserted an edge from the middle block to the unique exit block,
4133   // update uses outside the loop (phis) to account for the newly inserted
4134   // edge.
4135   if (!Cost->requiresScalarEpilogue(VF)) {
4136     // Fix-up external users of the induction variables.
4137     for (auto &Entry : Legal->getInductionVars())
4138       fixupIVUsers(Entry.first, Entry.second,
4139                    getOrCreateVectorTripCount(LI->getLoopFor(LoopVectorBody)),
4140                    IVEndValues[Entry.first], LoopMiddleBlock);
4141 
4142     fixLCSSAPHIs(State);
4143   }
4144 
4145   for (Instruction *PI : PredicatedInstructions)
4146     sinkScalarOperands(&*PI);
4147 
4148   // Remove redundant induction instructions.
4149   cse(LoopVectorBody);
4150 
4151   // Set/update profile weights for the vector and remainder loops as original
4152   // loop iterations are now distributed among them. Note that original loop
4153   // represented by LoopScalarBody becomes remainder loop after vectorization.
4154   //
4155   // For cases like foldTailByMasking() and requiresScalarEpiloque() we may
4156   // end up getting slightly roughened result but that should be OK since
4157   // profile is not inherently precise anyway. Note also possible bypass of
4158   // vector code caused by legality checks is ignored, assigning all the weight
4159   // to the vector loop, optimistically.
4160   //
4161   // For scalable vectorization we can't know at compile time how many iterations
4162   // of the loop are handled in one vector iteration, so instead assume a pessimistic
4163   // vscale of '1'.
4164   setProfileInfoAfterUnrolling(
4165       LI->getLoopFor(LoopScalarBody), LI->getLoopFor(LoopVectorBody),
4166       LI->getLoopFor(LoopScalarBody), VF.getKnownMinValue() * UF);
4167 }
4168 
4169 void InnerLoopVectorizer::fixCrossIterationPHIs(VPTransformState &State) {
4170   // In order to support recurrences we need to be able to vectorize Phi nodes.
4171   // Phi nodes have cycles, so we need to vectorize them in two stages. This is
4172   // stage #2: We now need to fix the recurrences by adding incoming edges to
4173   // the currently empty PHI nodes. At this point every instruction in the
4174   // original loop is widened to a vector form so we can use them to construct
4175   // the incoming edges.
4176   VPBasicBlock *Header = State.Plan->getEntry()->getEntryBasicBlock();
4177   for (VPRecipeBase &R : Header->phis()) {
4178     if (auto *ReductionPhi = dyn_cast<VPReductionPHIRecipe>(&R))
4179       fixReduction(ReductionPhi, State);
4180     else if (auto *FOR = dyn_cast<VPFirstOrderRecurrencePHIRecipe>(&R))
4181       fixFirstOrderRecurrence(FOR, State);
4182   }
4183 }
4184 
4185 void InnerLoopVectorizer::fixFirstOrderRecurrence(VPWidenPHIRecipe *PhiR,
4186                                                   VPTransformState &State) {
4187   // This is the second phase of vectorizing first-order recurrences. An
4188   // overview of the transformation is described below. Suppose we have the
4189   // following loop.
4190   //
4191   //   for (int i = 0; i < n; ++i)
4192   //     b[i] = a[i] - a[i - 1];
4193   //
4194   // There is a first-order recurrence on "a". For this loop, the shorthand
4195   // scalar IR looks like:
4196   //
4197   //   scalar.ph:
4198   //     s_init = a[-1]
4199   //     br scalar.body
4200   //
4201   //   scalar.body:
4202   //     i = phi [0, scalar.ph], [i+1, scalar.body]
4203   //     s1 = phi [s_init, scalar.ph], [s2, scalar.body]
4204   //     s2 = a[i]
4205   //     b[i] = s2 - s1
4206   //     br cond, scalar.body, ...
4207   //
4208   // In this example, s1 is a recurrence because it's value depends on the
4209   // previous iteration. In the first phase of vectorization, we created a
4210   // vector phi v1 for s1. We now complete the vectorization and produce the
4211   // shorthand vector IR shown below (for VF = 4, UF = 1).
4212   //
4213   //   vector.ph:
4214   //     v_init = vector(..., ..., ..., a[-1])
4215   //     br vector.body
4216   //
4217   //   vector.body
4218   //     i = phi [0, vector.ph], [i+4, vector.body]
4219   //     v1 = phi [v_init, vector.ph], [v2, vector.body]
4220   //     v2 = a[i, i+1, i+2, i+3];
4221   //     v3 = vector(v1(3), v2(0, 1, 2))
4222   //     b[i, i+1, i+2, i+3] = v2 - v3
4223   //     br cond, vector.body, middle.block
4224   //
4225   //   middle.block:
4226   //     x = v2(3)
4227   //     br scalar.ph
4228   //
4229   //   scalar.ph:
4230   //     s_init = phi [x, middle.block], [a[-1], otherwise]
4231   //     br scalar.body
4232   //
4233   // After execution completes the vector loop, we extract the next value of
4234   // the recurrence (x) to use as the initial value in the scalar loop.
4235 
4236   // Extract the last vector element in the middle block. This will be the
4237   // initial value for the recurrence when jumping to the scalar loop.
4238   VPValue *PreviousDef = PhiR->getBackedgeValue();
4239   Value *Incoming = State.get(PreviousDef, UF - 1);
4240   auto *ExtractForScalar = Incoming;
4241   auto *IdxTy = Builder.getInt32Ty();
4242   if (VF.isVector()) {
4243     auto *One = ConstantInt::get(IdxTy, 1);
4244     Builder.SetInsertPoint(LoopMiddleBlock->getTerminator());
4245     auto *RuntimeVF = getRuntimeVF(Builder, IdxTy, VF);
4246     auto *LastIdx = Builder.CreateSub(RuntimeVF, One);
4247     ExtractForScalar = Builder.CreateExtractElement(ExtractForScalar, LastIdx,
4248                                                     "vector.recur.extract");
4249   }
4250   // Extract the second last element in the middle block if the
4251   // Phi is used outside the loop. We need to extract the phi itself
4252   // and not the last element (the phi update in the current iteration). This
4253   // will be the value when jumping to the exit block from the LoopMiddleBlock,
4254   // when the scalar loop is not run at all.
4255   Value *ExtractForPhiUsedOutsideLoop = nullptr;
4256   if (VF.isVector()) {
4257     auto *RuntimeVF = getRuntimeVF(Builder, IdxTy, VF);
4258     auto *Idx = Builder.CreateSub(RuntimeVF, ConstantInt::get(IdxTy, 2));
4259     ExtractForPhiUsedOutsideLoop = Builder.CreateExtractElement(
4260         Incoming, Idx, "vector.recur.extract.for.phi");
4261   } else if (UF > 1)
4262     // When loop is unrolled without vectorizing, initialize
4263     // ExtractForPhiUsedOutsideLoop with the value just prior to unrolled value
4264     // of `Incoming`. This is analogous to the vectorized case above: extracting
4265     // the second last element when VF > 1.
4266     ExtractForPhiUsedOutsideLoop = State.get(PreviousDef, UF - 2);
4267 
4268   // Fix the initial value of the original recurrence in the scalar loop.
4269   Builder.SetInsertPoint(&*LoopScalarPreHeader->begin());
4270   PHINode *Phi = cast<PHINode>(PhiR->getUnderlyingValue());
4271   auto *Start = Builder.CreatePHI(Phi->getType(), 2, "scalar.recur.init");
4272   auto *ScalarInit = PhiR->getStartValue()->getLiveInIRValue();
4273   for (auto *BB : predecessors(LoopScalarPreHeader)) {
4274     auto *Incoming = BB == LoopMiddleBlock ? ExtractForScalar : ScalarInit;
4275     Start->addIncoming(Incoming, BB);
4276   }
4277 
4278   Phi->setIncomingValueForBlock(LoopScalarPreHeader, Start);
4279   Phi->setName("scalar.recur");
4280 
4281   // Finally, fix users of the recurrence outside the loop. The users will need
4282   // either the last value of the scalar recurrence or the last value of the
4283   // vector recurrence we extracted in the middle block. Since the loop is in
4284   // LCSSA form, we just need to find all the phi nodes for the original scalar
4285   // recurrence in the exit block, and then add an edge for the middle block.
4286   // Note that LCSSA does not imply single entry when the original scalar loop
4287   // had multiple exiting edges (as we always run the last iteration in the
4288   // scalar epilogue); in that case, there is no edge from middle to exit and
4289   // and thus no phis which needed updated.
4290   if (!Cost->requiresScalarEpilogue(VF))
4291     for (PHINode &LCSSAPhi : LoopExitBlock->phis())
4292       if (llvm::is_contained(LCSSAPhi.incoming_values(), Phi))
4293         LCSSAPhi.addIncoming(ExtractForPhiUsedOutsideLoop, LoopMiddleBlock);
4294 }
4295 
4296 void InnerLoopVectorizer::fixReduction(VPReductionPHIRecipe *PhiR,
4297                                        VPTransformState &State) {
4298   PHINode *OrigPhi = cast<PHINode>(PhiR->getUnderlyingValue());
4299   // Get it's reduction variable descriptor.
4300   assert(Legal->isReductionVariable(OrigPhi) &&
4301          "Unable to find the reduction variable");
4302   const RecurrenceDescriptor &RdxDesc = PhiR->getRecurrenceDescriptor();
4303 
4304   RecurKind RK = RdxDesc.getRecurrenceKind();
4305   TrackingVH<Value> ReductionStartValue = RdxDesc.getRecurrenceStartValue();
4306   Instruction *LoopExitInst = RdxDesc.getLoopExitInstr();
4307   setDebugLocFromInst(ReductionStartValue);
4308 
4309   VPValue *LoopExitInstDef = PhiR->getBackedgeValue();
4310   // This is the vector-clone of the value that leaves the loop.
4311   Type *VecTy = State.get(LoopExitInstDef, 0)->getType();
4312 
4313   // Wrap flags are in general invalid after vectorization, clear them.
4314   clearReductionWrapFlags(RdxDesc, State);
4315 
4316   // Before each round, move the insertion point right between
4317   // the PHIs and the values we are going to write.
4318   // This allows us to write both PHINodes and the extractelement
4319   // instructions.
4320   Builder.SetInsertPoint(&*LoopMiddleBlock->getFirstInsertionPt());
4321 
4322   setDebugLocFromInst(LoopExitInst);
4323 
4324   Type *PhiTy = OrigPhi->getType();
4325   // If tail is folded by masking, the vector value to leave the loop should be
4326   // a Select choosing between the vectorized LoopExitInst and vectorized Phi,
4327   // instead of the former. For an inloop reduction the reduction will already
4328   // be predicated, and does not need to be handled here.
4329   if (Cost->foldTailByMasking() && !PhiR->isInLoop()) {
4330     for (unsigned Part = 0; Part < UF; ++Part) {
4331       Value *VecLoopExitInst = State.get(LoopExitInstDef, Part);
4332       Value *Sel = nullptr;
4333       for (User *U : VecLoopExitInst->users()) {
4334         if (isa<SelectInst>(U)) {
4335           assert(!Sel && "Reduction exit feeding two selects");
4336           Sel = U;
4337         } else
4338           assert(isa<PHINode>(U) && "Reduction exit must feed Phi's or select");
4339       }
4340       assert(Sel && "Reduction exit feeds no select");
4341       State.reset(LoopExitInstDef, Sel, Part);
4342 
4343       // If the target can create a predicated operator for the reduction at no
4344       // extra cost in the loop (for example a predicated vadd), it can be
4345       // cheaper for the select to remain in the loop than be sunk out of it,
4346       // and so use the select value for the phi instead of the old
4347       // LoopExitValue.
4348       if (PreferPredicatedReductionSelect ||
4349           TTI->preferPredicatedReductionSelect(
4350               RdxDesc.getOpcode(), PhiTy,
4351               TargetTransformInfo::ReductionFlags())) {
4352         auto *VecRdxPhi =
4353             cast<PHINode>(State.get(PhiR, Part));
4354         VecRdxPhi->setIncomingValueForBlock(
4355             LI->getLoopFor(LoopVectorBody)->getLoopLatch(), Sel);
4356       }
4357     }
4358   }
4359 
4360   // If the vector reduction can be performed in a smaller type, we truncate
4361   // then extend the loop exit value to enable InstCombine to evaluate the
4362   // entire expression in the smaller type.
4363   if (VF.isVector() && PhiTy != RdxDesc.getRecurrenceType()) {
4364     assert(!PhiR->isInLoop() && "Unexpected truncated inloop reduction!");
4365     Type *RdxVecTy = VectorType::get(RdxDesc.getRecurrenceType(), VF);
4366     Builder.SetInsertPoint(
4367         LI->getLoopFor(LoopVectorBody)->getLoopLatch()->getTerminator());
4368     VectorParts RdxParts(UF);
4369     for (unsigned Part = 0; Part < UF; ++Part) {
4370       RdxParts[Part] = State.get(LoopExitInstDef, Part);
4371       Value *Trunc = Builder.CreateTrunc(RdxParts[Part], RdxVecTy);
4372       Value *Extnd = RdxDesc.isSigned() ? Builder.CreateSExt(Trunc, VecTy)
4373                                         : Builder.CreateZExt(Trunc, VecTy);
4374       for (Value::user_iterator UI = RdxParts[Part]->user_begin();
4375            UI != RdxParts[Part]->user_end();)
4376         if (*UI != Trunc) {
4377           (*UI++)->replaceUsesOfWith(RdxParts[Part], Extnd);
4378           RdxParts[Part] = Extnd;
4379         } else {
4380           ++UI;
4381         }
4382     }
4383     Builder.SetInsertPoint(&*LoopMiddleBlock->getFirstInsertionPt());
4384     for (unsigned Part = 0; Part < UF; ++Part) {
4385       RdxParts[Part] = Builder.CreateTrunc(RdxParts[Part], RdxVecTy);
4386       State.reset(LoopExitInstDef, RdxParts[Part], Part);
4387     }
4388   }
4389 
4390   // Reduce all of the unrolled parts into a single vector.
4391   Value *ReducedPartRdx = State.get(LoopExitInstDef, 0);
4392   unsigned Op = RecurrenceDescriptor::getOpcode(RK);
4393 
4394   // The middle block terminator has already been assigned a DebugLoc here (the
4395   // OrigLoop's single latch terminator). We want the whole middle block to
4396   // appear to execute on this line because: (a) it is all compiler generated,
4397   // (b) these instructions are always executed after evaluating the latch
4398   // conditional branch, and (c) other passes may add new predecessors which
4399   // terminate on this line. This is the easiest way to ensure we don't
4400   // accidentally cause an extra step back into the loop while debugging.
4401   setDebugLocFromInst(LoopMiddleBlock->getTerminator());
4402   if (PhiR->isOrdered())
4403     ReducedPartRdx = State.get(LoopExitInstDef, UF - 1);
4404   else {
4405     // Floating-point operations should have some FMF to enable the reduction.
4406     IRBuilderBase::FastMathFlagGuard FMFG(Builder);
4407     Builder.setFastMathFlags(RdxDesc.getFastMathFlags());
4408     for (unsigned Part = 1; Part < UF; ++Part) {
4409       Value *RdxPart = State.get(LoopExitInstDef, Part);
4410       if (Op != Instruction::ICmp && Op != Instruction::FCmp) {
4411         ReducedPartRdx = Builder.CreateBinOp(
4412             (Instruction::BinaryOps)Op, RdxPart, ReducedPartRdx, "bin.rdx");
4413       } else if (RecurrenceDescriptor::isSelectCmpRecurrenceKind(RK))
4414         ReducedPartRdx = createSelectCmpOp(Builder, ReductionStartValue, RK,
4415                                            ReducedPartRdx, RdxPart);
4416       else
4417         ReducedPartRdx = createMinMaxOp(Builder, RK, ReducedPartRdx, RdxPart);
4418     }
4419   }
4420 
4421   // Create the reduction after the loop. Note that inloop reductions create the
4422   // target reduction in the loop using a Reduction recipe.
4423   if (VF.isVector() && !PhiR->isInLoop()) {
4424     ReducedPartRdx =
4425         createTargetReduction(Builder, TTI, RdxDesc, ReducedPartRdx, OrigPhi);
4426     // If the reduction can be performed in a smaller type, we need to extend
4427     // the reduction to the wider type before we branch to the original loop.
4428     if (PhiTy != RdxDesc.getRecurrenceType())
4429       ReducedPartRdx = RdxDesc.isSigned()
4430                            ? Builder.CreateSExt(ReducedPartRdx, PhiTy)
4431                            : Builder.CreateZExt(ReducedPartRdx, PhiTy);
4432   }
4433 
4434   // Create a phi node that merges control-flow from the backedge-taken check
4435   // block and the middle block.
4436   PHINode *BCBlockPhi = PHINode::Create(PhiTy, 2, "bc.merge.rdx",
4437                                         LoopScalarPreHeader->getTerminator());
4438   for (unsigned I = 0, E = LoopBypassBlocks.size(); I != E; ++I)
4439     BCBlockPhi->addIncoming(ReductionStartValue, LoopBypassBlocks[I]);
4440   BCBlockPhi->addIncoming(ReducedPartRdx, LoopMiddleBlock);
4441 
4442   // Now, we need to fix the users of the reduction variable
4443   // inside and outside of the scalar remainder loop.
4444 
4445   // We know that the loop is in LCSSA form. We need to update the PHI nodes
4446   // in the exit blocks.  See comment on analogous loop in
4447   // fixFirstOrderRecurrence for a more complete explaination of the logic.
4448   if (!Cost->requiresScalarEpilogue(VF))
4449     for (PHINode &LCSSAPhi : LoopExitBlock->phis())
4450       if (llvm::is_contained(LCSSAPhi.incoming_values(), LoopExitInst))
4451         LCSSAPhi.addIncoming(ReducedPartRdx, LoopMiddleBlock);
4452 
4453   // Fix the scalar loop reduction variable with the incoming reduction sum
4454   // from the vector body and from the backedge value.
4455   int IncomingEdgeBlockIdx =
4456       OrigPhi->getBasicBlockIndex(OrigLoop->getLoopLatch());
4457   assert(IncomingEdgeBlockIdx >= 0 && "Invalid block index");
4458   // Pick the other block.
4459   int SelfEdgeBlockIdx = (IncomingEdgeBlockIdx ? 0 : 1);
4460   OrigPhi->setIncomingValue(SelfEdgeBlockIdx, BCBlockPhi);
4461   OrigPhi->setIncomingValue(IncomingEdgeBlockIdx, LoopExitInst);
4462 }
4463 
4464 void InnerLoopVectorizer::clearReductionWrapFlags(const RecurrenceDescriptor &RdxDesc,
4465                                                   VPTransformState &State) {
4466   RecurKind RK = RdxDesc.getRecurrenceKind();
4467   if (RK != RecurKind::Add && RK != RecurKind::Mul)
4468     return;
4469 
4470   Instruction *LoopExitInstr = RdxDesc.getLoopExitInstr();
4471   assert(LoopExitInstr && "null loop exit instruction");
4472   SmallVector<Instruction *, 8> Worklist;
4473   SmallPtrSet<Instruction *, 8> Visited;
4474   Worklist.push_back(LoopExitInstr);
4475   Visited.insert(LoopExitInstr);
4476 
4477   while (!Worklist.empty()) {
4478     Instruction *Cur = Worklist.pop_back_val();
4479     if (isa<OverflowingBinaryOperator>(Cur))
4480       for (unsigned Part = 0; Part < UF; ++Part) {
4481         // FIXME: Should not rely on getVPValue at this point.
4482         Value *V = State.get(State.Plan->getVPValue(Cur, true), Part);
4483         cast<Instruction>(V)->dropPoisonGeneratingFlags();
4484       }
4485 
4486     for (User *U : Cur->users()) {
4487       Instruction *UI = cast<Instruction>(U);
4488       if ((Cur != LoopExitInstr || OrigLoop->contains(UI->getParent())) &&
4489           Visited.insert(UI).second)
4490         Worklist.push_back(UI);
4491     }
4492   }
4493 }
4494 
4495 void InnerLoopVectorizer::fixLCSSAPHIs(VPTransformState &State) {
4496   for (PHINode &LCSSAPhi : LoopExitBlock->phis()) {
4497     if (LCSSAPhi.getBasicBlockIndex(LoopMiddleBlock) != -1)
4498       // Some phis were already hand updated by the reduction and recurrence
4499       // code above, leave them alone.
4500       continue;
4501 
4502     auto *IncomingValue = LCSSAPhi.getIncomingValue(0);
4503     // Non-instruction incoming values will have only one value.
4504 
4505     VPLane Lane = VPLane::getFirstLane();
4506     if (isa<Instruction>(IncomingValue) &&
4507         !Cost->isUniformAfterVectorization(cast<Instruction>(IncomingValue),
4508                                            VF))
4509       Lane = VPLane::getLastLaneForVF(VF);
4510 
4511     // Can be a loop invariant incoming value or the last scalar value to be
4512     // extracted from the vectorized loop.
4513     // FIXME: Should not rely on getVPValue at this point.
4514     Builder.SetInsertPoint(LoopMiddleBlock->getTerminator());
4515     Value *lastIncomingValue =
4516         OrigLoop->isLoopInvariant(IncomingValue)
4517             ? IncomingValue
4518             : State.get(State.Plan->getVPValue(IncomingValue, true),
4519                         VPIteration(UF - 1, Lane));
4520     LCSSAPhi.addIncoming(lastIncomingValue, LoopMiddleBlock);
4521   }
4522 }
4523 
4524 void InnerLoopVectorizer::sinkScalarOperands(Instruction *PredInst) {
4525   // The basic block and loop containing the predicated instruction.
4526   auto *PredBB = PredInst->getParent();
4527   auto *VectorLoop = LI->getLoopFor(PredBB);
4528 
4529   // Initialize a worklist with the operands of the predicated instruction.
4530   SetVector<Value *> Worklist(PredInst->op_begin(), PredInst->op_end());
4531 
4532   // Holds instructions that we need to analyze again. An instruction may be
4533   // reanalyzed if we don't yet know if we can sink it or not.
4534   SmallVector<Instruction *, 8> InstsToReanalyze;
4535 
4536   // Returns true if a given use occurs in the predicated block. Phi nodes use
4537   // their operands in their corresponding predecessor blocks.
4538   auto isBlockOfUsePredicated = [&](Use &U) -> bool {
4539     auto *I = cast<Instruction>(U.getUser());
4540     BasicBlock *BB = I->getParent();
4541     if (auto *Phi = dyn_cast<PHINode>(I))
4542       BB = Phi->getIncomingBlock(
4543           PHINode::getIncomingValueNumForOperand(U.getOperandNo()));
4544     return BB == PredBB;
4545   };
4546 
4547   // Iteratively sink the scalarized operands of the predicated instruction
4548   // into the block we created for it. When an instruction is sunk, it's
4549   // operands are then added to the worklist. The algorithm ends after one pass
4550   // through the worklist doesn't sink a single instruction.
4551   bool Changed;
4552   do {
4553     // Add the instructions that need to be reanalyzed to the worklist, and
4554     // reset the changed indicator.
4555     Worklist.insert(InstsToReanalyze.begin(), InstsToReanalyze.end());
4556     InstsToReanalyze.clear();
4557     Changed = false;
4558 
4559     while (!Worklist.empty()) {
4560       auto *I = dyn_cast<Instruction>(Worklist.pop_back_val());
4561 
4562       // We can't sink an instruction if it is a phi node, is not in the loop,
4563       // or may have side effects.
4564       if (!I || isa<PHINode>(I) || !VectorLoop->contains(I) ||
4565           I->mayHaveSideEffects())
4566         continue;
4567 
4568       // If the instruction is already in PredBB, check if we can sink its
4569       // operands. In that case, VPlan's sinkScalarOperands() succeeded in
4570       // sinking the scalar instruction I, hence it appears in PredBB; but it
4571       // may have failed to sink I's operands (recursively), which we try
4572       // (again) here.
4573       if (I->getParent() == PredBB) {
4574         Worklist.insert(I->op_begin(), I->op_end());
4575         continue;
4576       }
4577 
4578       // It's legal to sink the instruction if all its uses occur in the
4579       // predicated block. Otherwise, there's nothing to do yet, and we may
4580       // need to reanalyze the instruction.
4581       if (!llvm::all_of(I->uses(), isBlockOfUsePredicated)) {
4582         InstsToReanalyze.push_back(I);
4583         continue;
4584       }
4585 
4586       // Move the instruction to the beginning of the predicated block, and add
4587       // it's operands to the worklist.
4588       I->moveBefore(&*PredBB->getFirstInsertionPt());
4589       Worklist.insert(I->op_begin(), I->op_end());
4590 
4591       // The sinking may have enabled other instructions to be sunk, so we will
4592       // need to iterate.
4593       Changed = true;
4594     }
4595   } while (Changed);
4596 }
4597 
4598 void InnerLoopVectorizer::fixNonInductionPHIs(VPTransformState &State) {
4599   for (PHINode *OrigPhi : OrigPHIsToFix) {
4600     VPWidenPHIRecipe *VPPhi =
4601         cast<VPWidenPHIRecipe>(State.Plan->getVPValue(OrigPhi));
4602     PHINode *NewPhi = cast<PHINode>(State.get(VPPhi, 0));
4603     // Make sure the builder has a valid insert point.
4604     Builder.SetInsertPoint(NewPhi);
4605     for (unsigned i = 0; i < VPPhi->getNumOperands(); ++i) {
4606       VPValue *Inc = VPPhi->getIncomingValue(i);
4607       VPBasicBlock *VPBB = VPPhi->getIncomingBlock(i);
4608       NewPhi->addIncoming(State.get(Inc, 0), State.CFG.VPBB2IRBB[VPBB]);
4609     }
4610   }
4611 }
4612 
4613 bool InnerLoopVectorizer::useOrderedReductions(RecurrenceDescriptor &RdxDesc) {
4614   return Cost->useOrderedReductions(RdxDesc);
4615 }
4616 
4617 void InnerLoopVectorizer::widenGEP(GetElementPtrInst *GEP, VPValue *VPDef,
4618                                    VPUser &Operands, unsigned UF,
4619                                    ElementCount VF, bool IsPtrLoopInvariant,
4620                                    SmallBitVector &IsIndexLoopInvariant,
4621                                    VPTransformState &State) {
4622   // Construct a vector GEP by widening the operands of the scalar GEP as
4623   // necessary. We mark the vector GEP 'inbounds' if appropriate. A GEP
4624   // results in a vector of pointers when at least one operand of the GEP
4625   // is vector-typed. Thus, to keep the representation compact, we only use
4626   // vector-typed operands for loop-varying values.
4627 
4628   if (VF.isVector() && IsPtrLoopInvariant && IsIndexLoopInvariant.all()) {
4629     // If we are vectorizing, but the GEP has only loop-invariant operands,
4630     // the GEP we build (by only using vector-typed operands for
4631     // loop-varying values) would be a scalar pointer. Thus, to ensure we
4632     // produce a vector of pointers, we need to either arbitrarily pick an
4633     // operand to broadcast, or broadcast a clone of the original GEP.
4634     // Here, we broadcast a clone of the original.
4635     //
4636     // TODO: If at some point we decide to scalarize instructions having
4637     //       loop-invariant operands, this special case will no longer be
4638     //       required. We would add the scalarization decision to
4639     //       collectLoopScalars() and teach getVectorValue() to broadcast
4640     //       the lane-zero scalar value.
4641     auto *Clone = Builder.Insert(GEP->clone());
4642     for (unsigned Part = 0; Part < UF; ++Part) {
4643       Value *EntryPart = Builder.CreateVectorSplat(VF, Clone);
4644       State.set(VPDef, EntryPart, Part);
4645       addMetadata(EntryPart, GEP);
4646     }
4647   } else {
4648     // If the GEP has at least one loop-varying operand, we are sure to
4649     // produce a vector of pointers. But if we are only unrolling, we want
4650     // to produce a scalar GEP for each unroll part. Thus, the GEP we
4651     // produce with the code below will be scalar (if VF == 1) or vector
4652     // (otherwise). Note that for the unroll-only case, we still maintain
4653     // values in the vector mapping with initVector, as we do for other
4654     // instructions.
4655     for (unsigned Part = 0; Part < UF; ++Part) {
4656       // The pointer operand of the new GEP. If it's loop-invariant, we
4657       // won't broadcast it.
4658       auto *Ptr = IsPtrLoopInvariant
4659                       ? State.get(Operands.getOperand(0), VPIteration(0, 0))
4660                       : State.get(Operands.getOperand(0), Part);
4661 
4662       // Collect all the indices for the new GEP. If any index is
4663       // loop-invariant, we won't broadcast it.
4664       SmallVector<Value *, 4> Indices;
4665       for (unsigned I = 1, E = Operands.getNumOperands(); I < E; I++) {
4666         VPValue *Operand = Operands.getOperand(I);
4667         if (IsIndexLoopInvariant[I - 1])
4668           Indices.push_back(State.get(Operand, VPIteration(0, 0)));
4669         else
4670           Indices.push_back(State.get(Operand, Part));
4671       }
4672 
4673       // Create the new GEP. Note that this GEP may be a scalar if VF == 1,
4674       // but it should be a vector, otherwise.
4675       auto *NewGEP =
4676           GEP->isInBounds()
4677               ? Builder.CreateInBoundsGEP(GEP->getSourceElementType(), Ptr,
4678                                           Indices)
4679               : Builder.CreateGEP(GEP->getSourceElementType(), Ptr, Indices);
4680       assert((VF.isScalar() || NewGEP->getType()->isVectorTy()) &&
4681              "NewGEP is not a pointer vector");
4682       State.set(VPDef, NewGEP, Part);
4683       addMetadata(NewGEP, GEP);
4684     }
4685   }
4686 }
4687 
4688 void InnerLoopVectorizer::widenPHIInstruction(Instruction *PN,
4689                                               VPWidenPHIRecipe *PhiR,
4690                                               VPTransformState &State) {
4691   PHINode *P = cast<PHINode>(PN);
4692   if (EnableVPlanNativePath) {
4693     // Currently we enter here in the VPlan-native path for non-induction
4694     // PHIs where all control flow is uniform. We simply widen these PHIs.
4695     // Create a vector phi with no operands - the vector phi operands will be
4696     // set at the end of vector code generation.
4697     Type *VecTy = (State.VF.isScalar())
4698                       ? PN->getType()
4699                       : VectorType::get(PN->getType(), State.VF);
4700     Value *VecPhi = Builder.CreatePHI(VecTy, PN->getNumOperands(), "vec.phi");
4701     State.set(PhiR, VecPhi, 0);
4702     OrigPHIsToFix.push_back(P);
4703 
4704     return;
4705   }
4706 
4707   assert(PN->getParent() == OrigLoop->getHeader() &&
4708          "Non-header phis should have been handled elsewhere");
4709 
4710   // In order to support recurrences we need to be able to vectorize Phi nodes.
4711   // Phi nodes have cycles, so we need to vectorize them in two stages. This is
4712   // stage #1: We create a new vector PHI node with no incoming edges. We'll use
4713   // this value when we vectorize all of the instructions that use the PHI.
4714 
4715   assert(!Legal->isReductionVariable(P) &&
4716          "reductions should be handled elsewhere");
4717 
4718   setDebugLocFromInst(P);
4719 
4720   // This PHINode must be an induction variable.
4721   // Make sure that we know about it.
4722   assert(Legal->getInductionVars().count(P) && "Not an induction variable");
4723 
4724   InductionDescriptor II = Legal->getInductionVars().lookup(P);
4725   const DataLayout &DL = OrigLoop->getHeader()->getModule()->getDataLayout();
4726 
4727   // FIXME: The newly created binary instructions should contain nsw/nuw flags,
4728   // which can be found from the original scalar operations.
4729   switch (II.getKind()) {
4730   case InductionDescriptor::IK_NoInduction:
4731     llvm_unreachable("Unknown induction");
4732   case InductionDescriptor::IK_IntInduction:
4733   case InductionDescriptor::IK_FpInduction:
4734     llvm_unreachable("Integer/fp induction is handled elsewhere.");
4735   case InductionDescriptor::IK_PtrInduction: {
4736     // Handle the pointer induction variable case.
4737     assert(P->getType()->isPointerTy() && "Unexpected type.");
4738 
4739     if (Cost->isScalarAfterVectorization(P, State.VF)) {
4740       // This is the normalized GEP that starts counting at zero.
4741       Value *PtrInd =
4742           Builder.CreateSExtOrTrunc(Induction, II.getStep()->getType());
4743       // Determine the number of scalars we need to generate for each unroll
4744       // iteration. If the instruction is uniform, we only need to generate the
4745       // first lane. Otherwise, we generate all VF values.
4746       bool IsUniform = Cost->isUniformAfterVectorization(P, State.VF);
4747       unsigned Lanes = IsUniform ? 1 : State.VF.getKnownMinValue();
4748 
4749       bool NeedsVectorIndex = !IsUniform && VF.isScalable();
4750       Value *UnitStepVec = nullptr, *PtrIndSplat = nullptr;
4751       if (NeedsVectorIndex) {
4752         Type *VecIVTy = VectorType::get(PtrInd->getType(), VF);
4753         UnitStepVec = Builder.CreateStepVector(VecIVTy);
4754         PtrIndSplat = Builder.CreateVectorSplat(VF, PtrInd);
4755       }
4756 
4757       for (unsigned Part = 0; Part < UF; ++Part) {
4758         Value *PartStart = createStepForVF(
4759             Builder, ConstantInt::get(PtrInd->getType(), Part), VF);
4760 
4761         if (NeedsVectorIndex) {
4762           // Here we cache the whole vector, which means we can support the
4763           // extraction of any lane. However, in some cases the extractelement
4764           // instruction that is generated for scalar uses of this vector (e.g.
4765           // a load instruction) is not folded away. Therefore we still
4766           // calculate values for the first n lanes to avoid redundant moves
4767           // (when extracting the 0th element) and to produce scalar code (i.e.
4768           // additional add/gep instructions instead of expensive extractelement
4769           // instructions) when extracting higher-order elements.
4770           Value *PartStartSplat = Builder.CreateVectorSplat(VF, PartStart);
4771           Value *Indices = Builder.CreateAdd(PartStartSplat, UnitStepVec);
4772           Value *GlobalIndices = Builder.CreateAdd(PtrIndSplat, Indices);
4773           Value *SclrGep =
4774               emitTransformedIndex(Builder, GlobalIndices, PSE.getSE(), DL, II);
4775           SclrGep->setName("next.gep");
4776           State.set(PhiR, SclrGep, Part);
4777         }
4778 
4779         for (unsigned Lane = 0; Lane < Lanes; ++Lane) {
4780           Value *Idx = Builder.CreateAdd(
4781               PartStart, ConstantInt::get(PtrInd->getType(), Lane));
4782           Value *GlobalIdx = Builder.CreateAdd(PtrInd, Idx);
4783           Value *SclrGep =
4784               emitTransformedIndex(Builder, GlobalIdx, PSE.getSE(), DL, II);
4785           SclrGep->setName("next.gep");
4786           State.set(PhiR, SclrGep, VPIteration(Part, Lane));
4787         }
4788       }
4789       return;
4790     }
4791     assert(isa<SCEVConstant>(II.getStep()) &&
4792            "Induction step not a SCEV constant!");
4793     Type *PhiType = II.getStep()->getType();
4794 
4795     // Build a pointer phi
4796     Value *ScalarStartValue = II.getStartValue();
4797     Type *ScStValueType = ScalarStartValue->getType();
4798     PHINode *NewPointerPhi =
4799         PHINode::Create(ScStValueType, 2, "pointer.phi", Induction);
4800     NewPointerPhi->addIncoming(ScalarStartValue, LoopVectorPreHeader);
4801 
4802     // A pointer induction, performed by using a gep
4803     BasicBlock *LoopLatch = LI->getLoopFor(LoopVectorBody)->getLoopLatch();
4804     Instruction *InductionLoc = LoopLatch->getTerminator();
4805     const SCEV *ScalarStep = II.getStep();
4806     SCEVExpander Exp(*PSE.getSE(), DL, "induction");
4807     Value *ScalarStepValue =
4808         Exp.expandCodeFor(ScalarStep, PhiType, InductionLoc);
4809     Value *RuntimeVF = getRuntimeVF(Builder, PhiType, VF);
4810     Value *NumUnrolledElems =
4811         Builder.CreateMul(RuntimeVF, ConstantInt::get(PhiType, State.UF));
4812     Value *InductionGEP = GetElementPtrInst::Create(
4813         II.getElementType(), NewPointerPhi,
4814         Builder.CreateMul(ScalarStepValue, NumUnrolledElems), "ptr.ind",
4815         InductionLoc);
4816     NewPointerPhi->addIncoming(InductionGEP, LoopLatch);
4817 
4818     // Create UF many actual address geps that use the pointer
4819     // phi as base and a vectorized version of the step value
4820     // (<step*0, ..., step*N>) as offset.
4821     for (unsigned Part = 0; Part < State.UF; ++Part) {
4822       Type *VecPhiType = VectorType::get(PhiType, State.VF);
4823       Value *StartOffsetScalar =
4824           Builder.CreateMul(RuntimeVF, ConstantInt::get(PhiType, Part));
4825       Value *StartOffset =
4826           Builder.CreateVectorSplat(State.VF, StartOffsetScalar);
4827       // Create a vector of consecutive numbers from zero to VF.
4828       StartOffset =
4829           Builder.CreateAdd(StartOffset, Builder.CreateStepVector(VecPhiType));
4830 
4831       Value *GEP = Builder.CreateGEP(
4832           II.getElementType(), NewPointerPhi,
4833           Builder.CreateMul(
4834               StartOffset, Builder.CreateVectorSplat(State.VF, ScalarStepValue),
4835               "vector.gep"));
4836       State.set(PhiR, GEP, Part);
4837     }
4838   }
4839   }
4840 }
4841 
4842 /// A helper function for checking whether an integer division-related
4843 /// instruction may divide by zero (in which case it must be predicated if
4844 /// executed conditionally in the scalar code).
4845 /// TODO: It may be worthwhile to generalize and check isKnownNonZero().
4846 /// Non-zero divisors that are non compile-time constants will not be
4847 /// converted into multiplication, so we will still end up scalarizing
4848 /// the division, but can do so w/o predication.
4849 static bool mayDivideByZero(Instruction &I) {
4850   assert((I.getOpcode() == Instruction::UDiv ||
4851           I.getOpcode() == Instruction::SDiv ||
4852           I.getOpcode() == Instruction::URem ||
4853           I.getOpcode() == Instruction::SRem) &&
4854          "Unexpected instruction");
4855   Value *Divisor = I.getOperand(1);
4856   auto *CInt = dyn_cast<ConstantInt>(Divisor);
4857   return !CInt || CInt->isZero();
4858 }
4859 
4860 void InnerLoopVectorizer::widenInstruction(Instruction &I, VPValue *Def,
4861                                            VPUser &User,
4862                                            VPTransformState &State) {
4863   switch (I.getOpcode()) {
4864   case Instruction::Call:
4865   case Instruction::Br:
4866   case Instruction::PHI:
4867   case Instruction::GetElementPtr:
4868   case Instruction::Select:
4869     llvm_unreachable("This instruction is handled by a different recipe.");
4870   case Instruction::UDiv:
4871   case Instruction::SDiv:
4872   case Instruction::SRem:
4873   case Instruction::URem:
4874   case Instruction::Add:
4875   case Instruction::FAdd:
4876   case Instruction::Sub:
4877   case Instruction::FSub:
4878   case Instruction::FNeg:
4879   case Instruction::Mul:
4880   case Instruction::FMul:
4881   case Instruction::FDiv:
4882   case Instruction::FRem:
4883   case Instruction::Shl:
4884   case Instruction::LShr:
4885   case Instruction::AShr:
4886   case Instruction::And:
4887   case Instruction::Or:
4888   case Instruction::Xor: {
4889     // Just widen unops and binops.
4890     setDebugLocFromInst(&I);
4891 
4892     for (unsigned Part = 0; Part < UF; ++Part) {
4893       SmallVector<Value *, 2> Ops;
4894       for (VPValue *VPOp : User.operands())
4895         Ops.push_back(State.get(VPOp, Part));
4896 
4897       Value *V = Builder.CreateNAryOp(I.getOpcode(), Ops);
4898 
4899       if (auto *VecOp = dyn_cast<Instruction>(V))
4900         VecOp->copyIRFlags(&I);
4901 
4902       // Use this vector value for all users of the original instruction.
4903       State.set(Def, V, Part);
4904       addMetadata(V, &I);
4905     }
4906 
4907     break;
4908   }
4909   case Instruction::ICmp:
4910   case Instruction::FCmp: {
4911     // Widen compares. Generate vector compares.
4912     bool FCmp = (I.getOpcode() == Instruction::FCmp);
4913     auto *Cmp = cast<CmpInst>(&I);
4914     setDebugLocFromInst(Cmp);
4915     for (unsigned Part = 0; Part < UF; ++Part) {
4916       Value *A = State.get(User.getOperand(0), Part);
4917       Value *B = State.get(User.getOperand(1), Part);
4918       Value *C = nullptr;
4919       if (FCmp) {
4920         // Propagate fast math flags.
4921         IRBuilder<>::FastMathFlagGuard FMFG(Builder);
4922         Builder.setFastMathFlags(Cmp->getFastMathFlags());
4923         C = Builder.CreateFCmp(Cmp->getPredicate(), A, B);
4924       } else {
4925         C = Builder.CreateICmp(Cmp->getPredicate(), A, B);
4926       }
4927       State.set(Def, C, Part);
4928       addMetadata(C, &I);
4929     }
4930 
4931     break;
4932   }
4933 
4934   case Instruction::ZExt:
4935   case Instruction::SExt:
4936   case Instruction::FPToUI:
4937   case Instruction::FPToSI:
4938   case Instruction::FPExt:
4939   case Instruction::PtrToInt:
4940   case Instruction::IntToPtr:
4941   case Instruction::SIToFP:
4942   case Instruction::UIToFP:
4943   case Instruction::Trunc:
4944   case Instruction::FPTrunc:
4945   case Instruction::BitCast: {
4946     auto *CI = cast<CastInst>(&I);
4947     setDebugLocFromInst(CI);
4948 
4949     /// Vectorize casts.
4950     Type *DestTy =
4951         (VF.isScalar()) ? CI->getType() : VectorType::get(CI->getType(), VF);
4952 
4953     for (unsigned Part = 0; Part < UF; ++Part) {
4954       Value *A = State.get(User.getOperand(0), Part);
4955       Value *Cast = Builder.CreateCast(CI->getOpcode(), A, DestTy);
4956       State.set(Def, Cast, Part);
4957       addMetadata(Cast, &I);
4958     }
4959     break;
4960   }
4961   default:
4962     // This instruction is not vectorized by simple widening.
4963     LLVM_DEBUG(dbgs() << "LV: Found an unhandled instruction: " << I);
4964     llvm_unreachable("Unhandled instruction!");
4965   } // end of switch.
4966 }
4967 
4968 void InnerLoopVectorizer::widenCallInstruction(CallInst &I, VPValue *Def,
4969                                                VPUser &ArgOperands,
4970                                                VPTransformState &State) {
4971   assert(!isa<DbgInfoIntrinsic>(I) &&
4972          "DbgInfoIntrinsic should have been dropped during VPlan construction");
4973   setDebugLocFromInst(&I);
4974 
4975   Module *M = I.getParent()->getParent()->getParent();
4976   auto *CI = cast<CallInst>(&I);
4977 
4978   SmallVector<Type *, 4> Tys;
4979   for (Value *ArgOperand : CI->args())
4980     Tys.push_back(ToVectorTy(ArgOperand->getType(), VF.getKnownMinValue()));
4981 
4982   Intrinsic::ID ID = getVectorIntrinsicIDForCall(CI, TLI);
4983 
4984   // The flag shows whether we use Intrinsic or a usual Call for vectorized
4985   // version of the instruction.
4986   // Is it beneficial to perform intrinsic call compared to lib call?
4987   bool NeedToScalarize = false;
4988   InstructionCost CallCost = Cost->getVectorCallCost(CI, VF, NeedToScalarize);
4989   InstructionCost IntrinsicCost = ID ? Cost->getVectorIntrinsicCost(CI, VF) : 0;
4990   bool UseVectorIntrinsic = ID && IntrinsicCost <= CallCost;
4991   assert((UseVectorIntrinsic || !NeedToScalarize) &&
4992          "Instruction should be scalarized elsewhere.");
4993   assert((IntrinsicCost.isValid() || CallCost.isValid()) &&
4994          "Either the intrinsic cost or vector call cost must be valid");
4995 
4996   for (unsigned Part = 0; Part < UF; ++Part) {
4997     SmallVector<Type *, 2> TysForDecl = {CI->getType()};
4998     SmallVector<Value *, 4> Args;
4999     for (auto &I : enumerate(ArgOperands.operands())) {
5000       // Some intrinsics have a scalar argument - don't replace it with a
5001       // vector.
5002       Value *Arg;
5003       if (!UseVectorIntrinsic || !hasVectorInstrinsicScalarOpd(ID, I.index()))
5004         Arg = State.get(I.value(), Part);
5005       else {
5006         Arg = State.get(I.value(), VPIteration(0, 0));
5007         if (hasVectorInstrinsicOverloadedScalarOpd(ID, I.index()))
5008           TysForDecl.push_back(Arg->getType());
5009       }
5010       Args.push_back(Arg);
5011     }
5012 
5013     Function *VectorF;
5014     if (UseVectorIntrinsic) {
5015       // Use vector version of the intrinsic.
5016       if (VF.isVector())
5017         TysForDecl[0] = VectorType::get(CI->getType()->getScalarType(), VF);
5018       VectorF = Intrinsic::getDeclaration(M, ID, TysForDecl);
5019       assert(VectorF && "Can't retrieve vector intrinsic.");
5020     } else {
5021       // Use vector version of the function call.
5022       const VFShape Shape = VFShape::get(*CI, VF, false /*HasGlobalPred*/);
5023 #ifndef NDEBUG
5024       assert(VFDatabase(*CI).getVectorizedFunction(Shape) != nullptr &&
5025              "Can't create vector function.");
5026 #endif
5027         VectorF = VFDatabase(*CI).getVectorizedFunction(Shape);
5028     }
5029       SmallVector<OperandBundleDef, 1> OpBundles;
5030       CI->getOperandBundlesAsDefs(OpBundles);
5031       CallInst *V = Builder.CreateCall(VectorF, Args, OpBundles);
5032 
5033       if (isa<FPMathOperator>(V))
5034         V->copyFastMathFlags(CI);
5035 
5036       State.set(Def, V, Part);
5037       addMetadata(V, &I);
5038   }
5039 }
5040 
5041 void InnerLoopVectorizer::widenSelectInstruction(SelectInst &I, VPValue *VPDef,
5042                                                  VPUser &Operands,
5043                                                  bool InvariantCond,
5044                                                  VPTransformState &State) {
5045   setDebugLocFromInst(&I);
5046 
5047   // The condition can be loop invariant  but still defined inside the
5048   // loop. This means that we can't just use the original 'cond' value.
5049   // We have to take the 'vectorized' value and pick the first lane.
5050   // Instcombine will make this a no-op.
5051   auto *InvarCond = InvariantCond
5052                         ? State.get(Operands.getOperand(0), VPIteration(0, 0))
5053                         : nullptr;
5054 
5055   for (unsigned Part = 0; Part < UF; ++Part) {
5056     Value *Cond =
5057         InvarCond ? InvarCond : State.get(Operands.getOperand(0), Part);
5058     Value *Op0 = State.get(Operands.getOperand(1), Part);
5059     Value *Op1 = State.get(Operands.getOperand(2), Part);
5060     Value *Sel = Builder.CreateSelect(Cond, Op0, Op1);
5061     State.set(VPDef, Sel, Part);
5062     addMetadata(Sel, &I);
5063   }
5064 }
5065 
5066 void LoopVectorizationCostModel::collectLoopScalars(ElementCount VF) {
5067   // We should not collect Scalars more than once per VF. Right now, this
5068   // function is called from collectUniformsAndScalars(), which already does
5069   // this check. Collecting Scalars for VF=1 does not make any sense.
5070   assert(VF.isVector() && Scalars.find(VF) == Scalars.end() &&
5071          "This function should not be visited twice for the same VF");
5072 
5073   SmallSetVector<Instruction *, 8> Worklist;
5074 
5075   // These sets are used to seed the analysis with pointers used by memory
5076   // accesses that will remain scalar.
5077   SmallSetVector<Instruction *, 8> ScalarPtrs;
5078   SmallPtrSet<Instruction *, 8> PossibleNonScalarPtrs;
5079   auto *Latch = TheLoop->getLoopLatch();
5080 
5081   // A helper that returns true if the use of Ptr by MemAccess will be scalar.
5082   // The pointer operands of loads and stores will be scalar as long as the
5083   // memory access is not a gather or scatter operation. The value operand of a
5084   // store will remain scalar if the store is scalarized.
5085   auto isScalarUse = [&](Instruction *MemAccess, Value *Ptr) {
5086     InstWidening WideningDecision = getWideningDecision(MemAccess, VF);
5087     assert(WideningDecision != CM_Unknown &&
5088            "Widening decision should be ready at this moment");
5089     if (auto *Store = dyn_cast<StoreInst>(MemAccess))
5090       if (Ptr == Store->getValueOperand())
5091         return WideningDecision == CM_Scalarize;
5092     assert(Ptr == getLoadStorePointerOperand(MemAccess) &&
5093            "Ptr is neither a value or pointer operand");
5094     return WideningDecision != CM_GatherScatter;
5095   };
5096 
5097   // A helper that returns true if the given value is a bitcast or
5098   // getelementptr instruction contained in the loop.
5099   auto isLoopVaryingBitCastOrGEP = [&](Value *V) {
5100     return ((isa<BitCastInst>(V) && V->getType()->isPointerTy()) ||
5101             isa<GetElementPtrInst>(V)) &&
5102            !TheLoop->isLoopInvariant(V);
5103   };
5104 
5105   auto isScalarPtrInduction = [&](Instruction *MemAccess, Value *Ptr) {
5106     if (!isa<PHINode>(Ptr) ||
5107         !Legal->getInductionVars().count(cast<PHINode>(Ptr)))
5108       return false;
5109     auto &Induction = Legal->getInductionVars()[cast<PHINode>(Ptr)];
5110     if (Induction.getKind() != InductionDescriptor::IK_PtrInduction)
5111       return false;
5112     return isScalarUse(MemAccess, Ptr);
5113   };
5114 
5115   // A helper that evaluates a memory access's use of a pointer. If the
5116   // pointer is actually the pointer induction of a loop, it is being
5117   // inserted into Worklist. If the use will be a scalar use, and the
5118   // pointer is only used by memory accesses, we place the pointer in
5119   // ScalarPtrs. Otherwise, the pointer is placed in PossibleNonScalarPtrs.
5120   auto evaluatePtrUse = [&](Instruction *MemAccess, Value *Ptr) {
5121     if (isScalarPtrInduction(MemAccess, Ptr)) {
5122       Worklist.insert(cast<Instruction>(Ptr));
5123       LLVM_DEBUG(dbgs() << "LV: Found new scalar instruction: " << *Ptr
5124                         << "\n");
5125 
5126       Instruction *Update = cast<Instruction>(
5127           cast<PHINode>(Ptr)->getIncomingValueForBlock(Latch));
5128 
5129       // If there is more than one user of Update (Ptr), we shouldn't assume it
5130       // will be scalar after vectorisation as other users of the instruction
5131       // may require widening. Otherwise, add it to ScalarPtrs.
5132       if (Update->hasOneUse() && cast<Value>(*Update->user_begin()) == Ptr) {
5133         ScalarPtrs.insert(Update);
5134         return;
5135       }
5136     }
5137     // We only care about bitcast and getelementptr instructions contained in
5138     // the loop.
5139     if (!isLoopVaryingBitCastOrGEP(Ptr))
5140       return;
5141 
5142     // If the pointer has already been identified as scalar (e.g., if it was
5143     // also identified as uniform), there's nothing to do.
5144     auto *I = cast<Instruction>(Ptr);
5145     if (Worklist.count(I))
5146       return;
5147 
5148     // If the use of the pointer will be a scalar use, and all users of the
5149     // pointer are memory accesses, place the pointer in ScalarPtrs. Otherwise,
5150     // place the pointer in PossibleNonScalarPtrs.
5151     if (isScalarUse(MemAccess, Ptr) && llvm::all_of(I->users(), [&](User *U) {
5152           return isa<LoadInst>(U) || isa<StoreInst>(U);
5153         }))
5154       ScalarPtrs.insert(I);
5155     else
5156       PossibleNonScalarPtrs.insert(I);
5157   };
5158 
5159   // We seed the scalars analysis with three classes of instructions: (1)
5160   // instructions marked uniform-after-vectorization and (2) bitcast,
5161   // getelementptr and (pointer) phi instructions used by memory accesses
5162   // requiring a scalar use.
5163   //
5164   // (1) Add to the worklist all instructions that have been identified as
5165   // uniform-after-vectorization.
5166   Worklist.insert(Uniforms[VF].begin(), Uniforms[VF].end());
5167 
5168   // (2) Add to the worklist all bitcast and getelementptr instructions used by
5169   // memory accesses requiring a scalar use. The pointer operands of loads and
5170   // stores will be scalar as long as the memory accesses is not a gather or
5171   // scatter operation. The value operand of a store will remain scalar if the
5172   // store is scalarized.
5173   for (auto *BB : TheLoop->blocks())
5174     for (auto &I : *BB) {
5175       if (auto *Load = dyn_cast<LoadInst>(&I)) {
5176         evaluatePtrUse(Load, Load->getPointerOperand());
5177       } else if (auto *Store = dyn_cast<StoreInst>(&I)) {
5178         evaluatePtrUse(Store, Store->getPointerOperand());
5179         evaluatePtrUse(Store, Store->getValueOperand());
5180       }
5181     }
5182   for (auto *I : ScalarPtrs)
5183     if (!PossibleNonScalarPtrs.count(I)) {
5184       LLVM_DEBUG(dbgs() << "LV: Found scalar instruction: " << *I << "\n");
5185       Worklist.insert(I);
5186     }
5187 
5188   // Insert the forced scalars.
5189   // FIXME: Currently widenPHIInstruction() often creates a dead vector
5190   // induction variable when the PHI user is scalarized.
5191   auto ForcedScalar = ForcedScalars.find(VF);
5192   if (ForcedScalar != ForcedScalars.end())
5193     for (auto *I : ForcedScalar->second)
5194       Worklist.insert(I);
5195 
5196   // Expand the worklist by looking through any bitcasts and getelementptr
5197   // instructions we've already identified as scalar. This is similar to the
5198   // expansion step in collectLoopUniforms(); however, here we're only
5199   // expanding to include additional bitcasts and getelementptr instructions.
5200   unsigned Idx = 0;
5201   while (Idx != Worklist.size()) {
5202     Instruction *Dst = Worklist[Idx++];
5203     if (!isLoopVaryingBitCastOrGEP(Dst->getOperand(0)))
5204       continue;
5205     auto *Src = cast<Instruction>(Dst->getOperand(0));
5206     if (llvm::all_of(Src->users(), [&](User *U) -> bool {
5207           auto *J = cast<Instruction>(U);
5208           return !TheLoop->contains(J) || Worklist.count(J) ||
5209                  ((isa<LoadInst>(J) || isa<StoreInst>(J)) &&
5210                   isScalarUse(J, Src));
5211         })) {
5212       Worklist.insert(Src);
5213       LLVM_DEBUG(dbgs() << "LV: Found scalar instruction: " << *Src << "\n");
5214     }
5215   }
5216 
5217   // An induction variable will remain scalar if all users of the induction
5218   // variable and induction variable update remain scalar.
5219   for (auto &Induction : Legal->getInductionVars()) {
5220     auto *Ind = Induction.first;
5221     auto *IndUpdate = cast<Instruction>(Ind->getIncomingValueForBlock(Latch));
5222 
5223     // If tail-folding is applied, the primary induction variable will be used
5224     // to feed a vector compare.
5225     if (Ind == Legal->getPrimaryInduction() && foldTailByMasking())
5226       continue;
5227 
5228     // Determine if all users of the induction variable are scalar after
5229     // vectorization.
5230     auto ScalarInd = llvm::all_of(Ind->users(), [&](User *U) -> bool {
5231       auto *I = cast<Instruction>(U);
5232       return I == IndUpdate || !TheLoop->contains(I) || Worklist.count(I);
5233     });
5234     if (!ScalarInd)
5235       continue;
5236 
5237     // Determine if all users of the induction variable update instruction are
5238     // scalar after vectorization.
5239     auto ScalarIndUpdate =
5240         llvm::all_of(IndUpdate->users(), [&](User *U) -> bool {
5241           auto *I = cast<Instruction>(U);
5242           return I == Ind || !TheLoop->contains(I) || Worklist.count(I);
5243         });
5244     if (!ScalarIndUpdate)
5245       continue;
5246 
5247     // The induction variable and its update instruction will remain scalar.
5248     Worklist.insert(Ind);
5249     Worklist.insert(IndUpdate);
5250     LLVM_DEBUG(dbgs() << "LV: Found scalar instruction: " << *Ind << "\n");
5251     LLVM_DEBUG(dbgs() << "LV: Found scalar instruction: " << *IndUpdate
5252                       << "\n");
5253   }
5254 
5255   Scalars[VF].insert(Worklist.begin(), Worklist.end());
5256 }
5257 
5258 bool LoopVectorizationCostModel::isScalarWithPredication(Instruction *I) const {
5259   if (!blockNeedsPredication(I->getParent()))
5260     return false;
5261   switch(I->getOpcode()) {
5262   default:
5263     break;
5264   case Instruction::Load:
5265   case Instruction::Store: {
5266     if (!Legal->isMaskRequired(I))
5267       return false;
5268     auto *Ptr = getLoadStorePointerOperand(I);
5269     auto *Ty = getLoadStoreType(I);
5270     const Align Alignment = getLoadStoreAlignment(I);
5271     return isa<LoadInst>(I) ? !(isLegalMaskedLoad(Ty, Ptr, Alignment) ||
5272                                 TTI.isLegalMaskedGather(Ty, Alignment))
5273                             : !(isLegalMaskedStore(Ty, Ptr, Alignment) ||
5274                                 TTI.isLegalMaskedScatter(Ty, Alignment));
5275   }
5276   case Instruction::UDiv:
5277   case Instruction::SDiv:
5278   case Instruction::SRem:
5279   case Instruction::URem:
5280     return mayDivideByZero(*I);
5281   }
5282   return false;
5283 }
5284 
5285 bool LoopVectorizationCostModel::interleavedAccessCanBeWidened(
5286     Instruction *I, ElementCount VF) {
5287   assert(isAccessInterleaved(I) && "Expecting interleaved access.");
5288   assert(getWideningDecision(I, VF) == CM_Unknown &&
5289          "Decision should not be set yet.");
5290   auto *Group = getInterleavedAccessGroup(I);
5291   assert(Group && "Must have a group.");
5292 
5293   // If the instruction's allocated size doesn't equal it's type size, it
5294   // requires padding and will be scalarized.
5295   auto &DL = I->getModule()->getDataLayout();
5296   auto *ScalarTy = getLoadStoreType(I);
5297   if (hasIrregularType(ScalarTy, DL))
5298     return false;
5299 
5300   // Check if masking is required.
5301   // A Group may need masking for one of two reasons: it resides in a block that
5302   // needs predication, or it was decided to use masking to deal with gaps
5303   // (either a gap at the end of a load-access that may result in a speculative
5304   // load, or any gaps in a store-access).
5305   bool PredicatedAccessRequiresMasking =
5306       blockNeedsPredication(I->getParent()) && Legal->isMaskRequired(I);
5307   bool LoadAccessWithGapsRequiresEpilogMasking =
5308       isa<LoadInst>(I) && Group->requiresScalarEpilogue() &&
5309       !isScalarEpilogueAllowed();
5310   bool StoreAccessWithGapsRequiresMasking =
5311       isa<StoreInst>(I) && (Group->getNumMembers() < Group->getFactor());
5312   if (!PredicatedAccessRequiresMasking &&
5313       !LoadAccessWithGapsRequiresEpilogMasking &&
5314       !StoreAccessWithGapsRequiresMasking)
5315     return true;
5316 
5317   // If masked interleaving is required, we expect that the user/target had
5318   // enabled it, because otherwise it either wouldn't have been created or
5319   // it should have been invalidated by the CostModel.
5320   assert(useMaskedInterleavedAccesses(TTI) &&
5321          "Masked interleave-groups for predicated accesses are not enabled.");
5322 
5323   if (Group->isReverse())
5324     return false;
5325 
5326   auto *Ty = getLoadStoreType(I);
5327   const Align Alignment = getLoadStoreAlignment(I);
5328   return isa<LoadInst>(I) ? TTI.isLegalMaskedLoad(Ty, Alignment)
5329                           : TTI.isLegalMaskedStore(Ty, Alignment);
5330 }
5331 
5332 bool LoopVectorizationCostModel::memoryInstructionCanBeWidened(
5333     Instruction *I, ElementCount VF) {
5334   // Get and ensure we have a valid memory instruction.
5335   assert((isa<LoadInst, StoreInst>(I)) && "Invalid memory instruction");
5336 
5337   auto *Ptr = getLoadStorePointerOperand(I);
5338   auto *ScalarTy = getLoadStoreType(I);
5339 
5340   // In order to be widened, the pointer should be consecutive, first of all.
5341   if (!Legal->isConsecutivePtr(ScalarTy, Ptr))
5342     return false;
5343 
5344   // If the instruction is a store located in a predicated block, it will be
5345   // scalarized.
5346   if (isScalarWithPredication(I))
5347     return false;
5348 
5349   // If the instruction's allocated size doesn't equal it's type size, it
5350   // requires padding and will be scalarized.
5351   auto &DL = I->getModule()->getDataLayout();
5352   if (hasIrregularType(ScalarTy, DL))
5353     return false;
5354 
5355   return true;
5356 }
5357 
5358 void LoopVectorizationCostModel::collectLoopUniforms(ElementCount VF) {
5359   // We should not collect Uniforms more than once per VF. Right now,
5360   // this function is called from collectUniformsAndScalars(), which
5361   // already does this check. Collecting Uniforms for VF=1 does not make any
5362   // sense.
5363 
5364   assert(VF.isVector() && Uniforms.find(VF) == Uniforms.end() &&
5365          "This function should not be visited twice for the same VF");
5366 
5367   // Visit the list of Uniforms. If we'll not find any uniform value, we'll
5368   // not analyze again.  Uniforms.count(VF) will return 1.
5369   Uniforms[VF].clear();
5370 
5371   // We now know that the loop is vectorizable!
5372   // Collect instructions inside the loop that will remain uniform after
5373   // vectorization.
5374 
5375   // Global values, params and instructions outside of current loop are out of
5376   // scope.
5377   auto isOutOfScope = [&](Value *V) -> bool {
5378     Instruction *I = dyn_cast<Instruction>(V);
5379     return (!I || !TheLoop->contains(I));
5380   };
5381 
5382   // Worklist containing uniform instructions demanding lane 0.
5383   SetVector<Instruction *> Worklist;
5384   BasicBlock *Latch = TheLoop->getLoopLatch();
5385 
5386   // Add uniform instructions demanding lane 0 to the worklist. Instructions
5387   // that are scalar with predication must not be considered uniform after
5388   // vectorization, because that would create an erroneous replicating region
5389   // where only a single instance out of VF should be formed.
5390   // TODO: optimize such seldom cases if found important, see PR40816.
5391   auto addToWorklistIfAllowed = [&](Instruction *I) -> void {
5392     if (isOutOfScope(I)) {
5393       LLVM_DEBUG(dbgs() << "LV: Found not uniform due to scope: "
5394                         << *I << "\n");
5395       return;
5396     }
5397     if (isScalarWithPredication(I)) {
5398       LLVM_DEBUG(dbgs() << "LV: Found not uniform being ScalarWithPredication: "
5399                         << *I << "\n");
5400       return;
5401     }
5402     LLVM_DEBUG(dbgs() << "LV: Found uniform instruction: " << *I << "\n");
5403     Worklist.insert(I);
5404   };
5405 
5406   // Start with the conditional branch. If the branch condition is an
5407   // instruction contained in the loop that is only used by the branch, it is
5408   // uniform.
5409   auto *Cmp = dyn_cast<Instruction>(Latch->getTerminator()->getOperand(0));
5410   if (Cmp && TheLoop->contains(Cmp) && Cmp->hasOneUse())
5411     addToWorklistIfAllowed(Cmp);
5412 
5413   auto isUniformDecision = [&](Instruction *I, ElementCount VF) {
5414     InstWidening WideningDecision = getWideningDecision(I, VF);
5415     assert(WideningDecision != CM_Unknown &&
5416            "Widening decision should be ready at this moment");
5417 
5418     // A uniform memory op is itself uniform.  We exclude uniform stores
5419     // here as they demand the last lane, not the first one.
5420     if (isa<LoadInst>(I) && Legal->isUniformMemOp(*I)) {
5421       assert(WideningDecision == CM_Scalarize);
5422       return true;
5423     }
5424 
5425     return (WideningDecision == CM_Widen ||
5426             WideningDecision == CM_Widen_Reverse ||
5427             WideningDecision == CM_Interleave);
5428   };
5429 
5430 
5431   // Returns true if Ptr is the pointer operand of a memory access instruction
5432   // I, and I is known to not require scalarization.
5433   auto isVectorizedMemAccessUse = [&](Instruction *I, Value *Ptr) -> bool {
5434     return getLoadStorePointerOperand(I) == Ptr && isUniformDecision(I, VF);
5435   };
5436 
5437   // Holds a list of values which are known to have at least one uniform use.
5438   // Note that there may be other uses which aren't uniform.  A "uniform use"
5439   // here is something which only demands lane 0 of the unrolled iterations;
5440   // it does not imply that all lanes produce the same value (e.g. this is not
5441   // the usual meaning of uniform)
5442   SetVector<Value *> HasUniformUse;
5443 
5444   // Scan the loop for instructions which are either a) known to have only
5445   // lane 0 demanded or b) are uses which demand only lane 0 of their operand.
5446   for (auto *BB : TheLoop->blocks())
5447     for (auto &I : *BB) {
5448       if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(&I)) {
5449         switch (II->getIntrinsicID()) {
5450         case Intrinsic::sideeffect:
5451         case Intrinsic::experimental_noalias_scope_decl:
5452         case Intrinsic::assume:
5453         case Intrinsic::lifetime_start:
5454         case Intrinsic::lifetime_end:
5455           if (TheLoop->hasLoopInvariantOperands(&I))
5456             addToWorklistIfAllowed(&I);
5457           break;
5458         default:
5459           break;
5460         }
5461       }
5462 
5463       // ExtractValue instructions must be uniform, because the operands are
5464       // known to be loop-invariant.
5465       if (auto *EVI = dyn_cast<ExtractValueInst>(&I)) {
5466         assert(isOutOfScope(EVI->getAggregateOperand()) &&
5467                "Expected aggregate value to be loop invariant");
5468         addToWorklistIfAllowed(EVI);
5469         continue;
5470       }
5471 
5472       // If there's no pointer operand, there's nothing to do.
5473       auto *Ptr = getLoadStorePointerOperand(&I);
5474       if (!Ptr)
5475         continue;
5476 
5477       // A uniform memory op is itself uniform.  We exclude uniform stores
5478       // here as they demand the last lane, not the first one.
5479       if (isa<LoadInst>(I) && Legal->isUniformMemOp(I))
5480         addToWorklistIfAllowed(&I);
5481 
5482       if (isUniformDecision(&I, VF)) {
5483         assert(isVectorizedMemAccessUse(&I, Ptr) && "consistency check");
5484         HasUniformUse.insert(Ptr);
5485       }
5486     }
5487 
5488   // Add to the worklist any operands which have *only* uniform (e.g. lane 0
5489   // demanding) users.  Since loops are assumed to be in LCSSA form, this
5490   // disallows uses outside the loop as well.
5491   for (auto *V : HasUniformUse) {
5492     if (isOutOfScope(V))
5493       continue;
5494     auto *I = cast<Instruction>(V);
5495     auto UsersAreMemAccesses =
5496       llvm::all_of(I->users(), [&](User *U) -> bool {
5497         return isVectorizedMemAccessUse(cast<Instruction>(U), V);
5498       });
5499     if (UsersAreMemAccesses)
5500       addToWorklistIfAllowed(I);
5501   }
5502 
5503   // Expand Worklist in topological order: whenever a new instruction
5504   // is added , its users should be already inside Worklist.  It ensures
5505   // a uniform instruction will only be used by uniform instructions.
5506   unsigned idx = 0;
5507   while (idx != Worklist.size()) {
5508     Instruction *I = Worklist[idx++];
5509 
5510     for (auto OV : I->operand_values()) {
5511       // isOutOfScope operands cannot be uniform instructions.
5512       if (isOutOfScope(OV))
5513         continue;
5514       // First order recurrence Phi's should typically be considered
5515       // non-uniform.
5516       auto *OP = dyn_cast<PHINode>(OV);
5517       if (OP && Legal->isFirstOrderRecurrence(OP))
5518         continue;
5519       // If all the users of the operand are uniform, then add the
5520       // operand into the uniform worklist.
5521       auto *OI = cast<Instruction>(OV);
5522       if (llvm::all_of(OI->users(), [&](User *U) -> bool {
5523             auto *J = cast<Instruction>(U);
5524             return Worklist.count(J) || isVectorizedMemAccessUse(J, OI);
5525           }))
5526         addToWorklistIfAllowed(OI);
5527     }
5528   }
5529 
5530   // For an instruction to be added into Worklist above, all its users inside
5531   // the loop should also be in Worklist. However, this condition cannot be
5532   // true for phi nodes that form a cyclic dependence. We must process phi
5533   // nodes separately. An induction variable will remain uniform if all users
5534   // of the induction variable and induction variable update remain uniform.
5535   // The code below handles both pointer and non-pointer induction variables.
5536   for (auto &Induction : Legal->getInductionVars()) {
5537     auto *Ind = Induction.first;
5538     auto *IndUpdate = cast<Instruction>(Ind->getIncomingValueForBlock(Latch));
5539 
5540     // Determine if all users of the induction variable are uniform after
5541     // vectorization.
5542     auto UniformInd = llvm::all_of(Ind->users(), [&](User *U) -> bool {
5543       auto *I = cast<Instruction>(U);
5544       return I == IndUpdate || !TheLoop->contains(I) || Worklist.count(I) ||
5545              isVectorizedMemAccessUse(I, Ind);
5546     });
5547     if (!UniformInd)
5548       continue;
5549 
5550     // Determine if all users of the induction variable update instruction are
5551     // uniform after vectorization.
5552     auto UniformIndUpdate =
5553         llvm::all_of(IndUpdate->users(), [&](User *U) -> bool {
5554           auto *I = cast<Instruction>(U);
5555           return I == Ind || !TheLoop->contains(I) || Worklist.count(I) ||
5556                  isVectorizedMemAccessUse(I, IndUpdate);
5557         });
5558     if (!UniformIndUpdate)
5559       continue;
5560 
5561     // The induction variable and its update instruction will remain uniform.
5562     addToWorklistIfAllowed(Ind);
5563     addToWorklistIfAllowed(IndUpdate);
5564   }
5565 
5566   Uniforms[VF].insert(Worklist.begin(), Worklist.end());
5567 }
5568 
5569 bool LoopVectorizationCostModel::runtimeChecksRequired() {
5570   LLVM_DEBUG(dbgs() << "LV: Performing code size checks.\n");
5571 
5572   if (Legal->getRuntimePointerChecking()->Need) {
5573     reportVectorizationFailure("Runtime ptr check is required with -Os/-Oz",
5574         "runtime pointer checks needed. Enable vectorization of this "
5575         "loop with '#pragma clang loop vectorize(enable)' when "
5576         "compiling with -Os/-Oz",
5577         "CantVersionLoopWithOptForSize", ORE, TheLoop);
5578     return true;
5579   }
5580 
5581   if (!PSE.getUnionPredicate().getPredicates().empty()) {
5582     reportVectorizationFailure("Runtime SCEV check is required with -Os/-Oz",
5583         "runtime SCEV checks needed. Enable vectorization of this "
5584         "loop with '#pragma clang loop vectorize(enable)' when "
5585         "compiling with -Os/-Oz",
5586         "CantVersionLoopWithOptForSize", ORE, TheLoop);
5587     return true;
5588   }
5589 
5590   // FIXME: Avoid specializing for stride==1 instead of bailing out.
5591   if (!Legal->getLAI()->getSymbolicStrides().empty()) {
5592     reportVectorizationFailure("Runtime stride check for small trip count",
5593         "runtime stride == 1 checks needed. Enable vectorization of "
5594         "this loop without such check by compiling with -Os/-Oz",
5595         "CantVersionLoopWithOptForSize", ORE, TheLoop);
5596     return true;
5597   }
5598 
5599   return false;
5600 }
5601 
5602 ElementCount
5603 LoopVectorizationCostModel::getMaxLegalScalableVF(unsigned MaxSafeElements) {
5604   if (!TTI.supportsScalableVectors() && !ForceTargetSupportsScalableVectors)
5605     return ElementCount::getScalable(0);
5606 
5607   if (Hints->isScalableVectorizationDisabled()) {
5608     reportVectorizationInfo("Scalable vectorization is explicitly disabled",
5609                             "ScalableVectorizationDisabled", ORE, TheLoop);
5610     return ElementCount::getScalable(0);
5611   }
5612 
5613   LLVM_DEBUG(dbgs() << "LV: Scalable vectorization is available\n");
5614 
5615   auto MaxScalableVF = ElementCount::getScalable(
5616       std::numeric_limits<ElementCount::ScalarTy>::max());
5617 
5618   // Test that the loop-vectorizer can legalize all operations for this MaxVF.
5619   // FIXME: While for scalable vectors this is currently sufficient, this should
5620   // be replaced by a more detailed mechanism that filters out specific VFs,
5621   // instead of invalidating vectorization for a whole set of VFs based on the
5622   // MaxVF.
5623 
5624   // Disable scalable vectorization if the loop contains unsupported reductions.
5625   if (!canVectorizeReductions(MaxScalableVF)) {
5626     reportVectorizationInfo(
5627         "Scalable vectorization not supported for the reduction "
5628         "operations found in this loop.",
5629         "ScalableVFUnfeasible", ORE, TheLoop);
5630     return ElementCount::getScalable(0);
5631   }
5632 
5633   // Disable scalable vectorization if the loop contains any instructions
5634   // with element types not supported for scalable vectors.
5635   if (any_of(ElementTypesInLoop, [&](Type *Ty) {
5636         return !Ty->isVoidTy() &&
5637                !this->TTI.isElementTypeLegalForScalableVector(Ty);
5638       })) {
5639     reportVectorizationInfo("Scalable vectorization is not supported "
5640                             "for all element types found in this loop.",
5641                             "ScalableVFUnfeasible", ORE, TheLoop);
5642     return ElementCount::getScalable(0);
5643   }
5644 
5645   if (Legal->isSafeForAnyVectorWidth())
5646     return MaxScalableVF;
5647 
5648   // Limit MaxScalableVF by the maximum safe dependence distance.
5649   Optional<unsigned> MaxVScale = TTI.getMaxVScale();
5650   if (!MaxVScale && TheFunction->hasFnAttribute(Attribute::VScaleRange)) {
5651     unsigned VScaleMax = TheFunction->getFnAttribute(Attribute::VScaleRange)
5652                              .getVScaleRangeArgs()
5653                              .second;
5654     if (VScaleMax > 0)
5655       MaxVScale = VScaleMax;
5656   }
5657   MaxScalableVF = ElementCount::getScalable(
5658       MaxVScale ? (MaxSafeElements / MaxVScale.getValue()) : 0);
5659   if (!MaxScalableVF)
5660     reportVectorizationInfo(
5661         "Max legal vector width too small, scalable vectorization "
5662         "unfeasible.",
5663         "ScalableVFUnfeasible", ORE, TheLoop);
5664 
5665   return MaxScalableVF;
5666 }
5667 
5668 FixedScalableVFPair
5669 LoopVectorizationCostModel::computeFeasibleMaxVF(unsigned ConstTripCount,
5670                                                  ElementCount UserVF) {
5671   MinBWs = computeMinimumValueSizes(TheLoop->getBlocks(), *DB, &TTI);
5672   unsigned SmallestType, WidestType;
5673   std::tie(SmallestType, WidestType) = getSmallestAndWidestTypes();
5674 
5675   // Get the maximum safe dependence distance in bits computed by LAA.
5676   // It is computed by MaxVF * sizeOf(type) * 8, where type is taken from
5677   // the memory accesses that is most restrictive (involved in the smallest
5678   // dependence distance).
5679   unsigned MaxSafeElements =
5680       PowerOf2Floor(Legal->getMaxSafeVectorWidthInBits() / WidestType);
5681 
5682   auto MaxSafeFixedVF = ElementCount::getFixed(MaxSafeElements);
5683   auto MaxSafeScalableVF = getMaxLegalScalableVF(MaxSafeElements);
5684 
5685   LLVM_DEBUG(dbgs() << "LV: The max safe fixed VF is: " << MaxSafeFixedVF
5686                     << ".\n");
5687   LLVM_DEBUG(dbgs() << "LV: The max safe scalable VF is: " << MaxSafeScalableVF
5688                     << ".\n");
5689 
5690   // First analyze the UserVF, fall back if the UserVF should be ignored.
5691   if (UserVF) {
5692     auto MaxSafeUserVF =
5693         UserVF.isScalable() ? MaxSafeScalableVF : MaxSafeFixedVF;
5694 
5695     if (ElementCount::isKnownLE(UserVF, MaxSafeUserVF)) {
5696       // If `VF=vscale x N` is safe, then so is `VF=N`
5697       if (UserVF.isScalable())
5698         return FixedScalableVFPair(
5699             ElementCount::getFixed(UserVF.getKnownMinValue()), UserVF);
5700       else
5701         return UserVF;
5702     }
5703 
5704     assert(ElementCount::isKnownGT(UserVF, MaxSafeUserVF));
5705 
5706     // Only clamp if the UserVF is not scalable. If the UserVF is scalable, it
5707     // is better to ignore the hint and let the compiler choose a suitable VF.
5708     if (!UserVF.isScalable()) {
5709       LLVM_DEBUG(dbgs() << "LV: User VF=" << UserVF
5710                         << " is unsafe, clamping to max safe VF="
5711                         << MaxSafeFixedVF << ".\n");
5712       ORE->emit([&]() {
5713         return OptimizationRemarkAnalysis(DEBUG_TYPE, "VectorizationFactor",
5714                                           TheLoop->getStartLoc(),
5715                                           TheLoop->getHeader())
5716                << "User-specified vectorization factor "
5717                << ore::NV("UserVectorizationFactor", UserVF)
5718                << " is unsafe, clamping to maximum safe vectorization factor "
5719                << ore::NV("VectorizationFactor", MaxSafeFixedVF);
5720       });
5721       return MaxSafeFixedVF;
5722     }
5723 
5724     if (!TTI.supportsScalableVectors() && !ForceTargetSupportsScalableVectors) {
5725       LLVM_DEBUG(dbgs() << "LV: User VF=" << UserVF
5726                         << " is ignored because scalable vectors are not "
5727                            "available.\n");
5728       ORE->emit([&]() {
5729         return OptimizationRemarkAnalysis(DEBUG_TYPE, "VectorizationFactor",
5730                                           TheLoop->getStartLoc(),
5731                                           TheLoop->getHeader())
5732                << "User-specified vectorization factor "
5733                << ore::NV("UserVectorizationFactor", UserVF)
5734                << " is ignored because the target does not support scalable "
5735                   "vectors. The compiler will pick a more suitable value.";
5736       });
5737     } else {
5738       LLVM_DEBUG(dbgs() << "LV: User VF=" << UserVF
5739                         << " is unsafe. Ignoring scalable UserVF.\n");
5740       ORE->emit([&]() {
5741         return OptimizationRemarkAnalysis(DEBUG_TYPE, "VectorizationFactor",
5742                                           TheLoop->getStartLoc(),
5743                                           TheLoop->getHeader())
5744                << "User-specified vectorization factor "
5745                << ore::NV("UserVectorizationFactor", UserVF)
5746                << " is unsafe. Ignoring the hint to let the compiler pick a "
5747                   "more suitable value.";
5748       });
5749     }
5750   }
5751 
5752   LLVM_DEBUG(dbgs() << "LV: The Smallest and Widest types: " << SmallestType
5753                     << " / " << WidestType << " bits.\n");
5754 
5755   FixedScalableVFPair Result(ElementCount::getFixed(1),
5756                              ElementCount::getScalable(0));
5757   if (auto MaxVF = getMaximizedVFForTarget(ConstTripCount, SmallestType,
5758                                            WidestType, MaxSafeFixedVF))
5759     Result.FixedVF = MaxVF;
5760 
5761   if (auto MaxVF = getMaximizedVFForTarget(ConstTripCount, SmallestType,
5762                                            WidestType, MaxSafeScalableVF))
5763     if (MaxVF.isScalable()) {
5764       Result.ScalableVF = MaxVF;
5765       LLVM_DEBUG(dbgs() << "LV: Found feasible scalable VF = " << MaxVF
5766                         << "\n");
5767     }
5768 
5769   return Result;
5770 }
5771 
5772 FixedScalableVFPair
5773 LoopVectorizationCostModel::computeMaxVF(ElementCount UserVF, unsigned UserIC) {
5774   if (Legal->getRuntimePointerChecking()->Need && TTI.hasBranchDivergence()) {
5775     // TODO: It may by useful to do since it's still likely to be dynamically
5776     // uniform if the target can skip.
5777     reportVectorizationFailure(
5778         "Not inserting runtime ptr check for divergent target",
5779         "runtime pointer checks needed. Not enabled for divergent target",
5780         "CantVersionLoopWithDivergentTarget", ORE, TheLoop);
5781     return FixedScalableVFPair::getNone();
5782   }
5783 
5784   unsigned TC = PSE.getSE()->getSmallConstantTripCount(TheLoop);
5785   LLVM_DEBUG(dbgs() << "LV: Found trip count: " << TC << '\n');
5786   if (TC == 1) {
5787     reportVectorizationFailure("Single iteration (non) loop",
5788         "loop trip count is one, irrelevant for vectorization",
5789         "SingleIterationLoop", ORE, TheLoop);
5790     return FixedScalableVFPair::getNone();
5791   }
5792 
5793   switch (ScalarEpilogueStatus) {
5794   case CM_ScalarEpilogueAllowed:
5795     return computeFeasibleMaxVF(TC, UserVF);
5796   case CM_ScalarEpilogueNotAllowedUsePredicate:
5797     LLVM_FALLTHROUGH;
5798   case CM_ScalarEpilogueNotNeededUsePredicate:
5799     LLVM_DEBUG(
5800         dbgs() << "LV: vector predicate hint/switch found.\n"
5801                << "LV: Not allowing scalar epilogue, creating predicated "
5802                << "vector loop.\n");
5803     break;
5804   case CM_ScalarEpilogueNotAllowedLowTripLoop:
5805     // fallthrough as a special case of OptForSize
5806   case CM_ScalarEpilogueNotAllowedOptSize:
5807     if (ScalarEpilogueStatus == CM_ScalarEpilogueNotAllowedOptSize)
5808       LLVM_DEBUG(
5809           dbgs() << "LV: Not allowing scalar epilogue due to -Os/-Oz.\n");
5810     else
5811       LLVM_DEBUG(dbgs() << "LV: Not allowing scalar epilogue due to low trip "
5812                         << "count.\n");
5813 
5814     // Bail if runtime checks are required, which are not good when optimising
5815     // for size.
5816     if (runtimeChecksRequired())
5817       return FixedScalableVFPair::getNone();
5818 
5819     break;
5820   }
5821 
5822   // The only loops we can vectorize without a scalar epilogue, are loops with
5823   // a bottom-test and a single exiting block. We'd have to handle the fact
5824   // that not every instruction executes on the last iteration.  This will
5825   // require a lane mask which varies through the vector loop body.  (TODO)
5826   if (TheLoop->getExitingBlock() != TheLoop->getLoopLatch()) {
5827     // If there was a tail-folding hint/switch, but we can't fold the tail by
5828     // masking, fallback to a vectorization with a scalar epilogue.
5829     if (ScalarEpilogueStatus == CM_ScalarEpilogueNotNeededUsePredicate) {
5830       LLVM_DEBUG(dbgs() << "LV: Cannot fold tail by masking: vectorize with a "
5831                            "scalar epilogue instead.\n");
5832       ScalarEpilogueStatus = CM_ScalarEpilogueAllowed;
5833       return computeFeasibleMaxVF(TC, UserVF);
5834     }
5835     return FixedScalableVFPair::getNone();
5836   }
5837 
5838   // Now try the tail folding
5839 
5840   // Invalidate interleave groups that require an epilogue if we can't mask
5841   // the interleave-group.
5842   if (!useMaskedInterleavedAccesses(TTI)) {
5843     assert(WideningDecisions.empty() && Uniforms.empty() && Scalars.empty() &&
5844            "No decisions should have been taken at this point");
5845     // Note: There is no need to invalidate any cost modeling decisions here, as
5846     // non where taken so far.
5847     InterleaveInfo.invalidateGroupsRequiringScalarEpilogue();
5848   }
5849 
5850   FixedScalableVFPair MaxFactors = computeFeasibleMaxVF(TC, UserVF);
5851   // Avoid tail folding if the trip count is known to be a multiple of any VF
5852   // we chose.
5853   // FIXME: The condition below pessimises the case for fixed-width vectors,
5854   // when scalable VFs are also candidates for vectorization.
5855   if (MaxFactors.FixedVF.isVector() && !MaxFactors.ScalableVF) {
5856     ElementCount MaxFixedVF = MaxFactors.FixedVF;
5857     assert((UserVF.isNonZero() || isPowerOf2_32(MaxFixedVF.getFixedValue())) &&
5858            "MaxFixedVF must be a power of 2");
5859     unsigned MaxVFtimesIC = UserIC ? MaxFixedVF.getFixedValue() * UserIC
5860                                    : MaxFixedVF.getFixedValue();
5861     ScalarEvolution *SE = PSE.getSE();
5862     const SCEV *BackedgeTakenCount = PSE.getBackedgeTakenCount();
5863     const SCEV *ExitCount = SE->getAddExpr(
5864         BackedgeTakenCount, SE->getOne(BackedgeTakenCount->getType()));
5865     const SCEV *Rem = SE->getURemExpr(
5866         SE->applyLoopGuards(ExitCount, TheLoop),
5867         SE->getConstant(BackedgeTakenCount->getType(), MaxVFtimesIC));
5868     if (Rem->isZero()) {
5869       // Accept MaxFixedVF if we do not have a tail.
5870       LLVM_DEBUG(dbgs() << "LV: No tail will remain for any chosen VF.\n");
5871       return MaxFactors;
5872     }
5873   }
5874 
5875   // For scalable vectors, don't use tail folding as this is currently not yet
5876   // supported. The code is likely to have ended up here if the tripcount is
5877   // low, in which case it makes sense not to use scalable vectors.
5878   if (MaxFactors.ScalableVF.isVector())
5879     MaxFactors.ScalableVF = ElementCount::getScalable(0);
5880 
5881   // If we don't know the precise trip count, or if the trip count that we
5882   // found modulo the vectorization factor is not zero, try to fold the tail
5883   // by masking.
5884   // FIXME: look for a smaller MaxVF that does divide TC rather than masking.
5885   if (Legal->prepareToFoldTailByMasking()) {
5886     FoldTailByMasking = true;
5887     return MaxFactors;
5888   }
5889 
5890   // If there was a tail-folding hint/switch, but we can't fold the tail by
5891   // masking, fallback to a vectorization with a scalar epilogue.
5892   if (ScalarEpilogueStatus == CM_ScalarEpilogueNotNeededUsePredicate) {
5893     LLVM_DEBUG(dbgs() << "LV: Cannot fold tail by masking: vectorize with a "
5894                          "scalar epilogue instead.\n");
5895     ScalarEpilogueStatus = CM_ScalarEpilogueAllowed;
5896     return MaxFactors;
5897   }
5898 
5899   if (ScalarEpilogueStatus == CM_ScalarEpilogueNotAllowedUsePredicate) {
5900     LLVM_DEBUG(dbgs() << "LV: Can't fold tail by masking: don't vectorize\n");
5901     return FixedScalableVFPair::getNone();
5902   }
5903 
5904   if (TC == 0) {
5905     reportVectorizationFailure(
5906         "Unable to calculate the loop count due to complex control flow",
5907         "unable to calculate the loop count due to complex control flow",
5908         "UnknownLoopCountComplexCFG", ORE, TheLoop);
5909     return FixedScalableVFPair::getNone();
5910   }
5911 
5912   reportVectorizationFailure(
5913       "Cannot optimize for size and vectorize at the same time.",
5914       "cannot optimize for size and vectorize at the same time. "
5915       "Enable vectorization of this loop with '#pragma clang loop "
5916       "vectorize(enable)' when compiling with -Os/-Oz",
5917       "NoTailLoopWithOptForSize", ORE, TheLoop);
5918   return FixedScalableVFPair::getNone();
5919 }
5920 
5921 ElementCount LoopVectorizationCostModel::getMaximizedVFForTarget(
5922     unsigned ConstTripCount, unsigned SmallestType, unsigned WidestType,
5923     const ElementCount &MaxSafeVF) {
5924   bool ComputeScalableMaxVF = MaxSafeVF.isScalable();
5925   TypeSize WidestRegister = TTI.getRegisterBitWidth(
5926       ComputeScalableMaxVF ? TargetTransformInfo::RGK_ScalableVector
5927                            : TargetTransformInfo::RGK_FixedWidthVector);
5928 
5929   // Convenience function to return the minimum of two ElementCounts.
5930   auto MinVF = [](const ElementCount &LHS, const ElementCount &RHS) {
5931     assert((LHS.isScalable() == RHS.isScalable()) &&
5932            "Scalable flags must match");
5933     return ElementCount::isKnownLT(LHS, RHS) ? LHS : RHS;
5934   };
5935 
5936   // Ensure MaxVF is a power of 2; the dependence distance bound may not be.
5937   // Note that both WidestRegister and WidestType may not be a powers of 2.
5938   auto MaxVectorElementCount = ElementCount::get(
5939       PowerOf2Floor(WidestRegister.getKnownMinSize() / WidestType),
5940       ComputeScalableMaxVF);
5941   MaxVectorElementCount = MinVF(MaxVectorElementCount, MaxSafeVF);
5942   LLVM_DEBUG(dbgs() << "LV: The Widest register safe to use is: "
5943                     << (MaxVectorElementCount * WidestType) << " bits.\n");
5944 
5945   if (!MaxVectorElementCount) {
5946     LLVM_DEBUG(dbgs() << "LV: The target has no "
5947                       << (ComputeScalableMaxVF ? "scalable" : "fixed")
5948                       << " vector registers.\n");
5949     return ElementCount::getFixed(1);
5950   }
5951 
5952   const auto TripCountEC = ElementCount::getFixed(ConstTripCount);
5953   if (ConstTripCount &&
5954       ElementCount::isKnownLE(TripCountEC, MaxVectorElementCount) &&
5955       isPowerOf2_32(ConstTripCount)) {
5956     // We need to clamp the VF to be the ConstTripCount. There is no point in
5957     // choosing a higher viable VF as done in the loop below. If
5958     // MaxVectorElementCount is scalable, we only fall back on a fixed VF when
5959     // the TC is less than or equal to the known number of lanes.
5960     LLVM_DEBUG(dbgs() << "LV: Clamping the MaxVF to the constant trip count: "
5961                       << ConstTripCount << "\n");
5962     return TripCountEC;
5963   }
5964 
5965   ElementCount MaxVF = MaxVectorElementCount;
5966   if (TTI.shouldMaximizeVectorBandwidth() ||
5967       (MaximizeBandwidth && isScalarEpilogueAllowed())) {
5968     auto MaxVectorElementCountMaxBW = ElementCount::get(
5969         PowerOf2Floor(WidestRegister.getKnownMinSize() / SmallestType),
5970         ComputeScalableMaxVF);
5971     MaxVectorElementCountMaxBW = MinVF(MaxVectorElementCountMaxBW, MaxSafeVF);
5972 
5973     // Collect all viable vectorization factors larger than the default MaxVF
5974     // (i.e. MaxVectorElementCount).
5975     SmallVector<ElementCount, 8> VFs;
5976     for (ElementCount VS = MaxVectorElementCount * 2;
5977          ElementCount::isKnownLE(VS, MaxVectorElementCountMaxBW); VS *= 2)
5978       VFs.push_back(VS);
5979 
5980     // For each VF calculate its register usage.
5981     auto RUs = calculateRegisterUsage(VFs);
5982 
5983     // Select the largest VF which doesn't require more registers than existing
5984     // ones.
5985     for (int i = RUs.size() - 1; i >= 0; --i) {
5986       bool Selected = true;
5987       for (auto &pair : RUs[i].MaxLocalUsers) {
5988         unsigned TargetNumRegisters = TTI.getNumberOfRegisters(pair.first);
5989         if (pair.second > TargetNumRegisters)
5990           Selected = false;
5991       }
5992       if (Selected) {
5993         MaxVF = VFs[i];
5994         break;
5995       }
5996     }
5997     if (ElementCount MinVF =
5998             TTI.getMinimumVF(SmallestType, ComputeScalableMaxVF)) {
5999       if (ElementCount::isKnownLT(MaxVF, MinVF)) {
6000         LLVM_DEBUG(dbgs() << "LV: Overriding calculated MaxVF(" << MaxVF
6001                           << ") with target's minimum: " << MinVF << '\n');
6002         MaxVF = MinVF;
6003       }
6004     }
6005   }
6006   return MaxVF;
6007 }
6008 
6009 bool LoopVectorizationCostModel::isMoreProfitable(
6010     const VectorizationFactor &A, const VectorizationFactor &B) const {
6011   InstructionCost CostA = A.Cost;
6012   InstructionCost CostB = B.Cost;
6013 
6014   unsigned MaxTripCount = PSE.getSE()->getSmallConstantMaxTripCount(TheLoop);
6015 
6016   if (!A.Width.isScalable() && !B.Width.isScalable() && FoldTailByMasking &&
6017       MaxTripCount) {
6018     // If we are folding the tail and the trip count is a known (possibly small)
6019     // constant, the trip count will be rounded up to an integer number of
6020     // iterations. The total cost will be PerIterationCost*ceil(TripCount/VF),
6021     // which we compare directly. When not folding the tail, the total cost will
6022     // be PerIterationCost*floor(TC/VF) + Scalar remainder cost, and so is
6023     // approximated with the per-lane cost below instead of using the tripcount
6024     // as here.
6025     auto RTCostA = CostA * divideCeil(MaxTripCount, A.Width.getFixedValue());
6026     auto RTCostB = CostB * divideCeil(MaxTripCount, B.Width.getFixedValue());
6027     return RTCostA < RTCostB;
6028   }
6029 
6030   // When set to preferred, for now assume vscale may be larger than 1, so
6031   // that scalable vectorization is slightly favorable over fixed-width
6032   // vectorization.
6033   if (Hints->isScalableVectorizationPreferred())
6034     if (A.Width.isScalable() && !B.Width.isScalable())
6035       return (CostA * B.Width.getKnownMinValue()) <=
6036              (CostB * A.Width.getKnownMinValue());
6037 
6038   // To avoid the need for FP division:
6039   //      (CostA / A.Width) < (CostB / B.Width)
6040   // <=>  (CostA * B.Width) < (CostB * A.Width)
6041   return (CostA * B.Width.getKnownMinValue()) <
6042          (CostB * A.Width.getKnownMinValue());
6043 }
6044 
6045 VectorizationFactor LoopVectorizationCostModel::selectVectorizationFactor(
6046     const ElementCountSet &VFCandidates) {
6047   InstructionCost ExpectedCost = expectedCost(ElementCount::getFixed(1)).first;
6048   LLVM_DEBUG(dbgs() << "LV: Scalar loop costs: " << ExpectedCost << ".\n");
6049   assert(ExpectedCost.isValid() && "Unexpected invalid cost for scalar loop");
6050   assert(VFCandidates.count(ElementCount::getFixed(1)) &&
6051          "Expected Scalar VF to be a candidate");
6052 
6053   const VectorizationFactor ScalarCost(ElementCount::getFixed(1), ExpectedCost);
6054   VectorizationFactor ChosenFactor = ScalarCost;
6055 
6056   bool ForceVectorization = Hints->getForce() == LoopVectorizeHints::FK_Enabled;
6057   if (ForceVectorization && VFCandidates.size() > 1) {
6058     // Ignore scalar width, because the user explicitly wants vectorization.
6059     // Initialize cost to max so that VF = 2 is, at least, chosen during cost
6060     // evaluation.
6061     ChosenFactor.Cost = InstructionCost::getMax();
6062   }
6063 
6064   SmallVector<InstructionVFPair> InvalidCosts;
6065   for (const auto &i : VFCandidates) {
6066     // The cost for scalar VF=1 is already calculated, so ignore it.
6067     if (i.isScalar())
6068       continue;
6069 
6070     VectorizationCostTy C = expectedCost(i, &InvalidCosts);
6071     VectorizationFactor Candidate(i, C.first);
6072     LLVM_DEBUG(
6073         dbgs() << "LV: Vector loop of width " << i << " costs: "
6074                << (Candidate.Cost / Candidate.Width.getKnownMinValue())
6075                << (i.isScalable() ? " (assuming a minimum vscale of 1)" : "")
6076                << ".\n");
6077 
6078     if (!C.second && !ForceVectorization) {
6079       LLVM_DEBUG(
6080           dbgs() << "LV: Not considering vector loop of width " << i
6081                  << " because it will not generate any vector instructions.\n");
6082       continue;
6083     }
6084 
6085     // If profitable add it to ProfitableVF list.
6086     if (isMoreProfitable(Candidate, ScalarCost))
6087       ProfitableVFs.push_back(Candidate);
6088 
6089     if (isMoreProfitable(Candidate, ChosenFactor))
6090       ChosenFactor = Candidate;
6091   }
6092 
6093   // Emit a report of VFs with invalid costs in the loop.
6094   if (!InvalidCosts.empty()) {
6095     // Group the remarks per instruction, keeping the instruction order from
6096     // InvalidCosts.
6097     std::map<Instruction *, unsigned> Numbering;
6098     unsigned I = 0;
6099     for (auto &Pair : InvalidCosts)
6100       if (!Numbering.count(Pair.first))
6101         Numbering[Pair.first] = I++;
6102 
6103     // Sort the list, first on instruction(number) then on VF.
6104     llvm::sort(InvalidCosts,
6105                [&Numbering](InstructionVFPair &A, InstructionVFPair &B) {
6106                  if (Numbering[A.first] != Numbering[B.first])
6107                    return Numbering[A.first] < Numbering[B.first];
6108                  ElementCountComparator ECC;
6109                  return ECC(A.second, B.second);
6110                });
6111 
6112     // For a list of ordered instruction-vf pairs:
6113     //   [(load, vf1), (load, vf2), (store, vf1)]
6114     // Group the instructions together to emit separate remarks for:
6115     //   load  (vf1, vf2)
6116     //   store (vf1)
6117     auto Tail = ArrayRef<InstructionVFPair>(InvalidCosts);
6118     auto Subset = ArrayRef<InstructionVFPair>();
6119     do {
6120       if (Subset.empty())
6121         Subset = Tail.take_front(1);
6122 
6123       Instruction *I = Subset.front().first;
6124 
6125       // If the next instruction is different, or if there are no other pairs,
6126       // emit a remark for the collated subset. e.g.
6127       //   [(load, vf1), (load, vf2))]
6128       // to emit:
6129       //  remark: invalid costs for 'load' at VF=(vf, vf2)
6130       if (Subset == Tail || Tail[Subset.size()].first != I) {
6131         std::string OutString;
6132         raw_string_ostream OS(OutString);
6133         assert(!Subset.empty() && "Unexpected empty range");
6134         OS << "Instruction with invalid costs prevented vectorization at VF=(";
6135         for (auto &Pair : Subset)
6136           OS << (Pair.second == Subset.front().second ? "" : ", ")
6137              << Pair.second;
6138         OS << "):";
6139         if (auto *CI = dyn_cast<CallInst>(I))
6140           OS << " call to " << CI->getCalledFunction()->getName();
6141         else
6142           OS << " " << I->getOpcodeName();
6143         OS.flush();
6144         reportVectorizationInfo(OutString, "InvalidCost", ORE, TheLoop, I);
6145         Tail = Tail.drop_front(Subset.size());
6146         Subset = {};
6147       } else
6148         // Grow the subset by one element
6149         Subset = Tail.take_front(Subset.size() + 1);
6150     } while (!Tail.empty());
6151   }
6152 
6153   if (!EnableCondStoresVectorization && NumPredStores) {
6154     reportVectorizationFailure("There are conditional stores.",
6155         "store that is conditionally executed prevents vectorization",
6156         "ConditionalStore", ORE, TheLoop);
6157     ChosenFactor = ScalarCost;
6158   }
6159 
6160   LLVM_DEBUG(if (ForceVectorization && !ChosenFactor.Width.isScalar() &&
6161                  ChosenFactor.Cost >= ScalarCost.Cost) dbgs()
6162              << "LV: Vectorization seems to be not beneficial, "
6163              << "but was forced by a user.\n");
6164   LLVM_DEBUG(dbgs() << "LV: Selecting VF: " << ChosenFactor.Width << ".\n");
6165   return ChosenFactor;
6166 }
6167 
6168 bool LoopVectorizationCostModel::isCandidateForEpilogueVectorization(
6169     const Loop &L, ElementCount VF) const {
6170   // Cross iteration phis such as reductions need special handling and are
6171   // currently unsupported.
6172   if (any_of(L.getHeader()->phis(), [&](PHINode &Phi) {
6173         return Legal->isFirstOrderRecurrence(&Phi) ||
6174                Legal->isReductionVariable(&Phi);
6175       }))
6176     return false;
6177 
6178   // Phis with uses outside of the loop require special handling and are
6179   // currently unsupported.
6180   for (auto &Entry : Legal->getInductionVars()) {
6181     // Look for uses of the value of the induction at the last iteration.
6182     Value *PostInc = Entry.first->getIncomingValueForBlock(L.getLoopLatch());
6183     for (User *U : PostInc->users())
6184       if (!L.contains(cast<Instruction>(U)))
6185         return false;
6186     // Look for uses of penultimate value of the induction.
6187     for (User *U : Entry.first->users())
6188       if (!L.contains(cast<Instruction>(U)))
6189         return false;
6190   }
6191 
6192   // Induction variables that are widened require special handling that is
6193   // currently not supported.
6194   if (any_of(Legal->getInductionVars(), [&](auto &Entry) {
6195         return !(this->isScalarAfterVectorization(Entry.first, VF) ||
6196                  this->isProfitableToScalarize(Entry.first, VF));
6197       }))
6198     return false;
6199 
6200   // Epilogue vectorization code has not been auditted to ensure it handles
6201   // non-latch exits properly.  It may be fine, but it needs auditted and
6202   // tested.
6203   if (L.getExitingBlock() != L.getLoopLatch())
6204     return false;
6205 
6206   return true;
6207 }
6208 
6209 bool LoopVectorizationCostModel::isEpilogueVectorizationProfitable(
6210     const ElementCount VF) const {
6211   // FIXME: We need a much better cost-model to take different parameters such
6212   // as register pressure, code size increase and cost of extra branches into
6213   // account. For now we apply a very crude heuristic and only consider loops
6214   // with vectorization factors larger than a certain value.
6215   // We also consider epilogue vectorization unprofitable for targets that don't
6216   // consider interleaving beneficial (eg. MVE).
6217   if (TTI.getMaxInterleaveFactor(VF.getKnownMinValue()) <= 1)
6218     return false;
6219   if (VF.getFixedValue() >= EpilogueVectorizationMinVF)
6220     return true;
6221   return false;
6222 }
6223 
6224 VectorizationFactor
6225 LoopVectorizationCostModel::selectEpilogueVectorizationFactor(
6226     const ElementCount MainLoopVF, const LoopVectorizationPlanner &LVP) {
6227   VectorizationFactor Result = VectorizationFactor::Disabled();
6228   if (!EnableEpilogueVectorization) {
6229     LLVM_DEBUG(dbgs() << "LEV: Epilogue vectorization is disabled.\n";);
6230     return Result;
6231   }
6232 
6233   if (!isScalarEpilogueAllowed()) {
6234     LLVM_DEBUG(
6235         dbgs() << "LEV: Unable to vectorize epilogue because no epilogue is "
6236                   "allowed.\n";);
6237     return Result;
6238   }
6239 
6240   // FIXME: This can be fixed for scalable vectors later, because at this stage
6241   // the LoopVectorizer will only consider vectorizing a loop with scalable
6242   // vectors when the loop has a hint to enable vectorization for a given VF.
6243   if (MainLoopVF.isScalable()) {
6244     LLVM_DEBUG(dbgs() << "LEV: Epilogue vectorization for scalable vectors not "
6245                          "yet supported.\n");
6246     return Result;
6247   }
6248 
6249   // Not really a cost consideration, but check for unsupported cases here to
6250   // simplify the logic.
6251   if (!isCandidateForEpilogueVectorization(*TheLoop, MainLoopVF)) {
6252     LLVM_DEBUG(
6253         dbgs() << "LEV: Unable to vectorize epilogue because the loop is "
6254                   "not a supported candidate.\n";);
6255     return Result;
6256   }
6257 
6258   if (EpilogueVectorizationForceVF > 1) {
6259     LLVM_DEBUG(dbgs() << "LEV: Epilogue vectorization factor is forced.\n";);
6260     ElementCount ForcedEC = ElementCount::getFixed(EpilogueVectorizationForceVF);
6261     if (LVP.hasPlanWithVFs({MainLoopVF, ForcedEC}))
6262       return {ForcedEC, 0};
6263     else {
6264       LLVM_DEBUG(
6265           dbgs()
6266               << "LEV: Epilogue vectorization forced factor is not viable.\n";);
6267       return Result;
6268     }
6269   }
6270 
6271   if (TheLoop->getHeader()->getParent()->hasOptSize() ||
6272       TheLoop->getHeader()->getParent()->hasMinSize()) {
6273     LLVM_DEBUG(
6274         dbgs()
6275             << "LEV: Epilogue vectorization skipped due to opt for size.\n";);
6276     return Result;
6277   }
6278 
6279   if (!isEpilogueVectorizationProfitable(MainLoopVF))
6280     return Result;
6281 
6282   for (auto &NextVF : ProfitableVFs)
6283     if (ElementCount::isKnownLT(NextVF.Width, MainLoopVF) &&
6284         (Result.Width.getFixedValue() == 1 ||
6285          isMoreProfitable(NextVF, Result)) &&
6286         LVP.hasPlanWithVFs({MainLoopVF, NextVF.Width}))
6287       Result = NextVF;
6288 
6289   if (Result != VectorizationFactor::Disabled())
6290     LLVM_DEBUG(dbgs() << "LEV: Vectorizing epilogue loop with VF = "
6291                       << Result.Width.getFixedValue() << "\n";);
6292   return Result;
6293 }
6294 
6295 std::pair<unsigned, unsigned>
6296 LoopVectorizationCostModel::getSmallestAndWidestTypes() {
6297   unsigned MinWidth = -1U;
6298   unsigned MaxWidth = 8;
6299   const DataLayout &DL = TheFunction->getParent()->getDataLayout();
6300   for (Type *T : ElementTypesInLoop) {
6301     MinWidth = std::min<unsigned>(
6302         MinWidth, DL.getTypeSizeInBits(T->getScalarType()).getFixedSize());
6303     MaxWidth = std::max<unsigned>(
6304         MaxWidth, DL.getTypeSizeInBits(T->getScalarType()).getFixedSize());
6305   }
6306   return {MinWidth, MaxWidth};
6307 }
6308 
6309 void LoopVectorizationCostModel::collectElementTypesForWidening() {
6310   ElementTypesInLoop.clear();
6311   // For each block.
6312   for (BasicBlock *BB : TheLoop->blocks()) {
6313     // For each instruction in the loop.
6314     for (Instruction &I : BB->instructionsWithoutDebug()) {
6315       Type *T = I.getType();
6316 
6317       // Skip ignored values.
6318       if (ValuesToIgnore.count(&I))
6319         continue;
6320 
6321       // Only examine Loads, Stores and PHINodes.
6322       if (!isa<LoadInst>(I) && !isa<StoreInst>(I) && !isa<PHINode>(I))
6323         continue;
6324 
6325       // Examine PHI nodes that are reduction variables. Update the type to
6326       // account for the recurrence type.
6327       if (auto *PN = dyn_cast<PHINode>(&I)) {
6328         if (!Legal->isReductionVariable(PN))
6329           continue;
6330         const RecurrenceDescriptor &RdxDesc = Legal->getReductionVars()[PN];
6331         if (PreferInLoopReductions || useOrderedReductions(RdxDesc) ||
6332             TTI.preferInLoopReduction(RdxDesc.getOpcode(),
6333                                       RdxDesc.getRecurrenceType(),
6334                                       TargetTransformInfo::ReductionFlags()))
6335           continue;
6336         T = RdxDesc.getRecurrenceType();
6337       }
6338 
6339       // Examine the stored values.
6340       if (auto *ST = dyn_cast<StoreInst>(&I))
6341         T = ST->getValueOperand()->getType();
6342 
6343       // Ignore loaded pointer types and stored pointer types that are not
6344       // vectorizable.
6345       //
6346       // FIXME: The check here attempts to predict whether a load or store will
6347       //        be vectorized. We only know this for certain after a VF has
6348       //        been selected. Here, we assume that if an access can be
6349       //        vectorized, it will be. We should also look at extending this
6350       //        optimization to non-pointer types.
6351       //
6352       if (T->isPointerTy() && !isConsecutiveLoadOrStore(&I) &&
6353           !isAccessInterleaved(&I) && !isLegalGatherOrScatter(&I))
6354         continue;
6355 
6356       ElementTypesInLoop.insert(T);
6357     }
6358   }
6359 }
6360 
6361 unsigned LoopVectorizationCostModel::selectInterleaveCount(ElementCount VF,
6362                                                            unsigned LoopCost) {
6363   // -- The interleave heuristics --
6364   // We interleave the loop in order to expose ILP and reduce the loop overhead.
6365   // There are many micro-architectural considerations that we can't predict
6366   // at this level. For example, frontend pressure (on decode or fetch) due to
6367   // code size, or the number and capabilities of the execution ports.
6368   //
6369   // We use the following heuristics to select the interleave count:
6370   // 1. If the code has reductions, then we interleave to break the cross
6371   // iteration dependency.
6372   // 2. If the loop is really small, then we interleave to reduce the loop
6373   // overhead.
6374   // 3. We don't interleave if we think that we will spill registers to memory
6375   // due to the increased register pressure.
6376 
6377   if (!isScalarEpilogueAllowed())
6378     return 1;
6379 
6380   // We used the distance for the interleave count.
6381   if (Legal->getMaxSafeDepDistBytes() != -1U)
6382     return 1;
6383 
6384   auto BestKnownTC = getSmallBestKnownTC(*PSE.getSE(), TheLoop);
6385   const bool HasReductions = !Legal->getReductionVars().empty();
6386   // Do not interleave loops with a relatively small known or estimated trip
6387   // count. But we will interleave when InterleaveSmallLoopScalarReduction is
6388   // enabled, and the code has scalar reductions(HasReductions && VF = 1),
6389   // because with the above conditions interleaving can expose ILP and break
6390   // cross iteration dependences for reductions.
6391   if (BestKnownTC && (*BestKnownTC < TinyTripCountInterleaveThreshold) &&
6392       !(InterleaveSmallLoopScalarReduction && HasReductions && VF.isScalar()))
6393     return 1;
6394 
6395   RegisterUsage R = calculateRegisterUsage({VF})[0];
6396   // We divide by these constants so assume that we have at least one
6397   // instruction that uses at least one register.
6398   for (auto& pair : R.MaxLocalUsers) {
6399     pair.second = std::max(pair.second, 1U);
6400   }
6401 
6402   // We calculate the interleave count using the following formula.
6403   // Subtract the number of loop invariants from the number of available
6404   // registers. These registers are used by all of the interleaved instances.
6405   // Next, divide the remaining registers by the number of registers that is
6406   // required by the loop, in order to estimate how many parallel instances
6407   // fit without causing spills. All of this is rounded down if necessary to be
6408   // a power of two. We want power of two interleave count to simplify any
6409   // addressing operations or alignment considerations.
6410   // We also want power of two interleave counts to ensure that the induction
6411   // variable of the vector loop wraps to zero, when tail is folded by masking;
6412   // this currently happens when OptForSize, in which case IC is set to 1 above.
6413   unsigned IC = UINT_MAX;
6414 
6415   for (auto& pair : R.MaxLocalUsers) {
6416     unsigned TargetNumRegisters = TTI.getNumberOfRegisters(pair.first);
6417     LLVM_DEBUG(dbgs() << "LV: The target has " << TargetNumRegisters
6418                       << " registers of "
6419                       << TTI.getRegisterClassName(pair.first) << " register class\n");
6420     if (VF.isScalar()) {
6421       if (ForceTargetNumScalarRegs.getNumOccurrences() > 0)
6422         TargetNumRegisters = ForceTargetNumScalarRegs;
6423     } else {
6424       if (ForceTargetNumVectorRegs.getNumOccurrences() > 0)
6425         TargetNumRegisters = ForceTargetNumVectorRegs;
6426     }
6427     unsigned MaxLocalUsers = pair.second;
6428     unsigned LoopInvariantRegs = 0;
6429     if (R.LoopInvariantRegs.find(pair.first) != R.LoopInvariantRegs.end())
6430       LoopInvariantRegs = R.LoopInvariantRegs[pair.first];
6431 
6432     unsigned TmpIC = PowerOf2Floor((TargetNumRegisters - LoopInvariantRegs) / MaxLocalUsers);
6433     // Don't count the induction variable as interleaved.
6434     if (EnableIndVarRegisterHeur) {
6435       TmpIC =
6436           PowerOf2Floor((TargetNumRegisters - LoopInvariantRegs - 1) /
6437                         std::max(1U, (MaxLocalUsers - 1)));
6438     }
6439 
6440     IC = std::min(IC, TmpIC);
6441   }
6442 
6443   // Clamp the interleave ranges to reasonable counts.
6444   unsigned MaxInterleaveCount =
6445       TTI.getMaxInterleaveFactor(VF.getKnownMinValue());
6446 
6447   // Check if the user has overridden the max.
6448   if (VF.isScalar()) {
6449     if (ForceTargetMaxScalarInterleaveFactor.getNumOccurrences() > 0)
6450       MaxInterleaveCount = ForceTargetMaxScalarInterleaveFactor;
6451   } else {
6452     if (ForceTargetMaxVectorInterleaveFactor.getNumOccurrences() > 0)
6453       MaxInterleaveCount = ForceTargetMaxVectorInterleaveFactor;
6454   }
6455 
6456   // If trip count is known or estimated compile time constant, limit the
6457   // interleave count to be less than the trip count divided by VF, provided it
6458   // is at least 1.
6459   //
6460   // For scalable vectors we can't know if interleaving is beneficial. It may
6461   // not be beneficial for small loops if none of the lanes in the second vector
6462   // iterations is enabled. However, for larger loops, there is likely to be a
6463   // similar benefit as for fixed-width vectors. For now, we choose to leave
6464   // the InterleaveCount as if vscale is '1', although if some information about
6465   // the vector is known (e.g. min vector size), we can make a better decision.
6466   if (BestKnownTC) {
6467     MaxInterleaveCount =
6468         std::min(*BestKnownTC / VF.getKnownMinValue(), MaxInterleaveCount);
6469     // Make sure MaxInterleaveCount is greater than 0.
6470     MaxInterleaveCount = std::max(1u, MaxInterleaveCount);
6471   }
6472 
6473   assert(MaxInterleaveCount > 0 &&
6474          "Maximum interleave count must be greater than 0");
6475 
6476   // Clamp the calculated IC to be between the 1 and the max interleave count
6477   // that the target and trip count allows.
6478   if (IC > MaxInterleaveCount)
6479     IC = MaxInterleaveCount;
6480   else
6481     // Make sure IC is greater than 0.
6482     IC = std::max(1u, IC);
6483 
6484   assert(IC > 0 && "Interleave count must be greater than 0.");
6485 
6486   // If we did not calculate the cost for VF (because the user selected the VF)
6487   // then we calculate the cost of VF here.
6488   if (LoopCost == 0) {
6489     InstructionCost C = expectedCost(VF).first;
6490     assert(C.isValid() && "Expected to have chosen a VF with valid cost");
6491     LoopCost = *C.getValue();
6492   }
6493 
6494   assert(LoopCost && "Non-zero loop cost expected");
6495 
6496   // Interleave if we vectorized this loop and there is a reduction that could
6497   // benefit from interleaving.
6498   if (VF.isVector() && HasReductions) {
6499     LLVM_DEBUG(dbgs() << "LV: Interleaving because of reductions.\n");
6500     return IC;
6501   }
6502 
6503   // Note that if we've already vectorized the loop we will have done the
6504   // runtime check and so interleaving won't require further checks.
6505   bool InterleavingRequiresRuntimePointerCheck =
6506       (VF.isScalar() && Legal->getRuntimePointerChecking()->Need);
6507 
6508   // We want to interleave small loops in order to reduce the loop overhead and
6509   // potentially expose ILP opportunities.
6510   LLVM_DEBUG(dbgs() << "LV: Loop cost is " << LoopCost << '\n'
6511                     << "LV: IC is " << IC << '\n'
6512                     << "LV: VF is " << VF << '\n');
6513   const bool AggressivelyInterleaveReductions =
6514       TTI.enableAggressiveInterleaving(HasReductions);
6515   if (!InterleavingRequiresRuntimePointerCheck && LoopCost < SmallLoopCost) {
6516     // We assume that the cost overhead is 1 and we use the cost model
6517     // to estimate the cost of the loop and interleave until the cost of the
6518     // loop overhead is about 5% of the cost of the loop.
6519     unsigned SmallIC =
6520         std::min(IC, (unsigned)PowerOf2Floor(SmallLoopCost / LoopCost));
6521 
6522     // Interleave until store/load ports (estimated by max interleave count) are
6523     // saturated.
6524     unsigned NumStores = Legal->getNumStores();
6525     unsigned NumLoads = Legal->getNumLoads();
6526     unsigned StoresIC = IC / (NumStores ? NumStores : 1);
6527     unsigned LoadsIC = IC / (NumLoads ? NumLoads : 1);
6528 
6529     // There is little point in interleaving for reductions containing selects
6530     // and compares when VF=1 since it may just create more overhead than it's
6531     // worth for loops with small trip counts. This is because we still have to
6532     // do the final reduction after the loop.
6533     bool HasSelectCmpReductions =
6534         HasReductions &&
6535         any_of(Legal->getReductionVars(), [&](auto &Reduction) -> bool {
6536           const RecurrenceDescriptor &RdxDesc = Reduction.second;
6537           return RecurrenceDescriptor::isSelectCmpRecurrenceKind(
6538               RdxDesc.getRecurrenceKind());
6539         });
6540     if (HasSelectCmpReductions) {
6541       LLVM_DEBUG(dbgs() << "LV: Not interleaving select-cmp reductions.\n");
6542       return 1;
6543     }
6544 
6545     // If we have a scalar reduction (vector reductions are already dealt with
6546     // by this point), we can increase the critical path length if the loop
6547     // we're interleaving is inside another loop. For tree-wise reductions
6548     // set the limit to 2, and for ordered reductions it's best to disable
6549     // interleaving entirely.
6550     if (HasReductions && TheLoop->getLoopDepth() > 1) {
6551       bool HasOrderedReductions =
6552           any_of(Legal->getReductionVars(), [&](auto &Reduction) -> bool {
6553             const RecurrenceDescriptor &RdxDesc = Reduction.second;
6554             return RdxDesc.isOrdered();
6555           });
6556       if (HasOrderedReductions) {
6557         LLVM_DEBUG(
6558             dbgs() << "LV: Not interleaving scalar ordered reductions.\n");
6559         return 1;
6560       }
6561 
6562       unsigned F = static_cast<unsigned>(MaxNestedScalarReductionIC);
6563       SmallIC = std::min(SmallIC, F);
6564       StoresIC = std::min(StoresIC, F);
6565       LoadsIC = std::min(LoadsIC, F);
6566     }
6567 
6568     if (EnableLoadStoreRuntimeInterleave &&
6569         std::max(StoresIC, LoadsIC) > SmallIC) {
6570       LLVM_DEBUG(
6571           dbgs() << "LV: Interleaving to saturate store or load ports.\n");
6572       return std::max(StoresIC, LoadsIC);
6573     }
6574 
6575     // If there are scalar reductions and TTI has enabled aggressive
6576     // interleaving for reductions, we will interleave to expose ILP.
6577     if (InterleaveSmallLoopScalarReduction && VF.isScalar() &&
6578         AggressivelyInterleaveReductions) {
6579       LLVM_DEBUG(dbgs() << "LV: Interleaving to expose ILP.\n");
6580       // Interleave no less than SmallIC but not as aggressive as the normal IC
6581       // to satisfy the rare situation when resources are too limited.
6582       return std::max(IC / 2, SmallIC);
6583     } else {
6584       LLVM_DEBUG(dbgs() << "LV: Interleaving to reduce branch cost.\n");
6585       return SmallIC;
6586     }
6587   }
6588 
6589   // Interleave if this is a large loop (small loops are already dealt with by
6590   // this point) that could benefit from interleaving.
6591   if (AggressivelyInterleaveReductions) {
6592     LLVM_DEBUG(dbgs() << "LV: Interleaving to expose ILP.\n");
6593     return IC;
6594   }
6595 
6596   LLVM_DEBUG(dbgs() << "LV: Not Interleaving.\n");
6597   return 1;
6598 }
6599 
6600 SmallVector<LoopVectorizationCostModel::RegisterUsage, 8>
6601 LoopVectorizationCostModel::calculateRegisterUsage(ArrayRef<ElementCount> VFs) {
6602   // This function calculates the register usage by measuring the highest number
6603   // of values that are alive at a single location. Obviously, this is a very
6604   // rough estimation. We scan the loop in a topological order in order and
6605   // assign a number to each instruction. We use RPO to ensure that defs are
6606   // met before their users. We assume that each instruction that has in-loop
6607   // users starts an interval. We record every time that an in-loop value is
6608   // used, so we have a list of the first and last occurrences of each
6609   // instruction. Next, we transpose this data structure into a multi map that
6610   // holds the list of intervals that *end* at a specific location. This multi
6611   // map allows us to perform a linear search. We scan the instructions linearly
6612   // and record each time that a new interval starts, by placing it in a set.
6613   // If we find this value in the multi-map then we remove it from the set.
6614   // The max register usage is the maximum size of the set.
6615   // We also search for instructions that are defined outside the loop, but are
6616   // used inside the loop. We need this number separately from the max-interval
6617   // usage number because when we unroll, loop-invariant values do not take
6618   // more register.
6619   LoopBlocksDFS DFS(TheLoop);
6620   DFS.perform(LI);
6621 
6622   RegisterUsage RU;
6623 
6624   // Each 'key' in the map opens a new interval. The values
6625   // of the map are the index of the 'last seen' usage of the
6626   // instruction that is the key.
6627   using IntervalMap = DenseMap<Instruction *, unsigned>;
6628 
6629   // Maps instruction to its index.
6630   SmallVector<Instruction *, 64> IdxToInstr;
6631   // Marks the end of each interval.
6632   IntervalMap EndPoint;
6633   // Saves the list of instruction indices that are used in the loop.
6634   SmallPtrSet<Instruction *, 8> Ends;
6635   // Saves the list of values that are used in the loop but are
6636   // defined outside the loop, such as arguments and constants.
6637   SmallPtrSet<Value *, 8> LoopInvariants;
6638 
6639   for (BasicBlock *BB : make_range(DFS.beginRPO(), DFS.endRPO())) {
6640     for (Instruction &I : BB->instructionsWithoutDebug()) {
6641       IdxToInstr.push_back(&I);
6642 
6643       // Save the end location of each USE.
6644       for (Value *U : I.operands()) {
6645         auto *Instr = dyn_cast<Instruction>(U);
6646 
6647         // Ignore non-instruction values such as arguments, constants, etc.
6648         if (!Instr)
6649           continue;
6650 
6651         // If this instruction is outside the loop then record it and continue.
6652         if (!TheLoop->contains(Instr)) {
6653           LoopInvariants.insert(Instr);
6654           continue;
6655         }
6656 
6657         // Overwrite previous end points.
6658         EndPoint[Instr] = IdxToInstr.size();
6659         Ends.insert(Instr);
6660       }
6661     }
6662   }
6663 
6664   // Saves the list of intervals that end with the index in 'key'.
6665   using InstrList = SmallVector<Instruction *, 2>;
6666   DenseMap<unsigned, InstrList> TransposeEnds;
6667 
6668   // Transpose the EndPoints to a list of values that end at each index.
6669   for (auto &Interval : EndPoint)
6670     TransposeEnds[Interval.second].push_back(Interval.first);
6671 
6672   SmallPtrSet<Instruction *, 8> OpenIntervals;
6673   SmallVector<RegisterUsage, 8> RUs(VFs.size());
6674   SmallVector<SmallMapVector<unsigned, unsigned, 4>, 8> MaxUsages(VFs.size());
6675 
6676   LLVM_DEBUG(dbgs() << "LV(REG): Calculating max register usage:\n");
6677 
6678   // A lambda that gets the register usage for the given type and VF.
6679   const auto &TTICapture = TTI;
6680   auto GetRegUsage = [&TTICapture](Type *Ty, ElementCount VF) -> unsigned {
6681     if (Ty->isTokenTy() || !VectorType::isValidElementType(Ty))
6682       return 0;
6683     InstructionCost::CostType RegUsage =
6684         *TTICapture.getRegUsageForType(VectorType::get(Ty, VF)).getValue();
6685     assert(RegUsage >= 0 && RegUsage <= std::numeric_limits<unsigned>::max() &&
6686            "Nonsensical values for register usage.");
6687     return RegUsage;
6688   };
6689 
6690   for (unsigned int i = 0, s = IdxToInstr.size(); i < s; ++i) {
6691     Instruction *I = IdxToInstr[i];
6692 
6693     // Remove all of the instructions that end at this location.
6694     InstrList &List = TransposeEnds[i];
6695     for (Instruction *ToRemove : List)
6696       OpenIntervals.erase(ToRemove);
6697 
6698     // Ignore instructions that are never used within the loop.
6699     if (!Ends.count(I))
6700       continue;
6701 
6702     // Skip ignored values.
6703     if (ValuesToIgnore.count(I))
6704       continue;
6705 
6706     // For each VF find the maximum usage of registers.
6707     for (unsigned j = 0, e = VFs.size(); j < e; ++j) {
6708       // Count the number of live intervals.
6709       SmallMapVector<unsigned, unsigned, 4> RegUsage;
6710 
6711       if (VFs[j].isScalar()) {
6712         for (auto Inst : OpenIntervals) {
6713           unsigned ClassID = TTI.getRegisterClassForType(false, Inst->getType());
6714           if (RegUsage.find(ClassID) == RegUsage.end())
6715             RegUsage[ClassID] = 1;
6716           else
6717             RegUsage[ClassID] += 1;
6718         }
6719       } else {
6720         collectUniformsAndScalars(VFs[j]);
6721         for (auto Inst : OpenIntervals) {
6722           // Skip ignored values for VF > 1.
6723           if (VecValuesToIgnore.count(Inst))
6724             continue;
6725           if (isScalarAfterVectorization(Inst, VFs[j])) {
6726             unsigned ClassID = TTI.getRegisterClassForType(false, Inst->getType());
6727             if (RegUsage.find(ClassID) == RegUsage.end())
6728               RegUsage[ClassID] = 1;
6729             else
6730               RegUsage[ClassID] += 1;
6731           } else {
6732             unsigned ClassID = TTI.getRegisterClassForType(true, Inst->getType());
6733             if (RegUsage.find(ClassID) == RegUsage.end())
6734               RegUsage[ClassID] = GetRegUsage(Inst->getType(), VFs[j]);
6735             else
6736               RegUsage[ClassID] += GetRegUsage(Inst->getType(), VFs[j]);
6737           }
6738         }
6739       }
6740 
6741       for (auto& pair : RegUsage) {
6742         if (MaxUsages[j].find(pair.first) != MaxUsages[j].end())
6743           MaxUsages[j][pair.first] = std::max(MaxUsages[j][pair.first], pair.second);
6744         else
6745           MaxUsages[j][pair.first] = pair.second;
6746       }
6747     }
6748 
6749     LLVM_DEBUG(dbgs() << "LV(REG): At #" << i << " Interval # "
6750                       << OpenIntervals.size() << '\n');
6751 
6752     // Add the current instruction to the list of open intervals.
6753     OpenIntervals.insert(I);
6754   }
6755 
6756   for (unsigned i = 0, e = VFs.size(); i < e; ++i) {
6757     SmallMapVector<unsigned, unsigned, 4> Invariant;
6758 
6759     for (auto Inst : LoopInvariants) {
6760       unsigned Usage =
6761           VFs[i].isScalar() ? 1 : GetRegUsage(Inst->getType(), VFs[i]);
6762       unsigned ClassID =
6763           TTI.getRegisterClassForType(VFs[i].isVector(), Inst->getType());
6764       if (Invariant.find(ClassID) == Invariant.end())
6765         Invariant[ClassID] = Usage;
6766       else
6767         Invariant[ClassID] += Usage;
6768     }
6769 
6770     LLVM_DEBUG({
6771       dbgs() << "LV(REG): VF = " << VFs[i] << '\n';
6772       dbgs() << "LV(REG): Found max usage: " << MaxUsages[i].size()
6773              << " item\n";
6774       for (const auto &pair : MaxUsages[i]) {
6775         dbgs() << "LV(REG): RegisterClass: "
6776                << TTI.getRegisterClassName(pair.first) << ", " << pair.second
6777                << " registers\n";
6778       }
6779       dbgs() << "LV(REG): Found invariant usage: " << Invariant.size()
6780              << " item\n";
6781       for (const auto &pair : Invariant) {
6782         dbgs() << "LV(REG): RegisterClass: "
6783                << TTI.getRegisterClassName(pair.first) << ", " << pair.second
6784                << " registers\n";
6785       }
6786     });
6787 
6788     RU.LoopInvariantRegs = Invariant;
6789     RU.MaxLocalUsers = MaxUsages[i];
6790     RUs[i] = RU;
6791   }
6792 
6793   return RUs;
6794 }
6795 
6796 bool LoopVectorizationCostModel::useEmulatedMaskMemRefHack(Instruction *I){
6797   // TODO: Cost model for emulated masked load/store is completely
6798   // broken. This hack guides the cost model to use an artificially
6799   // high enough value to practically disable vectorization with such
6800   // operations, except where previously deployed legality hack allowed
6801   // using very low cost values. This is to avoid regressions coming simply
6802   // from moving "masked load/store" check from legality to cost model.
6803   // Masked Load/Gather emulation was previously never allowed.
6804   // Limited number of Masked Store/Scatter emulation was allowed.
6805   assert(isPredicatedInst(I) &&
6806          "Expecting a scalar emulated instruction");
6807   return isa<LoadInst>(I) ||
6808          (isa<StoreInst>(I) &&
6809           NumPredStores > NumberOfStoresToPredicate);
6810 }
6811 
6812 void LoopVectorizationCostModel::collectInstsToScalarize(ElementCount VF) {
6813   // If we aren't vectorizing the loop, or if we've already collected the
6814   // instructions to scalarize, there's nothing to do. Collection may already
6815   // have occurred if we have a user-selected VF and are now computing the
6816   // expected cost for interleaving.
6817   if (VF.isScalar() || VF.isZero() ||
6818       InstsToScalarize.find(VF) != InstsToScalarize.end())
6819     return;
6820 
6821   // Initialize a mapping for VF in InstsToScalalarize. If we find that it's
6822   // not profitable to scalarize any instructions, the presence of VF in the
6823   // map will indicate that we've analyzed it already.
6824   ScalarCostsTy &ScalarCostsVF = InstsToScalarize[VF];
6825 
6826   // Find all the instructions that are scalar with predication in the loop and
6827   // determine if it would be better to not if-convert the blocks they are in.
6828   // If so, we also record the instructions to scalarize.
6829   for (BasicBlock *BB : TheLoop->blocks()) {
6830     if (!blockNeedsPredication(BB))
6831       continue;
6832     for (Instruction &I : *BB)
6833       if (isScalarWithPredication(&I)) {
6834         ScalarCostsTy ScalarCosts;
6835         // Do not apply discount if scalable, because that would lead to
6836         // invalid scalarization costs.
6837         // Do not apply discount logic if hacked cost is needed
6838         // for emulated masked memrefs.
6839         if (!VF.isScalable() && !useEmulatedMaskMemRefHack(&I) &&
6840             computePredInstDiscount(&I, ScalarCosts, VF) >= 0)
6841           ScalarCostsVF.insert(ScalarCosts.begin(), ScalarCosts.end());
6842         // Remember that BB will remain after vectorization.
6843         PredicatedBBsAfterVectorization.insert(BB);
6844       }
6845   }
6846 }
6847 
6848 int LoopVectorizationCostModel::computePredInstDiscount(
6849     Instruction *PredInst, ScalarCostsTy &ScalarCosts, ElementCount VF) {
6850   assert(!isUniformAfterVectorization(PredInst, VF) &&
6851          "Instruction marked uniform-after-vectorization will be predicated");
6852 
6853   // Initialize the discount to zero, meaning that the scalar version and the
6854   // vector version cost the same.
6855   InstructionCost Discount = 0;
6856 
6857   // Holds instructions to analyze. The instructions we visit are mapped in
6858   // ScalarCosts. Those instructions are the ones that would be scalarized if
6859   // we find that the scalar version costs less.
6860   SmallVector<Instruction *, 8> Worklist;
6861 
6862   // Returns true if the given instruction can be scalarized.
6863   auto canBeScalarized = [&](Instruction *I) -> bool {
6864     // We only attempt to scalarize instructions forming a single-use chain
6865     // from the original predicated block that would otherwise be vectorized.
6866     // Although not strictly necessary, we give up on instructions we know will
6867     // already be scalar to avoid traversing chains that are unlikely to be
6868     // beneficial.
6869     if (!I->hasOneUse() || PredInst->getParent() != I->getParent() ||
6870         isScalarAfterVectorization(I, VF))
6871       return false;
6872 
6873     // If the instruction is scalar with predication, it will be analyzed
6874     // separately. We ignore it within the context of PredInst.
6875     if (isScalarWithPredication(I))
6876       return false;
6877 
6878     // If any of the instruction's operands are uniform after vectorization,
6879     // the instruction cannot be scalarized. This prevents, for example, a
6880     // masked load from being scalarized.
6881     //
6882     // We assume we will only emit a value for lane zero of an instruction
6883     // marked uniform after vectorization, rather than VF identical values.
6884     // Thus, if we scalarize an instruction that uses a uniform, we would
6885     // create uses of values corresponding to the lanes we aren't emitting code
6886     // for. This behavior can be changed by allowing getScalarValue to clone
6887     // the lane zero values for uniforms rather than asserting.
6888     for (Use &U : I->operands())
6889       if (auto *J = dyn_cast<Instruction>(U.get()))
6890         if (isUniformAfterVectorization(J, VF))
6891           return false;
6892 
6893     // Otherwise, we can scalarize the instruction.
6894     return true;
6895   };
6896 
6897   // Compute the expected cost discount from scalarizing the entire expression
6898   // feeding the predicated instruction. We currently only consider expressions
6899   // that are single-use instruction chains.
6900   Worklist.push_back(PredInst);
6901   while (!Worklist.empty()) {
6902     Instruction *I = Worklist.pop_back_val();
6903 
6904     // If we've already analyzed the instruction, there's nothing to do.
6905     if (ScalarCosts.find(I) != ScalarCosts.end())
6906       continue;
6907 
6908     // Compute the cost of the vector instruction. Note that this cost already
6909     // includes the scalarization overhead of the predicated instruction.
6910     InstructionCost VectorCost = getInstructionCost(I, VF).first;
6911 
6912     // Compute the cost of the scalarized instruction. This cost is the cost of
6913     // the instruction as if it wasn't if-converted and instead remained in the
6914     // predicated block. We will scale this cost by block probability after
6915     // computing the scalarization overhead.
6916     InstructionCost ScalarCost =
6917         VF.getFixedValue() *
6918         getInstructionCost(I, ElementCount::getFixed(1)).first;
6919 
6920     // Compute the scalarization overhead of needed insertelement instructions
6921     // and phi nodes.
6922     if (isScalarWithPredication(I) && !I->getType()->isVoidTy()) {
6923       ScalarCost += TTI.getScalarizationOverhead(
6924           cast<VectorType>(ToVectorTy(I->getType(), VF)),
6925           APInt::getAllOnes(VF.getFixedValue()), true, false);
6926       ScalarCost +=
6927           VF.getFixedValue() *
6928           TTI.getCFInstrCost(Instruction::PHI, TTI::TCK_RecipThroughput);
6929     }
6930 
6931     // Compute the scalarization overhead of needed extractelement
6932     // instructions. For each of the instruction's operands, if the operand can
6933     // be scalarized, add it to the worklist; otherwise, account for the
6934     // overhead.
6935     for (Use &U : I->operands())
6936       if (auto *J = dyn_cast<Instruction>(U.get())) {
6937         assert(VectorType::isValidElementType(J->getType()) &&
6938                "Instruction has non-scalar type");
6939         if (canBeScalarized(J))
6940           Worklist.push_back(J);
6941         else if (needsExtract(J, VF)) {
6942           ScalarCost += TTI.getScalarizationOverhead(
6943               cast<VectorType>(ToVectorTy(J->getType(), VF)),
6944               APInt::getAllOnes(VF.getFixedValue()), false, true);
6945         }
6946       }
6947 
6948     // Scale the total scalar cost by block probability.
6949     ScalarCost /= getReciprocalPredBlockProb();
6950 
6951     // Compute the discount. A non-negative discount means the vector version
6952     // of the instruction costs more, and scalarizing would be beneficial.
6953     Discount += VectorCost - ScalarCost;
6954     ScalarCosts[I] = ScalarCost;
6955   }
6956 
6957   return *Discount.getValue();
6958 }
6959 
6960 LoopVectorizationCostModel::VectorizationCostTy
6961 LoopVectorizationCostModel::expectedCost(
6962     ElementCount VF, SmallVectorImpl<InstructionVFPair> *Invalid) {
6963   VectorizationCostTy Cost;
6964 
6965   // For each block.
6966   for (BasicBlock *BB : TheLoop->blocks()) {
6967     VectorizationCostTy BlockCost;
6968 
6969     // For each instruction in the old loop.
6970     for (Instruction &I : BB->instructionsWithoutDebug()) {
6971       // Skip ignored values.
6972       if (ValuesToIgnore.count(&I) ||
6973           (VF.isVector() && VecValuesToIgnore.count(&I)))
6974         continue;
6975 
6976       VectorizationCostTy C = getInstructionCost(&I, VF);
6977 
6978       // Check if we should override the cost.
6979       if (C.first.isValid() &&
6980           ForceTargetInstructionCost.getNumOccurrences() > 0)
6981         C.first = InstructionCost(ForceTargetInstructionCost);
6982 
6983       // Keep a list of instructions with invalid costs.
6984       if (Invalid && !C.first.isValid())
6985         Invalid->emplace_back(&I, VF);
6986 
6987       BlockCost.first += C.first;
6988       BlockCost.second |= C.second;
6989       LLVM_DEBUG(dbgs() << "LV: Found an estimated cost of " << C.first
6990                         << " for VF " << VF << " For instruction: " << I
6991                         << '\n');
6992     }
6993 
6994     // If we are vectorizing a predicated block, it will have been
6995     // if-converted. This means that the block's instructions (aside from
6996     // stores and instructions that may divide by zero) will now be
6997     // unconditionally executed. For the scalar case, we may not always execute
6998     // the predicated block, if it is an if-else block. Thus, scale the block's
6999     // cost by the probability of executing it. blockNeedsPredication from
7000     // Legal is used so as to not include all blocks in tail folded loops.
7001     if (VF.isScalar() && Legal->blockNeedsPredication(BB))
7002       BlockCost.first /= getReciprocalPredBlockProb();
7003 
7004     Cost.first += BlockCost.first;
7005     Cost.second |= BlockCost.second;
7006   }
7007 
7008   return Cost;
7009 }
7010 
7011 /// Gets Address Access SCEV after verifying that the access pattern
7012 /// is loop invariant except the induction variable dependence.
7013 ///
7014 /// This SCEV can be sent to the Target in order to estimate the address
7015 /// calculation cost.
7016 static const SCEV *getAddressAccessSCEV(
7017               Value *Ptr,
7018               LoopVectorizationLegality *Legal,
7019               PredicatedScalarEvolution &PSE,
7020               const Loop *TheLoop) {
7021 
7022   auto *Gep = dyn_cast<GetElementPtrInst>(Ptr);
7023   if (!Gep)
7024     return nullptr;
7025 
7026   // We are looking for a gep with all loop invariant indices except for one
7027   // which should be an induction variable.
7028   auto SE = PSE.getSE();
7029   unsigned NumOperands = Gep->getNumOperands();
7030   for (unsigned i = 1; i < NumOperands; ++i) {
7031     Value *Opd = Gep->getOperand(i);
7032     if (!SE->isLoopInvariant(SE->getSCEV(Opd), TheLoop) &&
7033         !Legal->isInductionVariable(Opd))
7034       return nullptr;
7035   }
7036 
7037   // Now we know we have a GEP ptr, %inv, %ind, %inv. return the Ptr SCEV.
7038   return PSE.getSCEV(Ptr);
7039 }
7040 
7041 static bool isStrideMul(Instruction *I, LoopVectorizationLegality *Legal) {
7042   return Legal->hasStride(I->getOperand(0)) ||
7043          Legal->hasStride(I->getOperand(1));
7044 }
7045 
7046 InstructionCost
7047 LoopVectorizationCostModel::getMemInstScalarizationCost(Instruction *I,
7048                                                         ElementCount VF) {
7049   assert(VF.isVector() &&
7050          "Scalarization cost of instruction implies vectorization.");
7051   if (VF.isScalable())
7052     return InstructionCost::getInvalid();
7053 
7054   Type *ValTy = getLoadStoreType(I);
7055   auto SE = PSE.getSE();
7056 
7057   unsigned AS = getLoadStoreAddressSpace(I);
7058   Value *Ptr = getLoadStorePointerOperand(I);
7059   Type *PtrTy = ToVectorTy(Ptr->getType(), VF);
7060 
7061   // Figure out whether the access is strided and get the stride value
7062   // if it's known in compile time
7063   const SCEV *PtrSCEV = getAddressAccessSCEV(Ptr, Legal, PSE, TheLoop);
7064 
7065   // Get the cost of the scalar memory instruction and address computation.
7066   InstructionCost Cost =
7067       VF.getKnownMinValue() * TTI.getAddressComputationCost(PtrTy, SE, PtrSCEV);
7068 
7069   // Don't pass *I here, since it is scalar but will actually be part of a
7070   // vectorized loop where the user of it is a vectorized instruction.
7071   const Align Alignment = getLoadStoreAlignment(I);
7072   Cost += VF.getKnownMinValue() *
7073           TTI.getMemoryOpCost(I->getOpcode(), ValTy->getScalarType(), Alignment,
7074                               AS, TTI::TCK_RecipThroughput);
7075 
7076   // Get the overhead of the extractelement and insertelement instructions
7077   // we might create due to scalarization.
7078   Cost += getScalarizationOverhead(I, VF);
7079 
7080   // If we have a predicated load/store, it will need extra i1 extracts and
7081   // conditional branches, but may not be executed for each vector lane. Scale
7082   // the cost by the probability of executing the predicated block.
7083   if (isPredicatedInst(I)) {
7084     Cost /= getReciprocalPredBlockProb();
7085 
7086     // Add the cost of an i1 extract and a branch
7087     auto *Vec_i1Ty =
7088         VectorType::get(IntegerType::getInt1Ty(ValTy->getContext()), VF);
7089     Cost += TTI.getScalarizationOverhead(
7090         Vec_i1Ty, APInt::getAllOnes(VF.getKnownMinValue()),
7091         /*Insert=*/false, /*Extract=*/true);
7092     Cost += TTI.getCFInstrCost(Instruction::Br, TTI::TCK_RecipThroughput);
7093 
7094     if (useEmulatedMaskMemRefHack(I))
7095       // Artificially setting to a high enough value to practically disable
7096       // vectorization with such operations.
7097       Cost = 3000000;
7098   }
7099 
7100   return Cost;
7101 }
7102 
7103 InstructionCost
7104 LoopVectorizationCostModel::getConsecutiveMemOpCost(Instruction *I,
7105                                                     ElementCount VF) {
7106   Type *ValTy = getLoadStoreType(I);
7107   auto *VectorTy = cast<VectorType>(ToVectorTy(ValTy, VF));
7108   Value *Ptr = getLoadStorePointerOperand(I);
7109   unsigned AS = getLoadStoreAddressSpace(I);
7110   int ConsecutiveStride = Legal->isConsecutivePtr(ValTy, Ptr);
7111   enum TTI::TargetCostKind CostKind = TTI::TCK_RecipThroughput;
7112 
7113   assert((ConsecutiveStride == 1 || ConsecutiveStride == -1) &&
7114          "Stride should be 1 or -1 for consecutive memory access");
7115   const Align Alignment = getLoadStoreAlignment(I);
7116   InstructionCost Cost = 0;
7117   if (Legal->isMaskRequired(I))
7118     Cost += TTI.getMaskedMemoryOpCost(I->getOpcode(), VectorTy, Alignment, AS,
7119                                       CostKind);
7120   else
7121     Cost += TTI.getMemoryOpCost(I->getOpcode(), VectorTy, Alignment, AS,
7122                                 CostKind, I);
7123 
7124   bool Reverse = ConsecutiveStride < 0;
7125   if (Reverse)
7126     Cost +=
7127         TTI.getShuffleCost(TargetTransformInfo::SK_Reverse, VectorTy, None, 0);
7128   return Cost;
7129 }
7130 
7131 InstructionCost
7132 LoopVectorizationCostModel::getUniformMemOpCost(Instruction *I,
7133                                                 ElementCount VF) {
7134   assert(Legal->isUniformMemOp(*I));
7135 
7136   Type *ValTy = getLoadStoreType(I);
7137   auto *VectorTy = cast<VectorType>(ToVectorTy(ValTy, VF));
7138   const Align Alignment = getLoadStoreAlignment(I);
7139   unsigned AS = getLoadStoreAddressSpace(I);
7140   enum TTI::TargetCostKind CostKind = TTI::TCK_RecipThroughput;
7141   if (isa<LoadInst>(I)) {
7142     return TTI.getAddressComputationCost(ValTy) +
7143            TTI.getMemoryOpCost(Instruction::Load, ValTy, Alignment, AS,
7144                                CostKind) +
7145            TTI.getShuffleCost(TargetTransformInfo::SK_Broadcast, VectorTy);
7146   }
7147   StoreInst *SI = cast<StoreInst>(I);
7148 
7149   bool isLoopInvariantStoreValue = Legal->isUniform(SI->getValueOperand());
7150   return TTI.getAddressComputationCost(ValTy) +
7151          TTI.getMemoryOpCost(Instruction::Store, ValTy, Alignment, AS,
7152                              CostKind) +
7153          (isLoopInvariantStoreValue
7154               ? 0
7155               : TTI.getVectorInstrCost(Instruction::ExtractElement, VectorTy,
7156                                        VF.getKnownMinValue() - 1));
7157 }
7158 
7159 InstructionCost
7160 LoopVectorizationCostModel::getGatherScatterCost(Instruction *I,
7161                                                  ElementCount VF) {
7162   Type *ValTy = getLoadStoreType(I);
7163   auto *VectorTy = cast<VectorType>(ToVectorTy(ValTy, VF));
7164   const Align Alignment = getLoadStoreAlignment(I);
7165   const Value *Ptr = getLoadStorePointerOperand(I);
7166 
7167   return TTI.getAddressComputationCost(VectorTy) +
7168          TTI.getGatherScatterOpCost(
7169              I->getOpcode(), VectorTy, Ptr, Legal->isMaskRequired(I), Alignment,
7170              TargetTransformInfo::TCK_RecipThroughput, I);
7171 }
7172 
7173 InstructionCost
7174 LoopVectorizationCostModel::getInterleaveGroupCost(Instruction *I,
7175                                                    ElementCount VF) {
7176   // TODO: Once we have support for interleaving with scalable vectors
7177   // we can calculate the cost properly here.
7178   if (VF.isScalable())
7179     return InstructionCost::getInvalid();
7180 
7181   Type *ValTy = getLoadStoreType(I);
7182   auto *VectorTy = cast<VectorType>(ToVectorTy(ValTy, VF));
7183   unsigned AS = getLoadStoreAddressSpace(I);
7184 
7185   auto Group = getInterleavedAccessGroup(I);
7186   assert(Group && "Fail to get an interleaved access group.");
7187 
7188   unsigned InterleaveFactor = Group->getFactor();
7189   auto *WideVecTy = VectorType::get(ValTy, VF * InterleaveFactor);
7190 
7191   // Holds the indices of existing members in the interleaved group.
7192   SmallVector<unsigned, 4> Indices;
7193   for (unsigned IF = 0; IF < InterleaveFactor; IF++)
7194     if (Group->getMember(IF))
7195       Indices.push_back(IF);
7196 
7197   // Calculate the cost of the whole interleaved group.
7198   bool UseMaskForGaps =
7199       (Group->requiresScalarEpilogue() && !isScalarEpilogueAllowed()) ||
7200       (isa<StoreInst>(I) && (Group->getNumMembers() < Group->getFactor()));
7201   InstructionCost Cost = TTI.getInterleavedMemoryOpCost(
7202       I->getOpcode(), WideVecTy, Group->getFactor(), Indices, Group->getAlign(),
7203       AS, TTI::TCK_RecipThroughput, Legal->isMaskRequired(I), UseMaskForGaps);
7204 
7205   if (Group->isReverse()) {
7206     // TODO: Add support for reversed masked interleaved access.
7207     assert(!Legal->isMaskRequired(I) &&
7208            "Reverse masked interleaved access not supported.");
7209     Cost +=
7210         Group->getNumMembers() *
7211         TTI.getShuffleCost(TargetTransformInfo::SK_Reverse, VectorTy, None, 0);
7212   }
7213   return Cost;
7214 }
7215 
7216 Optional<InstructionCost> LoopVectorizationCostModel::getReductionPatternCost(
7217     Instruction *I, ElementCount VF, Type *Ty, TTI::TargetCostKind CostKind) {
7218   using namespace llvm::PatternMatch;
7219   // Early exit for no inloop reductions
7220   if (InLoopReductionChains.empty() || VF.isScalar() || !isa<VectorType>(Ty))
7221     return None;
7222   auto *VectorTy = cast<VectorType>(Ty);
7223 
7224   // We are looking for a pattern of, and finding the minimal acceptable cost:
7225   //  reduce(mul(ext(A), ext(B))) or
7226   //  reduce(mul(A, B)) or
7227   //  reduce(ext(A)) or
7228   //  reduce(A).
7229   // The basic idea is that we walk down the tree to do that, finding the root
7230   // reduction instruction in InLoopReductionImmediateChains. From there we find
7231   // the pattern of mul/ext and test the cost of the entire pattern vs the cost
7232   // of the components. If the reduction cost is lower then we return it for the
7233   // reduction instruction and 0 for the other instructions in the pattern. If
7234   // it is not we return an invalid cost specifying the orignal cost method
7235   // should be used.
7236   Instruction *RetI = I;
7237   if (match(RetI, m_ZExtOrSExt(m_Value()))) {
7238     if (!RetI->hasOneUser())
7239       return None;
7240     RetI = RetI->user_back();
7241   }
7242   if (match(RetI, m_Mul(m_Value(), m_Value())) &&
7243       RetI->user_back()->getOpcode() == Instruction::Add) {
7244     if (!RetI->hasOneUser())
7245       return None;
7246     RetI = RetI->user_back();
7247   }
7248 
7249   // Test if the found instruction is a reduction, and if not return an invalid
7250   // cost specifying the parent to use the original cost modelling.
7251   if (!InLoopReductionImmediateChains.count(RetI))
7252     return None;
7253 
7254   // Find the reduction this chain is a part of and calculate the basic cost of
7255   // the reduction on its own.
7256   Instruction *LastChain = InLoopReductionImmediateChains[RetI];
7257   Instruction *ReductionPhi = LastChain;
7258   while (!isa<PHINode>(ReductionPhi))
7259     ReductionPhi = InLoopReductionImmediateChains[ReductionPhi];
7260 
7261   const RecurrenceDescriptor &RdxDesc =
7262       Legal->getReductionVars()[cast<PHINode>(ReductionPhi)];
7263 
7264   InstructionCost BaseCost = TTI.getArithmeticReductionCost(
7265       RdxDesc.getOpcode(), VectorTy, RdxDesc.getFastMathFlags(), CostKind);
7266 
7267   // If we're using ordered reductions then we can just return the base cost
7268   // here, since getArithmeticReductionCost calculates the full ordered
7269   // reduction cost when FP reassociation is not allowed.
7270   if (useOrderedReductions(RdxDesc))
7271     return BaseCost;
7272 
7273   // Get the operand that was not the reduction chain and match it to one of the
7274   // patterns, returning the better cost if it is found.
7275   Instruction *RedOp = RetI->getOperand(1) == LastChain
7276                            ? dyn_cast<Instruction>(RetI->getOperand(0))
7277                            : dyn_cast<Instruction>(RetI->getOperand(1));
7278 
7279   VectorTy = VectorType::get(I->getOperand(0)->getType(), VectorTy);
7280 
7281   Instruction *Op0, *Op1;
7282   if (RedOp &&
7283       match(RedOp,
7284             m_ZExtOrSExt(m_Mul(m_Instruction(Op0), m_Instruction(Op1)))) &&
7285       match(Op0, m_ZExtOrSExt(m_Value())) &&
7286       Op0->getOpcode() == Op1->getOpcode() &&
7287       Op0->getOperand(0)->getType() == Op1->getOperand(0)->getType() &&
7288       !TheLoop->isLoopInvariant(Op0) && !TheLoop->isLoopInvariant(Op1) &&
7289       (Op0->getOpcode() == RedOp->getOpcode() || Op0 == Op1)) {
7290 
7291     // Matched reduce(ext(mul(ext(A), ext(B)))
7292     // Note that the extend opcodes need to all match, or if A==B they will have
7293     // been converted to zext(mul(sext(A), sext(A))) as it is known positive,
7294     // which is equally fine.
7295     bool IsUnsigned = isa<ZExtInst>(Op0);
7296     auto *ExtType = VectorType::get(Op0->getOperand(0)->getType(), VectorTy);
7297     auto *MulType = VectorType::get(Op0->getType(), VectorTy);
7298 
7299     InstructionCost ExtCost =
7300         TTI.getCastInstrCost(Op0->getOpcode(), MulType, ExtType,
7301                              TTI::CastContextHint::None, CostKind, Op0);
7302     InstructionCost MulCost =
7303         TTI.getArithmeticInstrCost(Instruction::Mul, MulType, CostKind);
7304     InstructionCost Ext2Cost =
7305         TTI.getCastInstrCost(RedOp->getOpcode(), VectorTy, MulType,
7306                              TTI::CastContextHint::None, CostKind, RedOp);
7307 
7308     InstructionCost RedCost = TTI.getExtendedAddReductionCost(
7309         /*IsMLA=*/true, IsUnsigned, RdxDesc.getRecurrenceType(), ExtType,
7310         CostKind);
7311 
7312     if (RedCost.isValid() &&
7313         RedCost < ExtCost * 2 + MulCost + Ext2Cost + BaseCost)
7314       return I == RetI ? RedCost : 0;
7315   } else if (RedOp && match(RedOp, m_ZExtOrSExt(m_Value())) &&
7316              !TheLoop->isLoopInvariant(RedOp)) {
7317     // Matched reduce(ext(A))
7318     bool IsUnsigned = isa<ZExtInst>(RedOp);
7319     auto *ExtType = VectorType::get(RedOp->getOperand(0)->getType(), VectorTy);
7320     InstructionCost RedCost = TTI.getExtendedAddReductionCost(
7321         /*IsMLA=*/false, IsUnsigned, RdxDesc.getRecurrenceType(), ExtType,
7322         CostKind);
7323 
7324     InstructionCost ExtCost =
7325         TTI.getCastInstrCost(RedOp->getOpcode(), VectorTy, ExtType,
7326                              TTI::CastContextHint::None, CostKind, RedOp);
7327     if (RedCost.isValid() && RedCost < BaseCost + ExtCost)
7328       return I == RetI ? RedCost : 0;
7329   } else if (RedOp &&
7330              match(RedOp, m_Mul(m_Instruction(Op0), m_Instruction(Op1)))) {
7331     if (match(Op0, m_ZExtOrSExt(m_Value())) &&
7332         Op0->getOpcode() == Op1->getOpcode() &&
7333         Op0->getOperand(0)->getType() == Op1->getOperand(0)->getType() &&
7334         !TheLoop->isLoopInvariant(Op0) && !TheLoop->isLoopInvariant(Op1)) {
7335       bool IsUnsigned = isa<ZExtInst>(Op0);
7336       auto *ExtType = VectorType::get(Op0->getOperand(0)->getType(), VectorTy);
7337       // Matched reduce(mul(ext, ext))
7338       InstructionCost ExtCost =
7339           TTI.getCastInstrCost(Op0->getOpcode(), VectorTy, ExtType,
7340                                TTI::CastContextHint::None, CostKind, Op0);
7341       InstructionCost MulCost =
7342           TTI.getArithmeticInstrCost(Instruction::Mul, VectorTy, CostKind);
7343 
7344       InstructionCost RedCost = TTI.getExtendedAddReductionCost(
7345           /*IsMLA=*/true, IsUnsigned, RdxDesc.getRecurrenceType(), ExtType,
7346           CostKind);
7347 
7348       if (RedCost.isValid() && RedCost < ExtCost * 2 + MulCost + BaseCost)
7349         return I == RetI ? RedCost : 0;
7350     } else if (!match(I, m_ZExtOrSExt(m_Value()))) {
7351       // Matched reduce(mul())
7352       InstructionCost MulCost =
7353           TTI.getArithmeticInstrCost(Instruction::Mul, VectorTy, CostKind);
7354 
7355       InstructionCost RedCost = TTI.getExtendedAddReductionCost(
7356           /*IsMLA=*/true, true, RdxDesc.getRecurrenceType(), VectorTy,
7357           CostKind);
7358 
7359       if (RedCost.isValid() && RedCost < MulCost + BaseCost)
7360         return I == RetI ? RedCost : 0;
7361     }
7362   }
7363 
7364   return I == RetI ? Optional<InstructionCost>(BaseCost) : None;
7365 }
7366 
7367 InstructionCost
7368 LoopVectorizationCostModel::getMemoryInstructionCost(Instruction *I,
7369                                                      ElementCount VF) {
7370   // Calculate scalar cost only. Vectorization cost should be ready at this
7371   // moment.
7372   if (VF.isScalar()) {
7373     Type *ValTy = getLoadStoreType(I);
7374     const Align Alignment = getLoadStoreAlignment(I);
7375     unsigned AS = getLoadStoreAddressSpace(I);
7376 
7377     return TTI.getAddressComputationCost(ValTy) +
7378            TTI.getMemoryOpCost(I->getOpcode(), ValTy, Alignment, AS,
7379                                TTI::TCK_RecipThroughput, I);
7380   }
7381   return getWideningCost(I, VF);
7382 }
7383 
7384 LoopVectorizationCostModel::VectorizationCostTy
7385 LoopVectorizationCostModel::getInstructionCost(Instruction *I,
7386                                                ElementCount VF) {
7387   // If we know that this instruction will remain uniform, check the cost of
7388   // the scalar version.
7389   if (isUniformAfterVectorization(I, VF))
7390     VF = ElementCount::getFixed(1);
7391 
7392   if (VF.isVector() && isProfitableToScalarize(I, VF))
7393     return VectorizationCostTy(InstsToScalarize[VF][I], false);
7394 
7395   // Forced scalars do not have any scalarization overhead.
7396   auto ForcedScalar = ForcedScalars.find(VF);
7397   if (VF.isVector() && ForcedScalar != ForcedScalars.end()) {
7398     auto InstSet = ForcedScalar->second;
7399     if (InstSet.count(I))
7400       return VectorizationCostTy(
7401           (getInstructionCost(I, ElementCount::getFixed(1)).first *
7402            VF.getKnownMinValue()),
7403           false);
7404   }
7405 
7406   Type *VectorTy;
7407   InstructionCost C = getInstructionCost(I, VF, VectorTy);
7408 
7409   bool TypeNotScalarized =
7410       VF.isVector() && VectorTy->isVectorTy() &&
7411       TTI.getNumberOfParts(VectorTy) < VF.getKnownMinValue();
7412   return VectorizationCostTy(C, TypeNotScalarized);
7413 }
7414 
7415 InstructionCost
7416 LoopVectorizationCostModel::getScalarizationOverhead(Instruction *I,
7417                                                      ElementCount VF) const {
7418 
7419   // There is no mechanism yet to create a scalable scalarization loop,
7420   // so this is currently Invalid.
7421   if (VF.isScalable())
7422     return InstructionCost::getInvalid();
7423 
7424   if (VF.isScalar())
7425     return 0;
7426 
7427   InstructionCost Cost = 0;
7428   Type *RetTy = ToVectorTy(I->getType(), VF);
7429   if (!RetTy->isVoidTy() &&
7430       (!isa<LoadInst>(I) || !TTI.supportsEfficientVectorElementLoadStore()))
7431     Cost += TTI.getScalarizationOverhead(
7432         cast<VectorType>(RetTy), APInt::getAllOnes(VF.getKnownMinValue()), true,
7433         false);
7434 
7435   // Some targets keep addresses scalar.
7436   if (isa<LoadInst>(I) && !TTI.prefersVectorizedAddressing())
7437     return Cost;
7438 
7439   // Some targets support efficient element stores.
7440   if (isa<StoreInst>(I) && TTI.supportsEfficientVectorElementLoadStore())
7441     return Cost;
7442 
7443   // Collect operands to consider.
7444   CallInst *CI = dyn_cast<CallInst>(I);
7445   Instruction::op_range Ops = CI ? CI->args() : I->operands();
7446 
7447   // Skip operands that do not require extraction/scalarization and do not incur
7448   // any overhead.
7449   SmallVector<Type *> Tys;
7450   for (auto *V : filterExtractingOperands(Ops, VF))
7451     Tys.push_back(MaybeVectorizeType(V->getType(), VF));
7452   return Cost + TTI.getOperandsScalarizationOverhead(
7453                     filterExtractingOperands(Ops, VF), Tys);
7454 }
7455 
7456 void LoopVectorizationCostModel::setCostBasedWideningDecision(ElementCount VF) {
7457   if (VF.isScalar())
7458     return;
7459   NumPredStores = 0;
7460   for (BasicBlock *BB : TheLoop->blocks()) {
7461     // For each instruction in the old loop.
7462     for (Instruction &I : *BB) {
7463       Value *Ptr =  getLoadStorePointerOperand(&I);
7464       if (!Ptr)
7465         continue;
7466 
7467       // TODO: We should generate better code and update the cost model for
7468       // predicated uniform stores. Today they are treated as any other
7469       // predicated store (see added test cases in
7470       // invariant-store-vectorization.ll).
7471       if (isa<StoreInst>(&I) && isScalarWithPredication(&I))
7472         NumPredStores++;
7473 
7474       if (Legal->isUniformMemOp(I)) {
7475         // TODO: Avoid replicating loads and stores instead of
7476         // relying on instcombine to remove them.
7477         // Load: Scalar load + broadcast
7478         // Store: Scalar store + isLoopInvariantStoreValue ? 0 : extract
7479         InstructionCost Cost;
7480         if (isa<StoreInst>(&I) && VF.isScalable() &&
7481             isLegalGatherOrScatter(&I)) {
7482           Cost = getGatherScatterCost(&I, VF);
7483           setWideningDecision(&I, VF, CM_GatherScatter, Cost);
7484         } else {
7485           assert((isa<LoadInst>(&I) || !VF.isScalable()) &&
7486                  "Cannot yet scalarize uniform stores");
7487           Cost = getUniformMemOpCost(&I, VF);
7488           setWideningDecision(&I, VF, CM_Scalarize, Cost);
7489         }
7490         continue;
7491       }
7492 
7493       // We assume that widening is the best solution when possible.
7494       if (memoryInstructionCanBeWidened(&I, VF)) {
7495         InstructionCost Cost = getConsecutiveMemOpCost(&I, VF);
7496         int ConsecutiveStride = Legal->isConsecutivePtr(
7497             getLoadStoreType(&I), getLoadStorePointerOperand(&I));
7498         assert((ConsecutiveStride == 1 || ConsecutiveStride == -1) &&
7499                "Expected consecutive stride.");
7500         InstWidening Decision =
7501             ConsecutiveStride == 1 ? CM_Widen : CM_Widen_Reverse;
7502         setWideningDecision(&I, VF, Decision, Cost);
7503         continue;
7504       }
7505 
7506       // Choose between Interleaving, Gather/Scatter or Scalarization.
7507       InstructionCost InterleaveCost = InstructionCost::getInvalid();
7508       unsigned NumAccesses = 1;
7509       if (isAccessInterleaved(&I)) {
7510         auto Group = getInterleavedAccessGroup(&I);
7511         assert(Group && "Fail to get an interleaved access group.");
7512 
7513         // Make one decision for the whole group.
7514         if (getWideningDecision(&I, VF) != CM_Unknown)
7515           continue;
7516 
7517         NumAccesses = Group->getNumMembers();
7518         if (interleavedAccessCanBeWidened(&I, VF))
7519           InterleaveCost = getInterleaveGroupCost(&I, VF);
7520       }
7521 
7522       InstructionCost GatherScatterCost =
7523           isLegalGatherOrScatter(&I)
7524               ? getGatherScatterCost(&I, VF) * NumAccesses
7525               : InstructionCost::getInvalid();
7526 
7527       InstructionCost ScalarizationCost =
7528           getMemInstScalarizationCost(&I, VF) * NumAccesses;
7529 
7530       // Choose better solution for the current VF,
7531       // write down this decision and use it during vectorization.
7532       InstructionCost Cost;
7533       InstWidening Decision;
7534       if (InterleaveCost <= GatherScatterCost &&
7535           InterleaveCost < ScalarizationCost) {
7536         Decision = CM_Interleave;
7537         Cost = InterleaveCost;
7538       } else if (GatherScatterCost < ScalarizationCost) {
7539         Decision = CM_GatherScatter;
7540         Cost = GatherScatterCost;
7541       } else {
7542         Decision = CM_Scalarize;
7543         Cost = ScalarizationCost;
7544       }
7545       // If the instructions belongs to an interleave group, the whole group
7546       // receives the same decision. The whole group receives the cost, but
7547       // the cost will actually be assigned to one instruction.
7548       if (auto Group = getInterleavedAccessGroup(&I))
7549         setWideningDecision(Group, VF, Decision, Cost);
7550       else
7551         setWideningDecision(&I, VF, Decision, Cost);
7552     }
7553   }
7554 
7555   // Make sure that any load of address and any other address computation
7556   // remains scalar unless there is gather/scatter support. This avoids
7557   // inevitable extracts into address registers, and also has the benefit of
7558   // activating LSR more, since that pass can't optimize vectorized
7559   // addresses.
7560   if (TTI.prefersVectorizedAddressing())
7561     return;
7562 
7563   // Start with all scalar pointer uses.
7564   SmallPtrSet<Instruction *, 8> AddrDefs;
7565   for (BasicBlock *BB : TheLoop->blocks())
7566     for (Instruction &I : *BB) {
7567       Instruction *PtrDef =
7568         dyn_cast_or_null<Instruction>(getLoadStorePointerOperand(&I));
7569       if (PtrDef && TheLoop->contains(PtrDef) &&
7570           getWideningDecision(&I, VF) != CM_GatherScatter)
7571         AddrDefs.insert(PtrDef);
7572     }
7573 
7574   // Add all instructions used to generate the addresses.
7575   SmallVector<Instruction *, 4> Worklist;
7576   append_range(Worklist, AddrDefs);
7577   while (!Worklist.empty()) {
7578     Instruction *I = Worklist.pop_back_val();
7579     for (auto &Op : I->operands())
7580       if (auto *InstOp = dyn_cast<Instruction>(Op))
7581         if ((InstOp->getParent() == I->getParent()) && !isa<PHINode>(InstOp) &&
7582             AddrDefs.insert(InstOp).second)
7583           Worklist.push_back(InstOp);
7584   }
7585 
7586   for (auto *I : AddrDefs) {
7587     if (isa<LoadInst>(I)) {
7588       // Setting the desired widening decision should ideally be handled in
7589       // by cost functions, but since this involves the task of finding out
7590       // if the loaded register is involved in an address computation, it is
7591       // instead changed here when we know this is the case.
7592       InstWidening Decision = getWideningDecision(I, VF);
7593       if (Decision == CM_Widen || Decision == CM_Widen_Reverse)
7594         // Scalarize a widened load of address.
7595         setWideningDecision(
7596             I, VF, CM_Scalarize,
7597             (VF.getKnownMinValue() *
7598              getMemoryInstructionCost(I, ElementCount::getFixed(1))));
7599       else if (auto Group = getInterleavedAccessGroup(I)) {
7600         // Scalarize an interleave group of address loads.
7601         for (unsigned I = 0; I < Group->getFactor(); ++I) {
7602           if (Instruction *Member = Group->getMember(I))
7603             setWideningDecision(
7604                 Member, VF, CM_Scalarize,
7605                 (VF.getKnownMinValue() *
7606                  getMemoryInstructionCost(Member, ElementCount::getFixed(1))));
7607         }
7608       }
7609     } else
7610       // Make sure I gets scalarized and a cost estimate without
7611       // scalarization overhead.
7612       ForcedScalars[VF].insert(I);
7613   }
7614 }
7615 
7616 InstructionCost
7617 LoopVectorizationCostModel::getInstructionCost(Instruction *I, ElementCount VF,
7618                                                Type *&VectorTy) {
7619   Type *RetTy = I->getType();
7620   if (canTruncateToMinimalBitwidth(I, VF))
7621     RetTy = IntegerType::get(RetTy->getContext(), MinBWs[I]);
7622   auto SE = PSE.getSE();
7623   TTI::TargetCostKind CostKind = TTI::TCK_RecipThroughput;
7624 
7625   auto hasSingleCopyAfterVectorization = [this](Instruction *I,
7626                                                 ElementCount VF) -> bool {
7627     if (VF.isScalar())
7628       return true;
7629 
7630     auto Scalarized = InstsToScalarize.find(VF);
7631     assert(Scalarized != InstsToScalarize.end() &&
7632            "VF not yet analyzed for scalarization profitability");
7633     return !Scalarized->second.count(I) &&
7634            llvm::all_of(I->users(), [&](User *U) {
7635              auto *UI = cast<Instruction>(U);
7636              return !Scalarized->second.count(UI);
7637            });
7638   };
7639   (void) hasSingleCopyAfterVectorization;
7640 
7641   if (isScalarAfterVectorization(I, VF)) {
7642     // With the exception of GEPs and PHIs, after scalarization there should
7643     // only be one copy of the instruction generated in the loop. This is
7644     // because the VF is either 1, or any instructions that need scalarizing
7645     // have already been dealt with by the the time we get here. As a result,
7646     // it means we don't have to multiply the instruction cost by VF.
7647     assert(I->getOpcode() == Instruction::GetElementPtr ||
7648            I->getOpcode() == Instruction::PHI ||
7649            (I->getOpcode() == Instruction::BitCast &&
7650             I->getType()->isPointerTy()) ||
7651            hasSingleCopyAfterVectorization(I, VF));
7652     VectorTy = RetTy;
7653   } else
7654     VectorTy = ToVectorTy(RetTy, VF);
7655 
7656   // TODO: We need to estimate the cost of intrinsic calls.
7657   switch (I->getOpcode()) {
7658   case Instruction::GetElementPtr:
7659     // We mark this instruction as zero-cost because the cost of GEPs in
7660     // vectorized code depends on whether the corresponding memory instruction
7661     // is scalarized or not. Therefore, we handle GEPs with the memory
7662     // instruction cost.
7663     return 0;
7664   case Instruction::Br: {
7665     // In cases of scalarized and predicated instructions, there will be VF
7666     // predicated blocks in the vectorized loop. Each branch around these
7667     // blocks requires also an extract of its vector compare i1 element.
7668     bool ScalarPredicatedBB = false;
7669     BranchInst *BI = cast<BranchInst>(I);
7670     if (VF.isVector() && BI->isConditional() &&
7671         (PredicatedBBsAfterVectorization.count(BI->getSuccessor(0)) ||
7672          PredicatedBBsAfterVectorization.count(BI->getSuccessor(1))))
7673       ScalarPredicatedBB = true;
7674 
7675     if (ScalarPredicatedBB) {
7676       // Not possible to scalarize scalable vector with predicated instructions.
7677       if (VF.isScalable())
7678         return InstructionCost::getInvalid();
7679       // Return cost for branches around scalarized and predicated blocks.
7680       auto *Vec_i1Ty =
7681           VectorType::get(IntegerType::getInt1Ty(RetTy->getContext()), VF);
7682       return (
7683           TTI.getScalarizationOverhead(
7684               Vec_i1Ty, APInt::getAllOnes(VF.getFixedValue()), false, true) +
7685           (TTI.getCFInstrCost(Instruction::Br, CostKind) * VF.getFixedValue()));
7686     } else if (I->getParent() == TheLoop->getLoopLatch() || VF.isScalar())
7687       // The back-edge branch will remain, as will all scalar branches.
7688       return TTI.getCFInstrCost(Instruction::Br, CostKind);
7689     else
7690       // This branch will be eliminated by if-conversion.
7691       return 0;
7692     // Note: We currently assume zero cost for an unconditional branch inside
7693     // a predicated block since it will become a fall-through, although we
7694     // may decide in the future to call TTI for all branches.
7695   }
7696   case Instruction::PHI: {
7697     auto *Phi = cast<PHINode>(I);
7698 
7699     // First-order recurrences are replaced by vector shuffles inside the loop.
7700     // NOTE: Don't use ToVectorTy as SK_ExtractSubvector expects a vector type.
7701     if (VF.isVector() && Legal->isFirstOrderRecurrence(Phi))
7702       return TTI.getShuffleCost(
7703           TargetTransformInfo::SK_ExtractSubvector, cast<VectorType>(VectorTy),
7704           None, VF.getKnownMinValue() - 1, FixedVectorType::get(RetTy, 1));
7705 
7706     // Phi nodes in non-header blocks (not inductions, reductions, etc.) are
7707     // converted into select instructions. We require N - 1 selects per phi
7708     // node, where N is the number of incoming values.
7709     if (VF.isVector() && Phi->getParent() != TheLoop->getHeader())
7710       return (Phi->getNumIncomingValues() - 1) *
7711              TTI.getCmpSelInstrCost(
7712                  Instruction::Select, ToVectorTy(Phi->getType(), VF),
7713                  ToVectorTy(Type::getInt1Ty(Phi->getContext()), VF),
7714                  CmpInst::BAD_ICMP_PREDICATE, CostKind);
7715 
7716     return TTI.getCFInstrCost(Instruction::PHI, CostKind);
7717   }
7718   case Instruction::UDiv:
7719   case Instruction::SDiv:
7720   case Instruction::URem:
7721   case Instruction::SRem:
7722     // If we have a predicated instruction, it may not be executed for each
7723     // vector lane. Get the scalarization cost and scale this amount by the
7724     // probability of executing the predicated block. If the instruction is not
7725     // predicated, we fall through to the next case.
7726     if (VF.isVector() && isScalarWithPredication(I)) {
7727       InstructionCost Cost = 0;
7728 
7729       // These instructions have a non-void type, so account for the phi nodes
7730       // that we will create. This cost is likely to be zero. The phi node
7731       // cost, if any, should be scaled by the block probability because it
7732       // models a copy at the end of each predicated block.
7733       Cost += VF.getKnownMinValue() *
7734               TTI.getCFInstrCost(Instruction::PHI, CostKind);
7735 
7736       // The cost of the non-predicated instruction.
7737       Cost += VF.getKnownMinValue() *
7738               TTI.getArithmeticInstrCost(I->getOpcode(), RetTy, CostKind);
7739 
7740       // The cost of insertelement and extractelement instructions needed for
7741       // scalarization.
7742       Cost += getScalarizationOverhead(I, VF);
7743 
7744       // Scale the cost by the probability of executing the predicated blocks.
7745       // This assumes the predicated block for each vector lane is equally
7746       // likely.
7747       return Cost / getReciprocalPredBlockProb();
7748     }
7749     LLVM_FALLTHROUGH;
7750   case Instruction::Add:
7751   case Instruction::FAdd:
7752   case Instruction::Sub:
7753   case Instruction::FSub:
7754   case Instruction::Mul:
7755   case Instruction::FMul:
7756   case Instruction::FDiv:
7757   case Instruction::FRem:
7758   case Instruction::Shl:
7759   case Instruction::LShr:
7760   case Instruction::AShr:
7761   case Instruction::And:
7762   case Instruction::Or:
7763   case Instruction::Xor: {
7764     // Since we will replace the stride by 1 the multiplication should go away.
7765     if (I->getOpcode() == Instruction::Mul && isStrideMul(I, Legal))
7766       return 0;
7767 
7768     // Detect reduction patterns
7769     if (auto RedCost = getReductionPatternCost(I, VF, VectorTy, CostKind))
7770       return *RedCost;
7771 
7772     // Certain instructions can be cheaper to vectorize if they have a constant
7773     // second vector operand. One example of this are shifts on x86.
7774     Value *Op2 = I->getOperand(1);
7775     TargetTransformInfo::OperandValueProperties Op2VP;
7776     TargetTransformInfo::OperandValueKind Op2VK =
7777         TTI.getOperandInfo(Op2, Op2VP);
7778     if (Op2VK == TargetTransformInfo::OK_AnyValue && Legal->isUniform(Op2))
7779       Op2VK = TargetTransformInfo::OK_UniformValue;
7780 
7781     SmallVector<const Value *, 4> Operands(I->operand_values());
7782     return TTI.getArithmeticInstrCost(
7783         I->getOpcode(), VectorTy, CostKind, TargetTransformInfo::OK_AnyValue,
7784         Op2VK, TargetTransformInfo::OP_None, Op2VP, Operands, I);
7785   }
7786   case Instruction::FNeg: {
7787     return TTI.getArithmeticInstrCost(
7788         I->getOpcode(), VectorTy, CostKind, TargetTransformInfo::OK_AnyValue,
7789         TargetTransformInfo::OK_AnyValue, TargetTransformInfo::OP_None,
7790         TargetTransformInfo::OP_None, I->getOperand(0), I);
7791   }
7792   case Instruction::Select: {
7793     SelectInst *SI = cast<SelectInst>(I);
7794     const SCEV *CondSCEV = SE->getSCEV(SI->getCondition());
7795     bool ScalarCond = (SE->isLoopInvariant(CondSCEV, TheLoop));
7796 
7797     const Value *Op0, *Op1;
7798     using namespace llvm::PatternMatch;
7799     if (!ScalarCond && (match(I, m_LogicalAnd(m_Value(Op0), m_Value(Op1))) ||
7800                         match(I, m_LogicalOr(m_Value(Op0), m_Value(Op1))))) {
7801       // select x, y, false --> x & y
7802       // select x, true, y --> x | y
7803       TTI::OperandValueProperties Op1VP = TTI::OP_None;
7804       TTI::OperandValueProperties Op2VP = TTI::OP_None;
7805       TTI::OperandValueKind Op1VK = TTI::getOperandInfo(Op0, Op1VP);
7806       TTI::OperandValueKind Op2VK = TTI::getOperandInfo(Op1, Op2VP);
7807       assert(Op0->getType()->getScalarSizeInBits() == 1 &&
7808               Op1->getType()->getScalarSizeInBits() == 1);
7809 
7810       SmallVector<const Value *, 2> Operands{Op0, Op1};
7811       return TTI.getArithmeticInstrCost(
7812           match(I, m_LogicalOr()) ? Instruction::Or : Instruction::And, VectorTy,
7813           CostKind, Op1VK, Op2VK, Op1VP, Op2VP, Operands, I);
7814     }
7815 
7816     Type *CondTy = SI->getCondition()->getType();
7817     if (!ScalarCond)
7818       CondTy = VectorType::get(CondTy, VF);
7819     return TTI.getCmpSelInstrCost(I->getOpcode(), VectorTy, CondTy,
7820                                   CmpInst::BAD_ICMP_PREDICATE, CostKind, I);
7821   }
7822   case Instruction::ICmp:
7823   case Instruction::FCmp: {
7824     Type *ValTy = I->getOperand(0)->getType();
7825     Instruction *Op0AsInstruction = dyn_cast<Instruction>(I->getOperand(0));
7826     if (canTruncateToMinimalBitwidth(Op0AsInstruction, VF))
7827       ValTy = IntegerType::get(ValTy->getContext(), MinBWs[Op0AsInstruction]);
7828     VectorTy = ToVectorTy(ValTy, VF);
7829     return TTI.getCmpSelInstrCost(I->getOpcode(), VectorTy, nullptr,
7830                                   CmpInst::BAD_ICMP_PREDICATE, CostKind, I);
7831   }
7832   case Instruction::Store:
7833   case Instruction::Load: {
7834     ElementCount Width = VF;
7835     if (Width.isVector()) {
7836       InstWidening Decision = getWideningDecision(I, Width);
7837       assert(Decision != CM_Unknown &&
7838              "CM decision should be taken at this point");
7839       if (Decision == CM_Scalarize)
7840         Width = ElementCount::getFixed(1);
7841     }
7842     VectorTy = ToVectorTy(getLoadStoreType(I), Width);
7843     return getMemoryInstructionCost(I, VF);
7844   }
7845   case Instruction::BitCast:
7846     if (I->getType()->isPointerTy())
7847       return 0;
7848     LLVM_FALLTHROUGH;
7849   case Instruction::ZExt:
7850   case Instruction::SExt:
7851   case Instruction::FPToUI:
7852   case Instruction::FPToSI:
7853   case Instruction::FPExt:
7854   case Instruction::PtrToInt:
7855   case Instruction::IntToPtr:
7856   case Instruction::SIToFP:
7857   case Instruction::UIToFP:
7858   case Instruction::Trunc:
7859   case Instruction::FPTrunc: {
7860     // Computes the CastContextHint from a Load/Store instruction.
7861     auto ComputeCCH = [&](Instruction *I) -> TTI::CastContextHint {
7862       assert((isa<LoadInst>(I) || isa<StoreInst>(I)) &&
7863              "Expected a load or a store!");
7864 
7865       if (VF.isScalar() || !TheLoop->contains(I))
7866         return TTI::CastContextHint::Normal;
7867 
7868       switch (getWideningDecision(I, VF)) {
7869       case LoopVectorizationCostModel::CM_GatherScatter:
7870         return TTI::CastContextHint::GatherScatter;
7871       case LoopVectorizationCostModel::CM_Interleave:
7872         return TTI::CastContextHint::Interleave;
7873       case LoopVectorizationCostModel::CM_Scalarize:
7874       case LoopVectorizationCostModel::CM_Widen:
7875         return Legal->isMaskRequired(I) ? TTI::CastContextHint::Masked
7876                                         : TTI::CastContextHint::Normal;
7877       case LoopVectorizationCostModel::CM_Widen_Reverse:
7878         return TTI::CastContextHint::Reversed;
7879       case LoopVectorizationCostModel::CM_Unknown:
7880         llvm_unreachable("Instr did not go through cost modelling?");
7881       }
7882 
7883       llvm_unreachable("Unhandled case!");
7884     };
7885 
7886     unsigned Opcode = I->getOpcode();
7887     TTI::CastContextHint CCH = TTI::CastContextHint::None;
7888     // For Trunc, the context is the only user, which must be a StoreInst.
7889     if (Opcode == Instruction::Trunc || Opcode == Instruction::FPTrunc) {
7890       if (I->hasOneUse())
7891         if (StoreInst *Store = dyn_cast<StoreInst>(*I->user_begin()))
7892           CCH = ComputeCCH(Store);
7893     }
7894     // For Z/Sext, the context is the operand, which must be a LoadInst.
7895     else if (Opcode == Instruction::ZExt || Opcode == Instruction::SExt ||
7896              Opcode == Instruction::FPExt) {
7897       if (LoadInst *Load = dyn_cast<LoadInst>(I->getOperand(0)))
7898         CCH = ComputeCCH(Load);
7899     }
7900 
7901     // We optimize the truncation of induction variables having constant
7902     // integer steps. The cost of these truncations is the same as the scalar
7903     // operation.
7904     if (isOptimizableIVTruncate(I, VF)) {
7905       auto *Trunc = cast<TruncInst>(I);
7906       return TTI.getCastInstrCost(Instruction::Trunc, Trunc->getDestTy(),
7907                                   Trunc->getSrcTy(), CCH, CostKind, Trunc);
7908     }
7909 
7910     // Detect reduction patterns
7911     if (auto RedCost = getReductionPatternCost(I, VF, VectorTy, CostKind))
7912       return *RedCost;
7913 
7914     Type *SrcScalarTy = I->getOperand(0)->getType();
7915     Type *SrcVecTy =
7916         VectorTy->isVectorTy() ? ToVectorTy(SrcScalarTy, VF) : SrcScalarTy;
7917     if (canTruncateToMinimalBitwidth(I, VF)) {
7918       // This cast is going to be shrunk. This may remove the cast or it might
7919       // turn it into slightly different cast. For example, if MinBW == 16,
7920       // "zext i8 %1 to i32" becomes "zext i8 %1 to i16".
7921       //
7922       // Calculate the modified src and dest types.
7923       Type *MinVecTy = VectorTy;
7924       if (Opcode == Instruction::Trunc) {
7925         SrcVecTy = smallestIntegerVectorType(SrcVecTy, MinVecTy);
7926         VectorTy =
7927             largestIntegerVectorType(ToVectorTy(I->getType(), VF), MinVecTy);
7928       } else if (Opcode == Instruction::ZExt || Opcode == Instruction::SExt) {
7929         SrcVecTy = largestIntegerVectorType(SrcVecTy, MinVecTy);
7930         VectorTy =
7931             smallestIntegerVectorType(ToVectorTy(I->getType(), VF), MinVecTy);
7932       }
7933     }
7934 
7935     return TTI.getCastInstrCost(Opcode, VectorTy, SrcVecTy, CCH, CostKind, I);
7936   }
7937   case Instruction::Call: {
7938     bool NeedToScalarize;
7939     CallInst *CI = cast<CallInst>(I);
7940     InstructionCost CallCost = getVectorCallCost(CI, VF, NeedToScalarize);
7941     if (getVectorIntrinsicIDForCall(CI, TLI)) {
7942       InstructionCost IntrinsicCost = getVectorIntrinsicCost(CI, VF);
7943       return std::min(CallCost, IntrinsicCost);
7944     }
7945     return CallCost;
7946   }
7947   case Instruction::ExtractValue:
7948     return TTI.getInstructionCost(I, TTI::TCK_RecipThroughput);
7949   case Instruction::Alloca:
7950     // We cannot easily widen alloca to a scalable alloca, as
7951     // the result would need to be a vector of pointers.
7952     if (VF.isScalable())
7953       return InstructionCost::getInvalid();
7954     LLVM_FALLTHROUGH;
7955   default:
7956     // This opcode is unknown. Assume that it is the same as 'mul'.
7957     return TTI.getArithmeticInstrCost(Instruction::Mul, VectorTy, CostKind);
7958   } // end of switch.
7959 }
7960 
7961 char LoopVectorize::ID = 0;
7962 
7963 static const char lv_name[] = "Loop Vectorization";
7964 
7965 INITIALIZE_PASS_BEGIN(LoopVectorize, LV_NAME, lv_name, false, false)
7966 INITIALIZE_PASS_DEPENDENCY(TargetTransformInfoWrapperPass)
7967 INITIALIZE_PASS_DEPENDENCY(BasicAAWrapperPass)
7968 INITIALIZE_PASS_DEPENDENCY(AAResultsWrapperPass)
7969 INITIALIZE_PASS_DEPENDENCY(GlobalsAAWrapperPass)
7970 INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker)
7971 INITIALIZE_PASS_DEPENDENCY(BlockFrequencyInfoWrapperPass)
7972 INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
7973 INITIALIZE_PASS_DEPENDENCY(ScalarEvolutionWrapperPass)
7974 INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass)
7975 INITIALIZE_PASS_DEPENDENCY(LoopAccessLegacyAnalysis)
7976 INITIALIZE_PASS_DEPENDENCY(DemandedBitsWrapperPass)
7977 INITIALIZE_PASS_DEPENDENCY(OptimizationRemarkEmitterWrapperPass)
7978 INITIALIZE_PASS_DEPENDENCY(ProfileSummaryInfoWrapperPass)
7979 INITIALIZE_PASS_DEPENDENCY(InjectTLIMappingsLegacy)
7980 INITIALIZE_PASS_END(LoopVectorize, LV_NAME, lv_name, false, false)
7981 
7982 namespace llvm {
7983 
7984 Pass *createLoopVectorizePass() { return new LoopVectorize(); }
7985 
7986 Pass *createLoopVectorizePass(bool InterleaveOnlyWhenForced,
7987                               bool VectorizeOnlyWhenForced) {
7988   return new LoopVectorize(InterleaveOnlyWhenForced, VectorizeOnlyWhenForced);
7989 }
7990 
7991 } // end namespace llvm
7992 
7993 bool LoopVectorizationCostModel::isConsecutiveLoadOrStore(Instruction *Inst) {
7994   // Check if the pointer operand of a load or store instruction is
7995   // consecutive.
7996   if (auto *Ptr = getLoadStorePointerOperand(Inst))
7997     return Legal->isConsecutivePtr(getLoadStoreType(Inst), Ptr);
7998   return false;
7999 }
8000 
8001 void LoopVectorizationCostModel::collectValuesToIgnore() {
8002   // Ignore ephemeral values.
8003   CodeMetrics::collectEphemeralValues(TheLoop, AC, ValuesToIgnore);
8004 
8005   // Ignore type-promoting instructions we identified during reduction
8006   // detection.
8007   for (auto &Reduction : Legal->getReductionVars()) {
8008     RecurrenceDescriptor &RedDes = Reduction.second;
8009     const SmallPtrSetImpl<Instruction *> &Casts = RedDes.getCastInsts();
8010     VecValuesToIgnore.insert(Casts.begin(), Casts.end());
8011   }
8012   // Ignore type-casting instructions we identified during induction
8013   // detection.
8014   for (auto &Induction : Legal->getInductionVars()) {
8015     InductionDescriptor &IndDes = Induction.second;
8016     const SmallVectorImpl<Instruction *> &Casts = IndDes.getCastInsts();
8017     VecValuesToIgnore.insert(Casts.begin(), Casts.end());
8018   }
8019 }
8020 
8021 void LoopVectorizationCostModel::collectInLoopReductions() {
8022   for (auto &Reduction : Legal->getReductionVars()) {
8023     PHINode *Phi = Reduction.first;
8024     RecurrenceDescriptor &RdxDesc = Reduction.second;
8025 
8026     // We don't collect reductions that are type promoted (yet).
8027     if (RdxDesc.getRecurrenceType() != Phi->getType())
8028       continue;
8029 
8030     // If the target would prefer this reduction to happen "in-loop", then we
8031     // want to record it as such.
8032     unsigned Opcode = RdxDesc.getOpcode();
8033     if (!PreferInLoopReductions && !useOrderedReductions(RdxDesc) &&
8034         !TTI.preferInLoopReduction(Opcode, Phi->getType(),
8035                                    TargetTransformInfo::ReductionFlags()))
8036       continue;
8037 
8038     // Check that we can correctly put the reductions into the loop, by
8039     // finding the chain of operations that leads from the phi to the loop
8040     // exit value.
8041     SmallVector<Instruction *, 4> ReductionOperations =
8042         RdxDesc.getReductionOpChain(Phi, TheLoop);
8043     bool InLoop = !ReductionOperations.empty();
8044     if (InLoop) {
8045       InLoopReductionChains[Phi] = ReductionOperations;
8046       // Add the elements to InLoopReductionImmediateChains for cost modelling.
8047       Instruction *LastChain = Phi;
8048       for (auto *I : ReductionOperations) {
8049         InLoopReductionImmediateChains[I] = LastChain;
8050         LastChain = I;
8051       }
8052     }
8053     LLVM_DEBUG(dbgs() << "LV: Using " << (InLoop ? "inloop" : "out of loop")
8054                       << " reduction for phi: " << *Phi << "\n");
8055   }
8056 }
8057 
8058 // TODO: we could return a pair of values that specify the max VF and
8059 // min VF, to be used in `buildVPlans(MinVF, MaxVF)` instead of
8060 // `buildVPlans(VF, VF)`. We cannot do it because VPLAN at the moment
8061 // doesn't have a cost model that can choose which plan to execute if
8062 // more than one is generated.
8063 static unsigned determineVPlanVF(const unsigned WidestVectorRegBits,
8064                                  LoopVectorizationCostModel &CM) {
8065   unsigned WidestType;
8066   std::tie(std::ignore, WidestType) = CM.getSmallestAndWidestTypes();
8067   return WidestVectorRegBits / WidestType;
8068 }
8069 
8070 VectorizationFactor
8071 LoopVectorizationPlanner::planInVPlanNativePath(ElementCount UserVF) {
8072   assert(!UserVF.isScalable() && "scalable vectors not yet supported");
8073   ElementCount VF = UserVF;
8074   // Outer loop handling: They may require CFG and instruction level
8075   // transformations before even evaluating whether vectorization is profitable.
8076   // Since we cannot modify the incoming IR, we need to build VPlan upfront in
8077   // the vectorization pipeline.
8078   if (!OrigLoop->isInnermost()) {
8079     // If the user doesn't provide a vectorization factor, determine a
8080     // reasonable one.
8081     if (UserVF.isZero()) {
8082       VF = ElementCount::getFixed(determineVPlanVF(
8083           TTI->getRegisterBitWidth(TargetTransformInfo::RGK_FixedWidthVector)
8084               .getFixedSize(),
8085           CM));
8086       LLVM_DEBUG(dbgs() << "LV: VPlan computed VF " << VF << ".\n");
8087 
8088       // Make sure we have a VF > 1 for stress testing.
8089       if (VPlanBuildStressTest && (VF.isScalar() || VF.isZero())) {
8090         LLVM_DEBUG(dbgs() << "LV: VPlan stress testing: "
8091                           << "overriding computed VF.\n");
8092         VF = ElementCount::getFixed(4);
8093       }
8094     }
8095     assert(EnableVPlanNativePath && "VPlan-native path is not enabled.");
8096     assert(isPowerOf2_32(VF.getKnownMinValue()) &&
8097            "VF needs to be a power of two");
8098     LLVM_DEBUG(dbgs() << "LV: Using " << (!UserVF.isZero() ? "user " : "")
8099                       << "VF " << VF << " to build VPlans.\n");
8100     buildVPlans(VF, VF);
8101 
8102     // For VPlan build stress testing, we bail out after VPlan construction.
8103     if (VPlanBuildStressTest)
8104       return VectorizationFactor::Disabled();
8105 
8106     return {VF, 0 /*Cost*/};
8107   }
8108 
8109   LLVM_DEBUG(
8110       dbgs() << "LV: Not vectorizing. Inner loops aren't supported in the "
8111                 "VPlan-native path.\n");
8112   return VectorizationFactor::Disabled();
8113 }
8114 
8115 Optional<VectorizationFactor>
8116 LoopVectorizationPlanner::plan(ElementCount UserVF, unsigned UserIC) {
8117   assert(OrigLoop->isInnermost() && "Inner loop expected.");
8118   FixedScalableVFPair MaxFactors = CM.computeMaxVF(UserVF, UserIC);
8119   if (!MaxFactors) // Cases that should not to be vectorized nor interleaved.
8120     return None;
8121 
8122   // Invalidate interleave groups if all blocks of loop will be predicated.
8123   if (CM.blockNeedsPredication(OrigLoop->getHeader()) &&
8124       !useMaskedInterleavedAccesses(*TTI)) {
8125     LLVM_DEBUG(
8126         dbgs()
8127         << "LV: Invalidate all interleaved groups due to fold-tail by masking "
8128            "which requires masked-interleaved support.\n");
8129     if (CM.InterleaveInfo.invalidateGroups())
8130       // Invalidating interleave groups also requires invalidating all decisions
8131       // based on them, which includes widening decisions and uniform and scalar
8132       // values.
8133       CM.invalidateCostModelingDecisions();
8134   }
8135 
8136   ElementCount MaxUserVF =
8137       UserVF.isScalable() ? MaxFactors.ScalableVF : MaxFactors.FixedVF;
8138   bool UserVFIsLegal = ElementCount::isKnownLE(UserVF, MaxUserVF);
8139   if (!UserVF.isZero() && UserVFIsLegal) {
8140     assert(isPowerOf2_32(UserVF.getKnownMinValue()) &&
8141            "VF needs to be a power of two");
8142     // Collect the instructions (and their associated costs) that will be more
8143     // profitable to scalarize.
8144     if (CM.selectUserVectorizationFactor(UserVF)) {
8145       LLVM_DEBUG(dbgs() << "LV: Using user VF " << UserVF << ".\n");
8146       CM.collectInLoopReductions();
8147       buildVPlansWithVPRecipes(UserVF, UserVF);
8148       LLVM_DEBUG(printPlans(dbgs()));
8149       return {{UserVF, 0}};
8150     } else
8151       reportVectorizationInfo("UserVF ignored because of invalid costs.",
8152                               "InvalidCost", ORE, OrigLoop);
8153   }
8154 
8155   // Populate the set of Vectorization Factor Candidates.
8156   ElementCountSet VFCandidates;
8157   for (auto VF = ElementCount::getFixed(1);
8158        ElementCount::isKnownLE(VF, MaxFactors.FixedVF); VF *= 2)
8159     VFCandidates.insert(VF);
8160   for (auto VF = ElementCount::getScalable(1);
8161        ElementCount::isKnownLE(VF, MaxFactors.ScalableVF); VF *= 2)
8162     VFCandidates.insert(VF);
8163 
8164   for (const auto &VF : VFCandidates) {
8165     // Collect Uniform and Scalar instructions after vectorization with VF.
8166     CM.collectUniformsAndScalars(VF);
8167 
8168     // Collect the instructions (and their associated costs) that will be more
8169     // profitable to scalarize.
8170     if (VF.isVector())
8171       CM.collectInstsToScalarize(VF);
8172   }
8173 
8174   CM.collectInLoopReductions();
8175   buildVPlansWithVPRecipes(ElementCount::getFixed(1), MaxFactors.FixedVF);
8176   buildVPlansWithVPRecipes(ElementCount::getScalable(1), MaxFactors.ScalableVF);
8177 
8178   LLVM_DEBUG(printPlans(dbgs()));
8179   if (!MaxFactors.hasVector())
8180     return VectorizationFactor::Disabled();
8181 
8182   // Select the optimal vectorization factor.
8183   auto SelectedVF = CM.selectVectorizationFactor(VFCandidates);
8184 
8185   // Check if it is profitable to vectorize with runtime checks.
8186   unsigned NumRuntimePointerChecks = Requirements.getNumRuntimePointerChecks();
8187   if (SelectedVF.Width.getKnownMinValue() > 1 && NumRuntimePointerChecks) {
8188     bool PragmaThresholdReached =
8189         NumRuntimePointerChecks > PragmaVectorizeMemoryCheckThreshold;
8190     bool ThresholdReached =
8191         NumRuntimePointerChecks > VectorizerParams::RuntimeMemoryCheckThreshold;
8192     if ((ThresholdReached && !Hints.allowReordering()) ||
8193         PragmaThresholdReached) {
8194       ORE->emit([&]() {
8195         return OptimizationRemarkAnalysisAliasing(
8196                    DEBUG_TYPE, "CantReorderMemOps", OrigLoop->getStartLoc(),
8197                    OrigLoop->getHeader())
8198                << "loop not vectorized: cannot prove it is safe to reorder "
8199                   "memory operations";
8200       });
8201       LLVM_DEBUG(dbgs() << "LV: Too many memory checks needed.\n");
8202       Hints.emitRemarkWithHints();
8203       return VectorizationFactor::Disabled();
8204     }
8205   }
8206   return SelectedVF;
8207 }
8208 
8209 VPlan &LoopVectorizationPlanner::getBestPlanFor(ElementCount VF) const {
8210   assert(count_if(VPlans,
8211                   [VF](const VPlanPtr &Plan) { return Plan->hasVF(VF); }) ==
8212              1 &&
8213          "Best VF has not a single VPlan.");
8214 
8215   for (const VPlanPtr &Plan : VPlans) {
8216     if (Plan->hasVF(VF))
8217       return *Plan.get();
8218   }
8219   llvm_unreachable("No plan found!");
8220 }
8221 
8222 void LoopVectorizationPlanner::executePlan(ElementCount BestVF, unsigned BestUF,
8223                                            VPlan &BestVPlan,
8224                                            InnerLoopVectorizer &ILV,
8225                                            DominatorTree *DT) {
8226   LLVM_DEBUG(dbgs() << "Executing best plan with VF=" << BestVF << ", UF=" << BestUF
8227                     << '\n');
8228 
8229   // Perform the actual loop transformation.
8230 
8231   // 1. Create a new empty loop. Unlink the old loop and connect the new one.
8232   VPTransformState State{BestVF, BestUF, LI, DT, ILV.Builder, &ILV, &BestVPlan};
8233   State.CFG.PrevBB = ILV.createVectorizedLoopSkeleton();
8234   State.TripCount = ILV.getOrCreateTripCount(nullptr);
8235   State.CanonicalIV = ILV.Induction;
8236 
8237   ILV.printDebugTracesAtStart();
8238 
8239   //===------------------------------------------------===//
8240   //
8241   // Notice: any optimization or new instruction that go
8242   // into the code below should also be implemented in
8243   // the cost-model.
8244   //
8245   //===------------------------------------------------===//
8246 
8247   // 2. Copy and widen instructions from the old loop into the new loop.
8248   BestVPlan.execute(&State);
8249 
8250   // 3. Fix the vectorized code: take care of header phi's, live-outs,
8251   //    predication, updating analyses.
8252   ILV.fixVectorizedLoop(State);
8253 
8254   ILV.printDebugTracesAtEnd();
8255 }
8256 
8257 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
8258 void LoopVectorizationPlanner::printPlans(raw_ostream &O) {
8259   for (const auto &Plan : VPlans)
8260     if (PrintVPlansInDotFormat)
8261       Plan->printDOT(O);
8262     else
8263       Plan->print(O);
8264 }
8265 #endif
8266 
8267 void LoopVectorizationPlanner::collectTriviallyDeadInstructions(
8268     SmallPtrSetImpl<Instruction *> &DeadInstructions) {
8269 
8270   // We create new control-flow for the vectorized loop, so the original exit
8271   // conditions will be dead after vectorization if it's only used by the
8272   // terminator
8273   SmallVector<BasicBlock*> ExitingBlocks;
8274   OrigLoop->getExitingBlocks(ExitingBlocks);
8275   for (auto *BB : ExitingBlocks) {
8276     auto *Cmp = dyn_cast<Instruction>(BB->getTerminator()->getOperand(0));
8277     if (!Cmp || !Cmp->hasOneUse())
8278       continue;
8279 
8280     // TODO: we should introduce a getUniqueExitingBlocks on Loop
8281     if (!DeadInstructions.insert(Cmp).second)
8282       continue;
8283 
8284     // The operands of the icmp is often a dead trunc, used by IndUpdate.
8285     // TODO: can recurse through operands in general
8286     for (Value *Op : Cmp->operands()) {
8287       if (isa<TruncInst>(Op) && Op->hasOneUse())
8288           DeadInstructions.insert(cast<Instruction>(Op));
8289     }
8290   }
8291 
8292   // We create new "steps" for induction variable updates to which the original
8293   // induction variables map. An original update instruction will be dead if
8294   // all its users except the induction variable are dead.
8295   auto *Latch = OrigLoop->getLoopLatch();
8296   for (auto &Induction : Legal->getInductionVars()) {
8297     PHINode *Ind = Induction.first;
8298     auto *IndUpdate = cast<Instruction>(Ind->getIncomingValueForBlock(Latch));
8299 
8300     // If the tail is to be folded by masking, the primary induction variable,
8301     // if exists, isn't dead: it will be used for masking. Don't kill it.
8302     if (CM.foldTailByMasking() && IndUpdate == Legal->getPrimaryInduction())
8303       continue;
8304 
8305     if (llvm::all_of(IndUpdate->users(), [&](User *U) -> bool {
8306           return U == Ind || DeadInstructions.count(cast<Instruction>(U));
8307         }))
8308       DeadInstructions.insert(IndUpdate);
8309 
8310     // We record as "Dead" also the type-casting instructions we had identified
8311     // during induction analysis. We don't need any handling for them in the
8312     // vectorized loop because we have proven that, under a proper runtime
8313     // test guarding the vectorized loop, the value of the phi, and the casted
8314     // value of the phi, are the same. The last instruction in this casting chain
8315     // will get its scalar/vector/widened def from the scalar/vector/widened def
8316     // of the respective phi node. Any other casts in the induction def-use chain
8317     // have no other uses outside the phi update chain, and will be ignored.
8318     InductionDescriptor &IndDes = Induction.second;
8319     const SmallVectorImpl<Instruction *> &Casts = IndDes.getCastInsts();
8320     DeadInstructions.insert(Casts.begin(), Casts.end());
8321   }
8322 }
8323 
8324 Value *InnerLoopUnroller::reverseVector(Value *Vec) { return Vec; }
8325 
8326 Value *InnerLoopUnroller::getBroadcastInstrs(Value *V) { return V; }
8327 
8328 Value *InnerLoopUnroller::getStepVector(Value *Val, Value *StartIdx,
8329                                         Value *Step,
8330                                         Instruction::BinaryOps BinOp) {
8331   // When unrolling and the VF is 1, we only need to add a simple scalar.
8332   Type *Ty = Val->getType();
8333   assert(!Ty->isVectorTy() && "Val must be a scalar");
8334 
8335   if (Ty->isFloatingPointTy()) {
8336     // Floating-point operations inherit FMF via the builder's flags.
8337     Value *MulOp = Builder.CreateFMul(StartIdx, Step);
8338     return Builder.CreateBinOp(BinOp, Val, MulOp);
8339   }
8340   return Builder.CreateAdd(Val, Builder.CreateMul(StartIdx, Step), "induction");
8341 }
8342 
8343 static void AddRuntimeUnrollDisableMetaData(Loop *L) {
8344   SmallVector<Metadata *, 4> MDs;
8345   // Reserve first location for self reference to the LoopID metadata node.
8346   MDs.push_back(nullptr);
8347   bool IsUnrollMetadata = false;
8348   MDNode *LoopID = L->getLoopID();
8349   if (LoopID) {
8350     // First find existing loop unrolling disable metadata.
8351     for (unsigned i = 1, ie = LoopID->getNumOperands(); i < ie; ++i) {
8352       auto *MD = dyn_cast<MDNode>(LoopID->getOperand(i));
8353       if (MD) {
8354         const auto *S = dyn_cast<MDString>(MD->getOperand(0));
8355         IsUnrollMetadata =
8356             S && S->getString().startswith("llvm.loop.unroll.disable");
8357       }
8358       MDs.push_back(LoopID->getOperand(i));
8359     }
8360   }
8361 
8362   if (!IsUnrollMetadata) {
8363     // Add runtime unroll disable metadata.
8364     LLVMContext &Context = L->getHeader()->getContext();
8365     SmallVector<Metadata *, 1> DisableOperands;
8366     DisableOperands.push_back(
8367         MDString::get(Context, "llvm.loop.unroll.runtime.disable"));
8368     MDNode *DisableNode = MDNode::get(Context, DisableOperands);
8369     MDs.push_back(DisableNode);
8370     MDNode *NewLoopID = MDNode::get(Context, MDs);
8371     // Set operand 0 to refer to the loop id itself.
8372     NewLoopID->replaceOperandWith(0, NewLoopID);
8373     L->setLoopID(NewLoopID);
8374   }
8375 }
8376 
8377 //===--------------------------------------------------------------------===//
8378 // EpilogueVectorizerMainLoop
8379 //===--------------------------------------------------------------------===//
8380 
8381 /// This function is partially responsible for generating the control flow
8382 /// depicted in https://llvm.org/docs/Vectorizers.html#epilogue-vectorization.
8383 BasicBlock *EpilogueVectorizerMainLoop::createEpilogueVectorizedLoopSkeleton() {
8384   MDNode *OrigLoopID = OrigLoop->getLoopID();
8385   Loop *Lp = createVectorLoopSkeleton("");
8386 
8387   // Generate the code to check the minimum iteration count of the vector
8388   // epilogue (see below).
8389   EPI.EpilogueIterationCountCheck =
8390       emitMinimumIterationCountCheck(Lp, LoopScalarPreHeader, true);
8391   EPI.EpilogueIterationCountCheck->setName("iter.check");
8392 
8393   // Generate the code to check any assumptions that we've made for SCEV
8394   // expressions.
8395   EPI.SCEVSafetyCheck = emitSCEVChecks(Lp, LoopScalarPreHeader);
8396 
8397   // Generate the code that checks at runtime if arrays overlap. We put the
8398   // checks into a separate block to make the more common case of few elements
8399   // faster.
8400   EPI.MemSafetyCheck = emitMemRuntimeChecks(Lp, LoopScalarPreHeader);
8401 
8402   // Generate the iteration count check for the main loop, *after* the check
8403   // for the epilogue loop, so that the path-length is shorter for the case
8404   // that goes directly through the vector epilogue. The longer-path length for
8405   // the main loop is compensated for, by the gain from vectorizing the larger
8406   // trip count. Note: the branch will get updated later on when we vectorize
8407   // the epilogue.
8408   EPI.MainLoopIterationCountCheck =
8409       emitMinimumIterationCountCheck(Lp, LoopScalarPreHeader, false);
8410 
8411   // Generate the induction variable.
8412   OldInduction = Legal->getPrimaryInduction();
8413   Type *IdxTy = Legal->getWidestInductionType();
8414   Value *StartIdx = ConstantInt::get(IdxTy, 0);
8415   Constant *Step = ConstantInt::get(IdxTy, VF.getKnownMinValue() * UF);
8416   Value *CountRoundDown = getOrCreateVectorTripCount(Lp);
8417   EPI.VectorTripCount = CountRoundDown;
8418   Induction =
8419       createInductionVariable(Lp, StartIdx, CountRoundDown, Step,
8420                               getDebugLocFromInstOrOperands(OldInduction));
8421 
8422   // Skip induction resume value creation here because they will be created in
8423   // the second pass. If we created them here, they wouldn't be used anyway,
8424   // because the vplan in the second pass still contains the inductions from the
8425   // original loop.
8426 
8427   return completeLoopSkeleton(Lp, OrigLoopID);
8428 }
8429 
8430 void EpilogueVectorizerMainLoop::printDebugTracesAtStart() {
8431   LLVM_DEBUG({
8432     dbgs() << "Create Skeleton for epilogue vectorized loop (first pass)\n"
8433            << "Main Loop VF:" << EPI.MainLoopVF
8434            << ", Main Loop UF:" << EPI.MainLoopUF
8435            << ", Epilogue Loop VF:" << EPI.EpilogueVF
8436            << ", Epilogue Loop UF:" << EPI.EpilogueUF << "\n";
8437   });
8438 }
8439 
8440 void EpilogueVectorizerMainLoop::printDebugTracesAtEnd() {
8441   DEBUG_WITH_TYPE(VerboseDebug, {
8442     dbgs() << "intermediate fn:\n" << *Induction->getFunction() << "\n";
8443   });
8444 }
8445 
8446 BasicBlock *EpilogueVectorizerMainLoop::emitMinimumIterationCountCheck(
8447     Loop *L, BasicBlock *Bypass, bool ForEpilogue) {
8448   assert(L && "Expected valid Loop.");
8449   assert(Bypass && "Expected valid bypass basic block.");
8450   ElementCount VFactor = ForEpilogue ? EPI.EpilogueVF : VF;
8451   unsigned UFactor = ForEpilogue ? EPI.EpilogueUF : UF;
8452   Value *Count = getOrCreateTripCount(L);
8453   // Reuse existing vector loop preheader for TC checks.
8454   // Note that new preheader block is generated for vector loop.
8455   BasicBlock *const TCCheckBlock = LoopVectorPreHeader;
8456   IRBuilder<> Builder(TCCheckBlock->getTerminator());
8457 
8458   // Generate code to check if the loop's trip count is less than VF * UF of the
8459   // main vector loop.
8460   auto P = Cost->requiresScalarEpilogue(ForEpilogue ? EPI.EpilogueVF : VF) ?
8461       ICmpInst::ICMP_ULE : ICmpInst::ICMP_ULT;
8462 
8463   Value *CheckMinIters = Builder.CreateICmp(
8464       P, Count, getRuntimeVF(Builder, Count->getType(), VFactor * UFactor),
8465       "min.iters.check");
8466 
8467   if (!ForEpilogue)
8468     TCCheckBlock->setName("vector.main.loop.iter.check");
8469 
8470   // Create new preheader for vector loop.
8471   LoopVectorPreHeader = SplitBlock(TCCheckBlock, TCCheckBlock->getTerminator(),
8472                                    DT, LI, nullptr, "vector.ph");
8473 
8474   if (ForEpilogue) {
8475     assert(DT->properlyDominates(DT->getNode(TCCheckBlock),
8476                                  DT->getNode(Bypass)->getIDom()) &&
8477            "TC check is expected to dominate Bypass");
8478 
8479     // Update dominator for Bypass & LoopExit.
8480     DT->changeImmediateDominator(Bypass, TCCheckBlock);
8481     if (!Cost->requiresScalarEpilogue(EPI.EpilogueVF))
8482       // For loops with multiple exits, there's no edge from the middle block
8483       // to exit blocks (as the epilogue must run) and thus no need to update
8484       // the immediate dominator of the exit blocks.
8485       DT->changeImmediateDominator(LoopExitBlock, TCCheckBlock);
8486 
8487     LoopBypassBlocks.push_back(TCCheckBlock);
8488 
8489     // Save the trip count so we don't have to regenerate it in the
8490     // vec.epilog.iter.check. This is safe to do because the trip count
8491     // generated here dominates the vector epilog iter check.
8492     EPI.TripCount = Count;
8493   }
8494 
8495   ReplaceInstWithInst(
8496       TCCheckBlock->getTerminator(),
8497       BranchInst::Create(Bypass, LoopVectorPreHeader, CheckMinIters));
8498 
8499   return TCCheckBlock;
8500 }
8501 
8502 //===--------------------------------------------------------------------===//
8503 // EpilogueVectorizerEpilogueLoop
8504 //===--------------------------------------------------------------------===//
8505 
8506 /// This function is partially responsible for generating the control flow
8507 /// depicted in https://llvm.org/docs/Vectorizers.html#epilogue-vectorization.
8508 BasicBlock *
8509 EpilogueVectorizerEpilogueLoop::createEpilogueVectorizedLoopSkeleton() {
8510   MDNode *OrigLoopID = OrigLoop->getLoopID();
8511   Loop *Lp = createVectorLoopSkeleton("vec.epilog.");
8512 
8513   // Now, compare the remaining count and if there aren't enough iterations to
8514   // execute the vectorized epilogue skip to the scalar part.
8515   BasicBlock *VecEpilogueIterationCountCheck = LoopVectorPreHeader;
8516   VecEpilogueIterationCountCheck->setName("vec.epilog.iter.check");
8517   LoopVectorPreHeader =
8518       SplitBlock(LoopVectorPreHeader, LoopVectorPreHeader->getTerminator(), DT,
8519                  LI, nullptr, "vec.epilog.ph");
8520   emitMinimumVectorEpilogueIterCountCheck(Lp, LoopScalarPreHeader,
8521                                           VecEpilogueIterationCountCheck);
8522 
8523   // Adjust the control flow taking the state info from the main loop
8524   // vectorization into account.
8525   assert(EPI.MainLoopIterationCountCheck && EPI.EpilogueIterationCountCheck &&
8526          "expected this to be saved from the previous pass.");
8527   EPI.MainLoopIterationCountCheck->getTerminator()->replaceUsesOfWith(
8528       VecEpilogueIterationCountCheck, LoopVectorPreHeader);
8529 
8530   DT->changeImmediateDominator(LoopVectorPreHeader,
8531                                EPI.MainLoopIterationCountCheck);
8532 
8533   EPI.EpilogueIterationCountCheck->getTerminator()->replaceUsesOfWith(
8534       VecEpilogueIterationCountCheck, LoopScalarPreHeader);
8535 
8536   if (EPI.SCEVSafetyCheck)
8537     EPI.SCEVSafetyCheck->getTerminator()->replaceUsesOfWith(
8538         VecEpilogueIterationCountCheck, LoopScalarPreHeader);
8539   if (EPI.MemSafetyCheck)
8540     EPI.MemSafetyCheck->getTerminator()->replaceUsesOfWith(
8541         VecEpilogueIterationCountCheck, LoopScalarPreHeader);
8542 
8543   DT->changeImmediateDominator(
8544       VecEpilogueIterationCountCheck,
8545       VecEpilogueIterationCountCheck->getSinglePredecessor());
8546 
8547   DT->changeImmediateDominator(LoopScalarPreHeader,
8548                                EPI.EpilogueIterationCountCheck);
8549   if (!Cost->requiresScalarEpilogue(EPI.EpilogueVF))
8550     // If there is an epilogue which must run, there's no edge from the
8551     // middle block to exit blocks  and thus no need to update the immediate
8552     // dominator of the exit blocks.
8553     DT->changeImmediateDominator(LoopExitBlock,
8554                                  EPI.EpilogueIterationCountCheck);
8555 
8556   // Keep track of bypass blocks, as they feed start values to the induction
8557   // phis in the scalar loop preheader.
8558   if (EPI.SCEVSafetyCheck)
8559     LoopBypassBlocks.push_back(EPI.SCEVSafetyCheck);
8560   if (EPI.MemSafetyCheck)
8561     LoopBypassBlocks.push_back(EPI.MemSafetyCheck);
8562   LoopBypassBlocks.push_back(EPI.EpilogueIterationCountCheck);
8563 
8564   // Generate a resume induction for the vector epilogue and put it in the
8565   // vector epilogue preheader
8566   Type *IdxTy = Legal->getWidestInductionType();
8567   PHINode *EPResumeVal = PHINode::Create(IdxTy, 2, "vec.epilog.resume.val",
8568                                          LoopVectorPreHeader->getFirstNonPHI());
8569   EPResumeVal->addIncoming(EPI.VectorTripCount, VecEpilogueIterationCountCheck);
8570   EPResumeVal->addIncoming(ConstantInt::get(IdxTy, 0),
8571                            EPI.MainLoopIterationCountCheck);
8572 
8573   // Generate the induction variable.
8574   OldInduction = Legal->getPrimaryInduction();
8575   Value *CountRoundDown = getOrCreateVectorTripCount(Lp);
8576   Constant *Step = ConstantInt::get(IdxTy, VF.getKnownMinValue() * UF);
8577   Value *StartIdx = EPResumeVal;
8578   Induction =
8579       createInductionVariable(Lp, StartIdx, CountRoundDown, Step,
8580                               getDebugLocFromInstOrOperands(OldInduction));
8581 
8582   // Generate induction resume values. These variables save the new starting
8583   // indexes for the scalar loop. They are used to test if there are any tail
8584   // iterations left once the vector loop has completed.
8585   // Note that when the vectorized epilogue is skipped due to iteration count
8586   // check, then the resume value for the induction variable comes from
8587   // the trip count of the main vector loop, hence passing the AdditionalBypass
8588   // argument.
8589   createInductionResumeValues(Lp, CountRoundDown,
8590                               {VecEpilogueIterationCountCheck,
8591                                EPI.VectorTripCount} /* AdditionalBypass */);
8592 
8593   AddRuntimeUnrollDisableMetaData(Lp);
8594   return completeLoopSkeleton(Lp, OrigLoopID);
8595 }
8596 
8597 BasicBlock *
8598 EpilogueVectorizerEpilogueLoop::emitMinimumVectorEpilogueIterCountCheck(
8599     Loop *L, BasicBlock *Bypass, BasicBlock *Insert) {
8600 
8601   assert(EPI.TripCount &&
8602          "Expected trip count to have been safed in the first pass.");
8603   assert(
8604       (!isa<Instruction>(EPI.TripCount) ||
8605        DT->dominates(cast<Instruction>(EPI.TripCount)->getParent(), Insert)) &&
8606       "saved trip count does not dominate insertion point.");
8607   Value *TC = EPI.TripCount;
8608   IRBuilder<> Builder(Insert->getTerminator());
8609   Value *Count = Builder.CreateSub(TC, EPI.VectorTripCount, "n.vec.remaining");
8610 
8611   // Generate code to check if the loop's trip count is less than VF * UF of the
8612   // vector epilogue loop.
8613   auto P = Cost->requiresScalarEpilogue(EPI.EpilogueVF) ?
8614       ICmpInst::ICMP_ULE : ICmpInst::ICMP_ULT;
8615 
8616   Value *CheckMinIters = Builder.CreateICmp(
8617       P, Count,
8618       getRuntimeVF(Builder, Count->getType(), EPI.EpilogueVF * EPI.EpilogueUF),
8619       "min.epilog.iters.check");
8620 
8621   ReplaceInstWithInst(
8622       Insert->getTerminator(),
8623       BranchInst::Create(Bypass, LoopVectorPreHeader, CheckMinIters));
8624 
8625   LoopBypassBlocks.push_back(Insert);
8626   return Insert;
8627 }
8628 
8629 void EpilogueVectorizerEpilogueLoop::printDebugTracesAtStart() {
8630   LLVM_DEBUG({
8631     dbgs() << "Create Skeleton for epilogue vectorized loop (second pass)\n"
8632            << "Epilogue Loop VF:" << EPI.EpilogueVF
8633            << ", Epilogue Loop UF:" << EPI.EpilogueUF << "\n";
8634   });
8635 }
8636 
8637 void EpilogueVectorizerEpilogueLoop::printDebugTracesAtEnd() {
8638   DEBUG_WITH_TYPE(VerboseDebug, {
8639     dbgs() << "final fn:\n" << *Induction->getFunction() << "\n";
8640   });
8641 }
8642 
8643 bool LoopVectorizationPlanner::getDecisionAndClampRange(
8644     const std::function<bool(ElementCount)> &Predicate, VFRange &Range) {
8645   assert(!Range.isEmpty() && "Trying to test an empty VF range.");
8646   bool PredicateAtRangeStart = Predicate(Range.Start);
8647 
8648   for (ElementCount TmpVF = Range.Start * 2;
8649        ElementCount::isKnownLT(TmpVF, Range.End); TmpVF *= 2)
8650     if (Predicate(TmpVF) != PredicateAtRangeStart) {
8651       Range.End = TmpVF;
8652       break;
8653     }
8654 
8655   return PredicateAtRangeStart;
8656 }
8657 
8658 /// Build VPlans for the full range of feasible VF's = {\p MinVF, 2 * \p MinVF,
8659 /// 4 * \p MinVF, ..., \p MaxVF} by repeatedly building a VPlan for a sub-range
8660 /// of VF's starting at a given VF and extending it as much as possible. Each
8661 /// vectorization decision can potentially shorten this sub-range during
8662 /// buildVPlan().
8663 void LoopVectorizationPlanner::buildVPlans(ElementCount MinVF,
8664                                            ElementCount MaxVF) {
8665   auto MaxVFPlusOne = MaxVF.getWithIncrement(1);
8666   for (ElementCount VF = MinVF; ElementCount::isKnownLT(VF, MaxVFPlusOne);) {
8667     VFRange SubRange = {VF, MaxVFPlusOne};
8668     VPlans.push_back(buildVPlan(SubRange));
8669     VF = SubRange.End;
8670   }
8671 }
8672 
8673 VPValue *VPRecipeBuilder::createEdgeMask(BasicBlock *Src, BasicBlock *Dst,
8674                                          VPlanPtr &Plan) {
8675   assert(is_contained(predecessors(Dst), Src) && "Invalid edge");
8676 
8677   // Look for cached value.
8678   std::pair<BasicBlock *, BasicBlock *> Edge(Src, Dst);
8679   EdgeMaskCacheTy::iterator ECEntryIt = EdgeMaskCache.find(Edge);
8680   if (ECEntryIt != EdgeMaskCache.end())
8681     return ECEntryIt->second;
8682 
8683   VPValue *SrcMask = createBlockInMask(Src, Plan);
8684 
8685   // The terminator has to be a branch inst!
8686   BranchInst *BI = dyn_cast<BranchInst>(Src->getTerminator());
8687   assert(BI && "Unexpected terminator found");
8688 
8689   if (!BI->isConditional() || BI->getSuccessor(0) == BI->getSuccessor(1))
8690     return EdgeMaskCache[Edge] = SrcMask;
8691 
8692   // If source is an exiting block, we know the exit edge is dynamically dead
8693   // in the vector loop, and thus we don't need to restrict the mask.  Avoid
8694   // adding uses of an otherwise potentially dead instruction.
8695   if (OrigLoop->isLoopExiting(Src))
8696     return EdgeMaskCache[Edge] = SrcMask;
8697 
8698   VPValue *EdgeMask = Plan->getOrAddVPValue(BI->getCondition());
8699   assert(EdgeMask && "No Edge Mask found for condition");
8700 
8701   if (BI->getSuccessor(0) != Dst)
8702     EdgeMask = Builder.createNot(EdgeMask);
8703 
8704   if (SrcMask) { // Otherwise block in-mask is all-one, no need to AND.
8705     // The condition is 'SrcMask && EdgeMask', which is equivalent to
8706     // 'select i1 SrcMask, i1 EdgeMask, i1 false'.
8707     // The select version does not introduce new UB if SrcMask is false and
8708     // EdgeMask is poison. Using 'and' here introduces undefined behavior.
8709     VPValue *False = Plan->getOrAddVPValue(
8710         ConstantInt::getFalse(BI->getCondition()->getType()));
8711     EdgeMask = Builder.createSelect(SrcMask, EdgeMask, False);
8712   }
8713 
8714   return EdgeMaskCache[Edge] = EdgeMask;
8715 }
8716 
8717 VPValue *VPRecipeBuilder::createBlockInMask(BasicBlock *BB, VPlanPtr &Plan) {
8718   assert(OrigLoop->contains(BB) && "Block is not a part of a loop");
8719 
8720   // Look for cached value.
8721   BlockMaskCacheTy::iterator BCEntryIt = BlockMaskCache.find(BB);
8722   if (BCEntryIt != BlockMaskCache.end())
8723     return BCEntryIt->second;
8724 
8725   // All-one mask is modelled as no-mask following the convention for masked
8726   // load/store/gather/scatter. Initialize BlockMask to no-mask.
8727   VPValue *BlockMask = nullptr;
8728 
8729   if (OrigLoop->getHeader() == BB) {
8730     if (!CM.blockNeedsPredication(BB))
8731       return BlockMaskCache[BB] = BlockMask; // Loop incoming mask is all-one.
8732 
8733     // Create the block in mask as the first non-phi instruction in the block.
8734     VPBuilder::InsertPointGuard Guard(Builder);
8735     auto NewInsertionPoint = Builder.getInsertBlock()->getFirstNonPhi();
8736     Builder.setInsertPoint(Builder.getInsertBlock(), NewInsertionPoint);
8737 
8738     // Introduce the early-exit compare IV <= BTC to form header block mask.
8739     // This is used instead of IV < TC because TC may wrap, unlike BTC.
8740     // Start by constructing the desired canonical IV.
8741     VPValue *IV = nullptr;
8742     if (Legal->getPrimaryInduction())
8743       IV = Plan->getOrAddVPValue(Legal->getPrimaryInduction());
8744     else {
8745       auto *IVRecipe = new VPWidenCanonicalIVRecipe();
8746       Builder.getInsertBlock()->insert(IVRecipe, NewInsertionPoint);
8747       IV = IVRecipe;
8748     }
8749     VPValue *BTC = Plan->getOrCreateBackedgeTakenCount();
8750     bool TailFolded = !CM.isScalarEpilogueAllowed();
8751 
8752     if (TailFolded && CM.TTI.emitGetActiveLaneMask()) {
8753       // While ActiveLaneMask is a binary op that consumes the loop tripcount
8754       // as a second argument, we only pass the IV here and extract the
8755       // tripcount from the transform state where codegen of the VP instructions
8756       // happen.
8757       BlockMask = Builder.createNaryOp(VPInstruction::ActiveLaneMask, {IV});
8758     } else {
8759       BlockMask = Builder.createNaryOp(VPInstruction::ICmpULE, {IV, BTC});
8760     }
8761     return BlockMaskCache[BB] = BlockMask;
8762   }
8763 
8764   // This is the block mask. We OR all incoming edges.
8765   for (auto *Predecessor : predecessors(BB)) {
8766     VPValue *EdgeMask = createEdgeMask(Predecessor, BB, Plan);
8767     if (!EdgeMask) // Mask of predecessor is all-one so mask of block is too.
8768       return BlockMaskCache[BB] = EdgeMask;
8769 
8770     if (!BlockMask) { // BlockMask has its initialized nullptr value.
8771       BlockMask = EdgeMask;
8772       continue;
8773     }
8774 
8775     BlockMask = Builder.createOr(BlockMask, EdgeMask);
8776   }
8777 
8778   return BlockMaskCache[BB] = BlockMask;
8779 }
8780 
8781 VPRecipeBase *VPRecipeBuilder::tryToWidenMemory(Instruction *I,
8782                                                 ArrayRef<VPValue *> Operands,
8783                                                 VFRange &Range,
8784                                                 VPlanPtr &Plan) {
8785   assert((isa<LoadInst>(I) || isa<StoreInst>(I)) &&
8786          "Must be called with either a load or store");
8787 
8788   auto willWiden = [&](ElementCount VF) -> bool {
8789     if (VF.isScalar())
8790       return false;
8791     LoopVectorizationCostModel::InstWidening Decision =
8792         CM.getWideningDecision(I, VF);
8793     assert(Decision != LoopVectorizationCostModel::CM_Unknown &&
8794            "CM decision should be taken at this point.");
8795     if (Decision == LoopVectorizationCostModel::CM_Interleave)
8796       return true;
8797     if (CM.isScalarAfterVectorization(I, VF) ||
8798         CM.isProfitableToScalarize(I, VF))
8799       return false;
8800     return Decision != LoopVectorizationCostModel::CM_Scalarize;
8801   };
8802 
8803   if (!LoopVectorizationPlanner::getDecisionAndClampRange(willWiden, Range))
8804     return nullptr;
8805 
8806   VPValue *Mask = nullptr;
8807   if (Legal->isMaskRequired(I))
8808     Mask = createBlockInMask(I->getParent(), Plan);
8809 
8810   // Determine if the pointer operand of the access is either consecutive or
8811   // reverse consecutive.
8812   LoopVectorizationCostModel::InstWidening Decision =
8813       CM.getWideningDecision(I, Range.Start);
8814   bool Reverse = Decision == LoopVectorizationCostModel::CM_Widen_Reverse;
8815   bool Consecutive =
8816       Reverse || Decision == LoopVectorizationCostModel::CM_Widen;
8817 
8818   if (LoadInst *Load = dyn_cast<LoadInst>(I))
8819     return new VPWidenMemoryInstructionRecipe(*Load, Operands[0], Mask,
8820                                               Consecutive, Reverse);
8821 
8822   StoreInst *Store = cast<StoreInst>(I);
8823   return new VPWidenMemoryInstructionRecipe(*Store, Operands[1], Operands[0],
8824                                             Mask, Consecutive, Reverse);
8825 }
8826 
8827 VPWidenIntOrFpInductionRecipe *
8828 VPRecipeBuilder::tryToOptimizeInductionPHI(PHINode *Phi,
8829                                            ArrayRef<VPValue *> Operands) const {
8830   // Check if this is an integer or fp induction. If so, build the recipe that
8831   // produces its scalar and vector values.
8832   InductionDescriptor II = Legal->getInductionVars().lookup(Phi);
8833   if (II.getKind() == InductionDescriptor::IK_IntInduction ||
8834       II.getKind() == InductionDescriptor::IK_FpInduction) {
8835     assert(II.getStartValue() ==
8836            Phi->getIncomingValueForBlock(OrigLoop->getLoopPreheader()));
8837     const SmallVectorImpl<Instruction *> &Casts = II.getCastInsts();
8838     return new VPWidenIntOrFpInductionRecipe(
8839         Phi, Operands[0], Casts.empty() ? nullptr : Casts.front());
8840   }
8841 
8842   return nullptr;
8843 }
8844 
8845 VPWidenIntOrFpInductionRecipe *VPRecipeBuilder::tryToOptimizeInductionTruncate(
8846     TruncInst *I, ArrayRef<VPValue *> Operands, VFRange &Range,
8847     VPlan &Plan) const {
8848   // Optimize the special case where the source is a constant integer
8849   // induction variable. Notice that we can only optimize the 'trunc' case
8850   // because (a) FP conversions lose precision, (b) sext/zext may wrap, and
8851   // (c) other casts depend on pointer size.
8852 
8853   // Determine whether \p K is a truncation based on an induction variable that
8854   // can be optimized.
8855   auto isOptimizableIVTruncate =
8856       [&](Instruction *K) -> std::function<bool(ElementCount)> {
8857     return [=](ElementCount VF) -> bool {
8858       return CM.isOptimizableIVTruncate(K, VF);
8859     };
8860   };
8861 
8862   if (LoopVectorizationPlanner::getDecisionAndClampRange(
8863           isOptimizableIVTruncate(I), Range)) {
8864 
8865     InductionDescriptor II =
8866         Legal->getInductionVars().lookup(cast<PHINode>(I->getOperand(0)));
8867     VPValue *Start = Plan.getOrAddVPValue(II.getStartValue());
8868     return new VPWidenIntOrFpInductionRecipe(cast<PHINode>(I->getOperand(0)),
8869                                              Start, nullptr, I);
8870   }
8871   return nullptr;
8872 }
8873 
8874 VPRecipeOrVPValueTy VPRecipeBuilder::tryToBlend(PHINode *Phi,
8875                                                 ArrayRef<VPValue *> Operands,
8876                                                 VPlanPtr &Plan) {
8877   // If all incoming values are equal, the incoming VPValue can be used directly
8878   // instead of creating a new VPBlendRecipe.
8879   VPValue *FirstIncoming = Operands[0];
8880   if (all_of(Operands, [FirstIncoming](const VPValue *Inc) {
8881         return FirstIncoming == Inc;
8882       })) {
8883     return Operands[0];
8884   }
8885 
8886   // We know that all PHIs in non-header blocks are converted into selects, so
8887   // we don't have to worry about the insertion order and we can just use the
8888   // builder. At this point we generate the predication tree. There may be
8889   // duplications since this is a simple recursive scan, but future
8890   // optimizations will clean it up.
8891   SmallVector<VPValue *, 2> OperandsWithMask;
8892   unsigned NumIncoming = Phi->getNumIncomingValues();
8893 
8894   for (unsigned In = 0; In < NumIncoming; In++) {
8895     VPValue *EdgeMask =
8896       createEdgeMask(Phi->getIncomingBlock(In), Phi->getParent(), Plan);
8897     assert((EdgeMask || NumIncoming == 1) &&
8898            "Multiple predecessors with one having a full mask");
8899     OperandsWithMask.push_back(Operands[In]);
8900     if (EdgeMask)
8901       OperandsWithMask.push_back(EdgeMask);
8902   }
8903   return toVPRecipeResult(new VPBlendRecipe(Phi, OperandsWithMask));
8904 }
8905 
8906 VPWidenCallRecipe *VPRecipeBuilder::tryToWidenCall(CallInst *CI,
8907                                                    ArrayRef<VPValue *> Operands,
8908                                                    VFRange &Range) const {
8909 
8910   bool IsPredicated = LoopVectorizationPlanner::getDecisionAndClampRange(
8911       [this, CI](ElementCount VF) { return CM.isScalarWithPredication(CI); },
8912       Range);
8913 
8914   if (IsPredicated)
8915     return nullptr;
8916 
8917   Intrinsic::ID ID = getVectorIntrinsicIDForCall(CI, TLI);
8918   if (ID && (ID == Intrinsic::assume || ID == Intrinsic::lifetime_end ||
8919              ID == Intrinsic::lifetime_start || ID == Intrinsic::sideeffect ||
8920              ID == Intrinsic::pseudoprobe ||
8921              ID == Intrinsic::experimental_noalias_scope_decl))
8922     return nullptr;
8923 
8924   auto willWiden = [&](ElementCount VF) -> bool {
8925     Intrinsic::ID ID = getVectorIntrinsicIDForCall(CI, TLI);
8926     // The following case may be scalarized depending on the VF.
8927     // The flag shows whether we use Intrinsic or a usual Call for vectorized
8928     // version of the instruction.
8929     // Is it beneficial to perform intrinsic call compared to lib call?
8930     bool NeedToScalarize = false;
8931     InstructionCost CallCost = CM.getVectorCallCost(CI, VF, NeedToScalarize);
8932     InstructionCost IntrinsicCost = ID ? CM.getVectorIntrinsicCost(CI, VF) : 0;
8933     bool UseVectorIntrinsic = ID && IntrinsicCost <= CallCost;
8934     return UseVectorIntrinsic || !NeedToScalarize;
8935   };
8936 
8937   if (!LoopVectorizationPlanner::getDecisionAndClampRange(willWiden, Range))
8938     return nullptr;
8939 
8940   ArrayRef<VPValue *> Ops = Operands.take_front(CI->arg_size());
8941   return new VPWidenCallRecipe(*CI, make_range(Ops.begin(), Ops.end()));
8942 }
8943 
8944 bool VPRecipeBuilder::shouldWiden(Instruction *I, VFRange &Range) const {
8945   assert(!isa<BranchInst>(I) && !isa<PHINode>(I) && !isa<LoadInst>(I) &&
8946          !isa<StoreInst>(I) && "Instruction should have been handled earlier");
8947   // Instruction should be widened, unless it is scalar after vectorization,
8948   // scalarization is profitable or it is predicated.
8949   auto WillScalarize = [this, I](ElementCount VF) -> bool {
8950     return CM.isScalarAfterVectorization(I, VF) ||
8951            CM.isProfitableToScalarize(I, VF) || CM.isScalarWithPredication(I);
8952   };
8953   return !LoopVectorizationPlanner::getDecisionAndClampRange(WillScalarize,
8954                                                              Range);
8955 }
8956 
8957 VPWidenRecipe *VPRecipeBuilder::tryToWiden(Instruction *I,
8958                                            ArrayRef<VPValue *> Operands) const {
8959   auto IsVectorizableOpcode = [](unsigned Opcode) {
8960     switch (Opcode) {
8961     case Instruction::Add:
8962     case Instruction::And:
8963     case Instruction::AShr:
8964     case Instruction::BitCast:
8965     case Instruction::FAdd:
8966     case Instruction::FCmp:
8967     case Instruction::FDiv:
8968     case Instruction::FMul:
8969     case Instruction::FNeg:
8970     case Instruction::FPExt:
8971     case Instruction::FPToSI:
8972     case Instruction::FPToUI:
8973     case Instruction::FPTrunc:
8974     case Instruction::FRem:
8975     case Instruction::FSub:
8976     case Instruction::ICmp:
8977     case Instruction::IntToPtr:
8978     case Instruction::LShr:
8979     case Instruction::Mul:
8980     case Instruction::Or:
8981     case Instruction::PtrToInt:
8982     case Instruction::SDiv:
8983     case Instruction::Select:
8984     case Instruction::SExt:
8985     case Instruction::Shl:
8986     case Instruction::SIToFP:
8987     case Instruction::SRem:
8988     case Instruction::Sub:
8989     case Instruction::Trunc:
8990     case Instruction::UDiv:
8991     case Instruction::UIToFP:
8992     case Instruction::URem:
8993     case Instruction::Xor:
8994     case Instruction::ZExt:
8995       return true;
8996     }
8997     return false;
8998   };
8999 
9000   if (!IsVectorizableOpcode(I->getOpcode()))
9001     return nullptr;
9002 
9003   // Success: widen this instruction.
9004   return new VPWidenRecipe(*I, make_range(Operands.begin(), Operands.end()));
9005 }
9006 
9007 void VPRecipeBuilder::fixHeaderPhis() {
9008   BasicBlock *OrigLatch = OrigLoop->getLoopLatch();
9009   for (VPWidenPHIRecipe *R : PhisToFix) {
9010     auto *PN = cast<PHINode>(R->getUnderlyingValue());
9011     VPRecipeBase *IncR =
9012         getRecipe(cast<Instruction>(PN->getIncomingValueForBlock(OrigLatch)));
9013     R->addOperand(IncR->getVPSingleValue());
9014   }
9015 }
9016 
9017 VPBasicBlock *VPRecipeBuilder::handleReplication(
9018     Instruction *I, VFRange &Range, VPBasicBlock *VPBB,
9019     VPlanPtr &Plan) {
9020   bool IsUniform = LoopVectorizationPlanner::getDecisionAndClampRange(
9021       [&](ElementCount VF) { return CM.isUniformAfterVectorization(I, VF); },
9022       Range);
9023 
9024   bool IsPredicated = LoopVectorizationPlanner::getDecisionAndClampRange(
9025       [&](ElementCount VF) { return CM.isPredicatedInst(I); }, Range);
9026 
9027   // Even if the instruction is not marked as uniform, there are certain
9028   // intrinsic calls that can be effectively treated as such, so we check for
9029   // them here. Conservatively, we only do this for scalable vectors, since
9030   // for fixed-width VFs we can always fall back on full scalarization.
9031   if (!IsUniform && Range.Start.isScalable() && isa<IntrinsicInst>(I)) {
9032     switch (cast<IntrinsicInst>(I)->getIntrinsicID()) {
9033     case Intrinsic::assume:
9034     case Intrinsic::lifetime_start:
9035     case Intrinsic::lifetime_end:
9036       // For scalable vectors if one of the operands is variant then we still
9037       // want to mark as uniform, which will generate one instruction for just
9038       // the first lane of the vector. We can't scalarize the call in the same
9039       // way as for fixed-width vectors because we don't know how many lanes
9040       // there are.
9041       //
9042       // The reasons for doing it this way for scalable vectors are:
9043       //   1. For the assume intrinsic generating the instruction for the first
9044       //      lane is still be better than not generating any at all. For
9045       //      example, the input may be a splat across all lanes.
9046       //   2. For the lifetime start/end intrinsics the pointer operand only
9047       //      does anything useful when the input comes from a stack object,
9048       //      which suggests it should always be uniform. For non-stack objects
9049       //      the effect is to poison the object, which still allows us to
9050       //      remove the call.
9051       IsUniform = true;
9052       break;
9053     default:
9054       break;
9055     }
9056   }
9057 
9058   auto *Recipe = new VPReplicateRecipe(I, Plan->mapToVPValues(I->operands()),
9059                                        IsUniform, IsPredicated);
9060   setRecipe(I, Recipe);
9061   Plan->addVPValue(I, Recipe);
9062 
9063   // Find if I uses a predicated instruction. If so, it will use its scalar
9064   // value. Avoid hoisting the insert-element which packs the scalar value into
9065   // a vector value, as that happens iff all users use the vector value.
9066   for (VPValue *Op : Recipe->operands()) {
9067     auto *PredR = dyn_cast_or_null<VPPredInstPHIRecipe>(Op->getDef());
9068     if (!PredR)
9069       continue;
9070     auto *RepR =
9071         cast_or_null<VPReplicateRecipe>(PredR->getOperand(0)->getDef());
9072     assert(RepR->isPredicated() &&
9073            "expected Replicate recipe to be predicated");
9074     RepR->setAlsoPack(false);
9075   }
9076 
9077   // Finalize the recipe for Instr, first if it is not predicated.
9078   if (!IsPredicated) {
9079     LLVM_DEBUG(dbgs() << "LV: Scalarizing:" << *I << "\n");
9080     VPBB->appendRecipe(Recipe);
9081     return VPBB;
9082   }
9083   LLVM_DEBUG(dbgs() << "LV: Scalarizing and predicating:" << *I << "\n");
9084   assert(VPBB->getSuccessors().empty() &&
9085          "VPBB has successors when handling predicated replication.");
9086   // Record predicated instructions for above packing optimizations.
9087   VPBlockBase *Region = createReplicateRegion(I, Recipe, Plan);
9088   VPBlockUtils::insertBlockAfter(Region, VPBB);
9089   auto *RegSucc = new VPBasicBlock();
9090   VPBlockUtils::insertBlockAfter(RegSucc, Region);
9091   return RegSucc;
9092 }
9093 
9094 VPRegionBlock *VPRecipeBuilder::createReplicateRegion(Instruction *Instr,
9095                                                       VPRecipeBase *PredRecipe,
9096                                                       VPlanPtr &Plan) {
9097   // Instructions marked for predication are replicated and placed under an
9098   // if-then construct to prevent side-effects.
9099 
9100   // Generate recipes to compute the block mask for this region.
9101   VPValue *BlockInMask = createBlockInMask(Instr->getParent(), Plan);
9102 
9103   // Build the triangular if-then region.
9104   std::string RegionName = (Twine("pred.") + Instr->getOpcodeName()).str();
9105   assert(Instr->getParent() && "Predicated instruction not in any basic block");
9106   auto *BOMRecipe = new VPBranchOnMaskRecipe(BlockInMask);
9107   auto *Entry = new VPBasicBlock(Twine(RegionName) + ".entry", BOMRecipe);
9108   auto *PHIRecipe = Instr->getType()->isVoidTy()
9109                         ? nullptr
9110                         : new VPPredInstPHIRecipe(Plan->getOrAddVPValue(Instr));
9111   if (PHIRecipe) {
9112     Plan->removeVPValueFor(Instr);
9113     Plan->addVPValue(Instr, PHIRecipe);
9114   }
9115   auto *Exit = new VPBasicBlock(Twine(RegionName) + ".continue", PHIRecipe);
9116   auto *Pred = new VPBasicBlock(Twine(RegionName) + ".if", PredRecipe);
9117   VPRegionBlock *Region = new VPRegionBlock(Entry, Exit, RegionName, true);
9118 
9119   // Note: first set Entry as region entry and then connect successors starting
9120   // from it in order, to propagate the "parent" of each VPBasicBlock.
9121   VPBlockUtils::insertTwoBlocksAfter(Pred, Exit, BlockInMask, Entry);
9122   VPBlockUtils::connectBlocks(Pred, Exit);
9123 
9124   return Region;
9125 }
9126 
9127 VPRecipeOrVPValueTy
9128 VPRecipeBuilder::tryToCreateWidenRecipe(Instruction *Instr,
9129                                         ArrayRef<VPValue *> Operands,
9130                                         VFRange &Range, VPlanPtr &Plan) {
9131   // First, check for specific widening recipes that deal with calls, memory
9132   // operations, inductions and Phi nodes.
9133   if (auto *CI = dyn_cast<CallInst>(Instr))
9134     return toVPRecipeResult(tryToWidenCall(CI, Operands, Range));
9135 
9136   if (isa<LoadInst>(Instr) || isa<StoreInst>(Instr))
9137     return toVPRecipeResult(tryToWidenMemory(Instr, Operands, Range, Plan));
9138 
9139   VPRecipeBase *Recipe;
9140   if (auto Phi = dyn_cast<PHINode>(Instr)) {
9141     if (Phi->getParent() != OrigLoop->getHeader())
9142       return tryToBlend(Phi, Operands, Plan);
9143     if ((Recipe = tryToOptimizeInductionPHI(Phi, Operands)))
9144       return toVPRecipeResult(Recipe);
9145 
9146     VPWidenPHIRecipe *PhiRecipe = nullptr;
9147     if (Legal->isReductionVariable(Phi) || Legal->isFirstOrderRecurrence(Phi)) {
9148       VPValue *StartV = Operands[0];
9149       if (Legal->isReductionVariable(Phi)) {
9150         RecurrenceDescriptor &RdxDesc = Legal->getReductionVars()[Phi];
9151         assert(RdxDesc.getRecurrenceStartValue() ==
9152                Phi->getIncomingValueForBlock(OrigLoop->getLoopPreheader()));
9153         PhiRecipe = new VPReductionPHIRecipe(Phi, RdxDesc, *StartV,
9154                                              CM.isInLoopReduction(Phi),
9155                                              CM.useOrderedReductions(RdxDesc));
9156       } else {
9157         PhiRecipe = new VPFirstOrderRecurrencePHIRecipe(Phi, *StartV);
9158       }
9159 
9160       // Record the incoming value from the backedge, so we can add the incoming
9161       // value from the backedge after all recipes have been created.
9162       recordRecipeOf(cast<Instruction>(
9163           Phi->getIncomingValueForBlock(OrigLoop->getLoopLatch())));
9164       PhisToFix.push_back(PhiRecipe);
9165     } else {
9166       // TODO: record start and backedge value for remaining pointer induction
9167       // phis.
9168       assert(Phi->getType()->isPointerTy() &&
9169              "only pointer phis should be handled here");
9170       PhiRecipe = new VPWidenPHIRecipe(Phi);
9171     }
9172 
9173     return toVPRecipeResult(PhiRecipe);
9174   }
9175 
9176   if (isa<TruncInst>(Instr) &&
9177       (Recipe = tryToOptimizeInductionTruncate(cast<TruncInst>(Instr), Operands,
9178                                                Range, *Plan)))
9179     return toVPRecipeResult(Recipe);
9180 
9181   if (!shouldWiden(Instr, Range))
9182     return nullptr;
9183 
9184   if (auto GEP = dyn_cast<GetElementPtrInst>(Instr))
9185     return toVPRecipeResult(new VPWidenGEPRecipe(
9186         GEP, make_range(Operands.begin(), Operands.end()), OrigLoop));
9187 
9188   if (auto *SI = dyn_cast<SelectInst>(Instr)) {
9189     bool InvariantCond =
9190         PSE.getSE()->isLoopInvariant(PSE.getSCEV(SI->getOperand(0)), OrigLoop);
9191     return toVPRecipeResult(new VPWidenSelectRecipe(
9192         *SI, make_range(Operands.begin(), Operands.end()), InvariantCond));
9193   }
9194 
9195   return toVPRecipeResult(tryToWiden(Instr, Operands));
9196 }
9197 
9198 void LoopVectorizationPlanner::buildVPlansWithVPRecipes(ElementCount MinVF,
9199                                                         ElementCount MaxVF) {
9200   assert(OrigLoop->isInnermost() && "Inner loop expected.");
9201 
9202   // Collect instructions from the original loop that will become trivially dead
9203   // in the vectorized loop. We don't need to vectorize these instructions. For
9204   // example, original induction update instructions can become dead because we
9205   // separately emit induction "steps" when generating code for the new loop.
9206   // Similarly, we create a new latch condition when setting up the structure
9207   // of the new loop, so the old one can become dead.
9208   SmallPtrSet<Instruction *, 4> DeadInstructions;
9209   collectTriviallyDeadInstructions(DeadInstructions);
9210 
9211   // Add assume instructions we need to drop to DeadInstructions, to prevent
9212   // them from being added to the VPlan.
9213   // TODO: We only need to drop assumes in blocks that get flattend. If the
9214   // control flow is preserved, we should keep them.
9215   auto &ConditionalAssumes = Legal->getConditionalAssumes();
9216   DeadInstructions.insert(ConditionalAssumes.begin(), ConditionalAssumes.end());
9217 
9218   MapVector<Instruction *, Instruction *> &SinkAfter = Legal->getSinkAfter();
9219   // Dead instructions do not need sinking. Remove them from SinkAfter.
9220   for (Instruction *I : DeadInstructions)
9221     SinkAfter.erase(I);
9222 
9223   // Cannot sink instructions after dead instructions (there won't be any
9224   // recipes for them). Instead, find the first non-dead previous instruction.
9225   for (auto &P : Legal->getSinkAfter()) {
9226     Instruction *SinkTarget = P.second;
9227     Instruction *FirstInst = &*SinkTarget->getParent()->begin();
9228     (void)FirstInst;
9229     while (DeadInstructions.contains(SinkTarget)) {
9230       assert(
9231           SinkTarget != FirstInst &&
9232           "Must find a live instruction (at least the one feeding the "
9233           "first-order recurrence PHI) before reaching beginning of the block");
9234       SinkTarget = SinkTarget->getPrevNode();
9235       assert(SinkTarget != P.first &&
9236              "sink source equals target, no sinking required");
9237     }
9238     P.second = SinkTarget;
9239   }
9240 
9241   auto MaxVFPlusOne = MaxVF.getWithIncrement(1);
9242   for (ElementCount VF = MinVF; ElementCount::isKnownLT(VF, MaxVFPlusOne);) {
9243     VFRange SubRange = {VF, MaxVFPlusOne};
9244     VPlans.push_back(
9245         buildVPlanWithVPRecipes(SubRange, DeadInstructions, SinkAfter));
9246     VF = SubRange.End;
9247   }
9248 }
9249 
9250 VPlanPtr LoopVectorizationPlanner::buildVPlanWithVPRecipes(
9251     VFRange &Range, SmallPtrSetImpl<Instruction *> &DeadInstructions,
9252     const MapVector<Instruction *, Instruction *> &SinkAfter) {
9253 
9254   SmallPtrSet<const InterleaveGroup<Instruction> *, 1> InterleaveGroups;
9255 
9256   VPRecipeBuilder RecipeBuilder(OrigLoop, TLI, Legal, CM, PSE, Builder);
9257 
9258   // ---------------------------------------------------------------------------
9259   // Pre-construction: record ingredients whose recipes we'll need to further
9260   // process after constructing the initial VPlan.
9261   // ---------------------------------------------------------------------------
9262 
9263   // Mark instructions we'll need to sink later and their targets as
9264   // ingredients whose recipe we'll need to record.
9265   for (auto &Entry : SinkAfter) {
9266     RecipeBuilder.recordRecipeOf(Entry.first);
9267     RecipeBuilder.recordRecipeOf(Entry.second);
9268   }
9269   for (auto &Reduction : CM.getInLoopReductionChains()) {
9270     PHINode *Phi = Reduction.first;
9271     RecurKind Kind = Legal->getReductionVars()[Phi].getRecurrenceKind();
9272     const SmallVector<Instruction *, 4> &ReductionOperations = Reduction.second;
9273 
9274     RecipeBuilder.recordRecipeOf(Phi);
9275     for (auto &R : ReductionOperations) {
9276       RecipeBuilder.recordRecipeOf(R);
9277       // For min/max reducitons, where we have a pair of icmp/select, we also
9278       // need to record the ICmp recipe, so it can be removed later.
9279       assert(!RecurrenceDescriptor::isSelectCmpRecurrenceKind(Kind) &&
9280              "Only min/max recurrences allowed for inloop reductions");
9281       if (RecurrenceDescriptor::isMinMaxRecurrenceKind(Kind))
9282         RecipeBuilder.recordRecipeOf(cast<Instruction>(R->getOperand(0)));
9283     }
9284   }
9285 
9286   // For each interleave group which is relevant for this (possibly trimmed)
9287   // Range, add it to the set of groups to be later applied to the VPlan and add
9288   // placeholders for its members' Recipes which we'll be replacing with a
9289   // single VPInterleaveRecipe.
9290   for (InterleaveGroup<Instruction> *IG : IAI.getInterleaveGroups()) {
9291     auto applyIG = [IG, this](ElementCount VF) -> bool {
9292       return (VF.isVector() && // Query is illegal for VF == 1
9293               CM.getWideningDecision(IG->getInsertPos(), VF) ==
9294                   LoopVectorizationCostModel::CM_Interleave);
9295     };
9296     if (!getDecisionAndClampRange(applyIG, Range))
9297       continue;
9298     InterleaveGroups.insert(IG);
9299     for (unsigned i = 0; i < IG->getFactor(); i++)
9300       if (Instruction *Member = IG->getMember(i))
9301         RecipeBuilder.recordRecipeOf(Member);
9302   };
9303 
9304   // ---------------------------------------------------------------------------
9305   // Build initial VPlan: Scan the body of the loop in a topological order to
9306   // visit each basic block after having visited its predecessor basic blocks.
9307   // ---------------------------------------------------------------------------
9308 
9309   // Create a dummy pre-entry VPBasicBlock to start building the VPlan.
9310   auto Plan = std::make_unique<VPlan>();
9311 
9312   // Scan the body of the loop in a topological order to visit each basic block
9313   // after having visited its predecessor basic blocks.
9314   LoopBlocksDFS DFS(OrigLoop);
9315   DFS.perform(LI);
9316 
9317   VPBasicBlock *VPBB = nullptr;
9318   for (BasicBlock *BB : make_range(DFS.beginRPO(), DFS.endRPO())) {
9319     // Relevant instructions from basic block BB will be grouped into VPRecipe
9320     // ingredients and fill a new VPBasicBlock.
9321     unsigned VPBBsForBB = 0;
9322     auto *FirstVPBBForBB = new VPBasicBlock(BB->getName());
9323     if (VPBB)
9324       VPBlockUtils::insertBlockAfter(FirstVPBBForBB, VPBB);
9325     else
9326       Plan->setEntry(FirstVPBBForBB);
9327     VPBB = FirstVPBBForBB;
9328     Builder.setInsertPoint(VPBB);
9329 
9330     // Introduce each ingredient into VPlan.
9331     // TODO: Model and preserve debug instrinsics in VPlan.
9332     for (Instruction &I : BB->instructionsWithoutDebug()) {
9333       Instruction *Instr = &I;
9334 
9335       // First filter out irrelevant instructions, to ensure no recipes are
9336       // built for them.
9337       if (isa<BranchInst>(Instr) || DeadInstructions.count(Instr))
9338         continue;
9339 
9340       SmallVector<VPValue *, 4> Operands;
9341       auto *Phi = dyn_cast<PHINode>(Instr);
9342       if (Phi && Phi->getParent() == OrigLoop->getHeader()) {
9343         Operands.push_back(Plan->getOrAddVPValue(
9344             Phi->getIncomingValueForBlock(OrigLoop->getLoopPreheader())));
9345       } else {
9346         auto OpRange = Plan->mapToVPValues(Instr->operands());
9347         Operands = {OpRange.begin(), OpRange.end()};
9348       }
9349       if (auto RecipeOrValue = RecipeBuilder.tryToCreateWidenRecipe(
9350               Instr, Operands, Range, Plan)) {
9351         // If Instr can be simplified to an existing VPValue, use it.
9352         if (RecipeOrValue.is<VPValue *>()) {
9353           auto *VPV = RecipeOrValue.get<VPValue *>();
9354           Plan->addVPValue(Instr, VPV);
9355           // If the re-used value is a recipe, register the recipe for the
9356           // instruction, in case the recipe for Instr needs to be recorded.
9357           if (auto *R = dyn_cast_or_null<VPRecipeBase>(VPV->getDef()))
9358             RecipeBuilder.setRecipe(Instr, R);
9359           continue;
9360         }
9361         // Otherwise, add the new recipe.
9362         VPRecipeBase *Recipe = RecipeOrValue.get<VPRecipeBase *>();
9363         for (auto *Def : Recipe->definedValues()) {
9364           auto *UV = Def->getUnderlyingValue();
9365           Plan->addVPValue(UV, Def);
9366         }
9367 
9368         RecipeBuilder.setRecipe(Instr, Recipe);
9369         if (isa<VPWidenIntOrFpInductionRecipe>(Recipe)) {
9370           // Make sure induction recipes are all kept in the header block.
9371           // VPWidenIntOrFpInductionRecipe may be generated when reaching a
9372           // Trunc of an induction Phi, where Trunc may not be in the header.
9373           auto *Header = Plan->getEntry()->getEntryBasicBlock();
9374           Header->insert(Recipe, Header->getFirstNonPhi());
9375         } else
9376           VPBB->appendRecipe(Recipe);
9377         continue;
9378       }
9379 
9380       // Otherwise, if all widening options failed, Instruction is to be
9381       // replicated. This may create a successor for VPBB.
9382       VPBasicBlock *NextVPBB =
9383           RecipeBuilder.handleReplication(Instr, Range, VPBB, Plan);
9384       if (NextVPBB != VPBB) {
9385         VPBB = NextVPBB;
9386         VPBB->setName(BB->hasName() ? BB->getName() + "." + Twine(VPBBsForBB++)
9387                                     : "");
9388       }
9389     }
9390   }
9391 
9392   assert(isa<VPBasicBlock>(Plan->getEntry()) &&
9393          !Plan->getEntry()->getEntryBasicBlock()->empty() &&
9394          "entry block must be set to a non-empty VPBasicBlock");
9395   RecipeBuilder.fixHeaderPhis();
9396 
9397   // ---------------------------------------------------------------------------
9398   // Transform initial VPlan: Apply previously taken decisions, in order, to
9399   // bring the VPlan to its final state.
9400   // ---------------------------------------------------------------------------
9401 
9402   // Apply Sink-After legal constraints.
9403   auto GetReplicateRegion = [](VPRecipeBase *R) -> VPRegionBlock * {
9404     auto *Region = dyn_cast_or_null<VPRegionBlock>(R->getParent()->getParent());
9405     if (Region && Region->isReplicator()) {
9406       assert(Region->getNumSuccessors() == 1 &&
9407              Region->getNumPredecessors() == 1 && "Expected SESE region!");
9408       assert(R->getParent()->size() == 1 &&
9409              "A recipe in an original replicator region must be the only "
9410              "recipe in its block");
9411       return Region;
9412     }
9413     return nullptr;
9414   };
9415   for (auto &Entry : SinkAfter) {
9416     VPRecipeBase *Sink = RecipeBuilder.getRecipe(Entry.first);
9417     VPRecipeBase *Target = RecipeBuilder.getRecipe(Entry.second);
9418 
9419     auto *TargetRegion = GetReplicateRegion(Target);
9420     auto *SinkRegion = GetReplicateRegion(Sink);
9421     if (!SinkRegion) {
9422       // If the sink source is not a replicate region, sink the recipe directly.
9423       if (TargetRegion) {
9424         // The target is in a replication region, make sure to move Sink to
9425         // the block after it, not into the replication region itself.
9426         VPBasicBlock *NextBlock =
9427             cast<VPBasicBlock>(TargetRegion->getSuccessors().front());
9428         Sink->moveBefore(*NextBlock, NextBlock->getFirstNonPhi());
9429       } else
9430         Sink->moveAfter(Target);
9431       continue;
9432     }
9433 
9434     // The sink source is in a replicate region. Unhook the region from the CFG.
9435     auto *SinkPred = SinkRegion->getSinglePredecessor();
9436     auto *SinkSucc = SinkRegion->getSingleSuccessor();
9437     VPBlockUtils::disconnectBlocks(SinkPred, SinkRegion);
9438     VPBlockUtils::disconnectBlocks(SinkRegion, SinkSucc);
9439     VPBlockUtils::connectBlocks(SinkPred, SinkSucc);
9440 
9441     if (TargetRegion) {
9442       // The target recipe is also in a replicate region, move the sink region
9443       // after the target region.
9444       auto *TargetSucc = TargetRegion->getSingleSuccessor();
9445       VPBlockUtils::disconnectBlocks(TargetRegion, TargetSucc);
9446       VPBlockUtils::connectBlocks(TargetRegion, SinkRegion);
9447       VPBlockUtils::connectBlocks(SinkRegion, TargetSucc);
9448     } else {
9449       // The sink source is in a replicate region, we need to move the whole
9450       // replicate region, which should only contain a single recipe in the
9451       // main block.
9452       auto *SplitBlock =
9453           Target->getParent()->splitAt(std::next(Target->getIterator()));
9454 
9455       auto *SplitPred = SplitBlock->getSinglePredecessor();
9456 
9457       VPBlockUtils::disconnectBlocks(SplitPred, SplitBlock);
9458       VPBlockUtils::connectBlocks(SplitPred, SinkRegion);
9459       VPBlockUtils::connectBlocks(SinkRegion, SplitBlock);
9460       if (VPBB == SplitPred)
9461         VPBB = SplitBlock;
9462     }
9463   }
9464 
9465   // Adjust the recipes for any inloop reductions.
9466   adjustRecipesForReductions(VPBB, Plan, RecipeBuilder, Range.Start);
9467 
9468   // Introduce a recipe to combine the incoming and previous values of a
9469   // first-order recurrence.
9470   for (VPRecipeBase &R : Plan->getEntry()->getEntryBasicBlock()->phis()) {
9471     auto *RecurPhi = dyn_cast<VPFirstOrderRecurrencePHIRecipe>(&R);
9472     if (!RecurPhi)
9473       continue;
9474 
9475     auto *RecurSplice = cast<VPInstruction>(
9476         Builder.createNaryOp(VPInstruction::FirstOrderRecurrenceSplice,
9477                              {RecurPhi, RecurPhi->getBackedgeValue()}));
9478 
9479     VPRecipeBase *PrevRecipe = RecurPhi->getBackedgeRecipe();
9480     if (auto *Region = GetReplicateRegion(PrevRecipe)) {
9481       VPBasicBlock *Succ = cast<VPBasicBlock>(Region->getSingleSuccessor());
9482       RecurSplice->moveBefore(*Succ, Succ->getFirstNonPhi());
9483     } else
9484       RecurSplice->moveAfter(PrevRecipe);
9485     RecurPhi->replaceAllUsesWith(RecurSplice);
9486     // Set the first operand of RecurSplice to RecurPhi again, after replacing
9487     // all users.
9488     RecurSplice->setOperand(0, RecurPhi);
9489   }
9490 
9491   // Interleave memory: for each Interleave Group we marked earlier as relevant
9492   // for this VPlan, replace the Recipes widening its memory instructions with a
9493   // single VPInterleaveRecipe at its insertion point.
9494   for (auto IG : InterleaveGroups) {
9495     auto *Recipe = cast<VPWidenMemoryInstructionRecipe>(
9496         RecipeBuilder.getRecipe(IG->getInsertPos()));
9497     SmallVector<VPValue *, 4> StoredValues;
9498     for (unsigned i = 0; i < IG->getFactor(); ++i)
9499       if (auto *SI = dyn_cast_or_null<StoreInst>(IG->getMember(i))) {
9500         auto *StoreR =
9501             cast<VPWidenMemoryInstructionRecipe>(RecipeBuilder.getRecipe(SI));
9502         StoredValues.push_back(StoreR->getStoredValue());
9503       }
9504 
9505     auto *VPIG = new VPInterleaveRecipe(IG, Recipe->getAddr(), StoredValues,
9506                                         Recipe->getMask());
9507     VPIG->insertBefore(Recipe);
9508     unsigned J = 0;
9509     for (unsigned i = 0; i < IG->getFactor(); ++i)
9510       if (Instruction *Member = IG->getMember(i)) {
9511         if (!Member->getType()->isVoidTy()) {
9512           VPValue *OriginalV = Plan->getVPValue(Member);
9513           Plan->removeVPValueFor(Member);
9514           Plan->addVPValue(Member, VPIG->getVPValue(J));
9515           OriginalV->replaceAllUsesWith(VPIG->getVPValue(J));
9516           J++;
9517         }
9518         RecipeBuilder.getRecipe(Member)->eraseFromParent();
9519       }
9520   }
9521 
9522   // From this point onwards, VPlan-to-VPlan transformations may change the plan
9523   // in ways that accessing values using original IR values is incorrect.
9524   Plan->disableValue2VPValue();
9525 
9526   VPlanTransforms::sinkScalarOperands(*Plan);
9527   VPlanTransforms::mergeReplicateRegions(*Plan);
9528 
9529   std::string PlanName;
9530   raw_string_ostream RSO(PlanName);
9531   ElementCount VF = Range.Start;
9532   Plan->addVF(VF);
9533   RSO << "Initial VPlan for VF={" << VF;
9534   for (VF *= 2; ElementCount::isKnownLT(VF, Range.End); VF *= 2) {
9535     Plan->addVF(VF);
9536     RSO << "," << VF;
9537   }
9538   RSO << "},UF>=1";
9539   RSO.flush();
9540   Plan->setName(PlanName);
9541 
9542   return Plan;
9543 }
9544 
9545 VPlanPtr LoopVectorizationPlanner::buildVPlan(VFRange &Range) {
9546   // Outer loop handling: They may require CFG and instruction level
9547   // transformations before even evaluating whether vectorization is profitable.
9548   // Since we cannot modify the incoming IR, we need to build VPlan upfront in
9549   // the vectorization pipeline.
9550   assert(!OrigLoop->isInnermost());
9551   assert(EnableVPlanNativePath && "VPlan-native path is not enabled.");
9552 
9553   // Create new empty VPlan
9554   auto Plan = std::make_unique<VPlan>();
9555 
9556   // Build hierarchical CFG
9557   VPlanHCFGBuilder HCFGBuilder(OrigLoop, LI, *Plan);
9558   HCFGBuilder.buildHierarchicalCFG();
9559 
9560   for (ElementCount VF = Range.Start; ElementCount::isKnownLT(VF, Range.End);
9561        VF *= 2)
9562     Plan->addVF(VF);
9563 
9564   if (EnableVPlanPredication) {
9565     VPlanPredicator VPP(*Plan);
9566     VPP.predicate();
9567 
9568     // Avoid running transformation to recipes until masked code generation in
9569     // VPlan-native path is in place.
9570     return Plan;
9571   }
9572 
9573   SmallPtrSet<Instruction *, 1> DeadInstructions;
9574   VPlanTransforms::VPInstructionsToVPRecipes(OrigLoop, Plan,
9575                                              Legal->getInductionVars(),
9576                                              DeadInstructions, *PSE.getSE());
9577   return Plan;
9578 }
9579 
9580 // Adjust the recipes for reductions. For in-loop reductions the chain of
9581 // instructions leading from the loop exit instr to the phi need to be converted
9582 // to reductions, with one operand being vector and the other being the scalar
9583 // reduction chain. For other reductions, a select is introduced between the phi
9584 // and live-out recipes when folding the tail.
9585 void LoopVectorizationPlanner::adjustRecipesForReductions(
9586     VPBasicBlock *LatchVPBB, VPlanPtr &Plan, VPRecipeBuilder &RecipeBuilder,
9587     ElementCount MinVF) {
9588   for (auto &Reduction : CM.getInLoopReductionChains()) {
9589     PHINode *Phi = Reduction.first;
9590     RecurrenceDescriptor &RdxDesc = Legal->getReductionVars()[Phi];
9591     const SmallVector<Instruction *, 4> &ReductionOperations = Reduction.second;
9592 
9593     if (MinVF.isScalar() && !CM.useOrderedReductions(RdxDesc))
9594       continue;
9595 
9596     // ReductionOperations are orders top-down from the phi's use to the
9597     // LoopExitValue. We keep a track of the previous item (the Chain) to tell
9598     // which of the two operands will remain scalar and which will be reduced.
9599     // For minmax the chain will be the select instructions.
9600     Instruction *Chain = Phi;
9601     for (Instruction *R : ReductionOperations) {
9602       VPRecipeBase *WidenRecipe = RecipeBuilder.getRecipe(R);
9603       RecurKind Kind = RdxDesc.getRecurrenceKind();
9604 
9605       VPValue *ChainOp = Plan->getVPValue(Chain);
9606       unsigned FirstOpId;
9607       assert(!RecurrenceDescriptor::isSelectCmpRecurrenceKind(Kind) &&
9608              "Only min/max recurrences allowed for inloop reductions");
9609       if (RecurrenceDescriptor::isMinMaxRecurrenceKind(Kind)) {
9610         assert(isa<VPWidenSelectRecipe>(WidenRecipe) &&
9611                "Expected to replace a VPWidenSelectSC");
9612         FirstOpId = 1;
9613       } else {
9614         assert((MinVF.isScalar() || isa<VPWidenRecipe>(WidenRecipe)) &&
9615                "Expected to replace a VPWidenSC");
9616         FirstOpId = 0;
9617       }
9618       unsigned VecOpId =
9619           R->getOperand(FirstOpId) == Chain ? FirstOpId + 1 : FirstOpId;
9620       VPValue *VecOp = Plan->getVPValue(R->getOperand(VecOpId));
9621 
9622       auto *CondOp = CM.foldTailByMasking()
9623                          ? RecipeBuilder.createBlockInMask(R->getParent(), Plan)
9624                          : nullptr;
9625       VPReductionRecipe *RedRecipe = new VPReductionRecipe(
9626           &RdxDesc, R, ChainOp, VecOp, CondOp, TTI);
9627       WidenRecipe->getVPSingleValue()->replaceAllUsesWith(RedRecipe);
9628       Plan->removeVPValueFor(R);
9629       Plan->addVPValue(R, RedRecipe);
9630       WidenRecipe->getParent()->insert(RedRecipe, WidenRecipe->getIterator());
9631       WidenRecipe->getVPSingleValue()->replaceAllUsesWith(RedRecipe);
9632       WidenRecipe->eraseFromParent();
9633 
9634       if (RecurrenceDescriptor::isMinMaxRecurrenceKind(Kind)) {
9635         VPRecipeBase *CompareRecipe =
9636             RecipeBuilder.getRecipe(cast<Instruction>(R->getOperand(0)));
9637         assert(isa<VPWidenRecipe>(CompareRecipe) &&
9638                "Expected to replace a VPWidenSC");
9639         assert(cast<VPWidenRecipe>(CompareRecipe)->getNumUsers() == 0 &&
9640                "Expected no remaining users");
9641         CompareRecipe->eraseFromParent();
9642       }
9643       Chain = R;
9644     }
9645   }
9646 
9647   // If tail is folded by masking, introduce selects between the phi
9648   // and the live-out instruction of each reduction, at the end of the latch.
9649   if (CM.foldTailByMasking()) {
9650     for (VPRecipeBase &R : Plan->getEntry()->getEntryBasicBlock()->phis()) {
9651       VPReductionPHIRecipe *PhiR = dyn_cast<VPReductionPHIRecipe>(&R);
9652       if (!PhiR || PhiR->isInLoop())
9653         continue;
9654       Builder.setInsertPoint(LatchVPBB);
9655       VPValue *Cond =
9656           RecipeBuilder.createBlockInMask(OrigLoop->getHeader(), Plan);
9657       VPValue *Red = PhiR->getBackedgeValue();
9658       Builder.createNaryOp(Instruction::Select, {Cond, Red, PhiR});
9659     }
9660   }
9661 }
9662 
9663 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
9664 void VPInterleaveRecipe::print(raw_ostream &O, const Twine &Indent,
9665                                VPSlotTracker &SlotTracker) const {
9666   O << Indent << "INTERLEAVE-GROUP with factor " << IG->getFactor() << " at ";
9667   IG->getInsertPos()->printAsOperand(O, false);
9668   O << ", ";
9669   getAddr()->printAsOperand(O, SlotTracker);
9670   VPValue *Mask = getMask();
9671   if (Mask) {
9672     O << ", ";
9673     Mask->printAsOperand(O, SlotTracker);
9674   }
9675 
9676   unsigned OpIdx = 0;
9677   for (unsigned i = 0; i < IG->getFactor(); ++i) {
9678     if (!IG->getMember(i))
9679       continue;
9680     if (getNumStoreOperands() > 0) {
9681       O << "\n" << Indent << "  store ";
9682       getOperand(1 + OpIdx)->printAsOperand(O, SlotTracker);
9683       O << " to index " << i;
9684     } else {
9685       O << "\n" << Indent << "  ";
9686       getVPValue(OpIdx)->printAsOperand(O, SlotTracker);
9687       O << " = load from index " << i;
9688     }
9689     ++OpIdx;
9690   }
9691 }
9692 #endif
9693 
9694 void VPWidenCallRecipe::execute(VPTransformState &State) {
9695   State.ILV->widenCallInstruction(*cast<CallInst>(getUnderlyingInstr()), this,
9696                                   *this, State);
9697 }
9698 
9699 void VPWidenSelectRecipe::execute(VPTransformState &State) {
9700   State.ILV->widenSelectInstruction(*cast<SelectInst>(getUnderlyingInstr()),
9701                                     this, *this, InvariantCond, State);
9702 }
9703 
9704 void VPWidenRecipe::execute(VPTransformState &State) {
9705   State.ILV->widenInstruction(*getUnderlyingInstr(), this, *this, State);
9706 }
9707 
9708 void VPWidenGEPRecipe::execute(VPTransformState &State) {
9709   State.ILV->widenGEP(cast<GetElementPtrInst>(getUnderlyingInstr()), this,
9710                       *this, State.UF, State.VF, IsPtrLoopInvariant,
9711                       IsIndexLoopInvariant, State);
9712 }
9713 
9714 void VPWidenIntOrFpInductionRecipe::execute(VPTransformState &State) {
9715   assert(!State.Instance && "Int or FP induction being replicated.");
9716   State.ILV->widenIntOrFpInduction(IV, getStartValue()->getLiveInIRValue(),
9717                                    getTruncInst(), getVPValue(0),
9718                                    getCastValue(), State);
9719 }
9720 
9721 void VPWidenPHIRecipe::execute(VPTransformState &State) {
9722   State.ILV->widenPHIInstruction(cast<PHINode>(getUnderlyingValue()), this,
9723                                  State);
9724 }
9725 
9726 void VPBlendRecipe::execute(VPTransformState &State) {
9727   State.ILV->setDebugLocFromInst(Phi, &State.Builder);
9728   // We know that all PHIs in non-header blocks are converted into
9729   // selects, so we don't have to worry about the insertion order and we
9730   // can just use the builder.
9731   // At this point we generate the predication tree. There may be
9732   // duplications since this is a simple recursive scan, but future
9733   // optimizations will clean it up.
9734 
9735   unsigned NumIncoming = getNumIncomingValues();
9736 
9737   // Generate a sequence of selects of the form:
9738   // SELECT(Mask3, In3,
9739   //        SELECT(Mask2, In2,
9740   //               SELECT(Mask1, In1,
9741   //                      In0)))
9742   // Note that Mask0 is never used: lanes for which no path reaches this phi and
9743   // are essentially undef are taken from In0.
9744   InnerLoopVectorizer::VectorParts Entry(State.UF);
9745   for (unsigned In = 0; In < NumIncoming; ++In) {
9746     for (unsigned Part = 0; Part < State.UF; ++Part) {
9747       // We might have single edge PHIs (blocks) - use an identity
9748       // 'select' for the first PHI operand.
9749       Value *In0 = State.get(getIncomingValue(In), Part);
9750       if (In == 0)
9751         Entry[Part] = In0; // Initialize with the first incoming value.
9752       else {
9753         // Select between the current value and the previous incoming edge
9754         // based on the incoming mask.
9755         Value *Cond = State.get(getMask(In), Part);
9756         Entry[Part] =
9757             State.Builder.CreateSelect(Cond, In0, Entry[Part], "predphi");
9758       }
9759     }
9760   }
9761   for (unsigned Part = 0; Part < State.UF; ++Part)
9762     State.set(this, Entry[Part], Part);
9763 }
9764 
9765 void VPInterleaveRecipe::execute(VPTransformState &State) {
9766   assert(!State.Instance && "Interleave group being replicated.");
9767   State.ILV->vectorizeInterleaveGroup(IG, definedValues(), State, getAddr(),
9768                                       getStoredValues(), getMask());
9769 }
9770 
9771 void VPReductionRecipe::execute(VPTransformState &State) {
9772   assert(!State.Instance && "Reduction being replicated.");
9773   Value *PrevInChain = State.get(getChainOp(), 0);
9774   RecurKind Kind = RdxDesc->getRecurrenceKind();
9775   bool IsOrdered = State.ILV->useOrderedReductions(*RdxDesc);
9776   // Propagate the fast-math flags carried by the underlying instruction.
9777   IRBuilderBase::FastMathFlagGuard FMFGuard(State.Builder);
9778   State.Builder.setFastMathFlags(RdxDesc->getFastMathFlags());
9779   for (unsigned Part = 0; Part < State.UF; ++Part) {
9780     Value *NewVecOp = State.get(getVecOp(), Part);
9781     if (VPValue *Cond = getCondOp()) {
9782       Value *NewCond = State.get(Cond, Part);
9783       VectorType *VecTy = cast<VectorType>(NewVecOp->getType());
9784       Value *Iden = RdxDesc->getRecurrenceIdentity(
9785           Kind, VecTy->getElementType(), RdxDesc->getFastMathFlags());
9786       Value *IdenVec =
9787           State.Builder.CreateVectorSplat(VecTy->getElementCount(), Iden);
9788       Value *Select = State.Builder.CreateSelect(NewCond, NewVecOp, IdenVec);
9789       NewVecOp = Select;
9790     }
9791     Value *NewRed;
9792     Value *NextInChain;
9793     if (IsOrdered) {
9794       if (State.VF.isVector())
9795         NewRed = createOrderedReduction(State.Builder, *RdxDesc, NewVecOp,
9796                                         PrevInChain);
9797       else
9798         NewRed = State.Builder.CreateBinOp(
9799             (Instruction::BinaryOps)RdxDesc->getOpcode(Kind), PrevInChain,
9800             NewVecOp);
9801       PrevInChain = NewRed;
9802     } else {
9803       PrevInChain = State.get(getChainOp(), Part);
9804       NewRed = createTargetReduction(State.Builder, TTI, *RdxDesc, NewVecOp);
9805     }
9806     if (RecurrenceDescriptor::isMinMaxRecurrenceKind(Kind)) {
9807       NextInChain =
9808           createMinMaxOp(State.Builder, RdxDesc->getRecurrenceKind(),
9809                          NewRed, PrevInChain);
9810     } else if (IsOrdered)
9811       NextInChain = NewRed;
9812     else
9813       NextInChain = State.Builder.CreateBinOp(
9814           (Instruction::BinaryOps)RdxDesc->getOpcode(Kind), NewRed,
9815           PrevInChain);
9816     State.set(this, NextInChain, Part);
9817   }
9818 }
9819 
9820 void VPReplicateRecipe::execute(VPTransformState &State) {
9821   if (State.Instance) { // Generate a single instance.
9822     assert(!State.VF.isScalable() && "Can't scalarize a scalable vector");
9823     State.ILV->scalarizeInstruction(getUnderlyingInstr(), this, *this,
9824                                     *State.Instance, IsPredicated, State);
9825     // Insert scalar instance packing it into a vector.
9826     if (AlsoPack && State.VF.isVector()) {
9827       // If we're constructing lane 0, initialize to start from poison.
9828       if (State.Instance->Lane.isFirstLane()) {
9829         assert(!State.VF.isScalable() && "VF is assumed to be non scalable.");
9830         Value *Poison = PoisonValue::get(
9831             VectorType::get(getUnderlyingValue()->getType(), State.VF));
9832         State.set(this, Poison, State.Instance->Part);
9833       }
9834       State.ILV->packScalarIntoVectorValue(this, *State.Instance, State);
9835     }
9836     return;
9837   }
9838 
9839   // Generate scalar instances for all VF lanes of all UF parts, unless the
9840   // instruction is uniform inwhich case generate only the first lane for each
9841   // of the UF parts.
9842   unsigned EndLane = IsUniform ? 1 : State.VF.getKnownMinValue();
9843   assert((!State.VF.isScalable() || IsUniform) &&
9844          "Can't scalarize a scalable vector");
9845   for (unsigned Part = 0; Part < State.UF; ++Part)
9846     for (unsigned Lane = 0; Lane < EndLane; ++Lane)
9847       State.ILV->scalarizeInstruction(getUnderlyingInstr(), this, *this,
9848                                       VPIteration(Part, Lane), IsPredicated,
9849                                       State);
9850 }
9851 
9852 void VPBranchOnMaskRecipe::execute(VPTransformState &State) {
9853   assert(State.Instance && "Branch on Mask works only on single instance.");
9854 
9855   unsigned Part = State.Instance->Part;
9856   unsigned Lane = State.Instance->Lane.getKnownLane();
9857 
9858   Value *ConditionBit = nullptr;
9859   VPValue *BlockInMask = getMask();
9860   if (BlockInMask) {
9861     ConditionBit = State.get(BlockInMask, Part);
9862     if (ConditionBit->getType()->isVectorTy())
9863       ConditionBit = State.Builder.CreateExtractElement(
9864           ConditionBit, State.Builder.getInt32(Lane));
9865   } else // Block in mask is all-one.
9866     ConditionBit = State.Builder.getTrue();
9867 
9868   // Replace the temporary unreachable terminator with a new conditional branch,
9869   // whose two destinations will be set later when they are created.
9870   auto *CurrentTerminator = State.CFG.PrevBB->getTerminator();
9871   assert(isa<UnreachableInst>(CurrentTerminator) &&
9872          "Expected to replace unreachable terminator with conditional branch.");
9873   auto *CondBr = BranchInst::Create(State.CFG.PrevBB, nullptr, ConditionBit);
9874   CondBr->setSuccessor(0, nullptr);
9875   ReplaceInstWithInst(CurrentTerminator, CondBr);
9876 }
9877 
9878 void VPPredInstPHIRecipe::execute(VPTransformState &State) {
9879   assert(State.Instance && "Predicated instruction PHI works per instance.");
9880   Instruction *ScalarPredInst =
9881       cast<Instruction>(State.get(getOperand(0), *State.Instance));
9882   BasicBlock *PredicatedBB = ScalarPredInst->getParent();
9883   BasicBlock *PredicatingBB = PredicatedBB->getSinglePredecessor();
9884   assert(PredicatingBB && "Predicated block has no single predecessor.");
9885   assert(isa<VPReplicateRecipe>(getOperand(0)) &&
9886          "operand must be VPReplicateRecipe");
9887 
9888   // By current pack/unpack logic we need to generate only a single phi node: if
9889   // a vector value for the predicated instruction exists at this point it means
9890   // the instruction has vector users only, and a phi for the vector value is
9891   // needed. In this case the recipe of the predicated instruction is marked to
9892   // also do that packing, thereby "hoisting" the insert-element sequence.
9893   // Otherwise, a phi node for the scalar value is needed.
9894   unsigned Part = State.Instance->Part;
9895   if (State.hasVectorValue(getOperand(0), Part)) {
9896     Value *VectorValue = State.get(getOperand(0), Part);
9897     InsertElementInst *IEI = cast<InsertElementInst>(VectorValue);
9898     PHINode *VPhi = State.Builder.CreatePHI(IEI->getType(), 2);
9899     VPhi->addIncoming(IEI->getOperand(0), PredicatingBB); // Unmodified vector.
9900     VPhi->addIncoming(IEI, PredicatedBB); // New vector with inserted element.
9901     if (State.hasVectorValue(this, Part))
9902       State.reset(this, VPhi, Part);
9903     else
9904       State.set(this, VPhi, Part);
9905     // NOTE: Currently we need to update the value of the operand, so the next
9906     // predicated iteration inserts its generated value in the correct vector.
9907     State.reset(getOperand(0), VPhi, Part);
9908   } else {
9909     Type *PredInstType = getOperand(0)->getUnderlyingValue()->getType();
9910     PHINode *Phi = State.Builder.CreatePHI(PredInstType, 2);
9911     Phi->addIncoming(PoisonValue::get(ScalarPredInst->getType()),
9912                      PredicatingBB);
9913     Phi->addIncoming(ScalarPredInst, PredicatedBB);
9914     if (State.hasScalarValue(this, *State.Instance))
9915       State.reset(this, Phi, *State.Instance);
9916     else
9917       State.set(this, Phi, *State.Instance);
9918     // NOTE: Currently we need to update the value of the operand, so the next
9919     // predicated iteration inserts its generated value in the correct vector.
9920     State.reset(getOperand(0), Phi, *State.Instance);
9921   }
9922 }
9923 
9924 void VPWidenMemoryInstructionRecipe::execute(VPTransformState &State) {
9925   VPValue *StoredValue = isStore() ? getStoredValue() : nullptr;
9926   State.ILV->vectorizeMemoryInstruction(
9927       &Ingredient, State, StoredValue ? nullptr : getVPSingleValue(), getAddr(),
9928       StoredValue, getMask(), Consecutive, Reverse);
9929 }
9930 
9931 // Determine how to lower the scalar epilogue, which depends on 1) optimising
9932 // for minimum code-size, 2) predicate compiler options, 3) loop hints forcing
9933 // predication, and 4) a TTI hook that analyses whether the loop is suitable
9934 // for predication.
9935 static ScalarEpilogueLowering getScalarEpilogueLowering(
9936     Function *F, Loop *L, LoopVectorizeHints &Hints, ProfileSummaryInfo *PSI,
9937     BlockFrequencyInfo *BFI, TargetTransformInfo *TTI, TargetLibraryInfo *TLI,
9938     AssumptionCache *AC, LoopInfo *LI, ScalarEvolution *SE, DominatorTree *DT,
9939     LoopVectorizationLegality &LVL) {
9940   // 1) OptSize takes precedence over all other options, i.e. if this is set,
9941   // don't look at hints or options, and don't request a scalar epilogue.
9942   // (For PGSO, as shouldOptimizeForSize isn't currently accessible from
9943   // LoopAccessInfo (due to code dependency and not being able to reliably get
9944   // PSI/BFI from a loop analysis under NPM), we cannot suppress the collection
9945   // of strides in LoopAccessInfo::analyzeLoop() and vectorize without
9946   // versioning when the vectorization is forced, unlike hasOptSize. So revert
9947   // back to the old way and vectorize with versioning when forced. See D81345.)
9948   if (F->hasOptSize() || (llvm::shouldOptimizeForSize(L->getHeader(), PSI, BFI,
9949                                                       PGSOQueryType::IRPass) &&
9950                           Hints.getForce() != LoopVectorizeHints::FK_Enabled))
9951     return CM_ScalarEpilogueNotAllowedOptSize;
9952 
9953   // 2) If set, obey the directives
9954   if (PreferPredicateOverEpilogue.getNumOccurrences()) {
9955     switch (PreferPredicateOverEpilogue) {
9956     case PreferPredicateTy::ScalarEpilogue:
9957       return CM_ScalarEpilogueAllowed;
9958     case PreferPredicateTy::PredicateElseScalarEpilogue:
9959       return CM_ScalarEpilogueNotNeededUsePredicate;
9960     case PreferPredicateTy::PredicateOrDontVectorize:
9961       return CM_ScalarEpilogueNotAllowedUsePredicate;
9962     };
9963   }
9964 
9965   // 3) If set, obey the hints
9966   switch (Hints.getPredicate()) {
9967   case LoopVectorizeHints::FK_Enabled:
9968     return CM_ScalarEpilogueNotNeededUsePredicate;
9969   case LoopVectorizeHints::FK_Disabled:
9970     return CM_ScalarEpilogueAllowed;
9971   };
9972 
9973   // 4) if the TTI hook indicates this is profitable, request predication.
9974   if (TTI->preferPredicateOverEpilogue(L, LI, *SE, *AC, TLI, DT,
9975                                        LVL.getLAI()))
9976     return CM_ScalarEpilogueNotNeededUsePredicate;
9977 
9978   return CM_ScalarEpilogueAllowed;
9979 }
9980 
9981 Value *VPTransformState::get(VPValue *Def, unsigned Part) {
9982   // If Values have been set for this Def return the one relevant for \p Part.
9983   if (hasVectorValue(Def, Part))
9984     return Data.PerPartOutput[Def][Part];
9985 
9986   if (!hasScalarValue(Def, {Part, 0})) {
9987     Value *IRV = Def->getLiveInIRValue();
9988     Value *B = ILV->getBroadcastInstrs(IRV);
9989     set(Def, B, Part);
9990     return B;
9991   }
9992 
9993   Value *ScalarValue = get(Def, {Part, 0});
9994   // If we aren't vectorizing, we can just copy the scalar map values over
9995   // to the vector map.
9996   if (VF.isScalar()) {
9997     set(Def, ScalarValue, Part);
9998     return ScalarValue;
9999   }
10000 
10001   auto *RepR = dyn_cast<VPReplicateRecipe>(Def);
10002   bool IsUniform = RepR && RepR->isUniform();
10003 
10004   unsigned LastLane = IsUniform ? 0 : VF.getKnownMinValue() - 1;
10005   // Check if there is a scalar value for the selected lane.
10006   if (!hasScalarValue(Def, {Part, LastLane})) {
10007     // At the moment, VPWidenIntOrFpInductionRecipes can also be uniform.
10008     assert(isa<VPWidenIntOrFpInductionRecipe>(Def->getDef()) &&
10009            "unexpected recipe found to be invariant");
10010     IsUniform = true;
10011     LastLane = 0;
10012   }
10013 
10014   auto *LastInst = cast<Instruction>(get(Def, {Part, LastLane}));
10015   // Set the insert point after the last scalarized instruction or after the
10016   // last PHI, if LastInst is a PHI. This ensures the insertelement sequence
10017   // will directly follow the scalar definitions.
10018   auto OldIP = Builder.saveIP();
10019   auto NewIP =
10020       isa<PHINode>(LastInst)
10021           ? BasicBlock::iterator(LastInst->getParent()->getFirstNonPHI())
10022           : std::next(BasicBlock::iterator(LastInst));
10023   Builder.SetInsertPoint(&*NewIP);
10024 
10025   // However, if we are vectorizing, we need to construct the vector values.
10026   // If the value is known to be uniform after vectorization, we can just
10027   // broadcast the scalar value corresponding to lane zero for each unroll
10028   // iteration. Otherwise, we construct the vector values using
10029   // insertelement instructions. Since the resulting vectors are stored in
10030   // State, we will only generate the insertelements once.
10031   Value *VectorValue = nullptr;
10032   if (IsUniform) {
10033     VectorValue = ILV->getBroadcastInstrs(ScalarValue);
10034     set(Def, VectorValue, Part);
10035   } else {
10036     // Initialize packing with insertelements to start from undef.
10037     assert(!VF.isScalable() && "VF is assumed to be non scalable.");
10038     Value *Undef = PoisonValue::get(VectorType::get(LastInst->getType(), VF));
10039     set(Def, Undef, Part);
10040     for (unsigned Lane = 0; Lane < VF.getKnownMinValue(); ++Lane)
10041       ILV->packScalarIntoVectorValue(Def, {Part, Lane}, *this);
10042     VectorValue = get(Def, Part);
10043   }
10044   Builder.restoreIP(OldIP);
10045   return VectorValue;
10046 }
10047 
10048 // Process the loop in the VPlan-native vectorization path. This path builds
10049 // VPlan upfront in the vectorization pipeline, which allows to apply
10050 // VPlan-to-VPlan transformations from the very beginning without modifying the
10051 // input LLVM IR.
10052 static bool processLoopInVPlanNativePath(
10053     Loop *L, PredicatedScalarEvolution &PSE, LoopInfo *LI, DominatorTree *DT,
10054     LoopVectorizationLegality *LVL, TargetTransformInfo *TTI,
10055     TargetLibraryInfo *TLI, DemandedBits *DB, AssumptionCache *AC,
10056     OptimizationRemarkEmitter *ORE, BlockFrequencyInfo *BFI,
10057     ProfileSummaryInfo *PSI, LoopVectorizeHints &Hints,
10058     LoopVectorizationRequirements &Requirements) {
10059 
10060   if (isa<SCEVCouldNotCompute>(PSE.getBackedgeTakenCount())) {
10061     LLVM_DEBUG(dbgs() << "LV: cannot compute the outer-loop trip count\n");
10062     return false;
10063   }
10064   assert(EnableVPlanNativePath && "VPlan-native path is disabled.");
10065   Function *F = L->getHeader()->getParent();
10066   InterleavedAccessInfo IAI(PSE, L, DT, LI, LVL->getLAI());
10067 
10068   ScalarEpilogueLowering SEL = getScalarEpilogueLowering(
10069       F, L, Hints, PSI, BFI, TTI, TLI, AC, LI, PSE.getSE(), DT, *LVL);
10070 
10071   LoopVectorizationCostModel CM(SEL, L, PSE, LI, LVL, *TTI, TLI, DB, AC, ORE, F,
10072                                 &Hints, IAI);
10073   // Use the planner for outer loop vectorization.
10074   // TODO: CM is not used at this point inside the planner. Turn CM into an
10075   // optional argument if we don't need it in the future.
10076   LoopVectorizationPlanner LVP(L, LI, TLI, TTI, LVL, CM, IAI, PSE, Hints,
10077                                Requirements, ORE);
10078 
10079   // Get user vectorization factor.
10080   ElementCount UserVF = Hints.getWidth();
10081 
10082   CM.collectElementTypesForWidening();
10083 
10084   // Plan how to best vectorize, return the best VF and its cost.
10085   const VectorizationFactor VF = LVP.planInVPlanNativePath(UserVF);
10086 
10087   // If we are stress testing VPlan builds, do not attempt to generate vector
10088   // code. Masked vector code generation support will follow soon.
10089   // Also, do not attempt to vectorize if no vector code will be produced.
10090   if (VPlanBuildStressTest || EnableVPlanPredication ||
10091       VectorizationFactor::Disabled() == VF)
10092     return false;
10093 
10094   VPlan &BestPlan = LVP.getBestPlanFor(VF.Width);
10095 
10096   {
10097     GeneratedRTChecks Checks(*PSE.getSE(), DT, LI,
10098                              F->getParent()->getDataLayout());
10099     InnerLoopVectorizer LB(L, PSE, LI, DT, TLI, TTI, AC, ORE, VF.Width, 1, LVL,
10100                            &CM, BFI, PSI, Checks);
10101     LLVM_DEBUG(dbgs() << "Vectorizing outer loop in \""
10102                       << L->getHeader()->getParent()->getName() << "\"\n");
10103     LVP.executePlan(VF.Width, 1, BestPlan, LB, DT);
10104   }
10105 
10106   // Mark the loop as already vectorized to avoid vectorizing again.
10107   Hints.setAlreadyVectorized();
10108   assert(!verifyFunction(*L->getHeader()->getParent(), &dbgs()));
10109   return true;
10110 }
10111 
10112 // Emit a remark if there are stores to floats that required a floating point
10113 // extension. If the vectorized loop was generated with floating point there
10114 // will be a performance penalty from the conversion overhead and the change in
10115 // the vector width.
10116 static void checkMixedPrecision(Loop *L, OptimizationRemarkEmitter *ORE) {
10117   SmallVector<Instruction *, 4> Worklist;
10118   for (BasicBlock *BB : L->getBlocks()) {
10119     for (Instruction &Inst : *BB) {
10120       if (auto *S = dyn_cast<StoreInst>(&Inst)) {
10121         if (S->getValueOperand()->getType()->isFloatTy())
10122           Worklist.push_back(S);
10123       }
10124     }
10125   }
10126 
10127   // Traverse the floating point stores upwards searching, for floating point
10128   // conversions.
10129   SmallPtrSet<const Instruction *, 4> Visited;
10130   SmallPtrSet<const Instruction *, 4> EmittedRemark;
10131   while (!Worklist.empty()) {
10132     auto *I = Worklist.pop_back_val();
10133     if (!L->contains(I))
10134       continue;
10135     if (!Visited.insert(I).second)
10136       continue;
10137 
10138     // Emit a remark if the floating point store required a floating
10139     // point conversion.
10140     // TODO: More work could be done to identify the root cause such as a
10141     // constant or a function return type and point the user to it.
10142     if (isa<FPExtInst>(I) && EmittedRemark.insert(I).second)
10143       ORE->emit([&]() {
10144         return OptimizationRemarkAnalysis(LV_NAME, "VectorMixedPrecision",
10145                                           I->getDebugLoc(), L->getHeader())
10146                << "floating point conversion changes vector width. "
10147                << "Mixed floating point precision requires an up/down "
10148                << "cast that will negatively impact performance.";
10149       });
10150 
10151     for (Use &Op : I->operands())
10152       if (auto *OpI = dyn_cast<Instruction>(Op))
10153         Worklist.push_back(OpI);
10154   }
10155 }
10156 
10157 LoopVectorizePass::LoopVectorizePass(LoopVectorizeOptions Opts)
10158     : InterleaveOnlyWhenForced(Opts.InterleaveOnlyWhenForced ||
10159                                !EnableLoopInterleaving),
10160       VectorizeOnlyWhenForced(Opts.VectorizeOnlyWhenForced ||
10161                               !EnableLoopVectorization) {}
10162 
10163 bool LoopVectorizePass::processLoop(Loop *L) {
10164   assert((EnableVPlanNativePath || L->isInnermost()) &&
10165          "VPlan-native path is not enabled. Only process inner loops.");
10166 
10167 #ifndef NDEBUG
10168   const std::string DebugLocStr = getDebugLocString(L);
10169 #endif /* NDEBUG */
10170 
10171   LLVM_DEBUG(dbgs() << "\nLV: Checking a loop in \""
10172                     << L->getHeader()->getParent()->getName() << "\" from "
10173                     << DebugLocStr << "\n");
10174 
10175   LoopVectorizeHints Hints(L, InterleaveOnlyWhenForced, *ORE);
10176 
10177   LLVM_DEBUG(
10178       dbgs() << "LV: Loop hints:"
10179              << " force="
10180              << (Hints.getForce() == LoopVectorizeHints::FK_Disabled
10181                      ? "disabled"
10182                      : (Hints.getForce() == LoopVectorizeHints::FK_Enabled
10183                             ? "enabled"
10184                             : "?"))
10185              << " width=" << Hints.getWidth()
10186              << " interleave=" << Hints.getInterleave() << "\n");
10187 
10188   // Function containing loop
10189   Function *F = L->getHeader()->getParent();
10190 
10191   // Looking at the diagnostic output is the only way to determine if a loop
10192   // was vectorized (other than looking at the IR or machine code), so it
10193   // is important to generate an optimization remark for each loop. Most of
10194   // these messages are generated as OptimizationRemarkAnalysis. Remarks
10195   // generated as OptimizationRemark and OptimizationRemarkMissed are
10196   // less verbose reporting vectorized loops and unvectorized loops that may
10197   // benefit from vectorization, respectively.
10198 
10199   if (!Hints.allowVectorization(F, L, VectorizeOnlyWhenForced)) {
10200     LLVM_DEBUG(dbgs() << "LV: Loop hints prevent vectorization.\n");
10201     return false;
10202   }
10203 
10204   PredicatedScalarEvolution PSE(*SE, *L);
10205 
10206   // Check if it is legal to vectorize the loop.
10207   LoopVectorizationRequirements Requirements;
10208   LoopVectorizationLegality LVL(L, PSE, DT, TTI, TLI, AA, F, GetLAA, LI, ORE,
10209                                 &Requirements, &Hints, DB, AC, BFI, PSI);
10210   if (!LVL.canVectorize(EnableVPlanNativePath)) {
10211     LLVM_DEBUG(dbgs() << "LV: Not vectorizing: Cannot prove legality.\n");
10212     Hints.emitRemarkWithHints();
10213     return false;
10214   }
10215 
10216   // Check the function attributes and profiles to find out if this function
10217   // should be optimized for size.
10218   ScalarEpilogueLowering SEL = getScalarEpilogueLowering(
10219       F, L, Hints, PSI, BFI, TTI, TLI, AC, LI, PSE.getSE(), DT, LVL);
10220 
10221   // Entrance to the VPlan-native vectorization path. Outer loops are processed
10222   // here. They may require CFG and instruction level transformations before
10223   // even evaluating whether vectorization is profitable. Since we cannot modify
10224   // the incoming IR, we need to build VPlan upfront in the vectorization
10225   // pipeline.
10226   if (!L->isInnermost())
10227     return processLoopInVPlanNativePath(L, PSE, LI, DT, &LVL, TTI, TLI, DB, AC,
10228                                         ORE, BFI, PSI, Hints, Requirements);
10229 
10230   assert(L->isInnermost() && "Inner loop expected.");
10231 
10232   // Check the loop for a trip count threshold: vectorize loops with a tiny trip
10233   // count by optimizing for size, to minimize overheads.
10234   auto ExpectedTC = getSmallBestKnownTC(*SE, L);
10235   if (ExpectedTC && *ExpectedTC < TinyTripCountVectorThreshold) {
10236     LLVM_DEBUG(dbgs() << "LV: Found a loop with a very small trip count. "
10237                       << "This loop is worth vectorizing only if no scalar "
10238                       << "iteration overheads are incurred.");
10239     if (Hints.getForce() == LoopVectorizeHints::FK_Enabled)
10240       LLVM_DEBUG(dbgs() << " But vectorizing was explicitly forced.\n");
10241     else {
10242       LLVM_DEBUG(dbgs() << "\n");
10243       SEL = CM_ScalarEpilogueNotAllowedLowTripLoop;
10244     }
10245   }
10246 
10247   // Check the function attributes to see if implicit floats are allowed.
10248   // FIXME: This check doesn't seem possibly correct -- what if the loop is
10249   // an integer loop and the vector instructions selected are purely integer
10250   // vector instructions?
10251   if (F->hasFnAttribute(Attribute::NoImplicitFloat)) {
10252     reportVectorizationFailure(
10253         "Can't vectorize when the NoImplicitFloat attribute is used",
10254         "loop not vectorized due to NoImplicitFloat attribute",
10255         "NoImplicitFloat", ORE, L);
10256     Hints.emitRemarkWithHints();
10257     return false;
10258   }
10259 
10260   // Check if the target supports potentially unsafe FP vectorization.
10261   // FIXME: Add a check for the type of safety issue (denormal, signaling)
10262   // for the target we're vectorizing for, to make sure none of the
10263   // additional fp-math flags can help.
10264   if (Hints.isPotentiallyUnsafe() &&
10265       TTI->isFPVectorizationPotentiallyUnsafe()) {
10266     reportVectorizationFailure(
10267         "Potentially unsafe FP op prevents vectorization",
10268         "loop not vectorized due to unsafe FP support.",
10269         "UnsafeFP", ORE, L);
10270     Hints.emitRemarkWithHints();
10271     return false;
10272   }
10273 
10274   bool AllowOrderedReductions;
10275   // If the flag is set, use that instead and override the TTI behaviour.
10276   if (ForceOrderedReductions.getNumOccurrences() > 0)
10277     AllowOrderedReductions = ForceOrderedReductions;
10278   else
10279     AllowOrderedReductions = TTI->enableOrderedReductions();
10280   if (!LVL.canVectorizeFPMath(AllowOrderedReductions)) {
10281     ORE->emit([&]() {
10282       auto *ExactFPMathInst = Requirements.getExactFPInst();
10283       return OptimizationRemarkAnalysisFPCommute(DEBUG_TYPE, "CantReorderFPOps",
10284                                                  ExactFPMathInst->getDebugLoc(),
10285                                                  ExactFPMathInst->getParent())
10286              << "loop not vectorized: cannot prove it is safe to reorder "
10287                 "floating-point operations";
10288     });
10289     LLVM_DEBUG(dbgs() << "LV: loop not vectorized: cannot prove it is safe to "
10290                          "reorder floating-point operations\n");
10291     Hints.emitRemarkWithHints();
10292     return false;
10293   }
10294 
10295   bool UseInterleaved = TTI->enableInterleavedAccessVectorization();
10296   InterleavedAccessInfo IAI(PSE, L, DT, LI, LVL.getLAI());
10297 
10298   // If an override option has been passed in for interleaved accesses, use it.
10299   if (EnableInterleavedMemAccesses.getNumOccurrences() > 0)
10300     UseInterleaved = EnableInterleavedMemAccesses;
10301 
10302   // Analyze interleaved memory accesses.
10303   if (UseInterleaved) {
10304     IAI.analyzeInterleaving(useMaskedInterleavedAccesses(*TTI));
10305   }
10306 
10307   // Use the cost model.
10308   LoopVectorizationCostModel CM(SEL, L, PSE, LI, &LVL, *TTI, TLI, DB, AC, ORE,
10309                                 F, &Hints, IAI);
10310   CM.collectValuesToIgnore();
10311   CM.collectElementTypesForWidening();
10312 
10313   // Use the planner for vectorization.
10314   LoopVectorizationPlanner LVP(L, LI, TLI, TTI, &LVL, CM, IAI, PSE, Hints,
10315                                Requirements, ORE);
10316 
10317   // Get user vectorization factor and interleave count.
10318   ElementCount UserVF = Hints.getWidth();
10319   unsigned UserIC = Hints.getInterleave();
10320 
10321   // Plan how to best vectorize, return the best VF and its cost.
10322   Optional<VectorizationFactor> MaybeVF = LVP.plan(UserVF, UserIC);
10323 
10324   VectorizationFactor VF = VectorizationFactor::Disabled();
10325   unsigned IC = 1;
10326 
10327   if (MaybeVF) {
10328     VF = *MaybeVF;
10329     // Select the interleave count.
10330     IC = CM.selectInterleaveCount(VF.Width, *VF.Cost.getValue());
10331   }
10332 
10333   // Identify the diagnostic messages that should be produced.
10334   std::pair<StringRef, std::string> VecDiagMsg, IntDiagMsg;
10335   bool VectorizeLoop = true, InterleaveLoop = true;
10336   if (VF.Width.isScalar()) {
10337     LLVM_DEBUG(dbgs() << "LV: Vectorization is possible but not beneficial.\n");
10338     VecDiagMsg = std::make_pair(
10339         "VectorizationNotBeneficial",
10340         "the cost-model indicates that vectorization is not beneficial");
10341     VectorizeLoop = false;
10342   }
10343 
10344   if (!MaybeVF && UserIC > 1) {
10345     // Tell the user interleaving was avoided up-front, despite being explicitly
10346     // requested.
10347     LLVM_DEBUG(dbgs() << "LV: Ignoring UserIC, because vectorization and "
10348                          "interleaving should be avoided up front\n");
10349     IntDiagMsg = std::make_pair(
10350         "InterleavingAvoided",
10351         "Ignoring UserIC, because interleaving was avoided up front");
10352     InterleaveLoop = false;
10353   } else if (IC == 1 && UserIC <= 1) {
10354     // Tell the user interleaving is not beneficial.
10355     LLVM_DEBUG(dbgs() << "LV: Interleaving is not beneficial.\n");
10356     IntDiagMsg = std::make_pair(
10357         "InterleavingNotBeneficial",
10358         "the cost-model indicates that interleaving is not beneficial");
10359     InterleaveLoop = false;
10360     if (UserIC == 1) {
10361       IntDiagMsg.first = "InterleavingNotBeneficialAndDisabled";
10362       IntDiagMsg.second +=
10363           " and is explicitly disabled or interleave count is set to 1";
10364     }
10365   } else if (IC > 1 && UserIC == 1) {
10366     // Tell the user interleaving is beneficial, but it explicitly disabled.
10367     LLVM_DEBUG(
10368         dbgs() << "LV: Interleaving is beneficial but is explicitly disabled.");
10369     IntDiagMsg = std::make_pair(
10370         "InterleavingBeneficialButDisabled",
10371         "the cost-model indicates that interleaving is beneficial "
10372         "but is explicitly disabled or interleave count is set to 1");
10373     InterleaveLoop = false;
10374   }
10375 
10376   // Override IC if user provided an interleave count.
10377   IC = UserIC > 0 ? UserIC : IC;
10378 
10379   // Emit diagnostic messages, if any.
10380   const char *VAPassName = Hints.vectorizeAnalysisPassName();
10381   if (!VectorizeLoop && !InterleaveLoop) {
10382     // Do not vectorize or interleaving the loop.
10383     ORE->emit([&]() {
10384       return OptimizationRemarkMissed(VAPassName, VecDiagMsg.first,
10385                                       L->getStartLoc(), L->getHeader())
10386              << VecDiagMsg.second;
10387     });
10388     ORE->emit([&]() {
10389       return OptimizationRemarkMissed(LV_NAME, IntDiagMsg.first,
10390                                       L->getStartLoc(), L->getHeader())
10391              << IntDiagMsg.second;
10392     });
10393     return false;
10394   } else if (!VectorizeLoop && InterleaveLoop) {
10395     LLVM_DEBUG(dbgs() << "LV: Interleave Count is " << IC << '\n');
10396     ORE->emit([&]() {
10397       return OptimizationRemarkAnalysis(VAPassName, VecDiagMsg.first,
10398                                         L->getStartLoc(), L->getHeader())
10399              << VecDiagMsg.second;
10400     });
10401   } else if (VectorizeLoop && !InterleaveLoop) {
10402     LLVM_DEBUG(dbgs() << "LV: Found a vectorizable loop (" << VF.Width
10403                       << ") in " << DebugLocStr << '\n');
10404     ORE->emit([&]() {
10405       return OptimizationRemarkAnalysis(LV_NAME, IntDiagMsg.first,
10406                                         L->getStartLoc(), L->getHeader())
10407              << IntDiagMsg.second;
10408     });
10409   } else if (VectorizeLoop && InterleaveLoop) {
10410     LLVM_DEBUG(dbgs() << "LV: Found a vectorizable loop (" << VF.Width
10411                       << ") in " << DebugLocStr << '\n');
10412     LLVM_DEBUG(dbgs() << "LV: Interleave Count is " << IC << '\n');
10413   }
10414 
10415   bool DisableRuntimeUnroll = false;
10416   MDNode *OrigLoopID = L->getLoopID();
10417   {
10418     // Optimistically generate runtime checks. Drop them if they turn out to not
10419     // be profitable. Limit the scope of Checks, so the cleanup happens
10420     // immediately after vector codegeneration is done.
10421     GeneratedRTChecks Checks(*PSE.getSE(), DT, LI,
10422                              F->getParent()->getDataLayout());
10423     if (!VF.Width.isScalar() || IC > 1)
10424       Checks.Create(L, *LVL.getLAI(), PSE.getUnionPredicate());
10425     VPlan &BestPlan = LVP.getBestPlanFor(VF.Width);
10426 
10427     using namespace ore;
10428     if (!VectorizeLoop) {
10429       assert(IC > 1 && "interleave count should not be 1 or 0");
10430       // If we decided that it is not legal to vectorize the loop, then
10431       // interleave it.
10432       InnerLoopUnroller Unroller(L, PSE, LI, DT, TLI, TTI, AC, ORE, IC, &LVL,
10433                                  &CM, BFI, PSI, Checks);
10434       LVP.executePlan(VF.Width, IC, BestPlan, Unroller, DT);
10435 
10436       ORE->emit([&]() {
10437         return OptimizationRemark(LV_NAME, "Interleaved", L->getStartLoc(),
10438                                   L->getHeader())
10439                << "interleaved loop (interleaved count: "
10440                << NV("InterleaveCount", IC) << ")";
10441       });
10442     } else {
10443       // If we decided that it is *legal* to vectorize the loop, then do it.
10444 
10445       // Consider vectorizing the epilogue too if it's profitable.
10446       VectorizationFactor EpilogueVF =
10447           CM.selectEpilogueVectorizationFactor(VF.Width, LVP);
10448       if (EpilogueVF.Width.isVector()) {
10449 
10450         // The first pass vectorizes the main loop and creates a scalar epilogue
10451         // to be vectorized by executing the plan (potentially with a different
10452         // factor) again shortly afterwards.
10453         EpilogueLoopVectorizationInfo EPI(VF.Width, IC, EpilogueVF.Width, 1);
10454         EpilogueVectorizerMainLoop MainILV(L, PSE, LI, DT, TLI, TTI, AC, ORE,
10455                                            EPI, &LVL, &CM, BFI, PSI, Checks);
10456 
10457         LVP.executePlan(EPI.MainLoopVF, EPI.MainLoopUF, BestPlan, MainILV, DT);
10458         ++LoopsVectorized;
10459 
10460         simplifyLoop(L, DT, LI, SE, AC, nullptr, false /* PreserveLCSSA */);
10461         formLCSSARecursively(*L, *DT, LI, SE);
10462 
10463         // Second pass vectorizes the epilogue and adjusts the control flow
10464         // edges from the first pass.
10465         EPI.MainLoopVF = EPI.EpilogueVF;
10466         EPI.MainLoopUF = EPI.EpilogueUF;
10467         EpilogueVectorizerEpilogueLoop EpilogILV(L, PSE, LI, DT, TLI, TTI, AC,
10468                                                  ORE, EPI, &LVL, &CM, BFI, PSI,
10469                                                  Checks);
10470         LVP.executePlan(EPI.EpilogueVF, EPI.EpilogueUF, BestPlan, EpilogILV,
10471                         DT);
10472         ++LoopsEpilogueVectorized;
10473 
10474         if (!MainILV.areSafetyChecksAdded())
10475           DisableRuntimeUnroll = true;
10476       } else {
10477         InnerLoopVectorizer LB(L, PSE, LI, DT, TLI, TTI, AC, ORE, VF.Width, IC,
10478                                &LVL, &CM, BFI, PSI, Checks);
10479         LVP.executePlan(VF.Width, IC, BestPlan, LB, DT);
10480         ++LoopsVectorized;
10481 
10482         // Add metadata to disable runtime unrolling a scalar loop when there
10483         // are no runtime checks about strides and memory. A scalar loop that is
10484         // rarely used is not worth unrolling.
10485         if (!LB.areSafetyChecksAdded())
10486           DisableRuntimeUnroll = true;
10487       }
10488       // Report the vectorization decision.
10489       ORE->emit([&]() {
10490         return OptimizationRemark(LV_NAME, "Vectorized", L->getStartLoc(),
10491                                   L->getHeader())
10492                << "vectorized loop (vectorization width: "
10493                << NV("VectorizationFactor", VF.Width)
10494                << ", interleaved count: " << NV("InterleaveCount", IC) << ")";
10495       });
10496     }
10497 
10498     if (ORE->allowExtraAnalysis(LV_NAME))
10499       checkMixedPrecision(L, ORE);
10500   }
10501 
10502   Optional<MDNode *> RemainderLoopID =
10503       makeFollowupLoopID(OrigLoopID, {LLVMLoopVectorizeFollowupAll,
10504                                       LLVMLoopVectorizeFollowupEpilogue});
10505   if (RemainderLoopID.hasValue()) {
10506     L->setLoopID(RemainderLoopID.getValue());
10507   } else {
10508     if (DisableRuntimeUnroll)
10509       AddRuntimeUnrollDisableMetaData(L);
10510 
10511     // Mark the loop as already vectorized to avoid vectorizing again.
10512     Hints.setAlreadyVectorized();
10513   }
10514 
10515   assert(!verifyFunction(*L->getHeader()->getParent(), &dbgs()));
10516   return true;
10517 }
10518 
10519 LoopVectorizeResult LoopVectorizePass::runImpl(
10520     Function &F, ScalarEvolution &SE_, LoopInfo &LI_, TargetTransformInfo &TTI_,
10521     DominatorTree &DT_, BlockFrequencyInfo &BFI_, TargetLibraryInfo *TLI_,
10522     DemandedBits &DB_, AAResults &AA_, AssumptionCache &AC_,
10523     std::function<const LoopAccessInfo &(Loop &)> &GetLAA_,
10524     OptimizationRemarkEmitter &ORE_, ProfileSummaryInfo *PSI_) {
10525   SE = &SE_;
10526   LI = &LI_;
10527   TTI = &TTI_;
10528   DT = &DT_;
10529   BFI = &BFI_;
10530   TLI = TLI_;
10531   AA = &AA_;
10532   AC = &AC_;
10533   GetLAA = &GetLAA_;
10534   DB = &DB_;
10535   ORE = &ORE_;
10536   PSI = PSI_;
10537 
10538   // Don't attempt if
10539   // 1. the target claims to have no vector registers, and
10540   // 2. interleaving won't help ILP.
10541   //
10542   // The second condition is necessary because, even if the target has no
10543   // vector registers, loop vectorization may still enable scalar
10544   // interleaving.
10545   if (!TTI->getNumberOfRegisters(TTI->getRegisterClassForType(true)) &&
10546       TTI->getMaxInterleaveFactor(1) < 2)
10547     return LoopVectorizeResult(false, false);
10548 
10549   bool Changed = false, CFGChanged = false;
10550 
10551   // The vectorizer requires loops to be in simplified form.
10552   // Since simplification may add new inner loops, it has to run before the
10553   // legality and profitability checks. This means running the loop vectorizer
10554   // will simplify all loops, regardless of whether anything end up being
10555   // vectorized.
10556   for (auto &L : *LI)
10557     Changed |= CFGChanged |=
10558         simplifyLoop(L, DT, LI, SE, AC, nullptr, false /* PreserveLCSSA */);
10559 
10560   // Build up a worklist of inner-loops to vectorize. This is necessary as
10561   // the act of vectorizing or partially unrolling a loop creates new loops
10562   // and can invalidate iterators across the loops.
10563   SmallVector<Loop *, 8> Worklist;
10564 
10565   for (Loop *L : *LI)
10566     collectSupportedLoops(*L, LI, ORE, Worklist);
10567 
10568   LoopsAnalyzed += Worklist.size();
10569 
10570   // Now walk the identified inner loops.
10571   while (!Worklist.empty()) {
10572     Loop *L = Worklist.pop_back_val();
10573 
10574     // For the inner loops we actually process, form LCSSA to simplify the
10575     // transform.
10576     Changed |= formLCSSARecursively(*L, *DT, LI, SE);
10577 
10578     Changed |= CFGChanged |= processLoop(L);
10579   }
10580 
10581   // Process each loop nest in the function.
10582   return LoopVectorizeResult(Changed, CFGChanged);
10583 }
10584 
10585 PreservedAnalyses LoopVectorizePass::run(Function &F,
10586                                          FunctionAnalysisManager &AM) {
10587     auto &SE = AM.getResult<ScalarEvolutionAnalysis>(F);
10588     auto &LI = AM.getResult<LoopAnalysis>(F);
10589     auto &TTI = AM.getResult<TargetIRAnalysis>(F);
10590     auto &DT = AM.getResult<DominatorTreeAnalysis>(F);
10591     auto &BFI = AM.getResult<BlockFrequencyAnalysis>(F);
10592     auto &TLI = AM.getResult<TargetLibraryAnalysis>(F);
10593     auto &AA = AM.getResult<AAManager>(F);
10594     auto &AC = AM.getResult<AssumptionAnalysis>(F);
10595     auto &DB = AM.getResult<DemandedBitsAnalysis>(F);
10596     auto &ORE = AM.getResult<OptimizationRemarkEmitterAnalysis>(F);
10597 
10598     auto &LAM = AM.getResult<LoopAnalysisManagerFunctionProxy>(F).getManager();
10599     std::function<const LoopAccessInfo &(Loop &)> GetLAA =
10600         [&](Loop &L) -> const LoopAccessInfo & {
10601       LoopStandardAnalysisResults AR = {AA,  AC,  DT,      LI,      SE,
10602                                         TLI, TTI, nullptr, nullptr, nullptr};
10603       return LAM.getResult<LoopAccessAnalysis>(L, AR);
10604     };
10605     auto &MAMProxy = AM.getResult<ModuleAnalysisManagerFunctionProxy>(F);
10606     ProfileSummaryInfo *PSI =
10607         MAMProxy.getCachedResult<ProfileSummaryAnalysis>(*F.getParent());
10608     LoopVectorizeResult Result =
10609         runImpl(F, SE, LI, TTI, DT, BFI, &TLI, DB, AA, AC, GetLAA, ORE, PSI);
10610     if (!Result.MadeAnyChange)
10611       return PreservedAnalyses::all();
10612     PreservedAnalyses PA;
10613 
10614     // We currently do not preserve loopinfo/dominator analyses with outer loop
10615     // vectorization. Until this is addressed, mark these analyses as preserved
10616     // only for non-VPlan-native path.
10617     // TODO: Preserve Loop and Dominator analyses for VPlan-native path.
10618     if (!EnableVPlanNativePath) {
10619       PA.preserve<LoopAnalysis>();
10620       PA.preserve<DominatorTreeAnalysis>();
10621     }
10622     if (!Result.MadeCFGChange)
10623       PA.preserveSet<CFGAnalyses>();
10624     return PA;
10625 }
10626 
10627 void LoopVectorizePass::printPipeline(
10628     raw_ostream &OS, function_ref<StringRef(StringRef)> MapClassName2PassName) {
10629   static_cast<PassInfoMixin<LoopVectorizePass> *>(this)->printPipeline(
10630       OS, MapClassName2PassName);
10631 
10632   OS << "<";
10633   OS << (InterleaveOnlyWhenForced ? "" : "no-") << "interleave-forced-only;";
10634   OS << (VectorizeOnlyWhenForced ? "" : "no-") << "vectorize-forced-only;";
10635   OS << ">";
10636 }
10637