1 //===-- BPFTargetMachine.cpp - Define TargetMachine for BPF ---------------===//
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
7 //===----------------------------------------------------------------------===//
9 // Implements the info about BPF target spec.
11 //===----------------------------------------------------------------------===//
13 #include "BPFTargetMachine.h"
15 #include "MCTargetDesc/BPFMCAsmInfo.h"
16 #include "llvm/CodeGen/Passes.h"
17 #include "llvm/CodeGen/TargetLoweringObjectFileImpl.h"
18 #include "llvm/CodeGen/TargetPassConfig.h"
19 #include "llvm/IR/LegacyPassManager.h"
20 #include "llvm/Support/FormattedStream.h"
21 #include "llvm/Support/TargetRegistry.h"
22 #include "llvm/Target/TargetOptions.h"
26 opt
<bool> DisableMIPeephole("disable-bpf-peephole", cl::Hidden
,
27 cl::desc("Disable machine peepholes for BPF"));
29 extern "C" void LLVMInitializeBPFTarget() {
30 // Register the target.
31 RegisterTargetMachine
<BPFTargetMachine
> X(getTheBPFleTarget());
32 RegisterTargetMachine
<BPFTargetMachine
> Y(getTheBPFbeTarget());
33 RegisterTargetMachine
<BPFTargetMachine
> Z(getTheBPFTarget());
35 PassRegistry
&PR
= *PassRegistry::getPassRegistry();
36 initializeBPFMIPeepholePass(PR
);
39 // DataLayout: little or big endian
40 static std::string
computeDataLayout(const Triple
&TT
) {
41 if (TT
.getArch() == Triple::bpfeb
)
42 return "E-m:e-p:64:64-i64:64-n32:64-S128";
44 return "e-m:e-p:64:64-i64:64-n32:64-S128";
47 static Reloc::Model
getEffectiveRelocModel(Optional
<Reloc::Model
> RM
) {
53 BPFTargetMachine::BPFTargetMachine(const Target
&T
, const Triple
&TT
,
54 StringRef CPU
, StringRef FS
,
55 const TargetOptions
&Options
,
56 Optional
<Reloc::Model
> RM
,
57 Optional
<CodeModel::Model
> CM
,
58 CodeGenOpt::Level OL
, bool JIT
)
59 : LLVMTargetMachine(T
, computeDataLayout(TT
), TT
, CPU
, FS
, Options
,
60 getEffectiveRelocModel(RM
),
61 getEffectiveCodeModel(CM
, CodeModel::Small
), OL
),
62 TLOF(make_unique
<TargetLoweringObjectFileELF
>()),
63 Subtarget(TT
, CPU
, FS
, *this) {
67 static_cast<BPFMCAsmInfo
*>(const_cast<MCAsmInfo
*>(AsmInfo
.get()));
68 MAI
->setDwarfUsesRelocationsAcrossSections(!Subtarget
.getUseDwarfRIS());
71 // BPF Code Generator Pass Configuration Options.
72 class BPFPassConfig
: public TargetPassConfig
{
74 BPFPassConfig(BPFTargetMachine
&TM
, PassManagerBase
&PM
)
75 : TargetPassConfig(TM
, PM
) {}
77 BPFTargetMachine
&getBPFTargetMachine() const {
78 return getTM
<BPFTargetMachine
>();
81 bool addInstSelector() override
;
82 void addMachineSSAOptimization() override
;
83 void addPreEmitPass() override
;
87 TargetPassConfig
*BPFTargetMachine::createPassConfig(PassManagerBase
&PM
) {
88 return new BPFPassConfig(*this, PM
);
91 // Install an instruction selector pass using
92 // the ISelDag to gen BPF code.
93 bool BPFPassConfig::addInstSelector() {
94 addPass(createBPFISelDag(getBPFTargetMachine()));
99 void BPFPassConfig::addMachineSSAOptimization() {
100 // The default implementation must be called first as we want eBPF
101 // Peephole ran at last.
102 TargetPassConfig::addMachineSSAOptimization();
104 const BPFSubtarget
*Subtarget
= getBPFTargetMachine().getSubtargetImpl();
105 if (Subtarget
->getHasAlu32() && !DisableMIPeephole
)
106 addPass(createBPFMIPeepholePass());
109 void BPFPassConfig::addPreEmitPass() {
110 const BPFSubtarget
*Subtarget
= getBPFTargetMachine().getSubtargetImpl();
112 addPass(createBPFMIPreEmitCheckingPass());
113 if (getOptLevel() != CodeGenOpt::None
)
114 if (Subtarget
->getHasAlu32() && !DisableMIPeephole
)
115 addPass(createBPFMIPreEmitPeepholePass());