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 /// Represent the analysis usage information of a pass. This tracks analyses
31 /// that the pass REQUIRES (must be available when the pass runs), REQUIRES
32 /// TRANSITIVE (must be available throughout the lifetime of the pass), and
33 /// analyses that the pass PRESERVES (the pass does not invalidate the results
34 /// of these analyses). This information is provided by a pass to the Pass
35 /// infrastructure through the getAnalysisUsage virtual function.
39 typedef SmallVectorImpl<AnalysisID> VectorType;
42 /// Sets of analyses required and preserved by a pass
43 // TODO: It's not clear that SmallVector is an appropriate data structure for
44 // this usecase. The sizes were picked to minimize wasted space, but are
45 // otherwise fairly meaningless.
46 SmallVector<AnalysisID, 8> Required;
47 SmallVector<AnalysisID, 2> RequiredTransitive;
48 SmallVector<AnalysisID, 2> Preserved;
49 SmallVector<AnalysisID, 0> Used;
53 AnalysisUsage() : PreservesAll(false) {}
56 /// Add the specified ID to the required set of the usage info for a pass.
57 AnalysisUsage &addRequiredID(const void *ID);
58 AnalysisUsage &addRequiredID(char &ID);
59 template<class PassClass>
60 AnalysisUsage &addRequired() {
61 return addRequiredID(PassClass::ID);
64 AnalysisUsage &addRequiredTransitiveID(char &ID);
65 template<class PassClass>
66 AnalysisUsage &addRequiredTransitive() {
67 return addRequiredTransitiveID(PassClass::ID);
72 /// Add the specified ID to the set of analyses preserved by this pass.
73 AnalysisUsage &addPreservedID(const void *ID) {
74 Preserved.push_back(ID);
77 AnalysisUsage &addPreservedID(char &ID) {
78 Preserved.push_back(&ID);
81 /// Add the specified Pass class to the set of analyses preserved by this pass.
82 template<class PassClass>
83 AnalysisUsage &addPreserved() {
84 Preserved.push_back(&PassClass::ID);
90 /// Add the specified ID to the set of analyses used by this pass if they are
92 AnalysisUsage &addUsedIfAvailableID(const void *ID) {
96 AnalysisUsage &addUsedIfAvailableID(char &ID) {
100 /// Add the specified Pass class to the set of analyses used by this pass.
101 template<class PassClass>
102 AnalysisUsage &addUsedIfAvailable() {
103 Used.push_back(&PassClass::ID);
108 /// Add the Pass with the specified argument string to the set of analyses
109 /// preserved by this pass. If no such Pass exists, do nothing. This can be
110 /// useful when a pass is trivially preserved, but may not be linked in. Be
111 /// careful about spelling!
112 AnalysisUsage &addPreserved(StringRef Arg);
114 /// Set by analyses that do not transform their input at all
115 void setPreservesAll() { PreservesAll = true; }
117 /// Determine whether a pass said it does not transform its input at all
118 bool getPreservesAll() const { return PreservesAll; }
120 /// This function should be called by the pass, iff they do not:
122 /// 1. Add or remove basic blocks from the function
123 /// 2. Modify terminator instructions in any way.
125 /// This function annotates the AnalysisUsage info object to say that analyses
126 /// that only depend on the CFG are preserved by this pass.
128 void setPreservesCFG();
130 const VectorType &getRequiredSet() const { return Required; }
131 const VectorType &getRequiredTransitiveSet() const {
132 return RequiredTransitive;
134 const VectorType &getPreservedSet() const { return Preserved; }
135 const VectorType &getUsedSet() const { return Used; }
138 //===----------------------------------------------------------------------===//
139 /// AnalysisResolver - Simple interface used by Pass objects to pull all
140 /// analysis information out of pass manager that is responsible to manage
144 class AnalysisResolver {
146 AnalysisResolver() = delete;
149 explicit AnalysisResolver(PMDataManager &P) : PM(P) { }
151 inline PMDataManager &getPMDataManager() { return PM; }
153 /// Find pass that is implementing PI.
154 Pass *findImplPass(AnalysisID PI) {
155 Pass *ResultPass = nullptr;
156 for (unsigned i = 0; i < AnalysisImpls.size() ; ++i) {
157 if (AnalysisImpls[i].first == PI) {
158 ResultPass = AnalysisImpls[i].second;
165 /// Find pass that is implementing PI. Initialize pass for Function F.
166 Pass *findImplPass(Pass *P, AnalysisID PI, Function &F);
168 void addAnalysisImplsPair(AnalysisID PI, Pass *P) {
169 if (findImplPass(PI) == P)
171 std::pair<AnalysisID, Pass*> pir = std::make_pair(PI,P);
172 AnalysisImpls.push_back(pir);
175 /// Clear cache that is used to connect a pass to the the analysis (PassInfo).
176 void clearAnalysisImpls() {
177 AnalysisImpls.clear();
180 /// Return analysis result or null if it doesn't exist.
181 Pass *getAnalysisIfAvailable(AnalysisID ID, bool Direction) const;
184 /// This keeps track of which passes implements the interfaces that are
185 /// required by the current pass (to implement getAnalysis()).
186 std::vector<std::pair<AnalysisID, Pass*> > AnalysisImpls;
188 /// PassManager that is used to resolve analysis info
192 /// getAnalysisIfAvailable<AnalysisType>() - Subclasses use this function to
193 /// get analysis information that might be around, for example to update it.
194 /// This is different than getAnalysis in that it can fail (if the analysis
195 /// results haven't been computed), so should only be used if you can handle
196 /// the case when the analysis is not available. This method is often used by
197 /// transformation APIs to update analysis results for a pass automatically as
198 /// the transform is performed.
200 template<typename AnalysisType>
201 AnalysisType *Pass::getAnalysisIfAvailable() const {
202 assert(Resolver && "Pass not resident in a PassManager object!");
204 const void *PI = &AnalysisType::ID;
206 Pass *ResultPass = Resolver->getAnalysisIfAvailable(PI, true);
207 if (!ResultPass) return nullptr;
209 // Because the AnalysisType may not be a subclass of pass (for
210 // AnalysisGroups), we use getAdjustedAnalysisPointer here to potentially
211 // adjust the return pointer (because the class may multiply inherit, once
212 // from pass, once from AnalysisType).
213 return (AnalysisType*)ResultPass->getAdjustedAnalysisPointer(PI);
216 /// getAnalysis<AnalysisType>() - This function is used by subclasses to get
217 /// to the analysis information that they claim to use by overriding the
218 /// getAnalysisUsage function.
220 template<typename AnalysisType>
221 AnalysisType &Pass::getAnalysis() const {
222 assert(Resolver && "Pass has not been inserted into a PassManager object!");
223 return getAnalysisID<AnalysisType>(&AnalysisType::ID);
226 template<typename AnalysisType>
227 AnalysisType &Pass::getAnalysisID(AnalysisID PI) const {
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(PI);
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 use getAdjustedAnalysisPointer here to potentially
240 // adjust the return pointer (because the class may multiply inherit, once
241 // from pass, once from AnalysisType).
242 return *(AnalysisType*)ResultPass->getAdjustedAnalysisPointer(PI);
245 /// getAnalysis<AnalysisType>() - This function is used by subclasses to get
246 /// to the analysis information that they claim to use by overriding the
247 /// getAnalysisUsage function.
249 template<typename AnalysisType>
250 AnalysisType &Pass::getAnalysis(Function &F) {
251 assert(Resolver &&"Pass has not been inserted into a PassManager object!");
253 return getAnalysisID<AnalysisType>(&AnalysisType::ID, F);
256 template<typename AnalysisType>
257 AnalysisType &Pass::getAnalysisID(AnalysisID PI, Function &F) {
258 assert(PI && "getAnalysis for unregistered pass!");
259 assert(Resolver && "Pass has not been inserted into a PassManager object!");
260 // PI *must* appear in AnalysisImpls. Because the number of passes used
261 // should be a small number, we just do a linear search over a (dense)
263 Pass *ResultPass = Resolver->findImplPass(this, PI, F);
264 assert(ResultPass && "Unable to find requested analysis info");
266 // Because the AnalysisType may not be a subclass of pass (for
267 // AnalysisGroups), we use getAdjustedAnalysisPointer here to potentially
268 // adjust the return pointer (because the class may multiply inherit, once
269 // from pass, once from AnalysisType).
270 return *(AnalysisType*)ResultPass->getAdjustedAnalysisPointer(PI);
273 } // End llvm namespace