LLVM 24.0.0git
CodeGenPassBuilder.h
Go to the documentation of this file.
1//===- Construction of codegen pass pipelines ------------------*- 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/// \file
9///
10/// Interfaces for producing common pass manager configurations.
11///
12//===----------------------------------------------------------------------===//
13
14#ifndef LLVM_PASSES_CODEGENPASSBUILDER_H
15#define LLVM_PASSES_CODEGENPASSBUILDER_H
16
20#include "llvm/ADT/StringRef.h"
23#include "llvm/IR/PassManager.h"
26#include "llvm/Support/Error.h"
29#include <cassert>
30#include <utility>
31
32namespace llvm {
33
34// FIXME: Dummy target independent passes definitions that have not yet been
35// ported to new pass manager. Once they do, remove these.
36#define DUMMY_FUNCTION_PASS(NAME, PASS_NAME) \
37 struct PASS_NAME : public OptionalPassInfoMixin<PASS_NAME> { \
38 template <typename... Ts> PASS_NAME(Ts &&...) {} \
39 PreservedAnalyses run(Function &, FunctionAnalysisManager &) { \
40 return PreservedAnalyses::all(); \
41 } \
42 };
43#define DUMMY_MACHINE_MODULE_PASS(NAME, PASS_NAME) \
44 struct PASS_NAME : public OptionalPassInfoMixin<PASS_NAME> { \
45 template <typename... Ts> PASS_NAME(Ts &&...) {} \
46 PreservedAnalyses run(Module &, ModuleAnalysisManager &) { \
47 return PreservedAnalyses::all(); \
48 } \
49 };
50#define DUMMY_MACHINE_FUNCTION_PASS(NAME, PASS_NAME) \
51 struct PASS_NAME : public OptionalPassInfoMixin<PASS_NAME> { \
52 template <typename... Ts> PASS_NAME(Ts &&...) {} \
53 PreservedAnalyses run(MachineFunction &, \
54 MachineFunctionAnalysisManager &) { \
55 return PreservedAnalyses::all(); \
56 } \
57 };
58#include "llvm/Passes/MachinePassRegistry.def"
59
60class PassManagerWrapper {
61private:
62 PassManagerWrapper(ModulePassManager &ModulePM) : MPM(ModulePM) {};
63
67
68 friend class CodeGenPassBuilder;
69};
70
71/// This class provides access to building LLVM's passes.
72///
73/// Its members provide the baseline state available to passes during their
74/// construction. The \c MachinePassRegistry.def file specifies how to construct
75/// all of the built-in passes, and those may reference these members during
76/// construction.
77///
78/// Targets customize the pipeline by deriving from this class and overriding
79/// the virtual add* hooks below: the add%Stage hooks replace a whole stage of
80/// the pipeline, while the addPre%Stage / addPost%Stage hooks inject passes
81/// around one. See addMachinePasses for how they fit together.
82///
83/// Dispatch is virtual rather than templated on the derived builder so that the
84/// target-independent pipeline is emitted once for the whole build instead of
85/// once per target.
87public:
93
96 CodeGenFileType FileType, MCContext &Ctx);
97
101
102protected:
103 template <typename PassT>
104 using is_module_pass_t = decltype(std::declval<PassT &>().run(
105 std::declval<Module &>(), std::declval<ModuleAnalysisManager &>()));
106
107 template <typename PassT>
108 using is_function_pass_t = decltype(std::declval<PassT &>().run(
109 std::declval<Function &>(), std::declval<FunctionAnalysisManager &>()));
110
111 template <typename PassT>
112 using is_machine_function_pass_t = decltype(std::declval<PassT &>().run(
113 std::declval<MachineFunction &>(),
114 std::declval<MachineFunctionAnalysisManager &>()));
115
116 template <typename PassT>
118 bool Force = false, StringRef Name = PassT::name()) {
120 "Only function passes are supported.");
121 if (!Force && !runBeforeAdding(Name))
122 return;
123 PMW.FPM.addPass(std::forward<PassT>(Pass));
124 }
125
126 template <typename PassT>
127 void addModulePass(PassT &&Pass, PassManagerWrapper &PMW, bool Force = false,
128 StringRef Name = PassT::name()) {
130 "Only module passes are suported.");
131 assert(PMW.FPM.isEmpty() && PMW.MFPM.isEmpty() &&
132 "You cannot insert a module pass without first flushing the current "
133 "function pipelines to the module pipeline.");
134 if (!Force && !runBeforeAdding(Name))
135 return;
136 PMW.MPM.addPass(std::forward<PassT>(Pass));
137 }
138
139 template <typename PassT>
141 bool Force = false,
142 StringRef Name = PassT::name()) {
144 "Only machine function passes are supported.");
145
146 if (!Force && !runBeforeAdding(Name))
147 return;
148 PMW.MFPM.addPass(std::forward<PassT>(Pass));
149 for (auto &C : AfterCallbacks)
150 C(Name, PMW.MFPM);
151 }
152
153 void flushFPMsToMPM(PassManagerWrapper &PMW,
154 bool FreeMachineFunctions = false);
155
157 assert(!AddInCGSCCOrder);
158 assert(PMW.FPM.isEmpty() && PMW.MFPM.isEmpty() &&
159 "Requiring CGSCC ordering requires flushing the current function "
160 "pipelines to the MPM.");
161 AddInCGSCCOrder = true;
162 }
163
165 assert(AddInCGSCCOrder);
166 assert(PMW.FPM.isEmpty() && PMW.MFPM.isEmpty() &&
167 "Stopping CGSCC ordering requires flushing the current function "
168 "pipelines to the MPM.");
169 AddInCGSCCOrder = false;
170 }
171
175 /// Whether no -stop-before/-stop-after option ends the pipeline early.
176 bool CompletesPipeline = true;
177
178 CodeGenOptLevel getOptLevel() const { return TM.getOptLevel(); }
179
180 /// Check whether or not GlobalISel should abort on error.
181 /// When this is disabled, GlobalISel will fall back on SDISel instead of
182 /// erroring out.
184 return TM.Options.GlobalISelAbort == GlobalISelAbortMode::Enable;
185 }
186
187 /// Check whether or not a diagnostic should be emitted when GlobalISel
188 /// uses the fallback path. In other words, it will emit a diagnostic
189 /// when GlobalISel failed and isGlobalISelAbortEnabled is false.
191 return TM.Options.GlobalISelAbort == GlobalISelAbortMode::DisableWithDiag;
192 }
193
194 /// addInstSelector - This method should install an instruction selector pass,
195 /// which converts from LLVM code to machine instructions.
196 virtual Error addInstSelector(PassManagerWrapper &PMW);
197
198 /// Target can override this to add GlobalMergePass before all IR passes.
200
201 /// Add passes that optimize instruction level parallelism for out-of-order
202 /// targets. These passes are run while the machine code is still in SSA
203 /// form, so they can use MachineTraceMetrics to control their heuristics.
204 ///
205 /// All passes added here should preserve the MachineDominatorTree,
206 /// MachineLoopInfo, and MachineTraceMetrics analyses.
207 virtual void addILPOpts(PassManagerWrapper &PMW) {}
208
209 /// This method may be implemented by targets that want to run passes
210 /// immediately before register allocation.
211 virtual void addPreRegAlloc(PassManagerWrapper &PMW) {}
212
213 /// addPreRewrite - Add passes to the optimized register allocation pipeline
214 /// after register allocation is complete, but before virtual registers are
215 /// rewritten to physical registers.
216 ///
217 /// These passes must preserve VirtRegMap and LiveIntervals, and when running
218 /// after RABasic or RAGreedy, they should take advantage of LiveRegMatrix.
219 /// When these passes run, VirtRegMap contains legal physreg assignments for
220 /// all virtual registers.
221 ///
222 /// Note if the target overloads addRegAssignAndRewriteOptimized, this may not
223 /// be honored. This is also not generally used for the fast variant,
224 /// where the allocation and rewriting are done in one pass.
225 virtual void addPreRewrite(PassManagerWrapper &PMW) {}
226
227 /// Add passes to be run immediately after virtual registers are rewritten
228 /// to physical registers.
229 virtual void addPostRewrite(PassManagerWrapper &PMW) {}
230
231 /// This method may be implemented by targets that want to run passes after
232 /// register allocation pass pipeline but before prolog-epilog insertion.
234
235 /// This method may be implemented by targets that want to run passes after
236 /// prolog-epilog insertion and before the second instruction scheduling pass.
237 virtual void addPreSched2(PassManagerWrapper &PMW) {}
238
239 /// This pass may be implemented by targets that want to run passes
240 /// immediately before machine code is emitted.
241 virtual void addPreEmitPass(PassManagerWrapper &PMW) {}
242
243 /// Targets may add passes immediately before machine code is emitted in this
244 /// callback. This is called even later than `addPreEmitPass`.
245 // FIXME: Rename `addPreEmitPass` to something more sensible given its actual
246 // position and remove the `2` suffix here as this callback is what
247 // `addPreEmitPass` *should* be but in reality isn't.
249
250 /// {{@ For GlobalISel
251 ///
252
253 /// addPreISel - This method should add any "last minute" LLVM->LLVM
254 /// passes (which are run just before instruction selector).
255 virtual void addPreISel(PassManagerWrapper &PMW) {}
256
257 /// This method should install an IR translator pass, which converts from
258 /// LLVM code to machine instructions with possibly generic opcodes.
259 virtual Error addIRTranslator(PassManagerWrapper &PMW);
260
261 /// This method may be implemented by targets that want to run passes
262 /// immediately before legalization.
264
265 /// This method should install a legalize pass, which converts the instruction
266 /// sequence into one that can be selected by the target.
267 virtual Error addLegalizeMachineIR(PassManagerWrapper &PMW);
268
269 /// This method may be implemented by targets that want to run passes
270 /// immediately before the register bank selection.
272
273 /// This method should install a register bank selector pass, which
274 /// assigns register banks to virtual registers without a register
275 /// class or register banks.
276 virtual Error addRegBankSelect(PassManagerWrapper &PMW);
277
278 /// This method may be implemented by targets that want to run passes
279 /// immediately before the (global) instruction selection.
281
282 /// This method should install a (global) instruction selector pass, which
283 /// converts possibly generic instructions to fully target-specific
284 /// instructions, thereby constraining all generic virtual registers to
285 /// register classes.
286 virtual Error addGlobalInstructionSelect(PassManagerWrapper &PMW);
287 /// @}}
288
289 /// High level function that adds all passes necessary to go from llvm IR
290 /// representation to the MI representation.
291 /// Adds IR based lowering and target specific optimization passes and finally
292 /// the core instruction selection passes.
293 void addISelPasses(PassManagerWrapper &PMW);
294
295 /// Add the actual instruction selection passes. This does not include
296 /// preparation passes on IR.
297 Error addCoreISelPasses(PassManagerWrapper &PMW);
298
299 /// Add the complete, standard set of LLVM CodeGen passes.
300 /// Fully developed targets will not generally override this.
301 virtual Error addMachinePasses(PassManagerWrapper &PMW);
302
303 /// Add passes to lower exception handling for the code generator.
304 virtual void addPassesToHandleExceptions(PassManagerWrapper &PMW);
305
306 /// Add common target configurable passes that perform LLVM IR to IR
307 /// transforms following machine independent optimization.
308 virtual void addIRPasses(PassManagerWrapper &PMW);
309
310 /// Add pass to prepare the LLVM IR for code generation. This should be done
311 /// before exception handling preparation passes.
312 virtual void addCodeGenPrepare(PassManagerWrapper &PMW);
313
314 /// Add common passes that perform LLVM IR to IR transforms in preparation for
315 /// instruction selection.
316 virtual void addISelPrepare(PassManagerWrapper &PMW);
317
318 /// Methods with trivial inline returns are convenient points in the common
319 /// codegen pass pipeline where targets may insert passes. Methods with
320 /// out-of-line standard implementations are major CodeGen stages called by
321 /// addMachinePasses. Some targets may override major stages when inserting
322 /// passes is insufficient, but maintaining overriden stages is more work.
323 ///
324
325 /// addMachineSSAOptimization - Add standard passes that optimize machine
326 /// instructions in SSA form.
327 virtual void addMachineSSAOptimization(PassManagerWrapper &PMW);
328
329 /// addFastRegAlloc - Add the minimum set of target-independent passes that
330 /// are required for fast register allocation.
331 virtual Error addFastRegAlloc(PassManagerWrapper &PMW);
332
333 /// addOptimizedRegAlloc - Add passes related to register allocation.
334 /// CodeGenTargetMachineImpl provides standard regalloc passes for most
335 /// targets.
336 virtual Error addOptimizedRegAlloc(PassManagerWrapper &PMW);
337
338 /// Add passes that optimize machine instructions after register allocation.
339 virtual void addMachineLateOptimization(PassManagerWrapper &PMW);
340
341 /// addGCPasses - Add late codegen passes that analyze code for garbage
342 /// collection. This should return true if GC info should be printed after
343 /// these passes.
344 virtual void addGCPasses(PassManagerWrapper &PMW) {}
345
346 /// Add standard basic block placement passes.
347 virtual void addBlockPlacement(PassManagerWrapper &PMW);
348
350
351 virtual void addAsmPrinterBegin(PassManagerWrapper &PMW);
352
353 virtual void addAsmPrinter(PassManagerWrapper &PMW);
354
355 virtual void addAsmPrinterEnd(PassManagerWrapper &PMW);
356
357 /// Utilities for targets to add passes to the pass manager.
358 ///
359
360 /// Create the register allocator pass for this target at the current
361 /// optimization level.
362 virtual void addTargetRegisterAllocator(PassManagerWrapper &PMW,
363 bool Optimized);
364
365 /// addMachinePasses helper to create the target-selected or overriden
366 /// regalloc pass.
367 void addRegAllocPass(PassManagerWrapper &PMW, bool Optimized);
368
369 /// Add core register allocator passes which do the actual register assignment
370 /// and rewriting. addRegAssignAndRewriteOptimized should return true if any
371 /// passes were added.
372 virtual Error addRegAssignAndRewriteFast(PassManagerWrapper &PMW);
373 virtual Expected<bool>
374 addRegAssignAndRewriteOptimized(PassManagerWrapper &PMW);
375
376 /// Allow the target to disable a specific pass by default.
377 /// Backend can declare unwanted passes in constructor.
378 template <typename... PassTs> void disablePass() {
379 BeforeCallbacks.emplace_back(
380 [](StringRef Name) { return ((Name != PassTs::name()) && ...); });
381 }
382
383 /// Insert InsertedPass pass after TargetPass pass.
384 /// Only machine function passes are supported.
385 template <typename TargetPassT, typename InsertedPassT>
386 void insertPass(InsertedPassT &&Pass) {
387 AfterCallbacks.emplace_back(
388 [&](StringRef Name, MachineFunctionPassManager &MFPM) mutable {
389 if (Name == TargetPassT::name() &&
390 runBeforeAdding(InsertedPassT::name())) {
391 MFPM.addPass(std::forward<InsertedPassT>(Pass));
392 }
393 });
394 }
395
396private:
397 bool runBeforeAdding(StringRef Name) {
398 bool ShouldAdd = true;
399 for (auto &C : BeforeCallbacks)
400 ShouldAdd &= C(Name);
401 return ShouldAdd;
402 }
403
404 void setStartStopPasses(const TargetPassConfig::StartStopInfo &Info);
405
406 Error verifyStartStop(const TargetPassConfig::StartStopInfo &Info) const;
407
408 SmallVector<llvm::unique_function<bool(StringRef)>, 4> BeforeCallbacks;
409 SmallVector<
410 llvm::unique_function<void(StringRef, MachineFunctionPassManager &)>, 4>
411 AfterCallbacks;
412
413 /// Helper variable for `-start-before/-start-after/-stop-before/-stop-after`
414 bool Started = true;
415 bool Stopped = true;
416 bool AddInCGSCCOrder = false;
417};
418
419} // namespace llvm
420
421#endif // LLVM_PASSES_CODEGENPASSBUILDER_H
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
static GCRegistry::Add< ShadowStackGC > C("shadow-stack", "Very portable GC for uncooperative code generators")
#define LLVM_ABI
Definition Compiler.h:211
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.
ModuleAnalysisManager MAM
This file contains library features backported from future STL versions.
This file defines the SmallVector class.
Target-Independent Code Generator Pass Configuration Options pass.
virtual void addPreEmitPass(PassManagerWrapper &PMW)
This pass may be implemented by targets that want to run passes immediately before machine code is em...
void disablePass()
Allow the target to disable a specific pass by default.
void addMachineFunctionPass(PassT &&Pass, PassManagerWrapper &PMW, bool Force=false, StringRef Name=PassT::name())
CodeGenPassBuilder(TargetMachine &TM, const CGPassBuilderOption &Opts, PassInstrumentationCallbacks *PIC)
virtual void addPreRewrite(PassManagerWrapper &PMW)
addPreRewrite - Add passes to the optimized register allocation pipeline after register allocation is...
void insertPass(InsertedPassT &&Pass)
Insert InsertedPass pass after TargetPass pass.
void addFunctionPass(PassT &&Pass, PassManagerWrapper &PMW, bool Force=false, StringRef Name=PassT::name())
CodeGenPassBuilder & operator=(const CodeGenPassBuilder &)=delete
Error buildPipeline(ModulePassManager &MPM, ModuleAnalysisManager &MAM, raw_pwrite_stream &Out, raw_pwrite_stream *DwoOut, CodeGenFileType FileType, MCContext &Ctx)
virtual void addPreRegAlloc(PassManagerWrapper &PMW)
This method may be implemented by targets that want to run passes immediately before register allocat...
decltype(std::declval< PassT & >().run( std::declval< Module & >(), std::declval< ModuleAnalysisManager & >())) is_module_pass_t
virtual void addPostRegAlloc(PassManagerWrapper &PMW)
This method may be implemented by targets that want to run passes after register allocation pass pipe...
bool isGlobalISelAbortEnabled() const
Check whether or not GlobalISel should abort on error.
virtual void addPreSched2(PassManagerWrapper &PMW)
This method may be implemented by targets that want to run passes after prolog-epilog insertion and b...
virtual void addPreEmitPass2(PassManagerWrapper &PMW)
Targets may add passes immediately before machine code is emitted in this callback.
virtual void addPreRegBankSelect(PassManagerWrapper &PMW)
This method may be implemented by targets that want to run passes immediately before the register ban...
virtual void addGlobalMergePass(PassManagerWrapper &PMW)
Target can override this to add GlobalMergePass before all IR passes.
decltype(std::declval< PassT & >().run( std::declval< Function & >(), std::declval< FunctionAnalysisManager & >())) is_function_pass_t
CodeGenOptLevel getOptLevel() const
virtual void addPreLegalizeMachineIR(PassManagerWrapper &PMW)
This method may be implemented by targets that want to run passes immediately before legalization.
virtual void addPreISel(PassManagerWrapper &PMW)
{{@ For GlobalISel
virtual void addPostBBSections(PassManagerWrapper &PMW)
void addModulePass(PassT &&Pass, PassManagerWrapper &PMW, bool Force=false, StringRef Name=PassT::name())
PassInstrumentationCallbacks * getPassInstrumentationCallbacks() const
void stopAddingInCGSCCOrder(PassManagerWrapper &PMW)
virtual void addPreGlobalInstructionSelect(PassManagerWrapper &PMW)
This method may be implemented by targets that want to run passes immediately before the (global) ins...
virtual void addILPOpts(PassManagerWrapper &PMW)
Add passes that optimize instruction level parallelism for out-of-order targets.
PassInstrumentationCallbacks * PIC
virtual void addPostRewrite(PassManagerWrapper &PMW)
Add passes to be run immediately after virtual registers are rewritten to physical registers.
bool reportDiagnosticWhenGlobalISelFallback() const
Check whether or not a diagnostic should be emitted when GlobalISel uses the fallback path.
decltype(std::declval< PassT & >().run( std::declval< MachineFunction & >(), std::declval< MachineFunctionAnalysisManager & >())) is_machine_function_pass_t
virtual void addGCPasses(PassManagerWrapper &PMW)
addGCPasses - Add late codegen passes that analyze code for garbage collection.
CodeGenPassBuilder(const CodeGenPassBuilder &)=delete
void requireCGSCCOrder(PassManagerWrapper &PMW)
bool CompletesPipeline
Whether no -stop-before/-stop-after option ends the pipeline early.
Lightweight error class with error context and mandatory checking.
Definition Error.h:159
Tagged union holding either a T or a Error.
Definition Error.h:485
Context object for machine code objects.
Definition MCContext.h:83
This class manages callbacks registration, as well as provides a way for PassInstrumentation to pass ...
LLVM_ATTRIBUTE_MINSIZE std::enable_if_t<!std::is_same_v< PassT, PassManager > > addPass(PassT &&Pass)
bool isEmpty() const
Returns if the pass manager contains any passes.
Pass interface - Implemented by all 'passes'.
Definition Pass.h:97
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
Primary interface to the complete machine description for the target machine.
An abstract base class for streams implementations that also support a pwrite operation.
unique_function is a type-erasing functor similar to std::function.
This is an optimization pass for GlobalISel generic memory operations.
CodeGenFileType
These enums are meant to be passed into addPassesToEmitFile to indicate what type of file to emit,...
Definition CodeGen.h:256
PassManager< Module > ModulePassManager
Convenience typedef for a pass manager over modules.
CodeGenOptLevel
Code generation optimization level.
Definition CodeGen.h:227
PassManager< Function > FunctionPassManager
Convenience typedef for a pass manager over functions.
typename detail::detector< void, Op, Args... >::value_t is_detected
Detects if a given trait holds for some set of arguments 'Args'.
PassManager< MachineFunction > MachineFunctionPassManager
Convenience typedef for a pass manager over functions.
AnalysisManager< Module > ModuleAnalysisManager
Convenience typedef for the Module analysis manager.
Definition MIRParser.h:39