xref: /freebsd/contrib/llvm-project/llvm/lib/Target/BPF/BPFTargetMachine.cpp (revision f126890ac5386406dadf7c4cfa9566cbb56537c5)
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"
16 #include "MCTargetDesc/BPFMCAsmInfo.h"
17 #include "TargetInfo/BPFTargetInfo.h"
18 #include "llvm/CodeGen/Passes.h"
19 #include "llvm/CodeGen/TargetLoweringObjectFileImpl.h"
20 #include "llvm/CodeGen/TargetPassConfig.h"
21 #include "llvm/IR/PassManager.h"
22 #include "llvm/MC/TargetRegistry.h"
23 #include "llvm/Passes/PassBuilder.h"
24 #include "llvm/Support/FormattedStream.h"
25 #include "llvm/Target/TargetOptions.h"
26 #include "llvm/Transforms/Scalar.h"
27 #include "llvm/Transforms/Scalar/SimplifyCFG.h"
28 #include "llvm/Transforms/Utils/SimplifyCFGOptions.h"
29 #include <optional>
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 
36 extern "C" LLVM_EXTERNAL_VISIBILITY void LLVMInitializeBPFTarget() {
37   // Register the target.
38   RegisterTargetMachine<BPFTargetMachine> X(getTheBPFleTarget());
39   RegisterTargetMachine<BPFTargetMachine> Y(getTheBPFbeTarget());
40   RegisterTargetMachine<BPFTargetMachine> Z(getTheBPFTarget());
41 
42   PassRegistry &PR = *PassRegistry::getPassRegistry();
43   initializeBPFCheckAndAdjustIRPass(PR);
44   initializeBPFMIPeepholePass(PR);
45   initializeBPFMIPeepholeTruncElimPass(PR);
46   initializeBPFDAGToDAGISelPass(PR);
47 }
48 
49 // DataLayout: little or big endian
50 static std::string computeDataLayout(const Triple &TT) {
51   if (TT.getArch() == Triple::bpfeb)
52     return "E-m:e-p:64:64-i64:64-i128:128-n32:64-S128";
53   else
54     return "e-m:e-p:64:64-i64:64-i128:128-n32:64-S128";
55 }
56 
57 static Reloc::Model getEffectiveRelocModel(std::optional<Reloc::Model> RM) {
58   return RM.value_or(Reloc::PIC_);
59 }
60 
61 BPFTargetMachine::BPFTargetMachine(const Target &T, const Triple &TT,
62                                    StringRef CPU, StringRef FS,
63                                    const TargetOptions &Options,
64                                    std::optional<Reloc::Model> RM,
65                                    std::optional<CodeModel::Model> CM,
66                                    CodeGenOpt::Level OL, bool JIT)
67     : LLVMTargetMachine(T, computeDataLayout(TT), TT, CPU, FS, Options,
68                         getEffectiveRelocModel(RM),
69                         getEffectiveCodeModel(CM, CodeModel::Small), OL),
70       TLOF(std::make_unique<TargetLoweringObjectFileELF>()),
71       Subtarget(TT, std::string(CPU), std::string(FS), *this) {
72   initAsmInfo();
73 
74   BPFMCAsmInfo *MAI =
75       static_cast<BPFMCAsmInfo *>(const_cast<MCAsmInfo *>(AsmInfo.get()));
76   MAI->setDwarfUsesRelocationsAcrossSections(!Subtarget.getUseDwarfRIS());
77 }
78 
79 namespace {
80 // BPF Code Generator Pass Configuration Options.
81 class BPFPassConfig : public TargetPassConfig {
82 public:
83   BPFPassConfig(BPFTargetMachine &TM, PassManagerBase &PM)
84       : TargetPassConfig(TM, PM) {}
85 
86   BPFTargetMachine &getBPFTargetMachine() const {
87     return getTM<BPFTargetMachine>();
88   }
89 
90   void addIRPasses() override;
91   bool addInstSelector() override;
92   void addMachineSSAOptimization() override;
93   void addPreEmitPass() override;
94 };
95 }
96 
97 TargetPassConfig *BPFTargetMachine::createPassConfig(PassManagerBase &PM) {
98   return new BPFPassConfig(*this, PM);
99 }
100 
101 void BPFTargetMachine::registerPassBuilderCallbacks(PassBuilder &PB) {
102   PB.registerPipelineParsingCallback(
103       [](StringRef PassName, FunctionPassManager &FPM,
104          ArrayRef<PassBuilder::PipelineElement>) {
105         if (PassName == "bpf-ir-peephole") {
106           FPM.addPass(BPFIRPeepholePass());
107           return true;
108         }
109         return false;
110       });
111   PB.registerPipelineStartEPCallback(
112       [=](ModulePassManager &MPM, OptimizationLevel) {
113         FunctionPassManager FPM;
114         FPM.addPass(BPFAbstractMemberAccessPass(this));
115         FPM.addPass(BPFPreserveDITypePass());
116         FPM.addPass(BPFIRPeepholePass());
117         MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM)));
118       });
119   PB.registerPeepholeEPCallback([=](FunctionPassManager &FPM,
120                                     OptimizationLevel Level) {
121     FPM.addPass(SimplifyCFGPass(SimplifyCFGOptions().hoistCommonInsts(true)));
122   });
123   PB.registerPipelineEarlySimplificationEPCallback(
124       [=](ModulePassManager &MPM, OptimizationLevel) {
125         MPM.addPass(BPFAdjustOptPass());
126       });
127 }
128 
129 void BPFPassConfig::addIRPasses() {
130   addPass(createBPFCheckAndAdjustIR());
131   TargetPassConfig::addIRPasses();
132 }
133 
134 TargetTransformInfo
135 BPFTargetMachine::getTargetTransformInfo(const Function &F) const {
136   return TargetTransformInfo(BPFTTIImpl(this, F));
137 }
138 
139 // Install an instruction selector pass using
140 // the ISelDag to gen BPF code.
141 bool BPFPassConfig::addInstSelector() {
142   addPass(createBPFISelDag(getBPFTargetMachine()));
143 
144   return false;
145 }
146 
147 void BPFPassConfig::addMachineSSAOptimization() {
148   addPass(createBPFMISimplifyPatchablePass());
149 
150   // The default implementation must be called first as we want eBPF
151   // Peephole ran at last.
152   TargetPassConfig::addMachineSSAOptimization();
153 
154   const BPFSubtarget *Subtarget = getBPFTargetMachine().getSubtargetImpl();
155   if (!DisableMIPeephole) {
156     if (Subtarget->getHasAlu32())
157       addPass(createBPFMIPeepholePass());
158     addPass(createBPFMIPeepholeTruncElimPass());
159   }
160 }
161 
162 void BPFPassConfig::addPreEmitPass() {
163   addPass(createBPFMIPreEmitCheckingPass());
164   if (getOptLevel() != CodeGenOpt::None)
165     if (!DisableMIPeephole)
166       addPass(createBPFMIPreEmitPeepholePass());
167 }
168