blob: 2fab03c988b2bb2ec252a742408d37aa59e2b4dc [file] [log] [blame]
David Blaikie1213dbf2015-06-26 16:57:30 +00001//===----------- VectorUtils.cpp - Vectorizer utility functions -----------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file defines vectorizer utilities.
11//
12//===----------------------------------------------------------------------===//
13
Chandler Carruth6bda14b2017-06-06 11:49:48 +000014#include "llvm/Analysis/VectorUtils.h"
James Molloy55d633b2015-10-12 12:34:45 +000015#include "llvm/ADT/EquivalenceClasses.h"
16#include "llvm/Analysis/DemandedBits.h"
Hal Finkel9cf58c42015-07-11 10:52:42 +000017#include "llvm/Analysis/LoopInfo.h"
Florian Hahn1086ce22018-09-12 08:01:57 +000018#include "llvm/Analysis/LoopIterator.h"
Hal Finkel9cf58c42015-07-11 10:52:42 +000019#include "llvm/Analysis/ScalarEvolution.h"
Chandler Carruth6bda14b2017-06-06 11:49:48 +000020#include "llvm/Analysis/ScalarEvolutionExpressions.h"
James Molloy55d633b2015-10-12 12:34:45 +000021#include "llvm/Analysis/TargetTransformInfo.h"
David Majnemerb4b27232016-04-19 19:10:21 +000022#include "llvm/Analysis/ValueTracking.h"
Chandler Carruth6bda14b2017-06-06 11:49:48 +000023#include "llvm/IR/Constants.h"
Hal Finkel9cf58c42015-07-11 10:52:42 +000024#include "llvm/IR/GetElementPtrTypeIterator.h"
Chandler Carruth6bda14b2017-06-06 11:49:48 +000025#include "llvm/IR/IRBuilder.h"
Hal Finkel9cf58c42015-07-11 10:52:42 +000026#include "llvm/IR/PatternMatch.h"
27#include "llvm/IR/Value.h"
Renato Golin3b1d3b02015-08-30 10:49:04 +000028
Florian Hahn1086ce22018-09-12 08:01:57 +000029#define DEBUG_TYPE "vectorutils"
30
David Majnemer5eaf08f2015-08-18 22:07:20 +000031using namespace llvm;
32using namespace llvm::PatternMatch;
David Blaikie1213dbf2015-06-26 16:57:30 +000033
Florian Hahn1086ce22018-09-12 08:01:57 +000034/// Maximum factor for an interleaved memory access.
35static cl::opt<unsigned> MaxInterleaveGroupFactor(
36 "max-interleave-group-factor", cl::Hidden,
37 cl::desc("Maximum factor for an interleaved access group (default = 8)"),
38 cl::init(8));
39
Sanjay Patel0f4f48062018-11-12 15:10:30 +000040/// Return true if all of the intrinsic's arguments and return type are scalars
41/// for the scalar form of the intrinsic and vectors for the vector form of the
42/// intrinsic.
David Blaikie1213dbf2015-06-26 16:57:30 +000043bool llvm::isTriviallyVectorizable(Intrinsic::ID ID) {
44 switch (ID) {
Sanjay Patel0f4f48062018-11-12 15:10:30 +000045 case Intrinsic::bswap: // Begin integer bit-manipulation.
46 case Intrinsic::bitreverse:
47 case Intrinsic::ctpop:
48 case Intrinsic::ctlz:
49 case Intrinsic::cttz:
50 case Intrinsic::sqrt: // Begin floating-point.
David Blaikie1213dbf2015-06-26 16:57:30 +000051 case Intrinsic::sin:
52 case Intrinsic::cos:
53 case Intrinsic::exp:
54 case Intrinsic::exp2:
55 case Intrinsic::log:
56 case Intrinsic::log10:
57 case Intrinsic::log2:
58 case Intrinsic::fabs:
59 case Intrinsic::minnum:
60 case Intrinsic::maxnum:
Thomas Lively8a91cf12018-10-19 21:11:43 +000061 case Intrinsic::minimum:
62 case Intrinsic::maximum:
David Blaikie1213dbf2015-06-26 16:57:30 +000063 case Intrinsic::copysign:
64 case Intrinsic::floor:
65 case Intrinsic::ceil:
66 case Intrinsic::trunc:
67 case Intrinsic::rint:
68 case Intrinsic::nearbyint:
69 case Intrinsic::round:
David Blaikie1213dbf2015-06-26 16:57:30 +000070 case Intrinsic::pow:
71 case Intrinsic::fma:
72 case Intrinsic::fmuladd:
David Blaikie1213dbf2015-06-26 16:57:30 +000073 case Intrinsic::powi:
Matt Arsenault80ea6dd2018-09-17 13:24:30 +000074 case Intrinsic::canonicalize:
David Blaikie1213dbf2015-06-26 16:57:30 +000075 return true;
76 default:
77 return false;
78 }
79}
80
Adrian Prantl5f8f34e42018-05-01 15:54:18 +000081/// Identifies if the intrinsic has a scalar operand. It check for
David Blaikie1213dbf2015-06-26 16:57:30 +000082/// ctlz,cttz and powi special intrinsics whose argument is scalar.
83bool llvm::hasVectorInstrinsicScalarOpd(Intrinsic::ID ID,
84 unsigned ScalarOpdIdx) {
85 switch (ID) {
86 case Intrinsic::ctlz:
87 case Intrinsic::cttz:
88 case Intrinsic::powi:
89 return (ScalarOpdIdx == 1);
90 default:
91 return false;
92 }
93}
94
Adrian Prantl5f8f34e42018-05-01 15:54:18 +000095/// Returns intrinsic ID for call.
David Blaikie1213dbf2015-06-26 16:57:30 +000096/// For the input call instruction it finds mapping intrinsic and returns
97/// its ID, in case it does not found it return not_intrinsic.
David Majnemerb4b27232016-04-19 19:10:21 +000098Intrinsic::ID llvm::getVectorIntrinsicIDForCall(const CallInst *CI,
99 const TargetLibraryInfo *TLI) {
100 Intrinsic::ID ID = getIntrinsicForCallSite(CI, TLI);
101 if (ID == Intrinsic::not_intrinsic)
David Blaikie1213dbf2015-06-26 16:57:30 +0000102 return Intrinsic::not_intrinsic;
103
David Majnemerb4b27232016-04-19 19:10:21 +0000104 if (isTriviallyVectorizable(ID) || ID == Intrinsic::lifetime_start ||
Dan Gohman2c74fe92017-11-08 21:59:51 +0000105 ID == Intrinsic::lifetime_end || ID == Intrinsic::assume ||
106 ID == Intrinsic::sideeffect)
David Majnemerb4b27232016-04-19 19:10:21 +0000107 return ID;
David Blaikie1213dbf2015-06-26 16:57:30 +0000108 return Intrinsic::not_intrinsic;
109}
Hal Finkel9cf58c42015-07-11 10:52:42 +0000110
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000111/// Find the operand of the GEP that should be checked for consecutive
Hal Finkel9cf58c42015-07-11 10:52:42 +0000112/// stores. This ignores trailing indices that have no effect on the final
113/// pointer.
114unsigned llvm::getGEPInductionOperand(const GetElementPtrInst *Gep) {
115 const DataLayout &DL = Gep->getModule()->getDataLayout();
116 unsigned LastOperand = Gep->getNumOperands() - 1;
Eduard Burtescu19eb0312016-01-19 17:28:00 +0000117 unsigned GEPAllocSize = DL.getTypeAllocSize(Gep->getResultElementType());
Hal Finkel9cf58c42015-07-11 10:52:42 +0000118
119 // Walk backwards and try to peel off zeros.
David Majnemer5eaf08f2015-08-18 22:07:20 +0000120 while (LastOperand > 1 && match(Gep->getOperand(LastOperand), m_Zero())) {
Hal Finkel9cf58c42015-07-11 10:52:42 +0000121 // Find the type we're currently indexing into.
122 gep_type_iterator GEPTI = gep_type_begin(Gep);
Peter Collingbourneab85225b2016-12-02 02:24:42 +0000123 std::advance(GEPTI, LastOperand - 2);
Hal Finkel9cf58c42015-07-11 10:52:42 +0000124
125 // If it's a type with the same allocation size as the result of the GEP we
126 // can peel off the zero index.
Peter Collingbourneab85225b2016-12-02 02:24:42 +0000127 if (DL.getTypeAllocSize(GEPTI.getIndexedType()) != GEPAllocSize)
Hal Finkel9cf58c42015-07-11 10:52:42 +0000128 break;
129 --LastOperand;
130 }
131
132 return LastOperand;
133}
134
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000135/// If the argument is a GEP, then returns the operand identified by
Hal Finkel9cf58c42015-07-11 10:52:42 +0000136/// getGEPInductionOperand. However, if there is some other non-loop-invariant
137/// operand, it returns that instead.
David Majnemer5eaf08f2015-08-18 22:07:20 +0000138Value *llvm::stripGetElementPtr(Value *Ptr, ScalarEvolution *SE, Loop *Lp) {
Hal Finkel9cf58c42015-07-11 10:52:42 +0000139 GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(Ptr);
140 if (!GEP)
141 return Ptr;
142
143 unsigned InductionOperand = getGEPInductionOperand(GEP);
144
145 // Check that all of the gep indices are uniform except for our induction
146 // operand.
147 for (unsigned i = 0, e = GEP->getNumOperands(); i != e; ++i)
148 if (i != InductionOperand &&
149 !SE->isLoopInvariant(SE->getSCEV(GEP->getOperand(i)), Lp))
150 return Ptr;
151 return GEP->getOperand(InductionOperand);
152}
153
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000154/// If a value has only one user that is a CastInst, return it.
David Majnemer5eaf08f2015-08-18 22:07:20 +0000155Value *llvm::getUniqueCastUse(Value *Ptr, Loop *Lp, Type *Ty) {
156 Value *UniqueCast = nullptr;
Hal Finkel9cf58c42015-07-11 10:52:42 +0000157 for (User *U : Ptr->users()) {
158 CastInst *CI = dyn_cast<CastInst>(U);
159 if (CI && CI->getType() == Ty) {
160 if (!UniqueCast)
161 UniqueCast = CI;
162 else
163 return nullptr;
164 }
165 }
166 return UniqueCast;
167}
168
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000169/// Get the stride of a pointer access in a loop. Looks for symbolic
Hal Finkel9cf58c42015-07-11 10:52:42 +0000170/// strides "a[i*stride]". Returns the symbolic stride, or null otherwise.
David Majnemer5eaf08f2015-08-18 22:07:20 +0000171Value *llvm::getStrideFromPointer(Value *Ptr, ScalarEvolution *SE, Loop *Lp) {
Craig Toppere3dcce92015-08-01 22:20:21 +0000172 auto *PtrTy = dyn_cast<PointerType>(Ptr->getType());
Hal Finkel9cf58c42015-07-11 10:52:42 +0000173 if (!PtrTy || PtrTy->isAggregateType())
174 return nullptr;
175
176 // Try to remove a gep instruction to make the pointer (actually index at this
Vedant Kumard3196742018-02-28 19:08:52 +0000177 // point) easier analyzable. If OrigPtr is equal to Ptr we are analyzing the
Hal Finkel9cf58c42015-07-11 10:52:42 +0000178 // pointer, otherwise, we are analyzing the index.
David Majnemer5eaf08f2015-08-18 22:07:20 +0000179 Value *OrigPtr = Ptr;
Hal Finkel9cf58c42015-07-11 10:52:42 +0000180
181 // The size of the pointer access.
182 int64_t PtrAccessSize = 1;
183
184 Ptr = stripGetElementPtr(Ptr, SE, Lp);
185 const SCEV *V = SE->getSCEV(Ptr);
186
187 if (Ptr != OrigPtr)
188 // Strip off casts.
189 while (const SCEVCastExpr *C = dyn_cast<SCEVCastExpr>(V))
190 V = C->getOperand();
191
192 const SCEVAddRecExpr *S = dyn_cast<SCEVAddRecExpr>(V);
193 if (!S)
194 return nullptr;
195
196 V = S->getStepRecurrence(*SE);
197 if (!V)
198 return nullptr;
199
200 // Strip off the size of access multiplication if we are still analyzing the
201 // pointer.
202 if (OrigPtr == Ptr) {
Hal Finkel9cf58c42015-07-11 10:52:42 +0000203 if (const SCEVMulExpr *M = dyn_cast<SCEVMulExpr>(V)) {
204 if (M->getOperand(0)->getSCEVType() != scConstant)
205 return nullptr;
206
Sanjoy Das0de2fec2015-12-17 20:28:46 +0000207 const APInt &APStepVal = cast<SCEVConstant>(M->getOperand(0))->getAPInt();
Hal Finkel9cf58c42015-07-11 10:52:42 +0000208
209 // Huge step value - give up.
210 if (APStepVal.getBitWidth() > 64)
211 return nullptr;
212
213 int64_t StepVal = APStepVal.getSExtValue();
214 if (PtrAccessSize != StepVal)
215 return nullptr;
216 V = M->getOperand(1);
217 }
218 }
219
220 // Strip off casts.
221 Type *StripedOffRecurrenceCast = nullptr;
222 if (const SCEVCastExpr *C = dyn_cast<SCEVCastExpr>(V)) {
223 StripedOffRecurrenceCast = C->getType();
224 V = C->getOperand();
225 }
226
227 // Look for the loop invariant symbolic value.
228 const SCEVUnknown *U = dyn_cast<SCEVUnknown>(V);
229 if (!U)
230 return nullptr;
231
David Majnemer5eaf08f2015-08-18 22:07:20 +0000232 Value *Stride = U->getValue();
Hal Finkel9cf58c42015-07-11 10:52:42 +0000233 if (!Lp->isLoopInvariant(Stride))
234 return nullptr;
235
236 // If we have stripped off the recurrence cast we have to make sure that we
237 // return the value that is used in this loop so that we can replace it later.
238 if (StripedOffRecurrenceCast)
239 Stride = getUniqueCastUse(Stride, Lp, StripedOffRecurrenceCast);
240
241 return Stride;
242}
David Majnemer599ca442015-07-13 01:15:53 +0000243
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000244/// Given a vector and an element number, see if the scalar value is
David Majnemer599ca442015-07-13 01:15:53 +0000245/// already around as a register, for example if it were inserted then extracted
246/// from the vector.
David Majnemer5eaf08f2015-08-18 22:07:20 +0000247Value *llvm::findScalarElement(Value *V, unsigned EltNo) {
David Majnemer599ca442015-07-13 01:15:53 +0000248 assert(V->getType()->isVectorTy() && "Not looking at a vector?");
249 VectorType *VTy = cast<VectorType>(V->getType());
250 unsigned Width = VTy->getNumElements();
251 if (EltNo >= Width) // Out of range access.
252 return UndefValue::get(VTy->getElementType());
253
254 if (Constant *C = dyn_cast<Constant>(V))
255 return C->getAggregateElement(EltNo);
256
257 if (InsertElementInst *III = dyn_cast<InsertElementInst>(V)) {
258 // If this is an insert to a variable element, we don't know what it is.
259 if (!isa<ConstantInt>(III->getOperand(2)))
260 return nullptr;
261 unsigned IIElt = cast<ConstantInt>(III->getOperand(2))->getZExtValue();
262
263 // If this is an insert to the element we are looking for, return the
264 // inserted value.
265 if (EltNo == IIElt)
266 return III->getOperand(1);
267
268 // Otherwise, the insertelement doesn't modify the value, recurse on its
269 // vector input.
270 return findScalarElement(III->getOperand(0), EltNo);
271 }
272
273 if (ShuffleVectorInst *SVI = dyn_cast<ShuffleVectorInst>(V)) {
274 unsigned LHSWidth = SVI->getOperand(0)->getType()->getVectorNumElements();
275 int InEl = SVI->getMaskValue(EltNo);
276 if (InEl < 0)
277 return UndefValue::get(VTy->getElementType());
278 if (InEl < (int)LHSWidth)
279 return findScalarElement(SVI->getOperand(0), InEl);
280 return findScalarElement(SVI->getOperand(1), InEl - LHSWidth);
281 }
282
283 // Extract a value from a vector add operation with a constant zero.
Sanjay Patel3413a662018-09-24 17:18:32 +0000284 // TODO: Use getBinOpIdentity() to generalize this.
285 Value *Val; Constant *C;
286 if (match(V, m_Add(m_Value(Val), m_Constant(C))))
287 if (Constant *Elt = C->getAggregateElement(EltNo))
David Majnemerc6bb0e22015-08-18 22:07:25 +0000288 if (Elt->isNullValue())
289 return findScalarElement(Val, EltNo);
David Majnemer599ca442015-07-13 01:15:53 +0000290
291 // Otherwise, we don't know.
292 return nullptr;
293}
Renato Golin3b1d3b02015-08-30 10:49:04 +0000294
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000295/// Get splat value if the input is a splat vector or return nullptr.
Elena Demikhovsky63a7ca92015-08-30 13:48:02 +0000296/// This function is not fully general. It checks only 2 cases:
297/// the input value is (1) a splat constants vector or (2) a sequence
298/// of instructions that broadcast a single value into a vector.
299///
Elena Demikhovsky0781d7b2015-12-01 12:08:36 +0000300const llvm::Value *llvm::getSplatValue(const Value *V) {
301
302 if (auto *C = dyn_cast<Constant>(V))
Elena Demikhovsky47fa2712015-12-01 12:30:40 +0000303 if (isa<VectorType>(V->getType()))
304 return C->getSplatValue();
Elena Demikhovsky63a7ca92015-08-30 13:48:02 +0000305
306 auto *ShuffleInst = dyn_cast<ShuffleVectorInst>(V);
Renato Golin3b1d3b02015-08-30 10:49:04 +0000307 if (!ShuffleInst)
308 return nullptr;
Elena Demikhovsky63a7ca92015-08-30 13:48:02 +0000309 // All-zero (or undef) shuffle mask elements.
310 for (int MaskElt : ShuffleInst->getShuffleMask())
311 if (MaskElt != 0 && MaskElt != -1)
Renato Golin3b1d3b02015-08-30 10:49:04 +0000312 return nullptr;
313 // The first shuffle source is 'insertelement' with index 0.
Elena Demikhovsky63a7ca92015-08-30 13:48:02 +0000314 auto *InsertEltInst =
315 dyn_cast<InsertElementInst>(ShuffleInst->getOperand(0));
Renato Golin3b1d3b02015-08-30 10:49:04 +0000316 if (!InsertEltInst || !isa<ConstantInt>(InsertEltInst->getOperand(2)) ||
Craig Topper79ab6432017-07-06 18:39:47 +0000317 !cast<ConstantInt>(InsertEltInst->getOperand(2))->isZero())
Renato Golin3b1d3b02015-08-30 10:49:04 +0000318 return nullptr;
319
320 return InsertEltInst->getOperand(1);
321}
James Molloy55d633b2015-10-12 12:34:45 +0000322
Charlie Turner54336a52015-11-26 20:39:51 +0000323MapVector<Instruction *, uint64_t>
James Molloy45f67d52015-11-09 14:32:05 +0000324llvm::computeMinimumValueSizes(ArrayRef<BasicBlock *> Blocks, DemandedBits &DB,
325 const TargetTransformInfo *TTI) {
James Molloy55d633b2015-10-12 12:34:45 +0000326
327 // DemandedBits will give us every value's live-out bits. But we want
328 // to ensure no extra casts would need to be inserted, so every DAG
329 // of connected values must have the same minimum bitwidth.
James Molloy45f67d52015-11-09 14:32:05 +0000330 EquivalenceClasses<Value *> ECs;
331 SmallVector<Value *, 16> Worklist;
332 SmallPtrSet<Value *, 4> Roots;
333 SmallPtrSet<Value *, 16> Visited;
334 DenseMap<Value *, uint64_t> DBits;
335 SmallPtrSet<Instruction *, 4> InstructionSet;
Charlie Turner54336a52015-11-26 20:39:51 +0000336 MapVector<Instruction *, uint64_t> MinBWs;
James Molloy45f67d52015-11-09 14:32:05 +0000337
James Molloy55d633b2015-10-12 12:34:45 +0000338 // Determine the roots. We work bottom-up, from truncs or icmps.
339 bool SeenExtFromIllegalType = false;
340 for (auto *BB : Blocks)
341 for (auto &I : *BB) {
342 InstructionSet.insert(&I);
343
344 if (TTI && (isa<ZExtInst>(&I) || isa<SExtInst>(&I)) &&
345 !TTI->isTypeLegal(I.getOperand(0)->getType()))
346 SeenExtFromIllegalType = true;
James Molloy45f67d52015-11-09 14:32:05 +0000347
James Molloy55d633b2015-10-12 12:34:45 +0000348 // Only deal with non-vector integers up to 64-bits wide.
349 if ((isa<TruncInst>(&I) || isa<ICmpInst>(&I)) &&
350 !I.getType()->isVectorTy() &&
351 I.getOperand(0)->getType()->getScalarSizeInBits() <= 64) {
352 // Don't make work for ourselves. If we know the loaded type is legal,
353 // don't add it to the worklist.
354 if (TTI && isa<TruncInst>(&I) && TTI->isTypeLegal(I.getType()))
355 continue;
James Molloy45f67d52015-11-09 14:32:05 +0000356
James Molloy55d633b2015-10-12 12:34:45 +0000357 Worklist.push_back(&I);
358 Roots.insert(&I);
359 }
360 }
361 // Early exit.
362 if (Worklist.empty() || (TTI && !SeenExtFromIllegalType))
363 return MinBWs;
James Molloy45f67d52015-11-09 14:32:05 +0000364
James Molloy55d633b2015-10-12 12:34:45 +0000365 // Now proceed breadth-first, unioning values together.
366 while (!Worklist.empty()) {
367 Value *Val = Worklist.pop_back_val();
368 Value *Leader = ECs.getOrInsertLeaderValue(Val);
James Molloy45f67d52015-11-09 14:32:05 +0000369
James Molloy55d633b2015-10-12 12:34:45 +0000370 if (Visited.count(Val))
371 continue;
372 Visited.insert(Val);
373
374 // Non-instructions terminate a chain successfully.
375 if (!isa<Instruction>(Val))
376 continue;
377 Instruction *I = cast<Instruction>(Val);
378
379 // If we encounter a type that is larger than 64 bits, we can't represent
380 // it so bail out.
James Molloyaa1d6382016-05-10 12:27:23 +0000381 if (DB.getDemandedBits(I).getBitWidth() > 64)
Charlie Turner54336a52015-11-26 20:39:51 +0000382 return MapVector<Instruction *, uint64_t>();
James Molloy45f67d52015-11-09 14:32:05 +0000383
James Molloyaa1d6382016-05-10 12:27:23 +0000384 uint64_t V = DB.getDemandedBits(I).getZExtValue();
385 DBits[Leader] |= V;
386 DBits[I] = V;
James Molloy45f67d52015-11-09 14:32:05 +0000387
James Molloy55d633b2015-10-12 12:34:45 +0000388 // Casts, loads and instructions outside of our range terminate a chain
389 // successfully.
390 if (isa<SExtInst>(I) || isa<ZExtInst>(I) || isa<LoadInst>(I) ||
391 !InstructionSet.count(I))
392 continue;
393
394 // Unsafe casts terminate a chain unsuccessfully. We can't do anything
395 // useful with bitcasts, ptrtoints or inttoptrs and it'd be unsafe to
396 // transform anything that relies on them.
397 if (isa<BitCastInst>(I) || isa<PtrToIntInst>(I) || isa<IntToPtrInst>(I) ||
398 !I->getType()->isIntegerTy()) {
399 DBits[Leader] |= ~0ULL;
400 continue;
401 }
402
403 // We don't modify the types of PHIs. Reductions will already have been
404 // truncated if possible, and inductions' sizes will have been chosen by
405 // indvars.
406 if (isa<PHINode>(I))
407 continue;
408
409 if (DBits[Leader] == ~0ULL)
410 // All bits demanded, no point continuing.
411 continue;
412
413 for (Value *O : cast<User>(I)->operands()) {
414 ECs.unionSets(Leader, O);
415 Worklist.push_back(O);
416 }
417 }
418
419 // Now we've discovered all values, walk them to see if there are
420 // any users we didn't see. If there are, we can't optimize that
421 // chain.
422 for (auto &I : DBits)
423 for (auto *U : I.first->users())
424 if (U->getType()->isIntegerTy() && DBits.count(U) == 0)
425 DBits[ECs.getOrInsertLeaderValue(I.first)] |= ~0ULL;
James Molloy45f67d52015-11-09 14:32:05 +0000426
James Molloy55d633b2015-10-12 12:34:45 +0000427 for (auto I = ECs.begin(), E = ECs.end(); I != E; ++I) {
428 uint64_t LeaderDemandedBits = 0;
429 for (auto MI = ECs.member_begin(I), ME = ECs.member_end(); MI != ME; ++MI)
430 LeaderDemandedBits |= DBits[*MI];
431
432 uint64_t MinBW = (sizeof(LeaderDemandedBits) * 8) -
433 llvm::countLeadingZeros(LeaderDemandedBits);
434 // Round up to a power of 2
435 if (!isPowerOf2_64((uint64_t)MinBW))
436 MinBW = NextPowerOf2(MinBW);
James Molloy8e46cd02016-03-30 10:11:43 +0000437
438 // We don't modify the types of PHIs. Reductions will already have been
439 // truncated if possible, and inductions' sizes will have been chosen by
440 // indvars.
441 // If we are required to shrink a PHI, abandon this entire equivalence class.
442 bool Abort = false;
443 for (auto MI = ECs.member_begin(I), ME = ECs.member_end(); MI != ME; ++MI)
444 if (isa<PHINode>(*MI) && MinBW < (*MI)->getType()->getScalarSizeInBits()) {
445 Abort = true;
446 break;
447 }
448 if (Abort)
449 continue;
450
James Molloy55d633b2015-10-12 12:34:45 +0000451 for (auto MI = ECs.member_begin(I), ME = ECs.member_end(); MI != ME; ++MI) {
452 if (!isa<Instruction>(*MI))
453 continue;
454 Type *Ty = (*MI)->getType();
455 if (Roots.count(*MI))
456 Ty = cast<Instruction>(*MI)->getOperand(0)->getType();
457 if (MinBW < Ty->getScalarSizeInBits())
458 MinBWs[cast<Instruction>(*MI)] = MinBW;
459 }
460 }
461
462 return MinBWs;
463}
Matt Arsenault727e2792016-06-30 21:17:59 +0000464
465/// \returns \p I after propagating metadata from \p VL.
466Instruction *llvm::propagateMetadata(Instruction *Inst, ArrayRef<Value *> VL) {
467 Instruction *I0 = cast<Instruction>(VL[0]);
468 SmallVector<std::pair<unsigned, MDNode *>, 4> Metadata;
469 I0->getAllMetadataOtherThanDebugLoc(Metadata);
470
Justin Lebar11a32042016-09-11 01:39:08 +0000471 for (auto Kind :
472 {LLVMContext::MD_tbaa, LLVMContext::MD_alias_scope,
473 LLVMContext::MD_noalias, LLVMContext::MD_fpmath,
474 LLVMContext::MD_nontemporal, LLVMContext::MD_invariant_load}) {
Matt Arsenault727e2792016-06-30 21:17:59 +0000475 MDNode *MD = I0->getMetadata(Kind);
476
477 for (int J = 1, E = VL.size(); MD && J != E; ++J) {
478 const Instruction *IJ = cast<Instruction>(VL[J]);
479 MDNode *IMD = IJ->getMetadata(Kind);
480 switch (Kind) {
481 case LLVMContext::MD_tbaa:
482 MD = MDNode::getMostGenericTBAA(MD, IMD);
483 break;
484 case LLVMContext::MD_alias_scope:
485 MD = MDNode::getMostGenericAliasScope(MD, IMD);
486 break;
Matt Arsenault727e2792016-06-30 21:17:59 +0000487 case LLVMContext::MD_fpmath:
488 MD = MDNode::getMostGenericFPMath(MD, IMD);
489 break;
Justin Lebar11a32042016-09-11 01:39:08 +0000490 case LLVMContext::MD_noalias:
Matt Arsenault727e2792016-06-30 21:17:59 +0000491 case LLVMContext::MD_nontemporal:
Justin Lebar11a32042016-09-11 01:39:08 +0000492 case LLVMContext::MD_invariant_load:
Matt Arsenault727e2792016-06-30 21:17:59 +0000493 MD = MDNode::intersect(MD, IMD);
494 break;
495 default:
496 llvm_unreachable("unhandled metadata");
497 }
498 }
499
500 Inst->setMetadata(Kind, MD);
501 }
502
503 return Inst;
504}
Matthew Simpsonba5cf9d2017-02-01 17:45:46 +0000505
Dorit Nuzman34da6dd2018-10-31 09:57:56 +0000506Constant *llvm::createBitMaskForGaps(IRBuilder<> &Builder, unsigned VF,
507 const InterleaveGroup &Group) {
508 // All 1's means mask is not needed.
509 if (Group.getNumMembers() == Group.getFactor())
510 return nullptr;
511
512 // TODO: support reversed access.
513 assert(!Group.isReverse() && "Reversed group not supported.");
514
515 SmallVector<Constant *, 16> Mask;
516 for (unsigned i = 0; i < VF; i++)
517 for (unsigned j = 0; j < Group.getFactor(); ++j) {
518 unsigned HasMember = Group.getMember(j) ? 1 : 0;
519 Mask.push_back(Builder.getInt1(HasMember));
520 }
521
522 return ConstantVector::get(Mask);
523}
524
Dorit Nuzman38bbf812018-10-14 08:50:06 +0000525Constant *llvm::createReplicatedMask(IRBuilder<> &Builder,
526 unsigned ReplicationFactor, unsigned VF) {
527 SmallVector<Constant *, 16> MaskVec;
528 for (unsigned i = 0; i < VF; i++)
529 for (unsigned j = 0; j < ReplicationFactor; j++)
530 MaskVec.push_back(Builder.getInt32(i));
531
532 return ConstantVector::get(MaskVec);
533}
534
Matthew Simpsonba5cf9d2017-02-01 17:45:46 +0000535Constant *llvm::createInterleaveMask(IRBuilder<> &Builder, unsigned VF,
536 unsigned NumVecs) {
537 SmallVector<Constant *, 16> Mask;
538 for (unsigned i = 0; i < VF; i++)
539 for (unsigned j = 0; j < NumVecs; j++)
540 Mask.push_back(Builder.getInt32(j * VF + i));
541
542 return ConstantVector::get(Mask);
543}
544
545Constant *llvm::createStrideMask(IRBuilder<> &Builder, unsigned Start,
546 unsigned Stride, unsigned VF) {
547 SmallVector<Constant *, 16> Mask;
548 for (unsigned i = 0; i < VF; i++)
549 Mask.push_back(Builder.getInt32(Start + i * Stride));
550
551 return ConstantVector::get(Mask);
552}
553
554Constant *llvm::createSequentialMask(IRBuilder<> &Builder, unsigned Start,
555 unsigned NumInts, unsigned NumUndefs) {
556 SmallVector<Constant *, 16> Mask;
557 for (unsigned i = 0; i < NumInts; i++)
558 Mask.push_back(Builder.getInt32(Start + i));
559
560 Constant *Undef = UndefValue::get(Builder.getInt32Ty());
561 for (unsigned i = 0; i < NumUndefs; i++)
562 Mask.push_back(Undef);
563
564 return ConstantVector::get(Mask);
565}
566
567/// A helper function for concatenating vectors. This function concatenates two
568/// vectors having the same element type. If the second vector has fewer
569/// elements than the first, it is padded with undefs.
570static Value *concatenateTwoVectors(IRBuilder<> &Builder, Value *V1,
571 Value *V2) {
572 VectorType *VecTy1 = dyn_cast<VectorType>(V1->getType());
573 VectorType *VecTy2 = dyn_cast<VectorType>(V2->getType());
574 assert(VecTy1 && VecTy2 &&
575 VecTy1->getScalarType() == VecTy2->getScalarType() &&
576 "Expect two vectors with the same element type");
577
578 unsigned NumElts1 = VecTy1->getNumElements();
579 unsigned NumElts2 = VecTy2->getNumElements();
580 assert(NumElts1 >= NumElts2 && "Unexpect the first vector has less elements");
581
582 if (NumElts1 > NumElts2) {
583 // Extend with UNDEFs.
584 Constant *ExtMask =
585 createSequentialMask(Builder, 0, NumElts2, NumElts1 - NumElts2);
586 V2 = Builder.CreateShuffleVector(V2, UndefValue::get(VecTy2), ExtMask);
587 }
588
589 Constant *Mask = createSequentialMask(Builder, 0, NumElts1 + NumElts2, 0);
590 return Builder.CreateShuffleVector(V1, V2, Mask);
591}
592
593Value *llvm::concatenateVectors(IRBuilder<> &Builder, ArrayRef<Value *> Vecs) {
594 unsigned NumVecs = Vecs.size();
595 assert(NumVecs > 1 && "Should be at least two vectors");
596
597 SmallVector<Value *, 8> ResList;
598 ResList.append(Vecs.begin(), Vecs.end());
599 do {
600 SmallVector<Value *, 8> TmpList;
601 for (unsigned i = 0; i < NumVecs - 1; i += 2) {
602 Value *V0 = ResList[i], *V1 = ResList[i + 1];
603 assert((V0->getType() == V1->getType() || i == NumVecs - 2) &&
604 "Only the last vector may have a different type");
605
606 TmpList.push_back(concatenateTwoVectors(Builder, V0, V1));
607 }
608
609 // Push the last vector if the total number of vectors is odd.
610 if (NumVecs % 2 != 0)
611 TmpList.push_back(ResList[NumVecs - 1]);
612
613 ResList = TmpList;
614 NumVecs = ResList.size();
615 } while (NumVecs > 1);
616
617 return ResList[0];
618}
Florian Hahn1086ce22018-09-12 08:01:57 +0000619
620bool InterleavedAccessInfo::isStrided(int Stride) {
621 unsigned Factor = std::abs(Stride);
622 return Factor >= 2 && Factor <= MaxInterleaveGroupFactor;
623}
624
625void InterleavedAccessInfo::collectConstStrideAccesses(
626 MapVector<Instruction *, StrideDescriptor> &AccessStrideInfo,
627 const ValueToValueMap &Strides) {
628 auto &DL = TheLoop->getHeader()->getModule()->getDataLayout();
629
630 // Since it's desired that the load/store instructions be maintained in
631 // "program order" for the interleaved access analysis, we have to visit the
632 // blocks in the loop in reverse postorder (i.e., in a topological order).
633 // Such an ordering will ensure that any load/store that may be executed
634 // before a second load/store will precede the second load/store in
635 // AccessStrideInfo.
636 LoopBlocksDFS DFS(TheLoop);
637 DFS.perform(LI);
638 for (BasicBlock *BB : make_range(DFS.beginRPO(), DFS.endRPO()))
639 for (auto &I : *BB) {
640 auto *LI = dyn_cast<LoadInst>(&I);
641 auto *SI = dyn_cast<StoreInst>(&I);
642 if (!LI && !SI)
643 continue;
644
645 Value *Ptr = getLoadStorePointerOperand(&I);
646 // We don't check wrapping here because we don't know yet if Ptr will be
647 // part of a full group or a group with gaps. Checking wrapping for all
648 // pointers (even those that end up in groups with no gaps) will be overly
649 // conservative. For full groups, wrapping should be ok since if we would
650 // wrap around the address space we would do a memory access at nullptr
651 // even without the transformation. The wrapping checks are therefore
652 // deferred until after we've formed the interleaved groups.
653 int64_t Stride = getPtrStride(PSE, Ptr, TheLoop, Strides,
654 /*Assume=*/true, /*ShouldCheckWrap=*/false);
655
656 const SCEV *Scev = replaceSymbolicStrideSCEV(PSE, Strides, Ptr);
657 PointerType *PtrTy = dyn_cast<PointerType>(Ptr->getType());
658 uint64_t Size = DL.getTypeAllocSize(PtrTy->getElementType());
659
660 // An alignment of 0 means target ABI alignment.
661 unsigned Align = getLoadStoreAlignment(&I);
662 if (!Align)
663 Align = DL.getABITypeAlignment(PtrTy->getElementType());
664
665 AccessStrideInfo[&I] = StrideDescriptor(Stride, Scev, Size, Align);
666 }
667}
668
669// Analyze interleaved accesses and collect them into interleaved load and
670// store groups.
671//
672// When generating code for an interleaved load group, we effectively hoist all
673// loads in the group to the location of the first load in program order. When
674// generating code for an interleaved store group, we sink all stores to the
675// location of the last store. This code motion can change the order of load
676// and store instructions and may break dependences.
677//
678// The code generation strategy mentioned above ensures that we won't violate
679// any write-after-read (WAR) dependences.
680//
681// E.g., for the WAR dependence: a = A[i]; // (1)
682// A[i] = b; // (2)
683//
684// The store group of (2) is always inserted at or below (2), and the load
685// group of (1) is always inserted at or above (1). Thus, the instructions will
686// never be reordered. All other dependences are checked to ensure the
687// correctness of the instruction reordering.
688//
689// The algorithm visits all memory accesses in the loop in bottom-up program
690// order. Program order is established by traversing the blocks in the loop in
691// reverse postorder when collecting the accesses.
692//
693// We visit the memory accesses in bottom-up order because it can simplify the
694// construction of store groups in the presence of write-after-write (WAW)
695// dependences.
696//
697// E.g., for the WAW dependence: A[i] = a; // (1)
698// A[i] = b; // (2)
699// A[i + 1] = c; // (3)
700//
701// We will first create a store group with (3) and (2). (1) can't be added to
702// this group because it and (2) are dependent. However, (1) can be grouped
703// with other accesses that may precede it in program order. Note that a
704// bottom-up order does not imply that WAW dependences should not be checked.
Dorit Nuzman38bbf812018-10-14 08:50:06 +0000705void InterleavedAccessInfo::analyzeInterleaving(
706 bool EnablePredicatedInterleavedMemAccesses) {
Florian Hahn1086ce22018-09-12 08:01:57 +0000707 LLVM_DEBUG(dbgs() << "LV: Analyzing interleaved accesses...\n");
708 const ValueToValueMap &Strides = LAI->getSymbolicStrides();
709
710 // Holds all accesses with a constant stride.
711 MapVector<Instruction *, StrideDescriptor> AccessStrideInfo;
712 collectConstStrideAccesses(AccessStrideInfo, Strides);
713
714 if (AccessStrideInfo.empty())
715 return;
716
717 // Collect the dependences in the loop.
718 collectDependences();
719
720 // Holds all interleaved store groups temporarily.
721 SmallSetVector<InterleaveGroup *, 4> StoreGroups;
722 // Holds all interleaved load groups temporarily.
723 SmallSetVector<InterleaveGroup *, 4> LoadGroups;
724
725 // Search in bottom-up program order for pairs of accesses (A and B) that can
726 // form interleaved load or store groups. In the algorithm below, access A
727 // precedes access B in program order. We initialize a group for B in the
728 // outer loop of the algorithm, and then in the inner loop, we attempt to
729 // insert each A into B's group if:
730 //
731 // 1. A and B have the same stride,
732 // 2. A and B have the same memory object size, and
733 // 3. A belongs in B's group according to its distance from B.
734 //
735 // Special care is taken to ensure group formation will not break any
736 // dependences.
737 for (auto BI = AccessStrideInfo.rbegin(), E = AccessStrideInfo.rend();
738 BI != E; ++BI) {
739 Instruction *B = BI->first;
740 StrideDescriptor DesB = BI->second;
741
742 // Initialize a group for B if it has an allowable stride. Even if we don't
743 // create a group for B, we continue with the bottom-up algorithm to ensure
744 // we don't break any of B's dependences.
745 InterleaveGroup *Group = nullptr;
Dorit Nuzman38bbf812018-10-14 08:50:06 +0000746 if (isStrided(DesB.Stride) &&
747 (!isPredicated(B->getParent()) || EnablePredicatedInterleavedMemAccesses)) {
Florian Hahn1086ce22018-09-12 08:01:57 +0000748 Group = getInterleaveGroup(B);
749 if (!Group) {
750 LLVM_DEBUG(dbgs() << "LV: Creating an interleave group with:" << *B
751 << '\n');
752 Group = createInterleaveGroup(B, DesB.Stride, DesB.Align);
753 }
754 if (B->mayWriteToMemory())
755 StoreGroups.insert(Group);
756 else
757 LoadGroups.insert(Group);
758 }
759
760 for (auto AI = std::next(BI); AI != E; ++AI) {
761 Instruction *A = AI->first;
762 StrideDescriptor DesA = AI->second;
763
764 // Our code motion strategy implies that we can't have dependences
765 // between accesses in an interleaved group and other accesses located
766 // between the first and last member of the group. Note that this also
767 // means that a group can't have more than one member at a given offset.
768 // The accesses in a group can have dependences with other accesses, but
769 // we must ensure we don't extend the boundaries of the group such that
770 // we encompass those dependent accesses.
771 //
772 // For example, assume we have the sequence of accesses shown below in a
773 // stride-2 loop:
774 //
775 // (1, 2) is a group | A[i] = a; // (1)
776 // | A[i-1] = b; // (2) |
777 // A[i-3] = c; // (3)
778 // A[i] = d; // (4) | (2, 4) is not a group
779 //
780 // Because accesses (2) and (3) are dependent, we can group (2) with (1)
781 // but not with (4). If we did, the dependent access (3) would be within
782 // the boundaries of the (2, 4) group.
783 if (!canReorderMemAccessesForInterleavedGroups(&*AI, &*BI)) {
784 // If a dependence exists and A is already in a group, we know that A
785 // must be a store since A precedes B and WAR dependences are allowed.
786 // Thus, A would be sunk below B. We release A's group to prevent this
787 // illegal code motion. A will then be free to form another group with
788 // instructions that precede it.
789 if (isInterleaved(A)) {
790 InterleaveGroup *StoreGroup = getInterleaveGroup(A);
791 StoreGroups.remove(StoreGroup);
792 releaseGroup(StoreGroup);
793 }
794
795 // If a dependence exists and A is not already in a group (or it was
796 // and we just released it), B might be hoisted above A (if B is a
797 // load) or another store might be sunk below A (if B is a store). In
798 // either case, we can't add additional instructions to B's group. B
799 // will only form a group with instructions that it precedes.
800 break;
801 }
802
803 // At this point, we've checked for illegal code motion. If either A or B
804 // isn't strided, there's nothing left to do.
805 if (!isStrided(DesA.Stride) || !isStrided(DesB.Stride))
806 continue;
807
808 // Ignore A if it's already in a group or isn't the same kind of memory
809 // operation as B.
810 // Note that mayReadFromMemory() isn't mutually exclusive to
811 // mayWriteToMemory in the case of atomic loads. We shouldn't see those
812 // here, canVectorizeMemory() should have returned false - except for the
813 // case we asked for optimization remarks.
814 if (isInterleaved(A) ||
815 (A->mayReadFromMemory() != B->mayReadFromMemory()) ||
816 (A->mayWriteToMemory() != B->mayWriteToMemory()))
817 continue;
818
819 // Check rules 1 and 2. Ignore A if its stride or size is different from
820 // that of B.
821 if (DesA.Stride != DesB.Stride || DesA.Size != DesB.Size)
822 continue;
823
824 // Ignore A if the memory object of A and B don't belong to the same
825 // address space
826 if (getLoadStoreAddressSpace(A) != getLoadStoreAddressSpace(B))
827 continue;
828
829 // Calculate the distance from A to B.
830 const SCEVConstant *DistToB = dyn_cast<SCEVConstant>(
831 PSE.getSE()->getMinusSCEV(DesA.Scev, DesB.Scev));
832 if (!DistToB)
833 continue;
834 int64_t DistanceToB = DistToB->getAPInt().getSExtValue();
835
836 // Check rule 3. Ignore A if its distance to B is not a multiple of the
837 // size.
838 if (DistanceToB % static_cast<int64_t>(DesB.Size))
839 continue;
840
Dorit Nuzman38bbf812018-10-14 08:50:06 +0000841 // All members of a predicated interleave-group must have the same predicate,
842 // and currently must reside in the same BB.
843 BasicBlock *BlockA = A->getParent();
844 BasicBlock *BlockB = B->getParent();
845 if ((isPredicated(BlockA) || isPredicated(BlockB)) &&
846 (!EnablePredicatedInterleavedMemAccesses || BlockA != BlockB))
Florian Hahn1086ce22018-09-12 08:01:57 +0000847 continue;
848
849 // The index of A is the index of B plus A's distance to B in multiples
850 // of the size.
851 int IndexA =
852 Group->getIndex(B) + DistanceToB / static_cast<int64_t>(DesB.Size);
853
854 // Try to insert A into B's group.
855 if (Group->insertMember(A, IndexA, DesA.Align)) {
856 LLVM_DEBUG(dbgs() << "LV: Inserted:" << *A << '\n'
857 << " into the interleave group with" << *B
858 << '\n');
859 InterleaveGroupMap[A] = Group;
860
861 // Set the first load in program order as the insert position.
862 if (A->mayReadFromMemory())
863 Group->setInsertPos(A);
864 }
865 } // Iteration over A accesses.
866 } // Iteration over B accesses.
867
868 // Remove interleaved store groups with gaps.
869 for (InterleaveGroup *Group : StoreGroups)
870 if (Group->getNumMembers() != Group->getFactor()) {
871 LLVM_DEBUG(
872 dbgs() << "LV: Invalidate candidate interleaved store group due "
873 "to gaps.\n");
874 releaseGroup(Group);
875 }
876 // Remove interleaved groups with gaps (currently only loads) whose memory
877 // accesses may wrap around. We have to revisit the getPtrStride analysis,
878 // this time with ShouldCheckWrap=true, since collectConstStrideAccesses does
879 // not check wrapping (see documentation there).
880 // FORNOW we use Assume=false;
881 // TODO: Change to Assume=true but making sure we don't exceed the threshold
882 // of runtime SCEV assumptions checks (thereby potentially failing to
883 // vectorize altogether).
884 // Additional optional optimizations:
885 // TODO: If we are peeling the loop and we know that the first pointer doesn't
886 // wrap then we can deduce that all pointers in the group don't wrap.
887 // This means that we can forcefully peel the loop in order to only have to
888 // check the first pointer for no-wrap. When we'll change to use Assume=true
889 // we'll only need at most one runtime check per interleaved group.
890 for (InterleaveGroup *Group : LoadGroups) {
891 // Case 1: A full group. Can Skip the checks; For full groups, if the wide
892 // load would wrap around the address space we would do a memory access at
893 // nullptr even without the transformation.
894 if (Group->getNumMembers() == Group->getFactor())
895 continue;
896
897 // Case 2: If first and last members of the group don't wrap this implies
898 // that all the pointers in the group don't wrap.
899 // So we check only group member 0 (which is always guaranteed to exist),
900 // and group member Factor - 1; If the latter doesn't exist we rely on
901 // peeling (if it is a non-reveresed accsess -- see Case 3).
902 Value *FirstMemberPtr = getLoadStorePointerOperand(Group->getMember(0));
903 if (!getPtrStride(PSE, FirstMemberPtr, TheLoop, Strides, /*Assume=*/false,
904 /*ShouldCheckWrap=*/true)) {
905 LLVM_DEBUG(
906 dbgs() << "LV: Invalidate candidate interleaved group due to "
907 "first group member potentially pointer-wrapping.\n");
908 releaseGroup(Group);
909 continue;
910 }
911 Instruction *LastMember = Group->getMember(Group->getFactor() - 1);
912 if (LastMember) {
913 Value *LastMemberPtr = getLoadStorePointerOperand(LastMember);
914 if (!getPtrStride(PSE, LastMemberPtr, TheLoop, Strides, /*Assume=*/false,
915 /*ShouldCheckWrap=*/true)) {
916 LLVM_DEBUG(
917 dbgs() << "LV: Invalidate candidate interleaved group due to "
918 "last group member potentially pointer-wrapping.\n");
919 releaseGroup(Group);
920 }
921 } else {
922 // Case 3: A non-reversed interleaved load group with gaps: We need
923 // to execute at least one scalar epilogue iteration. This will ensure
924 // we don't speculatively access memory out-of-bounds. We only need
925 // to look for a member at index factor - 1, since every group must have
926 // a member at index zero.
927 if (Group->isReverse()) {
928 LLVM_DEBUG(
929 dbgs() << "LV: Invalidate candidate interleaved group due to "
930 "a reverse access with gaps.\n");
931 releaseGroup(Group);
932 continue;
933 }
934 LLVM_DEBUG(
935 dbgs() << "LV: Interleaved group requires epilogue iteration.\n");
936 RequiresScalarEpilogue = true;
937 }
938 }
939}
Dorit Nuzman3ec99fe2018-10-22 06:17:09 +0000940
941void InterleavedAccessInfo::invalidateGroupsRequiringScalarEpilogue() {
942 // If no group had triggered the requirement to create an epilogue loop,
943 // there is nothing to do.
944 if (!requiresScalarEpilogue())
945 return;
946
947 // Avoid releasing a Group twice.
948 SmallPtrSet<InterleaveGroup *, 4> DelSet;
949 for (auto &I : InterleaveGroupMap) {
950 InterleaveGroup *Group = I.second;
951 if (Group->requiresScalarEpilogue())
952 DelSet.insert(Group);
953 }
954 for (auto *Ptr : DelSet) {
955 LLVM_DEBUG(
Dorit Nuzman34da6dd2018-10-31 09:57:56 +0000956 dbgs()
Dorit Nuzman3ec99fe2018-10-22 06:17:09 +0000957 << "LV: Invalidate candidate interleaved group due to gaps that "
Dorit Nuzman34da6dd2018-10-31 09:57:56 +0000958 "require a scalar epilogue (not allowed under optsize) and cannot "
959 "be masked (not enabled). \n");
Dorit Nuzman3ec99fe2018-10-22 06:17:09 +0000960 releaseGroup(Ptr);
961 }
962
963 RequiresScalarEpilogue = false;
964}