LLVM 24.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"
33#include <optional>
34using namespace llvm;
35
37 cl::desc("Disable machine peepholes for BPF"));
38
39static cl::opt<bool>
40 DisableCheckUnreachable("bpf-disable-trap-unreachable", cl::Hidden,
41 cl::desc("Disable Trap Unreachable for BPF"));
42
60
61static Reloc::Model getEffectiveRelocModel(std::optional<Reloc::Model> RM) {
62 return RM.value_or(Reloc::PIC_);
63}
64
66 StringRef CPU, StringRef FS,
68 std::optional<Reloc::Model> RM,
69 std::optional<CodeModel::Model> CM,
70 CodeGenOptLevel OL, bool JIT)
71 : CodeGenTargetMachineImpl(T, TT.computeDataLayout(), TT, CPU, FS, Options,
73 getEffectiveCodeModel(CM, CodeModel::Small), OL),
74 TLOF(std::make_unique<BPFTargetLoweringObjectFileELF>()),
75 Subtarget(TT, std::string(CPU), std::string(FS), *this) {
77 this->Options.TrapUnreachable = true;
78 this->Options.NoTrapAfterNoreturn = true;
79 }
80
82
83 BPFMCAsmInfo *MAI =
84 static_cast<BPFMCAsmInfo *>(const_cast<MCAsmInfo *>(AsmInfo.get()));
85 MAI->setDwarfUsesRelocationsAcrossSections(!Subtarget.getUseDwarfRIS());
86}
87
88namespace {
89// BPF Code Generator Pass Configuration Options.
90class BPFPassConfig : public TargetPassConfig {
91public:
92 BPFPassConfig(BPFTargetMachine &TM, PassManagerBase &PM)
93 : TargetPassConfig(TM, PM) {}
94
95 BPFTargetMachine &getBPFTargetMachine() const {
97 }
98
99 void addIRPasses() override;
100 bool addInstSelector() override;
101 void addMachineSSAOptimization() override;
102 void addPreEmitPass() override;
103
104 bool addIRTranslator() override;
105 bool addLegalizeMachineIR() override;
106 bool addRegBankSelect() override;
107 bool addGlobalInstructionSelect() override;
108};
109}
110
112 return new BPFPassConfig(*this, PM);
113}
114
115void BPFPassConfig::addIRPasses() {
118
120}
121
124 return TargetTransformInfo(std::make_unique<BPFTTIImpl>(this, F));
125}
126
127// Install an instruction selector pass using
128// the ISelDag to gen BPF code.
129bool BPFPassConfig::addInstSelector() {
130 addPass(createBPFISelDag(getBPFTargetMachine()));
131
132 return false;
133}
134
135void BPFPassConfig::addMachineSSAOptimization() {
137
138 // The default implementation must be called first as we want eBPF
139 // Peephole ran at last.
141
142 const BPFSubtarget *Subtarget = getBPFTargetMachine().getSubtargetImpl();
143 if (!DisableMIPeephole) {
144 if (Subtarget->getHasAlu32())
146 }
147}
148
149void BPFPassConfig::addPreEmitPass() {
151 if (!DisableMIPeephole) {
154 }
155}
156
157bool BPFPassConfig::addIRTranslator() {
158 addPass(new IRTranslatorLegacy());
159 return false;
160}
161
162bool BPFPassConfig::addLegalizeMachineIR() {
163 addPass(new LegalizerLegacy());
164 return false;
165}
166
167bool BPFPassConfig::addRegBankSelect() {
168 addPass(new RegBankSelectLegacy());
169 return false;
170}
171
172bool BPFPassConfig::addGlobalInstructionSelect() {
173 addPass(new InstructionSelectLegacy(getOptLevel()));
174 return false;
175}
static Reloc::Model getEffectiveRelocModel()
cl::opt< bool > DisableMIPeephole
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"))
cl::opt< bool > DisableMIPeephole("disable-bpf-peephole", cl::Hidden, cl::desc("Disable machine peepholes for BPF"))
#define X(NUM, ENUM, NAME)
Definition ELF.h:857
#define LLVM_ABI
Definition Compiler.h:215
#define LLVM_EXTERNAL_VISIBILITY
Definition Compiler.h:132
This file declares the IRTranslator pass.
This header defines various interfaces for pass management in LLVM.
#define F(x, y, z)
Definition MD5.cpp:54
#define T
This file describes the interface of the MachineFunctionPass responsible for assigning the generic vi...
const GCNTargetMachine & getTM(const GCNSubtarget *STI)
static TableGen::Emitter::Opt Y("gen-skeleton-entry", EmitSkeleton, "Generate example skeleton entry")
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 ...
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)
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:67
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.
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
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:48
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,...
This is an optimization pass for GlobalISel generic memory operations.
void initializeBPFMISimplifyPatchableLegacyPass(PassRegistry &)
FunctionPass * createBPFMIPeepholeLegacyPass()
Target & getTheBPFleTarget()
FunctionPass * createBPFMIPreEmitCheckingLegacyPass()
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()
void initializeBPFAsmPrinterPass(PassRegistry &)
Target & getTheBPFTarget()
void initializeBPFMIPeepholeLegacyPass(PassRegistry &)
CodeGenOptLevel
Code generation optimization level.
Definition CodeGen.h:149
void initializeBPFMIPreEmitCheckingLegacyPass(PassRegistry &)
LLVM_ABI void initializeGlobalISel(PassRegistry &)
Initialize all passes linked into the GlobalISel library.
void initializeBPFMIPreEmitPeepholeLegacyPass(PassRegistry &)
void initializeBPFMIExpandStackArgPseudosLegacyPass(PassRegistry &)
void initializeBPFCheckAndAdjustIRLegacyPass(PassRegistry &)
FunctionPass * createBPFISelDag(BPFTargetMachine &TM)
ModulePass * createBPFCheckAndAdjustIRLegacyPass()
FunctionPass * createBPFMISimplifyPatchableLegacyPass()
FunctionPass * createBPFMIPreEmitPeepholeLegacyPass()
LLVM_ABI FunctionPass * createAtomicExpandLegacyPass()
AtomicExpandPass - At IR level this pass replace atomic instructions with __atomic_* library calls,...
void initializeBPFDAGToDAGISelLegacyPass(PassRegistry &)
FunctionPass * createBPFMIExpandStackArgPseudosLegacyPass()
Implement std::hash so that hash_code can be used in STL containers.
Definition BitVector.h:878
RegisterTargetMachine - Helper template for registering a target machine implementation,...