1 //===- llvm/Analysis/MemoryDependenceAnalysis.h - Memory Deps --*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines the MemoryDependenceAnalysis analysis pass.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_ANALYSIS_MEMORY_DEPENDENCE_H
15 #define LLVM_ANALYSIS_MEMORY_DEPENDENCE_H
17 #include "llvm/BasicBlock.h"
18 #include "llvm/Pass.h"
19 #include "llvm/ADT/DenseMap.h"
20 #include "llvm/ADT/SmallPtrSet.h"
21 #include "llvm/ADT/OwningPtr.h"
22 #include "llvm/ADT/PointerIntPair.h"
31 class MemoryDependenceAnalysis;
32 class PredIteratorCache;
34 /// MemDepResult - A memory dependence query can return one of three different
35 /// answers, described below.
38 /// Invalid - Clients of MemDep never see this.
41 /// Clobber - This is a dependence on the specified instruction which
42 /// clobbers the desired value. The pointer member of the MemDepResult
43 /// pair holds the instruction that clobbers the memory. For example,
44 /// this occurs when we see a may-aliased store to the memory location we
48 /// Def - This is a dependence on the specified instruction which
49 /// defines/produces the desired memory location. The pointer member of
50 /// the MemDepResult pair holds the instruction that defines the memory.
51 /// Cases of interest:
52 /// 1. This could be a load or store for dependence queries on
53 /// load/store. The value loaded or stored is the produced value.
54 /// Note that the pointer operand may be different than that of the
55 /// queried pointer due to must aliases and phi translation. Note
56 /// that the def may not be the same type as the query, the pointers
57 /// may just be must aliases.
58 /// 2. For loads and stores, this could be an allocation instruction. In
59 /// this case, the load is loading an undef value or a store is the
60 /// first store to (that part of) the allocation.
61 /// 3. Dependence queries on calls return Def only when they are
62 /// readonly calls with identical callees and no intervening
63 /// clobbers. No validation is done that the operands to the calls
67 /// NonLocal - This marker indicates that the query has no dependency in
68 /// the specified block. To find out more, the client should query other
69 /// predecessor blocks.
72 typedef PointerIntPair<Instruction*, 2, DepType> PairTy;
74 explicit MemDepResult(PairTy V) : Value(V) {}
76 MemDepResult() : Value(0, Invalid) {}
78 /// get methods: These are static ctor methods for creating various
79 /// MemDepResult kinds.
80 static MemDepResult getDef(Instruction *Inst) {
81 return MemDepResult(PairTy(Inst, Def));
83 static MemDepResult getClobber(Instruction *Inst) {
84 return MemDepResult(PairTy(Inst, Clobber));
86 static MemDepResult getNonLocal() {
87 return MemDepResult(PairTy(0, NonLocal));
90 /// isClobber - Return true if this MemDepResult represents a query that is
91 /// a instruction clobber dependency.
92 bool isClobber() const { return Value.getInt() == Clobber; }
94 /// isDef - Return true if this MemDepResult represents a query that is
95 /// a instruction definition dependency.
96 bool isDef() const { return Value.getInt() == Def; }
98 /// isNonLocal - Return true if this MemDepResult represents a query that
99 /// is transparent to the start of the block, but where a non-local hasn't
101 bool isNonLocal() const { return Value.getInt() == NonLocal; }
103 /// getInst() - If this is a normal dependency, return the instruction that
104 /// is depended on. Otherwise, return null.
105 Instruction *getInst() const { return Value.getPointer(); }
107 bool operator==(const MemDepResult &M) const { return Value == M.Value; }
108 bool operator!=(const MemDepResult &M) const { return Value != M.Value; }
109 bool operator<(const MemDepResult &M) const { return Value < M.Value; }
110 bool operator>(const MemDepResult &M) const { return Value > M.Value; }
112 friend class MemoryDependenceAnalysis;
113 /// Dirty - Entries with this marker occur in a LocalDeps map or
114 /// NonLocalDeps map when the instruction they previously referenced was
115 /// removed from MemDep. In either case, the entry may include an
116 /// instruction pointer. If so, the pointer is an instruction in the
117 /// block where scanning can start from, saving some work.
119 /// In a default-constructed MemDepResult object, the type will be Dirty
120 /// and the instruction pointer will be null.
123 /// isDirty - Return true if this is a MemDepResult in its dirty/invalid.
125 bool isDirty() const { return Value.getInt() == Invalid; }
127 static MemDepResult getDirty(Instruction *Inst) {
128 return MemDepResult(PairTy(Inst, Invalid));
132 /// MemoryDependenceAnalysis - This is an analysis that determines, for a
133 /// given memory operation, what preceding memory operations it depends on.
134 /// It builds on alias analysis information, and tries to provide a lazy,
135 /// caching interface to a common kind of alias information query.
137 /// The dependency information returned is somewhat unusual, but is pragmatic.
138 /// If queried about a store or call that might modify memory, the analysis
139 /// will return the instruction[s] that may either load from that memory or
140 /// store to it. If queried with a load or call that can never modify memory,
141 /// the analysis will return calls and stores that might modify the pointer,
142 /// but generally does not return loads unless a) they are volatile, or
143 /// b) they load from *must-aliased* pointers. Returning a dependence on
144 /// must-alias'd pointers instead of all pointers interacts well with the
145 /// internal caching mechanism.
147 class MemoryDependenceAnalysis : public FunctionPass {
148 // A map from instructions to their dependency.
149 typedef DenseMap<Instruction*, MemDepResult> LocalDepMapType;
150 LocalDepMapType LocalDeps;
153 typedef std::pair<BasicBlock*, MemDepResult> NonLocalDepEntry;
154 typedef std::vector<NonLocalDepEntry> NonLocalDepInfo;
156 /// ValueIsLoadPair - This is a pair<Value*, bool> where the bool is true if
157 /// the dependence is a read only dependence, false if read/write.
158 typedef PointerIntPair<Value*, 1, bool> ValueIsLoadPair;
160 /// BBSkipFirstBlockPair - This pair is used when caching information for a
161 /// block. If the pointer is null, the cache value is not a full query that
162 /// starts at the specified block. If non-null, the bool indicates whether
163 /// or not the contents of the block was skipped.
164 typedef PointerIntPair<BasicBlock*, 1, bool> BBSkipFirstBlockPair;
166 /// CachedNonLocalPointerInfo - This map stores the cached results of doing
167 /// a pointer lookup at the bottom of a block. The key of this map is the
168 /// pointer+isload bit, the value is a list of <bb->result> mappings.
169 typedef DenseMap<ValueIsLoadPair, std::pair<BBSkipFirstBlockPair,
170 NonLocalDepInfo> > CachedNonLocalPointerInfo;
171 CachedNonLocalPointerInfo NonLocalPointerDeps;
173 // A map from instructions to their non-local pointer dependencies.
174 typedef DenseMap<Instruction*,
175 SmallPtrSet<ValueIsLoadPair, 4> > ReverseNonLocalPtrDepTy;
176 ReverseNonLocalPtrDepTy ReverseNonLocalPtrDeps;
179 /// PerInstNLInfo - This is the instruction we keep for each cached access
180 /// that we have for an instruction. The pointer is an owning pointer and
181 /// the bool indicates whether we have any dirty bits in the set.
182 typedef std::pair<NonLocalDepInfo, bool> PerInstNLInfo;
184 // A map from instructions to their non-local dependencies.
185 typedef DenseMap<Instruction*, PerInstNLInfo> NonLocalDepMapType;
187 NonLocalDepMapType NonLocalDeps;
189 // A reverse mapping from dependencies to the dependees. This is
190 // used when removing instructions to keep the cache coherent.
191 typedef DenseMap<Instruction*,
192 SmallPtrSet<Instruction*, 4> > ReverseDepMapType;
193 ReverseDepMapType ReverseLocalDeps;
195 // A reverse mapping form dependencies to the non-local dependees.
196 ReverseDepMapType ReverseNonLocalDeps;
198 /// Current AA implementation, just a cache.
201 OwningPtr<PredIteratorCache> PredCache;
203 MemoryDependenceAnalysis();
204 ~MemoryDependenceAnalysis();
207 /// Pass Implementation stuff. This doesn't do any analysis eagerly.
208 bool runOnFunction(Function &);
210 /// Clean up memory in between runs
211 void releaseMemory();
213 /// getAnalysisUsage - Does not modify anything. It uses Value Numbering
214 /// and Alias Analysis.
216 virtual void getAnalysisUsage(AnalysisUsage &AU) const;
218 /// getDependency - Return the instruction on which a memory operation
219 /// depends. See the class comment for more details. It is illegal to call
220 /// this on non-memory instructions.
221 MemDepResult getDependency(Instruction *QueryInst);
223 /// getNonLocalCallDependency - Perform a full dependency query for the
224 /// specified call, returning the set of blocks that the value is
225 /// potentially live across. The returned set of results will include a
226 /// "NonLocal" result for all blocks where the value is live across.
228 /// This method assumes the instruction returns a "NonLocal" dependency
229 /// within its own block.
231 /// This returns a reference to an internal data structure that may be
232 /// invalidated on the next non-local query or when an instruction is
233 /// removed. Clients must copy this data if they want it around longer than
235 const NonLocalDepInfo &getNonLocalCallDependency(CallSite QueryCS);
238 /// getNonLocalPointerDependency - Perform a full dependency query for an
239 /// access to the specified (non-volatile) memory location, returning the
240 /// set of instructions that either define or clobber the value.
242 /// This method assumes the pointer has a "NonLocal" dependency within BB.
243 void getNonLocalPointerDependency(Value *Pointer, bool isLoad,
245 SmallVectorImpl<NonLocalDepEntry> &Result);
247 /// removeInstruction - Remove an instruction from the dependence analysis,
248 /// updating the dependence of instructions that previously depended on it.
249 void removeInstruction(Instruction *InstToRemove);
251 /// invalidateCachedPointerInfo - This method is used to invalidate cached
252 /// information about the specified pointer, because it may be too
253 /// conservative in memdep. This is an optional call that can be used when
254 /// the client detects an equivalence between the pointer and some other
255 /// value and replaces the other value with ptr. This can make Ptr available
256 /// in more places that cached info does not necessarily keep.
257 void invalidateCachedPointerInfo(Value *Ptr);
260 MemDepResult getPointerDependencyFrom(Value *Pointer, uint64_t MemSize,
262 BasicBlock::iterator ScanIt,
264 MemDepResult getCallSiteDependencyFrom(CallSite C, bool isReadOnlyCall,
265 BasicBlock::iterator ScanIt,
267 bool getNonLocalPointerDepFromBB(Value *Pointer, uint64_t Size,
268 bool isLoad, BasicBlock *BB,
269 SmallVectorImpl<NonLocalDepEntry> &Result,
270 DenseMap<BasicBlock*, Value*> &Visited,
271 bool SkipFirstBlock = false);
272 MemDepResult GetNonLocalInfoForBlock(Value *Pointer, uint64_t PointeeSize,
273 bool isLoad, BasicBlock *BB,
274 NonLocalDepInfo *Cache,
275 unsigned NumSortedEntries);
277 void RemoveCachedNonLocalPointerDependencies(ValueIsLoadPair P);
279 /// verifyRemoved - Verify that the specified instruction does not occur
280 /// in our internal data structures.
281 void verifyRemoved(Instruction *Inst) const;
285 } // End llvm namespace