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
22using 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
30static cl::opt<bool> Disable_ldsx("disable-ldsx", cl::Hidden, cl::init(Val: false),
31 cl::desc("Disable ldsx insns"));
32static cl::opt<bool> Disable_movsx("disable-movsx", cl::Hidden, cl::init(Val: false),
33 cl::desc("Disable movsx insns"));
34static cl::opt<bool> Disable_bswap("disable-bswap", cl::Hidden, cl::init(Val: false),
35 cl::desc("Disable bswap insns"));
36static cl::opt<bool> Disable_sdiv_smod("disable-sdiv-smod", cl::Hidden,
37 cl::init(Val: false), cl::desc("Disable sdiv/smod insns"));
38static cl::opt<bool> Disable_gotol("disable-gotol", cl::Hidden, cl::init(Val: false),
39 cl::desc("Disable gotol insn"));
40static cl::opt<bool>
41 Disable_StoreImm("disable-storeimm", cl::Hidden, cl::init(Val: false),
42 cl::desc("Disable BPF_ST (immediate store) insn"));
43static 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"));
46static cl::opt<bool> Disable_gotox("disable-gotox", cl::Hidden, cl::init(Val: false),
47 cl::desc("Disable gotox insn"));
48static cl::opt<bool>
49 Disable_hmul("disable-hmul", cl::Hidden, cl::init(Val: false),
50 cl::desc("Disable high-half multiply (UHMUL/SHMUL) insns"));
51
52void BPFSubtarget::anchor() {}
53
54BPFSubtarget &BPFSubtarget::initializeSubtargetDependencies(StringRef CPU,
55 StringRef FS) {
56 initializeEnvironment();
57 initSubtargetFeatures(CPU, FS);
58 ParseSubtargetFeatures(CPU, /*TuneCPU*/ CPU, FS);
59 return *this;
60}
61
62void BPFSubtarget::initializeEnvironment() {
63 HasJmpExt = false;
64 HasJmp32 = false;
65 HasAlu32 = false;
66 UseDwarfRIS = false;
67 HasLdsx = false;
68 HasMovsx = false;
69 HasBswap = false;
70 HasSdivSmod = false;
71 HasGotol = false;
72 HasStoreImm = false;
73 HasLoadAcqStoreRel = false;
74 HasGotox = false;
75 HasHmul = false;
76 AllowsMisalignedMemAccess = false;
77 HasFuncTable = false;
78}
79
80void BPFSubtarget::initSubtargetFeatures(StringRef CPU, StringRef FS) {
81 if (CPU.empty())
82 CPU = "v3";
83 if (CPU == "probe")
84 CPU = sys::detail::getHostCPUNameForBPF();
85 if (CPU == "generic" || CPU == "v1")
86 return;
87 if (CPU == "v2") {
88 HasJmpExt = true;
89 return;
90 }
91 if (CPU == "v3") {
92 HasJmpExt = true;
93 HasJmp32 = true;
94 HasAlu32 = true;
95 return;
96 }
97 if (CPU == "v4" || CPU == "v5") {
98 HasJmpExt = true;
99 HasJmp32 = true;
100 HasAlu32 = true;
101 HasLdsx = !Disable_ldsx;
102 HasMovsx = !Disable_movsx;
103 HasBswap = !Disable_bswap;
104 HasSdivSmod = !Disable_sdiv_smod;
105 HasGotol = !Disable_gotol;
106 HasStoreImm = !Disable_StoreImm;
107 HasLoadAcqStoreRel = !Disable_load_acq_store_rel;
108 HasGotox = !Disable_gotox;
109 HasHmul = CPU == "v5" && !Disable_hmul;
110 return;
111 }
112}
113
114BPFSubtarget::BPFSubtarget(const Triple &TT, const std::string &CPU,
115 const std::string &FS, const TargetMachine &TM)
116 : BPFGenSubtargetInfo(TT, CPU, /*TuneCPU*/ CPU, FS),
117 InstrInfo(initializeSubtargetDependencies(CPU, FS)), FrameLowering(*this),
118 TLInfo(TM, *this) {
119 IsLittleEndian = TT.isLittleEndian();
120
121 CallLoweringInfo.reset(p: new BPFCallLowering(*getTargetLowering()));
122 Legalizer.reset(p: new BPFLegalizerInfo(*this));
123 auto *RBI = new BPFRegisterBankInfo(*getRegisterInfo());
124 RegBankInfo.reset(p: RBI);
125
126 InstSelector.reset(p: createBPFInstructionSelector(
127 *static_cast<const BPFTargetMachine *>(&TM), *this, *RBI));
128}
129
130const CallLowering *BPFSubtarget::getCallLowering() const {
131 return CallLoweringInfo.get();
132}
133
134InstructionSelector *BPFSubtarget::getInstructionSelector() const {
135 return InstSelector.get();
136}
137
138const LegalizerInfo *BPFSubtarget::getLegalizerInfo() const {
139 return Legalizer.get();
140}
141
142const RegisterBankInfo *BPFSubtarget::getRegBankInfo() const {
143 return RegBankInfo.get();
144}
145