Teach BasicAA::getModRefInfo(CallSite, CallSite) some
[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/OwningPtr.h"
22 #include "llvm/ADT/PointerIntPair.h"
23
24 namespace llvm {
25   class Function;
26   class FunctionPass;
27   class Instruction;
28   class CallSite;
29   class AliasAnalysis;
30   class TargetData;
31   class MemoryDependenceAnalysis;
32   class PredIteratorCache;
33   
34   /// MemDepResult - A memory dependence query can return one of three different
35   /// answers, described below.
36   class MemDepResult {
37     enum DepType {
38       /// Invalid - Clients of MemDep never see this.
39       Invalid = 0,
40       
41       /// Clobber - This is a dependence on the specified instruction which
42       /// clobbers the desired value.  The pointer member of the MemDepResult
43       /// pair holds the instruction that clobbers the memory.  For example,
44       /// this occurs when we see a may-aliased store to the memory location we
45       /// care about.
46       Clobber,
47
48       /// Def - This is a dependence on the specified instruction which
49       /// defines/produces the desired memory location.  The pointer member of
50       /// the MemDepResult pair holds the instruction that defines the memory.
51       /// Cases of interest:
52       ///   1. This could be a load or store for dependence queries on
53       ///      load/store.  The value loaded or stored is the produced value.
54       ///      Note that the pointer operand may be different than that of the
55       ///      queried pointer due to must aliases and phi translation.  Note
56       ///      that the def may not be the same type as the query, the pointers
57       ///      may just be must aliases.
58       ///   2. For loads and stores, this could be an allocation instruction. In
59       ///      this case, the load is loading an undef value or a store is the
60       ///      first store to (that part of) the allocation.
61       ///   3. Dependence queries on calls return Def only when they are
62       ///      readonly calls with identical callees and no intervening
63       ///      clobbers.  No validation is done that the operands to the calls
64       ///      are the same.
65       Def,
66       
67       /// NonLocal - This marker indicates that the query has no dependency in
68       /// the specified block.  To find out more, the client should query other
69       /// predecessor blocks.
70       NonLocal
71     };
72     typedef PointerIntPair<Instruction*, 2, DepType> PairTy;
73     PairTy Value;
74     explicit MemDepResult(PairTy V) : Value(V) {}
75   public:
76     MemDepResult() : Value(0, Invalid) {}
77     
78     /// get methods: These are static ctor methods for creating various
79     /// MemDepResult kinds.
80     static MemDepResult getDef(Instruction *Inst) {
81       return MemDepResult(PairTy(Inst, Def));
82     }
83     static MemDepResult getClobber(Instruction *Inst) {
84       return MemDepResult(PairTy(Inst, Clobber));
85     }
86     static MemDepResult getNonLocal() {
87       return MemDepResult(PairTy(0, NonLocal));
88     }
89
90     /// isClobber - Return true if this MemDepResult represents a query that is
91     /// a instruction clobber dependency.
92     bool isClobber() const { return Value.getInt() == Clobber; }
93
94     /// isDef - Return true if this MemDepResult represents a query that is
95     /// a instruction definition dependency.
96     bool isDef() const { return Value.getInt() == Def; }
97     
98     /// isNonLocal - Return true if this MemDepResult represents an query that
99     /// is transparent to the start of the block, but where a non-local hasn't
100     /// been done.
101     bool isNonLocal() const { return Value.getInt() == NonLocal; }
102     
103     /// getInst() - If this is a normal dependency, return the instruction that
104     /// is depended on.  Otherwise, return null.
105     Instruction *getInst() const { return Value.getPointer(); }
106     
107     bool operator==(const MemDepResult &M) const { return M.Value == Value; }
108     bool operator!=(const MemDepResult &M) const { return M.Value != Value; }
109     bool operator<(const MemDepResult &M) const { return M.Value < Value; }
110     bool operator>(const MemDepResult &M) const { return M.Value > Value; }
111   private:
112     friend class MemoryDependenceAnalysis;
113     /// Dirty - Entries with this marker occur in a LocalDeps map or
114     /// NonLocalDeps map when the instruction they previously referenced was
115     /// removed from MemDep.  In either case, the entry may include an
116     /// instruction pointer.  If so, the pointer is an instruction in the
117     /// block where scanning can start from, saving some work.
118     ///
119     /// In a default-constructed MemDepResult object, the type will be Dirty
120     /// and the instruction pointer will be null.
121     ///
122          
123     /// isDirty - Return true if this is a MemDepResult in its dirty/invalid.
124     /// state.
125     bool isDirty() const { return Value.getInt() == Invalid; }
126     
127     static MemDepResult getDirty(Instruction *Inst) {
128       return MemDepResult(PairTy(Inst, Invalid));
129     }
130   };
131
132   /// MemoryDependenceAnalysis - This is an analysis that determines, for a
133   /// given memory operation, what preceding memory operations it depends on.
134   /// It builds on alias analysis information, and tries to provide a lazy,
135   /// caching interface to a common kind of alias information query.
136   ///
137   /// The dependency information returned is somewhat unusual, but is pragmatic.
138   /// If queried about a store or call that might modify memory, the analysis
139   /// will return the instruction[s] that may either load from that memory or
140   /// store to it.  If queried with a load or call that can never modify memory,
141   /// the analysis will return calls and stores that might modify the pointer,
142   /// but generally does not return loads unless a) they are volatile, or
143   /// b) they load from *must-aliased* pointers.  Returning a dependence on
144   /// must-alias'd pointers instead of all pointers interacts well with the
145   /// internal caching mechanism.
146   ///
147   class MemoryDependenceAnalysis : public FunctionPass {
148     // A map from instructions to their dependency.
149     typedef DenseMap<Instruction*, MemDepResult> LocalDepMapType;
150     LocalDepMapType LocalDeps;
151
152   public:
153     typedef std::pair<BasicBlock*, MemDepResult> NonLocalDepEntry;
154     typedef std::vector<NonLocalDepEntry> NonLocalDepInfo;
155   private:
156     /// ValueIsLoadPair - This is a pair<Value*, bool> where the bool is true if
157     /// the dependence is a read only dependence, false if read/write.
158     typedef PointerIntPair<Value*, 1, bool> ValueIsLoadPair;
159     
160     /// CachedNonLocalPointerInfo - This map stores the cached results of doing
161     /// a pointer lookup at the bottom of a block.  The key of this map is the
162     /// pointer+isload bit, the value is a list of <bb->result> mappings.
163     typedef DenseMap<ValueIsLoadPair,
164       std::pair<BasicBlock*, NonLocalDepInfo> > CachedNonLocalPointerInfo;
165     CachedNonLocalPointerInfo NonLocalPointerDeps;
166
167     // A map from instructions to their non-local pointer dependencies.
168     // The elements of the SmallPtrSet are ValueIsLoadPair's.
169     typedef DenseMap<Instruction*, 
170                      SmallPtrSet<void*, 4> > ReverseNonLocalPtrDepTy;
171     ReverseNonLocalPtrDepTy ReverseNonLocalPtrDeps;
172
173     
174     /// PerInstNLInfo - This is the instruction we keep for each cached access
175     /// that we have for an instruction.  The pointer is an owning pointer and
176     /// the bool indicates whether we have any dirty bits in the set.
177     typedef std::pair<NonLocalDepInfo, bool> PerInstNLInfo;
178     
179     // A map from instructions to their non-local dependencies.
180     typedef DenseMap<Instruction*, PerInstNLInfo> NonLocalDepMapType;
181       
182     NonLocalDepMapType NonLocalDeps;
183     
184     // A reverse mapping from dependencies to the dependees.  This is
185     // used when removing instructions to keep the cache coherent.
186     typedef DenseMap<Instruction*,
187                      SmallPtrSet<Instruction*, 4> > ReverseDepMapType;
188     ReverseDepMapType ReverseLocalDeps;
189     
190     // A reverse mapping form dependencies to the non-local dependees.
191     ReverseDepMapType ReverseNonLocalDeps;
192     
193     /// Current AA implementation, just a cache.
194     AliasAnalysis *AA;
195     TargetData *TD;
196     OwningPtr<PredIteratorCache> PredCache;
197   public:
198     MemoryDependenceAnalysis();
199     ~MemoryDependenceAnalysis();
200     static char ID;
201
202     /// Pass Implementation stuff.  This doesn't do any analysis eagerly.
203     bool runOnFunction(Function &);
204     
205     /// Clean up memory in between runs
206     void releaseMemory();
207     
208     /// getAnalysisUsage - Does not modify anything.  It uses Value Numbering
209     /// and Alias Analysis.
210     ///
211     virtual void getAnalysisUsage(AnalysisUsage &AU) const;
212     
213     /// getDependency - Return the instruction on which a memory operation
214     /// depends.  See the class comment for more details.  It is illegal to call
215     /// this on non-memory instructions.
216     MemDepResult getDependency(Instruction *QueryInst);
217
218     /// getNonLocalCallDependency - Perform a full dependency query for the
219     /// specified call, returning the set of blocks that the value is
220     /// potentially live across.  The returned set of results will include a
221     /// "NonLocal" result for all blocks where the value is live across.
222     ///
223     /// This method assumes the instruction returns a "NonLocal" dependency
224     /// within its own block.
225     ///
226     /// This returns a reference to an internal data structure that may be
227     /// invalidated on the next non-local query or when an instruction is
228     /// removed.  Clients must copy this data if they want it around longer than
229     /// that.
230     const NonLocalDepInfo &getNonLocalCallDependency(CallSite QueryCS);
231     
232     
233     /// getNonLocalPointerDependency - Perform a full dependency query for an
234     /// access to the specified (non-volatile) memory location, returning the
235     /// set of instructions that either define or clobber the value.
236     ///
237     /// This method assumes the pointer has a "NonLocal" dependency within BB.
238     void getNonLocalPointerDependency(Value *Pointer, bool isLoad,
239                                       BasicBlock *BB,
240                                      SmallVectorImpl<NonLocalDepEntry> &Result);
241     
242     /// removeInstruction - Remove an instruction from the dependence analysis,
243     /// updating the dependence of instructions that previously depended on it.
244     void removeInstruction(Instruction *InstToRemove);
245     
246   private:
247     MemDepResult getPointerDependencyFrom(Value *Pointer, uint64_t MemSize,
248                                           bool isLoad, 
249                                           BasicBlock::iterator ScanIt,
250                                           BasicBlock *BB);
251     MemDepResult getCallSiteDependencyFrom(CallSite C, bool isReadOnlyCall,
252                                            BasicBlock::iterator ScanIt,
253                                            BasicBlock *BB);
254     void getNonLocalPointerDepFromBB(Value *Pointer, uint64_t Size,
255                                      bool isLoad, BasicBlock *BB,
256                                      SmallVectorImpl<NonLocalDepEntry> &Result,
257                                      SmallPtrSet<BasicBlock*, 64> &Visited);
258     MemDepResult GetNonLocalInfoForBlock(Value *Pointer, uint64_t PointeeSize,
259                                          bool isLoad, BasicBlock *BB,
260                                          NonLocalDepInfo *Cache,
261                                          unsigned NumSortedEntries);
262
263     
264     void RemoveCachedNonLocalPointerDependencies(ValueIsLoadPair P);
265     
266     /// verifyRemoved - Verify that the specified instruction does not occur
267     /// in our internal data structures.
268     void verifyRemoved(Instruction *Inst) const;
269     
270   };
271
272 } // End llvm namespace
273
274 #endif