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/PointerSumType.h"
19 #include "llvm/ADT/PointerEmbeddedInt.h"
20 #include "llvm/ADT/SmallPtrSet.h"
21 #include "llvm/Analysis/AliasAnalysis.h"
22 #include "llvm/IR/BasicBlock.h"
23 #include "llvm/IR/PassManager.h"
24 #include "llvm/IR/PredIteratorCache.h"
25 #include "llvm/IR/ValueHandle.h"
26 #include "llvm/Pass.h"
33 class AssumptionCache;
34 class MemoryDependenceResults;
35 class PredIteratorCache;
39 /// A memory dependence query can return one of three different answers.
42 /// Clients of MemDep never see this.
44 /// Entries with this marker occur in a LocalDeps map or NonLocalDeps map
45 /// when the instruction they previously referenced was removed from
46 /// MemDep. In either case, the entry may include an instruction pointer.
47 /// If so, the pointer is an instruction in the block where scanning can
48 /// start from, saving some work.
50 /// In a default-constructed MemDepResult object, the type will be Invalid
51 /// and the instruction pointer will be null.
54 /// This is a dependence on the specified instruction which clobbers the
55 /// desired value. The pointer member of the MemDepResult pair holds the
56 /// instruction that clobbers the memory. For example, this occurs when we
57 /// see a may-aliased store to the memory location we care about.
59 /// There are several cases that may be interesting here:
60 /// 1. Loads are clobbered by may-alias stores.
61 /// 2. Loads are considered clobbered by partially-aliased loads. The
62 /// client may choose to analyze deeper into these cases.
65 /// This is a dependence on the specified instruction which defines or
66 /// produces the desired memory location. The pointer member of the
67 /// MemDepResult pair holds the instruction that defines the memory.
69 /// Cases of interest:
70 /// 1. This could be a load or store for dependence queries on
71 /// load/store. The value loaded or stored is the produced value.
72 /// Note that the pointer operand may be different than that of the
73 /// queried pointer due to must aliases and phi translation. Note
74 /// that the def may not be the same type as the query, the pointers
75 /// may just be must aliases.
76 /// 2. For loads and stores, this could be an allocation instruction. In
77 /// this case, the load is loading an undef value or a store is the
78 /// first store to (that part of) the allocation.
79 /// 3. Dependence queries on calls return Def only when they are readonly
80 /// calls or memory use intrinsics with identical callees and no
81 /// intervening clobbers. No validation is done that the operands to
82 /// the calls are the same.
85 /// This marker indicates that the query has no known dependency in the
88 /// More detailed state info is encoded in the upper part of the pair (i.e.
93 /// If DepType is "Other", the upper part of the sum type is an encoding of
94 /// the following more detailed type information.
96 /// This marker indicates that the query has no dependency in the specified
99 /// To find out more, the client should query other predecessor blocks.
101 /// This marker indicates that the query has no dependency in the specified
104 /// This marker indicates that the query dependency is unknown.
108 typedef PointerSumType<
109 DepType, PointerSumTypeMember<Invalid, Instruction *>,
110 PointerSumTypeMember<Clobber, Instruction *>,
111 PointerSumTypeMember<Def, Instruction *>,
112 PointerSumTypeMember<Other, PointerEmbeddedInt<OtherType, 3>>>
115 explicit MemDepResult(ValueTy V) : Value(V) {}
118 MemDepResult() : Value() {}
120 /// get methods: These are static ctor methods for creating various
121 /// MemDepResult kinds.
122 static MemDepResult getDef(Instruction *Inst) {
123 assert(Inst && "Def requires inst");
124 return MemDepResult(ValueTy::create<Def>(Inst));
126 static MemDepResult getClobber(Instruction *Inst) {
127 assert(Inst && "Clobber requires inst");
128 return MemDepResult(ValueTy::create<Clobber>(Inst));
130 static MemDepResult getNonLocal() {
131 return MemDepResult(ValueTy::create<Other>(NonLocal));
133 static MemDepResult getNonFuncLocal() {
134 return MemDepResult(ValueTy::create<Other>(NonFuncLocal));
136 static MemDepResult getUnknown() {
137 return MemDepResult(ValueTy::create<Other>(Unknown));
140 /// Tests if this MemDepResult represents a query that is an instruction
141 /// clobber dependency.
142 bool isClobber() const { return Value.is<Clobber>(); }
144 /// Tests if this MemDepResult represents a query that is an instruction
145 /// definition dependency.
146 bool isDef() const { return Value.is<Def>(); }
148 /// Tests if this MemDepResult represents a query that is transparent to the
149 /// start of the block, but where a non-local hasn't been done.
150 bool isNonLocal() const {
151 return Value.is<Other>() && Value.cast<Other>() == NonLocal;
154 /// Tests if this MemDepResult represents a query that is transparent to the
155 /// start of the function.
156 bool isNonFuncLocal() const {
157 return Value.is<Other>() && Value.cast<Other>() == NonFuncLocal;
160 /// Tests if this MemDepResult represents a query which cannot and/or will
162 bool isUnknown() const {
163 return Value.is<Other>() && Value.cast<Other>() == Unknown;
166 /// If this is a normal dependency, returns the instruction that is depended
167 /// on. Otherwise, returns null.
168 Instruction *getInst() const {
169 switch (Value.getTag()) {
171 return Value.cast<Invalid>();
173 return Value.cast<Clobber>();
175 return Value.cast<Def>();
179 llvm_unreachable("Unknown discriminant!");
182 bool operator==(const MemDepResult &M) const { return Value == M.Value; }
183 bool operator!=(const MemDepResult &M) const { return Value != M.Value; }
184 bool operator<(const MemDepResult &M) const { return Value < M.Value; }
185 bool operator>(const MemDepResult &M) const { return Value > M.Value; }
188 friend class MemoryDependenceResults;
190 /// Tests if this is a MemDepResult in its dirty/invalid. state.
191 bool isDirty() const { return Value.is<Invalid>(); }
193 static MemDepResult getDirty(Instruction *Inst) {
194 return MemDepResult(ValueTy::create<Invalid>(Inst));
198 /// This is an entry in the NonLocalDepInfo cache.
200 /// For each BasicBlock (the BB entry) it keeps a MemDepResult.
201 class NonLocalDepEntry {
206 NonLocalDepEntry(BasicBlock *bb, MemDepResult result)
207 : BB(bb), Result(result) {}
209 // This is used for searches.
210 NonLocalDepEntry(BasicBlock *bb) : BB(bb) {}
212 // BB is the sort key, it can't be changed.
213 BasicBlock *getBB() const { return BB; }
215 void setResult(const MemDepResult &R) { Result = R; }
217 const MemDepResult &getResult() const { return Result; }
219 bool operator<(const NonLocalDepEntry &RHS) const { return BB < RHS.BB; }
222 /// This is a result from a NonLocal dependence query.
224 /// For each BasicBlock (the BB entry) it keeps a MemDepResult and the
225 /// (potentially phi translated) address that was live in the block.
226 class NonLocalDepResult {
227 NonLocalDepEntry Entry;
231 NonLocalDepResult(BasicBlock *bb, MemDepResult result, Value *address)
232 : Entry(bb, result), Address(address) {}
234 // BB is the sort key, it can't be changed.
235 BasicBlock *getBB() const { return Entry.getBB(); }
237 void setResult(const MemDepResult &R, Value *Addr) {
242 const MemDepResult &getResult() const { return Entry.getResult(); }
244 /// Returns the address of this pointer in this block.
246 /// This can be different than the address queried for the non-local result
247 /// because of phi translation. This returns null if the address was not
248 /// available in a block (i.e. because phi translation failed) or if this is
249 /// a cached result and that address was deleted.
251 /// The address is always null for a non-local 'call' dependence.
252 Value *getAddress() const { return Address; }
255 /// Provides a lazy, caching interface for making common memory aliasing
256 /// information queries, backed by LLVM's alias analysis passes.
258 /// The dependency information returned is somewhat unusual, but is pragmatic.
259 /// If queried about a store or call that might modify memory, the analysis
260 /// will return the instruction[s] that may either load from that memory or
261 /// store to it. If queried with a load or call that can never modify memory,
262 /// the analysis will return calls and stores that might modify the pointer,
263 /// but generally does not return loads unless a) they are volatile, or
264 /// b) they load from *must-aliased* pointers. Returning a dependence on
265 /// must-alias'd pointers instead of all pointers interacts well with the
266 /// internal caching mechanism.
267 class MemoryDependenceResults {
268 // A map from instructions to their dependency.
269 typedef DenseMap<Instruction *, MemDepResult> LocalDepMapType;
270 LocalDepMapType LocalDeps;
273 typedef std::vector<NonLocalDepEntry> NonLocalDepInfo;
276 /// A pair<Value*, bool> where the bool is true if the dependence is a read
277 /// only dependence, false if read/write.
278 typedef PointerIntPair<const Value *, 1, bool> ValueIsLoadPair;
280 /// This pair is used when caching information for a block.
282 /// If the pointer is null, the cache value is not a full query that starts
283 /// at the specified block. If non-null, the bool indicates whether or not
284 /// the contents of the block was skipped.
285 typedef PointerIntPair<BasicBlock *, 1, bool> BBSkipFirstBlockPair;
287 /// This record is the information kept for each (value, is load) pair.
288 struct NonLocalPointerInfo {
289 /// The pair of the block and the skip-first-block flag.
290 BBSkipFirstBlockPair Pair;
291 /// The results of the query for each relevant block.
292 NonLocalDepInfo NonLocalDeps;
293 /// The maximum size of the dereferences of the pointer.
295 /// May be UnknownSize if the sizes are unknown.
297 /// The AA tags associated with dereferences of the pointer.
299 /// The members may be null if there are no tags or conflicting tags.
302 NonLocalPointerInfo() : Size(MemoryLocation::UnknownSize) {}
305 /// This map stores the cached results of doing a pointer lookup at the
306 /// bottom of a block.
308 /// The key of this map is the pointer+isload bit, the value is a list of
309 /// <bb->result> mappings.
310 typedef DenseMap<ValueIsLoadPair, NonLocalPointerInfo>
311 CachedNonLocalPointerInfo;
312 CachedNonLocalPointerInfo NonLocalPointerDeps;
314 // A map from instructions to their non-local pointer dependencies.
315 typedef DenseMap<Instruction *, SmallPtrSet<ValueIsLoadPair, 4>>
316 ReverseNonLocalPtrDepTy;
317 ReverseNonLocalPtrDepTy ReverseNonLocalPtrDeps;
319 /// This is the instruction we keep for each cached access that we have for
322 /// The pointer is an owning pointer and the bool indicates whether we have
323 /// any dirty bits in the set.
324 typedef std::pair<NonLocalDepInfo, bool> PerInstNLInfo;
326 // A map from instructions to their non-local dependencies.
327 typedef DenseMap<Instruction *, PerInstNLInfo> NonLocalDepMapType;
329 NonLocalDepMapType NonLocalDeps;
331 // A reverse mapping from dependencies to the dependees. This is
332 // used when removing instructions to keep the cache coherent.
333 typedef DenseMap<Instruction *, SmallPtrSet<Instruction *, 4>>
335 ReverseDepMapType ReverseLocalDeps;
337 // A reverse mapping from dependencies to the non-local dependees.
338 ReverseDepMapType ReverseNonLocalDeps;
340 /// Current AA implementation, just a cache.
343 const TargetLibraryInfo &TLI;
345 PredIteratorCache PredCache;
348 MemoryDependenceResults(AliasAnalysis &AA, AssumptionCache &AC,
349 const TargetLibraryInfo &TLI,
351 : AA(AA), AC(AC), TLI(TLI), DT(DT) {}
353 /// Returns the instruction on which a memory operation depends.
355 /// See the class comment for more details. It is illegal to call this on
356 /// non-memory instructions.
357 MemDepResult getDependency(Instruction *QueryInst);
359 /// Perform a full dependency query for the specified call, returning the set
360 /// of blocks that the value is potentially live across.
362 /// The returned set of results will include a "NonLocal" result for all
363 /// blocks where the value is live across.
365 /// This method assumes the instruction returns a "NonLocal" dependency
366 /// within its own block.
368 /// This returns a reference to an internal data structure that may be
369 /// invalidated on the next non-local query or when an instruction is
370 /// removed. Clients must copy this data if they want it around longer than
372 const NonLocalDepInfo &getNonLocalCallDependency(CallSite QueryCS);
374 /// Perform a full dependency query for an access to the QueryInst's
375 /// specified memory location, returning the set of instructions that either
376 /// define or clobber the value.
378 /// Warning: For a volatile query instruction, the dependencies will be
379 /// accurate, and thus usable for reordering, but it is never legal to
380 /// remove the query instruction.
382 /// This method assumes the pointer has a "NonLocal" dependency within
383 /// QueryInst's parent basic block.
384 void getNonLocalPointerDependency(Instruction *QueryInst,
385 SmallVectorImpl<NonLocalDepResult> &Result);
387 /// Removes an instruction from the dependence analysis, updating the
388 /// dependence of instructions that previously depended on it.
389 void removeInstruction(Instruction *InstToRemove);
391 /// Invalidates cached information about the specified pointer, because it
392 /// may be too conservative in memdep.
394 /// This is an optional call that can be used when the client detects an
395 /// equivalence between the pointer and some other value and replaces the
396 /// other value with ptr. This can make Ptr available in more places that
397 /// cached info does not necessarily keep.
398 void invalidateCachedPointerInfo(Value *Ptr);
400 /// Clears the PredIteratorCache info.
402 /// This needs to be done when the CFG changes, e.g., due to splitting
404 void invalidateCachedPredecessors();
406 /// Returns the instruction on which a memory location depends.
408 /// If isLoad is true, this routine ignores may-aliases with read-only
409 /// operations. If isLoad is false, this routine ignores may-aliases
410 /// with reads from read-only locations. If possible, pass the query
411 /// instruction as well; this function may take advantage of the metadata
412 /// annotated to the query instruction to refine the result.
414 /// Note that this is an uncached query, and thus may be inefficient.
415 MemDepResult getPointerDependencyFrom(const MemoryLocation &Loc, bool isLoad,
416 BasicBlock::iterator ScanIt,
418 Instruction *QueryInst = nullptr);
420 MemDepResult getSimplePointerDependencyFrom(const MemoryLocation &MemLoc,
422 BasicBlock::iterator ScanIt,
424 Instruction *QueryInst);
426 /// This analysis looks for other loads and stores with invariant.group
427 /// metadata and the same pointer operand. Returns Unknown if it does not
428 /// find anything, and Def if it can be assumed that 2 instructions load or
429 /// store the same value.
430 /// FIXME: This analysis works only on single block because of restrictions
431 /// at the call site.
432 MemDepResult getInvariantGroupPointerDependency(LoadInst *LI, BasicBlock *BB);
434 /// Looks at a memory location for a load (specified by MemLocBase, Offs, and
435 /// Size) and compares it against a load.
437 /// If the specified load could be safely widened to a larger integer load
438 /// that is 1) still efficient, 2) safe for the target, and 3) would provide
439 /// the specified memory location value, then this function returns the size
440 /// in bytes of the load width to use. If not, this returns zero.
441 static unsigned getLoadLoadClobberFullWidthSize(const Value *MemLocBase,
446 /// Release memory in caches.
447 void releaseMemory();
450 MemDepResult getCallSiteDependencyFrom(CallSite C, bool isReadOnlyCall,
451 BasicBlock::iterator ScanIt,
453 bool getNonLocalPointerDepFromBB(Instruction *QueryInst,
454 const PHITransAddr &Pointer,
455 const MemoryLocation &Loc, bool isLoad,
457 SmallVectorImpl<NonLocalDepResult> &Result,
458 DenseMap<BasicBlock *, Value *> &Visited,
459 bool SkipFirstBlock = false);
460 MemDepResult GetNonLocalInfoForBlock(Instruction *QueryInst,
461 const MemoryLocation &Loc, bool isLoad,
462 BasicBlock *BB, NonLocalDepInfo *Cache,
463 unsigned NumSortedEntries);
465 void RemoveCachedNonLocalPointerDependencies(ValueIsLoadPair P);
467 void verifyRemoved(Instruction *Inst) const;
470 /// An analysis that produces \c MemoryDependenceResults for a function.
472 /// This is essentially a no-op because the results are computed entirely
474 class MemoryDependenceAnalysis
475 : public AnalysisInfoMixin<MemoryDependenceAnalysis> {
476 friend AnalysisInfoMixin<MemoryDependenceAnalysis>;
480 typedef MemoryDependenceResults Result;
482 MemoryDependenceResults run(Function &F, AnalysisManager<Function> &AM);
485 /// A wrapper analysis pass for the legacy pass manager that exposes a \c
486 /// MemoryDepnedenceResults instance.
487 class MemoryDependenceWrapperPass : public FunctionPass {
488 Optional<MemoryDependenceResults> MemDep;
490 MemoryDependenceWrapperPass();
491 ~MemoryDependenceWrapperPass() override;
494 /// Pass Implementation stuff. This doesn't do any analysis eagerly.
495 bool runOnFunction(Function &) override;
497 /// Clean up memory in between runs
498 void releaseMemory() override;
500 /// Does not modify anything. It uses Value Numbering and Alias Analysis.
501 void getAnalysisUsage(AnalysisUsage &AU) const override;
503 MemoryDependenceResults &getMemDep() { return *MemDep; }
506 } // End llvm namespace