Add missing #include, found by modules build.
[oota-llvm.git] / include / llvm / Analysis / LoopAccessAnalysis.h
1 //===- llvm/Analysis/LoopAccessAnalysis.h -----------------------*- C++ -*-===//
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 // This file defines the interface for the loop memory dependence framework that
11 // was originally developed for the Loop Vectorizer.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #ifndef LLVM_ANALYSIS_LOOPACCESSANALYSIS_H
16 #define LLVM_ANALYSIS_LOOPACCESSANALYSIS_H
17
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/ScalarEvolutionExpressions.h"
24 #include "llvm/IR/ValueHandle.h"
25 #include "llvm/Pass.h"
26 #include "llvm/Support/raw_ostream.h"
27
28 namespace llvm {
29
30 class Value;
31 class DataLayout;
32 class ScalarEvolution;
33 class Loop;
34 class SCEV;
35
36 /// Optimization analysis message produced during vectorization. Messages inform
37 /// the user why vectorization did not occur.
38 class LoopAccessReport {
39   std::string Message;
40   const Instruction *Instr;
41
42 protected:
43   LoopAccessReport(const Twine &Message, const Instruction *I)
44       : Message(Message.str()), Instr(I) {}
45
46 public:
47   LoopAccessReport(const Instruction *I = nullptr) : Instr(I) {}
48
49   template <typename A> LoopAccessReport &operator<<(const A &Value) {
50     raw_string_ostream Out(Message);
51     Out << Value;
52     return *this;
53   }
54
55   const Instruction *getInstr() const { return Instr; }
56
57   std::string &str() { return Message; }
58   const std::string &str() const { return Message; }
59   operator Twine() { return Message; }
60
61   /// \brief Emit an analysis note for \p PassName with the debug location from
62   /// the instruction in \p Message if available.  Otherwise use the location of
63   /// \p TheLoop.
64   static void emitAnalysis(const LoopAccessReport &Message,
65                            const Function *TheFunction,
66                            const Loop *TheLoop,
67                            const char *PassName);
68 };
69
70 /// \brief Collection of parameters shared beetween the Loop Vectorizer and the
71 /// Loop Access Analysis.
72 struct VectorizerParams {
73   /// \brief Maximum SIMD width.
74   static const unsigned MaxVectorWidth;
75
76   /// \brief VF as overridden by the user.
77   static unsigned VectorizationFactor;
78   /// \brief Interleave factor as overridden by the user.
79   static unsigned VectorizationInterleave;
80   /// \brief True if force-vector-interleave was specified by the user.
81   static bool isInterleaveForced();
82
83   /// \\brief When performing memory disambiguation checks at runtime do not
84   /// make more than this number of comparisons.
85   static unsigned RuntimeMemoryCheckThreshold;
86 };
87
88 /// \brief Checks memory dependences among accesses to the same underlying
89 /// object to determine whether there vectorization is legal or not (and at
90 /// which vectorization factor).
91 ///
92 /// Note: This class will compute a conservative dependence for access to
93 /// different underlying pointers. Clients, such as the loop vectorizer, will
94 /// sometimes deal these potential dependencies by emitting runtime checks.
95 ///
96 /// We use the ScalarEvolution framework to symbolically evalutate access
97 /// functions pairs. Since we currently don't restructure the loop we can rely
98 /// on the program order of memory accesses to determine their safety.
99 /// At the moment we will only deem accesses as safe for:
100 ///  * A negative constant distance assuming program order.
101 ///
102 ///      Safe: tmp = a[i + 1];     OR     a[i + 1] = x;
103 ///            a[i] = tmp;                y = a[i];
104 ///
105 ///   The latter case is safe because later checks guarantuee that there can't
106 ///   be a cycle through a phi node (that is, we check that "x" and "y" is not
107 ///   the same variable: a header phi can only be an induction or a reduction, a
108 ///   reduction can't have a memory sink, an induction can't have a memory
109 ///   source). This is important and must not be violated (or we have to
110 ///   resort to checking for cycles through memory).
111 ///
112 ///  * A positive constant distance assuming program order that is bigger
113 ///    than the biggest memory access.
114 ///
115 ///     tmp = a[i]        OR              b[i] = x
116 ///     a[i+2] = tmp                      y = b[i+2];
117 ///
118 ///     Safe distance: 2 x sizeof(a[0]), and 2 x sizeof(b[0]), respectively.
119 ///
120 ///  * Zero distances and all accesses have the same size.
121 ///
122 class MemoryDepChecker {
123 public:
124   typedef PointerIntPair<Value *, 1, bool> MemAccessInfo;
125   typedef SmallPtrSet<MemAccessInfo, 8> MemAccessInfoSet;
126   /// \brief Set of potential dependent memory accesses.
127   typedef EquivalenceClasses<MemAccessInfo> DepCandidates;
128
129   /// \brief Dependece between memory access instructions.
130   struct Dependence {
131     /// \brief The type of the dependence.
132     enum DepType {
133       // No dependence.
134       NoDep,
135       // We couldn't determine the direction or the distance.
136       Unknown,
137       // Lexically forward.
138       Forward,
139       // Forward, but if vectorized, is likely to prevent store-to-load
140       // forwarding.
141       ForwardButPreventsForwarding,
142       // Lexically backward.
143       Backward,
144       // Backward, but the distance allows a vectorization factor of
145       // MaxSafeDepDistBytes.
146       BackwardVectorizable,
147       // Same, but may prevent store-to-load forwarding.
148       BackwardVectorizableButPreventsForwarding
149     };
150
151     /// \brief String version of the types.
152     static const char *DepName[];
153
154     /// \brief Index of the source of the dependence in the InstMap vector.
155     unsigned Source;
156     /// \brief Index of the destination of the dependence in the InstMap vector.
157     unsigned Destination;
158     /// \brief The type of the dependence.
159     DepType Type;
160
161     Dependence(unsigned Source, unsigned Destination, DepType Type)
162         : Source(Source), Destination(Destination), Type(Type) {}
163
164     /// \brief Dependence types that don't prevent vectorization.
165     static bool isSafeForVectorization(DepType Type);
166
167     /// \brief Dependence types that can be queried from the analysis.
168     static bool isInterestingDependence(DepType Type);
169
170     /// \brief Lexically backward dependence types.
171     bool isPossiblyBackward() const;
172
173     /// \brief Print the dependence.  \p Instr is used to map the instruction
174     /// indices to instructions.
175     void print(raw_ostream &OS, unsigned Depth,
176                const SmallVectorImpl<Instruction *> &Instrs) const;
177   };
178
179   MemoryDepChecker(ScalarEvolution *Se, const Loop *L)
180       : SE(Se), InnermostLoop(L), AccessIdx(0),
181         ShouldRetryWithRuntimeCheck(false), SafeForVectorization(true),
182         RecordInterestingDependences(true) {}
183
184   /// \brief Register the location (instructions are given increasing numbers)
185   /// of a write access.
186   void addAccess(StoreInst *SI) {
187     Value *Ptr = SI->getPointerOperand();
188     Accesses[MemAccessInfo(Ptr, true)].push_back(AccessIdx);
189     InstMap.push_back(SI);
190     ++AccessIdx;
191   }
192
193   /// \brief Register the location (instructions are given increasing numbers)
194   /// of a write access.
195   void addAccess(LoadInst *LI) {
196     Value *Ptr = LI->getPointerOperand();
197     Accesses[MemAccessInfo(Ptr, false)].push_back(AccessIdx);
198     InstMap.push_back(LI);
199     ++AccessIdx;
200   }
201
202   /// \brief Check whether the dependencies between the accesses are safe.
203   ///
204   /// Only checks sets with elements in \p CheckDeps.
205   bool areDepsSafe(DepCandidates &AccessSets, MemAccessInfoSet &CheckDeps,
206                    const ValueToValueMap &Strides);
207
208   /// \brief No memory dependence was encountered that would inhibit
209   /// vectorization.
210   bool isSafeForVectorization() const { return SafeForVectorization; }
211
212   /// \brief The maximum number of bytes of a vector register we can vectorize
213   /// the accesses safely with.
214   unsigned getMaxSafeDepDistBytes() { return MaxSafeDepDistBytes; }
215
216   /// \brief In same cases when the dependency check fails we can still
217   /// vectorize the loop with a dynamic array access check.
218   bool shouldRetryWithRuntimeCheck() { return ShouldRetryWithRuntimeCheck; }
219
220   /// \brief Returns the interesting dependences.  If null is returned we
221   /// exceeded the MaxInterestingDependence threshold and this information is
222   /// not available.
223   const SmallVectorImpl<Dependence> *getInterestingDependences() const {
224     return RecordInterestingDependences ? &InterestingDependences : nullptr;
225   }
226
227   void clearInterestingDependences() { InterestingDependences.clear(); }
228
229   /// \brief The vector of memory access instructions.  The indices are used as
230   /// instruction identifiers in the Dependence class.
231   const SmallVectorImpl<Instruction *> &getMemoryInstructions() const {
232     return InstMap;
233   }
234
235   /// \brief Find the set of instructions that read or write via \p Ptr.
236   SmallVector<Instruction *, 4> getInstructionsForAccess(Value *Ptr,
237                                                          bool isWrite) const;
238
239 private:
240   ScalarEvolution *SE;
241   const Loop *InnermostLoop;
242
243   /// \brief Maps access locations (ptr, read/write) to program order.
244   DenseMap<MemAccessInfo, std::vector<unsigned> > Accesses;
245
246   /// \brief Memory access instructions in program order.
247   SmallVector<Instruction *, 16> InstMap;
248
249   /// \brief The program order index to be used for the next instruction.
250   unsigned AccessIdx;
251
252   // We can access this many bytes in parallel safely.
253   unsigned MaxSafeDepDistBytes;
254
255   /// \brief If we see a non-constant dependence distance we can still try to
256   /// vectorize this loop with runtime checks.
257   bool ShouldRetryWithRuntimeCheck;
258
259   /// \brief No memory dependence was encountered that would inhibit
260   /// vectorization.
261   bool SafeForVectorization;
262
263   //// \brief True if InterestingDependences reflects the dependences in the
264   //// loop.  If false we exceeded MaxInterestingDependence and
265   //// InterestingDependences is invalid.
266   bool RecordInterestingDependences;
267
268   /// \brief Interesting memory dependences collected during the analysis as
269   /// defined by isInterestingDependence.  Only valid if
270   /// RecordInterestingDependences is true.
271   SmallVector<Dependence, 8> InterestingDependences;
272
273   /// \brief Check whether there is a plausible dependence between the two
274   /// accesses.
275   ///
276   /// Access \p A must happen before \p B in program order. The two indices
277   /// identify the index into the program order map.
278   ///
279   /// This function checks  whether there is a plausible dependence (or the
280   /// absence of such can't be proved) between the two accesses. If there is a
281   /// plausible dependence but the dependence distance is bigger than one
282   /// element access it records this distance in \p MaxSafeDepDistBytes (if this
283   /// distance is smaller than any other distance encountered so far).
284   /// Otherwise, this function returns true signaling a possible dependence.
285   Dependence::DepType isDependent(const MemAccessInfo &A, unsigned AIdx,
286                                   const MemAccessInfo &B, unsigned BIdx,
287                                   const ValueToValueMap &Strides);
288
289   /// \brief Check whether the data dependence could prevent store-load
290   /// forwarding.
291   bool couldPreventStoreLoadForward(unsigned Distance, unsigned TypeByteSize);
292 };
293
294 /// \brief Holds information about the memory runtime legality checks to verify
295 /// that a group of pointers do not overlap.
296 class RuntimePointerChecking {
297 public:
298   struct PointerInfo {
299     /// Holds the pointer value that we need to check.
300     TrackingVH<Value> PointerValue;
301     /// Holds the pointer value at the beginning of the loop.
302     const SCEV *Start;
303     /// Holds the pointer value at the end of the loop.
304     const SCEV *End;
305     /// Holds the information if this pointer is used for writing to memory.
306     bool IsWritePtr;
307     /// Holds the id of the set of pointers that could be dependent because of a
308     /// shared underlying object.
309     unsigned DependencySetId;
310     /// Holds the id of the disjoint alias set to which this pointer belongs.
311     unsigned AliasSetId;
312     /// SCEV for the access.
313     const SCEV *Expr;
314
315     PointerInfo(Value *PointerValue, const SCEV *Start, const SCEV *End,
316                 bool IsWritePtr, unsigned DependencySetId, unsigned AliasSetId,
317                 const SCEV *Expr)
318         : PointerValue(PointerValue), Start(Start), End(End),
319           IsWritePtr(IsWritePtr), DependencySetId(DependencySetId),
320           AliasSetId(AliasSetId), Expr(Expr) {}
321   };
322
323   RuntimePointerChecking(ScalarEvolution *SE) : Need(false), SE(SE) {}
324
325   /// Reset the state of the pointer runtime information.
326   void reset() {
327     Need = false;
328     Pointers.clear();
329     Checks.clear();
330   }
331
332   /// Insert a pointer and calculate the start and end SCEVs.
333   void insert(Loop *Lp, Value *Ptr, bool WritePtr, unsigned DepSetId,
334               unsigned ASId, const ValueToValueMap &Strides);
335
336   /// \brief No run-time memory checking is necessary.
337   bool empty() const { return Pointers.empty(); }
338
339   /// A grouping of pointers. A single memcheck is required between
340   /// two groups.
341   struct CheckingPtrGroup {
342     /// \brief Create a new pointer checking group containing a single
343     /// pointer, with index \p Index in RtCheck.
344     CheckingPtrGroup(unsigned Index, RuntimePointerChecking &RtCheck)
345         : RtCheck(RtCheck), High(RtCheck.Pointers[Index].End),
346           Low(RtCheck.Pointers[Index].Start) {
347       Members.push_back(Index);
348     }
349
350     /// \brief Tries to add the pointer recorded in RtCheck at index
351     /// \p Index to this pointer checking group. We can only add a pointer
352     /// to a checking group if we will still be able to get
353     /// the upper and lower bounds of the check. Returns true in case
354     /// of success, false otherwise.
355     bool addPointer(unsigned Index);
356
357     /// Constitutes the context of this pointer checking group. For each
358     /// pointer that is a member of this group we will retain the index
359     /// at which it appears in RtCheck.
360     RuntimePointerChecking &RtCheck;
361     /// The SCEV expression which represents the upper bound of all the
362     /// pointers in this group.
363     const SCEV *High;
364     /// The SCEV expression which represents the lower bound of all the
365     /// pointers in this group.
366     const SCEV *Low;
367     /// Indices of all the pointers that constitute this grouping.
368     SmallVector<unsigned, 2> Members;
369   };
370
371   /// \brief A memcheck which made up of a pair of grouped pointers.
372   ///
373   /// These *have* to be const for now, since checks are generated from
374   /// CheckingPtrGroups in LAI::addRuntimeChecks which is a const member
375   /// function.  FIXME: once check-generation is moved inside this class (after
376   /// the PtrPartition hack is removed), we could drop const.
377   typedef std::pair<const CheckingPtrGroup *, const CheckingPtrGroup *>
378       PointerCheck;
379
380   /// \brief Generate the checks and store it.  This also performs the grouping
381   /// of pointers to reduce the number of memchecks necessary.
382   void generateChecks(MemoryDepChecker::DepCandidates &DepCands,
383                       bool UseDependencies);
384
385   /// \brief Returns the checks that generateChecks created.
386   const SmallVector<PointerCheck, 4> &getChecks() const { return Checks; }
387
388   /// \brief Decide if we need to add a check between two groups of pointers,
389   /// according to needsChecking.
390   bool needsChecking(const CheckingPtrGroup &M,
391                      const CheckingPtrGroup &N) const;
392
393   /// \brief Returns the number of run-time checks required according to
394   /// needsChecking.
395   unsigned getNumberOfChecks() const { return Checks.size(); }
396
397   /// \brief Print the list run-time memory checks necessary.
398   void print(raw_ostream &OS, unsigned Depth = 0) const;
399
400   /// Print \p Checks.
401   void printChecks(raw_ostream &OS, const SmallVectorImpl<PointerCheck> &Checks,
402                    unsigned Depth = 0) const;
403
404   /// This flag indicates if we need to add the runtime check.
405   bool Need;
406
407   /// Information about the pointers that may require checking.
408   SmallVector<PointerInfo, 2> Pointers;
409
410   /// Holds a partitioning of pointers into "check groups".
411   SmallVector<CheckingPtrGroup, 2> CheckingGroups;
412
413   /// \brief Check if pointers are in the same partition
414   ///
415   /// \p PtrToPartition contains the partition number for pointers (-1 if the
416   /// pointer belongs to multiple partitions).
417   static bool
418   arePointersInSamePartition(const SmallVectorImpl<int> &PtrToPartition,
419                              unsigned PtrIdx1, unsigned PtrIdx2);
420
421   /// \brief Decide whether we need to issue a run-time check for pointer at
422   /// index \p I and \p J to prove their independence.
423   bool needsChecking(unsigned I, unsigned J) const;
424
425 private:
426   /// \brief Groups pointers such that a single memcheck is required
427   /// between two different groups. This will clear the CheckingGroups vector
428   /// and re-compute it. We will only group dependecies if \p UseDependencies
429   /// is true, otherwise we will create a separate group for each pointer.
430   void groupChecks(MemoryDepChecker::DepCandidates &DepCands,
431                    bool UseDependencies);
432
433   /// Generate the checks and return them.
434   SmallVector<PointerCheck, 4>
435   generateChecks() const;
436
437   /// Holds a pointer to the ScalarEvolution analysis.
438   ScalarEvolution *SE;
439
440   /// \brief Set of run-time checks required to establish independence of
441   /// otherwise may-aliasing pointers in the loop.
442   SmallVector<PointerCheck, 4> Checks;
443 };
444
445 /// \brief Drive the analysis of memory accesses in the loop
446 ///
447 /// This class is responsible for analyzing the memory accesses of a loop.  It
448 /// collects the accesses and then its main helper the AccessAnalysis class
449 /// finds and categorizes the dependences in buildDependenceSets.
450 ///
451 /// For memory dependences that can be analyzed at compile time, it determines
452 /// whether the dependence is part of cycle inhibiting vectorization.  This work
453 /// is delegated to the MemoryDepChecker class.
454 ///
455 /// For memory dependences that cannot be determined at compile time, it
456 /// generates run-time checks to prove independence.  This is done by
457 /// AccessAnalysis::canCheckPtrAtRT and the checks are maintained by the
458 /// RuntimePointerCheck class.
459 class LoopAccessInfo {
460 public:
461   LoopAccessInfo(Loop *L, ScalarEvolution *SE, const DataLayout &DL,
462                  const TargetLibraryInfo *TLI, AliasAnalysis *AA,
463                  DominatorTree *DT, LoopInfo *LI,
464                  const ValueToValueMap &Strides);
465
466   /// Return true we can analyze the memory accesses in the loop and there are
467   /// no memory dependence cycles.
468   bool canVectorizeMemory() const { return CanVecMem; }
469
470   const RuntimePointerChecking *getRuntimePointerChecking() const {
471     return &PtrRtChecking;
472   }
473
474   /// \brief Number of memchecks required to prove independence of otherwise
475   /// may-alias pointers.
476   unsigned getNumRuntimePointerChecks() const {
477     return PtrRtChecking.getNumberOfChecks();
478   }
479
480   /// Return true if the block BB needs to be predicated in order for the loop
481   /// to be vectorized.
482   static bool blockNeedsPredication(BasicBlock *BB, Loop *TheLoop,
483                                     DominatorTree *DT);
484
485   /// Returns true if the value V is uniform within the loop.
486   bool isUniform(Value *V) const;
487
488   unsigned getMaxSafeDepDistBytes() const { return MaxSafeDepDistBytes; }
489   unsigned getNumStores() const { return NumStores; }
490   unsigned getNumLoads() const { return NumLoads;}
491
492   /// \brief Add code that checks at runtime if the accessed arrays overlap.
493   ///
494   /// Returns a pair of instructions where the first element is the first
495   /// instruction generated in possibly a sequence of instructions and the
496   /// second value is the final comparator value or NULL if no check is needed.
497   std::pair<Instruction *, Instruction *>
498   addRuntimeChecks(Instruction *Loc) const;
499
500   /// \brief Generete the instructions for the checks in \p PointerChecks.
501   ///
502   /// Returns a pair of instructions where the first element is the first
503   /// instruction generated in possibly a sequence of instructions and the
504   /// second value is the final comparator value or NULL if no check is needed.
505   std::pair<Instruction *, Instruction *>
506   addRuntimeChecks(Instruction *Loc,
507                    const SmallVectorImpl<RuntimePointerChecking::PointerCheck>
508                        &PointerChecks) const;
509
510   /// \brief The diagnostics report generated for the analysis.  E.g. why we
511   /// couldn't analyze the loop.
512   const Optional<LoopAccessReport> &getReport() const { return Report; }
513
514   /// \brief the Memory Dependence Checker which can determine the
515   /// loop-independent and loop-carried dependences between memory accesses.
516   const MemoryDepChecker &getDepChecker() const { return DepChecker; }
517
518   /// \brief Return the list of instructions that use \p Ptr to read or write
519   /// memory.
520   SmallVector<Instruction *, 4> getInstructionsForAccess(Value *Ptr,
521                                                          bool isWrite) const {
522     return DepChecker.getInstructionsForAccess(Ptr, isWrite);
523   }
524
525   /// \brief Print the information about the memory accesses in the loop.
526   void print(raw_ostream &OS, unsigned Depth = 0) const;
527
528   /// \brief Used to ensure that if the analysis was run with speculating the
529   /// value of symbolic strides, the client queries it with the same assumption.
530   /// Only used in DEBUG build but we don't want NDEBUG-dependent ABI.
531   unsigned NumSymbolicStrides;
532
533   /// \brief Checks existence of store to invariant address inside loop.
534   /// If the loop has any store to invariant address, then it returns true,
535   /// else returns false.
536   bool hasStoreToLoopInvariantAddress() const {
537     return StoreToLoopInvariantAddress;
538   }
539
540 private:
541   /// \brief Analyze the loop.  Substitute symbolic strides using Strides.
542   void analyzeLoop(const ValueToValueMap &Strides);
543
544   /// \brief Check if the structure of the loop allows it to be analyzed by this
545   /// pass.
546   bool canAnalyzeLoop();
547
548   void emitAnalysis(LoopAccessReport &Message);
549
550   /// We need to check that all of the pointers in this list are disjoint
551   /// at runtime.
552   RuntimePointerChecking PtrRtChecking;
553
554   /// \brief the Memory Dependence Checker which can determine the
555   /// loop-independent and loop-carried dependences between memory accesses.
556   MemoryDepChecker DepChecker;
557
558   Loop *TheLoop;
559   ScalarEvolution *SE;
560   const DataLayout &DL;
561   const TargetLibraryInfo *TLI;
562   AliasAnalysis *AA;
563   DominatorTree *DT;
564   LoopInfo *LI;
565
566   unsigned NumLoads;
567   unsigned NumStores;
568
569   unsigned MaxSafeDepDistBytes;
570
571   /// \brief Cache the result of analyzeLoop.
572   bool CanVecMem;
573
574   /// \brief Indicator for storing to uniform addresses.
575   /// If a loop has write to a loop invariant address then it should be true.
576   bool StoreToLoopInvariantAddress;
577
578   /// \brief The diagnostics report generated for the analysis.  E.g. why we
579   /// couldn't analyze the loop.
580   Optional<LoopAccessReport> Report;
581 };
582
583 Value *stripIntegerCast(Value *V);
584
585 ///\brief Return the SCEV corresponding to a pointer with the symbolic stride
586 ///replaced with constant one.
587 ///
588 /// If \p OrigPtr is not null, use it to look up the stride value instead of \p
589 /// Ptr.  \p PtrToStride provides the mapping between the pointer value and its
590 /// stride as collected by LoopVectorizationLegality::collectStridedAccess.
591 const SCEV *replaceSymbolicStrideSCEV(ScalarEvolution *SE,
592                                       const ValueToValueMap &PtrToStride,
593                                       Value *Ptr, Value *OrigPtr = nullptr);
594
595 /// \brief Check the stride of the pointer and ensure that it does not wrap in
596 /// the address space.
597 int isStridedPtr(ScalarEvolution *SE, Value *Ptr, const Loop *Lp,
598                  const ValueToValueMap &StridesMap);
599
600 /// \brief This analysis provides dependence information for the memory accesses
601 /// of a loop.
602 ///
603 /// It runs the analysis for a loop on demand.  This can be initiated by
604 /// querying the loop access info via LAA::getInfo.  getInfo return a
605 /// LoopAccessInfo object.  See this class for the specifics of what information
606 /// is provided.
607 class LoopAccessAnalysis : public FunctionPass {
608 public:
609   static char ID;
610
611   LoopAccessAnalysis() : FunctionPass(ID) {
612     initializeLoopAccessAnalysisPass(*PassRegistry::getPassRegistry());
613   }
614
615   bool runOnFunction(Function &F) override;
616
617   void getAnalysisUsage(AnalysisUsage &AU) const override;
618
619   /// \brief Query the result of the loop access information for the loop \p L.
620   ///
621   /// If the client speculates (and then issues run-time checks) for the values
622   /// of symbolic strides, \p Strides provides the mapping (see
623   /// replaceSymbolicStrideSCEV).  If there is no cached result available run
624   /// the analysis.
625   const LoopAccessInfo &getInfo(Loop *L, const ValueToValueMap &Strides);
626
627   void releaseMemory() override {
628     // Invalidate the cache when the pass is freed.
629     LoopAccessInfoMap.clear();
630   }
631
632   /// \brief Print the result of the analysis when invoked with -analyze.
633   void print(raw_ostream &OS, const Module *M = nullptr) const override;
634
635 private:
636   /// \brief The cache.
637   DenseMap<Loop *, std::unique_ptr<LoopAccessInfo>> LoopAccessInfoMap;
638
639   // The used analysis passes.
640   ScalarEvolution *SE;
641   const TargetLibraryInfo *TLI;
642   AliasAnalysis *AA;
643   DominatorTree *DT;
644   LoopInfo *LI;
645 };
646 } // End llvm namespace
647
648 #endif