1 //===- llvm/Analysis/LoopAccessAnalysis.h -----------------------*- C++ -*-===//
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 // This file defines the interface for the loop memory dependence framework that
11 // was originally developed for the Loop Vectorizer.
13 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_ANALYSIS_LOOPACCESSANALYSIS_H
16 #define LLVM_ANALYSIS_LOOPACCESSANALYSIS_H
18 #include "llvm/ADT/EquivalenceClasses.h"
19 #include "llvm/ADT/Optional.h"
20 #include "llvm/ADT/SetVector.h"
21 #include "llvm/Analysis/AliasAnalysis.h"
22 #include "llvm/Analysis/AliasSetTracker.h"
23 #include "llvm/Analysis/LoopAnalysisManager.h"
24 #include "llvm/Analysis/ScalarEvolutionExpressions.h"
25 #include "llvm/IR/DiagnosticInfo.h"
26 #include "llvm/IR/ValueHandle.h"
27 #include "llvm/Pass.h"
28 #include "llvm/Support/raw_ostream.h"
34 class ScalarEvolution;
37 class SCEVUnionPredicate;
39 class OptimizationRemarkEmitter;
41 /// \brief Collection of parameters shared beetween the Loop Vectorizer and the
42 /// Loop Access Analysis.
43 struct VectorizerParams {
44 /// \brief Maximum SIMD width.
45 static const unsigned MaxVectorWidth;
47 /// \brief VF as overridden by the user.
48 static unsigned VectorizationFactor;
49 /// \brief Interleave factor as overridden by the user.
50 static unsigned VectorizationInterleave;
51 /// \brief True if force-vector-interleave was specified by the user.
52 static bool isInterleaveForced();
54 /// \\brief When performing memory disambiguation checks at runtime do not
55 /// make more than this number of comparisons.
56 static unsigned RuntimeMemoryCheckThreshold;
59 /// \brief Checks memory dependences among accesses to the same underlying
60 /// object to determine whether there vectorization is legal or not (and at
61 /// which vectorization factor).
63 /// Note: This class will compute a conservative dependence for access to
64 /// different underlying pointers. Clients, such as the loop vectorizer, will
65 /// sometimes deal these potential dependencies by emitting runtime checks.
67 /// We use the ScalarEvolution framework to symbolically evalutate access
68 /// functions pairs. Since we currently don't restructure the loop we can rely
69 /// on the program order of memory accesses to determine their safety.
70 /// At the moment we will only deem accesses as safe for:
71 /// * A negative constant distance assuming program order.
73 /// Safe: tmp = a[i + 1]; OR a[i + 1] = x;
74 /// a[i] = tmp; y = a[i];
76 /// The latter case is safe because later checks guarantuee that there can't
77 /// be a cycle through a phi node (that is, we check that "x" and "y" is not
78 /// the same variable: a header phi can only be an induction or a reduction, a
79 /// reduction can't have a memory sink, an induction can't have a memory
80 /// source). This is important and must not be violated (or we have to
81 /// resort to checking for cycles through memory).
83 /// * A positive constant distance assuming program order that is bigger
84 /// than the biggest memory access.
86 /// tmp = a[i] OR b[i] = x
87 /// a[i+2] = tmp y = b[i+2];
89 /// Safe distance: 2 x sizeof(a[0]), and 2 x sizeof(b[0]), respectively.
91 /// * Zero distances and all accesses have the same size.
93 class MemoryDepChecker {
95 typedef PointerIntPair<Value *, 1, bool> MemAccessInfo;
96 typedef SmallVector<MemAccessInfo, 8> MemAccessInfoList;
97 /// \brief Set of potential dependent memory accesses.
98 typedef EquivalenceClasses<MemAccessInfo> DepCandidates;
100 /// \brief Dependece between memory access instructions.
102 /// \brief The type of the dependence.
106 // We couldn't determine the direction or the distance.
108 // Lexically forward.
110 // FIXME: If we only have loop-independent forward dependences (e.g. a
111 // read and write of A[i]), LAA will locally deem the dependence "safe"
112 // without querying the MemoryDepChecker. Therefore we can miss
113 // enumerating loop-independent forward dependences in
114 // getDependences. Note that as soon as there are different
115 // indices used to access the same array, the MemoryDepChecker *is*
116 // queried and the dependence list is complete.
118 // Forward, but if vectorized, is likely to prevent store-to-load
120 ForwardButPreventsForwarding,
121 // Lexically backward.
123 // Backward, but the distance allows a vectorization factor of
124 // MaxSafeDepDistBytes.
125 BackwardVectorizable,
126 // Same, but may prevent store-to-load forwarding.
127 BackwardVectorizableButPreventsForwarding
130 /// \brief String version of the types.
131 static const char *DepName[];
133 /// \brief Index of the source of the dependence in the InstMap vector.
135 /// \brief Index of the destination of the dependence in the InstMap vector.
136 unsigned Destination;
137 /// \brief The type of the dependence.
140 Dependence(unsigned Source, unsigned Destination, DepType Type)
141 : Source(Source), Destination(Destination), Type(Type) {}
143 /// \brief Return the source instruction of the dependence.
144 Instruction *getSource(const LoopAccessInfo &LAI) const;
145 /// \brief Return the destination instruction of the dependence.
146 Instruction *getDestination(const LoopAccessInfo &LAI) const;
148 /// \brief Dependence types that don't prevent vectorization.
149 static bool isSafeForVectorization(DepType Type);
151 /// \brief Lexically forward dependence.
152 bool isForward() const;
153 /// \brief Lexically backward dependence.
154 bool isBackward() const;
156 /// \brief May be a lexically backward dependence type (includes Unknown).
157 bool isPossiblyBackward() const;
159 /// \brief Print the dependence. \p Instr is used to map the instruction
160 /// indices to instructions.
161 void print(raw_ostream &OS, unsigned Depth,
162 const SmallVectorImpl<Instruction *> &Instrs) const;
165 MemoryDepChecker(PredicatedScalarEvolution &PSE, const Loop *L)
166 : PSE(PSE), InnermostLoop(L), AccessIdx(0),
167 ShouldRetryWithRuntimeCheck(false), SafeForVectorization(true),
168 RecordDependences(true) {}
170 /// \brief Register the location (instructions are given increasing numbers)
171 /// of a write access.
172 void addAccess(StoreInst *SI) {
173 Value *Ptr = SI->getPointerOperand();
174 Accesses[MemAccessInfo(Ptr, true)].push_back(AccessIdx);
175 InstMap.push_back(SI);
179 /// \brief Register the location (instructions are given increasing numbers)
180 /// of a write access.
181 void addAccess(LoadInst *LI) {
182 Value *Ptr = LI->getPointerOperand();
183 Accesses[MemAccessInfo(Ptr, false)].push_back(AccessIdx);
184 InstMap.push_back(LI);
188 /// \brief Check whether the dependencies between the accesses are safe.
190 /// Only checks sets with elements in \p CheckDeps.
191 bool areDepsSafe(DepCandidates &AccessSets, MemAccessInfoList &CheckDeps,
192 const ValueToValueMap &Strides);
194 /// \brief No memory dependence was encountered that would inhibit
196 bool isSafeForVectorization() const { return SafeForVectorization; }
198 /// \brief The maximum number of bytes of a vector register we can vectorize
199 /// the accesses safely with.
200 uint64_t getMaxSafeDepDistBytes() { return MaxSafeDepDistBytes; }
202 /// \brief In same cases when the dependency check fails we can still
203 /// vectorize the loop with a dynamic array access check.
204 bool shouldRetryWithRuntimeCheck() { return ShouldRetryWithRuntimeCheck; }
206 /// \brief Returns the memory dependences. If null is returned we exceeded
207 /// the MaxDependences threshold and this information is not
209 const SmallVectorImpl<Dependence> *getDependences() const {
210 return RecordDependences ? &Dependences : nullptr;
213 void clearDependences() { Dependences.clear(); }
215 /// \brief The vector of memory access instructions. The indices are used as
216 /// instruction identifiers in the Dependence class.
217 const SmallVectorImpl<Instruction *> &getMemoryInstructions() const {
221 /// \brief Generate a mapping between the memory instructions and their
222 /// indices according to program order.
223 DenseMap<Instruction *, unsigned> generateInstructionOrderMap() const {
224 DenseMap<Instruction *, unsigned> OrderMap;
226 for (unsigned I = 0; I < InstMap.size(); ++I)
227 OrderMap[InstMap[I]] = I;
232 /// \brief Find the set of instructions that read or write via \p Ptr.
233 SmallVector<Instruction *, 4> getInstructionsForAccess(Value *Ptr,
237 /// A wrapper around ScalarEvolution, used to add runtime SCEV checks, and
238 /// applies dynamic knowledge to simplify SCEV expressions and convert them
239 /// to a more usable form. We need this in case assumptions about SCEV
240 /// expressions need to be made in order to avoid unknown dependences. For
241 /// example we might assume a unit stride for a pointer in order to prove
242 /// that a memory access is strided and doesn't wrap.
243 PredicatedScalarEvolution &PSE;
244 const Loop *InnermostLoop;
246 /// \brief Maps access locations (ptr, read/write) to program order.
247 DenseMap<MemAccessInfo, std::vector<unsigned> > Accesses;
249 /// \brief Memory access instructions in program order.
250 SmallVector<Instruction *, 16> InstMap;
252 /// \brief The program order index to be used for the next instruction.
255 // We can access this many bytes in parallel safely.
256 uint64_t MaxSafeDepDistBytes;
258 /// \brief If we see a non-constant dependence distance we can still try to
259 /// vectorize this loop with runtime checks.
260 bool ShouldRetryWithRuntimeCheck;
262 /// \brief No memory dependence was encountered that would inhibit
264 bool SafeForVectorization;
266 //// \brief True if Dependences reflects the dependences in the
267 //// loop. If false we exceeded MaxDependences and
268 //// Dependences is invalid.
269 bool RecordDependences;
271 /// \brief Memory dependences collected during the analysis. Only valid if
272 /// RecordDependences is true.
273 SmallVector<Dependence, 8> Dependences;
275 /// \brief Check whether there is a plausible dependence between the two
278 /// Access \p A must happen before \p B in program order. The two indices
279 /// identify the index into the program order map.
281 /// This function checks whether there is a plausible dependence (or the
282 /// absence of such can't be proved) between the two accesses. If there is a
283 /// plausible dependence but the dependence distance is bigger than one
284 /// element access it records this distance in \p MaxSafeDepDistBytes (if this
285 /// distance is smaller than any other distance encountered so far).
286 /// Otherwise, this function returns true signaling a possible dependence.
287 Dependence::DepType isDependent(const MemAccessInfo &A, unsigned AIdx,
288 const MemAccessInfo &B, unsigned BIdx,
289 const ValueToValueMap &Strides);
291 /// \brief Check whether the data dependence could prevent store-load
294 /// \return false if we shouldn't vectorize at all or avoid larger
295 /// vectorization factors by limiting MaxSafeDepDistBytes.
296 bool couldPreventStoreLoadForward(uint64_t Distance, uint64_t TypeByteSize);
299 /// \brief Holds information about the memory runtime legality checks to verify
300 /// that a group of pointers do not overlap.
301 class RuntimePointerChecking {
304 /// Holds the pointer value that we need to check.
305 TrackingVH<Value> PointerValue;
306 /// Holds the smallest byte address accessed by the pointer throughout all
307 /// iterations of the loop.
309 /// Holds the largest byte address accessed by the pointer throughout all
310 /// iterations of the loop, plus 1.
312 /// Holds the information if this pointer is used for writing to memory.
314 /// Holds the id of the set of pointers that could be dependent because of a
315 /// shared underlying object.
316 unsigned DependencySetId;
317 /// Holds the id of the disjoint alias set to which this pointer belongs.
319 /// SCEV for the access.
322 PointerInfo(Value *PointerValue, const SCEV *Start, const SCEV *End,
323 bool IsWritePtr, unsigned DependencySetId, unsigned AliasSetId,
325 : PointerValue(PointerValue), Start(Start), End(End),
326 IsWritePtr(IsWritePtr), DependencySetId(DependencySetId),
327 AliasSetId(AliasSetId), Expr(Expr) {}
330 RuntimePointerChecking(ScalarEvolution *SE) : Need(false), SE(SE) {}
332 /// Reset the state of the pointer runtime information.
339 /// Insert a pointer and calculate the start and end SCEVs.
340 /// We need \p PSE in order to compute the SCEV expression of the pointer
341 /// according to the assumptions that we've made during the analysis.
342 /// The method might also version the pointer stride according to \p Strides,
343 /// and add new predicates to \p PSE.
344 void insert(Loop *Lp, Value *Ptr, bool WritePtr, unsigned DepSetId,
345 unsigned ASId, const ValueToValueMap &Strides,
346 PredicatedScalarEvolution &PSE);
348 /// \brief No run-time memory checking is necessary.
349 bool empty() const { return Pointers.empty(); }
351 /// A grouping of pointers. A single memcheck is required between
353 struct CheckingPtrGroup {
354 /// \brief Create a new pointer checking group containing a single
355 /// pointer, with index \p Index in RtCheck.
356 CheckingPtrGroup(unsigned Index, RuntimePointerChecking &RtCheck)
357 : RtCheck(RtCheck), High(RtCheck.Pointers[Index].End),
358 Low(RtCheck.Pointers[Index].Start) {
359 Members.push_back(Index);
362 /// \brief Tries to add the pointer recorded in RtCheck at index
363 /// \p Index to this pointer checking group. We can only add a pointer
364 /// to a checking group if we will still be able to get
365 /// the upper and lower bounds of the check. Returns true in case
366 /// of success, false otherwise.
367 bool addPointer(unsigned Index);
369 /// Constitutes the context of this pointer checking group. For each
370 /// pointer that is a member of this group we will retain the index
371 /// at which it appears in RtCheck.
372 RuntimePointerChecking &RtCheck;
373 /// The SCEV expression which represents the upper bound of all the
374 /// pointers in this group.
376 /// The SCEV expression which represents the lower bound of all the
377 /// pointers in this group.
379 /// Indices of all the pointers that constitute this grouping.
380 SmallVector<unsigned, 2> Members;
383 /// \brief A memcheck which made up of a pair of grouped pointers.
385 /// These *have* to be const for now, since checks are generated from
386 /// CheckingPtrGroups in LAI::addRuntimeChecks which is a const member
387 /// function. FIXME: once check-generation is moved inside this class (after
388 /// the PtrPartition hack is removed), we could drop const.
389 typedef std::pair<const CheckingPtrGroup *, const CheckingPtrGroup *>
392 /// \brief Generate the checks and store it. This also performs the grouping
393 /// of pointers to reduce the number of memchecks necessary.
394 void generateChecks(MemoryDepChecker::DepCandidates &DepCands,
395 bool UseDependencies);
397 /// \brief Returns the checks that generateChecks created.
398 const SmallVector<PointerCheck, 4> &getChecks() const { return Checks; }
400 /// \brief Decide if we need to add a check between two groups of pointers,
401 /// according to needsChecking.
402 bool needsChecking(const CheckingPtrGroup &M,
403 const CheckingPtrGroup &N) const;
405 /// \brief Returns the number of run-time checks required according to
407 unsigned getNumberOfChecks() const { return Checks.size(); }
409 /// \brief Print the list run-time memory checks necessary.
410 void print(raw_ostream &OS, unsigned Depth = 0) const;
413 void printChecks(raw_ostream &OS, const SmallVectorImpl<PointerCheck> &Checks,
414 unsigned Depth = 0) const;
416 /// This flag indicates if we need to add the runtime check.
419 /// Information about the pointers that may require checking.
420 SmallVector<PointerInfo, 2> Pointers;
422 /// Holds a partitioning of pointers into "check groups".
423 SmallVector<CheckingPtrGroup, 2> CheckingGroups;
425 /// \brief Check if pointers are in the same partition
427 /// \p PtrToPartition contains the partition number for pointers (-1 if the
428 /// pointer belongs to multiple partitions).
430 arePointersInSamePartition(const SmallVectorImpl<int> &PtrToPartition,
431 unsigned PtrIdx1, unsigned PtrIdx2);
433 /// \brief Decide whether we need to issue a run-time check for pointer at
434 /// index \p I and \p J to prove their independence.
435 bool needsChecking(unsigned I, unsigned J) const;
437 /// \brief Return PointerInfo for pointer at index \p PtrIdx.
438 const PointerInfo &getPointerInfo(unsigned PtrIdx) const {
439 return Pointers[PtrIdx];
443 /// \brief Groups pointers such that a single memcheck is required
444 /// between two different groups. This will clear the CheckingGroups vector
445 /// and re-compute it. We will only group dependecies if \p UseDependencies
446 /// is true, otherwise we will create a separate group for each pointer.
447 void groupChecks(MemoryDepChecker::DepCandidates &DepCands,
448 bool UseDependencies);
450 /// Generate the checks and return them.
451 SmallVector<PointerCheck, 4>
452 generateChecks() const;
454 /// Holds a pointer to the ScalarEvolution analysis.
457 /// \brief Set of run-time checks required to establish independence of
458 /// otherwise may-aliasing pointers in the loop.
459 SmallVector<PointerCheck, 4> Checks;
462 /// \brief Drive the analysis of memory accesses in the loop
464 /// This class is responsible for analyzing the memory accesses of a loop. It
465 /// collects the accesses and then its main helper the AccessAnalysis class
466 /// finds and categorizes the dependences in buildDependenceSets.
468 /// For memory dependences that can be analyzed at compile time, it determines
469 /// whether the dependence is part of cycle inhibiting vectorization. This work
470 /// is delegated to the MemoryDepChecker class.
472 /// For memory dependences that cannot be determined at compile time, it
473 /// generates run-time checks to prove independence. This is done by
474 /// AccessAnalysis::canCheckPtrAtRT and the checks are maintained by the
475 /// RuntimePointerCheck class.
477 /// If pointers can wrap or can't be expressed as affine AddRec expressions by
478 /// ScalarEvolution, we will generate run-time checks by emitting a
479 /// SCEVUnionPredicate.
481 /// Checks for both memory dependences and the SCEV predicates contained in the
482 /// PSE must be emitted in order for the results of this analysis to be valid.
483 class LoopAccessInfo {
485 LoopAccessInfo(Loop *L, ScalarEvolution *SE, const TargetLibraryInfo *TLI,
486 AliasAnalysis *AA, DominatorTree *DT, LoopInfo *LI);
488 /// Return true we can analyze the memory accesses in the loop and there are
489 /// no memory dependence cycles.
490 bool canVectorizeMemory() const { return CanVecMem; }
492 const RuntimePointerChecking *getRuntimePointerChecking() const {
493 return PtrRtChecking.get();
496 /// \brief Number of memchecks required to prove independence of otherwise
497 /// may-alias pointers.
498 unsigned getNumRuntimePointerChecks() const {
499 return PtrRtChecking->getNumberOfChecks();
502 /// Return true if the block BB needs to be predicated in order for the loop
503 /// to be vectorized.
504 static bool blockNeedsPredication(BasicBlock *BB, Loop *TheLoop,
507 /// Returns true if the value V is uniform within the loop.
508 bool isUniform(Value *V) const;
510 uint64_t getMaxSafeDepDistBytes() const { return MaxSafeDepDistBytes; }
511 unsigned getNumStores() const { return NumStores; }
512 unsigned getNumLoads() const { return NumLoads;}
514 /// \brief Add code that checks at runtime if the accessed arrays overlap.
516 /// Returns a pair of instructions where the first element is the first
517 /// instruction generated in possibly a sequence of instructions and the
518 /// second value is the final comparator value or NULL if no check is needed.
519 std::pair<Instruction *, Instruction *>
520 addRuntimeChecks(Instruction *Loc) const;
522 /// \brief Generete the instructions for the checks in \p PointerChecks.
524 /// Returns a pair of instructions where the first element is the first
525 /// instruction generated in possibly a sequence of instructions and the
526 /// second value is the final comparator value or NULL if no check is needed.
527 std::pair<Instruction *, Instruction *>
528 addRuntimeChecks(Instruction *Loc,
529 const SmallVectorImpl<RuntimePointerChecking::PointerCheck>
530 &PointerChecks) const;
532 /// \brief The diagnostics report generated for the analysis. E.g. why we
533 /// couldn't analyze the loop.
534 const OptimizationRemarkAnalysis *getReport() const { return Report.get(); }
536 /// \brief the Memory Dependence Checker which can determine the
537 /// loop-independent and loop-carried dependences between memory accesses.
538 const MemoryDepChecker &getDepChecker() const { return *DepChecker; }
540 /// \brief Return the list of instructions that use \p Ptr to read or write
542 SmallVector<Instruction *, 4> getInstructionsForAccess(Value *Ptr,
543 bool isWrite) const {
544 return DepChecker->getInstructionsForAccess(Ptr, isWrite);
547 /// \brief If an access has a symbolic strides, this maps the pointer value to
548 /// the stride symbol.
549 const ValueToValueMap &getSymbolicStrides() const { return SymbolicStrides; }
551 /// \brief Pointer has a symbolic stride.
552 bool hasStride(Value *V) const { return StrideSet.count(V); }
554 /// \brief Print the information about the memory accesses in the loop.
555 void print(raw_ostream &OS, unsigned Depth = 0) const;
557 /// \brief Checks existence of store to invariant address inside loop.
558 /// If the loop has any store to invariant address, then it returns true,
559 /// else returns false.
560 bool hasStoreToLoopInvariantAddress() const {
561 return StoreToLoopInvariantAddress;
564 /// Used to add runtime SCEV checks. Simplifies SCEV expressions and converts
565 /// them to a more usable form. All SCEV expressions during the analysis
566 /// should be re-written (and therefore simplified) according to PSE.
567 /// A user of LoopAccessAnalysis will need to emit the runtime checks
568 /// associated with this predicate.
569 const PredicatedScalarEvolution &getPSE() const { return *PSE; }
572 /// \brief Analyze the loop.
573 void analyzeLoop(AliasAnalysis *AA, LoopInfo *LI,
574 const TargetLibraryInfo *TLI, DominatorTree *DT);
576 /// \brief Check if the structure of the loop allows it to be analyzed by this
578 bool canAnalyzeLoop();
580 /// \brief Save the analysis remark.
582 /// LAA does not directly emits the remarks. Instead it stores it which the
583 /// client can retrieve and presents as its own analysis
584 /// (e.g. -Rpass-analysis=loop-vectorize).
585 OptimizationRemarkAnalysis &recordAnalysis(StringRef RemarkName,
586 Instruction *Instr = nullptr);
588 /// \brief Collect memory access with loop invariant strides.
590 /// Looks for accesses like "a[i * StrideA]" where "StrideA" is loop
592 void collectStridedAccess(Value *LoadOrStoreInst);
594 std::unique_ptr<PredicatedScalarEvolution> PSE;
596 /// We need to check that all of the pointers in this list are disjoint
597 /// at runtime. Using std::unique_ptr to make using move ctor simpler.
598 std::unique_ptr<RuntimePointerChecking> PtrRtChecking;
600 /// \brief the Memory Dependence Checker which can determine the
601 /// loop-independent and loop-carried dependences between memory accesses.
602 std::unique_ptr<MemoryDepChecker> DepChecker;
609 uint64_t MaxSafeDepDistBytes;
611 /// \brief Cache the result of analyzeLoop.
614 /// \brief Indicator for storing to uniform addresses.
615 /// If a loop has write to a loop invariant address then it should be true.
616 bool StoreToLoopInvariantAddress;
618 /// \brief The diagnostics report generated for the analysis. E.g. why we
619 /// couldn't analyze the loop.
620 std::unique_ptr<OptimizationRemarkAnalysis> Report;
622 /// \brief If an access has a symbolic strides, this maps the pointer value to
623 /// the stride symbol.
624 ValueToValueMap SymbolicStrides;
626 /// \brief Set of symbolic strides values.
627 SmallPtrSet<Value *, 8> StrideSet;
630 Value *stripIntegerCast(Value *V);
632 /// \brief Return the SCEV corresponding to a pointer with the symbolic stride
633 /// replaced with constant one, assuming the SCEV predicate associated with
636 /// If necessary this method will version the stride of the pointer according
637 /// to \p PtrToStride and therefore add further predicates to \p PSE.
639 /// If \p OrigPtr is not null, use it to look up the stride value instead of \p
640 /// Ptr. \p PtrToStride provides the mapping between the pointer value and its
641 /// stride as collected by LoopVectorizationLegality::collectStridedAccess.
642 const SCEV *replaceSymbolicStrideSCEV(PredicatedScalarEvolution &PSE,
643 const ValueToValueMap &PtrToStride,
644 Value *Ptr, Value *OrigPtr = nullptr);
646 /// \brief If the pointer has a constant stride return it in units of its
647 /// element size. Otherwise return zero.
649 /// Ensure that it does not wrap in the address space, assuming the predicate
650 /// associated with \p PSE is true.
652 /// If necessary this method will version the stride of the pointer according
653 /// to \p PtrToStride and therefore add further predicates to \p PSE.
654 /// The \p Assume parameter indicates if we are allowed to make additional
655 /// run-time assumptions.
656 int64_t getPtrStride(PredicatedScalarEvolution &PSE, Value *Ptr, const Loop *Lp,
657 const ValueToValueMap &StridesMap = ValueToValueMap(),
658 bool Assume = false, bool ShouldCheckWrap = true);
660 /// \brief Returns true if the memory operations \p A and \p B are consecutive.
661 /// This is a simple API that does not depend on the analysis pass.
662 bool isConsecutiveAccess(Value *A, Value *B, const DataLayout &DL,
663 ScalarEvolution &SE, bool CheckType = true);
665 /// \brief This analysis provides dependence information for the memory accesses
668 /// It runs the analysis for a loop on demand. This can be initiated by
669 /// querying the loop access info via LAA::getInfo. getInfo return a
670 /// LoopAccessInfo object. See this class for the specifics of what information
672 class LoopAccessLegacyAnalysis : public FunctionPass {
676 LoopAccessLegacyAnalysis() : FunctionPass(ID) {
677 initializeLoopAccessLegacyAnalysisPass(*PassRegistry::getPassRegistry());
680 bool runOnFunction(Function &F) override;
682 void getAnalysisUsage(AnalysisUsage &AU) const override;
684 /// \brief Query the result of the loop access information for the loop \p L.
686 /// If there is no cached result available run the analysis.
687 const LoopAccessInfo &getInfo(Loop *L);
689 void releaseMemory() override {
690 // Invalidate the cache when the pass is freed.
691 LoopAccessInfoMap.clear();
694 /// \brief Print the result of the analysis when invoked with -analyze.
695 void print(raw_ostream &OS, const Module *M = nullptr) const override;
698 /// \brief The cache.
699 DenseMap<Loop *, std::unique_ptr<LoopAccessInfo>> LoopAccessInfoMap;
701 // The used analysis passes.
703 const TargetLibraryInfo *TLI;
709 /// \brief This analysis provides dependence information for the memory
710 /// accesses of a loop.
712 /// It runs the analysis for a loop on demand. This can be initiated by
713 /// querying the loop access info via AM.getResult<LoopAccessAnalysis>.
714 /// getResult return a LoopAccessInfo object. See this class for the
715 /// specifics of what information is provided.
716 class LoopAccessAnalysis
717 : public AnalysisInfoMixin<LoopAccessAnalysis> {
718 friend AnalysisInfoMixin<LoopAccessAnalysis>;
719 static AnalysisKey Key;
722 typedef LoopAccessInfo Result;
724 Result run(Loop &L, LoopAnalysisManager &AM, LoopStandardAnalysisResults &AR);
727 inline Instruction *MemoryDepChecker::Dependence::getSource(
728 const LoopAccessInfo &LAI) const {
729 return LAI.getDepChecker().getMemoryInstructions()[Source];
732 inline Instruction *MemoryDepChecker::Dependence::getDestination(
733 const LoopAccessInfo &LAI) const {
734 return LAI.getDepChecker().getMemoryInstructions()[Destination];
737 } // End llvm namespace