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