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