LLVM  14.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"
28 #include "llvm/Transforms/Scalar.h"
31 using namespace llvm;
32 
33 static cl::
34 opt<bool> DisableMIPeephole("disable-bpf-peephole", cl::Hidden,
35  cl::desc("Disable machine peepholes for BPF"));
36 
38  // Register the target.
42 
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.getValueOr(Reloc::PIC_);
62 }
63 
65  StringRef CPU, StringRef FS,
66  const TargetOptions &Options,
69  CodeGenOpt::Level OL, bool JIT)
72  getEffectiveCodeModel(CM, CodeModel::Small), OL),
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 
105  Builder.addExtension(
107  [&](const PassManagerBuilder &, legacy::PassManagerBase &PM) {
110  });
111 
112  Builder.addExtension(
114  [&](const PassManagerBuilder &, legacy::PassManagerBase &PM) {
116  SimplifyCFGOptions().hoistCommonInsts(true)));
117  });
118  Builder.addExtension(
120  [&](const PassManagerBuilder &, legacy::PassManagerBase &PM) {
121  PM.add(createBPFAdjustOpt());
122  });
123 }
124 
132  });
135  FPM.addPass(SimplifyCFGPass(SimplifyCFGOptions().hoistCommonInsts(true)));
136  });
140  });
141 }
142 
143 void BPFPassConfig::addIRPasses() {
144  addPass(createBPFCheckAndAdjustIR());
146 }
147 
150  return TargetTransformInfo(BPFTTIImpl(this, F));
151 }
152 
153 // Install an instruction selector pass using
154 // the ISelDag to gen BPF code.
155 bool BPFPassConfig::addInstSelector() {
156  addPass(createBPFISelDag(getBPFTargetMachine()));
157 
158  return false;
159 }
160 
161 void BPFPassConfig::addMachineSSAOptimization() {
163 
164  // The default implementation must be called first as we want eBPF
165  // Peephole ran at last.
167 
168  const BPFSubtarget *Subtarget = getBPFTargetMachine().getSubtargetImpl();
169  if (!DisableMIPeephole) {
170  if (Subtarget->getHasAlu32())
171  addPass(createBPFMIPeepholePass());
173  }
174 }
175 
176 void BPFPassConfig::addPreEmitPass() {
178  if (getOptLevel() != CodeGenOpt::None)
179  if (!DisableMIPeephole)
181 }
llvm::getTheBPFleTarget
Target & getTheBPFleTarget()
Definition: BPFTargetInfo.cpp:14
llvm
---------------------— PointerInfo ------------------------------------—
Definition: AllocatorList.h:23
PassBuilder.h
llvm::BPFTTIImpl
Definition: BPFTargetTransformInfo.h:24
llvm::Triple::bpfeb
@ bpfeb
Definition: Triple.h:58
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:442
llvm::createBPFCheckAndAdjustIR
ModulePass * createBPFCheckAndAdjustIR()
llvm::TargetOptions
Definition: TargetOptions.h:124
Scalar.h
llvm::Function
Definition: Function.h:61
llvm::BPFAbstractMemberAccessPass
Definition: BPF.h:42
llvm::createCFGSimplificationPass
FunctionPass * createCFGSimplificationPass(SimplifyCFGOptions Options=SimplifyCFGOptions(), std::function< bool(const Function &)> Ftor=nullptr)
Definition: SimplifyCFGPass.cpp:415
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:137
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:168
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:45
llvm::BPFPreserveDITypePass
Definition: BPF.h:53
llvm::cl::Hidden
@ Hidden
Definition: CommandLine.h:143
llvm::createBPFPreserveDIType
FunctionPass * createBPFPreserveDIType()
llvm::TargetLoweringObjectFileELF
Definition: TargetLoweringObjectFileImpl.h:33
llvm::Optional< Reloc::Model >
T
#define T
Definition: Mips16ISelLowering.cpp:341
llvm::createBPFMIPreEmitPeepholePass
FunctionPass * createBPFMIPreEmitPeepholePass()
llvm::BPFMCAsmInfo
Definition: BPFMCAsmInfo.h:21
BPF.h
llvm::BPFTargetMachine::getTargetTransformInfo
TargetTransformInfo getTargetTransformInfo(const Function &F) override
Get a TargetTransformInfo implementation for the target.
Definition: BPFTargetMachine.cpp:149
LegacyPassManager.h
PassManagerBuilder.h
llvm::BPFTargetMachine::registerPassBuilderCallbacks
void registerPassBuilderCallbacks(PassBuilder &PB) override
Allow the target to modify the pass pipeline with New Pass Manager (similar to adjustPassManager for ...
Definition: BPFTargetMachine.cpp:125
llvm::createBPFAdjustOpt
ModulePass * createBPFAdjustOpt()
F
#define F(x, y, z)
Definition: MD5.cpp:56
BPFTargetInfo.h
llvm::initializeBPFAdjustOptPass
void initializeBPFAdjustOptPass(PassRegistry &)
llvm::Reloc::Model
Model
Definition: CodeGen.h:22
llvm::createBPFMIPreEmitCheckingPass
FunctionPass * createBPFMIPreEmitCheckingPass()
FormattedStream.h
BPFTargetTransformInfo.h
llvm::PassRegistry::getPassRegistry
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
Definition: PassRegistry.cpp:31
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:84
llvm::CodeModel::Small
@ Small
Definition: CodeGen.h:28
llvm::createModuleToFunctionPassAdaptor
ModuleToFunctionPassAdaptor createModuleToFunctionPassAdaptor(FunctionPassT &&Pass)
A function to deduce a function pass type and wrap it in the templated adaptor.
Definition: PassManager.h:1221
llvm::initializeBPFMIPeepholePass
void initializeBPFMIPeepholePass(PassRegistry &)
llvm::RegisterTargetMachine
RegisterTargetMachine - Helper template for registering a target machine implementation,...
Definition: TargetRegistry.h:1275
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:37
llvm::initializeBPFPreserveDITypePass
void initializeBPFPreserveDITypePass(PassRegistry &)
X
static GCMetadataPrinterRegistry::Add< ErlangGCPrinter > X("erlang", "erlang-compatible garbage collector")
llvm::legacy::PassManagerBase::add
virtual void add(Pass *P)=0
Add a pass to the queue of passes to run.
llvm::PassManagerBuilder
PassManagerBuilder - This class is used to set up a standard optimization sequence for languages like...
Definition: PassManagerBuilder.h:59
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:451
llvm::cl::opt< bool >
llvm::initializeBPFCheckAndAdjustIRPass
void initializeBPFCheckAndAdjustIRPass(PassRegistry &)
llvm::EngineKind::JIT
@ JIT
Definition: ExecutionEngine.h:524
LLVM_EXTERNAL_VISIBILITY
#define LLVM_EXTERNAL_VISIBILITY
Definition: Compiler.h:132
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:850
TargetPassConfig.h
llvm::getTheBPFbeTarget
Target & getTheBPFbeTarget()
Definition: BPFTargetInfo.cpp:18
llvm::X86AS::FS
@ FS
Definition: X86.h:188
Builder
assume Assume Builder
Definition: AssumeBundleBuilder.cpp:650
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:472
llvm::BPFMCAsmInfo::setDwarfUsesRelocationsAcrossSections
void setDwarfUsesRelocationsAcrossSections(bool enable)
Definition: BPFMCAsmInfo.h:48
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:58
this
Analysis the ScalarEvolution expression for r is this
Definition: README.txt:8
llvm::createBPFAbstractMemberAccess
FunctionPass * createBPFAbstractMemberAccess(BPFTargetMachine *TM)
llvm::PassManagerBuilder::EP_Peephole
@ EP_Peephole
EP_Peephole - This extension point allows adding passes that perform peephole optimizations similar t...
Definition: PassManagerBuilder.h:104
llvm::PICLevel::Level
Level
Definition: CodeGen.h:33
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:481
llvm::LLVMTargetMachine::initAsmInfo
void initAsmInfo()
Definition: LLVMTargetMachine.cpp:41
llvm::BPFTargetMachine::adjustPassManager
void adjustPassManager(PassManagerBuilder &) override
Allow the target to modify the pass manager, e.g.
Definition: BPFTargetMachine.cpp:104
llvm::PassManager< Module >
llvm::BPFTargetMachine
Definition: BPFTargetMachine.h:20
std
Definition: BitVector.h:838
llvm::TargetPassConfig::addMachineSSAOptimization
virtual void addMachineSSAOptimization()
addMachineSSAOptimization - Add standard passes that optimize machine instructions in SSA form.
Definition: TargetPassConfig.cpp:1287
llvm::BPFSubtarget::getHasAlu32
bool getHasAlu32() const
Definition: BPFSubtarget.h:73
llvm::PassManagerBuilder::EP_ModuleOptimizerEarly
@ EP_ModuleOptimizerEarly
EP_ModuleOptimizerEarly - This extension point allows adding passes just before the main module-level...
Definition: PassManagerBuilder.h:76
llvm::OptimizationLevel
Definition: OptimizationLevel.h:22
PassManager.h
llvm::LLVMTargetMachine
This class describes a target machine that is implemented with the LLVM target-independent code gener...
Definition: TargetMachine.h:393
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::PassManagerBuilder::EP_EarlyAsPossible
@ EP_EarlyAsPossible
EP_EarlyAsPossible - This extension point allows adding passes before any other transformations,...
Definition: PassManagerBuilder.h:72
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:107
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
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:376
llvm::cl::desc
Definition: CommandLine.h:414
llvm::PassManager::addPass
std::enable_if_t<!std::is_same< PassT, PassManager >::value > addPass(PassT &&Pass)
Definition: PassManager.h:552
llvm::BPFAdjustOptPass
Definition: BPF.h:60
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