1 //===-- RISCVBaseInfo.h - Top level definitions for RISC-V MC ---*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
9 // This file contains small standalone enum definitions for the RISC-V target
10 // useful for the compiler back-end and the MC libraries.
12 //===----------------------------------------------------------------------===//
13 #ifndef LLVM_LIB_TARGET_RISCV_MCTARGETDESC_RISCVBASEINFO_H
14 #define LLVM_LIB_TARGET_RISCV_MCTARGETDESC_RISCVBASEINFO_H
16 #include "MCTargetDesc/RISCVMCTargetDesc.h"
17 #include "llvm/ADT/APFloat.h"
18 #include "llvm/ADT/APInt.h"
19 #include "llvm/ADT/StringRef.h"
20 #include "llvm/ADT/StringSwitch.h"
21 #include "llvm/MC/MCInstrDesc.h"
22 #include "llvm/TargetParser/RISCVISAInfo.h"
23 #include "llvm/TargetParser/RISCVTargetParser.h"
24 #include "llvm/TargetParser/SubtargetFeature.h"
28 // RISCVII - This namespace holds all of the target specific flags that
29 // instruction info tracks. All definitions must match RISCVInstrFormats.td.
59 ConstraintShift
= InstFormatShift
+ 5,
60 VS2Constraint
= 0b001 << ConstraintShift
,
61 VS1Constraint
= 0b010 << ConstraintShift
,
62 VMConstraint
= 0b100 << ConstraintShift
,
63 ConstraintMask
= 0b111 << ConstraintShift
,
65 VLMulShift
= ConstraintShift
+ 3,
66 VLMulMask
= 0b111 << VLMulShift
,
68 // Force a tail agnostic policy even this instruction has a tied destination.
69 ForceTailAgnosticShift
= VLMulShift
+ 3,
70 ForceTailAgnosticMask
= 1 << ForceTailAgnosticShift
,
72 // Is this a _TIED vector pseudo instruction. For these instructions we
73 // shouldn't skip the tied operand when converting to MC instructions.
74 IsTiedPseudoShift
= ForceTailAgnosticShift
+ 1,
75 IsTiedPseudoMask
= 1 << IsTiedPseudoShift
,
77 // Does this instruction have a SEW operand. It will be the last explicit
78 // operand unless there is a vector policy operand. Used by RVV Pseudos.
79 HasSEWOpShift
= IsTiedPseudoShift
+ 1,
80 HasSEWOpMask
= 1 << HasSEWOpShift
,
82 // Does this instruction have a VL operand. It will be the second to last
83 // explicit operand unless there is a vector policy operand. Used by RVV
85 HasVLOpShift
= HasSEWOpShift
+ 1,
86 HasVLOpMask
= 1 << HasVLOpShift
,
88 // Does this instruction have a vector policy operand. It will be the last
89 // explicit operand. Used by RVV Pseudos.
90 HasVecPolicyOpShift
= HasVLOpShift
+ 1,
91 HasVecPolicyOpMask
= 1 << HasVecPolicyOpShift
,
93 // Is this instruction a vector widening reduction instruction. Used by RVV
95 IsRVVWideningReductionShift
= HasVecPolicyOpShift
+ 1,
96 IsRVVWideningReductionMask
= 1 << IsRVVWideningReductionShift
,
98 // Does this instruction care about mask policy. If it is not, the mask policy
99 // could be either agnostic or undisturbed. For example, unmasked, store, and
100 // reduction operations result would not be affected by mask policy, so
101 // compiler has free to select either one.
102 UsesMaskPolicyShift
= IsRVVWideningReductionShift
+ 1,
103 UsesMaskPolicyMask
= 1 << UsesMaskPolicyShift
,
105 // Indicates that the result can be considered sign extended from bit 31. Some
106 // instructions with this flag aren't W instructions, but are either sign
107 // extended from a smaller size, always outputs a small integer, or put zeros
108 // in bits 63:31. Used by the SExtWRemoval pass.
109 IsSignExtendingOpWShift
= UsesMaskPolicyShift
+ 1,
110 IsSignExtendingOpWMask
= 1ULL << IsSignExtendingOpWShift
,
112 HasRoundModeOpShift
= IsSignExtendingOpWShift
+ 1,
113 HasRoundModeOpMask
= 1 << HasRoundModeOpShift
,
115 UsesVXRMShift
= HasRoundModeOpShift
+ 1,
116 UsesVXRMMask
= 1 << UsesVXRMShift
,
118 // Indicates whether these instructions can partially overlap between source
119 // registers and destination registers according to the vector spec.
120 // 0 -> not a vector pseudo
121 // 1 -> default value for vector pseudos. not widening or narrowing.
122 // 2 -> narrowing case
123 // 3 -> widening case
124 TargetOverlapConstraintTypeShift
= UsesVXRMShift
+ 1,
125 TargetOverlapConstraintTypeMask
= 3ULL << TargetOverlapConstraintTypeShift
,
127 ElementsDependOnVLShift
= TargetOverlapConstraintTypeShift
+ 2,
128 ElementsDependOnVLMask
= 1ULL << ElementsDependOnVLShift
,
130 ElementsDependOnMaskShift
= ElementsDependOnVLShift
+ 1,
131 ElementsDependOnMaskMask
= 1ULL << ElementsDependOnMaskShift
,
133 // Indicates the EEW of a vector instruction's destination operand.
138 DestEEWShift
= ElementsDependOnMaskShift
+ 1,
139 DestEEWMask
= 3ULL << DestEEWShift
,
142 // Helper functions to read TSFlags.
143 /// \returns the format of the instruction.
144 static inline unsigned getFormat(uint64_t TSFlags
) {
145 return (TSFlags
& InstFormatMask
) >> InstFormatShift
;
147 /// \returns the LMUL for the instruction.
148 static inline VLMUL
getLMul(uint64_t TSFlags
) {
149 return static_cast<VLMUL
>((TSFlags
& VLMulMask
) >> VLMulShift
);
151 /// \returns true if tail agnostic is enforced for the instruction.
152 static inline bool doesForceTailAgnostic(uint64_t TSFlags
) {
153 return TSFlags
& ForceTailAgnosticMask
;
155 /// \returns true if this a _TIED pseudo.
156 static inline bool isTiedPseudo(uint64_t TSFlags
) {
157 return TSFlags
& IsTiedPseudoMask
;
159 /// \returns true if there is a SEW operand for the instruction.
160 static inline bool hasSEWOp(uint64_t TSFlags
) {
161 return TSFlags
& HasSEWOpMask
;
163 /// \returns true if there is a VL operand for the instruction.
164 static inline bool hasVLOp(uint64_t TSFlags
) {
165 return TSFlags
& HasVLOpMask
;
167 /// \returns true if there is a vector policy operand for this instruction.
168 static inline bool hasVecPolicyOp(uint64_t TSFlags
) {
169 return TSFlags
& HasVecPolicyOpMask
;
171 /// \returns true if it is a vector widening reduction instruction.
172 static inline bool isRVVWideningReduction(uint64_t TSFlags
) {
173 return TSFlags
& IsRVVWideningReductionMask
;
175 /// \returns true if mask policy is valid for the instruction.
176 static inline bool usesMaskPolicy(uint64_t TSFlags
) {
177 return TSFlags
& UsesMaskPolicyMask
;
180 /// \returns true if there is a rounding mode operand for this instruction
181 static inline bool hasRoundModeOp(uint64_t TSFlags
) {
182 return TSFlags
& HasRoundModeOpMask
;
185 /// \returns true if this instruction uses vxrm
186 static inline bool usesVXRM(uint64_t TSFlags
) { return TSFlags
& UsesVXRMMask
; }
188 /// \returns true if the elements in the body are affected by VL,
189 /// e.g. vslide1down.vx/vredsum.vs/viota.m
190 static inline bool elementsDependOnVL(uint64_t TSFlags
) {
191 return TSFlags
& ElementsDependOnVLMask
;
194 /// \returns true if the elements in the body are affected by the mask,
195 /// e.g. vredsum.vs/viota.m
196 static inline bool elementsDependOnMask(uint64_t TSFlags
) {
197 return TSFlags
& ElementsDependOnMaskMask
;
200 static inline unsigned getVLOpNum(const MCInstrDesc
&Desc
) {
201 const uint64_t TSFlags
= Desc
.TSFlags
;
202 // This method is only called if we expect to have a VL operand, and all
203 // instructions with VL also have SEW.
204 assert(hasSEWOp(TSFlags
) && hasVLOp(TSFlags
));
206 if (hasVecPolicyOp(TSFlags
))
208 return Desc
.getNumOperands() - Offset
;
211 static inline unsigned getTailExpandUseRegNo(const FeatureBitset
&FeatureBits
) {
212 // For Zicfilp, PseudoTAIL should be expanded to a software guarded branch.
213 // It means to use t2(x7) as rs1 of JALR to expand PseudoTAIL.
214 return FeatureBits
[RISCV::FeatureStdExtZicfilp
] ? RISCV::X7
: RISCV::X6
;
217 static inline unsigned getSEWOpNum(const MCInstrDesc
&Desc
) {
218 const uint64_t TSFlags
= Desc
.TSFlags
;
219 assert(hasSEWOp(TSFlags
));
221 if (hasVecPolicyOp(TSFlags
))
223 return Desc
.getNumOperands() - Offset
;
226 static inline unsigned getVecPolicyOpNum(const MCInstrDesc
&Desc
) {
227 assert(hasVecPolicyOp(Desc
.TSFlags
));
228 return Desc
.getNumOperands() - 1;
231 /// \returns the index to the rounding mode immediate value if any, otherwise
233 static inline int getFRMOpNum(const MCInstrDesc
&Desc
) {
234 const uint64_t TSFlags
= Desc
.TSFlags
;
235 if (!hasRoundModeOp(TSFlags
) || usesVXRM(TSFlags
))
239 // --------------------------------------
240 // | n-1 (if any) | n-2 | n-3 | n-4 |
241 // | policy | sew | vl | frm |
242 // --------------------------------------
243 return getVLOpNum(Desc
) - 1;
246 /// \returns the index to the rounding mode immediate value if any, otherwise
248 static inline int getVXRMOpNum(const MCInstrDesc
&Desc
) {
249 const uint64_t TSFlags
= Desc
.TSFlags
;
250 if (!hasRoundModeOp(TSFlags
) || !usesVXRM(TSFlags
))
253 // --------------------------------------
254 // | n-1 (if any) | n-2 | n-3 | n-4 |
255 // | policy | sew | vl | vxrm |
256 // --------------------------------------
257 return getVLOpNum(Desc
) - 1;
260 // Is the first def operand tied to the first use operand. This is true for
261 // vector pseudo instructions that have a merge operand for tail/mask
262 // undisturbed. It's also true for vector FMA instructions where one of the
263 // operands is also the destination register.
264 static inline bool isFirstDefTiedToFirstUse(const MCInstrDesc
&Desc
) {
265 return Desc
.getNumDefs() < Desc
.getNumOperands() &&
266 Desc
.getOperandConstraint(Desc
.getNumDefs(), MCOI::TIED_TO
) == 0;
269 // RISC-V Specific Machine Operand Flags
284 MO_TLSDESC_LOAD_LO
= 14,
285 MO_TLSDESC_ADD_LO
= 15,
286 MO_TLSDESC_CALL
= 16,
288 // Used to differentiate between target-specific "direct" flags and "bitmask"
289 // flags. A machine operand can only have one "direct" flag, but can have
290 // multiple "bitmask" flags.
291 MO_DIRECT_FLAG_MASK
= 31
293 } // namespace RISCVII
296 enum OperandType
: unsigned {
297 OPERAND_FIRST_RISCV_IMM
= MCOI::OPERAND_FIRST_TARGET
,
298 OPERAND_UIMM1
= OPERAND_FIRST_RISCV_IMM
,
304 OPERAND_UIMM5_NONZERO
,
313 OPERAND_UIMM8_LSB000
,
315 OPERAND_UIMM9_LSB000
,
316 OPERAND_UIMM10_LSB00_NONZERO
,
321 OPERAND_UIMMLOG2XLEN
,
322 OPERAND_UIMMLOG2XLEN_NONZERO
,
330 OPERAND_SIMM6_NONZERO
,
331 OPERAND_SIMM10_LSB0000_NONZERO
,
333 OPERAND_SIMM12_LSB00000
,
339 OPERAND_RVKRNUM_1_10
,
340 OPERAND_RVKRNUM_2_14
,
342 // Operand is a 3-bit rounding mode, '111' indicates FRM register.
343 // Represents 'frm' argument passing to floating-point operations.
345 // Operand is a 3-bit rounding mode where only RTZ is valid.
347 // Condition code used by select and short forward branch pseudos.
349 // Vector policy operand.
351 // Vector SEW operand. Stores in log2(SEW).
353 // Special SEW for mask only instructions. Always 0.
355 // Vector rounding mode for VXRM or FRM.
357 OPERAND_LAST_RISCV_IMM
= OPERAND_VEC_RM
,
358 // Operand is either a register or uimm5, this is used by V extension pseudo
359 // instructions to represent a value that be passed as AVL to either vsetvli
363 } // namespace RISCVOp
365 // Describes the predecessor/successor bits used in the FENCE instruction.
366 namespace RISCVFenceField
{
375 // Describes the supported floating point rounding mode encodings.
376 namespace RISCVFPRndMode
{
387 inline static StringRef
roundingModeToString(RoundingMode RndMode
) {
390 llvm_unreachable("Unknown floating point rounding mode");
391 case RISCVFPRndMode::RNE
:
393 case RISCVFPRndMode::RTZ
:
395 case RISCVFPRndMode::RDN
:
397 case RISCVFPRndMode::RUP
:
399 case RISCVFPRndMode::RMM
:
401 case RISCVFPRndMode::DYN
:
406 inline static RoundingMode
stringToRoundingMode(StringRef Str
) {
407 return StringSwitch
<RoundingMode
>(Str
)
408 .Case("rne", RISCVFPRndMode::RNE
)
409 .Case("rtz", RISCVFPRndMode::RTZ
)
410 .Case("rdn", RISCVFPRndMode::RDN
)
411 .Case("rup", RISCVFPRndMode::RUP
)
412 .Case("rmm", RISCVFPRndMode::RMM
)
413 .Case("dyn", RISCVFPRndMode::DYN
)
414 .Default(RISCVFPRndMode::Invalid
);
417 inline static bool isValidRoundingMode(unsigned Mode
) {
421 case RISCVFPRndMode::RNE
:
422 case RISCVFPRndMode::RTZ
:
423 case RISCVFPRndMode::RDN
:
424 case RISCVFPRndMode::RUP
:
425 case RISCVFPRndMode::RMM
:
426 case RISCVFPRndMode::DYN
:
430 } // namespace RISCVFPRndMode
432 namespace RISCVVXRndMode
{
439 } // namespace RISCVVXRndMode
441 //===----------------------------------------------------------------------===//
442 // Floating-point Immediates
445 namespace RISCVLoadFPImm
{
446 float getFPImm(unsigned Imm
);
448 /// getLoadFPImm - Return a 5-bit binary encoding of the floating-point
449 /// immediate value. If the value cannot be represented as a 5-bit binary
450 /// encoding, then return -1.
451 int getLoadFPImm(APFloat FPImm
);
452 } // namespace RISCVLoadFPImm
454 namespace RISCVSysReg
{
458 // FIXME: add these additional fields when needed.
459 // Privilege Access: Read, Write, Read-Only.
460 // unsigned ReadWrite;
461 // Privilege Mode: User, System or Machine.
465 // Register number without the privilege bits.
467 FeatureBitset FeaturesRequired
;
470 bool IsDeprecatedName
;
472 bool haveRequiredFeatures(const FeatureBitset
&ActiveFeatures
) const {
473 // Not in 32-bit mode.
474 if (IsRV32Only
&& ActiveFeatures
[RISCV::Feature64Bit
])
476 // No required feature associated with the system register.
477 if (FeaturesRequired
.none())
479 return (FeaturesRequired
& ActiveFeatures
) == FeaturesRequired
;
483 #define GET_SysRegEncodings_DECL
484 #define GET_SysRegsList_DECL
485 #include "RISCVGenSearchableTables.inc"
486 } // end namespace RISCVSysReg
488 namespace RISCVInsnOpcode
{
494 #define GET_RISCVOpcodesList_DECL
495 #include "RISCVGenSearchableTables.inc"
496 } // end namespace RISCVInsnOpcode
512 // Returns the target ABI, or else a StringError if the requested ABIName is
513 // not supported for the given TT and FeatureBits combination.
514 ABI
computeTargetABI(const Triple
&TT
, const FeatureBitset
&FeatureBits
,
517 ABI
getTargetABI(StringRef ABIName
);
519 // Returns the register used to hold the stack pointer after realignment.
520 MCRegister
getBPReg();
522 // Returns the register holding shadow call stack pointer.
523 MCRegister
getSCSPReg();
525 } // namespace RISCVABI
527 namespace RISCVFeatures
{
529 // Validates if the given combination of features are valid for the target
530 // triple. Exits with report_fatal_error if not.
531 void validate(const Triple
&TT
, const FeatureBitset
&FeatureBits
);
533 llvm::Expected
<std::unique_ptr
<RISCVISAInfo
>>
534 parseFeatureBits(bool IsRV64
, const FeatureBitset
&FeatureBits
);
536 } // namespace RISCVFeatures
539 bool compress(MCInst
&OutInst
, const MCInst
&MI
, const MCSubtargetInfo
&STI
);
540 bool uncompress(MCInst
&OutInst
, const MCInst
&MI
, const MCSubtargetInfo
&STI
);
541 } // namespace RISCVRVC
556 // note - to include s10, s11 must also be included
561 inline unsigned encodeRlist(MCRegister EndReg
, bool IsRV32E
= false) {
562 assert((!IsRV32E
|| EndReg
<= RISCV::X9
) && "Invalid Rlist for RV32E");
565 return RLISTENCODE::RA
;
567 return RLISTENCODE::RA_S0
;
569 return RLISTENCODE::RA_S0_S1
;
571 return RLISTENCODE::RA_S0_S2
;
573 return RLISTENCODE::RA_S0_S3
;
575 return RLISTENCODE::RA_S0_S4
;
577 return RLISTENCODE::RA_S0_S5
;
579 return RLISTENCODE::RA_S0_S6
;
581 return RLISTENCODE::RA_S0_S7
;
583 return RLISTENCODE::RA_S0_S8
;
585 return RLISTENCODE::RA_S0_S9
;
587 return RLISTENCODE::INVALID_RLIST
;
589 return RLISTENCODE::RA_S0_S11
;
591 llvm_unreachable("Undefined input.");
595 inline static unsigned getStackAdjBase(unsigned RlistVal
, bool IsRV64
) {
596 assert(RlistVal
!= RLISTENCODE::INVALID_RLIST
&&
597 "{ra, s0-s10} is not supported, s11 must be included.");
600 case RLISTENCODE::RA
:
601 case RLISTENCODE::RA_S0
:
602 case RLISTENCODE::RA_S0_S1
:
603 case RLISTENCODE::RA_S0_S2
:
605 case RLISTENCODE::RA_S0_S3
:
606 case RLISTENCODE::RA_S0_S4
:
607 case RLISTENCODE::RA_S0_S5
:
608 case RLISTENCODE::RA_S0_S6
:
610 case RLISTENCODE::RA_S0_S7
:
611 case RLISTENCODE::RA_S0_S8
:
612 case RLISTENCODE::RA_S0_S9
:
614 case RLISTENCODE::RA_S0_S11
:
619 case RLISTENCODE::RA
:
620 case RLISTENCODE::RA_S0
:
622 case RLISTENCODE::RA_S0_S1
:
623 case RLISTENCODE::RA_S0_S2
:
625 case RLISTENCODE::RA_S0_S3
:
626 case RLISTENCODE::RA_S0_S4
:
628 case RLISTENCODE::RA_S0_S5
:
629 case RLISTENCODE::RA_S0_S6
:
631 case RLISTENCODE::RA_S0_S7
:
632 case RLISTENCODE::RA_S0_S8
:
634 case RLISTENCODE::RA_S0_S9
:
636 case RLISTENCODE::RA_S0_S11
:
640 llvm_unreachable("Unexpected RlistVal");
643 inline static bool getSpimm(unsigned RlistVal
, unsigned &SpimmVal
,
644 int64_t StackAdjustment
, bool IsRV64
) {
645 if (RlistVal
== RLISTENCODE::INVALID_RLIST
)
647 unsigned StackAdjBase
= getStackAdjBase(RlistVal
, IsRV64
);
648 StackAdjustment
-= StackAdjBase
;
649 if (StackAdjustment
% 16 != 0)
651 SpimmVal
= StackAdjustment
/ 16;
657 void printRlist(unsigned SlistEncode
, raw_ostream
&OS
);
658 } // namespace RISCVZC