Bug Summary

File:/home/username/llvm-project/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp
Warning:line 5044, column 60
Division by zero

Annotated Source Code

Press '?' to see keyboard shortcuts

clang -cc1 -triple x86_64-unknown-linux-gnu -analyze -disable-free -main-file-name LoopVectorize.cpp -analyzer-store=region -analyzer-opt-analyze-nested-blocks -analyzer-checker=core -analyzer-checker=apiModeling -analyzer-checker=unix -analyzer-checker=deadcode -analyzer-checker=cplusplus -analyzer-checker=security.insecureAPI.UncheckedReturn -analyzer-checker=security.insecureAPI.getpw -analyzer-checker=security.insecureAPI.gets -analyzer-checker=security.insecureAPI.mktemp -analyzer-checker=security.insecureAPI.mkstemp -analyzer-checker=security.insecureAPI.vfork -analyzer-checker=nullability.NullPassedToNonnull -analyzer-checker=nullability.NullReturnedFromNonnull -analyzer-output plist -w -analyzer-config-compatibility-mode=true -mrelocation-model pic -pic-level 2 -mthread-model posix -mdisable-fp-elim -fmath-errno -masm-verbose -mconstructor-aliases -munwind-tables -fuse-init-array -target-cpu x86-64 -dwarf-column-info -debugger-tuning=gdb -resource-dir /home/username/llvm-project/build/lib/clang/9.0.0 -D GTEST_HAS_RTTI=0 -D _DEBUG -D _GNU_SOURCE -D __STDC_CONSTANT_MACROS -D __STDC_FORMAT_MACROS -D __STDC_LIMIT_MACROS -I lib/Transforms/Vectorize -I /home/username/llvm-project/llvm/lib/Transforms/Vectorize -I /usr/include/libxml2 -I include -I /home/username/llvm-project/llvm/include -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/8/../../../../include/c++/8 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/8/../../../../include/x86_64-linux-gnu/c++/8 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/8/../../../../include/x86_64-linux-gnu/c++/8 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/8/../../../../include/c++/8/backward -internal-isystem /usr/local/include -internal-isystem /home/username/llvm-project/build/lib/clang/9.0.0/include -internal-externc-isystem /usr/include/x86_64-linux-gnu -internal-externc-isystem /include -internal-externc-isystem /usr/include -Wno-unused-parameter -Wwrite-strings -Wno-missing-field-initializers -Wno-long-long -Wno-maybe-uninitialized -Wno-noexcept-type -Wno-comment -std=c++11 -fdeprecated-macro -fdebug-compilation-dir /home/username/llvm-project/build -ferror-limit 19 -fmessage-length 0 -fvisibility-inlines-hidden -fno-rtti -fobjc-runtime=gcc -fdiagnostics-show-option -fcolor-diagnostics -analyzer-output=html -analyzer-config stable-report-filename=true -o /home/username/gsoc/scan-build/test69/ -x c++ /home/username/llvm-project/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp -faddrsig
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 "VPlanHCFGBuilder.h"
60#include "VPlanHCFGTransforms.h"
61#include "VPlanPredicator.h"
62#include "llvm/ADT/APInt.h"
63#include "llvm/ADT/ArrayRef.h"
64#include "llvm/ADT/DenseMap.h"
65#include "llvm/ADT/DenseMapInfo.h"
66#include "llvm/ADT/Hashing.h"
67#include "llvm/ADT/MapVector.h"
68#include "llvm/ADT/None.h"
69#include "llvm/ADT/Optional.h"
70#include "llvm/ADT/STLExtras.h"
71#include "llvm/ADT/SetVector.h"
72#include "llvm/ADT/SmallPtrSet.h"
73#include "llvm/ADT/SmallVector.h"
74#include "llvm/ADT/Statistic.h"
75#include "llvm/ADT/StringRef.h"
76#include "llvm/ADT/Twine.h"
77#include "llvm/ADT/iterator_range.h"
78#include "llvm/Analysis/AssumptionCache.h"
79#include "llvm/Analysis/BasicAliasAnalysis.h"
80#include "llvm/Analysis/BlockFrequencyInfo.h"
81#include "llvm/Analysis/CFG.h"
82#include "llvm/Analysis/CodeMetrics.h"
83#include "llvm/Analysis/DemandedBits.h"
84#include "llvm/Analysis/GlobalsModRef.h"
85#include "llvm/Analysis/LoopAccessAnalysis.h"
86#include "llvm/Analysis/LoopAnalysisManager.h"
87#include "llvm/Analysis/LoopInfo.h"
88#include "llvm/Analysis/LoopIterator.h"
89#include "llvm/Analysis/MemorySSA.h"
90#include "llvm/Analysis/OptimizationRemarkEmitter.h"
91#include "llvm/Analysis/ScalarEvolution.h"
92#include "llvm/Analysis/ScalarEvolutionExpander.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/Type.h"
120#include "llvm/IR/Use.h"
121#include "llvm/IR/User.h"
122#include "llvm/IR/Value.h"
123#include "llvm/IR/ValueHandle.h"
124#include "llvm/IR/Verifier.h"
125#include "llvm/Pass.h"
126#include "llvm/Support/Casting.h"
127#include "llvm/Support/CommandLine.h"
128#include "llvm/Support/Compiler.h"
129#include "llvm/Support/Debug.h"
130#include "llvm/Support/ErrorHandling.h"
131#include "llvm/Support/MathExtras.h"
132#include "llvm/Support/raw_ostream.h"
133#include "llvm/Transforms/Utils/BasicBlockUtils.h"
134#include "llvm/Transforms/Utils/LoopSimplify.h"
135#include "llvm/Transforms/Utils/LoopUtils.h"
136#include "llvm/Transforms/Utils/LoopVersioning.h"
137#include "llvm/Transforms/Vectorize/LoopVectorizationLegality.h"
138#include <algorithm>
139#include <cassert>
140#include <cstdint>
141#include <cstdlib>
142#include <functional>
143#include <iterator>
144#include <limits>
145#include <memory>
146#include <string>
147#include <tuple>
148#include <utility>
149#include <vector>
150
151using namespace llvm;
152
153#define LV_NAME"loop-vectorize" "loop-vectorize"
154#define DEBUG_TYPE"loop-vectorize" LV_NAME"loop-vectorize"
155
156/// @{
157/// Metadata attribute names
158static const char *const LLVMLoopVectorizeFollowupAll =
159 "llvm.loop.vectorize.followup_all";
160static const char *const LLVMLoopVectorizeFollowupVectorized =
161 "llvm.loop.vectorize.followup_vectorized";
162static const char *const LLVMLoopVectorizeFollowupEpilogue =
163 "llvm.loop.vectorize.followup_epilogue";
164/// @}
165
166STATISTIC(LoopsVectorized, "Number of loops vectorized")static llvm::Statistic LoopsVectorized = {"loop-vectorize", "LoopsVectorized"
, "Number of loops vectorized", {0}, {false}}
;
167STATISTIC(LoopsAnalyzed, "Number of loops analyzed for vectorization")static llvm::Statistic LoopsAnalyzed = {"loop-vectorize", "LoopsAnalyzed"
, "Number of loops analyzed for vectorization", {0}, {false}}
;
168
169/// Loops with a known constant trip count below this number are vectorized only
170/// if no scalar iteration overheads are incurred.
171static cl::opt<unsigned> TinyTripCountVectorThreshold(
172 "vectorizer-min-trip-count", cl::init(16), cl::Hidden,
173 cl::desc("Loops with a constant trip count that is smaller than this "
174 "value are vectorized only if no scalar iteration overheads "
175 "are incurred."));
176
177static cl::opt<bool> MaximizeBandwidth(
178 "vectorizer-maximize-bandwidth", cl::init(false), cl::Hidden,
179 cl::desc("Maximize bandwidth when selecting vectorization factor which "
180 "will be determined by the smallest type in loop."));
181
182static cl::opt<bool> EnableInterleavedMemAccesses(
183 "enable-interleaved-mem-accesses", cl::init(false), cl::Hidden,
184 cl::desc("Enable vectorization on interleaved memory accesses in a loop"));
185
186/// An interleave-group may need masking if it resides in a block that needs
187/// predication, or in order to mask away gaps.
188static cl::opt<bool> EnableMaskedInterleavedMemAccesses(
189 "enable-masked-interleaved-mem-accesses", cl::init(false), cl::Hidden,
190 cl::desc("Enable vectorization on masked interleaved memory accesses in a loop"));
191
192/// We don't interleave loops with a known constant trip count below this
193/// number.
194static const unsigned TinyTripCountInterleaveThreshold = 128;
195
196static cl::opt<unsigned> ForceTargetNumScalarRegs(
197 "force-target-num-scalar-regs", cl::init(0), cl::Hidden,
198 cl::desc("A flag that overrides the target's number of scalar registers."));
199
200static cl::opt<unsigned> ForceTargetNumVectorRegs(
201 "force-target-num-vector-regs", cl::init(0), cl::Hidden,
202 cl::desc("A flag that overrides the target's number of vector registers."));
203
204static cl::opt<unsigned> ForceTargetMaxScalarInterleaveFactor(
205 "force-target-max-scalar-interleave", cl::init(0), cl::Hidden,
206 cl::desc("A flag that overrides the target's max interleave factor for "
207 "scalar loops."));
208
209static cl::opt<unsigned> ForceTargetMaxVectorInterleaveFactor(
210 "force-target-max-vector-interleave", cl::init(0), cl::Hidden,
211 cl::desc("A flag that overrides the target's max interleave factor for "
212 "vectorized loops."));
213
214static cl::opt<unsigned> ForceTargetInstructionCost(
215 "force-target-instruction-cost", cl::init(0), cl::Hidden,
216 cl::desc("A flag that overrides the target's expected cost for "
217 "an instruction to a single constant value. Mostly "
218 "useful for getting consistent testing."));
219
220static cl::opt<unsigned> SmallLoopCost(
221 "small-loop-cost", cl::init(20), cl::Hidden,
222 cl::desc(
223 "The cost of a loop that is considered 'small' by the interleaver."));
224
225static cl::opt<bool> LoopVectorizeWithBlockFrequency(
226 "loop-vectorize-with-block-frequency", cl::init(true), cl::Hidden,
227 cl::desc("Enable the use of the block frequency analysis to access PGO "
228 "heuristics minimizing code growth in cold regions and being more "
229 "aggressive in hot regions."));
230
231// Runtime interleave loops for load/store throughput.
232static cl::opt<bool> EnableLoadStoreRuntimeInterleave(
233 "enable-loadstore-runtime-interleave", cl::init(true), cl::Hidden,
234 cl::desc(
235 "Enable runtime interleaving until load/store ports are saturated"));
236
237/// The number of stores in a loop that are allowed to need predication.
238static cl::opt<unsigned> NumberOfStoresToPredicate(
239 "vectorize-num-stores-pred", cl::init(1), cl::Hidden,
240 cl::desc("Max number of stores to be predicated behind an if."));
241
242static cl::opt<bool> EnableIndVarRegisterHeur(
243 "enable-ind-var-reg-heur", cl::init(true), cl::Hidden,
244 cl::desc("Count the induction variable only once when interleaving"));
245
246static cl::opt<bool> EnableCondStoresVectorization(
247 "enable-cond-stores-vec", cl::init(true), cl::Hidden,
248 cl::desc("Enable if predication of stores during vectorization."));
249
250static cl::opt<unsigned> MaxNestedScalarReductionIC(
251 "max-nested-scalar-reduction-interleave", cl::init(2), cl::Hidden,
252 cl::desc("The maximum interleave count to use when interleaving a scalar "
253 "reduction in a nested loop."));
254
255cl::opt<bool> EnableVPlanNativePath(
256 "enable-vplan-native-path", cl::init(false), cl::Hidden,
257 cl::desc("Enable VPlan-native vectorization path with "
258 "support for outer loop vectorization."));
259
260// FIXME: Remove this switch once we have divergence analysis. Currently we
261// assume divergent non-backedge branches when this switch is true.
262cl::opt<bool> EnableVPlanPredication(
263 "enable-vplan-predication", cl::init(false), cl::Hidden,
264 cl::desc("Enable VPlan-native vectorization path predicator with "
265 "support for outer loop vectorization."));
266
267// This flag enables the stress testing of the VPlan H-CFG construction in the
268// VPlan-native vectorization path. It must be used in conjuction with
269// -enable-vplan-native-path. -vplan-verify-hcfg can also be used to enable the
270// verification of the H-CFGs built.
271static cl::opt<bool> VPlanBuildStressTest(
272 "vplan-build-stress-test", cl::init(false), cl::Hidden,
273 cl::desc(
274 "Build VPlan for every supported loop nest in the function and bail "
275 "out right after the build (stress test the VPlan H-CFG construction "
276 "in the VPlan-native vectorization path)."));
277
278/// A helper function for converting Scalar types to vector types.
279/// If the incoming type is void, we return void. If the VF is 1, we return
280/// the scalar type.
281static Type *ToVectorTy(Type *Scalar, unsigned VF) {
282 if (Scalar->isVoidTy() || VF == 1)
283 return Scalar;
284 return VectorType::get(Scalar, VF);
285}
286
287/// A helper function that returns the type of loaded or stored value.
288static Type *getMemInstValueType(Value *I) {
289 assert((isa<LoadInst>(I) || isa<StoreInst>(I)) &&(static_cast <bool> ((isa<LoadInst>(I) || isa<
StoreInst>(I)) && "Expected Load or Store instruction"
) ? void (0) : __assert_fail ("(isa<LoadInst>(I) || isa<StoreInst>(I)) && \"Expected Load or Store instruction\""
, "/home/username/llvm-project/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp"
, 290, __extension__ __PRETTY_FUNCTION__))
290 "Expected Load or Store instruction")(static_cast <bool> ((isa<LoadInst>(I) || isa<
StoreInst>(I)) && "Expected Load or Store instruction"
) ? void (0) : __assert_fail ("(isa<LoadInst>(I) || isa<StoreInst>(I)) && \"Expected Load or Store instruction\""
, "/home/username/llvm-project/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp"
, 290, __extension__ __PRETTY_FUNCTION__))
;
291 if (auto *LI = dyn_cast<LoadInst>(I))
292 return LI->getType();
293 return cast<StoreInst>(I)->getValueOperand()->getType();
294}
295
296/// A helper function that returns true if the given type is irregular. The
297/// type is irregular if its allocated size doesn't equal the store size of an
298/// element of the corresponding vector type at the given vectorization factor.
299static bool hasIrregularType(Type *Ty, const DataLayout &DL, unsigned VF) {
300 // Determine if an array of VF elements of type Ty is "bitcast compatible"
301 // with a <VF x Ty> vector.
302 if (VF > 1) {
303 auto *VectorTy = VectorType::get(Ty, VF);
304 return VF * DL.getTypeAllocSize(Ty) != DL.getTypeStoreSize(VectorTy);
305 }
306
307 // If the vectorization factor is one, we just check if an array of type Ty
308 // requires padding between elements.
309 return DL.getTypeAllocSizeInBits(Ty) != DL.getTypeSizeInBits(Ty);
310}
311
312/// A helper function that returns the reciprocal of the block probability of
313/// predicated blocks. If we return X, we are assuming the predicated block
314/// will execute once for every X iterations of the loop header.
315///
316/// TODO: We should use actual block probability here, if available. Currently,
317/// we always assume predicated blocks have a 50% chance of executing.
318static unsigned getReciprocalPredBlockProb() { return 2; }
319
320/// A helper function that adds a 'fast' flag to floating-point operations.
321static Value *addFastMathFlag(Value *V) {
322 if (isa<FPMathOperator>(V))
323 cast<Instruction>(V)->setFastMathFlags(FastMathFlags::getFast());
324 return V;
325}
326
327static Value *addFastMathFlag(Value *V, FastMathFlags FMF) {
328 if (isa<FPMathOperator>(V))
329 cast<Instruction>(V)->setFastMathFlags(FMF);
330 return V;
331}
332
333/// A helper function that returns an integer or floating-point constant with
334/// value C.
335static Constant *getSignedIntOrFpConstant(Type *Ty, int64_t C) {
336 return Ty->isIntegerTy() ? ConstantInt::getSigned(Ty, C)
337 : ConstantFP::get(Ty, C);
338}
339
340namespace llvm {
341
342/// InnerLoopVectorizer vectorizes loops which contain only one basic
343/// block to a specified vectorization factor (VF).
344/// This class performs the widening of scalars into vectors, or multiple
345/// scalars. This class also implements the following features:
346/// * It inserts an epilogue loop for handling loops that don't have iteration
347/// counts that are known to be a multiple of the vectorization factor.
348/// * It handles the code generation for reduction variables.
349/// * Scalarization (implementation using scalars) of un-vectorizable
350/// instructions.
351/// InnerLoopVectorizer does not perform any vectorization-legality
352/// checks, and relies on the caller to check for the different legality
353/// aspects. The InnerLoopVectorizer relies on the
354/// LoopVectorizationLegality class to provide information about the induction
355/// and reduction variables that were found to a given vectorization factor.
356class InnerLoopVectorizer {
357public:
358 InnerLoopVectorizer(Loop *OrigLoop, PredicatedScalarEvolution &PSE,
359 LoopInfo *LI, DominatorTree *DT,
360 const TargetLibraryInfo *TLI,
361 const TargetTransformInfo *TTI, AssumptionCache *AC,
362 OptimizationRemarkEmitter *ORE, unsigned VecWidth,
363 unsigned UnrollFactor, LoopVectorizationLegality *LVL,
364 LoopVectorizationCostModel *CM)
365 : OrigLoop(OrigLoop), PSE(PSE), LI(LI), DT(DT), TLI(TLI), TTI(TTI),
366 AC(AC), ORE(ORE), VF(VecWidth), UF(UnrollFactor),
367 Builder(PSE.getSE()->getContext()),
368 VectorLoopValueMap(UnrollFactor, VecWidth), Legal(LVL), Cost(CM) {}
369 virtual ~InnerLoopVectorizer() = default;
370
371 /// Create a new empty loop. Unlink the old loop and connect the new one.
372 /// Return the pre-header block of the new loop.
373 BasicBlock *createVectorizedLoopSkeleton();
374
375 /// Widen a single instruction within the innermost loop.
376 void widenInstruction(Instruction &I);
377
378 /// Fix the vectorized code, taking care of header phi's, live-outs, and more.
379 void fixVectorizedLoop();
380
381 // Return true if any runtime check is added.
382 bool areSafetyChecksAdded() { return AddedSafetyChecks; }
383
384 /// A type for vectorized values in the new loop. Each value from the
385 /// original loop, when vectorized, is represented by UF vector values in the
386 /// new unrolled loop, where UF is the unroll factor.
387 using VectorParts = SmallVector<Value *, 2>;
388
389 /// Vectorize a single PHINode in a block. This method handles the induction
390 /// variable canonicalization. It supports both VF = 1 for unrolled loops and
391 /// arbitrary length vectors.
392 void widenPHIInstruction(Instruction *PN, unsigned UF, unsigned VF);
393
394 /// A helper function to scalarize a single Instruction in the innermost loop.
395 /// Generates a sequence of scalar instances for each lane between \p MinLane
396 /// and \p MaxLane, times each part between \p MinPart and \p MaxPart,
397 /// inclusive..
398 void scalarizeInstruction(Instruction *Instr, const VPIteration &Instance,
399 bool IfPredicateInstr);
400
401 /// Widen an integer or floating-point induction variable \p IV. If \p Trunc
402 /// is provided, the integer induction variable will first be truncated to
403 /// the corresponding type.
404 void widenIntOrFpInduction(PHINode *IV, TruncInst *Trunc = nullptr);
405
406 /// getOrCreateVectorValue and getOrCreateScalarValue coordinate to generate a
407 /// vector or scalar value on-demand if one is not yet available. When
408 /// vectorizing a loop, we visit the definition of an instruction before its
409 /// uses. When visiting the definition, we either vectorize or scalarize the
410 /// instruction, creating an entry for it in the corresponding map. (In some
411 /// cases, such as induction variables, we will create both vector and scalar
412 /// entries.) Then, as we encounter uses of the definition, we derive values
413 /// for each scalar or vector use unless such a value is already available.
414 /// For example, if we scalarize a definition and one of its uses is vector,
415 /// we build the required vector on-demand with an insertelement sequence
416 /// when visiting the use. Otherwise, if the use is scalar, we can use the
417 /// existing scalar definition.
418 ///
419 /// Return a value in the new loop corresponding to \p V from the original
420 /// loop at unroll index \p Part. If the value has already been vectorized,
421 /// the corresponding vector entry in VectorLoopValueMap is returned. If,
422 /// however, the value has a scalar entry in VectorLoopValueMap, we construct
423 /// a new vector value on-demand by inserting the scalar values into a vector
424 /// with an insertelement sequence. If the value has been neither vectorized
425 /// nor scalarized, it must be loop invariant, so we simply broadcast the
426 /// value into a vector.
427 Value *getOrCreateVectorValue(Value *V, unsigned Part);
428
429 /// Return a value in the new loop corresponding to \p V from the original
430 /// loop at unroll and vector indices \p Instance. If the value has been
431 /// vectorized but not scalarized, the necessary extractelement instruction
432 /// will be generated.
433 Value *getOrCreateScalarValue(Value *V, const VPIteration &Instance);
434
435 /// Construct the vector value of a scalarized value \p V one lane at a time.
436 void packScalarIntoVectorValue(Value *V, const VPIteration &Instance);
437
438 /// Try to vectorize the interleaved access group that \p Instr belongs to,
439 /// optionally masking the vector operations if \p BlockInMask is non-null.
440 void vectorizeInterleaveGroup(Instruction *Instr,
441 VectorParts *BlockInMask = nullptr);
442
443 /// Vectorize Load and Store instructions, optionally masking the vector
444 /// operations if \p BlockInMask is non-null.
445 void vectorizeMemoryInstruction(Instruction *Instr,
446 VectorParts *BlockInMask = nullptr);
447
448 /// Set the debug location in the builder using the debug location in
449 /// the instruction.
450 void setDebugLocFromInst(IRBuilder<> &B, const Value *Ptr);
451
452 /// Fix the non-induction PHIs in the OrigPHIsToFix vector.
453 void fixNonInductionPHIs(void);
454
455protected:
456 friend class LoopVectorizationPlanner;
457
458 /// A small list of PHINodes.
459 using PhiVector = SmallVector<PHINode *, 4>;
460
461 /// A type for scalarized values in the new loop. Each value from the
462 /// original loop, when scalarized, is represented by UF x VF scalar values
463 /// in the new unrolled loop, where UF is the unroll factor and VF is the
464 /// vectorization factor.
465 using ScalarParts = SmallVector<SmallVector<Value *, 4>, 2>;
466
467 /// Set up the values of the IVs correctly when exiting the vector loop.
468 void fixupIVUsers(PHINode *OrigPhi, const InductionDescriptor &II,
469 Value *CountRoundDown, Value *EndValue,
470 BasicBlock *MiddleBlock);
471
472 /// Create a new induction variable inside L.
473 PHINode *createInductionVariable(Loop *L, Value *Start, Value *End,
474 Value *Step, Instruction *DL);
475
476 /// Handle all cross-iteration phis in the header.
477 void fixCrossIterationPHIs();
478
479 /// Fix a first-order recurrence. This is the second phase of vectorizing
480 /// this phi node.
481 void fixFirstOrderRecurrence(PHINode *Phi);
482
483 /// Fix a reduction cross-iteration phi. This is the second phase of
484 /// vectorizing this phi node.
485 void fixReduction(PHINode *Phi);
486
487 /// The Loop exit block may have single value PHI nodes with some
488 /// incoming value. While vectorizing we only handled real values
489 /// that were defined inside the loop and we should have one value for
490 /// each predecessor of its parent basic block. See PR14725.
491 void fixLCSSAPHIs();
492
493 /// Iteratively sink the scalarized operands of a predicated instruction into
494 /// the block that was created for it.
495 void sinkScalarOperands(Instruction *PredInst);
496
497 /// Shrinks vector element sizes to the smallest bitwidth they can be legally
498 /// represented as.
499 void truncateToMinimalBitwidths();
500
501 /// Insert the new loop to the loop hierarchy and pass manager
502 /// and update the analysis passes.
503 void updateAnalysis();
504
505 /// Create a broadcast instruction. This method generates a broadcast
506 /// instruction (shuffle) for loop invariant values and for the induction
507 /// value. If this is the induction variable then we extend it to N, N+1, ...
508 /// this is needed because each iteration in the loop corresponds to a SIMD
509 /// element.
510 virtual Value *getBroadcastInstrs(Value *V);
511
512 /// This function adds (StartIdx, StartIdx + Step, StartIdx + 2*Step, ...)
513 /// to each vector element of Val. The sequence starts at StartIndex.
514 /// \p Opcode is relevant for FP induction variable.
515 virtual Value *getStepVector(Value *Val, int StartIdx, Value *Step,
516 Instruction::BinaryOps Opcode =
517 Instruction::BinaryOpsEnd);
518
519 /// Compute scalar induction steps. \p ScalarIV is the scalar induction
520 /// variable on which to base the steps, \p Step is the size of the step, and
521 /// \p EntryVal is the value from the original loop that maps to the steps.
522 /// Note that \p EntryVal doesn't have to be an induction variable - it
523 /// can also be a truncate instruction.
524 void buildScalarSteps(Value *ScalarIV, Value *Step, Instruction *EntryVal,
525 const InductionDescriptor &ID);
526
527 /// Create a vector induction phi node based on an existing scalar one. \p
528 /// EntryVal is the value from the original loop that maps to the vector phi
529 /// node, and \p Step is the loop-invariant step. If \p EntryVal is a
530 /// truncate instruction, instead of widening the original IV, we widen a
531 /// version of the IV truncated to \p EntryVal's type.
532 void createVectorIntOrFpInductionPHI(const InductionDescriptor &II,
533 Value *Step, Instruction *EntryVal);
534
535 /// Returns true if an instruction \p I should be scalarized instead of
536 /// vectorized for the chosen vectorization factor.
537 bool shouldScalarizeInstruction(Instruction *I) const;
538
539 /// Returns true if we should generate a scalar version of \p IV.
540 bool needsScalarInduction(Instruction *IV) const;
541
542 /// If there is a cast involved in the induction variable \p ID, which should
543 /// be ignored in the vectorized loop body, this function records the
544 /// VectorLoopValue of the respective Phi also as the VectorLoopValue of the
545 /// cast. We had already proved that the casted Phi is equal to the uncasted
546 /// Phi in the vectorized loop (under a runtime guard), and therefore
547 /// there is no need to vectorize the cast - the same value can be used in the
548 /// vector loop for both the Phi and the cast.
549 /// If \p VectorLoopValue is a scalarized value, \p Lane is also specified,
550 /// Otherwise, \p VectorLoopValue is a widened/vectorized value.
551 ///
552 /// \p EntryVal is the value from the original loop that maps to the vector
553 /// phi node and is used to distinguish what is the IV currently being
554 /// processed - original one (if \p EntryVal is a phi corresponding to the
555 /// original IV) or the "newly-created" one based on the proof mentioned above
556 /// (see also buildScalarSteps() and createVectorIntOrFPInductionPHI()). In the
557 /// latter case \p EntryVal is a TruncInst and we must not record anything for
558 /// that IV, but it's error-prone to expect callers of this routine to care
559 /// about that, hence this explicit parameter.
560 void recordVectorLoopValueForInductionCast(const InductionDescriptor &ID,
561 const Instruction *EntryVal,
562 Value *VectorLoopValue,
563 unsigned Part,
564 unsigned Lane = UINT_MAX(2147483647 *2U +1U));
565
566 /// Generate a shuffle sequence that will reverse the vector Vec.
567 virtual Value *reverseVector(Value *Vec);
568
569 /// Returns (and creates if needed) the original loop trip count.
570 Value *getOrCreateTripCount(Loop *NewLoop);
571
572 /// Returns (and creates if needed) the trip count of the widened loop.
573 Value *getOrCreateVectorTripCount(Loop *NewLoop);
574
575 /// Returns a bitcasted value to the requested vector type.
576 /// Also handles bitcasts of vector<float> <-> vector<pointer> types.
577 Value *createBitOrPointerCast(Value *V, VectorType *DstVTy,
578 const DataLayout &DL);
579
580 /// Emit a bypass check to see if the vector trip count is zero, including if
581 /// it overflows.
582 void emitMinimumIterationCountCheck(Loop *L, BasicBlock *Bypass);
583
584 /// Emit a bypass check to see if all of the SCEV assumptions we've
585 /// had to make are correct.
586 void emitSCEVChecks(Loop *L, BasicBlock *Bypass);
587
588 /// Emit bypass checks to check any memory assumptions we may have made.
589 void emitMemRuntimeChecks(Loop *L, BasicBlock *Bypass);
590
591 /// Compute the transformed value of Index at offset StartValue using step
592 /// StepValue.
593 /// For integer induction, returns StartValue + Index * StepValue.
594 /// For pointer induction, returns StartValue[Index * StepValue].
595 /// FIXME: The newly created binary instructions should contain nsw/nuw
596 /// flags, which can be found from the original scalar operations.
597 Value *emitTransformedIndex(IRBuilder<> &B, Value *Index, ScalarEvolution *SE,
598 const DataLayout &DL,
599 const InductionDescriptor &ID) const;
600
601 /// Add additional metadata to \p To that was not present on \p Orig.
602 ///
603 /// Currently this is used to add the noalias annotations based on the
604 /// inserted memchecks. Use this for instructions that are *cloned* into the
605 /// vector loop.
606 void addNewMetadata(Instruction *To, const Instruction *Orig);
607
608 /// Add metadata from one instruction to another.
609 ///
610 /// This includes both the original MDs from \p From and additional ones (\see
611 /// addNewMetadata). Use this for *newly created* instructions in the vector
612 /// loop.
613 void addMetadata(Instruction *To, Instruction *From);
614
615 /// Similar to the previous function but it adds the metadata to a
616 /// vector of instructions.
617 void addMetadata(ArrayRef<Value *> To, Instruction *From);
618
619 /// The original loop.
620 Loop *OrigLoop;
621
622 /// A wrapper around ScalarEvolution used to add runtime SCEV checks. Applies
623 /// dynamic knowledge to simplify SCEV expressions and converts them to a
624 /// more usable form.
625 PredicatedScalarEvolution &PSE;
626
627 /// Loop Info.
628 LoopInfo *LI;
629
630 /// Dominator Tree.
631 DominatorTree *DT;
632
633 /// Alias Analysis.
634 AliasAnalysis *AA;
635
636 /// Target Library Info.
637 const TargetLibraryInfo *TLI;
638
639 /// Target Transform Info.
640 const TargetTransformInfo *TTI;
641
642 /// Assumption Cache.
643 AssumptionCache *AC;
644
645 /// Interface to emit optimization remarks.
646 OptimizationRemarkEmitter *ORE;
647
648 /// LoopVersioning. It's only set up (non-null) if memchecks were
649 /// used.
650 ///
651 /// This is currently only used to add no-alias metadata based on the
652 /// memchecks. The actually versioning is performed manually.
653 std::unique_ptr<LoopVersioning> LVer;
654
655 /// The vectorization SIMD factor to use. Each vector will have this many
656 /// vector elements.
657 unsigned VF;
658
659 /// The vectorization unroll factor to use. Each scalar is vectorized to this
660 /// many different vector instructions.
661 unsigned UF;
662
663 /// The builder that we use
664 IRBuilder<> Builder;
665
666 // --- Vectorization state ---
667
668 /// The vector-loop preheader.
669 BasicBlock *LoopVectorPreHeader;
670
671 /// The scalar-loop preheader.
672 BasicBlock *LoopScalarPreHeader;
673
674 /// Middle Block between the vector and the scalar.
675 BasicBlock *LoopMiddleBlock;
676
677 /// The ExitBlock of the scalar loop.
678 BasicBlock *LoopExitBlock;
679
680 /// The vector loop body.
681 BasicBlock *LoopVectorBody;
682
683 /// The scalar loop body.
684 BasicBlock *LoopScalarBody;
685
686 /// A list of all bypass blocks. The first block is the entry of the loop.
687 SmallVector<BasicBlock *, 4> LoopBypassBlocks;
688
689 /// The new Induction variable which was added to the new block.
690 PHINode *Induction = nullptr;
691
692 /// The induction variable of the old basic block.
693 PHINode *OldInduction = nullptr;
694
695 /// Maps values from the original loop to their corresponding values in the
696 /// vectorized loop. A key value can map to either vector values, scalar
697 /// values or both kinds of values, depending on whether the key was
698 /// vectorized and scalarized.
699 VectorizerValueMap VectorLoopValueMap;
700
701 /// Store instructions that were predicated.
702 SmallVector<Instruction *, 4> PredicatedInstructions;
703
704 /// Trip count of the original loop.
705 Value *TripCount = nullptr;
706
707 /// Trip count of the widened loop (TripCount - TripCount % (VF*UF))
708 Value *VectorTripCount = nullptr;
709
710 /// The legality analysis.
711 LoopVectorizationLegality *Legal;
712
713 /// The profitablity analysis.
714 LoopVectorizationCostModel *Cost;
715
716 // Record whether runtime checks are added.
717 bool AddedSafetyChecks = false;
718
719 // Holds the end values for each induction variable. We save the end values
720 // so we can later fix-up the external users of the induction variables.
721 DenseMap<PHINode *, Value *> IVEndValues;
722
723 // Vector of original scalar PHIs whose corresponding widened PHIs need to be
724 // fixed up at the end of vector code generation.
725 SmallVector<PHINode *, 8> OrigPHIsToFix;
726};
727
728class InnerLoopUnroller : public InnerLoopVectorizer {
729public:
730 InnerLoopUnroller(Loop *OrigLoop, PredicatedScalarEvolution &PSE,
731 LoopInfo *LI, DominatorTree *DT,
732 const TargetLibraryInfo *TLI,
733 const TargetTransformInfo *TTI, AssumptionCache *AC,
734 OptimizationRemarkEmitter *ORE, unsigned UnrollFactor,
735 LoopVectorizationLegality *LVL,
736 LoopVectorizationCostModel *CM)
737 : InnerLoopVectorizer(OrigLoop, PSE, LI, DT, TLI, TTI, AC, ORE, 1,
738 UnrollFactor, LVL, CM) {}
739
740private:
741 Value *getBroadcastInstrs(Value *V) override;
742 Value *getStepVector(Value *Val, int StartIdx, Value *Step,
743 Instruction::BinaryOps Opcode =
744 Instruction::BinaryOpsEnd) override;
745 Value *reverseVector(Value *Vec) override;
746};
747
748} // end namespace llvm
749
750/// Look for a meaningful debug location on the instruction or it's
751/// operands.
752static Instruction *getDebugLocFromInstOrOperands(Instruction *I) {
753 if (!I)
754 return I;
755
756 DebugLoc Empty;
757 if (I->getDebugLoc() != Empty)
758 return I;
759
760 for (User::op_iterator OI = I->op_begin(), OE = I->op_end(); OI != OE; ++OI) {
761 if (Instruction *OpInst = dyn_cast<Instruction>(*OI))
762 if (OpInst->getDebugLoc() != Empty)
763 return OpInst;
764 }
765
766 return I;
767}
768
769void InnerLoopVectorizer::setDebugLocFromInst(IRBuilder<> &B, const Value *Ptr) {
770 if (const Instruction *Inst = dyn_cast_or_null<Instruction>(Ptr)) {
771 const DILocation *DIL = Inst->getDebugLoc();
772 if (DIL && Inst->getFunction()->isDebugInfoForProfiling() &&
773 !isa<DbgInfoIntrinsic>(Inst)) {
774 auto NewDIL = DIL->cloneByMultiplyingDuplicationFactor(UF * VF);
775 if (NewDIL)
776 B.SetCurrentDebugLocation(NewDIL.getValue());
777 else
778 LLVM_DEBUG(dbgs()do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("loop-vectorize")) { dbgs() << "Failed to create new discriminator: "
<< DIL->getFilename() << " Line: " << DIL
->getLine(); } } while (false)
779 << "Failed to create new discriminator: "do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("loop-vectorize")) { dbgs() << "Failed to create new discriminator: "
<< DIL->getFilename() << " Line: " << DIL
->getLine(); } } while (false)
780 << DIL->getFilename() << " Line: " << DIL->getLine())do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("loop-vectorize")) { dbgs() << "Failed to create new discriminator: "
<< DIL->getFilename() << " Line: " << DIL
->getLine(); } } while (false)
;
781 }
782 else
783 B.SetCurrentDebugLocation(DIL);
784 } else
785 B.SetCurrentDebugLocation(DebugLoc());
786}
787
788#ifndef NDEBUG
789/// \return string containing a file name and a line # for the given loop.
790static std::string getDebugLocString(const Loop *L) {
791 std::string Result;
792 if (L) {
793 raw_string_ostream OS(Result);
794 if (const DebugLoc LoopDbgLoc = L->getStartLoc())
795 LoopDbgLoc.print(OS);
796 else
797 // Just print the module name.
798 OS << L->getHeader()->getParent()->getParent()->getModuleIdentifier();
799 OS.flush();
800 }
801 return Result;
802}
803#endif
804
805void InnerLoopVectorizer::addNewMetadata(Instruction *To,
806 const Instruction *Orig) {
807 // If the loop was versioned with memchecks, add the corresponding no-alias
808 // metadata.
809 if (LVer && (isa<LoadInst>(Orig) || isa<StoreInst>(Orig)))
810 LVer->annotateInstWithNoAlias(To, Orig);
811}
812
813void InnerLoopVectorizer::addMetadata(Instruction *To,
814 Instruction *From) {
815 propagateMetadata(To, From);
816 addNewMetadata(To, From);
817}
818
819void InnerLoopVectorizer::addMetadata(ArrayRef<Value *> To,
820 Instruction *From) {
821 for (Value *V : To) {
822 if (Instruction *I = dyn_cast<Instruction>(V))
823 addMetadata(I, From);
824 }
825}
826
827namespace llvm {
828
829/// LoopVectorizationCostModel - estimates the expected speedups due to
830/// vectorization.
831/// In many cases vectorization is not profitable. This can happen because of
832/// a number of reasons. In this class we mainly attempt to predict the
833/// expected speedup/slowdowns due to the supported instruction set. We use the
834/// TargetTransformInfo to query the different backends for the cost of
835/// different operations.
836class LoopVectorizationCostModel {
837public:
838 LoopVectorizationCostModel(Loop *L, PredicatedScalarEvolution &PSE,
839 LoopInfo *LI, LoopVectorizationLegality *Legal,
840 const TargetTransformInfo &TTI,
841 const TargetLibraryInfo *TLI, DemandedBits *DB,
842 AssumptionCache *AC,
843 OptimizationRemarkEmitter *ORE, const Function *F,
844 const LoopVectorizeHints *Hints,
845 InterleavedAccessInfo &IAI)
846 : TheLoop(L), PSE(PSE), LI(LI), Legal(Legal), TTI(TTI), TLI(TLI), DB(DB),
847 AC(AC), ORE(ORE), TheFunction(F), Hints(Hints), InterleaveInfo(IAI) {}
848
849 /// \return An upper bound for the vectorization factor, or None if
850 /// vectorization and interleaving should be avoided up front.
851 Optional<unsigned> computeMaxVF(bool OptForSize);
852
853 /// \return The most profitable vectorization factor and the cost of that VF.
854 /// This method checks every power of two up to MaxVF. If UserVF is not ZERO
855 /// then this vectorization factor will be selected if vectorization is
856 /// possible.
857 VectorizationFactor selectVectorizationFactor(unsigned MaxVF);
858
859 /// Setup cost-based decisions for user vectorization factor.
860 void selectUserVectorizationFactor(unsigned UserVF) {
861 collectUniformsAndScalars(UserVF);
862 collectInstsToScalarize(UserVF);
863 }
864
865 /// \return The size (in bits) of the smallest and widest types in the code
866 /// that needs to be vectorized. We ignore values that remain scalar such as
867 /// 64 bit loop indices.
868 std::pair<unsigned, unsigned> getSmallestAndWidestTypes();
869
870 /// \return The desired interleave count.
871 /// If interleave count has been specified by metadata it will be returned.
872 /// Otherwise, the interleave count is computed and returned. VF and LoopCost
873 /// are the selected vectorization factor and the cost of the selected VF.
874 unsigned selectInterleaveCount(bool OptForSize, unsigned VF,
875 unsigned LoopCost);
876
877 /// Memory access instruction may be vectorized in more than one way.
878 /// Form of instruction after vectorization depends on cost.
879 /// This function takes cost-based decisions for Load/Store instructions
880 /// and collects them in a map. This decisions map is used for building
881 /// the lists of loop-uniform and loop-scalar instructions.
882 /// The calculated cost is saved with widening decision in order to
883 /// avoid redundant calculations.
884 void setCostBasedWideningDecision(unsigned VF);
885
886 /// A struct that represents some properties of the register usage
887 /// of a loop.
888 struct RegisterUsage {
889 /// Holds the number of loop invariant values that are used in the loop.
890 unsigned LoopInvariantRegs;
891
892 /// Holds the maximum number of concurrent live intervals in the loop.
893 unsigned MaxLocalUsers;
894 };
895
896 /// \return Returns information about the register usages of the loop for the
897 /// given vectorization factors.
898 SmallVector<RegisterUsage, 8> calculateRegisterUsage(ArrayRef<unsigned> VFs);
899
900 /// Collect values we want to ignore in the cost model.
901 void collectValuesToIgnore();
902
903 /// \returns The smallest bitwidth each instruction can be represented with.
904 /// The vector equivalents of these instructions should be truncated to this
905 /// type.
906 const MapVector<Instruction *, uint64_t> &getMinimalBitwidths() const {
907 return MinBWs;
908 }
909
910 /// \returns True if it is more profitable to scalarize instruction \p I for
911 /// vectorization factor \p VF.
912 bool isProfitableToScalarize(Instruction *I, unsigned VF) const {
913 assert(VF > 1 && "Profitable to scalarize relevant only for VF > 1.")(static_cast <bool> (VF > 1 && "Profitable to scalarize relevant only for VF > 1."
) ? void (0) : __assert_fail ("VF > 1 && \"Profitable to scalarize relevant only for VF > 1.\""
, "/home/username/llvm-project/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp"
, 913, __extension__ __PRETTY_FUNCTION__))
;
914
915 // Cost model is not run in the VPlan-native path - return conservative
916 // result until this changes.
917 if (EnableVPlanNativePath)
918 return false;
919
920 auto Scalars = InstsToScalarize.find(VF);
921 assert(Scalars != InstsToScalarize.end() &&(static_cast <bool> (Scalars != InstsToScalarize.end() &&
"VF not yet analyzed for scalarization profitability") ? void
(0) : __assert_fail ("Scalars != InstsToScalarize.end() && \"VF not yet analyzed for scalarization profitability\""
, "/home/username/llvm-project/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp"
, 922, __extension__ __PRETTY_FUNCTION__))
922 "VF not yet analyzed for scalarization profitability")(static_cast <bool> (Scalars != InstsToScalarize.end() &&
"VF not yet analyzed for scalarization profitability") ? void
(0) : __assert_fail ("Scalars != InstsToScalarize.end() && \"VF not yet analyzed for scalarization profitability\""
, "/home/username/llvm-project/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp"
, 922, __extension__ __PRETTY_FUNCTION__))
;
923 return Scalars->second.find(I) != Scalars->second.end();
924 }
925
926 /// Returns true if \p I is known to be uniform after vectorization.
927 bool isUniformAfterVectorization(Instruction *I, unsigned VF) const {
928 if (VF == 1)
929 return true;
930
931 // Cost model is not run in the VPlan-native path - return conservative
932 // result until this changes.
933 if (EnableVPlanNativePath)
934 return false;
935
936 auto UniformsPerVF = Uniforms.find(VF);
937 assert(UniformsPerVF != Uniforms.end() &&(static_cast <bool> (UniformsPerVF != Uniforms.end() &&
"VF not yet analyzed for uniformity") ? void (0) : __assert_fail
("UniformsPerVF != Uniforms.end() && \"VF not yet analyzed for uniformity\""
, "/home/username/llvm-project/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp"
, 938, __extension__ __PRETTY_FUNCTION__))
938 "VF not yet analyzed for uniformity")(static_cast <bool> (UniformsPerVF != Uniforms.end() &&
"VF not yet analyzed for uniformity") ? void (0) : __assert_fail
("UniformsPerVF != Uniforms.end() && \"VF not yet analyzed for uniformity\""
, "/home/username/llvm-project/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp"
, 938, __extension__ __PRETTY_FUNCTION__))
;
939 return UniformsPerVF->second.find(I) != UniformsPerVF->second.end();
940 }
941
942 /// Returns true if \p I is known to be scalar after vectorization.
943 bool isScalarAfterVectorization(Instruction *I, unsigned VF) const {
944 if (VF == 1)
945 return true;
946
947 // Cost model is not run in the VPlan-native path - return conservative
948 // result until this changes.
949 if (EnableVPlanNativePath)
950 return false;
951
952 auto ScalarsPerVF = Scalars.find(VF);
953 assert(ScalarsPerVF != Scalars.end() &&(static_cast <bool> (ScalarsPerVF != Scalars.end() &&
"Scalar values are not calculated for VF") ? void (0) : __assert_fail
("ScalarsPerVF != Scalars.end() && \"Scalar values are not calculated for VF\""
, "/home/username/llvm-project/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp"
, 954, __extension__ __PRETTY_FUNCTION__))
954 "Scalar values are not calculated for VF")(static_cast <bool> (ScalarsPerVF != Scalars.end() &&
"Scalar values are not calculated for VF") ? void (0) : __assert_fail
("ScalarsPerVF != Scalars.end() && \"Scalar values are not calculated for VF\""
, "/home/username/llvm-project/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp"
, 954, __extension__ __PRETTY_FUNCTION__))
;
955 return ScalarsPerVF->second.find(I) != ScalarsPerVF->second.end();
956 }
957
958 /// \returns True if instruction \p I can be truncated to a smaller bitwidth
959 /// for vectorization factor \p VF.
960 bool canTruncateToMinimalBitwidth(Instruction *I, unsigned VF) const {
961 return VF > 1 && MinBWs.find(I) != MinBWs.end() &&
962 !isProfitableToScalarize(I, VF) &&
963 !isScalarAfterVectorization(I, VF);
964 }
965
966 /// Decision that was taken during cost calculation for memory instruction.
967 enum InstWidening {
968 CM_Unknown,
969 CM_Widen, // For consecutive accesses with stride +1.
970 CM_Widen_Reverse, // For consecutive accesses with stride -1.
971 CM_Interleave,
972 CM_GatherScatter,
973 CM_Scalarize
974 };
975
976 /// Save vectorization decision \p W and \p Cost taken by the cost model for
977 /// instruction \p I and vector width \p VF.
978 void setWideningDecision(Instruction *I, unsigned VF, InstWidening W,
979 unsigned Cost) {
980 assert(VF >= 2 && "Expected VF >=2")(static_cast <bool> (VF >= 2 && "Expected VF >=2"
) ? void (0) : __assert_fail ("VF >= 2 && \"Expected VF >=2\""
, "/home/username/llvm-project/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp"
, 980, __extension__ __PRETTY_FUNCTION__))
;
981 WideningDecisions[std::make_pair(I, VF)] = std::make_pair(W, Cost);
982 }
983
984 /// Save vectorization decision \p W and \p Cost taken by the cost model for
985 /// interleaving group \p Grp and vector width \p VF.
986 void setWideningDecision(const InterleaveGroup<Instruction> *Grp, unsigned VF,
987 InstWidening W, unsigned Cost) {
988 assert(VF >= 2 && "Expected VF >=2")(static_cast <bool> (VF >= 2 && "Expected VF >=2"
) ? void (0) : __assert_fail ("VF >= 2 && \"Expected VF >=2\""
, "/home/username/llvm-project/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp"
, 988, __extension__ __PRETTY_FUNCTION__))
;
989 /// Broadcast this decicion to all instructions inside the group.
990 /// But the cost will be assigned to one instruction only.
991 for (unsigned i = 0; i < Grp->getFactor(); ++i) {
992 if (auto *I = Grp->getMember(i)) {
993 if (Grp->getInsertPos() == I)
994 WideningDecisions[std::make_pair(I, VF)] = std::make_pair(W, Cost);
995 else
996 WideningDecisions[std::make_pair(I, VF)] = std::make_pair(W, 0);
997 }
998 }
999 }
1000
1001 /// Return the cost model decision for the given instruction \p I and vector
1002 /// width \p VF. Return CM_Unknown if this instruction did not pass
1003 /// through the cost modeling.
1004 InstWidening getWideningDecision(Instruction *I, unsigned VF) {
1005 assert(VF >= 2 && "Expected VF >=2")(static_cast <bool> (VF >= 2 && "Expected VF >=2"
) ? void (0) : __assert_fail ("VF >= 2 && \"Expected VF >=2\""
, "/home/username/llvm-project/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp"
, 1005, __extension__ __PRETTY_FUNCTION__))
;
1006
1007 // Cost model is not run in the VPlan-native path - return conservative
1008 // result until this changes.
1009 if (EnableVPlanNativePath)
1010 return CM_GatherScatter;
1011
1012 std::pair<Instruction *, unsigned> InstOnVF = std::make_pair(I, VF);
1013 auto Itr = WideningDecisions.find(InstOnVF);
1014 if (Itr == WideningDecisions.end())
1015 return CM_Unknown;
1016 return Itr->second.first;
1017 }
1018
1019 /// Return the vectorization cost for the given instruction \p I and vector
1020 /// width \p VF.
1021 unsigned getWideningCost(Instruction *I, unsigned VF) {
1022 assert(VF >= 2 && "Expected VF >=2")(static_cast <bool> (VF >= 2 && "Expected VF >=2"
) ? void (0) : __assert_fail ("VF >= 2 && \"Expected VF >=2\""
, "/home/username/llvm-project/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp"
, 1022, __extension__ __PRETTY_FUNCTION__))
;
1023 std::pair<Instruction *, unsigned> InstOnVF = std::make_pair(I, VF);
1024 assert(WideningDecisions.find(InstOnVF) != WideningDecisions.end() &&(static_cast <bool> (WideningDecisions.find(InstOnVF) !=
WideningDecisions.end() && "The cost is not calculated"
) ? void (0) : __assert_fail ("WideningDecisions.find(InstOnVF) != WideningDecisions.end() && \"The cost is not calculated\""
, "/home/username/llvm-project/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp"
, 1025, __extension__ __PRETTY_FUNCTION__))
1025 "The cost is not calculated")(static_cast <bool> (WideningDecisions.find(InstOnVF) !=
WideningDecisions.end() && "The cost is not calculated"
) ? void (0) : __assert_fail ("WideningDecisions.find(InstOnVF) != WideningDecisions.end() && \"The cost is not calculated\""
, "/home/username/llvm-project/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp"
, 1025, __extension__ __PRETTY_FUNCTION__))
;
1026 return WideningDecisions[InstOnVF].second;
1027 }
1028
1029 /// Return True if instruction \p I is an optimizable truncate whose operand
1030 /// is an induction variable. Such a truncate will be removed by adding a new
1031 /// induction variable with the destination type.
1032 bool isOptimizableIVTruncate(Instruction *I, unsigned VF) {
1033 // If the instruction is not a truncate, return false.
1034 auto *Trunc = dyn_cast<TruncInst>(I);
1035 if (!Trunc)
1036 return false;
1037
1038 // Get the source and destination types of the truncate.
1039 Type *SrcTy = ToVectorTy(cast<CastInst>(I)->getSrcTy(), VF);
1040 Type *DestTy = ToVectorTy(cast<CastInst>(I)->getDestTy(), VF);
1041
1042 // If the truncate is free for the given types, return false. Replacing a
1043 // free truncate with an induction variable would add an induction variable
1044 // update instruction to each iteration of the loop. We exclude from this
1045 // check the primary induction variable since it will need an update
1046 // instruction regardless.
1047 Value *Op = Trunc->getOperand(0);
1048 if (Op != Legal->getPrimaryInduction() && TTI.isTruncateFree(SrcTy, DestTy))
1049 return false;
1050
1051 // If the truncated value is not an induction variable, return false.
1052 return Legal->isInductionPhi(Op);
1053 }
1054
1055 /// Collects the instructions to scalarize for each predicated instruction in
1056 /// the loop.
1057 void collectInstsToScalarize(unsigned VF);
1058
1059 /// Collect Uniform and Scalar values for the given \p VF.
1060 /// The sets depend on CM decision for Load/Store instructions
1061 /// that may be vectorized as interleave, gather-scatter or scalarized.
1062 void collectUniformsAndScalars(unsigned VF) {
1063 // Do the analysis once.
1064 if (VF == 1 || Uniforms.find(VF) != Uniforms.end())
1065 return;
1066 setCostBasedWideningDecision(VF);
1067 collectLoopUniforms(VF);
1068 collectLoopScalars(VF);
1069 }
1070
1071 /// Returns true if the target machine supports masked store operation
1072 /// for the given \p DataType and kind of access to \p Ptr.
1073 bool isLegalMaskedStore(Type *DataType, Value *Ptr) {
1074 return Legal->isConsecutivePtr(Ptr) && TTI.isLegalMaskedStore(DataType);
1075 }
1076
1077 /// Returns true if the target machine supports masked load operation
1078 /// for the given \p DataType and kind of access to \p Ptr.
1079 bool isLegalMaskedLoad(Type *DataType, Value *Ptr) {
1080 return Legal->isConsecutivePtr(Ptr) && TTI.isLegalMaskedLoad(DataType);
1081 }
1082
1083 /// Returns true if the target machine supports masked scatter operation
1084 /// for the given \p DataType.
1085 bool isLegalMaskedScatter(Type *DataType) {
1086 return TTI.isLegalMaskedScatter(DataType);
1087 }
1088
1089 /// Returns true if the target machine supports masked gather operation
1090 /// for the given \p DataType.
1091 bool isLegalMaskedGather(Type *DataType) {
1092 return TTI.isLegalMaskedGather(DataType);
1093 }
1094
1095 /// Returns true if the target machine can represent \p V as a masked gather
1096 /// or scatter operation.
1097 bool isLegalGatherOrScatter(Value *V) {
1098 bool LI = isa<LoadInst>(V);
1099 bool SI = isa<StoreInst>(V);
1100 if (!LI && !SI)
1101 return false;
1102 auto *Ty = getMemInstValueType(V);
1103 return (LI && isLegalMaskedGather(Ty)) || (SI && isLegalMaskedScatter(Ty));
1104 }
1105
1106 /// Returns true if \p I is an instruction that will be scalarized with
1107 /// predication. Such instructions include conditional stores and
1108 /// instructions that may divide by zero.
1109 /// If a non-zero VF has been calculated, we check if I will be scalarized
1110 /// predication for that VF.
1111 bool isScalarWithPredication(Instruction *I, unsigned VF = 1);
1112
1113 // Returns true if \p I is an instruction that will be predicated either
1114 // through scalar predication or masked load/store or masked gather/scatter.
1115 // Superset of instructions that return true for isScalarWithPredication.
1116 bool isPredicatedInst(Instruction *I) {
1117 if (!blockNeedsPredication(I->getParent()))
1118 return false;
1119 // Loads and stores that need some form of masked operation are predicated
1120 // instructions.
1121 if (isa<LoadInst>(I) || isa<StoreInst>(I))
1122 return Legal->isMaskRequired(I);
1123 return isScalarWithPredication(I);
1124 }
1125
1126 /// Returns true if \p I is a memory instruction with consecutive memory
1127 /// access that can be widened.
1128 bool memoryInstructionCanBeWidened(Instruction *I, unsigned VF = 1);
1129
1130 /// Returns true if \p I is a memory instruction in an interleaved-group
1131 /// of memory accesses that can be vectorized with wide vector loads/stores
1132 /// and shuffles.
1133 bool interleavedAccessCanBeWidened(Instruction *I, unsigned VF = 1);
1134
1135 /// Check if \p Instr belongs to any interleaved access group.
1136 bool isAccessInterleaved(Instruction *Instr) {
1137 return InterleaveInfo.isInterleaved(Instr);
1138 }
1139
1140 /// Get the interleaved access group that \p Instr belongs to.
1141 const InterleaveGroup<Instruction> *
1142 getInterleavedAccessGroup(Instruction *Instr) {
1143 return InterleaveInfo.getInterleaveGroup(Instr);
1144 }
1145
1146 /// Returns true if an interleaved group requires a scalar iteration
1147 /// to handle accesses with gaps, and there is nothing preventing us from
1148 /// creating a scalar epilogue.
1149 bool requiresScalarEpilogue() const {
1150 return IsScalarEpilogueAllowed && InterleaveInfo.requiresScalarEpilogue();
1151 }
1152
1153 /// Returns true if a scalar epilogue is not allowed due to optsize.
1154 bool isScalarEpilogueAllowed() const { return IsScalarEpilogueAllowed; }
1155
1156 /// Returns true if all loop blocks should be masked to fold tail loop.
1157 bool foldTailByMasking() const { return FoldTailByMasking; }
1158
1159 bool blockNeedsPredication(BasicBlock *BB) {
1160 return foldTailByMasking() || Legal->blockNeedsPredication(BB);
1161 }
1162
1163private:
1164 unsigned NumPredStores = 0;
1165
1166 /// \return An upper bound for the vectorization factor, larger than zero.
1167 /// One is returned if vectorization should best be avoided due to cost.
1168 unsigned computeFeasibleMaxVF(bool OptForSize, unsigned ConstTripCount);
1169
1170 /// The vectorization cost is a combination of the cost itself and a boolean
1171 /// indicating whether any of the contributing operations will actually
1172 /// operate on
1173 /// vector values after type legalization in the backend. If this latter value
1174 /// is
1175 /// false, then all operations will be scalarized (i.e. no vectorization has
1176 /// actually taken place).
1177 using VectorizationCostTy = std::pair<unsigned, bool>;
1178
1179 /// Returns the expected execution cost. The unit of the cost does
1180 /// not matter because we use the 'cost' units to compare different
1181 /// vector widths. The cost that is returned is *not* normalized by
1182 /// the factor width.
1183 VectorizationCostTy expectedCost(unsigned VF);
1184
1185 /// Returns the execution time cost of an instruction for a given vector
1186 /// width. Vector width of one means scalar.
1187 VectorizationCostTy getInstructionCost(Instruction *I, unsigned VF);
1188
1189 /// The cost-computation logic from getInstructionCost which provides
1190 /// the vector type as an output parameter.
1191 unsigned getInstructionCost(Instruction *I, unsigned VF, Type *&VectorTy);
1192
1193 /// Calculate vectorization cost of memory instruction \p I.
1194 unsigned getMemoryInstructionCost(Instruction *I, unsigned VF);
1195
1196 /// The cost computation for scalarized memory instruction.
1197 unsigned getMemInstScalarizationCost(Instruction *I, unsigned VF);
1198
1199 /// The cost computation for interleaving group of memory instructions.
1200 unsigned getInterleaveGroupCost(Instruction *I, unsigned VF);
1201
1202 /// The cost computation for Gather/Scatter instruction.
1203 unsigned getGatherScatterCost(Instruction *I, unsigned VF);
1204
1205 /// The cost computation for widening instruction \p I with consecutive
1206 /// memory access.
1207 unsigned getConsecutiveMemOpCost(Instruction *I, unsigned VF);
1208
1209 /// The cost calculation for Load/Store instruction \p I with uniform pointer -
1210 /// Load: scalar load + broadcast.
1211 /// Store: scalar store + (loop invariant value stored? 0 : extract of last
1212 /// element)
1213 unsigned getUniformMemOpCost(Instruction *I, unsigned VF);
1214
1215 /// Returns whether the instruction is a load or store and will be a emitted
1216 /// as a vector operation.
1217 bool isConsecutiveLoadOrStore(Instruction *I);
1218
1219 /// Returns true if an artificially high cost for emulated masked memrefs
1220 /// should be used.
1221 bool useEmulatedMaskMemRefHack(Instruction *I);
1222
1223 /// Create an analysis remark that explains why vectorization failed
1224 ///
1225 /// \p RemarkName is the identifier for the remark. \return the remark object
1226 /// that can be streamed to.
1227 OptimizationRemarkAnalysis createMissedAnalysis(StringRef RemarkName) {
1228 return createLVMissedAnalysis(Hints->vectorizeAnalysisPassName(),
1229 RemarkName, TheLoop);
1230 }
1231
1232 /// Map of scalar integer values to the smallest bitwidth they can be legally
1233 /// represented as. The vector equivalents of these values should be truncated
1234 /// to this type.
1235 MapVector<Instruction *, uint64_t> MinBWs;
1236
1237 /// A type representing the costs for instructions if they were to be
1238 /// scalarized rather than vectorized. The entries are Instruction-Cost
1239 /// pairs.
1240 using ScalarCostsTy = DenseMap<Instruction *, unsigned>;
1241
1242 /// A set containing all BasicBlocks that are known to present after
1243 /// vectorization as a predicated block.
1244 SmallPtrSet<BasicBlock *, 4> PredicatedBBsAfterVectorization;
1245
1246 /// Records whether it is allowed to have the original scalar loop execute at
1247 /// least once. This may be needed as a fallback loop in case runtime
1248 /// aliasing/dependence checks fail, or to handle the tail/remainder
1249 /// iterations when the trip count is unknown or doesn't divide by the VF,
1250 /// or as a peel-loop to handle gaps in interleave-groups.
1251 /// Under optsize and when the trip count is very small we don't allow any
1252 /// iterations to execute in the scalar loop.
1253 bool IsScalarEpilogueAllowed = true;
1254
1255 /// All blocks of loop are to be masked to fold tail of scalar iterations.
1256 bool FoldTailByMasking = false;
1257
1258 /// A map holding scalar costs for different vectorization factors. The
1259 /// presence of a cost for an instruction in the mapping indicates that the
1260 /// instruction will be scalarized when vectorizing with the associated
1261 /// vectorization factor. The entries are VF-ScalarCostTy pairs.
1262 DenseMap<unsigned, ScalarCostsTy> InstsToScalarize;
1263
1264 /// Holds the instructions known to be uniform after vectorization.
1265 /// The data is collected per VF.
1266 DenseMap<unsigned, SmallPtrSet<Instruction *, 4>> Uniforms;
1267
1268 /// Holds the instructions known to be scalar after vectorization.
1269 /// The data is collected per VF.
1270 DenseMap<unsigned, SmallPtrSet<Instruction *, 4>> Scalars;
1271
1272 /// Holds the instructions (address computations) that are forced to be
1273 /// scalarized.
1274 DenseMap<unsigned, SmallPtrSet<Instruction *, 4>> ForcedScalars;
1275
1276 /// Returns the expected difference in cost from scalarizing the expression
1277 /// feeding a predicated instruction \p PredInst. The instructions to
1278 /// scalarize and their scalar costs are collected in \p ScalarCosts. A
1279 /// non-negative return value implies the expression will be scalarized.
1280 /// Currently, only single-use chains are considered for scalarization.
1281 int computePredInstDiscount(Instruction *PredInst, ScalarCostsTy &ScalarCosts,
1282 unsigned VF);
1283
1284 /// Collect the instructions that are uniform after vectorization. An
1285 /// instruction is uniform if we represent it with a single scalar value in
1286 /// the vectorized loop corresponding to each vector iteration. Examples of
1287 /// uniform instructions include pointer operands of consecutive or
1288 /// interleaved memory accesses. Note that although uniformity implies an
1289 /// instruction will be scalar, the reverse is not true. In general, a
1290 /// scalarized instruction will be represented by VF scalar values in the
1291 /// vectorized loop, each corresponding to an iteration of the original
1292 /// scalar loop.
1293 void collectLoopUniforms(unsigned VF);
1294
1295 /// Collect the instructions that are scalar after vectorization. An
1296 /// instruction is scalar if it is known to be uniform or will be scalarized
1297 /// during vectorization. Non-uniform scalarized instructions will be
1298 /// represented by VF values in the vectorized loop, each corresponding to an
1299 /// iteration of the original scalar loop.
1300 void collectLoopScalars(unsigned VF);
1301
1302 /// Keeps cost model vectorization decision and cost for instructions.
1303 /// Right now it is used for memory instructions only.
1304 using DecisionList = DenseMap<std::pair<Instruction *, unsigned>,
1305 std::pair<InstWidening, unsigned>>;
1306
1307 DecisionList WideningDecisions;
1308
1309public:
1310 /// The loop that we evaluate.
1311 Loop *TheLoop;
1312
1313 /// Predicated scalar evolution analysis.
1314 PredicatedScalarEvolution &PSE;
1315
1316 /// Loop Info analysis.
1317 LoopInfo *LI;
1318
1319 /// Vectorization legality.
1320 LoopVectorizationLegality *Legal;
1321
1322 /// Vector target information.
1323 const TargetTransformInfo &TTI;
1324
1325 /// Target Library Info.
1326 const TargetLibraryInfo *TLI;
1327
1328 /// Demanded bits analysis.
1329 DemandedBits *DB;
1330
1331 /// Assumption cache.
1332 AssumptionCache *AC;
1333
1334 /// Interface to emit optimization remarks.
1335 OptimizationRemarkEmitter *ORE;
1336
1337 const Function *TheFunction;
1338
1339 /// Loop Vectorize Hint.
1340 const LoopVectorizeHints *Hints;
1341
1342 /// The interleave access information contains groups of interleaved accesses
1343 /// with the same stride and close to each other.
1344 InterleavedAccessInfo &InterleaveInfo;
1345
1346 /// Values to ignore in the cost model.
1347 SmallPtrSet<const Value *, 16> ValuesToIgnore;
1348
1349 /// Values to ignore in the cost model when VF > 1.
1350 SmallPtrSet<const Value *, 16> VecValuesToIgnore;
1351};
1352
1353} // end namespace llvm
1354
1355// Return true if \p OuterLp is an outer loop annotated with hints for explicit
1356// vectorization. The loop needs to be annotated with #pragma omp simd
1357// simdlen(#) or #pragma clang vectorize(enable) vectorize_width(#). If the
1358// vector length information is not provided, vectorization is not considered
1359// explicit. Interleave hints are not allowed either. These limitations will be
1360// relaxed in the future.
1361// Please, note that we are currently forced to abuse the pragma 'clang
1362// vectorize' semantics. This pragma provides *auto-vectorization hints*
1363// (i.e., LV must check that vectorization is legal) whereas pragma 'omp simd'
1364// provides *explicit vectorization hints* (LV can bypass legal checks and
1365// assume that vectorization is legal). However, both hints are implemented
1366// using the same metadata (llvm.loop.vectorize, processed by
1367// LoopVectorizeHints). This will be fixed in the future when the native IR
1368// representation for pragma 'omp simd' is introduced.
1369static bool isExplicitVecOuterLoop(Loop *OuterLp,
1370 OptimizationRemarkEmitter *ORE) {
1371 assert(!OuterLp->empty() && "This is not an outer loop")(static_cast <bool> (!OuterLp->empty() && "This is not an outer loop"
) ? void (0) : __assert_fail ("!OuterLp->empty() && \"This is not an outer loop\""
, "/home/username/llvm-project/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp"
, 1371, __extension__ __PRETTY_FUNCTION__))
;
1372 LoopVectorizeHints Hints(OuterLp, true /*DisableInterleaving*/, *ORE);
1373
1374 // Only outer loops with an explicit vectorization hint are supported.
1375 // Unannotated outer loops are ignored.
1376 if (Hints.getForce() == LoopVectorizeHints::FK_Undefined)
1377 return false;
1378
1379 Function *Fn = OuterLp->getHeader()->getParent();
1380 if (!Hints.allowVectorization(Fn, OuterLp,
1381 true /*VectorizeOnlyWhenForced*/)) {
1382 LLVM_DEBUG(dbgs() << "LV: Loop hints prevent outer loop vectorization.\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("loop-vectorize")) { dbgs() << "LV: Loop hints prevent outer loop vectorization.\n"
; } } while (false)
;
1383 return false;
1384 }
1385
1386 if (Hints.getInterleave() > 1) {
1387 // TODO: Interleave support is future work.
1388 LLVM_DEBUG(dbgs() << "LV: Not vectorizing: Interleave is not supported for "do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("loop-vectorize")) { dbgs() << "LV: Not vectorizing: Interleave is not supported for "
"outer loops.\n"; } } while (false)
1389 "outer loops.\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("loop-vectorize")) { dbgs() << "LV: Not vectorizing: Interleave is not supported for "
"outer loops.\n"; } } while (false)
;
1390 Hints.emitRemarkWithHints();
1391 return false;
1392 }
1393
1394 return true;
1395}
1396
1397static void collectSupportedLoops(Loop &L, LoopInfo *LI,
1398 OptimizationRemarkEmitter *ORE,
1399 SmallVectorImpl<Loop *> &V) {
1400 // Collect inner loops and outer loops without irreducible control flow. For
1401 // now, only collect outer loops that have explicit vectorization hints. If we
1402 // are stress testing the VPlan H-CFG construction, we collect the outermost
1403 // loop of every loop nest.
1404 if (L.empty() || VPlanBuildStressTest ||
1405 (EnableVPlanNativePath && isExplicitVecOuterLoop(&L, ORE))) {
1406 LoopBlocksRPO RPOT(&L);
1407 RPOT.perform(LI);
1408 if (!containsIrreducibleCFG<const BasicBlock *>(RPOT, *LI)) {
1409 V.push_back(&L);
1410 // TODO: Collect inner loops inside marked outer loops in case
1411 // vectorization fails for the outer loop. Do not invoke
1412 // 'containsIrreducibleCFG' again for inner loops when the outer loop is
1413 // already known to be reducible. We can use an inherited attribute for
1414 // that.
1415 return;
1416 }
1417 }
1418 for (Loop *InnerL : L)
1419 collectSupportedLoops(*InnerL, LI, ORE, V);
1420}
1421
1422namespace {
1423
1424/// The LoopVectorize Pass.
1425struct LoopVectorize : public FunctionPass {
1426 /// Pass identification, replacement for typeid
1427 static char ID;
1428
1429 LoopVectorizePass Impl;
1430
1431 explicit LoopVectorize(bool InterleaveOnlyWhenForced = false,
1432 bool VectorizeOnlyWhenForced = false)
1433 : FunctionPass(ID) {
1434 Impl.InterleaveOnlyWhenForced = InterleaveOnlyWhenForced;
1435 Impl.VectorizeOnlyWhenForced = VectorizeOnlyWhenForced;
1436 initializeLoopVectorizePass(*PassRegistry::getPassRegistry());
1437 }
1438
1439 bool runOnFunction(Function &F) override {
1440 if (skipFunction(F))
1441 return false;
1442
1443 auto *SE = &getAnalysis<ScalarEvolutionWrapperPass>().getSE();
1444 auto *LI = &getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
1445 auto *TTI = &getAnalysis<TargetTransformInfoWrapperPass>().getTTI(F);
1446 auto *DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree();
1447 auto *BFI = &getAnalysis<BlockFrequencyInfoWrapperPass>().getBFI();
1448 auto *TLIP = getAnalysisIfAvailable<TargetLibraryInfoWrapperPass>();
1449 auto *TLI = TLIP ? &TLIP->getTLI() : nullptr;
1450 auto *AA = &getAnalysis<AAResultsWrapperPass>().getAAResults();
1451 auto *AC = &getAnalysis<AssumptionCacheTracker>().getAssumptionCache(F);
1452 auto *LAA = &getAnalysis<LoopAccessLegacyAnalysis>();
1453 auto *DB = &getAnalysis<DemandedBitsWrapperPass>().getDemandedBits();
1454 auto *ORE = &getAnalysis<OptimizationRemarkEmitterWrapperPass>().getORE();
1455
1456 std::function<const LoopAccessInfo &(Loop &)> GetLAA =
1457 [&](Loop &L) -> const LoopAccessInfo & { return LAA->getInfo(&L); };
1458
1459 return Impl.runImpl(F, *SE, *LI, *TTI, *DT, *BFI, TLI, *DB, *AA, *AC,
1460 GetLAA, *ORE);
1461 }
1462
1463 void getAnalysisUsage(AnalysisUsage &AU) const override {
1464 AU.addRequired<AssumptionCacheTracker>();
1465 AU.addRequired<BlockFrequencyInfoWrapperPass>();
1466 AU.addRequired<DominatorTreeWrapperPass>();
1467 AU.addRequired<LoopInfoWrapperPass>();
1468 AU.addRequired<ScalarEvolutionWrapperPass>();
1469 AU.addRequired<TargetTransformInfoWrapperPass>();
1470 AU.addRequired<AAResultsWrapperPass>();
1471 AU.addRequired<LoopAccessLegacyAnalysis>();
1472 AU.addRequired<DemandedBitsWrapperPass>();
1473 AU.addRequired<OptimizationRemarkEmitterWrapperPass>();
1474
1475 // We currently do not preserve loopinfo/dominator analyses with outer loop
1476 // vectorization. Until this is addressed, mark these analyses as preserved
1477 // only for non-VPlan-native path.
1478 // TODO: Preserve Loop and Dominator analyses for VPlan-native path.
1479 if (!EnableVPlanNativePath) {
1480 AU.addPreserved<LoopInfoWrapperPass>();
1481 AU.addPreserved<DominatorTreeWrapperPass>();
1482 }
1483
1484 AU.addPreserved<BasicAAWrapperPass>();
1485 AU.addPreserved<GlobalsAAWrapperPass>();
1486 }
1487};
1488
1489} // end anonymous namespace
1490
1491//===----------------------------------------------------------------------===//
1492// Implementation of LoopVectorizationLegality, InnerLoopVectorizer and
1493// LoopVectorizationCostModel and LoopVectorizationPlanner.
1494//===----------------------------------------------------------------------===//
1495
1496Value *InnerLoopVectorizer::getBroadcastInstrs(Value *V) {
1497 // We need to place the broadcast of invariant variables outside the loop,
1498 // but only if it's proven safe to do so. Else, broadcast will be inside
1499 // vector loop body.
1500 Instruction *Instr = dyn_cast<Instruction>(V);
1501 bool SafeToHoist = OrigLoop->isLoopInvariant(V) &&
1502 (!Instr ||
1503 DT->dominates(Instr->getParent(), LoopVectorPreHeader));
1504 // Place the code for broadcasting invariant variables in the new preheader.
1505 IRBuilder<>::InsertPointGuard Guard(Builder);
1506 if (SafeToHoist)
1507 Builder.SetInsertPoint(LoopVectorPreHeader->getTerminator());
1508
1509 // Broadcast the scalar into all locations in the vector.
1510 Value *Shuf = Builder.CreateVectorSplat(VF, V, "broadcast");
1511
1512 return Shuf;
1513}
1514
1515void InnerLoopVectorizer::createVectorIntOrFpInductionPHI(
1516 const InductionDescriptor &II, Value *Step, Instruction *EntryVal) {
1517 assert((isa<PHINode>(EntryVal) || isa<TruncInst>(EntryVal)) &&(static_cast <bool> ((isa<PHINode>(EntryVal) || isa
<TruncInst>(EntryVal)) && "Expected either an induction phi-node or a truncate of it!"
) ? void (0) : __assert_fail ("(isa<PHINode>(EntryVal) || isa<TruncInst>(EntryVal)) && \"Expected either an induction phi-node or a truncate of it!\""
, "/home/username/llvm-project/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp"
, 1518, __extension__ __PRETTY_FUNCTION__))
1518 "Expected either an induction phi-node or a truncate of it!")(static_cast <bool> ((isa<PHINode>(EntryVal) || isa
<TruncInst>(EntryVal)) && "Expected either an induction phi-node or a truncate of it!"
) ? void (0) : __assert_fail ("(isa<PHINode>(EntryVal) || isa<TruncInst>(EntryVal)) && \"Expected either an induction phi-node or a truncate of it!\""
, "/home/username/llvm-project/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp"
, 1518, __extension__ __PRETTY_FUNCTION__))
;
1519 Value *Start = II.getStartValue();
1520
1521 // Construct the initial value of the vector IV in the vector loop preheader
1522 auto CurrIP = Builder.saveIP();
1523 Builder.SetInsertPoint(LoopVectorPreHeader->getTerminator());
1524 if (isa<TruncInst>(EntryVal)) {
1525 assert(Start->getType()->isIntegerTy() &&(static_cast <bool> (Start->getType()->isIntegerTy
() && "Truncation requires an integer type") ? void (
0) : __assert_fail ("Start->getType()->isIntegerTy() && \"Truncation requires an integer type\""
, "/home/username/llvm-project/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp"
, 1526, __extension__ __PRETTY_FUNCTION__))
1526 "Truncation requires an integer type")(static_cast <bool> (Start->getType()->isIntegerTy
() && "Truncation requires an integer type") ? void (
0) : __assert_fail ("Start->getType()->isIntegerTy() && \"Truncation requires an integer type\""
, "/home/username/llvm-project/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp"
, 1526, __extension__ __PRETTY_FUNCTION__))
;
1527 auto *TruncType = cast<IntegerType>(EntryVal->getType());
1528 Step = Builder.CreateTrunc(Step, TruncType);
1529 Start = Builder.CreateCast(Instruction::Trunc, Start, TruncType);
1530 }
1531 Value *SplatStart = Builder.CreateVectorSplat(VF, Start);
1532 Value *SteppedStart =
1533 getStepVector(SplatStart, 0, Step, II.getInductionOpcode());
1534
1535 // We create vector phi nodes for both integer and floating-point induction
1536 // variables. Here, we determine the kind of arithmetic we will perform.
1537 Instruction::BinaryOps AddOp;
1538 Instruction::BinaryOps MulOp;
1539 if (Step->getType()->isIntegerTy()) {
1540 AddOp = Instruction::Add;
1541 MulOp = Instruction::Mul;
1542 } else {
1543 AddOp = II.getInductionOpcode();
1544 MulOp = Instruction::FMul;
1545 }
1546
1547 // Multiply the vectorization factor by the step using integer or
1548 // floating-point arithmetic as appropriate.
1549 Value *ConstVF = getSignedIntOrFpConstant(Step->getType(), VF);
1550 Value *Mul = addFastMathFlag(Builder.CreateBinOp(MulOp, Step, ConstVF));
1551
1552 // Create a vector splat to use in the induction update.
1553 //
1554 // FIXME: If the step is non-constant, we create the vector splat with
1555 // IRBuilder. IRBuilder can constant-fold the multiply, but it doesn't
1556 // handle a constant vector splat.
1557 Value *SplatVF = isa<Constant>(Mul)
1558 ? ConstantVector::getSplat(VF, cast<Constant>(Mul))
1559 : Builder.CreateVectorSplat(VF, Mul);
1560 Builder.restoreIP(CurrIP);
1561
1562 // We may need to add the step a number of times, depending on the unroll
1563 // factor. The last of those goes into the PHI.
1564 PHINode *VecInd = PHINode::Create(SteppedStart->getType(), 2, "vec.ind",
1565 &*LoopVectorBody->getFirstInsertionPt());
1566 VecInd->setDebugLoc(EntryVal->getDebugLoc());
1567 Instruction *LastInduction = VecInd;
1568 for (unsigned Part = 0; Part < UF; ++Part) {
1569 VectorLoopValueMap.setVectorValue(EntryVal, Part, LastInduction);
1570
1571 if (isa<TruncInst>(EntryVal))
1572 addMetadata(LastInduction, EntryVal);
1573 recordVectorLoopValueForInductionCast(II, EntryVal, LastInduction, Part);
1574
1575 LastInduction = cast<Instruction>(addFastMathFlag(
1576 Builder.CreateBinOp(AddOp, LastInduction, SplatVF, "step.add")));
1577 LastInduction->setDebugLoc(EntryVal->getDebugLoc());
1578 }
1579
1580 // Move the last step to the end of the latch block. This ensures consistent
1581 // placement of all induction updates.
1582 auto *LoopVectorLatch = LI->getLoopFor(LoopVectorBody)->getLoopLatch();
1583 auto *Br = cast<BranchInst>(LoopVectorLatch->getTerminator());
1584 auto *ICmp = cast<Instruction>(Br->getCondition());
1585 LastInduction->moveBefore(ICmp);
1586 LastInduction->setName("vec.ind.next");
1587
1588 VecInd->addIncoming(SteppedStart, LoopVectorPreHeader);
1589 VecInd->addIncoming(LastInduction, LoopVectorLatch);
1590}
1591
1592bool InnerLoopVectorizer::shouldScalarizeInstruction(Instruction *I) const {
1593 return Cost->isScalarAfterVectorization(I, VF) ||
1594 Cost->isProfitableToScalarize(I, VF);
1595}
1596
1597bool InnerLoopVectorizer::needsScalarInduction(Instruction *IV) const {
1598 if (shouldScalarizeInstruction(IV))
1599 return true;
1600 auto isScalarInst = [&](User *U) -> bool {
1601 auto *I = cast<Instruction>(U);
1602 return (OrigLoop->contains(I) && shouldScalarizeInstruction(I));
1603 };
1604 return llvm::any_of(IV->users(), isScalarInst);
1605}
1606
1607void InnerLoopVectorizer::recordVectorLoopValueForInductionCast(
1608 const InductionDescriptor &ID, const Instruction *EntryVal,
1609 Value *VectorLoopVal, unsigned Part, unsigned Lane) {
1610 assert((isa<PHINode>(EntryVal) || isa<TruncInst>(EntryVal)) &&(static_cast <bool> ((isa<PHINode>(EntryVal) || isa
<TruncInst>(EntryVal)) && "Expected either an induction phi-node or a truncate of it!"
) ? void (0) : __assert_fail ("(isa<PHINode>(EntryVal) || isa<TruncInst>(EntryVal)) && \"Expected either an induction phi-node or a truncate of it!\""
, "/home/username/llvm-project/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp"
, 1611, __extension__ __PRETTY_FUNCTION__))
1611 "Expected either an induction phi-node or a truncate of it!")(static_cast <bool> ((isa<PHINode>(EntryVal) || isa
<TruncInst>(EntryVal)) && "Expected either an induction phi-node or a truncate of it!"
) ? void (0) : __assert_fail ("(isa<PHINode>(EntryVal) || isa<TruncInst>(EntryVal)) && \"Expected either an induction phi-node or a truncate of it!\""
, "/home/username/llvm-project/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp"
, 1611, __extension__ __PRETTY_FUNCTION__))
;
1612
1613 // This induction variable is not the phi from the original loop but the
1614 // newly-created IV based on the proof that casted Phi is equal to the
1615 // uncasted Phi in the vectorized loop (under a runtime guard possibly). It
1616 // re-uses the same InductionDescriptor that original IV uses but we don't
1617 // have to do any recording in this case - that is done when original IV is
1618 // processed.
1619 if (isa<TruncInst>(EntryVal))
1620 return;
1621
1622 const SmallVectorImpl<Instruction *> &Casts = ID.getCastInsts();
1623 if (Casts.empty())
1624 return;
1625 // Only the first Cast instruction in the Casts vector is of interest.
1626 // The rest of the Casts (if exist) have no uses outside the
1627 // induction update chain itself.
1628 Instruction *CastInst = *Casts.begin();
1629 if (Lane < UINT_MAX(2147483647 *2U +1U))
1630 VectorLoopValueMap.setScalarValue(CastInst, {Part, Lane}, VectorLoopVal);
1631 else
1632 VectorLoopValueMap.setVectorValue(CastInst, Part, VectorLoopVal);
1633}
1634
1635void InnerLoopVectorizer::widenIntOrFpInduction(PHINode *IV, TruncInst *Trunc) {
1636 assert((IV->getType()->isIntegerTy() || IV != OldInduction) &&(static_cast <bool> ((IV->getType()->isIntegerTy(
) || IV != OldInduction) && "Primary induction variable must have an integer type"
) ? void (0) : __assert_fail ("(IV->getType()->isIntegerTy() || IV != OldInduction) && \"Primary induction variable must have an integer type\""
, "/home/username/llvm-project/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp"
, 1637, __extension__ __PRETTY_FUNCTION__))
1637 "Primary induction variable must have an integer type")(static_cast <bool> ((IV->getType()->isIntegerTy(
) || IV != OldInduction) && "Primary induction variable must have an integer type"
) ? void (0) : __assert_fail ("(IV->getType()->isIntegerTy() || IV != OldInduction) && \"Primary induction variable must have an integer type\""
, "/home/username/llvm-project/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp"
, 1637, __extension__ __PRETTY_FUNCTION__))
;
1638
1639 auto II = Legal->getInductionVars()->find(IV);
1640 assert(II != Legal->getInductionVars()->end() && "IV is not an induction")(static_cast <bool> (II != Legal->getInductionVars()
->end() && "IV is not an induction") ? void (0) : __assert_fail
("II != Legal->getInductionVars()->end() && \"IV is not an induction\""
, "/home/username/llvm-project/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp"
, 1640, __extension__ __PRETTY_FUNCTION__))
;
1641
1642 auto ID = II->second;
1643 assert(IV->getType() == ID.getStartValue()->getType() && "Types must match")(static_cast <bool> (IV->getType() == ID.getStartValue
()->getType() && "Types must match") ? void (0) : __assert_fail
("IV->getType() == ID.getStartValue()->getType() && \"Types must match\""
, "/home/username/llvm-project/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp"
, 1643, __extension__ __PRETTY_FUNCTION__))
;
1644
1645 // The scalar value to broadcast. This will be derived from the canonical
1646 // induction variable.
1647 Value *ScalarIV = nullptr;
1648
1649 // The value from the original loop to which we are mapping the new induction
1650 // variable.
1651 Instruction *EntryVal = Trunc ? cast<Instruction>(Trunc) : IV;
1652
1653 // True if we have vectorized the induction variable.
1654 auto VectorizedIV = false;
1655
1656 // Determine if we want a scalar version of the induction variable. This is
1657 // true if the induction variable itself is not widened, or if it has at
1658 // least one user in the loop that is not widened.
1659 auto NeedsScalarIV = VF > 1 && needsScalarInduction(EntryVal);
1660
1661 // Generate code for the induction step. Note that induction steps are
1662 // required to be loop-invariant
1663 assert(PSE.getSE()->isLoopInvariant(ID.getStep(), OrigLoop) &&(static_cast <bool> (PSE.getSE()->isLoopInvariant(ID
.getStep(), OrigLoop) && "Induction step should be loop invariant"
) ? void (0) : __assert_fail ("PSE.getSE()->isLoopInvariant(ID.getStep(), OrigLoop) && \"Induction step should be loop invariant\""
, "/home/username/llvm-project/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp"
, 1664, __extension__ __PRETTY_FUNCTION__))
1664 "Induction step should be loop invariant")(static_cast <bool> (PSE.getSE()->isLoopInvariant(ID
.getStep(), OrigLoop) && "Induction step should be loop invariant"
) ? void (0) : __assert_fail ("PSE.getSE()->isLoopInvariant(ID.getStep(), OrigLoop) && \"Induction step should be loop invariant\""
, "/home/username/llvm-project/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp"
, 1664, __extension__ __PRETTY_FUNCTION__))
;
1665 auto &DL = OrigLoop->getHeader()->getModule()->getDataLayout();
1666 Value *Step = nullptr;
1667 if (PSE.getSE()->isSCEVable(IV->getType())) {
1668 SCEVExpander Exp(*PSE.getSE(), DL, "induction");
1669 Step = Exp.expandCodeFor(ID.getStep(), ID.getStep()->getType(),
1670 LoopVectorPreHeader->getTerminator());
1671 } else {
1672 Step = cast<SCEVUnknown>(ID.getStep())->getValue();
1673 }
1674
1675 // Try to create a new independent vector induction variable. If we can't
1676 // create the phi node, we will splat the scalar induction variable in each
1677 // loop iteration.
1678 if (VF > 1 && !shouldScalarizeInstruction(EntryVal)) {
1679 createVectorIntOrFpInductionPHI(ID, Step, EntryVal);
1680 VectorizedIV = true;
1681 }
1682
1683 // If we haven't yet vectorized the induction variable, or if we will create
1684 // a scalar one, we need to define the scalar induction variable and step
1685 // values. If we were given a truncation type, truncate the canonical
1686 // induction variable and step. Otherwise, derive these values from the
1687 // induction descriptor.
1688 if (!VectorizedIV || NeedsScalarIV) {
1689 ScalarIV = Induction;
1690 if (IV != OldInduction) {
1691 ScalarIV = IV->getType()->isIntegerTy()
1692 ? Builder.CreateSExtOrTrunc(Induction, IV->getType())
1693 : Builder.CreateCast(Instruction::SIToFP, Induction,
1694 IV->getType());
1695 ScalarIV = emitTransformedIndex(Builder, ScalarIV, PSE.getSE(), DL, ID);
1696 ScalarIV->setName("offset.idx");
1697 }
1698 if (Trunc) {
1699 auto *TruncType = cast<IntegerType>(Trunc->getType());
1700 assert(Step->getType()->isIntegerTy() &&(static_cast <bool> (Step->getType()->isIntegerTy
() && "Truncation requires an integer step") ? void (
0) : __assert_fail ("Step->getType()->isIntegerTy() && \"Truncation requires an integer step\""
, "/home/username/llvm-project/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp"
, 1701, __extension__ __PRETTY_FUNCTION__))
1701 "Truncation requires an integer step")(static_cast <bool> (Step->getType()->isIntegerTy
() && "Truncation requires an integer step") ? void (
0) : __assert_fail ("Step->getType()->isIntegerTy() && \"Truncation requires an integer step\""
, "/home/username/llvm-project/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp"
, 1701, __extension__ __PRETTY_FUNCTION__))
;
1702 ScalarIV = Builder.CreateTrunc(ScalarIV, TruncType);
1703 Step = Builder.CreateTrunc(Step, TruncType);
1704 }
1705 }
1706
1707 // If we haven't yet vectorized the induction variable, splat the scalar
1708 // induction variable, and build the necessary step vectors.
1709 // TODO: Don't do it unless the vectorized IV is really required.
1710 if (!VectorizedIV) {
1711 Value *Broadcasted = getBroadcastInstrs(ScalarIV);
1712 for (unsigned Part = 0; Part < UF; ++Part) {
1713 Value *EntryPart =
1714 getStepVector(Broadcasted, VF * Part, Step, ID.getInductionOpcode());
1715 VectorLoopValueMap.setVectorValue(EntryVal, Part, EntryPart);
1716 if (Trunc)
1717 addMetadata(EntryPart, Trunc);
1718 recordVectorLoopValueForInductionCast(ID, EntryVal, EntryPart, Part);
1719 }
1720 }
1721
1722 // If an induction variable is only used for counting loop iterations or
1723 // calculating addresses, it doesn't need to be widened. Create scalar steps
1724 // that can be used by instructions we will later scalarize. Note that the
1725 // addition of the scalar steps will not increase the number of instructions
1726 // in the loop in the common case prior to InstCombine. We will be trading
1727 // one vector extract for each scalar step.
1728 if (NeedsScalarIV)
1729 buildScalarSteps(ScalarIV, Step, EntryVal, ID);
1730}
1731
1732Value *InnerLoopVectorizer::getStepVector(Value *Val, int StartIdx, Value *Step,
1733 Instruction::BinaryOps BinOp) {
1734 // Create and check the types.
1735 assert(Val->getType()->isVectorTy() && "Must be a vector")(static_cast <bool> (Val->getType()->isVectorTy()
&& "Must be a vector") ? void (0) : __assert_fail ("Val->getType()->isVectorTy() && \"Must be a vector\""
, "/home/username/llvm-project/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp"
, 1735, __extension__ __PRETTY_FUNCTION__))
;
1736 int VLen = Val->getType()->getVectorNumElements();
1737
1738 Type *STy = Val->getType()->getScalarType();
1739 assert((STy->isIntegerTy() || STy->isFloatingPointTy()) &&(static_cast <bool> ((STy->isIntegerTy() || STy->
isFloatingPointTy()) && "Induction Step must be an integer or FP"
) ? void (0) : __assert_fail ("(STy->isIntegerTy() || STy->isFloatingPointTy()) && \"Induction Step must be an integer or FP\""
, "/home/username/llvm-project/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp"
, 1740, __extension__ __PRETTY_FUNCTION__))
1740 "Induction Step must be an integer or FP")(static_cast <bool> ((STy->isIntegerTy() || STy->
isFloatingPointTy()) && "Induction Step must be an integer or FP"
) ? void (0) : __assert_fail ("(STy->isIntegerTy() || STy->isFloatingPointTy()) && \"Induction Step must be an integer or FP\""
, "/home/username/llvm-project/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp"
, 1740, __extension__ __PRETTY_FUNCTION__))
;
1741 assert(Step->getType() == STy && "Step has wrong type")(static_cast <bool> (Step->getType() == STy &&
"Step has wrong type") ? void (0) : __assert_fail ("Step->getType() == STy && \"Step has wrong type\""
, "/home/username/llvm-project/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp"
, 1741, __extension__ __PRETTY_FUNCTION__))
;
1742
1743 SmallVector<Constant *, 8> Indices;
1744
1745 if (STy->isIntegerTy()) {
1746 // Create a vector of consecutive numbers from zero to VF.
1747 for (int i = 0; i < VLen; ++i)
1748 Indices.push_back(ConstantInt::get(STy, StartIdx + i));
1749
1750 // Add the consecutive indices to the vector value.
1751 Constant *Cv = ConstantVector::get(Indices);
1752 assert(Cv->getType() == Val->getType() && "Invalid consecutive vec")(static_cast <bool> (Cv->getType() == Val->getType
() && "Invalid consecutive vec") ? void (0) : __assert_fail
("Cv->getType() == Val->getType() && \"Invalid consecutive vec\""
, "/home/username/llvm-project/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp"
, 1752, __extension__ __PRETTY_FUNCTION__))
;
1753 Step = Builder.CreateVectorSplat(VLen, Step);
1754 assert(Step->getType() == Val->getType() && "Invalid step vec")(static_cast <bool> (Step->getType() == Val->getType
() && "Invalid step vec") ? void (0) : __assert_fail (
"Step->getType() == Val->getType() && \"Invalid step vec\""
, "/home/username/llvm-project/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp"
, 1754, __extension__ __PRETTY_FUNCTION__))
;
1755 // FIXME: The newly created binary instructions should contain nsw/nuw flags,
1756 // which can be found from the original scalar operations.
1757 Step = Builder.CreateMul(Cv, Step);
1758 return Builder.CreateAdd(Val, Step, "induction");
1759 }
1760
1761 // Floating point induction.
1762 assert((BinOp == Instruction::FAdd || BinOp == Instruction::FSub) &&(static_cast <bool> ((BinOp == Instruction::FAdd || BinOp
== Instruction::FSub) && "Binary Opcode should be specified for FP induction"
) ? void (0) : __assert_fail ("(BinOp == Instruction::FAdd || BinOp == Instruction::FSub) && \"Binary Opcode should be specified for FP induction\""
, "/home/username/llvm-project/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp"
, 1763, __extension__ __PRETTY_FUNCTION__))
1763 "Binary Opcode should be specified for FP induction")(static_cast <bool> ((BinOp == Instruction::FAdd || BinOp
== Instruction::FSub) && "Binary Opcode should be specified for FP induction"
) ? void (0) : __assert_fail ("(BinOp == Instruction::FAdd || BinOp == Instruction::FSub) && \"Binary Opcode should be specified for FP induction\""
, "/home/username/llvm-project/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp"
, 1763, __extension__ __PRETTY_FUNCTION__))
;
1764 // Create a vector of consecutive numbers from zero to VF.
1765 for (int i = 0; i < VLen; ++i)
1766 Indices.push_back(ConstantFP::get(STy, (double)(StartIdx + i)));
1767
1768 // Add the consecutive indices to the vector value.
1769 Constant *Cv = ConstantVector::get(Indices);
1770
1771 Step = Builder.CreateVectorSplat(VLen, Step);
1772
1773 // Floating point operations had to be 'fast' to enable the induction.
1774 FastMathFlags Flags;
1775 Flags.setFast();
1776
1777 Value *MulOp = Builder.CreateFMul(Cv, Step);
1778 if (isa<Instruction>(MulOp))
1779 // Have to check, MulOp may be a constant
1780 cast<Instruction>(MulOp)->setFastMathFlags(Flags);
1781
1782 Value *BOp = Builder.CreateBinOp(BinOp, Val, MulOp, "induction");
1783 if (isa<Instruction>(BOp))
1784 cast<Instruction>(BOp)->setFastMathFlags(Flags);
1785 return BOp;
1786}
1787
1788void InnerLoopVectorizer::buildScalarSteps(Value *ScalarIV, Value *Step,
1789 Instruction *EntryVal,
1790 const InductionDescriptor &ID) {
1791 // We shouldn't have to build scalar steps if we aren't vectorizing.
1792 assert(VF > 1 && "VF should be greater than one")(static_cast <bool> (VF > 1 && "VF should be greater than one"
) ? void (0) : __assert_fail ("VF > 1 && \"VF should be greater than one\""
, "/home/username/llvm-project/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp"
, 1792, __extension__ __PRETTY_FUNCTION__))
;
1793
1794 // Get the value type and ensure it and the step have the same integer type.
1795 Type *ScalarIVTy = ScalarIV->getType()->getScalarType();
1796 assert(ScalarIVTy == Step->getType() &&(static_cast <bool> (ScalarIVTy == Step->getType() &&
"Val and Step should have the same type") ? void (0) : __assert_fail
("ScalarIVTy == Step->getType() && \"Val and Step should have the same type\""
, "/home/username/llvm-project/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp"
, 1797, __extension__ __PRETTY_FUNCTION__))
1797 "Val and Step should have the same type")(static_cast <bool> (ScalarIVTy == Step->getType() &&
"Val and Step should have the same type") ? void (0) : __assert_fail
("ScalarIVTy == Step->getType() && \"Val and Step should have the same type\""
, "/home/username/llvm-project/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp"
, 1797, __extension__ __PRETTY_FUNCTION__))
;
1798
1799 // We build scalar steps for both integer and floating-point induction
1800 // variables. Here, we determine the kind of arithmetic we will perform.
1801 Instruction::BinaryOps AddOp;
1802 Instruction::BinaryOps MulOp;
1803 if (ScalarIVTy->isIntegerTy()) {
1804 AddOp = Instruction::Add;
1805 MulOp = Instruction::Mul;
1806 } else {
1807 AddOp = ID.getInductionOpcode();
1808 MulOp = Instruction::FMul;
1809 }
1810
1811 // Determine the number of scalars we need to generate for each unroll
1812 // iteration. If EntryVal is uniform, we only need to generate the first
1813 // lane. Otherwise, we generate all VF values.
1814 unsigned Lanes =
1815 Cost->isUniformAfterVectorization(cast<Instruction>(EntryVal), VF) ? 1
1816 : VF;
1817 // Compute the scalar steps and save the results in VectorLoopValueMap.
1818 for (unsigned Part = 0; Part < UF; ++Part) {
1819 for (unsigned Lane = 0; Lane < Lanes; ++Lane) {
1820 auto *StartIdx = getSignedIntOrFpConstant(ScalarIVTy, VF * Part + Lane);
1821 auto *Mul = addFastMathFlag(Builder.CreateBinOp(MulOp, StartIdx, Step));
1822 auto *Add = addFastMathFlag(Builder.CreateBinOp(AddOp, ScalarIV, Mul));
1823 VectorLoopValueMap.setScalarValue(EntryVal, {Part, Lane}, Add);
1824 recordVectorLoopValueForInductionCast(ID, EntryVal, Add, Part, Lane);
1825 }
1826 }
1827}
1828
1829Value *InnerLoopVectorizer::getOrCreateVectorValue(Value *V, unsigned Part) {
1830 assert(V != Induction && "The new induction variable should not be used.")(static_cast <bool> (V != Induction && "The new induction variable should not be used."
) ? void (0) : __assert_fail ("V != Induction && \"The new induction variable should not be used.\""
, "/home/username/llvm-project/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp"
, 1830, __extension__ __PRETTY_FUNCTION__))
;
1831 assert(!V->getType()->isVectorTy() && "Can't widen a vector")(static_cast <bool> (!V->getType()->isVectorTy() &&
"Can't widen a vector") ? void (0) : __assert_fail ("!V->getType()->isVectorTy() && \"Can't widen a vector\""
, "/home/username/llvm-project/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp"
, 1831, __extension__ __PRETTY_FUNCTION__))
;
1832 assert(!V->getType()->isVoidTy() && "Type does not produce a value")(static_cast <bool> (!V->getType()->isVoidTy() &&
"Type does not produce a value") ? void (0) : __assert_fail (
"!V->getType()->isVoidTy() && \"Type does not produce a value\""
, "/home/username/llvm-project/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp"
, 1832, __extension__ __PRETTY_FUNCTION__))
;
1833
1834 // If we have a stride that is replaced by one, do it here. Defer this for
1835 // the VPlan-native path until we start running Legal checks in that path.
1836 if (!EnableVPlanNativePath && Legal->hasStride(V))
1837 V = ConstantInt::get(V->getType(), 1);
1838
1839 // If we have a vector mapped to this value, return it.
1840 if (VectorLoopValueMap.hasVectorValue(V, Part))
1841 return VectorLoopValueMap.getVectorValue(V, Part);
1842
1843 // If the value has not been vectorized, check if it has been scalarized
1844 // instead. If it has been scalarized, and we actually need the value in
1845 // vector form, we will construct the vector values on demand.
1846 if (VectorLoopValueMap.hasAnyScalarValue(V)) {
1847 Value *ScalarValue = VectorLoopValueMap.getScalarValue(V, {Part, 0});
1848
1849 // If we've scalarized a value, that value should be an instruction.
1850 auto *I = cast<Instruction>(V);
1851
1852 // If we aren't vectorizing, we can just copy the scalar map values over to
1853 // the vector map.
1854 if (VF == 1) {
1855 VectorLoopValueMap.setVectorValue(V, Part, ScalarValue);
1856 return ScalarValue;
1857 }
1858
1859 // Get the last scalar instruction we generated for V and Part. If the value
1860 // is known to be uniform after vectorization, this corresponds to lane zero
1861 // of the Part unroll iteration. Otherwise, the last instruction is the one
1862 // we created for the last vector lane of the Part unroll iteration.
1863 unsigned LastLane = Cost->isUniformAfterVectorization(I, VF) ? 0 : VF - 1;
1864 auto *LastInst = cast<Instruction>(
1865 VectorLoopValueMap.getScalarValue(V, {Part, LastLane}));
1866
1867 // Set the insert point after the last scalarized instruction. This ensures
1868 // the insertelement sequence will directly follow the scalar definitions.
1869 auto OldIP = Builder.saveIP();
1870 auto NewIP = std::next(BasicBlock::iterator(LastInst));
1871 Builder.SetInsertPoint(&*NewIP);
1872
1873 // However, if we are vectorizing, we need to construct the vector values.
1874 // If the value is known to be uniform after vectorization, we can just
1875 // broadcast the scalar value corresponding to lane zero for each unroll
1876 // iteration. Otherwise, we construct the vector values using insertelement
1877 // instructions. Since the resulting vectors are stored in
1878 // VectorLoopValueMap, we will only generate the insertelements once.
1879 Value *VectorValue = nullptr;
1880 if (Cost->isUniformAfterVectorization(I, VF)) {
1881 VectorValue = getBroadcastInstrs(ScalarValue);
1882 VectorLoopValueMap.setVectorValue(V, Part, VectorValue);
1883 } else {
1884 // Initialize packing with insertelements to start from undef.
1885 Value *Undef = UndefValue::get(VectorType::get(V->getType(), VF));
1886 VectorLoopValueMap.setVectorValue(V, Part, Undef);
1887 for (unsigned Lane = 0; Lane < VF; ++Lane)
1888 packScalarIntoVectorValue(V, {Part, Lane});
1889 VectorValue = VectorLoopValueMap.getVectorValue(V, Part);
1890 }
1891 Builder.restoreIP(OldIP);
1892 return VectorValue;
1893 }
1894
1895 // If this scalar is unknown, assume that it is a constant or that it is
1896 // loop invariant. Broadcast V and save the value for future uses.
1897 Value *B = getBroadcastInstrs(V);
1898 VectorLoopValueMap.setVectorValue(V, Part, B);
1899 return B;
1900}
1901
1902Value *
1903InnerLoopVectorizer::getOrCreateScalarValue(Value *V,
1904 const VPIteration &Instance) {
1905 // If the value is not an instruction contained in the loop, it should
1906 // already be scalar.
1907 if (OrigLoop->isLoopInvariant(V))
1908 return V;
1909
1910 assert(Instance.Lane > 0(static_cast <bool> (Instance.Lane > 0 ? !Cost->isUniformAfterVectorization
(cast<Instruction>(V), VF) : true && "Uniform values only have lane zero"
) ? void (0) : __assert_fail ("Instance.Lane > 0 ? !Cost->isUniformAfterVectorization(cast<Instruction>(V), VF) : true && \"Uniform values only have lane zero\""
, "/home/username/llvm-project/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp"
, 1912, __extension__ __PRETTY_FUNCTION__))
1911 ? !Cost->isUniformAfterVectorization(cast<Instruction>(V), VF)(static_cast <bool> (Instance.Lane > 0 ? !Cost->isUniformAfterVectorization
(cast<Instruction>(V), VF) : true && "Uniform values only have lane zero"
) ? void (0) : __assert_fail ("Instance.Lane > 0 ? !Cost->isUniformAfterVectorization(cast<Instruction>(V), VF) : true && \"Uniform values only have lane zero\""
, "/home/username/llvm-project/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp"
, 1912, __extension__ __PRETTY_FUNCTION__))
1912 : true && "Uniform values only have lane zero")(static_cast <bool> (Instance.Lane > 0 ? !Cost->isUniformAfterVectorization
(cast<Instruction>(V), VF) : true && "Uniform values only have lane zero"
) ? void (0) : __assert_fail ("Instance.Lane > 0 ? !Cost->isUniformAfterVectorization(cast<Instruction>(V), VF) : true && \"Uniform values only have lane zero\""
, "/home/username/llvm-project/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp"
, 1912, __extension__ __PRETTY_FUNCTION__))
;
1913
1914 // If the value from the original loop has not been vectorized, it is
1915 // represented by UF x VF scalar values in the new loop. Return the requested
1916 // scalar value.
1917 if (VectorLoopValueMap.hasScalarValue(V, Instance))
1918 return VectorLoopValueMap.getScalarValue(V, Instance);
1919
1920 // If the value has not been scalarized, get its entry in VectorLoopValueMap
1921 // for the given unroll part. If this entry is not a vector type (i.e., the
1922 // vectorization factor is one), there is no need to generate an
1923 // extractelement instruction.
1924 auto *U = getOrCreateVectorValue(V, Instance.Part);
1925 if (!U->getType()->isVectorTy()) {
1926 assert(VF == 1 && "Value not scalarized has non-vector type")(static_cast <bool> (VF == 1 && "Value not scalarized has non-vector type"
) ? void (0) : __assert_fail ("VF == 1 && \"Value not scalarized has non-vector type\""
, "/home/username/llvm-project/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp"
, 1926, __extension__ __PRETTY_FUNCTION__))
;
1927 return U;
1928 }
1929
1930 // Otherwise, the value from the original loop has been vectorized and is
1931 // represented by UF vector values. Extract and return the requested scalar
1932 // value from the appropriate vector lane.
1933 return Builder.CreateExtractElement(U, Builder.getInt32(Instance.Lane));
1934}
1935
1936void InnerLoopVectorizer::packScalarIntoVectorValue(
1937 Value *V, const VPIteration &Instance) {
1938 assert(V != Induction && "The new induction variable should not be used.")(static_cast <bool> (V != Induction && "The new induction variable should not be used."
) ? void (0) : __assert_fail ("V != Induction && \"The new induction variable should not be used.\""
, "/home/username/llvm-project/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp"
, 1938, __extension__ __PRETTY_FUNCTION__))
;
1939 assert(!V->getType()->isVectorTy() && "Can't pack a vector")(static_cast <bool> (!V->getType()->isVectorTy() &&
"Can't pack a vector") ? void (0) : __assert_fail ("!V->getType()->isVectorTy() && \"Can't pack a vector\""
, "/home/username/llvm-project/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp"
, 1939, __extension__ __PRETTY_FUNCTION__))
;
1940 assert(!V->getType()->isVoidTy() && "Type does not produce a value")(static_cast <bool> (!V->getType()->isVoidTy() &&
"Type does not produce a value") ? void (0) : __assert_fail (
"!V->getType()->isVoidTy() && \"Type does not produce a value\""
, "/home/username/llvm-project/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp"
, 1940, __extension__ __PRETTY_FUNCTION__))
;
1941
1942 Value *ScalarInst = VectorLoopValueMap.getScalarValue(V, Instance);
1943 Value *VectorValue = VectorLoopValueMap.getVectorValue(V, Instance.Part);
1944 VectorValue = Builder.CreateInsertElement(VectorValue, ScalarInst,
1945 Builder.getInt32(Instance.Lane));
1946 VectorLoopValueMap.resetVectorValue(V, Instance.Part, VectorValue);
1947}
1948
1949Value *InnerLoopVectorizer::reverseVector(Value *Vec) {
1950 assert(Vec->getType()->isVectorTy() && "Invalid type")(static_cast <bool> (Vec->getType()->isVectorTy()
&& "Invalid type") ? void (0) : __assert_fail ("Vec->getType()->isVectorTy() && \"Invalid type\""
, "/home/username/llvm-project/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp"
, 1950, __extension__ __PRETTY_FUNCTION__))
;
1951 SmallVector<Constant *, 8> ShuffleMask;
1952 for (unsigned i = 0; i < VF; ++i)
1953 ShuffleMask.push_back(Builder.getInt32(VF - i - 1));
1954
1955 return Builder.CreateShuffleVector(Vec, UndefValue::get(Vec->getType()),
1956 ConstantVector::get(ShuffleMask),
1957 "reverse");
1958}
1959
1960// Return whether we allow using masked interleave-groups (for dealing with
1961// strided loads/stores that reside in predicated blocks, or for dealing
1962// with gaps).
1963static bool useMaskedInterleavedAccesses(const TargetTransformInfo &TTI) {
1964 // If an override option has been passed in for interleaved accesses, use it.
1965 if (EnableMaskedInterleavedMemAccesses.getNumOccurrences() > 0)
1966 return EnableMaskedInterleavedMemAccesses;
1967
1968 return TTI.enableMaskedInterleavedAccessVectorization();
1969}
1970
1971// Try to vectorize the interleave group that \p Instr belongs to.
1972//
1973// E.g. Translate following interleaved load group (factor = 3):
1974// for (i = 0; i < N; i+=3) {
1975// R = Pic[i]; // Member of index 0
1976// G = Pic[i+1]; // Member of index 1
1977// B = Pic[i+2]; // Member of index 2
1978// ... // do something to R, G, B
1979// }
1980// To:
1981// %wide.vec = load <12 x i32> ; Read 4 tuples of R,G,B
1982// %R.vec = shuffle %wide.vec, undef, <0, 3, 6, 9> ; R elements
1983// %G.vec = shuffle %wide.vec, undef, <1, 4, 7, 10> ; G elements
1984// %B.vec = shuffle %wide.vec, undef, <2, 5, 8, 11> ; B elements
1985//
1986// Or translate following interleaved store group (factor = 3):
1987// for (i = 0; i < N; i+=3) {
1988// ... do something to R, G, B
1989// Pic[i] = R; // Member of index 0
1990// Pic[i+1] = G; // Member of index 1
1991// Pic[i+2] = B; // Member of index 2
1992// }
1993// To:
1994// %R_G.vec = shuffle %R.vec, %G.vec, <0, 1, 2, ..., 7>
1995// %B_U.vec = shuffle %B.vec, undef, <0, 1, 2, 3, u, u, u, u>
1996// %interleaved.vec = shuffle %R_G.vec, %B_U.vec,
1997// <0, 4, 8, 1, 5, 9, 2, 6, 10, 3, 7, 11> ; Interleave R,G,B elements
1998// store <12 x i32> %interleaved.vec ; Write 4 tuples of R,G,B
1999void InnerLoopVectorizer::vectorizeInterleaveGroup(Instruction *Instr,
2000 VectorParts *BlockInMask) {
2001 const InterleaveGroup<Instruction> *Group =
2002 Cost->getInterleavedAccessGroup(Instr);
2003 assert(Group && "Fail to get an interleaved access group.")(static_cast <bool> (Group && "Fail to get an interleaved access group."
) ? void (0) : __assert_fail ("Group && \"Fail to get an interleaved access group.\""
, "/home/username/llvm-project/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp"
, 2003, __extension__ __PRETTY_FUNCTION__))
;
2004
2005 // Skip if current instruction is not the insert position.
2006 if (Instr != Group->getInsertPos())
2007 return;
2008
2009 const DataLayout &DL = Instr->getModule()->getDataLayout();
2010 Value *Ptr = getLoadStorePointerOperand(Instr);
2011
2012 // Prepare for the vector type of the interleaved load/store.
2013 Type *ScalarTy = getMemInstValueType(Instr);
2014 unsigned InterleaveFactor = Group->getFactor();
2015 Type *VecTy = VectorType::get(ScalarTy, InterleaveFactor * VF);
2016 Type *PtrTy = VecTy->getPointerTo(getLoadStoreAddressSpace(Instr));
2017
2018 // Prepare for the new pointers.
2019 setDebugLocFromInst(Builder, Ptr);
2020 SmallVector<Value *, 2> NewPtrs;
2021 unsigned Index = Group->getIndex(Instr);
2022
2023 VectorParts Mask;
2024 bool IsMaskForCondRequired = BlockInMask;
2025 if (IsMaskForCondRequired) {
2026 Mask = *BlockInMask;
2027 // TODO: extend the masked interleaved-group support to reversed access.
2028 assert(!Group->isReverse() && "Reversed masked interleave-group "(static_cast <bool> (!Group->isReverse() && "Reversed masked interleave-group "
"not supported.") ? void (0) : __assert_fail ("!Group->isReverse() && \"Reversed masked interleave-group \" \"not supported.\""
, "/home/username/llvm-project/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp"
, 2029, __extension__ __PRETTY_FUNCTION__))
2029 "not supported.")(static_cast <bool> (!Group->isReverse() && "Reversed masked interleave-group "
"not supported.") ? void (0) : __assert_fail ("!Group->isReverse() && \"Reversed masked interleave-group \" \"not supported.\""
, "/home/username/llvm-project/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp"
, 2029, __extension__ __PRETTY_FUNCTION__))
;
2030 }
2031
2032 // If the group is reverse, adjust the index to refer to the last vector lane
2033 // instead of the first. We adjust the index from the first vector lane,
2034 // rather than directly getting the pointer for lane VF - 1, because the
2035 // pointer operand of the interleaved access is supposed to be uniform. For
2036 // uniform instructions, we're only required to generate a value for the
2037 // first vector lane in each unroll iteration.
2038 if (Group->isReverse())
2039 Index += (VF - 1) * Group->getFactor();
2040
2041 bool InBounds = false;
2042 if (auto *gep = dyn_cast<GetElementPtrInst>(Ptr->stripPointerCasts()))
2043 InBounds = gep->isInBounds();
2044
2045 for (unsigned Part = 0; Part < UF; Part++) {
2046 Value *NewPtr = getOrCreateScalarValue(Ptr, {Part, 0});
2047
2048 // Notice current instruction could be any index. Need to adjust the address
2049 // to the member of index 0.
2050 //
2051 // E.g. a = A[i+1]; // Member of index 1 (Current instruction)
2052 // b = A[i]; // Member of index 0
2053 // Current pointer is pointed to A[i+1], adjust it to A[i].
2054 //
2055 // E.g. A[i+1] = a; // Member of index 1
2056 // A[i] = b; // Member of index 0
2057 // A[i+2] = c; // Member of index 2 (Current instruction)
2058 // Current pointer is pointed to A[i+2], adjust it to A[i].
2059 NewPtr = Builder.CreateGEP(ScalarTy, NewPtr, Builder.getInt32(-Index));
2060 if (InBounds)
2061 cast<GetElementPtrInst>(NewPtr)->setIsInBounds(true);
2062
2063 // Cast to the vector pointer type.
2064 NewPtrs.push_back(Builder.CreateBitCast(NewPtr, PtrTy));
2065 }
2066
2067 setDebugLocFromInst(Builder, Instr);
2068 Value *UndefVec = UndefValue::get(VecTy);
2069
2070 Value *MaskForGaps = nullptr;
2071 if (Group->requiresScalarEpilogue() && !Cost->isScalarEpilogueAllowed()) {
2072 MaskForGaps = createBitMaskForGaps(Builder, VF, *Group);
2073 assert(MaskForGaps && "Mask for Gaps is required but it is null")(static_cast <bool> (MaskForGaps && "Mask for Gaps is required but it is null"
) ? void (0) : __assert_fail ("MaskForGaps && \"Mask for Gaps is required but it is null\""
, "/home/username/llvm-project/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp"
, 2073, __extension__ __PRETTY_FUNCTION__))
;
2074 }
2075
2076 // Vectorize the interleaved load group.
2077 if (isa<LoadInst>(Instr)) {
2078 // For each unroll part, create a wide load for the group.
2079 SmallVector<Value *, 2> NewLoads;
2080 for (unsigned Part = 0; Part < UF; Part++) {
2081 Instruction *NewLoad;
2082 if (IsMaskForCondRequired || MaskForGaps) {
2083 assert(useMaskedInterleavedAccesses(*TTI) &&(static_cast <bool> (useMaskedInterleavedAccesses(*TTI)
&& "masked interleaved groups are not allowed.") ? void
(0) : __assert_fail ("useMaskedInterleavedAccesses(*TTI) && \"masked interleaved groups are not allowed.\""
, "/home/username/llvm-project/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp"
, 2084, __extension__ __PRETTY_FUNCTION__))
2084 "masked interleaved groups are not allowed.")(static_cast <bool> (useMaskedInterleavedAccesses(*TTI)
&& "masked interleaved groups are not allowed.") ? void
(0) : __assert_fail ("useMaskedInterleavedAccesses(*TTI) && \"masked interleaved groups are not allowed.\""
, "/home/username/llvm-project/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp"
, 2084, __extension__ __PRETTY_FUNCTION__))
;
2085 Value *GroupMask = MaskForGaps;
2086 if (IsMaskForCondRequired) {
2087 auto *Undefs = UndefValue::get(Mask[Part]->getType());
2088 auto *RepMask = createReplicatedMask(Builder, InterleaveFactor, VF);
2089 Value *ShuffledMask = Builder.CreateShuffleVector(
2090 Mask[Part], Undefs, RepMask, "interleaved.mask");
2091 GroupMask = MaskForGaps
2092 ? Builder.CreateBinOp(Instruction::And, ShuffledMask,
2093 MaskForGaps)
2094 : ShuffledMask;
2095 }
2096 NewLoad =
2097 Builder.CreateMaskedLoad(NewPtrs[Part], Group->getAlignment(),
2098 GroupMask, UndefVec, "wide.masked.vec");
2099 }
2100 else
2101 NewLoad = Builder.CreateAlignedLoad(VecTy, NewPtrs[Part],
2102 Group->getAlignment(), "wide.vec");
2103 Group->addMetadata(NewLoad);
2104 NewLoads.push_back(NewLoad);
2105 }
2106
2107 // For each member in the group, shuffle out the appropriate data from the
2108 // wide loads.
2109 for (unsigned I = 0; I < InterleaveFactor; ++I) {
2110 Instruction *Member = Group->getMember(I);
2111
2112 // Skip the gaps in the group.
2113 if (!Member)
2114 continue;
2115
2116 Constant *StrideMask = createStrideMask(Builder, I, InterleaveFactor, VF);
2117 for (unsigned Part = 0; Part < UF; Part++) {
2118 Value *StridedVec = Builder.CreateShuffleVector(
2119 NewLoads[Part], UndefVec, StrideMask, "strided.vec");
2120
2121 // If this member has different type, cast the result type.
2122 if (Member->getType() != ScalarTy) {
2123 VectorType *OtherVTy = VectorType::get(Member->getType(), VF);
2124 StridedVec = createBitOrPointerCast(StridedVec, OtherVTy, DL);
2125 }
2126
2127 if (Group->isReverse())
2128 StridedVec = reverseVector(StridedVec);
2129
2130 VectorLoopValueMap.setVectorValue(Member, Part, StridedVec);
2131 }
2132 }
2133 return;
2134 }
2135
2136 // The sub vector type for current instruction.
2137 VectorType *SubVT = VectorType::get(ScalarTy, VF);
2138
2139 // Vectorize the interleaved store group.
2140 for (unsigned Part = 0; Part < UF; Part++) {
2141 // Collect the stored vector from each member.
2142 SmallVector<Value *, 4> StoredVecs;
2143 for (unsigned i = 0; i < InterleaveFactor; i++) {
2144 // Interleaved store group doesn't allow a gap, so each index has a member
2145 Instruction *Member = Group->getMember(i);
2146 assert(Member && "Fail to get a member from an interleaved store group")(static_cast <bool> (Member && "Fail to get a member from an interleaved store group"
) ? void (0) : __assert_fail ("Member && \"Fail to get a member from an interleaved store group\""
, "/home/username/llvm-project/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp"
, 2146, __extension__ __PRETTY_FUNCTION__))
;
2147
2148 Value *StoredVec = getOrCreateVectorValue(
2149 cast<StoreInst>(Member)->getValueOperand(), Part);
2150 if (Group->isReverse())
2151 StoredVec = reverseVector(StoredVec);
2152
2153 // If this member has different type, cast it to a unified type.
2154
2155 if (StoredVec->getType() != SubVT)
2156 StoredVec = createBitOrPointerCast(StoredVec, SubVT, DL);
2157
2158 StoredVecs.push_back(StoredVec);
2159 }
2160
2161 // Concatenate all vectors into a wide vector.
2162 Value *WideVec = concatenateVectors(Builder, StoredVecs);
2163
2164 // Interleave the elements in the wide vector.
2165 Constant *IMask = createInterleaveMask(Builder, VF, InterleaveFactor);
2166 Value *IVec = Builder.CreateShuffleVector(WideVec, UndefVec, IMask,
2167 "interleaved.vec");
2168
2169 Instruction *NewStoreInstr;
2170 if (IsMaskForCondRequired) {
2171 auto *Undefs = UndefValue::get(Mask[Part]->getType());
2172 auto *RepMask = createReplicatedMask(Builder, InterleaveFactor, VF);
2173 Value *ShuffledMask = Builder.CreateShuffleVector(
2174 Mask[Part], Undefs, RepMask, "interleaved.mask");
2175 NewStoreInstr = Builder.CreateMaskedStore(
2176 IVec, NewPtrs[Part], Group->getAlignment(), ShuffledMask);
2177 }
2178 else
2179 NewStoreInstr = Builder.CreateAlignedStore(IVec, NewPtrs[Part],
2180 Group->getAlignment());
2181
2182 Group->addMetadata(NewStoreInstr);
2183 }
2184}
2185
2186void InnerLoopVectorizer::vectorizeMemoryInstruction(Instruction *Instr,
2187 VectorParts *BlockInMask) {
2188 // Attempt to issue a wide load.
2189 LoadInst *LI = dyn_cast<LoadInst>(Instr);
2190 StoreInst *SI = dyn_cast<StoreInst>(Instr);
2191
2192 assert((LI || SI) && "Invalid Load/Store instruction")(static_cast <bool> ((LI || SI) && "Invalid Load/Store instruction"
) ? void (0) : __assert_fail ("(LI || SI) && \"Invalid Load/Store instruction\""
, "/home/username/llvm-project/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp"
, 2192, __extension__ __PRETTY_FUNCTION__))
;
2193
2194 LoopVectorizationCostModel::InstWidening Decision =
2195 Cost->getWideningDecision(Instr, VF);
2196 assert(Decision != LoopVectorizationCostModel::CM_Unknown &&(static_cast <bool> (Decision != LoopVectorizationCostModel
::CM_Unknown && "CM decision should be taken at this point"
) ? void (0) : __assert_fail ("Decision != LoopVectorizationCostModel::CM_Unknown && \"CM decision should be taken at this point\""
, "/home/username/llvm-project/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp"
, 2197, __extension__ __PRETTY_FUNCTION__))
2197 "CM decision should be taken at this point")(static_cast <bool> (Decision != LoopVectorizationCostModel
::CM_Unknown && "CM decision should be taken at this point"
) ? void (0) : __assert_fail ("Decision != LoopVectorizationCostModel::CM_Unknown && \"CM decision should be taken at this point\""
, "/home/username/llvm-project/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp"
, 2197, __extension__ __PRETTY_FUNCTION__))
;
2198 if (Decision == LoopVectorizationCostModel::CM_Interleave)
2199 return vectorizeInterleaveGroup(Instr);
2200
2201 Type *ScalarDataTy = getMemInstValueType(Instr);
2202 Type *DataTy = VectorType::get(ScalarDataTy, VF);
2203 Value *Ptr = getLoadStorePointerOperand(Instr);
2204 unsigned Alignment = getLoadStoreAlignment(Instr);
2205 // An alignment of 0 means target abi alignment. We need to use the scalar's
2206 // target abi alignment in such a case.
2207 const DataLayout &DL = Instr->getModule()->getDataLayout();
2208 if (!Alignment)
2209 Alignment = DL.getABITypeAlignment(ScalarDataTy);
2210 unsigned AddressSpace = getLoadStoreAddressSpace(Instr);
2211
2212 // Determine if the pointer operand of the access is either consecutive or
2213 // reverse consecutive.
2214 bool Reverse = (Decision == LoopVectorizationCostModel::CM_Widen_Reverse);
2215 bool ConsecutiveStride =
2216 Reverse || (Decision == LoopVectorizationCostModel::CM_Widen);
2217 bool CreateGatherScatter =
2218 (Decision == LoopVectorizationCostModel::CM_GatherScatter);
2219
2220 // Either Ptr feeds a vector load/store, or a vector GEP should feed a vector
2221 // gather/scatter. Otherwise Decision should have been to Scalarize.
2222 assert((ConsecutiveStride || CreateGatherScatter) &&(static_cast <bool> ((ConsecutiveStride || CreateGatherScatter
) && "The instruction should be scalarized") ? void (
0) : __assert_fail ("(ConsecutiveStride || CreateGatherScatter) && \"The instruction should be scalarized\""
, "/home/username/llvm-project/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp"
, 2223, __extension__ __PRETTY_FUNCTION__))
2223 "The instruction should be scalarized")(static_cast <bool> ((ConsecutiveStride || CreateGatherScatter
) && "The instruction should be scalarized") ? void (
0) : __assert_fail ("(ConsecutiveStride || CreateGatherScatter) && \"The instruction should be scalarized\""
, "/home/username/llvm-project/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp"
, 2223, __extension__ __PRETTY_FUNCTION__))
;
2224
2225 // Handle consecutive loads/stores.
2226 if (ConsecutiveStride)
2227 Ptr = getOrCreateScalarValue(Ptr, {0, 0});
2228
2229 VectorParts Mask;
2230 bool isMaskRequired = BlockInMask;
2231 if (isMaskRequired)
2232 Mask = *BlockInMask;
2233
2234 bool InBounds = false;
2235 if (auto *gep = dyn_cast<GetElementPtrInst>(
2236 getLoadStorePointerOperand(Instr)->stripPointerCasts()))
2237 InBounds = gep->isInBounds();
2238
2239 const auto CreateVecPtr = [&](unsigned Part, Value *Ptr) -> Value * {
2240 // Calculate the pointer for the specific unroll-part.
2241 GetElementPtrInst *PartPtr = nullptr;
2242
2243 if (Reverse) {
2244 // If the address is consecutive but reversed, then the
2245 // wide store needs to start at the last vector element.
2246 PartPtr = cast<GetElementPtrInst>(
2247 Builder.CreateGEP(ScalarDataTy, Ptr, Builder.getInt32(-Part * VF)));
2248 PartPtr->setIsInBounds(InBounds);
2249 PartPtr = cast<GetElementPtrInst>(
2250 Builder.CreateGEP(ScalarDataTy, PartPtr, Builder.getInt32(1 - VF)));
2251 PartPtr->setIsInBounds(InBounds);
2252 if (isMaskRequired) // Reverse of a null all-one mask is a null mask.
2253 Mask[Part] = reverseVector(Mask[Part]);
2254 } else {
2255 PartPtr = cast<GetElementPtrInst>(
2256 Builder.CreateGEP(ScalarDataTy, Ptr, Builder.getInt32(Part * VF)));
2257 PartPtr->setIsInBounds(InBounds);
2258 }
2259
2260 return Builder.CreateBitCast(PartPtr, DataTy->getPointerTo(AddressSpace));
2261 };
2262
2263 // Handle Stores:
2264 if (SI) {
2265 setDebugLocFromInst(Builder, SI);
2266
2267 for (unsigned Part = 0; Part < UF; ++Part) {
2268 Instruction *NewSI = nullptr;
2269 Value *StoredVal = getOrCreateVectorValue(SI->getValueOperand(), Part);
2270 if (CreateGatherScatter) {
2271 Value *MaskPart = isMaskRequired ? Mask[Part] : nullptr;
2272 Value *VectorGep = getOrCreateVectorValue(Ptr, Part);
2273 NewSI = Builder.CreateMaskedScatter(StoredVal, VectorGep, Alignment,
2274 MaskPart);
2275 } else {
2276 if (Reverse) {
2277 // If we store to reverse consecutive memory locations, then we need
2278 // to reverse the order of elements in the stored value.
2279 StoredVal = reverseVector(StoredVal);
2280 // We don't want to update the value in the map as it might be used in
2281 // another expression. So don't call resetVectorValue(StoredVal).
2282 }
2283 auto *VecPtr = CreateVecPtr(Part, Ptr);
2284 if (isMaskRequired)
2285 NewSI = Builder.CreateMaskedStore(StoredVal, VecPtr, Alignment,
2286 Mask[Part]);
2287 else
2288 NewSI = Builder.CreateAlignedStore(StoredVal, VecPtr, Alignment);
2289 }
2290 addMetadata(NewSI, SI);
2291 }
2292 return;
2293 }
2294
2295 // Handle loads.
2296 assert(LI && "Must have a load instruction")(static_cast <bool> (LI && "Must have a load instruction"
) ? void (0) : __assert_fail ("LI && \"Must have a load instruction\""
, "/home/username/llvm-project/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp"
, 2296, __extension__ __PRETTY_FUNCTION__))
;
2297 setDebugLocFromInst(Builder, LI);
2298 for (unsigned Part = 0; Part < UF; ++Part) {
2299 Value *NewLI;
2300 if (CreateGatherScatter) {
2301 Value *MaskPart = isMaskRequired ? Mask[Part] : nullptr;
2302 Value *VectorGep = getOrCreateVectorValue(Ptr, Part);
2303 NewLI = Builder.CreateMaskedGather(VectorGep, Alignment, MaskPart,
2304 nullptr, "wide.masked.gather");
2305 addMetadata(NewLI, LI);
2306 } else {
2307 auto *VecPtr = CreateVecPtr(Part, Ptr);
2308 if (isMaskRequired)
2309 NewLI = Builder.CreateMaskedLoad(VecPtr, Alignment, Mask[Part],
2310 UndefValue::get(DataTy),
2311 "wide.masked.load");
2312 else
2313 NewLI =
2314 Builder.CreateAlignedLoad(DataTy, VecPtr, Alignment, "wide.load");
2315
2316 // Add metadata to the load, but setVectorValue to the reverse shuffle.
2317 addMetadata(NewLI, LI);
2318 if (Reverse)
2319 NewLI = reverseVector(NewLI);
2320 }
2321 VectorLoopValueMap.setVectorValue(Instr, Part, NewLI);
2322 }
2323}
2324
2325void InnerLoopVectorizer::scalarizeInstruction(Instruction *Instr,
2326 const VPIteration &Instance,
2327 bool IfPredicateInstr) {
2328 assert(!Instr->getType()->isAggregateType() && "Can't handle vectors")(static_cast <bool> (!Instr->getType()->isAggregateType
() && "Can't handle vectors") ? void (0) : __assert_fail
("!Instr->getType()->isAggregateType() && \"Can't handle vectors\""
, "/home/username/llvm-project/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp"
, 2328, __extension__ __PRETTY_FUNCTION__))
;
2329
2330 setDebugLocFromInst(Builder, Instr);
2331
2332 // Does this instruction return a value ?
2333 bool IsVoidRetTy = Instr->getType()->isVoidTy();
2334
2335 Instruction *Cloned = Instr->clone();
2336 if (!IsVoidRetTy)
2337 Cloned->setName(Instr->getName() + ".cloned");
2338
2339 // Replace the operands of the cloned instructions with their scalar
2340 // equivalents in the new loop.
2341 for (unsigned op = 0, e = Instr->getNumOperands(); op != e; ++op) {
2342 auto *NewOp = getOrCreateScalarValue(Instr->getOperand(op), Instance);
2343 Cloned->setOperand(op, NewOp);
2344 }
2345 addNewMetadata(Cloned, Instr);
2346
2347 // Place the cloned scalar in the new loop.
2348 Builder.Insert(Cloned);
2349
2350 // Add the cloned scalar to the scalar map entry.
2351 VectorLoopValueMap.setScalarValue(Instr, Instance, Cloned);
2352
2353 // If we just cloned a new assumption, add it the assumption cache.
2354 if (auto *II = dyn_cast<IntrinsicInst>(Cloned))
2355 if (II->getIntrinsicID() == Intrinsic::assume)
2356 AC->registerAssumption(II);
2357
2358 // End if-block.
2359 if (IfPredicateInstr)
2360 PredicatedInstructions.push_back(Cloned);
2361}
2362
2363PHINode *InnerLoopVectorizer::createInductionVariable(Loop *L, Value *Start,
2364 Value *End, Value *Step,
2365 Instruction *DL) {
2366 BasicBlock *Header = L->getHeader();
2367 BasicBlock *Latch = L->getLoopLatch();
2368 // As we're just creating this loop, it's possible no latch exists
2369 // yet. If so, use the header as this will be a single block loop.
2370 if (!Latch)
2371 Latch = Header;
2372
2373 IRBuilder<> Builder(&*Header->getFirstInsertionPt());
2374 Instruction *OldInst = getDebugLocFromInstOrOperands(OldInduction);
2375 setDebugLocFromInst(Builder, OldInst);
2376 auto *Induction = Builder.CreatePHI(Start->getType(), 2, "index");
2377
2378 Builder.SetInsertPoint(Latch->getTerminator());
2379 setDebugLocFromInst(Builder, OldInst);
2380
2381 // Create i+1 and fill the PHINode.
2382 Value *Next = Builder.CreateAdd(Induction, Step, "index.next");
2383 Induction->addIncoming(Start, L->getLoopPreheader());
2384 Induction->addIncoming(Next, Latch);
2385 // Create the compare.
2386 Value *ICmp = Builder.CreateICmpEQ(Next, End);
2387 Builder.CreateCondBr(ICmp, L->getExitBlock(), Header);
2388
2389 // Now we have two terminators. Remove the old one from the block.
2390 Latch->getTerminator()->eraseFromParent();
2391
2392 return Induction;
2393}
2394
2395Value *InnerLoopVectorizer::getOrCreateTripCount(Loop *L) {
2396 if (TripCount)
2397 return TripCount;
2398
2399 assert(L && "Create Trip Count for null loop.")(static_cast <bool> (L && "Create Trip Count for null loop."
) ? void (0) : __assert_fail ("L && \"Create Trip Count for null loop.\""
, "/home/username/llvm-project/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp"
, 2399, __extension__ __PRETTY_FUNCTION__))
;
2400 IRBuilder<> Builder(L->getLoopPreheader()->getTerminator());
2401 // Find the loop boundaries.
2402 ScalarEvolution *SE = PSE.getSE();
2403 const SCEV *BackedgeTakenCount = PSE.getBackedgeTakenCount();
2404 assert(BackedgeTakenCount != SE->getCouldNotCompute() &&(static_cast <bool> (BackedgeTakenCount != SE->getCouldNotCompute
() && "Invalid loop count") ? void (0) : __assert_fail
("BackedgeTakenCount != SE->getCouldNotCompute() && \"Invalid loop count\""
, "/home/username/llvm-project/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp"
, 2405, __extension__ __PRETTY_FUNCTION__))
2405 "Invalid loop count")(static_cast <bool> (BackedgeTakenCount != SE->getCouldNotCompute
() && "Invalid loop count") ? void (0) : __assert_fail
("BackedgeTakenCount != SE->getCouldNotCompute() && \"Invalid loop count\""
, "/home/username/llvm-project/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp"
, 2405, __extension__ __PRETTY_FUNCTION__))
;
2406
2407 Type *IdxTy = Legal->getWidestInductionType();
2408 assert(IdxTy && "No type for induction")(static_cast <bool> (IdxTy && "No type for induction"
) ? void (0) : __assert_fail ("IdxTy && \"No type for induction\""
, "/home/username/llvm-project/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp"
, 2408, __extension__ __PRETTY_FUNCTION__))
;
2409
2410 // The exit count might have the type of i64 while the phi is i32. This can
2411 // happen if we have an induction variable that is sign extended before the
2412 // compare. The only way that we get a backedge taken count is that the
2413 // induction variable was signed and as such will not overflow. In such a case
2414 // truncation is legal.
2415 if (BackedgeTakenCount->getType()->getPrimitiveSizeInBits() >
2416 IdxTy->getPrimitiveSizeInBits())
2417 BackedgeTakenCount = SE->getTruncateOrNoop(BackedgeTakenCount, IdxTy);
2418 BackedgeTakenCount = SE->getNoopOrZeroExtend(BackedgeTakenCount, IdxTy);
2419
2420 // Get the total trip count from the count by adding 1.
2421 const SCEV *ExitCount = SE->getAddExpr(
2422 BackedgeTakenCount, SE->getOne(BackedgeTakenCount->getType()));
2423
2424 const DataLayout &DL = L->getHeader()->getModule()->getDataLayout();
2425
2426 // Expand the trip count and place the new instructions in the preheader.
2427 // Notice that the pre-header does not change, only the loop body.
2428 SCEVExpander Exp(*SE, DL, "induction");
2429
2430 // Count holds the overall loop count (N).
2431 TripCount = Exp.expandCodeFor(ExitCount, ExitCount->getType(),
2432 L->getLoopPreheader()->getTerminator());
2433
2434 if (TripCount->getType()->isPointerTy())
2435 TripCount =
2436 CastInst::CreatePointerCast(TripCount, IdxTy, "exitcount.ptrcnt.to.int",
2437 L->getLoopPreheader()->getTerminator());
2438
2439 return TripCount;
2440}
2441
2442Value *InnerLoopVectorizer::getOrCreateVectorTripCount(Loop *L) {
2443 if (VectorTripCount)
2444 return VectorTripCount;
2445
2446 Value *TC = getOrCreateTripCount(L);
2447 IRBuilder<> Builder(L->getLoopPreheader()->getTerminator());
2448
2449 Type *Ty = TC->getType();
2450 Constant *Step = ConstantInt::get(Ty, VF * UF);
2451
2452 // If the tail is to be folded by masking, round the number of iterations N
2453 // up to a multiple of Step instead of rounding down. This is done by first
2454 // adding Step-1 and then rounding down. Note that it's ok if this addition
2455 // overflows: the vector induction variable will eventually wrap to zero given
2456 // that it starts at zero and its Step is a power of two; the loop will then
2457 // exit, with the last early-exit vector comparison also producing all-true.
2458 if (Cost->foldTailByMasking()) {
2459 assert(isPowerOf2_32(VF * UF) &&(static_cast <bool> (isPowerOf2_32(VF * UF) && "VF*UF must be a power of 2 when folding tail by masking"
) ? void (0) : __assert_fail ("isPowerOf2_32(VF * UF) && \"VF*UF must be a power of 2 when folding tail by masking\""
, "/home/username/llvm-project/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp"
, 2460, __extension__ __PRETTY_FUNCTION__))
2460 "VF*UF must be a power of 2 when folding tail by masking")(static_cast <bool> (isPowerOf2_32(VF * UF) && "VF*UF must be a power of 2 when folding tail by masking"
) ? void (0) : __assert_fail ("isPowerOf2_32(VF * UF) && \"VF*UF must be a power of 2 when folding tail by masking\""
, "/home/username/llvm-project/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp"
, 2460, __extension__ __PRETTY_FUNCTION__))
;
2461 TC = Builder.CreateAdd(TC, ConstantInt::get(Ty, VF * UF - 1), "n.rnd.up");
2462 }
2463
2464 // Now we need to generate the expression for the part of the loop that the
2465 // vectorized body will execute. This is equal to N - (N % Step) if scalar
2466 // iterations are not required for correctness, or N - Step, otherwise. Step
2467 // is equal to the vectorization factor (number of SIMD elements) times the
2468 // unroll factor (number of SIMD instructions).
2469 Value *R = Builder.CreateURem(TC, Step, "n.mod.vf");
2470
2471 // If there is a non-reversed interleaved group that may speculatively access
2472 // memory out-of-bounds, we need to ensure that there will be at least one
2473 // iteration of the scalar epilogue loop. Thus, if the step evenly divides
2474 // the trip count, we set the remainder to be equal to the step. If the step
2475 // does not evenly divide the trip count, no adjustment is necessary since
2476 // there will already be scalar iterations. Note that the minimum iterations
2477 // check ensures that N >= Step.
2478 if (VF > 1 && Cost->requiresScalarEpilogue()) {
2479 auto *IsZero = Builder.CreateICmpEQ(R, ConstantInt::get(R->getType(), 0));
2480 R = Builder.CreateSelect(IsZero, Step, R);
2481 }
2482
2483 VectorTripCount = Builder.CreateSub(TC, R, "n.vec");
2484
2485 return VectorTripCount;
2486}
2487
2488Value *InnerLoopVectorizer::createBitOrPointerCast(Value *V, VectorType *DstVTy,
2489 const DataLayout &DL) {
2490 // Verify that V is a vector type with same number of elements as DstVTy.
2491 unsigned VF = DstVTy->getNumElements();
2492 VectorType *SrcVecTy = cast<VectorType>(V->getType());
2493 assert((VF == SrcVecTy->getNumElements()) && "Vector dimensions do not match")(static_cast <bool> ((VF == SrcVecTy->getNumElements
()) && "Vector dimensions do not match") ? void (0) :
__assert_fail ("(VF == SrcVecTy->getNumElements()) && \"Vector dimensions do not match\""
, "/home/username/llvm-project/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp"
, 2493, __extension__ __PRETTY_FUNCTION__))
;
2494 Type *SrcElemTy = SrcVecTy->getElementType();
2495 Type *DstElemTy = DstVTy->getElementType();
2496 assert((DL.getTypeSizeInBits(SrcElemTy) == DL.getTypeSizeInBits(DstElemTy)) &&(static_cast <bool> ((DL.getTypeSizeInBits(SrcElemTy) ==
DL.getTypeSizeInBits(DstElemTy)) && "Vector elements must have same size"
) ? void (0) : __assert_fail ("(DL.getTypeSizeInBits(SrcElemTy) == DL.getTypeSizeInBits(DstElemTy)) && \"Vector elements must have same size\""
, "/home/username/llvm-project/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp"
, 2497, __extension__ __PRETTY_FUNCTION__))
2497 "Vector elements must have same size")(static_cast <bool> ((DL.getTypeSizeInBits(SrcElemTy) ==
DL.getTypeSizeInBits(DstElemTy)) && "Vector elements must have same size"
) ? void (0) : __assert_fail ("(DL.getTypeSizeInBits(SrcElemTy) == DL.getTypeSizeInBits(DstElemTy)) && \"Vector elements must have same size\""
, "/home/username/llvm-project/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp"
, 2497, __extension__ __PRETTY_FUNCTION__))
;
2498
2499 // Do a direct cast if element types are castable.
2500 if (CastInst::isBitOrNoopPointerCastable(SrcElemTy, DstElemTy, DL)) {
2501 return Builder.CreateBitOrPointerCast(V, DstVTy);
2502 }
2503 // V cannot be directly casted to desired vector type.
2504 // May happen when V is a floating point vector but DstVTy is a vector of
2505 // pointers or vice-versa. Handle this using a two-step bitcast using an
2506 // intermediate Integer type for the bitcast i.e. Ptr <-> Int <-> Float.
2507 assert((DstElemTy->isPointerTy() != SrcElemTy->isPointerTy()) &&(static_cast <bool> ((DstElemTy->isPointerTy() != SrcElemTy
->isPointerTy()) && "Only one type should be a pointer type"
) ? void (0) : __assert_fail ("(DstElemTy->isPointerTy() != SrcElemTy->isPointerTy()) && \"Only one type should be a pointer type\""
, "/home/username/llvm-project/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp"
, 2508, __extension__ __PRETTY_FUNCTION__))
2508 "Only one type should be a pointer type")(static_cast <bool> ((DstElemTy->isPointerTy() != SrcElemTy
->isPointerTy()) && "Only one type should be a pointer type"
) ? void (0) : __assert_fail ("(DstElemTy->isPointerTy() != SrcElemTy->isPointerTy()) && \"Only one type should be a pointer type\""
, "/home/username/llvm-project/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp"
, 2508, __extension__ __PRETTY_FUNCTION__))
;
2509 assert((DstElemTy->isFloatingPointTy() != SrcElemTy->isFloatingPointTy()) &&(static_cast <bool> ((DstElemTy->isFloatingPointTy()
!= SrcElemTy->isFloatingPointTy()) && "Only one type should be a floating point type"
) ? void (0) : __assert_fail ("(DstElemTy->isFloatingPointTy() != SrcElemTy->isFloatingPointTy()) && \"Only one type should be a floating point type\""
, "/home/username/llvm-project/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp"
, 2510, __extension__ __PRETTY_FUNCTION__))
2510 "Only one type should be a floating point type")(static_cast <bool> ((DstElemTy->isFloatingPointTy()
!= SrcElemTy->isFloatingPointTy()) && "Only one type should be a floating point type"
) ? void (0) : __assert_fail ("(DstElemTy->isFloatingPointTy() != SrcElemTy->isFloatingPointTy()) && \"Only one type should be a floating point type\""
, "/home/username/llvm-project/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp"
, 2510, __extension__ __PRETTY_FUNCTION__))
;
2511 Type *IntTy =
2512 IntegerType::getIntNTy(V->getContext(), DL.getTypeSizeInBits(SrcElemTy));
2513 VectorType *VecIntTy = VectorType::get(IntTy, VF);
2514 Value *CastVal = Builder.CreateBitOrPointerCast(V, VecIntTy);
2515 return Builder.CreateBitOrPointerCast(CastVal, DstVTy);
2516}
2517
2518void InnerLoopVectorizer::emitMinimumIterationCountCheck(Loop *L,
2519 BasicBlock *Bypass) {
2520 Value *Count = getOrCreateTripCount(L);
2521 BasicBlock *BB = L->getLoopPreheader();
2522 IRBuilder<> Builder(BB->getTerminator());
2523
2524 // Generate code to check if the loop's trip count is less than VF * UF, or
2525 // equal to it in case a scalar epilogue is required; this implies that the
2526 // vector trip count is zero. This check also covers the case where adding one
2527 // to the backedge-taken count overflowed leading to an incorrect trip count
2528 // of zero. In this case we will also jump to the scalar loop.
2529 auto P = Cost->requiresScalarEpilogue() ? ICmpInst::ICMP_ULE
2530 : ICmpInst::ICMP_ULT;
2531
2532 // If tail is to be folded, vector loop takes care of all iterations.
2533 Value *CheckMinIters = Builder.getFalse();
2534 if (!Cost->foldTailByMasking())
2535 CheckMinIters = Builder.CreateICmp(
2536 P, Count, ConstantInt::get(Count->getType(), VF * UF),
2537 "min.iters.check");
2538
2539 BasicBlock *NewBB = BB->splitBasicBlock(BB->getTerminator(), "vector.ph");
2540 // Update dominator tree immediately if the generated block is a
2541 // LoopBypassBlock because SCEV expansions to generate loop bypass
2542 // checks may query it before the current function is finished.
2543 DT->addNewBlock(NewBB, BB);
2544 if (L->getParentLoop())
2545 L->getParentLoop()->addBasicBlockToLoop(NewBB, *LI);
2546 ReplaceInstWithInst(BB->getTerminator(),
2547 BranchInst::Create(Bypass, NewBB, CheckMinIters));
2548 LoopBypassBlocks.push_back(BB);
2549}
2550
2551void InnerLoopVectorizer::emitSCEVChecks(Loop *L, BasicBlock *Bypass) {
2552 BasicBlock *BB = L->getLoopPreheader();
2553
2554 // Generate the code to check that the SCEV assumptions that we made.
2555 // We want the new basic block to start at the first instruction in a
2556 // sequence of instructions that form a check.
2557 SCEVExpander Exp(*PSE.getSE(), Bypass->getModule()->getDataLayout(),
2558 "scev.check");
2559 Value *SCEVCheck =
2560 Exp.expandCodeForPredicate(&PSE.getUnionPredicate(), BB->getTerminator());
2561
2562 if (auto *C = dyn_cast<ConstantInt>(SCEVCheck))
2563 if (C->isZero())
2564 return;
2565
2566 assert(!Cost->foldTailByMasking() &&(static_cast <bool> (!Cost->foldTailByMasking() &&
"Cannot SCEV check stride or overflow when folding tail") ? void
(0) : __assert_fail ("!Cost->foldTailByMasking() && \"Cannot SCEV check stride or overflow when folding tail\""
, "/home/username/llvm-project/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp"
, 2567, __extension__ __PRETTY_FUNCTION__))
2567 "Cannot SCEV check stride or overflow when folding tail")(static_cast <bool> (!Cost->foldTailByMasking() &&
"Cannot SCEV check stride or overflow when folding tail") ? void
(0) : __assert_fail ("!Cost->foldTailByMasking() && \"Cannot SCEV check stride or overflow when folding tail\""
, "/home/username/llvm-project/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp"
, 2567, __extension__ __PRETTY_FUNCTION__))
;
2568 // Create a new block containing the stride check.
2569 BB->setName("vector.scevcheck");
2570 auto *NewBB = BB->splitBasicBlock(BB->getTerminator(), "vector.ph");
2571 // Update dominator tree immediately if the generated block is a
2572 // LoopBypassBlock because SCEV expansions to generate loop bypass
2573 // checks may query it before the current function is finished.
2574 DT->addNewBlock(NewBB, BB);
2575 if (L->getParentLoop())
2576 L->getParentLoop()->addBasicBlockToLoop(NewBB, *LI);
2577 ReplaceInstWithInst(BB->getTerminator(),
2578 BranchInst::Create(Bypass, NewBB, SCEVCheck));
2579 LoopBypassBlocks.push_back(BB);
2580 AddedSafetyChecks = true;
2581}
2582
2583void InnerLoopVectorizer::emitMemRuntimeChecks(Loop *L, BasicBlock *Bypass) {
2584 // VPlan-native path does not do any analysis for runtime checks currently.
2585 if (EnableVPlanNativePath)
2586 return;
2587
2588 BasicBlock *BB = L->getLoopPreheader();
2589
2590 // Generate the code that checks in runtime if arrays overlap. We put the
2591 // checks into a separate block to make the more common case of few elements
2592 // faster.
2593 Instruction *FirstCheckInst;
2594 Instruction *MemRuntimeCheck;
2595 std::tie(FirstCheckInst, MemRuntimeCheck) =
2596 Legal->getLAI()->addRuntimeChecks(BB->getTerminator());
2597 if (!MemRuntimeCheck)
2598 return;
2599
2600 assert(!Cost->foldTailByMasking() && "Cannot check memory when folding tail")(static_cast <bool> (!Cost->foldTailByMasking() &&
"Cannot check memory when folding tail") ? void (0) : __assert_fail
("!Cost->foldTailByMasking() && \"Cannot check memory when folding tail\""
, "/home/username/llvm-project/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp"
, 2600, __extension__ __PRETTY_FUNCTION__))
;
2601 // Create a new block containing the memory check.
2602 BB->setName("vector.memcheck");
2603 auto *NewBB = BB->splitBasicBlock(BB->getTerminator(), "vector.ph");
2604 // Update dominator tree immediately if the generated block is a
2605 // LoopBypassBlock because SCEV expansions to generate loop bypass
2606 // checks may query it before the current function is finished.
2607 DT->addNewBlock(NewBB, BB);
2608 if (L->getParentLoop())
2609 L->getParentLoop()->addBasicBlockToLoop(NewBB, *LI);
2610 ReplaceInstWithInst(BB->getTerminator(),
2611 BranchInst::Create(Bypass, NewBB, MemRuntimeCheck));
2612 LoopBypassBlocks.push_back(BB);
2613 AddedSafetyChecks = true;
2614
2615 // We currently don't use LoopVersioning for the actual loop cloning but we
2616 // still use it to add the noalias metadata.
2617 LVer = llvm::make_unique<LoopVersioning>(*Legal->getLAI(), OrigLoop, LI, DT,
2618 PSE.getSE());
2619 LVer->prepareNoAliasMetadata();
2620}
2621
2622Value *InnerLoopVectorizer::emitTransformedIndex(
2623 IRBuilder<> &B, Value *Index, ScalarEvolution *SE, const DataLayout &DL,
2624 const InductionDescriptor &ID) const {
2625
2626 SCEVExpander Exp(*SE, DL, "induction");
2627 auto Step = ID.getStep();
2628 auto StartValue = ID.getStartValue();
2629 assert(Index->getType() == Step->getType() &&(static_cast <bool> (Index->getType() == Step->getType
() && "Index type does not match StepValue type") ? void
(0) : __assert_fail ("Index->getType() == Step->getType() && \"Index type does not match StepValue type\""
, "/home/username/llvm-project/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp"
, 2630, __extension__ __PRETTY_FUNCTION__))
2630 "Index type does not match StepValue type")(static_cast <bool> (Index->getType() == Step->getType
() && "Index type does not match StepValue type") ? void
(0) : __assert_fail ("Index->getType() == Step->getType() && \"Index type does not match StepValue type\""
, "/home/username/llvm-project/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp"
, 2630, __extension__ __PRETTY_FUNCTION__))
;
2631
2632 // Note: the IR at this point is broken. We cannot use SE to create any new
2633 // SCEV and then expand it, hoping that SCEV's simplification will give us
2634 // a more optimal code. Unfortunately, attempt of doing so on invalid IR may
2635 // lead to various SCEV crashes. So all we can do is to use builder and rely
2636 // on InstCombine for future simplifications. Here we handle some trivial
2637 // cases only.
2638 auto CreateAdd = [&B](Value *X, Value *Y) {
2639 assert(X->getType() == Y->getType() && "Types don't match!")(static_cast <bool> (X->getType() == Y->getType()
&& "Types don't match!") ? void (0) : __assert_fail (
"X->getType() == Y->getType() && \"Types don't match!\""
, "/home/username/llvm-project/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp"
, 2639, __extension__ __PRETTY_FUNCTION__))
;
2640 if (auto *CX = dyn_cast<ConstantInt>(X))
2641 if (CX->isZero())
2642 return Y;
2643 if (auto *CY = dyn_cast<ConstantInt>(Y))
2644 if (CY->isZero())
2645 return X;
2646 return B.CreateAdd(X, Y);
2647 };
2648
2649 auto CreateMul = [&B](Value *X, Value *Y) {
2650 assert(X->getType() == Y->getType() && "Types don't match!")(static_cast <bool> (X->getType() == Y->getType()
&& "Types don't match!") ? void (0) : __assert_fail (
"X->getType() == Y->getType() && \"Types don't match!\""
, "/home/username/llvm-project/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp"
, 2650, __extension__ __PRETTY_FUNCTION__))
;
2651 if (auto *CX = dyn_cast<ConstantInt>(X))
2652 if (CX->isOne())
2653 return Y;
2654 if (auto *CY = dyn_cast<ConstantInt>(Y))
2655 if (CY->isOne())
2656 return X;
2657 return B.CreateMul(X, Y);
2658 };
2659
2660 switch (ID.getKind()) {
2661 case InductionDescriptor::IK_IntInduction: {
2662 assert(Index->getType() == StartValue->getType() &&(static_cast <bool> (Index->getType() == StartValue->
getType() && "Index type does not match StartValue type"
) ? void (0) : __assert_fail ("Index->getType() == StartValue->getType() && \"Index type does not match StartValue type\""
, "/home/username/llvm-project/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp"
, 2663, __extension__ __PRETTY_FUNCTION__))
2663 "Index type does not match StartValue type")(static_cast <bool> (Index->getType() == StartValue->
getType() && "Index type does not match StartValue type"
) ? void (0) : __assert_fail ("Index->getType() == StartValue->getType() && \"Index type does not match StartValue type\""
, "/home/username/llvm-project/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp"
, 2663, __extension__ __PRETTY_FUNCTION__))
;
2664 if (ID.getConstIntStepValue() && ID.getConstIntStepValue()->isMinusOne())
2665 return B.CreateSub(StartValue, Index);
2666 auto *Offset = CreateMul(
2667 Index, Exp.expandCodeFor(Step, Index->getType(), &*B.GetInsertPoint()));
2668 return CreateAdd(StartValue, Offset);
2669 }
2670 case InductionDescriptor::IK_PtrInduction: {
2671 assert(isa<SCEVConstant>(Step) &&(static_cast <bool> (isa<SCEVConstant>(Step) &&
"Expected constant step for pointer induction") ? void (0) :
__assert_fail ("isa<SCEVConstant>(Step) && \"Expected constant step for pointer induction\""
, "/home/username/llvm-project/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp"
, 2672, __extension__ __PRETTY_FUNCTION__))
2672 "Expected constant step for pointer induction")(static_cast <bool> (isa<SCEVConstant>(Step) &&
"Expected constant step for pointer induction") ? void (0) :
__assert_fail ("isa<SCEVConstant>(Step) && \"Expected constant step for pointer induction\""
, "/home/username/llvm-project/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp"
, 2672, __extension__ __PRETTY_FUNCTION__))
;
2673 return B.CreateGEP(
2674 StartValue->getType()->getPointerElementType(), StartValue,
2675 CreateMul(Index, Exp.expandCodeFor(Step, Index->getType(),
2676 &*B.GetInsertPoint())));
2677 }
2678 case InductionDescriptor::IK_FpInduction: {
2679 assert(Step->getType()->isFloatingPointTy() && "Expected FP Step value")(static_cast <bool> (Step->getType()->isFloatingPointTy
() && "Expected FP Step value") ? void (0) : __assert_fail
("Step->getType()->isFloatingPointTy() && \"Expected FP Step value\""
, "/home/username/llvm-project/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp"
, 2679, __extension__ __PRETTY_FUNCTION__))
;
2680 auto InductionBinOp = ID.getInductionBinOp();
2681 assert(InductionBinOp &&(static_cast <bool> (InductionBinOp && (InductionBinOp
->getOpcode() == Instruction::FAdd || InductionBinOp->getOpcode
() == Instruction::FSub) && "Original bin op should be defined for FP induction"
) ? void (0) : __assert_fail ("InductionBinOp && (InductionBinOp->getOpcode() == Instruction::FAdd || InductionBinOp->getOpcode() == Instruction::FSub) && \"Original bin op should be defined for FP induction\""
, "/home/username/llvm-project/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp"
, 2684, __extension__ __PRETTY_FUNCTION__))
2682 (InductionBinOp->getOpcode() == Instruction::FAdd ||(static_cast <bool> (InductionBinOp && (InductionBinOp
->getOpcode() == Instruction::FAdd || InductionBinOp->getOpcode
() == Instruction::FSub) && "Original bin op should be defined for FP induction"
) ? void (0) : __assert_fail ("InductionBinOp && (InductionBinOp->getOpcode() == Instruction::FAdd || InductionBinOp->getOpcode() == Instruction::FSub) && \"Original bin op should be defined for FP induction\""
, "/home/username/llvm-project/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp"
, 2684, __extension__ __PRETTY_FUNCTION__))
2683 InductionBinOp->getOpcode() == Instruction::FSub) &&(static_cast <bool> (InductionBinOp && (InductionBinOp
->getOpcode() == Instruction::FAdd || InductionBinOp->getOpcode
() == Instruction::FSub) && "Original bin op should be defined for FP induction"
) ? void (0) : __assert_fail ("InductionBinOp && (InductionBinOp->getOpcode() == Instruction::FAdd || InductionBinOp->getOpcode() == Instruction::FSub) && \"Original bin op should be defined for FP induction\""
, "/home/username/llvm-project/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp"
, 2684, __extension__ __PRETTY_FUNCTION__))
2684 "Original bin op should be defined for FP induction")(static_cast <bool> (InductionBinOp && (InductionBinOp
->getOpcode() == Instruction::FAdd || InductionBinOp->getOpcode
() == Instruction::FSub) && "Original bin op should be defined for FP induction"
) ? void (0) : __assert_fail ("InductionBinOp && (InductionBinOp->getOpcode() == Instruction::FAdd || InductionBinOp->getOpcode() == Instruction::FSub) && \"Original bin op should be defined for FP induction\""
, "/home/username/llvm-project/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp"
, 2684, __extension__ __PRETTY_FUNCTION__))
;
2685
2686 Value *StepValue = cast<SCEVUnknown>(Step)->getValue();
2687
2688 // Floating point operations had to be 'fast' to enable the induction.
2689 FastMathFlags Flags;
2690 Flags.setFast();
2691
2692 Value *MulExp = B.CreateFMul(StepValue, Index);
2693 if (isa<Instruction>(MulExp))
2694 // We have to check, the MulExp may be a constant.
2695 cast<Instruction>(MulExp)->setFastMathFlags(Flags);
2696
2697 Value *BOp = B.CreateBinOp(InductionBinOp->getOpcode(), StartValue, MulExp,
2698 "induction");
2699 if (isa<Instruction>(BOp))
2700 cast<Instruction>(BOp)->setFastMathFlags(Flags);
2701
2702 return BOp;
2703 }
2704 case InductionDescriptor::IK_NoInduction:
2705 return nullptr;
2706 }
2707 llvm_unreachable("invalid enum")::llvm::llvm_unreachable_internal("invalid enum", "/home/username/llvm-project/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp"
, 2707)
;
2708}
2709
2710BasicBlock *InnerLoopVectorizer::createVectorizedLoopSkeleton() {
2711 /*
2712 In this function we generate a new loop. The new loop will contain
2713 the vectorized instructions while the old loop will continue to run the
2714 scalar remainder.
2715
2716 [ ] <-- loop iteration number check.
2717 / |
2718 / v
2719 | [ ] <-- vector loop bypass (may consist of multiple blocks).
2720 | / |
2721 | / v
2722 || [ ] <-- vector pre header.
2723 |/ |
2724 | v
2725 | [ ] \
2726 | [ ]_| <-- vector loop.
2727 | |
2728 | v
2729 | -[ ] <--- middle-block.
2730 | / |
2731 | / v
2732 -|- >[ ] <--- new preheader.
2733 | |
2734 | v
2735 | [ ] \
2736 | [ ]_| <-- old scalar loop to handle remainder.
2737 \ |
2738 \ v
2739 >[ ] <-- exit block.
2740 ...
2741 */
2742
2743 BasicBlock *OldBasicBlock = OrigLoop->getHeader();
2744 BasicBlock *VectorPH = OrigLoop->getLoopPreheader();
2745 BasicBlock *ExitBlock = OrigLoop->getExitBlock();
2746 MDNode *OrigLoopID = OrigLoop->getLoopID();
2747 assert(VectorPH && "Invalid loop structure")(static_cast <bool> (VectorPH && "Invalid loop structure"
) ? void (0) : __assert_fail ("VectorPH && \"Invalid loop structure\""
, "/home/username/llvm-project/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp"
, 2747, __extension__ __PRETTY_FUNCTION__))
;
2748 assert(ExitBlock && "Must have an exit block")(static_cast <bool> (ExitBlock && "Must have an exit block"
) ? void (0) : __assert_fail ("ExitBlock && \"Must have an exit block\""
, "/home/username/llvm-project/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp"
, 2748, __extension__ __PRETTY_FUNCTION__))
;
2749
2750 // Some loops have a single integer induction variable, while other loops
2751 // don't. One example is c++ iterators that often have multiple pointer
2752 // induction variables. In the code below we also support a case where we
2753 // don't have a single induction variable.
2754 //
2755 // We try to obtain an induction variable from the original loop as hard
2756 // as possible. However if we don't find one that:
2757 // - is an integer
2758 // - counts from zero, stepping by one
2759 // - is the size of the widest induction variable type
2760 // then we create a new one.
2761 OldInduction = Legal->getPrimaryInduction();
2762 Type *IdxTy = Legal->getWidestInductionType();
2763
2764 // Split the single block loop into the two loop structure described above.
2765 BasicBlock *VecBody =
2766 VectorPH->splitBasicBlock(VectorPH->getTerminator(), "vector.body");
2767 BasicBlock *MiddleBlock =
2768 VecBody->splitBasicBlock(VecBody->getTerminator(), "middle.block");
2769 BasicBlock *ScalarPH =
2770 MiddleBlock->splitBasicBlock(MiddleBlock->getTerminator(), "scalar.ph");
2771
2772 // Create and register the new vector loop.
2773 Loop *Lp = LI->AllocateLoop();
2774 Loop *ParentLoop = OrigLoop->getParentLoop();
2775
2776 // Insert the new loop into the loop nest and register the new basic blocks
2777 // before calling any utilities such as SCEV that require valid LoopInfo.
2778 if (ParentLoop) {
2779 ParentLoop->addChildLoop(Lp);
2780 ParentLoop->addBasicBlockToLoop(ScalarPH, *LI);
2781 ParentLoop->addBasicBlockToLoop(MiddleBlock, *LI);
2782 } else {
2783 LI->addTopLevelLoop(Lp);
2784 }
2785 Lp->addBasicBlockToLoop(VecBody, *LI);
2786
2787 // Find the loop boundaries.
2788 Value *Count = getOrCreateTripCount(Lp);
2789
2790 Value *StartIdx = ConstantInt::get(IdxTy, 0);
2791
2792 // Now, compare the new count to zero. If it is zero skip the vector loop and
2793 // jump to the scalar loop. This check also covers the case where the
2794 // backedge-taken count is uint##_max: adding one to it will overflow leading
2795 // to an incorrect trip count of zero. In this (rare) case we will also jump
2796 // to the scalar loop.
2797 emitMinimumIterationCountCheck(Lp, ScalarPH);
2798
2799 // Generate the code to check any assumptions that we've made for SCEV
2800 // expressions.
2801 emitSCEVChecks(Lp, ScalarPH);
2802
2803 // Generate the code that checks in runtime if arrays overlap. We put the
2804 // checks into a separate block to make the more common case of few elements
2805 // faster.
2806 emitMemRuntimeChecks(Lp, ScalarPH);
2807
2808 // Generate the induction variable.
2809 // The loop step is equal to the vectorization factor (num of SIMD elements)
2810 // times the unroll factor (num of SIMD instructions).
2811 Value *CountRoundDown = getOrCreateVectorTripCount(Lp);
2812 Constant *Step = ConstantInt::get(IdxTy, VF * UF);
2813 Induction =
2814 createInductionVariable(Lp, StartIdx, CountRoundDown, Step,
2815 getDebugLocFromInstOrOperands(OldInduction));
2816
2817 // We are going to resume the execution of the scalar loop.
2818 // Go over all of the induction variables that we found and fix the
2819 // PHIs that are left in the scalar version of the loop.
2820 // The starting values of PHI nodes depend on the counter of the last
2821 // iteration in the vectorized loop.
2822 // If we come from a bypass edge then we need to start from the original
2823 // start value.
2824
2825 // This variable saves the new starting index for the scalar loop. It is used
2826 // to test if there are any tail iterations left once the vector loop has
2827 // completed.
2828 LoopVectorizationLegality::InductionList *List = Legal->getInductionVars();
2829 for (auto &InductionEntry : *List) {
2830 PHINode *OrigPhi = InductionEntry.first;
2831 InductionDescriptor II = InductionEntry.second;
2832
2833 // Create phi nodes to merge from the backedge-taken check block.
2834 PHINode *BCResumeVal = PHINode::Create(
2835 OrigPhi->getType(), 3, "bc.resume.val", ScalarPH->getTerminator());
2836 // Copy original phi DL over to the new one.
2837 BCResumeVal->setDebugLoc(OrigPhi->getDebugLoc());
2838 Value *&EndValue = IVEndValues[OrigPhi];
2839 if (OrigPhi == OldInduction) {
2840 // We know what the end value is.
2841 EndValue = CountRoundDown;
2842 } else {
2843 IRBuilder<> B(Lp->getLoopPreheader()->getTerminator());
2844 Type *StepType = II.getStep()->getType();
2845 Instruction::CastOps CastOp =
2846 CastInst::getCastOpcode(CountRoundDown, true, StepType, true);
2847 Value *CRD = B.CreateCast(CastOp, CountRoundDown, StepType, "cast.crd");
2848 const DataLayout &DL = OrigLoop->getHeader()->getModule()->getDataLayout();
2849 EndValue = emitTransformedIndex(B, CRD, PSE.getSE(), DL, II);
2850 EndValue->setName("ind.end");
2851 }
2852
2853 // The new PHI merges the original incoming value, in case of a bypass,
2854 // or the value at the end of the vectorized loop.
2855 BCResumeVal->addIncoming(EndValue, MiddleBlock);
2856
2857 // Fix the scalar body counter (PHI node).
2858 unsigned BlockIdx = OrigPhi->getBasicBlockIndex(ScalarPH);
2859
2860 // The old induction's phi node in the scalar body needs the truncated
2861 // value.
2862 for (BasicBlock *BB : LoopBypassBlocks)
2863 BCResumeVal->addIncoming(II.getStartValue(), BB);
2864 OrigPhi->setIncomingValue(BlockIdx, BCResumeVal);
2865 }
2866
2867 // We need the OrigLoop (scalar loop part) latch terminator to help
2868 // produce correct debug info for the middle block BB instructions.
2869 // The legality check stage guarantees that the loop will have a single
2870 // latch.
2871 assert(isa<BranchInst>(OrigLoop->getLoopLatch()->getTerminator()) &&(static_cast <bool> (isa<BranchInst>(OrigLoop->
getLoopLatch()->getTerminator()) && "Scalar loop latch terminator isn't a branch"
) ? void (0) : __assert_fail ("isa<BranchInst>(OrigLoop->getLoopLatch()->getTerminator()) && \"Scalar loop latch terminator isn't a branch\""
, "/home/username/llvm-project/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp"
, 2872, __extension__ __PRETTY_FUNCTION__))
2872 "Scalar loop latch terminator isn't a branch")(static_cast <bool> (isa<BranchInst>(OrigLoop->
getLoopLatch()->getTerminator()) && "Scalar loop latch terminator isn't a branch"
) ? void (0) : __assert_fail ("isa<BranchInst>(OrigLoop->getLoopLatch()->getTerminator()) && \"Scalar loop latch terminator isn't a branch\""
, "/home/username/llvm-project/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp"
, 2872, __extension__ __PRETTY_FUNCTION__))
;
2873 BranchInst *ScalarLatchBr =
2874 cast<BranchInst>(OrigLoop->getLoopLatch()->getTerminator());
2875
2876 // Add a check in the middle block to see if we have completed
2877 // all of the iterations in the first vector loop.
2878 // If (N - N%VF) == N, then we *don't* need to run the remainder.
2879 // If tail is to be folded, we know we don't need to run the remainder.
2880 Value *CmpN = Builder.getTrue();
2881 if (!Cost->foldTailByMasking()) {
2882 CmpN =
2883 CmpInst::Create(Instruction::ICmp, CmpInst::ICMP_EQ, Count,
2884 CountRoundDown, "cmp.n", MiddleBlock->getTerminator());
2885
2886 // Provide correct stepping behaviour by using the same DebugLoc as the
2887 // scalar loop latch branch cmp if it exists.
2888 if (CmpInst *ScalarLatchCmp =
2889 dyn_cast_or_null<CmpInst>(ScalarLatchBr->getCondition()))
2890 cast<Instruction>(CmpN)->setDebugLoc(ScalarLatchCmp->getDebugLoc());
2891 }
2892
2893 BranchInst *BrInst = BranchInst::Create(ExitBlock, ScalarPH, CmpN);
2894 BrInst->setDebugLoc(ScalarLatchBr->getDebugLoc());
2895 ReplaceInstWithInst(MiddleBlock->getTerminator(), BrInst);
2896
2897 // Get ready to start creating new instructions into the vectorized body.
2898 Builder.SetInsertPoint(&*VecBody->getFirstInsertionPt());
2899
2900 // Save the state.
2901 LoopVectorPreHeader = Lp->getLoopPreheader();
2902 LoopScalarPreHeader = ScalarPH;
2903 LoopMiddleBlock = MiddleBlock;
2904 LoopExitBlock = ExitBlock;
2905 LoopVectorBody = VecBody;
2906 LoopScalarBody = OldBasicBlock;
2907
2908 Optional<MDNode *> VectorizedLoopID =
2909 makeFollowupLoopID(OrigLoopID, {LLVMLoopVectorizeFollowupAll,
2910 LLVMLoopVectorizeFollowupVectorized});
2911 if (VectorizedLoopID.hasValue()) {
2912 Lp->setLoopID(VectorizedLoopID.getValue());
2913
2914 // Do not setAlreadyVectorized if loop attributes have been defined
2915 // explicitly.
2916 return LoopVectorPreHeader;
2917 }
2918
2919 // Keep all loop hints from the original loop on the vector loop (we'll
2920 // replace the vectorizer-specific hints below).
2921 if (MDNode *LID = OrigLoop->getLoopID())
2922 Lp->setLoopID(LID);
2923
2924 LoopVectorizeHints Hints(Lp, true, *ORE);
2925 Hints.setAlreadyVectorized();
2926
2927 return LoopVectorPreHeader;
2928}
2929
2930// Fix up external users of the induction variable. At this point, we are
2931// in LCSSA form, with all external PHIs that use the IV having one input value,
2932// coming from the remainder loop. We need those PHIs to also have a correct
2933// value for the IV when arriving directly from the middle block.
2934void InnerLoopVectorizer::fixupIVUsers(PHINode *OrigPhi,
2935 const InductionDescriptor &II,
2936 Value *CountRoundDown, Value *EndValue,
2937 BasicBlock *MiddleBlock) {
2938 // There are two kinds of external IV usages - those that use the value
2939 // computed in the last iteration (the PHI) and those that use the penultimate
2940 // value (the value that feeds into the phi from the loop latch).
2941 // We allow both, but they, obviously, have different values.
2942
2943 assert(OrigLoop->getExitBlock() && "Expected a single exit block")(static_cast <bool> (OrigLoop->getExitBlock() &&
"Expected a single exit block") ? void (0) : __assert_fail (
"OrigLoop->getExitBlock() && \"Expected a single exit block\""
, "/home/username/llvm-project/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp"
, 2943, __extension__ __PRETTY_FUNCTION__))
;
2944
2945 DenseMap<Value *, Value *> MissingVals;
2946
2947 // An external user of the last iteration's value should see the value that
2948 // the remainder loop uses to initialize its own IV.
2949 Value *PostInc = OrigPhi->getIncomingValueForBlock(OrigLoop->getLoopLatch());
2950 for (User *U : PostInc->users()) {
2951 Instruction *UI = cast<Instruction>(U);
2952 if (!OrigLoop->contains(UI)) {
2953 assert(isa<PHINode>(UI) && "Expected LCSSA form")(static_cast <bool> (isa<PHINode>(UI) && "Expected LCSSA form"
) ? void (0) : __assert_fail ("isa<PHINode>(UI) && \"Expected LCSSA form\""
, "/home/username/llvm-project/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp"
, 2953, __extension__ __PRETTY_FUNCTION__))
;
2954 MissingVals[UI] = EndValue;
2955 }
2956 }
2957
2958 // An external user of the penultimate value need to see EndValue - Step.
2959 // The simplest way to get this is to recompute it from the constituent SCEVs,
2960 // that is Start + (Step * (CRD - 1)).
2961 for (User *U : OrigPhi->users()) {
2962 auto *UI = cast<Instruction>(U);
2963 if (!OrigLoop->contains(UI)) {
2964 const DataLayout &DL =
2965 OrigLoop->getHeader()->getModule()->getDataLayout();
2966 assert(isa<PHINode>(UI) && "Expected LCSSA form")(static_cast <bool> (isa<PHINode>(UI) && "Expected LCSSA form"
) ? void (0) : __assert_fail ("isa<PHINode>(UI) && \"Expected LCSSA form\""
, "/home/username/llvm-project/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp"
, 2966, __extension__ __PRETTY_FUNCTION__))
;
2967
2968 IRBuilder<> B(MiddleBlock->getTerminator());
2969 Value *CountMinusOne = B.CreateSub(
2970 CountRoundDown, ConstantInt::get(CountRoundDown->getType(), 1));
2971 Value *CMO =
2972 !II.getStep()->getType()->isIntegerTy()
2973 ? B.CreateCast(Instruction::SIToFP, CountMinusOne,
2974 II.getStep()->getType())
2975 : B.CreateSExtOrTrunc(CountMinusOne, II.getStep()->getType());
2976 CMO->setName("cast.cmo");
2977 Value *Escape = emitTransformedIndex(B, CMO, PSE.getSE(), DL, II);
2978 Escape->setName("ind.escape");
2979 MissingVals[UI] = Escape;
2980 }
2981 }
2982
2983 for (auto &I : MissingVals) {
2984 PHINode *PHI = cast<PHINode>(I.first);
2985 // One corner case we have to handle is two IVs "chasing" each-other,
2986 // that is %IV2 = phi [...], [ %IV1, %latch ]
2987 // In this case, if IV1 has an external use, we need to avoid adding both
2988 // "last value of IV1" and "penultimate value of IV2". So, verify that we
2989 // don't already have an incoming value for the middle block.
2990 if (PHI->getBasicBlockIndex(MiddleBlock) == -1)
2991 PHI->addIncoming(I.second, MiddleBlock);
2992 }
2993}
2994
2995namespace {
2996
2997struct CSEDenseMapInfo {
2998 static bool canHandle(const Instruction *I) {
2999 return isa<InsertElementInst>(I) || isa<ExtractElementInst>(I) ||
3000 isa<ShuffleVectorInst>(I) || isa<GetElementPtrInst>(I);
3001 }
3002
3003 static inline Instruction *getEmptyKey() {
3004 return DenseMapInfo<Instruction *>::getEmptyKey();
3005 }
3006
3007 static inline Instruction *getTombstoneKey() {
3008 return DenseMapInfo<Instruction *>::getTombstoneKey();
3009 }
3010
3011 static unsigned getHashValue(const Instruction *I) {
3012 assert(canHandle(I) && "Unknown instruction!")(static_cast <bool> (canHandle(I) && "Unknown instruction!"
) ? void (0) : __assert_fail ("canHandle(I) && \"Unknown instruction!\""
, "/home/username/llvm-project/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp"
, 3012, __extension__ __PRETTY_FUNCTION__))
;
3013 return hash_combine(I->getOpcode(), hash_combine_range(I->value_op_begin(),
3014 I->value_op_end()));
3015 }
3016
3017 static bool isEqual(const Instruction *LHS, const Instruction *RHS) {
3018 if (LHS == getEmptyKey() || RHS == getEmptyKey() ||
3019 LHS == getTombstoneKey() || RHS == getTombstoneKey())
3020 return LHS == RHS;
3021 return LHS->isIdenticalTo(RHS);
3022 }
3023};
3024
3025} // end anonymous namespace
3026
3027///Perform cse of induction variable instructions.
3028static void cse(BasicBlock *BB) {
3029 // Perform simple cse.
3030 SmallDenseMap<Instruction *, Instruction *, 4, CSEDenseMapInfo> CSEMap;
3031 for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E;) {
3032 Instruction *In = &*I++;
3033
3034 if (!CSEDenseMapInfo::canHandle(In))
3035 continue;
3036
3037 // Check if we can replace this instruction with any of the
3038 // visited instructions.
3039 if (Instruction *V = CSEMap.lookup(In)) {
3040 In->replaceAllUsesWith(V);
3041 In->eraseFromParent();
3042 continue;
3043 }
3044
3045 CSEMap[In] = In;
3046 }
3047}
3048
3049/// Estimate the overhead of scalarizing an instruction. This is a
3050/// convenience wrapper for the type-based getScalarizationOverhead API.
3051static unsigned getScalarizationOverhead(Instruction *I, unsigned VF,
3052 const TargetTransformInfo &TTI) {
3053 if (VF == 1)
3054 return 0;
3055
3056 unsigned Cost = 0;
3057 Type *RetTy = ToVectorTy(I->getType(), VF);
3058 if (!RetTy->isVoidTy() &&
3059 (!isa<LoadInst>(I) ||
3060 !TTI.supportsEfficientVectorElementLoadStore()))
3061 Cost += TTI.getScalarizationOverhead(RetTy, true, false);
3062
3063 // Some targets keep addresses scalar.
3064 if (isa<LoadInst>(I) && !TTI.prefersVectorizedAddressing())
3065 return Cost;
3066
3067 if (CallInst *CI = dyn_cast<CallInst>(I)) {
3068 SmallVector<const Value *, 4> Operands(CI->arg_operands());
3069 Cost += TTI.getOperandsScalarizationOverhead(Operands, VF);
3070 }
3071 else if (!isa<StoreInst>(I) ||
3072 !TTI.supportsEfficientVectorElementLoadStore()) {
3073 SmallVector<const Value *, 4> Operands(I->operand_values());
3074 Cost += TTI.getOperandsScalarizationOverhead(Operands, VF);
3075 }
3076
3077 return Cost;
3078}
3079
3080// Estimate cost of a call instruction CI if it were vectorized with factor VF.
3081// Return the cost of the instruction, including scalarization overhead if it's
3082// needed. The flag NeedToScalarize shows if the call needs to be scalarized -
3083// i.e. either vector version isn't available, or is too expensive.
3084static unsigned getVectorCallCost(CallInst *CI, unsigned VF,
3085 const TargetTransformInfo &TTI,
3086 const TargetLibraryInfo *TLI,
3087 bool &NeedToScalarize) {
3088 Function *F = CI->getCalledFunction();
3089 StringRef FnName = CI->getCalledFunction()->getName();
3090 Type *ScalarRetTy = CI->getType();
3091 SmallVector<Type *, 4> Tys, ScalarTys;
3092 for (auto &ArgOp : CI->arg_operands())
3093 ScalarTys.push_back(ArgOp->getType());
3094
3095 // Estimate cost of scalarized vector call. The source operands are assumed
3096 // to be vectors, so we need to extract individual elements from there,
3097 // execute VF scalar calls, and then gather the result into the vector return
3098 // value.
3099 unsigned ScalarCallCost = TTI.getCallInstrCost(F, ScalarRetTy, ScalarTys);
3100 if (VF == 1)
3101 return ScalarCallCost;
3102
3103 // Compute corresponding vector type for return value and arguments.
3104 Type *RetTy = ToVectorTy(ScalarRetTy, VF);
3105 for (Type *ScalarTy : ScalarTys)
3106 Tys.push_back(ToVectorTy(ScalarTy, VF));
3107
3108 // Compute costs of unpacking argument values for the scalar calls and
3109 // packing the return values to a vector.
3110 unsigned ScalarizationCost = getScalarizationOverhead(CI, VF, TTI);
3111
3112 unsigned Cost = ScalarCallCost * VF + ScalarizationCost;
3113
3114 // If we can't emit a vector call for this function, then the currently found
3115 // cost is the cost we need to return.
3116 NeedToScalarize = true;
3117 if (!TLI || !TLI->isFunctionVectorizable(FnName, VF) || CI->isNoBuiltin())
3118 return Cost;
3119
3120 // If the corresponding vector cost is cheaper, return its cost.
3121 unsigned VectorCallCost = TTI.getCallInstrCost(nullptr, RetTy, Tys);
3122 if (VectorCallCost < Cost) {
3123 NeedToScalarize = false;
3124 return VectorCallCost;
3125 }
3126 return Cost;
3127}
3128
3129// Estimate cost of an intrinsic call instruction CI if it were vectorized with
3130// factor VF. Return the cost of the instruction, including scalarization
3131// overhead if it's needed.
3132static unsigned getVectorIntrinsicCost(CallInst *CI, unsigned VF,
3133 const TargetTransformInfo &TTI,
3134 const TargetLibraryInfo *TLI) {
3135 Intrinsic::ID ID = getVectorIntrinsicIDForCall(CI, TLI);
3136 assert(ID && "Expected intrinsic call!")(static_cast <bool> (ID && "Expected intrinsic call!"
) ? void (0) : __assert_fail ("ID && \"Expected intrinsic call!\""
, "/home/username/llvm-project/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp"
, 3136, __extension__ __PRETTY_FUNCTION__))
;
3137
3138 FastMathFlags FMF;
3139 if (auto *FPMO = dyn_cast<FPMathOperator>(CI))
3140 FMF = FPMO->getFastMathFlags();
3141
3142 SmallVector<Value *, 4> Operands(CI->arg_operands());
3143 return TTI.getIntrinsicInstrCost(ID, CI->getType(), Operands, FMF, VF);
3144}
3145
3146static Type *smallestIntegerVectorType(Type *T1, Type *T2) {
3147 auto *I1 = cast<IntegerType>(T1->getVectorElementType());
3148 auto *I2 = cast<IntegerType>(T2->getVectorElementType());
3149 return I1->getBitWidth() < I2->getBitWidth() ? T1 : T2;
3150}
3151static Type *largestIntegerVectorType(Type *T1, Type *T2) {
3152 auto *I1 = cast<IntegerType>(T1->getVectorElementType());
3153 auto *I2 = cast<IntegerType>(T2->getVectorElementType());
3154 return I1->getBitWidth() > I2->getBitWidth() ? T1 : T2;
3155}
3156
3157void InnerLoopVectorizer::truncateToMinimalBitwidths() {
3158 // For every instruction `I` in MinBWs, truncate the operands, create a
3159 // truncated version of `I` and reextend its result. InstCombine runs
3160 // later and will remove any ext/trunc pairs.
3161 SmallPtrSet<Value *, 4> Erased;
3162 for (const auto &KV : Cost->getMinimalBitwidths()) {
3163 // If the value wasn't vectorized, we must maintain the original scalar
3164 // type. The absence of the value from VectorLoopValueMap indicates that it
3165 // wasn't vectorized.
3166 if (!VectorLoopValueMap.hasAnyVectorValue(KV.first))
3167 continue;
3168 for (unsigned Part = 0; Part < UF; ++Part) {
3169 Value *I = getOrCreateVectorValue(KV.first, Part);
3170 if (Erased.find(I) != Erased.end() || I->use_empty() ||
3171 !isa<Instruction>(I))
3172 continue;
3173 Type *OriginalTy = I->getType();
3174 Type *ScalarTruncatedTy =
3175 IntegerType::get(OriginalTy->getContext(), KV.second);
3176 Type *TruncatedTy = VectorType::get(ScalarTruncatedTy,
3177 OriginalTy->getVectorNumElements());
3178 if (TruncatedTy == OriginalTy)
3179 continue;
3180
3181 IRBuilder<> B(cast<Instruction>(I));
3182 auto ShrinkOperand = [&](Value *V) -> Value * {
3183 if (auto *ZI = dyn_cast<ZExtInst>(V))
3184 if (ZI->getSrcTy() == TruncatedTy)
3185 return ZI->getOperand(0);
3186 return B.CreateZExtOrTrunc(V, TruncatedTy);
3187 };
3188
3189 // The actual instruction modification depends on the instruction type,
3190 // unfortunately.
3191 Value *NewI = nullptr;
3192 if (auto *BO = dyn_cast<BinaryOperator>(I)) {
3193 NewI = B.CreateBinOp(BO->getOpcode(), ShrinkOperand(BO->getOperand(0)),
3194 ShrinkOperand(BO->getOperand(1)));
3195
3196 // Any wrapping introduced by shrinking this operation shouldn't be
3197 // considered undefined behavior. So, we can't unconditionally copy
3198 // arithmetic wrapping flags to NewI.
3199 cast<BinaryOperator>(NewI)->copyIRFlags(I, /*IncludeWrapFlags=*/false);
3200 } else if (auto *CI = dyn_cast<ICmpInst>(I)) {
3201 NewI =
3202 B.CreateICmp(CI->getPredicate(), ShrinkOperand(CI->getOperand(0)),
3203 ShrinkOperand(CI->getOperand(1)));
3204 } else if (auto *SI = dyn_cast<SelectInst>(I)) {
3205 NewI = B.CreateSelect(SI->getCondition(),
3206 ShrinkOperand(SI->getTrueValue()),
3207 ShrinkOperand(SI->getFalseValue()));
3208 } else if (auto *CI = dyn_cast<CastInst>(I)) {
3209 switch (CI->getOpcode()) {
3210 default:
3211 llvm_unreachable("Unhandled cast!")::llvm::llvm_unreachable_internal("Unhandled cast!", "/home/username/llvm-project/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp"
, 3211)
;
3212 case Instruction::Trunc:
3213 NewI = ShrinkOperand(CI->getOperand(0));
3214 break;
3215 case Instruction::SExt:
3216 NewI = B.CreateSExtOrTrunc(
3217 CI->getOperand(0),
3218 smallestIntegerVectorType(OriginalTy, TruncatedTy));
3219 break;
3220 case Instruction::ZExt:
3221 NewI = B.CreateZExtOrTrunc(
3222 CI->getOperand(0),
3223 smallestIntegerVectorType(OriginalTy, TruncatedTy));
3224 break;
3225 }
3226 } else if (auto *SI = dyn_cast<ShuffleVectorInst>(I)) {
3227 auto Elements0 = SI->getOperand(0)->getType()->getVectorNumElements();
3228 auto *O0 = B.CreateZExtOrTrunc(
3229 SI->getOperand(0), VectorType::get(ScalarTruncatedTy, Elements0));
3230 auto Elements1 = SI->getOperand(1)->getType()->getVectorNumElements();
3231 auto *O1 = B.CreateZExtOrTrunc(
3232 SI->getOperand(1), VectorType::get(ScalarTruncatedTy, Elements1));
3233
3234 NewI = B.CreateShuffleVector(O0, O1, SI->getMask());
3235 } else if (isa<LoadInst>(I) || isa<PHINode>(I)) {
3236 // Don't do anything with the operands, just extend the result.
3237 continue;
3238 } else if (auto *IE = dyn_cast<InsertElementInst>(I)) {
3239 auto Elements = IE->getOperand(0)->getType()->getVectorNumElements();
3240 auto *O0 = B.CreateZExtOrTrunc(
3241 IE->getOperand(0), VectorType::get(ScalarTruncatedTy, Elements));
3242 auto *O1 = B.CreateZExtOrTrunc(IE->getOperand(1), ScalarTruncatedTy);
3243 NewI = B.CreateInsertElement(O0, O1, IE->getOperand(2));
3244 } else if (auto *EE = dyn_cast<ExtractElementInst>(I)) {
3245 auto Elements = EE->getOperand(0)->getType()->getVectorNumElements();
3246 auto *O0 = B.CreateZExtOrTrunc(
3247 EE->getOperand(0), VectorType::get(ScalarTruncatedTy, Elements));
3248 NewI = B.CreateExtractElement(O0, EE->getOperand(2));
3249 } else {
3250 // If we don't know what to do, be conservative and don't do anything.
3251 continue;
3252 }
3253
3254 // Lastly, extend the result.
3255 NewI->takeName(cast<Instruction>(I));
3256 Value *Res = B.CreateZExtOrTrunc(NewI, OriginalTy);
3257 I->replaceAllUsesWith(Res);
3258 cast<Instruction>(I)->eraseFromParent();
3259 Erased.insert(I);
3260 VectorLoopValueMap.resetVectorValue(KV.first, Part, Res);
3261 }
3262 }
3263
3264 // We'll have created a bunch of ZExts that are now parentless. Clean up.
3265 for (const auto &KV : Cost->getMinimalBitwidths()) {
3266 // If the value wasn't vectorized, we must maintain the original scalar
3267 // type. The absence of the value from VectorLoopValueMap indicates that it
3268 // wasn't vectorized.
3269 if (!VectorLoopValueMap.hasAnyVectorValue(KV.first))
3270 continue;
3271 for (unsigned Part = 0; Part < UF; ++Part) {
3272 Value *I = getOrCreateVectorValue(KV.first, Part);
3273 ZExtInst *Inst = dyn_cast<ZExtInst>(I);
3274 if (Inst && Inst->use_empty()) {
3275 Value *NewI = Inst->getOperand(0);
3276 Inst->eraseFromParent();
3277 VectorLoopValueMap.resetVectorValue(KV.first, Part, NewI);
3278 }
3279 }
3280 }
3281}
3282
3283void InnerLoopVectorizer::fixVectorizedLoop() {
3284 // Insert truncates and extends for any truncated instructions as hints to
3285 // InstCombine.
3286 if (VF > 1)
3287 truncateToMinimalBitwidths();
3288
3289 // Fix widened non-induction PHIs by setting up the PHI operands.
3290 if (OrigPHIsToFix.size()) {
3291 assert(EnableVPlanNativePath &&(static_cast <bool> (EnableVPlanNativePath && "Unexpected non-induction PHIs for fixup in non VPlan-native path"
) ? void (0) : __assert_fail ("EnableVPlanNativePath && \"Unexpected non-induction PHIs for fixup in non VPlan-native path\""
, "/home/username/llvm-project/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp"
, 3292, __extension__ __PRETTY_FUNCTION__))
3292 "Unexpected non-induction PHIs for fixup in non VPlan-native path")(static_cast <bool> (EnableVPlanNativePath && "Unexpected non-induction PHIs for fixup in non VPlan-native path"
) ? void (0) : __assert_fail ("EnableVPlanNativePath && \"Unexpected non-induction PHIs for fixup in non VPlan-native path\""
, "/home/username/llvm-project/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp"
, 3292, __extension__ __PRETTY_FUNCTION__))
;
3293 fixNonInductionPHIs();
3294 }
3295
3296 // At this point every instruction in the original loop is widened to a
3297 // vector form. Now we need to fix the recurrences in the loop. These PHI
3298 // nodes are currently empty because we did not want to introduce cycles.
3299 // This is the second stage of vectorizing recurrences.
3300 fixCrossIterationPHIs();
3301
3302 // Update the dominator tree.
3303 //
3304 // FIXME: After creating the structure of the new loop, the dominator tree is
3305 // no longer up-to-date, and it remains that way until we update it
3306 // here. An out-of-date dominator tree is problematic for SCEV,
3307 // because SCEVExpander uses it to guide code generation. The
3308 // vectorizer use SCEVExpanders in several places. Instead, we should
3309 // keep the dominator tree up-to-date as we go.
3310 updateAnalysis();
3311
3312 // Fix-up external users of the induction variables.
3313 for (auto &Entry : *Legal->getInductionVars())
3314 fixupIVUsers(Entry.first, Entry.second,
3315 getOrCreateVectorTripCount(LI->getLoopFor(LoopVectorBody)),
3316 IVEndValues[Entry.first], LoopMiddleBlock);
3317
3318 fixLCSSAPHIs();
3319 for (Instruction *PI : PredicatedInstructions)
3320 sinkScalarOperands(&*PI);
3321
3322 // Remove redundant induction instructions.
3323 cse(LoopVectorBody);
3324}
3325
3326void InnerLoopVectorizer::fixCrossIterationPHIs() {
3327 // In order to support recurrences we need to be able to vectorize Phi nodes.
3328 // Phi nodes have cycles, so we need to vectorize them in two stages. This is
3329 // stage #2: We now need to fix the recurrences by adding incoming edges to
3330 // the currently empty PHI nodes. At this point every instruction in the
3331 // original loop is widened to a vector form so we can use them to construct
3332 // the incoming edges.
3333 for (PHINode &Phi : OrigLoop->getHeader()->phis()) {
3334 // Handle first-order recurrences and reductions that need to be fixed.
3335 if (Legal->isFirstOrderRecurrence(&Phi))
3336 fixFirstOrderRecurrence(&Phi);
3337 else if (Legal->isReductionVariable(&Phi))
3338 fixReduction(&Phi);
3339 }
3340}
3341
3342void InnerLoopVectorizer::fixFirstOrderRecurrence(PHINode *Phi) {
3343 // This is the second phase of vectorizing first-order recurrences. An
3344 // overview of the transformation is described below. Suppose we have the
3345 // following loop.
3346 //
3347 // for (int i = 0; i < n; ++i)
3348 // b[i] = a[i] - a[i - 1];
3349 //
3350 // There is a first-order recurrence on "a". For this loop, the shorthand
3351 // scalar IR looks like:
3352 //
3353 // scalar.ph:
3354 // s_init = a[-1]
3355 // br scalar.body
3356 //
3357 // scalar.body:
3358 // i = phi [0, scalar.ph], [i+1, scalar.body]
3359 // s1 = phi [s_init, scalar.ph], [s2, scalar.body]
3360 // s2 = a[i]
3361 // b[i] = s2 - s1
3362 // br cond, scalar.body, ...
3363 //
3364 // In this example, s1 is a recurrence because it's value depends on the
3365 // previous iteration. In the first phase of vectorization, we created a
3366 // temporary value for s1. We now complete the vectorization and produce the
3367 // shorthand vector IR shown below (for VF = 4, UF = 1).
3368 //
3369 // vector.ph:
3370 // v_init = vector(..., ..., ..., a[-1])
3371 // br vector.body
3372 //
3373 // vector.body
3374 // i = phi [0, vector.ph], [i+4, vector.body]
3375 // v1 = phi [v_init, vector.ph], [v2, vector.body]
3376 // v2 = a[i, i+1, i+2, i+3];
3377 // v3 = vector(v1(3), v2(0, 1, 2))
3378 // b[i, i+1, i+2, i+3] = v2 - v3
3379 // br cond, vector.body, middle.block
3380 //
3381 // middle.block:
3382 // x = v2(3)
3383 // br scalar.ph
3384 //
3385 // scalar.ph:
3386 // s_init = phi [x, middle.block], [a[-1], otherwise]
3387 // br scalar.body
3388 //
3389 // After execution completes the vector loop, we extract the next value of
3390 // the recurrence (x) to use as the initial value in the scalar loop.
3391
3392 // Get the original loop preheader and single loop latch.
3393 auto *Preheader = OrigLoop->getLoopPreheader();
3394 auto *Latch = OrigLoop->getLoopLatch();
3395
3396 // Get the initial and previous values of the scalar recurrence.
3397 auto *ScalarInit = Phi->getIncomingValueForBlock(Preheader);
3398 auto *Previous = Phi->getIncomingValueForBlock(Latch);
3399
3400 // Create a vector from the initial value.
3401 auto *VectorInit = ScalarInit;
3402 if (VF > 1) {
3403 Builder.SetInsertPoint(LoopVectorPreHeader->getTerminator());
3404 VectorInit = Builder.CreateInsertElement(
3405 UndefValue::get(VectorType::get(VectorInit->getType(), VF)), VectorInit,
3406 Builder.getInt32(VF - 1), "vector.recur.init");
3407 }
3408
3409 // We constructed a temporary phi node in the first phase of vectorization.
3410 // This phi node will eventually be deleted.
3411 Builder.SetInsertPoint(
3412 cast<Instruction>(VectorLoopValueMap.getVectorValue(Phi, 0)));
3413
3414 // Create a phi node for the new recurrence. The current value will either be
3415 // the initial value inserted into a vector or loop-varying vector value.
3416 auto *VecPhi = Builder.CreatePHI(VectorInit->getType(), 2, "vector.recur");
3417 VecPhi->addIncoming(VectorInit, LoopVectorPreHeader);
3418
3419 // Get the vectorized previous value of the last part UF - 1. It appears last
3420 // among all unrolled iterations, due to the order of their construction.
3421 Value *PreviousLastPart = getOrCreateVectorValue(Previous, UF - 1);
3422
3423 // Set the insertion point after the previous value if it is an instruction.
3424 // Note that the previous value may have been constant-folded so it is not
3425 // guaranteed to be an instruction in the vector loop. Also, if the previous
3426 // value is a phi node, we should insert after all the phi nodes to avoid
3427 // breaking basic block verification.
3428 if (LI->getLoopFor(LoopVectorBody)->isLoopInvariant(PreviousLastPart) ||
3429 isa<PHINode>(PreviousLastPart))
3430 Builder.SetInsertPoint(&*LoopVectorBody->getFirstInsertionPt());
3431 else
3432 Builder.SetInsertPoint(
3433 &*++BasicBlock::iterator(cast<Instruction>(PreviousLastPart)));
3434
3435 // We will construct a vector for the recurrence by combining the values for
3436 // the current and previous iterations. This is the required shuffle mask.
3437 SmallVector<Constant *, 8> ShuffleMask(VF);
3438 ShuffleMask[0] = Builder.getInt32(VF - 1);
3439 for (unsigned I = 1; I < VF; ++I)
3440 ShuffleMask[I] = Builder.getInt32(I + VF - 1);
3441
3442 // The vector from which to take the initial value for the current iteration
3443 // (actual or unrolled). Initially, this is the vector phi node.
3444 Value *Incoming = VecPhi;
3445
3446 // Shuffle the current and previous vector and update the vector parts.
3447 for (unsigned Part = 0; Part < UF; ++Part) {
3448 Value *PreviousPart = getOrCreateVectorValue(Previous, Part);
3449 Value *PhiPart = VectorLoopValueMap.getVectorValue(Phi, Part);
3450 auto *Shuffle =
3451 VF > 1 ? Builder.CreateShuffleVector(Incoming, PreviousPart,
3452 ConstantVector::get(ShuffleMask))
3453 : Incoming;
3454 PhiPart->replaceAllUsesWith(Shuffle);
3455 cast<Instruction>(PhiPart)->eraseFromParent();
3456 VectorLoopValueMap.resetVectorValue(Phi, Part, Shuffle);
3457 Incoming = PreviousPart;
3458 }
3459
3460 // Fix the latch value of the new recurrence in the vector loop.
3461 VecPhi->addIncoming(Incoming, LI->getLoopFor(LoopVectorBody)->getLoopLatch());
3462
3463 // Extract the last vector element in the middle block. This will be the
3464 // initial value for the recurrence when jumping to the scalar loop.
3465 auto *ExtractForScalar = Incoming;
3466 if (VF > 1) {
3467 Builder.SetInsertPoint(LoopMiddleBlock->getTerminator());
3468 ExtractForScalar = Builder.CreateExtractElement(
3469 ExtractForScalar, Builder.getInt32(VF - 1), "vector.recur.extract");
3470 }
3471 // Extract the second last element in the middle block if the
3472 // Phi is used outside the loop. We need to extract the phi itself
3473 // and not the last element (the phi update in the current iteration). This
3474 // will be the value when jumping to the exit block from the LoopMiddleBlock,
3475 // when the scalar loop is not run at all.
3476 Value *ExtractForPhiUsedOutsideLoop = nullptr;
3477 if (VF > 1)
3478 ExtractForPhiUsedOutsideLoop = Builder.CreateExtractElement(
3479 Incoming, Builder.getInt32(VF - 2), "vector.recur.extract.for.phi");
3480 // When loop is unrolled without vectorizing, initialize
3481 // ExtractForPhiUsedOutsideLoop with the value just prior to unrolled value of
3482 // `Incoming`. This is analogous to the vectorized case above: extracting the
3483 // second last element when VF > 1.
3484 else if (UF > 1)
3485 ExtractForPhiUsedOutsideLoop = getOrCreateVectorValue(Previous, UF - 2);
3486
3487 // Fix the initial value of the original recurrence in the scalar loop.
3488 Builder.SetInsertPoint(&*LoopScalarPreHeader->begin());
3489 auto *Start = Builder.CreatePHI(Phi->getType(), 2, "scalar.recur.init");
3490 for (auto *BB : predecessors(LoopScalarPreHeader)) {
3491 auto *Incoming = BB == LoopMiddleBlock ? ExtractForScalar : ScalarInit;
3492 Start->addIncoming(Incoming, BB);
3493 }
3494
3495 Phi->setIncomingValue(Phi->getBasicBlockIndex(LoopScalarPreHeader), Start);
3496 Phi->setName("scalar.recur");
3497
3498 // Finally, fix users of the recurrence outside the loop. The users will need
3499 // either the last value of the scalar recurrence or the last value of the
3500 // vector recurrence we extracted in the middle block. Since the loop is in
3501 // LCSSA form, we just need to find all the phi nodes for the original scalar
3502 // recurrence in the exit block, and then add an edge for the middle block.
3503 for (PHINode &LCSSAPhi : LoopExitBlock->phis()) {
3504 if (LCSSAPhi.getIncomingValue(0) == Phi) {
3505 LCSSAPhi.addIncoming(ExtractForPhiUsedOutsideLoop, LoopMiddleBlock);
3506 }
3507 }
3508}
3509
3510void InnerLoopVectorizer::fixReduction(PHINode *Phi) {
3511 Constant *Zero = Builder.getInt32(0);
3512
3513 // Get it's reduction variable descriptor.
3514 assert(Legal->isReductionVariable(Phi) &&(static_cast <bool> (Legal->isReductionVariable(Phi)
&& "Unable to find the reduction variable") ? void (
0) : __assert_fail ("Legal->isReductionVariable(Phi) && \"Unable to find the reduction variable\""
, "/home/username/llvm-project/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp"
, 3515, __extension__ __PRETTY_FUNCTION__))
3515 "Unable to find the reduction variable")(static_cast <bool> (Legal->isReductionVariable(Phi)
&& "Unable to find the reduction variable") ? void (
0) : __assert_fail ("Legal->isReductionVariable(Phi) && \"Unable to find the reduction variable\""
, "/home/username/llvm-project/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp"
, 3515, __extension__ __PRETTY_FUNCTION__))
;
3516 RecurrenceDescriptor RdxDesc = (*Legal->getReductionVars())[Phi];
3517
3518 RecurrenceDescriptor::RecurrenceKind RK = RdxDesc.getRecurrenceKind();
3519 TrackingVH<Value> ReductionStartValue = RdxDesc.getRecurrenceStartValue();
3520 Instruction *LoopExitInst = RdxDesc.getLoopExitInstr();
3521 RecurrenceDescriptor::MinMaxRecurrenceKind MinMaxKind =
3522 RdxDesc.getMinMaxRecurrenceKind();
3523 setDebugLocFromInst(Builder, ReductionStartValue);
3524
3525 // We need to generate a reduction vector from the incoming scalar.
3526 // To do so, we need to generate the 'identity' vector and override
3527 // one of the elements with the incoming scalar reduction. We need
3528 // to do it in the vector-loop preheader.
3529 Builder.SetInsertPoint(LoopVectorPreHeader->getTerminator());
3530
3531 // This is the vector-clone of the value that leaves the loop.
3532 Type *VecTy = getOrCreateVectorValue(LoopExitInst, 0)->getType();
3533
3534 // Find the reduction identity variable. Zero for addition, or, xor,
3535 // one for multiplication, -1 for And.
3536 Value *Identity;
3537 Value *VectorStart;
3538 if (RK == RecurrenceDescriptor::RK_IntegerMinMax ||
3539 RK == RecurrenceDescriptor::RK_FloatMinMax) {
3540 // MinMax reduction have the start value as their identify.
3541 if (VF == 1) {
3542 VectorStart = Identity = ReductionStartValue;
3543 } else {
3544 VectorStart = Identity =
3545 Builder.CreateVectorSplat(VF, ReductionStartValue, "minmax.ident");
3546 }
3547 } else {
3548 // Handle other reduction kinds:
3549 Constant *Iden = RecurrenceDescriptor::getRecurrenceIdentity(
3550 RK, VecTy->getScalarType());
3551 if (VF == 1) {
3552 Identity = Iden;
3553 // This vector is the Identity vector where the first element is the
3554 // incoming scalar reduction.
3555 VectorStart = ReductionStartValue;
3556 } else {
3557 Identity = ConstantVector::getSplat(VF, Iden);
3558
3559 // This vector is the Identity vector where the first element is the
3560 // incoming scalar reduction.
3561 VectorStart =
3562 Builder.CreateInsertElement(Identity, ReductionStartValue, Zero);
3563 }
3564 }
3565
3566 // Fix the vector-loop phi.
3567
3568 // Reductions do not have to start at zero. They can start with
3569 // any loop invariant values.
3570 BasicBlock *Latch = OrigLoop->getLoopLatch();
3571 Value *LoopVal = Phi->getIncomingValueForBlock(Latch);
3572 for (unsigned Part = 0; Part < UF; ++Part) {
3573 Value *VecRdxPhi = getOrCreateVectorValue(Phi, Part);
3574 Value *Val = getOrCreateVectorValue(LoopVal, Part);
3575 // Make sure to add the reduction stat value only to the
3576 // first unroll part.
3577 Value *StartVal = (Part == 0) ? VectorStart : Identity;
3578 cast<PHINode>(VecRdxPhi)->addIncoming(StartVal, LoopVectorPreHeader);
3579 cast<PHINode>(VecRdxPhi)
3580 ->addIncoming(Val, LI->getLoopFor(LoopVectorBody)->getLoopLatch());
3581 }
3582
3583 // Before each round, move the insertion point right between
3584 // the PHIs and the values we are going to write.
3585 // This allows us to write both PHINodes and the extractelement
3586 // instructions.
3587 Builder.SetInsertPoint(&*LoopMiddleBlock->getFirstInsertionPt());
3588
3589 setDebugLocFromInst(Builder, LoopExitInst);
3590
3591 // If the vector reduction can be performed in a smaller type, we truncate
3592 // then extend the loop exit value to enable InstCombine to evaluate the
3593 // entire expression in the smaller type.
3594 if (VF > 1 && Phi->getType() != RdxDesc.getRecurrenceType()) {
3595 Type *RdxVecTy = VectorType::get(RdxDesc.getRecurrenceType(), VF);
3596 Builder.SetInsertPoint(
3597 LI->getLoopFor(LoopVectorBody)->getLoopLatch()->getTerminator());
3598 VectorParts RdxParts(UF);
3599 for (unsigned Part = 0; Part < UF; ++Part) {
3600 RdxParts[Part] = VectorLoopValueMap.getVectorValue(LoopExitInst, Part);
3601 Value *Trunc = Builder.CreateTrunc(RdxParts[Part], RdxVecTy);
3602 Value *Extnd = RdxDesc.isSigned() ? Builder.CreateSExt(Trunc, VecTy)
3603 : Builder.CreateZExt(Trunc, VecTy);
3604 for (Value::user_iterator UI = RdxParts[Part]->user_begin();
3605 UI != RdxParts[Part]->user_end();)
3606 if (*UI != Trunc) {
3607 (*UI++)->replaceUsesOfWith(RdxParts[Part], Extnd);
3608 RdxParts[Part] = Extnd;
3609 } else {
3610 ++UI;
3611 }
3612 }
3613 Builder.SetInsertPoint(&*LoopMiddleBlock->getFirstInsertionPt());
3614 for (unsigned Part = 0; Part < UF; ++Part) {
3615 RdxParts[Part] = Builder.CreateTrunc(RdxParts[Part], RdxVecTy);
3616 VectorLoopValueMap.resetVectorValue(LoopExitInst, Part, RdxParts[Part]);
3617 }
3618 }
3619
3620 // Reduce all of the unrolled parts into a single vector.
3621 Value *ReducedPartRdx = VectorLoopValueMap.getVectorValue(LoopExitInst, 0);
3622 unsigned Op = RecurrenceDescriptor::getRecurrenceBinOp(RK);
3623 setDebugLocFromInst(Builder, ReducedPartRdx);
3624 for (unsigned Part = 1; Part < UF; ++Part) {
3625 Value *RdxPart = VectorLoopValueMap.getVectorValue(LoopExitInst, Part);
3626 if (Op != Instruction::ICmp && Op != Instruction::FCmp)
3627 // Floating point operations had to be 'fast' to enable the reduction.
3628 ReducedPartRdx = addFastMathFlag(
3629 Builder.CreateBinOp((Instruction::BinaryOps)Op, RdxPart,
3630 ReducedPartRdx, "bin.rdx"),
3631 RdxDesc.getFastMathFlags());
3632 else
3633 ReducedPartRdx = createMinMaxOp(Builder, MinMaxKind, ReducedPartRdx,
3634 RdxPart);
3635 }
3636
3637 if (VF > 1) {
3638 bool NoNaN = Legal->hasFunNoNaNAttr();
3639 ReducedPartRdx =
3640 createTargetReduction(Builder, TTI, RdxDesc, ReducedPartRdx, NoNaN);
3641 // If the reduction can be performed in a smaller type, we need to extend
3642 // the reduction to the wider type before we branch to the original loop.
3643 if (Phi->getType() != RdxDesc.getRecurrenceType())
3644 ReducedPartRdx =
3645 RdxDesc.isSigned()
3646 ? Builder.CreateSExt(ReducedPartRdx, Phi->getType())
3647 : Builder.CreateZExt(ReducedPartRdx, Phi->getType());
3648 }
3649
3650 // Create a phi node that merges control-flow from the backedge-taken check
3651 // block and the middle block.
3652 PHINode *BCBlockPhi = PHINode::Create(Phi->getType(), 2, "bc.merge.rdx",
3653 LoopScalarPreHeader->getTerminator());
3654 for (unsigned I = 0, E = LoopBypassBlocks.size(); I != E; ++I)
3655 BCBlockPhi->addIncoming(ReductionStartValue, LoopBypassBlocks[I]);
3656 BCBlockPhi->addIncoming(ReducedPartRdx, LoopMiddleBlock);
3657
3658 // Now, we need to fix the users of the reduction variable
3659 // inside and outside of the scalar remainder loop.
3660 // We know that the loop is in LCSSA form. We need to update the
3661 // PHI nodes in the exit blocks.
3662 for (PHINode &LCSSAPhi : LoopExitBlock->phis()) {
3663 // All PHINodes need to have a single entry edge, or two if
3664 // we already fixed them.
3665 assert(LCSSAPhi.getNumIncomingValues() < 3 && "Invalid LCSSA PHI")(static_cast <bool> (LCSSAPhi.getNumIncomingValues() <
3 && "Invalid LCSSA PHI") ? void (0) : __assert_fail
("LCSSAPhi.getNumIncomingValues() < 3 && \"Invalid LCSSA PHI\""
, "/home/username/llvm-project/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp"
, 3665, __extension__ __PRETTY_FUNCTION__))
;
3666
3667 // We found a reduction value exit-PHI. Update it with the
3668 // incoming bypass edge.
3669 if (LCSSAPhi.getIncomingValue(0) == LoopExitInst)
3670 LCSSAPhi.addIncoming(ReducedPartRdx, LoopMiddleBlock);
3671 } // end of the LCSSA phi scan.
3672
3673 // Fix the scalar loop reduction variable with the incoming reduction sum
3674 // from the vector body and from the backedge value.
3675 int IncomingEdgeBlockIdx =
3676 Phi->getBasicBlockIndex(OrigLoop->getLoopLatch());
3677 assert(IncomingEdgeBlockIdx >= 0 && "Invalid block index")(static_cast <bool> (IncomingEdgeBlockIdx >= 0 &&
"Invalid block index") ? void (0) : __assert_fail ("IncomingEdgeBlockIdx >= 0 && \"Invalid block index\""
, "/home/username/llvm-project/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp"
, 3677, __extension__ __PRETTY_FUNCTION__))
;
3678 // Pick the other block.
3679 int SelfEdgeBlockIdx = (IncomingEdgeBlockIdx ? 0 : 1);
3680 Phi->setIncomingValue(SelfEdgeBlockIdx, BCBlockPhi);
3681 Phi->setIncomingValue(IncomingEdgeBlockIdx, LoopExitInst);
3682}
3683
3684void InnerLoopVectorizer::fixLCSSAPHIs() {
3685 for (PHINode &LCSSAPhi : LoopExitBlock->phis()) {
3686 if (LCSSAPhi.getNumIncomingValues() == 1) {
3687 auto *IncomingValue = LCSSAPhi.getIncomingValue(0);
3688 // Non-instruction incoming values will have only one value.
3689 unsigned LastLane = 0;
3690 if (isa<Instruction>(IncomingValue))
3691 LastLane = Cost->isUniformAfterVectorization(
3692 cast<Instruction>(IncomingValue), VF)
3693 ? 0
3694 : VF - 1;
3695 // Can be a loop invariant incoming value or the last scalar value to be
3696 // extracted from the vectorized loop.
3697 Builder.SetInsertPoint(LoopMiddleBlock->getTerminator());
3698 Value *lastIncomingValue =
3699 getOrCreateScalarValue(IncomingValue, { UF - 1, LastLane });
3700 LCSSAPhi.addIncoming(lastIncomingValue, LoopMiddleBlock);
3701 }
3702 }
3703}
3704
3705void InnerLoopVectorizer::sinkScalarOperands(Instruction *PredInst) {
3706 // The basic block and loop containing the predicated instruction.
3707 auto *PredBB = PredInst->getParent();
3708 auto *VectorLoop = LI->getLoopFor(PredBB);
3709
3710 // Initialize a worklist with the operands of the predicated instruction.
3711 SetVector<Value *> Worklist(PredInst->op_begin(), PredInst->op_end());
3712
3713 // Holds instructions that we need to analyze again. An instruction may be
3714 // reanalyzed if we don't yet know if we can sink it or not.
3715 SmallVector<Instruction *, 8> InstsToReanalyze;
3716
3717 // Returns true if a given use occurs in the predicated block. Phi nodes use
3718 // their operands in their corresponding predecessor blocks.
3719 auto isBlockOfUsePredicated = [&](Use &U) -> bool {
3720 auto *I = cast<Instruction>(U.getUser());
3721 BasicBlock *BB = I->getParent();
3722 if (auto *Phi = dyn_cast<PHINode>(I))
3723 BB = Phi->getIncomingBlock(
3724 PHINode::getIncomingValueNumForOperand(U.getOperandNo()));
3725 return BB == PredBB;
3726 };
3727
3728 // Iteratively sink the scalarized operands of the predicated instruction
3729 // into the block we created for it. When an instruction is sunk, it's
3730 // operands are then added to the worklist. The algorithm ends after one pass
3731 // through the worklist doesn't sink a single instruction.
3732 bool Changed;
3733 do {
3734 // Add the instructions that need to be reanalyzed to the worklist, and
3735 // reset the changed indicator.
3736 Worklist.insert(InstsToReanalyze.begin(), InstsToReanalyze.end());
3737 InstsToReanalyze.clear();
3738 Changed = false;
3739
3740 while (!Worklist.empty()) {
3741 auto *I = dyn_cast<Instruction>(Worklist.pop_back_val());
3742
3743 // We can't sink an instruction if it is a phi node, is already in the
3744 // predicated block, is not in the loop, or may have side effects.
3745 if (!I || isa<PHINode>(I) || I->getParent() == PredBB ||
3746 !VectorLoop->contains(I) || I->mayHaveSideEffects())
3747 continue;
3748
3749 // It's legal to sink the instruction if all its uses occur in the
3750 // predicated block. Otherwise, there's nothing to do yet, and we may
3751 // need to reanalyze the instruction.
3752 if (!llvm::all_of(I->uses(), isBlockOfUsePredicated)) {
3753 InstsToReanalyze.push_back(I);
3754 continue;
3755 }
3756
3757 // Move the instruction to the beginning of the predicated block, and add
3758 // it's operands to the worklist.
3759 I->moveBefore(&*PredBB->getFirstInsertionPt());
3760 Worklist.insert(I->op_begin(), I->op_end());
3761
3762 // The sinking may have enabled other instructions to be sunk, so we will
3763 // need to iterate.
3764 Changed = true;
3765 }
3766 } while (Changed);
3767}
3768
3769void InnerLoopVectorizer::fixNonInductionPHIs() {
3770 for (PHINode *OrigPhi : OrigPHIsToFix) {
3771 PHINode *NewPhi =
3772 cast<PHINode>(VectorLoopValueMap.getVectorValue(OrigPhi, 0));
3773 unsigned NumIncomingValues = OrigPhi->getNumIncomingValues();
3774
3775 SmallVector<BasicBlock *, 2> ScalarBBPredecessors(
3776 predecessors(OrigPhi->getParent()));
3777 SmallVector<BasicBlock *, 2> VectorBBPredecessors(
3778 predecessors(NewPhi->getParent()));
3779 assert(ScalarBBPredecessors.size() == VectorBBPredecessors.size() &&(static_cast <bool> (ScalarBBPredecessors.size() == VectorBBPredecessors
.size() && "Scalar and Vector BB should have the same number of predecessors"
) ? void (0) : __assert_fail ("ScalarBBPredecessors.size() == VectorBBPredecessors.size() && \"Scalar and Vector BB should have the same number of predecessors\""
, "/home/username/llvm-project/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp"
, 3780, __extension__ __PRETTY_FUNCTION__))
3780 "Scalar and Vector BB should have the same number of predecessors")(static_cast <bool> (ScalarBBPredecessors.size() == VectorBBPredecessors
.size() && "Scalar and Vector BB should have the same number of predecessors"
) ? void (0) : __assert_fail ("ScalarBBPredecessors.size() == VectorBBPredecessors.size() && \"Scalar and Vector BB should have the same number of predecessors\""
, "/home/username/llvm-project/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp"
, 3780, __extension__ __PRETTY_FUNCTION__))
;
3781
3782 // The insertion point in Builder may be invalidated by the time we get
3783 // here. Force the Builder insertion point to something valid so that we do
3784 // not run into issues during insertion point restore in
3785 // getOrCreateVectorValue calls below.
3786 Builder.SetInsertPoint(NewPhi);
3787
3788 // The predecessor order is preserved and we can rely on mapping between
3789 // scalar and vector block predecessors.
3790 for (unsigned i = 0; i < NumIncomingValues; ++i) {
3791 BasicBlock *NewPredBB = VectorBBPredecessors[i];
3792
3793 // When looking up the new scalar/vector values to fix up, use incoming
3794 // values from original phi.
3795 Value *ScIncV =
3796 OrigPhi->getIncomingValueForBlock(ScalarBBPredecessors[i]);
3797
3798 // Scalar incoming value may need a broadcast
3799 Value *NewIncV = getOrCreateVectorValue(ScIncV, 0);
3800 NewPhi->addIncoming(NewIncV, NewPredBB);
3801 }
3802 }
3803}
3804
3805void InnerLoopVectorizer::widenPHIInstruction(Instruction *PN, unsigned UF,
3806 unsigned VF) {
3807 PHINode *P = cast<PHINode>(PN);
3808 if (EnableVPlanNativePath) {
3809 // Currently we enter here in the VPlan-native path for non-induction
3810 // PHIs where all control flow is uniform. We simply widen these PHIs.
3811 // Create a vector phi with no operands - the vector phi operands will be
3812 // set at the end of vector code generation.
3813 Type *VecTy =
3814 (VF == 1) ? PN->getType() : VectorType::get(PN->getType(), VF);
3815 Value *VecPhi = Builder.CreatePHI(VecTy, PN->getNumOperands(), "vec.phi");
3816 VectorLoopValueMap.setVectorValue(P, 0, VecPhi);
3817 OrigPHIsToFix.push_back(P);
3818
3819 return;
3820 }
3821
3822 assert(PN->getParent() == OrigLoop->getHeader() &&(static_cast <bool> (PN->getParent() == OrigLoop->
getHeader() && "Non-header phis should have been handled elsewhere"
) ? void (0) : __assert_fail ("PN->getParent() == OrigLoop->getHeader() && \"Non-header phis should have been handled elsewhere\""
, "/home/username/llvm-project/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp"
, 3823, __extension__ __PRETTY_FUNCTION__))
3823 "Non-header phis should have been handled elsewhere")(static_cast <bool> (PN->getParent() == OrigLoop->
getHeader() && "Non-header phis should have been handled elsewhere"
) ? void (0) : __assert_fail ("PN->getParent() == OrigLoop->getHeader() && \"Non-header phis should have been handled elsewhere\""
, "/home/username/llvm-project/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp"
, 3823, __extension__ __PRETTY_FUNCTION__))
;
3824
3825 // In order to support recurrences we need to be able to vectorize Phi nodes.
3826 // Phi nodes have cycles, so we need to vectorize them in two stages. This is
3827 // stage #1: We create a new vector PHI node with no incoming edges. We'll use
3828 // this value when we vectorize all of the instructions that use the PHI.
3829 if (Legal->isReductionVariable(P) || Legal->isFirstOrderRecurrence(P)) {
3830 for (unsigned Part = 0; Part < UF; ++Part) {
3831 // This is phase one of vectorizing PHIs.
3832 Type *VecTy =
3833 (VF == 1) ? PN->getType() : VectorType::get(PN->getType(), VF);
3834 Value *EntryPart = PHINode::Create(
3835 VecTy, 2, "vec.phi", &*LoopVectorBody->getFirstInsertionPt());
3836 VectorLoopValueMap.setVectorValue(P, Part, EntryPart);
3837 }
3838 return;
3839 }
3840
3841 setDebugLocFromInst(Builder, P);
3842
3843 // This PHINode must be an induction variable.
3844 // Make sure that we know about it.
3845 assert(Legal->getInductionVars()->count(P) && "Not an induction variable")(static_cast <bool> (Legal->getInductionVars()->count
(P) && "Not an induction variable") ? void (0) : __assert_fail
("Legal->getInductionVars()->count(P) && \"Not an induction variable\""
, "/home/username/llvm-project/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp"
, 3845, __extension__ __PRETTY_FUNCTION__))
;
3846
3847 InductionDescriptor II = Legal->getInductionVars()->lookup(P);
3848 const DataLayout &DL = OrigLoop->getHeader()->getModule()->getDataLayout();
3849
3850 // FIXME: The newly created binary instructions should contain nsw/nuw flags,
3851 // which can be found from the original scalar operations.
3852 switch (II.getKind()) {
3853 case InductionDescriptor::IK_NoInduction:
3854 llvm_unreachable("Unknown induction")::llvm::llvm_unreachable_internal("Unknown induction", "/home/username/llvm-project/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp"
, 3854)
;
3855 case InductionDescriptor::IK_IntInduction:
3856 case InductionDescriptor::IK_FpInduction:
3857 llvm_unreachable("Integer/fp induction is handled elsewhere.")::llvm::llvm_unreachable_internal("Integer/fp induction is handled elsewhere."
, "/home/username/llvm-project/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp"
, 3857)
;
3858 case InductionDescriptor::IK_PtrInduction: {
3859 // Handle the pointer induction variable case.
3860 assert(P->getType()->isPointerTy() && "Unexpected type.")(static_cast <bool> (P->getType()->isPointerTy() &&
"Unexpected type.") ? void (0) : __assert_fail ("P->getType()->isPointerTy() && \"Unexpected type.\""
, "/home/username/llvm-project/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp"
, 3860, __extension__ __PRETTY_FUNCTION__))
;
3861 // This is the normalized GEP that starts counting at zero.
3862 Value *PtrInd = Induction;
3863 PtrInd = Builder.CreateSExtOrTrunc(PtrInd, II.getStep()->getType());
3864 // Determine the number of scalars we need to generate for each unroll
3865 // iteration. If the instruction is uniform, we only need to generate the
3866 // first lane. Otherwise, we generate all VF values.
3867 unsigned Lanes = Cost->isUniformAfterVectorization(P, VF) ? 1 : VF;
3868 // These are the scalar results. Notice that we don't generate vector GEPs
3869 // because scalar GEPs result in better code.
3870 for (unsigned Part = 0; Part < UF; ++Part) {
3871 for (unsigned Lane = 0; Lane < Lanes; ++Lane) {
3872 Constant *Idx = ConstantInt::get(PtrInd->getType(), Lane + Part * VF);
3873 Value *GlobalIdx = Builder.CreateAdd(PtrInd, Idx);
3874 Value *SclrGep =
3875 emitTransformedIndex(Builder, GlobalIdx, PSE.getSE(), DL, II);
3876 SclrGep->setName("next.gep");
3877 VectorLoopValueMap.setScalarValue(P, {Part, Lane}, SclrGep);
3878 }
3879 }
3880 return;
3881 }
3882 }
3883}
3884
3885/// A helper function for checking whether an integer division-related
3886/// instruction may divide by zero (in which case it must be predicated if
3887/// executed conditionally in the scalar code).
3888/// TODO: It may be worthwhile to generalize and check isKnownNonZero().
3889/// Non-zero divisors that are non compile-time constants will not be
3890/// converted into multiplication, so we will still end up scalarizing
3891/// the division, but can do so w/o predication.
3892static bool mayDivideByZero(Instruction &I) {
3893 assert((I.getOpcode() == Instruction::UDiv ||(static_cast <bool> ((I.getOpcode() == Instruction::UDiv
|| I.getOpcode() == Instruction::SDiv || I.getOpcode() == Instruction
::URem || I.getOpcode() == Instruction::SRem) && "Unexpected instruction"
) ? void (0) : __assert_fail ("(I.getOpcode() == Instruction::UDiv || I.getOpcode() == Instruction::SDiv || I.getOpcode() == Instruction::URem || I.getOpcode() == Instruction::SRem) && \"Unexpected instruction\""
, "/home/username/llvm-project/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp"
, 3897, __extension__ __PRETTY_FUNCTION__))
3894 I.getOpcode() == Instruction::SDiv ||(static_cast <bool> ((I.getOpcode() == Instruction::UDiv
|| I.getOpcode() == Instruction::SDiv || I.getOpcode() == Instruction
::URem || I.getOpcode() == Instruction::SRem) && "Unexpected instruction"
) ? void (0) : __assert_fail ("(I.getOpcode() == Instruction::UDiv || I.getOpcode() == Instruction::SDiv || I.getOpcode() == Instruction::URem || I.getOpcode() == Instruction::SRem) && \"Unexpected instruction\""
, "/home/username/llvm-project/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp"
, 3897, __extension__ __PRETTY_FUNCTION__))
3895 I.getOpcode() == Instruction::URem ||(static_cast <bool> ((I.getOpcode() == Instruction::UDiv
|| I.getOpcode() == Instruction::SDiv || I.getOpcode() == Instruction
::URem || I.getOpcode() == Instruction::SRem) && "Unexpected instruction"
) ? void (0) : __assert_fail ("(I.getOpcode() == Instruction::UDiv || I.getOpcode() == Instruction::SDiv || I.getOpcode() == Instruction::URem || I.getOpcode() == Instruction::SRem) && \"Unexpected instruction\""
, "/home/username/llvm-project/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp"
, 3897, __extension__ __PRETTY_FUNCTION__))
3896 I.getOpcode() == Instruction::SRem) &&(static_cast <bool> ((I.getOpcode() == Instruction::UDiv
|| I.getOpcode() == Instruction::SDiv || I.getOpcode() == Instruction
::URem || I.getOpcode() == Instruction::SRem) && "Unexpected instruction"
) ? void (0) : __assert_fail ("(I.getOpcode() == Instruction::UDiv || I.getOpcode() == Instruction::SDiv || I.getOpcode() == Instruction::URem || I.getOpcode() == Instruction::SRem) && \"Unexpected instruction\""
, "/home/username/llvm-project/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp"
, 3897, __extension__ __PRETTY_FUNCTION__))
3897 "Unexpected instruction")(static_cast <bool> ((I.getOpcode() == Instruction::UDiv
|| I.getOpcode() == Instruction::SDiv || I.getOpcode() == Instruction
::URem || I.getOpcode() == Instruction::SRem) && "Unexpected instruction"
) ? void (0) : __assert_fail ("(I.getOpcode() == Instruction::UDiv || I.getOpcode() == Instruction::SDiv || I.getOpcode() == Instruction::URem || I.getOpcode() == Instruction::SRem) && \"Unexpected instruction\""
, "/home/username/llvm-project/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp"
, 3897, __extension__ __PRETTY_FUNCTION__))
;
3898 Value *Divisor = I.getOperand(1);
3899 auto *CInt = dyn_cast<ConstantInt>(Divisor);
3900 return !CInt || CInt->isZero();
3901}
3902
3903void InnerLoopVectorizer::widenInstruction(Instruction &I) {
3904 switch (I.getOpcode()) {
3905 case Instruction::Br:
3906 case Instruction::PHI:
3907 llvm_unreachable("This instruction is handled by a different recipe.")::llvm::llvm_unreachable_internal("This instruction is handled by a different recipe."
, "/home/username/llvm-project/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp"
, 3907)
;
3908 case Instruction::GetElementPtr: {
3909 // Construct a vector GEP by widening the operands of the scalar GEP as
3910 // necessary. We mark the vector GEP 'inbounds' if appropriate. A GEP
3911 // results in a vector of pointers when at least one operand of the GEP
3912 // is vector-typed. Thus, to keep the representation compact, we only use
3913 // vector-typed operands for loop-varying values.
3914 auto *GEP = cast<GetElementPtrInst>(&I);
3915
3916 if (VF > 1 && OrigLoop->hasLoopInvariantOperands(GEP)) {
3917 // If we are vectorizing, but the GEP has only loop-invariant operands,
3918 // the GEP we build (by only using vector-typed operands for
3919 // loop-varying values) would be a scalar pointer. Thus, to ensure we
3920 // produce a vector of pointers, we need to either arbitrarily pick an
3921 // operand to broadcast, or broadcast a clone of the original GEP.
3922 // Here, we broadcast a clone of the original.
3923 //
3924 // TODO: If at some point we decide to scalarize instructions having
3925 // loop-invariant operands, this special case will no longer be
3926 // required. We would add the scalarization decision to
3927 // collectLoopScalars() and teach getVectorValue() to broadcast
3928 // the lane-zero scalar value.
3929 auto *Clone = Builder.Insert(GEP->clone());
3930 for (unsigned Part = 0; Part < UF; ++Part) {
3931 Value *EntryPart = Builder.CreateVectorSplat(VF, Clone);
3932 VectorLoopValueMap.setVectorValue(&I, Part, EntryPart);
3933 addMetadata(EntryPart, GEP);
3934 }
3935 } else {
3936 // If the GEP has at least one loop-varying operand, we are sure to
3937 // produce a vector of pointers. But if we are only unrolling, we want
3938 // to produce a scalar GEP for each unroll part. Thus, the GEP we
3939 // produce with the code below will be scalar (if VF == 1) or vector
3940 // (otherwise). Note that for the unroll-only case, we still maintain
3941 // values in the vector mapping with initVector, as we do for other
3942 // instructions.
3943 for (unsigned Part = 0; Part < UF; ++Part) {
3944 // The pointer operand of the new GEP. If it's loop-invariant, we
3945 // won't broadcast it.
3946 auto *Ptr =
3947 OrigLoop->isLoopInvariant(GEP->getPointerOperand())
3948 ? GEP->getPointerOperand()
3949 : getOrCreateVectorValue(GEP->getPointerOperand(), Part);
3950
3951 // Collect all the indices for the new GEP. If any index is
3952 // loop-invariant, we won't broadcast it.
3953 SmallVector<Value *, 4> Indices;
3954 for (auto &U : make_range(GEP->idx_begin(), GEP->idx_end())) {
3955 if (OrigLoop->isLoopInvariant(U.get()))
3956 Indices.push_back(U.get());
3957 else
3958 Indices.push_back(getOrCreateVectorValue(U.get(), Part));
3959 }
3960
3961 // Create the new GEP. Note that this GEP may be a scalar if VF == 1,
3962 // but it should be a vector, otherwise.
3963 auto *NewGEP =
3964 GEP->isInBounds()
3965 ? Builder.CreateInBoundsGEP(GEP->getSourceElementType(), Ptr,
3966 Indices)
3967 : Builder.CreateGEP(GEP->getSourceElementType(), Ptr, Indices);
3968 assert((VF == 1 || NewGEP->getType()->isVectorTy()) &&(static_cast <bool> ((VF == 1 || NewGEP->getType()->
isVectorTy()) && "NewGEP is not a pointer vector") ? void
(0) : __assert_fail ("(VF == 1 || NewGEP->getType()->isVectorTy()) && \"NewGEP is not a pointer vector\""
, "/home/username/llvm-project/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp"
, 3969, __extension__ __PRETTY_FUNCTION__))
3969 "NewGEP is not a pointer vector")(static_cast <bool> ((VF == 1 || NewGEP->getType()->
isVectorTy()) && "NewGEP is not a pointer vector") ? void
(0) : __assert_fail ("(VF == 1 || NewGEP->getType()->isVectorTy()) && \"NewGEP is not a pointer vector\""
, "/home/username/llvm-project/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp"
, 3969, __extension__ __PRETTY_FUNCTION__))
;
3970 VectorLoopValueMap.setVectorValue(&I, Part, NewGEP);
3971 addMetadata(NewGEP, GEP);
3972 }
3973 }
3974
3975 break;
3976 }
3977 case Instruction::UDiv:
3978 case Instruction::SDiv:
3979 case Instruction::SRem:
3980 case Instruction::URem:
3981 case Instruction::Add:
3982 case Instruction::FAdd:
3983 case Instruction::Sub:
3984 case Instruction::FSub:
3985 case Instruction::Mul:
3986 case Instruction::FMul:
3987 case Instruction::FDiv:
3988 case Instruction::FRem:
3989 case Instruction::Shl:
3990 case Instruction::LShr:
3991 case Instruction::AShr:
3992 case Instruction::And:
3993 case Instruction::Or:
3994 case Instruction::Xor: {
3995 // Just widen binops.
3996 auto *BinOp = cast<BinaryOperator>(&I);
3997 setDebugLocFromInst(Builder, BinOp);
3998
3999 for (unsigned Part = 0; Part < UF; ++Part) {
4000 Value *A = getOrCreateVectorValue(BinOp->getOperand(0), Part);
4001 Value *B = getOrCreateVectorValue(BinOp->getOperand(1), Part);
4002 Value *V = Builder.CreateBinOp(BinOp->getOpcode(), A, B);
4003
4004 if (BinaryOperator *VecOp = dyn_cast<BinaryOperator>(V))
4005 VecOp->copyIRFlags(BinOp);
4006
4007 // Use this vector value for all users of the original instruction.
4008 VectorLoopValueMap.setVectorValue(&I, Part, V);
4009 addMetadata(V, BinOp);
4010 }
4011
4012 break;
4013 }
4014 case Instruction::Select: {
4015 // Widen selects.
4016 // If the selector is loop invariant we can create a select
4017 // instruction with a scalar condition. Otherwise, use vector-select.
4018 auto *SE = PSE.getSE();
4019 bool InvariantCond =
4020 SE->isLoopInvariant(PSE.getSCEV(I.getOperand(0)), OrigLoop);
4021 setDebugLocFromInst(Builder, &I);
4022
4023 // The condition can be loop invariant but still defined inside the
4024 // loop. This means that we can't just use the original 'cond' value.
4025 // We have to take the 'vectorized' value and pick the first lane.
4026 // Instcombine will make this a no-op.
4027
4028 auto *ScalarCond = getOrCreateScalarValue(I.getOperand(0), {0, 0});
4029
4030 for (unsigned Part = 0; Part < UF; ++Part) {
4031 Value *Cond = getOrCreateVectorValue(I.getOperand(0), Part);
4032 Value *Op0 = getOrCreateVectorValue(I.getOperand(1), Part);
4033 Value *Op1 = getOrCreateVectorValue(I.getOperand(2), Part);
4034 Value *Sel =
4035 Builder.CreateSelect(InvariantCond ? ScalarCond : Cond, Op0, Op1);
4036 VectorLoopValueMap.setVectorValue(&I, Part, Sel);
4037 addMetadata(Sel, &I);
4038 }
4039
4040 break;
4041 }
4042
4043 case Instruction::ICmp:
4044 case Instruction::FCmp: {
4045 // Widen compares. Generate vector compares.
4046 bool FCmp = (I.getOpcode() == Instruction::FCmp);
4047 auto *Cmp = dyn_cast<CmpInst>(&I);
4048 setDebugLocFromInst(Builder, Cmp);
4049 for (unsigned Part = 0; Part < UF; ++Part) {
4050 Value *A = getOrCreateVectorValue(Cmp->getOperand(0), Part);
4051 Value *B = getOrCreateVectorValue(Cmp->getOperand(1), Part);
4052 Value *C = nullptr;
4053 if (FCmp) {
4054 // Propagate fast math flags.
4055 IRBuilder<>::FastMathFlagGuard FMFG(Builder);
4056 Builder.setFastMathFlags(Cmp->getFastMathFlags());
4057 C = Builder.CreateFCmp(Cmp->getPredicate(), A, B);
4058 } else {
4059 C = Builder.CreateICmp(Cmp->getPredicate(), A, B);
4060 }
4061 VectorLoopValueMap.setVectorValue(&I, Part, C);
4062 addMetadata(C, &I);
4063 }
4064
4065 break;
4066 }
4067
4068 case Instruction::ZExt:
4069 case Instruction::SExt:
4070 case Instruction::FPToUI:
4071 case Instruction::FPToSI:
4072 case Instruction::FPExt:
4073 case Instruction::PtrToInt:
4074 case Instruction::IntToPtr:
4075 case Instruction::SIToFP:
4076 case Instruction::UIToFP:
4077 case Instruction::Trunc:
4078 case Instruction::FPTrunc:
4079 case Instruction::BitCast: {
4080 auto *CI = dyn_cast<CastInst>(&I);
4081 setDebugLocFromInst(Builder, CI);
4082
4083 /// Vectorize casts.
4084 Type *DestTy =
4085 (VF == 1) ? CI->getType() : VectorType::get(CI->getType(), VF);
4086
4087 for (unsigned Part = 0; Part < UF; ++Part) {
4088 Value *A = getOrCreateVectorValue(CI->getOperand(0), Part);
4089 Value *Cast = Builder.CreateCast(CI->getOpcode(), A, DestTy);
4090 VectorLoopValueMap.setVectorValue(&I, Part, Cast);
4091 addMetadata(Cast, &I);
4092 }
4093 break;
4094 }
4095
4096 case Instruction::Call: {
4097 // Ignore dbg intrinsics.
4098 if (isa<DbgInfoIntrinsic>(I))
4099 break;
4100 setDebugLocFromInst(Builder, &I);
4101
4102 Module *M = I.getParent()->getParent()->getParent();
4103 auto *CI = cast<CallInst>(&I);
4104
4105 StringRef FnName = CI->getCalledFunction()->getName();
4106 Function *F = CI->getCalledFunction();
4107 Type *RetTy = ToVectorTy(CI->getType(), VF);
4108 SmallVector<Type *, 4> Tys;
4109 for (Value *ArgOperand : CI->arg_operands())
4110 Tys.push_back(ToVectorTy(ArgOperand->getType(), VF));
4111
4112 Intrinsic::ID ID = getVectorIntrinsicIDForCall(CI, TLI);
4113
4114 // The flag shows whether we use Intrinsic or a usual Call for vectorized
4115 // version of the instruction.
4116 // Is it beneficial to perform intrinsic call compared to lib call?
4117 bool NeedToScalarize;
4118 unsigned CallCost = getVectorCallCost(CI, VF, *TTI, TLI, NeedToScalarize);
4119 bool UseVectorIntrinsic =
4120 ID && getVectorIntrinsicCost(CI, VF, *TTI, TLI) <= CallCost;
4121 assert((UseVectorIntrinsic || !NeedToScalarize) &&(static_cast <bool> ((UseVectorIntrinsic || !NeedToScalarize
) && "Instruction should be scalarized elsewhere.") ?
void (0) : __assert_fail ("(UseVectorIntrinsic || !NeedToScalarize) && \"Instruction should be scalarized elsewhere.\""
, "/home/username/llvm-project/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp"
, 4122, __extension__ __PRETTY_FUNCTION__))
4122 "Instruction should be scalarized elsewhere.")(static_cast <bool> ((UseVectorIntrinsic || !NeedToScalarize
) && "Instruction should be scalarized elsewhere.") ?
void (0) : __assert_fail ("(UseVectorIntrinsic || !NeedToScalarize) && \"Instruction should be scalarized elsewhere.\""
, "/home/username/llvm-project/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp"
, 4122, __extension__ __PRETTY_FUNCTION__))
;
4123
4124 for (unsigned Part = 0; Part < UF; ++Part) {
4125 SmallVector<Value *, 4> Args;
4126 for (unsigned i = 0, ie = CI->getNumArgOperands(); i != ie; ++i) {
4127 Value *Arg = CI->getArgOperand(i);
4128 // Some intrinsics have a scalar argument - don't replace it with a
4129 // vector.
4130 if (!UseVectorIntrinsic || !hasVectorInstrinsicScalarOpd(ID, i))
4131 Arg = getOrCreateVectorValue(CI->getArgOperand(i), Part);
4132 Args.push_back(Arg);
4133 }
4134
4135 Function *VectorF;
4136 if (UseVectorIntrinsic) {
4137 // Use vector version of the intrinsic.
4138 Type *TysForDecl[] = {CI->getType()};
4139 if (VF > 1)
4140 TysForDecl[0] = VectorType::get(CI->getType()->getScalarType(), VF);
4141 VectorF = Intrinsic::getDeclaration(M, ID, TysForDecl);
4142 } else {
4143 // Use vector version of the library call.
4144 StringRef VFnName = TLI->getVectorizedFunction(FnName, VF);
4145 assert(!VFnName.empty() && "Vector function name is empty.")(static_cast <bool> (!VFnName.empty() && "Vector function name is empty."
) ? void (0) : __assert_fail ("!VFnName.empty() && \"Vector function name is empty.\""
, "/home/username/llvm-project/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp"
, 4145, __extension__ __PRETTY_FUNCTION__))
;
4146 VectorF = M->getFunction(VFnName);
4147 if (!VectorF) {
4148 // Generate a declaration
4149 FunctionType *FTy = FunctionType::get(RetTy, Tys, false);
4150 VectorF =
4151 Function::Create(FTy, Function::ExternalLinkage, VFnName, M);
4152 VectorF->copyAttributesFrom(F);
4153 }
4154 }
4155 assert(VectorF && "Can't create vector function.")(static_cast <bool> (VectorF && "Can't create vector function."
) ? void (0) : __assert_fail ("VectorF && \"Can't create vector function.\""
, "/home/username/llvm-project/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp"
, 4155, __extension__ __PRETTY_FUNCTION__))
;
4156
4157 SmallVector<OperandBundleDef, 1> OpBundles;
4158 CI->getOperandBundlesAsDefs(OpBundles);
4159 CallInst *V = Builder.CreateCall(VectorF, Args, OpBundles);
4160
4161 if (isa<FPMathOperator>(V))
4162 V->copyFastMathFlags(CI);
4163
4164 VectorLoopValueMap.setVectorValue(&I, Part, V);
4165 addMetadata(V, &I);
4166 }
4167
4168 break;
4169 }
4170
4171 default:
4172 // This instruction is not vectorized by simple widening.
4173 LLVM_DEBUG(dbgs() << "LV: Found an unhandled instruction: " << I)do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("loop-vectorize")) { dbgs() << "LV: Found an unhandled instruction: "
<< I; } } while (false)
;
4174 llvm_unreachable("Unhandled instruction!")::llvm::llvm_unreachable_internal("Unhandled instruction!", "/home/username/llvm-project/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp"
, 4174)
;
4175 } // end of switch.
4176}
4177
4178void InnerLoopVectorizer::updateAnalysis() {
4179 // Forget the original basic block.
4180 PSE.getSE()->forgetLoop(OrigLoop);
4181
4182 // DT is not kept up-to-date for outer loop vectorization
4183 if (EnableVPlanNativePath)
4184 return;
4185
4186 // Update the dominator tree information.
4187 assert(DT->properlyDominates(LoopBypassBlocks.front(), LoopExitBlock) &&(static_cast <bool> (DT->properlyDominates(LoopBypassBlocks
.front(), LoopExitBlock) && "Entry does not dominate exit."
) ? void (0) : __assert_fail ("DT->properlyDominates(LoopBypassBlocks.front(), LoopExitBlock) && \"Entry does not dominate exit.\""
, "/home/username/llvm-project/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp"
, 4188, __extension__ __PRETTY_FUNCTION__))
4188 "Entry does not dominate exit.")(static_cast <bool> (DT->properlyDominates(LoopBypassBlocks
.front(), LoopExitBlock) && "Entry does not dominate exit."
) ? void (0) : __assert_fail ("DT->properlyDominates(LoopBypassBlocks.front(), LoopExitBlock) && \"Entry does not dominate exit.\""
, "/home/username/llvm-project/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp"
, 4188, __extension__ __PRETTY_FUNCTION__))
;
4189
4190 DT->addNewBlock(LoopMiddleBlock,
4191 LI->getLoopFor(LoopVectorBody)->getLoopLatch());
4192 DT->addNewBlock(LoopScalarPreHeader, LoopBypassBlocks[0]);
4193 DT->changeImmediateDominator(LoopScalarBody, LoopScalarPreHeader);
4194 DT->changeImmediateDominator(LoopExitBlock, LoopBypassBlocks[0]);
4195 assert(DT->verify(DominatorTree::VerificationLevel::Fast))(static_cast <bool> (DT->verify(DominatorTree::VerificationLevel
::Fast)) ? void (0) : __assert_fail ("DT->verify(DominatorTree::VerificationLevel::Fast)"
, "/home/username/llvm-project/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp"
, 4195, __extension__ __PRETTY_FUNCTION__))
;
4196}
4197
4198void LoopVectorizationCostModel::collectLoopScalars(unsigned VF) {
4199 // We should not collect Scalars more than once per VF. Right now, this
4200 // function is called from collectUniformsAndScalars(), which already does
4201 // this check. Collecting Scalars for VF=1 does not make any sense.
4202 assert(VF >= 2 && Scalars.find(VF) == Scalars.end() &&(static_cast <bool> (VF >= 2 && Scalars.find
(VF) == Scalars.end() && "This function should not be visited twice for the same VF"
) ? void (0) : __assert_fail ("VF >= 2 && Scalars.find(VF) == Scalars.end() && \"This function should not be visited twice for the same VF\""
, "/home/username/llvm-project/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp"
, 4203, __extension__ __PRETTY_FUNCTION__))
4203 "This function should not be visited twice for the same VF")(static_cast <bool> (VF >= 2 && Scalars.find
(VF) == Scalars.end() && "This function should not be visited twice for the same VF"
) ? void (0) : __assert_fail ("VF >= 2 && Scalars.find(VF) == Scalars.end() && \"This function should not be visited twice for the same VF\""
, "/home/username/llvm-project/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp"
, 4203, __extension__ __PRETTY_FUNCTION__))
;
4204
4205 SmallSetVector<Instruction *, 8> Worklist;
4206
4207 // These sets are used to seed the analysis with pointers used by memory
4208 // accesses that will remain scalar.
4209 SmallSetVector<Instruction *, 8> ScalarPtrs;
4210 SmallPtrSet<Instruction *, 8> PossibleNonScalarPtrs;
4211
4212 // A helper that returns true if the use of Ptr by MemAccess will be scalar.
4213 // The pointer operands of loads and stores will be scalar as long as the
4214 // memory access is not a gather or scatter operation. The value operand of a
4215 // store will remain scalar if the store is scalarized.
4216 auto isScalarUse = [&](Instruction *MemAccess, Value *Ptr) {
4217 InstWidening WideningDecision = getWideningDecision(MemAccess, VF);
4218 assert(WideningDecision != CM_Unknown &&(static_cast <bool> (WideningDecision != CM_Unknown &&
"Widening decision should be ready at this moment") ? void (
0) : __assert_fail ("WideningDecision != CM_Unknown && \"Widening decision should be ready at this moment\""
, "/home/username/llvm-project/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp"
, 4219, __extension__ __PRETTY_FUNCTION__))
4219 "Widening decision should be ready at this moment")(static_cast <bool> (WideningDecision != CM_Unknown &&
"Widening decision should be ready at this moment") ? void (
0) : __assert_fail ("WideningDecision != CM_Unknown && \"Widening decision should be ready at this moment\""
, "/home/username/llvm-project/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp"
, 4219, __extension__ __PRETTY_FUNCTION__))
;
4220 if (auto *Store = dyn_cast<StoreInst>(MemAccess))
4221 if (Ptr == Store->getValueOperand())
4222 return WideningDecision == CM_Scalarize;
4223 assert(Ptr == getLoadStorePointerOperand(MemAccess) &&(static_cast <bool> (Ptr == getLoadStorePointerOperand(
MemAccess) && "Ptr is neither a value or pointer operand"
) ? void (0) : __assert_fail ("Ptr == getLoadStorePointerOperand(MemAccess) && \"Ptr is neither a value or pointer operand\""
, "/home/username/llvm-project/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp"
, 4224, __extension__ __PRETTY_FUNCTION__))
4224 "Ptr is neither a value or pointer operand")(static_cast <bool> (Ptr == getLoadStorePointerOperand(
MemAccess) && "Ptr is neither a value or pointer operand"
) ? void (0) : __assert_fail ("Ptr == getLoadStorePointerOperand(MemAccess) && \"Ptr is neither a value or pointer operand\""
, "/home/username/llvm-project/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp"
, 4224, __extension__ __PRETTY_FUNCTION__))
;
4225 return WideningDecision != CM_GatherScatter;
4226 };
4227
4228 // A helper that returns true if the given value is a bitcast or
4229 // getelementptr instruction contained in the loop.
4230 auto isLoopVaryingBitCastOrGEP = [&](Value *V) {
4231 return ((isa<BitCastInst>(V) && V->getType()->isPointerTy()) ||
4232 isa<GetElementPtrInst>(V)) &&
4233 !TheLoop->isLoopInvariant(V);
4234 };
4235
4236 // A helper that evaluates a memory access's use of a pointer. If the use
4237 // will be a scalar use, and the pointer is only used by memory accesses, we
4238 // place the pointer in ScalarPtrs. Otherwise, the pointer is placed in
4239 // PossibleNonScalarPtrs.
4240 auto evaluatePtrUse = [&](Instruction *MemAccess, Value *Ptr) {
4241 // We only care about bitcast and getelementptr instructions contained in
4242 // the loop.
4243 if (!isLoopVaryingBitCastOrGEP(Ptr))
4244 return;
4245
4246 // If the pointer has already been identified as scalar (e.g., if it was
4247 // also identified as uniform), there's nothing to do.
4248 auto *I = cast<Instruction>(Ptr);
4249 if (Worklist.count(I))
4250 return;
4251
4252 // If the use of the pointer will be a scalar use, and all users of the
4253 // pointer are memory accesses, place the pointer in ScalarPtrs. Otherwise,
4254 // place the pointer in PossibleNonScalarPtrs.
4255 if (isScalarUse(MemAccess, Ptr) && llvm::all_of(I->users(), [&](User *U) {
4256 return isa<LoadInst>(U) || isa<StoreInst>(U);
4257 }))
4258 ScalarPtrs.insert(I);
4259 else
4260 PossibleNonScalarPtrs.insert(I);
4261 };
4262
4263 // We seed the scalars analysis with three classes of instructions: (1)
4264 // instructions marked uniform-after-vectorization, (2) bitcast and
4265 // getelementptr instructions used by memory accesses requiring a scalar use,
4266 // and (3) pointer induction variables and their update instructions (we
4267 // currently only scalarize these).
4268 //
4269 // (1) Add to the worklist all instructions that have been identified as
4270 // uniform-after-vectorization.
4271 Worklist.insert(Uniforms[VF].begin(), Uniforms[VF].end());
4272
4273 // (2) Add to the worklist all bitcast and getelementptr instructions used by
4274 // memory accesses requiring a scalar use. The pointer operands of loads and
4275 // stores will be scalar as long as the memory accesses is not a gather or
4276 // scatter operation. The value operand of a store will remain scalar if the
4277 // store is scalarized.
4278 for (auto *BB : TheLoop->blocks())
4279 for (auto &I : *BB) {
4280 if (auto *Load = dyn_cast<LoadInst>(&I)) {
4281 evaluatePtrUse(Load, Load->getPointerOperand());
4282 } else if (auto *Store = dyn_cast<StoreInst>(&I)) {
4283 evaluatePtrUse(Store, Store->getPointerOperand());
4284 evaluatePtrUse(Store, Store->getValueOperand());
4285 }
4286 }
4287 for (auto *I : ScalarPtrs)
4288 if (PossibleNonScalarPtrs.find(I) == PossibleNonScalarPtrs.end()) {
4289 LLVM_DEBUG(dbgs() << "LV: Found scalar instruction: " << *I << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("loop-vectorize")) { dbgs() << "LV: Found scalar instruction: "
<< *I << "\n"; } } while (false)
;
4290 Worklist.insert(I);
4291 }
4292
4293 // (3) Add to the worklist all pointer induction variables and their update
4294 // instructions.
4295 //
4296 // TODO: Once we are able to vectorize pointer induction variables we should
4297 // no longer insert them into the worklist here.
4298 auto *Latch = TheLoop->getLoopLatch();
4299 for (auto &Induction : *Legal->getInductionVars()) {
4300 auto *Ind = Induction.first;
4301 auto *IndUpdate = cast<Instruction>(Ind->getIncomingValueForBlock(Latch));
4302 if (Induction.second.getKind() != InductionDescriptor::IK_PtrInduction)
4303 continue;
4304 Worklist.insert(Ind);
4305 Worklist.insert(IndUpdate);
4306 LLVM_DEBUG(dbgs() << "LV: Found scalar instruction: " << *Ind << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("loop-vectorize")) { dbgs() << "LV: Found scalar instruction: "
<< *Ind << "\n"; } } while (false)
;
4307 LLVM_DEBUG(dbgs() << "LV: Found scalar instruction: " << *IndUpdatedo { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("loop-vectorize")) { dbgs() << "LV: Found scalar instruction: "
<< *IndUpdate << "\n"; } } while (false)
4308 << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("loop-vectorize")) { dbgs() << "LV: Found scalar instruction: "
<< *IndUpdate << "\n"; } } while (false)
;
4309 }
4310
4311 // Insert the forced scalars.
4312 // FIXME: Currently widenPHIInstruction() often creates a dead vector
4313 // induction variable when the PHI user is scalarized.
4314 auto ForcedScalar = ForcedScalars.find(VF);
4315 if (ForcedScalar != ForcedScalars.end())
4316 for (auto *I : ForcedScalar->second)
4317 Worklist.insert(I);
4318
4319 // Expand the worklist by looking through any bitcasts and getelementptr
4320 // instructions we've already identified as scalar. This is similar to the
4321 // expansion step in collectLoopUniforms(); however, here we're only
4322 // expanding to include additional bitcasts and getelementptr instructions.
4323 unsigned Idx = 0;
4324 while (Idx != Worklist.size()) {
4325 Instruction *Dst = Worklist[Idx++];
4326 if (!isLoopVaryingBitCastOrGEP(Dst->getOperand(0)))
4327 continue;
4328 auto *Src = cast<Instruction>(Dst->getOperand(0));
4329 if (llvm::all_of(Src->users(), [&](User *U) -> bool {
4330 auto *J = cast<Instruction>(U);
4331 return !TheLoop->contains(J) || Worklist.count(J) ||
4332 ((isa<LoadInst>(J) || isa<StoreInst>(J)) &&
4333 isScalarUse(J, Src));
4334 })) {
4335 Worklist.insert(Src);
4336 LLVM_DEBUG(dbgs() << "LV: Found scalar instruction: " << *Src << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("loop-vectorize")) { dbgs() << "LV: Found scalar instruction: "
<< *Src << "\n"; } } while (false)
;
4337 }
4338 }
4339
4340 // An induction variable will remain scalar if all users of the induction
4341 // variable and induction variable update remain scalar.
4342 for (auto &Induction : *Legal->getInductionVars()) {
4343 auto *Ind = Induction.first;
4344 auto *IndUpdate = cast<Instruction>(Ind->getIncomingValueForBlock(Latch));
4345
4346 // We already considered pointer induction variables, so there's no reason
4347 // to look at their users again.
4348 //
4349 // TODO: Once we are able to vectorize pointer induction variables we
4350 // should no longer skip over them here.
4351 if (Induction.second.getKind() == InductionDescriptor::IK_PtrInduction)
4352 continue;
4353
4354 // Determine if all users of the induction variable are scalar after
4355 // vectorization.
4356 auto ScalarInd = llvm::all_of(Ind->users(), [&](User *U) -> bool {
4357 auto *I = cast<Instruction>(U);
4358 return I == IndUpdate || !TheLoop->contains(I) || Worklist.count(I);
4359 });
4360 if (!ScalarInd)
4361 continue;
4362
4363 // Determine if all users of the induction variable update instruction are
4364 // scalar after vectorization.
4365 auto ScalarIndUpdate =
4366 llvm::all_of(IndUpdate->users(), [&](User *U) -> bool {
4367 auto *I = cast<Instruction>(U);
4368 return I == Ind || !TheLoop->contains(I) || Worklist.count(I);
4369 });
4370 if (!ScalarIndUpdate)
4371 continue;
4372
4373 // The induction variable and its update instruction will remain scalar.
4374 Worklist.insert(Ind);
4375 Worklist.insert(IndUpdate);
4376 LLVM_DEBUG(dbgs() << "LV: Found scalar instruction: " << *Ind << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("loop-vectorize")) { dbgs() << "LV: Found scalar instruction: "
<< *Ind << "\n"; } } while (false)
;
4377 LLVM_DEBUG(dbgs() << "LV: Found scalar instruction: " << *IndUpdatedo { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("loop-vectorize")) { dbgs() << "LV: Found scalar instruction: "
<< *IndUpdate << "\n"; } } while (false)
4378 << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("loop-vectorize")) { dbgs() << "LV: Found scalar instruction: "
<< *IndUpdate << "\n"; } } while (false)
;
4379 }
4380
4381 Scalars[VF].insert(Worklist.begin(), Worklist.end());
4382}
4383
4384bool LoopVectorizationCostModel::isScalarWithPredication(Instruction *I, unsigned VF) {
4385 if (!blockNeedsPredication(I->getParent()))
4386 return false;
4387 switch(I->getOpcode()) {
4388 default:
4389 break;
4390 case Instruction::Load:
4391 case Instruction::Store: {
4392 if (!Legal->isMaskRequired(I))
4393 return false;
4394 auto *Ptr = getLoadStorePointerOperand(I);
4395 auto *Ty = getMemInstValueType(I);
4396 // We have already decided how to vectorize this instruction, get that
4397 // result.
4398 if (VF > 1) {
4399 InstWidening WideningDecision = getWideningDecision(I, VF);
4400 assert(WideningDecision != CM_Unknown &&(static_cast <bool> (WideningDecision != CM_Unknown &&
"Widening decision should be ready at this moment") ? void (
0) : __assert_fail ("WideningDecision != CM_Unknown && \"Widening decision should be ready at this moment\""
, "/home/username/llvm-project/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp"
, 4401, __extension__ __PRETTY_FUNCTION__))
4401 "Widening decision should be ready at this moment")(static_cast <bool> (WideningDecision != CM_Unknown &&
"Widening decision should be ready at this moment") ? void (
0) : __assert_fail ("WideningDecision != CM_Unknown && \"Widening decision should be ready at this moment\""
, "/home/username/llvm-project/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp"
, 4401, __extension__ __PRETTY_FUNCTION__))
;
4402 return WideningDecision == CM_Scalarize;
4403 }
4404 return isa<LoadInst>(I) ?
4405 !(isLegalMaskedLoad(Ty, Ptr) || isLegalMaskedGather(Ty))
4406 : !(isLegalMaskedStore(Ty, Ptr) || isLegalMaskedScatter(Ty));
4407 }
4408 case Instruction::UDiv:
4409 case Instruction::SDiv:
4410 case Instruction::SRem:
4411 case Instruction::URem:
4412 return mayDivideByZero(*I);
4413 }
4414 return false;
4415}
4416
4417bool LoopVectorizationCostModel::interleavedAccessCanBeWidened(Instruction *I,
4418 unsigned VF) {
4419 assert(isAccessInterleaved(I) && "Expecting interleaved access.")(static_cast <bool> (isAccessInterleaved(I) && "Expecting interleaved access."
) ? void (0) : __assert_fail ("isAccessInterleaved(I) && \"Expecting interleaved access.\""
, "/home/username/llvm-project/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp"
, 4419, __extension__ __PRETTY_FUNCTION__))
;
4420 assert(getWideningDecision(I, VF) == CM_Unknown &&(static_cast <bool> (getWideningDecision(I, VF) == CM_Unknown
&& "Decision should not be set yet.") ? void (0) : __assert_fail
("getWideningDecision(I, VF) == CM_Unknown && \"Decision should not be set yet.\""
, "/home/username/llvm-project/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp"
, 4421, __extension__ __PRETTY_FUNCTION__))
4421 "Decision should not be set yet.")(static_cast <bool> (getWideningDecision(I, VF) == CM_Unknown
&& "Decision should not be set yet.") ? void (0) : __assert_fail
("getWideningDecision(I, VF) == CM_Unknown && \"Decision should not be set yet.\""
, "/home/username/llvm-project/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp"
, 4421, __extension__ __PRETTY_FUNCTION__))
;
4422 auto *Group = getInterleavedAccessGroup(I);
4423 assert(Group && "Must have a group.")(static_cast <bool> (Group && "Must have a group."
) ? void (0) : __assert_fail ("Group && \"Must have a group.\""
, "/home/username/llvm-project/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp"
, 4423, __extension__ __PRETTY_FUNCTION__))
;
4424
4425 // Check if masking is required.
4426 // A Group may need masking for one of two reasons: it resides in a block that
4427 // needs predication, or it was decided to use masking to deal with gaps.
4428 bool PredicatedAccessRequiresMasking =
4429 Legal->blockNeedsPredication(I->getParent()) && Legal->isMaskRequired(I);
4430 bool AccessWithGapsRequiresMasking =
4431 Group->requiresScalarEpilogue() && !IsScalarEpilogueAllowed;
4432 if (!PredicatedAccessRequiresMasking && !AccessWithGapsRequiresMasking)
4433 return true;
4434
4435 // If masked interleaving is required, we expect that the user/target had
4436 // enabled it, because otherwise it either wouldn't have been created or
4437 // it should have been invalidated by the CostModel.
4438 assert(useMaskedInterleavedAccesses(TTI) &&(static_cast <bool> (useMaskedInterleavedAccesses(TTI) &&
"Masked interleave-groups for predicated accesses are not enabled."
) ? void (0) : __assert_fail ("useMaskedInterleavedAccesses(TTI) && \"Masked interleave-groups for predicated accesses are not enabled.\""
, "/home/username/llvm-project/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp"
, 4439, __extension__ __PRETTY_FUNCTION__))
4439 "Masked interleave-groups for predicated accesses are not enabled.")(static_cast <bool> (useMaskedInterleavedAccesses(TTI) &&
"Masked interleave-groups for predicated accesses are not enabled."
) ? void (0) : __assert_fail ("useMaskedInterleavedAccesses(TTI) && \"Masked interleave-groups for predicated accesses are not enabled.\""
, "/home/username/llvm-project/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp"
, 4439, __extension__ __PRETTY_FUNCTION__))
;
4440
4441 auto *Ty = getMemInstValueType(I);
4442 return isa<LoadInst>(I) ? TTI.isLegalMaskedLoad(Ty)
4443 : TTI.isLegalMaskedStore(Ty);
4444}
4445
4446bool LoopVectorizationCostModel::memoryInstructionCanBeWidened(Instruction *I,
4447 unsigned VF) {
4448 // Get and ensure we have a valid memory instruction.
4449 LoadInst *LI = dyn_cast<LoadInst>(I);
4450 StoreInst *SI = dyn_cast<StoreInst>(I);
4451 assert((LI || SI) && "Invalid memory instruction")(static_cast <bool> ((LI || SI) && "Invalid memory instruction"
) ? void (0) : __assert_fail ("(LI || SI) && \"Invalid memory instruction\""
, "/home/username/llvm-project/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp"
, 4451, __extension__ __PRETTY_FUNCTION__))
;
4452
4453 auto *Ptr = getLoadStorePointerOperand(I);
4454
4455 // In order to be widened, the pointer should be consecutive, first of all.
4456 if (!Legal->isConsecutivePtr(Ptr))
4457 return false;
4458
4459 // If the instruction is a store located in a predicated block, it will be
4460 // scalarized.
4461 if (isScalarWithPredication(I))
4462 return false;
4463
4464 // If the instruction's allocated size doesn't equal it's type size, it
4465 // requires padding and will be scalarized.
4466 auto &DL = I->getModule()->getDataLayout();
4467 auto *ScalarTy = LI ? LI->getType() : SI->getValueOperand()->getType();
4468 if (hasIrregularType(ScalarTy, DL, VF))
4469 return false;
4470
4471 return true;
4472}
4473
4474void LoopVectorizationCostModel::collectLoopUniforms(unsigned VF) {
4475 // We should not collect Uniforms more than once per VF. Right now,
4476 // this function is called from collectUniformsAndScalars(), which
4477 // already does this check. Collecting Uniforms for VF=1 does not make any
4478 // sense.
4479
4480 assert(VF >= 2 && Uniforms.find(VF) == Uniforms.end() &&(static_cast <bool> (VF >= 2 && Uniforms.find
(VF) == Uniforms.end() && "This function should not be visited twice for the same VF"
) ? void (0) : __assert_fail ("VF >= 2 && Uniforms.find(VF) == Uniforms.end() && \"This function should not be visited twice for the same VF\""
, "/home/username/llvm-project/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp"
, 4481, __extension__ __PRETTY_FUNCTION__))
4481 "This function should not be visited twice for the same VF")(static_cast <bool> (VF >= 2 && Uniforms.find
(VF) == Uniforms.end() && "This function should not be visited twice for the same VF"
) ? void (0) : __assert_fail ("VF >= 2 && Uniforms.find(VF) == Uniforms.end() && \"This function should not be visited twice for the same VF\""
, "/home/username/llvm-project/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp"
, 4481, __extension__ __PRETTY_FUNCTION__))
;
4482
4483 // Visit the list of Uniforms. If we'll not find any uniform value, we'll
4484 // not analyze again. Uniforms.count(VF) will return 1.
4485 Uniforms[VF].clear();
4486
4487 // We now know that the loop is vectorizable!
4488 // Collect instructions inside the loop that will remain uniform after
4489 // vectorization.
4490
4491 // Global values, params and instructions outside of current loop are out of
4492 // scope.
4493 auto isOutOfScope = [&](Value *V) -> bool {
4494 Instruction *I = dyn_cast<Instruction>(V);
4495 return (!I || !TheLoop->contains(I));
4496 };
4497
4498 SetVector<Instruction *> Worklist;
4499 BasicBlock *Latch = TheLoop->getLoopLatch();
4500
4501 // Start with the conditional branch. If the branch condition is an
4502 // instruction contained in the loop that is only used by the branch, it is
4503 // uniform.
4504 auto *Cmp = dyn_cast<Instruction>(Latch->getTerminator()->getOperand(0));
4505 if (Cmp && TheLoop->contains(Cmp) && Cmp->hasOneUse()) {
4506 Worklist.insert(Cmp);
4507 LLVM_DEBUG(dbgs() << "LV: Found uniform instruction: " << *Cmp << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("loop-vectorize")) { dbgs() << "LV: Found uniform instruction: "
<< *Cmp << "\n"; } } while (false)
;
4508 }
4509
4510 // Holds consecutive and consecutive-like pointers. Consecutive-like pointers
4511 // are pointers that are treated like consecutive pointers during
4512 // vectorization. The pointer operands of interleaved accesses are an
4513 // example.
4514 SmallSetVector<Instruction *, 8> ConsecutiveLikePtrs;
4515
4516 // Holds pointer operands of instructions that are possibly non-uniform.
4517 SmallPtrSet<Instruction *, 8> PossibleNonUniformPtrs;
4518
4519 auto isUniformDecision = [&](Instruction *I, unsigned VF) {
4520 InstWidening WideningDecision = getWideningDecision(I, VF);
4521 assert(WideningDecision != CM_Unknown &&(static_cast <bool> (WideningDecision != CM_Unknown &&
"Widening decision should be ready at this moment") ? void (
0) : __assert_fail ("WideningDecision != CM_Unknown && \"Widening decision should be ready at this moment\""
, "/home/username/llvm-project/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp"
, 4522, __extension__ __PRETTY_FUNCTION__))
4522 "Widening decision should be ready at this moment")(static_cast <bool> (WideningDecision != CM_Unknown &&
"Widening decision should be ready at this moment") ? void (
0) : __assert_fail ("WideningDecision != CM_Unknown && \"Widening decision should be ready at this moment\""
, "/home/username/llvm-project/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp"
, 4522, __extension__ __PRETTY_FUNCTION__))
;
4523
4524 return (WideningDecision == CM_Widen ||
4525 WideningDecision == CM_Widen_Reverse ||
4526 WideningDecision == CM_Interleave);
4527 };
4528 // Iterate over the instructions in the loop, and collect all
4529 // consecutive-like pointer operands in ConsecutiveLikePtrs. If it's possible
4530 // that a consecutive-like pointer operand will be scalarized, we collect it
4531 // in PossibleNonUniformPtrs instead. We use two sets here because a single
4532 // getelementptr instruction can be used by both vectorized and scalarized
4533 // memory instructions. For example, if a loop loads and stores from the same
4534 // location, but the store is conditional, the store will be scalarized, and
4535 // the getelementptr won't remain uniform.
4536 for (auto *BB : TheLoop->blocks())
4537 for (auto &I : *BB) {
4538 // If there's no pointer operand, there's nothing to do.
4539 auto *Ptr = dyn_cast_or_null<Instruction>(getLoadStorePointerOperand(&I));
4540 if (!Ptr)
4541 continue;
4542
4543 // True if all users of Ptr are memory accesses that have Ptr as their
4544 // pointer operand.
4545 auto UsersAreMemAccesses =
4546 llvm::all_of(Ptr->users(), [&](User *U) -> bool {
4547 return getLoadStorePointerOperand(U) == Ptr;
4548 });
4549
4550 // Ensure the memory instruction will not be scalarized or used by
4551 // gather/scatter, making its pointer operand non-uniform. If the pointer
4552 // operand is used by any instruction other than a memory access, we
4553 // conservatively assume the pointer operand may be non-uniform.
4554 if (!UsersAreMemAccesses || !isUniformDecision(&I, VF))
4555 PossibleNonUniformPtrs.insert(Ptr);
4556
4557 // If the memory instruction will be vectorized and its pointer operand
4558 // is consecutive-like, or interleaving - the pointer operand should
4559 // remain uniform.
4560 else
4561 ConsecutiveLikePtrs.insert(Ptr);
4562 }
4563
4564 // Add to the Worklist all consecutive and consecutive-like pointers that
4565 // aren't also identified as possibly non-uniform.
4566 for (auto *V : ConsecutiveLikePtrs)
4567 if (PossibleNonUniformPtrs.find(V) == PossibleNonUniformPtrs.end()) {
4568 LLVM_DEBUG(dbgs() << "LV: Found uniform instruction: " << *V << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("loop-vectorize")) { dbgs() << "LV: Found uniform instruction: "
<< *V << "\n"; } } while (false)
;
4569 Worklist.insert(V);
4570 }
4571
4572 // Expand Worklist in topological order: whenever a new instruction
4573 // is added , its users should be already inside Worklist. It ensures
4574 // a uniform instruction will only be used by uniform instructions.
4575 unsigned idx = 0;
4576 while (idx != Worklist.size()) {
4577 Instruction *I = Worklist[idx++];
4578
4579 for (auto OV : I->operand_values()) {
4580 // isOutOfScope operands cannot be uniform instructions.
4581 if (isOutOfScope(OV))
4582 continue;
4583 // First order recurrence Phi's should typically be considered
4584 // non-uniform.
4585 auto *OP = dyn_cast<PHINode>(OV);
4586 if (OP && Legal->isFirstOrderRecurrence(OP))
4587 continue;
4588 // If all the users of the operand are uniform, then add the
4589 // operand into the uniform worklist.
4590 auto *OI = cast<Instruction>(OV);
4591 if (llvm::all_of(OI->users(), [&](User *U) -> bool {
4592 auto *J = cast<Instruction>(U);
4593 return Worklist.count(J) ||
4594 (OI == getLoadStorePointerOperand(J) &&
4595 isUniformDecision(J, VF));
4596 })) {
4597 Worklist.insert(OI);
4598 LLVM_DEBUG(dbgs() << "LV: Found uniform instruction: " << *OI << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("loop-vectorize")) { dbgs() << "LV: Found uniform instruction: "
<< *OI << "\n"; } } while (false)
;
4599 }
4600 }
4601 }
4602
4603 // Returns true if Ptr is the pointer operand of a memory access instruction
4604 // I, and I is known to not require scalarization.
4605 auto isVectorizedMemAccessUse = [&](Instruction *I, Value *Ptr) -> bool {
4606 return getLoadStorePointerOperand(I) == Ptr && isUniformDecision(I, VF);
4607 };
4608
4609 // For an instruction to be added into Worklist above, all its users inside
4610 // the loop should also be in Worklist. However, this condition cannot be
4611 // true for phi nodes that form a cyclic dependence. We must process phi
4612 // nodes separately. An induction variable will remain uniform if all users
4613 // of the induction variable and induction variable update remain uniform.
4614 // The code below handles both pointer and non-pointer induction variables.
4615 for (auto &Induction : *Legal->getInductionVars()) {
4616 auto *Ind = Induction.first;
4617 auto *IndUpdate = cast<Instruction>(Ind->getIncomingValueForBlock(Latch));
4618
4619 // Determine if all users of the induction variable are uniform after
4620 // vectorization.
4621 auto UniformInd = llvm::all_of(Ind->users(), [&](User *U) -> bool {
4622 auto *I = cast<Instruction>(U);
4623 return I == IndUpdate || !TheLoop->contains(I) || Worklist.count(I) ||
4624 isVectorizedMemAccessUse(I, Ind);
4625 });
4626 if (!UniformInd)
4627 continue;
4628
4629 // Determine if all users of the induction variable update instruction are
4630 // uniform after vectorization.
4631 auto UniformIndUpdate =
4632 llvm::all_of(IndUpdate->users(), [&](User *U) -> bool {
4633 auto *I = cast<Instruction>(U);
4634 return I == Ind || !TheLoop->contains(I) || Worklist.count(I) ||
4635 isVectorizedMemAccessUse(I, IndUpdate);
4636 });
4637 if (!UniformIndUpdate)
4638 continue;
4639
4640 // The induction variable and its update instruction will remain uniform.
4641 Worklist.insert(Ind);
4642 Worklist.insert(IndUpdate);
4643 LLVM_DEBUG(dbgs() << "LV: Found uniform instruction: " << *Ind << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("loop-vectorize")) { dbgs() << "LV: Found uniform instruction: "
<< *Ind << "\n"; } } while (false)
;
4644 LLVM_DEBUG(dbgs() << "LV: Found uniform instruction: " << *IndUpdatedo { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("loop-vectorize")) { dbgs() << "LV: Found uniform instruction: "
<< *IndUpdate << "\n"; } } while (false)
4645 << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("loop-vectorize")) { dbgs() << "LV: Found uniform instruction: "
<< *IndUpdate << "\n"; } } while (false)
;
4646 }
4647
4648 Uniforms[VF].insert(Worklist.begin(), Worklist.end());
4649}
4650
4651Optional<unsigned> LoopVectorizationCostModel::computeMaxVF(bool OptForSize) {
4652 if (Legal->getRuntimePointerChecking()->Need && TTI.hasBranchDivergence()) {
4653 // TODO: It may by useful to do since it's still likely to be dynamically
4654 // uniform if the target can skip.
4655 LLVM_DEBUG(do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("loop-vectorize")) { dbgs() << "LV: Not inserting runtime ptr check for divergent target"
; } } while (false)
4656 dbgs() << "LV: Not inserting runtime ptr check for divergent target")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("loop-vectorize")) { dbgs() << "LV: Not inserting runtime ptr check for divergent target"
; } } while (false)
;
4657
4658 ORE->emit(
4659 createMissedAnalysis("CantVersionLoopWithDivergentTarget")
4660 << "runtime pointer checks needed. Not enabled for divergent target");
4661
4662 return None;
4663 }
4664
4665 unsigned TC = PSE.getSE()->getSmallConstantTripCount(TheLoop);
4666 if (!OptForSize) // Remaining checks deal with scalar loop when OptForSize.
4667 return computeFeasibleMaxVF(OptForSize, TC);
4668
4669 if (Legal->getRuntimePointerChecking()->Need) {
4670 ORE->emit(createMissedAnalysis("CantVersionLoopWithOptForSize")
4671 << "runtime pointer checks needed. Enable vectorization of this "
4672 "loop with '#pragma clang loop vectorize(enable)' when "
4673 "compiling with -Os/-Oz");
4674 LLVM_DEBUG(do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("loop-vectorize")) { dbgs() << "LV: Aborting. Runtime ptr check is required with -Os/-Oz.\n"
; } } while (false)
4675 dbgs()do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("loop-vectorize")) { dbgs() << "LV: Aborting. Runtime ptr check is required with -Os/-Oz.\n"
; } } while (false)
4676 << "LV: Aborting. Runtime ptr check is required with -Os/-Oz.\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("loop-vectorize")) { dbgs() << "LV: Aborting. Runtime ptr check is required with -Os/-Oz.\n"
; } } while (false)
;
4677 return None;
4678 }
4679
4680 if (!PSE.getUnionPredicate().getPredicates().empty()) {
4681 ORE->emit(createMissedAnalysis("CantVersionLoopWithOptForSize")
4682 << "runtime SCEV checks needed. Enable vectorization of this "
4683 "loop with '#pragma clang loop vectorize(enable)' when "
4684 "compiling with -Os/-Oz");
4685 LLVM_DEBUG(do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("loop-vectorize")) { dbgs() << "LV: Aborting. Runtime SCEV check is required with -Os/-Oz.\n"
; } } while (false)
4686 dbgs()do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("loop-vectorize")) { dbgs() << "LV: Aborting. Runtime SCEV check is required with -Os/-Oz.\n"
; } } while (false)
4687 << "LV: Aborting. Runtime SCEV check is required with -Os/-Oz.\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("loop-vectorize")) { dbgs() << "LV: Aborting. Runtime SCEV check is required with -Os/-Oz.\n"
; } } while (false)
;
4688 return None;
4689 }
4690
4691 // FIXME: Avoid specializing for stride==1 instead of bailing out.
4692 if (!Legal->getLAI()->getSymbolicStrides().empty()) {
4693 ORE->emit(createMissedAnalysis("CantVersionLoopWithOptForSize")
4694 << "runtime stride == 1 checks needed. Enable vectorization of "
4695 "this loop with '#pragma clang loop vectorize(enable)' when "
4696 "compiling with -Os/-Oz");
4697 LLVM_DEBUG(do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("loop-vectorize")) { dbgs() << "LV: Aborting. Runtime stride check is required with -Os/-Oz.\n"
; } } while (false)
4698 dbgs()do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("loop-vectorize")) { dbgs() << "LV: Aborting. Runtime stride check is required with -Os/-Oz.\n"
; } } while (false)
4699 << "LV: Aborting. Runtime stride check is required with -Os/-Oz.\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("loop-vectorize")) { dbgs() << "LV: Aborting. Runtime stride check is required with -Os/-Oz.\n"
; } } while (false)
;
4700 return None;
4701 }
4702
4703 // If we optimize the program for size, avoid creating the tail loop.
4704 LLVM_DEBUG(dbgs() << "LV: Found trip count: " << TC << '\n')do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("loop-vectorize")) { dbgs() << "LV: Found trip count: "
<< TC << '\n'; } } while (false)
;
4705
4706 if (TC == 1) {
4707 ORE->emit(createMissedAnalysis("SingleIterationLoop")
4708 << "loop trip count is one, irrelevant for vectorization");
4709 LLVM_DEBUG(dbgs() << "LV: Aborting, single iteration (non) loop.\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("loop-vectorize")) { dbgs() << "LV: Aborting, single iteration (non) loop.\n"
; } } while (false)
;
4710 return None;
4711 }
4712
4713 // Record that scalar epilogue is not allowed.
4714 LLVM_DEBUG(dbgs() << "LV: Not allowing scalar epilogue due to -Os/-Oz.\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("loop-vectorize")) { dbgs() << "LV: Not allowing scalar epilogue due to -Os/-Oz.\n"
; } } while (false)
;
4715
4716 IsScalarEpilogueAllowed = !OptForSize;
4717
4718 // We don't create an epilogue when optimizing for size.
4719 // Invalidate interleave groups that require an epilogue if we can't mask
4720 // the interleave-group.
4721 if (!useMaskedInterleavedAccesses(TTI))
4722 InterleaveInfo.invalidateGroupsRequiringScalarEpilogue();
4723
4724 unsigned MaxVF = computeFeasibleMaxVF(OptForSize, TC);
4725
4726 if (TC > 0 && TC % MaxVF == 0) {
4727 LLVM_DEBUG(dbgs() << "LV: No tail will remain for any chosen VF.\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("loop-vectorize")) { dbgs() << "LV: No tail will remain for any chosen VF.\n"
; } } while (false)
;
4728 return MaxVF;
4729 }
4730
4731 // If we don't know the precise trip count, or if the trip count that we
4732 // found modulo the vectorization factor is not zero, try to fold the tail
4733 // by masking.
4734 // FIXME: look for a smaller MaxVF that does divide TC rather than masking.
4735 if (Legal->canFoldTailByMasking()) {
4736 FoldTailByMasking = true;
4737 return MaxVF;
4738 }
4739
4740 if (TC == 0) {
4741 ORE->emit(
4742 createMissedAnalysis("UnknownLoopCountComplexCFG")
4743 << "unable to calculate the loop count due to complex control flow");
4744 return None;
4745 }
4746
4747 ORE->emit(createMissedAnalysis("NoTailLoopWithOptForSize")
4748 << "cannot optimize for size and vectorize at the same time. "
4749 "Enable vectorization of this loop with '#pragma clang loop "
4750 "vectorize(enable)' when compiling with -Os/-Oz");
4751 return None;
4752}
4753
4754unsigned
4755LoopVectorizationCostModel::computeFeasibleMaxVF(bool OptForSize,
4756 unsigned ConstTripCount) {
4757 MinBWs = computeMinimumValueSizes(TheLoop->getBlocks(), *DB, &TTI);
4758 unsigned SmallestType, WidestType;
4759 std::tie(SmallestType, WidestType) = getSmallestAndWidestTypes();
4760 unsigned WidestRegister = TTI.getRegisterBitWidth(true);
4761
4762 // Get the maximum safe dependence distance in bits computed by LAA.
4763 // It is computed by MaxVF * sizeOf(type) * 8, where type is taken from
4764 // the memory accesses that is most restrictive (involved in the smallest
4765 // dependence distance).
4766 unsigned MaxSafeRegisterWidth = Legal->getMaxSafeRegisterWidth();
4767
4768 WidestRegister = std::min(WidestRegister, MaxSafeRegisterWidth);
4769
4770 unsigned MaxVectorSize = WidestRegister / WidestType;
4771
4772 LLVM_DEBUG(dbgs() << "LV: The Smallest and Widest types: " << SmallestTypedo { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("loop-vectorize")) { dbgs() << "LV: The Smallest and Widest types: "
<< SmallestType << " / " << WidestType <<
" bits.\n"; } } while (false)
4773 << " / " << WidestType << " bits.\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("loop-vectorize")) { dbgs() << "LV: The Smallest and Widest types: "
<< SmallestType << " / " << WidestType <<
" bits.\n"; } } while (false)
;
4774 LLVM_DEBUG(dbgs() << "LV: The Widest register safe to use is: "do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("loop-vectorize")) { dbgs() << "LV: The Widest register safe to use is: "
<< WidestRegister << " bits.\n"; } } while (false
)
4775 << WidestRegister << " bits.\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("loop-vectorize")) { dbgs() << "LV: The Widest register safe to use is: "
<< WidestRegister << " bits.\n"; } } while (false
)
;
4776
4777 assert(MaxVectorSize <= 256 && "Did not expect to pack so many elements"(static_cast <bool> (MaxVectorSize <= 256 &&
"Did not expect to pack so many elements" " into one vector!"
) ? void (0) : __assert_fail ("MaxVectorSize <= 256 && \"Did not expect to pack so many elements\" \" into one vector!\""
, "/home/username/llvm-project/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp"
, 4778, __extension__ __PRETTY_FUNCTION__))
4778 " into one vector!")(static_cast <bool> (MaxVectorSize <= 256 &&
"Did not expect to pack so many elements" " into one vector!"
) ? void (0) : __assert_fail ("MaxVectorSize <= 256 && \"Did not expect to pack so many elements\" \" into one vector!\""
, "/home/username/llvm-project/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp"
, 4778, __extension__ __PRETTY_FUNCTION__))
;
4779 if (MaxVectorSize == 0) {
4780 LLVM_DEBUG(dbgs() << "LV: The target has no vector registers.\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("loop-vectorize")) { dbgs() << "LV: The target has no vector registers.\n"
; } } while (false)
;
4781 MaxVectorSize = 1;
4782 return MaxVectorSize;
4783 } else if (ConstTripCount && ConstTripCount < MaxVectorSize &&
4784 isPowerOf2_32(ConstTripCount)) {
4785 // We need to clamp the VF to be the ConstTripCount. There is no point in
4786 // choosing a higher viable VF as done in the loop below.
4787 LLVM_DEBUG(dbgs() << "LV: Clamping the MaxVF to the constant trip count: "do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("loop-vectorize")) { dbgs() << "LV: Clamping the MaxVF to the constant trip count: "
<< ConstTripCount << "\n"; } } while (false)
4788 << ConstTripCount << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("loop-vectorize")) { dbgs() << "LV: Clamping the MaxVF to the constant trip count: "
<< ConstTripCount << "\n"; } } while (false)
;
4789 MaxVectorSize = ConstTripCount;
4790 return MaxVectorSize;
4791 }
4792
4793 unsigned MaxVF = MaxVectorSize;
4794 if (TTI.shouldMaximizeVectorBandwidth(OptForSize) ||
4795 (MaximizeBandwidth && !OptForSize)) {
4796 // Collect all viable vectorization factors larger than the default MaxVF
4797 // (i.e. MaxVectorSize).
4798 SmallVector<unsigned, 8> VFs;
4799 unsigned NewMaxVectorSize = WidestRegister / SmallestType;
4800 for (unsigned VS = MaxVectorSize * 2; VS <= NewMaxVectorSize; VS *= 2)
4801 VFs.push_back(VS);
4802
4803 // For each VF calculate its register usage.
4804 auto RUs = calculateRegisterUsage(VFs);
4805
4806 // Select the largest VF which doesn't require more registers than existing
4807 // ones.
4808 unsigned TargetNumRegisters = TTI.getNumberOfRegisters(true);
4809 for (int i = RUs.size() - 1; i >= 0; --i) {
4810 if (RUs[i].MaxLocalUsers <= TargetNumRegisters) {
4811 MaxVF = VFs[i];
4812 break;
4813 }
4814 }
4815 if (unsigned MinVF = TTI.getMinimumVF(SmallestType)) {
4816 if (MaxVF < MinVF) {
4817 LLVM_DEBUG(dbgs() << "LV: Overriding calculated MaxVF(" << MaxVFdo { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("loop-vectorize")) { dbgs() << "LV: Overriding calculated MaxVF("
<< MaxVF << ") with target's minimum: " <<
MinVF << '\n'; } } while (false)
4818 << ") with target's minimum: " << MinVF << '\n')do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("loop-vectorize")) { dbgs() << "LV: Overriding calculated MaxVF("
<< MaxVF << ") with target's minimum: " <<
MinVF << '\n'; } } while (false)
;
4819 MaxVF = MinVF;
4820 }
4821 }
4822 }
4823 return MaxVF;
4824}
4825
4826VectorizationFactor
4827LoopVectorizationCostModel::selectVectorizationFactor(unsigned MaxVF) {
4828 float Cost = expectedCost(1).first;
4829 const float ScalarCost = Cost;
4830 unsigned Width = 1;
4831 LLVM_DEBUG(dbgs() << "LV: Scalar loop costs: " << (int)ScalarCost << ".\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("loop-vectorize")) { dbgs() << "LV: Scalar loop costs: "
<< (int)ScalarCost << ".\n"; } } while (false)
;
4832
4833 bool ForceVectorization = Hints->getForce() == LoopVectorizeHints::FK_Enabled;
4834 if (ForceVectorization && MaxVF > 1) {
4835 // Ignore scalar width, because the user explicitly wants vectorization.
4836 // Initialize cost to max so that VF = 2 is, at least, chosen during cost
4837 // evaluation.
4838 Cost = std::numeric_limits<float>::max();
4839 }
4840
4841 for (unsigned i = 2; i <= MaxVF; i *= 2) {
4842 // Notice that the vector loop needs to be executed less times, so
4843 // we need to divide the cost of the vector loops by the width of
4844 // the vector elements.
4845 VectorizationCostTy C = expectedCost(i);
4846 float VectorCost = C.first / (float)i;
4847 LLVM_DEBUG(dbgs() << "LV: Vector loop of width " << ido { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("loop-vectorize")) { dbgs() << "LV: Vector loop of width "
<< i << " costs: " << (int)VectorCost <<
".\n"; } } while (false)
4848 << " costs: " << (int)VectorCost << ".\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("loop-vectorize")) { dbgs() << "LV: Vector loop of width "
<< i << " costs: " << (int)VectorCost <<
".\n"; } } while (false)
;
4849 if (!C.second && !ForceVectorization) {
4850 LLVM_DEBUG(do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("loop-vectorize")) { dbgs() << "LV: Not considering vector loop of width "
<< i << " because it will not generate any vector instructions.\n"
; } } while (false)
4851 dbgs() << "LV: Not considering vector loop of width " << ido { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("loop-vectorize")) { dbgs() << "LV: Not considering vector loop of width "
<< i << " because it will not generate any vector instructions.\n"
; } } while (false)
4852 << " because it will not generate any vector instructions.\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("loop-vectorize")) { dbgs() << "LV: Not considering vector loop of width "
<< i << " because it will not generate any vector instructions.\n"
; } } while (false)
;
4853 continue;
4854 }
4855 if (VectorCost < Cost) {
4856 Cost = VectorCost;
4857 Width = i;
4858 }
4859 }
4860
4861 if (!EnableCondStoresVectorization && NumPredStores) {
4862 ORE->emit(createMissedAnalysis("ConditionalStore")
4863 << "store that is conditionally executed prevents vectorization");
4864 LLVM_DEBUG(do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("loop-vectorize")) { dbgs() << "LV: No vectorization. There are conditional stores.\n"
; } } while (false)
4865 dbgs() << "LV: No vectorization. There are conditional stores.\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("loop-vectorize")) { dbgs() << "LV: No vectorization. There are conditional stores.\n"
; } } while (false)
;
4866 Width = 1;
4867 Cost = ScalarCost;
4868 }
4869
4870 LLVM_DEBUG(if (ForceVectorization && Width > 1 && Cost >= ScalarCost) dbgs()do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("loop-vectorize")) { if (ForceVectorization && Width
> 1 && Cost >= ScalarCost) dbgs() << "LV: Vectorization seems to be not beneficial, "
<< "but was forced by a user.\n"; } } while (false)
4871 << "LV: Vectorization seems to be not beneficial, "do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("loop-vectorize")) { if (ForceVectorization && Width
> 1 && Cost >= ScalarCost) dbgs() << "LV: Vectorization seems to be not beneficial, "
<< "but was forced by a user.\n"; } } while (false)
4872 << "but was forced by a user.\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("loop-vectorize")) { if (ForceVectorization && Width
> 1 && Cost >= ScalarCost) dbgs() << "LV: Vectorization seems to be not beneficial, "
<< "but was forced by a user.\n"; } } while (false)
;
4873 LLVM_DEBUG(dbgs() << "LV: Selecting VF: " << Width << ".\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("loop-vectorize")) { dbgs() << "LV: Selecting VF: " <<
Width << ".\n"; } } while (false)
;
4874 VectorizationFactor Factor = {Width, (unsigned)(Width * Cost)};
4875 return Factor;
4876}
4877
4878std::pair<unsigned, unsigned>
4879LoopVectorizationCostModel::getSmallestAndWidestTypes() {
4880 unsigned MinWidth = -1U;
4881 unsigned MaxWidth = 8;
4882 const DataLayout &DL = TheFunction->getParent()->getDataLayout();
4883
4884 // For each block.
4885 for (BasicBlock *BB : TheLoop->blocks()) {
4886 // For each instruction in the loop.
4887 for (Instruction &I : BB->instructionsWithoutDebug()) {
4888 Type *T = I.getType();
4889
4890 // Skip ignored values.
4891 if (ValuesToIgnore.find(&I) != ValuesToIgnore.end())
4892 continue;
4893
4894 // Only examine Loads, Stores and PHINodes.
4895 if (!isa<LoadInst>(I) && !isa<StoreInst>(I) && !isa<PHINode>(I))
4896 continue;
4897
4898 // Examine PHI nodes that are reduction variables. Update the type to
4899 // account for the recurrence type.
4900 if (auto *PN = dyn_cast<PHINode>(&I)) {
4901 if (!Legal->isReductionVariable(PN))
4902 continue;
4903 RecurrenceDescriptor RdxDesc = (*Legal->getReductionVars())[PN];
4904 T = RdxDesc.getRecurrenceType();
4905 }
4906
4907 // Examine the stored values.
4908 if (auto *ST = dyn_cast<StoreInst>(&I))
4909 T = ST->getValueOperand()->getType();
4910
4911 // Ignore loaded pointer types and stored pointer types that are not
4912 // vectorizable.
4913 //
4914 // FIXME: The check here attempts to predict whether a load or store will
4915 // be vectorized. We only know this for certain after a VF has
4916 // been selected. Here, we assume that if an access can be
4917 // vectorized, it will be. We should also look at extending this
4918 // optimization to non-pointer types.
4919 //
4920 if (T->isPointerTy() && !isConsecutiveLoadOrStore(&I) &&
4921 !isAccessInterleaved(&I) && !isLegalGatherOrScatter(&I))
4922 continue;
4923
4924 MinWidth = std::min(MinWidth,
4925 (unsigned)DL.getTypeSizeInBits(T->getScalarType()));
4926 MaxWidth = std::max(MaxWidth,
4927 (unsigned)DL.getTypeSizeInBits(T->getScalarType()));
4928 }
4929 }
4930
4931 return {MinWidth, MaxWidth};
4932}
4933
4934unsigned LoopVectorizationCostModel::selectInterleaveCount(bool OptForSize,
4935 unsigned VF,
4936 unsigned LoopCost) {
4937 // -- The interleave heuristics --
4938 // We interleave the loop in order to expose ILP and reduce the loop overhead.
4939 // There are many micro-architectural considerations that we can't predict
4940 // at this level. For example, frontend pressure (on decode or fetch) due to
4941 // code size, or the number and capabilities of the execution ports.
4942 //
4943 // We use the following heuristics to select the interleave count:
4944 // 1. If the code has reductions, then we interleave to break the cross
4945 // iteration dependency.
4946 // 2. If the loop is really small, then we interleave to reduce the loop
4947 // overhead.
4948 // 3. We don't interleave if we think that we will spill registers to memory
4949 // due to the increased register pressure.
4950
4951 // When we optimize for size, we don't interleave.
4952 if (OptForSize'OptForSize' is false)
30
Taking false branch
4953 return 1;
4954
4955 // We used the distance for the interleave count.
4956 if (Legal->getMaxSafeDepDistBytes() != -1U)
31
Assuming the condition is false
32
Taking false branch
4957 return 1;
4958
4959 // Do not interleave loops with a relatively small trip count.
4960 unsigned TC = PSE.getSE()->getSmallConstantTripCount(TheLoop);
4961 if (TC > 1 && TC < TinyTripCountInterleaveThreshold)
33
Assuming 'TC' is <= 1
4962 return 1;
4963
4964 unsigned TargetNumRegisters = TTI.getNumberOfRegisters(VF'VF' is > 1 > 1);
4965 LLVM_DEBUG(dbgs() << "LV: The target has " << TargetNumRegistersdo { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("loop-vectorize")) { dbgs() << "LV: The target has " <<
TargetNumRegisters << " registers\n"; } } while (false
)
34
Assuming 'DebugFlag' is false
35
Loop condition is false. Exiting loop
4966 << " registers\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("loop-vectorize")) { dbgs() << "LV: The target has " <<
TargetNumRegisters << " registers\n"; } } while (false
)
;
4967
4968 if (VF'VF' is not equal to 1 == 1) {
36
Taking false branch
4969 if (ForceTargetNumScalarRegs.getNumOccurrences() > 0)
4970 TargetNumRegisters = ForceTargetNumScalarRegs;
4971 } else {
4972 if (ForceTargetNumVectorRegs.getNumOccurrences() > 0)
37
Assuming the condition is false
38
Taking false branch
4973 TargetNumRegisters = ForceTargetNumVectorRegs;
4974 }
4975
4976 RegisterUsage R = calculateRegisterUsage({VF})[0];
4977 // We divide by these constants so assume that we have at least one
4978 // instruction that uses at least one register.
4979 R.MaxLocalUsers = std::max(R.MaxLocalUsers, 1U);
4980
4981 // We calculate the interleave count using the following formula.
4982 // Subtract the number of loop invariants from the number of available
4983 // registers. These registers are used by all of the interleaved instances.
4984 // Next, divide the remaining registers by the number of registers that is
4985 // required by the loop, in order to estimate how many parallel instances
4986 // fit without causing spills. All of this is rounded down if necessary to be
4987 // a power of two. We want power of two interleave count to simplify any
4988 // addressing operations or alignment considerations.
4989 // We also want power of two interleave counts to ensure that the induction
4990 // variable of the vector loop wraps to zero, when tail is folded by masking;
4991 // this currently happens when OptForSize, in which case IC is set to 1 above.
4992 unsigned IC = PowerOf2Floor((TargetNumRegisters - R.LoopInvariantRegs) /
4993 R.MaxLocalUsers);
4994
4995 // Don't count the induction variable as interleaved.
4996 if (EnableIndVarRegisterHeur)
39
Assuming the condition is false
40
Taking false branch
4997 IC = PowerOf2Floor((TargetNumRegisters - R.LoopInvariantRegs - 1) /
4998 std::max(1U, (R.MaxLocalUsers - 1)));
4999
5000 // Clamp the interleave ranges to reasonable counts.
5001 unsigned MaxInterleaveCount = TTI.getMaxInterleaveFactor(VF);
5002
5003 // Check if the user has overridden the max.
5004 if (VF'VF' is not equal to 1 == 1) {
41
Taking false branch
5005 if (ForceTargetMaxScalarInterleaveFactor.getNumOccurrences() > 0)
5006 MaxInterleaveCount = ForceTargetMaxScalarInterleaveFactor;
5007 } else {
5008 if (ForceTargetMaxVectorInterleaveFactor.getNumOccurrences() > 0)
42
Assuming the condition is false
43
Taking false branch
5009 MaxInterleaveCount = ForceTargetMaxVectorInterleaveFactor;
5010 }
5011
5012 // If we did not calculate the cost for VF (because the user selected the VF)
5013 // then we calculate the cost of VF here.
5014 if (LoopCost'LoopCost' is equal to 0 == 0)
44
Taking true branch
5015 LoopCost = expectedCost(VF).first;
45
The value 0 is assigned to 'LoopCost'
5016
5017 // Clamp the calculated IC to be between the 1 and the max interleave count
5018 // that the target allows.
5019 if (IC > MaxInterleaveCount)
46
Assuming 'IC' is <= 'MaxInterleaveCount'
47
Taking false branch
5020 IC = MaxInterleaveCount;
5021 else if (IC < 1)
48
Assuming 'IC' is >= 1
49
Taking false branch
5022 IC = 1;
5023
5024 // Interleave if we vectorized this loop and there is a reduction that could
5025 // benefit from interleaving.
5026 if (VF'VF' is > 1 > 1 && !Legal->getReductionVars()->empty()) {
50
Assuming the condition is false
51
Taking false branch
5027 LLVM_DEBUG(dbgs() << "LV: Interleaving because of reductions.\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("loop-vectorize")) { dbgs() << "LV: Interleaving because of reductions.\n"
; } } while (false)
;
5028 return IC;
5029 }
5030
5031 // Note that if we've already vectorized the loop we will have done the
5032 // runtime check and so interleaving won't require further checks.
5033 bool InterleavingRequiresRuntimePointerCheck =
5034 (VF'VF' is not equal to 1 == 1 && Legal->getRuntimePointerChecking()->Need);
5035
5036 // We want to interleave small loops in order to reduce the loop overhead and
5037 // potentially expose ILP opportunities.
5038 LLVM_DEBUG(dbgs() << "LV: Loop cost is " << LoopCost << '\n')do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("loop-vectorize")) { dbgs() << "LV: Loop cost is " <<
LoopCost << '\n'; } } while (false)
;
52
Assuming 'DebugFlag' is false
53
Loop condition is false. Exiting loop
5039 if (!InterleavingRequiresRuntimePointerCheck && LoopCost < SmallLoopCost) {
54
Assuming the condition is true
55
Taking true branch
5040 // We assume that the cost overhead is 1 and we use the cost model
5041 // to estimate the cost of the loop and interleave until the cost of the
5042 // loop overhead is about 5% of the cost of the loop.
5043 unsigned SmallIC =
5044 std::min(IC, (unsigned)PowerOf2Floor(SmallLoopCost / LoopCost));
56
Division by zero
5045
5046 // Interleave until store/load ports (estimated by max interleave count) are
5047 // saturated.
5048 unsigned NumStores = Legal->getNumStores();
5049 unsigned NumLoads = Legal->getNumLoads();
5050 unsigned StoresIC = IC / (NumStores ? NumStores : 1);
5051 unsigned LoadsIC = IC / (NumLoads ? NumLoads : 1);
5052
5053 // If we have a scalar reduction (vector reductions are already dealt with
5054 // by this point), we can increase the critical path length if the loop
5055 // we're interleaving is inside another loop. Limit, by default to 2, so the
5056 // critical path only gets increased by one reduction operation.
5057 if (!Legal->getReductionVars()->empty() && TheLoop->getLoopDepth() > 1) {
5058 unsigned F = static_cast<unsigned>(MaxNestedScalarReductionIC);
5059 SmallIC = std::min(SmallIC, F);
5060 StoresIC = std::min(StoresIC, F);
5061 LoadsIC = std::min(LoadsIC, F);
5062 }
5063
5064 if (EnableLoadStoreRuntimeInterleave &&
5065 std::max(StoresIC, LoadsIC) > SmallIC) {
5066 LLVM_DEBUG(do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("loop-vectorize")) { dbgs() << "LV: Interleaving to saturate store or load ports.\n"
; } } while (false)
5067 dbgs() << "LV: Interleaving to saturate store or load ports.\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("loop-vectorize")) { dbgs() << "LV: Interleaving to saturate store or load ports.\n"
; } } while (false)
;
5068 return std::max(StoresIC, LoadsIC);
5069 }
5070
5071 LLVM_DEBUG(dbgs() << "LV: Interleaving to reduce branch cost.\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("loop-vectorize")) { dbgs() << "LV: Interleaving to reduce branch cost.\n"
; } } while (false)
;
5072 return SmallIC;
5073 }
5074
5075 // Interleave if this is a large loop (small loops are already dealt with by
5076 // this point) that could benefit from interleaving.
5077 bool HasReductions = !Legal->getReductionVars()->empty();
5078 if (TTI.enableAggressiveInterleaving(HasReductions)) {
5079 LLVM_DEBUG(dbgs() << "LV: Interleaving to expose ILP.\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("loop-vectorize")) { dbgs() << "LV: Interleaving to expose ILP.\n"
; } } while (false)
;
5080 return IC;
5081 }
5082
5083 LLVM_DEBUG(dbgs() << "LV: Not Interleaving.\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("loop-vectorize")) { dbgs() << "LV: Not Interleaving.\n"
; } } while (false)
;
5084 return 1;
5085}
5086
5087SmallVector<LoopVectorizationCostModel::RegisterUsage, 8>
5088LoopVectorizationCostModel::calculateRegisterUsage(ArrayRef<unsigned> VFs) {
5089 // This function calculates the register usage by measuring the highest number
5090 // of values that are alive at a single location. Obviously, this is a very
5091 // rough estimation. We scan the loop in a topological order in order and
5092 // assign a number to each instruction. We use RPO to ensure that defs are
5093 // met before their users. We assume that each instruction that has in-loop
5094 // users starts an interval. We record every time that an in-loop value is
5095 // used, so we have a list of the first and last occurrences of each
5096 // instruction. Next, we transpose this data structure into a multi map that
5097 // holds the list of intervals that *end* at a specific location. This multi
5098 // map allows us to perform a linear search. We scan the instructions linearly
5099 // and record each time that a new interval starts, by placing it in a set.
5100 // If we find this value in the multi-map then we remove it from the set.
5101 // The max register usage is the maximum size of the set.
5102 // We also search for instructions that are defined outside the loop, but are
5103 // used inside the loop. We need this number separately from the max-interval
5104 // usage number because when we unroll, loop-invariant values do not take
5105 // more register.
5106 LoopBlocksDFS DFS(TheLoop);
5107 DFS.perform(LI);
5108
5109 RegisterUsage RU;
5110
5111 // Each 'key' in the map opens a new interval. The values
5112 // of the map are the index of the 'last seen' usage of the
5113 // instruction that is the key.
5114 using IntervalMap = DenseMap<Instruction *, unsigned>;
5115
5116 // Maps instruction to its index.
5117 SmallVector<Instruction *, 64> IdxToInstr;
5118 // Marks the end of each interval.
5119 IntervalMap EndPoint;
5120 // Saves the list of instruction indices that are used in the loop.
5121 SmallPtrSet<Instruction *, 8> Ends;
5122 // Saves the list of values that are used in the loop but are
5123 // defined outside the loop, such as arguments and constants.
5124 SmallPtrSet<Value *, 8> LoopInvariants;
5125
5126 for (BasicBlock *BB : make_range(DFS.beginRPO(), DFS.endRPO())) {
5127 for (Instruction &I : BB->instructionsWithoutDebug()) {
5128 IdxToInstr.push_back(&I);
5129
5130 // Save the end location of each USE.
5131 for (Value *U : I.operands()) {
5132 auto *Instr = dyn_cast<Instruction>(U);
5133
5134 // Ignore non-instruction values such as arguments, constants, etc.
5135 if (!Instr)
5136 continue;
5137
5138 // If this instruction is outside the loop then record it and continue.
5139 if (!TheLoop->contains(Instr)) {
5140 LoopInvariants.insert(Instr);
5141 continue;
5142 }
5143
5144 // Overwrite previous end points.
5145 EndPoint[Instr] = IdxToInstr.size();
5146 Ends.insert(Instr);
5147 }
5148 }
5149 }
5150
5151 // Saves the list of intervals that end with the index in 'key'.
5152 using InstrList = SmallVector<Instruction *, 2>;
5153 DenseMap<unsigned, InstrList> TransposeEnds;
5154
5155 // Transpose the EndPoints to a list of values that end at each index.
5156 for (auto &Interval : EndPoint)
5157 TransposeEnds[Interval.second].push_back(Interval.first);
5158
5159 SmallPtrSet<Instruction *, 8> OpenIntervals;
5160
5161 // Get the size of the widest register.
5162 unsigned MaxSafeDepDist = -1U;
5163 if (Legal->getMaxSafeDepDistBytes() != -1U)
5164 MaxSafeDepDist = Legal->getMaxSafeDepDistBytes() * 8;
5165 unsigned WidestRegister =
5166 std::min(TTI.getRegisterBitWidth(true), MaxSafeDepDist);
5167 const DataLayout &DL = TheFunction->getParent()->getDataLayout();
5168
5169 SmallVector<RegisterUsage, 8> RUs(VFs.size());
5170 SmallVector<unsigned, 8> MaxUsages(VFs.size(), 0);
5171
5172 LLVM_DEBUG(dbgs() << "LV(REG): Calculating max register usage:\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("loop-vectorize")) { dbgs() << "LV(REG): Calculating max register usage:\n"
; } } while (false)
;
5173
5174 // A lambda that gets the register usage for the given type and VF.
5175 auto GetRegUsage = [&DL, WidestRegister](Type *Ty, unsigned VF) {
5176 if (Ty->isTokenTy())
5177 return 0U;
5178 unsigned TypeSize = DL.getTypeSizeInBits(Ty->getScalarType());
5179 return std::max<unsigned>(1, VF * TypeSize / WidestRegister);
5180 };
5181
5182 for (unsigned int i = 0, s = IdxToInstr.size(); i < s; ++i) {
5183 Instruction *I = IdxToInstr[i];
5184
5185 // Remove all of the instructions that end at this location.
5186 InstrList &List = TransposeEnds[i];
5187 for (Instruction *ToRemove : List)
5188 OpenIntervals.erase(ToRemove);
5189
5190 // Ignore instructions that are never used within the loop.
5191 if (Ends.find(I) == Ends.end())
5192 continue;
5193
5194 // Skip ignored values.
5195 if (ValuesToIgnore.find(I) != ValuesToIgnore.end())
5196 continue;
5197
5198 // For each VF find the maximum usage of registers.
5199 for (unsigned j = 0, e = VFs.size(); j < e; ++j) {
5200 if (VFs[j] == 1) {
5201 MaxUsages[j] = std::max(MaxUsages[j], OpenIntervals.size());
5202 continue;
5203 }
5204 collectUniformsAndScalars(VFs[j]);
5205 // Count the number of live intervals.
5206 unsigned RegUsage = 0;
5207 for (auto Inst : OpenIntervals) {
5208 // Skip ignored values for VF > 1.
5209 if (VecValuesToIgnore.find(Inst) != VecValuesToIgnore.end() ||
5210 isScalarAfterVectorization(Inst, VFs[j]))
5211 continue;
5212 RegUsage += GetRegUsage(Inst->getType(), VFs[j]);
5213 }
5214 MaxUsages[j] = std::max(MaxUsages[j], RegUsage);
5215 }
5216
5217 LLVM_DEBUG(dbgs() << "LV(REG): At #" << i << " Interval # "do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("loop-vectorize")) { dbgs() << "LV(REG): At #" <<
i << " Interval # " << OpenIntervals.size() <<
'\n'; } } while (false)
5218 << OpenIntervals.size() << '\n')do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("loop-vectorize")) { dbgs() << "LV(REG): At #" <<
i << " Interval # " << OpenIntervals.size() <<
'\n'; } } while (false)
;
5219
5220 // Add the current instruction to the list of open intervals.
5221 OpenIntervals.insert(I);
5222 }
5223
5224 for (unsigned i = 0, e = VFs.size(); i < e; ++i) {
5225 unsigned Invariant = 0;
5226 if (VFs[i] == 1)
5227 Invariant = LoopInvariants.size();
5228 else {
5229 for (auto Inst : LoopInvariants)
5230 Invariant += GetRegUsage(Inst->getType(), VFs[i]);
5231 }
5232
5233 LLVM_DEBUG(dbgs() << "LV(REG): VF = " << VFs[i] << '\n')do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("loop-vectorize")) { dbgs() << "LV(REG): VF = " <<
VFs[i] << '\n'; } } while (false)
;
5234 LLVM_DEBUG(dbgs() << "LV(REG): Found max usage: " << MaxUsages[i] << '\n')do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("loop-vectorize")) { dbgs() << "LV(REG): Found max usage: "
<< MaxUsages[i] << '\n'; } } while (false)
;
5235 LLVM_DEBUG(dbgs() << "LV(REG): Found invariant usage: " << Invariantdo { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("loop-vectorize")) { dbgs() << "LV(REG): Found invariant usage: "
<< Invariant << '\n'; } } while (false)
5236 << '\n')do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("loop-vectorize")) { dbgs() << "LV(REG): Found invariant usage: "
<< Invariant << '\n'; } } while (false)
;
5237
5238 RU.LoopInvariantRegs = Invariant;
5239 RU.MaxLocalUsers = MaxUsages[i];
5240 RUs[i] = RU;
5241 }
5242
5243 return RUs;
5244}
5245
5246bool LoopVectorizationCostModel::useEmulatedMaskMemRefHack(Instruction *I){
5247 // TODO: Cost model for emulated masked load/store is completely
5248 // broken. This hack guides the cost model to use an artificially
5249 // high enough value to practically disable vectorization with such
5250 // operations, except where previously deployed legality hack allowed
5251 // using very low cost values. This is to avoid regressions coming simply
5252 // from moving "masked load/store" check from legality to cost model.
5253 // Masked Load/Gather emulation was previously never allowed.
5254 // Limited number of Masked Store/Scatter emulation was allowed.
5255 assert(isPredicatedInst(I) && "Expecting a scalar emulated instruction")(static_cast <bool> (isPredicatedInst(I) && "Expecting a scalar emulated instruction"
) ? void (0) : __assert_fail ("isPredicatedInst(I) && \"Expecting a scalar emulated instruction\""
, "/home/username/llvm-project/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp"
, 5255, __extension__ __PRETTY_FUNCTION__))
;
5256 return isa<LoadInst>(I) ||
5257 (isa<StoreInst>(I) &&
5258 NumPredStores > NumberOfStoresToPredicate);
5259}
5260
5261void LoopVectorizationCostModel::collectInstsToScalarize(unsigned VF) {
5262 // If we aren't vectorizing the loop, or if we've already collected the
5263 // instructions to scalarize, there's nothing to do. Collection may already
5264 // have occurred if we have a user-selected VF and are now computing the
5265 // expected cost for interleaving.
5266 if (VF < 2 || InstsToScalarize.find(VF) != InstsToScalarize.end())
5267 return;
5268
5269 // Initialize a mapping for VF in InstsToScalalarize. If we find that it's
5270 // not profitable to scalarize any instructions, the presence of VF in the
5271 // map will indicate that we've analyzed it already.
5272 ScalarCostsTy &ScalarCostsVF = InstsToScalarize[VF];
5273
5274 // Find all the instructions that are scalar with predication in the loop and
5275 // determine if it would be better to not if-convert the blocks they are in.
5276 // If so, we also record the instructions to scalarize.
5277 for (BasicBlock *BB : TheLoop->blocks()) {
5278 if (!blockNeedsPredication(BB))
5279 continue;
5280 for (Instruction &I : *BB)
5281 if (isScalarWithPredication(&I)) {
5282 ScalarCostsTy ScalarCosts;
5283 // Do not apply discount logic if hacked cost is needed
5284 // for emulated masked memrefs.
5285 if (!useEmulatedMaskMemRefHack(&I) &&
5286 computePredInstDiscount(&I, ScalarCosts, VF) >= 0)
5287 ScalarCostsVF.insert(ScalarCosts.begin(), ScalarCosts.end());
5288 // Remember that BB will remain after vectorization.
5289 PredicatedBBsAfterVectorization.insert(BB);
5290 }
5291 }
5292}
5293
5294int LoopVectorizationCostModel::computePredInstDiscount(
5295 Instruction *PredInst, DenseMap<Instruction *, unsigned> &ScalarCosts,
5296 unsigned VF) {
5297 assert(!isUniformAfterVectorization(PredInst, VF) &&(static_cast <bool> (!isUniformAfterVectorization(PredInst
, VF) && "Instruction marked uniform-after-vectorization will be predicated"
) ? void (0) : __assert_fail ("!isUniformAfterVectorization(PredInst, VF) && \"Instruction marked uniform-after-vectorization will be predicated\""
, "/home/username/llvm-project/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp"
, 5298, __extension__ __PRETTY_FUNCTION__))
5298 "Instruction marked uniform-after-vectorization will be predicated")(static_cast <bool> (!isUniformAfterVectorization(PredInst
, VF) && "Instruction marked uniform-after-vectorization will be predicated"
) ? void (0) : __assert_fail ("!isUniformAfterVectorization(PredInst, VF) && \"Instruction marked uniform-after-vectorization will be predicated\""
, "/home/username/llvm-project/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp"
, 5298, __extension__ __PRETTY_FUNCTION__))
;
5299
5300 // Initialize the discount to zero, meaning that the scalar version and the
5301 // vector version cost the same.
5302 int Discount = 0;
5303
5304 // Holds instructions to analyze. The instructions we visit are mapped in
5305 // ScalarCosts. Those instructions are the ones that would be scalarized if
5306 // we find that the scalar version costs less.
5307 SmallVector<Instruction *, 8> Worklist;
5308
5309 // Returns true if the given instruction can be scalarized.
5310 auto canBeScalarized = [&](Instruction *I) -> bool {
5311 // We only attempt to scalarize instructions forming a single-use chain
5312 // from the original predicated block that would otherwise be vectorized.
5313 // Although not strictly necessary, we give up on instructions we know will
5314 // already be scalar to avoid traversing chains that are unlikely to be
5315 // beneficial.
5316 if (!I->hasOneUse() || PredInst->getParent() != I->getParent() ||
5317 isScalarAfterVectorization(I, VF))
5318 return false;
5319
5320 // If the instruction is scalar with predication, it will be analyzed
5321 // separately. We ignore it within the context of PredInst.
5322 if (isScalarWithPredication(I))
5323 return false;
5324
5325 // If any of the instruction's operands are uniform after vectorization,
5326 // the instruction cannot be scalarized. This prevents, for example, a
5327 // masked load from being scalarized.
5328 //
5329 // We assume we will only emit a value for lane zero of an instruction
5330 // marked uniform after vectorization, rather than VF identical values.
5331 // Thus, if we scalarize an instruction that uses a uniform, we would
5332 // create uses of values corresponding to the lanes we aren't emitting code
5333 // for. This behavior can be changed by allowing getScalarValue to clone
5334 // the lane zero values for uniforms rather than asserting.
5335 for (Use &U : I->operands())
5336 if (auto *J = dyn_cast<Instruction>(U.get()))
5337 if (isUniformAfterVectorization(J, VF))
5338 return false;
5339
5340 // Otherwise, we can scalarize the instruction.
5341 return true;
5342 };
5343
5344 // Returns true if an operand that cannot be scalarized must be extracted
5345 // from a vector. We will account for this scalarization overhead below. Note
5346 // that the non-void predicated instructions are placed in their own blocks,
5347 // and their return values are inserted into vectors. Thus, an extract would
5348 // still be required.
5349 auto needsExtract = [&](Instruction *I) -> bool {
5350 return TheLoop->contains(I) && !isScalarAfterVectorization(I, VF);
5351 };
5352
5353 // Compute the expected cost discount from scalarizing the entire expression
5354 // feeding the predicated instruction. We currently only consider expressions
5355 // that are single-use instruction chains.
5356 Worklist.push_back(PredInst);
5357 while (!Worklist.empty()) {
5358 Instruction *I = Worklist.pop_back_val();
5359
5360 // If we've already analyzed the instruction, there's nothing to do.
5361 if (ScalarCosts.find(I) != ScalarCosts.end())
5362 continue;
5363
5364 // Compute the cost of the vector instruction. Note that this cost already
5365 // includes the scalarization overhead of the predicated instruction.
5366 unsigned VectorCost = getInstructionCost(I, VF).first;
5367
5368 // Compute the cost of the scalarized instruction. This cost is the cost of
5369 // the instruction as if it wasn't if-converted and instead remained in the
5370 // predicated block. We will scale this cost by block probability after
5371 // computing the scalarization overhead.
5372 unsigned ScalarCost = VF * getInstructionCost(I, 1).first;
5373
5374 // Compute the scalarization overhead of needed insertelement instructions
5375 // and phi nodes.
5376 if (isScalarWithPredication(I) && !I->getType()->isVoidTy()) {
5377 ScalarCost += TTI.getScalarizationOverhead(ToVectorTy(I->getType(), VF),
5378 true, false);
5379 ScalarCost += VF * TTI.getCFInstrCost(Instruction::PHI);
5380 }
5381
5382 // Compute the scalarization overhead of needed extractelement
5383 // instructions. For each of the instruction's operands, if the operand can
5384 // be scalarized, add it to the worklist; otherwise, account for the
5385 // overhead.
5386 for (Use &U : I->operands())
5387 if (auto *J = dyn_cast<Instruction>(U.get())) {
5388 assert(VectorType::isValidElementType(J->getType()) &&(static_cast <bool> (VectorType::isValidElementType(J->
getType()) && "Instruction has non-scalar type") ? void
(0) : __assert_fail ("VectorType::isValidElementType(J->getType()) && \"Instruction has non-scalar type\""
, "/home/username/llvm-project/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp"
, 5389, __extension__ __PRETTY_FUNCTION__))
5389 "Instruction has non-scalar type")(static_cast <bool> (VectorType::isValidElementType(J->
getType()) && "Instruction has non-scalar type") ? void
(0) : __assert_fail ("VectorType::isValidElementType(J->getType()) && \"Instruction has non-scalar type\""
, "/home/username/llvm-project/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp"
, 5389, __extension__ __PRETTY_FUNCTION__))
;
5390 if (canBeScalarized(J))
5391 Worklist.push_back(J);
5392 else if (needsExtract(J))
5393 ScalarCost += TTI.getScalarizationOverhead(
5394 ToVectorTy(J->getType(),VF), false, true);
5395 }
5396
5397 // Scale the total scalar cost by block probability.
5398 ScalarCost /= getReciprocalPredBlockProb();
5399
5400 // Compute the discount. A non-negative discount means the vector version
5401 // of the instruction costs more, and scalarizing would be beneficial.
5402 Discount += VectorCost - ScalarCost;
5403 ScalarCosts[I] = ScalarCost;
5404 }
5405
5406 return Discount;
5407}
5408
5409LoopVectorizationCostModel::VectorizationCostTy
5410LoopVectorizationCostModel::expectedCost(unsigned VF) {
5411 VectorizationCostTy Cost;
5412
5413 // For each block.
5414 for (BasicBlock *BB : TheLoop->blocks()) {
5415 VectorizationCostTy BlockCost;
5416
5417 // For each instruction in the old loop.
5418 for (Instruction &I : BB->instructionsWithoutDebug()) {
5419 // Skip ignored values.
5420 if (ValuesToIgnore.find(&I) != ValuesToIgnore.end() ||
5421 (VF > 1 && VecValuesToIgnore.find(&I) != VecValuesToIgnore.end()))
5422 continue;
5423
5424 VectorizationCostTy C = getInstructionCost(&I, VF);
5425
5426 // Check if we should override the cost.
5427 if (ForceTargetInstructionCost.getNumOccurrences() > 0)