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//===----------------------------------------------------------------------===//
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>;
38
39namespace detail {
40
41template <typename PassT>
46 std::move(Pass)) {}
47
49 using std::swap;
50 swap(LHS.Pass, RHS.Pass);
51 }
52
54 swap(*this, RHS);
55 return *this;
56 }
57
60 MachineFunctionAnalysisManager &AM) override {
61#ifndef NDEBUG
63 auto &MFProps = IR.getProperties();
64 auto RequiredProperties = this->Pass.getRequiredProperties();
65 if (!MFProps.verifyRequiredProperties(RequiredProperties)) {
66 errs() << "MachineFunctionProperties required by " << PassT::name()
67 << " pass are not met by function " << IR.getName() << ".\n"
68 << "Required properties: ";
69 RequiredProperties.print(errs());
70 errs() << "\nCurrent properties: ";
71 MFProps.print(errs());
72 errs() << '\n';
73 report_fatal_error("MachineFunctionProperties check failed");
74 }
75 }
76#endif
77
78 auto PA = this->Pass.run(IR, AM);
79
81 IR.getProperties().set(this->Pass.getSetProperties());
83 IR.getProperties().reset(this->Pass.getClearedProperties());
84 return PA;
85 }
86
87private:
88 template <typename T>
89 using has_get_required_properties_t =
90 decltype(std::declval<T &>().getRequiredProperties());
91
92 template <typename T>
93 using has_get_set_properties_t =
94 decltype(std::declval<T &>().getSetProperties());
95
96 template <typename T>
97 using has_get_cleared_properties_t =
98 decltype(std::declval<T &>().getClearedProperties());
99};
100} // namespace detail
101
104
105template <>
106bool MachineFunctionAnalysisManagerModuleProxy::Result::invalidate(
107 Module &M, const PreservedAnalyses &PA,
110 Module>;
113
114template <>
115bool MachineFunctionAnalysisManagerFunctionProxy::Result::invalidate(
116 Function &F, const PreservedAnalyses &PA,
119 Function>;
120
123/// Provide the \c ModuleAnalysisManager to \c Function proxy.
126
128 : public AnalysisInfoMixin<FunctionAnalysisManagerMachineFunctionProxy> {
129public:
130 class Result {
131 public:
132 explicit Result(FunctionAnalysisManager &FAM) : FAM(&FAM) {}
133
134 Result(Result &&Arg) : FAM(std::move(Arg.FAM)) {
135 // We have to null out the analysis manager in the moved-from state
136 // because we are taking ownership of the responsibilty to clear the
137 // analysis state.
138 Arg.FAM = nullptr;
139 }
140
142 FAM = RHS.FAM;
143 // We have to null out the analysis manager in the moved-from state
144 // because we are taking ownership of the responsibilty to clear the
145 // analysis state.
146 RHS.FAM = nullptr;
147 return *this;
148 }
149
150 /// Accessor for the analysis manager.
152
153 /// Handler for invalidation of the outer IR unit, \c IRUnitT.
154 ///
155 /// If the proxy analysis itself is not preserved, we assume that the set of
156 /// inner IR objects contained in IRUnit may have changed. In this case,
157 /// we have to call \c clear() on the inner analysis manager, as it may now
158 /// have stale pointers to its inner IR objects.
159 ///
160 /// Regardless of whether the proxy analysis is marked as preserved, all of
161 /// the analyses in the inner analysis manager are potentially invalidated
162 /// based on the set of preserved analyses.
165
166 private:
168 };
169
172 : FAM(&FAM) {}
173
174 /// Run the analysis pass and create our proxy result object.
175 ///
176 /// This doesn't do any interesting work; it is primarily used to insert our
177 /// proxy result object into the outer analysis cache so that we can proxy
178 /// invalidation to the inner analysis manager.
180 return Result(*FAM);
181 }
182
184
185private:
187};
188
190 : public PassInfoMixin<FunctionToMachineFunctionPassAdaptor> {
191public:
194
196 std::unique_ptr<PassConceptT> Pass)
197 : Pass(std::move(Pass)) {}
198
199 /// Runs the function pass across every function in the function.
202 function_ref<StringRef(StringRef)> MapClassName2PassName);
203
204 static bool isRequired() { return true; }
205
206private:
207 std::unique_ptr<PassConceptT> Pass;
208};
209
210template <typename MachineFunctionPassT>
211FunctionToMachineFunctionPassAdaptor
213 using PassModelT = detail::PassModel<MachineFunction, MachineFunctionPassT,
215 // Do not use make_unique, it causes too many template instantiations,
216 // causing terrible compile times.
218 std::unique_ptr<FunctionToMachineFunctionPassAdaptor::PassConceptT>(
219 new PassModelT(std::forward<MachineFunctionPassT>(Pass))));
220}
221
222template <>
223template <typename PassT>
225 using MachinePassModelT = detail::MachinePassModel<PassT>;
226 // Do not use make_unique or emplace_back, they cause too many template
227 // instantiations, causing terrible compile times.
228 if constexpr (std::is_same_v<PassT, PassManager<MachineFunction>>) {
229 for (auto &P : Pass.Passes)
230 Passes.push_back(std::move(P));
231 } else {
232 Passes.push_back(std::unique_ptr<MachinePassModelT>(
233 new MachinePassModelT(std::forward<PassT>(Pass))));
234 }
235}
236
237template <>
241extern template class PassManager<MachineFunction>;
242
243/// Convenience typedef for a pass manager over functions.
245
246/// Returns the minimum set of Analyses that all machine function passes must
247/// preserve.
249
250} // end namespace llvm
251
252#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(N)
FunctionAnalysisManager FAM
const char * Passes
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:360
A container for analyses that lazily runs them and caches their results.
Definition: PassManager.h:321
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:631
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:756
Manages a sequence of passes over a particular unit of IR.
Definition: PassManager.h:173
LLVM_ATTRIBUTE_MINSIZE void addPass(PassT &&Pass)
Definition: PassManager.h:249
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:201
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
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:1849
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
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:97
A special type used by analysis passes to provide an address that identifies that particular analysis...
Definition: Analysis.h:26
A CRTP mix-in to automatically provide informational APIs needed for passes.
Definition: PassManager.h:74
MachinePassModel & operator=(const MachinePassModel &)=delete
PreservedAnalyses run(MachineFunction &IR, MachineFunctionAnalysisManager &AM) override
MachinePassModel & operator=(MachinePassModel RHS)
friend void swap(MachinePassModel &LHS, MachinePassModel &RHS)
Template for the abstract base class used to dispatch polymorphically over pass objects.
A template wrapper used to implement the polymorphic API.