1 //===- llvm/PassAnalysisSupport.h - Analysis Pass Support code --*- C++ -*-===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // This file defines stuff that is used to define and "use" Analysis Passes.
10 // This file is automatically #included by Pass.h, so:
12 // NO .CPP FILES SHOULD INCLUDE THIS FILE DIRECTLY
14 // Instead, #include Pass.h
16 //===----------------------------------------------------------------------===//
18 #ifndef LLVM_PASSANALYSISSUPPORT_H
19 #define LLVM_PASSANALYSISSUPPORT_H
22 #include "llvm/ADT/SmallVector.h"
23 #include "llvm/ADT/StringRef.h"
34 //===----------------------------------------------------------------------===//
35 /// Represent the analysis usage information of a pass. This tracks analyses
36 /// that the pass REQUIRES (must be available when the pass runs), REQUIRES
37 /// TRANSITIVE (must be available throughout the lifetime of the pass), and
38 /// analyses that the pass PRESERVES (the pass does not invalidate the results
39 /// of these analyses). This information is provided by a pass to the Pass
40 /// infrastructure through the getAnalysisUsage virtual function.
44 using VectorType
= SmallVectorImpl
<AnalysisID
>;
47 /// Sets of analyses required and preserved by a pass
48 // TODO: It's not clear that SmallVector is an appropriate data structure for
49 // this usecase. The sizes were picked to minimize wasted space, but are
50 // otherwise fairly meaningless.
51 SmallVector
<AnalysisID
, 8> Required
;
52 SmallVector
<AnalysisID
, 2> RequiredTransitive
;
53 SmallVector
<AnalysisID
, 2> Preserved
;
54 SmallVector
<AnalysisID
, 0> Used
;
55 bool PreservesAll
= false;
58 AnalysisUsage() = default;
61 /// Add the specified ID to the required set of the usage info for a pass.
62 AnalysisUsage
&addRequiredID(const void *ID
);
63 AnalysisUsage
&addRequiredID(char &ID
);
64 template<class PassClass
>
65 AnalysisUsage
&addRequired() {
66 return addRequiredID(PassClass::ID
);
69 AnalysisUsage
&addRequiredTransitiveID(char &ID
);
70 template<class PassClass
>
71 AnalysisUsage
&addRequiredTransitive() {
72 return addRequiredTransitiveID(PassClass::ID
);
77 /// Add the specified ID to the set of analyses preserved by this pass.
78 AnalysisUsage
&addPreservedID(const void *ID
) {
79 Preserved
.push_back(ID
);
82 AnalysisUsage
&addPreservedID(char &ID
) {
83 Preserved
.push_back(&ID
);
86 /// Add the specified Pass class to the set of analyses preserved by this pass.
87 template<class PassClass
>
88 AnalysisUsage
&addPreserved() {
89 Preserved
.push_back(&PassClass::ID
);
95 /// Add the specified ID to the set of analyses used by this pass if they are
97 AnalysisUsage
&addUsedIfAvailableID(const void *ID
) {
101 AnalysisUsage
&addUsedIfAvailableID(char &ID
) {
105 /// Add the specified Pass class to the set of analyses used by this pass.
106 template<class PassClass
>
107 AnalysisUsage
&addUsedIfAvailable() {
108 Used
.push_back(&PassClass::ID
);
113 /// Add the Pass with the specified argument string to the set of analyses
114 /// preserved by this pass. If no such Pass exists, do nothing. This can be
115 /// useful when a pass is trivially preserved, but may not be linked in. Be
116 /// careful about spelling!
117 AnalysisUsage
&addPreserved(StringRef Arg
);
119 /// Set by analyses that do not transform their input at all
120 void setPreservesAll() { PreservesAll
= true; }
122 /// Determine whether a pass said it does not transform its input at all
123 bool getPreservesAll() const { return PreservesAll
; }
125 /// This function should be called by the pass, iff they do not:
127 /// 1. Add or remove basic blocks from the function
128 /// 2. Modify terminator instructions in any way.
130 /// This function annotates the AnalysisUsage info object to say that analyses
131 /// that only depend on the CFG are preserved by this pass.
132 void setPreservesCFG();
134 const VectorType
&getRequiredSet() const { return Required
; }
135 const VectorType
&getRequiredTransitiveSet() const {
136 return RequiredTransitive
;
138 const VectorType
&getPreservedSet() const { return Preserved
; }
139 const VectorType
&getUsedSet() const { return Used
; }
142 //===----------------------------------------------------------------------===//
143 /// AnalysisResolver - Simple interface used by Pass objects to pull all
144 /// analysis information out of pass manager that is responsible to manage
147 class AnalysisResolver
{
149 AnalysisResolver() = delete;
150 explicit AnalysisResolver(PMDataManager
&P
) : PM(P
) {}
152 PMDataManager
&getPMDataManager() { return PM
; }
154 /// Find pass that is implementing PI.
155 Pass
*findImplPass(AnalysisID PI
) {
156 Pass
*ResultPass
= nullptr;
157 for (const auto &AnalysisImpl
: AnalysisImpls
) {
158 if (AnalysisImpl
.first
== PI
) {
159 ResultPass
= AnalysisImpl
.second
;
166 /// Find pass that is implementing PI. Initialize pass for Function F.
167 Pass
*findImplPass(Pass
*P
, AnalysisID PI
, Function
&F
);
169 void addAnalysisImplsPair(AnalysisID PI
, Pass
*P
) {
170 if (findImplPass(PI
) == P
)
172 std::pair
<AnalysisID
, Pass
*> pir
= std::make_pair(PI
,P
);
173 AnalysisImpls
.push_back(pir
);
176 /// Clear cache that is used to connect a pass to the analysis (PassInfo).
177 void clearAnalysisImpls() {
178 AnalysisImpls
.clear();
181 /// Return analysis result or null if it doesn't exist.
182 Pass
*getAnalysisIfAvailable(AnalysisID ID
, bool Direction
) const;
185 /// This keeps track of which passes implements the interfaces that are
186 /// required by the current pass (to implement getAnalysis()).
187 std::vector
<std::pair
<AnalysisID
, Pass
*>> AnalysisImpls
;
189 /// PassManager that is used to resolve analysis info
193 /// getAnalysisIfAvailable<AnalysisType>() - Subclasses use this function to
194 /// get analysis information that might be around, for example to update it.
195 /// This is different than getAnalysis in that it can fail (if the analysis
196 /// results haven't been computed), so should only be used if you can handle
197 /// the case when the analysis is not available. This method is often used by
198 /// transformation APIs to update analysis results for a pass automatically as
199 /// 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.
219 template<typename AnalysisType
>
220 AnalysisType
&Pass::getAnalysis() const {
221 assert(Resolver
&& "Pass has not been inserted into a PassManager object!");
222 return getAnalysisID
<AnalysisType
>(&AnalysisType::ID
);
225 template<typename AnalysisType
>
226 AnalysisType
&Pass::getAnalysisID(AnalysisID PI
) const {
227 assert(PI
&& "getAnalysis for unregistered pass!");
228 assert(Resolver
&&"Pass has not been inserted into a PassManager object!");
229 // PI *must* appear in AnalysisImpls. Because the number of passes used
230 // should be a small number, we just do a linear search over a (dense)
232 Pass
*ResultPass
= Resolver
->findImplPass(PI
);
234 "getAnalysis*() called on an analysis that was not "
235 "'required' by pass!");
237 // Because the AnalysisType may not be a subclass of pass (for
238 // AnalysisGroups), we use getAdjustedAnalysisPointer here to potentially
239 // adjust the return pointer (because the class may multiply inherit, once
240 // from pass, once from AnalysisType).
241 return *(AnalysisType
*)ResultPass
->getAdjustedAnalysisPointer(PI
);
244 /// getAnalysis<AnalysisType>() - This function is used by subclasses to get
245 /// to the analysis information that they claim to use by overriding the
246 /// getAnalysisUsage function.
247 template<typename AnalysisType
>
248 AnalysisType
&Pass::getAnalysis(Function
&F
) {
249 assert(Resolver
&&"Pass has not been inserted into a PassManager object!");
251 return getAnalysisID
<AnalysisType
>(&AnalysisType::ID
, F
);
254 template<typename AnalysisType
>
255 AnalysisType
&Pass::getAnalysisID(AnalysisID PI
, Function
&F
) {
256 assert(PI
&& "getAnalysis for unregistered pass!");
257 assert(Resolver
&& "Pass has not been inserted into a PassManager object!");
258 // PI *must* appear in AnalysisImpls. Because the number of passes used
259 // should be a small number, we just do a linear search over a (dense)
261 Pass
*ResultPass
= Resolver
->findImplPass(this, PI
, F
);
262 assert(ResultPass
&& "Unable to find requested analysis info");
264 // Because the AnalysisType may not be a subclass of pass (for
265 // AnalysisGroups), we use getAdjustedAnalysisPointer here to potentially
266 // adjust the return pointer (because the class may multiply inherit, once
267 // from pass, once from AnalysisType).
268 return *(AnalysisType
*)ResultPass
->getAdjustedAnalysisPointer(PI
);
271 } // end namespace llvm
273 #endif // LLVM_PASSANALYSISSUPPORT_H