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"
25 #include "llvm/ADT/StringRef.h"
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.
41 typedef SmallVector<AnalysisID, 32> VectorType;
44 // Sets of analyses required and preserved by a pass
45 VectorType Required, RequiredTransitive, Preserved;
49 AnalysisUsage() : PreservesAll(false) {}
51 // addRequired - Add the specified ID to the required set of the usage info
54 AnalysisUsage &addRequiredID(AnalysisID ID) {
55 assert(ID && "Pass class not registered!");
56 Required.push_back(ID);
59 template<class PassClass>
60 AnalysisUsage &addRequired() {
61 return addRequiredID(Pass::getClassPassInfo<PassClass>());
64 AnalysisUsage &addRequiredTransitiveID(AnalysisID ID) {
65 assert(ID && "Pass class not registered!");
66 Required.push_back(ID);
67 RequiredTransitive.push_back(ID);
70 template<class PassClass>
71 AnalysisUsage &addRequiredTransitive() {
72 AnalysisID ID = Pass::getClassPassInfo<PassClass>();
73 return addRequiredTransitiveID(ID);
76 // addPreserved - Add the specified ID to the set of analyses preserved by
79 AnalysisUsage &addPreservedID(AnalysisID ID) {
80 Preserved.push_back(ID);
84 // addPreserved - Add the specified Pass class to the set of analyses
85 // preserved by this pass.
87 template<class PassClass>
88 AnalysisUsage &addPreserved() {
89 assert(Pass::getClassPassInfo<PassClass>() && "Pass class not registered!");
90 Preserved.push_back(Pass::getClassPassInfo<PassClass>());
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!
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);
106 // setPreservesAll - Set by analyses that do not transform their input at all
107 void setPreservesAll() { PreservesAll = true; }
108 bool getPreservesAll() const { return PreservesAll; }
110 /// setPreservesCFG - This function should be called by the pass, iff they do
113 /// 1. Add or remove basic blocks from the function
114 /// 2. Modify terminator instructions in any way.
116 /// This function annotates the AnalysisUsage info object to say that analyses
117 /// that only depend on the CFG are preserved by this pass.
119 void setPreservesCFG();
121 const VectorType &getRequiredSet() const { return Required; }
122 const VectorType &getRequiredTransitiveSet() const {
123 return RequiredTransitive;
125 const VectorType &getPreservedSet() const { return Preserved; }
128 //===----------------------------------------------------------------------===//
129 // AnalysisResolver - Simple interface used by Pass objects to pull all
130 // analysis information out of pass manager that is responsible to manage
134 class AnalysisResolver {
136 AnalysisResolver(); // DO NOT IMPLEMENT
139 explicit AnalysisResolver(PMDataManager &P) : PM(P) { }
141 inline PMDataManager &getPMDataManager() { return PM; }
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;
155 // Find pass that is implementing PI. Initialize pass for Function F.
156 Pass *findImplPass(Pass *P, const PassInfo *PI, Function &F);
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);
163 /// clearAnalysisImpls - Clear cache that is used to connect a pass to the
164 /// the analysis (PassInfo).
165 void clearAnalysisImpls() {
166 AnalysisImpls.clear();
169 // getAnalysisIfAvailable - Return analysis result or null if it doesn't exist
170 Pass *getAnalysisIfAvailable(AnalysisID ID, bool Direction) const;
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;
177 // PassManager that is used to resolve analysis info
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.
189 template<typename AnalysisType>
190 AnalysisType *Pass::getAnalysisIfAvailable() const {
191 assert(Resolver && "Pass not resident in a PassManager object!");
193 const PassInfo *PI = getClassPassInfo<AnalysisType>();
194 if (PI == 0) return 0;
195 return dynamic_cast<AnalysisType*>
196 (Resolver->getAnalysisIfAvailable(PI, true));
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.
203 template<typename AnalysisType>
204 AnalysisType &Pass::getAnalysis() const {
205 assert(Resolver &&"Pass has not been inserted into a PassManager object!");
207 return getAnalysisID<AnalysisType>(getClassPassInfo<AnalysisType>());
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)
217 Pass *ResultPass = Resolver->findImplPass(PI);
218 assert (ResultPass &&
219 "getAnalysis*() called on an analysis that was not "
220 "'required' by pass!");
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).
227 AnalysisType *Result = dynamic_cast<AnalysisType*>(ResultPass);
228 assert(Result && "Pass does not implement interface required!");
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.
236 template<typename AnalysisType>
237 AnalysisType &Pass::getAnalysis(Function &F) {
238 assert(Resolver &&"Pass has not been inserted into a PassManager object!");
240 return getAnalysisID<AnalysisType>(getClassPassInfo<AnalysisType>(), F);
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)
250 Pass *ResultPass = Resolver->findImplPass(this, PI, F);
251 assert (ResultPass && "Unable to find requested analysis info");
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).
258 AnalysisType *Result = dynamic_cast<AnalysisType*>(ResultPass);
259 assert(Result && "Pass does not implement interface required!");
263 } // End llvm namespace