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