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/ADT/SmallVector.h"
27 // No need to include Pass.h, we are being included by it!
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.
39 typedef SmallVector<AnalysisID, 32> VectorType;
42 // Sets of analyses required and preserved by a pass
43 VectorType Required, RequiredTransitive, Preserved;
47 AnalysisUsage() : PreservesAll(false) {}
49 // addRequired - Add the specified ID to the required set of the usage info
52 AnalysisUsage &addRequiredID(AnalysisID ID) {
53 assert(ID && "Pass class not registered!");
54 Required.push_back(ID);
57 template<class PassClass>
58 AnalysisUsage &addRequired() {
59 return addRequiredID(Pass::getClassPassInfo<PassClass>());
62 AnalysisUsage &addRequiredTransitiveID(AnalysisID ID) {
63 assert(ID && "Pass class not registered!");
64 Required.push_back(ID);
65 RequiredTransitive.push_back(ID);
68 template<class PassClass>
69 AnalysisUsage &addRequiredTransitive() {
70 AnalysisID ID = Pass::getClassPassInfo<PassClass>();
71 return addRequiredTransitiveID(ID);
74 // addPreserved - Add the specified ID to the set of analyses preserved by
77 AnalysisUsage &addPreservedID(AnalysisID ID) {
78 Preserved.push_back(ID);
82 template<class PassClass>
83 AnalysisUsage &addPreserved() {
84 assert(Pass::getClassPassInfo<PassClass>() && "Pass class not registered!");
85 Preserved.push_back(Pass::getClassPassInfo<PassClass>());
89 // setPreservesAll - Set by analyses that do not transform their input at all
90 void setPreservesAll() { PreservesAll = true; }
91 bool getPreservesAll() const { return PreservesAll; }
93 /// setPreservesCFG - This function should be called by the pass, iff they do
96 /// 1. Add or remove basic blocks from the function
97 /// 2. Modify terminator instructions in any way.
99 /// This function annotates the AnalysisUsage info object to say that analyses
100 /// that only depend on the CFG are preserved by this pass.
102 void setPreservesCFG();
104 const VectorType &getRequiredSet() const { return Required; }
105 const VectorType &getRequiredTransitiveSet() const {
106 return RequiredTransitive;
108 const VectorType &getPreservedSet() const { return Preserved; }
111 //===----------------------------------------------------------------------===//
112 // AnalysisResolver - Simple interface used by Pass objects to pull all
113 // analysis information out of pass manager that is responsible to manage
117 class AnalysisResolver {
119 AnalysisResolver(); // DO NOT IMPLEMENT
122 explicit AnalysisResolver(PMDataManager &P) : PM(P) { }
124 inline PMDataManager &getPMDataManager() { return PM; }
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;
138 // Find pass that is implementing PI. Initialize pass for Function F.
139 Pass *findImplPass(Pass *P, const PassInfo *PI, Function &F);
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);
146 /// clearAnalysisImpls - Clear cache that is used to connect a pass to the
147 /// the analysis (PassInfo).
148 void clearAnalysisImpls() {
149 AnalysisImpls.clear();
152 // getAnalysisIfAvailable - Return analysis result or null if it doesn't exist
153 Pass *getAnalysisIfAvailable(AnalysisID ID, bool Direction) const;
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;
160 // PassManager that is used to resolve analysis info
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.
172 template<typename AnalysisType>
173 AnalysisType *Pass::getAnalysisIfAvailable() const {
174 assert(Resolver && "Pass not resident in a PassManager object!");
176 const PassInfo *PI = getClassPassInfo<AnalysisType>();
177 if (PI == 0) return 0;
178 return dynamic_cast<AnalysisType*>
179 (Resolver->getAnalysisIfAvailable(PI, true));
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.
186 template<typename AnalysisType>
187 AnalysisType &Pass::getAnalysis() const {
188 assert(Resolver &&"Pass has not been inserted into a PassManager object!");
190 return getAnalysisID<AnalysisType>(getClassPassInfo<AnalysisType>());
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)
200 Pass *ResultPass = Resolver->findImplPass(PI);
201 assert (ResultPass &&
202 "getAnalysis*() called on an analysis that was not "
203 "'required' by pass!");
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).
210 AnalysisType *Result = dynamic_cast<AnalysisType*>(ResultPass);
211 assert(Result && "Pass does not implement interface required!");
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.
219 template<typename AnalysisType>
220 AnalysisType &Pass::getAnalysis(Function &F) {
221 assert(Resolver &&"Pass has not been inserted into a PassManager object!");
223 return getAnalysisID<AnalysisType>(getClassPassInfo<AnalysisType>(), F);
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)
233 Pass *ResultPass = Resolver->findImplPass(this, PI, F);
234 assert (ResultPass &&
235 "getAnalysis*() called on an analysis that was not "
236 "'required' by pass!");
238 // Because the AnalysisType may not be a subclass of pass (for
239 // AnalysisGroups), we must use dynamic_cast here to potentially adjust the
240 // return pointer (because the class may multiply inherit, once from pass,
241 // once from AnalysisType).
243 AnalysisType *Result = dynamic_cast<AnalysisType*>(ResultPass);
244 assert(Result && "Pass does not implement interface required!");
248 } // End llvm namespace