LLVM 22.0.0git
RISCVBaseInfo.h
Go to the documentation of this file.
1//===-- RISCVBaseInfo.h - Top level definitions for RISC-V MC ---*- 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 contains small standalone enum definitions for the RISC-V target
10// useful for the compiler back-end and the MC libraries.
11//
12//===----------------------------------------------------------------------===//
13#ifndef LLVM_LIB_TARGET_RISCV_MCTARGETDESC_RISCVBASEINFO_H
14#define LLVM_LIB_TARGET_RISCV_MCTARGETDESC_RISCVBASEINFO_H
15
17#include "llvm/ADT/APFloat.h"
18#include "llvm/ADT/APInt.h"
19#include "llvm/ADT/StringRef.h"
21#include "llvm/MC/MCInstrDesc.h"
25
26namespace llvm {
27
28// RISCVII - This namespace holds all of the target specific flags that
29// instruction info tracks. All definitions must match RISCVInstrFormats.td.
30namespace RISCVII {
31enum {
61
64
70
73
74 // Is this a _TIED vector pseudo instruction. For these instructions we
75 // shouldn't skip the tied operand when converting to MC instructions.
78
79 // Does this instruction have a SEW operand. It will be the last explicit
80 // operand unless there is a vector policy operand. Used by RVV Pseudos.
83
84 // Does this instruction have a VL operand. It will be the second to last
85 // explicit operand unless there is a vector policy operand. Used by RVV
86 // Pseudos.
89
90 // Does this instruction have a vector policy operand. It will be the last
91 // explicit operand. Used by RVV Pseudos.
94
95 // Is this instruction a vector widening reduction instruction. Used by RVV
96 // Pseudos.
99
100 // Does this instruction care about mask policy. If it is not, the mask policy
101 // could be either agnostic or undisturbed. For example, unmasked, store, and
102 // reduction operations result would not be affected by mask policy, so
103 // compiler has free to select either one.
106
107 // Indicates that the result can be considered sign extended from bit 31. Some
108 // instructions with this flag aren't W instructions, but are either sign
109 // extended from a smaller size, always outputs a small integer, or put zeros
110 // in bits 63:31. Used by the SExtWRemoval pass.
113
116
119
120 // Indicates whether these instructions can partially overlap between source
121 // registers and destination registers according to the vector spec.
122 // 0 -> not a vector pseudo
123 // 1 -> default value for vector pseudos. not widening or narrowing.
124 // 2 -> narrowing case
125 // 3 -> widening case
128
131
134
135 // Indicates the EEW of a vector instruction's destination operand.
136 // 0 -> 1
137 // 1 -> SEW
138 // 2 -> SEW * 2
139 // 3 -> SEW * 4
142
145};
146
147// Helper functions to read TSFlags.
148/// \returns the format of the instruction.
149static inline unsigned getFormat(uint64_t TSFlags) {
150 return (TSFlags & InstFormatMask) >> InstFormatShift;
151}
152/// \returns the LMUL for the instruction.
153static inline RISCVVType::VLMUL getLMul(uint64_t TSFlags) {
154 return static_cast<RISCVVType::VLMUL>((TSFlags & VLMulMask) >> VLMulShift);
155}
156/// \returns true if this a _TIED pseudo.
157static inline bool isTiedPseudo(uint64_t TSFlags) {
158 return TSFlags & IsTiedPseudoMask;
159}
160/// \returns true if there is a SEW operand for the instruction.
161static inline bool hasSEWOp(uint64_t TSFlags) {
162 return TSFlags & HasSEWOpMask;
163}
164/// \returns true if there is a VL operand for the instruction.
165static inline bool hasVLOp(uint64_t TSFlags) {
166 return TSFlags & HasVLOpMask;
167}
168/// \returns true if there is a vector policy operand for this instruction.
169static inline bool hasVecPolicyOp(uint64_t TSFlags) {
170 return TSFlags & HasVecPolicyOpMask;
171}
172/// \returns true if it is a vector widening reduction instruction.
173static inline bool isRVVWideningReduction(uint64_t TSFlags) {
174 return TSFlags & IsRVVWideningReductionMask;
175}
176/// \returns true if mask policy is valid for the instruction.
177static inline bool usesMaskPolicy(uint64_t TSFlags) {
178 return TSFlags & UsesMaskPolicyMask;
179}
180
181/// \returns true if there is a rounding mode operand for this instruction
182static inline bool hasRoundModeOp(uint64_t TSFlags) {
183 return TSFlags & HasRoundModeOpMask;
184}
185
186/// \returns true if this instruction uses vxrm
187static inline bool usesVXRM(uint64_t TSFlags) { return TSFlags & UsesVXRMMask; }
188
189/// \returns true if the elements in the body are affected by VL,
190/// e.g. vslide1down.vx/vredsum.vs/viota.m
191static inline bool elementsDependOnVL(uint64_t TSFlags) {
192 return TSFlags & ElementsDependOnVLMask;
193}
194
195/// \returns true if the elements in the body are affected by the mask,
196/// e.g. vredsum.vs/viota.m
197static inline bool elementsDependOnMask(uint64_t TSFlags) {
198 return TSFlags & ElementsDependOnMaskMask;
199}
200
201/// \returns true if the instruction may read elements past VL, e.g.
202/// vslidedown/vrgather
203static inline bool readsPastVL(uint64_t TSFlags) {
204 return TSFlags & ReadsPastVLMask;
205}
206
207static inline unsigned getVLOpNum(const MCInstrDesc &Desc) {
208 const uint64_t TSFlags = Desc.TSFlags;
209 // This method is only called if we expect to have a VL operand, and all
210 // instructions with VL also have SEW.
211 assert(hasSEWOp(TSFlags) && hasVLOp(TSFlags));
212 unsigned Offset = 2;
213 if (hasVecPolicyOp(TSFlags))
214 Offset = 3;
215 return Desc.getNumOperands() - Offset;
216}
217
218static inline MCRegister
220 // For Zicfilp, PseudoTAIL should be expanded to a software guarded branch.
221 // It means to use t2(x7) as rs1 of JALR to expand PseudoTAIL.
222 return FeatureBits[RISCV::FeatureStdExtZicfilp] ? RISCV::X7 : RISCV::X6;
223}
224
225static inline unsigned getSEWOpNum(const MCInstrDesc &Desc) {
226 const uint64_t TSFlags = Desc.TSFlags;
227 assert(hasSEWOp(TSFlags));
228 unsigned Offset = 1;
229 if (hasVecPolicyOp(TSFlags))
230 Offset = 2;
231 return Desc.getNumOperands() - Offset;
232}
233
234static inline unsigned getVecPolicyOpNum(const MCInstrDesc &Desc) {
235 assert(hasVecPolicyOp(Desc.TSFlags));
236 return Desc.getNumOperands() - 1;
237}
238
239/// \returns the index to the rounding mode immediate value if any, otherwise
240/// returns -1.
241static inline int getFRMOpNum(const MCInstrDesc &Desc) {
242 const uint64_t TSFlags = Desc.TSFlags;
243 if (!hasRoundModeOp(TSFlags) || usesVXRM(TSFlags))
244 return -1;
245
246 // The operand order
247 // --------------------------------------
248 // | n-1 (if any) | n-2 | n-3 | n-4 |
249 // | policy | sew | vl | frm |
250 // --------------------------------------
251 return getVLOpNum(Desc) - 1;
252}
253
254/// \returns the index to the rounding mode immediate value if any, otherwise
255/// returns -1.
256static inline int getVXRMOpNum(const MCInstrDesc &Desc) {
257 const uint64_t TSFlags = Desc.TSFlags;
258 if (!hasRoundModeOp(TSFlags) || !usesVXRM(TSFlags))
259 return -1;
260 // The operand order
261 // --------------------------------------
262 // | n-1 (if any) | n-2 | n-3 | n-4 |
263 // | policy | sew | vl | vxrm |
264 // --------------------------------------
265 return getVLOpNum(Desc) - 1;
266}
267
268// Is the first def operand tied to the first use operand. This is true for
269// vector pseudo instructions that have a merge operand for tail/mask
270// undisturbed. It's also true for vector FMA instructions where one of the
271// operands is also the destination register.
272static inline bool isFirstDefTiedToFirstUse(const MCInstrDesc &Desc) {
273 return Desc.getNumDefs() < Desc.getNumOperands() &&
274 Desc.getOperandConstraint(Desc.getNumDefs(), MCOI::TIED_TO) == 0;
275}
276
277// RISC-V Specific Machine Operand Flags
278enum {
281 MO_LO = 3,
282 MO_HI = 4,
295
296 // Used to differentiate between target-specific "direct" flags and "bitmask"
297 // flags. A machine operand can only have one "direct" flag, but can have
298 // multiple "bitmask" flags.
301} // namespace RISCVII
302
303namespace RISCVOp {
304enum OperandType : unsigned {
372 // Operand is a 3-bit rounding mode, '111' indicates FRM register.
373 // Represents 'frm' argument passing to floating-point operations.
375 // Operand is a 3-bit rounding mode where only RTZ is valid.
377 // Condition code used by select and short forward branch pseudos.
379 // Vector policy operand.
381 // Vector SEW operand. Stores in log2(SEW).
383 // Special SEW for mask only instructions. Always 0.
385 // Vector rounding mode for VXRM or FRM.
388 // Operand is either a register or uimm5, this is used by V extension pseudo
389 // instructions to represent a value that be passed as AVL to either vsetvli
390 // or vsetivli.
392};
393} // namespace RISCVOp
394
395// Describes the predecessor/successor bits used in the FENCE instruction.
396namespace RISCVFenceField {
398 I = 8,
399 O = 4,
400 R = 2,
401 W = 1
403}
404
405// Describes the supported floating point rounding mode encodings.
406namespace RISCVFPRndMode {
408 RNE = 0,
409 RTZ = 1,
410 RDN = 2,
411 RUP = 3,
412 RMM = 4,
413 DYN = 7,
414 Invalid
416
418 switch (RndMode) {
419 default:
420 llvm_unreachable("Unknown floating point rounding mode");
422 return "rne";
424 return "rtz";
426 return "rdn";
428 return "rup";
430 return "rmm";
432 return "dyn";
433 }
434}
435
445}
446
447inline static bool isValidRoundingMode(unsigned Mode) {
448 switch (Mode) {
449 default:
450 return false;
457 return true;
458 }
459}
460} // namespace RISCVFPRndMode
461
462namespace RISCVVXRndMode {
464 RNU = 0,
465 RNE = 1,
466 RDN = 2,
467 ROD = 3,
468 Invalid
470
472 switch (RndMode) {
473 default:
474 llvm_unreachable("Unknown vector fixed-point rounding mode");
476 return "rnu";
478 return "rne";
480 return "rdn";
482 return "rod";
483 }
484}
485
493}
494
495inline static bool isValidRoundingMode(unsigned Mode) {
496 switch (Mode) {
497 default:
498 return false;
503 return true;
504 }
505}
506} // namespace RISCVVXRndMode
507
508namespace RISCVExceptFlags {
510 NX = 0x01, // Inexact
511 UF = 0x02, // Underflow
512 OF = 0x04, // Overflow
513 DZ = 0x08, // Divide by zero
514 NV = 0x10, // Invalid operation
515 ALL = 0x1F // Mask for all accrued exception flags
517}
518
519//===----------------------------------------------------------------------===//
520// Floating-point Immediates
521//
522
523namespace RISCVLoadFPImm {
524float getFPImm(unsigned Imm);
525
526/// getLoadFPImm - Return a 5-bit binary encoding of the floating-point
527/// immediate value. If the value cannot be represented as a 5-bit binary
528/// encoding, then return -1.
529int getLoadFPImm(APFloat FPImm);
530} // namespace RISCVLoadFPImm
531
532namespace RISCVSysReg {
533struct SysReg {
534 const char Name[32];
535 unsigned Encoding;
536 // FIXME: add these additional fields when needed.
537 // Privilege Access: Read, Write, Read-Only.
538 // unsigned ReadWrite;
539 // Privilege Mode: User, System or Machine.
540 // unsigned Mode;
541 // Check field name.
542 // unsigned Extra;
543 // Register number without the privilege bits.
544 // unsigned Number;
549
550 bool haveRequiredFeatures(const FeatureBitset &ActiveFeatures) const {
551 // Not in 32-bit mode.
552 if (IsRV32Only && ActiveFeatures[RISCV::Feature64Bit])
553 return false;
554 // No required feature associated with the system register.
556 return true;
557 return (FeaturesRequired & ActiveFeatures) == FeaturesRequired;
558 }
559};
560
561#define GET_SysRegEncodings_DECL
562#define GET_SysRegsList_DECL
563#include "RISCVGenSearchableTables.inc"
564} // end namespace RISCVSysReg
565
566namespace RISCVInsnOpcode {
568 char Name[10];
570};
571
572#define GET_RISCVOpcodesList_DECL
573#include "RISCVGenSearchableTables.inc"
574} // end namespace RISCVInsnOpcode
575
576namespace RISCVABI {
577
578enum ABI {
589
590// Returns the target ABI, or else a StringError if the requested ABIName is
591// not supported for the given TT and FeatureBits combination.
592ABI computeTargetABI(const Triple &TT, const FeatureBitset &FeatureBits,
593 StringRef ABIName);
594
595ABI getTargetABI(StringRef ABIName);
596
597// Returns the register used to hold the stack pointer after realignment.
599
600// Returns the register holding shadow call stack pointer.
602
603} // namespace RISCVABI
604
605namespace RISCVFeatures {
606
607// Validates if the given combination of features are valid for the target
608// triple. Exits with report_fatal_error if not.
609void validate(const Triple &TT, const FeatureBitset &FeatureBits);
610
612parseFeatureBits(bool IsRV64, const FeatureBitset &FeatureBits);
613
614} // namespace RISCVFeatures
615
616namespace RISCVRVC {
617bool compress(MCInst &OutInst, const MCInst &MI, const MCSubtargetInfo &STI);
618bool uncompress(MCInst &OutInst, const MCInst &MI, const MCSubtargetInfo &STI);
619} // namespace RISCVRVC
620
621namespace RISCVZC {
623 RA = 4,
634 // note - to include s10, s11 must also be included
637};
638
639inline unsigned encodeRegList(MCRegister EndReg, bool IsRVE = false) {
640 assert((!IsRVE || EndReg <= RISCV::X9) && "Invalid Rlist for RV32E");
641 switch (EndReg) {
642 case RISCV::X1:
643 return RLISTENCODE::RA;
644 case RISCV::X8:
645 return RLISTENCODE::RA_S0;
646 case RISCV::X9:
648 case RISCV::X18:
650 case RISCV::X19:
652 case RISCV::X20:
654 case RISCV::X21:
656 case RISCV::X22:
658 case RISCV::X23:
660 case RISCV::X24:
662 case RISCV::X25:
664 case RISCV::X27:
666 default:
667 llvm_unreachable("Undefined input.");
668 }
669}
670
671inline static unsigned encodeRegListNumRegs(unsigned NumRegs) {
672 assert(NumRegs > 0 && NumRegs < 14 && NumRegs != 12 &&
673 "Unexpected number of registers");
674 if (NumRegs == 13)
676
677 return RLISTENCODE::RA + (NumRegs - 1);
678}
679
680inline static unsigned getStackAdjBase(unsigned RlistVal, bool IsRV64) {
681 assert(RlistVal >= RLISTENCODE::RA && RlistVal <= RLISTENCODE::RA_S0_S11 &&
682 "Invalid Rlist");
683 unsigned NumRegs = (RlistVal - RLISTENCODE::RA) + 1;
684 // s10 and s11 are saved together.
685 if (RlistVal == RLISTENCODE::RA_S0_S11)
686 ++NumRegs;
687
688 unsigned RegSize = IsRV64 ? 8 : 4;
689 return alignTo(NumRegs * RegSize, 16);
690}
691
692void printRegList(unsigned RlistEncode, raw_ostream &OS);
693} // namespace RISCVZC
694
695namespace RISCVVInversePseudosTable {
701};
702
703#define GET_RISCVVInversePseudosTable_DECL
704#include "RISCVGenSearchableTables.inc"
705} // namespace RISCVVInversePseudosTable
706
707namespace RISCV {
716};
717
726};
727
735};
736
745};
746
747struct VLEPseudo {
754};
755
756struct VSEPseudo {
762};
763
771};
772
779};
780
781#define GET_RISCVVSSEGTable_DECL
782#define GET_RISCVVLSEGTable_DECL
783#define GET_RISCVVLXSEGTable_DECL
784#define GET_RISCVVSXSEGTable_DECL
785#define GET_RISCVVLETable_DECL
786#define GET_RISCVVSETable_DECL
787#define GET_RISCVVLXTable_DECL
788#define GET_RISCVVSXTable_DECL
789#define GET_RISCVNDSVLNTable_DECL
790#include "RISCVGenSearchableTables.inc"
791} // namespace RISCV
792
793} // namespace llvm
794
795#endif
unsigned RegSize
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
This file declares a class to represent arbitrary precision floating point values and provide a varie...
This file implements a class to represent arbitrary precision integral constant values and operations...
IRTranslator LLVM IR MI
static cl::opt< RegAllocEvictionAdvisorAnalysisLegacy::AdvisorMode > Mode("regalloc-enable-advisor", cl::Hidden, cl::init(RegAllocEvictionAdvisorAnalysisLegacy::AdvisorMode::Default), cl::desc("Enable regalloc advisor mode"), cl::values(clEnumValN(RegAllocEvictionAdvisorAnalysisLegacy::AdvisorMode::Default, "default", "Default"), clEnumValN(RegAllocEvictionAdvisorAnalysisLegacy::AdvisorMode::Release, "release", "precompiled"), clEnumValN(RegAllocEvictionAdvisorAnalysisLegacy::AdvisorMode::Development, "development", "for training")))
raw_pwrite_stream & OS
This file implements the StringSwitch template, which mimics a switch() statement whose cases are str...
Tagged union holding either a T or a Error.
Definition: Error.h:485
Container class for subtarget features.
Instances of this class represent a single low-level machine instruction.
Definition: MCInst.h:188
Describe properties that are true of each instruction in the target description file.
Definition: MCInstrDesc.h:199
Wrapper class representing physical registers. Should be passed by value.
Definition: MCRegister.h:33
Generic base class for all target subtargets.
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:55
A switch()-like statement whose cases are string literals.
Definition: StringSwitch.h:43
StringSwitch & Case(StringLiteral S, T Value)
Definition: StringSwitch.h:68
R Default(T Value)
Definition: StringSwitch.h:177
Triple - Helper class for working with autoconf configuration names.
Definition: Triple.h:47
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:53
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
@ OPERAND_FIRST_TARGET
Definition: MCInstrDesc.h:79
ABI getTargetABI(StringRef ABIName)
ABI computeTargetABI(const Triple &TT, const FeatureBitset &FeatureBits, StringRef ABIName)
MCRegister getBPReg()
MCRegister getSCSPReg()
static bool isValidRoundingMode(unsigned Mode)
static RoundingMode stringToRoundingMode(StringRef Str)
static StringRef roundingModeToString(RoundingMode RndMode)
void validate(const Triple &TT, const FeatureBitset &FeatureBits)
llvm::Expected< std::unique_ptr< RISCVISAInfo > > parseFeatureBits(bool IsRV64, const FeatureBitset &FeatureBits)
static unsigned getVecPolicyOpNum(const MCInstrDesc &Desc)
static bool usesMaskPolicy(uint64_t TSFlags)
static bool hasRoundModeOp(uint64_t TSFlags)
static bool readsPastVL(uint64_t TSFlags)
static bool isTiedPseudo(uint64_t TSFlags)
static RISCVVType::VLMUL getLMul(uint64_t TSFlags)
static unsigned getVLOpNum(const MCInstrDesc &Desc)
static unsigned getFormat(uint64_t TSFlags)
static bool hasVLOp(uint64_t TSFlags)
static MCRegister getTailExpandUseRegNo(const FeatureBitset &FeatureBits)
static bool elementsDependOnMask(uint64_t TSFlags)
static int getFRMOpNum(const MCInstrDesc &Desc)
static int getVXRMOpNum(const MCInstrDesc &Desc)
static bool hasVecPolicyOp(uint64_t TSFlags)
static bool usesVXRM(uint64_t TSFlags)
static bool isRVVWideningReduction(uint64_t TSFlags)
static unsigned getSEWOpNum(const MCInstrDesc &Desc)
static bool elementsDependOnVL(uint64_t TSFlags)
static bool hasSEWOp(uint64_t TSFlags)
@ TargetOverlapConstraintTypeMask
@ TargetOverlapConstraintTypeShift
@ IsRVVWideningReductionShift
Definition: RISCVBaseInfo.h:97
@ IsRVVWideningReductionMask
Definition: RISCVBaseInfo.h:98
static bool isFirstDefTiedToFirstUse(const MCInstrDesc &Desc)
int getLoadFPImm(APFloat FPImm)
getLoadFPImm - Return a 5-bit binary encoding of the floating-point immediate value.
float getFPImm(unsigned Imm)
@ OPERAND_UIMMLOG2XLEN_NONZERO
@ OPERAND_UIMM10_LSB00_NONZERO
@ OPERAND_SIMM10_LSB0000_NONZERO
bool uncompress(MCInst &OutInst, const MCInst &MI, const MCSubtargetInfo &STI)
bool compress(MCInst &OutInst, const MCInst &MI, const MCSubtargetInfo &STI)
static bool isValidRoundingMode(unsigned Mode)
static RoundingMode stringToRoundingMode(StringRef Str)
static StringRef roundingModeToString(RoundingMode RndMode)
unsigned encodeRegList(MCRegister EndReg, bool IsRVE=false)
static unsigned getStackAdjBase(unsigned RlistVal, bool IsRV64)
void printRegList(unsigned RlistEncode, raw_ostream &OS)
static unsigned encodeRegListNumRegs(unsigned NumRegs)
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
@ Offset
Definition: DWP.cpp:477
uint64_t alignTo(uint64_t Size, Align A)
Returns a multiple of A needed to store Size bytes.
Definition: Alignment.h:155
Description of the encoding of one expression Op.
FeatureBitset FeaturesRequired
bool haveRequiredFeatures(const FeatureBitset &ActiveFeatures) const