MemoryDependenceAnalysis.h 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- llvm/Analysis/MemoryDependenceAnalysis.h - Memory Deps ---*- C++ -*-===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. //
  14. // This file defines the MemoryDependenceAnalysis analysis pass.
  15. //
  16. //===----------------------------------------------------------------------===//
  17. #ifndef LLVM_ANALYSIS_MEMORYDEPENDENCEANALYSIS_H
  18. #define LLVM_ANALYSIS_MEMORYDEPENDENCEANALYSIS_H
  19. #include "llvm/ADT/DenseMap.h"
  20. #include "llvm/ADT/Optional.h"
  21. #include "llvm/ADT/PointerEmbeddedInt.h"
  22. #include "llvm/ADT/PointerIntPair.h"
  23. #include "llvm/ADT/PointerSumType.h"
  24. #include "llvm/ADT/SmallPtrSet.h"
  25. #include "llvm/Analysis/MemoryLocation.h"
  26. #include "llvm/IR/BasicBlock.h"
  27. #include "llvm/IR/Metadata.h"
  28. #include "llvm/IR/PassManager.h"
  29. #include "llvm/IR/PredIteratorCache.h"
  30. #include "llvm/IR/ValueHandle.h"
  31. #include "llvm/Pass.h"
  32. #include "llvm/Support/ErrorHandling.h"
  33. #include <cassert>
  34. #include <cstdint>
  35. #include <utility>
  36. #include <vector>
  37. namespace llvm {
  38. class AAResults;
  39. class AssumptionCache;
  40. class DominatorTree;
  41. class Function;
  42. class Instruction;
  43. class LoadInst;
  44. class PHITransAddr;
  45. class TargetLibraryInfo;
  46. class PhiValues;
  47. class Value;
  48. /// A memory dependence query can return one of three different answers.
  49. class MemDepResult {
  50. enum DepType {
  51. /// Clients of MemDep never see this.
  52. ///
  53. /// Entries with this marker occur in a LocalDeps map or NonLocalDeps map
  54. /// when the instruction they previously referenced was removed from
  55. /// MemDep. In either case, the entry may include an instruction pointer.
  56. /// If so, the pointer is an instruction in the block where scanning can
  57. /// start from, saving some work.
  58. ///
  59. /// In a default-constructed MemDepResult object, the type will be Invalid
  60. /// and the instruction pointer will be null.
  61. Invalid = 0,
  62. /// This is a dependence on the specified instruction which clobbers the
  63. /// desired value. The pointer member of the MemDepResult pair holds the
  64. /// instruction that clobbers the memory. For example, this occurs when we
  65. /// see a may-aliased store to the memory location we care about.
  66. ///
  67. /// There are several cases that may be interesting here:
  68. /// 1. Loads are clobbered by may-alias stores.
  69. /// 2. Loads are considered clobbered by partially-aliased loads. The
  70. /// client may choose to analyze deeper into these cases.
  71. Clobber,
  72. /// This is a dependence on the specified instruction which defines or
  73. /// produces the desired memory location. The pointer member of the
  74. /// MemDepResult pair holds the instruction that defines the memory.
  75. ///
  76. /// Cases of interest:
  77. /// 1. This could be a load or store for dependence queries on
  78. /// load/store. The value loaded or stored is the produced value.
  79. /// Note that the pointer operand may be different than that of the
  80. /// queried pointer due to must aliases and phi translation. Note
  81. /// that the def may not be the same type as the query, the pointers
  82. /// may just be must aliases.
  83. /// 2. For loads and stores, this could be an allocation instruction. In
  84. /// this case, the load is loading an undef value or a store is the
  85. /// first store to (that part of) the allocation.
  86. /// 3. Dependence queries on calls return Def only when they are readonly
  87. /// calls or memory use intrinsics with identical callees and no
  88. /// intervening clobbers. No validation is done that the operands to
  89. /// the calls are the same.
  90. Def,
  91. /// This marker indicates that the query has no known dependency in the
  92. /// specified block.
  93. ///
  94. /// More detailed state info is encoded in the upper part of the pair (i.e.
  95. /// the Instruction*)
  96. Other
  97. };
  98. /// If DepType is "Other", the upper part of the sum type is an encoding of
  99. /// the following more detailed type information.
  100. enum OtherType {
  101. /// This marker indicates that the query has no dependency in the specified
  102. /// block.
  103. ///
  104. /// To find out more, the client should query other predecessor blocks.
  105. NonLocal = 1,
  106. /// This marker indicates that the query has no dependency in the specified
  107. /// function.
  108. NonFuncLocal,
  109. /// This marker indicates that the query dependency is unknown.
  110. Unknown
  111. };
  112. using ValueTy = PointerSumType<
  113. DepType, PointerSumTypeMember<Invalid, Instruction *>,
  114. PointerSumTypeMember<Clobber, Instruction *>,
  115. PointerSumTypeMember<Def, Instruction *>,
  116. PointerSumTypeMember<Other, PointerEmbeddedInt<OtherType, 3>>>;
  117. ValueTy Value;
  118. explicit MemDepResult(ValueTy V) : Value(V) {}
  119. public:
  120. MemDepResult() = default;
  121. /// get methods: These are static ctor methods for creating various
  122. /// MemDepResult kinds.
  123. static MemDepResult getDef(Instruction *Inst) {
  124. assert(Inst && "Def requires inst");
  125. return MemDepResult(ValueTy::create<Def>(Inst));
  126. }
  127. static MemDepResult getClobber(Instruction *Inst) {
  128. assert(Inst && "Clobber requires inst");
  129. return MemDepResult(ValueTy::create<Clobber>(Inst));
  130. }
  131. static MemDepResult getNonLocal() {
  132. return MemDepResult(ValueTy::create<Other>(NonLocal));
  133. }
  134. static MemDepResult getNonFuncLocal() {
  135. return MemDepResult(ValueTy::create<Other>(NonFuncLocal));
  136. }
  137. static MemDepResult getUnknown() {
  138. return MemDepResult(ValueTy::create<Other>(Unknown));
  139. }
  140. /// Tests if this MemDepResult represents a query that is an instruction
  141. /// clobber dependency.
  142. bool isClobber() const { return Value.is<Clobber>(); }
  143. /// Tests if this MemDepResult represents a query that is an instruction
  144. /// definition dependency.
  145. bool isDef() const { return Value.is<Def>(); }
  146. /// Tests if this MemDepResult represents a query that is transparent to the
  147. /// start of the block, but where a non-local hasn't been done.
  148. bool isNonLocal() const {
  149. return Value.is<Other>() && Value.cast<Other>() == NonLocal;
  150. }
  151. /// Tests if this MemDepResult represents a query that is transparent to the
  152. /// start of the function.
  153. bool isNonFuncLocal() const {
  154. return Value.is<Other>() && Value.cast<Other>() == NonFuncLocal;
  155. }
  156. /// Tests if this MemDepResult represents a query which cannot and/or will
  157. /// not be computed.
  158. bool isUnknown() const {
  159. return Value.is<Other>() && Value.cast<Other>() == Unknown;
  160. }
  161. /// If this is a normal dependency, returns the instruction that is depended
  162. /// on. Otherwise, returns null.
  163. Instruction *getInst() const {
  164. switch (Value.getTag()) {
  165. case Invalid:
  166. return Value.cast<Invalid>();
  167. case Clobber:
  168. return Value.cast<Clobber>();
  169. case Def:
  170. return Value.cast<Def>();
  171. case Other:
  172. return nullptr;
  173. }
  174. llvm_unreachable("Unknown discriminant!");
  175. }
  176. bool operator==(const MemDepResult &M) const { return Value == M.Value; }
  177. bool operator!=(const MemDepResult &M) const { return Value != M.Value; }
  178. bool operator<(const MemDepResult &M) const { return Value < M.Value; }
  179. bool operator>(const MemDepResult &M) const { return Value > M.Value; }
  180. private:
  181. friend class MemoryDependenceResults;
  182. /// Tests if this is a MemDepResult in its dirty/invalid. state.
  183. bool isDirty() const { return Value.is<Invalid>(); }
  184. static MemDepResult getDirty(Instruction *Inst) {
  185. return MemDepResult(ValueTy::create<Invalid>(Inst));
  186. }
  187. };
  188. /// This is an entry in the NonLocalDepInfo cache.
  189. ///
  190. /// For each BasicBlock (the BB entry) it keeps a MemDepResult.
  191. class NonLocalDepEntry {
  192. BasicBlock *BB;
  193. MemDepResult Result;
  194. public:
  195. NonLocalDepEntry(BasicBlock *bb, MemDepResult result)
  196. : BB(bb), Result(result) {}
  197. // This is used for searches.
  198. NonLocalDepEntry(BasicBlock *bb) : BB(bb) {}
  199. // BB is the sort key, it can't be changed.
  200. BasicBlock *getBB() const { return BB; }
  201. void setResult(const MemDepResult &R) { Result = R; }
  202. const MemDepResult &getResult() const { return Result; }
  203. bool operator<(const NonLocalDepEntry &RHS) const { return BB < RHS.BB; }
  204. };
  205. /// This is a result from a NonLocal dependence query.
  206. ///
  207. /// For each BasicBlock (the BB entry) it keeps a MemDepResult and the
  208. /// (potentially phi translated) address that was live in the block.
  209. class NonLocalDepResult {
  210. NonLocalDepEntry Entry;
  211. Value *Address;
  212. public:
  213. NonLocalDepResult(BasicBlock *bb, MemDepResult result, Value *address)
  214. : Entry(bb, result), Address(address) {}
  215. // BB is the sort key, it can't be changed.
  216. BasicBlock *getBB() const { return Entry.getBB(); }
  217. void setResult(const MemDepResult &R, Value *Addr) {
  218. Entry.setResult(R);
  219. Address = Addr;
  220. }
  221. const MemDepResult &getResult() const { return Entry.getResult(); }
  222. /// Returns the address of this pointer in this block.
  223. ///
  224. /// This can be different than the address queried for the non-local result
  225. /// because of phi translation. This returns null if the address was not
  226. /// available in a block (i.e. because phi translation failed) or if this is
  227. /// a cached result and that address was deleted.
  228. ///
  229. /// The address is always null for a non-local 'call' dependence.
  230. Value *getAddress() const { return Address; }
  231. };
  232. /// Provides a lazy, caching interface for making common memory aliasing
  233. /// information queries, backed by LLVM's alias analysis passes.
  234. ///
  235. /// The dependency information returned is somewhat unusual, but is pragmatic.
  236. /// If queried about a store or call that might modify memory, the analysis
  237. /// will return the instruction[s] that may either load from that memory or
  238. /// store to it. If queried with a load or call that can never modify memory,
  239. /// the analysis will return calls and stores that might modify the pointer,
  240. /// but generally does not return loads unless a) they are volatile, or
  241. /// b) they load from *must-aliased* pointers. Returning a dependence on
  242. /// must-alias'd pointers instead of all pointers interacts well with the
  243. /// internal caching mechanism.
  244. class MemoryDependenceResults {
  245. // A map from instructions to their dependency.
  246. using LocalDepMapType = DenseMap<Instruction *, MemDepResult>;
  247. LocalDepMapType LocalDeps;
  248. public:
  249. using NonLocalDepInfo = std::vector<NonLocalDepEntry>;
  250. private:
  251. /// A pair<Value*, bool> where the bool is true if the dependence is a read
  252. /// only dependence, false if read/write.
  253. using ValueIsLoadPair = PointerIntPair<const Value *, 1, bool>;
  254. /// This pair is used when caching information for a block.
  255. ///
  256. /// If the pointer is null, the cache value is not a full query that starts
  257. /// at the specified block. If non-null, the bool indicates whether or not
  258. /// the contents of the block was skipped.
  259. using BBSkipFirstBlockPair = PointerIntPair<BasicBlock *, 1, bool>;
  260. /// This record is the information kept for each (value, is load) pair.
  261. struct NonLocalPointerInfo {
  262. /// The pair of the block and the skip-first-block flag.
  263. BBSkipFirstBlockPair Pair;
  264. /// The results of the query for each relevant block.
  265. NonLocalDepInfo NonLocalDeps;
  266. /// The maximum size of the dereferences of the pointer.
  267. ///
  268. /// May be UnknownSize if the sizes are unknown.
  269. LocationSize Size = LocationSize::afterPointer();
  270. /// The AA tags associated with dereferences of the pointer.
  271. ///
  272. /// The members may be null if there are no tags or conflicting tags.
  273. AAMDNodes AATags;
  274. NonLocalPointerInfo() = default;
  275. };
  276. /// Cache storing single nonlocal def for the instruction.
  277. /// It is set when nonlocal def would be found in function returning only
  278. /// local dependencies.
  279. DenseMap<AssertingVH<const Value>, NonLocalDepResult> NonLocalDefsCache;
  280. using ReverseNonLocalDefsCacheTy =
  281. DenseMap<Instruction *, SmallPtrSet<const Value*, 4>>;
  282. ReverseNonLocalDefsCacheTy ReverseNonLocalDefsCache;
  283. /// This map stores the cached results of doing a pointer lookup at the
  284. /// bottom of a block.
  285. ///
  286. /// The key of this map is the pointer+isload bit, the value is a list of
  287. /// <bb->result> mappings.
  288. using CachedNonLocalPointerInfo =
  289. DenseMap<ValueIsLoadPair, NonLocalPointerInfo>;
  290. CachedNonLocalPointerInfo NonLocalPointerDeps;
  291. // A map from instructions to their non-local pointer dependencies.
  292. using ReverseNonLocalPtrDepTy =
  293. DenseMap<Instruction *, SmallPtrSet<ValueIsLoadPair, 4>>;
  294. ReverseNonLocalPtrDepTy ReverseNonLocalPtrDeps;
  295. /// This is the instruction we keep for each cached access that we have for
  296. /// an instruction.
  297. ///
  298. /// The pointer is an owning pointer and the bool indicates whether we have
  299. /// any dirty bits in the set.
  300. using PerInstNLInfo = std::pair<NonLocalDepInfo, bool>;
  301. // A map from instructions to their non-local dependencies.
  302. using NonLocalDepMapType = DenseMap<Instruction *, PerInstNLInfo>;
  303. NonLocalDepMapType NonLocalDeps;
  304. // A reverse mapping from dependencies to the dependees. This is
  305. // used when removing instructions to keep the cache coherent.
  306. using ReverseDepMapType =
  307. DenseMap<Instruction *, SmallPtrSet<Instruction *, 4>>;
  308. ReverseDepMapType ReverseLocalDeps;
  309. // A reverse mapping from dependencies to the non-local dependees.
  310. ReverseDepMapType ReverseNonLocalDeps;
  311. /// Current AA implementation, just a cache.
  312. AAResults &AA;
  313. AssumptionCache &AC;
  314. const TargetLibraryInfo &TLI;
  315. DominatorTree &DT;
  316. PhiValues &PV;
  317. PredIteratorCache PredCache;
  318. unsigned DefaultBlockScanLimit;
  319. public:
  320. MemoryDependenceResults(AAResults &AA, AssumptionCache &AC,
  321. const TargetLibraryInfo &TLI, DominatorTree &DT,
  322. PhiValues &PV, unsigned DefaultBlockScanLimit)
  323. : AA(AA), AC(AC), TLI(TLI), DT(DT), PV(PV),
  324. DefaultBlockScanLimit(DefaultBlockScanLimit) {}
  325. /// Handle invalidation in the new PM.
  326. bool invalidate(Function &F, const PreservedAnalyses &PA,
  327. FunctionAnalysisManager::Invalidator &Inv);
  328. /// Some methods limit the number of instructions they will examine.
  329. /// The return value of this method is the default limit that will be
  330. /// used if no limit is explicitly passed in.
  331. unsigned getDefaultBlockScanLimit() const;
  332. /// Returns the instruction on which a memory operation depends.
  333. ///
  334. /// See the class comment for more details. It is illegal to call this on
  335. /// non-memory instructions.
  336. MemDepResult getDependency(Instruction *QueryInst);
  337. /// Perform a full dependency query for the specified call, returning the set
  338. /// of blocks that the value is potentially live across.
  339. ///
  340. /// The returned set of results will include a "NonLocal" result for all
  341. /// blocks where the value is live across.
  342. ///
  343. /// This method assumes the instruction returns a "NonLocal" dependency
  344. /// within its own block.
  345. ///
  346. /// This returns a reference to an internal data structure that may be
  347. /// invalidated on the next non-local query or when an instruction is
  348. /// removed. Clients must copy this data if they want it around longer than
  349. /// that.
  350. const NonLocalDepInfo &getNonLocalCallDependency(CallBase *QueryCall);
  351. /// Perform a full dependency query for an access to the QueryInst's
  352. /// specified memory location, returning the set of instructions that either
  353. /// define or clobber the value.
  354. ///
  355. /// Warning: For a volatile query instruction, the dependencies will be
  356. /// accurate, and thus usable for reordering, but it is never legal to
  357. /// remove the query instruction.
  358. ///
  359. /// This method assumes the pointer has a "NonLocal" dependency within
  360. /// QueryInst's parent basic block.
  361. void getNonLocalPointerDependency(Instruction *QueryInst,
  362. SmallVectorImpl<NonLocalDepResult> &Result);
  363. /// Removes an instruction from the dependence analysis, updating the
  364. /// dependence of instructions that previously depended on it.
  365. void removeInstruction(Instruction *InstToRemove);
  366. /// Invalidates cached information about the specified pointer, because it
  367. /// may be too conservative in memdep.
  368. ///
  369. /// This is an optional call that can be used when the client detects an
  370. /// equivalence between the pointer and some other value and replaces the
  371. /// other value with ptr. This can make Ptr available in more places that
  372. /// cached info does not necessarily keep.
  373. void invalidateCachedPointerInfo(Value *Ptr);
  374. /// Clears the PredIteratorCache info.
  375. ///
  376. /// This needs to be done when the CFG changes, e.g., due to splitting
  377. /// critical edges.
  378. void invalidateCachedPredecessors();
  379. /// Returns the instruction on which a memory location depends.
  380. ///
  381. /// If isLoad is true, this routine ignores may-aliases with read-only
  382. /// operations. If isLoad is false, this routine ignores may-aliases
  383. /// with reads from read-only locations. If possible, pass the query
  384. /// instruction as well; this function may take advantage of the metadata
  385. /// annotated to the query instruction to refine the result. \p Limit
  386. /// can be used to set the maximum number of instructions that will be
  387. /// examined to find the pointer dependency. On return, it will be set to
  388. /// the number of instructions left to examine. If a null pointer is passed
  389. /// in, the limit will default to the value of -memdep-block-scan-limit.
  390. ///
  391. /// Note that this is an uncached query, and thus may be inefficient.
  392. MemDepResult getPointerDependencyFrom(const MemoryLocation &Loc, bool isLoad,
  393. BasicBlock::iterator ScanIt,
  394. BasicBlock *BB,
  395. Instruction *QueryInst = nullptr,
  396. unsigned *Limit = nullptr);
  397. MemDepResult
  398. getSimplePointerDependencyFrom(const MemoryLocation &MemLoc, bool isLoad,
  399. BasicBlock::iterator ScanIt, BasicBlock *BB,
  400. Instruction *QueryInst, unsigned *Limit);
  401. /// This analysis looks for other loads and stores with invariant.group
  402. /// metadata and the same pointer operand. Returns Unknown if it does not
  403. /// find anything, and Def if it can be assumed that 2 instructions load or
  404. /// store the same value and NonLocal which indicate that non-local Def was
  405. /// found, which can be retrieved by calling getNonLocalPointerDependency
  406. /// with the same queried instruction.
  407. MemDepResult getInvariantGroupPointerDependency(LoadInst *LI, BasicBlock *BB);
  408. /// Release memory in caches.
  409. void releaseMemory();
  410. private:
  411. MemDepResult getCallDependencyFrom(CallBase *Call, bool isReadOnlyCall,
  412. BasicBlock::iterator ScanIt,
  413. BasicBlock *BB);
  414. bool getNonLocalPointerDepFromBB(Instruction *QueryInst,
  415. const PHITransAddr &Pointer,
  416. const MemoryLocation &Loc, bool isLoad,
  417. BasicBlock *BB,
  418. SmallVectorImpl<NonLocalDepResult> &Result,
  419. DenseMap<BasicBlock *, Value *> &Visited,
  420. bool SkipFirstBlock = false,
  421. bool IsIncomplete = false);
  422. MemDepResult GetNonLocalInfoForBlock(Instruction *QueryInst,
  423. const MemoryLocation &Loc, bool isLoad,
  424. BasicBlock *BB, NonLocalDepInfo *Cache,
  425. unsigned NumSortedEntries);
  426. void RemoveCachedNonLocalPointerDependencies(ValueIsLoadPair P);
  427. void verifyRemoved(Instruction *Inst) const;
  428. };
  429. /// An analysis that produces \c MemoryDependenceResults for a function.
  430. ///
  431. /// This is essentially a no-op because the results are computed entirely
  432. /// lazily.
  433. class MemoryDependenceAnalysis
  434. : public AnalysisInfoMixin<MemoryDependenceAnalysis> {
  435. friend AnalysisInfoMixin<MemoryDependenceAnalysis>;
  436. static AnalysisKey Key;
  437. unsigned DefaultBlockScanLimit;
  438. public:
  439. using Result = MemoryDependenceResults;
  440. MemoryDependenceAnalysis();
  441. MemoryDependenceAnalysis(unsigned DefaultBlockScanLimit) : DefaultBlockScanLimit(DefaultBlockScanLimit) { }
  442. MemoryDependenceResults run(Function &F, FunctionAnalysisManager &AM);
  443. };
  444. /// A wrapper analysis pass for the legacy pass manager that exposes a \c
  445. /// MemoryDepnedenceResults instance.
  446. class MemoryDependenceWrapperPass : public FunctionPass {
  447. Optional<MemoryDependenceResults> MemDep;
  448. public:
  449. static char ID;
  450. MemoryDependenceWrapperPass();
  451. ~MemoryDependenceWrapperPass() override;
  452. /// Pass Implementation stuff. This doesn't do any analysis eagerly.
  453. bool runOnFunction(Function &) override;
  454. /// Clean up memory in between runs
  455. void releaseMemory() override;
  456. /// Does not modify anything. It uses Value Numbering and Alias Analysis.
  457. void getAnalysisUsage(AnalysisUsage &AU) const override;
  458. MemoryDependenceResults &getMemDep() { return *MemDep; }
  459. };
  460. } // end namespace llvm
  461. #endif // LLVM_ANALYSIS_MEMORYDEPENDENCEANALYSIS_H
  462. #ifdef __GNUC__
  463. #pragma GCC diagnostic pop
  464. #endif