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