LLVM  16.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"
15 #include "BPFTargetTransformInfo.h"
18 #include "llvm/CodeGen/Passes.h"
22 #include "llvm/IR/PassManager.h"
23 #include "llvm/MC/TargetRegistry.h"
27 #include "llvm/Transforms/Scalar.h"
30 using namespace llvm;
31 
32 static cl::
33 opt<bool> DisableMIPeephole("disable-bpf-peephole", cl::Hidden,
34  cl::desc("Disable machine peepholes for BPF"));
35 
37  // Register the target.
41 
50 }
51 
52 // DataLayout: little or big endian
53 static std::string computeDataLayout(const Triple &TT) {
54  if (TT.getArch() == Triple::bpfeb)
55  return "E-m:e-p:64:64-i64:64-i128:128-n32:64-S128";
56  else
57  return "e-m:e-p:64:64-i64:64-i128:128-n32:64-S128";
58 }
59 
61  return RM.value_or(Reloc::PIC_);
62 }
63 
65  StringRef CPU, StringRef FS,
66  const TargetOptions &Options,
69  CodeGenOpt::Level OL, bool JIT)
73  TLOF(std::make_unique<TargetLoweringObjectFileELF>()),
74  Subtarget(TT, std::string(CPU), std::string(FS), *this) {
75  initAsmInfo();
76 
77  BPFMCAsmInfo *MAI =
78  static_cast<BPFMCAsmInfo *>(const_cast<MCAsmInfo *>(AsmInfo.get()));
80 }
81 
82 namespace {
83 // BPF Code Generator Pass Configuration Options.
84 class BPFPassConfig : public TargetPassConfig {
85 public:
86  BPFPassConfig(BPFTargetMachine &TM, PassManagerBase &PM)
87  : TargetPassConfig(TM, PM) {}
88 
89  BPFTargetMachine &getBPFTargetMachine() const {
90  return getTM<BPFTargetMachine>();
91  }
92 
93  void addIRPasses() override;
94  bool addInstSelector() override;
95  void addMachineSSAOptimization() override;
96  void addPreEmitPass() override;
97 };
98 }
99 
101  return new BPFPassConfig(*this, PM);
102 }
103 
110  FPM.addPass(BPFIRPeepholePass());
112  });
114  OptimizationLevel Level) {
115  FPM.addPass(SimplifyCFGPass(SimplifyCFGOptions().hoistCommonInsts(true)));
116  });
120  });
121 }
122 
123 void BPFPassConfig::addIRPasses() {
124  addPass(createBPFCheckAndAdjustIR());
126 }
127 
130  return TargetTransformInfo(BPFTTIImpl(this, F));
131 }
132 
133 // Install an instruction selector pass using
134 // the ISelDag to gen BPF code.
135 bool BPFPassConfig::addInstSelector() {
136  addPass(createBPFISelDag(getBPFTargetMachine()));
137 
138  return false;
139 }
140 
141 void BPFPassConfig::addMachineSSAOptimization() {
143 
144  // The default implementation must be called first as we want eBPF
145  // Peephole ran at last.
147 
148  const BPFSubtarget *Subtarget = getBPFTargetMachine().getSubtargetImpl();
149  if (!DisableMIPeephole) {
150  if (Subtarget->getHasAlu32())
151  addPass(createBPFMIPeepholePass());
153  }
154 }
155 
156 void BPFPassConfig::addPreEmitPass() {
158  if (getOptLevel() != CodeGenOpt::None)
159  if (!DisableMIPeephole)
161 }
llvm::getTheBPFleTarget
Target & getTheBPFleTarget()
Definition: BPFTargetInfo.cpp:14
llvm::initializeBPFIRPeepholePass
void initializeBPFIRPeepholePass(PassRegistry &)
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
PassBuilder.h
llvm::BPFTTIImpl
Definition: BPFTargetTransformInfo.h:24
llvm::Triple::bpfeb
@ bpfeb
Definition: Triple.h:57
llvm::PassBuilder::registerPipelineStartEPCallback
void registerPipelineStartEPCallback(const std::function< void(ModulePassManager &, OptimizationLevel)> &C)
Register a callback for a default optimizer pipeline extension point.
Definition: PassBuilder.h:455
llvm::createBPFCheckAndAdjustIR
ModulePass * createBPFCheckAndAdjustIR()
llvm::TargetOptions
Definition: TargetOptions.h:124
Scalar.h
llvm::Function
Definition: Function.h:60
llvm::PassManager::addPass
LLVM_ATTRIBUTE_MINSIZE std::enable_if_t<!std::is_same< PassT, PassManager >::value > addPass(PassT &&Pass)
Definition: PassManager.h:544
llvm::BPFAbstractMemberAccessPass
Definition: BPF.h:46
llvm::MCAsmInfo
This class is intended to be used as a base class for asm properties and features specific to the tar...
Definition: MCAsmInfo.h:56
llvm::Target
Target - Wrapper for Target specific information.
Definition: TargetRegistry.h:149
llvm::initializeBPFAbstractMemberAccessLegacyPassPass
void initializeBPFAbstractMemberAccessLegacyPassPass(PassRegistry &)
llvm::TargetTransformInfo
This pass provides access to the codegen interfaces that are needed for IR-level transformations.
Definition: TargetTransformInfo.h:173
llvm::BPFTargetMachine::createPassConfig
TargetPassConfig * createPassConfig(PassManagerBase &PM) override
Create a pass configuration object to be used by addPassToEmitX methods for generating a pipeline of ...
Definition: BPFTargetMachine.cpp:100
llvm::BPFSubtarget::getUseDwarfRIS
bool getUseDwarfRIS() const
Definition: BPFSubtarget.h:74
llvm::createBPFMIPeepholeTruncElimPass
FunctionPass * createBPFMIPeepholeTruncElimPass()
llvm::Triple
Triple - Helper class for working with autoconf configuration names.
Definition: Triple.h:44
llvm::BPFPreserveDITypePass
Definition: BPF.h:57
llvm::cl::Hidden
@ Hidden
Definition: CommandLine.h:140
llvm::TargetLoweringObjectFileELF
Definition: TargetLoweringObjectFileImpl.h:34
llvm::X86AS::FS
@ FS
Definition: X86.h:200
llvm::Optional< Reloc::Model >
T
#define T
Definition: Mips16ISelLowering.cpp:341
llvm::createBPFMIPreEmitPeepholePass
FunctionPass * createBPFMIPreEmitPeepholePass()
llvm::BPFMCAsmInfo
Definition: BPFMCAsmInfo.h:21
BPF.h
LegacyPassManager.h
llvm::BPFTargetMachine::registerPassBuilderCallbacks
void registerPassBuilderCallbacks(PassBuilder &PB) override
Allow the target to modify the pass pipeline.
Definition: BPFTargetMachine.cpp:104
F
#define F(x, y, z)
Definition: MD5.cpp:55
BPFTargetInfo.h
llvm::initializeBPFAdjustOptPass
void initializeBPFAdjustOptPass(PassRegistry &)
llvm::Reloc::Model
Model
Definition: CodeGen.h:22
llvm::createBPFMIPreEmitCheckingPass
FunctionPass * createBPFMIPreEmitCheckingPass()
FormattedStream.h
BPFTargetTransformInfo.h
llvm::MSP430Attrs::CodeModel
CodeModel
Definition: MSP430Attributes.h:37
llvm::PassRegistry::getPassRegistry
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
Definition: PassRegistry.cpp:24
computeDataLayout
static std::string computeDataLayout(const Triple &TT)
Definition: BPFTargetMachine.cpp:53
Y
static GCMetadataPrinterRegistry::Add< OcamlGCMetadataPrinter > Y("ocaml", "ocaml 3.10-compatible collector")
llvm::PassBuilder
This class provides access to building LLVM's passes.
Definition: PassBuilder.h:97
llvm::CodeModel::Small
@ Small
Definition: CodeGen.h:28
llvm::initializeBPFMIPeepholePass
void initializeBPFMIPeepholePass(PassRegistry &)
llvm::RegisterTargetMachine
RegisterTargetMachine - Helper template for registering a target machine implementation,...
Definition: TargetRegistry.h:1356
llvm::PassRegistry
PassRegistry - This class manages the registration and intitialization of the pass subsystem as appli...
Definition: PassRegistry.h:38
Options
const char LLVMTargetMachineRef LLVMPassBuilderOptionsRef Options
Definition: PassBuilderBindings.cpp:48
LLVMInitializeBPFTarget
LLVM_EXTERNAL_VISIBILITY void LLVMInitializeBPFTarget()
Definition: BPFTargetMachine.cpp:36
llvm::initializeBPFPreserveDITypePass
void initializeBPFPreserveDITypePass(PassRegistry &)
X
static GCMetadataPrinterRegistry::Add< ErlangGCPrinter > X("erlang", "erlang-compatible garbage collector")
PB
PassBuilder PB(Machine, PassOpts->PTO, None, &PIC)
Passes.h
llvm::TargetPassConfig
Target-Independent Code Generator Pass Configuration Options.
Definition: TargetPassConfig.h:84
llvm::PassBuilder::registerPipelineEarlySimplificationEPCallback
void registerPipelineEarlySimplificationEPCallback(const std::function< void(ModulePassManager &, OptimizationLevel)> &C)
Register a callback for a default optimizer pipeline extension point.
Definition: PassBuilder.h:464
llvm::cl::opt< bool >
llvm::createModuleToFunctionPassAdaptor
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
llvm::initializeBPFCheckAndAdjustIRPass
void initializeBPFCheckAndAdjustIRPass(PassRegistry &)
llvm::EngineKind::JIT
@ JIT
Definition: ExecutionEngine.h:524
LLVM_EXTERNAL_VISIBILITY
#define LLVM_EXTERNAL_VISIBILITY
Definition: Compiler.h:127
move
compiles ldr LCPI1_0 ldr ldr mov lsr tst moveq r1 ldr LCPI1_1 and r0 bx lr It would be better to do something like to fold the shift into the conditional move
Definition: README.txt:546
BPFMCAsmInfo.h
llvm::TargetPassConfig::addIRPasses
virtual void addIRPasses()
Add common target configurable passes that perform LLVM IR to IR transforms following machine indepen...
Definition: TargetPassConfig.cpp:854
TargetPassConfig.h
llvm::getTheBPFbeTarget
Target & getTheBPFbeTarget()
Definition: BPFTargetInfo.cpp:18
llvm::createBPFISelDag
FunctionPass * createBPFISelDag(BPFTargetMachine &TM)
Definition: BPFISelDAGToDAG.cpp:496
llvm::CodeGenOpt::None
@ None
Definition: CodeGen.h:53
TargetOptions.h
llvm::AArch64::RM
@ RM
Definition: AArch64ISelLowering.h:487
llvm::BPFMCAsmInfo::setDwarfUsesRelocationsAcrossSections
void setDwarfUsesRelocationsAcrossSections(bool enable)
Definition: BPFMCAsmInfo.h:46
llvm::createBPFMISimplifyPatchablePass
FunctionPass * createBPFMISimplifyPatchablePass()
llvm::Reloc::PIC_
@ PIC_
Definition: CodeGen.h:22
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
this
Analysis the ScalarEvolution expression for r is this
Definition: README.txt:8
llvm::BPFIRPeepholePass
Definition: BPF.h:64
llvm::CodeGenOpt::Level
Level
Definition: CodeGen.h:52
llvm::getEffectiveRelocModel
static Reloc::Model getEffectiveRelocModel(Optional< Reloc::Model > RM)
Definition: AVRTargetMachine.cpp:40
llvm::getEffectiveCodeModel
CodeModel::Model getEffectiveCodeModel(Optional< CodeModel::Model > CM, CodeModel::Model Default)
Helper method for getting the code model, returning Default if CM does not have a value.
Definition: TargetMachine.h:500
llvm::LLVMTargetMachine::initAsmInfo
void initAsmInfo()
Definition: LLVMTargetMachine.cpp:40
llvm::PassManager< Module >
llvm::BPFTargetMachine
Definition: BPFTargetMachine.h:20
std
Definition: BitVector.h:851
llvm::TargetPassConfig::addMachineSSAOptimization
virtual void addMachineSSAOptimization()
addMachineSSAOptimization - Add standard passes that optimize machine instructions in SSA form.
Definition: TargetPassConfig.cpp:1308
llvm::BPFSubtarget::getHasAlu32
bool getHasAlu32() const
Definition: BPFSubtarget.h:73
llvm::OptimizationLevel
Definition: OptimizationLevel.h:22
PassManager.h
llvm::ARCCC::Z
@ Z
Definition: ARCInfo.h:41
llvm::LLVMTargetMachine
This class describes a target machine that is implemented with the LLVM target-independent code gener...
Definition: TargetMachine.h:408
llvm::createBPFMIPeepholePass
FunctionPass * createBPFMIPeepholePass()
SimplifyCFGOptions.h
DisableMIPeephole
static cl::opt< bool > DisableMIPeephole("disable-bpf-peephole", cl::Hidden, cl::desc("Disable machine peepholes for BPF"))
llvm::SimplifyCFGOptions
Definition: SimplifyCFGOptions.h:23
llvm::initializeBPFMIPeepholeTruncElimPass
void initializeBPFMIPeepholeTruncElimPass(PassRegistry &)
llvm::getTheBPFTarget
Target & getTheBPFTarget()
Definition: BPFTargetInfo.cpp:22
llvm::TargetMachine::AsmInfo
std::unique_ptr< const MCAsmInfo > AsmInfo
Contains target specific asm information.
Definition: TargetMachine.h:104
llvm::BPFTargetMachine::BPFTargetMachine
BPFTargetMachine(const Target &T, const Triple &TT, StringRef CPU, StringRef FS, const TargetOptions &Options, Optional< Reloc::Model > RM, Optional< CodeModel::Model > CM, CodeGenOpt::Level OL, bool JIT)
Definition: BPFTargetMachine.cpp:64
llvm::legacy::PassManagerBase
PassManagerBase - An abstract interface to allow code to add passes to a pass manager without having ...
Definition: LegacyPassManager.h:39
llvm::BPFTargetMachine::getTargetTransformInfo
TargetTransformInfo getTargetTransformInfo(const Function &F) const override
Get a TargetTransformInfo implementation for the target.
Definition: BPFTargetMachine.cpp:129
TM
const char LLVMTargetMachineRef TM
Definition: PassBuilderBindings.cpp:47
llvm::PassBuilder::registerPeepholeEPCallback
void registerPeepholeEPCallback(const std::function< void(FunctionPassManager &, OptimizationLevel)> &C)
Register a callback for a default optimizer pipeline extension point.
Definition: PassBuilder.h:389
llvm::cl::desc
Definition: CommandLine.h:413
llvm::BPFAdjustOptPass
Definition: BPF.h:71
SimplifyCFG.h
TargetRegistry.h
MPM
ModulePassManager MPM
Definition: PassBuilderBindings.cpp:70
llvm::SimplifyCFGPass
A pass to simplify and canonicalize the CFG of a function.
Definition: SimplifyCFG.h:29
SpecialSubKind::string
@ string
llvm::BPFSubtarget
Definition: BPFSubtarget.h:31
BPFTargetMachine.h
TargetLoweringObjectFileImpl.h