453740517bd2b112a1a60e69e4c6208c98b58775
[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 DepResultTy 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) { return M.Value == Value; }
89     bool operator!=(const MemDepResult &M) { return M.Value != Value; }
90   private:
91     friend class MemoryDependenceAnalysis;
92     /// Dirty - Entries with this marker occur in a LocalDeps map or
93     /// NonLocalDeps map when the instruction they previously referenced was
94     /// removed from MemDep.  In either case, the entry may include an
95     /// instruction pointer.  If so, the pointer is an instruction in the
96     /// block where scanning can start from, saving some work.
97     ///
98     /// In a default-constructed DepResultTy object, the type will be Dirty
99     /// and the instruction pointer will be null.
100     ///
101     
102     /// isDirty - Return true if this is a MemDepResult in its dirty/invalid.
103     /// state.
104     bool isDirty() const { return Value.getInt() == Invalid; }
105
106     static MemDepResult getDirty(Instruction *Inst) {
107       return MemDepResult(PairTy(Inst, Invalid));
108     }
109   };
110
111   /// MemoryDependenceAnalysis - This is an analysis that determines, for a
112   /// given memory operation, what preceding memory operations it depends on.
113   /// It builds on alias analysis information, and tries to provide a lazy,
114   /// caching interface to a common kind of alias information query.
115   ///
116   /// The dependency information returned is somewhat unusual, but is pragmatic.
117   /// If queried about a store or call that might modify memory, the analysis
118   /// will return the instruction[s] that may either load from that memory or
119   /// store to it.  If queried with a load or call that can never modify memory,
120   /// the analysis will return calls and stores that might modify the pointer,
121   /// but generally does not return loads unless a) they are volatile, or
122   /// b) they load from *must-aliased* pointers.  Returning a dependence on
123   /// must-alias'd pointers instead of all pointers interacts well with the
124   /// internal caching mechanism.
125   ///
126   class MemoryDependenceAnalysis : public FunctionPass {
127     // A map from instructions to their dependency.
128     typedef DenseMap<Instruction*, MemDepResult> LocalDepMapType;
129     LocalDepMapType LocalDeps;
130
131     typedef DenseMap<BasicBlock*, MemDepResult> NonLocalDepInfo;
132     
133     /// PerInstNLInfo - This is the instruction we keep for each cached access
134     /// that we have for an instruction.  The pointer is an owning pointer and
135     /// the bool indicates whether we have any dirty bits in the set.
136     typedef PointerIntPair<NonLocalDepInfo*, 1, bool> PerInstNLInfo;
137     
138     // A map from instructions to their non-local dependencies.
139     typedef DenseMap<Instruction*, PerInstNLInfo> NonLocalDepMapType;
140       
141     NonLocalDepMapType NonLocalDeps;
142     
143     // A reverse mapping from dependencies to the dependees.  This is
144     // used when removing instructions to keep the cache coherent.
145     typedef DenseMap<Instruction*,
146                      SmallPtrSet<Instruction*, 4> > ReverseDepMapType;
147     ReverseDepMapType ReverseLocalDeps;
148     
149     // A reverse mapping form dependencies to the non-local dependees.
150     ReverseDepMapType ReverseNonLocalDeps;
151     
152     /// Current AA implementation, just a cache.
153     AliasAnalysis *AA;
154     TargetData *TD;
155   public:
156     MemoryDependenceAnalysis() : FunctionPass(&ID) {}
157     static char ID;
158
159     /// Pass Implementation stuff.  This doesn't do any analysis eagerly.
160     bool runOnFunction(Function &);
161     
162     /// Clean up memory in between runs
163     void releaseMemory() {
164       LocalDeps.clear();
165       for (NonLocalDepMapType::iterator I = NonLocalDeps.begin(),
166            E = NonLocalDeps.end(); I != E; ++I)
167         delete I->second.getPointer();
168       NonLocalDeps.clear();
169       ReverseLocalDeps.clear();
170       ReverseNonLocalDeps.clear();
171     }
172
173     /// getAnalysisUsage - Does not modify anything.  It uses Value Numbering
174     /// and Alias Analysis.
175     ///
176     virtual void getAnalysisUsage(AnalysisUsage &AU) const;
177     
178     /// getDependency - Return the instruction on which a memory operation
179     /// depends.  See the class comment for more details.
180     MemDepResult getDependency(Instruction *QueryInst);
181
182     /// getDependencyFrom - Return the instruction on which the memory operation
183     /// 'QueryInst' depends.  This starts scanning from the instruction before
184     /// the position indicated by ScanIt.
185     ///
186     /// Note that this method does no caching at all.  You should use
187     /// getDependency where possible.
188     MemDepResult getDependencyFrom(Instruction *QueryInst,
189                                    BasicBlock::iterator ScanIt, BasicBlock *BB);
190
191     
192     /// getNonLocalDependency - Perform a full dependency query for the
193     /// specified instruction, returning the set of blocks that the value is
194     /// potentially live across.  The returned set of results will include a
195     /// "NonLocal" result for all blocks where the value is live across.
196     ///
197     /// This method assumes the instruction returns a "nonlocal" dependency
198     /// within its own block.
199     void getNonLocalDependency(Instruction *QueryInst,
200                                SmallVectorImpl<std::pair<BasicBlock*, 
201                                                        MemDepResult> > &Result);
202     
203     /// removeInstruction - Remove an instruction from the dependence analysis,
204     /// updating the dependence of instructions that previously depended on it.
205     void removeInstruction(Instruction *InstToRemove);
206     
207   private:
208     /// verifyRemoved - Verify that the specified instruction does not occur
209     /// in our internal data structures.
210     void verifyRemoved(Instruction *Inst) const;
211     
212     MemDepResult getCallSiteDependency(CallSite C, BasicBlock::iterator ScanIt,
213                                       BasicBlock *BB);
214   };
215
216 } // End llvm namespace
217
218 #endif