Add a const.
[oota-llvm.git] / include / llvm / PassAnalysisSupport.h
1 //===- llvm/PassAnalysisSupport.h - Analysis Pass Support code --*- 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 stuff that is used to define and "use" Analysis Passes.
11 // This file is automatically #included by Pass.h, so:
12 //
13 //           NO .CPP FILES SHOULD INCLUDE THIS FILE DIRECTLY
14 //
15 // Instead, #include Pass.h
16 //
17 //===----------------------------------------------------------------------===//
18
19 #ifndef LLVM_PASS_ANALYSIS_SUPPORT_H
20 #define LLVM_PASS_ANALYSIS_SUPPORT_H
21
22 #include "llvm/Pass.h"
23 #include "llvm/ADT/SmallVector.h"
24 #include "llvm/ADT/StringRef.h"
25 #include <vector>
26
27 namespace llvm {
28
29 //===----------------------------------------------------------------------===//
30 // AnalysisUsage - Represent the analysis usage information of a pass.  This
31 // tracks analyses that the pass REQUIRES (must be available when the pass
32 // runs), REQUIRES TRANSITIVE (must be available throughout the lifetime of the
33 // pass), and analyses that the pass PRESERVES (the pass does not invalidate the
34 // results of these analyses).  This information is provided by a pass to the
35 // Pass infrastructure through the getAnalysisUsage virtual function.
36 //
37 class AnalysisUsage {
38 public:
39   typedef SmallVector<AnalysisID, 32> VectorType;
40
41 private:
42   // Sets of analyses required and preserved by a pass
43   VectorType Required, RequiredTransitive, Preserved;
44   bool PreservesAll;
45
46 public:
47   AnalysisUsage() : PreservesAll(false) {}
48
49   // addRequired - Add the specified ID to the required set of the usage info
50   // for a pass.
51   //
52   AnalysisUsage &addRequiredID(AnalysisID ID) {
53     assert(ID && "Pass class not registered!");
54     Required.push_back(ID);
55     return *this;
56   }
57   template<class PassClass>
58   AnalysisUsage &addRequired() {
59     return addRequiredID(Pass::getClassPassInfo<PassClass>());
60   }
61
62   AnalysisUsage &addRequiredTransitiveID(AnalysisID ID) {
63     assert(ID && "Pass class not registered!");
64     Required.push_back(ID);
65     RequiredTransitive.push_back(ID);
66     return *this;
67   }
68   template<class PassClass>
69   AnalysisUsage &addRequiredTransitive() {
70     AnalysisID ID = Pass::getClassPassInfo<PassClass>();
71     return addRequiredTransitiveID(ID);
72   }
73
74   // addPreserved - Add the specified ID to the set of analyses preserved by
75   // this pass
76   //
77   AnalysisUsage &addPreservedID(AnalysisID ID) {
78     Preserved.push_back(ID);
79     return *this;
80   }
81
82   // addPreserved - Add the specified Pass class to the set of analyses
83   // preserved by this pass.
84   //
85   template<class PassClass>
86   AnalysisUsage &addPreserved() {
87     assert(Pass::getClassPassInfo<PassClass>() && "Pass class not registered!");
88     Preserved.push_back(Pass::getClassPassInfo<PassClass>());
89     return *this;
90   }
91
92   // addPreserved - Add the Pass with the specified argument string to the set
93   // of analyses preserved by this pass. If no such Pass exists, do nothing.
94   // This can be useful when a pass is trivially preserved, but may not be
95   // linked in. Be careful about spelling!
96   //
97   AnalysisUsage &addPreserved(StringRef Arg) {
98     const PassInfo *PI = Pass::lookupPassInfo(Arg);
99     // If the pass exists, preserve it. Otherwise silently do nothing.
100     if (PI) Preserved.push_back(PI);
101     return *this;
102   }
103
104   // setPreservesAll - Set by analyses that do not transform their input at all
105   void setPreservesAll() { PreservesAll = true; }
106   bool getPreservesAll() const { return PreservesAll; }
107
108   /// setPreservesCFG - This function should be called by the pass, iff they do
109   /// not:
110   ///
111   ///  1. Add or remove basic blocks from the function
112   ///  2. Modify terminator instructions in any way.
113   ///
114   /// This function annotates the AnalysisUsage info object to say that analyses
115   /// that only depend on the CFG are preserved by this pass.
116   ///
117   void setPreservesCFG();
118
119   const VectorType &getRequiredSet() const { return Required; }
120   const VectorType &getRequiredTransitiveSet() const {
121     return RequiredTransitive;
122   }
123   const VectorType &getPreservedSet() const { return Preserved; }
124 };
125
126 //===----------------------------------------------------------------------===//
127 // AnalysisResolver - Simple interface used by Pass objects to pull all
128 // analysis information out of pass manager that is responsible to manage
129 // the pass.
130 //
131 class PMDataManager;
132 class AnalysisResolver {
133 private:
134   AnalysisResolver();  // DO NOT IMPLEMENT
135
136 public:
137   explicit AnalysisResolver(PMDataManager &P) : PM(P) { }
138   
139   inline PMDataManager &getPMDataManager() { return PM; }
140
141   // Find pass that is implementing PI.
142   Pass *findImplPass(const PassInfo *PI) {
143     Pass *ResultPass = 0;
144     for (unsigned i = 0; i < AnalysisImpls.size() ; ++i) {
145       if (AnalysisImpls[i].first == PI) {
146         ResultPass = AnalysisImpls[i].second;
147         break;
148       }
149     }
150     return ResultPass;
151   }
152
153   // Find pass that is implementing PI. Initialize pass for Function F.
154   Pass *findImplPass(Pass *P, const PassInfo *PI, Function &F);
155
156   void addAnalysisImplsPair(const PassInfo *PI, Pass *P) {
157     std::pair<const PassInfo*, Pass*> pir = std::make_pair(PI,P);
158     AnalysisImpls.push_back(pir);
159   }
160
161   /// clearAnalysisImpls - Clear cache that is used to connect a pass to the
162   /// the analysis (PassInfo).
163   void clearAnalysisImpls() {
164     AnalysisImpls.clear();
165   }
166
167   // getAnalysisIfAvailable - Return analysis result or null if it doesn't exist
168   Pass *getAnalysisIfAvailable(AnalysisID ID, bool Direction) const;
169
170   // AnalysisImpls - This keeps track of which passes implements the interfaces
171   // that are required by the current pass (to implement getAnalysis()).
172   std::vector<std::pair<const PassInfo*, Pass*> > AnalysisImpls;
173
174 private:
175   // PassManager that is used to resolve analysis info
176   PMDataManager &PM;
177 };
178
179 /// getAnalysisIfAvailable<AnalysisType>() - Subclasses use this function to
180 /// get analysis information that might be around, for example to update it.
181 /// This is different than getAnalysis in that it can fail (if the analysis
182 /// results haven't been computed), so should only be used if you can handle
183 /// the case when the analysis is not available.  This method is often used by
184 /// transformation APIs to update analysis results for a pass automatically as
185 /// the transform is performed.
186 ///
187 template<typename AnalysisType>
188 AnalysisType *Pass::getAnalysisIfAvailable() const {
189   assert(Resolver && "Pass not resident in a PassManager object!");
190
191   const PassInfo *PI = getClassPassInfo<AnalysisType>();
192   if (PI == 0) return 0;
193
194   Pass *ResultPass = Resolver->getAnalysisIfAvailable(PI, true);
195   if (ResultPass == 0) return 0;
196
197   // Because the AnalysisType may not be a subclass of pass (for
198   // AnalysisGroups), we use getAdjustedAnalysisPointer here to potentially
199   // adjust the return pointer (because the class may multiply inherit, once
200   // from pass, once from AnalysisType).
201   return (AnalysisType*)ResultPass->getAdjustedAnalysisPointer(PI);
202 }
203
204 /// getAnalysis<AnalysisType>() - This function is used by subclasses to get
205 /// to the analysis information that they claim to use by overriding the
206 /// getAnalysisUsage function.
207 ///
208 template<typename AnalysisType>
209 AnalysisType &Pass::getAnalysis() const {
210   assert(Resolver && "Pass has not been inserted into a PassManager object!");
211   return getAnalysisID<AnalysisType>(getClassPassInfo<AnalysisType>());
212 }
213
214 template<typename AnalysisType>
215 AnalysisType &Pass::getAnalysisID(const PassInfo *PI) const {
216   assert(PI && "getAnalysis for unregistered pass!");
217   assert(Resolver&&"Pass has not been inserted into a PassManager object!");
218   // PI *must* appear in AnalysisImpls.  Because the number of passes used
219   // should be a small number, we just do a linear search over a (dense)
220   // vector.
221   Pass *ResultPass = Resolver->findImplPass(PI);
222   assert (ResultPass && 
223           "getAnalysis*() called on an analysis that was not "
224           "'required' by pass!");
225
226   // Because the AnalysisType may not be a subclass of pass (for
227   // AnalysisGroups), we use getAdjustedAnalysisPointer here to potentially
228   // adjust the return pointer (because the class may multiply inherit, once
229   // from pass, once from AnalysisType).
230   return *(AnalysisType*)ResultPass->getAdjustedAnalysisPointer(PI);
231 }
232
233 /// getAnalysis<AnalysisType>() - This function is used by subclasses to get
234 /// to the analysis information that they claim to use by overriding the
235 /// getAnalysisUsage function.
236 ///
237 template<typename AnalysisType>
238 AnalysisType &Pass::getAnalysis(Function &F) {
239   assert(Resolver &&"Pass has not been inserted into a PassManager object!");
240
241   return getAnalysisID<AnalysisType>(getClassPassInfo<AnalysisType>(), F);
242 }
243
244 template<typename AnalysisType>
245 AnalysisType &Pass::getAnalysisID(const PassInfo *PI, Function &F) {
246   assert(PI && "getAnalysis for unregistered pass!");
247   assert(Resolver && "Pass has not been inserted into a PassManager object!");
248   // PI *must* appear in AnalysisImpls.  Because the number of passes used
249   // should be a small number, we just do a linear search over a (dense)
250   // vector.
251   Pass *ResultPass = Resolver->findImplPass(this, PI, F);
252   assert(ResultPass && "Unable to find requested analysis info");
253   
254   // Because the AnalysisType may not be a subclass of pass (for
255   // AnalysisGroups), we use getAdjustedAnalysisPointer here to potentially
256   // adjust the return pointer (because the class may multiply inherit, once
257   // from pass, once from AnalysisType).
258   return *(AnalysisType*)ResultPass->getAdjustedAnalysisPointer(PI);
259 }
260
261 } // End llvm namespace
262
263 #endif