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 /// Normal - This is a normal instruction dependence. The pointer member
40 /// of the MemDepResult pair holds the instruction.
43 /// NonLocal - This marker indicates that the query has no dependency in
44 /// the specified block. To find out more, the client should query other
45 /// predecessor blocks.
48 /// None - This dependence type indicates that the query does not depend
49 /// on any instructions, either because it is not a memory instruction or
50 /// because it scanned to the definition of the memory (alloca/malloc)
54 typedef PointerIntPair<Instruction*, 2, DepType> PairTy;
56 explicit MemDepResult(PairTy V) : Value(V) {}
58 MemDepResult() : Value(0, Invalid) {}
60 /// get methods: These are static ctor methods for creating various
61 /// MemDepResult kinds.
62 static MemDepResult get(Instruction *Inst) {
63 return MemDepResult(PairTy(Inst, Normal));
65 static MemDepResult getNonLocal() {
66 return MemDepResult(PairTy(0, NonLocal));
68 static MemDepResult getNone() {
69 return MemDepResult(PairTy(0, None));
72 /// isNormal - Return true if this MemDepResult represents a query that is
73 /// a normal instruction dependency.
74 bool isNormal() const { return Value.getInt() == Normal; }
76 /// isNonLocal - Return true if this MemDepResult represents an query that
77 /// is transparent to the start of the block, but where a non-local hasn't
79 bool isNonLocal() const { return Value.getInt() == NonLocal; }
81 /// isNone - Return true if this MemDepResult represents a query that
82 /// doesn't depend on any instruction.
83 bool isNone() const { return Value.getInt() == None; }
85 /// getInst() - If this is a normal dependency, return the instruction that
86 /// is depended on. Otherwise, return null.
87 Instruction *getInst() const { return Value.getPointer(); }
89 bool operator==(const MemDepResult &M) const { return M.Value == Value; }
90 bool operator!=(const MemDepResult &M) const { return M.Value != Value; }
91 bool operator<(const MemDepResult &M) const { return M.Value < Value; }
92 bool operator>(const MemDepResult &M) const { return M.Value > Value; }
94 friend class MemoryDependenceAnalysis;
95 /// Dirty - Entries with this marker occur in a LocalDeps map or
96 /// NonLocalDeps map when the instruction they previously referenced was
97 /// removed from MemDep. In either case, the entry may include an
98 /// instruction pointer. If so, the pointer is an instruction in the
99 /// block where scanning can start from, saving some work.
101 /// In a default-constructed MemDepResult object, the type will be Dirty
102 /// and the instruction pointer will be null.
105 /// isDirty - Return true if this is a MemDepResult in its dirty/invalid.
107 bool isDirty() const { return Value.getInt() == Invalid; }
109 static MemDepResult getDirty(Instruction *Inst) {
110 return MemDepResult(PairTy(Inst, Invalid));
114 /// MemoryDependenceAnalysis - This is an analysis that determines, for a
115 /// given memory operation, what preceding memory operations it depends on.
116 /// It builds on alias analysis information, and tries to provide a lazy,
117 /// caching interface to a common kind of alias information query.
119 /// The dependency information returned is somewhat unusual, but is pragmatic.
120 /// If queried about a store or call that might modify memory, the analysis
121 /// will return the instruction[s] that may either load from that memory or
122 /// store to it. If queried with a load or call that can never modify memory,
123 /// the analysis will return calls and stores that might modify the pointer,
124 /// but generally does not return loads unless a) they are volatile, or
125 /// b) they load from *must-aliased* pointers. Returning a dependence on
126 /// must-alias'd pointers instead of all pointers interacts well with the
127 /// internal caching mechanism.
129 class MemoryDependenceAnalysis : public FunctionPass {
130 // A map from instructions to their dependency.
131 typedef DenseMap<Instruction*, MemDepResult> LocalDepMapType;
132 LocalDepMapType LocalDeps;
135 typedef std::pair<BasicBlock*, MemDepResult> NonLocalDepEntry;
136 typedef std::vector<NonLocalDepEntry> NonLocalDepInfo;
139 /// PerInstNLInfo - This is the instruction we keep for each cached access
140 /// that we have for an instruction. The pointer is an owning pointer and
141 /// the bool indicates whether we have any dirty bits in the set.
142 typedef std::pair<NonLocalDepInfo, bool> PerInstNLInfo;
144 // A map from instructions to their non-local dependencies.
145 typedef DenseMap<Instruction*, PerInstNLInfo> NonLocalDepMapType;
147 NonLocalDepMapType NonLocalDeps;
149 // A reverse mapping from dependencies to the dependees. This is
150 // used when removing instructions to keep the cache coherent.
151 typedef DenseMap<Instruction*,
152 SmallPtrSet<Instruction*, 4> > ReverseDepMapType;
153 ReverseDepMapType ReverseLocalDeps;
155 // A reverse mapping form dependencies to the non-local dependees.
156 ReverseDepMapType ReverseNonLocalDeps;
158 /// Current AA implementation, just a cache.
162 MemoryDependenceAnalysis() : FunctionPass(&ID) {}
165 /// Pass Implementation stuff. This doesn't do any analysis eagerly.
166 bool runOnFunction(Function &);
168 /// Clean up memory in between runs
169 void releaseMemory() {
171 NonLocalDeps.clear();
172 NonLocalDeps.clear();
173 ReverseLocalDeps.clear();
174 ReverseNonLocalDeps.clear();
177 /// getAnalysisUsage - Does not modify anything. It uses Value Numbering
178 /// and Alias Analysis.
180 virtual void getAnalysisUsage(AnalysisUsage &AU) const;
182 /// getDependency - Return the instruction on which a memory operation
183 /// depends. See the class comment for more details. It is illegal to call
184 /// this on non-memory instructions.
185 MemDepResult getDependency(Instruction *QueryInst);
187 /// getDependencyFrom - Return the instruction on which the memory operation
188 /// 'QueryInst' depends. This starts scanning from the instruction before
189 /// the position indicated by ScanIt.
191 /// Note that this method does no caching at all. You should use
192 /// getDependency where possible.
193 MemDepResult getDependencyFrom(Instruction *QueryInst,
194 BasicBlock::iterator ScanIt, BasicBlock *BB);
197 /// getNonLocalDependency - Perform a full dependency query for the
198 /// specified instruction, returning the set of blocks that the value is
199 /// potentially live across. The returned set of results will include a
200 /// "NonLocal" result for all blocks where the value is live across.
202 /// This method assumes the instruction returns a "NonLocal" dependency
203 /// within its own block.
205 /// This returns a reference to an internal data structure that may be
206 /// invalidated on the next non-local query or when an instruction is
207 /// removed. Clients must copy this data if they want it around longer than
209 const NonLocalDepInfo &getNonLocalDependency(Instruction *QueryInst);
211 /// removeInstruction - Remove an instruction from the dependence analysis,
212 /// updating the dependence of instructions that previously depended on it.
213 void removeInstruction(Instruction *InstToRemove);
216 /// verifyRemoved - Verify that the specified instruction does not occur
217 /// in our internal data structures.
218 void verifyRemoved(Instruction *Inst) const;
220 MemDepResult getCallSiteDependency(CallSite C, BasicBlock::iterator ScanIt,
224 } // End llvm namespace