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 MemDepResult 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) const { return M.Value == Value; }
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; }
93 friend class MemoryDependenceAnalysis;
94 /// Dirty - Entries with this marker occur in a LocalDeps map or
95 /// NonLocalDeps map when the instruction they previously referenced was
96 /// removed from MemDep. In either case, the entry may include an
97 /// instruction pointer. If so, the pointer is an instruction in the
98 /// block where scanning can start from, saving some work.
100 /// In a default-constructed MemDepResult object, the type will be Dirty
101 /// and the instruction pointer will be null.
104 /// isDirty - Return true if this is a MemDepResult in its dirty/invalid.
106 bool isDirty() const { return Value.getInt() == Invalid; }
108 static MemDepResult getDirty(Instruction *Inst) {
109 return MemDepResult(PairTy(Inst, Invalid));
113 /// MemoryDependenceAnalysis - This is an analysis that determines, for a
114 /// given memory operation, what preceding memory operations it depends on.
115 /// It builds on alias analysis information, and tries to provide a lazy,
116 /// caching interface to a common kind of alias information query.
118 /// The dependency information returned is somewhat unusual, but is pragmatic.
119 /// If queried about a store or call that might modify memory, the analysis
120 /// will return the instruction[s] that may either load from that memory or
121 /// store to it. If queried with a load or call that can never modify memory,
122 /// the analysis will return calls and stores that might modify the pointer,
123 /// but generally does not return loads unless a) they are volatile, or
124 /// b) they load from *must-aliased* pointers. Returning a dependence on
125 /// must-alias'd pointers instead of all pointers interacts well with the
126 /// internal caching mechanism.
128 class MemoryDependenceAnalysis : public FunctionPass {
129 // A map from instructions to their dependency.
130 typedef DenseMap<Instruction*, MemDepResult> LocalDepMapType;
131 LocalDepMapType LocalDeps;
134 typedef std::pair<BasicBlock*, MemDepResult> NonLocalDepEntry;
135 typedef std::vector<NonLocalDepEntry> NonLocalDepInfo;
138 /// PerInstNLInfo - This is the instruction we keep for each cached access
139 /// that we have for an instruction. The pointer is an owning pointer and
140 /// the bool indicates whether we have any dirty bits in the set.
141 typedef std::pair<NonLocalDepInfo, bool> PerInstNLInfo;
143 // A map from instructions to their non-local dependencies.
144 typedef DenseMap<Instruction*, PerInstNLInfo> NonLocalDepMapType;
146 NonLocalDepMapType NonLocalDeps;
148 // A reverse mapping from dependencies to the dependees. This is
149 // used when removing instructions to keep the cache coherent.
150 typedef DenseMap<Instruction*,
151 SmallPtrSet<Instruction*, 4> > ReverseDepMapType;
152 ReverseDepMapType ReverseLocalDeps;
154 // A reverse mapping form dependencies to the non-local dependees.
155 ReverseDepMapType ReverseNonLocalDeps;
157 /// Current AA implementation, just a cache.
161 MemoryDependenceAnalysis() : FunctionPass(&ID) {}
164 /// Pass Implementation stuff. This doesn't do any analysis eagerly.
165 bool runOnFunction(Function &);
167 /// Clean up memory in between runs
168 void releaseMemory() {
170 NonLocalDeps.clear();
171 NonLocalDeps.clear();
172 ReverseLocalDeps.clear();
173 ReverseNonLocalDeps.clear();
176 /// getAnalysisUsage - Does not modify anything. It uses Value Numbering
177 /// and Alias Analysis.
179 virtual void getAnalysisUsage(AnalysisUsage &AU) const;
181 /// getDependency - Return the instruction on which a memory operation
182 /// depends. See the class comment for more details.
183 MemDepResult getDependency(Instruction *QueryInst);
185 /// getDependencyFrom - Return the instruction on which the memory operation
186 /// 'QueryInst' depends. This starts scanning from the instruction before
187 /// the position indicated by ScanIt.
189 /// Note that this method does no caching at all. You should use
190 /// getDependency where possible.
191 MemDepResult getDependencyFrom(Instruction *QueryInst,
192 BasicBlock::iterator ScanIt, BasicBlock *BB);
195 /// getNonLocalDependency - Perform a full dependency query for the
196 /// specified instruction, returning the set of blocks that the value is
197 /// potentially live across. The returned set of results will include a
198 /// "NonLocal" result for all blocks where the value is live across.
200 /// This method assumes the instruction returns a "NonLocal" dependency
201 /// within its own block.
203 /// This returns a reference to an internal data structure that may be
204 /// invalidated on the next non-local query or when an instruction is
205 /// removed. Clients must copy this data if they want it around longer than
207 const NonLocalDepInfo &getNonLocalDependency(Instruction *QueryInst);
209 /// removeInstruction - Remove an instruction from the dependence analysis,
210 /// updating the dependence of instructions that previously depended on it.
211 void removeInstruction(Instruction *InstToRemove);
214 /// verifyRemoved - Verify that the specified instruction does not occur
215 /// in our internal data structures.
216 void verifyRemoved(Instruction *Inst) const;
218 MemDepResult getCallSiteDependency(CallSite C, BasicBlock::iterator ScanIt,
222 } // End llvm namespace