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