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"));
48
49void BPFSubtarget::anchor() {}
50
51BPFSubtarget &BPFSubtarget::initializeSubtargetDependencies(StringRef CPU,
52 StringRef FS) {
53 initializeEnvironment();
54 initSubtargetFeatures(CPU, FS);
55 ParseSubtargetFeatures(CPU, /*TuneCPU*/ CPU, FS);
56 return *this;
57}
58
59void BPFSubtarget::initializeEnvironment() {
60 HasJmpExt = false;
61 HasJmp32 = false;
62 HasAlu32 = false;
63 UseDwarfRIS = false;
64 HasLdsx = false;
65 HasMovsx = false;
66 HasBswap = false;
67 HasSdivSmod = false;
68 HasGotol = false;
69 HasStoreImm = false;
70 HasLoadAcqStoreRel = false;
71 HasGotox = false;
72 AllowsMisalignedMemAccess = false;
73}
74
75void BPFSubtarget::initSubtargetFeatures(StringRef CPU, StringRef FS) {
76 if (CPU.empty())
77 CPU = "v3";
78 if (CPU == "probe")
79 CPU = sys::detail::getHostCPUNameForBPF();
80 if (CPU == "generic" || CPU == "v1")
81 return;
82 if (CPU == "v2") {
83 HasJmpExt = true;
84 return;
85 }
86 if (CPU == "v3") {
87 HasJmpExt = true;
88 HasJmp32 = true;
89 HasAlu32 = true;
90 return;
91 }
92 if (CPU == "v4") {
93 HasJmpExt = true;
94 HasJmp32 = true;
95 HasAlu32 = true;
96 HasLdsx = !Disable_ldsx;
97 HasMovsx = !Disable_movsx;
98 HasBswap = !Disable_bswap;
99 HasSdivSmod = !Disable_sdiv_smod;
100 HasGotol = !Disable_gotol;
101 HasStoreImm = !Disable_StoreImm;
102 HasLoadAcqStoreRel = !Disable_load_acq_store_rel;
103 HasGotox = !Disable_gotox;
104 return;
105 }
106}
107
108BPFSubtarget::BPFSubtarget(const Triple &TT, const std::string &CPU,
109 const std::string &FS, const TargetMachine &TM)
110 : BPFGenSubtargetInfo(TT, CPU, /*TuneCPU*/ CPU, FS),
111 InstrInfo(initializeSubtargetDependencies(CPU, FS)), FrameLowering(*this),
112 TLInfo(TM, *this) {
113 IsLittleEndian = TT.isLittleEndian();
114
115 CallLoweringInfo.reset(p: new BPFCallLowering(*getTargetLowering()));
116 Legalizer.reset(p: new BPFLegalizerInfo(*this));
117 auto *RBI = new BPFRegisterBankInfo(*getRegisterInfo());
118 RegBankInfo.reset(p: RBI);
119
120 InstSelector.reset(p: createBPFInstructionSelector(
121 *static_cast<const BPFTargetMachine *>(&TM), *this, *RBI));
122}
123
124const CallLowering *BPFSubtarget::getCallLowering() const {
125 return CallLoweringInfo.get();
126}
127
128InstructionSelector *BPFSubtarget::getInstructionSelector() const {
129 return InstSelector.get();
130}
131
132const LegalizerInfo *BPFSubtarget::getLegalizerInfo() const {
133 return Legalizer.get();
134}
135
136const RegisterBankInfo *BPFSubtarget::getRegBankInfo() const {
137 return RegBankInfo.get();
138}
139