LLVM 23.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
38/// polymorphically over pass objects.
39template <typename IRUnitT, typename AnalysisManagerT, typename... ExtraArgTs>
41 // Boiler plate necessary for the container of derived classes.
42 virtual ~PassConcept() = default;
43
44 /// The polymorphic API which runs the pass over a given IR entity.
45 ///
46 /// Note that actual pass object can omit the analysis manager argument if
47 /// desired. Also that the analysis manager may be null if there is no
48 /// analysis manager in the pass pipeline.
49 virtual PreservedAnalyses run(IRUnitT &IR, AnalysisManagerT &AM,
50 ExtraArgTs... ExtraArgs) = 0;
51
52 virtual void
54 function_ref<StringRef(StringRef)> MapClassName2PassName) = 0;
55 /// Polymorphic method to access the name of a pass.
56 virtual StringRef name() const = 0;
57
58 /// Polymorphic method to let a pass optionally exempted from skipping by
59 /// PassInstrumentation.
60 /// To opt-in, pass should implement `static bool isRequired()`, or inherit
61 /// from `RequiredPassInfoMixin` or `OptionalPassInfoMixin`.
62 /// It's no-op to have `isRequired` always return false since that is the
63 /// default.
64 virtual bool isRequired() const = 0;
65};
66
67/// A template wrapper used to implement the polymorphic API.
68///
69/// Can be instantiated for any object which provides a \c run method accepting
70/// an \c IRUnitT& and an \c AnalysisManager<IRUnit>&. It requires the pass to
71/// be a copyable object.
72template <typename IRUnitT, typename PassT, typename AnalysisManagerT,
73 typename... ExtraArgTs>
74struct PassModel : PassConcept<IRUnitT, AnalysisManagerT, ExtraArgTs...> {
75 explicit PassModel(PassT Pass) : Pass(std::move(Pass)) {}
76 // We have to explicitly define all the special member functions because MSVC
77 // refuses to generate them.
78 PassModel(const PassModel &Arg) : Pass(Arg.Pass) {}
79 PassModel(PassModel &&Arg) : Pass(std::move(Arg.Pass)) {}
80
81 friend void swap(PassModel &LHS, PassModel &RHS) {
82 using std::swap;
83 swap(LHS.Pass, RHS.Pass);
84 }
85
87 swap(*this, RHS);
88 return *this;
89 }
90
91 PreservedAnalyses run(IRUnitT &IR, AnalysisManagerT &AM,
92 ExtraArgTs... ExtraArgs) override {
93 return Pass.run(IR, AM, ExtraArgs...);
94 }
95
97 raw_ostream &OS,
98 function_ref<StringRef(StringRef)> MapClassName2PassName) override {
99 Pass.printPipeline(OS, MapClassName2PassName);
100 }
101
102 StringRef name() const override { return PassT::name(); }
103
104 bool isRequired() const override { return PassT::isRequired(); }
105
106 PassT Pass;
107};
108
109/// Abstract concept of an analysis result.
110///
111/// This concept is parameterized over the IR unit that this result pertains
112/// to.
113template <typename IRUnitT, typename InvalidatorT>
115 virtual ~AnalysisResultConcept() = default;
116
117 /// Method to try and mark a result as invalid.
118 ///
119 /// When the outer analysis manager detects a change in some underlying
120 /// unit of the IR, it will call this method on all of the results cached.
121 ///
122 /// \p PA is a set of preserved analyses which can be used to avoid
123 /// invalidation because the pass which changed the underlying IR took care
124 /// to update or preserve the analysis result in some way.
125 ///
126 /// \p Inv is typically a \c AnalysisManager::Invalidator object that can be
127 /// used by a particular analysis result to discover if other analyses
128 /// results are also invalidated in the event that this result depends on
129 /// them. See the documentation in the \c AnalysisManager for more details.
130 ///
131 /// \returns true if the result is indeed invalid (the default).
132 virtual bool invalidate(IRUnitT &IR, const PreservedAnalyses &PA,
133 InvalidatorT &Inv) = 0;
134};
135
136/// SFINAE metafunction for computing whether \c ResultT provides an
137/// \c invalidate member function.
138template <typename IRUnitT, typename ResultT> class ResultHasInvalidateMethod {
139 using EnabledType = char;
140 struct DisabledType {
141 char a, b;
142 };
143
144 // Purely to help out MSVC which fails to disable the below specialization,
145 // explicitly enable using the result type's invalidate routine if we can
146 // successfully call that routine.
147 template <typename T> struct Nonce { using Type = EnabledType; };
148 template <typename T>
149 static typename Nonce<decltype(std::declval<T>().invalidate(
150 std::declval<IRUnitT &>(), std::declval<PreservedAnalyses>()))>::Type
151 check(rank<2>);
152
153 // First we define an overload that can only be taken if there is no
154 // invalidate member. We do this by taking the address of an invalidate
155 // member in an adjacent base class of a derived class. This would be
156 // ambiguous if there were an invalidate member in the result type.
157 template <typename T, typename U> static DisabledType NonceFunction(T U::*);
158 struct CheckerBase { int invalidate; };
159 template <typename T> struct Checker : CheckerBase, std::remove_cv_t<T> {};
160 template <typename T>
161 static decltype(NonceFunction(&Checker<T>::invalidate)) check(rank<1>);
162
163 // Now we have the fallback that will only be reached when there is an
164 // invalidate member, and enables the trait.
165 template <typename T>
166 static EnabledType check(rank<0>);
167
168public:
169 enum { Value = sizeof(check<ResultT>(rank<2>())) == sizeof(EnabledType) };
170};
171
172/// Wrapper to model the analysis result concept.
173///
174/// By default, this will implement the invalidate method with a trivial
175/// implementation so that the actual analysis result doesn't need to provide
176/// an invalidation handler. It is only selected when the invalidation handler
177/// is not part of the ResultT's interface.
178template <typename IRUnitT, typename PassT, typename ResultT,
179 typename InvalidatorT,
180 bool HasInvalidateHandler =
183
184/// Specialization of \c AnalysisResultModel which provides the default
185/// invalidate functionality.
186template <typename IRUnitT, typename PassT, typename ResultT,
187 typename InvalidatorT>
188struct AnalysisResultModel<IRUnitT, PassT, ResultT, InvalidatorT, false>
189 : AnalysisResultConcept<IRUnitT, InvalidatorT> {
190 explicit AnalysisResultModel(ResultT Result) : Result(std::move(Result)) {}
191 // We have to explicitly define all the special member functions because MSVC
192 // refuses to generate them.
196
198 using std::swap;
199 swap(LHS.Result, RHS.Result);
200 }
201
203 swap(*this, RHS);
204 return *this;
205 }
206
207 /// The model bases invalidation solely on being in the preserved set.
208 //
209 // FIXME: We should actually use two different concepts for analysis results
210 // rather than two different models, and avoid the indirect function call for
211 // ones that use the trivial behavior.
212 bool invalidate(IRUnitT &, const PreservedAnalyses &PA,
213 InvalidatorT &) override {
214 auto PAC = PA.template getChecker<PassT>();
215 return !PAC.preserved() &&
216 !PAC.template preservedSet<AllAnalysesOn<IRUnitT>>();
217 }
218
219 ResultT Result;
220};
221
222/// Specialization of \c AnalysisResultModel which delegates invalidate
223/// handling to \c ResultT.
224template <typename IRUnitT, typename PassT, typename ResultT,
225 typename InvalidatorT>
226struct AnalysisResultModel<IRUnitT, PassT, ResultT, InvalidatorT, true>
227 : AnalysisResultConcept<IRUnitT, InvalidatorT> {
228 explicit AnalysisResultModel(ResultT Result) : Result(std::move(Result)) {}
229 // We have to explicitly define all the special member functions because MSVC
230 // refuses to generate them.
234
236 using std::swap;
237 swap(LHS.Result, RHS.Result);
238 }
239
241 swap(*this, RHS);
242 return *this;
243 }
244
245 /// The model delegates to the \c ResultT method.
246 bool invalidate(IRUnitT &IR, const PreservedAnalyses &PA,
247 InvalidatorT &Inv) override {
248 return Result.invalidate(IR, PA, Inv);
249 }
250
251 ResultT Result;
252};
253
254/// Abstract concept of an analysis pass.
255///
256/// This concept is parameterized over the IR unit that it can run over and
257/// produce an analysis result.
258template <typename IRUnitT, typename InvalidatorT, typename... ExtraArgTs>
260 virtual ~AnalysisPassConcept() = default;
261
262 /// Method to run this analysis over a unit of IR.
263 /// \returns A unique_ptr to the analysis result object to be queried by
264 /// users.
265 virtual std::unique_ptr<AnalysisResultConcept<IRUnitT, InvalidatorT>>
267 ExtraArgTs... ExtraArgs) = 0;
268
269 /// Polymorphic method to access the name of a pass.
270 virtual StringRef name() const = 0;
271};
272
273/// Wrapper to model the analysis pass concept.
274///
275/// Can wrap any type which implements a suitable \c run method. The method
276/// must accept an \c IRUnitT& and an \c AnalysisManager<IRUnitT>& as arguments
277/// and produce an object which can be wrapped in a \c AnalysisResultModel.
278template <typename IRUnitT, typename PassT, typename InvalidatorT,
279 typename... ExtraArgTs>
281 : AnalysisPassConcept<IRUnitT, InvalidatorT, ExtraArgTs...> {
282 explicit AnalysisPassModel(PassT Pass) : Pass(std::move(Pass)) {}
283 // We have to explicitly define all the special member functions because MSVC
284 // refuses to generate them.
287
289 using std::swap;
290 swap(LHS.Pass, RHS.Pass);
291 }
292
294 swap(*this, RHS);
295 return *this;
296 }
297
298 // FIXME: Replace PassT::Result with type traits when we use C++11.
301
302 /// The model delegates to the \c PassT::run method.
303 ///
304 /// The return is wrapped in an \c AnalysisResultModel.
305 std::unique_ptr<AnalysisResultConcept<IRUnitT, InvalidatorT>>
307 ExtraArgTs... ExtraArgs) override {
308 return std::make_unique<ResultModelT>(
309 Pass.run(IR, AM, std::forward<ExtraArgTs>(ExtraArgs)...));
310 }
311
312 /// The model delegates to a static \c PassT::name method.
313 ///
314 /// The returned string ref must point to constant immutable data!
315 StringRef name() const override { return PassT::name(); }
316
317 PassT Pass;
318};
319
320} // end namespace detail
321
322} // end namespace llvm
323
324#endif // LLVM_IR_PASSMANAGERINTERNAL_H
Legalize the Machine IR a function s Machine IR
Definition Legalizer.cpp:81
#define T
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
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:1916
Implement std::hash so that hash_code can be used in STL containers.
Definition BitVector.h:874
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
Definition BitVector.h:876
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.
Template for the abstract base class used to dispatch polymorphically over pass objects.
virtual StringRef name() const =0
Polymorphic method to access the name of a pass.
virtual void printPipeline(raw_ostream &OS, function_ref< StringRef(StringRef)> MapClassName2PassName)=0
virtual bool isRequired() const =0
Polymorphic method to let a pass optionally exempted from skipping by PassInstrumentation.
virtual PreservedAnalyses run(IRUnitT &IR, AnalysisManagerT &AM, ExtraArgTs... ExtraArgs)=0
The polymorphic API which runs the pass over a given IR entity.
virtual ~PassConcept()=default
friend void swap(PassModel &LHS, PassModel &RHS)
void printPipeline(raw_ostream &OS, function_ref< StringRef(StringRef)> MapClassName2PassName) override
PreservedAnalyses run(IRUnitT &IR, AnalysisManagerT &AM, ExtraArgTs... ExtraArgs) override
The polymorphic API which runs the pass over a given IR entity.
PassModel & operator=(PassModel RHS)
bool isRequired() const override
Polymorphic method to let a pass optionally exempted from skipping by PassInstrumentation.
PassModel(const PassModel &Arg)
StringRef name() const override
Polymorphic method to access the name of a pass.
Utility type to build an inheritance chain that makes it easy to rank overload candidates.
Definition STLExtras.h:1467