Simplify assertion message to avoid confusion.
[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/ADT/SmallVector.h"
24
25 namespace llvm {
26
27 // No need to include Pass.h, we are being included by it!
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   template<class PassClass>
83   AnalysisUsage &addPreserved() {
84     assert(Pass::getClassPassInfo<PassClass>() && "Pass class not registered!");
85     Preserved.push_back(Pass::getClassPassInfo<PassClass>());
86     return *this;
87   }
88
89   // setPreservesAll - Set by analyses that do not transform their input at all
90   void setPreservesAll() { PreservesAll = true; }
91   bool getPreservesAll() const { return PreservesAll; }
92
93   /// setPreservesCFG - This function should be called by the pass, iff they do
94   /// not:
95   ///
96   ///  1. Add or remove basic blocks from the function
97   ///  2. Modify terminator instructions in any way.
98   ///
99   /// This function annotates the AnalysisUsage info object to say that analyses
100   /// that only depend on the CFG are preserved by this pass.
101   ///
102   void setPreservesCFG();
103
104   const VectorType &getRequiredSet() const { return Required; }
105   const VectorType &getRequiredTransitiveSet() const {
106     return RequiredTransitive;
107   }
108   const VectorType &getPreservedSet() const { return Preserved; }
109 };
110
111 //===----------------------------------------------------------------------===//
112 // AnalysisResolver - Simple interface used by Pass objects to pull all
113 // analysis information out of pass manager that is responsible to manage
114 // the pass.
115 //
116 class PMDataManager;
117 class AnalysisResolver {
118 private:
119   AnalysisResolver();  // DO NOT IMPLEMENT
120
121 public:
122   explicit AnalysisResolver(PMDataManager &P) : PM(P) { }
123   
124   inline PMDataManager &getPMDataManager() { return PM; }
125
126   // Find pass that is implementing PI.
127   Pass *findImplPass(const PassInfo *PI) {
128     Pass *ResultPass = 0;
129     for (unsigned i = 0; i < AnalysisImpls.size() ; ++i) {
130       if (AnalysisImpls[i].first == PI) {
131         ResultPass = AnalysisImpls[i].second;
132         break;
133       }
134     }
135     return ResultPass;
136   }
137
138   // Find pass that is implementing PI. Initialize pass for Function F.
139   Pass *findImplPass(Pass *P, const PassInfo *PI, Function &F);
140
141   void addAnalysisImplsPair(const PassInfo *PI, Pass *P) {
142     std::pair<const PassInfo*, Pass*> pir = std::make_pair(PI,P);
143     AnalysisImpls.push_back(pir);
144   }
145
146   /// clearAnalysisImpls - Clear cache that is used to connect a pass to the
147   /// the analysis (PassInfo).
148   void clearAnalysisImpls() {
149     AnalysisImpls.clear();
150   }
151
152   // getAnalysisIfAvailable - Return analysis result or null if it doesn't exist
153   Pass *getAnalysisIfAvailable(AnalysisID ID, bool Direction) const;
154
155   // AnalysisImpls - This keeps track of which passes implements the interfaces
156   // that are required by the current pass (to implement getAnalysis()).
157   std::vector<std::pair<const PassInfo*, Pass*> > AnalysisImpls;
158
159 private:
160   // PassManager that is used to resolve analysis info
161   PMDataManager &PM;
162 };
163
164 /// getAnalysisIfAvailable<AnalysisType>() - Subclasses use this function to
165 /// get analysis information that might be around, for example to update it.
166 /// This is different than getAnalysis in that it can fail (if the analysis
167 /// results haven't been computed), so should only be used if you can handle
168 /// the case when the analysis is not available.  This method is often used by
169 /// transformation APIs to update analysis results for a pass automatically as
170 /// the transform is performed.
171 ///
172 template<typename AnalysisType>
173 AnalysisType *Pass::getAnalysisIfAvailable() const {
174   assert(Resolver && "Pass not resident in a PassManager object!");
175
176   const PassInfo *PI = getClassPassInfo<AnalysisType>();
177   if (PI == 0) return 0;
178   return dynamic_cast<AnalysisType*>
179     (Resolver->getAnalysisIfAvailable(PI, true));
180 }
181
182 /// getAnalysis<AnalysisType>() - This function is used by subclasses to get
183 /// to the analysis information that they claim to use by overriding the
184 /// getAnalysisUsage function.
185 ///
186 template<typename AnalysisType>
187 AnalysisType &Pass::getAnalysis() const {
188   assert(Resolver &&"Pass has not been inserted into a PassManager object!");
189
190   return getAnalysisID<AnalysisType>(getClassPassInfo<AnalysisType>());
191 }
192
193 template<typename AnalysisType>
194 AnalysisType &Pass::getAnalysisID(const PassInfo *PI) const {
195   assert(PI && "getAnalysis for unregistered pass!");
196   assert(Resolver&&"Pass has not been inserted into a PassManager object!");
197   // PI *must* appear in AnalysisImpls.  Because the number of passes used
198   // should be a small number, we just do a linear search over a (dense)
199   // vector.
200   Pass *ResultPass = Resolver->findImplPass(PI);
201   assert (ResultPass && 
202           "getAnalysis*() called on an analysis that was not "
203           "'required' by pass!");
204
205   // Because the AnalysisType may not be a subclass of pass (for
206   // AnalysisGroups), we must use dynamic_cast here to potentially adjust the
207   // return pointer (because the class may multiply inherit, once from pass,
208   // once from AnalysisType).
209   //
210   AnalysisType *Result = dynamic_cast<AnalysisType*>(ResultPass);
211   assert(Result && "Pass does not implement interface required!");
212   return *Result;
213 }
214
215 /// getAnalysis<AnalysisType>() - This function is used by subclasses to get
216 /// to the analysis information that they claim to use by overriding the
217 /// getAnalysisUsage function.
218 ///
219 template<typename AnalysisType>
220 AnalysisType &Pass::getAnalysis(Function &F) {
221   assert(Resolver &&"Pass has not been inserted into a PassManager object!");
222
223   return getAnalysisID<AnalysisType>(getClassPassInfo<AnalysisType>(), F);
224 }
225
226 template<typename AnalysisType>
227 AnalysisType &Pass::getAnalysisID(const PassInfo *PI, Function &F) {
228   assert(PI && "getAnalysis for unregistered pass!");
229   assert(Resolver && "Pass has not been inserted into a PassManager object!");
230   // PI *must* appear in AnalysisImpls.  Because the number of passes used
231   // should be a small number, we just do a linear search over a (dense)
232   // vector.
233   Pass *ResultPass = Resolver->findImplPass(this, PI, F);
234   assert (ResultPass &&  "Unable to find requested analysis info");
235   
236   // Because the AnalysisType may not be a subclass of pass (for
237   // AnalysisGroups), we must use dynamic_cast here to potentially adjust the
238   // return pointer (because the class may multiply inherit, once from pass,
239   // once from AnalysisType).
240   //
241   AnalysisType *Result = dynamic_cast<AnalysisType*>(ResultPass);
242   assert(Result && "Pass does not implement interface required!");
243   return *Result;
244 }
245
246 } // End llvm namespace
247
248 #endif