1 //===-- BPFTargetMachine.cpp - Define TargetMachine for BPF ---------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // Implements the info about BPF target spec.
12 //===----------------------------------------------------------------------===//
14 #include "BPFTargetMachine.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"
27 opt<bool> DisableMIPeephole("disable-bpf-peephole", cl::Hidden,
28 cl::desc("Disable machine peepholes for BPF"));
30 extern "C" void LLVMInitializeBPFTarget() {
31 // Register the target.
32 RegisterTargetMachine<BPFTargetMachine> X(getTheBPFleTarget());
33 RegisterTargetMachine<BPFTargetMachine> Y(getTheBPFbeTarget());
34 RegisterTargetMachine<BPFTargetMachine> Z(getTheBPFTarget());
36 PassRegistry &PR = *PassRegistry::getPassRegistry();
37 initializeBPFMIPeepholePass(PR);
40 // DataLayout: little or big endian
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";
45 return "e-m:e-p:64:64-i64:64-n32:64-S128";
48 static Reloc::Model getEffectiveRelocModel(Optional<Reloc::Model> RM) {
54 static CodeModel::Model getEffectiveCodeModel(Optional<CodeModel::Model> CM) {
57 return CodeModel::Small;
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),
69 TLOF(make_unique<TargetLoweringObjectFileELF>()),
70 Subtarget(TT, CPU, FS, *this) {
73 BPFMCAsmInfo *MAI = static_cast<BPFMCAsmInfo *>(const_cast<MCAsmInfo *>(AsmInfo));
74 MAI->setDwarfUsesRelocationsAcrossSections(!Subtarget.getUseDwarfRIS());
77 // BPF Code Generator Pass Configuration Options.
78 class BPFPassConfig : public TargetPassConfig {
80 BPFPassConfig(BPFTargetMachine &TM, PassManagerBase &PM)
81 : TargetPassConfig(TM, PM) {}
83 BPFTargetMachine &getBPFTargetMachine() const {
84 return getTM<BPFTargetMachine>();
87 bool addInstSelector() override;
88 void addMachineSSAOptimization() override;
89 void addPreEmitPass() override;
93 TargetPassConfig *BPFTargetMachine::createPassConfig(PassManagerBase &PM) {
94 return new BPFPassConfig(*this, PM);
97 // Install an instruction selector pass using
98 // the ISelDag to gen BPF code.
99 bool BPFPassConfig::addInstSelector() {
100 addPass(createBPFISelDag(getBPFTargetMachine()));
105 void BPFPassConfig::addMachineSSAOptimization() {
106 // The default implementation must be called first as we want eBPF
107 // Peephole ran at last.
108 TargetPassConfig::addMachineSSAOptimization();
110 const BPFSubtarget *Subtarget = getBPFTargetMachine().getSubtargetImpl();
111 if (Subtarget->getHasAlu32() && !DisableMIPeephole)
112 addPass(createBPFMIPeepholePass());
115 void BPFPassConfig::addPreEmitPass() {
116 const BPFSubtarget *Subtarget = getBPFTargetMachine().getSubtargetImpl();
118 if (getOptLevel() != CodeGenOpt::None)
119 if (Subtarget->getHasAlu32() && !DisableMIPeephole)
120 addPass(createBPFMIPreEmitPeepholePass());