Fix uninitialized TBAAFlag field values.
[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/Support/ValueHandle.h"
20 #include "llvm/Analysis/AliasAnalysis.h"
21 #include "llvm/ADT/DenseMap.h"
22 #include "llvm/ADT/SmallPtrSet.h"
23 #include "llvm/ADT/OwningPtr.h"
24 #include "llvm/ADT/PointerIntPair.h"
25
26 namespace llvm {
27   class Function;
28   class FunctionPass;
29   class Instruction;
30   class CallSite;
31   class AliasAnalysis;
32   class TargetData;
33   class MemoryDependenceAnalysis;
34   class PredIteratorCache;
35   class DominatorTree;
36   class PHITransAddr;
37   
38   /// MemDepResult - A memory dependence query can return one of three different
39   /// answers, described below.
40   class MemDepResult {
41     enum DepType {
42       /// Invalid - Clients of MemDep never see this.
43       Invalid = 0,
44       
45       /// Clobber - This is a dependence on the specified instruction which
46       /// clobbers the desired value.  The pointer member of the MemDepResult
47       /// pair holds the instruction that clobbers the memory.  For example,
48       /// this occurs when we see a may-aliased store to the memory location we
49       /// care about.
50       Clobber,
51
52       /// Def - This is a dependence on the specified instruction which
53       /// defines/produces the desired memory location.  The pointer member of
54       /// the MemDepResult pair holds the instruction that defines the memory.
55       /// Cases of interest:
56       ///   1. This could be a load or store for dependence queries on
57       ///      load/store.  The value loaded or stored is the produced value.
58       ///      Note that the pointer operand may be different than that of the
59       ///      queried pointer due to must aliases and phi translation.  Note
60       ///      that the def may not be the same type as the query, the pointers
61       ///      may just be must aliases.
62       ///   2. For loads and stores, this could be an allocation instruction. In
63       ///      this case, the load is loading an undef value or a store is the
64       ///      first store to (that part of) the allocation.
65       ///   3. Dependence queries on calls return Def only when they are
66       ///      readonly calls or memory use intrinsics with identical callees
67       ///      and no intervening clobbers.  No validation is done that the
68       ///      operands to the calls are the same.
69       Def,
70       
71       /// NonLocal - This marker indicates that the query has no dependency in
72       /// the specified block.  To find out more, the client should query other
73       /// predecessor blocks.
74       NonLocal
75     };
76     typedef PointerIntPair<Instruction*, 2, DepType> PairTy;
77     PairTy Value;
78     explicit MemDepResult(PairTy V) : Value(V) {}
79   public:
80     MemDepResult() : Value(0, Invalid) {}
81     
82     /// get methods: These are static ctor methods for creating various
83     /// MemDepResult kinds.
84     static MemDepResult getDef(Instruction *Inst) {
85       return MemDepResult(PairTy(Inst, Def));
86     }
87     static MemDepResult getClobber(Instruction *Inst) {
88       return MemDepResult(PairTy(Inst, Clobber));
89     }
90     static MemDepResult getNonLocal() {
91       return MemDepResult(PairTy(0, NonLocal));
92     }
93
94     /// isClobber - Return true if this MemDepResult represents a query that is
95     /// a instruction clobber dependency.
96     bool isClobber() const { return Value.getInt() == Clobber; }
97
98     /// isDef - Return true if this MemDepResult represents a query that is
99     /// a instruction definition dependency.
100     bool isDef() const { return Value.getInt() == Def; }
101     
102     /// isNonLocal - Return true if this MemDepResult represents a query that
103     /// is transparent to the start of the block, but where a non-local hasn't
104     /// been done.
105     bool isNonLocal() const { return Value.getInt() == NonLocal; }
106     
107     /// getInst() - If this is a normal dependency, return the instruction that
108     /// is depended on.  Otherwise, return null.
109     Instruction *getInst() const { return Value.getPointer(); }
110     
111     bool operator==(const MemDepResult &M) const { return Value == M.Value; }
112     bool operator!=(const MemDepResult &M) const { return Value != M.Value; }
113     bool operator<(const MemDepResult &M) const { return Value < M.Value; }
114     bool operator>(const MemDepResult &M) const { return Value > M.Value; }
115   private:
116     friend class MemoryDependenceAnalysis;
117     /// Dirty - Entries with this marker occur in a LocalDeps map or
118     /// NonLocalDeps map when the instruction they previously referenced was
119     /// removed from MemDep.  In either case, the entry may include an
120     /// instruction pointer.  If so, the pointer is an instruction in the
121     /// block where scanning can start from, saving some work.
122     ///
123     /// In a default-constructed MemDepResult object, the type will be Dirty
124     /// and the instruction pointer will be null.
125     ///
126          
127     /// isDirty - Return true if this is a MemDepResult in its dirty/invalid.
128     /// state.
129     bool isDirty() const { return Value.getInt() == Invalid; }
130     
131     static MemDepResult getDirty(Instruction *Inst) {
132       return MemDepResult(PairTy(Inst, Invalid));
133     }
134   };
135
136   /// NonLocalDepResult - This is a result from a NonLocal dependence query.
137   /// For each BasicBlock (the BB entry) it keeps a MemDepResult and the
138   /// (potentially phi translated) address that was live in the block.
139   class NonLocalDepResult {
140     BasicBlock *BB;
141     MemDepResult Result;
142     Value *Address;
143   public:
144     NonLocalDepResult(BasicBlock *bb, MemDepResult result, Value *address)
145       : BB(bb), Result(result), Address(address) {}
146     
147     // BB is the sort key, it can't be changed.
148     BasicBlock *getBB() const { return BB; }
149     
150     void setResult(const MemDepResult &R, Value *Addr) {
151       Result = R;
152       Address = Addr;
153     }
154     
155     const MemDepResult &getResult() const { return Result; }
156     
157     /// getAddress - Return the address of this pointer in this block.  This can
158     /// be different than the address queried for the non-local result because
159     /// of phi translation.  This returns null if the address was not available
160     /// in a block (i.e. because phi translation failed) or if this is a cached
161     /// result and that address was deleted.
162     ///
163     /// The address is always null for a non-local 'call' dependence.
164     Value *getAddress() const { return Address; }
165   };
166   
167   /// NonLocalDepEntry - This is an entry in the NonLocalDepInfo cache.  For
168   /// each BasicBlock (the BB entry) it keeps a MemDepResult.
169   class NonLocalDepEntry {
170     BasicBlock *BB;
171     MemDepResult Result;
172   public:
173     NonLocalDepEntry(BasicBlock *bb, MemDepResult result)
174       : BB(bb), Result(result) {}
175
176     // This is used for searches.
177     NonLocalDepEntry(BasicBlock *bb) : BB(bb) {}
178
179     // BB is the sort key, it can't be changed.
180     BasicBlock *getBB() const { return BB; }
181     
182     void setResult(const MemDepResult &R) { Result = R; }
183
184     const MemDepResult &getResult() const { return Result; }
185     
186     bool operator<(const NonLocalDepEntry &RHS) const {
187       return BB < RHS.BB;
188     }
189   };
190   
191   /// MemoryDependenceAnalysis - This is an analysis that determines, for a
192   /// given memory operation, what preceding memory operations it depends on.
193   /// It builds on alias analysis information, and tries to provide a lazy,
194   /// caching interface to a common kind of alias information query.
195   ///
196   /// The dependency information returned is somewhat unusual, but is pragmatic.
197   /// If queried about a store or call that might modify memory, the analysis
198   /// will return the instruction[s] that may either load from that memory or
199   /// store to it.  If queried with a load or call that can never modify memory,
200   /// the analysis will return calls and stores that might modify the pointer,
201   /// but generally does not return loads unless a) they are volatile, or
202   /// b) they load from *must-aliased* pointers.  Returning a dependence on
203   /// must-alias'd pointers instead of all pointers interacts well with the
204   /// internal caching mechanism.
205   ///
206   class MemoryDependenceAnalysis : public FunctionPass {
207     // A map from instructions to their dependency.
208     typedef DenseMap<Instruction*, MemDepResult> LocalDepMapType;
209     LocalDepMapType LocalDeps;
210
211   public:
212     typedef std::vector<NonLocalDepEntry> NonLocalDepInfo;
213   private:
214     /// ValueIsLoadPair - This is a pair<Value*, bool> where the bool is true if
215     /// the dependence is a read only dependence, false if read/write.
216     typedef PointerIntPair<const Value*, 1, bool> ValueIsLoadPair;
217
218     /// BBSkipFirstBlockPair - This pair is used when caching information for a
219     /// block.  If the pointer is null, the cache value is not a full query that
220     /// starts at the specified block.  If non-null, the bool indicates whether
221     /// or not the contents of the block was skipped.
222     typedef PointerIntPair<BasicBlock*, 1, bool> BBSkipFirstBlockPair;
223
224     /// NonLocalPointerInfo - This record is the information kept for each
225     /// (value, is load) pair.
226     struct NonLocalPointerInfo {
227       /// Pair - The pair of the block and the skip-first-block flag.
228       BBSkipFirstBlockPair Pair;
229       /// NonLocalDeps - The results of the query for each relevant block.
230       NonLocalDepInfo NonLocalDeps;
231       /// TBAATag - The TBAA tag associated with dereferences of the
232       /// pointer. May be null if there are no tags or conflicting tags.
233       MDNode *TBAATag;
234
235       NonLocalPointerInfo() : TBAATag(0) {}
236     };
237
238     /// CachedNonLocalPointerInfo - This map stores the cached results of doing
239     /// a pointer lookup at the bottom of a block.  The key of this map is the
240     /// pointer+isload bit, the value is a list of <bb->result> mappings.
241     typedef DenseMap<ValueIsLoadPair,
242                      NonLocalPointerInfo> CachedNonLocalPointerInfo;
243     CachedNonLocalPointerInfo NonLocalPointerDeps;
244
245     // A map from instructions to their non-local pointer dependencies.
246     typedef DenseMap<Instruction*, 
247                      SmallPtrSet<ValueIsLoadPair, 4> > ReverseNonLocalPtrDepTy;
248     ReverseNonLocalPtrDepTy ReverseNonLocalPtrDeps;
249
250     
251     /// PerInstNLInfo - This is the instruction we keep for each cached access
252     /// that we have for an instruction.  The pointer is an owning pointer and
253     /// the bool indicates whether we have any dirty bits in the set.
254     typedef std::pair<NonLocalDepInfo, bool> PerInstNLInfo;
255     
256     // A map from instructions to their non-local dependencies.
257     typedef DenseMap<Instruction*, PerInstNLInfo> NonLocalDepMapType;
258       
259     NonLocalDepMapType NonLocalDeps;
260     
261     // A reverse mapping from dependencies to the dependees.  This is
262     // used when removing instructions to keep the cache coherent.
263     typedef DenseMap<Instruction*,
264                      SmallPtrSet<Instruction*, 4> > ReverseDepMapType;
265     ReverseDepMapType ReverseLocalDeps;
266     
267     // A reverse mapping from dependencies to the non-local dependees.
268     ReverseDepMapType ReverseNonLocalDeps;
269     
270     /// Current AA implementation, just a cache.
271     AliasAnalysis *AA;
272     TargetData *TD;
273     OwningPtr<PredIteratorCache> PredCache;
274   public:
275     MemoryDependenceAnalysis();
276     ~MemoryDependenceAnalysis();
277     static char ID;
278
279     /// Pass Implementation stuff.  This doesn't do any analysis eagerly.
280     bool runOnFunction(Function &);
281     
282     /// Clean up memory in between runs
283     void releaseMemory();
284     
285     /// getAnalysisUsage - Does not modify anything.  It uses Value Numbering
286     /// and Alias Analysis.
287     ///
288     virtual void getAnalysisUsage(AnalysisUsage &AU) const;
289     
290     /// getDependency - Return the instruction on which a memory operation
291     /// depends.  See the class comment for more details.  It is illegal to call
292     /// this on non-memory instructions.
293     MemDepResult getDependency(Instruction *QueryInst);
294
295     /// getNonLocalCallDependency - Perform a full dependency query for the
296     /// specified call, returning the set of blocks that the value is
297     /// potentially live across.  The returned set of results will include a
298     /// "NonLocal" result for all blocks where the value is live across.
299     ///
300     /// This method assumes the instruction returns a "NonLocal" dependency
301     /// within its own block.
302     ///
303     /// This returns a reference to an internal data structure that may be
304     /// invalidated on the next non-local query or when an instruction is
305     /// removed.  Clients must copy this data if they want it around longer than
306     /// that.
307     const NonLocalDepInfo &getNonLocalCallDependency(CallSite QueryCS);
308     
309     
310     /// getNonLocalPointerDependency - Perform a full dependency query for an
311     /// access to the specified (non-volatile) memory location, returning the
312     /// set of instructions that either define or clobber the value.
313     ///
314     /// This method assumes the pointer has a "NonLocal" dependency within BB.
315     void getNonLocalPointerDependency(const AliasAnalysis::Location &Loc,
316                                       bool isLoad, BasicBlock *BB,
317                                     SmallVectorImpl<NonLocalDepResult> &Result);
318
319     /// getNonLocalPointerDependence - A convenience wrapper.
320     void getNonLocalPointerDependency(Value *Pointer, bool isLoad,
321                                       BasicBlock *BB,
322                                     SmallVectorImpl<NonLocalDepResult> &Result){
323       return getNonLocalPointerDependency(AliasAnalysis::Location(Pointer),
324                                           isLoad, BB, Result);
325     }
326     
327     /// removeInstruction - Remove an instruction from the dependence analysis,
328     /// updating the dependence of instructions that previously depended on it.
329     void removeInstruction(Instruction *InstToRemove);
330     
331     /// invalidateCachedPointerInfo - This method is used to invalidate cached
332     /// information about the specified pointer, because it may be too
333     /// conservative in memdep.  This is an optional call that can be used when
334     /// the client detects an equivalence between the pointer and some other
335     /// value and replaces the other value with ptr. This can make Ptr available
336     /// in more places that cached info does not necessarily keep.
337     void invalidateCachedPointerInfo(Value *Ptr);
338
339     /// invalidateCachedPredecessors - Clear the PredIteratorCache info.
340     /// This needs to be done when the CFG changes, e.g., due to splitting
341     /// critical edges.
342     void invalidateCachedPredecessors();
343     
344   private:
345     MemDepResult getPointerDependencyFrom(const AliasAnalysis::Location &Loc,
346                                           bool isLoad, 
347                                           BasicBlock::iterator ScanIt,
348                                           BasicBlock *BB);
349     MemDepResult getCallSiteDependencyFrom(CallSite C, bool isReadOnlyCall,
350                                            BasicBlock::iterator ScanIt,
351                                            BasicBlock *BB);
352     bool getNonLocalPointerDepFromBB(const PHITransAddr &Pointer,
353                                      const AliasAnalysis::Location &Loc,
354                                      bool isLoad, BasicBlock *BB,
355                                      SmallVectorImpl<NonLocalDepResult> &Result,
356                                      DenseMap<BasicBlock*, Value*> &Visited,
357                                      bool SkipFirstBlock = false);
358     MemDepResult GetNonLocalInfoForBlock(const AliasAnalysis::Location &Loc,
359                                          bool isLoad, BasicBlock *BB,
360                                          NonLocalDepInfo *Cache,
361                                          unsigned NumSortedEntries);
362
363     void RemoveCachedNonLocalPointerDependencies(ValueIsLoadPair P);
364     
365     /// verifyRemoved - Verify that the specified instruction does not occur
366     /// in our internal data structures.
367     void verifyRemoved(Instruction *Inst) const;
368     
369   };
370
371 } // End llvm namespace
372
373 #endif