blob: a8f80545adc384b0ebd7bfb718c8e466026fa09a [file] [log] [blame]
Adam Nemet04563272015-02-01 16:56:15 +00001//===- LoopAccessAnalysis.cpp - Loop Access Analysis Implementation --------==//
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// The implementation for the loop memory dependence that was originally
11// developed for the loop vectorizer.
12//
13//===----------------------------------------------------------------------===//
14
15#include "llvm/Analysis/LoopAccessAnalysis.h"
16#include "llvm/Analysis/LoopInfo.h"
Adam Nemet7206d7a2015-02-06 18:31:04 +000017#include "llvm/Analysis/ScalarEvolutionExpander.h"
Adam Nemet04563272015-02-01 16:56:15 +000018#include "llvm/Analysis/ValueTracking.h"
19#include "llvm/IR/DiagnosticInfo.h"
20#include "llvm/IR/Dominators.h"
Adam Nemet7206d7a2015-02-06 18:31:04 +000021#include "llvm/IR/IRBuilder.h"
Adam Nemet04563272015-02-01 16:56:15 +000022#include "llvm/Support/Debug.h"
23#include "llvm/Transforms/Utils/VectorUtils.h"
24using namespace llvm;
25
Adam Nemet339f42b2015-02-19 19:15:07 +000026#define DEBUG_TYPE "loop-accesses"
Adam Nemet04563272015-02-01 16:56:15 +000027
Adam Nemetf219c642015-02-19 19:14:52 +000028static cl::opt<unsigned, true>
29VectorizationFactor("force-vector-width", cl::Hidden,
30 cl::desc("Sets the SIMD width. Zero is autoselect."),
31 cl::location(VectorizerParams::VectorizationFactor));
Adam Nemet1d862af2015-02-26 04:39:09 +000032unsigned VectorizerParams::VectorizationFactor;
Adam Nemetf219c642015-02-19 19:14:52 +000033
34static cl::opt<unsigned, true>
35VectorizationInterleave("force-vector-interleave", cl::Hidden,
36 cl::desc("Sets the vectorization interleave count. "
37 "Zero is autoselect."),
38 cl::location(
39 VectorizerParams::VectorizationInterleave));
Adam Nemet1d862af2015-02-26 04:39:09 +000040unsigned VectorizerParams::VectorizationInterleave;
Adam Nemetf219c642015-02-19 19:14:52 +000041
Adam Nemet1d862af2015-02-26 04:39:09 +000042static cl::opt<unsigned, true> RuntimeMemoryCheckThreshold(
43 "runtime-memory-check-threshold", cl::Hidden,
44 cl::desc("When performing memory disambiguation checks at runtime do not "
45 "generate more than this number of comparisons (default = 8)."),
46 cl::location(VectorizerParams::RuntimeMemoryCheckThreshold), cl::init(8));
47unsigned VectorizerParams::RuntimeMemoryCheckThreshold;
Adam Nemetf219c642015-02-19 19:14:52 +000048
49/// Maximum SIMD width.
50const unsigned VectorizerParams::MaxVectorWidth = 64;
51
Adam Nemet9c926572015-03-10 17:40:37 +000052/// \brief We collect interesting dependences up to this threshold.
53static cl::opt<unsigned> MaxInterestingDependence(
54 "max-interesting-dependences", cl::Hidden,
55 cl::desc("Maximum number of interesting dependences collected by "
56 "loop-access analysis (default = 100)"),
57 cl::init(100));
58
Adam Nemetf219c642015-02-19 19:14:52 +000059bool VectorizerParams::isInterleaveForced() {
60 return ::VectorizationInterleave.getNumOccurrences() > 0;
61}
62
Adam Nemet2bd6e982015-02-19 19:15:15 +000063void LoopAccessReport::emitAnalysis(const LoopAccessReport &Message,
64 const Function *TheFunction,
65 const Loop *TheLoop,
66 const char *PassName) {
Adam Nemet04563272015-02-01 16:56:15 +000067 DebugLoc DL = TheLoop->getStartLoc();
Adam Nemet3e876342015-02-19 19:15:13 +000068 if (const Instruction *I = Message.getInstr())
Adam Nemet04563272015-02-01 16:56:15 +000069 DL = I->getDebugLoc();
Adam Nemet339f42b2015-02-19 19:15:07 +000070 emitOptimizationRemarkAnalysis(TheFunction->getContext(), PassName,
Adam Nemet04563272015-02-01 16:56:15 +000071 *TheFunction, DL, Message.str());
72}
73
74Value *llvm::stripIntegerCast(Value *V) {
75 if (CastInst *CI = dyn_cast<CastInst>(V))
76 if (CI->getOperand(0)->getType()->isIntegerTy())
77 return CI->getOperand(0);
78 return V;
79}
80
81const SCEV *llvm::replaceSymbolicStrideSCEV(ScalarEvolution *SE,
Adam Nemet8bc61df2015-02-24 00:41:59 +000082 const ValueToValueMap &PtrToStride,
Adam Nemet04563272015-02-01 16:56:15 +000083 Value *Ptr, Value *OrigPtr) {
84
85 const SCEV *OrigSCEV = SE->getSCEV(Ptr);
86
87 // If there is an entry in the map return the SCEV of the pointer with the
88 // symbolic stride replaced by one.
Adam Nemet8bc61df2015-02-24 00:41:59 +000089 ValueToValueMap::const_iterator SI =
90 PtrToStride.find(OrigPtr ? OrigPtr : Ptr);
Adam Nemet04563272015-02-01 16:56:15 +000091 if (SI != PtrToStride.end()) {
92 Value *StrideVal = SI->second;
93
94 // Strip casts.
95 StrideVal = stripIntegerCast(StrideVal);
96
97 // Replace symbolic stride by one.
98 Value *One = ConstantInt::get(StrideVal->getType(), 1);
99 ValueToValueMap RewriteMap;
100 RewriteMap[StrideVal] = One;
101
102 const SCEV *ByOne =
103 SCEVParameterRewriter::rewrite(OrigSCEV, *SE, RewriteMap, true);
Adam Nemet339f42b2015-02-19 19:15:07 +0000104 DEBUG(dbgs() << "LAA: Replacing SCEV: " << *OrigSCEV << " by: " << *ByOne
Adam Nemet04563272015-02-01 16:56:15 +0000105 << "\n");
106 return ByOne;
107 }
108
109 // Otherwise, just return the SCEV of the original pointer.
110 return SE->getSCEV(Ptr);
111}
112
Adam Nemet8bc61df2015-02-24 00:41:59 +0000113void LoopAccessInfo::RuntimePointerCheck::insert(
114 ScalarEvolution *SE, Loop *Lp, Value *Ptr, bool WritePtr, unsigned DepSetId,
115 unsigned ASId, const ValueToValueMap &Strides) {
Adam Nemet04563272015-02-01 16:56:15 +0000116 // Get the stride replaced scev.
117 const SCEV *Sc = replaceSymbolicStrideSCEV(SE, Strides, Ptr);
118 const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(Sc);
119 assert(AR && "Invalid addrec expression");
120 const SCEV *Ex = SE->getBackedgeTakenCount(Lp);
121 const SCEV *ScEnd = AR->evaluateAtIteration(Ex, *SE);
122 Pointers.push_back(Ptr);
123 Starts.push_back(AR->getStart());
124 Ends.push_back(ScEnd);
125 IsWritePtr.push_back(WritePtr);
126 DependencySetId.push_back(DepSetId);
127 AliasSetId.push_back(ASId);
128}
129
Adam Nemeta8945b72015-02-18 03:43:58 +0000130bool LoopAccessInfo::RuntimePointerCheck::needsChecking(unsigned I,
131 unsigned J) const {
132 // No need to check if two readonly pointers intersect.
133 if (!IsWritePtr[I] && !IsWritePtr[J])
134 return false;
135
136 // Only need to check pointers between two different dependency sets.
137 if (DependencySetId[I] == DependencySetId[J])
138 return false;
139
140 // Only need to check pointers in the same alias set.
141 if (AliasSetId[I] != AliasSetId[J])
142 return false;
143
144 return true;
145}
146
Adam Nemete91cc6e2015-02-19 19:15:19 +0000147void LoopAccessInfo::RuntimePointerCheck::print(raw_ostream &OS,
148 unsigned Depth) const {
149 unsigned NumPointers = Pointers.size();
150 if (NumPointers == 0)
151 return;
152
153 OS.indent(Depth) << "Run-time memory checks:\n";
154 unsigned N = 0;
155 for (unsigned I = 0; I < NumPointers; ++I)
156 for (unsigned J = I + 1; J < NumPointers; ++J)
157 if (needsChecking(I, J)) {
158 OS.indent(Depth) << N++ << ":\n";
159 OS.indent(Depth + 2) << *Pointers[I] << "\n";
160 OS.indent(Depth + 2) << *Pointers[J] << "\n";
161 }
162}
163
Adam Nemet04563272015-02-01 16:56:15 +0000164namespace {
165/// \brief Analyses memory accesses in a loop.
166///
167/// Checks whether run time pointer checks are needed and builds sets for data
168/// dependence checking.
169class AccessAnalysis {
170public:
171 /// \brief Read or write access location.
172 typedef PointerIntPair<Value *, 1, bool> MemAccessInfo;
173 typedef SmallPtrSet<MemAccessInfo, 8> MemAccessInfoSet;
174
Adam Nemetdee666b2015-03-10 17:40:34 +0000175 AccessAnalysis(const DataLayout &Dl, AliasAnalysis *AA,
176 MemoryDepChecker::DepCandidates &DA)
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000177 : DL(Dl), AST(*AA), DepCands(DA), IsRTCheckNeeded(false) {}
Adam Nemet04563272015-02-01 16:56:15 +0000178
179 /// \brief Register a load and whether it is only read from.
180 void addLoad(AliasAnalysis::Location &Loc, bool IsReadOnly) {
181 Value *Ptr = const_cast<Value*>(Loc.Ptr);
182 AST.add(Ptr, AliasAnalysis::UnknownSize, Loc.AATags);
183 Accesses.insert(MemAccessInfo(Ptr, false));
184 if (IsReadOnly)
185 ReadOnlyPtr.insert(Ptr);
186 }
187
188 /// \brief Register a store.
189 void addStore(AliasAnalysis::Location &Loc) {
190 Value *Ptr = const_cast<Value*>(Loc.Ptr);
191 AST.add(Ptr, AliasAnalysis::UnknownSize, Loc.AATags);
192 Accesses.insert(MemAccessInfo(Ptr, true));
193 }
194
195 /// \brief Check whether we can check the pointers at runtime for
196 /// non-intersection.
Adam Nemet30f16e12015-02-18 03:42:35 +0000197 bool canCheckPtrAtRT(LoopAccessInfo::RuntimePointerCheck &RtCheck,
Adam Nemet8bc61df2015-02-24 00:41:59 +0000198 unsigned &NumComparisons, ScalarEvolution *SE,
199 Loop *TheLoop, const ValueToValueMap &Strides,
Adam Nemet04563272015-02-01 16:56:15 +0000200 bool ShouldCheckStride = false);
201
202 /// \brief Goes over all memory accesses, checks whether a RT check is needed
203 /// and builds sets of dependent accesses.
204 void buildDependenceSets() {
205 processMemAccesses();
206 }
207
208 bool isRTCheckNeeded() { return IsRTCheckNeeded; }
209
210 bool isDependencyCheckNeeded() { return !CheckDeps.empty(); }
211 void resetDepChecks() { CheckDeps.clear(); }
212
213 MemAccessInfoSet &getDependenciesToCheck() { return CheckDeps; }
214
215private:
216 typedef SetVector<MemAccessInfo> PtrAccessSet;
217
218 /// \brief Go over all memory access and check whether runtime pointer checks
219 /// are needed /// and build sets of dependency check candidates.
220 void processMemAccesses();
221
222 /// Set of all accesses.
223 PtrAccessSet Accesses;
224
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000225 const DataLayout &DL;
226
Adam Nemet04563272015-02-01 16:56:15 +0000227 /// Set of accesses that need a further dependence check.
228 MemAccessInfoSet CheckDeps;
229
230 /// Set of pointers that are read only.
231 SmallPtrSet<Value*, 16> ReadOnlyPtr;
232
Adam Nemet04563272015-02-01 16:56:15 +0000233 /// An alias set tracker to partition the access set by underlying object and
234 //intrinsic property (such as TBAA metadata).
235 AliasSetTracker AST;
236
237 /// Sets of potentially dependent accesses - members of one set share an
238 /// underlying pointer. The set "CheckDeps" identfies which sets really need a
239 /// dependence check.
Adam Nemetdee666b2015-03-10 17:40:34 +0000240 MemoryDepChecker::DepCandidates &DepCands;
Adam Nemet04563272015-02-01 16:56:15 +0000241
242 bool IsRTCheckNeeded;
243};
244
245} // end anonymous namespace
246
247/// \brief Check whether a pointer can participate in a runtime bounds check.
Adam Nemet8bc61df2015-02-24 00:41:59 +0000248static bool hasComputableBounds(ScalarEvolution *SE,
249 const ValueToValueMap &Strides, Value *Ptr) {
Adam Nemet04563272015-02-01 16:56:15 +0000250 const SCEV *PtrScev = replaceSymbolicStrideSCEV(SE, Strides, Ptr);
251 const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(PtrScev);
252 if (!AR)
253 return false;
254
255 return AR->isAffine();
256}
257
258/// \brief Check the stride of the pointer and ensure that it does not wrap in
259/// the address space.
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000260static int isStridedPtr(ScalarEvolution *SE, Value *Ptr, const Loop *Lp,
261 const ValueToValueMap &StridesMap);
Adam Nemet04563272015-02-01 16:56:15 +0000262
263bool AccessAnalysis::canCheckPtrAtRT(
Adam Nemet8bc61df2015-02-24 00:41:59 +0000264 LoopAccessInfo::RuntimePointerCheck &RtCheck, unsigned &NumComparisons,
265 ScalarEvolution *SE, Loop *TheLoop, const ValueToValueMap &StridesMap,
266 bool ShouldCheckStride) {
Adam Nemet04563272015-02-01 16:56:15 +0000267 // Find pointers with computable bounds. We are going to use this information
268 // to place a runtime bound check.
269 bool CanDoRT = true;
270
271 bool IsDepCheckNeeded = isDependencyCheckNeeded();
272 NumComparisons = 0;
273
274 // We assign a consecutive id to access from different alias sets.
275 // Accesses between different groups doesn't need to be checked.
276 unsigned ASId = 1;
277 for (auto &AS : AST) {
278 unsigned NumReadPtrChecks = 0;
279 unsigned NumWritePtrChecks = 0;
280
281 // We assign consecutive id to access from different dependence sets.
282 // Accesses within the same set don't need a runtime check.
283 unsigned RunningDepId = 1;
284 DenseMap<Value *, unsigned> DepSetId;
285
286 for (auto A : AS) {
287 Value *Ptr = A.getValue();
288 bool IsWrite = Accesses.count(MemAccessInfo(Ptr, true));
289 MemAccessInfo Access(Ptr, IsWrite);
290
291 if (IsWrite)
292 ++NumWritePtrChecks;
293 else
294 ++NumReadPtrChecks;
295
296 if (hasComputableBounds(SE, StridesMap, Ptr) &&
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000297 // When we run after a failing dependency check we have to make sure
298 // we don't have wrapping pointers.
Adam Nemet04563272015-02-01 16:56:15 +0000299 (!ShouldCheckStride ||
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000300 isStridedPtr(SE, Ptr, TheLoop, StridesMap) == 1)) {
Adam Nemet04563272015-02-01 16:56:15 +0000301 // The id of the dependence set.
302 unsigned DepId;
303
304 if (IsDepCheckNeeded) {
305 Value *Leader = DepCands.getLeaderValue(Access).getPointer();
306 unsigned &LeaderId = DepSetId[Leader];
307 if (!LeaderId)
308 LeaderId = RunningDepId++;
309 DepId = LeaderId;
310 } else
311 // Each access has its own dependence set.
312 DepId = RunningDepId++;
313
314 RtCheck.insert(SE, TheLoop, Ptr, IsWrite, DepId, ASId, StridesMap);
315
Adam Nemet339f42b2015-02-19 19:15:07 +0000316 DEBUG(dbgs() << "LAA: Found a runtime check ptr:" << *Ptr << '\n');
Adam Nemet04563272015-02-01 16:56:15 +0000317 } else {
318 CanDoRT = false;
319 }
320 }
321
322 if (IsDepCheckNeeded && CanDoRT && RunningDepId == 2)
323 NumComparisons += 0; // Only one dependence set.
324 else {
325 NumComparisons += (NumWritePtrChecks * (NumReadPtrChecks +
326 NumWritePtrChecks - 1));
327 }
328
329 ++ASId;
330 }
331
332 // If the pointers that we would use for the bounds comparison have different
333 // address spaces, assume the values aren't directly comparable, so we can't
334 // use them for the runtime check. We also have to assume they could
335 // overlap. In the future there should be metadata for whether address spaces
336 // are disjoint.
337 unsigned NumPointers = RtCheck.Pointers.size();
338 for (unsigned i = 0; i < NumPointers; ++i) {
339 for (unsigned j = i + 1; j < NumPointers; ++j) {
340 // Only need to check pointers between two different dependency sets.
341 if (RtCheck.DependencySetId[i] == RtCheck.DependencySetId[j])
342 continue;
343 // Only need to check pointers in the same alias set.
344 if (RtCheck.AliasSetId[i] != RtCheck.AliasSetId[j])
345 continue;
346
347 Value *PtrI = RtCheck.Pointers[i];
348 Value *PtrJ = RtCheck.Pointers[j];
349
350 unsigned ASi = PtrI->getType()->getPointerAddressSpace();
351 unsigned ASj = PtrJ->getType()->getPointerAddressSpace();
352 if (ASi != ASj) {
Adam Nemet339f42b2015-02-19 19:15:07 +0000353 DEBUG(dbgs() << "LAA: Runtime check would require comparison between"
Adam Nemet04d41632015-02-19 19:14:34 +0000354 " different address spaces\n");
Adam Nemet04563272015-02-01 16:56:15 +0000355 return false;
356 }
357 }
358 }
359
360 return CanDoRT;
361}
362
363void AccessAnalysis::processMemAccesses() {
364 // We process the set twice: first we process read-write pointers, last we
365 // process read-only pointers. This allows us to skip dependence tests for
366 // read-only pointers.
367
Adam Nemet339f42b2015-02-19 19:15:07 +0000368 DEBUG(dbgs() << "LAA: Processing memory accesses...\n");
Adam Nemet04563272015-02-01 16:56:15 +0000369 DEBUG(dbgs() << " AST: "; AST.dump());
Adam Nemet9c926572015-03-10 17:40:37 +0000370 DEBUG(dbgs() << "LAA: Accesses(" << Accesses.size() << "):\n");
Adam Nemet04563272015-02-01 16:56:15 +0000371 DEBUG({
372 for (auto A : Accesses)
373 dbgs() << "\t" << *A.getPointer() << " (" <<
374 (A.getInt() ? "write" : (ReadOnlyPtr.count(A.getPointer()) ?
375 "read-only" : "read")) << ")\n";
376 });
377
378 // The AliasSetTracker has nicely partitioned our pointers by metadata
379 // compatibility and potential for underlying-object overlap. As a result, we
380 // only need to check for potential pointer dependencies within each alias
381 // set.
382 for (auto &AS : AST) {
383 // Note that both the alias-set tracker and the alias sets themselves used
384 // linked lists internally and so the iteration order here is deterministic
385 // (matching the original instruction order within each set).
386
387 bool SetHasWrite = false;
388
389 // Map of pointers to last access encountered.
390 typedef DenseMap<Value*, MemAccessInfo> UnderlyingObjToAccessMap;
391 UnderlyingObjToAccessMap ObjToLastAccess;
392
393 // Set of access to check after all writes have been processed.
394 PtrAccessSet DeferredAccesses;
395
396 // Iterate over each alias set twice, once to process read/write pointers,
397 // and then to process read-only pointers.
398 for (int SetIteration = 0; SetIteration < 2; ++SetIteration) {
399 bool UseDeferred = SetIteration > 0;
400 PtrAccessSet &S = UseDeferred ? DeferredAccesses : Accesses;
401
402 for (auto AV : AS) {
403 Value *Ptr = AV.getValue();
404
405 // For a single memory access in AliasSetTracker, Accesses may contain
406 // both read and write, and they both need to be handled for CheckDeps.
407 for (auto AC : S) {
408 if (AC.getPointer() != Ptr)
409 continue;
410
411 bool IsWrite = AC.getInt();
412
413 // If we're using the deferred access set, then it contains only
414 // reads.
415 bool IsReadOnlyPtr = ReadOnlyPtr.count(Ptr) && !IsWrite;
416 if (UseDeferred && !IsReadOnlyPtr)
417 continue;
418 // Otherwise, the pointer must be in the PtrAccessSet, either as a
419 // read or a write.
420 assert(((IsReadOnlyPtr && UseDeferred) || IsWrite ||
421 S.count(MemAccessInfo(Ptr, false))) &&
422 "Alias-set pointer not in the access set?");
423
424 MemAccessInfo Access(Ptr, IsWrite);
425 DepCands.insert(Access);
426
427 // Memorize read-only pointers for later processing and skip them in
428 // the first round (they need to be checked after we have seen all
429 // write pointers). Note: we also mark pointer that are not
430 // consecutive as "read-only" pointers (so that we check
431 // "a[b[i]] +="). Hence, we need the second check for "!IsWrite".
432 if (!UseDeferred && IsReadOnlyPtr) {
433 DeferredAccesses.insert(Access);
434 continue;
435 }
436
437 // If this is a write - check other reads and writes for conflicts. If
438 // this is a read only check other writes for conflicts (but only if
439 // there is no other write to the ptr - this is an optimization to
440 // catch "a[i] = a[i] + " without having to do a dependence check).
441 if ((IsWrite || IsReadOnlyPtr) && SetHasWrite) {
442 CheckDeps.insert(Access);
443 IsRTCheckNeeded = true;
444 }
445
446 if (IsWrite)
447 SetHasWrite = true;
448
449 // Create sets of pointers connected by a shared alias set and
450 // underlying object.
451 typedef SmallVector<Value *, 16> ValueVector;
452 ValueVector TempObjects;
453 GetUnderlyingObjects(Ptr, TempObjects, DL);
454 for (Value *UnderlyingObj : TempObjects) {
455 UnderlyingObjToAccessMap::iterator Prev =
456 ObjToLastAccess.find(UnderlyingObj);
457 if (Prev != ObjToLastAccess.end())
458 DepCands.unionSets(Access, Prev->second);
459
460 ObjToLastAccess[UnderlyingObj] = Access;
461 }
462 }
463 }
464 }
465 }
466}
467
Adam Nemet04563272015-02-01 16:56:15 +0000468static bool isInBoundsGep(Value *Ptr) {
469 if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(Ptr))
470 return GEP->isInBounds();
471 return false;
472}
473
474/// \brief Check whether the access through \p Ptr has a constant stride.
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000475static int isStridedPtr(ScalarEvolution *SE, Value *Ptr, const Loop *Lp,
476 const ValueToValueMap &StridesMap) {
Adam Nemet04563272015-02-01 16:56:15 +0000477 const Type *Ty = Ptr->getType();
478 assert(Ty->isPointerTy() && "Unexpected non-ptr");
479
480 // Make sure that the pointer does not point to aggregate types.
481 const PointerType *PtrTy = cast<PointerType>(Ty);
482 if (PtrTy->getElementType()->isAggregateType()) {
Adam Nemet339f42b2015-02-19 19:15:07 +0000483 DEBUG(dbgs() << "LAA: Bad stride - Not a pointer to a scalar type"
484 << *Ptr << "\n");
Adam Nemet04563272015-02-01 16:56:15 +0000485 return 0;
486 }
487
488 const SCEV *PtrScev = replaceSymbolicStrideSCEV(SE, StridesMap, Ptr);
489
490 const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(PtrScev);
491 if (!AR) {
Adam Nemet339f42b2015-02-19 19:15:07 +0000492 DEBUG(dbgs() << "LAA: Bad stride - Not an AddRecExpr pointer "
Adam Nemet04d41632015-02-19 19:14:34 +0000493 << *Ptr << " SCEV: " << *PtrScev << "\n");
Adam Nemet04563272015-02-01 16:56:15 +0000494 return 0;
495 }
496
497 // The accesss function must stride over the innermost loop.
498 if (Lp != AR->getLoop()) {
Adam Nemet339f42b2015-02-19 19:15:07 +0000499 DEBUG(dbgs() << "LAA: Bad stride - Not striding over innermost loop " <<
Adam Nemet04d41632015-02-19 19:14:34 +0000500 *Ptr << " SCEV: " << *PtrScev << "\n");
Adam Nemet04563272015-02-01 16:56:15 +0000501 }
502
503 // The address calculation must not wrap. Otherwise, a dependence could be
504 // inverted.
505 // An inbounds getelementptr that is a AddRec with a unit stride
506 // cannot wrap per definition. The unit stride requirement is checked later.
507 // An getelementptr without an inbounds attribute and unit stride would have
508 // to access the pointer value "0" which is undefined behavior in address
509 // space 0, therefore we can also vectorize this case.
510 bool IsInBoundsGEP = isInBoundsGep(Ptr);
511 bool IsNoWrapAddRec = AR->getNoWrapFlags(SCEV::NoWrapMask);
512 bool IsInAddressSpaceZero = PtrTy->getAddressSpace() == 0;
513 if (!IsNoWrapAddRec && !IsInBoundsGEP && !IsInAddressSpaceZero) {
Adam Nemet339f42b2015-02-19 19:15:07 +0000514 DEBUG(dbgs() << "LAA: Bad stride - Pointer may wrap in the address space "
Adam Nemet04d41632015-02-19 19:14:34 +0000515 << *Ptr << " SCEV: " << *PtrScev << "\n");
Adam Nemet04563272015-02-01 16:56:15 +0000516 return 0;
517 }
518
519 // Check the step is constant.
520 const SCEV *Step = AR->getStepRecurrence(*SE);
521
522 // Calculate the pointer stride and check if it is consecutive.
523 const SCEVConstant *C = dyn_cast<SCEVConstant>(Step);
524 if (!C) {
Adam Nemet339f42b2015-02-19 19:15:07 +0000525 DEBUG(dbgs() << "LAA: Bad stride - Not a constant strided " << *Ptr <<
Adam Nemet04d41632015-02-19 19:14:34 +0000526 " SCEV: " << *PtrScev << "\n");
Adam Nemet04563272015-02-01 16:56:15 +0000527 return 0;
528 }
529
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000530 auto &DL = Lp->getHeader()->getModule()->getDataLayout();
531 int64_t Size = DL.getTypeAllocSize(PtrTy->getElementType());
Adam Nemet04563272015-02-01 16:56:15 +0000532 const APInt &APStepVal = C->getValue()->getValue();
533
534 // Huge step value - give up.
535 if (APStepVal.getBitWidth() > 64)
536 return 0;
537
538 int64_t StepVal = APStepVal.getSExtValue();
539
540 // Strided access.
541 int64_t Stride = StepVal / Size;
542 int64_t Rem = StepVal % Size;
543 if (Rem)
544 return 0;
545
546 // If the SCEV could wrap but we have an inbounds gep with a unit stride we
547 // know we can't "wrap around the address space". In case of address space
548 // zero we know that this won't happen without triggering undefined behavior.
549 if (!IsNoWrapAddRec && (IsInBoundsGEP || IsInAddressSpaceZero) &&
550 Stride != 1 && Stride != -1)
551 return 0;
552
553 return Stride;
554}
555
Adam Nemet9c926572015-03-10 17:40:37 +0000556bool MemoryDepChecker::Dependence::isSafeForVectorization(DepType Type) {
557 switch (Type) {
558 case NoDep:
559 case Forward:
560 case BackwardVectorizable:
561 return true;
562
563 case Unknown:
564 case ForwardButPreventsForwarding:
565 case Backward:
566 case BackwardVectorizableButPreventsForwarding:
567 return false;
568 }
569}
570
571bool MemoryDepChecker::Dependence::isInterestingDependence(DepType Type) {
572 switch (Type) {
573 case NoDep:
574 case Forward:
575 return false;
576
577 case BackwardVectorizable:
578 case Unknown:
579 case ForwardButPreventsForwarding:
580 case Backward:
581 case BackwardVectorizableButPreventsForwarding:
582 return true;
583 }
584}
585
586bool MemoryDepChecker::Dependence::isPossiblyBackward() const {
587 switch (Type) {
588 case NoDep:
589 case Forward:
590 case ForwardButPreventsForwarding:
591 return false;
592
593 case Unknown:
594 case BackwardVectorizable:
595 case Backward:
596 case BackwardVectorizableButPreventsForwarding:
597 return true;
598 }
599}
600
Adam Nemet04563272015-02-01 16:56:15 +0000601bool MemoryDepChecker::couldPreventStoreLoadForward(unsigned Distance,
602 unsigned TypeByteSize) {
603 // If loads occur at a distance that is not a multiple of a feasible vector
604 // factor store-load forwarding does not take place.
605 // Positive dependences might cause troubles because vectorizing them might
606 // prevent store-load forwarding making vectorized code run a lot slower.
607 // a[i] = a[i-3] ^ a[i-8];
608 // The stores to a[i:i+1] don't align with the stores to a[i-3:i-2] and
609 // hence on your typical architecture store-load forwarding does not take
610 // place. Vectorizing in such cases does not make sense.
611 // Store-load forwarding distance.
612 const unsigned NumCyclesForStoreLoadThroughMemory = 8*TypeByteSize;
613 // Maximum vector factor.
Adam Nemetf219c642015-02-19 19:14:52 +0000614 unsigned MaxVFWithoutSLForwardIssues =
615 VectorizerParams::MaxVectorWidth * TypeByteSize;
Adam Nemet04d41632015-02-19 19:14:34 +0000616 if(MaxSafeDepDistBytes < MaxVFWithoutSLForwardIssues)
Adam Nemet04563272015-02-01 16:56:15 +0000617 MaxVFWithoutSLForwardIssues = MaxSafeDepDistBytes;
618
619 for (unsigned vf = 2*TypeByteSize; vf <= MaxVFWithoutSLForwardIssues;
620 vf *= 2) {
621 if (Distance % vf && Distance / vf < NumCyclesForStoreLoadThroughMemory) {
622 MaxVFWithoutSLForwardIssues = (vf >>=1);
623 break;
624 }
625 }
626
Adam Nemet04d41632015-02-19 19:14:34 +0000627 if (MaxVFWithoutSLForwardIssues< 2*TypeByteSize) {
Adam Nemet339f42b2015-02-19 19:15:07 +0000628 DEBUG(dbgs() << "LAA: Distance " << Distance <<
Adam Nemet04d41632015-02-19 19:14:34 +0000629 " that could cause a store-load forwarding conflict\n");
Adam Nemet04563272015-02-01 16:56:15 +0000630 return true;
631 }
632
633 if (MaxVFWithoutSLForwardIssues < MaxSafeDepDistBytes &&
Adam Nemetf219c642015-02-19 19:14:52 +0000634 MaxVFWithoutSLForwardIssues !=
635 VectorizerParams::MaxVectorWidth * TypeByteSize)
Adam Nemet04563272015-02-01 16:56:15 +0000636 MaxSafeDepDistBytes = MaxVFWithoutSLForwardIssues;
637 return false;
638}
639
Adam Nemet9c926572015-03-10 17:40:37 +0000640MemoryDepChecker::Dependence::DepType
641MemoryDepChecker::isDependent(const MemAccessInfo &A, unsigned AIdx,
642 const MemAccessInfo &B, unsigned BIdx,
643 const ValueToValueMap &Strides) {
Adam Nemet04563272015-02-01 16:56:15 +0000644 assert (AIdx < BIdx && "Must pass arguments in program order");
645
646 Value *APtr = A.getPointer();
647 Value *BPtr = B.getPointer();
648 bool AIsWrite = A.getInt();
649 bool BIsWrite = B.getInt();
650
651 // Two reads are independent.
652 if (!AIsWrite && !BIsWrite)
Adam Nemet9c926572015-03-10 17:40:37 +0000653 return Dependence::NoDep;
Adam Nemet04563272015-02-01 16:56:15 +0000654
655 // We cannot check pointers in different address spaces.
656 if (APtr->getType()->getPointerAddressSpace() !=
657 BPtr->getType()->getPointerAddressSpace())
Adam Nemet9c926572015-03-10 17:40:37 +0000658 return Dependence::Unknown;
Adam Nemet04563272015-02-01 16:56:15 +0000659
660 const SCEV *AScev = replaceSymbolicStrideSCEV(SE, Strides, APtr);
661 const SCEV *BScev = replaceSymbolicStrideSCEV(SE, Strides, BPtr);
662
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000663 int StrideAPtr = isStridedPtr(SE, APtr, InnermostLoop, Strides);
664 int StrideBPtr = isStridedPtr(SE, BPtr, InnermostLoop, Strides);
Adam Nemet04563272015-02-01 16:56:15 +0000665
666 const SCEV *Src = AScev;
667 const SCEV *Sink = BScev;
668
669 // If the induction step is negative we have to invert source and sink of the
670 // dependence.
671 if (StrideAPtr < 0) {
672 //Src = BScev;
673 //Sink = AScev;
674 std::swap(APtr, BPtr);
675 std::swap(Src, Sink);
676 std::swap(AIsWrite, BIsWrite);
677 std::swap(AIdx, BIdx);
678 std::swap(StrideAPtr, StrideBPtr);
679 }
680
681 const SCEV *Dist = SE->getMinusSCEV(Sink, Src);
682
Adam Nemet339f42b2015-02-19 19:15:07 +0000683 DEBUG(dbgs() << "LAA: Src Scev: " << *Src << "Sink Scev: " << *Sink
Adam Nemet04d41632015-02-19 19:14:34 +0000684 << "(Induction step: " << StrideAPtr << ")\n");
Adam Nemet339f42b2015-02-19 19:15:07 +0000685 DEBUG(dbgs() << "LAA: Distance for " << *InstMap[AIdx] << " to "
Adam Nemet04d41632015-02-19 19:14:34 +0000686 << *InstMap[BIdx] << ": " << *Dist << "\n");
Adam Nemet04563272015-02-01 16:56:15 +0000687
688 // Need consecutive accesses. We don't want to vectorize
689 // "A[B[i]] += ..." and similar code or pointer arithmetic that could wrap in
690 // the address space.
691 if (!StrideAPtr || !StrideBPtr || StrideAPtr != StrideBPtr){
692 DEBUG(dbgs() << "Non-consecutive pointer access\n");
Adam Nemet9c926572015-03-10 17:40:37 +0000693 return Dependence::Unknown;
Adam Nemet04563272015-02-01 16:56:15 +0000694 }
695
696 const SCEVConstant *C = dyn_cast<SCEVConstant>(Dist);
697 if (!C) {
Adam Nemet339f42b2015-02-19 19:15:07 +0000698 DEBUG(dbgs() << "LAA: Dependence because of non-constant distance\n");
Adam Nemet04563272015-02-01 16:56:15 +0000699 ShouldRetryWithRuntimeCheck = true;
Adam Nemet9c926572015-03-10 17:40:37 +0000700 return Dependence::Unknown;
Adam Nemet04563272015-02-01 16:56:15 +0000701 }
702
703 Type *ATy = APtr->getType()->getPointerElementType();
704 Type *BTy = BPtr->getType()->getPointerElementType();
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000705 auto &DL = InnermostLoop->getHeader()->getModule()->getDataLayout();
706 unsigned TypeByteSize = DL.getTypeAllocSize(ATy);
Adam Nemet04563272015-02-01 16:56:15 +0000707
708 // Negative distances are not plausible dependencies.
709 const APInt &Val = C->getValue()->getValue();
710 if (Val.isNegative()) {
711 bool IsTrueDataDependence = (AIsWrite && !BIsWrite);
712 if (IsTrueDataDependence &&
713 (couldPreventStoreLoadForward(Val.abs().getZExtValue(), TypeByteSize) ||
714 ATy != BTy))
Adam Nemet9c926572015-03-10 17:40:37 +0000715 return Dependence::ForwardButPreventsForwarding;
Adam Nemet04563272015-02-01 16:56:15 +0000716
Adam Nemet339f42b2015-02-19 19:15:07 +0000717 DEBUG(dbgs() << "LAA: Dependence is negative: NoDep\n");
Adam Nemet9c926572015-03-10 17:40:37 +0000718 return Dependence::Forward;
Adam Nemet04563272015-02-01 16:56:15 +0000719 }
720
721 // Write to the same location with the same size.
722 // Could be improved to assert type sizes are the same (i32 == float, etc).
723 if (Val == 0) {
724 if (ATy == BTy)
Adam Nemet9c926572015-03-10 17:40:37 +0000725 return Dependence::NoDep;
Adam Nemet339f42b2015-02-19 19:15:07 +0000726 DEBUG(dbgs() << "LAA: Zero dependence difference but different types\n");
Adam Nemet9c926572015-03-10 17:40:37 +0000727 return Dependence::Unknown;
Adam Nemet04563272015-02-01 16:56:15 +0000728 }
729
730 assert(Val.isStrictlyPositive() && "Expect a positive value");
731
Adam Nemet04563272015-02-01 16:56:15 +0000732 if (ATy != BTy) {
Adam Nemet04d41632015-02-19 19:14:34 +0000733 DEBUG(dbgs() <<
Adam Nemet339f42b2015-02-19 19:15:07 +0000734 "LAA: ReadWrite-Write positive dependency with different types\n");
Adam Nemet9c926572015-03-10 17:40:37 +0000735 return Dependence::Unknown;
Adam Nemet04563272015-02-01 16:56:15 +0000736 }
737
738 unsigned Distance = (unsigned) Val.getZExtValue();
739
740 // Bail out early if passed-in parameters make vectorization not feasible.
Adam Nemetf219c642015-02-19 19:14:52 +0000741 unsigned ForcedFactor = (VectorizerParams::VectorizationFactor ?
742 VectorizerParams::VectorizationFactor : 1);
743 unsigned ForcedUnroll = (VectorizerParams::VectorizationInterleave ?
744 VectorizerParams::VectorizationInterleave : 1);
Adam Nemet04563272015-02-01 16:56:15 +0000745
746 // The distance must be bigger than the size needed for a vectorized version
747 // of the operation and the size of the vectorized operation must not be
748 // bigger than the currrent maximum size.
749 if (Distance < 2*TypeByteSize ||
750 2*TypeByteSize > MaxSafeDepDistBytes ||
751 Distance < TypeByteSize * ForcedUnroll * ForcedFactor) {
Adam Nemet339f42b2015-02-19 19:15:07 +0000752 DEBUG(dbgs() << "LAA: Failure because of Positive distance "
Adam Nemet04d41632015-02-19 19:14:34 +0000753 << Val.getSExtValue() << '\n');
Adam Nemet9c926572015-03-10 17:40:37 +0000754 return Dependence::Backward;
Adam Nemet04563272015-02-01 16:56:15 +0000755 }
756
Adam Nemet9cc0c392015-02-26 17:58:48 +0000757 // Positive distance bigger than max vectorization factor.
Adam Nemet04563272015-02-01 16:56:15 +0000758 MaxSafeDepDistBytes = Distance < MaxSafeDepDistBytes ?
759 Distance : MaxSafeDepDistBytes;
760
761 bool IsTrueDataDependence = (!AIsWrite && BIsWrite);
762 if (IsTrueDataDependence &&
763 couldPreventStoreLoadForward(Distance, TypeByteSize))
Adam Nemet9c926572015-03-10 17:40:37 +0000764 return Dependence::BackwardVectorizableButPreventsForwarding;
Adam Nemet04563272015-02-01 16:56:15 +0000765
Adam Nemet339f42b2015-02-19 19:15:07 +0000766 DEBUG(dbgs() << "LAA: Positive distance " << Val.getSExtValue() <<
Adam Nemet04d41632015-02-19 19:14:34 +0000767 " with max VF = " << MaxSafeDepDistBytes / TypeByteSize << '\n');
Adam Nemet04563272015-02-01 16:56:15 +0000768
Adam Nemet9c926572015-03-10 17:40:37 +0000769 return Dependence::BackwardVectorizable;
Adam Nemet04563272015-02-01 16:56:15 +0000770}
771
Adam Nemetdee666b2015-03-10 17:40:34 +0000772bool MemoryDepChecker::areDepsSafe(DepCandidates &AccessSets,
Adam Nemet04563272015-02-01 16:56:15 +0000773 MemAccessInfoSet &CheckDeps,
Adam Nemet8bc61df2015-02-24 00:41:59 +0000774 const ValueToValueMap &Strides) {
Adam Nemet04563272015-02-01 16:56:15 +0000775
776 MaxSafeDepDistBytes = -1U;
777 while (!CheckDeps.empty()) {
778 MemAccessInfo CurAccess = *CheckDeps.begin();
779
780 // Get the relevant memory access set.
781 EquivalenceClasses<MemAccessInfo>::iterator I =
782 AccessSets.findValue(AccessSets.getLeaderValue(CurAccess));
783
784 // Check accesses within this set.
785 EquivalenceClasses<MemAccessInfo>::member_iterator AI, AE;
786 AI = AccessSets.member_begin(I), AE = AccessSets.member_end();
787
788 // Check every access pair.
789 while (AI != AE) {
790 CheckDeps.erase(*AI);
791 EquivalenceClasses<MemAccessInfo>::member_iterator OI = std::next(AI);
792 while (OI != AE) {
793 // Check every accessing instruction pair in program order.
794 for (std::vector<unsigned>::iterator I1 = Accesses[*AI].begin(),
795 I1E = Accesses[*AI].end(); I1 != I1E; ++I1)
796 for (std::vector<unsigned>::iterator I2 = Accesses[*OI].begin(),
797 I2E = Accesses[*OI].end(); I2 != I2E; ++I2) {
Adam Nemet9c926572015-03-10 17:40:37 +0000798 auto A = std::make_pair(&*AI, *I1);
799 auto B = std::make_pair(&*OI, *I2);
800
801 assert(*I1 != *I2);
802 if (*I1 > *I2)
803 std::swap(A, B);
804
805 Dependence::DepType Type =
806 isDependent(*A.first, A.second, *B.first, B.second, Strides);
807 SafeForVectorization &= Dependence::isSafeForVectorization(Type);
808
809 // Gather dependences unless we accumulated MaxInterestingDependence
810 // dependences. In that case return as soon as we find the first
811 // unsafe dependence. This puts a limit on this quadratic
812 // algorithm.
813 if (RecordInterestingDependences) {
814 if (Dependence::isInterestingDependence(Type))
815 InterestingDependences.push_back(
816 Dependence(A.second, B.second, Type));
817
818 if (InterestingDependences.size() >= MaxInterestingDependence) {
819 RecordInterestingDependences = false;
820 InterestingDependences.clear();
821 DEBUG(dbgs() << "Too many dependences, stopped recording\n");
822 }
823 }
824 if (!RecordInterestingDependences && !SafeForVectorization)
Adam Nemet04563272015-02-01 16:56:15 +0000825 return false;
826 }
827 ++OI;
828 }
829 AI++;
830 }
831 }
Adam Nemet9c926572015-03-10 17:40:37 +0000832
833 DEBUG(dbgs() << "Total Interesting Dependences: "
834 << InterestingDependences.size() << "\n");
835 return SafeForVectorization;
Adam Nemet04563272015-02-01 16:56:15 +0000836}
837
Adam Nemet929c38e2015-02-19 19:15:10 +0000838bool LoopAccessInfo::canAnalyzeLoop() {
839 // We can only analyze innermost loops.
840 if (!TheLoop->empty()) {
Adam Nemet2bd6e982015-02-19 19:15:15 +0000841 emitAnalysis(LoopAccessReport() << "loop is not the innermost loop");
Adam Nemet929c38e2015-02-19 19:15:10 +0000842 return false;
843 }
844
845 // We must have a single backedge.
846 if (TheLoop->getNumBackEdges() != 1) {
847 emitAnalysis(
Adam Nemet2bd6e982015-02-19 19:15:15 +0000848 LoopAccessReport() <<
Adam Nemet929c38e2015-02-19 19:15:10 +0000849 "loop control flow is not understood by analyzer");
850 return false;
851 }
852
853 // We must have a single exiting block.
854 if (!TheLoop->getExitingBlock()) {
855 emitAnalysis(
Adam Nemet2bd6e982015-02-19 19:15:15 +0000856 LoopAccessReport() <<
Adam Nemet929c38e2015-02-19 19:15:10 +0000857 "loop control flow is not understood by analyzer");
858 return false;
859 }
860
861 // We only handle bottom-tested loops, i.e. loop in which the condition is
862 // checked at the end of each iteration. With that we can assume that all
863 // instructions in the loop are executed the same number of times.
864 if (TheLoop->getExitingBlock() != TheLoop->getLoopLatch()) {
865 emitAnalysis(
Adam Nemet2bd6e982015-02-19 19:15:15 +0000866 LoopAccessReport() <<
Adam Nemet929c38e2015-02-19 19:15:10 +0000867 "loop control flow is not understood by analyzer");
868 return false;
869 }
870
871 // We need to have a loop header.
872 DEBUG(dbgs() << "LAA: Found a loop: " <<
873 TheLoop->getHeader()->getName() << '\n');
874
875 // ScalarEvolution needs to be able to find the exit count.
876 const SCEV *ExitCount = SE->getBackedgeTakenCount(TheLoop);
877 if (ExitCount == SE->getCouldNotCompute()) {
Adam Nemet2bd6e982015-02-19 19:15:15 +0000878 emitAnalysis(LoopAccessReport() <<
Adam Nemet929c38e2015-02-19 19:15:10 +0000879 "could not determine number of loop iterations");
880 DEBUG(dbgs() << "LAA: SCEV could not compute the loop exit count.\n");
881 return false;
882 }
883
884 return true;
885}
886
Adam Nemet8bc61df2015-02-24 00:41:59 +0000887void LoopAccessInfo::analyzeLoop(const ValueToValueMap &Strides) {
Adam Nemet04563272015-02-01 16:56:15 +0000888
889 typedef SmallVector<Value*, 16> ValueVector;
890 typedef SmallPtrSet<Value*, 16> ValueSet;
891
892 // Holds the Load and Store *instructions*.
893 ValueVector Loads;
894 ValueVector Stores;
895
896 // Holds all the different accesses in the loop.
897 unsigned NumReads = 0;
898 unsigned NumReadWrites = 0;
899
900 PtrRtCheck.Pointers.clear();
901 PtrRtCheck.Need = false;
902
903 const bool IsAnnotatedParallel = TheLoop->isAnnotatedParallel();
Adam Nemet04563272015-02-01 16:56:15 +0000904
905 // For each block.
906 for (Loop::block_iterator bb = TheLoop->block_begin(),
907 be = TheLoop->block_end(); bb != be; ++bb) {
908
909 // Scan the BB and collect legal loads and stores.
910 for (BasicBlock::iterator it = (*bb)->begin(), e = (*bb)->end(); it != e;
911 ++it) {
912
913 // If this is a load, save it. If this instruction can read from memory
914 // but is not a load, then we quit. Notice that we don't handle function
915 // calls that read or write.
916 if (it->mayReadFromMemory()) {
917 // Many math library functions read the rounding mode. We will only
918 // vectorize a loop if it contains known function calls that don't set
919 // the flag. Therefore, it is safe to ignore this read from memory.
920 CallInst *Call = dyn_cast<CallInst>(it);
921 if (Call && getIntrinsicIDForCall(Call, TLI))
922 continue;
923
924 LoadInst *Ld = dyn_cast<LoadInst>(it);
925 if (!Ld || (!Ld->isSimple() && !IsAnnotatedParallel)) {
Adam Nemet2bd6e982015-02-19 19:15:15 +0000926 emitAnalysis(LoopAccessReport(Ld)
Adam Nemet04563272015-02-01 16:56:15 +0000927 << "read with atomic ordering or volatile read");
Adam Nemet339f42b2015-02-19 19:15:07 +0000928 DEBUG(dbgs() << "LAA: Found a non-simple load.\n");
Adam Nemet436018c2015-02-19 19:15:00 +0000929 CanVecMem = false;
930 return;
Adam Nemet04563272015-02-01 16:56:15 +0000931 }
932 NumLoads++;
933 Loads.push_back(Ld);
934 DepChecker.addAccess(Ld);
935 continue;
936 }
937
938 // Save 'store' instructions. Abort if other instructions write to memory.
939 if (it->mayWriteToMemory()) {
940 StoreInst *St = dyn_cast<StoreInst>(it);
941 if (!St) {
Adam Nemet2bd6e982015-02-19 19:15:15 +0000942 emitAnalysis(LoopAccessReport(it) <<
Adam Nemet04d41632015-02-19 19:14:34 +0000943 "instruction cannot be vectorized");
Adam Nemet436018c2015-02-19 19:15:00 +0000944 CanVecMem = false;
945 return;
Adam Nemet04563272015-02-01 16:56:15 +0000946 }
947 if (!St->isSimple() && !IsAnnotatedParallel) {
Adam Nemet2bd6e982015-02-19 19:15:15 +0000948 emitAnalysis(LoopAccessReport(St)
Adam Nemet04563272015-02-01 16:56:15 +0000949 << "write with atomic ordering or volatile write");
Adam Nemet339f42b2015-02-19 19:15:07 +0000950 DEBUG(dbgs() << "LAA: Found a non-simple store.\n");
Adam Nemet436018c2015-02-19 19:15:00 +0000951 CanVecMem = false;
952 return;
Adam Nemet04563272015-02-01 16:56:15 +0000953 }
954 NumStores++;
955 Stores.push_back(St);
956 DepChecker.addAccess(St);
957 }
958 } // Next instr.
959 } // Next block.
960
961 // Now we have two lists that hold the loads and the stores.
962 // Next, we find the pointers that they use.
963
964 // Check if we see any stores. If there are no stores, then we don't
965 // care if the pointers are *restrict*.
966 if (!Stores.size()) {
Adam Nemet339f42b2015-02-19 19:15:07 +0000967 DEBUG(dbgs() << "LAA: Found a read-only loop!\n");
Adam Nemet436018c2015-02-19 19:15:00 +0000968 CanVecMem = true;
969 return;
Adam Nemet04563272015-02-01 16:56:15 +0000970 }
971
Adam Nemetdee666b2015-03-10 17:40:34 +0000972 MemoryDepChecker::DepCandidates DependentAccesses;
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000973 AccessAnalysis Accesses(TheLoop->getHeader()->getModule()->getDataLayout(),
974 AA, DependentAccesses);
Adam Nemet04563272015-02-01 16:56:15 +0000975
976 // Holds the analyzed pointers. We don't want to call GetUnderlyingObjects
977 // multiple times on the same object. If the ptr is accessed twice, once
978 // for read and once for write, it will only appear once (on the write
979 // list). This is okay, since we are going to check for conflicts between
980 // writes and between reads and writes, but not between reads and reads.
981 ValueSet Seen;
982
983 ValueVector::iterator I, IE;
984 for (I = Stores.begin(), IE = Stores.end(); I != IE; ++I) {
985 StoreInst *ST = cast<StoreInst>(*I);
986 Value* Ptr = ST->getPointerOperand();
987
988 if (isUniform(Ptr)) {
989 emitAnalysis(
Adam Nemet2bd6e982015-02-19 19:15:15 +0000990 LoopAccessReport(ST)
Adam Nemet04563272015-02-01 16:56:15 +0000991 << "write to a loop invariant address could not be vectorized");
Adam Nemet339f42b2015-02-19 19:15:07 +0000992 DEBUG(dbgs() << "LAA: We don't allow storing to uniform addresses\n");
Adam Nemet436018c2015-02-19 19:15:00 +0000993 CanVecMem = false;
994 return;
Adam Nemet04563272015-02-01 16:56:15 +0000995 }
996
997 // If we did *not* see this pointer before, insert it to the read-write
998 // list. At this phase it is only a 'write' list.
999 if (Seen.insert(Ptr).second) {
1000 ++NumReadWrites;
1001
1002 AliasAnalysis::Location Loc = AA->getLocation(ST);
1003 // The TBAA metadata could have a control dependency on the predication
1004 // condition, so we cannot rely on it when determining whether or not we
1005 // need runtime pointer checks.
Adam Nemet01abb2c2015-02-18 03:43:19 +00001006 if (blockNeedsPredication(ST->getParent(), TheLoop, DT))
Adam Nemet04563272015-02-01 16:56:15 +00001007 Loc.AATags.TBAA = nullptr;
1008
1009 Accesses.addStore(Loc);
1010 }
1011 }
1012
1013 if (IsAnnotatedParallel) {
Adam Nemet04d41632015-02-19 19:14:34 +00001014 DEBUG(dbgs()
Adam Nemet339f42b2015-02-19 19:15:07 +00001015 << "LAA: A loop annotated parallel, ignore memory dependency "
Adam Nemet04d41632015-02-19 19:14:34 +00001016 << "checks.\n");
Adam Nemet436018c2015-02-19 19:15:00 +00001017 CanVecMem = true;
1018 return;
Adam Nemet04563272015-02-01 16:56:15 +00001019 }
1020
1021 for (I = Loads.begin(), IE = Loads.end(); I != IE; ++I) {
1022 LoadInst *LD = cast<LoadInst>(*I);
1023 Value* Ptr = LD->getPointerOperand();
1024 // If we did *not* see this pointer before, insert it to the
1025 // read list. If we *did* see it before, then it is already in
1026 // the read-write list. This allows us to vectorize expressions
1027 // such as A[i] += x; Because the address of A[i] is a read-write
1028 // pointer. This only works if the index of A[i] is consecutive.
1029 // If the address of i is unknown (for example A[B[i]]) then we may
1030 // read a few words, modify, and write a few words, and some of the
1031 // words may be written to the same address.
1032 bool IsReadOnlyPtr = false;
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001033 if (Seen.insert(Ptr).second || !isStridedPtr(SE, Ptr, TheLoop, Strides)) {
Adam Nemet04563272015-02-01 16:56:15 +00001034 ++NumReads;
1035 IsReadOnlyPtr = true;
1036 }
1037
1038 AliasAnalysis::Location Loc = AA->getLocation(LD);
1039 // The TBAA metadata could have a control dependency on the predication
1040 // condition, so we cannot rely on it when determining whether or not we
1041 // need runtime pointer checks.
Adam Nemet01abb2c2015-02-18 03:43:19 +00001042 if (blockNeedsPredication(LD->getParent(), TheLoop, DT))
Adam Nemet04563272015-02-01 16:56:15 +00001043 Loc.AATags.TBAA = nullptr;
1044
1045 Accesses.addLoad(Loc, IsReadOnlyPtr);
1046 }
1047
1048 // If we write (or read-write) to a single destination and there are no
1049 // other reads in this loop then is it safe to vectorize.
1050 if (NumReadWrites == 1 && NumReads == 0) {
Adam Nemet339f42b2015-02-19 19:15:07 +00001051 DEBUG(dbgs() << "LAA: Found a write-only loop!\n");
Adam Nemet436018c2015-02-19 19:15:00 +00001052 CanVecMem = true;
1053 return;
Adam Nemet04563272015-02-01 16:56:15 +00001054 }
1055
1056 // Build dependence sets and check whether we need a runtime pointer bounds
1057 // check.
1058 Accesses.buildDependenceSets();
1059 bool NeedRTCheck = Accesses.isRTCheckNeeded();
1060
1061 // Find pointers with computable bounds. We are going to use this information
1062 // to place a runtime bound check.
1063 unsigned NumComparisons = 0;
1064 bool CanDoRT = false;
1065 if (NeedRTCheck)
1066 CanDoRT = Accesses.canCheckPtrAtRT(PtrRtCheck, NumComparisons, SE, TheLoop,
1067 Strides);
1068
Adam Nemet339f42b2015-02-19 19:15:07 +00001069 DEBUG(dbgs() << "LAA: We need to do " << NumComparisons <<
Adam Nemet04d41632015-02-19 19:14:34 +00001070 " pointer comparisons.\n");
Adam Nemet04563272015-02-01 16:56:15 +00001071
1072 // If we only have one set of dependences to check pointers among we don't
1073 // need a runtime check.
1074 if (NumComparisons == 0 && NeedRTCheck)
1075 NeedRTCheck = false;
1076
1077 // Check that we did not collect too many pointers or found an unsizeable
1078 // pointer.
Adam Nemet1d862af2015-02-26 04:39:09 +00001079 if (!CanDoRT || NumComparisons > RuntimeMemoryCheckThreshold) {
Adam Nemet04563272015-02-01 16:56:15 +00001080 PtrRtCheck.reset();
1081 CanDoRT = false;
1082 }
1083
1084 if (CanDoRT) {
Adam Nemet339f42b2015-02-19 19:15:07 +00001085 DEBUG(dbgs() << "LAA: We can perform a memory runtime check if needed.\n");
Adam Nemet04563272015-02-01 16:56:15 +00001086 }
1087
1088 if (NeedRTCheck && !CanDoRT) {
Adam Nemet2bd6e982015-02-19 19:15:15 +00001089 emitAnalysis(LoopAccessReport() << "cannot identify array bounds");
Adam Nemet339f42b2015-02-19 19:15:07 +00001090 DEBUG(dbgs() << "LAA: We can't vectorize because we can't find " <<
Adam Nemet04d41632015-02-19 19:14:34 +00001091 "the array bounds.\n");
Adam Nemet04563272015-02-01 16:56:15 +00001092 PtrRtCheck.reset();
Adam Nemet436018c2015-02-19 19:15:00 +00001093 CanVecMem = false;
1094 return;
Adam Nemet04563272015-02-01 16:56:15 +00001095 }
1096
1097 PtrRtCheck.Need = NeedRTCheck;
1098
Adam Nemet436018c2015-02-19 19:15:00 +00001099 CanVecMem = true;
Adam Nemet04563272015-02-01 16:56:15 +00001100 if (Accesses.isDependencyCheckNeeded()) {
Adam Nemet339f42b2015-02-19 19:15:07 +00001101 DEBUG(dbgs() << "LAA: Checking memory dependencies\n");
Adam Nemet04563272015-02-01 16:56:15 +00001102 CanVecMem = DepChecker.areDepsSafe(
1103 DependentAccesses, Accesses.getDependenciesToCheck(), Strides);
1104 MaxSafeDepDistBytes = DepChecker.getMaxSafeDepDistBytes();
1105
1106 if (!CanVecMem && DepChecker.shouldRetryWithRuntimeCheck()) {
Adam Nemet339f42b2015-02-19 19:15:07 +00001107 DEBUG(dbgs() << "LAA: Retrying with memory checks\n");
Adam Nemet04563272015-02-01 16:56:15 +00001108 NeedRTCheck = true;
1109
1110 // Clear the dependency checks. We assume they are not needed.
1111 Accesses.resetDepChecks();
1112
1113 PtrRtCheck.reset();
1114 PtrRtCheck.Need = true;
1115
1116 CanDoRT = Accesses.canCheckPtrAtRT(PtrRtCheck, NumComparisons, SE,
1117 TheLoop, Strides, true);
1118 // Check that we did not collect too many pointers or found an unsizeable
1119 // pointer.
Adam Nemet1d862af2015-02-26 04:39:09 +00001120 if (!CanDoRT || NumComparisons > RuntimeMemoryCheckThreshold) {
Adam Nemet04563272015-02-01 16:56:15 +00001121 if (!CanDoRT && NumComparisons > 0)
Adam Nemet2bd6e982015-02-19 19:15:15 +00001122 emitAnalysis(LoopAccessReport()
Adam Nemet04563272015-02-01 16:56:15 +00001123 << "cannot check memory dependencies at runtime");
1124 else
Adam Nemet2bd6e982015-02-19 19:15:15 +00001125 emitAnalysis(LoopAccessReport()
Adam Nemet04563272015-02-01 16:56:15 +00001126 << NumComparisons << " exceeds limit of "
Adam Nemet1d862af2015-02-26 04:39:09 +00001127 << RuntimeMemoryCheckThreshold
Adam Nemet04563272015-02-01 16:56:15 +00001128 << " dependent memory operations checked at runtime");
Adam Nemet339f42b2015-02-19 19:15:07 +00001129 DEBUG(dbgs() << "LAA: Can't vectorize with memory checks\n");
Adam Nemet04563272015-02-01 16:56:15 +00001130 PtrRtCheck.reset();
Adam Nemet436018c2015-02-19 19:15:00 +00001131 CanVecMem = false;
1132 return;
Adam Nemet04563272015-02-01 16:56:15 +00001133 }
1134
1135 CanVecMem = true;
1136 }
1137 }
1138
1139 if (!CanVecMem)
Adam Nemet2bd6e982015-02-19 19:15:15 +00001140 emitAnalysis(LoopAccessReport() <<
Adam Nemet04d41632015-02-19 19:14:34 +00001141 "unsafe dependent memory operations in loop");
Adam Nemet04563272015-02-01 16:56:15 +00001142
Adam Nemet339f42b2015-02-19 19:15:07 +00001143 DEBUG(dbgs() << "LAA: We" << (NeedRTCheck ? "" : " don't") <<
Adam Nemet04d41632015-02-19 19:14:34 +00001144 " need a runtime memory check.\n");
Adam Nemet04563272015-02-01 16:56:15 +00001145}
1146
Adam Nemet01abb2c2015-02-18 03:43:19 +00001147bool LoopAccessInfo::blockNeedsPredication(BasicBlock *BB, Loop *TheLoop,
1148 DominatorTree *DT) {
Adam Nemet04563272015-02-01 16:56:15 +00001149 assert(TheLoop->contains(BB) && "Unknown block used");
1150
1151 // Blocks that do not dominate the latch need predication.
1152 BasicBlock* Latch = TheLoop->getLoopLatch();
1153 return !DT->dominates(BB, Latch);
1154}
1155
Adam Nemet2bd6e982015-02-19 19:15:15 +00001156void LoopAccessInfo::emitAnalysis(LoopAccessReport &Message) {
Adam Nemetc9228532015-02-19 19:14:56 +00001157 assert(!Report && "Multiple reports generated");
1158 Report = Message;
Adam Nemet04563272015-02-01 16:56:15 +00001159}
1160
Adam Nemet57ac7662015-02-19 19:15:21 +00001161bool LoopAccessInfo::isUniform(Value *V) const {
Adam Nemet04563272015-02-01 16:56:15 +00001162 return (SE->isLoopInvariant(SE->getSCEV(V), TheLoop));
1163}
Adam Nemet7206d7a2015-02-06 18:31:04 +00001164
1165// FIXME: this function is currently a duplicate of the one in
1166// LoopVectorize.cpp.
1167static Instruction *getFirstInst(Instruction *FirstInst, Value *V,
1168 Instruction *Loc) {
1169 if (FirstInst)
1170 return FirstInst;
1171 if (Instruction *I = dyn_cast<Instruction>(V))
1172 return I->getParent() == Loc->getParent() ? I : nullptr;
1173 return nullptr;
1174}
1175
1176std::pair<Instruction *, Instruction *>
Adam Nemet57ac7662015-02-19 19:15:21 +00001177LoopAccessInfo::addRuntimeCheck(Instruction *Loc) const {
Adam Nemet7206d7a2015-02-06 18:31:04 +00001178 Instruction *tnullptr = nullptr;
1179 if (!PtrRtCheck.Need)
1180 return std::pair<Instruction *, Instruction *>(tnullptr, tnullptr);
1181
1182 unsigned NumPointers = PtrRtCheck.Pointers.size();
1183 SmallVector<TrackingVH<Value> , 2> Starts;
1184 SmallVector<TrackingVH<Value> , 2> Ends;
1185
1186 LLVMContext &Ctx = Loc->getContext();
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001187 SCEVExpander Exp(*SE, DL, "induction");
Adam Nemet7206d7a2015-02-06 18:31:04 +00001188 Instruction *FirstInst = nullptr;
1189
1190 for (unsigned i = 0; i < NumPointers; ++i) {
1191 Value *Ptr = PtrRtCheck.Pointers[i];
1192 const SCEV *Sc = SE->getSCEV(Ptr);
1193
1194 if (SE->isLoopInvariant(Sc, TheLoop)) {
Adam Nemet339f42b2015-02-19 19:15:07 +00001195 DEBUG(dbgs() << "LAA: Adding RT check for a loop invariant ptr:" <<
Adam Nemet04d41632015-02-19 19:14:34 +00001196 *Ptr <<"\n");
Adam Nemet7206d7a2015-02-06 18:31:04 +00001197 Starts.push_back(Ptr);
1198 Ends.push_back(Ptr);
1199 } else {
Adam Nemet339f42b2015-02-19 19:15:07 +00001200 DEBUG(dbgs() << "LAA: Adding RT check for range:" << *Ptr << '\n');
Adam Nemet7206d7a2015-02-06 18:31:04 +00001201 unsigned AS = Ptr->getType()->getPointerAddressSpace();
1202
1203 // Use this type for pointer arithmetic.
1204 Type *PtrArithTy = Type::getInt8PtrTy(Ctx, AS);
1205
1206 Value *Start = Exp.expandCodeFor(PtrRtCheck.Starts[i], PtrArithTy, Loc);
1207 Value *End = Exp.expandCodeFor(PtrRtCheck.Ends[i], PtrArithTy, Loc);
1208 Starts.push_back(Start);
1209 Ends.push_back(End);
1210 }
1211 }
1212
1213 IRBuilder<> ChkBuilder(Loc);
1214 // Our instructions might fold to a constant.
1215 Value *MemoryRuntimeCheck = nullptr;
1216 for (unsigned i = 0; i < NumPointers; ++i) {
1217 for (unsigned j = i+1; j < NumPointers; ++j) {
Adam Nemeta8945b72015-02-18 03:43:58 +00001218 if (!PtrRtCheck.needsChecking(i, j))
Adam Nemet7206d7a2015-02-06 18:31:04 +00001219 continue;
1220
1221 unsigned AS0 = Starts[i]->getType()->getPointerAddressSpace();
1222 unsigned AS1 = Starts[j]->getType()->getPointerAddressSpace();
1223
1224 assert((AS0 == Ends[j]->getType()->getPointerAddressSpace()) &&
1225 (AS1 == Ends[i]->getType()->getPointerAddressSpace()) &&
1226 "Trying to bounds check pointers with different address spaces");
1227
1228 Type *PtrArithTy0 = Type::getInt8PtrTy(Ctx, AS0);
1229 Type *PtrArithTy1 = Type::getInt8PtrTy(Ctx, AS1);
1230
1231 Value *Start0 = ChkBuilder.CreateBitCast(Starts[i], PtrArithTy0, "bc");
1232 Value *Start1 = ChkBuilder.CreateBitCast(Starts[j], PtrArithTy1, "bc");
1233 Value *End0 = ChkBuilder.CreateBitCast(Ends[i], PtrArithTy1, "bc");
1234 Value *End1 = ChkBuilder.CreateBitCast(Ends[j], PtrArithTy0, "bc");
1235
1236 Value *Cmp0 = ChkBuilder.CreateICmpULE(Start0, End1, "bound0");
1237 FirstInst = getFirstInst(FirstInst, Cmp0, Loc);
1238 Value *Cmp1 = ChkBuilder.CreateICmpULE(Start1, End0, "bound1");
1239 FirstInst = getFirstInst(FirstInst, Cmp1, Loc);
1240 Value *IsConflict = ChkBuilder.CreateAnd(Cmp0, Cmp1, "found.conflict");
1241 FirstInst = getFirstInst(FirstInst, IsConflict, Loc);
1242 if (MemoryRuntimeCheck) {
1243 IsConflict = ChkBuilder.CreateOr(MemoryRuntimeCheck, IsConflict,
1244 "conflict.rdx");
1245 FirstInst = getFirstInst(FirstInst, IsConflict, Loc);
1246 }
1247 MemoryRuntimeCheck = IsConflict;
1248 }
1249 }
1250
1251 // We have to do this trickery because the IRBuilder might fold the check to a
1252 // constant expression in which case there is no Instruction anchored in a
1253 // the block.
1254 Instruction *Check = BinaryOperator::CreateAnd(MemoryRuntimeCheck,
1255 ConstantInt::getTrue(Ctx));
1256 ChkBuilder.Insert(Check, "memcheck.conflict");
1257 FirstInst = getFirstInst(FirstInst, Check, Loc);
1258 return std::make_pair(FirstInst, Check);
1259}
Adam Nemet3bfd93d2015-02-19 19:15:04 +00001260
1261LoopAccessInfo::LoopAccessInfo(Loop *L, ScalarEvolution *SE,
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001262 const DataLayout &DL,
Adam Nemet3bfd93d2015-02-19 19:15:04 +00001263 const TargetLibraryInfo *TLI, AliasAnalysis *AA,
Adam Nemet8bc61df2015-02-24 00:41:59 +00001264 DominatorTree *DT,
1265 const ValueToValueMap &Strides)
Adam Nemetdee666b2015-03-10 17:40:34 +00001266 : DepChecker(SE, L), TheLoop(L), SE(SE), DL(DL), TLI(TLI), AA(AA),
1267 DT(DT), NumLoads(0), NumStores(0), MaxSafeDepDistBytes(-1U),
1268 CanVecMem(false) {
Adam Nemet929c38e2015-02-19 19:15:10 +00001269 if (canAnalyzeLoop())
1270 analyzeLoop(Strides);
Adam Nemet3bfd93d2015-02-19 19:15:04 +00001271}
1272
Adam Nemete91cc6e2015-02-19 19:15:19 +00001273void LoopAccessInfo::print(raw_ostream &OS, unsigned Depth) const {
1274 if (CanVecMem) {
1275 if (PtrRtCheck.empty())
1276 OS.indent(Depth) << "Memory dependences are safe\n";
1277 else
1278 OS.indent(Depth) << "Memory dependences are safe with run-time checks\n";
1279 }
1280
1281 if (Report)
1282 OS.indent(Depth) << "Report: " << Report->str() << "\n";
1283
1284 // FIXME: Print unsafe dependences
1285
1286 // List the pair of accesses need run-time checks to prove independence.
1287 PtrRtCheck.print(OS, Depth);
1288 OS << "\n";
1289}
1290
Adam Nemet8bc61df2015-02-24 00:41:59 +00001291const LoopAccessInfo &
1292LoopAccessAnalysis::getInfo(Loop *L, const ValueToValueMap &Strides) {
Adam Nemet3bfd93d2015-02-19 19:15:04 +00001293 auto &LAI = LoopAccessInfoMap[L];
1294
1295#ifndef NDEBUG
1296 assert((!LAI || LAI->NumSymbolicStrides == Strides.size()) &&
1297 "Symbolic strides changed for loop");
1298#endif
1299
1300 if (!LAI) {
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001301 const DataLayout &DL = L->getHeader()->getModule()->getDataLayout();
Adam Nemet3bfd93d2015-02-19 19:15:04 +00001302 LAI = llvm::make_unique<LoopAccessInfo>(L, SE, DL, TLI, AA, DT, Strides);
1303#ifndef NDEBUG
1304 LAI->NumSymbolicStrides = Strides.size();
1305#endif
1306 }
1307 return *LAI.get();
1308}
1309
Adam Nemete91cc6e2015-02-19 19:15:19 +00001310void LoopAccessAnalysis::print(raw_ostream &OS, const Module *M) const {
1311 LoopAccessAnalysis &LAA = *const_cast<LoopAccessAnalysis *>(this);
1312
1313 LoopInfo *LI = &getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
1314 ValueToValueMap NoSymbolicStrides;
1315
1316 for (Loop *TopLevelLoop : *LI)
1317 for (Loop *L : depth_first(TopLevelLoop)) {
1318 OS.indent(2) << L->getHeader()->getName() << ":\n";
1319 auto &LAI = LAA.getInfo(L, NoSymbolicStrides);
1320 LAI.print(OS, 4);
1321 }
1322}
1323
Adam Nemet3bfd93d2015-02-19 19:15:04 +00001324bool LoopAccessAnalysis::runOnFunction(Function &F) {
1325 SE = &getAnalysis<ScalarEvolution>();
Adam Nemet3bfd93d2015-02-19 19:15:04 +00001326 auto *TLIP = getAnalysisIfAvailable<TargetLibraryInfoWrapperPass>();
1327 TLI = TLIP ? &TLIP->getTLI() : nullptr;
1328 AA = &getAnalysis<AliasAnalysis>();
1329 DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree();
1330
1331 return false;
1332}
1333
1334void LoopAccessAnalysis::getAnalysisUsage(AnalysisUsage &AU) const {
1335 AU.addRequired<ScalarEvolution>();
1336 AU.addRequired<AliasAnalysis>();
1337 AU.addRequired<DominatorTreeWrapperPass>();
Adam Nemete91cc6e2015-02-19 19:15:19 +00001338 AU.addRequired<LoopInfoWrapperPass>();
Adam Nemet3bfd93d2015-02-19 19:15:04 +00001339
1340 AU.setPreservesAll();
1341}
1342
1343char LoopAccessAnalysis::ID = 0;
1344static const char laa_name[] = "Loop Access Analysis";
1345#define LAA_NAME "loop-accesses"
1346
1347INITIALIZE_PASS_BEGIN(LoopAccessAnalysis, LAA_NAME, laa_name, false, true)
1348INITIALIZE_AG_DEPENDENCY(AliasAnalysis)
1349INITIALIZE_PASS_DEPENDENCY(ScalarEvolution)
1350INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
Adam Nemete91cc6e2015-02-19 19:15:19 +00001351INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass)
Adam Nemet3bfd93d2015-02-19 19:15:04 +00001352INITIALIZE_PASS_END(LoopAccessAnalysis, LAA_NAME, laa_name, false, true)
1353
1354namespace llvm {
1355 Pass *createLAAPass() {
1356 return new LoopAccessAnalysis();
1357 }
1358}