| 1 | //===-- BPFSubtarget.cpp - BPF Subtarget Information ----------------------===// |
| 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 | // This file implements the BPF specific subclass of TargetSubtargetInfo. |
| 10 | // |
| 11 | //===----------------------------------------------------------------------===// |
| 12 | |
| 13 | #include "BPFSubtarget.h" |
| 14 | #include "BPF.h" |
| 15 | #include "BPFTargetMachine.h" |
| 16 | #include "GISel/BPFCallLowering.h" |
| 17 | #include "GISel/BPFLegalizerInfo.h" |
| 18 | #include "GISel/BPFRegisterBankInfo.h" |
| 19 | #include "llvm/MC/TargetRegistry.h" |
| 20 | #include "llvm/TargetParser/Host.h" |
| 21 | |
| 22 | using namespace llvm; |
| 23 | |
| 24 | #define DEBUG_TYPE "bpf-subtarget" |
| 25 | |
| 26 | #define GET_SUBTARGETINFO_TARGET_DESC |
| 27 | #define GET_SUBTARGETINFO_CTOR |
| 28 | #include "BPFGenSubtargetInfo.inc" |
| 29 | |
| 30 | static cl::opt<bool> Disable_ldsx("disable-ldsx" , cl::Hidden, cl::init(Val: false), |
| 31 | cl::desc("Disable ldsx insns" )); |
| 32 | static cl::opt<bool> Disable_movsx("disable-movsx" , cl::Hidden, cl::init(Val: false), |
| 33 | cl::desc("Disable movsx insns" )); |
| 34 | static cl::opt<bool> Disable_bswap("disable-bswap" , cl::Hidden, cl::init(Val: false), |
| 35 | cl::desc("Disable bswap insns" )); |
| 36 | static cl::opt<bool> Disable_sdiv_smod("disable-sdiv-smod" , cl::Hidden, |
| 37 | cl::init(Val: false), cl::desc("Disable sdiv/smod insns" )); |
| 38 | static cl::opt<bool> Disable_gotol("disable-gotol" , cl::Hidden, cl::init(Val: false), |
| 39 | cl::desc("Disable gotol insn" )); |
| 40 | static cl::opt<bool> |
| 41 | Disable_StoreImm("disable-storeimm" , cl::Hidden, cl::init(Val: false), |
| 42 | cl::desc("Disable BPF_ST (immediate store) insn" )); |
| 43 | static cl::opt<bool> Disable_load_acq_store_rel( |
| 44 | "disable-load-acq-store-rel" , cl::Hidden, cl::init(Val: false), |
| 45 | cl::desc("Disable load-acquire and store-release insns" )); |
| 46 | |
| 47 | void BPFSubtarget::anchor() {} |
| 48 | |
| 49 | BPFSubtarget &BPFSubtarget::initializeSubtargetDependencies(StringRef CPU, |
| 50 | StringRef FS) { |
| 51 | initializeEnvironment(); |
| 52 | initSubtargetFeatures(CPU, FS); |
| 53 | ParseSubtargetFeatures(CPU, /*TuneCPU*/ CPU, FS); |
| 54 | return *this; |
| 55 | } |
| 56 | |
| 57 | void BPFSubtarget::initializeEnvironment() { |
| 58 | HasJmpExt = false; |
| 59 | HasJmp32 = false; |
| 60 | HasAlu32 = false; |
| 61 | UseDwarfRIS = false; |
| 62 | HasLdsx = false; |
| 63 | HasMovsx = false; |
| 64 | HasBswap = false; |
| 65 | HasSdivSmod = false; |
| 66 | HasGotol = false; |
| 67 | HasStoreImm = false; |
| 68 | HasLoadAcqStoreRel = false; |
| 69 | } |
| 70 | |
| 71 | void BPFSubtarget::initSubtargetFeatures(StringRef CPU, StringRef FS) { |
| 72 | if (CPU.empty()) |
| 73 | CPU = "v3" ; |
| 74 | if (CPU == "probe" ) |
| 75 | CPU = sys::detail::getHostCPUNameForBPF(); |
| 76 | if (CPU == "generic" || CPU == "v1" ) |
| 77 | return; |
| 78 | if (CPU == "v2" ) { |
| 79 | HasJmpExt = true; |
| 80 | return; |
| 81 | } |
| 82 | if (CPU == "v3" ) { |
| 83 | HasJmpExt = true; |
| 84 | HasJmp32 = true; |
| 85 | HasAlu32 = true; |
| 86 | return; |
| 87 | } |
| 88 | if (CPU == "v4" ) { |
| 89 | HasJmpExt = true; |
| 90 | HasJmp32 = true; |
| 91 | HasAlu32 = true; |
| 92 | HasLdsx = !Disable_ldsx; |
| 93 | HasMovsx = !Disable_movsx; |
| 94 | HasBswap = !Disable_bswap; |
| 95 | HasSdivSmod = !Disable_sdiv_smod; |
| 96 | HasGotol = !Disable_gotol; |
| 97 | HasStoreImm = !Disable_StoreImm; |
| 98 | HasLoadAcqStoreRel = !Disable_load_acq_store_rel; |
| 99 | return; |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | BPFSubtarget::BPFSubtarget(const Triple &TT, const std::string &CPU, |
| 104 | const std::string &FS, const TargetMachine &TM) |
| 105 | : BPFGenSubtargetInfo(TT, CPU, /*TuneCPU*/ CPU, FS), |
| 106 | FrameLowering(initializeSubtargetDependencies(CPU, FS)), |
| 107 | TLInfo(TM, *this) { |
| 108 | IsLittleEndian = TT.isLittleEndian(); |
| 109 | |
| 110 | CallLoweringInfo.reset(p: new BPFCallLowering(*getTargetLowering())); |
| 111 | Legalizer.reset(p: new BPFLegalizerInfo(*this)); |
| 112 | auto *RBI = new BPFRegisterBankInfo(*getRegisterInfo()); |
| 113 | RegBankInfo.reset(p: RBI); |
| 114 | |
| 115 | InstSelector.reset(p: createBPFInstructionSelector( |
| 116 | *static_cast<const BPFTargetMachine *>(&TM), *this, *RBI)); |
| 117 | } |
| 118 | |
| 119 | const CallLowering *BPFSubtarget::getCallLowering() const { |
| 120 | return CallLoweringInfo.get(); |
| 121 | } |
| 122 | |
| 123 | InstructionSelector *BPFSubtarget::getInstructionSelector() const { |
| 124 | return InstSelector.get(); |
| 125 | } |
| 126 | |
| 127 | const LegalizerInfo *BPFSubtarget::getLegalizerInfo() const { |
| 128 | return Legalizer.get(); |
| 129 | } |
| 130 | |
| 131 | const RegisterBankInfo *BPFSubtarget::getRegBankInfo() const { |
| 132 | return RegBankInfo.get(); |
| 133 | } |
| 134 | |