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/Support/ValueHandle.h"
20 #include "llvm/Analysis/AliasAnalysis.h"
21 #include "llvm/ADT/DenseMap.h"
22 #include "llvm/ADT/SmallPtrSet.h"
23 #include "llvm/ADT/OwningPtr.h"
24 #include "llvm/ADT/PointerIntPair.h"
33 class MemoryDependenceAnalysis;
34 class PredIteratorCache;
38 /// MemDepResult - A memory dependence query can return one of three different
39 /// answers, described below.
42 /// Invalid - Clients of MemDep never see this.
45 /// Clobber - This is a dependence on the specified instruction which
46 /// clobbers the desired value. The pointer member of the MemDepResult
47 /// pair holds the instruction that clobbers the memory. For example,
48 /// this occurs when we see a may-aliased store to the memory location we
51 /// There are several cases that may be interesting here:
52 /// 1. Loads are clobbered by may-alias stores.
53 /// 2. Loads are considered clobbered by partially-aliased loads. The
54 /// client may choose to analyze deeper into these cases.
56 /// A dependence query on the first instruction of the entry block will
57 /// return a clobber(self) result.
60 /// Def - This is a dependence on the specified instruction which
61 /// defines/produces the desired memory location. The pointer member of
62 /// the MemDepResult pair holds the instruction that defines the memory.
63 /// Cases of interest:
64 /// 1. This could be a load or store for dependence queries on
65 /// load/store. The value loaded or stored is the produced value.
66 /// Note that the pointer operand may be different than that of the
67 /// queried pointer due to must aliases and phi translation. Note
68 /// that the def may not be the same type as the query, the pointers
69 /// may just be must aliases.
70 /// 2. For loads and stores, this could be an allocation instruction. In
71 /// this case, the load is loading an undef value or a store is the
72 /// first store to (that part of) the allocation.
73 /// 3. Dependence queries on calls return Def only when they are
74 /// readonly calls or memory use intrinsics with identical callees
75 /// and no intervening clobbers. No validation is done that the
76 /// operands to the calls are the same.
79 /// NonLocal - This marker indicates that the query has no dependency in
80 /// the specified block. To find out more, the client should query other
81 /// predecessor blocks.
84 typedef PointerIntPair<Instruction*, 2, DepType> PairTy;
86 explicit MemDepResult(PairTy V) : Value(V) {}
88 MemDepResult() : Value(0, Invalid) {}
90 /// get methods: These are static ctor methods for creating various
91 /// MemDepResult kinds.
92 static MemDepResult getDef(Instruction *Inst) {
93 return MemDepResult(PairTy(Inst, Def));
95 static MemDepResult getClobber(Instruction *Inst) {
96 return MemDepResult(PairTy(Inst, Clobber));
98 static MemDepResult getNonLocal() {
99 return MemDepResult(PairTy(0, NonLocal));
102 /// isClobber - Return true if this MemDepResult represents a query that is
103 /// a instruction clobber dependency.
104 bool isClobber() const { return Value.getInt() == Clobber; }
106 /// isDef - Return true if this MemDepResult represents a query that is
107 /// a instruction definition dependency.
108 bool isDef() const { return Value.getInt() == Def; }
110 /// isNonLocal - Return true if this MemDepResult represents a query that
111 /// is transparent to the start of the block, but where a non-local hasn't
113 bool isNonLocal() const { return Value.getInt() == NonLocal; }
115 /// getInst() - If this is a normal dependency, return the instruction that
116 /// is depended on. Otherwise, return null.
117 Instruction *getInst() const { return Value.getPointer(); }
119 bool operator==(const MemDepResult &M) const { return Value == M.Value; }
120 bool operator!=(const MemDepResult &M) const { return Value != M.Value; }
121 bool operator<(const MemDepResult &M) const { return Value < M.Value; }
122 bool operator>(const MemDepResult &M) const { return Value > M.Value; }
124 friend class MemoryDependenceAnalysis;
125 /// Dirty - Entries with this marker occur in a LocalDeps map or
126 /// NonLocalDeps map when the instruction they previously referenced was
127 /// removed from MemDep. In either case, the entry may include an
128 /// instruction pointer. If so, the pointer is an instruction in the
129 /// block where scanning can start from, saving some work.
131 /// In a default-constructed MemDepResult object, the type will be Dirty
132 /// and the instruction pointer will be null.
135 /// isDirty - Return true if this is a MemDepResult in its dirty/invalid.
137 bool isDirty() const { return Value.getInt() == Invalid; }
139 static MemDepResult getDirty(Instruction *Inst) {
140 return MemDepResult(PairTy(Inst, Invalid));
144 /// NonLocalDepEntry - This is an entry in the NonLocalDepInfo cache. For
145 /// each BasicBlock (the BB entry) it keeps a MemDepResult.
146 class NonLocalDepEntry {
150 NonLocalDepEntry(BasicBlock *bb, MemDepResult result)
151 : BB(bb), Result(result) {}
153 // This is used for searches.
154 NonLocalDepEntry(BasicBlock *bb) : BB(bb) {}
156 // BB is the sort key, it can't be changed.
157 BasicBlock *getBB() const { return BB; }
159 void setResult(const MemDepResult &R) { Result = R; }
161 const MemDepResult &getResult() const { return Result; }
163 bool operator<(const NonLocalDepEntry &RHS) const {
168 /// NonLocalDepResult - This is a result from a NonLocal dependence query.
169 /// For each BasicBlock (the BB entry) it keeps a MemDepResult and the
170 /// (potentially phi translated) address that was live in the block.
171 class NonLocalDepResult {
172 NonLocalDepEntry Entry;
175 NonLocalDepResult(BasicBlock *bb, MemDepResult result, Value *address)
176 : Entry(bb, result), Address(address) {}
178 // BB is the sort key, it can't be changed.
179 BasicBlock *getBB() const { return Entry.getBB(); }
181 void setResult(const MemDepResult &R, Value *Addr) {
186 const MemDepResult &getResult() const { return Entry.getResult(); }
188 /// getAddress - Return the address of this pointer in this block. This can
189 /// be different than the address queried for the non-local result because
190 /// of phi translation. This returns null if the address was not available
191 /// in a block (i.e. because phi translation failed) or if this is a cached
192 /// result and that address was deleted.
194 /// The address is always null for a non-local 'call' dependence.
195 Value *getAddress() const { return Address; }
198 /// MemoryDependenceAnalysis - This is an analysis that determines, for a
199 /// given memory operation, what preceding memory operations it depends on.
200 /// It builds on alias analysis information, and tries to provide a lazy,
201 /// caching interface to a common kind of alias information query.
203 /// The dependency information returned is somewhat unusual, but is pragmatic.
204 /// If queried about a store or call that might modify memory, the analysis
205 /// will return the instruction[s] that may either load from that memory or
206 /// store to it. If queried with a load or call that can never modify memory,
207 /// the analysis will return calls and stores that might modify the pointer,
208 /// but generally does not return loads unless a) they are volatile, or
209 /// b) they load from *must-aliased* pointers. Returning a dependence on
210 /// must-alias'd pointers instead of all pointers interacts well with the
211 /// internal caching mechanism.
213 class MemoryDependenceAnalysis : public FunctionPass {
214 // A map from instructions to their dependency.
215 typedef DenseMap<Instruction*, MemDepResult> LocalDepMapType;
216 LocalDepMapType LocalDeps;
219 typedef std::vector<NonLocalDepEntry> NonLocalDepInfo;
221 /// ValueIsLoadPair - This is a pair<Value*, bool> where the bool is true if
222 /// the dependence is a read only dependence, false if read/write.
223 typedef PointerIntPair<const Value*, 1, bool> ValueIsLoadPair;
225 /// BBSkipFirstBlockPair - This pair is used when caching information for a
226 /// block. If the pointer is null, the cache value is not a full query that
227 /// starts at the specified block. If non-null, the bool indicates whether
228 /// or not the contents of the block was skipped.
229 typedef PointerIntPair<BasicBlock*, 1, bool> BBSkipFirstBlockPair;
231 /// NonLocalPointerInfo - This record is the information kept for each
232 /// (value, is load) pair.
233 struct NonLocalPointerInfo {
234 /// Pair - The pair of the block and the skip-first-block flag.
235 BBSkipFirstBlockPair Pair;
236 /// NonLocalDeps - The results of the query for each relevant block.
237 NonLocalDepInfo NonLocalDeps;
238 /// Size - The maximum size of the dereferences of the
239 /// pointer. May be UnknownSize if the sizes are unknown.
241 /// TBAATag - The TBAA tag associated with dereferences of the
242 /// pointer. May be null if there are no tags or conflicting tags.
243 const MDNode *TBAATag;
245 NonLocalPointerInfo() : Size(AliasAnalysis::UnknownSize), TBAATag(0) {}
248 /// CachedNonLocalPointerInfo - This map stores the cached results of doing
249 /// a pointer lookup at the bottom of a block. The key of this map is the
250 /// pointer+isload bit, the value is a list of <bb->result> mappings.
251 typedef DenseMap<ValueIsLoadPair,
252 NonLocalPointerInfo> CachedNonLocalPointerInfo;
253 CachedNonLocalPointerInfo NonLocalPointerDeps;
255 // A map from instructions to their non-local pointer dependencies.
256 typedef DenseMap<Instruction*,
257 SmallPtrSet<ValueIsLoadPair, 4> > ReverseNonLocalPtrDepTy;
258 ReverseNonLocalPtrDepTy ReverseNonLocalPtrDeps;
261 /// PerInstNLInfo - This is the instruction we keep for each cached access
262 /// that we have for an instruction. The pointer is an owning pointer and
263 /// the bool indicates whether we have any dirty bits in the set.
264 typedef std::pair<NonLocalDepInfo, bool> PerInstNLInfo;
266 // A map from instructions to their non-local dependencies.
267 typedef DenseMap<Instruction*, PerInstNLInfo> NonLocalDepMapType;
269 NonLocalDepMapType NonLocalDeps;
271 // A reverse mapping from dependencies to the dependees. This is
272 // used when removing instructions to keep the cache coherent.
273 typedef DenseMap<Instruction*,
274 SmallPtrSet<Instruction*, 4> > ReverseDepMapType;
275 ReverseDepMapType ReverseLocalDeps;
277 // A reverse mapping from dependencies to the non-local dependees.
278 ReverseDepMapType ReverseNonLocalDeps;
280 /// Current AA implementation, just a cache.
283 OwningPtr<PredIteratorCache> PredCache;
285 MemoryDependenceAnalysis();
286 ~MemoryDependenceAnalysis();
289 /// Pass Implementation stuff. This doesn't do any analysis eagerly.
290 bool runOnFunction(Function &);
292 /// Clean up memory in between runs
293 void releaseMemory();
295 /// getAnalysisUsage - Does not modify anything. It uses Value Numbering
296 /// and Alias Analysis.
298 virtual void getAnalysisUsage(AnalysisUsage &AU) const;
300 /// getDependency - Return the instruction on which a memory operation
301 /// depends. See the class comment for more details. It is illegal to call
302 /// this on non-memory instructions.
303 MemDepResult getDependency(Instruction *QueryInst);
305 /// getNonLocalCallDependency - Perform a full dependency query for the
306 /// specified call, returning the set of blocks that the value is
307 /// potentially live across. The returned set of results will include a
308 /// "NonLocal" result for all blocks where the value is live across.
310 /// This method assumes the instruction returns a "NonLocal" dependency
311 /// within its own block.
313 /// This returns a reference to an internal data structure that may be
314 /// invalidated on the next non-local query or when an instruction is
315 /// removed. Clients must copy this data if they want it around longer than
317 const NonLocalDepInfo &getNonLocalCallDependency(CallSite QueryCS);
320 /// getNonLocalPointerDependency - Perform a full dependency query for an
321 /// access to the specified (non-volatile) memory location, returning the
322 /// set of instructions that either define or clobber the value.
324 /// This method assumes the pointer has a "NonLocal" dependency within BB.
325 void getNonLocalPointerDependency(const AliasAnalysis::Location &Loc,
326 bool isLoad, BasicBlock *BB,
327 SmallVectorImpl<NonLocalDepResult> &Result);
329 /// removeInstruction - Remove an instruction from the dependence analysis,
330 /// updating the dependence of instructions that previously depended on it.
331 void removeInstruction(Instruction *InstToRemove);
333 /// invalidateCachedPointerInfo - This method is used to invalidate cached
334 /// information about the specified pointer, because it may be too
335 /// conservative in memdep. This is an optional call that can be used when
336 /// the client detects an equivalence between the pointer and some other
337 /// value and replaces the other value with ptr. This can make Ptr available
338 /// in more places that cached info does not necessarily keep.
339 void invalidateCachedPointerInfo(Value *Ptr);
341 /// invalidateCachedPredecessors - Clear the PredIteratorCache info.
342 /// This needs to be done when the CFG changes, e.g., due to splitting
344 void invalidateCachedPredecessors();
346 /// getPointerDependencyFrom - Return the instruction on which a memory
347 /// location depends. If isLoad is true, this routine ignores may-aliases
348 /// with read-only operations. If isLoad is false, this routine ignores
349 /// may-aliases with reads from read-only locations.
351 /// Note that this is an uncached query, and thus may be inefficient.
353 MemDepResult getPointerDependencyFrom(const AliasAnalysis::Location &Loc,
355 BasicBlock::iterator ScanIt,
359 MemDepResult getCallSiteDependencyFrom(CallSite C, bool isReadOnlyCall,
360 BasicBlock::iterator ScanIt,
362 bool getNonLocalPointerDepFromBB(const PHITransAddr &Pointer,
363 const AliasAnalysis::Location &Loc,
364 bool isLoad, BasicBlock *BB,
365 SmallVectorImpl<NonLocalDepResult> &Result,
366 DenseMap<BasicBlock*, Value*> &Visited,
367 bool SkipFirstBlock = false);
368 MemDepResult GetNonLocalInfoForBlock(const AliasAnalysis::Location &Loc,
369 bool isLoad, BasicBlock *BB,
370 NonLocalDepInfo *Cache,
371 unsigned NumSortedEntries);
373 void RemoveCachedNonLocalPointerDependencies(ValueIsLoadPair P);
375 /// verifyRemoved - Verify that the specified instruction does not occur
376 /// in our internal data structures.
377 void verifyRemoved(Instruction *Inst) const;
381 } // End llvm namespace