LoopAccessAnalysis.h 24 KB

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