Now that DepType is private, we can start cleaning up some of its uses:
[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/Pass.h"
18 #include "llvm/ADT/DenseMap.h"
19 #include "llvm/ADT/SmallPtrSet.h"
20 #include "llvm/ADT/PointerIntPair.h"
21
22 namespace llvm {
23   class Function;
24   class FunctionPass;
25   class Instruction;
26   class CallSite;
27   
28   /// MemDepResult - A memory dependence query can return one of three different
29   /// answers:
30   ///   Normal  : The query is dependent on a specific instruction.
31   ///   NonLocal: The query does not depend on anything inside this block, but
32   ///             we haven't scanned beyond the block to find out what.
33   ///   None    : The query does not depend on anything: we found the entry
34   ///             block or the allocation site of the memory.
35   class MemDepResult {
36     enum DepType {
37       Invalid = 0, Normal, NonLocal, None
38     };
39     typedef PointerIntPair<Instruction*, 2, DepType> PairTy;
40     PairTy Value;
41     explicit MemDepResult(PairTy V) : Value(V) {}
42   public:
43     MemDepResult() : Value(0, Invalid) {}
44     
45     /// get methods: These are static ctor methods for creating various
46     /// MemDepResult kinds.
47     static MemDepResult get(Instruction *Inst) {
48       return MemDepResult(PairTy(Inst, Normal));
49     }
50     static MemDepResult getNonLocal() {
51       return MemDepResult(PairTy(0, NonLocal));
52     }
53     static MemDepResult getNone() {
54       return MemDepResult(PairTy(0, None));
55     }
56
57     /// isNormal - Return true if this MemDepResult represents a query that is
58     /// a normal instruction dependency.
59     bool isNormal()          const { return Value.getInt() == Normal; }
60     
61     /// isNonLocal - Return true if this MemDepResult represents an query that
62     /// is transparent to the start of the block, but where a non-local hasn't
63     /// been done.
64     bool isNonLocal()      const { return Value.getInt() == NonLocal; }
65     
66     /// isNone - Return true if this MemDepResult represents a query that
67     /// doesn't depend on any instruction.
68     bool isNone()          const { return Value.getInt() == None; }
69
70     /// getInst() - If this is a normal dependency, return the instruction that
71     /// is depended on.  Otherwise, return null.
72     Instruction *getInst() const { return isNormal() ? Value.getPointer() : 0; }
73     
74     bool operator==(const MemDepResult &M) { return M.Value == Value; }
75     bool operator!=(const MemDepResult &M) { return M.Value != Value; }
76   };
77
78   /// MemoryDependenceAnalysis - This is an analysis that determines, for a
79   /// given memory operation, what preceding memory operations it depends on.
80   /// It builds on alias analysis information, and tries to provide a lazy,
81   /// caching interface to a common kind of alias information query.
82   class MemoryDependenceAnalysis : public FunctionPass {
83     /// DepType - This enum is used to indicate what flavor of dependence this
84     /// is.  If the type is Normal, there is an associated instruction pointer.
85     enum DepType {
86       /// Dirty - Entries with this marker may come in two forms, depending on
87       /// whether they are in a LocalDeps map or NonLocalDeps map.  In either
88       /// case, this marker indicates that the cached value has been invalidated
89       /// by a removeInstruction call.
90       ///
91       /// If in the LocalDeps map, the Instruction field will indicate the place
92       /// in the current block to start scanning.  If in the non-localdeps map,
93       /// the instruction will be null.
94       ///
95       /// In a default-constructed DepResultTy object, the type will be Dirty
96       /// and the instruction pointer will be null.
97       ///
98       /// FIXME: Why not add a scanning point for the non-local deps map???
99       Dirty = 0,
100       
101       /// Normal - This is a normal instruction dependence.  The pointer member
102       /// of the DepResultTy pair holds the instruction.
103       Normal,
104
105       /// None - This dependence type indicates that the query does not depend
106       /// on any instructions, either because it scanned to the start of the
107       /// function or it scanned to the definition of the memory
108       /// (alloca/malloc).
109       None,
110       
111       /// NonLocal - This marker indicates that the query has no dependency in
112       /// the specified block.  To find out more, the client should query other
113       /// predecessor blocks.
114       NonLocal
115     };
116     typedef PointerIntPair<Instruction*, 2, DepType> DepResultTy;
117
118     // A map from instructions to their dependency.
119     typedef DenseMap<Instruction*, DepResultTy> LocalDepMapType;
120     LocalDepMapType LocalDeps;
121
122     // A map from instructions to their non-local dependencies.
123     // FIXME: DENSEMAP of DENSEMAP not a great idea.
124     typedef DenseMap<Instruction*,
125                      DenseMap<BasicBlock*, DepResultTy> > nonLocalDepMapType;
126     nonLocalDepMapType depGraphNonLocal;
127     
128     // A reverse mapping from dependencies to the dependees.  This is
129     // used when removing instructions to keep the cache coherent.
130     typedef DenseMap<Instruction*,
131                      SmallPtrSet<Instruction*, 4> > reverseDepMapType;
132     reverseDepMapType reverseDep;
133     
134     // A reverse mapping form dependencies to the non-local dependees.
135     reverseDepMapType reverseDepNonLocal;
136     
137   public:
138     MemoryDependenceAnalysis() : FunctionPass(&ID) {}
139     static char ID;
140
141     /// Pass Implementation stuff.  This doesn't do any analysis.
142     ///
143     bool runOnFunction(Function &) {return false; }
144     
145     /// Clean up memory in between runs
146     void releaseMemory() {
147       LocalDeps.clear();
148       depGraphNonLocal.clear();
149       reverseDep.clear();
150       reverseDepNonLocal.clear();
151     }
152
153     /// getAnalysisUsage - Does not modify anything.  It uses Value Numbering
154     /// and Alias Analysis.
155     ///
156     virtual void getAnalysisUsage(AnalysisUsage &AU) const;
157     
158     /// getDependency - Return the instruction on which a memory operation
159     /// depends, starting with start.
160     MemDepResult getDependency(Instruction *query, Instruction *start = 0,
161                                BasicBlock *block = 0);
162     
163     /// getNonLocalDependency - Fills the passed-in map with the non-local 
164     /// dependencies of the queries.  The map will contain NonLocal for
165     /// blocks between the query and its dependencies.
166     void getNonLocalDependency(Instruction* query,
167                                DenseMap<BasicBlock*, MemDepResult> &resp);
168     
169     /// removeInstruction - Remove an instruction from the dependence analysis,
170     /// updating the dependence of instructions that previously depended on it.
171     void removeInstruction(Instruction *InstToRemove);
172     
173     /// dropInstruction - Remove an instruction from the analysis, making 
174     /// absolutely conservative assumptions when updating the cache.  This is
175     /// useful, for example when an instruction is changed rather than removed.
176     void dropInstruction(Instruction *InstToDrop);
177     
178   private:
179     DepResultTy ConvFromResult(MemDepResult R) {
180       if (Instruction *I = R.getInst())
181         return DepResultTy(I, Normal);
182       if (R.isNonLocal())
183         return DepResultTy(0, NonLocal);
184       assert(R.isNone() && "Unknown MemDepResult!");
185       return DepResultTy(0, None);
186     }
187     
188     MemDepResult ConvToResult(DepResultTy R) {
189       if (R.getInt() == Normal)
190         return MemDepResult::get(R.getPointer());
191       if (R.getInt() == NonLocal)
192         return MemDepResult::getNonLocal();
193       assert(R.getInt() == None && "Unknown MemDepResult!");
194       return MemDepResult::getNone();
195     }
196     
197     
198     /// verifyRemoved - Verify that the specified instruction does not occur
199     /// in our internal data structures.
200     void verifyRemoved(Instruction *Inst) const;
201     
202     MemDepResult getCallSiteDependency(CallSite C, Instruction* start,
203                                        BasicBlock* block);
204     void nonLocalHelper(Instruction* query, BasicBlock* block,
205                         DenseMap<BasicBlock*, DepResultTy> &resp);
206   };
207
208 } // End llvm namespace
209
210 #endif