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/PointerIntPair.h"
30 class MemoryDependenceAnalysis;
32 /// MemDepResult - A memory dependence query can return one of three different
33 /// answers, described below.
36 /// Invalid - Clients of MemDep never see this.
39 /// Clobber - This is a dependence on the specified instruction which
40 /// clobbers the desired value. The pointer member of the MemDepResult
41 /// pair holds the instruction that clobbers the memory. For example,
42 /// this occurs when we see a may-aliased store to the memory location we
46 /// Def - This is a dependence on the specified instruction which
47 /// defines/produces the desired memory location. The pointer member of
48 /// the MemDepResult pair holds the instruction that defines the memory.
49 /// Cases of interest:
50 /// 1. This could be a load or store for dependence queries on
51 /// load/store. The value loaded or stored is the produced value.
52 /// Note that the pointer operand may be different than that of the
53 /// queried pointer due to must aliases and phi translation. Note
54 /// that the def may not be the same type as the query, the pointers
55 /// may just be must aliases.
56 /// 2. For loads and stores, this could be an allocation instruction. In
57 /// this case, the load is loading an undef value or a store is the
58 /// first store to (that part of) the allocation.
59 /// 3. Dependence queries on calls return Def only when they are
60 /// readonly calls with identical callees and no intervening
61 /// clobbers. No validation is done that the operands to the calls
65 /// NonLocal - This marker indicates that the query has no dependency in
66 /// the specified block. To find out more, the client should query other
67 /// predecessor blocks.
70 typedef PointerIntPair<Instruction*, 2, DepType> PairTy;
72 explicit MemDepResult(PairTy V) : Value(V) {}
74 MemDepResult() : Value(0, Invalid) {}
76 /// get methods: These are static ctor methods for creating various
77 /// MemDepResult kinds.
78 static MemDepResult getDef(Instruction *Inst) {
79 return MemDepResult(PairTy(Inst, Def));
81 static MemDepResult getClobber(Instruction *Inst) {
82 return MemDepResult(PairTy(Inst, Clobber));
84 static MemDepResult getNonLocal() {
85 return MemDepResult(PairTy(0, NonLocal));
88 /// isClobber - Return true if this MemDepResult represents a query that is
89 /// a instruction clobber dependency.
90 bool isClobber() const { return Value.getInt() == Clobber; }
92 /// isDef - Return true if this MemDepResult represents a query that is
93 /// a instruction definition dependency.
94 bool isDef() const { return Value.getInt() == Def; }
96 /// isNonLocal - Return true if this MemDepResult represents an query that
97 /// is transparent to the start of the block, but where a non-local hasn't
99 bool isNonLocal() const { return Value.getInt() == NonLocal; }
101 /// getInst() - If this is a normal dependency, return the instruction that
102 /// is depended on. Otherwise, return null.
103 Instruction *getInst() const { return Value.getPointer(); }
105 bool operator==(const MemDepResult &M) const { return M.Value == Value; }
106 bool operator!=(const MemDepResult &M) const { return M.Value != Value; }
107 bool operator<(const MemDepResult &M) const { return M.Value < Value; }
108 bool operator>(const MemDepResult &M) const { return M.Value > Value; }
110 friend class MemoryDependenceAnalysis;
111 /// Dirty - Entries with this marker occur in a LocalDeps map or
112 /// NonLocalDeps map when the instruction they previously referenced was
113 /// removed from MemDep. In either case, the entry may include an
114 /// instruction pointer. If so, the pointer is an instruction in the
115 /// block where scanning can start from, saving some work.
117 /// In a default-constructed MemDepResult object, the type will be Dirty
118 /// and the instruction pointer will be null.
121 /// isDirty - Return true if this is a MemDepResult in its dirty/invalid.
123 bool isDirty() const { return Value.getInt() == Invalid; }
125 static MemDepResult getDirty(Instruction *Inst) {
126 return MemDepResult(PairTy(Inst, Invalid));
130 /// MemoryDependenceAnalysis - This is an analysis that determines, for a
131 /// given memory operation, what preceding memory operations it depends on.
132 /// It builds on alias analysis information, and tries to provide a lazy,
133 /// caching interface to a common kind of alias information query.
135 /// The dependency information returned is somewhat unusual, but is pragmatic.
136 /// If queried about a store or call that might modify memory, the analysis
137 /// will return the instruction[s] that may either load from that memory or
138 /// store to it. If queried with a load or call that can never modify memory,
139 /// the analysis will return calls and stores that might modify the pointer,
140 /// but generally does not return loads unless a) they are volatile, or
141 /// b) they load from *must-aliased* pointers. Returning a dependence on
142 /// must-alias'd pointers instead of all pointers interacts well with the
143 /// internal caching mechanism.
145 class MemoryDependenceAnalysis : public FunctionPass {
146 // A map from instructions to their dependency.
147 typedef DenseMap<Instruction*, MemDepResult> LocalDepMapType;
148 LocalDepMapType LocalDeps;
151 typedef std::pair<BasicBlock*, MemDepResult> NonLocalDepEntry;
152 typedef std::vector<NonLocalDepEntry> NonLocalDepInfo;
155 /// PerInstNLInfo - This is the instruction we keep for each cached access
156 /// that we have for an instruction. The pointer is an owning pointer and
157 /// the bool indicates whether we have any dirty bits in the set.
158 typedef std::pair<NonLocalDepInfo, bool> PerInstNLInfo;
160 // A map from instructions to their non-local dependencies.
161 typedef DenseMap<Instruction*, PerInstNLInfo> NonLocalDepMapType;
163 NonLocalDepMapType NonLocalDeps;
165 // A reverse mapping from dependencies to the dependees. This is
166 // used when removing instructions to keep the cache coherent.
167 typedef DenseMap<Instruction*,
168 SmallPtrSet<Instruction*, 4> > ReverseDepMapType;
169 ReverseDepMapType ReverseLocalDeps;
171 // A reverse mapping form dependencies to the non-local dependees.
172 ReverseDepMapType ReverseNonLocalDeps;
174 /// Current AA implementation, just a cache.
178 MemoryDependenceAnalysis() : FunctionPass(&ID) {}
181 /// Pass Implementation stuff. This doesn't do any analysis eagerly.
182 bool runOnFunction(Function &);
184 /// Clean up memory in between runs
185 void releaseMemory() {
187 NonLocalDeps.clear();
188 NonLocalDeps.clear();
189 ReverseLocalDeps.clear();
190 ReverseNonLocalDeps.clear();
193 /// getAnalysisUsage - Does not modify anything. It uses Value Numbering
194 /// and Alias Analysis.
196 virtual void getAnalysisUsage(AnalysisUsage &AU) const;
198 /// getDependency - Return the instruction on which a memory operation
199 /// depends. See the class comment for more details. It is illegal to call
200 /// this on non-memory instructions.
201 MemDepResult getDependency(Instruction *QueryInst);
203 /// getNonLocalDependency - Perform a full dependency query for the
204 /// specified instruction, returning the set of blocks that the value is
205 /// potentially live across. The returned set of results will include a
206 /// "NonLocal" result for all blocks where the value is live across.
208 /// This method assumes the instruction returns a "NonLocal" dependency
209 /// within its own block.
211 /// This returns a reference to an internal data structure that may be
212 /// invalidated on the next non-local query or when an instruction is
213 /// removed. Clients must copy this data if they want it around longer than
215 const NonLocalDepInfo &getNonLocalDependency(Instruction *QueryInst);
218 /// getNonLocalPointerDependency - Perform a full dependency query for an
219 /// access to the specified (non-volatile) memory location, returning the
220 /// set of instructions that either define or clobber the value.
222 /// This method assumes the pointer has a "NonLocal" dependency within BB
223 /// and assumes that Result is empty when you call it.
225 void getNonLocalPointerDependency(Value *Pointer, bool isLoad,
227 SmallVectorImpl<NonLocalDepEntry> &Result);
229 /// removeInstruction - Remove an instruction from the dependence analysis,
230 /// updating the dependence of instructions that previously depended on it.
231 void removeInstruction(Instruction *InstToRemove);
235 /// getDependencyFrom - Return the instruction on which the memory location
236 /// '*Pointer' depends. This starts scanning from the instruction before
237 /// the position indicated by ScanIt.
238 MemDepResult getPointerDependencyFrom(Value *Pointer, uint64_t MemSize,
240 BasicBlock::iterator ScanIt,
242 MemDepResult getCallSiteDependencyFrom(CallSite C,
243 BasicBlock::iterator ScanIt,
246 /// verifyRemoved - Verify that the specified instruction does not occur
247 /// in our internal data structures.
248 void verifyRemoved(Instruction *Inst) const;
252 } // End llvm namespace