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
22 #include "llvm/Pass.h"
23 #include "llvm/ADT/SmallVector.h"
24 #include "llvm/ADT/StringRef.h"
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(const void *ID);
53 AnalysisUsage &addRequiredID(char &ID);
54 template<class PassClass>
55 AnalysisUsage &addRequired() {
56 return addRequiredID(PassClass::ID);
59 AnalysisUsage &addRequiredTransitiveID(char &ID);
60 template<class PassClass>
61 AnalysisUsage &addRequiredTransitive() {
62 return addRequiredTransitiveID(PassClass::ID);
65 // addPreserved - Add the specified ID to the set of analyses preserved by
68 AnalysisUsage &addPreservedID(const void *ID) {
69 Preserved.push_back(ID);
72 AnalysisUsage &addPreservedID(char &ID) {
73 Preserved.push_back(&ID);
77 // addPreserved - Add the specified Pass class to the set of analyses
78 // preserved by this pass.
80 template<class PassClass>
81 AnalysisUsage &addPreserved() {
82 Preserved.push_back(&PassClass::ID);
86 // addPreserved - Add the Pass with the specified argument string to the set
87 // of analyses preserved by this pass. If no such Pass exists, do nothing.
88 // This can be useful when a pass is trivially preserved, but may not be
89 // linked in. Be careful about spelling!
91 AnalysisUsage &addPreserved(StringRef Arg);
93 // setPreservesAll - Set by analyses that do not transform their input at all
94 void setPreservesAll() { PreservesAll = true; }
95 bool getPreservesAll() const { return PreservesAll; }
97 /// setPreservesCFG - This function should be called by the pass, iff they do
100 /// 1. Add or remove basic blocks from the function
101 /// 2. Modify terminator instructions in any way.
103 /// This function annotates the AnalysisUsage info object to say that analyses
104 /// that only depend on the CFG are preserved by this pass.
106 void setPreservesCFG();
108 const VectorType &getRequiredSet() const { return Required; }
109 const VectorType &getRequiredTransitiveSet() const {
110 return RequiredTransitive;
112 const VectorType &getPreservedSet() const { return Preserved; }
115 //===----------------------------------------------------------------------===//
116 // AnalysisResolver - Simple interface used by Pass objects to pull all
117 // analysis information out of pass manager that is responsible to manage
121 class AnalysisResolver {
123 AnalysisResolver(); // DO NOT IMPLEMENT
126 explicit AnalysisResolver(PMDataManager &P) : PM(P) { }
128 inline PMDataManager &getPMDataManager() { return PM; }
130 // Find pass that is implementing PI.
131 Pass *findImplPass(AnalysisID PI) {
132 Pass *ResultPass = 0;
133 for (unsigned i = 0; i < AnalysisImpls.size() ; ++i) {
134 if (AnalysisImpls[i].first == PI) {
135 ResultPass = AnalysisImpls[i].second;
142 // Find pass that is implementing PI. Initialize pass for Function F.
143 Pass *findImplPass(Pass *P, AnalysisID PI, Function &F);
145 void addAnalysisImplsPair(AnalysisID PI, Pass *P) {
146 std::pair<AnalysisID, Pass*> pir = std::make_pair(PI,P);
147 AnalysisImpls.push_back(pir);
150 /// clearAnalysisImpls - Clear cache that is used to connect a pass to the
151 /// the analysis (PassInfo).
152 void clearAnalysisImpls() {
153 AnalysisImpls.clear();
156 // getAnalysisIfAvailable - Return analysis result or null if it doesn't exist
157 Pass *getAnalysisIfAvailable(AnalysisID ID, bool Direction) const;
159 // AnalysisImpls - This keeps track of which passes implements the interfaces
160 // that are required by the current pass (to implement getAnalysis()).
161 std::vector<std::pair<AnalysisID, Pass*> > AnalysisImpls;
164 // PassManager that is used to resolve analysis info
168 /// getAnalysisIfAvailable<AnalysisType>() - Subclasses use this function to
169 /// get analysis information that might be around, for example to update it.
170 /// This is different than getAnalysis in that it can fail (if the analysis
171 /// results haven't been computed), so should only be used if you can handle
172 /// the case when the analysis is not available. This method is often used by
173 /// transformation APIs to update analysis results for a pass automatically as
174 /// the transform is performed.
176 template<typename AnalysisType>
177 AnalysisType *Pass::getAnalysisIfAvailable() const {
178 assert(Resolver && "Pass not resident in a PassManager object!");
180 const void *PI = &AnalysisType::ID;
182 Pass *ResultPass = Resolver->getAnalysisIfAvailable(PI, true);
183 if (ResultPass == 0) return 0;
185 // Because the AnalysisType may not be a subclass of pass (for
186 // AnalysisGroups), we use getAdjustedAnalysisPointer here to potentially
187 // adjust the return pointer (because the class may multiply inherit, once
188 // from pass, once from AnalysisType).
189 return (AnalysisType*)ResultPass->getAdjustedAnalysisPointer(PI);
192 /// getAnalysis<AnalysisType>() - This function is used by subclasses to get
193 /// to the analysis information that they claim to use by overriding the
194 /// getAnalysisUsage function.
196 template<typename AnalysisType>
197 AnalysisType &Pass::getAnalysis() const {
198 assert(Resolver && "Pass has not been inserted into a PassManager object!");
199 return getAnalysisID<AnalysisType>(&AnalysisType::ID);
202 template<typename AnalysisType>
203 AnalysisType &Pass::getAnalysisID(AnalysisID PI) const {
204 assert(PI && "getAnalysis for unregistered pass!");
205 assert(Resolver&&"Pass has not been inserted into a PassManager object!");
206 // PI *must* appear in AnalysisImpls. Because the number of passes used
207 // should be a small number, we just do a linear search over a (dense)
209 Pass *ResultPass = Resolver->findImplPass(PI);
210 assert (ResultPass &&
211 "getAnalysis*() called on an analysis that was not "
212 "'required' by pass!");
214 // Because the AnalysisType may not be a subclass of pass (for
215 // AnalysisGroups), we use getAdjustedAnalysisPointer here to potentially
216 // adjust the return pointer (because the class may multiply inherit, once
217 // from pass, once from AnalysisType).
218 return *(AnalysisType*)ResultPass->getAdjustedAnalysisPointer(PI);
221 /// getAnalysis<AnalysisType>() - This function is used by subclasses to get
222 /// to the analysis information that they claim to use by overriding the
223 /// getAnalysisUsage function.
225 template<typename AnalysisType>
226 AnalysisType &Pass::getAnalysis(Function &F) {
227 assert(Resolver &&"Pass has not been inserted into a PassManager object!");
229 return getAnalysisID<AnalysisType>(&AnalysisType::ID, F);
232 template<typename AnalysisType>
233 AnalysisType &Pass::getAnalysisID(AnalysisID PI, Function &F) {
234 assert(PI && "getAnalysis for unregistered pass!");
235 assert(Resolver && "Pass has not been inserted into a PassManager object!");
236 // PI *must* appear in AnalysisImpls. Because the number of passes used
237 // should be a small number, we just do a linear search over a (dense)
239 Pass *ResultPass = Resolver->findImplPass(this, PI, F);
240 assert(ResultPass && "Unable to find requested analysis info");
242 // Because the AnalysisType may not be a subclass of pass (for
243 // AnalysisGroups), we use getAdjustedAnalysisPointer here to potentially
244 // adjust the return pointer (because the class may multiply inherit, once
245 // from pass, once from AnalysisType).
246 return *(AnalysisType*)ResultPass->getAdjustedAnalysisPointer(PI);
249 } // End llvm namespace