LLVM 19.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"
22#include "llvm/CodeGen/Passes.h"
25#include "llvm/IR/PassManager.h"
34#include <optional>
35using namespace llvm;
36
37static cl::
38opt<bool> DisableMIPeephole("disable-bpf-peephole", cl::Hidden,
39 cl::desc("Disable machine peepholes for BPF"));
40
42 // Register the target.
46
52}
53
54// DataLayout: little or big endian
55static std::string computeDataLayout(const Triple &TT) {
56 if (TT.getArch() == Triple::bpfeb)
57 return "E-m:e-p:64:64-i64:64-i128:128-n32:64-S128";
58 else
59 return "e-m:e-p:64:64-i64:64-i128:128-n32:64-S128";
60}
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 : LLVMTargetMachine(T, computeDataLayout(TT), TT, CPU, FS, Options,
74 getEffectiveCodeModel(CM, CodeModel::Small), OL),
75 TLOF(std::make_unique<TargetLoweringObjectFileELF>()),
76 Subtarget(TT, std::string(CPU), std::string(FS), *this) {
78
79 BPFMCAsmInfo *MAI =
80 static_cast<BPFMCAsmInfo *>(const_cast<MCAsmInfo *>(AsmInfo.get()));
82}
83
84namespace {
85// BPF Code Generator Pass Configuration Options.
86class BPFPassConfig : public TargetPassConfig {
87public:
88 BPFPassConfig(BPFTargetMachine &TM, PassManagerBase &PM)
89 : TargetPassConfig(TM, PM) {}
90
91 BPFTargetMachine &getBPFTargetMachine() const {
92 return getTM<BPFTargetMachine>();
93 }
94
95 void addIRPasses() override;
96 bool addInstSelector() override;
97 void addMachineSSAOptimization() override;
98 void addPreEmitPass() override;
99
100 bool addIRTranslator() override;
101 bool addLegalizeMachineIR() override;
102 bool addRegBankSelect() override;
103 bool addGlobalInstructionSelect() override;
104};
105}
106
108 return new BPFPassConfig(*this, PM);
109}
110
112 return PassBuilder::parseSinglePassOption(Params, "allow-partial",
113 "BPFPreserveStaticOffsetPass");
114}
115
117 PassBuilder &PB, bool PopulateClassToPassNames) {
118#define GET_PASS_REGISTRY "BPFPassRegistry.def"
120
129 });
131 OptimizationLevel Level) {
132 FPM.addPass(SimplifyCFGPass(SimplifyCFGOptions().hoistCommonInsts(true)));
134 });
136 [=](FunctionPassManager &FPM, OptimizationLevel Level) {
137 // Run this after loop unrolling but before
138 // SimplifyCFGPass(... .sinkCommonInsts(true))
140 });
144 });
145}
146
147void BPFPassConfig::addIRPasses() {
149 addPass(createBPFCheckAndAdjustIR());
150
152}
153
156 return TargetTransformInfo(BPFTTIImpl(this, F));
157}
158
159// Install an instruction selector pass using
160// the ISelDag to gen BPF code.
161bool BPFPassConfig::addInstSelector() {
162 addPass(createBPFISelDag(getBPFTargetMachine()));
163
164 return false;
165}
166
167void BPFPassConfig::addMachineSSAOptimization() {
169
170 // The default implementation must be called first as we want eBPF
171 // Peephole ran at last.
173
174 const BPFSubtarget *Subtarget = getBPFTargetMachine().getSubtargetImpl();
175 if (!DisableMIPeephole) {
176 if (Subtarget->getHasAlu32())
177 addPass(createBPFMIPeepholePass());
178 }
179}
180
181void BPFPassConfig::addPreEmitPass() {
183 if (getOptLevel() != CodeGenOptLevel::None)
186}
187
188bool BPFPassConfig::addIRTranslator() {
189 addPass(new IRTranslator());
190 return false;
191}
192
193bool BPFPassConfig::addLegalizeMachineIR() {
194 addPass(new Legalizer());
195 return false;
196}
197
198bool BPFPassConfig::addRegBankSelect() {
199 addPass(new RegBankSelect());
200 return false;
201}
202
203bool BPFPassConfig::addGlobalInstructionSelect() {
204 addPass(new InstructionSelect(getOptLevel()));
205 return false;
206}
LLVM_EXTERNAL_VISIBILITY void LLVMInitializeBPFTarget()
static Expected< bool > parseBPFPreserveStaticOffsetOptions(StringRef Params)
static Reloc::Model getEffectiveRelocModel(std::optional< Reloc::Model > RM)
static cl::opt< bool > DisableMIPeephole("disable-bpf-peephole", cl::Hidden, cl::desc("Disable machine peepholes for BPF"))
#define LLVM_EXTERNAL_VISIBILITY
Definition: Compiler.h:135
static GCMetadataPrinterRegistry::Add< ErlangGCPrinter > X("erlang", "erlang-compatible garbage collector")
This file declares the IRTranslator pass.
static LVOptions Options
Definition: LVOptions.cpp:25
static std::string computeDataLayout()
#define F(x, y, z)
Definition: MD5.cpp:55
static GCMetadataPrinterRegistry::Add< OcamlGCMetadataPrinter > Y("ocaml", "ocaml 3.10-compatible collector")
ModulePassManager MPM
const char LLVMTargetMachineRef TM
PassBuilder PB(Machine, PassOpts->PTO, std::nullopt, &PIC)
This header defines various interfaces for pass management in LLVM.
This file describes the interface of the MachineFunctionPass responsible for assigning the generic vi...
This file provides the interface for the pass responsible for both simplifying and canonicalizing the...
Target-Independent Code Generator Pass Configuration Options pass.
void setDwarfUsesRelocationsAcrossSections(bool enable)
Definition: BPFMCAsmInfo.h:46
bool getUseDwarfRIS() const
Definition: BPFSubtarget.h:88
bool getHasAlu32() const
Definition: BPFSubtarget.h:87
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, bool PopulateClassToPassNames) 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
Get a TargetTransformInfo implementation for the target.
Tagged union holding either a T or a Error.
Definition: Error.h:481
This pass is responsible for selecting generic machine instructions to target-specific instructions.
This class describes a target machine that is implemented with the LLVM target-independent code gener...
This class is intended to be used as a base class for asm properties and features specific to the tar...
Definition: MCAsmInfo.h:56
This class provides access to building LLVM's passes.
Definition: PassBuilder.h:105
void registerPipelineEarlySimplificationEPCallback(const std::function< void(ModulePassManager &, OptimizationLevel)> &C)
Register a callback for a default optimizer pipeline extension point.
Definition: PassBuilder.h:479
void registerPipelineStartEPCallback(const std::function< void(ModulePassManager &, OptimizationLevel)> &C)
Register a callback for a default optimizer pipeline extension point.
Definition: PassBuilder.h:470
void registerPeepholeEPCallback(const std::function< void(FunctionPassManager &, OptimizationLevel)> &C)
Register a callback for a default optimizer pipeline extension point.
Definition: PassBuilder.h:404
void registerScalarOptimizerLateEPCallback(const std::function< void(FunctionPassManager &, OptimizationLevel)> &C)
Register a callback for a default optimizer pipeline extension point.
Definition: PassBuilder.h:438
static Expected< bool > parseSinglePassOption(StringRef Params, StringRef OptionName, StringRef PassName)
Handle passes only accept one bool-valued parameter.
LLVM_ATTRIBUTE_MINSIZE void addPass(PassT &&Pass)
Definition: PassManager.h:249
PassRegistry - This class manages the registration and intitialization of the pass subsystem as appli...
Definition: PassRegistry.h:37
static 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.
Definition: RegBankSelect.h:91
A pass to simplify and canonicalize the CFG of a function.
Definition: SimplifyCFG.h:29
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
std::unique_ptr< const MCAsmInfo > AsmInfo
Contains target specific asm information.
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:44
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.
Definition: AddressRanges.h:18
FunctionPass * createBPFMISimplifyPatchablePass()
ModuleToFunctionPassAdaptor createModuleToFunctionPassAdaptor(FunctionPassT &&Pass, bool EagerlyInvalidate=false)
A function to deduce a function pass type and wrap it in the templated adaptor.
Definition: PassManager.h:916
FunctionPass * createBPFMIPreEmitCheckingPass()
Target & getTheBPFleTarget()
ModulePass * createBPFCheckAndAdjustIR()
FunctionPass * createBPFMIPreEmitPeepholePass()
void initializeBPFMIPeepholePass(PassRegistry &)
FunctionPass * createBPFMIPeepholePass()
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()
Target & getTheBPFTarget()
CodeGenOptLevel
Code generation optimization level.
Definition: CodeGen.h:54
void initializeGlobalISel(PassRegistry &)
Initialize all passes linked into the GlobalISel library.
Definition: GlobalISel.cpp:17
void initializeBPFCheckAndAdjustIRPass(PassRegistry &)
FunctionPass * createBPFISelDag(BPFTargetMachine &TM)
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:858
RegisterTargetMachine - Helper template for registering a target machine implementation,...