LLVM 22.0.0git
BPFTargetMachine.cpp
Go to the documentation of this file.
1//===-- BPFTargetMachine.cpp - Define TargetMachine for BPF ---------------===//
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// Implements the info about BPF target spec.
10//
11//===----------------------------------------------------------------------===//
12
13#include "BPFTargetMachine.h"
14#include "BPF.h"
23#include "llvm/CodeGen/Passes.h"
26#include "llvm/IR/PassManager.h"
35#include <optional>
36using namespace llvm;
37
38static cl::
39opt<bool> DisableMIPeephole("disable-bpf-peephole", cl::Hidden,
40 cl::desc("Disable machine peepholes for BPF"));
41
42static cl::opt<bool>
43 DisableCheckUnreachable("bpf-disable-trap-unreachable", cl::Hidden,
44 cl::desc("Disable Trap Unreachable for BPF"));
45
62
63static Reloc::Model getEffectiveRelocModel(std::optional<Reloc::Model> RM) {
64 return RM.value_or(Reloc::PIC_);
65}
66
68 StringRef CPU, StringRef FS,
70 std::optional<Reloc::Model> RM,
71 std::optional<CodeModel::Model> CM,
72 CodeGenOptLevel OL, bool JIT)
73 : CodeGenTargetMachineImpl(T, TT.computeDataLayout(), TT, CPU, FS, Options,
75 getEffectiveCodeModel(CM, CodeModel::Small), OL),
76 TLOF(std::make_unique<BPFTargetLoweringObjectFileELF>()),
77 Subtarget(TT, std::string(CPU), std::string(FS), *this) {
79 this->Options.TrapUnreachable = true;
80 this->Options.NoTrapAfterNoreturn = true;
81 }
82
84
85 BPFMCAsmInfo *MAI =
86 static_cast<BPFMCAsmInfo *>(const_cast<MCAsmInfo *>(AsmInfo.get()));
87 MAI->setDwarfUsesRelocationsAcrossSections(!Subtarget.getUseDwarfRIS());
88}
89
90namespace {
91// BPF Code Generator Pass Configuration Options.
92class BPFPassConfig : public TargetPassConfig {
93public:
94 BPFPassConfig(BPFTargetMachine &TM, PassManagerBase &PM)
95 : TargetPassConfig(TM, PM) {}
96
97 BPFTargetMachine &getBPFTargetMachine() const {
99 }
100
101 void addIRPasses() override;
102 bool addInstSelector() override;
103 void addMachineSSAOptimization() override;
104 void addPreEmitPass() override;
105
106 bool addIRTranslator() override;
107 bool addLegalizeMachineIR() override;
108 bool addRegBankSelect() override;
109 bool addGlobalInstructionSelect() override;
110};
111}
112
114 return new BPFPassConfig(*this, PM);
115}
116
118 return PassBuilder::parseSinglePassOption(Params, "allow-partial",
119 "BPFPreserveStaticOffsetPass");
120}
121
123#define GET_PASS_REGISTRY "BPFPassRegistry.def"
125
126 PB.registerPipelineStartEPCallback(
133 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM)));
134 });
135 PB.registerPeepholeEPCallback([=](FunctionPassManager &FPM,
136 OptimizationLevel Level) {
137 FPM.addPass(SimplifyCFGPass(SimplifyCFGOptions().hoistCommonInsts(true)));
139 });
140 PB.registerScalarOptimizerLateEPCallback(
141 [=](FunctionPassManager &FPM, OptimizationLevel Level) {
142 // Run this after loop unrolling but before
143 // SimplifyCFGPass(... .sinkCommonInsts(true))
145 });
146 PB.registerPipelineEarlySimplificationEPCallback(
149 });
150}
151
152void BPFPassConfig::addIRPasses() {
154 addPass(createBPFCheckAndAdjustIR());
155
157}
158
161 return TargetTransformInfo(std::make_unique<BPFTTIImpl>(this, F));
162}
163
164// Install an instruction selector pass using
165// the ISelDag to gen BPF code.
166bool BPFPassConfig::addInstSelector() {
167 addPass(createBPFISelDag(getBPFTargetMachine()));
168
169 return false;
170}
171
172void BPFPassConfig::addMachineSSAOptimization() {
174
175 // The default implementation must be called first as we want eBPF
176 // Peephole ran at last.
178
179 const BPFSubtarget *Subtarget = getBPFTargetMachine().getSubtargetImpl();
180 if (!DisableMIPeephole) {
181 if (Subtarget->getHasAlu32())
182 addPass(createBPFMIPeepholePass());
183 }
184}
185
186void BPFPassConfig::addPreEmitPass() {
188 if (getOptLevel() != CodeGenOptLevel::None)
191}
192
193bool BPFPassConfig::addIRTranslator() {
194 addPass(new IRTranslator());
195 return false;
196}
197
198bool BPFPassConfig::addLegalizeMachineIR() {
199 addPass(new Legalizer());
200 return false;
201}
202
203bool BPFPassConfig::addRegBankSelect() {
204 addPass(new RegBankSelect());
205 return false;
206}
207
208bool BPFPassConfig::addGlobalInstructionSelect() {
209 addPass(new InstructionSelect(getOptLevel()));
210 return false;
211}
static Expected< bool > parseBPFPreserveStaticOffsetOptions(StringRef Params)
LLVM_ABI LLVM_EXTERNAL_VISIBILITY void LLVMInitializeBPFTarget()
static cl::opt< bool > DisableCheckUnreachable("bpf-disable-trap-unreachable", cl::Hidden, cl::desc("Disable Trap Unreachable for BPF"))
static cl::opt< bool > DisableMIPeephole("disable-bpf-peephole", cl::Hidden, cl::desc("Disable machine peepholes for BPF"))
#define LLVM_ABI
Definition Compiler.h:213
#define LLVM_EXTERNAL_VISIBILITY
Definition Compiler.h:132
DXIL Legalizer
This file declares the IRTranslator pass.
This header defines various interfaces for pass management in LLVM.
#define F(x, y, z)
Definition MD5.cpp:55
#define T
PassBuilder PB(Machine, PassOpts->PTO, std::nullopt, &PIC)
This file describes the interface of the MachineFunctionPass responsible for assigning the generic vi...
const GCNTargetMachine & getTM(const GCNSubtarget *STI)
This file provides the interface for the pass responsible for both simplifying and canonicalizing the...
static TableGen::Emitter::Opt Y("gen-skeleton-entry", EmitSkeleton, "Generate example skeleton entry")
static TableGen::Emitter::OptClass< SkeletonEmitter > X("gen-skeleton-class", "Generate example skeleton class")
Target-Independent Code Generator Pass Configuration Options pass.
void setDwarfUsesRelocationsAcrossSections(bool enable)
bool getHasAlu32() const
TargetPassConfig * createPassConfig(PassManagerBase &PM) override
Create a pass configuration object to be used by addPassToEmitX methods for generating a pipeline of ...
void registerPassBuilderCallbacks(PassBuilder &PB) override
Allow the target to modify the pass pipeline.
BPFTargetMachine(const Target &T, const Triple &TT, StringRef CPU, StringRef FS, const TargetOptions &Options, std::optional< Reloc::Model > RM, std::optional< CodeModel::Model > CM, CodeGenOptLevel OL, bool JIT)
TargetTransformInfo getTargetTransformInfo(const Function &F) const override
Return a TargetTransformInfo for a given function.
CodeGenTargetMachineImpl(const Target &T, StringRef DataLayoutString, const Triple &TT, StringRef CPU, StringRef FS, const TargetOptions &Options, Reloc::Model RM, CodeModel::Model CM, CodeGenOptLevel OL)
Tagged union holding either a T or a Error.
Definition Error.h:485
This pass is responsible for selecting generic machine instructions to target-specific instructions.
This class is intended to be used as a base class for asm properties and features specific to the tar...
Definition MCAsmInfo.h:64
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.
LLVM_ATTRIBUTE_MINSIZE std::enable_if_t<!std::is_same_v< PassT, PassManager > > addPass(PassT &&Pass)
PassRegistry - This class manages the registration and intitialization of the pass subsystem as appli...
static LLVM_ABI PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
This pass implements the reg bank selector pass used in the GlobalISel pipeline.
A pass to simplify and canonicalize the CFG of a function.
Definition SimplifyCFG.h:30
StringRef - Represent a constant reference to a string, i.e.
Definition StringRef.h:55
std::unique_ptr< const MCAsmInfo > AsmInfo
Contains target specific asm information.
TargetOptions Options
unsigned NoTrapAfterNoreturn
Do not emit a trap instruction for 'unreachable' IR instructions behind noreturn calls,...
unsigned TrapUnreachable
Emit target-specific trap instruction for 'unreachable' IR instructions.
Target-Independent Code Generator Pass Configuration Options.
virtual void addIRPasses()
Add common target configurable passes that perform LLVM IR to IR transforms following machine indepen...
virtual void addMachineSSAOptimization()
addMachineSSAOptimization - Add standard passes that optimize machine instructions in SSA form.
This pass provides access to the codegen interfaces that are needed for IR-level transformations.
Target - Wrapper for Target specific information.
Triple - Helper class for working with autoconf configuration names.
Definition Triple.h:47
PassManagerBase - An abstract interface to allow code to add passes to a pass manager without having ...
Interfaces for registering analysis passes, producing common pass manager configurations,...
template class LLVM_TEMPLATE_ABI opt< bool >
This is an optimization pass for GlobalISel generic memory operations.
void initializeBPFMIPreEmitPeepholePass(PassRegistry &)
void initializeBPFMISimplifyPatchablePass(PassRegistry &)
FunctionPass * createBPFMISimplifyPatchablePass()
ModuleToFunctionPassAdaptor createModuleToFunctionPassAdaptor(FunctionPassT &&Pass, bool EagerlyInvalidate=false)
A function to deduce a function pass type and wrap it in the templated adaptor.
FunctionPass * createBPFMIPreEmitCheckingPass()
Target & getTheBPFleTarget()
ModulePass * createBPFCheckAndAdjustIR()
FunctionPass * createBPFMIPreEmitPeepholePass()
void initializeBPFMIPeepholePass(PassRegistry &)
FunctionPass * createBPFMIPeepholePass()
ThinOrFullLTOPhase
This enumerates the LLVM full LTO or ThinLTO optimization phases.
Definition Pass.h:77
static Reloc::Model getEffectiveRelocModel(std::optional< Reloc::Model > RM)
CodeModel::Model getEffectiveCodeModel(std::optional< CodeModel::Model > CM, CodeModel::Model Default)
Helper method for getting the code model, returning Default if CM does not have a value.
Target & getTheBPFbeTarget()
PassManager< Module > ModulePassManager
Convenience typedef for a pass manager over modules.
void initializeBPFAsmPrinterPass(PassRegistry &)
Target & getTheBPFTarget()
CodeGenOptLevel
Code generation optimization level.
Definition CodeGen.h:82
LLVM_ABI void initializeGlobalISel(PassRegistry &)
Initialize all passes linked into the GlobalISel library.
void initializeBPFCheckAndAdjustIRPass(PassRegistry &)
PassManager< Function > FunctionPassManager
Convenience typedef for a pass manager over functions.
void initializeBPFMIPreEmitCheckingPass(PassRegistry &)
FunctionPass * createBPFISelDag(BPFTargetMachine &TM)
LLVM_ABI FunctionPass * createAtomicExpandLegacyPass()
AtomicExpandPass - At IR level this pass replace atomic instructions with __atomic_* library calls,...
void initializeBPFDAGToDAGISelLegacyPass(PassRegistry &)
Implement std::hash so that hash_code can be used in STL containers.
Definition BitVector.h:851
RegisterTargetMachine - Helper template for registering a target machine implementation,...