1 //===-- RISCVSubtarget.h - Define Subtarget for the RISCV -------*- C++ -*-===//
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 declares the RISCV specific subclass of TargetSubtargetInfo.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #ifndef LLVM_LIB_TARGET_RISCV_RISCVSUBTARGET_H
14 #define LLVM_LIB_TARGET_RISCV_RISCVSUBTARGET_H
15 
16 #include "MCTargetDesc/RISCVBaseInfo.h"
17 #include "RISCVFrameLowering.h"
18 #include "RISCVISelLowering.h"
19 #include "RISCVInstrInfo.h"
20 #include "llvm/CodeGen/GlobalISel/CallLowering.h"
21 #include "llvm/CodeGen/GlobalISel/InstructionSelector.h"
22 #include "llvm/CodeGen/GlobalISel/LegalizerInfo.h"
23 #include "llvm/CodeGen/RegisterBankInfo.h"
24 #include "llvm/CodeGen/SelectionDAGTargetInfo.h"
25 #include "llvm/CodeGen/TargetSubtargetInfo.h"
26 #include "llvm/IR/DataLayout.h"
27 #include "llvm/Target/TargetMachine.h"
28 
29 #define GET_SUBTARGETINFO_HEADER
30 #include "RISCVGenSubtargetInfo.inc"
31 
32 namespace llvm {
33 class StringRef;
34 
35 class RISCVSubtarget : public RISCVGenSubtargetInfo {
36 public:
37   enum RISCVProcFamilyEnum : uint8_t {
38     Others,
39     SiFive7,
40   };
41 
42 private:
43   virtual void anchor();
44 
45   RISCVProcFamilyEnum RISCVProcFamily = Others;
46 
47   bool HasStdExtM = false;
48   bool HasStdExtA = false;
49   bool HasStdExtF = false;
50   bool HasStdExtD = false;
51   bool HasStdExtC = false;
52   bool HasStdExtZihintpause = false;
53   bool HasStdExtZba = false;
54   bool HasStdExtZbb = false;
55   bool HasStdExtZbc = false;
56   bool HasStdExtZbe = false;
57   bool HasStdExtZbf = false;
58   bool HasStdExtZbm = false;
59   bool HasStdExtZbp = false;
60   bool HasStdExtZbr = false;
61   bool HasStdExtZbs = false;
62   bool HasStdExtZbt = false;
63   bool HasStdExtV = false;
64   bool HasStdExtZve32x = false;
65   bool HasStdExtZve32f = false;
66   bool HasStdExtZve64x = false;
67   bool HasStdExtZve64f = false;
68   bool HasStdExtZve64d = false;
69   bool HasStdExtZvfh = false;
70   bool HasStdExtZfhmin = false;
71   bool HasStdExtZfh = false;
72   bool HasStdExtZfinx = false;
73   bool HasStdExtZdinx = false;
74   bool HasStdExtZhinxmin = false;
75   bool HasStdExtZhinx = false;
76   bool HasStdExtZbkb = false;
77   bool HasStdExtZbkc = false;
78   bool HasStdExtZbkx = false;
79   bool HasStdExtZknd = false;
80   bool HasStdExtZkne = false;
81   bool HasStdExtZknh = false;
82   bool HasStdExtZksed = false;
83   bool HasStdExtZksh = false;
84   bool HasStdExtZkr = false;
85   bool HasStdExtZkn = false;
86   bool HasStdExtZks = false;
87   bool HasStdExtZkt = false;
88   bool HasStdExtZk = false;
89   bool HasRV64 = false;
90   bool IsRV32E = false;
91   bool EnableLinkerRelax = false;
92   bool EnableRVCHintInstrs = true;
93   bool EnableSaveRestore = false;
94   unsigned XLen = 32;
95   unsigned ZvlLen = 0;
96   MVT XLenVT = MVT::i32;
97   uint8_t MaxInterleaveFactor = 2;
98   RISCVABI::ABI TargetABI = RISCVABI::ABI_Unknown;
99   BitVector UserReservedRegister;
100   RISCVFrameLowering FrameLowering;
101   RISCVInstrInfo InstrInfo;
102   RISCVRegisterInfo RegInfo;
103   RISCVTargetLowering TLInfo;
104   SelectionDAGTargetInfo TSInfo;
105 
106   /// Initializes using the passed in CPU and feature strings so that we can
107   /// use initializer lists for subtarget initialization.
108   RISCVSubtarget &initializeSubtargetDependencies(const Triple &TT,
109                                                   StringRef CPU,
110                                                   StringRef TuneCPU,
111                                                   StringRef FS,
112                                                   StringRef ABIName);
113 
114 public:
115   // Initializes the data members to match that of the specified triple.
116   RISCVSubtarget(const Triple &TT, StringRef CPU, StringRef TuneCPU,
117                  StringRef FS, StringRef ABIName, const TargetMachine &TM);
118 
119   // Parses features string setting specified subtarget options. The
120   // definition of this function is auto-generated by tblgen.
121   void ParseSubtargetFeatures(StringRef CPU, StringRef TuneCPU, StringRef FS);
122 
123   const RISCVFrameLowering *getFrameLowering() const override {
124     return &FrameLowering;
125   }
126   const RISCVInstrInfo *getInstrInfo() const override { return &InstrInfo; }
127   const RISCVRegisterInfo *getRegisterInfo() const override {
128     return &RegInfo;
129   }
130   const RISCVTargetLowering *getTargetLowering() const override {
131     return &TLInfo;
132   }
133   const SelectionDAGTargetInfo *getSelectionDAGInfo() const override {
134     return &TSInfo;
135   }
136   bool enableMachineScheduler() const override { return true; }
137 
138   /// Returns RISCV processor family.
139   /// Avoid this function! CPU specifics should be kept local to this class
140   /// and preferably modeled with SubtargetFeatures or properties in
141   /// initializeProperties().
142   RISCVProcFamilyEnum getProcFamily() const { return RISCVProcFamily; }
143 
144   bool hasStdExtM() const { return HasStdExtM; }
145   bool hasStdExtA() const { return HasStdExtA; }
146   bool hasStdExtF() const { return HasStdExtF; }
147   bool hasStdExtD() const { return HasStdExtD; }
148   bool hasStdExtC() const { return HasStdExtC; }
149   bool hasStdExtV() const { return HasStdExtV; }
150   bool hasStdExtZihintpause() const { return HasStdExtZihintpause; }
151   bool hasStdExtZba() const { return HasStdExtZba; }
152   bool hasStdExtZbb() const { return HasStdExtZbb; }
153   bool hasStdExtZbc() const { return HasStdExtZbc; }
154   bool hasStdExtZbe() const { return HasStdExtZbe; }
155   bool hasStdExtZbf() const { return HasStdExtZbf; }
156   bool hasStdExtZbm() const { return HasStdExtZbm; }
157   bool hasStdExtZbp() const { return HasStdExtZbp; }
158   bool hasStdExtZbr() const { return HasStdExtZbr; }
159   bool hasStdExtZbs() const { return HasStdExtZbs; }
160   bool hasStdExtZbt() const { return HasStdExtZbt; }
161   bool hasStdExtZvl() const { return ZvlLen != 0; }
162   bool hasStdExtZvfh() const { return HasStdExtZvfh; }
163   bool hasStdExtZfhmin() const { return HasStdExtZfhmin; }
164   bool hasStdExtZfh() const { return HasStdExtZfh; }
165   bool hasStdExtZfinx() const { return HasStdExtZfinx; }
166   bool hasStdExtZdinx() const { return HasStdExtZdinx; }
167   bool hasStdExtZhinxmin() const { return HasStdExtZhinxmin; }
168   bool hasStdExtZhinx() const { return HasStdExtZhinx; }
169   bool hasStdExtZbkb() const { return HasStdExtZbkb; }
170   bool hasStdExtZbkc() const { return HasStdExtZbkc; }
171   bool hasStdExtZbkx() const { return HasStdExtZbkx; }
172   bool hasStdExtZknd() const { return HasStdExtZknd; }
173   bool hasStdExtZkne() const { return HasStdExtZkne; }
174   bool hasStdExtZknh() const { return HasStdExtZknh; }
175   bool hasStdExtZksed() const { return HasStdExtZksed; }
176   bool hasStdExtZksh() const { return HasStdExtZksh; }
177   bool hasStdExtZkr() const { return HasStdExtZkr; }
178   bool is64Bit() const { return HasRV64; }
179   bool isRV32E() const { return IsRV32E; }
180   bool enableLinkerRelax() const { return EnableLinkerRelax; }
181   bool enableRVCHintInstrs() const { return EnableRVCHintInstrs; }
182   bool enableSaveRestore() const { return EnableSaveRestore; }
183   MVT getXLenVT() const { return XLenVT; }
184   unsigned getXLen() const { return XLen; }
185   unsigned getFLen() const {
186     if (HasStdExtD)
187       return 64;
188 
189     if (HasStdExtF)
190       return 32;
191 
192     return 0;
193   }
194   unsigned getELEN() const {
195     assert(hasVInstructions() && "Expected V extension");
196     return hasVInstructionsI64() ? 64 : 32;
197   }
198   unsigned getMinVLen() const { return ZvlLen; }
199   unsigned getMaxVLen() const { return 65536; }
200   unsigned getRealMinVLen() const {
201     unsigned VLen = getMinRVVVectorSizeInBits();
202     return VLen == 0 ? getMinVLen() : VLen;
203   }
204   unsigned getRealMaxVLen() const {
205     unsigned VLen = getMaxRVVVectorSizeInBits();
206     return VLen == 0 ? getMaxVLen() : VLen;
207   }
208   RISCVABI::ABI getTargetABI() const { return TargetABI; }
209   bool isRegisterReservedByUser(Register i) const {
210     assert(i < RISCV::NUM_TARGET_REGS && "Register out of range");
211     return UserReservedRegister[i];
212   }
213 
214   // Vector codegen related methods.
215   bool hasVInstructions() const { return HasStdExtZve32x; }
216   bool hasVInstructionsI64() const { return HasStdExtZve64x; }
217   bool hasVInstructionsF16() const { return HasStdExtZvfh && HasStdExtZfh; }
218   // FIXME: Consider Zfinx in the future
219   bool hasVInstructionsF32() const { return HasStdExtZve32f && HasStdExtF; }
220   // FIXME: Consider Zdinx in the future
221   bool hasVInstructionsF64() const { return HasStdExtZve64d && HasStdExtD; }
222   // F16 and F64 both require F32.
223   bool hasVInstructionsAnyF() const { return hasVInstructionsF32(); }
224   unsigned getMaxInterleaveFactor() const {
225     return hasVInstructions() ? MaxInterleaveFactor : 1;
226   }
227 
228 protected:
229   // GlobalISel related APIs.
230   std::unique_ptr<CallLowering> CallLoweringInfo;
231   std::unique_ptr<InstructionSelector> InstSelector;
232   std::unique_ptr<LegalizerInfo> Legalizer;
233   std::unique_ptr<RegisterBankInfo> RegBankInfo;
234 
235 public:
236   const CallLowering *getCallLowering() const override;
237   InstructionSelector *getInstructionSelector() const override;
238   const LegalizerInfo *getLegalizerInfo() const override;
239   const RegisterBankInfo *getRegBankInfo() const override;
240 
241   bool useConstantPoolForLargeInts() const;
242 
243   // Maximum cost used for building integers, integers will be put into constant
244   // pool if exceeded.
245   unsigned getMaxBuildIntsCost() const;
246 
247   // Return the known range for the bit length of RVV data registers. A value
248   // of 0 means nothing is known about that particular limit beyond what's
249   // implied by the architecture.
250   unsigned getMaxRVVVectorSizeInBits() const;
251   unsigned getMinRVVVectorSizeInBits() const;
252   unsigned getMaxLMULForFixedLengthVectors() const;
253   bool useRVVForFixedLengthVectors() const;
254 };
255 } // End llvm namespace
256 
257 #endif
258