LLVM 23.0.0git
BPFCodeGenPassBuilder.cpp
Go to the documentation of this file.
1//===----------------------------------------------------------------------===//
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#include "BPF.h"
10#include "BPFAsmPrinter.h"
11#include "BPFSubtarget.h"
12#include "BPFTargetMachine.h"
15#include "llvm/MC/MCStreamer.h"
18#include "llvm/Support/Error.h"
22
23using namespace llvm;
24
26
27namespace {
28
29class BPFCodeGenPassBuilder
30 : public CodeGenPassBuilder<BPFCodeGenPassBuilder, BPFTargetMachine> {
31 using Base = CodeGenPassBuilder<BPFCodeGenPassBuilder, BPFTargetMachine>;
32
33public:
34 explicit BPFCodeGenPassBuilder(BPFTargetMachine &TM,
35 const CGPassBuilderOption &Opts,
36 PassInstrumentationCallbacks *PIC)
37 : CodeGenPassBuilder(TM, Opts, PIC) {}
38
39 void addIRPasses(PassManagerWrapper &PMW) const;
40 Error addInstSelector(PassManagerWrapper &PMW) const;
41 void addMachineSSAOptimization(PassManagerWrapper &PMW) const;
42 void addPreEmitPass(PassManagerWrapper &PMW) const;
43 void addAsmPrinterBegin(PassManagerWrapper &PMW) const;
44 void addAsmPrinter(PassManagerWrapper &PMW) const;
45 void addAsmPrinterEnd(PassManagerWrapper &PMW) const;
46};
47
48void BPFCodeGenPassBuilder::addIRPasses(PassManagerWrapper &PMW) const {
49 addFunctionPass(AtomicExpandPass(TM), PMW);
50 flushFPMsToMPM(PMW);
51 addModulePass(BPFCheckAndAdjustIRPass(), PMW);
52
53 Base::addIRPasses(PMW);
54}
55
56Error BPFCodeGenPassBuilder::addInstSelector(PassManagerWrapper &PMW) const {
57 addMachineFunctionPass(BPFISelDAGToDAGPass(TM), PMW);
58 return Error::success();
59}
60
61void BPFCodeGenPassBuilder::addMachineSSAOptimization(
62 PassManagerWrapper &PMW) const {
63 addMachineFunctionPass(BPFMISimplifyPatchablePass(), PMW);
64
65 Base::addMachineSSAOptimization(PMW);
66
67 const BPFSubtarget *Subtarget = TM.getSubtargetImpl();
68 if (!DisableMIPeephole) {
69 if (Subtarget->getHasAlu32())
70 addMachineFunctionPass(BPFMIPeepholePass(), PMW);
71 }
72}
73
74void BPFCodeGenPassBuilder::addPreEmitPass(PassManagerWrapper &PMW) const {
75 addMachineFunctionPass(BPFMIPreEmitCheckingPass(), PMW);
76 if (!DisableMIPeephole) {
77 addMachineFunctionPass(BPFMIExpandStackArgPseudosPass(), PMW);
78 addMachineFunctionPass(BPFMIPreEmitPeepholePass(), PMW);
79 }
80}
81
82void BPFCodeGenPassBuilder::addAsmPrinterBegin(PassManagerWrapper &PMW) const {
83 addModulePass(BPFAsmPrinterBeginPass(), PMW, /*Force=*/true);
84}
85
86void BPFCodeGenPassBuilder::addAsmPrinter(PassManagerWrapper &PMW) const {
87 addMachineFunctionPass(BPFAsmPrinterPass(), PMW);
88}
89
90void BPFCodeGenPassBuilder::addAsmPrinterEnd(PassManagerWrapper &PMW) const {
91 addModulePass(BPFAsmPrinterEndPass(), PMW);
92}
93
94} // namespace
95
97 return PassBuilder::parseSinglePassOption(Params, "allow-partial",
98 "BPFPreserveStaticOffsetPass");
99}
100
102#define GET_PASS_REGISTRY "BPFPassRegistry.def"
104 // TODO(boomanaiden154): Move this into the base CodeGenPassBuilder once all
105 // targets that currently implement it have a ported asm-printer pass.
106 if (PIC) {
107 PIC->addClassToPassName(BPFAsmPrinterBeginPass::name(),
108 "bpf-asm-printer-begin");
109 PIC->addClassToPassName(BPFAsmPrinterPass::name(), "bpf-asmprinter");
110 PIC->addClassToPassName(BPFAsmPrinterEndPass::name(),
111 "bpf-asm-printer-end");
112 }
113
114 PB.registerPipelineStartEPCallback(
121 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM)));
122 });
123 PB.registerPeepholeEPCallback([=](FunctionPassManager &FPM,
124 OptimizationLevel Level) {
125 FPM.addPass(SimplifyCFGPass(SimplifyCFGOptions().hoistCommonInsts(true)));
127 });
128 PB.registerScalarOptimizerLateEPCallback(
129 [=](FunctionPassManager &FPM, OptimizationLevel Level) {
130 // Run this after loop unrolling but before
131 // SimplifyCFGPass(... .sinkCommonInsts(true))
133 });
134 PB.registerPipelineEarlySimplificationEPCallback(
137 });
138}
139
142 raw_pwrite_stream *DwoOut, CodeGenFileType FileType,
143 const CGPassBuilderOption &Opt, MCContext &Ctx,
145 auto CGPB = BPFCodeGenPassBuilder(*this, Opt, PIC);
146 return CGPB.buildPipeline(MPM, MAM, Out, DwoOut, FileType, Ctx);
147}
cl::opt< bool > DisableMIPeephole
static Expected< bool > parseBPFPreserveStaticOffsetOptions(StringRef Params)
Interfaces for producing common pass manager configurations.
ModuleAnalysisManager MAM
PassInstrumentationCallbacks PIC
PassBuilder PB(Machine, PassOpts->PTO, std::nullopt, &PIC)
This file defines the Pass Instrumentation classes that provide instrumentation points into the pass ...
This file provides the interface for the pass responsible for both simplifying and canonicalizing the...
bool getHasAlu32() const
void registerPassBuilderCallbacks(PassBuilder &PB) override
Allow the target to modify the pass pipeline.
Error buildCodeGenPipeline(ModulePassManager &MPM, ModuleAnalysisManager &MAM, raw_pwrite_stream &Out, raw_pwrite_stream *DwoOut, CodeGenFileType FileType, const CGPassBuilderOption &Opt, MCContext &Ctx, PassInstrumentationCallbacks *PIC) override
This class provides access to building LLVM's passes.
Lightweight error class with error context and mandatory checking.
Definition Error.h:159
static ErrorSuccess success()
Create a success value.
Definition Error.h:336
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 provides access to building LLVM's passes.
static LLVM_ABI Expected< bool > parseSinglePassOption(StringRef Params, StringRef OptionName, StringRef PassName)
Handle passes only accept one bool-valued parameter.
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)
A pass to simplify and canonicalize the CFG of a function.
Definition SimplifyCFG.h:30
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
An abstract base class for streams implementations that also support a pwrite operation.
Interfaces for registering analysis passes, producing common pass manager configurations,...
This is an optimization pass for GlobalISel generic memory operations.
ModuleToFunctionPassAdaptor createModuleToFunctionPassAdaptor(FunctionPassT &&Pass, bool EagerlyInvalidate=false)
A function to deduce a function pass type and wrap it in the templated adaptor.
ThinOrFullLTOPhase
This enumerates the LLVM full LTO or ThinLTO optimization phases.
Definition Pass.h:77
CodeGenFileType
These enums are meant to be passed into addPassesToEmitFile to indicate what type of file to emit,...
Definition CodeGen.h:111
PassManager< Module > ModulePassManager
Convenience typedef for a pass manager over modules.
PassManager< Function > FunctionPassManager
Convenience typedef for a pass manager over functions.
AnalysisManager< Module > ModuleAnalysisManager
Convenience typedef for the Module analysis manager.
Definition MIRParser.h:39