1 //===-- BPFTargetMachine.cpp - Define TargetMachine for BPF ---------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // Implements the info about BPF target spec.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "BPFTargetMachine.h"
15 #include "BPF.h"
16 #include "MCTargetDesc/BPFMCAsmInfo.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/Support/FormattedStream.h"
22 #include "llvm/Support/TargetRegistry.h"
23 #include "llvm/Target/TargetOptions.h"
24 using namespace llvm;
25
26 static cl::
27 opt<bool> DisableMIPeephole("disable-bpf-peephole", cl::Hidden,
28 cl::desc("Disable machine peepholes for BPF"));
29
LLVMInitializeBPFTarget()30 extern "C" void LLVMInitializeBPFTarget() {
31 // Register the target.
32 RegisterTargetMachine<BPFTargetMachine> X(getTheBPFleTarget());
33 RegisterTargetMachine<BPFTargetMachine> Y(getTheBPFbeTarget());
34 RegisterTargetMachine<BPFTargetMachine> Z(getTheBPFTarget());
35
36 PassRegistry &PR = *PassRegistry::getPassRegistry();
37 initializeBPFMIPeepholePass(PR);
38 }
39
40 // DataLayout: little or big endian
computeDataLayout(const Triple & TT)41 static std::string computeDataLayout(const Triple &TT) {
42 if (TT.getArch() == Triple::bpfeb)
43 return "E-m:e-p:64:64-i64:64-n32:64-S128";
44 else
45 return "e-m:e-p:64:64-i64:64-n32:64-S128";
46 }
47
getEffectiveRelocModel(Optional<Reloc::Model> RM)48 static Reloc::Model getEffectiveRelocModel(Optional<Reloc::Model> RM) {
49 if (!RM.hasValue())
50 return Reloc::PIC_;
51 return *RM;
52 }
53
getEffectiveCodeModel(Optional<CodeModel::Model> CM)54 static CodeModel::Model getEffectiveCodeModel(Optional<CodeModel::Model> CM) {
55 if (CM)
56 return *CM;
57 return CodeModel::Small;
58 }
59
BPFTargetMachine(const Target & T,const Triple & TT,StringRef CPU,StringRef FS,const TargetOptions & Options,Optional<Reloc::Model> RM,Optional<CodeModel::Model> CM,CodeGenOpt::Level OL,bool JIT)60 BPFTargetMachine::BPFTargetMachine(const Target &T, const Triple &TT,
61 StringRef CPU, StringRef FS,
62 const TargetOptions &Options,
63 Optional<Reloc::Model> RM,
64 Optional<CodeModel::Model> CM,
65 CodeGenOpt::Level OL, bool JIT)
66 : LLVMTargetMachine(T, computeDataLayout(TT), TT, CPU, FS, Options,
67 getEffectiveRelocModel(RM), getEffectiveCodeModel(CM),
68 OL),
69 TLOF(make_unique<TargetLoweringObjectFileELF>()),
70 Subtarget(TT, CPU, FS, *this) {
71 initAsmInfo();
72
73 BPFMCAsmInfo *MAI = static_cast<BPFMCAsmInfo *>(const_cast<MCAsmInfo *>(AsmInfo));
74 MAI->setDwarfUsesRelocationsAcrossSections(!Subtarget.getUseDwarfRIS());
75 }
76 namespace {
77 // BPF Code Generator Pass Configuration Options.
78 class BPFPassConfig : public TargetPassConfig {
79 public:
BPFPassConfig(BPFTargetMachine & TM,PassManagerBase & PM)80 BPFPassConfig(BPFTargetMachine &TM, PassManagerBase &PM)
81 : TargetPassConfig(TM, PM) {}
82
getBPFTargetMachine() const83 BPFTargetMachine &getBPFTargetMachine() const {
84 return getTM<BPFTargetMachine>();
85 }
86
87 bool addInstSelector() override;
88 void addMachineSSAOptimization() override;
89 void addPreEmitPass() override;
90 };
91 }
92
createPassConfig(PassManagerBase & PM)93 TargetPassConfig *BPFTargetMachine::createPassConfig(PassManagerBase &PM) {
94 return new BPFPassConfig(*this, PM);
95 }
96
97 // Install an instruction selector pass using
98 // the ISelDag to gen BPF code.
addInstSelector()99 bool BPFPassConfig::addInstSelector() {
100 addPass(createBPFISelDag(getBPFTargetMachine()));
101
102 return false;
103 }
104
addMachineSSAOptimization()105 void BPFPassConfig::addMachineSSAOptimization() {
106 // The default implementation must be called first as we want eBPF
107 // Peephole ran at last.
108 TargetPassConfig::addMachineSSAOptimization();
109
110 const BPFSubtarget *Subtarget = getBPFTargetMachine().getSubtargetImpl();
111 if (Subtarget->getHasAlu32() && !DisableMIPeephole)
112 addPass(createBPFMIPeepholePass());
113 }
114
addPreEmitPass()115 void BPFPassConfig::addPreEmitPass() {
116 const BPFSubtarget *Subtarget = getBPFTargetMachine().getSubtargetImpl();
117
118 if (getOptLevel() != CodeGenOpt::None)
119 if (Subtarget->getHasAlu32() && !DisableMIPeephole)
120 addPass(createBPFMIPreEmitPeepholePass());
121 }
122