Make it illegal to call getDependency* on non-memory instructions
[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       /// Normal - This is a normal instruction dependence.  The pointer member
40       /// of the MemDepResult pair holds the instruction.
41       Normal,
42
43       /// NonLocal - This marker indicates that the query has no dependency in
44       /// the specified block.  To find out more, the client should query other
45       /// predecessor blocks.
46       NonLocal,
47
48       /// None - This dependence type indicates that the query does not depend
49       /// on any instructions, either because it is not a memory instruction or
50       /// because it scanned to the definition of the memory (alloca/malloc)
51       /// being accessed.
52       None
53     };
54     typedef PointerIntPair<Instruction*, 2, DepType> PairTy;
55     PairTy Value;
56     explicit MemDepResult(PairTy V) : Value(V) {}
57   public:
58     MemDepResult() : Value(0, Invalid) {}
59     
60     /// get methods: These are static ctor methods for creating various
61     /// MemDepResult kinds.
62     static MemDepResult get(Instruction *Inst) {
63       return MemDepResult(PairTy(Inst, Normal));
64     }
65     static MemDepResult getNonLocal() {
66       return MemDepResult(PairTy(0, NonLocal));
67     }
68     static MemDepResult getNone() {
69       return MemDepResult(PairTy(0, None));
70     }
71
72     /// isNormal - Return true if this MemDepResult represents a query that is
73     /// a normal instruction dependency.
74     bool isNormal() const { return Value.getInt() == Normal; }
75     
76     /// isNonLocal - Return true if this MemDepResult represents an query that
77     /// is transparent to the start of the block, but where a non-local hasn't
78     /// been done.
79     bool isNonLocal() const { return Value.getInt() == NonLocal; }
80     
81     /// isNone - Return true if this MemDepResult represents a query that
82     /// doesn't depend on any instruction.
83     bool isNone() const { return Value.getInt() == None; }
84
85     /// getInst() - If this is a normal dependency, return the instruction that
86     /// is depended on.  Otherwise, return null.
87     Instruction *getInst() const { return Value.getPointer(); }
88     
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; }
92     bool operator>(const MemDepResult &M) const { return M.Value > Value; }
93   private:
94     friend class MemoryDependenceAnalysis;
95     /// Dirty - Entries with this marker occur in a LocalDeps map or
96     /// NonLocalDeps map when the instruction they previously referenced was
97     /// removed from MemDep.  In either case, the entry may include an
98     /// instruction pointer.  If so, the pointer is an instruction in the
99     /// block where scanning can start from, saving some work.
100     ///
101     /// In a default-constructed MemDepResult object, the type will be Dirty
102     /// and the instruction pointer will be null.
103     ///
104          
105     /// isDirty - Return true if this is a MemDepResult in its dirty/invalid.
106     /// state.
107     bool isDirty() const { return Value.getInt() == Invalid; }
108     
109     static MemDepResult getDirty(Instruction *Inst) {
110       return MemDepResult(PairTy(Inst, Invalid));
111     }
112   };
113
114   /// MemoryDependenceAnalysis - This is an analysis that determines, for a
115   /// given memory operation, what preceding memory operations it depends on.
116   /// It builds on alias analysis information, and tries to provide a lazy,
117   /// caching interface to a common kind of alias information query.
118   ///
119   /// The dependency information returned is somewhat unusual, but is pragmatic.
120   /// If queried about a store or call that might modify memory, the analysis
121   /// will return the instruction[s] that may either load from that memory or
122   /// store to it.  If queried with a load or call that can never modify memory,
123   /// the analysis will return calls and stores that might modify the pointer,
124   /// but generally does not return loads unless a) they are volatile, or
125   /// b) they load from *must-aliased* pointers.  Returning a dependence on
126   /// must-alias'd pointers instead of all pointers interacts well with the
127   /// internal caching mechanism.
128   ///
129   class MemoryDependenceAnalysis : public FunctionPass {
130     // A map from instructions to their dependency.
131     typedef DenseMap<Instruction*, MemDepResult> LocalDepMapType;
132     LocalDepMapType LocalDeps;
133
134   public:
135     typedef std::pair<BasicBlock*, MemDepResult> NonLocalDepEntry;
136     typedef std::vector<NonLocalDepEntry> NonLocalDepInfo;
137   private:
138     
139     /// PerInstNLInfo - This is the instruction we keep for each cached access
140     /// that we have for an instruction.  The pointer is an owning pointer and
141     /// the bool indicates whether we have any dirty bits in the set.
142     typedef std::pair<NonLocalDepInfo, bool> PerInstNLInfo;
143     
144     // A map from instructions to their non-local dependencies.
145     typedef DenseMap<Instruction*, PerInstNLInfo> NonLocalDepMapType;
146       
147     NonLocalDepMapType NonLocalDeps;
148     
149     // A reverse mapping from dependencies to the dependees.  This is
150     // used when removing instructions to keep the cache coherent.
151     typedef DenseMap<Instruction*,
152                      SmallPtrSet<Instruction*, 4> > ReverseDepMapType;
153     ReverseDepMapType ReverseLocalDeps;
154     
155     // A reverse mapping form dependencies to the non-local dependees.
156     ReverseDepMapType ReverseNonLocalDeps;
157     
158     /// Current AA implementation, just a cache.
159     AliasAnalysis *AA;
160     TargetData *TD;
161   public:
162     MemoryDependenceAnalysis() : FunctionPass(&ID) {}
163     static char ID;
164
165     /// Pass Implementation stuff.  This doesn't do any analysis eagerly.
166     bool runOnFunction(Function &);
167     
168     /// Clean up memory in between runs
169     void releaseMemory() {
170       LocalDeps.clear();
171       NonLocalDeps.clear();
172       NonLocalDeps.clear();
173       ReverseLocalDeps.clear();
174       ReverseNonLocalDeps.clear();
175     }
176
177     /// getAnalysisUsage - Does not modify anything.  It uses Value Numbering
178     /// and Alias Analysis.
179     ///
180     virtual void getAnalysisUsage(AnalysisUsage &AU) const;
181     
182     /// getDependency - Return the instruction on which a memory operation
183     /// depends.  See the class comment for more details.  It is illegal to call
184     /// this on non-memory instructions.
185     MemDepResult getDependency(Instruction *QueryInst);
186
187     /// getDependencyFrom - Return the instruction on which the memory operation
188     /// 'QueryInst' depends.  This starts scanning from the instruction before
189     /// the position indicated by ScanIt.
190     ///
191     /// Note that this method does no caching at all.  You should use
192     /// getDependency where possible.
193     MemDepResult getDependencyFrom(Instruction *QueryInst,
194                                    BasicBlock::iterator ScanIt, BasicBlock *BB);
195
196     
197     /// getNonLocalDependency - Perform a full dependency query for the
198     /// specified instruction, returning the set of blocks that the value is
199     /// potentially live across.  The returned set of results will include a
200     /// "NonLocal" result for all blocks where the value is live across.
201     ///
202     /// This method assumes the instruction returns a "NonLocal" dependency
203     /// within its own block.
204     ///
205     /// This returns a reference to an internal data structure that may be
206     /// invalidated on the next non-local query or when an instruction is
207     /// removed.  Clients must copy this data if they want it around longer than
208     /// that.
209     const NonLocalDepInfo &getNonLocalDependency(Instruction *QueryInst);
210     
211     /// removeInstruction - Remove an instruction from the dependence analysis,
212     /// updating the dependence of instructions that previously depended on it.
213     void removeInstruction(Instruction *InstToRemove);
214     
215   private:
216     /// verifyRemoved - Verify that the specified instruction does not occur
217     /// in our internal data structures.
218     void verifyRemoved(Instruction *Inst) const;
219     
220     MemDepResult getCallSiteDependency(CallSite C, BasicBlock::iterator ScanIt,
221                                        BasicBlock *BB);
222   };
223
224 } // End llvm namespace
225
226 #endif