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"
29 /// MemDepResult - A memory dependence query can return one of three different
31 /// Normal : The query is dependent on a specific instruction.
32 /// NonLocal: The query does not depend on anything inside this block, but
33 /// we haven't scanned beyond the block to find out what.
34 /// None : The query does not depend on anything: we found the entry
35 /// block or the allocation site of the memory.
38 Invalid = 0, Normal, NonLocal, None
40 typedef PointerIntPair<Instruction*, 2, DepType> PairTy;
42 explicit MemDepResult(PairTy V) : Value(V) {}
44 MemDepResult() : Value(0, Invalid) {}
46 /// get methods: These are static ctor methods for creating various
47 /// MemDepResult kinds.
48 static MemDepResult get(Instruction *Inst) {
49 return MemDepResult(PairTy(Inst, Normal));
51 static MemDepResult getNonLocal() {
52 return MemDepResult(PairTy(0, NonLocal));
54 static MemDepResult getNone() {
55 return MemDepResult(PairTy(0, None));
58 /// isNormal - Return true if this MemDepResult represents a query that is
59 /// a normal instruction dependency.
60 bool isNormal() const { return Value.getInt() == Normal; }
62 /// isNonLocal - Return true if this MemDepResult represents an query that
63 /// is transparent to the start of the block, but where a non-local hasn't
65 bool isNonLocal() const { return Value.getInt() == NonLocal; }
67 /// isNone - Return true if this MemDepResult represents a query that
68 /// doesn't depend on any instruction.
69 bool isNone() const { return Value.getInt() == None; }
71 /// getInst() - If this is a normal dependency, return the instruction that
72 /// is depended on. Otherwise, return null.
73 Instruction *getInst() const { return isNormal() ? Value.getPointer() : 0; }
75 bool operator==(const MemDepResult &M) { return M.Value == Value; }
76 bool operator!=(const MemDepResult &M) { return M.Value != Value; }
79 /// MemoryDependenceAnalysis - This is an analysis that determines, for a
80 /// given memory operation, what preceding memory operations it depends on.
81 /// It builds on alias analysis information, and tries to provide a lazy,
82 /// caching interface to a common kind of alias information query.
84 /// The dependency information returned is somewhat unusual, but is pragmatic.
85 /// If queried about a store or call that might modify memory, the analysis
86 /// will return the instruction[s] that may either load from that memory or
87 /// store to it. If queried with a load or call that can never modify memory,
88 /// the analysis will return calls and stores that might modify the pointer,
89 /// but generally does not return loads unless a) they are volatile, or
90 /// b) they load from *must-aliased* pointers. Returning a dependence on
91 /// must-alias'd pointers instead of all pointers interacts well with the
92 /// internal caching mechanism.
94 class MemoryDependenceAnalysis : public FunctionPass {
95 /// DepType - This enum is used to indicate what flavor of dependence this
96 /// is. If the type is Normal, there is an associated instruction pointer.
98 /// Dirty - Entries with this marker occur in a LocalDeps map or
99 /// NonLocalDeps map when the instruction they previously referenced was
100 /// removed from MemDep. In either case, the entry may include an
101 /// instruction pointer. If so, the pointer is an instruction in the
102 /// block where scanning can start from, saving some work.
104 /// In a default-constructed DepResultTy object, the type will be Dirty
105 /// and the instruction pointer will be null.
109 /// Normal - This is a normal instruction dependence. The pointer member
110 /// of the DepResultTy pair holds the instruction.
113 /// None - This dependence type indicates that the query does not depend
114 /// on any instructions, either because it is not a memory instruction or
115 /// because it scanned to the definition of the memory (alloca/malloc)
119 /// NonLocal - This marker indicates that the query has no dependency in
120 /// the specified block. To find out more, the client should query other
121 /// predecessor blocks.
124 typedef PointerIntPair<Instruction*, 2, DepType> DepResultTy;
126 // A map from instructions to their dependency.
127 typedef DenseMap<Instruction*, DepResultTy> LocalDepMapType;
128 LocalDepMapType LocalDeps;
130 typedef DenseMap<BasicBlock*, DepResultTy> NonLocalDepInfo;
132 /// PerInstNLInfo - This is the instruction we keep for each cached access
133 /// that we have for an instruction. The pointer is an owning pointer and
134 /// the bool indicates whether we have any dirty bits in the set.
135 typedef PointerIntPair<NonLocalDepInfo*, 1, bool> PerInstNLInfo;
137 // A map from instructions to their non-local dependencies.
138 typedef DenseMap<Instruction*, PerInstNLInfo> NonLocalDepMapType;
140 NonLocalDepMapType NonLocalDeps;
142 // A reverse mapping from dependencies to the dependees. This is
143 // used when removing instructions to keep the cache coherent.
144 typedef DenseMap<Instruction*,
145 SmallPtrSet<Instruction*, 4> > ReverseDepMapType;
146 ReverseDepMapType ReverseLocalDeps;
148 // A reverse mapping form dependencies to the non-local dependees.
149 ReverseDepMapType ReverseNonLocalDeps;
152 MemoryDependenceAnalysis() : FunctionPass(&ID) {}
155 /// Pass Implementation stuff. This doesn't do any analysis.
157 bool runOnFunction(Function &) {return false; }
159 /// Clean up memory in between runs
160 void releaseMemory() {
162 for (NonLocalDepMapType::iterator I = NonLocalDeps.begin(),
163 E = NonLocalDeps.end(); I != E; ++I)
164 delete I->second.getPointer();
165 NonLocalDeps.clear();
166 ReverseLocalDeps.clear();
167 ReverseNonLocalDeps.clear();
170 /// getAnalysisUsage - Does not modify anything. It uses Value Numbering
171 /// and Alias Analysis.
173 virtual void getAnalysisUsage(AnalysisUsage &AU) const;
175 /// getDependency - Return the instruction on which a memory operation
176 /// depends. See the class comment for more details.
177 MemDepResult getDependency(Instruction *QueryInst);
179 /// getDependencyFrom - Return the instruction on which the memory operation
180 /// 'QueryInst' depends. This starts scanning from the instruction before
181 /// the position indicated by ScanIt.
183 /// Note that this method does no caching at all. You should use
184 /// getDependency where possible.
185 MemDepResult getDependencyFrom(Instruction *QueryInst,
186 BasicBlock::iterator ScanIt, BasicBlock *BB){
187 return ConvToResult(getDependencyFromInternal(QueryInst, ScanIt, BB));
191 /// getNonLocalDependency - Perform a full dependency query for the
192 /// specified instruction, returning the set of blocks that the value is
193 /// potentially live across. The returned set of results will include a
194 /// "NonLocal" result for all blocks where the value is live across.
196 /// This method assumes the instruction returns a "nonlocal" dependency
197 /// within its own block.
198 void getNonLocalDependency(Instruction *QueryInst,
199 SmallVectorImpl<std::pair<BasicBlock*,
200 MemDepResult> > &Result);
202 /// removeInstruction - Remove an instruction from the dependence analysis,
203 /// updating the dependence of instructions that previously depended on it.
204 void removeInstruction(Instruction *InstToRemove);
207 MemDepResult ConvToResult(DepResultTy R) {
208 if (R.getInt() == Normal)
209 return MemDepResult::get(R.getPointer());
210 if (R.getInt() == NonLocal)
211 return MemDepResult::getNonLocal();
212 assert(R.getInt() == None && "Unknown MemDepResult!");
213 return MemDepResult::getNone();
216 /// verifyRemoved - Verify that the specified instruction does not occur
217 /// in our internal data structures.
218 void verifyRemoved(Instruction *Inst) const;
220 /// getDependencyFromInternal - Return the instruction on which the memory
221 /// operation 'QueryInst' depends. This starts scanning from the
222 /// instruction before the position indicated by ScanIt.
223 DepResultTy getDependencyFromInternal(Instruction *QueryInst,
224 BasicBlock::iterator ScanIt, BasicBlock *BB);
225 DepResultTy getCallSiteDependency(CallSite C, BasicBlock::iterator ScanIt,
229 } // End llvm namespace