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
76 MachineFunctionAnalysisManager &AM) override {
77 return Pass.run(IR, AM);
78 }
79
82 function_ref<StringRef(StringRef)> MapClassName2PassName) override {
83 Pass.printPipeline(OS, MapClassName2PassName);
84 }
85
86 StringRef name() const override { return PassT::name(); }
87
88 template <typename T>
89 using has_required_t = decltype(std::declval<T &>().isRequired());
90 template <typename T>
91 static std::enable_if_t<is_detected<has_required_t, T>::value, bool>
93 return T::isRequired();
94 }
95 template <typename T>
96 static std::enable_if_t<!is_detected<has_required_t, T>::value, bool>
98 return false;
99 }
100 bool isRequired() const override { return passIsRequiredImpl<PassT>(); }
101
102 template <typename T>
104 decltype(std::declval<T &>().getRequiredProperties());
105 template <typename T>
106 static std::enable_if_t<is_detected<has_get_required_properties_t, T>::value,
109 return PassT::getRequiredProperties();
110 }
111 template <typename T>
112 static std::enable_if_t<!is_detected<has_get_required_properties_t, T>::value,
116 }
118 return getRequiredPropertiesImpl<PassT>();
119 }
120
121 template <typename T>
123 decltype(std::declval<T &>().getSetProperties());
124 template <typename T>
125 static std::enable_if_t<is_detected<has_get_set_properties_t, T>::value,
128 return PassT::getSetProperties();
129 }
130 template <typename T>
131 static std::enable_if_t<!is_detected<has_get_set_properties_t, T>::value,
135 }
137 return getSetPropertiesImpl<PassT>();
138 }
139
140 template <typename T>
142 decltype(std::declval<T &>().getClearedProperties());
143 template <typename T>
144 static std::enable_if_t<is_detected<has_get_cleared_properties_t, T>::value,
147 return PassT::getClearedProperties();
148 }
149 template <typename T>
150 static std::enable_if_t<!is_detected<has_get_cleared_properties_t, T>::value,
154 }
156 return getClearedPropertiesImpl<PassT>();
157 }
158
159 PassT Pass;
160};
161} // namespace detail
162
165
166template <>
167bool MachineFunctionAnalysisManagerModuleProxy::Result::invalidate(
168 Module &M, const PreservedAnalyses &PA,
171 Module>;
172
175/// Provide the \c ModuleAnalysisManager to \c Function proxy.
178
180 : public AnalysisInfoMixin<FunctionAnalysisManagerMachineFunctionProxy> {
181public:
182 class Result {
183 public:
184 explicit Result(FunctionAnalysisManager &FAM) : FAM(&FAM) {}
185
186 Result(Result &&Arg) : FAM(std::move(Arg.FAM)) {
187 // We have to null out the analysis manager in the moved-from state
188 // because we are taking ownership of the responsibilty to clear the
189 // analysis state.
190 Arg.FAM = nullptr;
191 }
192
194 // FAM is cleared in a moved from state where there is nothing to do.
195 if (!FAM)
196 return;
197
198 // Clear out the analysis manager if we're being destroyed -- it means we
199 // didn't even see an invalidate call when we got invalidated.
200 FAM->clear();
201 }
202
204 FAM = RHS.FAM;
205 // We have to null out the analysis manager in the moved-from state
206 // because we are taking ownership of the responsibilty to clear the
207 // analysis state.
208 RHS.FAM = nullptr;
209 return *this;
210 }
211
212 /// Accessor for the analysis manager.
214
215 /// Handler for invalidation of the outer IR unit, \c IRUnitT.
216 ///
217 /// If the proxy analysis itself is not preserved, we assume that the set of
218 /// inner IR objects contained in IRUnit may have changed. In this case,
219 /// we have to call \c clear() on the inner analysis manager, as it may now
220 /// have stale pointers to its inner IR objects.
221 ///
222 /// Regardless of whether the proxy analysis is marked as preserved, all of
223 /// the analyses in the inner analysis manager are potentially invalidated
224 /// based on the set of preserved analyses.
227
228 private:
230 };
231
234 : FAM(&FAM) {}
235
236 /// Run the analysis pass and create our proxy result object.
237 ///
238 /// This doesn't do any interesting work; it is primarily used to insert our
239 /// proxy result object into the outer analysis cache so that we can proxy
240 /// invalidation to the inner analysis manager.
242 return Result(*FAM);
243 }
244
246
247private:
249};
250
252 : public PassInfoMixin<ModuleToMachineFunctionPassAdaptor> {
254
255public:
257 std::unique_ptr<MachinePassConcept> Pass)
258 : Pass(std::move(Pass)) {}
259
260 /// Runs the function pass across every function in the module.
263 function_ref<StringRef(StringRef)> MapClassName2PassName);
264
265 static bool isRequired() { return true; }
266
267private:
268 std::unique_ptr<MachinePassConcept> Pass;
269};
270
271template <typename MachineFunctionPassT>
272ModuleToMachineFunctionPassAdaptor
275 // Do not use make_unique, it causes too many template instantiations,
276 // causing terrible compile times.
278 std::unique_ptr<detail::MachinePassConcept>(
279 new PassModelT(std::forward<MachineFunctionPassT>(Pass))));
280}
281
282template <>
283PreservedAnalyses
286extern template class PassManager<MachineFunction>;
287
288/// Convenience typedef for a pass manager over functions.
290
291} // end namespace llvm
292
293#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
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
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.