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 {
432 // Operand is a 3-bit rounding mode, '111' indicates FRM register.
433 // Represents 'frm' argument passing to floating-point operations.
435 // Operand is a 3-bit rounding mode where only RTZ is valid.
437 // Condition code used by select and short forward branch pseudos.
439 // Vector policy operand.
441 // Vector SEW operand. Stores in log2(SEW).
443 // Special SEW for mask only instructions. Always 0.
445 // Vector rounding mode for VXRM or FRM.
447 // Vtype operand for XSfmm extension.
450 // Operand is either a register or uimm5, this is used by V extension pseudo
451 // instructions to represent a value that be passed as AVL to either vsetvli
452 // or vsetivli.
454};
455} // namespace RISCVOp
456
457// Describes the predecessor/successor bits used in the FENCE instruction.
460 I = 8,
461 O = 4,
462 R = 2,
463 W = 1
464};
465}
466
467// Describes the supported floating point rounding mode encodings.
468namespace RISCVFPRndMode {
470 RNE = 0,
471 RTZ = 1,
472 RDN = 2,
473 RUP = 3,
474 RMM = 4,
475 DYN = 7,
477};
478
480 switch (RndMode) {
481 default:
482 llvm_unreachable("Unknown floating point rounding mode");
484 return "rne";
486 return "rtz";
488 return "rdn";
490 return "rup";
492 return "rmm";
494 return "dyn";
495 }
496}
497
508
509inline static bool isValidRoundingMode(unsigned Mode) {
510 switch (Mode) {
511 default:
512 return false;
519 return true;
520 }
521}
522} // namespace RISCVFPRndMode
523
524namespace RISCVVXRndMode {
526 RNU = 0,
527 RNE = 1,
528 RDN = 2,
529 ROD = 3,
531};
532
534 switch (RndMode) {
535 default:
536 llvm_unreachable("Unknown vector fixed-point rounding mode");
538 return "rnu";
540 return "rne";
542 return "rdn";
544 return "rod";
545 }
546}
547
556
557inline static bool isValidRoundingMode(unsigned Mode) {
558 switch (Mode) {
559 default:
560 return false;
565 return true;
566 }
567}
568} // namespace RISCVVXRndMode
569
572 NX = 0x01, // Inexact
573 UF = 0x02, // Underflow
574 OF = 0x04, // Overflow
575 DZ = 0x08, // Divide by zero
576 NV = 0x10, // Invalid operation
577 ALL = 0x1F // Mask for all accrued exception flags
578};
579}
580
581//===----------------------------------------------------------------------===//
582// Floating-point Immediates
583//
584
585namespace RISCVLoadFPImm {
586float getFPImm(unsigned Imm);
587
588/// getLoadFPImm - Return a 5-bit binary encoding of the floating-point
589/// immediate value. If the value cannot be represented as a 5-bit binary
590/// encoding, then return -1.
591int getLoadFPImm(APFloat FPImm);
592} // namespace RISCVLoadFPImm
593
594namespace RISCVSysReg {
595struct SysReg {
596 const char Name[32];
597 unsigned Encoding;
598 // FIXME: add these additional fields when needed.
599 // Privilege Access: Read, Write, Read-Only.
600 // unsigned ReadWrite;
601 // Privilege Mode: User, System or Machine.
602 // unsigned Mode;
603 // Check field name.
604 // unsigned Extra;
605 // Register number without the privilege bits.
606 // unsigned Number;
611
612 bool haveRequiredFeatures(const FeatureBitset &ActiveFeatures) const {
613 // Not in 32-bit mode.
614 if (IsRV32Only && ActiveFeatures[RISCV::Feature64Bit])
615 return false;
616 // No required feature associated with the system register.
617 if (FeaturesRequired.none())
618 return true;
619 return (FeaturesRequired & ActiveFeatures) == FeaturesRequired;
620 }
621};
622
623#define GET_SysRegEncodings_DECL
624#define GET_SysRegsList_DECL
625#include "RISCVGenSearchableTables.inc"
626} // end namespace RISCVSysReg
627
628namespace RISCVInsnOpcode {
630 char Name[10];
632};
633
634#define GET_RISCVOpcodesList_DECL
635#include "RISCVGenSearchableTables.inc"
636} // end namespace RISCVInsnOpcode
637
638namespace RISCVABI {
639
651
652// Returns the target ABI, or else a StringError if the requested ABIName is
653// not supported for the given TT and FeatureBits combination.
654ABI computeTargetABI(const Triple &TT, const FeatureBitset &FeatureBits,
655 StringRef ABIName);
656
657ABI getTargetABI(StringRef ABIName);
658
659// Returns the register used to hold the stack pointer after realignment.
661
662// Returns the register holding shadow call stack pointer.
664
665} // namespace RISCVABI
666
667namespace RISCVFeatures {
668
669// Validates if the given combination of features are valid for the target
670// triple. Exits with report_fatal_error if not.
671void validate(const Triple &TT, const FeatureBitset &FeatureBits);
672
674parseFeatureBits(bool IsRV64, const FeatureBitset &FeatureBits);
675
676} // namespace RISCVFeatures
677
678namespace RISCVRVC {
679bool compress(MCInst &OutInst, const MCInst &MI, const MCSubtargetInfo &STI);
680bool uncompress(MCInst &OutInst, const MCInst &MI, const MCSubtargetInfo &STI);
681} // namespace RISCVRVC
682
683namespace RISCVZC {
700
701inline unsigned encodeRegList(MCRegister EndReg, bool IsRVE = false) {
702 assert((!IsRVE || EndReg <= RISCV::X9) && "Invalid Rlist for RV32E");
703 switch (EndReg) {
704 case RISCV::X1:
705 return RLISTENCODE::RA;
706 case RISCV::X8:
707 return RLISTENCODE::RA_S0;
708 case RISCV::X9:
710 case RISCV::X18:
712 case RISCV::X19:
714 case RISCV::X20:
716 case RISCV::X21:
718 case RISCV::X22:
720 case RISCV::X23:
722 case RISCV::X24:
724 case RISCV::X25:
726 case RISCV::X27:
728 default:
729 llvm_unreachable("Undefined input.");
730 }
731}
732
733inline static unsigned encodeRegListNumRegs(unsigned NumRegs) {
734 assert(NumRegs > 0 && NumRegs < 14 && NumRegs != 12 &&
735 "Unexpected number of registers");
736 if (NumRegs == 13)
738
739 return RLISTENCODE::RA + (NumRegs - 1);
740}
741
742inline static unsigned getStackAdjBase(unsigned RlistVal, bool IsRV64) {
743 assert(RlistVal >= RLISTENCODE::RA && RlistVal <= RLISTENCODE::RA_S0_S11 &&
744 "Invalid Rlist");
745 unsigned NumRegs = (RlistVal - RLISTENCODE::RA) + 1;
746 // s10 and s11 are saved together.
747 if (RlistVal == RLISTENCODE::RA_S0_S11)
748 ++NumRegs;
749
750 unsigned RegSize = IsRV64 ? 8 : 4;
751 return alignTo(NumRegs * RegSize, 16);
752}
753
754void printRegList(unsigned RlistEncode, raw_ostream &OS);
755} // namespace RISCVZC
756
757namespace RISCVVInversePseudosTable {
764
765#define GET_RISCVVInversePseudosTable_DECL
766#include "RISCVGenSearchableTables.inc"
767} // namespace RISCVVInversePseudosTable
768
769namespace RISCV {
779
789
798
808
817
825
834
842
843#define GET_RISCVVSSEGTable_DECL
844#define GET_RISCVVLSEGTable_DECL
845#define GET_RISCVVLXSEGTable_DECL
846#define GET_RISCVVSXSEGTable_DECL
847#define GET_RISCVVLETable_DECL
848#define GET_RISCVVSETable_DECL
849#define GET_RISCVVLXTable_DECL
850#define GET_RISCVVSXTable_DECL
851#define GET_RISCVNDSVLNTable_DECL
852#include "RISCVGenSearchableTables.inc"
853} // namespace RISCV
854
855} // namespace llvm
856
857#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