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_PASSANALYSISSUPPORT_H
20 #define LLVM_PASSANALYSISSUPPORT_H
22 #include "llvm/ADT/SmallVector.h"
23 #include "llvm/ADT/StringRef.h"
24 #include "llvm/Pass.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() = delete;
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 = nullptr;
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 if (findImplPass(PI) == P)
148 std::pair<AnalysisID, 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;
162 // AnalysisImpls - This keeps track of which passes implements the interfaces
163 // that are required by the current pass (to implement getAnalysis()).
164 std::vector<std::pair<AnalysisID, Pass*> > AnalysisImpls;
166 // PassManager that is used to resolve analysis info
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 void *PI = &AnalysisType::ID;
184 Pass *ResultPass = Resolver->getAnalysisIfAvailable(PI, true);
185 if (!ResultPass) return nullptr;
187 // Because the AnalysisType may not be a subclass of pass (for
188 // AnalysisGroups), we use getAdjustedAnalysisPointer here to potentially
189 // adjust the return pointer (because the class may multiply inherit, once
190 // from pass, once from AnalysisType).
191 return (AnalysisType*)ResultPass->getAdjustedAnalysisPointer(PI);
194 /// getAnalysis<AnalysisType>() - This function is used by subclasses to get
195 /// to the analysis information that they claim to use by overriding the
196 /// getAnalysisUsage function.
198 template<typename AnalysisType>
199 AnalysisType &Pass::getAnalysis() const {
200 assert(Resolver && "Pass has not been inserted into a PassManager object!");
201 return getAnalysisID<AnalysisType>(&AnalysisType::ID);
204 template<typename AnalysisType>
205 AnalysisType &Pass::getAnalysisID(AnalysisID PI) const {
206 assert(PI && "getAnalysis for unregistered pass!");
207 assert(Resolver&&"Pass has not been inserted into a PassManager object!");
208 // PI *must* appear in AnalysisImpls. Because the number of passes used
209 // should be a small number, we just do a linear search over a (dense)
211 Pass *ResultPass = Resolver->findImplPass(PI);
212 assert (ResultPass &&
213 "getAnalysis*() called on an analysis that was not "
214 "'required' by pass!");
216 // Because the AnalysisType may not be a subclass of pass (for
217 // AnalysisGroups), we use getAdjustedAnalysisPointer here to potentially
218 // adjust the return pointer (because the class may multiply inherit, once
219 // from pass, once from AnalysisType).
220 return *(AnalysisType*)ResultPass->getAdjustedAnalysisPointer(PI);
223 /// getAnalysis<AnalysisType>() - This function is used by subclasses to get
224 /// to the analysis information that they claim to use by overriding the
225 /// getAnalysisUsage function.
227 template<typename AnalysisType>
228 AnalysisType &Pass::getAnalysis(Function &F) {
229 assert(Resolver &&"Pass has not been inserted into a PassManager object!");
231 return getAnalysisID<AnalysisType>(&AnalysisType::ID, F);
234 template<typename AnalysisType>
235 AnalysisType &Pass::getAnalysisID(AnalysisID PI, Function &F) {
236 assert(PI && "getAnalysis for unregistered pass!");
237 assert(Resolver && "Pass has not been inserted into a PassManager object!");
238 // PI *must* appear in AnalysisImpls. Because the number of passes used
239 // should be a small number, we just do a linear search over a (dense)
241 Pass *ResultPass = Resolver->findImplPass(this, PI, F);
242 assert(ResultPass && "Unable to find requested analysis info");
244 // Because the AnalysisType may not be a subclass of pass (for
245 // AnalysisGroups), we use getAdjustedAnalysisPointer here to potentially
246 // adjust the return pointer (because the class may multiply inherit, once
247 // from pass, once from AnalysisType).
248 return *(AnalysisType*)ResultPass->getAdjustedAnalysisPointer(PI);
251 } // End llvm namespace