LLVM 22.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//===----------------------------------------------------------------------===//
20
21#ifndef LLVM_CODEGEN_MACHINEPASSMANAGER_H
22#define LLVM_CODEGEN_MACHINEPASSMANAGER_H
23
28#include "llvm/IR/PassManager.h"
31#include "llvm/Support/Error.h"
32
33namespace llvm {
34class Module;
35class Function;
36
37/// An RAII based helper class to modify MachineFunctionProperties when running
38/// pass. Define a MFPropsModifier in PassT::run to set
39/// MachineFunctionProperties properly.
40template <typename PassT> class MFPropsModifier {
41public:
42 MFPropsModifier(const PassT &P_, MachineFunction &MF_) : P(P_), MF(MF_) {
43 auto &MFProps = MF.getProperties();
44#ifndef NDEBUG
45 if constexpr (has_get_required_properties_v<PassT>) {
46 auto &MFProps = MF.getProperties();
47 auto RequiredProperties = P.getRequiredProperties();
48 if (!MFProps.verifyRequiredProperties(RequiredProperties)) {
49 errs() << "MachineFunctionProperties required by " << PassT::name()
50 << " pass are not met by function " << MF.getName() << ".\n"
51 << "Required properties: ";
52 RequiredProperties.print(errs());
53 errs() << "\nCurrent properties: ";
54 MFProps.print(errs());
55 errs() << '\n';
56 report_fatal_error("MachineFunctionProperties check failed");
57 }
58 }
59#endif // NDEBUG
60 if constexpr (has_get_cleared_properties_v<PassT>)
61 MFProps.reset(P.getClearedProperties());
62 }
63
65 if constexpr (has_get_set_properties_v<PassT>) {
66 auto &MFProps = MF.getProperties();
67 MFProps.set(P.getSetProperties());
68 }
69 }
70
71private:
72 const PassT &P;
74
75 template <typename T>
76 using has_get_required_properties_t =
77 decltype(std::declval<T &>().getRequiredProperties());
78
79 template <typename T>
80 using has_get_set_properties_t =
81 decltype(std::declval<T &>().getSetProperties());
82
83 template <typename T>
84 using has_get_cleared_properties_t =
85 decltype(std::declval<T &>().getClearedProperties());
86
87 template <typename T>
88 static constexpr bool has_get_required_properties_v =
90
91 template <typename T>
92 static constexpr bool has_get_set_properties_v =
94
95 template <typename T>
96 static constexpr bool has_get_cleared_properties_v =
98};
99
100// Additional deduction guide to suppress warning.
101template <typename PassT>
103
106
107template <>
108LLVM_ABI bool MachineFunctionAnalysisManagerModuleProxy::Result::invalidate(
109 Module &M, const PreservedAnalyses &PA,
112 Module>;
115
116template <>
117LLVM_ABI bool MachineFunctionAnalysisManagerFunctionProxy::Result::invalidate(
118 Function &F, const PreservedAnalyses &PA,
121 Function>;
122
123extern template class LLVM_TEMPLATE_ABI
125/// Provide the \c ModuleAnalysisManager to \c Function proxy.
128
131public:
132 class Result {
133 public:
135
136 Result(Result &&Arg) : FAM(std::move(Arg.FAM)) {
137 // We have to null out the analysis manager in the moved-from state
138 // because we are taking ownership of the responsibilty to clear the
139 // analysis state.
140 Arg.FAM = nullptr;
141 }
142
144 FAM = RHS.FAM;
145 // We have to null out the analysis manager in the moved-from state
146 // because we are taking ownership of the responsibilty to clear the
147 // analysis state.
148 RHS.FAM = nullptr;
149 return *this;
150 }
151
152 /// Accessor for the analysis manager.
154
155 /// Handler for invalidation of the outer IR unit, \c IRUnitT.
156 ///
157 /// If the proxy analysis itself is not preserved, we assume that the set of
158 /// inner IR objects contained in IRUnit may have changed. In this case,
159 /// we have to call \c clear() on the inner analysis manager, as it may now
160 /// have stale pointers to its inner IR objects.
161 ///
162 /// Regardless of whether the proxy analysis is marked as preserved, all of
163 /// the analyses in the inner analysis manager are potentially invalidated
164 /// based on the set of preserved analyses.
165 LLVM_ABI bool invalidate(MachineFunction &IR, const PreservedAnalyses &PA,
167
168 private:
170 };
171
174 : FAM(&FAM) {}
175
176 /// Run the analysis pass and create our proxy result object.
177 ///
178 /// This doesn't do any interesting work; it is primarily used to insert our
179 /// proxy result object into the outer analysis cache so that we can proxy
180 /// invalidation to the inner analysis manager.
182 return Result(*FAM);
183 }
184
186
187private:
189};
190
193public:
196
198 std::unique_ptr<PassConceptT> Pass)
199 : Pass(std::move(Pass)) {}
200
201 /// Runs the function pass across every function in the function.
203 LLVM_ABI void
204 printPipeline(raw_ostream &OS,
205 function_ref<StringRef(StringRef)> MapClassName2PassName);
206
207 static bool isRequired() { return true; }
208
209private:
210 std::unique_ptr<PassConceptT> Pass;
211};
212
213template <typename MachineFunctionPassT>
214FunctionToMachineFunctionPassAdaptor
216 using PassModelT = detail::PassModel<MachineFunction, MachineFunctionPassT,
218 // Do not use make_unique, it causes too many template instantiations,
219 // causing terrible compile times.
221 std::unique_ptr<FunctionToMachineFunctionPassAdaptor::PassConceptT>(
222 new PassModelT(std::forward<MachineFunctionPassT>(Pass))));
223}
224
225template <>
228extern template class PassManager<MachineFunction>;
229
230/// Convenience typedef for a pass manager over functions.
232
233/// Returns the minimum set of Analyses that all machine function passes must
234/// preserve.
236
237} // end namespace llvm
238
239#endif // LLVM_CODEGEN_MACHINEPASSMANAGER_H
#define LLVM_ABI
Definition: Compiler.h:213
#define LLVM_TEMPLATE_ABI
Definition: Compiler.h:214
This file provides a collection of function (or more generally, callable) type erasure utilities supp...
This header defines various interfaces for pass management in LLVM.
Legalize the Machine IR a function s Machine IR
Definition: Legalizer.cpp:80
#define F(x, y, z)
Definition: MD5.cpp:55
Machine Check Debug Module
#define P_
#define P(N)
FunctionAnalysisManager FAM
This header provides internal APIs and implementation details used by the pass management interfaces ...
raw_pwrite_stream & OS
This file defines the SmallVector class.
Value * RHS
API to communicate dependencies between analyses during invalidation.
Definition: PassManager.h:294
A container for analyses that lazily runs them and caches their results.
Definition: PassManager.h:255
FunctionAnalysisManager & getManager()
Accessor for the analysis manager.
FunctionAnalysisManagerMachineFunctionProxy(FunctionAnalysisManager &FAM)
Result run(MachineFunction &, MachineFunctionAnalysisManager &)
Run the analysis pass and create our proxy result object.
FunctionToMachineFunctionPassAdaptor(std::unique_ptr< PassConceptT > Pass)
An analysis over an "outer" IR unit that provides access to an analysis manager over an "inner" IR un...
Definition: PassManager.h:585
An RAII based helper class to modify MachineFunctionProperties when running pass.
MFPropsModifier(const PassT &P_, MachineFunction &MF_)
MachineFunctionProperties & set(Property P)
StringRef getName() const
getName - Return the name of the corresponding LLVM function.
const MachineFunctionProperties & getProperties() const
Get the function properties.
A Module instance is used to store all the information related to an LLVM module.
Definition: Module.h:67
An analysis over an "inner" IR unit that provides access to an analysis manager over a "outer" IR uni...
Definition: PassManager.h:716
Manages a sequence of passes over a particular unit of IR.
Definition: PassManager.h:163
PreservedAnalyses run(IRUnitT &IR, AnalysisManagerT &AM, ExtraArgTs... ExtraArgs)
Run all of the passes in this manager over the given unit of IR.
Pass interface - Implemented by all 'passes'.
Definition: Pass.h:99
A set of analyses that are preserved following a run of a transformation pass.
Definition: Analysis.h:112
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:55
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
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
AnalysisManager< MachineFunction > MachineFunctionAnalysisManager
typename detail::detector< void, Op, Args... >::value_t is_detected
Detects if a given trait holds for some set of arguments 'Args'.
Definition: STLExtras.h:79
LLVM_ABI PreservedAnalyses getMachineFunctionPassPreservedAnalyses()
Returns the minimum set of Analyses that all machine function passes must preserve.
FunctionToMachineFunctionPassAdaptor createFunctionToMachineFunctionPassAdaptor(MachineFunctionPassT &&Pass)
LLVM_ABI void report_fatal_error(Error Err, bool gen_crash_diag=true)
Definition: Error.cpp:167
LLVM_ABI raw_fd_ostream & errs()
This returns a reference to a raw_ostream for standard error.
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:1886
Implement std::hash so that hash_code can be used in STL containers.
Definition: BitVector.h:856
A CRTP mix-in that provides informational APIs needed for analysis passes.
Definition: PassManager.h:93
A special type used by analysis passes to provide an address that identifies that particular analysis...
Definition: Analysis.h:29
A CRTP mix-in to automatically provide informational APIs needed for passes.
Definition: PassManager.h:70
Template for the abstract base class used to dispatch polymorphically over pass objects.
A template wrapper used to implement the polymorphic API.