LLVM 20.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
27#include "llvm/IR/PassManager.h"
29#include "llvm/Support/Error.h"
30
31namespace llvm {
32class Module;
33class Function;
34class MachineFunction;
35
36extern template class AnalysisManager<MachineFunction>;
37using MachineFunctionAnalysisManager = AnalysisManager<MachineFunction>;
38
39/// An RAII based helper class to modify MachineFunctionProperties when running
40/// pass. Define a MFPropsModifier in PassT::run to set
41/// MachineFunctionProperties properly.
42template <typename PassT> class MFPropsModifier {
43public:
44 MFPropsModifier(PassT &P_, MachineFunction &MF_) : P(P_), MF(MF_) {
45 auto &MFProps = MF.getProperties();
46#ifndef NDEBUG
47 if constexpr (has_get_required_properties_v<PassT>) {
48 auto &MFProps = MF.getProperties();
49 auto RequiredProperties = P.getRequiredProperties();
50 if (!MFProps.verifyRequiredProperties(RequiredProperties)) {
51 errs() << "MachineFunctionProperties required by " << PassT::name()
52 << " pass are not met by function " << MF.getName() << ".\n"
53 << "Required properties: ";
54 RequiredProperties.print(errs());
55 errs() << "\nCurrent properties: ";
56 MFProps.print(errs());
57 errs() << '\n';
58 report_fatal_error("MachineFunctionProperties check failed");
59 }
60 }
61#endif // NDEBUG
62 if constexpr (has_get_cleared_properties_v<PassT>)
63 MFProps.reset(P.getClearedProperties());
64 }
65
67 if constexpr (has_get_set_properties_v<PassT>) {
68 auto &MFProps = MF.getProperties();
69 MFProps.set(P.getSetProperties());
70 }
71 }
72
73private:
74 PassT &P;
76
77 template <typename T>
78 using has_get_required_properties_t =
79 decltype(std::declval<T &>().getRequiredProperties());
80
81 template <typename T>
82 using has_get_set_properties_t =
83 decltype(std::declval<T &>().getSetProperties());
84
85 template <typename T>
86 using has_get_cleared_properties_t =
87 decltype(std::declval<T &>().getClearedProperties());
88
89 template <typename T>
90 static constexpr bool has_get_required_properties_v =
92
93 template <typename T>
94 static constexpr bool has_get_set_properties_v =
96
97 template <typename T>
98 static constexpr bool has_get_cleared_properties_v =
100};
101
102// Additional deduction guide to suppress warning.
103template <typename PassT>
105
108
109template <>
110bool MachineFunctionAnalysisManagerModuleProxy::Result::invalidate(
111 Module &M, const PreservedAnalyses &PA,
114 Module>;
117
118template <>
119bool MachineFunctionAnalysisManagerFunctionProxy::Result::invalidate(
120 Function &F, const PreservedAnalyses &PA,
123 Function>;
124
127/// Provide the \c ModuleAnalysisManager to \c Function proxy.
130
132 : public AnalysisInfoMixin<FunctionAnalysisManagerMachineFunctionProxy> {
133public:
134 class Result {
135 public:
136 explicit Result(FunctionAnalysisManager &FAM) : FAM(&FAM) {}
137
138 Result(Result &&Arg) : FAM(std::move(Arg.FAM)) {
139 // We have to null out the analysis manager in the moved-from state
140 // because we are taking ownership of the responsibilty to clear the
141 // analysis state.
142 Arg.FAM = nullptr;
143 }
144
146 FAM = RHS.FAM;
147 // We have to null out the analysis manager in the moved-from state
148 // because we are taking ownership of the responsibilty to clear the
149 // analysis state.
150 RHS.FAM = nullptr;
151 return *this;
152 }
153
154 /// Accessor for the analysis manager.
156
157 /// Handler for invalidation of the outer IR unit, \c IRUnitT.
158 ///
159 /// If the proxy analysis itself is not preserved, we assume that the set of
160 /// inner IR objects contained in IRUnit may have changed. In this case,
161 /// we have to call \c clear() on the inner analysis manager, as it may now
162 /// have stale pointers to its inner IR objects.
163 ///
164 /// Regardless of whether the proxy analysis is marked as preserved, all of
165 /// the analyses in the inner analysis manager are potentially invalidated
166 /// based on the set of preserved analyses.
169
170 private:
172 };
173
176 : FAM(&FAM) {}
177
178 /// Run the analysis pass and create our proxy result object.
179 ///
180 /// This doesn't do any interesting work; it is primarily used to insert our
181 /// proxy result object into the outer analysis cache so that we can proxy
182 /// invalidation to the inner analysis manager.
184 return Result(*FAM);
185 }
186
188
189private:
191};
192
194 : public PassInfoMixin<FunctionToMachineFunctionPassAdaptor> {
195public:
198
200 std::unique_ptr<PassConceptT> Pass)
201 : Pass(std::move(Pass)) {}
202
203 /// Runs the function pass across every function in the function.
206 function_ref<StringRef(StringRef)> MapClassName2PassName);
207
208 static bool isRequired() { return true; }
209
210private:
211 std::unique_ptr<PassConceptT> Pass;
212};
213
214template <typename MachineFunctionPassT>
215FunctionToMachineFunctionPassAdaptor
217 using PassModelT = detail::PassModel<MachineFunction, MachineFunctionPassT,
219 // Do not use make_unique, it causes too many template instantiations,
220 // causing terrible compile times.
222 std::unique_ptr<FunctionToMachineFunctionPassAdaptor::PassConceptT>(
223 new PassModelT(std::forward<MachineFunctionPassT>(Pass))));
224}
225
226template <>
227PreservedAnalyses
230extern template class PassManager<MachineFunction>;
231
232/// Convenience typedef for a pass manager over functions.
234
235/// Returns the minimum set of Analyses that all machine function passes must
236/// preserve.
238
239} // end namespace llvm
240
241#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
#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 ...
This header defines various interfaces for pass management in LLVM.
raw_pwrite_stream & OS
This file defines the SmallVector class.
Value * RHS
API to communicate dependencies between analyses during invalidation.
Definition: PassManager.h:292
A container for analyses that lazily runs them and caches their results.
Definition: PassManager.h:253
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.
PreservedAnalyses run(Function &F, FunctionAnalysisManager &FAM)
Runs the function pass across every function in the function.
void printPipeline(raw_ostream &OS, function_ref< StringRef(StringRef)> MapClassName2PassName)
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:563
An RAII based helper class to modify MachineFunctionProperties when running pass.
MFPropsModifier(PassT &P_, MachineFunction &MF_)
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:688
Manages a sequence of passes over a particular unit of IR.
Definition: PassManager.h:162
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:94
A set of analyses that are preserved following a run of a transformation pass.
Definition: Analysis.h:111
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
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
PreservedAnalyses getMachineFunctionPassPreservedAnalyses()
Returns the minimum set of Analyses that all machine function passes must preserve.
FunctionToMachineFunctionPassAdaptor createFunctionToMachineFunctionPassAdaptor(MachineFunctionPassT &&Pass)
void report_fatal_error(Error Err, bool gen_crash_diag=true)
Report a serious error, calling any installed error handler.
Definition: Error.cpp:167
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:1856
AnalysisManager< Module > ModuleAnalysisManager
Convenience typedef for the Module analysis manager.
Definition: MIRParser.h:38
Implement std::hash so that hash_code can be used in STL containers.
Definition: BitVector.h:858
A CRTP mix-in that provides informational APIs needed for analysis passes.
Definition: PassManager.h:92
A special type used by analysis passes to provide an address that identifies that particular analysis...
Definition: Analysis.h:28
A CRTP mix-in to automatically provide informational APIs needed for passes.
Definition: PassManager.h:69
Template for the abstract base class used to dispatch polymorphically over pass objects.
A template wrapper used to implement the polymorphic API.