LLVM 17.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"
18#include "llvm/CodeGen/Passes.h"
21#include "llvm/IR/PassManager.h"
29#include <optional>
30using namespace llvm;
31
32static cl::
33opt<bool> DisableMIPeephole("disable-bpf-peephole", cl::Hidden,
34 cl::desc("Disable machine peepholes for BPF"));
35
37 // Register the target.
41
51}
52
53// DataLayout: little or big endian
54static std::string computeDataLayout(const Triple &TT) {
55 if (TT.getArch() == Triple::bpfeb)
56 return "E-m:e-p:64:64-i64:64-i128:128-n32:64-S128";
57 else
58 return "e-m:e-p:64:64-i64:64-i128:128-n32:64-S128";
59}
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 CodeGenOpt::Level OL, bool JIT)
71 : LLVMTargetMachine(T, computeDataLayout(TT), TT, CPU, FS, Options,
73 getEffectiveCodeModel(CM, CodeModel::Small), OL),
74 TLOF(std::make_unique<TargetLoweringObjectFileELF>()),
75 Subtarget(TT, std::string(CPU), std::string(FS), *this) {
77
78 BPFMCAsmInfo *MAI =
79 static_cast<BPFMCAsmInfo *>(const_cast<MCAsmInfo *>(AsmInfo.get()));
81}
82
83namespace {
84// BPF Code Generator Pass Configuration Options.
85class BPFPassConfig : public TargetPassConfig {
86public:
87 BPFPassConfig(BPFTargetMachine &TM, PassManagerBase &PM)
88 : TargetPassConfig(TM, PM) {}
89
90 BPFTargetMachine &getBPFTargetMachine() const {
91 return getTM<BPFTargetMachine>();
92 }
93
94 void addIRPasses() override;
95 bool addInstSelector() override;
96 void addMachineSSAOptimization() override;
97 void addPreEmitPass() override;
98};
99}
100
102 return new BPFPassConfig(*this, PM);
103}
104
113 });
115 OptimizationLevel Level) {
116 FPM.addPass(SimplifyCFGPass(SimplifyCFGOptions().hoistCommonInsts(true)));
117 });
121 });
122}
123
124void BPFPassConfig::addIRPasses() {
125 addPass(createBPFCheckAndAdjustIR());
127}
128
131 return TargetTransformInfo(BPFTTIImpl(this, F));
132}
133
134// Install an instruction selector pass using
135// the ISelDag to gen BPF code.
136bool BPFPassConfig::addInstSelector() {
137 addPass(createBPFISelDag(getBPFTargetMachine()));
138
139 return false;
140}
141
142void BPFPassConfig::addMachineSSAOptimization() {
144
145 // The default implementation must be called first as we want eBPF
146 // Peephole ran at last.
148
149 const BPFSubtarget *Subtarget = getBPFTargetMachine().getSubtargetImpl();
150 if (!DisableMIPeephole) {
151 if (Subtarget->getHasAlu32())
152 addPass(createBPFMIPeepholePass());
154 }
155}
156
157void BPFPassConfig::addPreEmitPass() {
159 if (getOptLevel() != CodeGenOpt::None)
162}
LLVM_EXTERNAL_VISIBILITY void LLVMInitializeBPFTarget()
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:127
static GCMetadataPrinterRegistry::Add< ErlangGCPrinter > X("erlang", "erlang-compatible garbage collector")
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 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:74
bool getHasAlu32() const
Definition: BPFSubtarget.h:73
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, CodeGenOpt::Level OL, bool JIT)
TargetTransformInfo getTargetTransformInfo(const Function &F) const override
Get a TargetTransformInfo implementation for the target.
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:100
void registerPipelineEarlySimplificationEPCallback(const std::function< void(ModulePassManager &, OptimizationLevel)> &C)
Register a callback for a default optimizer pipeline extension point.
Definition: PassBuilder.h:452
void registerPipelineStartEPCallback(const std::function< void(ModulePassManager &, OptimizationLevel)> &C)
Register a callback for a default optimizer pipeline extension point.
Definition: PassBuilder.h:443
void registerPeepholeEPCallback(const std::function< void(FunctionPassManager &, OptimizationLevel)> &C)
Register a callback for a default optimizer pipeline extension point.
Definition: PassBuilder.h:377
LLVM_ATTRIBUTE_MINSIZE std::enable_if_t<!std::is_same< PassT, PassManager >::value > addPass(PassT &&Pass)
Definition: PassManager.h:544
PassRegistry - This class manages the registration and intitialization of the pass subsystem as appli...
Definition: PassRegistry.h:38
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
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,...
Level
Code generation optimization level.
Definition: CodeGen.h:57
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
void initializeBPFIRPeepholePass(PassRegistry &)
void initializeBPFAdjustOptPass(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.
Definition: PassManager.h:1218
FunctionPass * createBPFMIPreEmitCheckingPass()
FunctionPass * createBPFMIPeepholeTruncElimPass()
Target & getTheBPFleTarget()
ModulePass * createBPFCheckAndAdjustIR()
FunctionPass * createBPFMIPreEmitPeepholePass()
void initializeBPFMIPeepholePass(PassRegistry &)
void initializeBPFAbstractMemberAccessLegacyPassPass(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()
void initializeBPFMIPeepholeTruncElimPass(PassRegistry &)
void initializeBPFCheckAndAdjustIRPass(PassRegistry &)
FunctionPass * createBPFISelDag(BPFTargetMachine &TM)
void initializeBPFPreserveDITypePass(PassRegistry &)
void initializeBPFDAGToDAGISelPass(PassRegistry &)
Definition: BitVector.h:858
RegisterTargetMachine - Helper template for registering a target machine implementation,...