LLVM 20.0.0git
PassAnalysisSupport.h
Go to the documentation of this file.
1//===- llvm/PassAnalysisSupport.h - Analysis Pass Support code --*- C++ -*-===//
2//
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
6//
7//===----------------------------------------------------------------------===//
8//
9// This file defines stuff that is used to define and "use" Analysis Passes.
10// This file is automatically #included by Pass.h, so:
11//
12// NO .CPP FILES SHOULD INCLUDE THIS FILE DIRECTLY
13//
14// Instead, #include Pass.h
15//
16//===----------------------------------------------------------------------===//
17
18#if !defined(LLVM_PASS_H) || defined(LLVM_PASSANALYSISSUPPORT_H)
19#error "Do not include <PassAnalysisSupport.h>; include <Pass.h> instead"
20#endif
21
22#ifndef LLVM_PASSANALYSISSUPPORT_H
23#define LLVM_PASSANALYSISSUPPORT_H
24
25#include "llvm/ADT/STLExtras.h"
27#include <cassert>
28#include <tuple>
29#include <utility>
30#include <vector>
31
32namespace llvm {
33
34class Function;
35class Pass;
36class PMDataManager;
37class StringRef;
38
39//===----------------------------------------------------------------------===//
40/// Represent the analysis usage information of a pass. This tracks analyses
41/// that the pass REQUIRES (must be available when the pass runs), REQUIRES
42/// TRANSITIVE (must be available throughout the lifetime of the pass), and
43/// analyses that the pass PRESERVES (the pass does not invalidate the results
44/// of these analyses). This information is provided by a pass to the Pass
45/// infrastructure through the getAnalysisUsage virtual function.
46///
48public:
50
51private:
52 /// Sets of analyses required and preserved by a pass
53 // TODO: It's not clear that SmallVector is an appropriate data structure for
54 // this usecase. The sizes were picked to minimize wasted space, but are
55 // otherwise fairly meaningless.
57 SmallVector<AnalysisID, 2> RequiredTransitive;
60 bool PreservesAll = false;
61
62 void pushUnique(VectorType &Set, AnalysisID ID) {
63 if (!llvm::is_contained(Set, ID))
64 Set.push_back(ID);
65 }
66
67public:
68 AnalysisUsage() = default;
69
70 ///@{
71 /// Add the specified ID to the required set of the usage info for a pass.
72 AnalysisUsage &addRequiredID(const void *ID);
74 template<class PassClass>
76 return addRequiredID(PassClass::ID);
77 }
78
80 template<class PassClass>
82 return addRequiredTransitiveID(PassClass::ID);
83 }
84 ///@}
85
86 ///@{
87 /// Add the specified ID to the set of analyses preserved by this pass.
89 pushUnique(Preserved, ID);
90 return *this;
91 }
93 pushUnique(Preserved, &ID);
94 return *this;
95 }
96 /// Add the specified Pass class to the set of analyses preserved by this pass.
97 template<class PassClass>
99 pushUnique(Preserved, &PassClass::ID);
100 return *this;
101 }
102 ///@}
103
104 ///@{
105 /// Add the specified ID to the set of analyses used by this pass if they are
106 /// available..
108 pushUnique(Used, ID);
109 return *this;
110 }
112 pushUnique(Used, &ID);
113 return *this;
114 }
115 /// Add the specified Pass class to the set of analyses used by this pass.
116 template<class PassClass>
118 pushUnique(Used, &PassClass::ID);
119 return *this;
120 }
121 ///@}
122
123 /// Add the Pass with the specified argument string to the set of analyses
124 /// preserved by this pass. If no such Pass exists, do nothing. This can be
125 /// useful when a pass is trivially preserved, but may not be linked in. Be
126 /// careful about spelling!
128
129 /// Set by analyses that do not transform their input at all
130 void setPreservesAll() { PreservesAll = true; }
131
132 /// Determine whether a pass said it does not transform its input at all
133 bool getPreservesAll() const { return PreservesAll; }
134
135 /// This function should be called by the pass, iff they do not:
136 ///
137 /// 1. Add or remove basic blocks from the function
138 /// 2. Modify terminator instructions in any way.
139 ///
140 /// This function annotates the AnalysisUsage info object to say that analyses
141 /// that only depend on the CFG are preserved by this pass.
142 void setPreservesCFG();
143
144 const VectorType &getRequiredSet() const { return Required; }
146 return RequiredTransitive;
147 }
148 const VectorType &getPreservedSet() const { return Preserved; }
149 const VectorType &getUsedSet() const { return Used; }
150};
151
152//===----------------------------------------------------------------------===//
153/// AnalysisResolver - Simple interface used by Pass objects to pull all
154/// analysis information out of pass manager that is responsible to manage
155/// the pass.
156///
158public:
160 explicit AnalysisResolver(PMDataManager &P) : PM(P) {}
161
163
164 /// Find pass that is implementing PI.
166 Pass *ResultPass = nullptr;
167 for (const auto &AnalysisImpl : AnalysisImpls) {
168 if (AnalysisImpl.first == PI) {
169 ResultPass = AnalysisImpl.second;
170 break;
171 }
172 }
173 return ResultPass;
174 }
175
176 /// Find pass that is implementing PI. Initialize pass for Function F.
177 std::tuple<Pass *, bool> findImplPass(Pass *P, AnalysisID PI, Function &F);
178
180 if (findImplPass(PI) == P)
181 return;
182 std::pair<AnalysisID, Pass*> pir = std::make_pair(PI,P);
183 AnalysisImpls.push_back(pir);
184 }
185
186 /// Clear cache that is used to connect a pass to the analysis (PassInfo).
188 AnalysisImpls.clear();
189 }
190
191 /// Return analysis result or null if it doesn't exist.
193
194private:
195 /// This keeps track of which passes implements the interfaces that are
196 /// required by the current pass (to implement getAnalysis()).
197 std::vector<std::pair<AnalysisID, Pass *>> AnalysisImpls;
198
199 /// PassManager that is used to resolve analysis info
200 PMDataManager &PM;
201};
202
203/// getAnalysisIfAvailable<AnalysisType>() - Subclasses use this function to
204/// get analysis information that might be around, for example to update it.
205/// This is different than getAnalysis in that it can fail (if the analysis
206/// results haven't been computed), so should only be used if you can handle
207/// the case when the analysis is not available. This method is often used by
208/// transformation APIs to update analysis results for a pass automatically as
209/// the transform is performed.
210template<typename AnalysisType>
211AnalysisType *Pass::getAnalysisIfAvailable() const {
212 assert(Resolver && "Pass not resident in a PassManager object!");
213
214 const void *PI = &AnalysisType::ID;
215
216 Pass *ResultPass = Resolver->getAnalysisIfAvailable(PI);
217 if (!ResultPass) return nullptr;
218
219 // Because the AnalysisType may not be a subclass of pass (for
220 // AnalysisGroups), we use getAdjustedAnalysisPointer here to potentially
221 // adjust the return pointer (because the class may multiply inherit, once
222 // from pass, once from AnalysisType).
223 return (AnalysisType*)ResultPass->getAdjustedAnalysisPointer(PI);
224}
225
226/// getAnalysis<AnalysisType>() - This function is used by subclasses to get
227/// to the analysis information that they claim to use by overriding the
228/// getAnalysisUsage function.
229template<typename AnalysisType>
230AnalysisType &Pass::getAnalysis() const {
231 assert(Resolver && "Pass has not been inserted into a PassManager object!");
232 return getAnalysisID<AnalysisType>(&AnalysisType::ID);
233}
234
235template<typename AnalysisType>
236AnalysisType &Pass::getAnalysisID(AnalysisID PI) const {
237 assert(PI && "getAnalysis for unregistered pass!");
238 assert(Resolver&&"Pass has not been inserted into a PassManager object!");
239 // PI *must* appear in AnalysisImpls. Because the number of passes used
240 // should be a small number, we just do a linear search over a (dense)
241 // vector.
242 Pass *ResultPass = Resolver->findImplPass(PI);
243 assert(ResultPass &&
244 "getAnalysis*() called on an analysis that was not "
245 "'required' by pass!");
246
247 // Because the AnalysisType may not be a subclass of pass (for
248 // AnalysisGroups), we use getAdjustedAnalysisPointer here to potentially
249 // adjust the return pointer (because the class may multiply inherit, once
250 // from pass, once from AnalysisType).
251 return *(AnalysisType*)ResultPass->getAdjustedAnalysisPointer(PI);
252}
253
254/// getAnalysis<AnalysisType>() - This function is used by subclasses to get
255/// to the analysis information that they claim to use by overriding the
256/// getAnalysisUsage function. If as part of the dependencies, an IR
257/// transformation is triggered (e.g. because the analysis requires
258/// BreakCriticalEdges), and Changed is non null, *Changed is updated.
259template <typename AnalysisType>
260AnalysisType &Pass::getAnalysis(Function &F, bool *Changed) {
261 assert(Resolver &&"Pass has not been inserted into a PassManager object!");
262
263 return getAnalysisID<AnalysisType>(&AnalysisType::ID, F, Changed);
264}
265
266template <typename AnalysisType>
267AnalysisType &Pass::getAnalysisID(AnalysisID PI, Function &F, bool *Changed) {
268 assert(PI && "getAnalysis for unregistered pass!");
269 assert(Resolver && "Pass has not been inserted into a PassManager object!");
270 // PI *must* appear in AnalysisImpls. Because the number of passes used
271 // should be a small number, we just do a linear search over a (dense)
272 // vector.
273 Pass *ResultPass;
274 bool LocalChanged;
275 std::tie(ResultPass, LocalChanged) = Resolver->findImplPass(this, PI, F);
276
277 assert(ResultPass && "Unable to find requested analysis info");
278 if (Changed)
279 *Changed |= LocalChanged;
280 else
281 assert(!LocalChanged &&
282 "A pass trigged a code update but the update status is lost");
283
284 // Because the AnalysisType may not be a subclass of pass (for
285 // AnalysisGroups), we use getAdjustedAnalysisPointer here to potentially
286 // adjust the return pointer (because the class may multiply inherit, once
287 // from pass, once from AnalysisType).
288 return *(AnalysisType*)ResultPass->getAdjustedAnalysisPointer(PI);
289}
290
291} // end namespace llvm
292
293#endif // LLVM_PASSANALYSISSUPPORT_H
aarch64 AArch64 CCMP Pass
#define F(x, y, z)
Definition: MD5.cpp:55
#define P(N)
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
This file contains some templates that are useful if you are working with the STL at all.
This file defines the SmallVector class.
AnalysisResolver - Simple interface used by Pass objects to pull all analysis information out of pass...
void addAnalysisImplsPair(AnalysisID PI, Pass *P)
Pass * findImplPass(AnalysisID PI)
Find pass that is implementing PI.
PMDataManager & getPMDataManager()
AnalysisResolver(PMDataManager &P)
void clearAnalysisImpls()
Clear cache that is used to connect a pass to the analysis (PassInfo).
Pass * getAnalysisIfAvailable(AnalysisID ID) const
Return analysis result or null if it doesn't exist.
Represent the analysis usage information of a pass.
AnalysisUsage & addRequiredID(const void *ID)
Definition: Pass.cpp:270
const VectorType & getRequiredSet() const
AnalysisUsage & addRequiredTransitiveID(char &ID)
Definition: Pass.cpp:280
AnalysisUsage & addPreservedID(char &ID)
AnalysisUsage & addPreservedID(const void *ID)
AnalysisUsage & addUsedIfAvailableID(const void *ID)
AnalysisUsage & addUsedIfAvailableID(char &ID)
AnalysisUsage()=default
const VectorType & getRequiredTransitiveSet() const
AnalysisUsage & addUsedIfAvailable()
Add the specified Pass class to the set of analyses used by this pass.
const VectorType & getUsedSet() const
AnalysisUsage & addRequired()
AnalysisUsage & addPreserved()
Add the specified Pass class to the set of analyses preserved by this pass.
void setPreservesCFG()
This function should be called by the pass, iff they do not:
Definition: Pass.cpp:256
void setPreservesAll()
Set by analyses that do not transform their input at all.
bool getPreservesAll() const
Determine whether a pass said it does not transform its input at all.
const VectorType & getPreservedSet() const
AnalysisUsage & addRequiredTransitive()
PMDataManager provides the common place to manage the analysis data used by pass managers.
Pass interface - Implemented by all 'passes'.
Definition: Pass.h:94
virtual void * getAdjustedAnalysisPointer(AnalysisID ID)
getAdjustedAnalysisPointer - This method is used when a pass implements an analysis interface through...
Definition: Pass.cpp:110
AnalysisType & getAnalysis() const
getAnalysis<AnalysisType>() - This function is used by subclasses to get to the analysis information ...
AnalysisType & getAnalysisID(AnalysisID PI) const
AnalysisType * getAnalysisIfAvailable() const
getAnalysisIfAvailable<AnalysisType>() - Subclasses use this function to get analysis information tha...
Interface for looking up the initializer for a variable name, used by Init::resolveReferences.
Definition: Record.h:2212
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1209
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
const void * AnalysisID
Definition: Pass.h:50
bool is_contained(R &&Range, const E &Element)
Returns true if Element is found in Range.
Definition: STLExtras.h:1886