1 //===- llvm/PassAnalysisSupport.h - Analysis Pass Support code --*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines stuff that is used to define and "use" Analysis Passes.
11 // This file is automatically #included by Pass.h, so:
13 // NO .CPP FILES SHOULD INCLUDE THIS FILE DIRECTLY
15 // Instead, #include Pass.h
17 //===----------------------------------------------------------------------===//
19 #ifndef LLVM_PASS_ANALYSIS_SUPPORT_H
20 #define LLVM_PASS_ANALYSIS_SUPPORT_H
23 #include "llvm/Pass.h"
24 #include "llvm/ADT/SmallVector.h"
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.
40 typedef SmallVector<AnalysisID, 32> VectorType;
43 // Sets of analyses required and preserved by a pass
44 VectorType Required, RequiredTransitive, Preserved;
48 AnalysisUsage() : PreservesAll(false) {}
50 // addRequired - Add the specified ID to the required set of the usage info
53 AnalysisUsage &addRequiredID(AnalysisID ID) {
54 assert(ID && "Pass class not registered!");
55 Required.push_back(ID);
58 template<class PassClass>
59 AnalysisUsage &addRequired() {
60 return addRequiredID(Pass::getClassPassInfo<PassClass>());
63 AnalysisUsage &addRequiredTransitiveID(AnalysisID ID) {
64 assert(ID && "Pass class not registered!");
65 Required.push_back(ID);
66 RequiredTransitive.push_back(ID);
69 template<class PassClass>
70 AnalysisUsage &addRequiredTransitive() {
71 AnalysisID ID = Pass::getClassPassInfo<PassClass>();
72 return addRequiredTransitiveID(ID);
75 // addPreserved - Add the specified ID to the set of analyses preserved by
78 AnalysisUsage &addPreservedID(AnalysisID ID) {
79 Preserved.push_back(ID);
83 // addPreserved - Add the specified Pass class to the set of analyses
84 // preserved by this pass.
86 template<class PassClass>
87 AnalysisUsage &addPreserved() {
88 assert(Pass::getClassPassInfo<PassClass>() && "Pass class not registered!");
89 Preserved.push_back(Pass::getClassPassInfo<PassClass>());
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!
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);
105 // setPreservesAll - Set by analyses that do not transform their input at all
106 void setPreservesAll() { PreservesAll = true; }
107 bool getPreservesAll() const { return PreservesAll; }
109 /// setPreservesCFG - This function should be called by the pass, iff they do
112 /// 1. Add or remove basic blocks from the function
113 /// 2. Modify terminator instructions in any way.
115 /// This function annotates the AnalysisUsage info object to say that analyses
116 /// that only depend on the CFG are preserved by this pass.
118 void setPreservesCFG();
120 const VectorType &getRequiredSet() const { return Required; }
121 const VectorType &getRequiredTransitiveSet() const {
122 return RequiredTransitive;
124 const VectorType &getPreservedSet() const { return Preserved; }
127 //===----------------------------------------------------------------------===//
128 // AnalysisResolver - Simple interface used by Pass objects to pull all
129 // analysis information out of pass manager that is responsible to manage
133 class AnalysisResolver {
135 AnalysisResolver(); // DO NOT IMPLEMENT
138 explicit AnalysisResolver(PMDataManager &P) : PM(P) { }
140 inline PMDataManager &getPMDataManager() { return PM; }
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;
154 // Find pass that is implementing PI. Initialize pass for Function F.
155 Pass *findImplPass(Pass *P, const PassInfo *PI, Function &F);
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);
162 /// clearAnalysisImpls - Clear cache that is used to connect a pass to the
163 /// the analysis (PassInfo).
164 void clearAnalysisImpls() {
165 AnalysisImpls.clear();
168 // getAnalysisIfAvailable - Return analysis result or null if it doesn't exist
169 Pass *getAnalysisIfAvailable(AnalysisID ID, bool Direction) const;
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;
176 // PassManager that is used to resolve analysis info
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.
188 template<typename AnalysisType>
189 AnalysisType *Pass::getAnalysisIfAvailable() const {
190 assert(Resolver && "Pass not resident in a PassManager object!");
192 const PassInfo *PI = getClassPassInfo<AnalysisType>();
193 if (PI == 0) return 0;
194 return dynamic_cast<AnalysisType*>
195 (Resolver->getAnalysisIfAvailable(PI, true));
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.
202 template<typename AnalysisType>
203 AnalysisType &Pass::getAnalysis() const {
204 assert(Resolver &&"Pass has not been inserted into a PassManager object!");
206 return getAnalysisID<AnalysisType>(getClassPassInfo<AnalysisType>());
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)
216 Pass *ResultPass = Resolver->findImplPass(PI);
217 assert (ResultPass &&
218 "getAnalysis*() called on an analysis that was not "
219 "'required' by pass!");
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).
226 AnalysisType *Result = dynamic_cast<AnalysisType*>(ResultPass);
227 assert(Result && "Pass does not implement interface required!");
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.
235 template<typename AnalysisType>
236 AnalysisType &Pass::getAnalysis(Function &F) {
237 assert(Resolver &&"Pass has not been inserted into a PassManager object!");
239 return getAnalysisID<AnalysisType>(getClassPassInfo<AnalysisType>(), F);
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)
249 Pass *ResultPass = Resolver->findImplPass(this, PI, F);
250 assert (ResultPass && "Unable to find requested analysis info");
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).
257 AnalysisType *Result = dynamic_cast<AnalysisType*>(ResultPass);
258 assert(Result && "Pass does not implement interface required!");
262 } // End llvm namespace