Reimplement the non-local dependency data structure in terms of a sorted
[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       /// Normal - This is a normal instruction dependence.  The pointer member
39       /// of the MemDepResult pair holds the instruction.
40       Normal,
41
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.
45       NonLocal,
46
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)
50       /// being accessed.
51       None
52     };
53     typedef PointerIntPair<Instruction*, 2, DepType> PairTy;
54     PairTy Value;
55     explicit MemDepResult(PairTy V) : Value(V) {}
56   public:
57     MemDepResult() : Value(0, Invalid) {}
58     
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));
63     }
64     static MemDepResult getNonLocal() {
65       return MemDepResult(PairTy(0, NonLocal));
66     }
67     static MemDepResult getNone() {
68       return MemDepResult(PairTy(0, None));
69     }
70
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; }
74     
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
77     /// been done.
78     bool isNonLocal() const { return Value.getInt() == NonLocal; }
79     
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; }
83
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(); }
87     
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; }
92   private:
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.
99     ///
100     /// In a default-constructed MemDepResult object, the type will be Dirty
101     /// and the instruction pointer will be null.
102     ///
103          
104     /// isDirty - Return true if this is a MemDepResult in its dirty/invalid.
105     /// state.
106     bool isDirty() const { return Value.getInt() == Invalid; }
107     
108     static MemDepResult getDirty(Instruction *Inst) {
109       return MemDepResult(PairTy(Inst, Invalid));
110     }
111   };
112
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.
117   ///
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.
127   ///
128   class MemoryDependenceAnalysis : public FunctionPass {
129     // A map from instructions to their dependency.
130     typedef DenseMap<Instruction*, MemDepResult> LocalDepMapType;
131     LocalDepMapType LocalDeps;
132
133   public:
134     typedef std::pair<BasicBlock*, MemDepResult> NonLocalDepEntry;
135     typedef std::vector<NonLocalDepEntry> NonLocalDepInfo;
136   private:
137     
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;
142     
143     // A map from instructions to their non-local dependencies.
144     typedef DenseMap<Instruction*, PerInstNLInfo> NonLocalDepMapType;
145       
146     NonLocalDepMapType NonLocalDeps;
147     
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;
153     
154     // A reverse mapping form dependencies to the non-local dependees.
155     ReverseDepMapType ReverseNonLocalDeps;
156     
157     /// Current AA implementation, just a cache.
158     AliasAnalysis *AA;
159     TargetData *TD;
160   public:
161     MemoryDependenceAnalysis() : FunctionPass(&ID) {}
162     static char ID;
163
164     /// Pass Implementation stuff.  This doesn't do any analysis eagerly.
165     bool runOnFunction(Function &);
166     
167     /// Clean up memory in between runs
168     void releaseMemory() {
169       LocalDeps.clear();
170       NonLocalDeps.clear();
171       NonLocalDeps.clear();
172       ReverseLocalDeps.clear();
173       ReverseNonLocalDeps.clear();
174     }
175
176     /// getAnalysisUsage - Does not modify anything.  It uses Value Numbering
177     /// and Alias Analysis.
178     ///
179     virtual void getAnalysisUsage(AnalysisUsage &AU) const;
180     
181     /// getDependency - Return the instruction on which a memory operation
182     /// depends.  See the class comment for more details.
183     MemDepResult getDependency(Instruction *QueryInst);
184
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.
188     ///
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);
193
194     
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.
199     ///
200     /// This method assumes the instruction returns a "NonLocal" dependency
201     /// within its own block.
202     ///
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
206     /// that.
207     const NonLocalDepInfo &getNonLocalDependency(Instruction *QueryInst);
208     
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);
212     
213   private:
214     /// verifyRemoved - Verify that the specified instruction does not occur
215     /// in our internal data structures.
216     void verifyRemoved(Instruction *Inst) const;
217     
218     MemDepResult getCallSiteDependency(CallSite C, BasicBlock::iterator ScanIt,
219                                       BasicBlock *BB);
220   };
221
222 } // End llvm namespace
223
224 #endif