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
61
62static Reloc::Model getEffectiveRelocModel(std::optional<Reloc::Model> RM) {
63 return RM.value_or(Reloc::PIC_);
64}
65
67 StringRef CPU, StringRef FS,
69 std::optional<Reloc::Model> RM,
70 std::optional<CodeModel::Model> CM,
71 CodeGenOptLevel OL, bool JIT)
72 : CodeGenTargetMachineImpl(T, TT.computeDataLayout(), TT, CPU, FS, Options,
74 getEffectiveCodeModel(CM, CodeModel::Small), OL),
75 TLOF(std::make_unique<BPFTargetLoweringObjectFileELF>()),
76 Subtarget(TT, std::string(CPU), std::string(FS), *this) {
78 this->Options.TrapUnreachable = true;
79 this->Options.NoTrapAfterNoreturn = true;
80 }
81
83
84 BPFMCAsmInfo *MAI =
85 static_cast<BPFMCAsmInfo *>(const_cast<MCAsmInfo *>(AsmInfo.get()));
86 MAI->setDwarfUsesRelocationsAcrossSections(!Subtarget.getUseDwarfRIS());
87}
88
89namespace {
90// BPF Code Generator Pass Configuration Options.
91class BPFPassConfig : public TargetPassConfig {
92public:
93 BPFPassConfig(BPFTargetMachine &TM, PassManagerBase &PM)
94 : TargetPassConfig(TM, PM) {}
95
96 BPFTargetMachine &getBPFTargetMachine() const {
98 }
99
100 void addIRPasses() override;
101 bool addInstSelector() override;
102 void addMachineSSAOptimization() override;
103 void addPreEmitPass() override;
104
105 bool addIRTranslator() override;
106 bool addLegalizeMachineIR() override;
107 bool addRegBankSelect() override;
108 bool addGlobalInstructionSelect() override;
109};
110}
111
113 return new BPFPassConfig(*this, PM);
114}
115
116void BPFPassConfig::addIRPasses() {
118 addPass(createBPFCheckAndAdjustIR());
119
121}
122
125 return TargetTransformInfo(std::make_unique<BPFTTIImpl>(this, F));
126}
127
128// Install an instruction selector pass using
129// the ISelDag to gen BPF code.
130bool BPFPassConfig::addInstSelector() {
131 addPass(createBPFISelDag(getBPFTargetMachine()));
132
133 return false;
134}
135
136void BPFPassConfig::addMachineSSAOptimization() {
138
139 // The default implementation must be called first as we want eBPF
140 // Peephole ran at last.
142
143 const BPFSubtarget *Subtarget = getBPFTargetMachine().getSubtargetImpl();
144 if (!DisableMIPeephole) {
145 if (Subtarget->getHasAlu32())
146 addPass(createBPFMIPeepholePass());
147 }
148}
149
150void BPFPassConfig::addPreEmitPass() {
154}
155
156bool BPFPassConfig::addIRTranslator() {
157 addPass(new IRTranslator());
158 return false;
159}
160
161bool BPFPassConfig::addLegalizeMachineIR() {
162 addPass(new Legalizer());
163 return false;
164}
165
166bool BPFPassConfig::addRegBankSelect() {
167 addPass(new RegBankSelect());
168 return false;
169}
170
171bool BPFPassConfig::addGlobalInstructionSelect() {
172 addPass(new InstructionSelect(getOptLevel()));
173 return false;
174}
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 initializeBPFMIPreEmitPeepholePass(PassRegistry &)
void initializeBPFMISimplifyPatchablePass(PassRegistry &)
FunctionPass * createBPFMISimplifyPatchablePass()
FunctionPass * createBPFMIPreEmitCheckingPass()
Target & getTheBPFleTarget()
ModulePass * createBPFCheckAndAdjustIR()
FunctionPass * createBPFMIPreEmitPeepholePass()
void initializeBPFMIPeepholePass(PassRegistry &)
FunctionPass * createBPFMIPeepholePass()
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()
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 &)
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:860
RegisterTargetMachine - Helper template for registering a target machine implementation,...