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"
23 #include "llvm/MC/TargetRegistry.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 
51 }
52 
53 // DataLayout: little or big endian
54 static 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 
62  return RM.getValueOr(Reloc::PIC_);
63 }
64 
66  StringRef CPU, StringRef FS,
67  const TargetOptions &Options,
70  CodeGenOpt::Level OL, bool JIT)
74  TLOF(std::make_unique<TargetLoweringObjectFileELF>()),
75  Subtarget(TT, std::string(CPU), std::string(FS), *this) {
76  initAsmInfo();
77 
78  BPFMCAsmInfo *MAI =
79  static_cast<BPFMCAsmInfo *>(const_cast<MCAsmInfo *>(AsmInfo.get()));
81 }
82 
83 namespace {
84 // BPF Code Generator Pass Configuration Options.
85 class BPFPassConfig : public TargetPassConfig {
86 public:
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 
106  Builder.addExtension(
108  [&](const PassManagerBuilder &, legacy::PassManagerBase &PM) {
111  PM.add(createBPFIRPeephole());
112  });
113 
114  Builder.addExtension(
116  [&](const PassManagerBuilder &, legacy::PassManagerBase &PM) {
118  SimplifyCFGOptions().hoistCommonInsts(true)));
119  });
120  Builder.addExtension(
122  [&](const PassManagerBuilder &, legacy::PassManagerBase &PM) {
123  PM.add(createBPFAdjustOpt());
124  });
125 }
126 
133  FPM.addPass(BPFIRPeepholePass());
135  });
138  FPM.addPass(SimplifyCFGPass(SimplifyCFGOptions().hoistCommonInsts(true)));
139  });
143  });
144 }
145 
146 void BPFPassConfig::addIRPasses() {
147  addPass(createBPFCheckAndAdjustIR());
149 }
150 
153  return TargetTransformInfo(BPFTTIImpl(this, F));
154 }
155 
156 // Install an instruction selector pass using
157 // the ISelDag to gen BPF code.
158 bool BPFPassConfig::addInstSelector() {
159  addPass(createBPFISelDag(getBPFTargetMachine()));
160 
161  return false;
162 }
163 
164 void BPFPassConfig::addMachineSSAOptimization() {
166 
167  // The default implementation must be called first as we want eBPF
168  // Peephole ran at last.
170 
171  const BPFSubtarget *Subtarget = getBPFTargetMachine().getSubtargetImpl();
172  if (!DisableMIPeephole) {
173  if (Subtarget->getHasAlu32())
174  addPass(createBPFMIPeepholePass());
176  }
177 }
178 
179 void BPFPassConfig::addPreEmitPass() {
181  if (getOptLevel() != CodeGenOpt::None)
182  if (!DisableMIPeephole)
184 }
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: AllocatorList.h:23
PassBuilder.h
llvm::BPFTTIImpl
Definition: BPFTargetTransformInfo.h:24
llvm::Triple::bpfeb
@ bpfeb
Definition: Triple.h:58
llvm::X86AS::FS
@ FS
Definition: X86.h:188
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:457
llvm::createBPFCheckAndAdjustIR
ModulePass * createBPFCheckAndAdjustIR()
llvm::TargetOptions
Definition: TargetOptions.h:124
Scalar.h
T
llvm::Function
Definition: Function.h:62
llvm::PassManager::addPass
LLVM_ATTRIBUTE_MINSIZE std::enable_if_t<!std::is_same< PassT, PassManager >::value > addPass(PassT &&Pass)
Definition: PassManager.h:553
llvm::BPFAbstractMemberAccessPass
Definition: BPF.h:44
llvm::createCFGSimplificationPass
FunctionPass * createCFGSimplificationPass(SimplifyCFGOptions Options=SimplifyCFGOptions(), std::function< bool(const Function &)> Ftor=nullptr)
Definition: SimplifyCFGPass.cpp:418
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:169
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:101
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:55
llvm::cl::Hidden
@ Hidden
Definition: CommandLine.h:143
llvm::createBPFPreserveDIType
FunctionPass * createBPFPreserveDIType()
llvm::TargetLoweringObjectFileELF
Definition: TargetLoweringObjectFileImpl.h:33
llvm::Optional< Reloc::Model >
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:152
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:127
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::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:31
computeDataLayout
static std::string computeDataLayout(const Triple &TT)
Definition: BPFTargetMachine.cpp:54
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:94
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: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:466
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:1227
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:844
TargetPassConfig.h
llvm::getTheBPFbeTarget
Target & getTheBPFbeTarget()
Definition: BPFTargetInfo.cpp:18
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:478
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:57
this
Analysis the ScalarEvolution expression for r is this
Definition: README.txt:8
llvm::BPFIRPeepholePass
Definition: BPF.h:62
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:498
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:105
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:1277
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:406
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:108
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:65
llvm::createBPFIRPeephole
FunctionPass * createBPFIRPeephole()
Definition: BPFIRPeephole.cpp:110
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:391
llvm::cl::desc
Definition: CommandLine.h:412
llvm::BPFAdjustOptPass
Definition: BPF.h:69
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