LLVM 19.0.0git
MachinePassManager.h
Go to the documentation of this file.
1//===- PassManager.h --- Pass management for CodeGen ------------*- 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 header defines the pass manager interface for codegen. The codegen
10// pipeline consists of only machine function passes. There is no container
11// relationship between IR module/function and machine function in terms of pass
12// manager organization. So there is no need for adaptor classes (for example
13// ModuleToMachineFunctionAdaptor). Since invalidation could only happen among
14// machine function passes, there is no proxy classes to handle cross-IR-unit
15// invalidation. IR analysis results are provided for machine function passes by
16// their respective analysis managers such as ModuleAnalysisManager and
17// FunctionAnalysisManager.
18//
19// TODO: Add MachineFunctionProperties support.
20//
21//===----------------------------------------------------------------------===//
22
23#ifndef LLVM_CODEGEN_MACHINEPASSMANAGER_H
24#define LLVM_CODEGEN_MACHINEPASSMANAGER_H
25
29#include "llvm/IR/PassManager.h"
31#include "llvm/Support/Error.h"
32
33namespace llvm {
34class Module;
35class Function;
36class MachineFunction;
37
38extern template class AnalysisManager<MachineFunction>;
40
41/// A CRTP mix-in that provides informational APIs needed for machine passes.
42///
43/// This provides some boilerplate for types that are machine passes. It
44/// automatically mixes in \c PassInfoMixin.
45template <typename DerivedT>
47 // TODO: Add MachineFunctionProperties support.
48};
49
50namespace detail {
56};
57
58template <typename PassT> struct MachinePassModel : MachinePassConcept {
59 explicit MachinePassModel(PassT &&Pass) : Pass(std::move(Pass)) {}
60 // We have to explicitly define all the special member functions because MSVC
61 // refuses to generate them.
64
66 using std::swap;
67 swap(LHS.Pass, RHS.Pass);
68 }
69
71 swap(*this, RHS);
72 return *this;
73 }
74
77 MachineFunctionAnalysisManager &AM) override {
78 return Pass.run(IR, AM);
79 }
80
83 function_ref<StringRef(StringRef)> MapClassName2PassName) override {
84 Pass.printPipeline(OS, MapClassName2PassName);
85 }
86
87 StringRef name() const override { return PassT::name(); }
88
89 template <typename T>
90 using has_required_t = decltype(std::declval<T &>().isRequired());
91 template <typename T>
92 static std::enable_if_t<is_detected<has_required_t, T>::value, bool>
94 return T::isRequired();
95 }
96 template <typename T>
97 static std::enable_if_t<!is_detected<has_required_t, T>::value, bool>
99 return false;
100 }
101 bool isRequired() const override { return passIsRequiredImpl<PassT>(); }
102
103 template <typename T>
105 decltype(std::declval<T &>().getRequiredProperties());
106 template <typename T>
107 static std::enable_if_t<is_detected<has_get_required_properties_t, T>::value,
110 return PassT::getRequiredProperties();
111 }
112 template <typename T>
113 static std::enable_if_t<!is_detected<has_get_required_properties_t, T>::value,
117 }
119 return getRequiredPropertiesImpl<PassT>();
120 }
121
122 template <typename T>
124 decltype(std::declval<T &>().getSetProperties());
125 template <typename T>
126 static std::enable_if_t<is_detected<has_get_set_properties_t, T>::value,
129 return PassT::getSetProperties();
130 }
131 template <typename T>
132 static std::enable_if_t<!is_detected<has_get_set_properties_t, T>::value,
136 }
138 return getSetPropertiesImpl<PassT>();
139 }
140
141 template <typename T>
143 decltype(std::declval<T &>().getClearedProperties());
144 template <typename T>
145 static std::enable_if_t<is_detected<has_get_cleared_properties_t, T>::value,
148 return PassT::getClearedProperties();
149 }
150 template <typename T>
151 static std::enable_if_t<!is_detected<has_get_cleared_properties_t, T>::value,
155 }
157 return getClearedPropertiesImpl<PassT>();
158 }
159
160 PassT Pass;
161};
162} // namespace detail
163
166
167template <>
168bool MachineFunctionAnalysisManagerModuleProxy::Result::invalidate(
169 Module &M, const PreservedAnalyses &PA,
172 Module>;
173
176/// Provide the \c ModuleAnalysisManager to \c Function proxy.
179
181 : public AnalysisInfoMixin<FunctionAnalysisManagerMachineFunctionProxy> {
182public:
183 class Result {
184 public:
185 explicit Result(FunctionAnalysisManager &FAM) : FAM(&FAM) {}
186
187 Result(Result &&Arg) : FAM(std::move(Arg.FAM)) {
188 // We have to null out the analysis manager in the moved-from state
189 // because we are taking ownership of the responsibilty to clear the
190 // analysis state.
191 Arg.FAM = nullptr;
192 }
193
195 // FAM is cleared in a moved from state where there is nothing to do.
196 if (!FAM)
197 return;
198
199 // Clear out the analysis manager if we're being destroyed -- it means we
200 // didn't even see an invalidate call when we got invalidated.
201 FAM->clear();
202 }
203
205 FAM = RHS.FAM;
206 // We have to null out the analysis manager in the moved-from state
207 // because we are taking ownership of the responsibilty to clear the
208 // analysis state.
209 RHS.FAM = nullptr;
210 return *this;
211 }
212
213 /// Accessor for the analysis manager.
215
216 /// Handler for invalidation of the outer IR unit, \c IRUnitT.
217 ///
218 /// If the proxy analysis itself is not preserved, we assume that the set of
219 /// inner IR objects contained in IRUnit may have changed. In this case,
220 /// we have to call \c clear() on the inner analysis manager, as it may now
221 /// have stale pointers to its inner IR objects.
222 ///
223 /// Regardless of whether the proxy analysis is marked as preserved, all of
224 /// the analyses in the inner analysis manager are potentially invalidated
225 /// based on the set of preserved analyses.
228
229 private:
231 };
232
235 : FAM(&FAM) {}
236
237 /// Run the analysis pass and create our proxy result object.
238 ///
239 /// This doesn't do any interesting work; it is primarily used to insert our
240 /// proxy result object into the outer analysis cache so that we can proxy
241 /// invalidation to the inner analysis manager.
243 return Result(*FAM);
244 }
245
247
248private:
250};
251
253 : public PassInfoMixin<ModuleToMachineFunctionPassAdaptor> {
255
256public:
258 std::unique_ptr<MachinePassConcept> Pass)
259 : Pass(std::move(Pass)) {}
260
261 /// Runs the function pass across every function in the module.
264 function_ref<StringRef(StringRef)> MapClassName2PassName);
265
266 static bool isRequired() { return true; }
267
268private:
269 std::unique_ptr<MachinePassConcept> Pass;
270};
271
272template <typename MachineFunctionPassT>
273ModuleToMachineFunctionPassAdaptor
276 // Do not use make_unique, it causes too many template instantiations,
277 // causing terrible compile times.
279 std::unique_ptr<detail::MachinePassConcept>(
280 new PassModelT(std::forward<MachineFunctionPassT>(Pass))));
281}
282
283template <>
284PreservedAnalyses
287extern template class PassManager<MachineFunction>;
288
289/// Convenience typedef for a pass manager over functions.
291
292} // end namespace llvm
293
294#endif // LLVM_CODEGEN_MACHINEPASSMANAGER_H
This file provides a collection of function (or more generally, callable) type erasure utilities supp...
Legalize the Machine IR a function s Machine IR
Definition: Legalizer.cpp:81
Machine Check Debug Module
FunctionAnalysisManager FAM
This header provides internal APIs and implementation details used by the pass management interfaces ...
This header defines various interfaces for pass management in LLVM.
raw_pwrite_stream & OS
This file defines the SmallVector class.
Value * RHS
Value * LHS
API to communicate dependencies between analyses during invalidation.
Definition: PassManager.h:387
A container for analyses that lazily runs them and caches their results.
Definition: PassManager.h:348
void clear(IRUnitT &IR, llvm::StringRef Name)
Clear any cached analysis results for a single unit of IR.
FunctionAnalysisManager & getManager()
Accessor for the analysis manager.
bool invalidate(MachineFunction &IR, const PreservedAnalyses &PA, MachineFunctionAnalysisManager::Invalidator &Inv)
Handler for invalidation of the outer IR unit, IRUnitT.
FunctionAnalysisManagerMachineFunctionProxy(FunctionAnalysisManager &FAM)
Result run(MachineFunction &, MachineFunctionAnalysisManager &)
Run the analysis pass and create our proxy result object.
An analysis over an "outer" IR unit that provides access to an analysis manager over an "inner" IR un...
Definition: PassManager.h:658
Properties which a MachineFunction may have at a given point in time.
ModuleToMachineFunctionPassAdaptor(std::unique_ptr< MachinePassConcept > Pass)
void printPipeline(raw_ostream &OS, function_ref< StringRef(StringRef)> MapClassName2PassName)
PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM)
Runs the function pass across every function in the module.
A Module instance is used to store all the information related to an LLVM module.
Definition: Module.h:65
An analysis over an "inner" IR unit that provides access to an analysis manager over a "outer" IR uni...
Definition: PassManager.h:783
Manages a sequence of passes over a particular unit of IR.
Definition: PassManager.h:190
PreservedAnalyses run(IRUnitT &IR, AnalysisManagerT &AM, ExtraArgTs... ExtraArgs)
Run all of the passes in this manager over the given unit of IR.
Definition: PassManager.h:218
Pass interface - Implemented by all 'passes'.
Definition: Pass.h:94
A set of analyses that are preserved following a run of a transformation pass.
Definition: Analysis.h:109
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
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:52
Definition: ClauseT.h:79
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
AnalysisManager< MachineFunction > MachineFunctionAnalysisManager
AnalysisManager< Module > ModuleAnalysisManager
Convenience typedef for the Module analysis manager.
Definition: PassManager.h:632
ModuleToMachineFunctionPassAdaptor createModuleToMachineFunctionPassAdaptor(MachineFunctionPassT &&Pass)
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:1858
Implement std::hash so that hash_code can be used in STL containers.
Definition: BitVector.h:858
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
Definition: BitVector.h:860
A CRTP mix-in that provides informational APIs needed for analysis passes.
Definition: PassManager.h:114
A special type used by analysis passes to provide an address that identifies that particular analysis...
Definition: Analysis.h:26
A CRTP mix-in that provides informational APIs needed for machine passes.
A CRTP mix-in to automatically provide informational APIs needed for passes.
Definition: PassManager.h:91
virtual MachineFunctionProperties getClearedProperties() const =0
virtual MachineFunctionProperties getRequiredProperties() const =0
virtual MachineFunctionProperties getSetProperties() const =0
static std::enable_if_t<!is_detected< has_get_required_properties_t, T >::value, MachineFunctionProperties > getRequiredPropertiesImpl()
MachineFunctionProperties getSetProperties() const override
StringRef name() const override
Polymorphic method to access the name of a pass.
static std::enable_if_t< is_detected< has_required_t, T >::value, bool > passIsRequiredImpl()
MachineFunctionProperties getRequiredProperties() const override
MachinePassModel(MachinePassModel &&Arg)
decltype(std::declval< T & >().isRequired()) has_required_t
MachinePassModel & operator=(const MachinePassModel &)=delete
static std::enable_if_t< is_detected< has_get_cleared_properties_t, T >::value, MachineFunctionProperties > getClearedPropertiesImpl()
PreservedAnalyses run(MachineFunction &IR, MachineFunctionAnalysisManager &AM) override
MachineFunctionProperties getClearedProperties() const override
void printPipeline(raw_ostream &OS, function_ref< StringRef(StringRef)> MapClassName2PassName) override
decltype(std::declval< T & >().getSetProperties()) has_get_set_properties_t
MachinePassModel & operator=(MachinePassModel RHS)
static std::enable_if_t<!is_detected< has_get_set_properties_t, T >::value, MachineFunctionProperties > getSetPropertiesImpl()
static std::enable_if_t< is_detected< has_get_required_properties_t, T >::value, MachineFunctionProperties > getRequiredPropertiesImpl()
bool isRequired() const override
Polymorphic method to let a pass optionally exempted from skipping by PassInstrumentation.
static std::enable_if_t< is_detected< has_get_set_properties_t, T >::value, MachineFunctionProperties > getSetPropertiesImpl()
MachinePassModel(const MachinePassModel &Arg)
static std::enable_if_t<!is_detected< has_get_cleared_properties_t, T >::value, MachineFunctionProperties > getClearedPropertiesImpl()
decltype(std::declval< T & >().getRequiredProperties()) has_get_required_properties_t
friend void swap(MachinePassModel &LHS, MachinePassModel &RHS)
decltype(std::declval< T & >().getClearedProperties()) has_get_cleared_properties_t
static std::enable_if_t<!is_detected< has_required_t, T >::value, bool > passIsRequiredImpl()
Template for the abstract base class used to dispatch polymorphically over pass objects.