1 //===- LoopAccessAnalysis.cpp - Loop Access Analysis Implementation --------==//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // The implementation for the loop memory dependence that was originally
11 // developed for the loop vectorizer.
13 //===----------------------------------------------------------------------===//
15 #include "llvm/Analysis/LoopAccessAnalysis.h"
16 #include "llvm/Analysis/LoopInfo.h"
17 #include "llvm/Analysis/ScalarEvolutionExpander.h"
18 #include "llvm/Analysis/ValueTracking.h"
19 #include "llvm/IR/DiagnosticInfo.h"
20 #include "llvm/IR/Dominators.h"
21 #include "llvm/IR/IRBuilder.h"
22 #include "llvm/Support/Debug.h"
23 #include "llvm/Transforms/Utils/VectorUtils.h"
26 #define DEBUG_TYPE "loop-accesses"
28 static cl::opt<unsigned, true>
29 VectorizationFactor("force-vector-width", cl::Hidden,
30 cl::desc("Sets the SIMD width. Zero is autoselect."),
31 cl::location(VectorizerParams::VectorizationFactor));
32 unsigned VectorizerParams::VectorizationFactor = 0;
34 static cl::opt<unsigned, true>
35 VectorizationInterleave("force-vector-interleave", cl::Hidden,
36 cl::desc("Sets the vectorization interleave count. "
37 "Zero is autoselect."),
39 VectorizerParams::VectorizationInterleave));
40 unsigned VectorizerParams::VectorizationInterleave = 0;
42 /// When performing memory disambiguation checks at runtime do not make more
43 /// than this number of comparisons.
44 const unsigned VectorizerParams::RuntimeMemoryCheckThreshold = 8;
46 /// Maximum SIMD width.
47 const unsigned VectorizerParams::MaxVectorWidth = 64;
49 bool VectorizerParams::isInterleaveForced() {
50 return ::VectorizationInterleave.getNumOccurrences() > 0;
53 void LoopAccessReport::emitAnalysis(const LoopAccessReport &Message,
54 const Function *TheFunction,
56 const char *PassName) {
57 DebugLoc DL = TheLoop->getStartLoc();
58 if (const Instruction *I = Message.getInstr())
59 DL = I->getDebugLoc();
60 emitOptimizationRemarkAnalysis(TheFunction->getContext(), PassName,
61 *TheFunction, DL, Message.str());
64 Value *llvm::stripIntegerCast(Value *V) {
65 if (CastInst *CI = dyn_cast<CastInst>(V))
66 if (CI->getOperand(0)->getType()->isIntegerTy())
67 return CI->getOperand(0);
71 const SCEV *llvm::replaceSymbolicStrideSCEV(ScalarEvolution *SE,
72 const ValueToValueMap &PtrToStride,
73 Value *Ptr, Value *OrigPtr) {
75 const SCEV *OrigSCEV = SE->getSCEV(Ptr);
77 // If there is an entry in the map return the SCEV of the pointer with the
78 // symbolic stride replaced by one.
79 ValueToValueMap::const_iterator SI =
80 PtrToStride.find(OrigPtr ? OrigPtr : Ptr);
81 if (SI != PtrToStride.end()) {
82 Value *StrideVal = SI->second;
85 StrideVal = stripIntegerCast(StrideVal);
87 // Replace symbolic stride by one.
88 Value *One = ConstantInt::get(StrideVal->getType(), 1);
89 ValueToValueMap RewriteMap;
90 RewriteMap[StrideVal] = One;
93 SCEVParameterRewriter::rewrite(OrigSCEV, *SE, RewriteMap, true);
94 DEBUG(dbgs() << "LAA: Replacing SCEV: " << *OrigSCEV << " by: " << *ByOne
99 // Otherwise, just return the SCEV of the original pointer.
100 return SE->getSCEV(Ptr);
103 void LoopAccessInfo::RuntimePointerCheck::insert(
104 ScalarEvolution *SE, Loop *Lp, Value *Ptr, bool WritePtr, unsigned DepSetId,
105 unsigned ASId, const ValueToValueMap &Strides) {
106 // Get the stride replaced scev.
107 const SCEV *Sc = replaceSymbolicStrideSCEV(SE, Strides, Ptr);
108 const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(Sc);
109 assert(AR && "Invalid addrec expression");
110 const SCEV *Ex = SE->getBackedgeTakenCount(Lp);
111 const SCEV *ScEnd = AR->evaluateAtIteration(Ex, *SE);
112 Pointers.push_back(Ptr);
113 Starts.push_back(AR->getStart());
114 Ends.push_back(ScEnd);
115 IsWritePtr.push_back(WritePtr);
116 DependencySetId.push_back(DepSetId);
117 AliasSetId.push_back(ASId);
120 bool LoopAccessInfo::RuntimePointerCheck::needsChecking(unsigned I,
122 // No need to check if two readonly pointers intersect.
123 if (!IsWritePtr[I] && !IsWritePtr[J])
126 // Only need to check pointers between two different dependency sets.
127 if (DependencySetId[I] == DependencySetId[J])
130 // Only need to check pointers in the same alias set.
131 if (AliasSetId[I] != AliasSetId[J])
137 void LoopAccessInfo::RuntimePointerCheck::print(raw_ostream &OS,
138 unsigned Depth) const {
139 unsigned NumPointers = Pointers.size();
140 if (NumPointers == 0)
143 OS.indent(Depth) << "Run-time memory checks:\n";
145 for (unsigned I = 0; I < NumPointers; ++I)
146 for (unsigned J = I + 1; J < NumPointers; ++J)
147 if (needsChecking(I, J)) {
148 OS.indent(Depth) << N++ << ":\n";
149 OS.indent(Depth + 2) << *Pointers[I] << "\n";
150 OS.indent(Depth + 2) << *Pointers[J] << "\n";
155 /// \brief Analyses memory accesses in a loop.
157 /// Checks whether run time pointer checks are needed and builds sets for data
158 /// dependence checking.
159 class AccessAnalysis {
161 /// \brief Read or write access location.
162 typedef PointerIntPair<Value *, 1, bool> MemAccessInfo;
163 typedef SmallPtrSet<MemAccessInfo, 8> MemAccessInfoSet;
165 /// \brief Set of potential dependent memory accesses.
166 typedef EquivalenceClasses<MemAccessInfo> DepCandidates;
168 AccessAnalysis(const DataLayout *Dl, AliasAnalysis *AA, DepCandidates &DA) :
169 DL(Dl), AST(*AA), DepCands(DA), IsRTCheckNeeded(false) {}
171 /// \brief Register a load and whether it is only read from.
172 void addLoad(AliasAnalysis::Location &Loc, bool IsReadOnly) {
173 Value *Ptr = const_cast<Value*>(Loc.Ptr);
174 AST.add(Ptr, AliasAnalysis::UnknownSize, Loc.AATags);
175 Accesses.insert(MemAccessInfo(Ptr, false));
177 ReadOnlyPtr.insert(Ptr);
180 /// \brief Register a store.
181 void addStore(AliasAnalysis::Location &Loc) {
182 Value *Ptr = const_cast<Value*>(Loc.Ptr);
183 AST.add(Ptr, AliasAnalysis::UnknownSize, Loc.AATags);
184 Accesses.insert(MemAccessInfo(Ptr, true));
187 /// \brief Check whether we can check the pointers at runtime for
188 /// non-intersection.
189 bool canCheckPtrAtRT(LoopAccessInfo::RuntimePointerCheck &RtCheck,
190 unsigned &NumComparisons, ScalarEvolution *SE,
191 Loop *TheLoop, const ValueToValueMap &Strides,
192 bool ShouldCheckStride = false);
194 /// \brief Goes over all memory accesses, checks whether a RT check is needed
195 /// and builds sets of dependent accesses.
196 void buildDependenceSets() {
197 processMemAccesses();
200 bool isRTCheckNeeded() { return IsRTCheckNeeded; }
202 bool isDependencyCheckNeeded() { return !CheckDeps.empty(); }
203 void resetDepChecks() { CheckDeps.clear(); }
205 MemAccessInfoSet &getDependenciesToCheck() { return CheckDeps; }
208 typedef SetVector<MemAccessInfo> PtrAccessSet;
210 /// \brief Go over all memory access and check whether runtime pointer checks
211 /// are needed /// and build sets of dependency check candidates.
212 void processMemAccesses();
214 /// Set of all accesses.
215 PtrAccessSet Accesses;
217 /// Set of accesses that need a further dependence check.
218 MemAccessInfoSet CheckDeps;
220 /// Set of pointers that are read only.
221 SmallPtrSet<Value*, 16> ReadOnlyPtr;
223 const DataLayout *DL;
225 /// An alias set tracker to partition the access set by underlying object and
226 //intrinsic property (such as TBAA metadata).
229 /// Sets of potentially dependent accesses - members of one set share an
230 /// underlying pointer. The set "CheckDeps" identfies which sets really need a
231 /// dependence check.
232 DepCandidates &DepCands;
234 bool IsRTCheckNeeded;
237 } // end anonymous namespace
239 /// \brief Check whether a pointer can participate in a runtime bounds check.
240 static bool hasComputableBounds(ScalarEvolution *SE,
241 const ValueToValueMap &Strides, Value *Ptr) {
242 const SCEV *PtrScev = replaceSymbolicStrideSCEV(SE, Strides, Ptr);
243 const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(PtrScev);
247 return AR->isAffine();
250 /// \brief Check the stride of the pointer and ensure that it does not wrap in
251 /// the address space.
252 static int isStridedPtr(ScalarEvolution *SE, const DataLayout *DL, Value *Ptr,
253 const Loop *Lp, const ValueToValueMap &StridesMap);
255 bool AccessAnalysis::canCheckPtrAtRT(
256 LoopAccessInfo::RuntimePointerCheck &RtCheck, unsigned &NumComparisons,
257 ScalarEvolution *SE, Loop *TheLoop, const ValueToValueMap &StridesMap,
258 bool ShouldCheckStride) {
259 // Find pointers with computable bounds. We are going to use this information
260 // to place a runtime bound check.
263 bool IsDepCheckNeeded = isDependencyCheckNeeded();
266 // We assign a consecutive id to access from different alias sets.
267 // Accesses between different groups doesn't need to be checked.
269 for (auto &AS : AST) {
270 unsigned NumReadPtrChecks = 0;
271 unsigned NumWritePtrChecks = 0;
273 // We assign consecutive id to access from different dependence sets.
274 // Accesses within the same set don't need a runtime check.
275 unsigned RunningDepId = 1;
276 DenseMap<Value *, unsigned> DepSetId;
279 Value *Ptr = A.getValue();
280 bool IsWrite = Accesses.count(MemAccessInfo(Ptr, true));
281 MemAccessInfo Access(Ptr, IsWrite);
288 if (hasComputableBounds(SE, StridesMap, Ptr) &&
289 // When we run after a failing dependency check we have to make sure we
290 // don't have wrapping pointers.
291 (!ShouldCheckStride ||
292 isStridedPtr(SE, DL, Ptr, TheLoop, StridesMap) == 1)) {
293 // The id of the dependence set.
296 if (IsDepCheckNeeded) {
297 Value *Leader = DepCands.getLeaderValue(Access).getPointer();
298 unsigned &LeaderId = DepSetId[Leader];
300 LeaderId = RunningDepId++;
303 // Each access has its own dependence set.
304 DepId = RunningDepId++;
306 RtCheck.insert(SE, TheLoop, Ptr, IsWrite, DepId, ASId, StridesMap);
308 DEBUG(dbgs() << "LAA: Found a runtime check ptr:" << *Ptr << '\n');
314 if (IsDepCheckNeeded && CanDoRT && RunningDepId == 2)
315 NumComparisons += 0; // Only one dependence set.
317 NumComparisons += (NumWritePtrChecks * (NumReadPtrChecks +
318 NumWritePtrChecks - 1));
324 // If the pointers that we would use for the bounds comparison have different
325 // address spaces, assume the values aren't directly comparable, so we can't
326 // use them for the runtime check. We also have to assume they could
327 // overlap. In the future there should be metadata for whether address spaces
329 unsigned NumPointers = RtCheck.Pointers.size();
330 for (unsigned i = 0; i < NumPointers; ++i) {
331 for (unsigned j = i + 1; j < NumPointers; ++j) {
332 // Only need to check pointers between two different dependency sets.
333 if (RtCheck.DependencySetId[i] == RtCheck.DependencySetId[j])
335 // Only need to check pointers in the same alias set.
336 if (RtCheck.AliasSetId[i] != RtCheck.AliasSetId[j])
339 Value *PtrI = RtCheck.Pointers[i];
340 Value *PtrJ = RtCheck.Pointers[j];
342 unsigned ASi = PtrI->getType()->getPointerAddressSpace();
343 unsigned ASj = PtrJ->getType()->getPointerAddressSpace();
345 DEBUG(dbgs() << "LAA: Runtime check would require comparison between"
346 " different address spaces\n");
355 void AccessAnalysis::processMemAccesses() {
356 // We process the set twice: first we process read-write pointers, last we
357 // process read-only pointers. This allows us to skip dependence tests for
358 // read-only pointers.
360 DEBUG(dbgs() << "LAA: Processing memory accesses...\n");
361 DEBUG(dbgs() << " AST: "; AST.dump());
362 DEBUG(dbgs() << "LAA: Accesses:\n");
364 for (auto A : Accesses)
365 dbgs() << "\t" << *A.getPointer() << " (" <<
366 (A.getInt() ? "write" : (ReadOnlyPtr.count(A.getPointer()) ?
367 "read-only" : "read")) << ")\n";
370 // The AliasSetTracker has nicely partitioned our pointers by metadata
371 // compatibility and potential for underlying-object overlap. As a result, we
372 // only need to check for potential pointer dependencies within each alias
374 for (auto &AS : AST) {
375 // Note that both the alias-set tracker and the alias sets themselves used
376 // linked lists internally and so the iteration order here is deterministic
377 // (matching the original instruction order within each set).
379 bool SetHasWrite = false;
381 // Map of pointers to last access encountered.
382 typedef DenseMap<Value*, MemAccessInfo> UnderlyingObjToAccessMap;
383 UnderlyingObjToAccessMap ObjToLastAccess;
385 // Set of access to check after all writes have been processed.
386 PtrAccessSet DeferredAccesses;
388 // Iterate over each alias set twice, once to process read/write pointers,
389 // and then to process read-only pointers.
390 for (int SetIteration = 0; SetIteration < 2; ++SetIteration) {
391 bool UseDeferred = SetIteration > 0;
392 PtrAccessSet &S = UseDeferred ? DeferredAccesses : Accesses;
395 Value *Ptr = AV.getValue();
397 // For a single memory access in AliasSetTracker, Accesses may contain
398 // both read and write, and they both need to be handled for CheckDeps.
400 if (AC.getPointer() != Ptr)
403 bool IsWrite = AC.getInt();
405 // If we're using the deferred access set, then it contains only
407 bool IsReadOnlyPtr = ReadOnlyPtr.count(Ptr) && !IsWrite;
408 if (UseDeferred && !IsReadOnlyPtr)
410 // Otherwise, the pointer must be in the PtrAccessSet, either as a
412 assert(((IsReadOnlyPtr && UseDeferred) || IsWrite ||
413 S.count(MemAccessInfo(Ptr, false))) &&
414 "Alias-set pointer not in the access set?");
416 MemAccessInfo Access(Ptr, IsWrite);
417 DepCands.insert(Access);
419 // Memorize read-only pointers for later processing and skip them in
420 // the first round (they need to be checked after we have seen all
421 // write pointers). Note: we also mark pointer that are not
422 // consecutive as "read-only" pointers (so that we check
423 // "a[b[i]] +="). Hence, we need the second check for "!IsWrite".
424 if (!UseDeferred && IsReadOnlyPtr) {
425 DeferredAccesses.insert(Access);
429 // If this is a write - check other reads and writes for conflicts. If
430 // this is a read only check other writes for conflicts (but only if
431 // there is no other write to the ptr - this is an optimization to
432 // catch "a[i] = a[i] + " without having to do a dependence check).
433 if ((IsWrite || IsReadOnlyPtr) && SetHasWrite) {
434 CheckDeps.insert(Access);
435 IsRTCheckNeeded = true;
441 // Create sets of pointers connected by a shared alias set and
442 // underlying object.
443 typedef SmallVector<Value *, 16> ValueVector;
444 ValueVector TempObjects;
445 GetUnderlyingObjects(Ptr, TempObjects, DL);
446 for (Value *UnderlyingObj : TempObjects) {
447 UnderlyingObjToAccessMap::iterator Prev =
448 ObjToLastAccess.find(UnderlyingObj);
449 if (Prev != ObjToLastAccess.end())
450 DepCands.unionSets(Access, Prev->second);
452 ObjToLastAccess[UnderlyingObj] = Access;
461 /// \brief Checks memory dependences among accesses to the same underlying
462 /// object to determine whether there vectorization is legal or not (and at
463 /// which vectorization factor).
465 /// This class works under the assumption that we already checked that memory
466 /// locations with different underlying pointers are "must-not alias".
467 /// We use the ScalarEvolution framework to symbolically evalutate access
468 /// functions pairs. Since we currently don't restructure the loop we can rely
469 /// on the program order of memory accesses to determine their safety.
470 /// At the moment we will only deem accesses as safe for:
471 /// * A negative constant distance assuming program order.
473 /// Safe: tmp = a[i + 1]; OR a[i + 1] = x;
474 /// a[i] = tmp; y = a[i];
476 /// The latter case is safe because later checks guarantuee that there can't
477 /// be a cycle through a phi node (that is, we check that "x" and "y" is not
478 /// the same variable: a header phi can only be an induction or a reduction, a
479 /// reduction can't have a memory sink, an induction can't have a memory
480 /// source). This is important and must not be violated (or we have to
481 /// resort to checking for cycles through memory).
483 /// * A positive constant distance assuming program order that is bigger
484 /// than the biggest memory access.
486 /// tmp = a[i] OR b[i] = x
487 /// a[i+2] = tmp y = b[i+2];
489 /// Safe distance: 2 x sizeof(a[0]), and 2 x sizeof(b[0]), respectively.
491 /// * Zero distances and all accesses have the same size.
493 class MemoryDepChecker {
495 typedef PointerIntPair<Value *, 1, bool> MemAccessInfo;
496 typedef SmallPtrSet<MemAccessInfo, 8> MemAccessInfoSet;
498 MemoryDepChecker(ScalarEvolution *Se, const DataLayout *Dl, const Loop *L)
499 : SE(Se), DL(Dl), InnermostLoop(L), AccessIdx(0),
500 ShouldRetryWithRuntimeCheck(false) {}
502 /// \brief Register the location (instructions are given increasing numbers)
503 /// of a write access.
504 void addAccess(StoreInst *SI) {
505 Value *Ptr = SI->getPointerOperand();
506 Accesses[MemAccessInfo(Ptr, true)].push_back(AccessIdx);
507 InstMap.push_back(SI);
511 /// \brief Register the location (instructions are given increasing numbers)
512 /// of a write access.
513 void addAccess(LoadInst *LI) {
514 Value *Ptr = LI->getPointerOperand();
515 Accesses[MemAccessInfo(Ptr, false)].push_back(AccessIdx);
516 InstMap.push_back(LI);
520 /// \brief Check whether the dependencies between the accesses are safe.
522 /// Only checks sets with elements in \p CheckDeps.
523 bool areDepsSafe(AccessAnalysis::DepCandidates &AccessSets,
524 MemAccessInfoSet &CheckDeps, const ValueToValueMap &Strides);
526 /// \brief The maximum number of bytes of a vector register we can vectorize
527 /// the accesses safely with.
528 unsigned getMaxSafeDepDistBytes() { return MaxSafeDepDistBytes; }
530 /// \brief In same cases when the dependency check fails we can still
531 /// vectorize the loop with a dynamic array access check.
532 bool shouldRetryWithRuntimeCheck() { return ShouldRetryWithRuntimeCheck; }
536 const DataLayout *DL;
537 const Loop *InnermostLoop;
539 /// \brief Maps access locations (ptr, read/write) to program order.
540 DenseMap<MemAccessInfo, std::vector<unsigned> > Accesses;
542 /// \brief Memory access instructions in program order.
543 SmallVector<Instruction *, 16> InstMap;
545 /// \brief The program order index to be used for the next instruction.
548 // We can access this many bytes in parallel safely.
549 unsigned MaxSafeDepDistBytes;
551 /// \brief If we see a non-constant dependence distance we can still try to
552 /// vectorize this loop with runtime checks.
553 bool ShouldRetryWithRuntimeCheck;
555 /// \brief Check whether there is a plausible dependence between the two
558 /// Access \p A must happen before \p B in program order. The two indices
559 /// identify the index into the program order map.
561 /// This function checks whether there is a plausible dependence (or the
562 /// absence of such can't be proved) between the two accesses. If there is a
563 /// plausible dependence but the dependence distance is bigger than one
564 /// element access it records this distance in \p MaxSafeDepDistBytes (if this
565 /// distance is smaller than any other distance encountered so far).
566 /// Otherwise, this function returns true signaling a possible dependence.
567 bool isDependent(const MemAccessInfo &A, unsigned AIdx,
568 const MemAccessInfo &B, unsigned BIdx,
569 const ValueToValueMap &Strides);
571 /// \brief Check whether the data dependence could prevent store-load
573 bool couldPreventStoreLoadForward(unsigned Distance, unsigned TypeByteSize);
576 } // end anonymous namespace
578 static bool isInBoundsGep(Value *Ptr) {
579 if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(Ptr))
580 return GEP->isInBounds();
584 /// \brief Check whether the access through \p Ptr has a constant stride.
585 static int isStridedPtr(ScalarEvolution *SE, const DataLayout *DL, Value *Ptr,
586 const Loop *Lp, const ValueToValueMap &StridesMap) {
587 const Type *Ty = Ptr->getType();
588 assert(Ty->isPointerTy() && "Unexpected non-ptr");
590 // Make sure that the pointer does not point to aggregate types.
591 const PointerType *PtrTy = cast<PointerType>(Ty);
592 if (PtrTy->getElementType()->isAggregateType()) {
593 DEBUG(dbgs() << "LAA: Bad stride - Not a pointer to a scalar type"
598 const SCEV *PtrScev = replaceSymbolicStrideSCEV(SE, StridesMap, Ptr);
600 const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(PtrScev);
602 DEBUG(dbgs() << "LAA: Bad stride - Not an AddRecExpr pointer "
603 << *Ptr << " SCEV: " << *PtrScev << "\n");
607 // The accesss function must stride over the innermost loop.
608 if (Lp != AR->getLoop()) {
609 DEBUG(dbgs() << "LAA: Bad stride - Not striding over innermost loop " <<
610 *Ptr << " SCEV: " << *PtrScev << "\n");
613 // The address calculation must not wrap. Otherwise, a dependence could be
615 // An inbounds getelementptr that is a AddRec with a unit stride
616 // cannot wrap per definition. The unit stride requirement is checked later.
617 // An getelementptr without an inbounds attribute and unit stride would have
618 // to access the pointer value "0" which is undefined behavior in address
619 // space 0, therefore we can also vectorize this case.
620 bool IsInBoundsGEP = isInBoundsGep(Ptr);
621 bool IsNoWrapAddRec = AR->getNoWrapFlags(SCEV::NoWrapMask);
622 bool IsInAddressSpaceZero = PtrTy->getAddressSpace() == 0;
623 if (!IsNoWrapAddRec && !IsInBoundsGEP && !IsInAddressSpaceZero) {
624 DEBUG(dbgs() << "LAA: Bad stride - Pointer may wrap in the address space "
625 << *Ptr << " SCEV: " << *PtrScev << "\n");
629 // Check the step is constant.
630 const SCEV *Step = AR->getStepRecurrence(*SE);
632 // Calculate the pointer stride and check if it is consecutive.
633 const SCEVConstant *C = dyn_cast<SCEVConstant>(Step);
635 DEBUG(dbgs() << "LAA: Bad stride - Not a constant strided " << *Ptr <<
636 " SCEV: " << *PtrScev << "\n");
640 int64_t Size = DL->getTypeAllocSize(PtrTy->getElementType());
641 const APInt &APStepVal = C->getValue()->getValue();
643 // Huge step value - give up.
644 if (APStepVal.getBitWidth() > 64)
647 int64_t StepVal = APStepVal.getSExtValue();
650 int64_t Stride = StepVal / Size;
651 int64_t Rem = StepVal % Size;
655 // If the SCEV could wrap but we have an inbounds gep with a unit stride we
656 // know we can't "wrap around the address space". In case of address space
657 // zero we know that this won't happen without triggering undefined behavior.
658 if (!IsNoWrapAddRec && (IsInBoundsGEP || IsInAddressSpaceZero) &&
659 Stride != 1 && Stride != -1)
665 bool MemoryDepChecker::couldPreventStoreLoadForward(unsigned Distance,
666 unsigned TypeByteSize) {
667 // If loads occur at a distance that is not a multiple of a feasible vector
668 // factor store-load forwarding does not take place.
669 // Positive dependences might cause troubles because vectorizing them might
670 // prevent store-load forwarding making vectorized code run a lot slower.
671 // a[i] = a[i-3] ^ a[i-8];
672 // The stores to a[i:i+1] don't align with the stores to a[i-3:i-2] and
673 // hence on your typical architecture store-load forwarding does not take
674 // place. Vectorizing in such cases does not make sense.
675 // Store-load forwarding distance.
676 const unsigned NumCyclesForStoreLoadThroughMemory = 8*TypeByteSize;
677 // Maximum vector factor.
678 unsigned MaxVFWithoutSLForwardIssues =
679 VectorizerParams::MaxVectorWidth * TypeByteSize;
680 if(MaxSafeDepDistBytes < MaxVFWithoutSLForwardIssues)
681 MaxVFWithoutSLForwardIssues = MaxSafeDepDistBytes;
683 for (unsigned vf = 2*TypeByteSize; vf <= MaxVFWithoutSLForwardIssues;
685 if (Distance % vf && Distance / vf < NumCyclesForStoreLoadThroughMemory) {
686 MaxVFWithoutSLForwardIssues = (vf >>=1);
691 if (MaxVFWithoutSLForwardIssues< 2*TypeByteSize) {
692 DEBUG(dbgs() << "LAA: Distance " << Distance <<
693 " that could cause a store-load forwarding conflict\n");
697 if (MaxVFWithoutSLForwardIssues < MaxSafeDepDistBytes &&
698 MaxVFWithoutSLForwardIssues !=
699 VectorizerParams::MaxVectorWidth * TypeByteSize)
700 MaxSafeDepDistBytes = MaxVFWithoutSLForwardIssues;
704 bool MemoryDepChecker::isDependent(const MemAccessInfo &A, unsigned AIdx,
705 const MemAccessInfo &B, unsigned BIdx,
706 const ValueToValueMap &Strides) {
707 assert (AIdx < BIdx && "Must pass arguments in program order");
709 Value *APtr = A.getPointer();
710 Value *BPtr = B.getPointer();
711 bool AIsWrite = A.getInt();
712 bool BIsWrite = B.getInt();
714 // Two reads are independent.
715 if (!AIsWrite && !BIsWrite)
718 // We cannot check pointers in different address spaces.
719 if (APtr->getType()->getPointerAddressSpace() !=
720 BPtr->getType()->getPointerAddressSpace())
723 const SCEV *AScev = replaceSymbolicStrideSCEV(SE, Strides, APtr);
724 const SCEV *BScev = replaceSymbolicStrideSCEV(SE, Strides, BPtr);
726 int StrideAPtr = isStridedPtr(SE, DL, APtr, InnermostLoop, Strides);
727 int StrideBPtr = isStridedPtr(SE, DL, BPtr, InnermostLoop, Strides);
729 const SCEV *Src = AScev;
730 const SCEV *Sink = BScev;
732 // If the induction step is negative we have to invert source and sink of the
734 if (StrideAPtr < 0) {
737 std::swap(APtr, BPtr);
738 std::swap(Src, Sink);
739 std::swap(AIsWrite, BIsWrite);
740 std::swap(AIdx, BIdx);
741 std::swap(StrideAPtr, StrideBPtr);
744 const SCEV *Dist = SE->getMinusSCEV(Sink, Src);
746 DEBUG(dbgs() << "LAA: Src Scev: " << *Src << "Sink Scev: " << *Sink
747 << "(Induction step: " << StrideAPtr << ")\n");
748 DEBUG(dbgs() << "LAA: Distance for " << *InstMap[AIdx] << " to "
749 << *InstMap[BIdx] << ": " << *Dist << "\n");
751 // Need consecutive accesses. We don't want to vectorize
752 // "A[B[i]] += ..." and similar code or pointer arithmetic that could wrap in
753 // the address space.
754 if (!StrideAPtr || !StrideBPtr || StrideAPtr != StrideBPtr){
755 DEBUG(dbgs() << "Non-consecutive pointer access\n");
759 const SCEVConstant *C = dyn_cast<SCEVConstant>(Dist);
761 DEBUG(dbgs() << "LAA: Dependence because of non-constant distance\n");
762 ShouldRetryWithRuntimeCheck = true;
766 Type *ATy = APtr->getType()->getPointerElementType();
767 Type *BTy = BPtr->getType()->getPointerElementType();
768 unsigned TypeByteSize = DL->getTypeAllocSize(ATy);
770 // Negative distances are not plausible dependencies.
771 const APInt &Val = C->getValue()->getValue();
772 if (Val.isNegative()) {
773 bool IsTrueDataDependence = (AIsWrite && !BIsWrite);
774 if (IsTrueDataDependence &&
775 (couldPreventStoreLoadForward(Val.abs().getZExtValue(), TypeByteSize) ||
779 DEBUG(dbgs() << "LAA: Dependence is negative: NoDep\n");
783 // Write to the same location with the same size.
784 // Could be improved to assert type sizes are the same (i32 == float, etc).
788 DEBUG(dbgs() << "LAA: Zero dependence difference but different types\n");
792 assert(Val.isStrictlyPositive() && "Expect a positive value");
794 // Positive distance bigger than max vectorization factor.
797 "LAA: ReadWrite-Write positive dependency with different types\n");
801 unsigned Distance = (unsigned) Val.getZExtValue();
803 // Bail out early if passed-in parameters make vectorization not feasible.
804 unsigned ForcedFactor = (VectorizerParams::VectorizationFactor ?
805 VectorizerParams::VectorizationFactor : 1);
806 unsigned ForcedUnroll = (VectorizerParams::VectorizationInterleave ?
807 VectorizerParams::VectorizationInterleave : 1);
809 // The distance must be bigger than the size needed for a vectorized version
810 // of the operation and the size of the vectorized operation must not be
811 // bigger than the currrent maximum size.
812 if (Distance < 2*TypeByteSize ||
813 2*TypeByteSize > MaxSafeDepDistBytes ||
814 Distance < TypeByteSize * ForcedUnroll * ForcedFactor) {
815 DEBUG(dbgs() << "LAA: Failure because of Positive distance "
816 << Val.getSExtValue() << '\n');
820 MaxSafeDepDistBytes = Distance < MaxSafeDepDistBytes ?
821 Distance : MaxSafeDepDistBytes;
823 bool IsTrueDataDependence = (!AIsWrite && BIsWrite);
824 if (IsTrueDataDependence &&
825 couldPreventStoreLoadForward(Distance, TypeByteSize))
828 DEBUG(dbgs() << "LAA: Positive distance " << Val.getSExtValue() <<
829 " with max VF = " << MaxSafeDepDistBytes / TypeByteSize << '\n');
834 bool MemoryDepChecker::areDepsSafe(AccessAnalysis::DepCandidates &AccessSets,
835 MemAccessInfoSet &CheckDeps,
836 const ValueToValueMap &Strides) {
838 MaxSafeDepDistBytes = -1U;
839 while (!CheckDeps.empty()) {
840 MemAccessInfo CurAccess = *CheckDeps.begin();
842 // Get the relevant memory access set.
843 EquivalenceClasses<MemAccessInfo>::iterator I =
844 AccessSets.findValue(AccessSets.getLeaderValue(CurAccess));
846 // Check accesses within this set.
847 EquivalenceClasses<MemAccessInfo>::member_iterator AI, AE;
848 AI = AccessSets.member_begin(I), AE = AccessSets.member_end();
850 // Check every access pair.
852 CheckDeps.erase(*AI);
853 EquivalenceClasses<MemAccessInfo>::member_iterator OI = std::next(AI);
855 // Check every accessing instruction pair in program order.
856 for (std::vector<unsigned>::iterator I1 = Accesses[*AI].begin(),
857 I1E = Accesses[*AI].end(); I1 != I1E; ++I1)
858 for (std::vector<unsigned>::iterator I2 = Accesses[*OI].begin(),
859 I2E = Accesses[*OI].end(); I2 != I2E; ++I2) {
860 if (*I1 < *I2 && isDependent(*AI, *I1, *OI, *I2, Strides))
862 if (*I2 < *I1 && isDependent(*OI, *I2, *AI, *I1, Strides))
873 bool LoopAccessInfo::canAnalyzeLoop() {
874 // We can only analyze innermost loops.
875 if (!TheLoop->empty()) {
876 emitAnalysis(LoopAccessReport() << "loop is not the innermost loop");
880 // We must have a single backedge.
881 if (TheLoop->getNumBackEdges() != 1) {
883 LoopAccessReport() <<
884 "loop control flow is not understood by analyzer");
888 // We must have a single exiting block.
889 if (!TheLoop->getExitingBlock()) {
891 LoopAccessReport() <<
892 "loop control flow is not understood by analyzer");
896 // We only handle bottom-tested loops, i.e. loop in which the condition is
897 // checked at the end of each iteration. With that we can assume that all
898 // instructions in the loop are executed the same number of times.
899 if (TheLoop->getExitingBlock() != TheLoop->getLoopLatch()) {
901 LoopAccessReport() <<
902 "loop control flow is not understood by analyzer");
906 // We need to have a loop header.
907 DEBUG(dbgs() << "LAA: Found a loop: " <<
908 TheLoop->getHeader()->getName() << '\n');
910 // ScalarEvolution needs to be able to find the exit count.
911 const SCEV *ExitCount = SE->getBackedgeTakenCount(TheLoop);
912 if (ExitCount == SE->getCouldNotCompute()) {
913 emitAnalysis(LoopAccessReport() <<
914 "could not determine number of loop iterations");
915 DEBUG(dbgs() << "LAA: SCEV could not compute the loop exit count.\n");
922 void LoopAccessInfo::analyzeLoop(const ValueToValueMap &Strides) {
924 typedef SmallVector<Value*, 16> ValueVector;
925 typedef SmallPtrSet<Value*, 16> ValueSet;
927 // Holds the Load and Store *instructions*.
931 // Holds all the different accesses in the loop.
932 unsigned NumReads = 0;
933 unsigned NumReadWrites = 0;
935 PtrRtCheck.Pointers.clear();
936 PtrRtCheck.Need = false;
938 const bool IsAnnotatedParallel = TheLoop->isAnnotatedParallel();
939 MemoryDepChecker DepChecker(SE, DL, TheLoop);
942 for (Loop::block_iterator bb = TheLoop->block_begin(),
943 be = TheLoop->block_end(); bb != be; ++bb) {
945 // Scan the BB and collect legal loads and stores.
946 for (BasicBlock::iterator it = (*bb)->begin(), e = (*bb)->end(); it != e;
949 // If this is a load, save it. If this instruction can read from memory
950 // but is not a load, then we quit. Notice that we don't handle function
951 // calls that read or write.
952 if (it->mayReadFromMemory()) {
953 // Many math library functions read the rounding mode. We will only
954 // vectorize a loop if it contains known function calls that don't set
955 // the flag. Therefore, it is safe to ignore this read from memory.
956 CallInst *Call = dyn_cast<CallInst>(it);
957 if (Call && getIntrinsicIDForCall(Call, TLI))
960 LoadInst *Ld = dyn_cast<LoadInst>(it);
961 if (!Ld || (!Ld->isSimple() && !IsAnnotatedParallel)) {
962 emitAnalysis(LoopAccessReport(Ld)
963 << "read with atomic ordering or volatile read");
964 DEBUG(dbgs() << "LAA: Found a non-simple load.\n");
970 DepChecker.addAccess(Ld);
974 // Save 'store' instructions. Abort if other instructions write to memory.
975 if (it->mayWriteToMemory()) {
976 StoreInst *St = dyn_cast<StoreInst>(it);
978 emitAnalysis(LoopAccessReport(it) <<
979 "instruction cannot be vectorized");
983 if (!St->isSimple() && !IsAnnotatedParallel) {
984 emitAnalysis(LoopAccessReport(St)
985 << "write with atomic ordering or volatile write");
986 DEBUG(dbgs() << "LAA: Found a non-simple store.\n");
991 Stores.push_back(St);
992 DepChecker.addAccess(St);
997 // Now we have two lists that hold the loads and the stores.
998 // Next, we find the pointers that they use.
1000 // Check if we see any stores. If there are no stores, then we don't
1001 // care if the pointers are *restrict*.
1002 if (!Stores.size()) {
1003 DEBUG(dbgs() << "LAA: Found a read-only loop!\n");
1008 AccessAnalysis::DepCandidates DependentAccesses;
1009 AccessAnalysis Accesses(DL, AA, DependentAccesses);
1011 // Holds the analyzed pointers. We don't want to call GetUnderlyingObjects
1012 // multiple times on the same object. If the ptr is accessed twice, once
1013 // for read and once for write, it will only appear once (on the write
1014 // list). This is okay, since we are going to check for conflicts between
1015 // writes and between reads and writes, but not between reads and reads.
1018 ValueVector::iterator I, IE;
1019 for (I = Stores.begin(), IE = Stores.end(); I != IE; ++I) {
1020 StoreInst *ST = cast<StoreInst>(*I);
1021 Value* Ptr = ST->getPointerOperand();
1023 if (isUniform(Ptr)) {
1025 LoopAccessReport(ST)
1026 << "write to a loop invariant address could not be vectorized");
1027 DEBUG(dbgs() << "LAA: We don't allow storing to uniform addresses\n");
1032 // If we did *not* see this pointer before, insert it to the read-write
1033 // list. At this phase it is only a 'write' list.
1034 if (Seen.insert(Ptr).second) {
1037 AliasAnalysis::Location Loc = AA->getLocation(ST);
1038 // The TBAA metadata could have a control dependency on the predication
1039 // condition, so we cannot rely on it when determining whether or not we
1040 // need runtime pointer checks.
1041 if (blockNeedsPredication(ST->getParent(), TheLoop, DT))
1042 Loc.AATags.TBAA = nullptr;
1044 Accesses.addStore(Loc);
1048 if (IsAnnotatedParallel) {
1050 << "LAA: A loop annotated parallel, ignore memory dependency "
1056 for (I = Loads.begin(), IE = Loads.end(); I != IE; ++I) {
1057 LoadInst *LD = cast<LoadInst>(*I);
1058 Value* Ptr = LD->getPointerOperand();
1059 // If we did *not* see this pointer before, insert it to the
1060 // read list. If we *did* see it before, then it is already in
1061 // the read-write list. This allows us to vectorize expressions
1062 // such as A[i] += x; Because the address of A[i] is a read-write
1063 // pointer. This only works if the index of A[i] is consecutive.
1064 // If the address of i is unknown (for example A[B[i]]) then we may
1065 // read a few words, modify, and write a few words, and some of the
1066 // words may be written to the same address.
1067 bool IsReadOnlyPtr = false;
1068 if (Seen.insert(Ptr).second ||
1069 !isStridedPtr(SE, DL, Ptr, TheLoop, Strides)) {
1071 IsReadOnlyPtr = true;
1074 AliasAnalysis::Location Loc = AA->getLocation(LD);
1075 // The TBAA metadata could have a control dependency on the predication
1076 // condition, so we cannot rely on it when determining whether or not we
1077 // need runtime pointer checks.
1078 if (blockNeedsPredication(LD->getParent(), TheLoop, DT))
1079 Loc.AATags.TBAA = nullptr;
1081 Accesses.addLoad(Loc, IsReadOnlyPtr);
1084 // If we write (or read-write) to a single destination and there are no
1085 // other reads in this loop then is it safe to vectorize.
1086 if (NumReadWrites == 1 && NumReads == 0) {
1087 DEBUG(dbgs() << "LAA: Found a write-only loop!\n");
1092 // Build dependence sets and check whether we need a runtime pointer bounds
1094 Accesses.buildDependenceSets();
1095 bool NeedRTCheck = Accesses.isRTCheckNeeded();
1097 // Find pointers with computable bounds. We are going to use this information
1098 // to place a runtime bound check.
1099 unsigned NumComparisons = 0;
1100 bool CanDoRT = false;
1102 CanDoRT = Accesses.canCheckPtrAtRT(PtrRtCheck, NumComparisons, SE, TheLoop,
1105 DEBUG(dbgs() << "LAA: We need to do " << NumComparisons <<
1106 " pointer comparisons.\n");
1108 // If we only have one set of dependences to check pointers among we don't
1109 // need a runtime check.
1110 if (NumComparisons == 0 && NeedRTCheck)
1111 NeedRTCheck = false;
1113 // Check that we did not collect too many pointers or found an unsizeable
1116 NumComparisons > VectorizerParams::RuntimeMemoryCheckThreshold) {
1122 DEBUG(dbgs() << "LAA: We can perform a memory runtime check if needed.\n");
1125 if (NeedRTCheck && !CanDoRT) {
1126 emitAnalysis(LoopAccessReport() << "cannot identify array bounds");
1127 DEBUG(dbgs() << "LAA: We can't vectorize because we can't find " <<
1128 "the array bounds.\n");
1134 PtrRtCheck.Need = NeedRTCheck;
1137 if (Accesses.isDependencyCheckNeeded()) {
1138 DEBUG(dbgs() << "LAA: Checking memory dependencies\n");
1139 CanVecMem = DepChecker.areDepsSafe(
1140 DependentAccesses, Accesses.getDependenciesToCheck(), Strides);
1141 MaxSafeDepDistBytes = DepChecker.getMaxSafeDepDistBytes();
1143 if (!CanVecMem && DepChecker.shouldRetryWithRuntimeCheck()) {
1144 DEBUG(dbgs() << "LAA: Retrying with memory checks\n");
1147 // Clear the dependency checks. We assume they are not needed.
1148 Accesses.resetDepChecks();
1151 PtrRtCheck.Need = true;
1153 CanDoRT = Accesses.canCheckPtrAtRT(PtrRtCheck, NumComparisons, SE,
1154 TheLoop, Strides, true);
1155 // Check that we did not collect too many pointers or found an unsizeable
1158 NumComparisons > VectorizerParams::RuntimeMemoryCheckThreshold) {
1159 if (!CanDoRT && NumComparisons > 0)
1160 emitAnalysis(LoopAccessReport()
1161 << "cannot check memory dependencies at runtime");
1163 emitAnalysis(LoopAccessReport()
1164 << NumComparisons << " exceeds limit of "
1165 << VectorizerParams::RuntimeMemoryCheckThreshold
1166 << " dependent memory operations checked at runtime");
1167 DEBUG(dbgs() << "LAA: Can't vectorize with memory checks\n");
1178 emitAnalysis(LoopAccessReport() <<
1179 "unsafe dependent memory operations in loop");
1181 DEBUG(dbgs() << "LAA: We" << (NeedRTCheck ? "" : " don't") <<
1182 " need a runtime memory check.\n");
1185 bool LoopAccessInfo::blockNeedsPredication(BasicBlock *BB, Loop *TheLoop,
1186 DominatorTree *DT) {
1187 assert(TheLoop->contains(BB) && "Unknown block used");
1189 // Blocks that do not dominate the latch need predication.
1190 BasicBlock* Latch = TheLoop->getLoopLatch();
1191 return !DT->dominates(BB, Latch);
1194 void LoopAccessInfo::emitAnalysis(LoopAccessReport &Message) {
1195 assert(!Report && "Multiple reports generated");
1199 bool LoopAccessInfo::isUniform(Value *V) const {
1200 return (SE->isLoopInvariant(SE->getSCEV(V), TheLoop));
1203 // FIXME: this function is currently a duplicate of the one in
1204 // LoopVectorize.cpp.
1205 static Instruction *getFirstInst(Instruction *FirstInst, Value *V,
1209 if (Instruction *I = dyn_cast<Instruction>(V))
1210 return I->getParent() == Loc->getParent() ? I : nullptr;
1214 std::pair<Instruction *, Instruction *>
1215 LoopAccessInfo::addRuntimeCheck(Instruction *Loc) const {
1216 Instruction *tnullptr = nullptr;
1217 if (!PtrRtCheck.Need)
1218 return std::pair<Instruction *, Instruction *>(tnullptr, tnullptr);
1220 unsigned NumPointers = PtrRtCheck.Pointers.size();
1221 SmallVector<TrackingVH<Value> , 2> Starts;
1222 SmallVector<TrackingVH<Value> , 2> Ends;
1224 LLVMContext &Ctx = Loc->getContext();
1225 SCEVExpander Exp(*SE, "induction");
1226 Instruction *FirstInst = nullptr;
1228 for (unsigned i = 0; i < NumPointers; ++i) {
1229 Value *Ptr = PtrRtCheck.Pointers[i];
1230 const SCEV *Sc = SE->getSCEV(Ptr);
1232 if (SE->isLoopInvariant(Sc, TheLoop)) {
1233 DEBUG(dbgs() << "LAA: Adding RT check for a loop invariant ptr:" <<
1235 Starts.push_back(Ptr);
1236 Ends.push_back(Ptr);
1238 DEBUG(dbgs() << "LAA: Adding RT check for range:" << *Ptr << '\n');
1239 unsigned AS = Ptr->getType()->getPointerAddressSpace();
1241 // Use this type for pointer arithmetic.
1242 Type *PtrArithTy = Type::getInt8PtrTy(Ctx, AS);
1244 Value *Start = Exp.expandCodeFor(PtrRtCheck.Starts[i], PtrArithTy, Loc);
1245 Value *End = Exp.expandCodeFor(PtrRtCheck.Ends[i], PtrArithTy, Loc);
1246 Starts.push_back(Start);
1247 Ends.push_back(End);
1251 IRBuilder<> ChkBuilder(Loc);
1252 // Our instructions might fold to a constant.
1253 Value *MemoryRuntimeCheck = nullptr;
1254 for (unsigned i = 0; i < NumPointers; ++i) {
1255 for (unsigned j = i+1; j < NumPointers; ++j) {
1256 if (!PtrRtCheck.needsChecking(i, j))
1259 unsigned AS0 = Starts[i]->getType()->getPointerAddressSpace();
1260 unsigned AS1 = Starts[j]->getType()->getPointerAddressSpace();
1262 assert((AS0 == Ends[j]->getType()->getPointerAddressSpace()) &&
1263 (AS1 == Ends[i]->getType()->getPointerAddressSpace()) &&
1264 "Trying to bounds check pointers with different address spaces");
1266 Type *PtrArithTy0 = Type::getInt8PtrTy(Ctx, AS0);
1267 Type *PtrArithTy1 = Type::getInt8PtrTy(Ctx, AS1);
1269 Value *Start0 = ChkBuilder.CreateBitCast(Starts[i], PtrArithTy0, "bc");
1270 Value *Start1 = ChkBuilder.CreateBitCast(Starts[j], PtrArithTy1, "bc");
1271 Value *End0 = ChkBuilder.CreateBitCast(Ends[i], PtrArithTy1, "bc");
1272 Value *End1 = ChkBuilder.CreateBitCast(Ends[j], PtrArithTy0, "bc");
1274 Value *Cmp0 = ChkBuilder.CreateICmpULE(Start0, End1, "bound0");
1275 FirstInst = getFirstInst(FirstInst, Cmp0, Loc);
1276 Value *Cmp1 = ChkBuilder.CreateICmpULE(Start1, End0, "bound1");
1277 FirstInst = getFirstInst(FirstInst, Cmp1, Loc);
1278 Value *IsConflict = ChkBuilder.CreateAnd(Cmp0, Cmp1, "found.conflict");
1279 FirstInst = getFirstInst(FirstInst, IsConflict, Loc);
1280 if (MemoryRuntimeCheck) {
1281 IsConflict = ChkBuilder.CreateOr(MemoryRuntimeCheck, IsConflict,
1283 FirstInst = getFirstInst(FirstInst, IsConflict, Loc);
1285 MemoryRuntimeCheck = IsConflict;
1289 // We have to do this trickery because the IRBuilder might fold the check to a
1290 // constant expression in which case there is no Instruction anchored in a
1292 Instruction *Check = BinaryOperator::CreateAnd(MemoryRuntimeCheck,
1293 ConstantInt::getTrue(Ctx));
1294 ChkBuilder.Insert(Check, "memcheck.conflict");
1295 FirstInst = getFirstInst(FirstInst, Check, Loc);
1296 return std::make_pair(FirstInst, Check);
1299 LoopAccessInfo::LoopAccessInfo(Loop *L, ScalarEvolution *SE,
1300 const DataLayout *DL,
1301 const TargetLibraryInfo *TLI, AliasAnalysis *AA,
1303 const ValueToValueMap &Strides)
1304 : TheLoop(L), SE(SE), DL(DL), TLI(TLI), AA(AA), DT(DT), NumLoads(0),
1305 NumStores(0), MaxSafeDepDistBytes(-1U), CanVecMem(false) {
1306 if (canAnalyzeLoop())
1307 analyzeLoop(Strides);
1310 void LoopAccessInfo::print(raw_ostream &OS, unsigned Depth) const {
1312 if (PtrRtCheck.empty())
1313 OS.indent(Depth) << "Memory dependences are safe\n";
1315 OS.indent(Depth) << "Memory dependences are safe with run-time checks\n";
1319 OS.indent(Depth) << "Report: " << Report->str() << "\n";
1321 // FIXME: Print unsafe dependences
1323 // List the pair of accesses need run-time checks to prove independence.
1324 PtrRtCheck.print(OS, Depth);
1328 const LoopAccessInfo &
1329 LoopAccessAnalysis::getInfo(Loop *L, const ValueToValueMap &Strides) {
1330 auto &LAI = LoopAccessInfoMap[L];
1333 assert((!LAI || LAI->NumSymbolicStrides == Strides.size()) &&
1334 "Symbolic strides changed for loop");
1338 LAI = llvm::make_unique<LoopAccessInfo>(L, SE, DL, TLI, AA, DT, Strides);
1340 LAI->NumSymbolicStrides = Strides.size();
1346 void LoopAccessAnalysis::print(raw_ostream &OS, const Module *M) const {
1347 LoopAccessAnalysis &LAA = *const_cast<LoopAccessAnalysis *>(this);
1349 LoopInfo *LI = &getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
1350 ValueToValueMap NoSymbolicStrides;
1352 for (Loop *TopLevelLoop : *LI)
1353 for (Loop *L : depth_first(TopLevelLoop)) {
1354 OS.indent(2) << L->getHeader()->getName() << ":\n";
1355 auto &LAI = LAA.getInfo(L, NoSymbolicStrides);
1360 bool LoopAccessAnalysis::runOnFunction(Function &F) {
1361 SE = &getAnalysis<ScalarEvolution>();
1362 DL = F.getParent()->getDataLayout();
1363 auto *TLIP = getAnalysisIfAvailable<TargetLibraryInfoWrapperPass>();
1364 TLI = TLIP ? &TLIP->getTLI() : nullptr;
1365 AA = &getAnalysis<AliasAnalysis>();
1366 DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree();
1371 void LoopAccessAnalysis::getAnalysisUsage(AnalysisUsage &AU) const {
1372 AU.addRequired<ScalarEvolution>();
1373 AU.addRequired<AliasAnalysis>();
1374 AU.addRequired<DominatorTreeWrapperPass>();
1375 AU.addRequired<LoopInfoWrapperPass>();
1377 AU.setPreservesAll();
1380 char LoopAccessAnalysis::ID = 0;
1381 static const char laa_name[] = "Loop Access Analysis";
1382 #define LAA_NAME "loop-accesses"
1384 INITIALIZE_PASS_BEGIN(LoopAccessAnalysis, LAA_NAME, laa_name, false, true)
1385 INITIALIZE_AG_DEPENDENCY(AliasAnalysis)
1386 INITIALIZE_PASS_DEPENDENCY(ScalarEvolution)
1387 INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
1388 INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass)
1389 INITIALIZE_PASS_END(LoopAccessAnalysis, LAA_NAME, laa_name, false, true)
1392 Pass *createLAAPass() {
1393 return new LoopAccessAnalysis();