blob: 71affef2c61966efcda83dc59e936b18dc999428 [file] [log] [blame]
David Blaikie1213dbf2015-06-26 16:57:30 +00001//===----------- VectorUtils.cpp - Vectorizer utility functions -----------===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
David Blaikie1213dbf2015-06-26 16:57:30 +00006//
7//===----------------------------------------------------------------------===//
8//
9// This file defines vectorizer utilities.
10//
11//===----------------------------------------------------------------------===//
12
Chandler Carruth6bda14b2017-06-06 11:49:48 +000013#include "llvm/Analysis/VectorUtils.h"
James Molloy55d633b2015-10-12 12:34:45 +000014#include "llvm/ADT/EquivalenceClasses.h"
15#include "llvm/Analysis/DemandedBits.h"
Hal Finkel9cf58c42015-07-11 10:52:42 +000016#include "llvm/Analysis/LoopInfo.h"
Florian Hahn1086ce22018-09-12 08:01:57 +000017#include "llvm/Analysis/LoopIterator.h"
Hal Finkel9cf58c42015-07-11 10:52:42 +000018#include "llvm/Analysis/ScalarEvolution.h"
Chandler Carruth6bda14b2017-06-06 11:49:48 +000019#include "llvm/Analysis/ScalarEvolutionExpressions.h"
James Molloy55d633b2015-10-12 12:34:45 +000020#include "llvm/Analysis/TargetTransformInfo.h"
David Majnemerb4b27232016-04-19 19:10:21 +000021#include "llvm/Analysis/ValueTracking.h"
Chandler Carruth6bda14b2017-06-06 11:49:48 +000022#include "llvm/IR/Constants.h"
Hal Finkel9cf58c42015-07-11 10:52:42 +000023#include "llvm/IR/GetElementPtrTypeIterator.h"
Chandler Carruth6bda14b2017-06-06 11:49:48 +000024#include "llvm/IR/IRBuilder.h"
Hal Finkel9cf58c42015-07-11 10:52:42 +000025#include "llvm/IR/PatternMatch.h"
26#include "llvm/IR/Value.h"
Renato Golin3b1d3b02015-08-30 10:49:04 +000027
Florian Hahn1086ce22018-09-12 08:01:57 +000028#define DEBUG_TYPE "vectorutils"
29
David Majnemer5eaf08f2015-08-18 22:07:20 +000030using namespace llvm;
31using namespace llvm::PatternMatch;
David Blaikie1213dbf2015-06-26 16:57:30 +000032
Florian Hahn1086ce22018-09-12 08:01:57 +000033/// Maximum factor for an interleaved memory access.
34static cl::opt<unsigned> MaxInterleaveGroupFactor(
35 "max-interleave-group-factor", cl::Hidden,
36 cl::desc("Maximum factor for an interleaved access group (default = 8)"),
37 cl::init(8));
38
Sanjay Patel0f4f48062018-11-12 15:10:30 +000039/// Return true if all of the intrinsic's arguments and return type are scalars
40/// for the scalar form of the intrinsic and vectors for the vector form of the
41/// intrinsic.
David Blaikie1213dbf2015-06-26 16:57:30 +000042bool llvm::isTriviallyVectorizable(Intrinsic::ID ID) {
43 switch (ID) {
Sanjay Patel0f4f48062018-11-12 15:10:30 +000044 case Intrinsic::bswap: // Begin integer bit-manipulation.
45 case Intrinsic::bitreverse:
46 case Intrinsic::ctpop:
47 case Intrinsic::ctlz:
48 case Intrinsic::cttz:
Sanjay Patel1456fd72018-11-12 15:20:14 +000049 case Intrinsic::fshl:
50 case Intrinsic::fshr:
Sanjay Patel0f4f48062018-11-12 15:10:30 +000051 case Intrinsic::sqrt: // Begin floating-point.
David Blaikie1213dbf2015-06-26 16:57:30 +000052 case Intrinsic::sin:
53 case Intrinsic::cos:
54 case Intrinsic::exp:
55 case Intrinsic::exp2:
56 case Intrinsic::log:
57 case Intrinsic::log10:
58 case Intrinsic::log2:
59 case Intrinsic::fabs:
60 case Intrinsic::minnum:
61 case Intrinsic::maxnum:
Thomas Lively8a91cf12018-10-19 21:11:43 +000062 case Intrinsic::minimum:
63 case Intrinsic::maximum:
David Blaikie1213dbf2015-06-26 16:57:30 +000064 case Intrinsic::copysign:
65 case Intrinsic::floor:
66 case Intrinsic::ceil:
67 case Intrinsic::trunc:
68 case Intrinsic::rint:
69 case Intrinsic::nearbyint:
70 case Intrinsic::round:
David Blaikie1213dbf2015-06-26 16:57:30 +000071 case Intrinsic::pow:
72 case Intrinsic::fma:
73 case Intrinsic::fmuladd:
David Blaikie1213dbf2015-06-26 16:57:30 +000074 case Intrinsic::powi:
Matt Arsenault80ea6dd2018-09-17 13:24:30 +000075 case Intrinsic::canonicalize:
Simon Pilgrimc2aadfa2019-01-03 12:18:23 +000076 case Intrinsic::sadd_sat:
77 case Intrinsic::ssub_sat:
78 case Intrinsic::uadd_sat:
79 case Intrinsic::usub_sat:
David Blaikie1213dbf2015-06-26 16:57:30 +000080 return true;
81 default:
82 return false;
83 }
84}
85
Adrian Prantl5f8f34e42018-05-01 15:54:18 +000086/// Identifies if the intrinsic has a scalar operand. It check for
David Blaikie1213dbf2015-06-26 16:57:30 +000087/// ctlz,cttz and powi special intrinsics whose argument is scalar.
88bool llvm::hasVectorInstrinsicScalarOpd(Intrinsic::ID ID,
89 unsigned ScalarOpdIdx) {
90 switch (ID) {
91 case Intrinsic::ctlz:
92 case Intrinsic::cttz:
93 case Intrinsic::powi:
94 return (ScalarOpdIdx == 1);
95 default:
96 return false;
97 }
98}
99
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000100/// Returns intrinsic ID for call.
David Blaikie1213dbf2015-06-26 16:57:30 +0000101/// For the input call instruction it finds mapping intrinsic and returns
102/// its ID, in case it does not found it return not_intrinsic.
David Majnemerb4b27232016-04-19 19:10:21 +0000103Intrinsic::ID llvm::getVectorIntrinsicIDForCall(const CallInst *CI,
104 const TargetLibraryInfo *TLI) {
105 Intrinsic::ID ID = getIntrinsicForCallSite(CI, TLI);
106 if (ID == Intrinsic::not_intrinsic)
David Blaikie1213dbf2015-06-26 16:57:30 +0000107 return Intrinsic::not_intrinsic;
108
David Majnemerb4b27232016-04-19 19:10:21 +0000109 if (isTriviallyVectorizable(ID) || ID == Intrinsic::lifetime_start ||
Dan Gohman2c74fe92017-11-08 21:59:51 +0000110 ID == Intrinsic::lifetime_end || ID == Intrinsic::assume ||
111 ID == Intrinsic::sideeffect)
David Majnemerb4b27232016-04-19 19:10:21 +0000112 return ID;
David Blaikie1213dbf2015-06-26 16:57:30 +0000113 return Intrinsic::not_intrinsic;
114}
Hal Finkel9cf58c42015-07-11 10:52:42 +0000115
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000116/// Find the operand of the GEP that should be checked for consecutive
Hal Finkel9cf58c42015-07-11 10:52:42 +0000117/// stores. This ignores trailing indices that have no effect on the final
118/// pointer.
119unsigned llvm::getGEPInductionOperand(const GetElementPtrInst *Gep) {
120 const DataLayout &DL = Gep->getModule()->getDataLayout();
121 unsigned LastOperand = Gep->getNumOperands() - 1;
Eduard Burtescu19eb0312016-01-19 17:28:00 +0000122 unsigned GEPAllocSize = DL.getTypeAllocSize(Gep->getResultElementType());
Hal Finkel9cf58c42015-07-11 10:52:42 +0000123
124 // Walk backwards and try to peel off zeros.
David Majnemer5eaf08f2015-08-18 22:07:20 +0000125 while (LastOperand > 1 && match(Gep->getOperand(LastOperand), m_Zero())) {
Hal Finkel9cf58c42015-07-11 10:52:42 +0000126 // Find the type we're currently indexing into.
127 gep_type_iterator GEPTI = gep_type_begin(Gep);
Peter Collingbourneab85225b2016-12-02 02:24:42 +0000128 std::advance(GEPTI, LastOperand - 2);
Hal Finkel9cf58c42015-07-11 10:52:42 +0000129
130 // If it's a type with the same allocation size as the result of the GEP we
131 // can peel off the zero index.
Peter Collingbourneab85225b2016-12-02 02:24:42 +0000132 if (DL.getTypeAllocSize(GEPTI.getIndexedType()) != GEPAllocSize)
Hal Finkel9cf58c42015-07-11 10:52:42 +0000133 break;
134 --LastOperand;
135 }
136
137 return LastOperand;
138}
139
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000140/// If the argument is a GEP, then returns the operand identified by
Hal Finkel9cf58c42015-07-11 10:52:42 +0000141/// getGEPInductionOperand. However, if there is some other non-loop-invariant
142/// operand, it returns that instead.
David Majnemer5eaf08f2015-08-18 22:07:20 +0000143Value *llvm::stripGetElementPtr(Value *Ptr, ScalarEvolution *SE, Loop *Lp) {
Hal Finkel9cf58c42015-07-11 10:52:42 +0000144 GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(Ptr);
145 if (!GEP)
146 return Ptr;
147
148 unsigned InductionOperand = getGEPInductionOperand(GEP);
149
150 // Check that all of the gep indices are uniform except for our induction
151 // operand.
152 for (unsigned i = 0, e = GEP->getNumOperands(); i != e; ++i)
153 if (i != InductionOperand &&
154 !SE->isLoopInvariant(SE->getSCEV(GEP->getOperand(i)), Lp))
155 return Ptr;
156 return GEP->getOperand(InductionOperand);
157}
158
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000159/// If a value has only one user that is a CastInst, return it.
David Majnemer5eaf08f2015-08-18 22:07:20 +0000160Value *llvm::getUniqueCastUse(Value *Ptr, Loop *Lp, Type *Ty) {
161 Value *UniqueCast = nullptr;
Hal Finkel9cf58c42015-07-11 10:52:42 +0000162 for (User *U : Ptr->users()) {
163 CastInst *CI = dyn_cast<CastInst>(U);
164 if (CI && CI->getType() == Ty) {
165 if (!UniqueCast)
166 UniqueCast = CI;
167 else
168 return nullptr;
169 }
170 }
171 return UniqueCast;
172}
173
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000174/// Get the stride of a pointer access in a loop. Looks for symbolic
Hal Finkel9cf58c42015-07-11 10:52:42 +0000175/// strides "a[i*stride]". Returns the symbolic stride, or null otherwise.
David Majnemer5eaf08f2015-08-18 22:07:20 +0000176Value *llvm::getStrideFromPointer(Value *Ptr, ScalarEvolution *SE, Loop *Lp) {
Craig Toppere3dcce92015-08-01 22:20:21 +0000177 auto *PtrTy = dyn_cast<PointerType>(Ptr->getType());
Hal Finkel9cf58c42015-07-11 10:52:42 +0000178 if (!PtrTy || PtrTy->isAggregateType())
179 return nullptr;
180
181 // Try to remove a gep instruction to make the pointer (actually index at this
Vedant Kumard3196742018-02-28 19:08:52 +0000182 // point) easier analyzable. If OrigPtr is equal to Ptr we are analyzing the
Hal Finkel9cf58c42015-07-11 10:52:42 +0000183 // pointer, otherwise, we are analyzing the index.
David Majnemer5eaf08f2015-08-18 22:07:20 +0000184 Value *OrigPtr = Ptr;
Hal Finkel9cf58c42015-07-11 10:52:42 +0000185
186 // The size of the pointer access.
187 int64_t PtrAccessSize = 1;
188
189 Ptr = stripGetElementPtr(Ptr, SE, Lp);
190 const SCEV *V = SE->getSCEV(Ptr);
191
192 if (Ptr != OrigPtr)
193 // Strip off casts.
194 while (const SCEVCastExpr *C = dyn_cast<SCEVCastExpr>(V))
195 V = C->getOperand();
196
197 const SCEVAddRecExpr *S = dyn_cast<SCEVAddRecExpr>(V);
198 if (!S)
199 return nullptr;
200
201 V = S->getStepRecurrence(*SE);
202 if (!V)
203 return nullptr;
204
205 // Strip off the size of access multiplication if we are still analyzing the
206 // pointer.
207 if (OrigPtr == Ptr) {
Hal Finkel9cf58c42015-07-11 10:52:42 +0000208 if (const SCEVMulExpr *M = dyn_cast<SCEVMulExpr>(V)) {
209 if (M->getOperand(0)->getSCEVType() != scConstant)
210 return nullptr;
211
Sanjoy Das0de2fec2015-12-17 20:28:46 +0000212 const APInt &APStepVal = cast<SCEVConstant>(M->getOperand(0))->getAPInt();
Hal Finkel9cf58c42015-07-11 10:52:42 +0000213
214 // Huge step value - give up.
215 if (APStepVal.getBitWidth() > 64)
216 return nullptr;
217
218 int64_t StepVal = APStepVal.getSExtValue();
219 if (PtrAccessSize != StepVal)
220 return nullptr;
221 V = M->getOperand(1);
222 }
223 }
224
225 // Strip off casts.
226 Type *StripedOffRecurrenceCast = nullptr;
227 if (const SCEVCastExpr *C = dyn_cast<SCEVCastExpr>(V)) {
228 StripedOffRecurrenceCast = C->getType();
229 V = C->getOperand();
230 }
231
232 // Look for the loop invariant symbolic value.
233 const SCEVUnknown *U = dyn_cast<SCEVUnknown>(V);
234 if (!U)
235 return nullptr;
236
David Majnemer5eaf08f2015-08-18 22:07:20 +0000237 Value *Stride = U->getValue();
Hal Finkel9cf58c42015-07-11 10:52:42 +0000238 if (!Lp->isLoopInvariant(Stride))
239 return nullptr;
240
241 // If we have stripped off the recurrence cast we have to make sure that we
242 // return the value that is used in this loop so that we can replace it later.
243 if (StripedOffRecurrenceCast)
244 Stride = getUniqueCastUse(Stride, Lp, StripedOffRecurrenceCast);
245
246 return Stride;
247}
David Majnemer599ca442015-07-13 01:15:53 +0000248
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000249/// Given a vector and an element number, see if the scalar value is
David Majnemer599ca442015-07-13 01:15:53 +0000250/// already around as a register, for example if it were inserted then extracted
251/// from the vector.
David Majnemer5eaf08f2015-08-18 22:07:20 +0000252Value *llvm::findScalarElement(Value *V, unsigned EltNo) {
David Majnemer599ca442015-07-13 01:15:53 +0000253 assert(V->getType()->isVectorTy() && "Not looking at a vector?");
254 VectorType *VTy = cast<VectorType>(V->getType());
255 unsigned Width = VTy->getNumElements();
256 if (EltNo >= Width) // Out of range access.
257 return UndefValue::get(VTy->getElementType());
258
259 if (Constant *C = dyn_cast<Constant>(V))
260 return C->getAggregateElement(EltNo);
261
262 if (InsertElementInst *III = dyn_cast<InsertElementInst>(V)) {
263 // If this is an insert to a variable element, we don't know what it is.
264 if (!isa<ConstantInt>(III->getOperand(2)))
265 return nullptr;
266 unsigned IIElt = cast<ConstantInt>(III->getOperand(2))->getZExtValue();
267
268 // If this is an insert to the element we are looking for, return the
269 // inserted value.
270 if (EltNo == IIElt)
271 return III->getOperand(1);
272
273 // Otherwise, the insertelement doesn't modify the value, recurse on its
274 // vector input.
275 return findScalarElement(III->getOperand(0), EltNo);
276 }
277
278 if (ShuffleVectorInst *SVI = dyn_cast<ShuffleVectorInst>(V)) {
279 unsigned LHSWidth = SVI->getOperand(0)->getType()->getVectorNumElements();
280 int InEl = SVI->getMaskValue(EltNo);
281 if (InEl < 0)
282 return UndefValue::get(VTy->getElementType());
283 if (InEl < (int)LHSWidth)
284 return findScalarElement(SVI->getOperand(0), InEl);
285 return findScalarElement(SVI->getOperand(1), InEl - LHSWidth);
286 }
287
288 // Extract a value from a vector add operation with a constant zero.
Sanjay Patel3413a662018-09-24 17:18:32 +0000289 // TODO: Use getBinOpIdentity() to generalize this.
290 Value *Val; Constant *C;
291 if (match(V, m_Add(m_Value(Val), m_Constant(C))))
292 if (Constant *Elt = C->getAggregateElement(EltNo))
David Majnemerc6bb0e22015-08-18 22:07:25 +0000293 if (Elt->isNullValue())
294 return findScalarElement(Val, EltNo);
David Majnemer599ca442015-07-13 01:15:53 +0000295
296 // Otherwise, we don't know.
297 return nullptr;
298}
Renato Golin3b1d3b02015-08-30 10:49:04 +0000299
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000300/// Get splat value if the input is a splat vector or return nullptr.
Elena Demikhovsky63a7ca92015-08-30 13:48:02 +0000301/// This function is not fully general. It checks only 2 cases:
302/// the input value is (1) a splat constants vector or (2) a sequence
303/// of instructions that broadcast a single value into a vector.
304///
Elena Demikhovsky0781d7b2015-12-01 12:08:36 +0000305const llvm::Value *llvm::getSplatValue(const Value *V) {
306
307 if (auto *C = dyn_cast<Constant>(V))
Elena Demikhovsky47fa2712015-12-01 12:30:40 +0000308 if (isa<VectorType>(V->getType()))
309 return C->getSplatValue();
Elena Demikhovsky63a7ca92015-08-30 13:48:02 +0000310
311 auto *ShuffleInst = dyn_cast<ShuffleVectorInst>(V);
Renato Golin3b1d3b02015-08-30 10:49:04 +0000312 if (!ShuffleInst)
313 return nullptr;
Elena Demikhovsky63a7ca92015-08-30 13:48:02 +0000314 // All-zero (or undef) shuffle mask elements.
315 for (int MaskElt : ShuffleInst->getShuffleMask())
316 if (MaskElt != 0 && MaskElt != -1)
Renato Golin3b1d3b02015-08-30 10:49:04 +0000317 return nullptr;
318 // The first shuffle source is 'insertelement' with index 0.
Elena Demikhovsky63a7ca92015-08-30 13:48:02 +0000319 auto *InsertEltInst =
320 dyn_cast<InsertElementInst>(ShuffleInst->getOperand(0));
Renato Golin3b1d3b02015-08-30 10:49:04 +0000321 if (!InsertEltInst || !isa<ConstantInt>(InsertEltInst->getOperand(2)) ||
Craig Topper79ab6432017-07-06 18:39:47 +0000322 !cast<ConstantInt>(InsertEltInst->getOperand(2))->isZero())
Renato Golin3b1d3b02015-08-30 10:49:04 +0000323 return nullptr;
324
325 return InsertEltInst->getOperand(1);
326}
James Molloy55d633b2015-10-12 12:34:45 +0000327
Charlie Turner54336a52015-11-26 20:39:51 +0000328MapVector<Instruction *, uint64_t>
James Molloy45f67d52015-11-09 14:32:05 +0000329llvm::computeMinimumValueSizes(ArrayRef<BasicBlock *> Blocks, DemandedBits &DB,
330 const TargetTransformInfo *TTI) {
James Molloy55d633b2015-10-12 12:34:45 +0000331
332 // DemandedBits will give us every value's live-out bits. But we want
333 // to ensure no extra casts would need to be inserted, so every DAG
334 // of connected values must have the same minimum bitwidth.
James Molloy45f67d52015-11-09 14:32:05 +0000335 EquivalenceClasses<Value *> ECs;
336 SmallVector<Value *, 16> Worklist;
337 SmallPtrSet<Value *, 4> Roots;
338 SmallPtrSet<Value *, 16> Visited;
339 DenseMap<Value *, uint64_t> DBits;
340 SmallPtrSet<Instruction *, 4> InstructionSet;
Charlie Turner54336a52015-11-26 20:39:51 +0000341 MapVector<Instruction *, uint64_t> MinBWs;
James Molloy45f67d52015-11-09 14:32:05 +0000342
James Molloy55d633b2015-10-12 12:34:45 +0000343 // Determine the roots. We work bottom-up, from truncs or icmps.
344 bool SeenExtFromIllegalType = false;
345 for (auto *BB : Blocks)
346 for (auto &I : *BB) {
347 InstructionSet.insert(&I);
348
349 if (TTI && (isa<ZExtInst>(&I) || isa<SExtInst>(&I)) &&
350 !TTI->isTypeLegal(I.getOperand(0)->getType()))
351 SeenExtFromIllegalType = true;
James Molloy45f67d52015-11-09 14:32:05 +0000352
James Molloy55d633b2015-10-12 12:34:45 +0000353 // Only deal with non-vector integers up to 64-bits wide.
354 if ((isa<TruncInst>(&I) || isa<ICmpInst>(&I)) &&
355 !I.getType()->isVectorTy() &&
356 I.getOperand(0)->getType()->getScalarSizeInBits() <= 64) {
357 // Don't make work for ourselves. If we know the loaded type is legal,
358 // don't add it to the worklist.
359 if (TTI && isa<TruncInst>(&I) && TTI->isTypeLegal(I.getType()))
360 continue;
James Molloy45f67d52015-11-09 14:32:05 +0000361
James Molloy55d633b2015-10-12 12:34:45 +0000362 Worklist.push_back(&I);
363 Roots.insert(&I);
364 }
365 }
366 // Early exit.
367 if (Worklist.empty() || (TTI && !SeenExtFromIllegalType))
368 return MinBWs;
James Molloy45f67d52015-11-09 14:32:05 +0000369
James Molloy55d633b2015-10-12 12:34:45 +0000370 // Now proceed breadth-first, unioning values together.
371 while (!Worklist.empty()) {
372 Value *Val = Worklist.pop_back_val();
373 Value *Leader = ECs.getOrInsertLeaderValue(Val);
James Molloy45f67d52015-11-09 14:32:05 +0000374
James Molloy55d633b2015-10-12 12:34:45 +0000375 if (Visited.count(Val))
376 continue;
377 Visited.insert(Val);
378
379 // Non-instructions terminate a chain successfully.
380 if (!isa<Instruction>(Val))
381 continue;
382 Instruction *I = cast<Instruction>(Val);
383
384 // If we encounter a type that is larger than 64 bits, we can't represent
385 // it so bail out.
James Molloyaa1d6382016-05-10 12:27:23 +0000386 if (DB.getDemandedBits(I).getBitWidth() > 64)
Charlie Turner54336a52015-11-26 20:39:51 +0000387 return MapVector<Instruction *, uint64_t>();
James Molloy45f67d52015-11-09 14:32:05 +0000388
James Molloyaa1d6382016-05-10 12:27:23 +0000389 uint64_t V = DB.getDemandedBits(I).getZExtValue();
390 DBits[Leader] |= V;
391 DBits[I] = V;
James Molloy45f67d52015-11-09 14:32:05 +0000392
James Molloy55d633b2015-10-12 12:34:45 +0000393 // Casts, loads and instructions outside of our range terminate a chain
394 // successfully.
395 if (isa<SExtInst>(I) || isa<ZExtInst>(I) || isa<LoadInst>(I) ||
396 !InstructionSet.count(I))
397 continue;
398
399 // Unsafe casts terminate a chain unsuccessfully. We can't do anything
400 // useful with bitcasts, ptrtoints or inttoptrs and it'd be unsafe to
401 // transform anything that relies on them.
402 if (isa<BitCastInst>(I) || isa<PtrToIntInst>(I) || isa<IntToPtrInst>(I) ||
403 !I->getType()->isIntegerTy()) {
404 DBits[Leader] |= ~0ULL;
405 continue;
406 }
407
408 // We don't modify the types of PHIs. Reductions will already have been
409 // truncated if possible, and inductions' sizes will have been chosen by
410 // indvars.
411 if (isa<PHINode>(I))
412 continue;
413
414 if (DBits[Leader] == ~0ULL)
415 // All bits demanded, no point continuing.
416 continue;
417
418 for (Value *O : cast<User>(I)->operands()) {
419 ECs.unionSets(Leader, O);
420 Worklist.push_back(O);
421 }
422 }
423
424 // Now we've discovered all values, walk them to see if there are
425 // any users we didn't see. If there are, we can't optimize that
426 // chain.
427 for (auto &I : DBits)
428 for (auto *U : I.first->users())
429 if (U->getType()->isIntegerTy() && DBits.count(U) == 0)
430 DBits[ECs.getOrInsertLeaderValue(I.first)] |= ~0ULL;
James Molloy45f67d52015-11-09 14:32:05 +0000431
James Molloy55d633b2015-10-12 12:34:45 +0000432 for (auto I = ECs.begin(), E = ECs.end(); I != E; ++I) {
433 uint64_t LeaderDemandedBits = 0;
434 for (auto MI = ECs.member_begin(I), ME = ECs.member_end(); MI != ME; ++MI)
435 LeaderDemandedBits |= DBits[*MI];
436
437 uint64_t MinBW = (sizeof(LeaderDemandedBits) * 8) -
438 llvm::countLeadingZeros(LeaderDemandedBits);
439 // Round up to a power of 2
440 if (!isPowerOf2_64((uint64_t)MinBW))
441 MinBW = NextPowerOf2(MinBW);
James Molloy8e46cd02016-03-30 10:11:43 +0000442
443 // We don't modify the types of PHIs. Reductions will already have been
444 // truncated if possible, and inductions' sizes will have been chosen by
445 // indvars.
446 // If we are required to shrink a PHI, abandon this entire equivalence class.
447 bool Abort = false;
448 for (auto MI = ECs.member_begin(I), ME = ECs.member_end(); MI != ME; ++MI)
449 if (isa<PHINode>(*MI) && MinBW < (*MI)->getType()->getScalarSizeInBits()) {
450 Abort = true;
451 break;
452 }
453 if (Abort)
454 continue;
455
James Molloy55d633b2015-10-12 12:34:45 +0000456 for (auto MI = ECs.member_begin(I), ME = ECs.member_end(); MI != ME; ++MI) {
457 if (!isa<Instruction>(*MI))
458 continue;
459 Type *Ty = (*MI)->getType();
460 if (Roots.count(*MI))
461 Ty = cast<Instruction>(*MI)->getOperand(0)->getType();
462 if (MinBW < Ty->getScalarSizeInBits())
463 MinBWs[cast<Instruction>(*MI)] = MinBW;
464 }
465 }
466
467 return MinBWs;
468}
Matt Arsenault727e2792016-06-30 21:17:59 +0000469
Michael Kruse978ba612018-12-20 04:58:07 +0000470/// Add all access groups in @p AccGroups to @p List.
471template <typename ListT>
472static void addToAccessGroupList(ListT &List, MDNode *AccGroups) {
473 // Interpret an access group as a list containing itself.
474 if (AccGroups->getNumOperands() == 0) {
475 assert(isValidAsAccessGroup(AccGroups) && "Node must be an access group");
476 List.insert(AccGroups);
477 return;
478 }
479
480 for (auto &AccGroupListOp : AccGroups->operands()) {
481 auto *Item = cast<MDNode>(AccGroupListOp.get());
482 assert(isValidAsAccessGroup(Item) && "List item must be an access group");
483 List.insert(Item);
484 }
Clement Courbetd4bd3eb2018-12-20 09:20:07 +0000485}
Michael Kruse978ba612018-12-20 04:58:07 +0000486
487MDNode *llvm::uniteAccessGroups(MDNode *AccGroups1, MDNode *AccGroups2) {
488 if (!AccGroups1)
489 return AccGroups2;
490 if (!AccGroups2)
491 return AccGroups1;
492 if (AccGroups1 == AccGroups2)
493 return AccGroups1;
494
495 SmallSetVector<Metadata *, 4> Union;
496 addToAccessGroupList(Union, AccGroups1);
497 addToAccessGroupList(Union, AccGroups2);
498
499 if (Union.size() == 0)
500 return nullptr;
501 if (Union.size() == 1)
502 return cast<MDNode>(Union.front());
503
504 LLVMContext &Ctx = AccGroups1->getContext();
505 return MDNode::get(Ctx, Union.getArrayRef());
506}
507
508MDNode *llvm::intersectAccessGroups(const Instruction *Inst1,
509 const Instruction *Inst2) {
510 bool MayAccessMem1 = Inst1->mayReadOrWriteMemory();
511 bool MayAccessMem2 = Inst2->mayReadOrWriteMemory();
512
513 if (!MayAccessMem1 && !MayAccessMem2)
514 return nullptr;
515 if (!MayAccessMem1)
516 return Inst2->getMetadata(LLVMContext::MD_access_group);
517 if (!MayAccessMem2)
518 return Inst1->getMetadata(LLVMContext::MD_access_group);
519
520 MDNode *MD1 = Inst1->getMetadata(LLVMContext::MD_access_group);
521 MDNode *MD2 = Inst2->getMetadata(LLVMContext::MD_access_group);
522 if (!MD1 || !MD2)
523 return nullptr;
524 if (MD1 == MD2)
525 return MD1;
526
527 // Use set for scalable 'contains' check.
528 SmallPtrSet<Metadata *, 4> AccGroupSet2;
529 addToAccessGroupList(AccGroupSet2, MD2);
530
531 SmallVector<Metadata *, 4> Intersection;
532 if (MD1->getNumOperands() == 0) {
533 assert(isValidAsAccessGroup(MD1) && "Node must be an access group");
534 if (AccGroupSet2.count(MD1))
535 Intersection.push_back(MD1);
536 } else {
537 for (const MDOperand &Node : MD1->operands()) {
538 auto *Item = cast<MDNode>(Node.get());
539 assert(isValidAsAccessGroup(Item) && "List item must be an access group");
540 if (AccGroupSet2.count(Item))
541 Intersection.push_back(Item);
542 }
543 }
544
545 if (Intersection.size() == 0)
546 return nullptr;
547 if (Intersection.size() == 1)
548 return cast<MDNode>(Intersection.front());
549
550 LLVMContext &Ctx = Inst1->getContext();
551 return MDNode::get(Ctx, Intersection);
552}
553
Matt Arsenault727e2792016-06-30 21:17:59 +0000554/// \returns \p I after propagating metadata from \p VL.
555Instruction *llvm::propagateMetadata(Instruction *Inst, ArrayRef<Value *> VL) {
556 Instruction *I0 = cast<Instruction>(VL[0]);
557 SmallVector<std::pair<unsigned, MDNode *>, 4> Metadata;
558 I0->getAllMetadataOtherThanDebugLoc(Metadata);
559
Michael Kruse978ba612018-12-20 04:58:07 +0000560 for (auto Kind : {LLVMContext::MD_tbaa, LLVMContext::MD_alias_scope,
561 LLVMContext::MD_noalias, LLVMContext::MD_fpmath,
562 LLVMContext::MD_nontemporal, LLVMContext::MD_invariant_load,
563 LLVMContext::MD_access_group}) {
Matt Arsenault727e2792016-06-30 21:17:59 +0000564 MDNode *MD = I0->getMetadata(Kind);
565
566 for (int J = 1, E = VL.size(); MD && J != E; ++J) {
567 const Instruction *IJ = cast<Instruction>(VL[J]);
568 MDNode *IMD = IJ->getMetadata(Kind);
569 switch (Kind) {
570 case LLVMContext::MD_tbaa:
571 MD = MDNode::getMostGenericTBAA(MD, IMD);
572 break;
573 case LLVMContext::MD_alias_scope:
574 MD = MDNode::getMostGenericAliasScope(MD, IMD);
575 break;
Matt Arsenault727e2792016-06-30 21:17:59 +0000576 case LLVMContext::MD_fpmath:
577 MD = MDNode::getMostGenericFPMath(MD, IMD);
578 break;
Justin Lebar11a32042016-09-11 01:39:08 +0000579 case LLVMContext::MD_noalias:
Matt Arsenault727e2792016-06-30 21:17:59 +0000580 case LLVMContext::MD_nontemporal:
Justin Lebar11a32042016-09-11 01:39:08 +0000581 case LLVMContext::MD_invariant_load:
Matt Arsenault727e2792016-06-30 21:17:59 +0000582 MD = MDNode::intersect(MD, IMD);
583 break;
Michael Kruse978ba612018-12-20 04:58:07 +0000584 case LLVMContext::MD_access_group:
585 MD = intersectAccessGroups(Inst, IJ);
586 break;
Matt Arsenault727e2792016-06-30 21:17:59 +0000587 default:
588 llvm_unreachable("unhandled metadata");
589 }
590 }
591
592 Inst->setMetadata(Kind, MD);
593 }
594
595 return Inst;
596}
Matthew Simpsonba5cf9d2017-02-01 17:45:46 +0000597
Florian Hahna4dc7fe2018-11-13 15:58:18 +0000598Constant *
599llvm::createBitMaskForGaps(IRBuilder<> &Builder, unsigned VF,
600 const InterleaveGroup<Instruction> &Group) {
Dorit Nuzman34da6dd2018-10-31 09:57:56 +0000601 // All 1's means mask is not needed.
602 if (Group.getNumMembers() == Group.getFactor())
603 return nullptr;
604
605 // TODO: support reversed access.
606 assert(!Group.isReverse() && "Reversed group not supported.");
607
608 SmallVector<Constant *, 16> Mask;
609 for (unsigned i = 0; i < VF; i++)
610 for (unsigned j = 0; j < Group.getFactor(); ++j) {
611 unsigned HasMember = Group.getMember(j) ? 1 : 0;
612 Mask.push_back(Builder.getInt1(HasMember));
613 }
614
615 return ConstantVector::get(Mask);
616}
617
Dorit Nuzman38bbf812018-10-14 08:50:06 +0000618Constant *llvm::createReplicatedMask(IRBuilder<> &Builder,
619 unsigned ReplicationFactor, unsigned VF) {
620 SmallVector<Constant *, 16> MaskVec;
621 for (unsigned i = 0; i < VF; i++)
622 for (unsigned j = 0; j < ReplicationFactor; j++)
623 MaskVec.push_back(Builder.getInt32(i));
624
625 return ConstantVector::get(MaskVec);
626}
627
Matthew Simpsonba5cf9d2017-02-01 17:45:46 +0000628Constant *llvm::createInterleaveMask(IRBuilder<> &Builder, unsigned VF,
629 unsigned NumVecs) {
630 SmallVector<Constant *, 16> Mask;
631 for (unsigned i = 0; i < VF; i++)
632 for (unsigned j = 0; j < NumVecs; j++)
633 Mask.push_back(Builder.getInt32(j * VF + i));
634
635 return ConstantVector::get(Mask);
636}
637
638Constant *llvm::createStrideMask(IRBuilder<> &Builder, unsigned Start,
639 unsigned Stride, unsigned VF) {
640 SmallVector<Constant *, 16> Mask;
641 for (unsigned i = 0; i < VF; i++)
642 Mask.push_back(Builder.getInt32(Start + i * Stride));
643
644 return ConstantVector::get(Mask);
645}
646
647Constant *llvm::createSequentialMask(IRBuilder<> &Builder, unsigned Start,
648 unsigned NumInts, unsigned NumUndefs) {
649 SmallVector<Constant *, 16> Mask;
650 for (unsigned i = 0; i < NumInts; i++)
651 Mask.push_back(Builder.getInt32(Start + i));
652
653 Constant *Undef = UndefValue::get(Builder.getInt32Ty());
654 for (unsigned i = 0; i < NumUndefs; i++)
655 Mask.push_back(Undef);
656
657 return ConstantVector::get(Mask);
658}
659
660/// A helper function for concatenating vectors. This function concatenates two
661/// vectors having the same element type. If the second vector has fewer
662/// elements than the first, it is padded with undefs.
663static Value *concatenateTwoVectors(IRBuilder<> &Builder, Value *V1,
664 Value *V2) {
665 VectorType *VecTy1 = dyn_cast<VectorType>(V1->getType());
666 VectorType *VecTy2 = dyn_cast<VectorType>(V2->getType());
667 assert(VecTy1 && VecTy2 &&
668 VecTy1->getScalarType() == VecTy2->getScalarType() &&
669 "Expect two vectors with the same element type");
670
671 unsigned NumElts1 = VecTy1->getNumElements();
672 unsigned NumElts2 = VecTy2->getNumElements();
673 assert(NumElts1 >= NumElts2 && "Unexpect the first vector has less elements");
674
675 if (NumElts1 > NumElts2) {
676 // Extend with UNDEFs.
677 Constant *ExtMask =
678 createSequentialMask(Builder, 0, NumElts2, NumElts1 - NumElts2);
679 V2 = Builder.CreateShuffleVector(V2, UndefValue::get(VecTy2), ExtMask);
680 }
681
682 Constant *Mask = createSequentialMask(Builder, 0, NumElts1 + NumElts2, 0);
683 return Builder.CreateShuffleVector(V1, V2, Mask);
684}
685
686Value *llvm::concatenateVectors(IRBuilder<> &Builder, ArrayRef<Value *> Vecs) {
687 unsigned NumVecs = Vecs.size();
688 assert(NumVecs > 1 && "Should be at least two vectors");
689
690 SmallVector<Value *, 8> ResList;
691 ResList.append(Vecs.begin(), Vecs.end());
692 do {
693 SmallVector<Value *, 8> TmpList;
694 for (unsigned i = 0; i < NumVecs - 1; i += 2) {
695 Value *V0 = ResList[i], *V1 = ResList[i + 1];
696 assert((V0->getType() == V1->getType() || i == NumVecs - 2) &&
697 "Only the last vector may have a different type");
698
699 TmpList.push_back(concatenateTwoVectors(Builder, V0, V1));
700 }
701
702 // Push the last vector if the total number of vectors is odd.
703 if (NumVecs % 2 != 0)
704 TmpList.push_back(ResList[NumVecs - 1]);
705
706 ResList = TmpList;
707 NumVecs = ResList.size();
708 } while (NumVecs > 1);
709
710 return ResList[0];
711}
Florian Hahn1086ce22018-09-12 08:01:57 +0000712
713bool InterleavedAccessInfo::isStrided(int Stride) {
714 unsigned Factor = std::abs(Stride);
715 return Factor >= 2 && Factor <= MaxInterleaveGroupFactor;
716}
717
718void InterleavedAccessInfo::collectConstStrideAccesses(
719 MapVector<Instruction *, StrideDescriptor> &AccessStrideInfo,
720 const ValueToValueMap &Strides) {
721 auto &DL = TheLoop->getHeader()->getModule()->getDataLayout();
722
723 // Since it's desired that the load/store instructions be maintained in
724 // "program order" for the interleaved access analysis, we have to visit the
725 // blocks in the loop in reverse postorder (i.e., in a topological order).
726 // Such an ordering will ensure that any load/store that may be executed
727 // before a second load/store will precede the second load/store in
728 // AccessStrideInfo.
729 LoopBlocksDFS DFS(TheLoop);
730 DFS.perform(LI);
731 for (BasicBlock *BB : make_range(DFS.beginRPO(), DFS.endRPO()))
732 for (auto &I : *BB) {
733 auto *LI = dyn_cast<LoadInst>(&I);
734 auto *SI = dyn_cast<StoreInst>(&I);
735 if (!LI && !SI)
736 continue;
737
738 Value *Ptr = getLoadStorePointerOperand(&I);
739 // We don't check wrapping here because we don't know yet if Ptr will be
740 // part of a full group or a group with gaps. Checking wrapping for all
741 // pointers (even those that end up in groups with no gaps) will be overly
742 // conservative. For full groups, wrapping should be ok since if we would
743 // wrap around the address space we would do a memory access at nullptr
744 // even without the transformation. The wrapping checks are therefore
745 // deferred until after we've formed the interleaved groups.
746 int64_t Stride = getPtrStride(PSE, Ptr, TheLoop, Strides,
747 /*Assume=*/true, /*ShouldCheckWrap=*/false);
748
749 const SCEV *Scev = replaceSymbolicStrideSCEV(PSE, Strides, Ptr);
750 PointerType *PtrTy = dyn_cast<PointerType>(Ptr->getType());
751 uint64_t Size = DL.getTypeAllocSize(PtrTy->getElementType());
752
753 // An alignment of 0 means target ABI alignment.
754 unsigned Align = getLoadStoreAlignment(&I);
755 if (!Align)
756 Align = DL.getABITypeAlignment(PtrTy->getElementType());
757
758 AccessStrideInfo[&I] = StrideDescriptor(Stride, Scev, Size, Align);
759 }
760}
761
762// Analyze interleaved accesses and collect them into interleaved load and
763// store groups.
764//
765// When generating code for an interleaved load group, we effectively hoist all
766// loads in the group to the location of the first load in program order. When
767// generating code for an interleaved store group, we sink all stores to the
768// location of the last store. This code motion can change the order of load
769// and store instructions and may break dependences.
770//
771// The code generation strategy mentioned above ensures that we won't violate
772// any write-after-read (WAR) dependences.
773//
774// E.g., for the WAR dependence: a = A[i]; // (1)
775// A[i] = b; // (2)
776//
777// The store group of (2) is always inserted at or below (2), and the load
778// group of (1) is always inserted at or above (1). Thus, the instructions will
779// never be reordered. All other dependences are checked to ensure the
780// correctness of the instruction reordering.
781//
782// The algorithm visits all memory accesses in the loop in bottom-up program
783// order. Program order is established by traversing the blocks in the loop in
784// reverse postorder when collecting the accesses.
785//
786// We visit the memory accesses in bottom-up order because it can simplify the
787// construction of store groups in the presence of write-after-write (WAW)
788// dependences.
789//
790// E.g., for the WAW dependence: A[i] = a; // (1)
791// A[i] = b; // (2)
792// A[i + 1] = c; // (3)
793//
794// We will first create a store group with (3) and (2). (1) can't be added to
795// this group because it and (2) are dependent. However, (1) can be grouped
796// with other accesses that may precede it in program order. Note that a
797// bottom-up order does not imply that WAW dependences should not be checked.
Dorit Nuzman38bbf812018-10-14 08:50:06 +0000798void InterleavedAccessInfo::analyzeInterleaving(
799 bool EnablePredicatedInterleavedMemAccesses) {
Florian Hahn1086ce22018-09-12 08:01:57 +0000800 LLVM_DEBUG(dbgs() << "LV: Analyzing interleaved accesses...\n");
801 const ValueToValueMap &Strides = LAI->getSymbolicStrides();
802
803 // Holds all accesses with a constant stride.
804 MapVector<Instruction *, StrideDescriptor> AccessStrideInfo;
805 collectConstStrideAccesses(AccessStrideInfo, Strides);
806
807 if (AccessStrideInfo.empty())
808 return;
809
810 // Collect the dependences in the loop.
811 collectDependences();
812
813 // Holds all interleaved store groups temporarily.
Florian Hahna4dc7fe2018-11-13 15:58:18 +0000814 SmallSetVector<InterleaveGroup<Instruction> *, 4> StoreGroups;
Florian Hahn1086ce22018-09-12 08:01:57 +0000815 // Holds all interleaved load groups temporarily.
Florian Hahna4dc7fe2018-11-13 15:58:18 +0000816 SmallSetVector<InterleaveGroup<Instruction> *, 4> LoadGroups;
Florian Hahn1086ce22018-09-12 08:01:57 +0000817
818 // Search in bottom-up program order for pairs of accesses (A and B) that can
819 // form interleaved load or store groups. In the algorithm below, access A
820 // precedes access B in program order. We initialize a group for B in the
821 // outer loop of the algorithm, and then in the inner loop, we attempt to
822 // insert each A into B's group if:
823 //
824 // 1. A and B have the same stride,
825 // 2. A and B have the same memory object size, and
826 // 3. A belongs in B's group according to its distance from B.
827 //
828 // Special care is taken to ensure group formation will not break any
829 // dependences.
830 for (auto BI = AccessStrideInfo.rbegin(), E = AccessStrideInfo.rend();
831 BI != E; ++BI) {
832 Instruction *B = BI->first;
833 StrideDescriptor DesB = BI->second;
834
835 // Initialize a group for B if it has an allowable stride. Even if we don't
836 // create a group for B, we continue with the bottom-up algorithm to ensure
837 // we don't break any of B's dependences.
Florian Hahna4dc7fe2018-11-13 15:58:18 +0000838 InterleaveGroup<Instruction> *Group = nullptr;
Dorit Nuzman38bbf812018-10-14 08:50:06 +0000839 if (isStrided(DesB.Stride) &&
840 (!isPredicated(B->getParent()) || EnablePredicatedInterleavedMemAccesses)) {
Florian Hahn1086ce22018-09-12 08:01:57 +0000841 Group = getInterleaveGroup(B);
842 if (!Group) {
843 LLVM_DEBUG(dbgs() << "LV: Creating an interleave group with:" << *B
844 << '\n');
845 Group = createInterleaveGroup(B, DesB.Stride, DesB.Align);
846 }
847 if (B->mayWriteToMemory())
848 StoreGroups.insert(Group);
849 else
850 LoadGroups.insert(Group);
851 }
852
853 for (auto AI = std::next(BI); AI != E; ++AI) {
854 Instruction *A = AI->first;
855 StrideDescriptor DesA = AI->second;
856
857 // Our code motion strategy implies that we can't have dependences
858 // between accesses in an interleaved group and other accesses located
859 // between the first and last member of the group. Note that this also
860 // means that a group can't have more than one member at a given offset.
861 // The accesses in a group can have dependences with other accesses, but
862 // we must ensure we don't extend the boundaries of the group such that
863 // we encompass those dependent accesses.
864 //
865 // For example, assume we have the sequence of accesses shown below in a
866 // stride-2 loop:
867 //
868 // (1, 2) is a group | A[i] = a; // (1)
869 // | A[i-1] = b; // (2) |
870 // A[i-3] = c; // (3)
871 // A[i] = d; // (4) | (2, 4) is not a group
872 //
873 // Because accesses (2) and (3) are dependent, we can group (2) with (1)
874 // but not with (4). If we did, the dependent access (3) would be within
875 // the boundaries of the (2, 4) group.
876 if (!canReorderMemAccessesForInterleavedGroups(&*AI, &*BI)) {
877 // If a dependence exists and A is already in a group, we know that A
878 // must be a store since A precedes B and WAR dependences are allowed.
879 // Thus, A would be sunk below B. We release A's group to prevent this
880 // illegal code motion. A will then be free to form another group with
881 // instructions that precede it.
882 if (isInterleaved(A)) {
Florian Hahna4dc7fe2018-11-13 15:58:18 +0000883 InterleaveGroup<Instruction> *StoreGroup = getInterleaveGroup(A);
Florian Hahn1086ce22018-09-12 08:01:57 +0000884 StoreGroups.remove(StoreGroup);
885 releaseGroup(StoreGroup);
886 }
887
888 // If a dependence exists and A is not already in a group (or it was
889 // and we just released it), B might be hoisted above A (if B is a
890 // load) or another store might be sunk below A (if B is a store). In
891 // either case, we can't add additional instructions to B's group. B
892 // will only form a group with instructions that it precedes.
893 break;
894 }
895
896 // At this point, we've checked for illegal code motion. If either A or B
897 // isn't strided, there's nothing left to do.
898 if (!isStrided(DesA.Stride) || !isStrided(DesB.Stride))
899 continue;
900
901 // Ignore A if it's already in a group or isn't the same kind of memory
902 // operation as B.
903 // Note that mayReadFromMemory() isn't mutually exclusive to
904 // mayWriteToMemory in the case of atomic loads. We shouldn't see those
905 // here, canVectorizeMemory() should have returned false - except for the
906 // case we asked for optimization remarks.
907 if (isInterleaved(A) ||
908 (A->mayReadFromMemory() != B->mayReadFromMemory()) ||
909 (A->mayWriteToMemory() != B->mayWriteToMemory()))
910 continue;
911
912 // Check rules 1 and 2. Ignore A if its stride or size is different from
913 // that of B.
914 if (DesA.Stride != DesB.Stride || DesA.Size != DesB.Size)
915 continue;
916
917 // Ignore A if the memory object of A and B don't belong to the same
918 // address space
919 if (getLoadStoreAddressSpace(A) != getLoadStoreAddressSpace(B))
920 continue;
921
922 // Calculate the distance from A to B.
923 const SCEVConstant *DistToB = dyn_cast<SCEVConstant>(
924 PSE.getSE()->getMinusSCEV(DesA.Scev, DesB.Scev));
925 if (!DistToB)
926 continue;
927 int64_t DistanceToB = DistToB->getAPInt().getSExtValue();
928
929 // Check rule 3. Ignore A if its distance to B is not a multiple of the
930 // size.
931 if (DistanceToB % static_cast<int64_t>(DesB.Size))
932 continue;
933
Dorit Nuzman38bbf812018-10-14 08:50:06 +0000934 // All members of a predicated interleave-group must have the same predicate,
935 // and currently must reside in the same BB.
936 BasicBlock *BlockA = A->getParent();
937 BasicBlock *BlockB = B->getParent();
938 if ((isPredicated(BlockA) || isPredicated(BlockB)) &&
939 (!EnablePredicatedInterleavedMemAccesses || BlockA != BlockB))
Florian Hahn1086ce22018-09-12 08:01:57 +0000940 continue;
941
942 // The index of A is the index of B plus A's distance to B in multiples
943 // of the size.
944 int IndexA =
945 Group->getIndex(B) + DistanceToB / static_cast<int64_t>(DesB.Size);
946
947 // Try to insert A into B's group.
948 if (Group->insertMember(A, IndexA, DesA.Align)) {
949 LLVM_DEBUG(dbgs() << "LV: Inserted:" << *A << '\n'
950 << " into the interleave group with" << *B
951 << '\n');
952 InterleaveGroupMap[A] = Group;
953
954 // Set the first load in program order as the insert position.
955 if (A->mayReadFromMemory())
956 Group->setInsertPos(A);
957 }
958 } // Iteration over A accesses.
959 } // Iteration over B accesses.
960
961 // Remove interleaved store groups with gaps.
Florian Hahna4dc7fe2018-11-13 15:58:18 +0000962 for (auto *Group : StoreGroups)
Florian Hahn1086ce22018-09-12 08:01:57 +0000963 if (Group->getNumMembers() != Group->getFactor()) {
964 LLVM_DEBUG(
965 dbgs() << "LV: Invalidate candidate interleaved store group due "
966 "to gaps.\n");
967 releaseGroup(Group);
968 }
969 // Remove interleaved groups with gaps (currently only loads) whose memory
970 // accesses may wrap around. We have to revisit the getPtrStride analysis,
971 // this time with ShouldCheckWrap=true, since collectConstStrideAccesses does
972 // not check wrapping (see documentation there).
973 // FORNOW we use Assume=false;
974 // TODO: Change to Assume=true but making sure we don't exceed the threshold
975 // of runtime SCEV assumptions checks (thereby potentially failing to
976 // vectorize altogether).
977 // Additional optional optimizations:
978 // TODO: If we are peeling the loop and we know that the first pointer doesn't
979 // wrap then we can deduce that all pointers in the group don't wrap.
980 // This means that we can forcefully peel the loop in order to only have to
981 // check the first pointer for no-wrap. When we'll change to use Assume=true
982 // we'll only need at most one runtime check per interleaved group.
Florian Hahna4dc7fe2018-11-13 15:58:18 +0000983 for (auto *Group : LoadGroups) {
Florian Hahn1086ce22018-09-12 08:01:57 +0000984 // Case 1: A full group. Can Skip the checks; For full groups, if the wide
985 // load would wrap around the address space we would do a memory access at
986 // nullptr even without the transformation.
987 if (Group->getNumMembers() == Group->getFactor())
988 continue;
989
990 // Case 2: If first and last members of the group don't wrap this implies
991 // that all the pointers in the group don't wrap.
992 // So we check only group member 0 (which is always guaranteed to exist),
993 // and group member Factor - 1; If the latter doesn't exist we rely on
994 // peeling (if it is a non-reveresed accsess -- see Case 3).
995 Value *FirstMemberPtr = getLoadStorePointerOperand(Group->getMember(0));
996 if (!getPtrStride(PSE, FirstMemberPtr, TheLoop, Strides, /*Assume=*/false,
997 /*ShouldCheckWrap=*/true)) {
998 LLVM_DEBUG(
999 dbgs() << "LV: Invalidate candidate interleaved group due to "
1000 "first group member potentially pointer-wrapping.\n");
1001 releaseGroup(Group);
1002 continue;
1003 }
1004 Instruction *LastMember = Group->getMember(Group->getFactor() - 1);
1005 if (LastMember) {
1006 Value *LastMemberPtr = getLoadStorePointerOperand(LastMember);
1007 if (!getPtrStride(PSE, LastMemberPtr, TheLoop, Strides, /*Assume=*/false,
1008 /*ShouldCheckWrap=*/true)) {
1009 LLVM_DEBUG(
1010 dbgs() << "LV: Invalidate candidate interleaved group due to "
1011 "last group member potentially pointer-wrapping.\n");
1012 releaseGroup(Group);
1013 }
1014 } else {
1015 // Case 3: A non-reversed interleaved load group with gaps: We need
1016 // to execute at least one scalar epilogue iteration. This will ensure
1017 // we don't speculatively access memory out-of-bounds. We only need
1018 // to look for a member at index factor - 1, since every group must have
1019 // a member at index zero.
1020 if (Group->isReverse()) {
1021 LLVM_DEBUG(
1022 dbgs() << "LV: Invalidate candidate interleaved group due to "
1023 "a reverse access with gaps.\n");
1024 releaseGroup(Group);
1025 continue;
1026 }
1027 LLVM_DEBUG(
1028 dbgs() << "LV: Interleaved group requires epilogue iteration.\n");
1029 RequiresScalarEpilogue = true;
1030 }
1031 }
1032}
Dorit Nuzman3ec99fe2018-10-22 06:17:09 +00001033
1034void InterleavedAccessInfo::invalidateGroupsRequiringScalarEpilogue() {
1035 // If no group had triggered the requirement to create an epilogue loop,
1036 // there is nothing to do.
1037 if (!requiresScalarEpilogue())
1038 return;
1039
1040 // Avoid releasing a Group twice.
Florian Hahna4dc7fe2018-11-13 15:58:18 +00001041 SmallPtrSet<InterleaveGroup<Instruction> *, 4> DelSet;
Dorit Nuzman3ec99fe2018-10-22 06:17:09 +00001042 for (auto &I : InterleaveGroupMap) {
Florian Hahna4dc7fe2018-11-13 15:58:18 +00001043 InterleaveGroup<Instruction> *Group = I.second;
Dorit Nuzman3ec99fe2018-10-22 06:17:09 +00001044 if (Group->requiresScalarEpilogue())
1045 DelSet.insert(Group);
1046 }
1047 for (auto *Ptr : DelSet) {
1048 LLVM_DEBUG(
Dorit Nuzman34da6dd2018-10-31 09:57:56 +00001049 dbgs()
Dorit Nuzman3ec99fe2018-10-22 06:17:09 +00001050 << "LV: Invalidate candidate interleaved group due to gaps that "
Dorit Nuzman34da6dd2018-10-31 09:57:56 +00001051 "require a scalar epilogue (not allowed under optsize) and cannot "
1052 "be masked (not enabled). \n");
Dorit Nuzman3ec99fe2018-10-22 06:17:09 +00001053 releaseGroup(Ptr);
1054 }
1055
1056 RequiresScalarEpilogue = false;
1057}
Florian Hahna4dc7fe2018-11-13 15:58:18 +00001058
Florian Hahn86ed3472018-11-13 16:26:34 +00001059template <typename InstT>
1060void InterleaveGroup<InstT>::addMetadata(InstT *NewInst) const {
1061 llvm_unreachable("addMetadata can only be used for Instruction");
1062}
1063
1064namespace llvm {
Florian Hahna4dc7fe2018-11-13 15:58:18 +00001065template <>
1066void InterleaveGroup<Instruction>::addMetadata(Instruction *NewInst) const {
1067 SmallVector<Value *, 4> VL;
1068 std::transform(Members.begin(), Members.end(), std::back_inserter(VL),
1069 [](std::pair<int, Instruction *> p) { return p.second; });
1070 propagateMetadata(NewInst, VL);
1071}
Florian Hahna4dc7fe2018-11-13 15:58:18 +00001072}