Counter Strike : Global Offensive Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

445 lines
19 KiB

  1. //===- llvm/Analysis/MemoryDependenceAnalysis.h - Memory Deps --*- 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 MemoryDependenceAnalysis analysis pass.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #ifndef LLVM_ANALYSIS_MEMORYDEPENDENCEANALYSIS_H
  14. #define LLVM_ANALYSIS_MEMORYDEPENDENCEANALYSIS_H
  15. #include "llvm/ADT/DenseMap.h"
  16. #include "llvm/ADT/OwningPtr.h"
  17. #include "llvm/ADT/PointerIntPair.h"
  18. #include "llvm/ADT/SmallPtrSet.h"
  19. #include "llvm/Analysis/AliasAnalysis.h"
  20. #include "llvm/IR/BasicBlock.h"
  21. #include "llvm/Pass.h"
  22. #include "llvm/Support/ValueHandle.h"
  23. namespace llvm {
  24. class Function;
  25. class FunctionPass;
  26. class Instruction;
  27. class CallSite;
  28. class AliasAnalysis;
  29. class DataLayout;
  30. class MemoryDependenceAnalysis;
  31. class PredIteratorCache;
  32. class DominatorTree;
  33. class PHITransAddr;
  34. /// MemDepResult - A memory dependence query can return one of three different
  35. /// answers, described below.
  36. class MemDepResult {
  37. enum DepType {
  38. /// Invalid - Clients of MemDep never see this.
  39. Invalid = 0,
  40. /// Clobber - This is a dependence on the specified instruction which
  41. /// clobbers the desired value. The pointer member of the MemDepResult
  42. /// pair holds the instruction that clobbers the memory. For example,
  43. /// this occurs when we see a may-aliased store to the memory location we
  44. /// care about.
  45. ///
  46. /// There are several cases that may be interesting here:
  47. /// 1. Loads are clobbered by may-alias stores.
  48. /// 2. Loads are considered clobbered by partially-aliased loads. The
  49. /// client may choose to analyze deeper into these cases.
  50. Clobber,
  51. /// Def - This is a dependence on the specified instruction which
  52. /// defines/produces the desired memory location. The pointer member of
  53. /// the MemDepResult pair holds the instruction that defines the memory.
  54. /// Cases of interest:
  55. /// 1. This could be a load or store for dependence queries on
  56. /// load/store. The value loaded or stored is the produced value.
  57. /// Note that the pointer operand may be different than that of the
  58. /// queried pointer due to must aliases and phi translation. Note
  59. /// that the def may not be the same type as the query, the pointers
  60. /// may just be must aliases.
  61. /// 2. For loads and stores, this could be an allocation instruction. In
  62. /// this case, the load is loading an undef value or a store is the
  63. /// first store to (that part of) the allocation.
  64. /// 3. Dependence queries on calls return Def only when they are
  65. /// readonly calls or memory use intrinsics with identical callees
  66. /// and no intervening clobbers. No validation is done that the
  67. /// operands to the calls are the same.
  68. Def,
  69. /// Other - This marker indicates that the query has no known dependency
  70. /// in the specified block. More detailed state info is encoded in the
  71. /// upper part of the pair (i.e. the Instruction*)
  72. Other
  73. };
  74. /// If DepType is "Other", the upper part of the pair
  75. /// (i.e. the Instruction* part) is instead used to encode more detailed
  76. /// type information as follows
  77. enum OtherType {
  78. /// NonLocal - This marker indicates that the query has no dependency in
  79. /// the specified block. To find out more, the client should query other
  80. /// predecessor blocks.
  81. NonLocal = 0x4,
  82. /// NonFuncLocal - This marker indicates that the query has no
  83. /// dependency in the specified function.
  84. NonFuncLocal = 0x8,
  85. /// Unknown - This marker indicates that the query dependency
  86. /// is unknown.
  87. Unknown = 0xc
  88. };
  89. typedef PointerIntPair<Instruction*, 2, DepType> PairTy;
  90. PairTy Value;
  91. explicit MemDepResult(PairTy V) : Value(V) {}
  92. public:
  93. MemDepResult() : Value(0, Invalid) {}
  94. /// get methods: These are static ctor methods for creating various
  95. /// MemDepResult kinds.
  96. static MemDepResult getDef(Instruction *Inst) {
  97. assert(Inst && "Def requires inst");
  98. return MemDepResult(PairTy(Inst, Def));
  99. }
  100. static MemDepResult getClobber(Instruction *Inst) {
  101. assert(Inst && "Clobber requires inst");
  102. return MemDepResult(PairTy(Inst, Clobber));
  103. }
  104. static MemDepResult getNonLocal() {
  105. return MemDepResult(
  106. PairTy(reinterpret_cast<Instruction*>(NonLocal), Other));
  107. }
  108. static MemDepResult getNonFuncLocal() {
  109. return MemDepResult(
  110. PairTy(reinterpret_cast<Instruction*>(NonFuncLocal), Other));
  111. }
  112. static MemDepResult getUnknown() {
  113. return MemDepResult(
  114. PairTy(reinterpret_cast<Instruction*>(Unknown), Other));
  115. }
  116. /// isClobber - Return true if this MemDepResult represents a query that is
  117. /// an instruction clobber dependency.
  118. bool isClobber() const { return Value.getInt() == Clobber; }
  119. /// isDef - Return true if this MemDepResult represents a query that is
  120. /// an instruction definition dependency.
  121. bool isDef() const { return Value.getInt() == Def; }
  122. /// isNonLocal - Return true if this MemDepResult represents a query that
  123. /// is transparent to the start of the block, but where a non-local hasn't
  124. /// been done.
  125. bool isNonLocal() const {
  126. return Value.getInt() == Other
  127. && Value.getPointer() == reinterpret_cast<Instruction*>(NonLocal);
  128. }
  129. /// isNonFuncLocal - Return true if this MemDepResult represents a query
  130. /// that is transparent to the start of the function.
  131. bool isNonFuncLocal() const {
  132. return Value.getInt() == Other
  133. && Value.getPointer() == reinterpret_cast<Instruction*>(NonFuncLocal);
  134. }
  135. /// isUnknown - Return true if this MemDepResult represents a query which
  136. /// cannot and/or will not be computed.
  137. bool isUnknown() const {
  138. return Value.getInt() == Other
  139. && Value.getPointer() == reinterpret_cast<Instruction*>(Unknown);
  140. }
  141. /// getInst() - If this is a normal dependency, return the instruction that
  142. /// is depended on. Otherwise, return null.
  143. Instruction *getInst() const {
  144. if (Value.getInt() == Other) return NULL;
  145. return Value.getPointer();
  146. }
  147. bool operator==(const MemDepResult &M) const { return Value == M.Value; }
  148. bool operator!=(const MemDepResult &M) const { return Value != M.Value; }
  149. bool operator<(const MemDepResult &M) const { return Value < M.Value; }
  150. bool operator>(const MemDepResult &M) const { return Value > M.Value; }
  151. private:
  152. friend class MemoryDependenceAnalysis;
  153. /// Dirty - Entries with this marker occur in a LocalDeps map or
  154. /// NonLocalDeps map when the instruction they previously referenced was
  155. /// removed from MemDep. In either case, the entry may include an
  156. /// instruction pointer. If so, the pointer is an instruction in the
  157. /// block where scanning can start from, saving some work.
  158. ///
  159. /// In a default-constructed MemDepResult object, the type will be Dirty
  160. /// and the instruction pointer will be null.
  161. ///
  162. /// isDirty - Return true if this is a MemDepResult in its dirty/invalid.
  163. /// state.
  164. bool isDirty() const { return Value.getInt() == Invalid; }
  165. static MemDepResult getDirty(Instruction *Inst) {
  166. return MemDepResult(PairTy(Inst, Invalid));
  167. }
  168. };
  169. /// NonLocalDepEntry - This is an entry in the NonLocalDepInfo cache. For
  170. /// each BasicBlock (the BB entry) it keeps a MemDepResult.
  171. class NonLocalDepEntry {
  172. BasicBlock *BB;
  173. MemDepResult Result;
  174. public:
  175. NonLocalDepEntry(BasicBlock *bb, MemDepResult result)
  176. : BB(bb), Result(result) {}
  177. // This is used for searches.
  178. NonLocalDepEntry(BasicBlock *bb) : BB(bb) {}
  179. // BB is the sort key, it can't be changed.
  180. BasicBlock *getBB() const { return BB; }
  181. void setResult(const MemDepResult &R) { Result = R; }
  182. const MemDepResult &getResult() const { return Result; }
  183. bool operator<(const NonLocalDepEntry &RHS) const {
  184. return BB < RHS.BB;
  185. }
  186. };
  187. /// NonLocalDepResult - This is a result from a NonLocal dependence query.
  188. /// For each BasicBlock (the BB entry) it keeps a MemDepResult and the
  189. /// (potentially phi translated) address that was live in the block.
  190. class NonLocalDepResult {
  191. NonLocalDepEntry Entry;
  192. Value *Address;
  193. public:
  194. NonLocalDepResult(BasicBlock *bb, MemDepResult result, Value *address)
  195. : Entry(bb, result), Address(address) {}
  196. // BB is the sort key, it can't be changed.
  197. BasicBlock *getBB() const { return Entry.getBB(); }
  198. void setResult(const MemDepResult &R, Value *Addr) {
  199. Entry.setResult(R);
  200. Address = Addr;
  201. }
  202. const MemDepResult &getResult() const { return Entry.getResult(); }
  203. /// getAddress - Return the address of this pointer in this block. This can
  204. /// be different than the address queried for the non-local result because
  205. /// of phi translation. This returns null if the address was not available
  206. /// in a block (i.e. because phi translation failed) or if this is a cached
  207. /// result and that address was deleted.
  208. ///
  209. /// The address is always null for a non-local 'call' dependence.
  210. Value *getAddress() const { return Address; }
  211. };
  212. /// MemoryDependenceAnalysis - This is an analysis that determines, for a
  213. /// given memory operation, what preceding memory operations it depends on.
  214. /// It builds on alias analysis information, and tries to provide a lazy,
  215. /// caching interface to a common kind of alias information query.
  216. ///
  217. /// The dependency information returned is somewhat unusual, but is pragmatic.
  218. /// If queried about a store or call that might modify memory, the analysis
  219. /// will return the instruction[s] that may either load from that memory or
  220. /// store to it. If queried with a load or call that can never modify memory,
  221. /// the analysis will return calls and stores that might modify the pointer,
  222. /// but generally does not return loads unless a) they are volatile, or
  223. /// b) they load from *must-aliased* pointers. Returning a dependence on
  224. /// must-alias'd pointers instead of all pointers interacts well with the
  225. /// internal caching mechanism.
  226. ///
  227. class MemoryDependenceAnalysis : public FunctionPass {
  228. // A map from instructions to their dependency.
  229. typedef DenseMap<Instruction*, MemDepResult> LocalDepMapType;
  230. LocalDepMapType LocalDeps;
  231. public:
  232. typedef std::vector<NonLocalDepEntry> NonLocalDepInfo;
  233. private:
  234. /// ValueIsLoadPair - This is a pair<Value*, bool> where the bool is true if
  235. /// the dependence is a read only dependence, false if read/write.
  236. typedef PointerIntPair<const Value*, 1, bool> ValueIsLoadPair;
  237. /// BBSkipFirstBlockPair - This pair is used when caching information for a
  238. /// block. If the pointer is null, the cache value is not a full query that
  239. /// starts at the specified block. If non-null, the bool indicates whether
  240. /// or not the contents of the block was skipped.
  241. typedef PointerIntPair<BasicBlock*, 1, bool> BBSkipFirstBlockPair;
  242. /// NonLocalPointerInfo - This record is the information kept for each
  243. /// (value, is load) pair.
  244. struct NonLocalPointerInfo {
  245. /// Pair - The pair of the block and the skip-first-block flag.
  246. BBSkipFirstBlockPair Pair;
  247. /// NonLocalDeps - The results of the query for each relevant block.
  248. NonLocalDepInfo NonLocalDeps;
  249. /// Size - The maximum size of the dereferences of the
  250. /// pointer. May be UnknownSize if the sizes are unknown.
  251. uint64_t Size;
  252. /// TBAATag - The TBAA tag associated with dereferences of the
  253. /// pointer. May be null if there are no tags or conflicting tags.
  254. const MDNode *TBAATag;
  255. NonLocalPointerInfo() : Size(AliasAnalysis::UnknownSize), TBAATag(0) {}
  256. };
  257. /// CachedNonLocalPointerInfo - This map stores the cached results of doing
  258. /// a pointer lookup at the bottom of a block. The key of this map is the
  259. /// pointer+isload bit, the value is a list of <bb->result> mappings.
  260. typedef DenseMap<ValueIsLoadPair,
  261. NonLocalPointerInfo> CachedNonLocalPointerInfo;
  262. CachedNonLocalPointerInfo NonLocalPointerDeps;
  263. // A map from instructions to their non-local pointer dependencies.
  264. typedef DenseMap<Instruction*,
  265. SmallPtrSet<ValueIsLoadPair, 4> > ReverseNonLocalPtrDepTy;
  266. ReverseNonLocalPtrDepTy ReverseNonLocalPtrDeps;
  267. /// PerInstNLInfo - This is the instruction we keep for each cached access
  268. /// that we have for an instruction. The pointer is an owning pointer and
  269. /// the bool indicates whether we have any dirty bits in the set.
  270. typedef std::pair<NonLocalDepInfo, bool> PerInstNLInfo;
  271. // A map from instructions to their non-local dependencies.
  272. typedef DenseMap<Instruction*, PerInstNLInfo> NonLocalDepMapType;
  273. NonLocalDepMapType NonLocalDeps;
  274. // A reverse mapping from dependencies to the dependees. This is
  275. // used when removing instructions to keep the cache coherent.
  276. typedef DenseMap<Instruction*,
  277. SmallPtrSet<Instruction*, 4> > ReverseDepMapType;
  278. ReverseDepMapType ReverseLocalDeps;
  279. // A reverse mapping from dependencies to the non-local dependees.
  280. ReverseDepMapType ReverseNonLocalDeps;
  281. /// Current AA implementation, just a cache.
  282. AliasAnalysis *AA;
  283. DataLayout *TD;
  284. DominatorTree *DT;
  285. OwningPtr<PredIteratorCache> PredCache;
  286. public:
  287. MemoryDependenceAnalysis();
  288. ~MemoryDependenceAnalysis();
  289. static char ID;
  290. /// Pass Implementation stuff. This doesn't do any analysis eagerly.
  291. bool runOnFunction(Function &);
  292. /// Clean up memory in between runs
  293. void releaseMemory();
  294. /// getAnalysisUsage - Does not modify anything. It uses Value Numbering
  295. /// and Alias Analysis.
  296. ///
  297. virtual void getAnalysisUsage(AnalysisUsage &AU) const;
  298. /// getDependency - Return the instruction on which a memory operation
  299. /// depends. See the class comment for more details. It is illegal to call
  300. /// this on non-memory instructions.
  301. MemDepResult getDependency(Instruction *QueryInst);
  302. /// getNonLocalCallDependency - Perform a full dependency query for the
  303. /// specified call, returning the set of blocks that the value is
  304. /// potentially live across. The returned set of results will include a
  305. /// "NonLocal" result for all blocks where the value is live across.
  306. ///
  307. /// This method assumes the instruction returns a "NonLocal" dependency
  308. /// within its own block.
  309. ///
  310. /// This returns a reference to an internal data structure that may be
  311. /// invalidated on the next non-local query or when an instruction is
  312. /// removed. Clients must copy this data if they want it around longer than
  313. /// that.
  314. const NonLocalDepInfo &getNonLocalCallDependency(CallSite QueryCS);
  315. /// getNonLocalPointerDependency - Perform a full dependency query for an
  316. /// access to the specified (non-volatile) memory location, returning the
  317. /// set of instructions that either define or clobber the value.
  318. ///
  319. /// This method assumes the pointer has a "NonLocal" dependency within BB.
  320. void getNonLocalPointerDependency(const AliasAnalysis::Location &Loc,
  321. bool isLoad, BasicBlock *BB,
  322. SmallVectorImpl<NonLocalDepResult> &Result);
  323. /// removeInstruction - Remove an instruction from the dependence analysis,
  324. /// updating the dependence of instructions that previously depended on it.
  325. void removeInstruction(Instruction *InstToRemove);
  326. /// invalidateCachedPointerInfo - This method is used to invalidate cached
  327. /// information about the specified pointer, because it may be too
  328. /// conservative in memdep. This is an optional call that can be used when
  329. /// the client detects an equivalence between the pointer and some other
  330. /// value and replaces the other value with ptr. This can make Ptr available
  331. /// in more places that cached info does not necessarily keep.
  332. void invalidateCachedPointerInfo(Value *Ptr);
  333. /// invalidateCachedPredecessors - Clear the PredIteratorCache info.
  334. /// This needs to be done when the CFG changes, e.g., due to splitting
  335. /// critical edges.
  336. void invalidateCachedPredecessors();
  337. /// getPointerDependencyFrom - Return the instruction on which a memory
  338. /// location depends. If isLoad is true, this routine ignores may-aliases
  339. /// with read-only operations. If isLoad is false, this routine ignores
  340. /// may-aliases with reads from read-only locations. If possible, pass
  341. /// the query instruction as well; this function may take advantage of
  342. /// the metadata annotated to the query instruction to refine the result.
  343. ///
  344. /// Note that this is an uncached query, and thus may be inefficient.
  345. ///
  346. MemDepResult getPointerDependencyFrom(const AliasAnalysis::Location &Loc,
  347. bool isLoad,
  348. BasicBlock::iterator ScanIt,
  349. BasicBlock *BB,
  350. Instruction *QueryInst = 0);
  351. /// getLoadLoadClobberFullWidthSize - This is a little bit of analysis that
  352. /// looks at a memory location for a load (specified by MemLocBase, Offs,
  353. /// and Size) and compares it against a load. If the specified load could
  354. /// be safely widened to a larger integer load that is 1) still efficient,
  355. /// 2) safe for the target, and 3) would provide the specified memory
  356. /// location value, then this function returns the size in bytes of the
  357. /// load width to use. If not, this returns zero.
  358. static unsigned getLoadLoadClobberFullWidthSize(const Value *MemLocBase,
  359. int64_t MemLocOffs,
  360. unsigned MemLocSize,
  361. const LoadInst *LI,
  362. const DataLayout &TD);
  363. private:
  364. MemDepResult getCallSiteDependencyFrom(CallSite C, bool isReadOnlyCall,
  365. BasicBlock::iterator ScanIt,
  366. BasicBlock *BB);
  367. bool getNonLocalPointerDepFromBB(const PHITransAddr &Pointer,
  368. const AliasAnalysis::Location &Loc,
  369. bool isLoad, BasicBlock *BB,
  370. SmallVectorImpl<NonLocalDepResult> &Result,
  371. DenseMap<BasicBlock*, Value*> &Visited,
  372. bool SkipFirstBlock = false);
  373. MemDepResult GetNonLocalInfoForBlock(const AliasAnalysis::Location &Loc,
  374. bool isLoad, BasicBlock *BB,
  375. NonLocalDepInfo *Cache,
  376. unsigned NumSortedEntries);
  377. void RemoveCachedNonLocalPointerDependencies(ValueIsLoadPair P);
  378. /// verifyRemoved - Verify that the specified instruction does not occur
  379. /// in our internal data structures.
  380. void verifyRemoved(Instruction *Inst) const;
  381. };
  382. } // End llvm namespace
  383. #endif