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/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"
29
30namespace llvm {
31
32class Value;
33class DataLayout;
34class ScalarEvolution;
35class Loop;
36class SCEV;
37class SCEVUnionPredicate;
38class LoopAccessInfo;
39class OptimizationRemarkEmitter;
40
41/// \brief Collection of parameters shared beetween the Loop Vectorizer and the
42/// Loop Access Analysis.
43struct VectorizerParams {
44  /// \brief Maximum SIMD width.
45  static const unsigned MaxVectorWidth;
46
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();
53
54  /// \\brief When performing memory disambiguation checks at runtime do not
55  /// make more than this number of comparisons.
56  static unsigned RuntimeMemoryCheckThreshold;
57};
58
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).
62///
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.
66///
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.
72///
73///      Safe: tmp = a[i + 1];     OR     a[i + 1] = x;
74///            a[i] = tmp;                y = a[i];
75///
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).
82///
83///  * A positive constant distance assuming program order that is bigger
84///    than the biggest memory access.
85///
86///     tmp = a[i]        OR              b[i] = x
87///     a[i+2] = tmp                      y = b[i+2];
88///
89///     Safe distance: 2 x sizeof(a[0]), and 2 x sizeof(b[0]), respectively.
90///
91///  * Zero distances and all accesses have the same size.
92///
93class MemoryDepChecker {
94public:
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;
99
100  /// \brief Dependece between memory access instructions.
101  struct Dependence {
102    /// \brief The type of the dependence.
103    enum DepType {
104      // No dependence.
105      NoDep,
106      // We couldn't determine the direction or the distance.
107      Unknown,
108      // Lexically forward.
109      //
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.
117      Forward,
118      // Forward, but if vectorized, is likely to prevent store-to-load
119      // forwarding.
120      ForwardButPreventsForwarding,
121      // Lexically backward.
122      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
128    };
129
130    /// \brief String version of the types.
131    static const char *DepName[];
132
133    /// \brief Index of the source of the dependence in the InstMap vector.
134    unsigned Source;
135    /// \brief Index of the destination of the dependence in the InstMap vector.
136    unsigned Destination;
137    /// \brief The type of the dependence.
138    DepType Type;
139
140    Dependence(unsigned Source, unsigned Destination, DepType Type)
141        : Source(Source), Destination(Destination), Type(Type) {}
142
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;
147
148    /// \brief Dependence types that don't prevent vectorization.
149    static bool isSafeForVectorization(DepType Type);
150
151    /// \brief Lexically forward dependence.
152    bool isForward() const;
153    /// \brief Lexically backward dependence.
154    bool isBackward() const;
155
156    /// \brief May be a lexically backward dependence type (includes Unknown).
157    bool isPossiblyBackward() const;
158
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;
163  };
164
165  MemoryDepChecker(PredicatedScalarEvolution &PSE, const Loop *L)
166      : PSE(PSE), InnermostLoop(L), AccessIdx(0),
167        ShouldRetryWithRuntimeCheck(false), SafeForVectorization(true),
168        RecordDependences(true) {}
169
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);
176    ++AccessIdx;
177  }
178
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);
185    ++AccessIdx;
186  }
187
188  /// \brief Check whether the dependencies between the accesses are safe.
189  ///
190  /// Only checks sets with elements in \p CheckDeps.
191  bool areDepsSafe(DepCandidates &AccessSets, MemAccessInfoList &CheckDeps,
192                   const ValueToValueMap &Strides);
193
194  /// \brief No memory dependence was encountered that would inhibit
195  /// vectorization.
196  bool isSafeForVectorization() const { return SafeForVectorization; }
197
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; }
201
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; }
205
206  /// \brief Returns the memory dependences.  If null is returned we exceeded
207  /// the MaxDependences threshold and this information is not
208  /// available.
209  const SmallVectorImpl<Dependence> *getDependences() const {
210    return RecordDependences ? &Dependences : nullptr;
211  }
212
213  void clearDependences() { Dependences.clear(); }
214
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 {
218    return InstMap;
219  }
220
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;
225
226    for (unsigned I = 0; I < InstMap.size(); ++I)
227      OrderMap[InstMap[I]] = I;
228
229    return OrderMap;
230  }
231
232  /// \brief Find the set of instructions that read or write via \p Ptr.
233  SmallVector<Instruction *, 4> getInstructionsForAccess(Value *Ptr,
234                                                         bool isWrite) const;
235
236private:
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;
245
246  /// \brief Maps access locations (ptr, read/write) to program order.
247  DenseMap<MemAccessInfo, std::vector<unsigned> > Accesses;
248
249  /// \brief Memory access instructions in program order.
250  SmallVector<Instruction *, 16> InstMap;
251
252  /// \brief The program order index to be used for the next instruction.
253  unsigned AccessIdx;
254
255  // We can access this many bytes in parallel safely.
256  uint64_t MaxSafeDepDistBytes;
257
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;
261
262  /// \brief No memory dependence was encountered that would inhibit
263  /// vectorization.
264  bool SafeForVectorization;
265
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;
270
271  /// \brief Memory dependences collected during the analysis.  Only valid if
272  /// RecordDependences is true.
273  SmallVector<Dependence, 8> Dependences;
274
275  /// \brief Check whether there is a plausible dependence between the two
276  /// accesses.
277  ///
278  /// Access \p A must happen before \p B in program order. The two indices
279  /// identify the index into the program order map.
280  ///
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);
290
291  /// \brief Check whether the data dependence could prevent store-load
292  /// forwarding.
293  ///
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);
297};
298
299/// \brief Holds information about the memory runtime legality checks to verify
300/// that a group of pointers do not overlap.
301class RuntimePointerChecking {
302public:
303  struct PointerInfo {
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.
308    const SCEV *Start;
309    /// Holds the largest byte address accessed by the pointer throughout all
310    /// iterations of the loop, plus 1.
311    const SCEV *End;
312    /// Holds the information if this pointer is used for writing to memory.
313    bool IsWritePtr;
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.
318    unsigned AliasSetId;
319    /// SCEV for the access.
320    const SCEV *Expr;
321
322    PointerInfo(Value *PointerValue, const SCEV *Start, const SCEV *End,
323                bool IsWritePtr, unsigned DependencySetId, unsigned AliasSetId,
324                const SCEV *Expr)
325        : PointerValue(PointerValue), Start(Start), End(End),
326          IsWritePtr(IsWritePtr), DependencySetId(DependencySetId),
327          AliasSetId(AliasSetId), Expr(Expr) {}
328  };
329
330  RuntimePointerChecking(ScalarEvolution *SE) : Need(false), SE(SE) {}
331
332  /// Reset the state of the pointer runtime information.
333  void reset() {
334    Need = false;
335    Pointers.clear();
336    Checks.clear();
337  }
338
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);
347
348  /// \brief No run-time memory checking is necessary.
349  bool empty() const { return Pointers.empty(); }
350
351  /// A grouping of pointers. A single memcheck is required between
352  /// two groups.
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);
360    }
361
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);
368
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.
375    const SCEV *High;
376    /// The SCEV expression which represents the lower bound of all the
377    /// pointers in this group.
378    const SCEV *Low;
379    /// Indices of all the pointers that constitute this grouping.
380    SmallVector<unsigned, 2> Members;
381  };
382
383  /// \brief A memcheck which made up of a pair of grouped pointers.
384  ///
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 *>
390      PointerCheck;
391
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);
396
397  /// \brief Returns the checks that generateChecks created.
398  const SmallVector<PointerCheck, 4> &getChecks() const { return Checks; }
399
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;
404
405  /// \brief Returns the number of run-time checks required according to
406  /// needsChecking.
407  unsigned getNumberOfChecks() const { return Checks.size(); }
408
409  /// \brief Print the list run-time memory checks necessary.
410  void print(raw_ostream &OS, unsigned Depth = 0) const;
411
412  /// Print \p Checks.
413  void printChecks(raw_ostream &OS, const SmallVectorImpl<PointerCheck> &Checks,
414                   unsigned Depth = 0) const;
415
416  /// This flag indicates if we need to add the runtime check.
417  bool Need;
418
419  /// Information about the pointers that may require checking.
420  SmallVector<PointerInfo, 2> Pointers;
421
422  /// Holds a partitioning of pointers into "check groups".
423  SmallVector<CheckingPtrGroup, 2> CheckingGroups;
424
425  /// \brief Check if pointers are in the same partition
426  ///
427  /// \p PtrToPartition contains the partition number for pointers (-1 if the
428  /// pointer belongs to multiple partitions).
429  static bool
430  arePointersInSamePartition(const SmallVectorImpl<int> &PtrToPartition,
431                             unsigned PtrIdx1, unsigned PtrIdx2);
432
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;
436
437  /// \brief Return PointerInfo for pointer at index \p PtrIdx.
438  const PointerInfo &getPointerInfo(unsigned PtrIdx) const {
439    return Pointers[PtrIdx];
440  }
441
442private:
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);
449
450  /// Generate the checks and return them.
451  SmallVector<PointerCheck, 4>
452  generateChecks() const;
453
454  /// Holds a pointer to the ScalarEvolution analysis.
455  ScalarEvolution *SE;
456
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;
460};
461
462/// \brief Drive the analysis of memory accesses in the loop
463///
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.
467///
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.
471///
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.
476///
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.
480///
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.
483class LoopAccessInfo {
484public:
485  LoopAccessInfo(Loop *L, ScalarEvolution *SE, const TargetLibraryInfo *TLI,
486                 AliasAnalysis *AA, DominatorTree *DT, LoopInfo *LI);
487
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; }
491
492  const RuntimePointerChecking *getRuntimePointerChecking() const {
493    return PtrRtChecking.get();
494  }
495
496  /// \brief Number of memchecks required to prove independence of otherwise
497  /// may-alias pointers.
498  unsigned getNumRuntimePointerChecks() const {
499    return PtrRtChecking->getNumberOfChecks();
500  }
501
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,
505                                    DominatorTree *DT);
506
507  /// Returns true if the value V is uniform within the loop.
508  bool isUniform(Value *V) const;
509
510  uint64_t getMaxSafeDepDistBytes() const { return MaxSafeDepDistBytes; }
511  unsigned getNumStores() const { return NumStores; }
512  unsigned getNumLoads() const { return NumLoads;}
513
514  /// \brief Add code that checks at runtime if the accessed arrays overlap.
515  ///
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;
521
522  /// \brief Generete the instructions for the checks in \p PointerChecks.
523  ///
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;
531
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(); }
535
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; }
539
540  /// \brief Return the list of instructions that use \p Ptr to read or write
541  /// memory.
542  SmallVector<Instruction *, 4> getInstructionsForAccess(Value *Ptr,
543                                                         bool isWrite) const {
544    return DepChecker->getInstructionsForAccess(Ptr, isWrite);
545  }
546
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; }
550
551  /// \brief Pointer has a symbolic stride.
552  bool hasStride(Value *V) const { return StrideSet.count(V); }
553
554  /// \brief Print the information about the memory accesses in the loop.
555  void print(raw_ostream &OS, unsigned Depth = 0) const;
556
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;
562  }
563
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; }
570
571private:
572  /// \brief Analyze the loop.
573  void analyzeLoop(AliasAnalysis *AA, LoopInfo *LI,
574                   const TargetLibraryInfo *TLI, DominatorTree *DT);
575
576  /// \brief Check if the structure of the loop allows it to be analyzed by this
577  /// pass.
578  bool canAnalyzeLoop();
579
580  /// \brief Save the analysis remark.
581  ///
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);
587
588  /// \brief Collect memory access with loop invariant strides.
589  ///
590  /// Looks for accesses like "a[i * StrideA]" where "StrideA" is loop
591  /// invariant.
592  void collectStridedAccess(Value *LoadOrStoreInst);
593
594  std::unique_ptr<PredicatedScalarEvolution> PSE;
595
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;
599
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;
603
604  Loop *TheLoop;
605
606  unsigned NumLoads;
607  unsigned NumStores;
608
609  uint64_t MaxSafeDepDistBytes;
610
611  /// \brief Cache the result of analyzeLoop.
612  bool CanVecMem;
613
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;
617
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;
621
622  /// \brief If an access has a symbolic strides, this maps the pointer value to
623  /// the stride symbol.
624  ValueToValueMap SymbolicStrides;
625
626  /// \brief Set of symbolic strides values.
627  SmallPtrSet<Value *, 8> StrideSet;
628};
629
630Value *stripIntegerCast(Value *V);
631
632/// \brief Return the SCEV corresponding to a pointer with the symbolic stride
633/// replaced with constant one, assuming the SCEV predicate associated with
634/// \p PSE is true.
635///
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.
638///
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.
642const SCEV *replaceSymbolicStrideSCEV(PredicatedScalarEvolution &PSE,
643                                      const ValueToValueMap &PtrToStride,
644                                      Value *Ptr, Value *OrigPtr = nullptr);
645
646/// \brief If the pointer has a constant stride return it in units of its
647/// element size.  Otherwise return zero.
648///
649/// Ensure that it does not wrap in the address space, assuming the predicate
650/// associated with \p PSE is true.
651///
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.
656int64_t getPtrStride(PredicatedScalarEvolution &PSE, Value *Ptr, const Loop *Lp,
657                     const ValueToValueMap &StridesMap = ValueToValueMap(),
658                     bool Assume = false, bool ShouldCheckWrap = true);
659
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.
662bool isConsecutiveAccess(Value *A, Value *B, const DataLayout &DL,
663                         ScalarEvolution &SE, bool CheckType = true);
664
665/// \brief This analysis provides dependence information for the memory accesses
666/// of a loop.
667///
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
671/// is provided.
672class LoopAccessLegacyAnalysis : public FunctionPass {
673public:
674  static char ID;
675
676  LoopAccessLegacyAnalysis() : FunctionPass(ID) {
677    initializeLoopAccessLegacyAnalysisPass(*PassRegistry::getPassRegistry());
678  }
679
680  bool runOnFunction(Function &F) override;
681
682  void getAnalysisUsage(AnalysisUsage &AU) const override;
683
684  /// \brief Query the result of the loop access information for the loop \p L.
685  ///
686  /// If there is no cached result available run the analysis.
687  const LoopAccessInfo &getInfo(Loop *L);
688
689  void releaseMemory() override {
690    // Invalidate the cache when the pass is freed.
691    LoopAccessInfoMap.clear();
692  }
693
694  /// \brief Print the result of the analysis when invoked with -analyze.
695  void print(raw_ostream &OS, const Module *M = nullptr) const override;
696
697private:
698  /// \brief The cache.
699  DenseMap<Loop *, std::unique_ptr<LoopAccessInfo>> LoopAccessInfoMap;
700
701  // The used analysis passes.
702  ScalarEvolution *SE;
703  const TargetLibraryInfo *TLI;
704  AliasAnalysis *AA;
705  DominatorTree *DT;
706  LoopInfo *LI;
707};
708
709/// \brief This analysis provides dependence information for the memory
710/// accesses of a loop.
711///
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.
716class LoopAccessAnalysis
717    : public AnalysisInfoMixin<LoopAccessAnalysis> {
718  friend AnalysisInfoMixin<LoopAccessAnalysis>;
719  static AnalysisKey Key;
720
721public:
722  typedef LoopAccessInfo Result;
723
724  Result run(Loop &L, LoopAnalysisManager &AM, LoopStandardAnalysisResults &AR);
725};
726
727inline Instruction *MemoryDepChecker::Dependence::getSource(
728    const LoopAccessInfo &LAI) const {
729  return LAI.getDepChecker().getMemoryInstructions()[Source];
730}
731
732inline Instruction *MemoryDepChecker::Dependence::getDestination(
733    const LoopAccessInfo &LAI) const {
734  return LAI.getDepChecker().getMemoryInstructions()[Destination];
735}
736
737} // End llvm namespace
738
739#endif
740