LLVM 23.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
39 cl::desc("Disable machine peepholes for BPF"));
40
41static cl::opt<bool>
42 DisableCheckUnreachable("bpf-disable-trap-unreachable", cl::Hidden,
43 cl::desc("Disable Trap Unreachable for BPF"));
44
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
117void BPFPassConfig::addIRPasses() {
120
122}
123
126 return TargetTransformInfo(std::make_unique<BPFTTIImpl>(this, F));
127}
128
129// Install an instruction selector pass using
130// the ISelDag to gen BPF code.
131bool BPFPassConfig::addInstSelector() {
132 addPass(createBPFISelDag(getBPFTargetMachine()));
133
134 return false;
135}
136
137void BPFPassConfig::addMachineSSAOptimization() {
139
140 // The default implementation must be called first as we want eBPF
141 // Peephole ran at last.
143
144 const BPFSubtarget *Subtarget = getBPFTargetMachine().getSubtargetImpl();
145 if (!DisableMIPeephole) {
146 if (Subtarget->getHasAlu32())
148 }
149}
150
151void BPFPassConfig::addPreEmitPass() {
153 if (!DisableMIPeephole) {
156 }
157}
158
159bool BPFPassConfig::addIRTranslator() {
160 addPass(new IRTranslator());
161 return false;
162}
163
164bool BPFPassConfig::addLegalizeMachineIR() {
165 addPass(new Legalizer());
166 return false;
167}
168
169bool BPFPassConfig::addRegBankSelect() {
170 addPass(new RegBankSelect());
171 return false;
172}
173
174bool BPFPassConfig::addGlobalInstructionSelect() {
175 addPass(new InstructionSelect(getOptLevel()));
176 return false;
177}
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:856
#define LLVM_ABI
Definition Compiler.h:215
#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:54
#define T
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")
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:66
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: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,...
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:82
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:860
RegisterTargetMachine - Helper template for registering a target machine implementation,...