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 = PassT::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(PassT::getSetProperties());
83 IR.getProperties().reset(PassT::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>;
111
114/// Provide the \c ModuleAnalysisManager to \c Function proxy.
117
119 : public AnalysisInfoMixin<FunctionAnalysisManagerMachineFunctionProxy> {
120public:
121 class Result {
122 public:
123 explicit Result(FunctionAnalysisManager &FAM) : FAM(&FAM) {}
124
125 Result(Result &&Arg) : FAM(std::move(Arg.FAM)) {
126 // We have to null out the analysis manager in the moved-from state
127 // because we are taking ownership of the responsibilty to clear the
128 // analysis state.
129 Arg.FAM = nullptr;
130 }
131
133 // FAM is cleared in a moved from state where there is nothing to do.
134 if (!FAM)
135 return;
136
137 // Clear out the analysis manager if we're being destroyed -- it means we
138 // didn't even see an invalidate call when we got invalidated.
139 FAM->clear();
140 }
141
143 FAM = RHS.FAM;
144 // We have to null out the analysis manager in the moved-from state
145 // because we are taking ownership of the responsibilty to clear the
146 // analysis state.
147 RHS.FAM = nullptr;
148 return *this;
149 }
150
151 /// Accessor for the analysis manager.
153
154 /// Handler for invalidation of the outer IR unit, \c IRUnitT.
155 ///
156 /// If the proxy analysis itself is not preserved, we assume that the set of
157 /// inner IR objects contained in IRUnit may have changed. In this case,
158 /// we have to call \c clear() on the inner analysis manager, as it may now
159 /// have stale pointers to its inner IR objects.
160 ///
161 /// Regardless of whether the proxy analysis is marked as preserved, all of
162 /// the analyses in the inner analysis manager are potentially invalidated
163 /// based on the set of preserved analyses.
166
167 private:
169 };
170
173 : FAM(&FAM) {}
174
175 /// Run the analysis pass and create our proxy result object.
176 ///
177 /// This doesn't do any interesting work; it is primarily used to insert our
178 /// proxy result object into the outer analysis cache so that we can proxy
179 /// invalidation to the inner analysis manager.
181 return Result(*FAM);
182 }
183
185
186private:
188};
189
191 : public PassInfoMixin<ModuleToMachineFunctionPassAdaptor> {
192public:
195
197 std::unique_ptr<PassConceptT> Pass)
198 : Pass(std::move(Pass)) {}
199
200 /// Runs the function pass across every function in the module.
203 function_ref<StringRef(StringRef)> MapClassName2PassName);
204
205 static bool isRequired() { return true; }
206
207private:
208 std::unique_ptr<PassConceptT> Pass;
209};
210
211template <typename MachineFunctionPassT>
212ModuleToMachineFunctionPassAdaptor
214 using PassModelT = detail::PassModel<MachineFunction, MachineFunctionPassT,
216 // Do not use make_unique, it causes too many template instantiations,
217 // causing terrible compile times.
219 std::unique_ptr<ModuleToMachineFunctionPassAdaptor::PassConceptT>(
220 new PassModelT(std::forward<MachineFunctionPassT>(Pass))));
221}
222
223template <>
224template <typename PassT>
226 using MachinePassModelT = detail::MachinePassModel<PassT>;
227 // Do not use make_unique or emplace_back, they cause too many template
228 // instantiations, causing terrible compile times.
229 if constexpr (std::is_same_v<PassT, PassManager<MachineFunction>>) {
230 for (auto &P : Pass.Passes)
231 Passes.push_back(std::move(P));
232 } else {
233 Passes.push_back(std::unique_ptr<MachinePassModelT>(
234 new MachinePassModelT(std::forward<PassT>(Pass))));
235 }
236}
237
238template <>
242extern template class PassManager<MachineFunction>;
243
244/// Convenience typedef for a pass manager over functions.
246
247} // end namespace llvm
248
249#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
#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
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:631
ModuleToMachineFunctionPassAdaptor(std::unique_ptr< PassConceptT > 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: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
AnalysisManager< Module > ModuleAnalysisManager
Convenience typedef for the Module analysis manager.
Definition: PassManager.h:605
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
ModuleToMachineFunctionPassAdaptor createModuleToMachineFunctionPassAdaptor(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:156
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
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.