LLVM 24.0.0git
PassManagerInternal.h
Go to the documentation of this file.
1//===- PassManager internal APIs and implementation details -----*- 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/// \file
9///
10/// This header provides internal APIs and implementation details used by the
11/// pass management interfaces exposed in PassManager.h. To understand more
12/// context of why these particular interfaces are needed, see that header
13/// file. None of these APIs should be used elsewhere.
14///
15//===----------------------------------------------------------------------===//
16
17#ifndef LLVM_IR_PASSMANAGERINTERNAL_H
18#define LLVM_IR_PASSMANAGERINTERNAL_H
19
20#include "llvm/ADT/STLExtras.h"
21#include "llvm/ADT/StringRef.h"
22#include "llvm/IR/Analysis.h"
24#include <memory>
25#include <type_traits>
26#include <utility>
27
28namespace llvm {
29
30template <typename IRUnitT> class AllAnalysesOn;
31template <typename IRUnitT, typename... ExtraArgTs> class AnalysisManager;
33
34// Implementation details of the pass manager interfaces.
35namespace detail {
36
37/// Template for the abstract base class used to dispatch over pass objects.
38/// This doesn't use virtual functions to avoid vtables, which cost a fair
39/// amount of storage that needs to be relocated in PIC builds and add an extra
40/// indirection on dispatch.
41template <typename IRUnitT, typename AnalysisManagerT, typename... ExtraArgTs>
43private:
44 using DestroyTy = void (*)(PassConcept &);
45 using RunTy = PreservedAnalyses (*)(PassConcept &, IRUnitT &,
46 AnalysisManagerT &, ExtraArgTs...);
47 using PrintPipelineTy =
48 void (*)(PassConcept &, raw_ostream &,
49 function_ref<StringRef(StringRef)> MapClassName2PassName);
50
51public:
52 struct Deleter {
53 void operator()(PassConcept *P) { P->Destroy(*P); }
54 };
55
56 using unique_ptr = std::unique_ptr<PassConcept, Deleter>;
57
58private:
59 StringRef Name;
60 bool IsRequired;
61
62 DestroyTy Destroy;
63 RunTy Run;
64 PrintPipelineTy PrintPipeline;
65
66protected:
67 PassConcept(StringRef Name, bool IsRequired, DestroyTy Destroy, RunTy Run,
68 PrintPipelineTy PrintPipeline)
69 : Name(Name), IsRequired(IsRequired), Destroy(Destroy), Run(Run),
70 PrintPipeline(PrintPipeline) {}
71
72 // Note: this is intentionally not public to catch uses of delete and
73 // unique_ptr<PassConcept>.
74 void operator delete(void *P) { ::operator delete(P); }
75
76public:
77 // Passes are immovable.
78 PassConcept(const PassConcept &) = delete;
79 PassConcept &operator=(const PassConcept &) = delete;
80
81 /// Run the pass.
82 PreservedAnalyses run(IRUnitT &IR, AnalysisManagerT &AM,
83 ExtraArgTs... ExtraArgs) {
84 return Run(*this, IR, AM, std::forward<ExtraArgTs>(ExtraArgs)...);
85 }
86
88 function_ref<StringRef(StringRef)> MapClassName2PassName) {
89 PrintPipeline(*this, OS, MapClassName2PassName);
90 }
91
92 /// Get name of a pass.
93 StringRef name() const { return Name; }
94
95 /// Indicate whether a pass can optionally be exempted from skipping by
96 /// PassInstrumentation.
97 /// To opt-in, pass should implement `static bool isRequired()`, or inherit
98 /// from `RequiredPassInfoMixin` or `OptionalPassInfoMixin`.
99 /// It's no-op to have `isRequired` always return false since that is the
100 /// default.
101 bool isRequired() const { return IsRequired; }
102};
103
104/// A template wrapper used to implement PassConcept.
105///
106/// Can be instantiated for any object which provides a \c run method accepting
107/// an \c IRUnitT& and an \c AnalysisManager<IRUnit>&.
108template <typename IRUnitT, typename PassT, typename AnalysisManagerT,
109 typename... ExtraArgTs>
110class PassModel final
111 : public PassConcept<IRUnitT, AnalysisManagerT, ExtraArgTs...> {
112private:
113 using PassConceptT = PassConcept<IRUnitT, AnalysisManagerT, ExtraArgTs...>;
114
115 PassT Pass;
116
117 static PassT &getPass(PassConceptT &Self) {
118 return static_cast<PassModel &>(Self).Pass;
119 }
120
121 static void destroyImpl(PassConceptT &Self) {
122 delete static_cast<PassModel *>(&Self);
123 }
124
125 static PreservedAnalyses runImpl(PassConceptT &Self, IRUnitT &IR,
126 AnalysisManagerT &AM,
127 ExtraArgTs... ExtraArgs) {
128 return getPass(Self).run(IR, AM, ExtraArgs...);
129 }
130
131 static void
132 printPipelineImpl(PassConceptT &Self, raw_ostream &OS,
133 function_ref<StringRef(StringRef)> MapClassName2PassName) {
134 getPass(Self).printPipeline(OS, MapClassName2PassName);
135 }
136
137 explicit PassModel(PassT &&Pass)
138 : PassConceptT(PassT::name(), PassT::isRequired(), destroyImpl, runImpl,
139 printPipelineImpl),
140 Pass(std::move(Pass)) {}
141
142public:
143 static typename PassConceptT::unique_ptr create(PassT &&Pass) {
144 return typename PassConceptT::unique_ptr(new PassModel(std::move(Pass)));
145 }
146};
147
148/// Abstract concept of an analysis result.
149///
150/// This concept is parameterized over the IR unit that this result pertains
151/// to.
152template <typename IRUnitT, typename InvalidatorT>
154 virtual ~AnalysisResultConcept() = default;
155
156 /// Method to try and mark a result as invalid.
157 ///
158 /// When the outer analysis manager detects a change in some underlying
159 /// unit of the IR, it will call this method on all of the results cached.
160 ///
161 /// \p PA is a set of preserved analyses which can be used to avoid
162 /// invalidation because the pass which changed the underlying IR took care
163 /// to update or preserve the analysis result in some way.
164 ///
165 /// \p Inv is typically a \c AnalysisManager::Invalidator object that can be
166 /// used by a particular analysis result to discover if other analyses
167 /// results are also invalidated in the event that this result depends on
168 /// them. See the documentation in the \c AnalysisManager for more details.
169 ///
170 /// \returns true if the result is indeed invalid (the default).
171 virtual bool invalidate(IRUnitT &IR, const PreservedAnalyses &PA,
172 InvalidatorT &Inv) = 0;
173};
174
175/// SFINAE metafunction for computing whether \c ResultT provides an
176/// \c invalidate member function.
177template <typename IRUnitT, typename ResultT> class ResultHasInvalidateMethod {
178 using EnabledType = char;
179 struct DisabledType {
180 char a, b;
181 };
182
183 // Purely to help out MSVC which fails to disable the below specialization,
184 // explicitly enable using the result type's invalidate routine if we can
185 // successfully call that routine.
186 template <typename T> struct Nonce { using Type = EnabledType; };
187 template <typename T>
188 static typename Nonce<decltype(std::declval<T>().invalidate(
189 std::declval<IRUnitT &>(), std::declval<PreservedAnalyses>()))>::Type
190 check(rank<2>);
191
192 // First we define an overload that can only be taken if there is no
193 // invalidate member. We do this by taking the address of an invalidate
194 // member in an adjacent base class of a derived class. This would be
195 // ambiguous if there were an invalidate member in the result type.
196 template <typename T, typename U> static DisabledType NonceFunction(T U::*);
197 struct CheckerBase { int invalidate; };
198 template <typename T> struct Checker : CheckerBase, std::remove_cv_t<T> {};
199 template <typename T>
200 static decltype(NonceFunction(&Checker<T>::invalidate)) check(rank<1>);
201
202 // Now we have the fallback that will only be reached when there is an
203 // invalidate member, and enables the trait.
204 template <typename T>
205 static EnabledType check(rank<0>);
206
207public:
208 enum { Value = sizeof(check<ResultT>(rank<2>())) == sizeof(EnabledType) };
209};
210
211/// Wrapper to model the analysis result concept.
212///
213/// By default, this will implement the invalidate method with a trivial
214/// implementation so that the actual analysis result doesn't need to provide
215/// an invalidation handler. It is only selected when the invalidation handler
216/// is not part of the ResultT's interface.
217template <typename IRUnitT, typename PassT, typename ResultT,
218 typename InvalidatorT,
219 bool HasInvalidateHandler =
222
223/// Specialization of \c AnalysisResultModel which provides the default
224/// invalidate functionality.
225template <typename IRUnitT, typename PassT, typename ResultT,
226 typename InvalidatorT>
227struct AnalysisResultModel<IRUnitT, PassT, ResultT, InvalidatorT, false>
228 : AnalysisResultConcept<IRUnitT, InvalidatorT> {
229 explicit AnalysisResultModel(ResultT Result) : Result(std::move(Result)) {}
230 // We have to explicitly define all the special member functions because MSVC
231 // refuses to generate them.
235
237 using std::swap;
238 swap(LHS.Result, RHS.Result);
239 }
240
242 swap(*this, RHS);
243 return *this;
244 }
245
246 /// The model bases invalidation solely on being in the preserved set.
247 //
248 // FIXME: We should actually use two different concepts for analysis results
249 // rather than two different models, and avoid the indirect function call for
250 // ones that use the trivial behavior.
251 bool invalidate(IRUnitT &, const PreservedAnalyses &PA,
252 InvalidatorT &) override {
253 auto PAC = PA.template getChecker<PassT>();
254 return !PAC.preserved() &&
255 !PAC.template preservedSet<AllAnalysesOn<IRUnitT>>();
256 }
257
258 ResultT Result;
259};
260
261/// Specialization of \c AnalysisResultModel which delegates invalidate
262/// handling to \c ResultT.
263template <typename IRUnitT, typename PassT, typename ResultT,
264 typename InvalidatorT>
265struct AnalysisResultModel<IRUnitT, PassT, ResultT, InvalidatorT, true>
266 : AnalysisResultConcept<IRUnitT, InvalidatorT> {
267 explicit AnalysisResultModel(ResultT Result) : Result(std::move(Result)) {}
268 // We have to explicitly define all the special member functions because MSVC
269 // refuses to generate them.
273
275 using std::swap;
276 swap(LHS.Result, RHS.Result);
277 }
278
280 swap(*this, RHS);
281 return *this;
282 }
283
284 /// The model delegates to the \c ResultT method.
285 bool invalidate(IRUnitT &IR, const PreservedAnalyses &PA,
286 InvalidatorT &Inv) override {
287 return Result.invalidate(IR, PA, Inv);
288 }
289
290 ResultT Result;
291};
292
293/// Abstract concept of an analysis pass.
294///
295/// This concept is parameterized over the IR unit that it can run over and
296/// produce an analysis result.
297template <typename IRUnitT, typename InvalidatorT, typename... ExtraArgTs>
299 virtual ~AnalysisPassConcept() = default;
300
301 /// Method to run this analysis over a unit of IR.
302 /// \returns A unique_ptr to the analysis result object to be queried by
303 /// users.
304 virtual std::unique_ptr<AnalysisResultConcept<IRUnitT, InvalidatorT>>
306 ExtraArgTs... ExtraArgs) = 0;
307
308 /// Polymorphic method to access the name of a pass.
309 virtual StringRef name() const = 0;
310};
311
312/// Wrapper to model the analysis pass concept.
313///
314/// Can wrap any type which implements a suitable \c run method. The method
315/// must accept an \c IRUnitT& and an \c AnalysisManager<IRUnitT>& as arguments
316/// and produce an object which can be wrapped in a \c AnalysisResultModel.
317template <typename IRUnitT, typename PassT, typename InvalidatorT,
318 typename... ExtraArgTs>
320 : AnalysisPassConcept<IRUnitT, InvalidatorT, ExtraArgTs...> {
321 explicit AnalysisPassModel(PassT Pass) : Pass(std::move(Pass)) {}
322 // We have to explicitly define all the special member functions because MSVC
323 // refuses to generate them.
326
328 using std::swap;
329 swap(LHS.Pass, RHS.Pass);
330 }
331
333 swap(*this, RHS);
334 return *this;
335 }
336
337 // FIXME: Replace PassT::Result with type traits when we use C++11.
340
341 /// The model delegates to the \c PassT::run method.
342 ///
343 /// The return is wrapped in an \c AnalysisResultModel.
344 std::unique_ptr<AnalysisResultConcept<IRUnitT, InvalidatorT>>
346 ExtraArgTs... ExtraArgs) override {
347 return std::make_unique<ResultModelT>(
348 Pass.run(IR, AM, std::forward<ExtraArgTs>(ExtraArgs)...));
349 }
350
351 /// The model delegates to a static \c PassT::name method.
352 ///
353 /// The returned string ref must point to constant immutable data!
354 StringRef name() const override { return PassT::name(); }
355
356 PassT Pass;
357};
358
359} // end namespace detail
360
361} // end namespace llvm
362
363#endif // LLVM_IR_PASSMANAGERINTERNAL_H
Legalize the Machine IR a function s Machine IR
Definition Legalizer.cpp:81
print mir2vec MIR2Vec Vocabulary Printer Pass
Definition MIR2Vec.cpp:598
#define T
#define P(N)
This file contains some templates that are useful if you are working with the STL at all.
Value * RHS
Value * LHS
This templated class represents "all analyses that operate over <aparticular IR unit>" (e....
Definition Analysis.h:50
A container for analyses that lazily runs them and caches their results.
A set of analyses that are preserved following a run of a transformation pass.
Definition Analysis.h:112
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
The instances of the Type class are immutable: once they are created, they are never changed.
Definition Type.h:46
PassConcept(const PassConcept &)=delete
PassConcept & operator=(const PassConcept &)=delete
PassConcept(StringRef Name, bool IsRequired, DestroyTy Destroy, RunTy Run, PrintPipelineTy PrintPipeline)
bool isRequired() const
Indicate whether a pass can optionally be exempted from skipping by PassInstrumentation.
void printPipeline(raw_ostream &OS, function_ref< StringRef(StringRef)> MapClassName2PassName)
StringRef name() const
Get name of a pass.
PreservedAnalyses run(IRUnitT &IR, AnalysisManagerT &AM, ExtraArgTs... ExtraArgs)
Run the pass.
static PassConceptT::unique_ptr create(PassT &&Pass)
SFINAE metafunction for computing whether ResultT provides an invalidate member function.
An efficient, type-erasing, non-owning reference to a callable.
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition raw_ostream.h:53
Pass manager infrastructure for declaring and invalidating analyses.
A self-contained host- and target-independent arbitrary-precision floating-point software implementat...
Definition ADL.h:123
This is an optimization pass for GlobalISel generic memory operations.
OutputIt move(R &&Range, OutputIt Out)
Provide wrappers to std::move which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1917
Implement std::hash so that hash_code can be used in STL containers.
Definition BitVector.h:878
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
Definition BitVector.h:880
Abstract concept of an analysis pass.
virtual StringRef name() const =0
Polymorphic method to access the name of a pass.
virtual std::unique_ptr< AnalysisResultConcept< IRUnitT, InvalidatorT > > run(IRUnitT &IR, AnalysisManager< IRUnitT, ExtraArgTs... > &AM, ExtraArgTs... ExtraArgs)=0
Method to run this analysis over a unit of IR.
virtual ~AnalysisPassConcept()=default
AnalysisPassModel(const AnalysisPassModel &Arg)
StringRef name() const override
The model delegates to a static PassT::name method.
AnalysisResultModel< IRUnitT, PassT, typename PassT::Result, InvalidatorT > ResultModelT
std::unique_ptr< AnalysisResultConcept< IRUnitT, InvalidatorT > > run(IRUnitT &IR, AnalysisManager< IRUnitT, ExtraArgTs... > &AM, ExtraArgTs... ExtraArgs) override
The model delegates to the PassT::run method.
friend void swap(AnalysisPassModel &LHS, AnalysisPassModel &RHS)
AnalysisPassModel & operator=(AnalysisPassModel RHS)
AnalysisPassModel(AnalysisPassModel &&Arg)
Abstract concept of an analysis result.
virtual ~AnalysisResultConcept()=default
virtual bool invalidate(IRUnitT &IR, const PreservedAnalyses &PA, InvalidatorT &Inv)=0
Method to try and mark a result as invalid.
bool invalidate(IRUnitT &, const PreservedAnalyses &PA, InvalidatorT &) override
The model bases invalidation solely on being in the preserved set.
friend void swap(AnalysisResultModel &LHS, AnalysisResultModel &RHS)
bool invalidate(IRUnitT &IR, const PreservedAnalyses &PA, InvalidatorT &Inv) override
The model delegates to the ResultT method.
friend void swap(AnalysisResultModel &LHS, AnalysisResultModel &RHS)
Wrapper to model the analysis result concept.
Utility type to build an inheritance chain that makes it easy to rank overload candidates.
Definition STLExtras.h:1468