xref: /freebsd/contrib/llvm-project/llvm/lib/Target/BPF/BPFTargetMachine.cpp (revision 22d7dd834bc5cd189810e414701e3ad1e98102e4)
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/Scalar.h"
28 #include "llvm/Transforms/Scalar/SimplifyCFG.h"
29 #include "llvm/Transforms/Utils/SimplifyCFGOptions.h"
30 #include <optional>
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   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                                    CodeGenOpt::Level 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 }
101 
102 TargetPassConfig *BPFTargetMachine::createPassConfig(PassManagerBase &PM) {
103   return new BPFPassConfig(*this, PM);
104 }
105 
106 void BPFTargetMachine::registerPassBuilderCallbacks(PassBuilder &PB) {
107   PB.registerPipelineStartEPCallback(
108       [=](ModulePassManager &MPM, OptimizationLevel) {
109         FunctionPassManager FPM;
110         FPM.addPass(BPFAbstractMemberAccessPass(this));
111         FPM.addPass(BPFPreserveDITypePass());
112         FPM.addPass(BPFIRPeepholePass());
113         MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM)));
114       });
115   PB.registerPeepholeEPCallback([=](FunctionPassManager &FPM,
116                                     OptimizationLevel Level) {
117     FPM.addPass(SimplifyCFGPass(SimplifyCFGOptions().hoistCommonInsts(true)));
118   });
119   PB.registerPipelineEarlySimplificationEPCallback(
120       [=](ModulePassManager &MPM, OptimizationLevel) {
121         MPM.addPass(BPFAdjustOptPass());
122       });
123 }
124 
125 void BPFPassConfig::addIRPasses() {
126   addPass(createBPFCheckAndAdjustIR());
127   TargetPassConfig::addIRPasses();
128 }
129 
130 TargetTransformInfo
131 BPFTargetMachine::getTargetTransformInfo(const Function &F) const {
132   return TargetTransformInfo(BPFTTIImpl(this, F));
133 }
134 
135 // Install an instruction selector pass using
136 // the ISelDag to gen BPF code.
137 bool BPFPassConfig::addInstSelector() {
138   addPass(createBPFISelDag(getBPFTargetMachine()));
139 
140   return false;
141 }
142 
143 void BPFPassConfig::addMachineSSAOptimization() {
144   addPass(createBPFMISimplifyPatchablePass());
145 
146   // The default implementation must be called first as we want eBPF
147   // Peephole ran at last.
148   TargetPassConfig::addMachineSSAOptimization();
149 
150   const BPFSubtarget *Subtarget = getBPFTargetMachine().getSubtargetImpl();
151   if (!DisableMIPeephole) {
152     if (Subtarget->getHasAlu32())
153       addPass(createBPFMIPeepholePass());
154     addPass(createBPFMIPeepholeTruncElimPass());
155   }
156 }
157 
158 void BPFPassConfig::addPreEmitPass() {
159   addPass(createBPFMIPreEmitCheckingPass());
160   if (getOptLevel() != CodeGenOpt::None)
161     if (!DisableMIPeephole)
162       addPass(createBPFMIPreEmitPeepholePass());
163 }
164