Alexei Starovoitov | e4c8c80 | 2015-01-24 17:51:26 +0000 | [diff] [blame] | 1 | //===-- BPFTargetMachine.cpp - Define TargetMachine for BPF ---------------===// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 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 |
Alexei Starovoitov | e4c8c80 | 2015-01-24 17:51:26 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | // Implements the info about BPF target spec. |
| 10 | // |
| 11 | //===----------------------------------------------------------------------===// |
| 12 | |
Alexei Starovoitov | e4c8c80 | 2015-01-24 17:51:26 +0000 | [diff] [blame] | 13 | #include "BPFTargetMachine.h" |
Chandler Carruth | 6bda14b | 2017-06-06 11:49:48 +0000 | [diff] [blame] | 14 | #include "BPF.h" |
Yonghong Song | 03e1c8b | 2018-03-01 23:04:59 +0000 | [diff] [blame] | 15 | #include "MCTargetDesc/BPFMCAsmInfo.h" |
Alexei Starovoitov | e4c8c80 | 2015-01-24 17:51:26 +0000 | [diff] [blame] | 16 | #include "llvm/CodeGen/Passes.h" |
Chandler Carruth | 6bda14b | 2017-06-06 11:49:48 +0000 | [diff] [blame] | 17 | #include "llvm/CodeGen/TargetLoweringObjectFileImpl.h" |
Matthias Braun | 31d19d4 | 2016-05-10 03:21:59 +0000 | [diff] [blame] | 18 | #include "llvm/CodeGen/TargetPassConfig.h" |
Chandler Carruth | 6bda14b | 2017-06-06 11:49:48 +0000 | [diff] [blame] | 19 | #include "llvm/IR/LegacyPassManager.h" |
Alexei Starovoitov | e4c8c80 | 2015-01-24 17:51:26 +0000 | [diff] [blame] | 20 | #include "llvm/Support/FormattedStream.h" |
| 21 | #include "llvm/Support/TargetRegistry.h" |
| 22 | #include "llvm/Target/TargetOptions.h" |
| 23 | using namespace llvm; |
| 24 | |
Yonghong Song | 60fed1f | 2018-02-23 23:49:32 +0000 | [diff] [blame] | 25 | static cl:: |
| 26 | opt<bool> DisableMIPeephole("disable-bpf-peephole", cl::Hidden, |
| 27 | cl::desc("Disable machine peepholes for BPF")); |
| 28 | |
Alexei Starovoitov | e4c8c80 | 2015-01-24 17:51:26 +0000 | [diff] [blame] | 29 | extern "C" void LLVMInitializeBPFTarget() { |
| 30 | // Register the target. |
Mehdi Amini | f42454b | 2016-10-09 23:00:34 +0000 | [diff] [blame] | 31 | RegisterTargetMachine<BPFTargetMachine> X(getTheBPFleTarget()); |
| 32 | RegisterTargetMachine<BPFTargetMachine> Y(getTheBPFbeTarget()); |
| 33 | RegisterTargetMachine<BPFTargetMachine> Z(getTheBPFTarget()); |
Yonghong Song | 60fed1f | 2018-02-23 23:49:32 +0000 | [diff] [blame] | 34 | |
| 35 | PassRegistry &PR = *PassRegistry::getPassRegistry(); |
| 36 | initializeBPFMIPeepholePass(PR); |
Alexei Starovoitov | e4c8c80 | 2015-01-24 17:51:26 +0000 | [diff] [blame] | 37 | } |
| 38 | |
Alexei Starovoitov | 310dead | 2015-06-04 19:15:05 +0000 | [diff] [blame] | 39 | // DataLayout: little or big endian |
Daniel Sanders | 3e5de88 | 2015-06-11 19:41:26 +0000 | [diff] [blame] | 40 | static std::string computeDataLayout(const Triple &TT) { |
| 41 | if (TT.getArch() == Triple::bpfeb) |
Alexei Starovoitov | 310dead | 2015-06-04 19:15:05 +0000 | [diff] [blame] | 42 | return "E-m:e-p:64:64-i64:64-n32:64-S128"; |
| 43 | else |
| 44 | return "e-m:e-p:64:64-i64:64-n32:64-S128"; |
| 45 | } |
| 46 | |
Rafael Espindola | 8c34dd8 | 2016-05-18 22:04:49 +0000 | [diff] [blame] | 47 | static Reloc::Model getEffectiveRelocModel(Optional<Reloc::Model> RM) { |
| 48 | if (!RM.hasValue()) |
| 49 | return Reloc::PIC_; |
| 50 | return *RM; |
| 51 | } |
| 52 | |
Daniel Sanders | 3e5de88 | 2015-06-11 19:41:26 +0000 | [diff] [blame] | 53 | BPFTargetMachine::BPFTargetMachine(const Target &T, const Triple &TT, |
| 54 | StringRef CPU, StringRef FS, |
| 55 | const TargetOptions &Options, |
Rafael Espindola | 8c34dd8 | 2016-05-18 22:04:49 +0000 | [diff] [blame] | 56 | Optional<Reloc::Model> RM, |
Rafael Espindola | 79e238a | 2017-08-03 02:16:21 +0000 | [diff] [blame] | 57 | Optional<CodeModel::Model> CM, |
| 58 | CodeGenOpt::Level OL, bool JIT) |
Matthias Braun | bb8507e | 2017-10-12 22:57:28 +0000 | [diff] [blame] | 59 | : LLVMTargetMachine(T, computeDataLayout(TT), TT, CPU, FS, Options, |
David Green | ca29c27 | 2018-12-07 12:10:23 +0000 | [diff] [blame] | 60 | getEffectiveRelocModel(RM), |
| 61 | getEffectiveCodeModel(CM, CodeModel::Small), OL), |
Alexei Starovoitov | e4c8c80 | 2015-01-24 17:51:26 +0000 | [diff] [blame] | 62 | TLOF(make_unique<TargetLoweringObjectFileELF>()), |
Daniel Sanders | c81f450 | 2015-06-16 15:44:21 +0000 | [diff] [blame] | 63 | Subtarget(TT, CPU, FS, *this) { |
Alexei Starovoitov | e4c8c80 | 2015-01-24 17:51:26 +0000 | [diff] [blame] | 64 | initAsmInfo(); |
Yonghong Song | 03e1c8b | 2018-03-01 23:04:59 +0000 | [diff] [blame] | 65 | |
Fangrui Song | 10a2162 | 2018-09-25 06:19:31 +0000 | [diff] [blame] | 66 | BPFMCAsmInfo *MAI = |
| 67 | static_cast<BPFMCAsmInfo *>(const_cast<MCAsmInfo *>(AsmInfo.get())); |
Yonghong Song | 03e1c8b | 2018-03-01 23:04:59 +0000 | [diff] [blame] | 68 | MAI->setDwarfUsesRelocationsAcrossSections(!Subtarget.getUseDwarfRIS()); |
Alexei Starovoitov | e4c8c80 | 2015-01-24 17:51:26 +0000 | [diff] [blame] | 69 | } |
| 70 | namespace { |
| 71 | // BPF Code Generator Pass Configuration Options. |
| 72 | class BPFPassConfig : public TargetPassConfig { |
| 73 | public: |
Matthias Braun | 5e394c3 | 2017-05-30 21:36:41 +0000 | [diff] [blame] | 74 | BPFPassConfig(BPFTargetMachine &TM, PassManagerBase &PM) |
Alexei Starovoitov | e4c8c80 | 2015-01-24 17:51:26 +0000 | [diff] [blame] | 75 | : TargetPassConfig(TM, PM) {} |
| 76 | |
| 77 | BPFTargetMachine &getBPFTargetMachine() const { |
| 78 | return getTM<BPFTargetMachine>(); |
| 79 | } |
| 80 | |
| 81 | bool addInstSelector() override; |
Yonghong Song | 60fed1f | 2018-02-23 23:49:32 +0000 | [diff] [blame] | 82 | void addMachineSSAOptimization() override; |
Yonghong Song | e91802f | 2018-03-13 06:47:06 +0000 | [diff] [blame] | 83 | void addPreEmitPass() override; |
Alexei Starovoitov | e4c8c80 | 2015-01-24 17:51:26 +0000 | [diff] [blame] | 84 | }; |
Alexander Kornienko | f00654e | 2015-06-23 09:49:53 +0000 | [diff] [blame] | 85 | } |
Alexei Starovoitov | e4c8c80 | 2015-01-24 17:51:26 +0000 | [diff] [blame] | 86 | |
| 87 | TargetPassConfig *BPFTargetMachine::createPassConfig(PassManagerBase &PM) { |
Matthias Braun | 5e394c3 | 2017-05-30 21:36:41 +0000 | [diff] [blame] | 88 | return new BPFPassConfig(*this, PM); |
Alexei Starovoitov | e4c8c80 | 2015-01-24 17:51:26 +0000 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | // Install an instruction selector pass using |
| 92 | // the ISelDag to gen BPF code. |
| 93 | bool BPFPassConfig::addInstSelector() { |
| 94 | addPass(createBPFISelDag(getBPFTargetMachine())); |
| 95 | |
| 96 | return false; |
| 97 | } |
Yonghong Song | 60fed1f | 2018-02-23 23:49:32 +0000 | [diff] [blame] | 98 | |
| 99 | void BPFPassConfig::addMachineSSAOptimization() { |
| 100 | // The default implementation must be called first as we want eBPF |
| 101 | // Peephole ran at last. |
| 102 | TargetPassConfig::addMachineSSAOptimization(); |
| 103 | |
| 104 | const BPFSubtarget *Subtarget = getBPFTargetMachine().getSubtargetImpl(); |
| 105 | if (Subtarget->getHasAlu32() && !DisableMIPeephole) |
| 106 | addPass(createBPFMIPeepholePass()); |
| 107 | } |
Yonghong Song | e91802f | 2018-03-13 06:47:06 +0000 | [diff] [blame] | 108 | |
| 109 | void BPFPassConfig::addPreEmitPass() { |
| 110 | const BPFSubtarget *Subtarget = getBPFTargetMachine().getSubtargetImpl(); |
| 111 | |
Yonghong Song | 150ca51 | 2018-09-20 22:24:27 +0000 | [diff] [blame] | 112 | addPass(createBPFMIPreEmitCheckingPass()); |
Yonghong Song | e91802f | 2018-03-13 06:47:06 +0000 | [diff] [blame] | 113 | if (getOptLevel() != CodeGenOpt::None) |
| 114 | if (Subtarget->getHasAlu32() && !DisableMIPeephole) |
| 115 | addPass(createBPFMIPreEmitPeepholePass()); |
| 116 | } |