xref: /freebsd/contrib/llvm-project/llvm/lib/Target/BPF/BPFTargetMachine.cpp (revision 1db9f3b21e39176dd5b67cf8ac378633b172463e)
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/GlobalISel/IRTranslator.h"
19 #include "llvm/CodeGen/GlobalISel/InstructionSelect.h"
20 #include "llvm/CodeGen/GlobalISel/Legalizer.h"
21 #include "llvm/CodeGen/GlobalISel/RegBankSelect.h"
22 #include "llvm/CodeGen/Passes.h"
23 #include "llvm/CodeGen/TargetLoweringObjectFileImpl.h"
24 #include "llvm/CodeGen/TargetPassConfig.h"
25 #include "llvm/IR/PassManager.h"
26 #include "llvm/InitializePasses.h"
27 #include "llvm/MC/TargetRegistry.h"
28 #include "llvm/Passes/PassBuilder.h"
29 #include "llvm/Support/FormattedStream.h"
30 #include "llvm/Target/TargetOptions.h"
31 #include "llvm/Transforms/Scalar.h"
32 #include "llvm/Transforms/Scalar/SimplifyCFG.h"
33 #include "llvm/Transforms/Utils/SimplifyCFGOptions.h"
34 #include <optional>
35 using namespace llvm;
36 
37 static cl::
38 opt<bool> DisableMIPeephole("disable-bpf-peephole", cl::Hidden,
39                             cl::desc("Disable machine peepholes for BPF"));
40 
41 extern "C" LLVM_EXTERNAL_VISIBILITY void LLVMInitializeBPFTarget() {
42   // Register the target.
43   RegisterTargetMachine<BPFTargetMachine> X(getTheBPFleTarget());
44   RegisterTargetMachine<BPFTargetMachine> Y(getTheBPFbeTarget());
45   RegisterTargetMachine<BPFTargetMachine> Z(getTheBPFTarget());
46 
47   PassRegistry &PR = *PassRegistry::getPassRegistry();
48   initializeGlobalISel(PR);
49   initializeBPFCheckAndAdjustIRPass(PR);
50   initializeBPFMIPeepholePass(PR);
51   initializeBPFDAGToDAGISelPass(PR);
52 }
53 
54 // DataLayout: little or big endian
55 static std::string computeDataLayout(const Triple &TT) {
56   if (TT.getArch() == Triple::bpfeb)
57     return "E-m:e-p:64:64-i64:64-i128:128-n32:64-S128";
58   else
59     return "e-m:e-p:64:64-i64:64-i128:128-n32:64-S128";
60 }
61 
62 static Reloc::Model getEffectiveRelocModel(std::optional<Reloc::Model> RM) {
63   return RM.value_or(Reloc::PIC_);
64 }
65 
66 BPFTargetMachine::BPFTargetMachine(const Target &T, const Triple &TT,
67                                    StringRef CPU, StringRef FS,
68                                    const TargetOptions &Options,
69                                    std::optional<Reloc::Model> RM,
70                                    std::optional<CodeModel::Model> CM,
71                                    CodeGenOptLevel OL, bool JIT)
72     : LLVMTargetMachine(T, computeDataLayout(TT), TT, CPU, FS, Options,
73                         getEffectiveRelocModel(RM),
74                         getEffectiveCodeModel(CM, CodeModel::Small), OL),
75       TLOF(std::make_unique<TargetLoweringObjectFileELF>()),
76       Subtarget(TT, std::string(CPU), std::string(FS), *this) {
77   initAsmInfo();
78 
79   BPFMCAsmInfo *MAI =
80       static_cast<BPFMCAsmInfo *>(const_cast<MCAsmInfo *>(AsmInfo.get()));
81   MAI->setDwarfUsesRelocationsAcrossSections(!Subtarget.getUseDwarfRIS());
82 }
83 
84 namespace {
85 // BPF Code Generator Pass Configuration Options.
86 class BPFPassConfig : public TargetPassConfig {
87 public:
88   BPFPassConfig(BPFTargetMachine &TM, PassManagerBase &PM)
89       : TargetPassConfig(TM, PM) {}
90 
91   BPFTargetMachine &getBPFTargetMachine() const {
92     return getTM<BPFTargetMachine>();
93   }
94 
95   void addIRPasses() override;
96   bool addInstSelector() override;
97   void addMachineSSAOptimization() override;
98   void addPreEmitPass() override;
99 
100   bool addIRTranslator() override;
101   bool addLegalizeMachineIR() override;
102   bool addRegBankSelect() override;
103   bool addGlobalInstructionSelect() override;
104 };
105 }
106 
107 TargetPassConfig *BPFTargetMachine::createPassConfig(PassManagerBase &PM) {
108   return new BPFPassConfig(*this, PM);
109 }
110 
111 void BPFTargetMachine::registerPassBuilderCallbacks(
112     PassBuilder &PB, bool PopulateClassToPassNames) {
113   PB.registerPipelineParsingCallback(
114       [](StringRef PassName, FunctionPassManager &FPM,
115          ArrayRef<PassBuilder::PipelineElement>) {
116         if (PassName == "bpf-ir-peephole") {
117           FPM.addPass(BPFIRPeepholePass());
118           return true;
119         }
120         if (PassName == "bpf-preserve-static-offset") {
121           FPM.addPass(BPFPreserveStaticOffsetPass(false));
122           return true;
123         }
124         return false;
125       });
126   PB.registerPipelineStartEPCallback(
127       [=](ModulePassManager &MPM, OptimizationLevel) {
128         FunctionPassManager FPM;
129         FPM.addPass(BPFPreserveStaticOffsetPass(true));
130         FPM.addPass(BPFAbstractMemberAccessPass(this));
131         FPM.addPass(BPFPreserveDITypePass());
132         FPM.addPass(BPFIRPeepholePass());
133         MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM)));
134       });
135   PB.registerPeepholeEPCallback([=](FunctionPassManager &FPM,
136                                     OptimizationLevel Level) {
137     FPM.addPass(SimplifyCFGPass(SimplifyCFGOptions().hoistCommonInsts(true)));
138   });
139   PB.registerScalarOptimizerLateEPCallback(
140       [=](FunctionPassManager &FPM, OptimizationLevel Level) {
141         // Run this after loop unrolling but before
142         // SimplifyCFGPass(... .sinkCommonInsts(true))
143         FPM.addPass(BPFPreserveStaticOffsetPass(false));
144       });
145   PB.registerPipelineEarlySimplificationEPCallback(
146       [=](ModulePassManager &MPM, OptimizationLevel) {
147         MPM.addPass(BPFAdjustOptPass());
148       });
149 }
150 
151 void BPFPassConfig::addIRPasses() {
152   addPass(createAtomicExpandPass());
153   addPass(createBPFCheckAndAdjustIR());
154 
155   TargetPassConfig::addIRPasses();
156 }
157 
158 TargetTransformInfo
159 BPFTargetMachine::getTargetTransformInfo(const Function &F) const {
160   return TargetTransformInfo(BPFTTIImpl(this, F));
161 }
162 
163 // Install an instruction selector pass using
164 // the ISelDag to gen BPF code.
165 bool BPFPassConfig::addInstSelector() {
166   addPass(createBPFISelDag(getBPFTargetMachine()));
167 
168   return false;
169 }
170 
171 void BPFPassConfig::addMachineSSAOptimization() {
172   addPass(createBPFMISimplifyPatchablePass());
173 
174   // The default implementation must be called first as we want eBPF
175   // Peephole ran at last.
176   TargetPassConfig::addMachineSSAOptimization();
177 
178   const BPFSubtarget *Subtarget = getBPFTargetMachine().getSubtargetImpl();
179   if (!DisableMIPeephole) {
180     if (Subtarget->getHasAlu32())
181       addPass(createBPFMIPeepholePass());
182   }
183 }
184 
185 void BPFPassConfig::addPreEmitPass() {
186   addPass(createBPFMIPreEmitCheckingPass());
187   if (getOptLevel() != CodeGenOptLevel::None)
188     if (!DisableMIPeephole)
189       addPass(createBPFMIPreEmitPeepholePass());
190 }
191 
192 bool BPFPassConfig::addIRTranslator() {
193   addPass(new IRTranslator());
194   return false;
195 }
196 
197 bool BPFPassConfig::addLegalizeMachineIR() {
198   addPass(new Legalizer());
199   return false;
200 }
201 
202 bool BPFPassConfig::addRegBankSelect() {
203   addPass(new RegBankSelect());
204   return false;
205 }
206 
207 bool BPFPassConfig::addGlobalInstructionSelect() {
208   addPass(new InstructionSelect(getOptLevel()));
209   return false;
210 }
211