Introduce a new MemDep::getNonLocalPointerDependency
[oota-llvm.git] / include / llvm / Analysis / MemoryDependenceAnalysis.h
1 //===- llvm/Analysis/MemoryDependenceAnalysis.h - Memory Deps  --*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file defines the MemoryDependenceAnalysis analysis pass.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #ifndef LLVM_ANALYSIS_MEMORY_DEPENDENCE_H
15 #define LLVM_ANALYSIS_MEMORY_DEPENDENCE_H
16
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"
22
23 namespace llvm {
24   class Function;
25   class FunctionPass;
26   class Instruction;
27   class CallSite;
28   class AliasAnalysis;
29   class TargetData;
30   class MemoryDependenceAnalysis;
31   
32   /// MemDepResult - A memory dependence query can return one of three different
33   /// answers, described below.
34   class MemDepResult {
35     enum DepType {
36       /// Invalid - Clients of MemDep never see this.
37       Invalid = 0,
38       
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
43       /// care about.
44       Clobber,
45
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
62       ///      are the same.
63       Def,
64       
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.
68       NonLocal
69     };
70     typedef PointerIntPair<Instruction*, 2, DepType> PairTy;
71     PairTy Value;
72     explicit MemDepResult(PairTy V) : Value(V) {}
73   public:
74     MemDepResult() : Value(0, Invalid) {}
75     
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));
80     }
81     static MemDepResult getClobber(Instruction *Inst) {
82       return MemDepResult(PairTy(Inst, Clobber));
83     }
84     static MemDepResult getNonLocal() {
85       return MemDepResult(PairTy(0, NonLocal));
86     }
87
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; }
91
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; }
95     
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
98     /// been done.
99     bool isNonLocal() const { return Value.getInt() == NonLocal; }
100     
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(); }
104     
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; }
109   private:
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.
116     ///
117     /// In a default-constructed MemDepResult object, the type will be Dirty
118     /// and the instruction pointer will be null.
119     ///
120          
121     /// isDirty - Return true if this is a MemDepResult in its dirty/invalid.
122     /// state.
123     bool isDirty() const { return Value.getInt() == Invalid; }
124     
125     static MemDepResult getDirty(Instruction *Inst) {
126       return MemDepResult(PairTy(Inst, Invalid));
127     }
128   };
129
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.
134   ///
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.
144   ///
145   class MemoryDependenceAnalysis : public FunctionPass {
146     // A map from instructions to their dependency.
147     typedef DenseMap<Instruction*, MemDepResult> LocalDepMapType;
148     LocalDepMapType LocalDeps;
149
150   public:
151     typedef std::pair<BasicBlock*, MemDepResult> NonLocalDepEntry;
152     typedef std::vector<NonLocalDepEntry> NonLocalDepInfo;
153   private:
154     
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;
159     
160     // A map from instructions to their non-local dependencies.
161     typedef DenseMap<Instruction*, PerInstNLInfo> NonLocalDepMapType;
162       
163     NonLocalDepMapType NonLocalDeps;
164     
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;
170     
171     // A reverse mapping form dependencies to the non-local dependees.
172     ReverseDepMapType ReverseNonLocalDeps;
173     
174     /// Current AA implementation, just a cache.
175     AliasAnalysis *AA;
176     TargetData *TD;
177   public:
178     MemoryDependenceAnalysis() : FunctionPass(&ID) {}
179     static char ID;
180
181     /// Pass Implementation stuff.  This doesn't do any analysis eagerly.
182     bool runOnFunction(Function &);
183     
184     /// Clean up memory in between runs
185     void releaseMemory() {
186       LocalDeps.clear();
187       NonLocalDeps.clear();
188       NonLocalDeps.clear();
189       ReverseLocalDeps.clear();
190       ReverseNonLocalDeps.clear();
191     }
192
193     /// getAnalysisUsage - Does not modify anything.  It uses Value Numbering
194     /// and Alias Analysis.
195     ///
196     virtual void getAnalysisUsage(AnalysisUsage &AU) const;
197     
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);
202
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.
207     ///
208     /// This method assumes the instruction returns a "NonLocal" dependency
209     /// within its own block.
210     ///
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
214     /// that.
215     const NonLocalDepInfo &getNonLocalDependency(Instruction *QueryInst);
216     
217     
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.
221     ///
222     /// This method assumes the pointer has a "NonLocal" dependency within BB
223     /// and assumes that Result is empty when you call it.
224     ///
225     void getNonLocalPointerDependency(Value *Pointer, bool isLoad,
226                                       BasicBlock *BB,
227                                      SmallVectorImpl<NonLocalDepEntry> &Result);
228     
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);
232     
233   private:
234     
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,
239                                           bool isLoad, 
240                                           BasicBlock::iterator ScanIt,
241                                           BasicBlock *BB);
242     MemDepResult getCallSiteDependencyFrom(CallSite C,
243                                            BasicBlock::iterator ScanIt,
244                                            BasicBlock *BB);
245     
246     /// verifyRemoved - Verify that the specified instruction does not occur
247     /// in our internal data structures.
248     void verifyRemoved(Instruction *Inst) const;
249     
250   };
251
252 } // End llvm namespace
253
254 #endif