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_MEMORYDEPENDENCEANALYSIS_H
15 #define LLVM_ANALYSIS_MEMORYDEPENDENCEANALYSIS_H
17 #include "llvm/ADT/DenseMap.h"
18 #include "llvm/ADT/PointerIntPair.h"
19 #include "llvm/ADT/SmallPtrSet.h"
20 #include "llvm/Analysis/AliasAnalysis.h"
21 #include "llvm/IR/BasicBlock.h"
22 #include "llvm/IR/PredIteratorCache.h"
23 #include "llvm/IR/ValueHandle.h"
24 #include "llvm/Pass.h"
31 class AssumptionCache;
32 class MemoryDependenceAnalysis;
33 class PredIteratorCache;
37 /// MemDepResult - A memory dependence query can return one of three different
38 /// answers, described below.
41 /// Invalid - Clients of MemDep never see this.
44 /// Clobber - This is a dependence on the specified instruction which
45 /// clobbers the desired value. The pointer member of the MemDepResult
46 /// pair holds the instruction that clobbers the memory. For example,
47 /// this occurs when we see a may-aliased store to the memory location we
50 /// There are several cases that may be interesting here:
51 /// 1. Loads are clobbered by may-alias stores.
52 /// 2. Loads are considered clobbered by partially-aliased loads. The
53 /// client may choose to analyze deeper into these cases.
56 /// Def - This is a dependence on the specified instruction which
57 /// defines/produces the desired memory location. The pointer member of
58 /// the MemDepResult pair holds the instruction that defines the memory.
59 /// Cases of interest:
60 /// 1. This could be a load or store for dependence queries on
61 /// load/store. The value loaded or stored is the produced value.
62 /// Note that the pointer operand may be different than that of the
63 /// queried pointer due to must aliases and phi translation. Note
64 /// that the def may not be the same type as the query, the pointers
65 /// may just be must aliases.
66 /// 2. For loads and stores, this could be an allocation instruction. In
67 /// this case, the load is loading an undef value or a store is the
68 /// first store to (that part of) the allocation.
69 /// 3. Dependence queries on calls return Def only when they are
70 /// readonly calls or memory use intrinsics with identical callees
71 /// and no intervening clobbers. No validation is done that the
72 /// operands to the calls are the same.
75 /// Other - This marker indicates that the query has no known dependency
76 /// in the specified block. More detailed state info is encoded in the
77 /// upper part of the pair (i.e. the Instruction*)
80 /// If DepType is "Other", the upper part of the pair
81 /// (i.e. the Instruction* part) is instead used to encode more detailed
82 /// type information as follows
84 /// NonLocal - This marker indicates that the query has no dependency in
85 /// the specified block. To find out more, the client should query other
86 /// predecessor blocks.
88 /// NonFuncLocal - This marker indicates that the query has no
89 /// dependency in the specified function.
91 /// Unknown - This marker indicates that the query dependency
96 typedef PointerIntPair<Instruction*, 2, DepType> PairTy;
98 explicit MemDepResult(PairTy V) : Value(V) {}
101 MemDepResult() : Value(nullptr, Invalid) {}
103 /// get methods: These are static ctor methods for creating various
104 /// MemDepResult kinds.
105 static MemDepResult getDef(Instruction *Inst) {
106 assert(Inst && "Def requires inst");
107 return MemDepResult(PairTy(Inst, Def));
109 static MemDepResult getClobber(Instruction *Inst) {
110 assert(Inst && "Clobber requires inst");
111 return MemDepResult(PairTy(Inst, Clobber));
113 static MemDepResult getNonLocal() {
115 PairTy(reinterpret_cast<Instruction*>(NonLocal), Other));
117 static MemDepResult getNonFuncLocal() {
119 PairTy(reinterpret_cast<Instruction*>(NonFuncLocal), Other));
121 static MemDepResult getUnknown() {
123 PairTy(reinterpret_cast<Instruction*>(Unknown), Other));
126 /// isClobber - Return true if this MemDepResult represents a query that is
127 /// an instruction clobber dependency.
128 bool isClobber() const { return Value.getInt() == Clobber; }
130 /// isDef - Return true if this MemDepResult represents a query that is
131 /// an instruction definition dependency.
132 bool isDef() const { return Value.getInt() == Def; }
134 /// isNonLocal - Return true if this MemDepResult represents a query that
135 /// is transparent to the start of the block, but where a non-local hasn't
137 bool isNonLocal() const {
138 return Value.getInt() == Other
139 && Value.getPointer() == reinterpret_cast<Instruction*>(NonLocal);
142 /// isNonFuncLocal - Return true if this MemDepResult represents a query
143 /// that is transparent to the start of the function.
144 bool isNonFuncLocal() const {
145 return Value.getInt() == Other
146 && Value.getPointer() == reinterpret_cast<Instruction*>(NonFuncLocal);
149 /// isUnknown - Return true if this MemDepResult represents a query which
150 /// cannot and/or will not be computed.
151 bool isUnknown() const {
152 return Value.getInt() == Other
153 && Value.getPointer() == reinterpret_cast<Instruction*>(Unknown);
156 /// getInst() - If this is a normal dependency, return the instruction that
157 /// is depended on. Otherwise, return null.
158 Instruction *getInst() const {
159 if (Value.getInt() == Other) return nullptr;
160 return Value.getPointer();
163 bool operator==(const MemDepResult &M) const { return Value == M.Value; }
164 bool operator!=(const MemDepResult &M) const { return Value != M.Value; }
165 bool operator<(const MemDepResult &M) const { return Value < M.Value; }
166 bool operator>(const MemDepResult &M) const { return Value > M.Value; }
169 friend class MemoryDependenceAnalysis;
170 /// Dirty - Entries with this marker occur in a LocalDeps map or
171 /// NonLocalDeps map when the instruction they previously referenced was
172 /// removed from MemDep. In either case, the entry may include an
173 /// instruction pointer. If so, the pointer is an instruction in the
174 /// block where scanning can start from, saving some work.
176 /// In a default-constructed MemDepResult object, the type will be Dirty
177 /// and the instruction pointer will be null.
180 /// isDirty - Return true if this is a MemDepResult in its dirty/invalid.
182 bool isDirty() const { return Value.getInt() == Invalid; }
184 static MemDepResult getDirty(Instruction *Inst) {
185 return MemDepResult(PairTy(Inst, Invalid));
189 /// NonLocalDepEntry - This is an entry in the NonLocalDepInfo cache. For
190 /// each BasicBlock (the BB entry) it keeps a MemDepResult.
191 class NonLocalDepEntry {
196 NonLocalDepEntry(BasicBlock *bb, MemDepResult result)
197 : BB(bb), Result(result) {}
199 // This is used for searches.
200 NonLocalDepEntry(BasicBlock *bb) : BB(bb) {}
202 // BB is the sort key, it can't be changed.
203 BasicBlock *getBB() const { return BB; }
205 void setResult(const MemDepResult &R) { Result = R; }
207 const MemDepResult &getResult() const { return Result; }
209 bool operator<(const NonLocalDepEntry &RHS) const {
214 /// NonLocalDepResult - This is a result from a NonLocal dependence query.
215 /// For each BasicBlock (the BB entry) it keeps a MemDepResult and the
216 /// (potentially phi translated) address that was live in the block.
217 class NonLocalDepResult {
218 NonLocalDepEntry Entry;
222 NonLocalDepResult(BasicBlock *bb, MemDepResult result, Value *address)
223 : Entry(bb, result), Address(address) {}
225 // BB is the sort key, it can't be changed.
226 BasicBlock *getBB() const { return Entry.getBB(); }
228 void setResult(const MemDepResult &R, Value *Addr) {
233 const MemDepResult &getResult() const { return Entry.getResult(); }
235 /// getAddress - Return the address of this pointer in this block. This can
236 /// be different than the address queried for the non-local result because
237 /// of phi translation. This returns null if the address was not available
238 /// in a block (i.e. because phi translation failed) or if this is a cached
239 /// result and that address was deleted.
241 /// The address is always null for a non-local 'call' dependence.
242 Value *getAddress() const { return Address; }
245 /// MemoryDependenceAnalysis - This is an analysis that determines, for a
246 /// given memory operation, what preceding memory operations it depends on.
247 /// It builds on alias analysis information, and tries to provide a lazy,
248 /// caching interface to a common kind of alias information query.
250 /// The dependency information returned is somewhat unusual, but is pragmatic.
251 /// If queried about a store or call that might modify memory, the analysis
252 /// will return the instruction[s] that may either load from that memory or
253 /// store to it. If queried with a load or call that can never modify memory,
254 /// the analysis will return calls and stores that might modify the pointer,
255 /// but generally does not return loads unless a) they are volatile, or
256 /// b) they load from *must-aliased* pointers. Returning a dependence on
257 /// must-alias'd pointers instead of all pointers interacts well with the
258 /// internal caching mechanism.
260 class MemoryDependenceAnalysis : public FunctionPass {
261 // A map from instructions to their dependency.
262 typedef DenseMap<Instruction*, MemDepResult> LocalDepMapType;
263 LocalDepMapType LocalDeps;
266 typedef std::vector<NonLocalDepEntry> NonLocalDepInfo;
269 /// ValueIsLoadPair - This is a pair<Value*, bool> where the bool is true if
270 /// the dependence is a read only dependence, false if read/write.
271 typedef PointerIntPair<const Value*, 1, bool> ValueIsLoadPair;
273 /// BBSkipFirstBlockPair - This pair is used when caching information for a
274 /// block. If the pointer is null, the cache value is not a full query that
275 /// starts at the specified block. If non-null, the bool indicates whether
276 /// or not the contents of the block was skipped.
277 typedef PointerIntPair<BasicBlock*, 1, bool> BBSkipFirstBlockPair;
279 /// NonLocalPointerInfo - This record is the information kept for each
280 /// (value, is load) pair.
281 struct NonLocalPointerInfo {
282 /// Pair - The pair of the block and the skip-first-block flag.
283 BBSkipFirstBlockPair Pair;
284 /// NonLocalDeps - The results of the query for each relevant block.
285 NonLocalDepInfo NonLocalDeps;
286 /// Size - The maximum size of the dereferences of the
287 /// pointer. May be UnknownSize if the sizes are unknown.
289 /// AATags - The AA tags associated with dereferences of the
290 /// pointer. The members may be null if there are no tags or
291 /// conflicting tags.
294 NonLocalPointerInfo() : Size(MemoryLocation::UnknownSize) {}
297 /// CachedNonLocalPointerInfo - This map stores the cached results of doing
298 /// a pointer lookup at the bottom of a block. The key of this map is the
299 /// pointer+isload bit, the value is a list of <bb->result> mappings.
300 typedef DenseMap<ValueIsLoadPair,
301 NonLocalPointerInfo> CachedNonLocalPointerInfo;
302 CachedNonLocalPointerInfo NonLocalPointerDeps;
304 // A map from instructions to their non-local pointer dependencies.
305 typedef DenseMap<Instruction*,
306 SmallPtrSet<ValueIsLoadPair, 4> > ReverseNonLocalPtrDepTy;
307 ReverseNonLocalPtrDepTy ReverseNonLocalPtrDeps;
309 /// PerInstNLInfo - This is the instruction we keep for each cached access
310 /// that we have for an instruction. The pointer is an owning pointer and
311 /// the bool indicates whether we have any dirty bits in the set.
312 typedef std::pair<NonLocalDepInfo, bool> PerInstNLInfo;
314 // A map from instructions to their non-local dependencies.
315 typedef DenseMap<Instruction*, PerInstNLInfo> NonLocalDepMapType;
317 NonLocalDepMapType NonLocalDeps;
319 // A reverse mapping from dependencies to the dependees. This is
320 // used when removing instructions to keep the cache coherent.
321 typedef DenseMap<Instruction*,
322 SmallPtrSet<Instruction*, 4> > ReverseDepMapType;
323 ReverseDepMapType ReverseLocalDeps;
325 // A reverse mapping from dependencies to the non-local dependees.
326 ReverseDepMapType ReverseNonLocalDeps;
328 /// Current AA implementation, just a cache.
332 const TargetLibraryInfo *TLI;
333 PredIteratorCache PredCache;
336 MemoryDependenceAnalysis();
337 ~MemoryDependenceAnalysis() override;
340 /// Pass Implementation stuff. This doesn't do any analysis eagerly.
341 bool runOnFunction(Function &) override;
343 /// Clean up memory in between runs
344 void releaseMemory() override;
346 /// getAnalysisUsage - Does not modify anything. It uses Value Numbering
347 /// and Alias Analysis.
349 void getAnalysisUsage(AnalysisUsage &AU) const override;
351 /// getDependency - Return the instruction on which a memory operation
352 /// depends. See the class comment for more details. It is illegal to call
353 /// this on non-memory instructions.
354 MemDepResult getDependency(Instruction *QueryInst);
356 /// getNonLocalCallDependency - Perform a full dependency query for the
357 /// specified call, returning the set of blocks that the value is
358 /// potentially live across. The returned set of results will include a
359 /// "NonLocal" result for all blocks where the value is live across.
361 /// This method assumes the instruction returns a "NonLocal" dependency
362 /// within its own block.
364 /// This returns a reference to an internal data structure that may be
365 /// invalidated on the next non-local query or when an instruction is
366 /// removed. Clients must copy this data if they want it around longer than
368 const NonLocalDepInfo &getNonLocalCallDependency(CallSite QueryCS);
370 /// getNonLocalPointerDependency - Perform a full dependency query for an
371 /// access to the QueryInst's specified memory location, returning the set
372 /// of instructions that either define or clobber the value.
374 /// Warning: For a volatile query instruction, the dependencies will be
375 /// accurate, and thus usable for reordering, but it is never legal to
376 /// remove the query instruction.
378 /// This method assumes the pointer has a "NonLocal" dependency within
379 /// QueryInst's parent basic block.
380 void getNonLocalPointerDependency(Instruction *QueryInst,
381 SmallVectorImpl<NonLocalDepResult> &Result);
383 /// removeInstruction - Remove an instruction from the dependence analysis,
384 /// updating the dependence of instructions that previously depended on it.
385 void removeInstruction(Instruction *InstToRemove);
387 /// invalidateCachedPointerInfo - This method is used to invalidate cached
388 /// information about the specified pointer, because it may be too
389 /// conservative in memdep. This is an optional call that can be used when
390 /// the client detects an equivalence between the pointer and some other
391 /// value and replaces the other value with ptr. This can make Ptr available
392 /// in more places that cached info does not necessarily keep.
393 void invalidateCachedPointerInfo(Value *Ptr);
395 /// invalidateCachedPredecessors - Clear the PredIteratorCache info.
396 /// This needs to be done when the CFG changes, e.g., due to splitting
398 void invalidateCachedPredecessors();
400 /// \brief Return the instruction on which a memory location depends.
401 /// If isLoad is true, this routine ignores may-aliases with read-only
402 /// operations. If isLoad is false, this routine ignores may-aliases
403 /// with reads from read-only locations. If possible, pass the query
404 /// instruction as well; this function may take advantage of the metadata
405 /// annotated to the query instruction to refine the result.
407 /// Note that this is an uncached query, and thus may be inefficient.
409 MemDepResult getPointerDependencyFrom(const MemoryLocation &Loc,
411 BasicBlock::iterator ScanIt,
413 Instruction *QueryInst = nullptr);
415 MemDepResult getSimplePointerDependencyFrom(const MemoryLocation &MemLoc,
417 BasicBlock::iterator ScanIt,
419 Instruction *QueryInst);
421 /// This analysis looks for other loads and stores with invariant.group
422 /// metadata and the same pointer operand. Returns Unknown if it does not
423 /// find anything, and Def if it can be assumed that 2 instructions load or
424 /// store the same value.
425 /// FIXME: This analysis works only on single block because of restrictions
426 /// at the call site.
427 MemDepResult getInvariantGroupPointerDependency(LoadInst *LI,
430 /// getLoadLoadClobberFullWidthSize - This is a little bit of analysis that
431 /// looks at a memory location for a load (specified by MemLocBase, Offs,
432 /// and Size) and compares it against a load. If the specified load could
433 /// be safely widened to a larger integer load that is 1) still efficient,
434 /// 2) safe for the target, and 3) would provide the specified memory
435 /// location value, then this function returns the size in bytes of the
436 /// load width to use. If not, this returns zero.
437 static unsigned getLoadLoadClobberFullWidthSize(const Value *MemLocBase,
443 MemDepResult getCallSiteDependencyFrom(CallSite C, bool isReadOnlyCall,
444 BasicBlock::iterator ScanIt,
446 bool getNonLocalPointerDepFromBB(Instruction *QueryInst,
447 const PHITransAddr &Pointer,
448 const MemoryLocation &Loc, bool isLoad,
450 SmallVectorImpl<NonLocalDepResult> &Result,
451 DenseMap<BasicBlock *, Value *> &Visited,
452 bool SkipFirstBlock = false);
453 MemDepResult GetNonLocalInfoForBlock(Instruction *QueryInst,
454 const MemoryLocation &Loc, bool isLoad,
455 BasicBlock *BB, NonLocalDepInfo *Cache,
456 unsigned NumSortedEntries);
458 void RemoveCachedNonLocalPointerDependencies(ValueIsLoadPair P);
460 /// verifyRemoved - Verify that the specified instruction does not occur
461 /// in our internal data structures.
462 void verifyRemoved(Instruction *Inst) const;
465 } // End llvm namespace