blob: cfa8fbe4a8c59c0a5a3f1c81bb1a4ee700819e37 [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"
Benjamin Kramer799003b2015-03-23 19:32:43 +000018#include "llvm/Analysis/TargetLibraryInfo.h"
Adam Nemet04563272015-02-01 16:56:15 +000019#include "llvm/Analysis/ValueTracking.h"
20#include "llvm/IR/DiagnosticInfo.h"
21#include "llvm/IR/Dominators.h"
Adam Nemet7206d7a2015-02-06 18:31:04 +000022#include "llvm/IR/IRBuilder.h"
Adam Nemet04563272015-02-01 16:56:15 +000023#include "llvm/Support/Debug.h"
Benjamin Kramer799003b2015-03-23 19:32:43 +000024#include "llvm/Support/raw_ostream.h"
Adam Nemet04563272015-02-01 16:56:15 +000025#include "llvm/Transforms/Utils/VectorUtils.h"
26using namespace llvm;
27
Adam Nemet339f42b2015-02-19 19:15:07 +000028#define DEBUG_TYPE "loop-accesses"
Adam Nemet04563272015-02-01 16:56:15 +000029
Adam Nemetf219c642015-02-19 19:14:52 +000030static cl::opt<unsigned, true>
31VectorizationFactor("force-vector-width", cl::Hidden,
32 cl::desc("Sets the SIMD width. Zero is autoselect."),
33 cl::location(VectorizerParams::VectorizationFactor));
Adam Nemet1d862af2015-02-26 04:39:09 +000034unsigned VectorizerParams::VectorizationFactor;
Adam Nemetf219c642015-02-19 19:14:52 +000035
36static cl::opt<unsigned, true>
37VectorizationInterleave("force-vector-interleave", cl::Hidden,
38 cl::desc("Sets the vectorization interleave count. "
39 "Zero is autoselect."),
40 cl::location(
41 VectorizerParams::VectorizationInterleave));
Adam Nemet1d862af2015-02-26 04:39:09 +000042unsigned VectorizerParams::VectorizationInterleave;
Adam Nemetf219c642015-02-19 19:14:52 +000043
Adam Nemet1d862af2015-02-26 04:39:09 +000044static cl::opt<unsigned, true> RuntimeMemoryCheckThreshold(
45 "runtime-memory-check-threshold", cl::Hidden,
46 cl::desc("When performing memory disambiguation checks at runtime do not "
47 "generate more than this number of comparisons (default = 8)."),
48 cl::location(VectorizerParams::RuntimeMemoryCheckThreshold), cl::init(8));
49unsigned VectorizerParams::RuntimeMemoryCheckThreshold;
Adam Nemetf219c642015-02-19 19:14:52 +000050
51/// Maximum SIMD width.
52const unsigned VectorizerParams::MaxVectorWidth = 64;
53
Adam Nemet9c926572015-03-10 17:40:37 +000054/// \brief We collect interesting dependences up to this threshold.
55static cl::opt<unsigned> MaxInterestingDependence(
56 "max-interesting-dependences", cl::Hidden,
57 cl::desc("Maximum number of interesting dependences collected by "
58 "loop-access analysis (default = 100)"),
59 cl::init(100));
60
Adam Nemetf219c642015-02-19 19:14:52 +000061bool VectorizerParams::isInterleaveForced() {
62 return ::VectorizationInterleave.getNumOccurrences() > 0;
63}
64
Adam Nemet2bd6e982015-02-19 19:15:15 +000065void LoopAccessReport::emitAnalysis(const LoopAccessReport &Message,
66 const Function *TheFunction,
67 const Loop *TheLoop,
68 const char *PassName) {
Adam Nemet04563272015-02-01 16:56:15 +000069 DebugLoc DL = TheLoop->getStartLoc();
Adam Nemet3e876342015-02-19 19:15:13 +000070 if (const Instruction *I = Message.getInstr())
Adam Nemet04563272015-02-01 16:56:15 +000071 DL = I->getDebugLoc();
Adam Nemet339f42b2015-02-19 19:15:07 +000072 emitOptimizationRemarkAnalysis(TheFunction->getContext(), PassName,
Adam Nemet04563272015-02-01 16:56:15 +000073 *TheFunction, DL, Message.str());
74}
75
76Value *llvm::stripIntegerCast(Value *V) {
77 if (CastInst *CI = dyn_cast<CastInst>(V))
78 if (CI->getOperand(0)->getType()->isIntegerTy())
79 return CI->getOperand(0);
80 return V;
81}
82
83const SCEV *llvm::replaceSymbolicStrideSCEV(ScalarEvolution *SE,
Adam Nemet8bc61df2015-02-24 00:41:59 +000084 const ValueToValueMap &PtrToStride,
Adam Nemet04563272015-02-01 16:56:15 +000085 Value *Ptr, Value *OrigPtr) {
86
87 const SCEV *OrigSCEV = SE->getSCEV(Ptr);
88
89 // If there is an entry in the map return the SCEV of the pointer with the
90 // symbolic stride replaced by one.
Adam Nemet8bc61df2015-02-24 00:41:59 +000091 ValueToValueMap::const_iterator SI =
92 PtrToStride.find(OrigPtr ? OrigPtr : Ptr);
Adam Nemet04563272015-02-01 16:56:15 +000093 if (SI != PtrToStride.end()) {
94 Value *StrideVal = SI->second;
95
96 // Strip casts.
97 StrideVal = stripIntegerCast(StrideVal);
98
99 // Replace symbolic stride by one.
100 Value *One = ConstantInt::get(StrideVal->getType(), 1);
101 ValueToValueMap RewriteMap;
102 RewriteMap[StrideVal] = One;
103
104 const SCEV *ByOne =
105 SCEVParameterRewriter::rewrite(OrigSCEV, *SE, RewriteMap, true);
Adam Nemet339f42b2015-02-19 19:15:07 +0000106 DEBUG(dbgs() << "LAA: Replacing SCEV: " << *OrigSCEV << " by: " << *ByOne
Adam Nemet04563272015-02-01 16:56:15 +0000107 << "\n");
108 return ByOne;
109 }
110
111 // Otherwise, just return the SCEV of the original pointer.
112 return SE->getSCEV(Ptr);
113}
114
Adam Nemet8bc61df2015-02-24 00:41:59 +0000115void LoopAccessInfo::RuntimePointerCheck::insert(
116 ScalarEvolution *SE, Loop *Lp, Value *Ptr, bool WritePtr, unsigned DepSetId,
117 unsigned ASId, const ValueToValueMap &Strides) {
Adam Nemet04563272015-02-01 16:56:15 +0000118 // Get the stride replaced scev.
119 const SCEV *Sc = replaceSymbolicStrideSCEV(SE, Strides, Ptr);
120 const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(Sc);
121 assert(AR && "Invalid addrec expression");
122 const SCEV *Ex = SE->getBackedgeTakenCount(Lp);
123 const SCEV *ScEnd = AR->evaluateAtIteration(Ex, *SE);
124 Pointers.push_back(Ptr);
125 Starts.push_back(AR->getStart());
126 Ends.push_back(ScEnd);
127 IsWritePtr.push_back(WritePtr);
128 DependencySetId.push_back(DepSetId);
129 AliasSetId.push_back(ASId);
130}
131
Adam Nemetec1e2bb2015-03-10 18:54:26 +0000132bool LoopAccessInfo::RuntimePointerCheck::needsChecking(
133 unsigned I, unsigned J, const SmallVectorImpl<int> *PtrPartition) const {
Adam Nemeta8945b72015-02-18 03:43:58 +0000134 // No need to check if two readonly pointers intersect.
135 if (!IsWritePtr[I] && !IsWritePtr[J])
136 return false;
137
138 // Only need to check pointers between two different dependency sets.
139 if (DependencySetId[I] == DependencySetId[J])
140 return false;
141
142 // Only need to check pointers in the same alias set.
143 if (AliasSetId[I] != AliasSetId[J])
144 return false;
145
Adam Nemetec1e2bb2015-03-10 18:54:26 +0000146 // If PtrPartition is set omit checks between pointers of the same partition.
147 // Partition number -1 means that the pointer is used in multiple partitions.
148 // In this case we can't omit the check.
149 if (PtrPartition && (*PtrPartition)[I] != -1 &&
150 (*PtrPartition)[I] == (*PtrPartition)[J])
151 return false;
152
Adam Nemeta8945b72015-02-18 03:43:58 +0000153 return true;
154}
155
Adam Nemetec1e2bb2015-03-10 18:54:26 +0000156void LoopAccessInfo::RuntimePointerCheck::print(
157 raw_ostream &OS, unsigned Depth,
158 const SmallVectorImpl<int> *PtrPartition) const {
Adam Nemete91cc6e2015-02-19 19:15:19 +0000159 unsigned NumPointers = Pointers.size();
160 if (NumPointers == 0)
161 return;
162
163 OS.indent(Depth) << "Run-time memory checks:\n";
164 unsigned N = 0;
165 for (unsigned I = 0; I < NumPointers; ++I)
166 for (unsigned J = I + 1; J < NumPointers; ++J)
Adam Nemetec1e2bb2015-03-10 18:54:26 +0000167 if (needsChecking(I, J, PtrPartition)) {
Adam Nemete91cc6e2015-02-19 19:15:19 +0000168 OS.indent(Depth) << N++ << ":\n";
Adam Nemetec1e2bb2015-03-10 18:54:26 +0000169 OS.indent(Depth + 2) << *Pointers[I];
170 if (PtrPartition)
171 OS << " (Partition: " << (*PtrPartition)[I] << ")";
172 OS << "\n";
173 OS.indent(Depth + 2) << *Pointers[J];
174 if (PtrPartition)
175 OS << " (Partition: " << (*PtrPartition)[J] << ")";
176 OS << "\n";
Adam Nemete91cc6e2015-02-19 19:15:19 +0000177 }
178}
179
Adam Nemet51870d12015-04-07 03:35:26 +0000180bool LoopAccessInfo::RuntimePointerCheck::needsAnyChecking(
181 const SmallVectorImpl<int> *PtrPartition) const {
182 unsigned NumPointers = Pointers.size();
183
184 for (unsigned I = 0; I < NumPointers; ++I)
185 for (unsigned J = I + 1; J < NumPointers; ++J)
186 if (needsChecking(I, J, PtrPartition))
187 return true;
188 return false;
189}
190
Adam Nemet04563272015-02-01 16:56:15 +0000191namespace {
192/// \brief Analyses memory accesses in a loop.
193///
194/// Checks whether run time pointer checks are needed and builds sets for data
195/// dependence checking.
196class AccessAnalysis {
197public:
198 /// \brief Read or write access location.
199 typedef PointerIntPair<Value *, 1, bool> MemAccessInfo;
200 typedef SmallPtrSet<MemAccessInfo, 8> MemAccessInfoSet;
201
Adam Nemete2b885c2015-04-23 20:09:20 +0000202 AccessAnalysis(const DataLayout &Dl, AliasAnalysis *AA, LoopInfo *LI,
Adam Nemetdee666b2015-03-10 17:40:34 +0000203 MemoryDepChecker::DepCandidates &DA)
Adam Nemete2b885c2015-04-23 20:09:20 +0000204 : DL(Dl), AST(*AA), LI(LI), DepCands(DA), IsRTCheckNeeded(false) {}
Adam Nemet04563272015-02-01 16:56:15 +0000205
206 /// \brief Register a load and whether it is only read from.
207 void addLoad(AliasAnalysis::Location &Loc, bool IsReadOnly) {
208 Value *Ptr = const_cast<Value*>(Loc.Ptr);
209 AST.add(Ptr, AliasAnalysis::UnknownSize, Loc.AATags);
210 Accesses.insert(MemAccessInfo(Ptr, false));
211 if (IsReadOnly)
212 ReadOnlyPtr.insert(Ptr);
213 }
214
215 /// \brief Register a store.
216 void addStore(AliasAnalysis::Location &Loc) {
217 Value *Ptr = const_cast<Value*>(Loc.Ptr);
218 AST.add(Ptr, AliasAnalysis::UnknownSize, Loc.AATags);
219 Accesses.insert(MemAccessInfo(Ptr, true));
220 }
221
222 /// \brief Check whether we can check the pointers at runtime for
223 /// non-intersection.
Adam Nemet30f16e12015-02-18 03:42:35 +0000224 bool canCheckPtrAtRT(LoopAccessInfo::RuntimePointerCheck &RtCheck,
Adam Nemet8bc61df2015-02-24 00:41:59 +0000225 unsigned &NumComparisons, ScalarEvolution *SE,
226 Loop *TheLoop, const ValueToValueMap &Strides,
Adam Nemet04563272015-02-01 16:56:15 +0000227 bool ShouldCheckStride = false);
228
229 /// \brief Goes over all memory accesses, checks whether a RT check is needed
230 /// and builds sets of dependent accesses.
231 void buildDependenceSets() {
232 processMemAccesses();
233 }
234
235 bool isRTCheckNeeded() { return IsRTCheckNeeded; }
236
237 bool isDependencyCheckNeeded() { return !CheckDeps.empty(); }
238 void resetDepChecks() { CheckDeps.clear(); }
239
240 MemAccessInfoSet &getDependenciesToCheck() { return CheckDeps; }
241
242private:
243 typedef SetVector<MemAccessInfo> PtrAccessSet;
244
245 /// \brief Go over all memory access and check whether runtime pointer checks
246 /// are needed /// and build sets of dependency check candidates.
247 void processMemAccesses();
248
249 /// Set of all accesses.
250 PtrAccessSet Accesses;
251
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000252 const DataLayout &DL;
253
Adam Nemet04563272015-02-01 16:56:15 +0000254 /// Set of accesses that need a further dependence check.
255 MemAccessInfoSet CheckDeps;
256
257 /// Set of pointers that are read only.
258 SmallPtrSet<Value*, 16> ReadOnlyPtr;
259
Adam Nemet04563272015-02-01 16:56:15 +0000260 /// An alias set tracker to partition the access set by underlying object and
261 //intrinsic property (such as TBAA metadata).
262 AliasSetTracker AST;
263
Adam Nemete2b885c2015-04-23 20:09:20 +0000264 LoopInfo *LI;
265
Adam Nemet04563272015-02-01 16:56:15 +0000266 /// Sets of potentially dependent accesses - members of one set share an
267 /// underlying pointer. The set "CheckDeps" identfies which sets really need a
268 /// dependence check.
Adam Nemetdee666b2015-03-10 17:40:34 +0000269 MemoryDepChecker::DepCandidates &DepCands;
Adam Nemet04563272015-02-01 16:56:15 +0000270
271 bool IsRTCheckNeeded;
272};
273
274} // end anonymous namespace
275
276/// \brief Check whether a pointer can participate in a runtime bounds check.
Adam Nemet8bc61df2015-02-24 00:41:59 +0000277static bool hasComputableBounds(ScalarEvolution *SE,
278 const ValueToValueMap &Strides, Value *Ptr) {
Adam Nemet04563272015-02-01 16:56:15 +0000279 const SCEV *PtrScev = replaceSymbolicStrideSCEV(SE, Strides, Ptr);
280 const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(PtrScev);
281 if (!AR)
282 return false;
283
284 return AR->isAffine();
285}
286
287/// \brief Check the stride of the pointer and ensure that it does not wrap in
288/// the address space.
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000289static int isStridedPtr(ScalarEvolution *SE, Value *Ptr, const Loop *Lp,
290 const ValueToValueMap &StridesMap);
Adam Nemet04563272015-02-01 16:56:15 +0000291
292bool AccessAnalysis::canCheckPtrAtRT(
Adam Nemet8bc61df2015-02-24 00:41:59 +0000293 LoopAccessInfo::RuntimePointerCheck &RtCheck, unsigned &NumComparisons,
294 ScalarEvolution *SE, Loop *TheLoop, const ValueToValueMap &StridesMap,
295 bool ShouldCheckStride) {
Adam Nemet04563272015-02-01 16:56:15 +0000296 // Find pointers with computable bounds. We are going to use this information
297 // to place a runtime bound check.
298 bool CanDoRT = true;
299
300 bool IsDepCheckNeeded = isDependencyCheckNeeded();
301 NumComparisons = 0;
302
303 // We assign a consecutive id to access from different alias sets.
304 // Accesses between different groups doesn't need to be checked.
305 unsigned ASId = 1;
306 for (auto &AS : AST) {
307 unsigned NumReadPtrChecks = 0;
308 unsigned NumWritePtrChecks = 0;
309
310 // We assign consecutive id to access from different dependence sets.
311 // Accesses within the same set don't need a runtime check.
312 unsigned RunningDepId = 1;
313 DenseMap<Value *, unsigned> DepSetId;
314
315 for (auto A : AS) {
316 Value *Ptr = A.getValue();
317 bool IsWrite = Accesses.count(MemAccessInfo(Ptr, true));
318 MemAccessInfo Access(Ptr, IsWrite);
319
320 if (IsWrite)
321 ++NumWritePtrChecks;
322 else
323 ++NumReadPtrChecks;
324
325 if (hasComputableBounds(SE, StridesMap, Ptr) &&
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000326 // When we run after a failing dependency check we have to make sure
327 // we don't have wrapping pointers.
Adam Nemet04563272015-02-01 16:56:15 +0000328 (!ShouldCheckStride ||
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000329 isStridedPtr(SE, Ptr, TheLoop, StridesMap) == 1)) {
Adam Nemet04563272015-02-01 16:56:15 +0000330 // The id of the dependence set.
331 unsigned DepId;
332
333 if (IsDepCheckNeeded) {
334 Value *Leader = DepCands.getLeaderValue(Access).getPointer();
335 unsigned &LeaderId = DepSetId[Leader];
336 if (!LeaderId)
337 LeaderId = RunningDepId++;
338 DepId = LeaderId;
339 } else
340 // Each access has its own dependence set.
341 DepId = RunningDepId++;
342
343 RtCheck.insert(SE, TheLoop, Ptr, IsWrite, DepId, ASId, StridesMap);
344
Adam Nemet339f42b2015-02-19 19:15:07 +0000345 DEBUG(dbgs() << "LAA: Found a runtime check ptr:" << *Ptr << '\n');
Adam Nemet04563272015-02-01 16:56:15 +0000346 } else {
347 CanDoRT = false;
348 }
349 }
350
351 if (IsDepCheckNeeded && CanDoRT && RunningDepId == 2)
352 NumComparisons += 0; // Only one dependence set.
353 else {
354 NumComparisons += (NumWritePtrChecks * (NumReadPtrChecks +
355 NumWritePtrChecks - 1));
356 }
357
358 ++ASId;
359 }
360
361 // If the pointers that we would use for the bounds comparison have different
362 // address spaces, assume the values aren't directly comparable, so we can't
363 // use them for the runtime check. We also have to assume they could
364 // overlap. In the future there should be metadata for whether address spaces
365 // are disjoint.
366 unsigned NumPointers = RtCheck.Pointers.size();
367 for (unsigned i = 0; i < NumPointers; ++i) {
368 for (unsigned j = i + 1; j < NumPointers; ++j) {
369 // Only need to check pointers between two different dependency sets.
370 if (RtCheck.DependencySetId[i] == RtCheck.DependencySetId[j])
371 continue;
372 // Only need to check pointers in the same alias set.
373 if (RtCheck.AliasSetId[i] != RtCheck.AliasSetId[j])
374 continue;
375
376 Value *PtrI = RtCheck.Pointers[i];
377 Value *PtrJ = RtCheck.Pointers[j];
378
379 unsigned ASi = PtrI->getType()->getPointerAddressSpace();
380 unsigned ASj = PtrJ->getType()->getPointerAddressSpace();
381 if (ASi != ASj) {
Adam Nemet339f42b2015-02-19 19:15:07 +0000382 DEBUG(dbgs() << "LAA: Runtime check would require comparison between"
Adam Nemet04d41632015-02-19 19:14:34 +0000383 " different address spaces\n");
Adam Nemet04563272015-02-01 16:56:15 +0000384 return false;
385 }
386 }
387 }
388
389 return CanDoRT;
390}
391
392void AccessAnalysis::processMemAccesses() {
393 // We process the set twice: first we process read-write pointers, last we
394 // process read-only pointers. This allows us to skip dependence tests for
395 // read-only pointers.
396
Adam Nemet339f42b2015-02-19 19:15:07 +0000397 DEBUG(dbgs() << "LAA: Processing memory accesses...\n");
Adam Nemet04563272015-02-01 16:56:15 +0000398 DEBUG(dbgs() << " AST: "; AST.dump());
Adam Nemet9c926572015-03-10 17:40:37 +0000399 DEBUG(dbgs() << "LAA: Accesses(" << Accesses.size() << "):\n");
Adam Nemet04563272015-02-01 16:56:15 +0000400 DEBUG({
401 for (auto A : Accesses)
402 dbgs() << "\t" << *A.getPointer() << " (" <<
403 (A.getInt() ? "write" : (ReadOnlyPtr.count(A.getPointer()) ?
404 "read-only" : "read")) << ")\n";
405 });
406
407 // The AliasSetTracker has nicely partitioned our pointers by metadata
408 // compatibility and potential for underlying-object overlap. As a result, we
409 // only need to check for potential pointer dependencies within each alias
410 // set.
411 for (auto &AS : AST) {
412 // Note that both the alias-set tracker and the alias sets themselves used
413 // linked lists internally and so the iteration order here is deterministic
414 // (matching the original instruction order within each set).
415
416 bool SetHasWrite = false;
417
418 // Map of pointers to last access encountered.
419 typedef DenseMap<Value*, MemAccessInfo> UnderlyingObjToAccessMap;
420 UnderlyingObjToAccessMap ObjToLastAccess;
421
422 // Set of access to check after all writes have been processed.
423 PtrAccessSet DeferredAccesses;
424
425 // Iterate over each alias set twice, once to process read/write pointers,
426 // and then to process read-only pointers.
427 for (int SetIteration = 0; SetIteration < 2; ++SetIteration) {
428 bool UseDeferred = SetIteration > 0;
429 PtrAccessSet &S = UseDeferred ? DeferredAccesses : Accesses;
430
431 for (auto AV : AS) {
432 Value *Ptr = AV.getValue();
433
434 // For a single memory access in AliasSetTracker, Accesses may contain
435 // both read and write, and they both need to be handled for CheckDeps.
436 for (auto AC : S) {
437 if (AC.getPointer() != Ptr)
438 continue;
439
440 bool IsWrite = AC.getInt();
441
442 // If we're using the deferred access set, then it contains only
443 // reads.
444 bool IsReadOnlyPtr = ReadOnlyPtr.count(Ptr) && !IsWrite;
445 if (UseDeferred && !IsReadOnlyPtr)
446 continue;
447 // Otherwise, the pointer must be in the PtrAccessSet, either as a
448 // read or a write.
449 assert(((IsReadOnlyPtr && UseDeferred) || IsWrite ||
450 S.count(MemAccessInfo(Ptr, false))) &&
451 "Alias-set pointer not in the access set?");
452
453 MemAccessInfo Access(Ptr, IsWrite);
454 DepCands.insert(Access);
455
456 // Memorize read-only pointers for later processing and skip them in
457 // the first round (they need to be checked after we have seen all
458 // write pointers). Note: we also mark pointer that are not
459 // consecutive as "read-only" pointers (so that we check
460 // "a[b[i]] +="). Hence, we need the second check for "!IsWrite".
461 if (!UseDeferred && IsReadOnlyPtr) {
462 DeferredAccesses.insert(Access);
463 continue;
464 }
465
466 // If this is a write - check other reads and writes for conflicts. If
467 // this is a read only check other writes for conflicts (but only if
468 // there is no other write to the ptr - this is an optimization to
469 // catch "a[i] = a[i] + " without having to do a dependence check).
470 if ((IsWrite || IsReadOnlyPtr) && SetHasWrite) {
471 CheckDeps.insert(Access);
472 IsRTCheckNeeded = true;
473 }
474
475 if (IsWrite)
476 SetHasWrite = true;
477
478 // Create sets of pointers connected by a shared alias set and
479 // underlying object.
480 typedef SmallVector<Value *, 16> ValueVector;
481 ValueVector TempObjects;
Adam Nemete2b885c2015-04-23 20:09:20 +0000482
483 GetUnderlyingObjects(Ptr, TempObjects, DL, LI);
484 DEBUG(dbgs() << "Underlying objects for pointer " << *Ptr << "\n");
Adam Nemet04563272015-02-01 16:56:15 +0000485 for (Value *UnderlyingObj : TempObjects) {
486 UnderlyingObjToAccessMap::iterator Prev =
487 ObjToLastAccess.find(UnderlyingObj);
488 if (Prev != ObjToLastAccess.end())
489 DepCands.unionSets(Access, Prev->second);
490
491 ObjToLastAccess[UnderlyingObj] = Access;
Adam Nemete2b885c2015-04-23 20:09:20 +0000492 DEBUG(dbgs() << " " << *UnderlyingObj << "\n");
Adam Nemet04563272015-02-01 16:56:15 +0000493 }
494 }
495 }
496 }
497 }
498}
499
Adam Nemet04563272015-02-01 16:56:15 +0000500static bool isInBoundsGep(Value *Ptr) {
501 if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(Ptr))
502 return GEP->isInBounds();
503 return false;
504}
505
506/// \brief Check whether the access through \p Ptr has a constant stride.
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000507static int isStridedPtr(ScalarEvolution *SE, Value *Ptr, const Loop *Lp,
508 const ValueToValueMap &StridesMap) {
Adam Nemet04563272015-02-01 16:56:15 +0000509 const Type *Ty = Ptr->getType();
510 assert(Ty->isPointerTy() && "Unexpected non-ptr");
511
512 // Make sure that the pointer does not point to aggregate types.
513 const PointerType *PtrTy = cast<PointerType>(Ty);
514 if (PtrTy->getElementType()->isAggregateType()) {
Adam Nemet339f42b2015-02-19 19:15:07 +0000515 DEBUG(dbgs() << "LAA: Bad stride - Not a pointer to a scalar type"
516 << *Ptr << "\n");
Adam Nemet04563272015-02-01 16:56:15 +0000517 return 0;
518 }
519
520 const SCEV *PtrScev = replaceSymbolicStrideSCEV(SE, StridesMap, Ptr);
521
522 const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(PtrScev);
523 if (!AR) {
Adam Nemet339f42b2015-02-19 19:15:07 +0000524 DEBUG(dbgs() << "LAA: Bad stride - Not an AddRecExpr pointer "
Adam Nemet04d41632015-02-19 19:14:34 +0000525 << *Ptr << " SCEV: " << *PtrScev << "\n");
Adam Nemet04563272015-02-01 16:56:15 +0000526 return 0;
527 }
528
529 // The accesss function must stride over the innermost loop.
530 if (Lp != AR->getLoop()) {
Adam Nemet339f42b2015-02-19 19:15:07 +0000531 DEBUG(dbgs() << "LAA: Bad stride - Not striding over innermost loop " <<
Adam Nemet04d41632015-02-19 19:14:34 +0000532 *Ptr << " SCEV: " << *PtrScev << "\n");
Adam Nemet04563272015-02-01 16:56:15 +0000533 }
534
535 // The address calculation must not wrap. Otherwise, a dependence could be
536 // inverted.
537 // An inbounds getelementptr that is a AddRec with a unit stride
538 // cannot wrap per definition. The unit stride requirement is checked later.
539 // An getelementptr without an inbounds attribute and unit stride would have
540 // to access the pointer value "0" which is undefined behavior in address
541 // space 0, therefore we can also vectorize this case.
542 bool IsInBoundsGEP = isInBoundsGep(Ptr);
543 bool IsNoWrapAddRec = AR->getNoWrapFlags(SCEV::NoWrapMask);
544 bool IsInAddressSpaceZero = PtrTy->getAddressSpace() == 0;
545 if (!IsNoWrapAddRec && !IsInBoundsGEP && !IsInAddressSpaceZero) {
Adam Nemet339f42b2015-02-19 19:15:07 +0000546 DEBUG(dbgs() << "LAA: Bad stride - Pointer may wrap in the address space "
Adam Nemet04d41632015-02-19 19:14:34 +0000547 << *Ptr << " SCEV: " << *PtrScev << "\n");
Adam Nemet04563272015-02-01 16:56:15 +0000548 return 0;
549 }
550
551 // Check the step is constant.
552 const SCEV *Step = AR->getStepRecurrence(*SE);
553
554 // Calculate the pointer stride and check if it is consecutive.
555 const SCEVConstant *C = dyn_cast<SCEVConstant>(Step);
556 if (!C) {
Adam Nemet339f42b2015-02-19 19:15:07 +0000557 DEBUG(dbgs() << "LAA: Bad stride - Not a constant strided " << *Ptr <<
Adam Nemet04d41632015-02-19 19:14:34 +0000558 " SCEV: " << *PtrScev << "\n");
Adam Nemet04563272015-02-01 16:56:15 +0000559 return 0;
560 }
561
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000562 auto &DL = Lp->getHeader()->getModule()->getDataLayout();
563 int64_t Size = DL.getTypeAllocSize(PtrTy->getElementType());
Adam Nemet04563272015-02-01 16:56:15 +0000564 const APInt &APStepVal = C->getValue()->getValue();
565
566 // Huge step value - give up.
567 if (APStepVal.getBitWidth() > 64)
568 return 0;
569
570 int64_t StepVal = APStepVal.getSExtValue();
571
572 // Strided access.
573 int64_t Stride = StepVal / Size;
574 int64_t Rem = StepVal % Size;
575 if (Rem)
576 return 0;
577
578 // If the SCEV could wrap but we have an inbounds gep with a unit stride we
579 // know we can't "wrap around the address space". In case of address space
580 // zero we know that this won't happen without triggering undefined behavior.
581 if (!IsNoWrapAddRec && (IsInBoundsGEP || IsInAddressSpaceZero) &&
582 Stride != 1 && Stride != -1)
583 return 0;
584
585 return Stride;
586}
587
Adam Nemet9c926572015-03-10 17:40:37 +0000588bool MemoryDepChecker::Dependence::isSafeForVectorization(DepType Type) {
589 switch (Type) {
590 case NoDep:
591 case Forward:
592 case BackwardVectorizable:
593 return true;
594
595 case Unknown:
596 case ForwardButPreventsForwarding:
597 case Backward:
598 case BackwardVectorizableButPreventsForwarding:
599 return false;
600 }
David Majnemerd388e932015-03-10 20:23:29 +0000601 llvm_unreachable("unexpected DepType!");
Adam Nemet9c926572015-03-10 17:40:37 +0000602}
603
604bool MemoryDepChecker::Dependence::isInterestingDependence(DepType Type) {
605 switch (Type) {
606 case NoDep:
607 case Forward:
608 return false;
609
610 case BackwardVectorizable:
611 case Unknown:
612 case ForwardButPreventsForwarding:
613 case Backward:
614 case BackwardVectorizableButPreventsForwarding:
615 return true;
616 }
David Majnemerd388e932015-03-10 20:23:29 +0000617 llvm_unreachable("unexpected DepType!");
Adam Nemet9c926572015-03-10 17:40:37 +0000618}
619
620bool MemoryDepChecker::Dependence::isPossiblyBackward() const {
621 switch (Type) {
622 case NoDep:
623 case Forward:
624 case ForwardButPreventsForwarding:
625 return false;
626
627 case Unknown:
628 case BackwardVectorizable:
629 case Backward:
630 case BackwardVectorizableButPreventsForwarding:
631 return true;
632 }
David Majnemerd388e932015-03-10 20:23:29 +0000633 llvm_unreachable("unexpected DepType!");
Adam Nemet9c926572015-03-10 17:40:37 +0000634}
635
Adam Nemet04563272015-02-01 16:56:15 +0000636bool MemoryDepChecker::couldPreventStoreLoadForward(unsigned Distance,
637 unsigned TypeByteSize) {
638 // If loads occur at a distance that is not a multiple of a feasible vector
639 // factor store-load forwarding does not take place.
640 // Positive dependences might cause troubles because vectorizing them might
641 // prevent store-load forwarding making vectorized code run a lot slower.
642 // a[i] = a[i-3] ^ a[i-8];
643 // The stores to a[i:i+1] don't align with the stores to a[i-3:i-2] and
644 // hence on your typical architecture store-load forwarding does not take
645 // place. Vectorizing in such cases does not make sense.
646 // Store-load forwarding distance.
647 const unsigned NumCyclesForStoreLoadThroughMemory = 8*TypeByteSize;
648 // Maximum vector factor.
Adam Nemetf219c642015-02-19 19:14:52 +0000649 unsigned MaxVFWithoutSLForwardIssues =
650 VectorizerParams::MaxVectorWidth * TypeByteSize;
Adam Nemet04d41632015-02-19 19:14:34 +0000651 if(MaxSafeDepDistBytes < MaxVFWithoutSLForwardIssues)
Adam Nemet04563272015-02-01 16:56:15 +0000652 MaxVFWithoutSLForwardIssues = MaxSafeDepDistBytes;
653
654 for (unsigned vf = 2*TypeByteSize; vf <= MaxVFWithoutSLForwardIssues;
655 vf *= 2) {
656 if (Distance % vf && Distance / vf < NumCyclesForStoreLoadThroughMemory) {
657 MaxVFWithoutSLForwardIssues = (vf >>=1);
658 break;
659 }
660 }
661
Adam Nemet04d41632015-02-19 19:14:34 +0000662 if (MaxVFWithoutSLForwardIssues< 2*TypeByteSize) {
Adam Nemet339f42b2015-02-19 19:15:07 +0000663 DEBUG(dbgs() << "LAA: Distance " << Distance <<
Adam Nemet04d41632015-02-19 19:14:34 +0000664 " that could cause a store-load forwarding conflict\n");
Adam Nemet04563272015-02-01 16:56:15 +0000665 return true;
666 }
667
668 if (MaxVFWithoutSLForwardIssues < MaxSafeDepDistBytes &&
Adam Nemetf219c642015-02-19 19:14:52 +0000669 MaxVFWithoutSLForwardIssues !=
670 VectorizerParams::MaxVectorWidth * TypeByteSize)
Adam Nemet04563272015-02-01 16:56:15 +0000671 MaxSafeDepDistBytes = MaxVFWithoutSLForwardIssues;
672 return false;
673}
674
Adam Nemet9c926572015-03-10 17:40:37 +0000675MemoryDepChecker::Dependence::DepType
676MemoryDepChecker::isDependent(const MemAccessInfo &A, unsigned AIdx,
677 const MemAccessInfo &B, unsigned BIdx,
678 const ValueToValueMap &Strides) {
Adam Nemet04563272015-02-01 16:56:15 +0000679 assert (AIdx < BIdx && "Must pass arguments in program order");
680
681 Value *APtr = A.getPointer();
682 Value *BPtr = B.getPointer();
683 bool AIsWrite = A.getInt();
684 bool BIsWrite = B.getInt();
685
686 // Two reads are independent.
687 if (!AIsWrite && !BIsWrite)
Adam Nemet9c926572015-03-10 17:40:37 +0000688 return Dependence::NoDep;
Adam Nemet04563272015-02-01 16:56:15 +0000689
690 // We cannot check pointers in different address spaces.
691 if (APtr->getType()->getPointerAddressSpace() !=
692 BPtr->getType()->getPointerAddressSpace())
Adam Nemet9c926572015-03-10 17:40:37 +0000693 return Dependence::Unknown;
Adam Nemet04563272015-02-01 16:56:15 +0000694
695 const SCEV *AScev = replaceSymbolicStrideSCEV(SE, Strides, APtr);
696 const SCEV *BScev = replaceSymbolicStrideSCEV(SE, Strides, BPtr);
697
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000698 int StrideAPtr = isStridedPtr(SE, APtr, InnermostLoop, Strides);
699 int StrideBPtr = isStridedPtr(SE, BPtr, InnermostLoop, Strides);
Adam Nemet04563272015-02-01 16:56:15 +0000700
701 const SCEV *Src = AScev;
702 const SCEV *Sink = BScev;
703
704 // If the induction step is negative we have to invert source and sink of the
705 // dependence.
706 if (StrideAPtr < 0) {
707 //Src = BScev;
708 //Sink = AScev;
709 std::swap(APtr, BPtr);
710 std::swap(Src, Sink);
711 std::swap(AIsWrite, BIsWrite);
712 std::swap(AIdx, BIdx);
713 std::swap(StrideAPtr, StrideBPtr);
714 }
715
716 const SCEV *Dist = SE->getMinusSCEV(Sink, Src);
717
Adam Nemet339f42b2015-02-19 19:15:07 +0000718 DEBUG(dbgs() << "LAA: Src Scev: " << *Src << "Sink Scev: " << *Sink
Adam Nemet04d41632015-02-19 19:14:34 +0000719 << "(Induction step: " << StrideAPtr << ")\n");
Adam Nemet339f42b2015-02-19 19:15:07 +0000720 DEBUG(dbgs() << "LAA: Distance for " << *InstMap[AIdx] << " to "
Adam Nemet04d41632015-02-19 19:14:34 +0000721 << *InstMap[BIdx] << ": " << *Dist << "\n");
Adam Nemet04563272015-02-01 16:56:15 +0000722
723 // Need consecutive accesses. We don't want to vectorize
724 // "A[B[i]] += ..." and similar code or pointer arithmetic that could wrap in
725 // the address space.
726 if (!StrideAPtr || !StrideBPtr || StrideAPtr != StrideBPtr){
727 DEBUG(dbgs() << "Non-consecutive pointer access\n");
Adam Nemet9c926572015-03-10 17:40:37 +0000728 return Dependence::Unknown;
Adam Nemet04563272015-02-01 16:56:15 +0000729 }
730
731 const SCEVConstant *C = dyn_cast<SCEVConstant>(Dist);
732 if (!C) {
Adam Nemet339f42b2015-02-19 19:15:07 +0000733 DEBUG(dbgs() << "LAA: Dependence because of non-constant distance\n");
Adam Nemet04563272015-02-01 16:56:15 +0000734 ShouldRetryWithRuntimeCheck = true;
Adam Nemet9c926572015-03-10 17:40:37 +0000735 return Dependence::Unknown;
Adam Nemet04563272015-02-01 16:56:15 +0000736 }
737
738 Type *ATy = APtr->getType()->getPointerElementType();
739 Type *BTy = BPtr->getType()->getPointerElementType();
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000740 auto &DL = InnermostLoop->getHeader()->getModule()->getDataLayout();
741 unsigned TypeByteSize = DL.getTypeAllocSize(ATy);
Adam Nemet04563272015-02-01 16:56:15 +0000742
743 // Negative distances are not plausible dependencies.
744 const APInt &Val = C->getValue()->getValue();
745 if (Val.isNegative()) {
746 bool IsTrueDataDependence = (AIsWrite && !BIsWrite);
747 if (IsTrueDataDependence &&
748 (couldPreventStoreLoadForward(Val.abs().getZExtValue(), TypeByteSize) ||
749 ATy != BTy))
Adam Nemet9c926572015-03-10 17:40:37 +0000750 return Dependence::ForwardButPreventsForwarding;
Adam Nemet04563272015-02-01 16:56:15 +0000751
Adam Nemet339f42b2015-02-19 19:15:07 +0000752 DEBUG(dbgs() << "LAA: Dependence is negative: NoDep\n");
Adam Nemet9c926572015-03-10 17:40:37 +0000753 return Dependence::Forward;
Adam Nemet04563272015-02-01 16:56:15 +0000754 }
755
756 // Write to the same location with the same size.
757 // Could be improved to assert type sizes are the same (i32 == float, etc).
758 if (Val == 0) {
759 if (ATy == BTy)
Adam Nemet9c926572015-03-10 17:40:37 +0000760 return Dependence::NoDep;
Adam Nemet339f42b2015-02-19 19:15:07 +0000761 DEBUG(dbgs() << "LAA: Zero dependence difference but different types\n");
Adam Nemet9c926572015-03-10 17:40:37 +0000762 return Dependence::Unknown;
Adam Nemet04563272015-02-01 16:56:15 +0000763 }
764
765 assert(Val.isStrictlyPositive() && "Expect a positive value");
766
Adam Nemet04563272015-02-01 16:56:15 +0000767 if (ATy != BTy) {
Adam Nemet04d41632015-02-19 19:14:34 +0000768 DEBUG(dbgs() <<
Adam Nemet339f42b2015-02-19 19:15:07 +0000769 "LAA: ReadWrite-Write positive dependency with different types\n");
Adam Nemet9c926572015-03-10 17:40:37 +0000770 return Dependence::Unknown;
Adam Nemet04563272015-02-01 16:56:15 +0000771 }
772
773 unsigned Distance = (unsigned) Val.getZExtValue();
774
775 // Bail out early if passed-in parameters make vectorization not feasible.
Adam Nemetf219c642015-02-19 19:14:52 +0000776 unsigned ForcedFactor = (VectorizerParams::VectorizationFactor ?
777 VectorizerParams::VectorizationFactor : 1);
778 unsigned ForcedUnroll = (VectorizerParams::VectorizationInterleave ?
779 VectorizerParams::VectorizationInterleave : 1);
Adam Nemet04563272015-02-01 16:56:15 +0000780
781 // The distance must be bigger than the size needed for a vectorized version
782 // of the operation and the size of the vectorized operation must not be
783 // bigger than the currrent maximum size.
784 if (Distance < 2*TypeByteSize ||
785 2*TypeByteSize > MaxSafeDepDistBytes ||
786 Distance < TypeByteSize * ForcedUnroll * ForcedFactor) {
Adam Nemet339f42b2015-02-19 19:15:07 +0000787 DEBUG(dbgs() << "LAA: Failure because of Positive distance "
Adam Nemet04d41632015-02-19 19:14:34 +0000788 << Val.getSExtValue() << '\n');
Adam Nemet9c926572015-03-10 17:40:37 +0000789 return Dependence::Backward;
Adam Nemet04563272015-02-01 16:56:15 +0000790 }
791
Adam Nemet9cc0c392015-02-26 17:58:48 +0000792 // Positive distance bigger than max vectorization factor.
Adam Nemet04563272015-02-01 16:56:15 +0000793 MaxSafeDepDistBytes = Distance < MaxSafeDepDistBytes ?
794 Distance : MaxSafeDepDistBytes;
795
796 bool IsTrueDataDependence = (!AIsWrite && BIsWrite);
797 if (IsTrueDataDependence &&
798 couldPreventStoreLoadForward(Distance, TypeByteSize))
Adam Nemet9c926572015-03-10 17:40:37 +0000799 return Dependence::BackwardVectorizableButPreventsForwarding;
Adam Nemet04563272015-02-01 16:56:15 +0000800
Adam Nemet339f42b2015-02-19 19:15:07 +0000801 DEBUG(dbgs() << "LAA: Positive distance " << Val.getSExtValue() <<
Adam Nemet04d41632015-02-19 19:14:34 +0000802 " with max VF = " << MaxSafeDepDistBytes / TypeByteSize << '\n');
Adam Nemet04563272015-02-01 16:56:15 +0000803
Adam Nemet9c926572015-03-10 17:40:37 +0000804 return Dependence::BackwardVectorizable;
Adam Nemet04563272015-02-01 16:56:15 +0000805}
806
Adam Nemetdee666b2015-03-10 17:40:34 +0000807bool MemoryDepChecker::areDepsSafe(DepCandidates &AccessSets,
Adam Nemet04563272015-02-01 16:56:15 +0000808 MemAccessInfoSet &CheckDeps,
Adam Nemet8bc61df2015-02-24 00:41:59 +0000809 const ValueToValueMap &Strides) {
Adam Nemet04563272015-02-01 16:56:15 +0000810
811 MaxSafeDepDistBytes = -1U;
812 while (!CheckDeps.empty()) {
813 MemAccessInfo CurAccess = *CheckDeps.begin();
814
815 // Get the relevant memory access set.
816 EquivalenceClasses<MemAccessInfo>::iterator I =
817 AccessSets.findValue(AccessSets.getLeaderValue(CurAccess));
818
819 // Check accesses within this set.
820 EquivalenceClasses<MemAccessInfo>::member_iterator AI, AE;
821 AI = AccessSets.member_begin(I), AE = AccessSets.member_end();
822
823 // Check every access pair.
824 while (AI != AE) {
825 CheckDeps.erase(*AI);
826 EquivalenceClasses<MemAccessInfo>::member_iterator OI = std::next(AI);
827 while (OI != AE) {
828 // Check every accessing instruction pair in program order.
829 for (std::vector<unsigned>::iterator I1 = Accesses[*AI].begin(),
830 I1E = Accesses[*AI].end(); I1 != I1E; ++I1)
831 for (std::vector<unsigned>::iterator I2 = Accesses[*OI].begin(),
832 I2E = Accesses[*OI].end(); I2 != I2E; ++I2) {
Adam Nemet9c926572015-03-10 17:40:37 +0000833 auto A = std::make_pair(&*AI, *I1);
834 auto B = std::make_pair(&*OI, *I2);
835
836 assert(*I1 != *I2);
837 if (*I1 > *I2)
838 std::swap(A, B);
839
840 Dependence::DepType Type =
841 isDependent(*A.first, A.second, *B.first, B.second, Strides);
842 SafeForVectorization &= Dependence::isSafeForVectorization(Type);
843
844 // Gather dependences unless we accumulated MaxInterestingDependence
845 // dependences. In that case return as soon as we find the first
846 // unsafe dependence. This puts a limit on this quadratic
847 // algorithm.
848 if (RecordInterestingDependences) {
849 if (Dependence::isInterestingDependence(Type))
850 InterestingDependences.push_back(
851 Dependence(A.second, B.second, Type));
852
853 if (InterestingDependences.size() >= MaxInterestingDependence) {
854 RecordInterestingDependences = false;
855 InterestingDependences.clear();
856 DEBUG(dbgs() << "Too many dependences, stopped recording\n");
857 }
858 }
859 if (!RecordInterestingDependences && !SafeForVectorization)
Adam Nemet04563272015-02-01 16:56:15 +0000860 return false;
861 }
862 ++OI;
863 }
864 AI++;
865 }
866 }
Adam Nemet9c926572015-03-10 17:40:37 +0000867
868 DEBUG(dbgs() << "Total Interesting Dependences: "
869 << InterestingDependences.size() << "\n");
870 return SafeForVectorization;
Adam Nemet04563272015-02-01 16:56:15 +0000871}
872
Adam Nemetec1e2bb2015-03-10 18:54:26 +0000873SmallVector<Instruction *, 4>
874MemoryDepChecker::getInstructionsForAccess(Value *Ptr, bool isWrite) const {
875 MemAccessInfo Access(Ptr, isWrite);
876 auto &IndexVector = Accesses.find(Access)->second;
877
878 SmallVector<Instruction *, 4> Insts;
879 std::transform(IndexVector.begin(), IndexVector.end(),
880 std::back_inserter(Insts),
881 [&](unsigned Idx) { return this->InstMap[Idx]; });
882 return Insts;
883}
884
Adam Nemet58913d62015-03-10 17:40:43 +0000885const char *MemoryDepChecker::Dependence::DepName[] = {
886 "NoDep", "Unknown", "Forward", "ForwardButPreventsForwarding", "Backward",
887 "BackwardVectorizable", "BackwardVectorizableButPreventsForwarding"};
888
889void MemoryDepChecker::Dependence::print(
890 raw_ostream &OS, unsigned Depth,
891 const SmallVectorImpl<Instruction *> &Instrs) const {
892 OS.indent(Depth) << DepName[Type] << ":\n";
893 OS.indent(Depth + 2) << *Instrs[Source] << " -> \n";
894 OS.indent(Depth + 2) << *Instrs[Destination] << "\n";
895}
896
Adam Nemet929c38e2015-02-19 19:15:10 +0000897bool LoopAccessInfo::canAnalyzeLoop() {
Adam Nemet8dcb3b62015-04-17 22:43:10 +0000898 // We need to have a loop header.
899 DEBUG(dbgs() << "LAA: Found a loop: " <<
900 TheLoop->getHeader()->getName() << '\n');
901
Adam Nemet929c38e2015-02-19 19:15:10 +0000902 // We can only analyze innermost loops.
903 if (!TheLoop->empty()) {
Adam Nemet8dcb3b62015-04-17 22:43:10 +0000904 DEBUG(dbgs() << "LAA: loop is not the innermost loop\n");
Adam Nemet2bd6e982015-02-19 19:15:15 +0000905 emitAnalysis(LoopAccessReport() << "loop is not the innermost loop");
Adam Nemet929c38e2015-02-19 19:15:10 +0000906 return false;
907 }
908
909 // We must have a single backedge.
910 if (TheLoop->getNumBackEdges() != 1) {
Adam Nemet8dcb3b62015-04-17 22:43:10 +0000911 DEBUG(dbgs() << "LAA: loop control flow is not understood by analyzer\n");
Adam Nemet929c38e2015-02-19 19:15:10 +0000912 emitAnalysis(
Adam Nemet2bd6e982015-02-19 19:15:15 +0000913 LoopAccessReport() <<
Adam Nemet929c38e2015-02-19 19:15:10 +0000914 "loop control flow is not understood by analyzer");
915 return false;
916 }
917
918 // We must have a single exiting block.
919 if (!TheLoop->getExitingBlock()) {
Adam Nemet8dcb3b62015-04-17 22:43:10 +0000920 DEBUG(dbgs() << "LAA: loop control flow is not understood by analyzer\n");
Adam Nemet929c38e2015-02-19 19:15:10 +0000921 emitAnalysis(
Adam Nemet2bd6e982015-02-19 19:15:15 +0000922 LoopAccessReport() <<
Adam Nemet929c38e2015-02-19 19:15:10 +0000923 "loop control flow is not understood by analyzer");
924 return false;
925 }
926
927 // We only handle bottom-tested loops, i.e. loop in which the condition is
928 // checked at the end of each iteration. With that we can assume that all
929 // instructions in the loop are executed the same number of times.
930 if (TheLoop->getExitingBlock() != TheLoop->getLoopLatch()) {
Adam Nemet8dcb3b62015-04-17 22:43:10 +0000931 DEBUG(dbgs() << "LAA: loop control flow is not understood by analyzer\n");
Adam Nemet929c38e2015-02-19 19:15:10 +0000932 emitAnalysis(
Adam Nemet2bd6e982015-02-19 19:15:15 +0000933 LoopAccessReport() <<
Adam Nemet929c38e2015-02-19 19:15:10 +0000934 "loop control flow is not understood by analyzer");
935 return false;
936 }
937
Adam Nemet929c38e2015-02-19 19:15:10 +0000938 // ScalarEvolution needs to be able to find the exit count.
939 const SCEV *ExitCount = SE->getBackedgeTakenCount(TheLoop);
940 if (ExitCount == SE->getCouldNotCompute()) {
Adam Nemet2bd6e982015-02-19 19:15:15 +0000941 emitAnalysis(LoopAccessReport() <<
Adam Nemet929c38e2015-02-19 19:15:10 +0000942 "could not determine number of loop iterations");
943 DEBUG(dbgs() << "LAA: SCEV could not compute the loop exit count.\n");
944 return false;
945 }
946
947 return true;
948}
949
Adam Nemet8bc61df2015-02-24 00:41:59 +0000950void LoopAccessInfo::analyzeLoop(const ValueToValueMap &Strides) {
Adam Nemet04563272015-02-01 16:56:15 +0000951
952 typedef SmallVector<Value*, 16> ValueVector;
953 typedef SmallPtrSet<Value*, 16> ValueSet;
954
955 // Holds the Load and Store *instructions*.
956 ValueVector Loads;
957 ValueVector Stores;
958
959 // Holds all the different accesses in the loop.
960 unsigned NumReads = 0;
961 unsigned NumReadWrites = 0;
962
963 PtrRtCheck.Pointers.clear();
964 PtrRtCheck.Need = false;
965
966 const bool IsAnnotatedParallel = TheLoop->isAnnotatedParallel();
Adam Nemet04563272015-02-01 16:56:15 +0000967
968 // For each block.
969 for (Loop::block_iterator bb = TheLoop->block_begin(),
970 be = TheLoop->block_end(); bb != be; ++bb) {
971
972 // Scan the BB and collect legal loads and stores.
973 for (BasicBlock::iterator it = (*bb)->begin(), e = (*bb)->end(); it != e;
974 ++it) {
975
976 // If this is a load, save it. If this instruction can read from memory
977 // but is not a load, then we quit. Notice that we don't handle function
978 // calls that read or write.
979 if (it->mayReadFromMemory()) {
980 // Many math library functions read the rounding mode. We will only
981 // vectorize a loop if it contains known function calls that don't set
982 // the flag. Therefore, it is safe to ignore this read from memory.
983 CallInst *Call = dyn_cast<CallInst>(it);
984 if (Call && getIntrinsicIDForCall(Call, TLI))
985 continue;
986
Michael Zolotukhin9b3cf602015-03-17 19:46:50 +0000987 // If the function has an explicit vectorized counterpart, we can safely
988 // assume that it can be vectorized.
989 if (Call && !Call->isNoBuiltin() && Call->getCalledFunction() &&
990 TLI->isFunctionVectorizable(Call->getCalledFunction()->getName()))
991 continue;
992
Adam Nemet04563272015-02-01 16:56:15 +0000993 LoadInst *Ld = dyn_cast<LoadInst>(it);
994 if (!Ld || (!Ld->isSimple() && !IsAnnotatedParallel)) {
Adam Nemet2bd6e982015-02-19 19:15:15 +0000995 emitAnalysis(LoopAccessReport(Ld)
Adam Nemet04563272015-02-01 16:56:15 +0000996 << "read with atomic ordering or volatile read");
Adam Nemet339f42b2015-02-19 19:15:07 +0000997 DEBUG(dbgs() << "LAA: Found a non-simple load.\n");
Adam Nemet436018c2015-02-19 19:15:00 +0000998 CanVecMem = false;
999 return;
Adam Nemet04563272015-02-01 16:56:15 +00001000 }
1001 NumLoads++;
1002 Loads.push_back(Ld);
1003 DepChecker.addAccess(Ld);
1004 continue;
1005 }
1006
1007 // Save 'store' instructions. Abort if other instructions write to memory.
1008 if (it->mayWriteToMemory()) {
1009 StoreInst *St = dyn_cast<StoreInst>(it);
1010 if (!St) {
Adam Nemet2bd6e982015-02-19 19:15:15 +00001011 emitAnalysis(LoopAccessReport(it) <<
Adam Nemet04d41632015-02-19 19:14:34 +00001012 "instruction cannot be vectorized");
Adam Nemet436018c2015-02-19 19:15:00 +00001013 CanVecMem = false;
1014 return;
Adam Nemet04563272015-02-01 16:56:15 +00001015 }
1016 if (!St->isSimple() && !IsAnnotatedParallel) {
Adam Nemet2bd6e982015-02-19 19:15:15 +00001017 emitAnalysis(LoopAccessReport(St)
Adam Nemet04563272015-02-01 16:56:15 +00001018 << "write with atomic ordering or volatile write");
Adam Nemet339f42b2015-02-19 19:15:07 +00001019 DEBUG(dbgs() << "LAA: Found a non-simple store.\n");
Adam Nemet436018c2015-02-19 19:15:00 +00001020 CanVecMem = false;
1021 return;
Adam Nemet04563272015-02-01 16:56:15 +00001022 }
1023 NumStores++;
1024 Stores.push_back(St);
1025 DepChecker.addAccess(St);
1026 }
1027 } // Next instr.
1028 } // Next block.
1029
1030 // Now we have two lists that hold the loads and the stores.
1031 // Next, we find the pointers that they use.
1032
1033 // Check if we see any stores. If there are no stores, then we don't
1034 // care if the pointers are *restrict*.
1035 if (!Stores.size()) {
Adam Nemet339f42b2015-02-19 19:15:07 +00001036 DEBUG(dbgs() << "LAA: Found a read-only loop!\n");
Adam Nemet436018c2015-02-19 19:15:00 +00001037 CanVecMem = true;
1038 return;
Adam Nemet04563272015-02-01 16:56:15 +00001039 }
1040
Adam Nemetdee666b2015-03-10 17:40:34 +00001041 MemoryDepChecker::DepCandidates DependentAccesses;
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001042 AccessAnalysis Accesses(TheLoop->getHeader()->getModule()->getDataLayout(),
Adam Nemete2b885c2015-04-23 20:09:20 +00001043 AA, LI, DependentAccesses);
Adam Nemet04563272015-02-01 16:56:15 +00001044
1045 // Holds the analyzed pointers. We don't want to call GetUnderlyingObjects
1046 // multiple times on the same object. If the ptr is accessed twice, once
1047 // for read and once for write, it will only appear once (on the write
1048 // list). This is okay, since we are going to check for conflicts between
1049 // writes and between reads and writes, but not between reads and reads.
1050 ValueSet Seen;
1051
1052 ValueVector::iterator I, IE;
1053 for (I = Stores.begin(), IE = Stores.end(); I != IE; ++I) {
1054 StoreInst *ST = cast<StoreInst>(*I);
1055 Value* Ptr = ST->getPointerOperand();
Adam Nemetce482502015-04-08 17:48:40 +00001056 // Check for store to loop invariant address.
1057 StoreToLoopInvariantAddress |= isUniform(Ptr);
Adam Nemet04563272015-02-01 16:56:15 +00001058 // If we did *not* see this pointer before, insert it to the read-write
1059 // list. At this phase it is only a 'write' list.
1060 if (Seen.insert(Ptr).second) {
1061 ++NumReadWrites;
1062
1063 AliasAnalysis::Location Loc = AA->getLocation(ST);
1064 // The TBAA metadata could have a control dependency on the predication
1065 // condition, so we cannot rely on it when determining whether or not we
1066 // need runtime pointer checks.
Adam Nemet01abb2c2015-02-18 03:43:19 +00001067 if (blockNeedsPredication(ST->getParent(), TheLoop, DT))
Adam Nemet04563272015-02-01 16:56:15 +00001068 Loc.AATags.TBAA = nullptr;
1069
1070 Accesses.addStore(Loc);
1071 }
1072 }
1073
1074 if (IsAnnotatedParallel) {
Adam Nemet04d41632015-02-19 19:14:34 +00001075 DEBUG(dbgs()
Adam Nemet339f42b2015-02-19 19:15:07 +00001076 << "LAA: A loop annotated parallel, ignore memory dependency "
Adam Nemet04d41632015-02-19 19:14:34 +00001077 << "checks.\n");
Adam Nemet436018c2015-02-19 19:15:00 +00001078 CanVecMem = true;
1079 return;
Adam Nemet04563272015-02-01 16:56:15 +00001080 }
1081
1082 for (I = Loads.begin(), IE = Loads.end(); I != IE; ++I) {
1083 LoadInst *LD = cast<LoadInst>(*I);
1084 Value* Ptr = LD->getPointerOperand();
1085 // If we did *not* see this pointer before, insert it to the
1086 // read list. If we *did* see it before, then it is already in
1087 // the read-write list. This allows us to vectorize expressions
1088 // such as A[i] += x; Because the address of A[i] is a read-write
1089 // pointer. This only works if the index of A[i] is consecutive.
1090 // If the address of i is unknown (for example A[B[i]]) then we may
1091 // read a few words, modify, and write a few words, and some of the
1092 // words may be written to the same address.
1093 bool IsReadOnlyPtr = false;
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001094 if (Seen.insert(Ptr).second || !isStridedPtr(SE, Ptr, TheLoop, Strides)) {
Adam Nemet04563272015-02-01 16:56:15 +00001095 ++NumReads;
1096 IsReadOnlyPtr = true;
1097 }
1098
1099 AliasAnalysis::Location Loc = AA->getLocation(LD);
1100 // The TBAA metadata could have a control dependency on the predication
1101 // condition, so we cannot rely on it when determining whether or not we
1102 // need runtime pointer checks.
Adam Nemet01abb2c2015-02-18 03:43:19 +00001103 if (blockNeedsPredication(LD->getParent(), TheLoop, DT))
Adam Nemet04563272015-02-01 16:56:15 +00001104 Loc.AATags.TBAA = nullptr;
1105
1106 Accesses.addLoad(Loc, IsReadOnlyPtr);
1107 }
1108
1109 // If we write (or read-write) to a single destination and there are no
1110 // other reads in this loop then is it safe to vectorize.
1111 if (NumReadWrites == 1 && NumReads == 0) {
Adam Nemet339f42b2015-02-19 19:15:07 +00001112 DEBUG(dbgs() << "LAA: Found a write-only loop!\n");
Adam Nemet436018c2015-02-19 19:15:00 +00001113 CanVecMem = true;
1114 return;
Adam Nemet04563272015-02-01 16:56:15 +00001115 }
1116
1117 // Build dependence sets and check whether we need a runtime pointer bounds
1118 // check.
1119 Accesses.buildDependenceSets();
1120 bool NeedRTCheck = Accesses.isRTCheckNeeded();
1121
1122 // Find pointers with computable bounds. We are going to use this information
1123 // to place a runtime bound check.
Adam Nemet04563272015-02-01 16:56:15 +00001124 bool CanDoRT = false;
1125 if (NeedRTCheck)
1126 CanDoRT = Accesses.canCheckPtrAtRT(PtrRtCheck, NumComparisons, SE, TheLoop,
1127 Strides);
1128
Adam Nemet339f42b2015-02-19 19:15:07 +00001129 DEBUG(dbgs() << "LAA: We need to do " << NumComparisons <<
Adam Nemet04d41632015-02-19 19:14:34 +00001130 " pointer comparisons.\n");
Adam Nemet04563272015-02-01 16:56:15 +00001131
1132 // If we only have one set of dependences to check pointers among we don't
1133 // need a runtime check.
1134 if (NumComparisons == 0 && NeedRTCheck)
1135 NeedRTCheck = false;
1136
Adam Nemet949e91a2015-03-10 19:12:41 +00001137 // Check that we found the bounds for the pointer.
Adam Nemetb6dc76f2015-03-10 18:54:19 +00001138 if (CanDoRT)
Adam Nemet339f42b2015-02-19 19:15:07 +00001139 DEBUG(dbgs() << "LAA: We can perform a memory runtime check if needed.\n");
Adam Nemetb6dc76f2015-03-10 18:54:19 +00001140 else if (NeedRTCheck) {
Adam Nemet2bd6e982015-02-19 19:15:15 +00001141 emitAnalysis(LoopAccessReport() << "cannot identify array bounds");
Adam Nemet339f42b2015-02-19 19:15:07 +00001142 DEBUG(dbgs() << "LAA: We can't vectorize because we can't find " <<
Adam Nemet04d41632015-02-19 19:14:34 +00001143 "the array bounds.\n");
Adam Nemet04563272015-02-01 16:56:15 +00001144 PtrRtCheck.reset();
Adam Nemet436018c2015-02-19 19:15:00 +00001145 CanVecMem = false;
1146 return;
Adam Nemet04563272015-02-01 16:56:15 +00001147 }
1148
1149 PtrRtCheck.Need = NeedRTCheck;
1150
Adam Nemet436018c2015-02-19 19:15:00 +00001151 CanVecMem = true;
Adam Nemet04563272015-02-01 16:56:15 +00001152 if (Accesses.isDependencyCheckNeeded()) {
Adam Nemet339f42b2015-02-19 19:15:07 +00001153 DEBUG(dbgs() << "LAA: Checking memory dependencies\n");
Adam Nemet04563272015-02-01 16:56:15 +00001154 CanVecMem = DepChecker.areDepsSafe(
1155 DependentAccesses, Accesses.getDependenciesToCheck(), Strides);
1156 MaxSafeDepDistBytes = DepChecker.getMaxSafeDepDistBytes();
1157
1158 if (!CanVecMem && DepChecker.shouldRetryWithRuntimeCheck()) {
Adam Nemet339f42b2015-02-19 19:15:07 +00001159 DEBUG(dbgs() << "LAA: Retrying with memory checks\n");
Adam Nemet04563272015-02-01 16:56:15 +00001160 NeedRTCheck = true;
1161
1162 // Clear the dependency checks. We assume they are not needed.
1163 Accesses.resetDepChecks();
1164
1165 PtrRtCheck.reset();
1166 PtrRtCheck.Need = true;
1167
1168 CanDoRT = Accesses.canCheckPtrAtRT(PtrRtCheck, NumComparisons, SE,
1169 TheLoop, Strides, true);
Adam Nemet949e91a2015-03-10 19:12:41 +00001170 // Check that we found the bounds for the pointer.
Adam Nemetb6dc76f2015-03-10 18:54:19 +00001171 if (!CanDoRT && NumComparisons > 0) {
1172 emitAnalysis(LoopAccessReport()
1173 << "cannot check memory dependencies at runtime");
1174 DEBUG(dbgs() << "LAA: Can't vectorize with memory checks\n");
1175 PtrRtCheck.reset();
1176 CanVecMem = false;
1177 return;
1178 }
1179
Adam Nemet04563272015-02-01 16:56:15 +00001180 CanVecMem = true;
1181 }
1182 }
1183
Adam Nemet4bb90a72015-03-10 21:47:39 +00001184 if (CanVecMem)
1185 DEBUG(dbgs() << "LAA: No unsafe dependent memory operations in loop. We"
1186 << (NeedRTCheck ? "" : " don't")
1187 << " need a runtime memory check.\n");
1188 else {
Adam Nemet2bd6e982015-02-19 19:15:15 +00001189 emitAnalysis(LoopAccessReport() <<
Adam Nemet04d41632015-02-19 19:14:34 +00001190 "unsafe dependent memory operations in loop");
Adam Nemet4bb90a72015-03-10 21:47:39 +00001191 DEBUG(dbgs() << "LAA: unsafe dependent memory operations in loop\n");
1192 }
Adam Nemet04563272015-02-01 16:56:15 +00001193}
1194
Adam Nemet01abb2c2015-02-18 03:43:19 +00001195bool LoopAccessInfo::blockNeedsPredication(BasicBlock *BB, Loop *TheLoop,
1196 DominatorTree *DT) {
Adam Nemet04563272015-02-01 16:56:15 +00001197 assert(TheLoop->contains(BB) && "Unknown block used");
1198
1199 // Blocks that do not dominate the latch need predication.
1200 BasicBlock* Latch = TheLoop->getLoopLatch();
1201 return !DT->dominates(BB, Latch);
1202}
1203
Adam Nemet2bd6e982015-02-19 19:15:15 +00001204void LoopAccessInfo::emitAnalysis(LoopAccessReport &Message) {
Adam Nemetc9228532015-02-19 19:14:56 +00001205 assert(!Report && "Multiple reports generated");
1206 Report = Message;
Adam Nemet04563272015-02-01 16:56:15 +00001207}
1208
Adam Nemet57ac7662015-02-19 19:15:21 +00001209bool LoopAccessInfo::isUniform(Value *V) const {
Adam Nemet04563272015-02-01 16:56:15 +00001210 return (SE->isLoopInvariant(SE->getSCEV(V), TheLoop));
1211}
Adam Nemet7206d7a2015-02-06 18:31:04 +00001212
1213// FIXME: this function is currently a duplicate of the one in
1214// LoopVectorize.cpp.
1215static Instruction *getFirstInst(Instruction *FirstInst, Value *V,
1216 Instruction *Loc) {
1217 if (FirstInst)
1218 return FirstInst;
1219 if (Instruction *I = dyn_cast<Instruction>(V))
1220 return I->getParent() == Loc->getParent() ? I : nullptr;
1221 return nullptr;
1222}
1223
Adam Nemetec1e2bb2015-03-10 18:54:26 +00001224std::pair<Instruction *, Instruction *> LoopAccessInfo::addRuntimeCheck(
1225 Instruction *Loc, const SmallVectorImpl<int> *PtrPartition) const {
Adam Nemet7206d7a2015-02-06 18:31:04 +00001226 if (!PtrRtCheck.Need)
Adam Nemet90fec842015-04-02 17:51:57 +00001227 return std::make_pair(nullptr, nullptr);
Adam Nemet7206d7a2015-02-06 18:31:04 +00001228
1229 unsigned NumPointers = PtrRtCheck.Pointers.size();
1230 SmallVector<TrackingVH<Value> , 2> Starts;
1231 SmallVector<TrackingVH<Value> , 2> Ends;
1232
1233 LLVMContext &Ctx = Loc->getContext();
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001234 SCEVExpander Exp(*SE, DL, "induction");
Adam Nemet7206d7a2015-02-06 18:31:04 +00001235 Instruction *FirstInst = nullptr;
1236
1237 for (unsigned i = 0; i < NumPointers; ++i) {
1238 Value *Ptr = PtrRtCheck.Pointers[i];
1239 const SCEV *Sc = SE->getSCEV(Ptr);
1240
1241 if (SE->isLoopInvariant(Sc, TheLoop)) {
Adam Nemet339f42b2015-02-19 19:15:07 +00001242 DEBUG(dbgs() << "LAA: Adding RT check for a loop invariant ptr:" <<
Adam Nemet04d41632015-02-19 19:14:34 +00001243 *Ptr <<"\n");
Adam Nemet7206d7a2015-02-06 18:31:04 +00001244 Starts.push_back(Ptr);
1245 Ends.push_back(Ptr);
1246 } else {
Adam Nemet339f42b2015-02-19 19:15:07 +00001247 DEBUG(dbgs() << "LAA: Adding RT check for range:" << *Ptr << '\n');
Adam Nemet7206d7a2015-02-06 18:31:04 +00001248 unsigned AS = Ptr->getType()->getPointerAddressSpace();
1249
1250 // Use this type for pointer arithmetic.
1251 Type *PtrArithTy = Type::getInt8PtrTy(Ctx, AS);
1252
1253 Value *Start = Exp.expandCodeFor(PtrRtCheck.Starts[i], PtrArithTy, Loc);
1254 Value *End = Exp.expandCodeFor(PtrRtCheck.Ends[i], PtrArithTy, Loc);
1255 Starts.push_back(Start);
1256 Ends.push_back(End);
1257 }
1258 }
1259
1260 IRBuilder<> ChkBuilder(Loc);
1261 // Our instructions might fold to a constant.
1262 Value *MemoryRuntimeCheck = nullptr;
1263 for (unsigned i = 0; i < NumPointers; ++i) {
1264 for (unsigned j = i+1; j < NumPointers; ++j) {
Adam Nemetec1e2bb2015-03-10 18:54:26 +00001265 if (!PtrRtCheck.needsChecking(i, j, PtrPartition))
Adam Nemet7206d7a2015-02-06 18:31:04 +00001266 continue;
1267
1268 unsigned AS0 = Starts[i]->getType()->getPointerAddressSpace();
1269 unsigned AS1 = Starts[j]->getType()->getPointerAddressSpace();
1270
1271 assert((AS0 == Ends[j]->getType()->getPointerAddressSpace()) &&
1272 (AS1 == Ends[i]->getType()->getPointerAddressSpace()) &&
1273 "Trying to bounds check pointers with different address spaces");
1274
1275 Type *PtrArithTy0 = Type::getInt8PtrTy(Ctx, AS0);
1276 Type *PtrArithTy1 = Type::getInt8PtrTy(Ctx, AS1);
1277
1278 Value *Start0 = ChkBuilder.CreateBitCast(Starts[i], PtrArithTy0, "bc");
1279 Value *Start1 = ChkBuilder.CreateBitCast(Starts[j], PtrArithTy1, "bc");
1280 Value *End0 = ChkBuilder.CreateBitCast(Ends[i], PtrArithTy1, "bc");
1281 Value *End1 = ChkBuilder.CreateBitCast(Ends[j], PtrArithTy0, "bc");
1282
1283 Value *Cmp0 = ChkBuilder.CreateICmpULE(Start0, End1, "bound0");
1284 FirstInst = getFirstInst(FirstInst, Cmp0, Loc);
1285 Value *Cmp1 = ChkBuilder.CreateICmpULE(Start1, End0, "bound1");
1286 FirstInst = getFirstInst(FirstInst, Cmp1, Loc);
1287 Value *IsConflict = ChkBuilder.CreateAnd(Cmp0, Cmp1, "found.conflict");
1288 FirstInst = getFirstInst(FirstInst, IsConflict, Loc);
1289 if (MemoryRuntimeCheck) {
1290 IsConflict = ChkBuilder.CreateOr(MemoryRuntimeCheck, IsConflict,
1291 "conflict.rdx");
1292 FirstInst = getFirstInst(FirstInst, IsConflict, Loc);
1293 }
1294 MemoryRuntimeCheck = IsConflict;
1295 }
1296 }
1297
Adam Nemet90fec842015-04-02 17:51:57 +00001298 if (!MemoryRuntimeCheck)
1299 return std::make_pair(nullptr, nullptr);
1300
Adam Nemet7206d7a2015-02-06 18:31:04 +00001301 // We have to do this trickery because the IRBuilder might fold the check to a
1302 // constant expression in which case there is no Instruction anchored in a
1303 // the block.
1304 Instruction *Check = BinaryOperator::CreateAnd(MemoryRuntimeCheck,
1305 ConstantInt::getTrue(Ctx));
1306 ChkBuilder.Insert(Check, "memcheck.conflict");
1307 FirstInst = getFirstInst(FirstInst, Check, Loc);
1308 return std::make_pair(FirstInst, Check);
1309}
Adam Nemet3bfd93d2015-02-19 19:15:04 +00001310
1311LoopAccessInfo::LoopAccessInfo(Loop *L, ScalarEvolution *SE,
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001312 const DataLayout &DL,
Adam Nemet3bfd93d2015-02-19 19:15:04 +00001313 const TargetLibraryInfo *TLI, AliasAnalysis *AA,
Adam Nemete2b885c2015-04-23 20:09:20 +00001314 DominatorTree *DT, LoopInfo *LI,
Adam Nemet8bc61df2015-02-24 00:41:59 +00001315 const ValueToValueMap &Strides)
Adam Nemet98c4c5d2015-03-10 18:54:23 +00001316 : DepChecker(SE, L), NumComparisons(0), TheLoop(L), SE(SE), DL(DL),
Adam Nemete2b885c2015-04-23 20:09:20 +00001317 TLI(TLI), AA(AA), DT(DT), LI(LI), NumLoads(0), NumStores(0),
Adam Nemetce482502015-04-08 17:48:40 +00001318 MaxSafeDepDistBytes(-1U), CanVecMem(false),
1319 StoreToLoopInvariantAddress(false) {
Adam Nemet929c38e2015-02-19 19:15:10 +00001320 if (canAnalyzeLoop())
1321 analyzeLoop(Strides);
Adam Nemet3bfd93d2015-02-19 19:15:04 +00001322}
1323
Adam Nemete91cc6e2015-02-19 19:15:19 +00001324void LoopAccessInfo::print(raw_ostream &OS, unsigned Depth) const {
1325 if (CanVecMem) {
Adam Nemet26da8e92015-04-14 01:12:55 +00001326 if (PtrRtCheck.Need)
Adam Nemete91cc6e2015-02-19 19:15:19 +00001327 OS.indent(Depth) << "Memory dependences are safe with run-time checks\n";
Adam Nemet26da8e92015-04-14 01:12:55 +00001328 else
1329 OS.indent(Depth) << "Memory dependences are safe\n";
Adam Nemete91cc6e2015-02-19 19:15:19 +00001330 }
1331
Adam Nemetce482502015-04-08 17:48:40 +00001332 OS.indent(Depth) << "Store to invariant address was "
1333 << (StoreToLoopInvariantAddress ? "" : "not ")
1334 << "found in loop.\n";
1335
Adam Nemete91cc6e2015-02-19 19:15:19 +00001336 if (Report)
1337 OS.indent(Depth) << "Report: " << Report->str() << "\n";
1338
Adam Nemet58913d62015-03-10 17:40:43 +00001339 if (auto *InterestingDependences = DepChecker.getInterestingDependences()) {
1340 OS.indent(Depth) << "Interesting Dependences:\n";
1341 for (auto &Dep : *InterestingDependences) {
1342 Dep.print(OS, Depth + 2, DepChecker.getMemoryInstructions());
1343 OS << "\n";
1344 }
1345 } else
1346 OS.indent(Depth) << "Too many interesting dependences, not recorded\n";
Adam Nemete91cc6e2015-02-19 19:15:19 +00001347
1348 // List the pair of accesses need run-time checks to prove independence.
1349 PtrRtCheck.print(OS, Depth);
1350 OS << "\n";
1351}
1352
Adam Nemet8bc61df2015-02-24 00:41:59 +00001353const LoopAccessInfo &
1354LoopAccessAnalysis::getInfo(Loop *L, const ValueToValueMap &Strides) {
Adam Nemet3bfd93d2015-02-19 19:15:04 +00001355 auto &LAI = LoopAccessInfoMap[L];
1356
1357#ifndef NDEBUG
1358 assert((!LAI || LAI->NumSymbolicStrides == Strides.size()) &&
1359 "Symbolic strides changed for loop");
1360#endif
1361
1362 if (!LAI) {
Mehdi Aminia28d91d2015-03-10 02:37:25 +00001363 const DataLayout &DL = L->getHeader()->getModule()->getDataLayout();
Adam Nemete2b885c2015-04-23 20:09:20 +00001364 LAI = llvm::make_unique<LoopAccessInfo>(L, SE, DL, TLI, AA, DT, LI,
1365 Strides);
Adam Nemet3bfd93d2015-02-19 19:15:04 +00001366#ifndef NDEBUG
1367 LAI->NumSymbolicStrides = Strides.size();
1368#endif
1369 }
1370 return *LAI.get();
1371}
1372
Adam Nemete91cc6e2015-02-19 19:15:19 +00001373void LoopAccessAnalysis::print(raw_ostream &OS, const Module *M) const {
1374 LoopAccessAnalysis &LAA = *const_cast<LoopAccessAnalysis *>(this);
1375
Adam Nemete91cc6e2015-02-19 19:15:19 +00001376 ValueToValueMap NoSymbolicStrides;
1377
1378 for (Loop *TopLevelLoop : *LI)
1379 for (Loop *L : depth_first(TopLevelLoop)) {
1380 OS.indent(2) << L->getHeader()->getName() << ":\n";
1381 auto &LAI = LAA.getInfo(L, NoSymbolicStrides);
1382 LAI.print(OS, 4);
1383 }
1384}
1385
Adam Nemet3bfd93d2015-02-19 19:15:04 +00001386bool LoopAccessAnalysis::runOnFunction(Function &F) {
1387 SE = &getAnalysis<ScalarEvolution>();
Adam Nemet3bfd93d2015-02-19 19:15:04 +00001388 auto *TLIP = getAnalysisIfAvailable<TargetLibraryInfoWrapperPass>();
1389 TLI = TLIP ? &TLIP->getTLI() : nullptr;
1390 AA = &getAnalysis<AliasAnalysis>();
1391 DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree();
Adam Nemete2b885c2015-04-23 20:09:20 +00001392 LI = &getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
Adam Nemet3bfd93d2015-02-19 19:15:04 +00001393
1394 return false;
1395}
1396
1397void LoopAccessAnalysis::getAnalysisUsage(AnalysisUsage &AU) const {
1398 AU.addRequired<ScalarEvolution>();
1399 AU.addRequired<AliasAnalysis>();
1400 AU.addRequired<DominatorTreeWrapperPass>();
Adam Nemete91cc6e2015-02-19 19:15:19 +00001401 AU.addRequired<LoopInfoWrapperPass>();
Adam Nemet3bfd93d2015-02-19 19:15:04 +00001402
1403 AU.setPreservesAll();
1404}
1405
1406char LoopAccessAnalysis::ID = 0;
1407static const char laa_name[] = "Loop Access Analysis";
1408#define LAA_NAME "loop-accesses"
1409
1410INITIALIZE_PASS_BEGIN(LoopAccessAnalysis, LAA_NAME, laa_name, false, true)
1411INITIALIZE_AG_DEPENDENCY(AliasAnalysis)
1412INITIALIZE_PASS_DEPENDENCY(ScalarEvolution)
1413INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
Adam Nemete91cc6e2015-02-19 19:15:19 +00001414INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass)
Adam Nemet3bfd93d2015-02-19 19:15:04 +00001415INITIALIZE_PASS_END(LoopAccessAnalysis, LAA_NAME, laa_name, false, true)
1416
1417namespace llvm {
1418 Pass *createLAAPass() {
1419 return new LoopAccessAnalysis();
1420 }
1421}