xref: /freebsd/contrib/llvm-project/llvm/lib/Target/BPF/BPFTargetMachine.cpp (revision 5abaf0866445a61c11665fffc148ecd13a7bb9ac)
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/LegacyPassManager.h"
22 #include "llvm/IR/PassManager.h"
23 #include "llvm/MC/TargetRegistry.h"
24 #include "llvm/Passes/PassBuilder.h"
25 #include "llvm/Support/FormattedStream.h"
26 #include "llvm/Target/TargetOptions.h"
27 #include "llvm/Transforms/IPO/PassManagerBuilder.h"
28 #include "llvm/Transforms/Scalar.h"
29 #include "llvm/Transforms/Scalar/SimplifyCFG.h"
30 #include "llvm/Transforms/Utils/SimplifyCFGOptions.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 
37 extern "C" LLVM_EXTERNAL_VISIBILITY void LLVMInitializeBPFTarget() {
38   // Register the target.
39   RegisterTargetMachine<BPFTargetMachine> X(getTheBPFleTarget());
40   RegisterTargetMachine<BPFTargetMachine> Y(getTheBPFbeTarget());
41   RegisterTargetMachine<BPFTargetMachine> Z(getTheBPFTarget());
42 
43   PassRegistry &PR = *PassRegistry::getPassRegistry();
44   initializeBPFAbstractMemberAccessLegacyPassPass(PR);
45   initializeBPFPreserveDITypePass(PR);
46   initializeBPFIRPeepholePass(PR);
47   initializeBPFAdjustOptPass(PR);
48   initializeBPFCheckAndAdjustIRPass(PR);
49   initializeBPFMIPeepholePass(PR);
50   initializeBPFMIPeepholeTruncElimPass(PR);
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 
61 static Reloc::Model getEffectiveRelocModel(Optional<Reloc::Model> RM) {
62   return RM.value_or(Reloc::PIC_);
63 }
64 
65 BPFTargetMachine::BPFTargetMachine(const Target &T, const Triple &TT,
66                                    StringRef CPU, StringRef FS,
67                                    const TargetOptions &Options,
68                                    Optional<Reloc::Model> RM,
69                                    Optional<CodeModel::Model> CM,
70                                    CodeGenOpt::Level OL, bool JIT)
71     : LLVMTargetMachine(T, computeDataLayout(TT), TT, CPU, FS, Options,
72                         getEffectiveRelocModel(RM),
73                         getEffectiveCodeModel(CM, CodeModel::Small), OL),
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()));
80   MAI->setDwarfUsesRelocationsAcrossSections(!Subtarget.getUseDwarfRIS());
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 
101 TargetPassConfig *BPFTargetMachine::createPassConfig(PassManagerBase &PM) {
102   return new BPFPassConfig(*this, PM);
103 }
104 
105 void BPFTargetMachine::adjustPassManager(PassManagerBuilder &Builder) {
106  Builder.addExtension(
107       PassManagerBuilder::EP_EarlyAsPossible,
108       [&](const PassManagerBuilder &, legacy::PassManagerBase &PM) {
109         PM.add(createBPFAbstractMemberAccess(this));
110         PM.add(createBPFPreserveDIType());
111         PM.add(createBPFIRPeephole());
112       });
113 
114   Builder.addExtension(
115       PassManagerBuilder::EP_Peephole,
116       [&](const PassManagerBuilder &, legacy::PassManagerBase &PM) {
117         PM.add(createCFGSimplificationPass(
118             SimplifyCFGOptions().hoistCommonInsts(true)));
119       });
120   Builder.addExtension(
121       PassManagerBuilder::EP_ModuleOptimizerEarly,
122       [&](const PassManagerBuilder &, legacy::PassManagerBase &PM) {
123         PM.add(createBPFAdjustOpt());
124       });
125 }
126 
127 void BPFTargetMachine::registerPassBuilderCallbacks(PassBuilder &PB) {
128   PB.registerPipelineStartEPCallback(
129       [=](ModulePassManager &MPM, OptimizationLevel) {
130         FunctionPassManager FPM;
131         FPM.addPass(BPFAbstractMemberAccessPass(this));
132         FPM.addPass(BPFPreserveDITypePass());
133         FPM.addPass(BPFIRPeepholePass());
134         MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM)));
135       });
136   PB.registerPeepholeEPCallback([=](FunctionPassManager &FPM,
137                                     OptimizationLevel Level) {
138     FPM.addPass(SimplifyCFGPass(SimplifyCFGOptions().hoistCommonInsts(true)));
139   });
140   PB.registerPipelineEarlySimplificationEPCallback(
141       [=](ModulePassManager &MPM, OptimizationLevel) {
142         MPM.addPass(BPFAdjustOptPass());
143       });
144 }
145 
146 void BPFPassConfig::addIRPasses() {
147   addPass(createBPFCheckAndAdjustIR());
148   TargetPassConfig::addIRPasses();
149 }
150 
151 TargetTransformInfo
152 BPFTargetMachine::getTargetTransformInfo(const Function &F) const {
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() {
165   addPass(createBPFMISimplifyPatchablePass());
166 
167   // The default implementation must be called first as we want eBPF
168   // Peephole ran at last.
169   TargetPassConfig::addMachineSSAOptimization();
170 
171   const BPFSubtarget *Subtarget = getBPFTargetMachine().getSubtargetImpl();
172   if (!DisableMIPeephole) {
173     if (Subtarget->getHasAlu32())
174       addPass(createBPFMIPeepholePass());
175     addPass(createBPFMIPeepholeTruncElimPass());
176   }
177 }
178 
179 void BPFPassConfig::addPreEmitPass() {
180   addPass(createBPFMIPreEmitCheckingPass());
181   if (getOptLevel() != CodeGenOpt::None)
182     if (!DisableMIPeephole)
183       addPass(createBPFMIPreEmitPeepholePass());
184 }
185