LLVM 23.0.0git
RISCVSubtarget.h
Go to the documentation of this file.
1//===-- RISCVSubtarget.h - Define Subtarget for the RISC-V ------*- 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 RISC-V specific subclass of TargetSubtargetInfo.
10//
11//===----------------------------------------------------------------------===//
12
13#ifndef LLVM_LIB_TARGET_RISCV_RISCVSUBTARGET_H
14#define LLVM_LIB_TARGET_RISCV_RISCVSUBTARGET_H
15
18#include "RISCVFrameLowering.h"
19#include "RISCVISelLowering.h"
20#include "RISCVInstrInfo.h"
27#include "llvm/IR/DataLayout.h"
30#include <bitset>
31#include <memory>
32
33#define GET_RISCV_MACRO_FUSION_PRED_DECL
34#include "RISCVGenMacroFusion.inc"
35
36#define GET_SUBTARGETINFO_HEADER
37#include "RISCVGenSubtargetInfo.inc"
38
39namespace llvm {
40class StringRef;
41
42namespace RISCVTuneInfoTable {
43
78
79#define GET_RISCVTuneInfoTable_DECL
80#include "RISCVGenSearchableTables.inc"
81} // namespace RISCVTuneInfoTable
82
84public:
85 // clang-format off
97 // clang-format on
98private:
99 virtual void anchor();
100
101 RISCVProcFamilyEnum RISCVProcFamily = Others;
102 RISCVVRGatherCostModelEnum RISCVVRGatherCostModel = Quadratic;
103
104 bool IsLittleEndian = true;
105
106#define GET_SUBTARGETINFO_MACRO(ATTRIBUTE, DEFAULT, GETTER) \
107 bool ATTRIBUTE = DEFAULT;
108#include "RISCVGenSubtargetInfo.inc"
109
110 unsigned XSfmmTE = 0;
111 unsigned ZvlLen = 0;
112 unsigned RVVVectorBitsMin;
113 unsigned RVVVectorBitsMax;
116 std::bitset<RISCV::NUM_TARGET_REGS> UserReservedRegister;
117 const RISCVTuneInfoTable::RISCVTuneInfo *TuneInfo;
118
119 RISCVFrameLowering FrameLowering;
120 RISCVInstrInfo InstrInfo;
121 RISCVTargetLowering TLInfo;
122
123 /// Initializes using the passed in CPU and feature strings so that we can
124 /// use initializer lists for subtarget initialization.
125 RISCVSubtarget &initializeSubtargetDependencies(const Triple &TT,
126 StringRef CPU,
127 StringRef TuneCPU,
128 StringRef FS,
129 StringRef ABIName);
130
131public:
132 // Initializes the data members to match that of the specified triple.
133 RISCVSubtarget(const Triple &TT, StringRef CPU, StringRef TuneCPU,
134 StringRef FS, StringRef ABIName, unsigned RVVVectorBitsMin,
135 unsigned RVVVectorLMULMax, const TargetMachine &TM);
136
137 ~RISCVSubtarget() override;
138
139 // Parses features string setting specified subtarget options. The
140 // definition of this function is auto-generated by tblgen.
142
143 const RISCVFrameLowering *getFrameLowering() const override {
144 return &FrameLowering;
145 }
146 const RISCVInstrInfo *getInstrInfo() const override { return &InstrInfo; }
147 const RISCVRegisterInfo *getRegisterInfo() const override {
148 return &InstrInfo.getRegisterInfo();
149 }
150 const RISCVTargetLowering *getTargetLowering() const override {
151 return &TLInfo;
152 }
153
154 void mirFileLoaded(MachineFunction &MF) const override;
155
156 bool enableMachineScheduler() const override { return true; }
157
158 bool enablePostRAScheduler() const override { return UsePostRAScheduler; }
159
161 return Align(TuneInfo->PrefFunctionAlignment);
162 }
164 return Align(TuneInfo->PrefLoopAlignment);
165 }
166
167 /// Returns RISC-V processor family.
168 /// Avoid this function! CPU specifics should be kept local to this class
169 /// and preferably modeled with SubtargetFeatures or properties in
170 /// initializeProperties().
171 RISCVProcFamilyEnum getProcFamily() const { return RISCVProcFamily; }
172
173 RISCVVRGatherCostModelEnum getVRGatherCostModel() const { return RISCVVRGatherCostModel; }
174
175#define GET_SUBTARGETINFO_MACRO(ATTRIBUTE, DEFAULT, GETTER) \
176 bool GETTER() const { return ATTRIBUTE; }
177#include "RISCVGenSubtargetInfo.inc"
178
179 LLVM_DEPRECATED("Now Equivalent to hasStdExtZcd", "hasStdExtZcd")
180 bool hasStdExtCOrZcd() const { return HasStdExtZcd; }
181 LLVM_DEPRECATED("Now Equivalent to hasStdExtZcf", "hasStdExtZcf")
182 bool hasStdExtCOrZcfOrZce() const { return HasStdExtZcf; }
183 bool hasStdExtZvl() const { return ZvlLen != 0; }
184 bool hasStdExtFOrZfinx() const { return HasStdExtF || HasStdExtZfinx; }
185 bool hasStdExtDOrZdinx() const { return HasStdExtD || HasStdExtZdinx; }
186 bool hasStdExtZfhOrZhinx() const { return HasStdExtZfh || HasStdExtZhinx; }
188 return HasStdExtZfhmin || HasStdExtZhinxmin;
189 }
191 return HasStdExtZfhmin || HasStdExtZfbfmin;
192 }
193
194 bool hasCLZLike() const { return HasStdExtZbb || HasVendorXTHeadBb; }
195 bool hasCTZLike() const {
196 return HasStdExtZbb || (HasVendorXCVbitmanip && !IsRV64);
197 }
198 bool hasCPOPLike() const {
199 return HasStdExtZbb || (HasVendorXCVbitmanip && !IsRV64);
200 }
201 bool hasREV8Like() const {
202 return HasStdExtZbb || HasStdExtZbkb || HasVendorXTHeadBb;
203 }
204 bool hasREVLike() const {
205 return HasStdExtP || ((HasVendorXCVbitmanip || HasVendorXqcibm) && !IsRV64);
206 }
207
208 bool hasBEXTILike() const { return HasStdExtZbs || HasVendorXTHeadBs; }
209
210 bool hasCZEROLike() const {
211 return HasStdExtZicond || HasVendorXVentanaCondOps;
212 }
213
215 // Do we support fusing a branch+mv or branch+c.mv as a conditional move.
216 return (hasConditionalCompressedMoveFusion() && hasStdExtZca()) ||
217 hasShortForwardBranchIALU();
218 }
219
220 bool hasShlAdd(int64_t ShAmt) const {
221 if (ShAmt <= 0)
222 return false;
223 if (ShAmt <= 3)
224 return HasStdExtZba || HasVendorXAndesPerf || HasVendorXTHeadBa;
225 return ShAmt <= 31 && HasVendorXqciac;
226 }
227
228 bool is64Bit() const { return IsRV64; }
229 bool isLittleEndian() const { return IsLittleEndian; }
230 MVT getXLenVT() const {
231 return is64Bit() ? MVT::i64 : MVT::i32;
232 }
233 unsigned getXLen() const {
234 return is64Bit() ? 64 : 32;
235 }
236 bool useMIPSLoadStorePairs() const;
237 bool useMIPSCCMovInsn() const;
238 unsigned getFLen() const {
239 if (HasStdExtD)
240 return 64;
241
242 if (HasStdExtF)
243 return 32;
244
245 return 0;
246 }
247
249 if (enableUnalignedScalarMem())
250 return Align(1);
251
252 if (allowZilsdWordAlign())
253 return Align(4);
254
255 return Align(8);
256 }
257
258 unsigned getELen() const {
259 assert(hasVInstructions() && "Expected V extension");
260 return hasVInstructionsI64() ? 64 : 32;
261 }
262 unsigned getRealMinVLen() const {
263 unsigned VLen = getMinRVVVectorSizeInBits();
264 return VLen == 0 ? ZvlLen : VLen;
265 }
266 unsigned getRealMaxVLen() const {
267 unsigned VLen = getMaxRVVVectorSizeInBits();
268 return VLen == 0 ? 65536 : VLen;
269 }
270 // If we know the exact VLEN, return it. Otherwise, return std::nullopt.
271 std::optional<unsigned> getRealVLen() const {
272 unsigned Min = getRealMinVLen();
273 if (Min != getRealMaxVLen())
274 return std::nullopt;
275 return Min;
276 }
277
278 /// If the ElementCount or TypeSize \p X is scalable and VScale (VLEN) is
279 /// exactly known, returns \p X converted to a fixed quantity. Otherwise
280 /// returns \p X unmodified.
281 template <typename Quantity> Quantity expandVScale(Quantity X) const {
282 if (auto VLen = getRealVLen(); VLen && X.isScalable()) {
283 const unsigned VScale = *VLen / RISCV::RVVBitsPerBlock;
284 X = Quantity::getFixed(X.getKnownMinValue() * VScale);
285 }
286 return X;
287 }
288
289 RISCVABI::ABI getTargetABI() const { return TargetABI; }
290 bool isSoftFPABI() const {
291 return TargetABI == RISCVABI::ABI_LP64 ||
292 TargetABI == RISCVABI::ABI_ILP32 ||
293 TargetABI == RISCVABI::ABI_ILP32E;
294 }
295 bool isRegisterReservedByUser(Register i) const override {
296 assert(i.id() < RISCV::NUM_TARGET_REGS && "Register out of range");
297 return UserReservedRegister[i.id()];
298 }
299
300 // XRay support - require D and C extensions.
301 bool isXRaySupported() const override { return hasStdExtD() && hasStdExtC(); }
302
303 // Vector codegen related methods.
304 bool hasVInstructions() const { return HasStdExtZve32x; }
305 bool hasVInstructionsI64() const { return HasStdExtZve64x; }
306 bool hasVInstructionsF16Minimal() const { return HasStdExtZvfhmin; }
307 bool hasVInstructionsF16() const { return HasStdExtZvfh; }
309 return HasStdExtZvfbfmin || HasStdExtZvfbfa;
310 }
311 bool hasVInstructionsF32() const { return HasStdExtZve32f; }
312 bool hasVInstructionsF64() const { return HasStdExtZve64d; }
313 bool hasVInstructionsBF16() const { return HasStdExtZvfbfa; }
314 // F16 and F64 both require F32.
315 bool hasVInstructionsAnyF() const { return hasVInstructionsF32(); }
316 bool hasVInstructionsFullMultiply() const { return HasStdExtV; }
317 unsigned getMaxInterleaveFactor() const {
318 return hasVInstructions() ? MaxInterleaveFactor : 1;
319 }
320
321 bool hasOptimizedSegmentLoadStore(unsigned NF) const {
322 switch (NF) {
323 case 2:
324 return hasOptimizedNF2SegmentLoadStore();
325 case 3:
326 return hasOptimizedNF3SegmentLoadStore();
327 case 4:
328 return hasOptimizedNF4SegmentLoadStore();
329 case 5:
330 return hasOptimizedNF5SegmentLoadStore();
331 case 6:
332 return hasOptimizedNF6SegmentLoadStore();
333 case 7:
334 return hasOptimizedNF7SegmentLoadStore();
335 case 8:
336 return hasOptimizedNF8SegmentLoadStore();
337 default:
338 llvm_unreachable("Unexpected NF");
339 }
340 }
341
342 bool isPExtPackedType(MVT VT) const;
343 bool isPExtPackedDoubleType(MVT VT) const;
344
345 // Returns VLEN divided by DLEN. Where DLEN is the datapath width of the
346 // vector hardware implementation which may be less than VLEN.
347 unsigned getDLenFactor() const {
348 if (DLenFactor2)
349 return 2;
350 return 1;
351 }
352
353protected:
354 // SelectionDAGISel related APIs.
355 std::unique_ptr<const SelectionDAGTargetInfo> TSInfo;
356
357 // GlobalISel related APIs.
358 mutable std::unique_ptr<CallLowering> CallLoweringInfo;
359 mutable std::unique_ptr<InstructionSelector> InstSelector;
360 mutable std::unique_ptr<LegalizerInfo> Legalizer;
361 mutable std::unique_ptr<RISCVRegisterBankInfo> RegBankInfo;
362 mutable std::unique_ptr<InlineAsmLowering> InlineAsmLoweringInfo;
363
364 // Return the known range for the bit length of RVV data registers as set
365 // at the command line. A value of 0 means nothing is known about that particular
366 // limit beyond what's implied by the architecture.
367 // NOTE: Please use getRealMinVLen and getRealMaxVLen instead!
368 unsigned getMaxRVVVectorSizeInBits() const;
369 unsigned getMinRVVVectorSizeInBits() const;
370
371public:
372 const SelectionDAGTargetInfo *getSelectionDAGInfo() const override;
373 const CallLowering *getCallLowering() const override;
375 const LegalizerInfo *getLegalizerInfo() const override;
376 const RISCVRegisterBankInfo *getRegBankInfo() const override;
377 const InlineAsmLowering *getInlineAsmLowering() const override;
378
379 bool isTargetAndroid() const { return getTargetTriple().isAndroid(); }
380 bool isTargetFuchsia() const { return getTargetTriple().isOSFuchsia(); }
381
382 bool useConstantPoolForLargeInts() const;
383
384 // Maximum cost used for building integers, integers will be put into constant
385 // pool if exceeded.
386 unsigned getMaxBuildIntsCost() const;
387
388 unsigned getMaxLMULForFixedLengthVectors() const;
389 bool useRVVForFixedLengthVectors() const;
390
391 bool enableSubRegLiveness() const override;
392
393 bool enableMachinePipeliner() const override;
394
395 bool useDFAforSMS() const override { return false; }
396
397 bool useAA() const override;
398
399 unsigned getCacheLineSize() const override {
400 return TuneInfo->CacheLineSize;
401 };
402 unsigned getPrefetchDistance() const override {
403 return TuneInfo->PrefetchDistance;
404 };
405 unsigned getMinPrefetchStride(unsigned NumMemAccesses,
406 unsigned NumStridedMemAccesses,
407 unsigned NumPrefetches,
408 bool HasCall) const override {
409 return TuneInfo->MinPrefetchStride;
410 };
411 unsigned getMaxPrefetchIterationsAhead() const override {
412 return TuneInfo->MaxPrefetchIterationsAhead;
413 };
414 bool enableWritePrefetching() const override { return true; }
415
416 unsigned getMinimumJumpTableEntries() const;
417
419 return TuneInfo->TailDupAggressiveThreshold;
420 }
421
422 unsigned getMaxStoresPerMemset(bool OptSize) const {
423 return OptSize ? TuneInfo->MaxStoresPerMemsetOptSize
424 : TuneInfo->MaxStoresPerMemset;
425 }
426
427 unsigned getMaxGluedStoresPerMemcpy() const {
428 return TuneInfo->MaxGluedStoresPerMemcpy;
429 }
430
431 unsigned getMaxStoresPerMemcpy(bool OptSize) const {
432 return OptSize ? TuneInfo->MaxStoresPerMemcpyOptSize
433 : TuneInfo->MaxStoresPerMemcpy;
434 }
435
436 unsigned getMaxStoresPerMemmove(bool OptSize) const {
437 return OptSize ? TuneInfo->MaxStoresPerMemmoveOptSize
438 : TuneInfo->MaxStoresPerMemmove;
439 }
440
441 unsigned getMaxLoadsPerMemcmp(bool OptSize) const {
442 return OptSize ? TuneInfo->MaxLoadsPerMemcmpOptSize
443 : TuneInfo->MaxLoadsPerMemcmp;
444 }
445
447 return TuneInfo->PostRASchedDirection;
448 }
449
450 bool isJumpExpensive() const { return TuneInfo->IsJumpExpensive; }
451
453 const SchedRegion &Region) const override;
454
456 const SchedRegion &Region) const override;
457};
458} // namespace llvm
459
460#endif
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
#define X(NUM, ENUM, NAME)
Definition ELF.h:853
This file describes how to lower LLVM calls to machine code calls.
#define LLVM_DEPRECATED(MSG, FIX)
Definition Compiler.h:254
This file describes how to lower LLVM inline asm to machine code INLINEASM.
Interface for Targets to specify which operations they can successfully select and how the others sho...
static const unsigned MaxInterleaveFactor
Maximum vectorization interleave count.
This file declares the targeting of the RegisterBankInfo class for RISC-V.
static cl::opt< unsigned > RVVVectorLMULMax("riscv-v-fixed-length-vector-lmul-max", cl::desc("The maximum LMUL value to use for fixed length vectors. " "Fractional LMUL values are not supported."), cl::init(8), cl::Hidden)
Machine Value Type.
This class provides the information for the target register banks.
RISCVABI::ABI getTargetABI() const
unsigned getMinimumJumpTableEntries() const
const LegalizerInfo * getLegalizerInfo() const override
bool isJumpExpensive() const
void overrideSchedPolicy(MachineSchedPolicy &Policy, const SchedRegion &Region) const override
bool enableWritePrefetching() const override
std::unique_ptr< LegalizerInfo > Legalizer
unsigned getMaxLMULForFixedLengthVectors() const
bool hasVInstructionsI64() const
bool isPExtPackedDoubleType(MVT VT) const
unsigned getMaxPrefetchIterationsAhead() const override
bool hasVInstructionsF64() const
unsigned getMaxStoresPerMemcpy(bool OptSize) const
bool hasStdExtDOrZdinx() const
unsigned getMaxLoadsPerMemcmp(bool OptSize) const
bool hasStdExtZfhOrZhinx() const
bool hasShlAdd(int64_t ShAmt) const
bool useDFAforSMS() const override
unsigned getTailDupAggressiveThreshold() const
unsigned getRealMinVLen() const
unsigned getMaxStoresPerMemset(bool OptSize) const
bool useMIPSLoadStorePairs() const
const InlineAsmLowering * getInlineAsmLowering() const override
Quantity expandVScale(Quantity X) const
If the ElementCount or TypeSize X is scalable and VScale (VLEN) is exactly known, returns X converted...
bool useRVVForFixedLengthVectors() const
RISCVVRGatherCostModelEnum getVRGatherCostModel() const
MISched::Direction getPostRASchedDirection() const
std::unique_ptr< InlineAsmLowering > InlineAsmLoweringInfo
bool isPExtPackedType(MVT VT) const
bool isTargetFuchsia() const
bool hasVInstructionsBF16Minimal() const
unsigned getDLenFactor() const
unsigned getMaxStoresPerMemmove(bool OptSize) const
unsigned getMinRVVVectorSizeInBits() const
std::unique_ptr< InstructionSelector > InstSelector
bool hasVInstructionsF16Minimal() const
RISCVSubtarget(const Triple &TT, StringRef CPU, StringRef TuneCPU, StringRef FS, StringRef ABIName, unsigned RVVVectorBitsMin, unsigned RVVVectorLMULMax, const TargetMachine &TM)
unsigned getMaxGluedStoresPerMemcpy() const
unsigned getXLen() const
bool hasConditionalMoveFusion() const
bool useMIPSCCMovInsn() const
bool hasVInstructionsF16() const
bool hasVInstructionsBF16() const
const RISCVRegisterBankInfo * getRegBankInfo() const override
const CallLowering * getCallLowering() const override
bool enableMachineScheduler() const override
InstructionSelector * getInstructionSelector() const override
unsigned getMaxBuildIntsCost() const
Align getPrefLoopAlignment() const
std::unique_ptr< const SelectionDAGTargetInfo > TSInfo
bool hasVInstructions() const
bool useAA() const override
Enable use of alias analysis during code generation (during MI scheduling, DAGCombine,...
bool isRegisterReservedByUser(Register i) const override
bool hasVInstructionsAnyF() const
std::optional< unsigned > getRealVLen() const
bool isXRaySupported() const override
bool enableMachinePipeliner() const override
bool hasOptimizedSegmentLoadStore(unsigned NF) const
bool useConstantPoolForLargeInts() const
bool isLittleEndian() const
bool hasStdExtCOrZcfOrZce() const
Align getPrefFunctionAlignment() const
~RISCVSubtarget() override
RISCVProcFamilyEnum getProcFamily() const
Returns RISC-V processor family.
unsigned getMaxRVVVectorSizeInBits() const
bool hasStdExtZfhminOrZhinxmin() const
unsigned getRealMaxVLen() const
unsigned getMinPrefetchStride(unsigned NumMemAccesses, unsigned NumStridedMemAccesses, unsigned NumPrefetches, bool HasCall) const override
void ParseSubtargetFeatures(StringRef CPU, StringRef TuneCPU, StringRef FS)
const RISCVRegisterInfo * getRegisterInfo() const override
Align getZilsdAlign() const
std::unique_ptr< RISCVRegisterBankInfo > RegBankInfo
void mirFileLoaded(MachineFunction &MF) const override
const RISCVInstrInfo * getInstrInfo() const override
unsigned getCacheLineSize() const override
std::unique_ptr< CallLowering > CallLoweringInfo
bool hasBEXTILike() const
bool hasStdExtCOrZcd() const
bool hasVInstructionsFullMultiply() const
const RISCVTargetLowering * getTargetLowering() const override
void overridePostRASchedPolicy(MachineSchedPolicy &Policy, const SchedRegion &Region) const override
bool hasVInstructionsF32() const
unsigned getMaxInterleaveFactor() const
bool hasCZEROLike() const
bool enableSubRegLiveness() const override
unsigned getELen() const
const SelectionDAGTargetInfo * getSelectionDAGInfo() const override
bool isTargetAndroid() const
bool hasStdExtFOrZfinx() const
bool enablePostRAScheduler() const override
bool hasStdExtZvl() const
bool hasHalfFPLoadStoreMove() const
const RISCVFrameLowering * getFrameLowering() const override
unsigned getFLen() const
unsigned getPrefetchDistance() const override
Wrapper class representing virtual and physical registers.
Definition Register.h:20
constexpr unsigned id() const
Definition Register.h:100
Targets can subclass this to parameterize the SelectionDAG lowering and instruction selection process...
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
static constexpr unsigned RVVBitsPerBlock
This is an optimization pass for GlobalISel generic memory operations.
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition Alignment.h:39
Define a generic scheduling policy for targets that don't provide their own MachineSchedStrategy.
A region of an MBB for scheduling.