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.
38 /// Normal - This is a normal instruction dependence. The pointer member
39 /// of the DepResultTy pair holds the instruction.
42 /// NonLocal - This marker indicates that the query has no dependency in
43 /// the specified block. To find out more, the client should query other
44 /// predecessor blocks.
47 /// None - This dependence type indicates that the query does not depend
48 /// on any instructions, either because it is not a memory instruction or
49 /// because it scanned to the definition of the memory (alloca/malloc)
53 typedef PointerIntPair<Instruction*, 2, DepType> PairTy;
55 explicit MemDepResult(PairTy V) : Value(V) {}
57 MemDepResult() : Value(0, Invalid) {}
59 /// get methods: These are static ctor methods for creating various
60 /// MemDepResult kinds.
61 static MemDepResult get(Instruction *Inst) {
62 return MemDepResult(PairTy(Inst, Normal));
64 static MemDepResult getNonLocal() {
65 return MemDepResult(PairTy(0, NonLocal));
67 static MemDepResult getNone() {
68 return MemDepResult(PairTy(0, None));
71 /// isNormal - Return true if this MemDepResult represents a query that is
72 /// a normal instruction dependency.
73 bool isNormal() const { return Value.getInt() == Normal; }
75 /// isNonLocal - Return true if this MemDepResult represents an query that
76 /// is transparent to the start of the block, but where a non-local hasn't
78 bool isNonLocal() const { return Value.getInt() == NonLocal; }
80 /// isNone - Return true if this MemDepResult represents a query that
81 /// doesn't depend on any instruction.
82 bool isNone() const { return Value.getInt() == None; }
84 /// getInst() - If this is a normal dependency, return the instruction that
85 /// is depended on. Otherwise, return null.
86 Instruction *getInst() const { return Value.getPointer(); }
88 bool operator==(const MemDepResult &M) { return M.Value == Value; }
89 bool operator!=(const MemDepResult &M) { return M.Value != Value; }
91 friend class MemoryDependenceAnalysis;
92 /// Dirty - Entries with this marker occur in a LocalDeps map or
93 /// NonLocalDeps map when the instruction they previously referenced was
94 /// removed from MemDep. In either case, the entry may include an
95 /// instruction pointer. If so, the pointer is an instruction in the
96 /// block where scanning can start from, saving some work.
98 /// In a default-constructed DepResultTy object, the type will be Dirty
99 /// and the instruction pointer will be null.
102 /// isDirty - Return true if this is a MemDepResult in its dirty/invalid.
104 bool isDirty() const { return Value.getInt() == Invalid; }
106 static MemDepResult getDirty(Instruction *Inst) {
107 return MemDepResult(PairTy(Inst, Invalid));
111 /// MemoryDependenceAnalysis - This is an analysis that determines, for a
112 /// given memory operation, what preceding memory operations it depends on.
113 /// It builds on alias analysis information, and tries to provide a lazy,
114 /// caching interface to a common kind of alias information query.
116 /// The dependency information returned is somewhat unusual, but is pragmatic.
117 /// If queried about a store or call that might modify memory, the analysis
118 /// will return the instruction[s] that may either load from that memory or
119 /// store to it. If queried with a load or call that can never modify memory,
120 /// the analysis will return calls and stores that might modify the pointer,
121 /// but generally does not return loads unless a) they are volatile, or
122 /// b) they load from *must-aliased* pointers. Returning a dependence on
123 /// must-alias'd pointers instead of all pointers interacts well with the
124 /// internal caching mechanism.
126 class MemoryDependenceAnalysis : public FunctionPass {
127 // A map from instructions to their dependency.
128 typedef DenseMap<Instruction*, MemDepResult> LocalDepMapType;
129 LocalDepMapType LocalDeps;
131 typedef DenseMap<BasicBlock*, MemDepResult> NonLocalDepInfo;
133 /// PerInstNLInfo - This is the instruction we keep for each cached access
134 /// that we have for an instruction. The pointer is an owning pointer and
135 /// the bool indicates whether we have any dirty bits in the set.
136 typedef PointerIntPair<NonLocalDepInfo*, 1, bool> PerInstNLInfo;
138 // A map from instructions to their non-local dependencies.
139 typedef DenseMap<Instruction*, PerInstNLInfo> NonLocalDepMapType;
141 NonLocalDepMapType NonLocalDeps;
143 // A reverse mapping from dependencies to the dependees. This is
144 // used when removing instructions to keep the cache coherent.
145 typedef DenseMap<Instruction*,
146 SmallPtrSet<Instruction*, 4> > ReverseDepMapType;
147 ReverseDepMapType ReverseLocalDeps;
149 // A reverse mapping form dependencies to the non-local dependees.
150 ReverseDepMapType ReverseNonLocalDeps;
152 /// Current AA implementation, just a cache.
156 MemoryDependenceAnalysis() : FunctionPass(&ID) {}
159 /// Pass Implementation stuff. This doesn't do any analysis eagerly.
160 bool runOnFunction(Function &);
162 /// Clean up memory in between runs
163 void releaseMemory() {
165 for (NonLocalDepMapType::iterator I = NonLocalDeps.begin(),
166 E = NonLocalDeps.end(); I != E; ++I)
167 delete I->second.getPointer();
168 NonLocalDeps.clear();
169 ReverseLocalDeps.clear();
170 ReverseNonLocalDeps.clear();
173 /// getAnalysisUsage - Does not modify anything. It uses Value Numbering
174 /// and Alias Analysis.
176 virtual void getAnalysisUsage(AnalysisUsage &AU) const;
178 /// getDependency - Return the instruction on which a memory operation
179 /// depends. See the class comment for more details.
180 MemDepResult getDependency(Instruction *QueryInst);
182 /// getDependencyFrom - Return the instruction on which the memory operation
183 /// 'QueryInst' depends. This starts scanning from the instruction before
184 /// the position indicated by ScanIt.
186 /// Note that this method does no caching at all. You should use
187 /// getDependency where possible.
188 MemDepResult getDependencyFrom(Instruction *QueryInst,
189 BasicBlock::iterator ScanIt, BasicBlock *BB);
192 /// getNonLocalDependency - Perform a full dependency query for the
193 /// specified instruction, returning the set of blocks that the value is
194 /// potentially live across. The returned set of results will include a
195 /// "NonLocal" result for all blocks where the value is live across.
197 /// This method assumes the instruction returns a "nonlocal" dependency
198 /// within its own block.
199 void getNonLocalDependency(Instruction *QueryInst,
200 SmallVectorImpl<std::pair<BasicBlock*,
201 MemDepResult> > &Result);
203 /// removeInstruction - Remove an instruction from the dependence analysis,
204 /// updating the dependence of instructions that previously depended on it.
205 void removeInstruction(Instruction *InstToRemove);
208 /// verifyRemoved - Verify that the specified instruction does not occur
209 /// in our internal data structures.
210 void verifyRemoved(Instruction *Inst) const;
212 MemDepResult getCallSiteDependency(CallSite C, BasicBlock::iterator ScanIt,
216 } // End llvm namespace