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