tidy up some variable names.
[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   class MemoryDependenceAnalysis : public FunctionPass {
84     /// DepType - This enum is used to indicate what flavor of dependence this
85     /// is.  If the type is Normal, there is an associated instruction pointer.
86     enum DepType {
87       /// Dirty - Entries with this marker may come in two forms, depending on
88       /// whether they are in a LocalDeps map or NonLocalDeps map.  In either
89       /// case, this marker indicates that the cached value has been invalidated
90       /// by a removeInstruction call.
91       ///
92       /// If in the LocalDeps map, the Instruction field will indicate the place
93       /// in the current block to start scanning.  If in the non-localdeps map,
94       /// the instruction will be null.
95       ///
96       /// In a default-constructed DepResultTy object, the type will be Dirty
97       /// and the instruction pointer will be null.
98       ///
99       /// FIXME: Why not add a scanning point for the non-local deps map???
100       Dirty = 0,
101       
102       /// Normal - This is a normal instruction dependence.  The pointer member
103       /// of the DepResultTy pair holds the instruction.
104       Normal,
105
106       /// None - This dependence type indicates that the query does not depend
107       /// on any instructions, either because it scanned to the start of the
108       /// function or it scanned to the definition of the memory
109       /// (alloca/malloc).
110       None,
111       
112       /// NonLocal - This marker indicates that the query has no dependency in
113       /// the specified block.  To find out more, the client should query other
114       /// predecessor blocks.
115       NonLocal
116     };
117     typedef PointerIntPair<Instruction*, 2, DepType> DepResultTy;
118
119     // A map from instructions to their dependency.
120     typedef DenseMap<Instruction*, DepResultTy> LocalDepMapType;
121     LocalDepMapType LocalDeps;
122
123     // A map from instructions to their non-local dependencies.
124     // FIXME: DENSEMAP of DENSEMAP not a great idea.
125     typedef DenseMap<Instruction*,
126                      DenseMap<BasicBlock*, DepResultTy> > NonLocalDepMapType;
127     NonLocalDepMapType NonLocalDeps;
128     
129     // A reverse mapping from dependencies to the dependees.  This is
130     // used when removing instructions to keep the cache coherent.
131     typedef DenseMap<Instruction*,
132                      SmallPtrSet<Instruction*, 4> > ReverseDepMapType;
133     ReverseDepMapType ReverseLocalDeps;
134     
135     // A reverse mapping form dependencies to the non-local dependees.
136     ReverseDepMapType ReverseNonLocalDeps;
137     
138   public:
139     MemoryDependenceAnalysis() : FunctionPass(&ID) {}
140     static char ID;
141
142     /// Pass Implementation stuff.  This doesn't do any analysis.
143     ///
144     bool runOnFunction(Function &) {return false; }
145     
146     /// Clean up memory in between runs
147     void releaseMemory() {
148       LocalDeps.clear();
149       NonLocalDeps.clear();
150       ReverseLocalDeps.clear();
151       ReverseNonLocalDeps.clear();
152     }
153
154     /// getAnalysisUsage - Does not modify anything.  It uses Value Numbering
155     /// and Alias Analysis.
156     ///
157     virtual void getAnalysisUsage(AnalysisUsage &AU) const;
158     
159     /// getDependency - Return the instruction on which a memory operation
160     /// depends.
161     MemDepResult getDependency(Instruction *QueryInst);
162
163     /// getDependencyFrom - Return the instruction on which the memory operation
164     /// 'QueryInst' depends.  This starts scanning from the instruction before
165     /// the position indicated by ScanIt.
166     MemDepResult getDependencyFrom(Instruction *QueryInst,
167                                    BasicBlock::iterator ScanIt, BasicBlock *BB);
168
169     
170     /// getNonLocalDependency - Fills the passed-in map with the non-local 
171     /// dependencies of the queries.  The map will contain NonLocal for
172     /// blocks between the query and its dependencies.
173     void getNonLocalDependency(Instruction *QueryInst,
174                                DenseMap<BasicBlock*, MemDepResult> &Result);
175     
176     /// removeInstruction - Remove an instruction from the dependence analysis,
177     /// updating the dependence of instructions that previously depended on it.
178     void removeInstruction(Instruction *InstToRemove);
179     
180     /// dropInstruction - Remove an instruction from the analysis, making 
181     /// absolutely conservative assumptions when updating the cache.  This is
182     /// useful, for example when an instruction is changed rather than removed.
183     void dropInstruction(Instruction *InstToDrop);
184     
185   private:
186     DepResultTy ConvFromResult(MemDepResult R) {
187       if (Instruction *I = R.getInst())
188         return DepResultTy(I, Normal);
189       if (R.isNonLocal())
190         return DepResultTy(0, NonLocal);
191       assert(R.isNone() && "Unknown MemDepResult!");
192       return DepResultTy(0, None);
193     }
194     
195     MemDepResult ConvToResult(DepResultTy R) {
196       if (R.getInt() == Normal)
197         return MemDepResult::get(R.getPointer());
198       if (R.getInt() == NonLocal)
199         return MemDepResult::getNonLocal();
200       assert(R.getInt() == None && "Unknown MemDepResult!");
201       return MemDepResult::getNone();
202     }
203     
204     /// verifyRemoved - Verify that the specified instruction does not occur
205     /// in our internal data structures.
206     void verifyRemoved(Instruction *Inst) const;
207     
208     MemDepResult getCallSiteDependency(CallSite C, BasicBlock::iterator ScanIt,
209                                        BasicBlock *BB);
210     void nonLocalHelper(Instruction *Query, BasicBlock *BB,
211                         DenseMap<BasicBlock*, DepResultTy> &Result);
212   };
213
214 } // End llvm namespace
215
216 #endif