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 // 0 -> Don't care about altfmt bit in VTYPE.
147 // 1 -> Is not altfmt.
148 // 2 -> Is altfmt(BF16).
151
152 // XSfmmbase
155
158
161};
162
163// Helper functions to read TSFlags.
164/// \returns the format of the instruction.
165static inline unsigned getFormat(uint64_t TSFlags) {
166 return (TSFlags & InstFormatMask) >> InstFormatShift;
167}
168/// \returns the LMUL for the instruction.
169static inline RISCVVType::VLMUL getLMul(uint64_t TSFlags) {
170 return static_cast<RISCVVType::VLMUL>((TSFlags & VLMulMask) >> VLMulShift);
171}
172/// \returns true if this a _TIED pseudo.
173static inline bool isTiedPseudo(uint64_t TSFlags) {
174 return TSFlags & IsTiedPseudoMask;
175}
176/// \returns true if there is a SEW operand for the instruction.
177static inline bool hasSEWOp(uint64_t TSFlags) {
178 return TSFlags & HasSEWOpMask;
179}
180/// \returns true if there is a VL operand for the instruction.
181static inline bool hasVLOp(uint64_t TSFlags) {
182 return TSFlags & HasVLOpMask;
183}
184/// \returns true if there is a vector policy operand for this instruction.
185static inline bool hasVecPolicyOp(uint64_t TSFlags) {
186 return TSFlags & HasVecPolicyOpMask;
187}
188/// \returns true if it is a vector widening reduction instruction.
189static inline bool isRVVWideningReduction(uint64_t TSFlags) {
190 return TSFlags & IsRVVWideningReductionMask;
191}
192/// \returns true if mask policy is valid for the instruction.
193static inline bool usesMaskPolicy(uint64_t TSFlags) {
194 return TSFlags & UsesMaskPolicyMask;
195}
196
197/// \returns true if there is a rounding mode operand for this instruction
198static inline bool hasRoundModeOp(uint64_t TSFlags) {
199 return TSFlags & HasRoundModeOpMask;
200}
201
203static inline AltFmtType getAltFmtType(uint64_t TSFlags) {
204 return static_cast<AltFmtType>((TSFlags & AltFmtTypeMask) >> AltFmtTypeShift);
205}
206
207/// \returns true if this instruction uses vxrm
208static inline bool usesVXRM(uint64_t TSFlags) { return TSFlags & UsesVXRMMask; }
209
210/// \returns true if the elements in the body are affected by VL,
211/// e.g. vslide1down.vx/vredsum.vs/viota.m
212static inline bool elementsDependOnVL(uint64_t TSFlags) {
213 return TSFlags & ElementsDependOnVLMask;
214}
215
216/// \returns true if the elements in the body are affected by the mask,
217/// e.g. vredsum.vs/viota.m
218static inline bool elementsDependOnMask(uint64_t TSFlags) {
219 return TSFlags & ElementsDependOnMaskMask;
220}
221
222/// \returns true if the instruction may read elements past VL, e.g.
223/// vslidedown/vrgather
224static inline bool readsPastVL(uint64_t TSFlags) {
225 return TSFlags & ReadsPastVLMask;
226}
227
228// XSfmmbase
229static inline bool hasTWidenOp(uint64_t TSFlags) {
230 return TSFlags & HasTWidenOpMask;
231}
232
233static inline bool hasTMOp(uint64_t TSFlags) { return TSFlags & HasTMOpMask; }
234
235static inline bool hasTKOp(uint64_t TSFlags) { return TSFlags & HasTKOpMask; }
236
237static inline unsigned getTNOpNum(const MCInstrDesc &Desc) {
238 const uint64_t TSFlags = Desc.TSFlags;
239 assert(hasTWidenOp(TSFlags) && hasVLOp(TSFlags));
240 unsigned Offset = 3;
241 if (hasTKOp(TSFlags))
242 Offset = 4;
243 return Desc.getNumOperands() - Offset;
244}
245
246static inline unsigned getTMOpNum(const MCInstrDesc &Desc) {
247 const uint64_t TSFlags = Desc.TSFlags;
248 assert(hasTWidenOp(TSFlags) && hasTMOp(TSFlags));
249 if (hasTKOp(TSFlags))
250 return Desc.getNumOperands() - 5;
251 // vtzero.t
252 return Desc.getNumOperands() - 4;
253}
254
255static inline unsigned getTKOpNum(const MCInstrDesc &Desc) {
256 [[maybe_unused]] const uint64_t TSFlags = Desc.TSFlags;
257 assert(hasTWidenOp(TSFlags) && hasTKOp(TSFlags));
258 return Desc.getNumOperands() - 3;
259}
260
261static inline unsigned getVLOpNum(const MCInstrDesc &Desc) {
262 const uint64_t TSFlags = Desc.TSFlags;
263 // This method is only called if we expect to have a VL operand, and all
264 // instructions with VL also have SEW.
265 assert(hasSEWOp(TSFlags) && hasVLOp(TSFlags));
266 // In Xsfmmbase, TN is an alias for VL, so here we use the same TSFlags bit.
267 if (hasTWidenOp(TSFlags))
268 return getTNOpNum(Desc);
269 unsigned Offset = 2;
270 if (hasVecPolicyOp(TSFlags))
271 Offset = 3;
272 return Desc.getNumOperands() - Offset;
273}
274
275static inline MCRegister
277 // For Zicfilp, PseudoTAIL should be expanded to a software guarded branch.
278 // It means to use t2(x7) as rs1 of JALR to expand PseudoTAIL.
279 return FeatureBits[RISCV::FeatureStdExtZicfilp] ? RISCV::X7 : RISCV::X6;
280}
281
282static inline unsigned getSEWOpNum(const MCInstrDesc &Desc) {
283 const uint64_t TSFlags = Desc.TSFlags;
284 assert(hasSEWOp(TSFlags));
285 unsigned Offset = 1;
286 if (hasVecPolicyOp(TSFlags) || hasTWidenOp(TSFlags))
287 Offset = 2;
288 return Desc.getNumOperands() - Offset;
289}
290
291static inline unsigned getVecPolicyOpNum(const MCInstrDesc &Desc) {
292 assert(hasVecPolicyOp(Desc.TSFlags));
293 return Desc.getNumOperands() - 1;
294}
295
296/// \returns the index to the rounding mode immediate value if any, otherwise
297/// returns -1.
298static inline int getFRMOpNum(const MCInstrDesc &Desc) {
299 const uint64_t TSFlags = Desc.TSFlags;
300 if (!hasRoundModeOp(TSFlags) || usesVXRM(TSFlags))
301 return -1;
302
303 if (hasTWidenOp(TSFlags) && hasTMOp(TSFlags))
304 return getTMOpNum(Desc) - 1;
305
306 // The operand order
307 // --------------------------------------
308 // | n-1 (if any) | n-2 | n-3 | n-4 |
309 // | policy | sew | vl | frm |
310 // --------------------------------------
311 return getVLOpNum(Desc) - 1;
312}
313
314/// \returns the index to the rounding mode immediate value if any, otherwise
315/// returns -1.
316static inline int getVXRMOpNum(const MCInstrDesc &Desc) {
317 const uint64_t TSFlags = Desc.TSFlags;
318 if (!hasRoundModeOp(TSFlags) || !usesVXRM(TSFlags))
319 return -1;
320 // The operand order
321 // --------------------------------------
322 // | n-1 (if any) | n-2 | n-3 | n-4 |
323 // | policy | sew | vl | vxrm |
324 // --------------------------------------
325 return getVLOpNum(Desc) - 1;
326}
327
328// Is the first def operand tied to the first use operand. This is true for
329// vector pseudo instructions that have a merge operand for tail/mask
330// undisturbed. It's also true for vector FMA instructions where one of the
331// operands is also the destination register.
332static inline bool isFirstDefTiedToFirstUse(const MCInstrDesc &Desc) {
333 return Desc.getNumDefs() < Desc.getNumOperands() &&
334 Desc.getOperandConstraint(Desc.getNumDefs(), MCOI::TIED_TO) == 0;
335}
336
337// RISC-V Specific Machine Operand Flags
338enum {
341 MO_LO = 3,
342 MO_HI = 4,
355
356 // Used to differentiate between target-specific "direct" flags and "bitmask"
357 // flags. A machine operand can only have one "direct" flag, but can have
358 // multiple "bitmask" flags.
360};
361} // namespace RISCVII
362
363namespace RISCVOp {
364enum OperandType : unsigned {
433 // Operand is a 3-bit rounding mode, '111' indicates FRM register.
434 // Represents 'frm' argument passing to floating-point operations.
436 // Operand is a 3-bit rounding mode where only RTZ is valid.
438 // Condition code used by select and short forward branch pseudos.
440 // Vector policy operand.
442 // Vector SEW operand. Stores in log2(SEW).
444 // Special SEW for mask only instructions. Always 0.
446 // Vector rounding mode for VXRM or FRM.
448 // Vtype operand for XSfmm extension.
451 // Operand is either a register or uimm5, this is used by V extension pseudo
452 // instructions to represent a value that be passed as AVL to either vsetvli
453 // or vsetivli.
455};
456} // namespace RISCVOp
457
458// Describes the predecessor/successor bits used in the FENCE instruction.
461 I = 8,
462 O = 4,
463 R = 2,
464 W = 1
465};
466}
467
468// Describes the supported floating point rounding mode encodings.
469namespace RISCVFPRndMode {
471 RNE = 0,
472 RTZ = 1,
473 RDN = 2,
474 RUP = 3,
475 RMM = 4,
476 DYN = 7,
478};
479
481 switch (RndMode) {
482 default:
483 llvm_unreachable("Unknown floating point rounding mode");
485 return "rne";
487 return "rtz";
489 return "rdn";
491 return "rup";
493 return "rmm";
495 return "dyn";
496 }
497}
498
509
510inline static bool isValidRoundingMode(unsigned Mode) {
511 switch (Mode) {
512 default:
513 return false;
520 return true;
521 }
522}
523} // namespace RISCVFPRndMode
524
525namespace RISCVVXRndMode {
527 RNU = 0,
528 RNE = 1,
529 RDN = 2,
530 ROD = 3,
532};
533
535 switch (RndMode) {
536 default:
537 llvm_unreachable("Unknown vector fixed-point rounding mode");
539 return "rnu";
541 return "rne";
543 return "rdn";
545 return "rod";
546 }
547}
548
557
558inline static bool isValidRoundingMode(unsigned Mode) {
559 switch (Mode) {
560 default:
561 return false;
566 return true;
567 }
568}
569} // namespace RISCVVXRndMode
570
573 NX = 0x01, // Inexact
574 UF = 0x02, // Underflow
575 OF = 0x04, // Overflow
576 DZ = 0x08, // Divide by zero
577 NV = 0x10, // Invalid operation
578 ALL = 0x1F // Mask for all accrued exception flags
579};
580}
581
582//===----------------------------------------------------------------------===//
583// Floating-point Immediates
584//
585
586namespace RISCVLoadFPImm {
587float getFPImm(unsigned Imm);
588
589/// getLoadFPImm - Return a 5-bit binary encoding of the floating-point
590/// immediate value. If the value cannot be represented as a 5-bit binary
591/// encoding, then return -1.
592int getLoadFPImm(APFloat FPImm);
593} // namespace RISCVLoadFPImm
594
595namespace RISCVSysReg {
596struct SysReg {
597 const char Name[32];
598 unsigned Encoding;
599 // FIXME: add these additional fields when needed.
600 // Privilege Access: Read, Write, Read-Only.
601 // unsigned ReadWrite;
602 // Privilege Mode: User, System or Machine.
603 // unsigned Mode;
604 // Check field name.
605 // unsigned Extra;
606 // Register number without the privilege bits.
607 // unsigned Number;
612
613 bool haveRequiredFeatures(const FeatureBitset &ActiveFeatures) const {
614 // Not in 32-bit mode.
615 if (IsRV32Only && ActiveFeatures[RISCV::Feature64Bit])
616 return false;
617 // No required feature associated with the system register.
618 if (FeaturesRequired.none())
619 return true;
620 return (FeaturesRequired & ActiveFeatures) == FeaturesRequired;
621 }
622};
623
624#define GET_SysRegEncodings_DECL
625#define GET_SysRegsList_DECL
626#include "RISCVGenSearchableTables.inc"
627} // end namespace RISCVSysReg
628
629namespace RISCVInsnOpcode {
631 char Name[10];
633};
634
635#define GET_RISCVOpcodesList_DECL
636#include "RISCVGenSearchableTables.inc"
637} // end namespace RISCVInsnOpcode
638
639namespace RISCVABI {
640
652
653// Returns the target ABI, or else a StringError if the requested ABIName is
654// not supported for the given TT and FeatureBits combination.
655ABI computeTargetABI(const Triple &TT, const FeatureBitset &FeatureBits,
656 StringRef ABIName);
657
658ABI getTargetABI(StringRef ABIName);
659
660// Returns the register used to hold the stack pointer after realignment.
662
663// Returns the register holding shadow call stack pointer.
665
666} // namespace RISCVABI
667
668namespace RISCVFeatures {
669
670// Validates if the given combination of features are valid for the target
671// triple. Exits with report_fatal_error if not.
672void validate(const Triple &TT, const FeatureBitset &FeatureBits);
673
675parseFeatureBits(bool IsRV64, const FeatureBitset &FeatureBits);
676
677} // namespace RISCVFeatures
678
679namespace RISCVRVC {
680bool compress(MCInst &OutInst, const MCInst &MI, const MCSubtargetInfo &STI);
681bool uncompress(MCInst &OutInst, const MCInst &MI, const MCSubtargetInfo &STI);
682} // namespace RISCVRVC
683
684namespace RISCVZC {
701
702inline unsigned encodeRegList(MCRegister EndReg, bool IsRVE = false) {
703 assert((!IsRVE || EndReg <= RISCV::X9) && "Invalid Rlist for RV32E");
704 switch (EndReg) {
705 case RISCV::X1:
706 return RLISTENCODE::RA;
707 case RISCV::X8:
708 return RLISTENCODE::RA_S0;
709 case RISCV::X9:
711 case RISCV::X18:
713 case RISCV::X19:
715 case RISCV::X20:
717 case RISCV::X21:
719 case RISCV::X22:
721 case RISCV::X23:
723 case RISCV::X24:
725 case RISCV::X25:
727 case RISCV::X27:
729 default:
730 llvm_unreachable("Undefined input.");
731 }
732}
733
734inline static unsigned encodeRegListNumRegs(unsigned NumRegs) {
735 assert(NumRegs > 0 && NumRegs < 14 && NumRegs != 12 &&
736 "Unexpected number of registers");
737 if (NumRegs == 13)
739
740 return RLISTENCODE::RA + (NumRegs - 1);
741}
742
743inline static unsigned getStackAdjBase(unsigned RlistVal, bool IsRV64) {
744 assert(RlistVal >= RLISTENCODE::RA && RlistVal <= RLISTENCODE::RA_S0_S11 &&
745 "Invalid Rlist");
746 unsigned NumRegs = (RlistVal - RLISTENCODE::RA) + 1;
747 // s10 and s11 are saved together.
748 if (RlistVal == RLISTENCODE::RA_S0_S11)
749 ++NumRegs;
750
751 unsigned RegSize = IsRV64 ? 8 : 4;
752 return alignTo(NumRegs * RegSize, 16);
753}
754
755void printRegList(unsigned RlistEncode, raw_ostream &OS);
756} // namespace RISCVZC
757
758namespace RISCVVInversePseudosTable {
765
766#define GET_RISCVVInversePseudosTable_DECL
767#include "RISCVGenSearchableTables.inc"
768} // namespace RISCVVInversePseudosTable
769
770namespace RISCV {
780
790
799
809
818
826
835
843
844#define GET_RISCVVSSEGTable_DECL
845#define GET_RISCVVLSEGTable_DECL
846#define GET_RISCVVLXSEGTable_DECL
847#define GET_RISCVVSXSEGTable_DECL
848#define GET_RISCVVLETable_DECL
849#define GET_RISCVVSETable_DECL
850#define GET_RISCVVLXTable_DECL
851#define GET_RISCVVSXTable_DECL
852#define GET_RISCVNDSVLNTable_DECL
853#include "RISCVGenSearchableTables.inc"
854} // namespace RISCV
855
856} // namespace llvm
857
858#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")))
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.
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.
StringSwitch & Case(StringLiteral S, T Value)
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:80
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 unsigned getTMOpNum(const MCInstrDesc &Desc)
static bool usesMaskPolicy(uint64_t TSFlags)
static bool hasRoundModeOp(uint64_t TSFlags)
@ TargetOverlapConstraintTypeMask
@ TargetOverlapConstraintTypeShift
@ IsRVVWideningReductionShift
static bool readsPastVL(uint64_t TSFlags)
static bool hasTWidenOp(uint64_t TSFlags)
static bool isTiedPseudo(uint64_t TSFlags)
static RISCVVType::VLMUL getLMul(uint64_t TSFlags)
static unsigned getTKOpNum(const MCInstrDesc &Desc)
static unsigned getVLOpNum(const MCInstrDesc &Desc)
static AltFmtType getAltFmtType(uint64_t TSFlags)
static unsigned getFormat(uint64_t TSFlags)
static bool hasTKOp(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 bool hasTMOp(uint64_t TSFlags)
static int getVXRMOpNum(const MCInstrDesc &Desc)
static unsigned getTNOpNum(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)
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_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.
@ Offset
Definition DWP.cpp:477
Op::Description Desc
uint64_t alignTo(uint64_t Size, Align A)
Returns a multiple of A needed to store Size bytes.
Definition Alignment.h:144
bool haveRequiredFeatures(const FeatureBitset &ActiveFeatures) const