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
52 /// Def - This is a dependence on the specified instruction which
53 /// defines/produces the desired memory location. The pointer member of
54 /// the MemDepResult pair holds the instruction that defines the memory.
55 /// Cases of interest:
56 /// 1. This could be a load or store for dependence queries on
57 /// load/store. The value loaded or stored is the produced value.
58 /// Note that the pointer operand may be different than that of the
59 /// queried pointer due to must aliases and phi translation. Note
60 /// that the def may not be the same type as the query, the pointers
61 /// may just be must aliases.
62 /// 2. For loads and stores, this could be an allocation instruction. In
63 /// this case, the load is loading an undef value or a store is the
64 /// first store to (that part of) the allocation.
65 /// 3. Dependence queries on calls return Def only when they are
66 /// readonly calls or memory use intrinsics with identical callees
67 /// and no intervening clobbers. No validation is done that the
68 /// operands to the calls are the same.
71 /// NonLocal - This marker indicates that the query has no dependency in
72 /// the specified block. To find out more, the client should query other
73 /// predecessor blocks.
76 typedef PointerIntPair<Instruction*, 2, DepType> PairTy;
78 explicit MemDepResult(PairTy V) : Value(V) {}
80 MemDepResult() : Value(0, Invalid) {}
82 /// get methods: These are static ctor methods for creating various
83 /// MemDepResult kinds.
84 static MemDepResult getDef(Instruction *Inst) {
85 return MemDepResult(PairTy(Inst, Def));
87 static MemDepResult getClobber(Instruction *Inst) {
88 return MemDepResult(PairTy(Inst, Clobber));
90 static MemDepResult getNonLocal() {
91 return MemDepResult(PairTy(0, NonLocal));
94 /// isClobber - Return true if this MemDepResult represents a query that is
95 /// a instruction clobber dependency.
96 bool isClobber() const { return Value.getInt() == Clobber; }
98 /// isDef - Return true if this MemDepResult represents a query that is
99 /// a instruction definition dependency.
100 bool isDef() const { return Value.getInt() == Def; }
102 /// isNonLocal - Return true if this MemDepResult represents a query that
103 /// is transparent to the start of the block, but where a non-local hasn't
105 bool isNonLocal() const { return Value.getInt() == NonLocal; }
107 /// getInst() - If this is a normal dependency, return the instruction that
108 /// is depended on. Otherwise, return null.
109 Instruction *getInst() const { return Value.getPointer(); }
111 bool operator==(const MemDepResult &M) const { return Value == M.Value; }
112 bool operator!=(const MemDepResult &M) const { return Value != M.Value; }
113 bool operator<(const MemDepResult &M) const { return Value < M.Value; }
114 bool operator>(const MemDepResult &M) const { return Value > M.Value; }
116 friend class MemoryDependenceAnalysis;
117 /// Dirty - Entries with this marker occur in a LocalDeps map or
118 /// NonLocalDeps map when the instruction they previously referenced was
119 /// removed from MemDep. In either case, the entry may include an
120 /// instruction pointer. If so, the pointer is an instruction in the
121 /// block where scanning can start from, saving some work.
123 /// In a default-constructed MemDepResult object, the type will be Dirty
124 /// and the instruction pointer will be null.
127 /// isDirty - Return true if this is a MemDepResult in its dirty/invalid.
129 bool isDirty() const { return Value.getInt() == Invalid; }
131 static MemDepResult getDirty(Instruction *Inst) {
132 return MemDepResult(PairTy(Inst, Invalid));
136 /// NonLocalDepResult - This is a result from a NonLocal dependence query.
137 /// For each BasicBlock (the BB entry) it keeps a MemDepResult and the
138 /// (potentially phi translated) address that was live in the block.
139 class NonLocalDepResult {
144 NonLocalDepResult(BasicBlock *bb, MemDepResult result, Value *address)
145 : BB(bb), Result(result), Address(address) {}
147 // BB is the sort key, it can't be changed.
148 BasicBlock *getBB() const { return BB; }
150 void setResult(const MemDepResult &R, Value *Addr) {
155 const MemDepResult &getResult() const { return Result; }
157 /// getAddress - Return the address of this pointer in this block. This can
158 /// be different than the address queried for the non-local result because
159 /// of phi translation. This returns null if the address was not available
160 /// in a block (i.e. because phi translation failed) or if this is a cached
161 /// result and that address was deleted.
163 /// The address is always null for a non-local 'call' dependence.
164 Value *getAddress() const { return Address; }
167 /// NonLocalDepEntry - This is an entry in the NonLocalDepInfo cache. For
168 /// each BasicBlock (the BB entry) it keeps a MemDepResult.
169 class NonLocalDepEntry {
173 NonLocalDepEntry(BasicBlock *bb, MemDepResult result)
174 : BB(bb), Result(result) {}
176 // This is used for searches.
177 NonLocalDepEntry(BasicBlock *bb) : BB(bb) {}
179 // BB is the sort key, it can't be changed.
180 BasicBlock *getBB() const { return BB; }
182 void setResult(const MemDepResult &R) { Result = R; }
184 const MemDepResult &getResult() const { return Result; }
186 bool operator<(const NonLocalDepEntry &RHS) const {
191 /// MemoryDependenceAnalysis - This is an analysis that determines, for a
192 /// given memory operation, what preceding memory operations it depends on.
193 /// It builds on alias analysis information, and tries to provide a lazy,
194 /// caching interface to a common kind of alias information query.
196 /// The dependency information returned is somewhat unusual, but is pragmatic.
197 /// If queried about a store or call that might modify memory, the analysis
198 /// will return the instruction[s] that may either load from that memory or
199 /// store to it. If queried with a load or call that can never modify memory,
200 /// the analysis will return calls and stores that might modify the pointer,
201 /// but generally does not return loads unless a) they are volatile, or
202 /// b) they load from *must-aliased* pointers. Returning a dependence on
203 /// must-alias'd pointers instead of all pointers interacts well with the
204 /// internal caching mechanism.
206 class MemoryDependenceAnalysis : public FunctionPass {
207 // A map from instructions to their dependency.
208 typedef DenseMap<Instruction*, MemDepResult> LocalDepMapType;
209 LocalDepMapType LocalDeps;
212 typedef std::vector<NonLocalDepEntry> NonLocalDepInfo;
214 /// ValueIsLoadPair - This is a pair<Value*, bool> where the bool is true if
215 /// the dependence is a read only dependence, false if read/write.
216 typedef PointerIntPair<const Value*, 1, bool> ValueIsLoadPair;
218 /// BBSkipFirstBlockPair - This pair is used when caching information for a
219 /// block. If the pointer is null, the cache value is not a full query that
220 /// starts at the specified block. If non-null, the bool indicates whether
221 /// or not the contents of the block was skipped.
222 typedef PointerIntPair<BasicBlock*, 1, bool> BBSkipFirstBlockPair;
224 /// NonLocalPointerInfo - This record is the information kept for each
225 /// (value, is load) pair.
226 struct NonLocalPointerInfo {
227 /// Pair - The pair of the block and the skip-first-block flag.
228 BBSkipFirstBlockPair Pair;
229 /// NonLocalDeps - The results of the query for each relevant block.
230 NonLocalDepInfo NonLocalDeps;
231 /// TBAATag - The TBAA tag associated with dereferences of the
232 /// pointer. May be null if there are no tags or conflicting tags.
235 NonLocalPointerInfo() : TBAATag(0) {}
238 /// CachedNonLocalPointerInfo - This map stores the cached results of doing
239 /// a pointer lookup at the bottom of a block. The key of this map is the
240 /// pointer+isload bit, the value is a list of <bb->result> mappings.
241 typedef DenseMap<ValueIsLoadPair,
242 NonLocalPointerInfo> CachedNonLocalPointerInfo;
243 CachedNonLocalPointerInfo NonLocalPointerDeps;
245 // A map from instructions to their non-local pointer dependencies.
246 typedef DenseMap<Instruction*,
247 SmallPtrSet<ValueIsLoadPair, 4> > ReverseNonLocalPtrDepTy;
248 ReverseNonLocalPtrDepTy ReverseNonLocalPtrDeps;
251 /// PerInstNLInfo - This is the instruction we keep for each cached access
252 /// that we have for an instruction. The pointer is an owning pointer and
253 /// the bool indicates whether we have any dirty bits in the set.
254 typedef std::pair<NonLocalDepInfo, bool> PerInstNLInfo;
256 // A map from instructions to their non-local dependencies.
257 typedef DenseMap<Instruction*, PerInstNLInfo> NonLocalDepMapType;
259 NonLocalDepMapType NonLocalDeps;
261 // A reverse mapping from dependencies to the dependees. This is
262 // used when removing instructions to keep the cache coherent.
263 typedef DenseMap<Instruction*,
264 SmallPtrSet<Instruction*, 4> > ReverseDepMapType;
265 ReverseDepMapType ReverseLocalDeps;
267 // A reverse mapping from dependencies to the non-local dependees.
268 ReverseDepMapType ReverseNonLocalDeps;
270 /// Current AA implementation, just a cache.
273 OwningPtr<PredIteratorCache> PredCache;
275 MemoryDependenceAnalysis();
276 ~MemoryDependenceAnalysis();
279 /// Pass Implementation stuff. This doesn't do any analysis eagerly.
280 bool runOnFunction(Function &);
282 /// Clean up memory in between runs
283 void releaseMemory();
285 /// getAnalysisUsage - Does not modify anything. It uses Value Numbering
286 /// and Alias Analysis.
288 virtual void getAnalysisUsage(AnalysisUsage &AU) const;
290 /// getDependency - Return the instruction on which a memory operation
291 /// depends. See the class comment for more details. It is illegal to call
292 /// this on non-memory instructions.
293 MemDepResult getDependency(Instruction *QueryInst);
295 /// getNonLocalCallDependency - Perform a full dependency query for the
296 /// specified call, returning the set of blocks that the value is
297 /// potentially live across. The returned set of results will include a
298 /// "NonLocal" result for all blocks where the value is live across.
300 /// This method assumes the instruction returns a "NonLocal" dependency
301 /// within its own block.
303 /// This returns a reference to an internal data structure that may be
304 /// invalidated on the next non-local query or when an instruction is
305 /// removed. Clients must copy this data if they want it around longer than
307 const NonLocalDepInfo &getNonLocalCallDependency(CallSite QueryCS);
310 /// getNonLocalPointerDependency - Perform a full dependency query for an
311 /// access to the specified (non-volatile) memory location, returning the
312 /// set of instructions that either define or clobber the value.
314 /// This method assumes the pointer has a "NonLocal" dependency within BB.
315 void getNonLocalPointerDependency(const AliasAnalysis::Location &Loc,
316 bool isLoad, BasicBlock *BB,
317 SmallVectorImpl<NonLocalDepResult> &Result);
319 /// getNonLocalPointerDependence - A convenience wrapper.
320 void getNonLocalPointerDependency(Value *Pointer, bool isLoad,
322 SmallVectorImpl<NonLocalDepResult> &Result){
323 return getNonLocalPointerDependency(AliasAnalysis::Location(Pointer),
327 /// removeInstruction - Remove an instruction from the dependence analysis,
328 /// updating the dependence of instructions that previously depended on it.
329 void removeInstruction(Instruction *InstToRemove);
331 /// invalidateCachedPointerInfo - This method is used to invalidate cached
332 /// information about the specified pointer, because it may be too
333 /// conservative in memdep. This is an optional call that can be used when
334 /// the client detects an equivalence between the pointer and some other
335 /// value and replaces the other value with ptr. This can make Ptr available
336 /// in more places that cached info does not necessarily keep.
337 void invalidateCachedPointerInfo(Value *Ptr);
339 /// invalidateCachedPredecessors - Clear the PredIteratorCache info.
340 /// This needs to be done when the CFG changes, e.g., due to splitting
342 void invalidateCachedPredecessors();
345 MemDepResult getPointerDependencyFrom(const AliasAnalysis::Location &Loc,
347 BasicBlock::iterator ScanIt,
349 MemDepResult getCallSiteDependencyFrom(CallSite C, bool isReadOnlyCall,
350 BasicBlock::iterator ScanIt,
352 bool getNonLocalPointerDepFromBB(const PHITransAddr &Pointer,
353 const AliasAnalysis::Location &Loc,
354 bool isLoad, BasicBlock *BB,
355 SmallVectorImpl<NonLocalDepResult> &Result,
356 DenseMap<BasicBlock*, Value*> &Visited,
357 bool SkipFirstBlock = false);
358 MemDepResult GetNonLocalInfoForBlock(const AliasAnalysis::Location &Loc,
359 bool isLoad, BasicBlock *BB,
360 NonLocalDepInfo *Cache,
361 unsigned NumSortedEntries);
363 void RemoveCachedNonLocalPointerDependencies(ValueIsLoadPair P);
365 /// verifyRemoved - Verify that the specified instruction does not occur
366 /// in our internal data structures.
367 void verifyRemoved(Instruction *Inst) const;
371 } // End llvm namespace