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.
300};
301} // namespace RISCVII
302
303namespace RISCVOp {
304enum OperandType : unsigned {
373 // Operand is a 3-bit rounding mode, '111' indicates FRM register.
374 // Represents 'frm' argument passing to floating-point operations.
376 // Operand is a 3-bit rounding mode where only RTZ is valid.
378 // Condition code used by select and short forward branch pseudos.
380 // Vector policy operand.
382 // Vector SEW operand. Stores in log2(SEW).
384 // Special SEW for mask only instructions. Always 0.
386 // Vector rounding mode for VXRM or FRM.
389 // Operand is either a register or uimm5, this is used by V extension pseudo
390 // instructions to represent a value that be passed as AVL to either vsetvli
391 // or vsetivli.
393};
394} // namespace RISCVOp
395
396// Describes the predecessor/successor bits used in the FENCE instruction.
399 I = 8,
400 O = 4,
401 R = 2,
402 W = 1
403};
404}
405
406// Describes the supported floating point rounding mode encodings.
407namespace RISCVFPRndMode {
409 RNE = 0,
410 RTZ = 1,
411 RDN = 2,
412 RUP = 3,
413 RMM = 4,
414 DYN = 7,
416};
417
419 switch (RndMode) {
420 default:
421 llvm_unreachable("Unknown floating point rounding mode");
423 return "rne";
425 return "rtz";
427 return "rdn";
429 return "rup";
431 return "rmm";
433 return "dyn";
434 }
435}
436
447
448inline static bool isValidRoundingMode(unsigned Mode) {
449 switch (Mode) {
450 default:
451 return false;
458 return true;
459 }
460}
461} // namespace RISCVFPRndMode
462
463namespace RISCVVXRndMode {
465 RNU = 0,
466 RNE = 1,
467 RDN = 2,
468 ROD = 3,
470};
471
473 switch (RndMode) {
474 default:
475 llvm_unreachable("Unknown vector fixed-point rounding mode");
477 return "rnu";
479 return "rne";
481 return "rdn";
483 return "rod";
484 }
485}
486
495
496inline static bool isValidRoundingMode(unsigned Mode) {
497 switch (Mode) {
498 default:
499 return false;
504 return true;
505 }
506}
507} // namespace RISCVVXRndMode
508
511 NX = 0x01, // Inexact
512 UF = 0x02, // Underflow
513 OF = 0x04, // Overflow
514 DZ = 0x08, // Divide by zero
515 NV = 0x10, // Invalid operation
516 ALL = 0x1F // Mask for all accrued exception flags
517};
518}
519
520//===----------------------------------------------------------------------===//
521// Floating-point Immediates
522//
523
524namespace RISCVLoadFPImm {
525float getFPImm(unsigned Imm);
526
527/// getLoadFPImm - Return a 5-bit binary encoding of the floating-point
528/// immediate value. If the value cannot be represented as a 5-bit binary
529/// encoding, then return -1.
530int getLoadFPImm(APFloat FPImm);
531} // namespace RISCVLoadFPImm
532
533namespace RISCVSysReg {
534struct SysReg {
535 const char Name[32];
536 unsigned Encoding;
537 // FIXME: add these additional fields when needed.
538 // Privilege Access: Read, Write, Read-Only.
539 // unsigned ReadWrite;
540 // Privilege Mode: User, System or Machine.
541 // unsigned Mode;
542 // Check field name.
543 // unsigned Extra;
544 // Register number without the privilege bits.
545 // unsigned Number;
550
551 bool haveRequiredFeatures(const FeatureBitset &ActiveFeatures) const {
552 // Not in 32-bit mode.
553 if (IsRV32Only && ActiveFeatures[RISCV::Feature64Bit])
554 return false;
555 // No required feature associated with the system register.
556 if (FeaturesRequired.none())
557 return true;
558 return (FeaturesRequired & ActiveFeatures) == FeaturesRequired;
559 }
560};
561
562#define GET_SysRegEncodings_DECL
563#define GET_SysRegsList_DECL
564#include "RISCVGenSearchableTables.inc"
565} // end namespace RISCVSysReg
566
567namespace RISCVInsnOpcode {
569 char Name[10];
571};
572
573#define GET_RISCVOpcodesList_DECL
574#include "RISCVGenSearchableTables.inc"
575} // end namespace RISCVInsnOpcode
576
577namespace RISCVABI {
578
590
591// Returns the target ABI, or else a StringError if the requested ABIName is
592// not supported for the given TT and FeatureBits combination.
593ABI computeTargetABI(const Triple &TT, const FeatureBitset &FeatureBits,
594 StringRef ABIName);
595
596ABI getTargetABI(StringRef ABIName);
597
598// Returns the register used to hold the stack pointer after realignment.
600
601// Returns the register holding shadow call stack pointer.
603
604} // namespace RISCVABI
605
606namespace RISCVFeatures {
607
608// Validates if the given combination of features are valid for the target
609// triple. Exits with report_fatal_error if not.
610void validate(const Triple &TT, const FeatureBitset &FeatureBits);
611
613parseFeatureBits(bool IsRV64, const FeatureBitset &FeatureBits);
614
615} // namespace RISCVFeatures
616
617namespace RISCVRVC {
618bool compress(MCInst &OutInst, const MCInst &MI, const MCSubtargetInfo &STI);
619bool uncompress(MCInst &OutInst, const MCInst &MI, const MCSubtargetInfo &STI);
620} // namespace RISCVRVC
621
622namespace RISCVZC {
639
640inline unsigned encodeRegList(MCRegister EndReg, bool IsRVE = false) {
641 assert((!IsRVE || EndReg <= RISCV::X9) && "Invalid Rlist for RV32E");
642 switch (EndReg) {
643 case RISCV::X1:
644 return RLISTENCODE::RA;
645 case RISCV::X8:
646 return RLISTENCODE::RA_S0;
647 case RISCV::X9:
649 case RISCV::X18:
651 case RISCV::X19:
653 case RISCV::X20:
655 case RISCV::X21:
657 case RISCV::X22:
659 case RISCV::X23:
661 case RISCV::X24:
663 case RISCV::X25:
665 case RISCV::X27:
667 default:
668 llvm_unreachable("Undefined input.");
669 }
670}
671
672inline static unsigned encodeRegListNumRegs(unsigned NumRegs) {
673 assert(NumRegs > 0 && NumRegs < 14 && NumRegs != 12 &&
674 "Unexpected number of registers");
675 if (NumRegs == 13)
677
678 return RLISTENCODE::RA + (NumRegs - 1);
679}
680
681inline static unsigned getStackAdjBase(unsigned RlistVal, bool IsRV64) {
682 assert(RlistVal >= RLISTENCODE::RA && RlistVal <= RLISTENCODE::RA_S0_S11 &&
683 "Invalid Rlist");
684 unsigned NumRegs = (RlistVal - RLISTENCODE::RA) + 1;
685 // s10 and s11 are saved together.
686 if (RlistVal == RLISTENCODE::RA_S0_S11)
687 ++NumRegs;
688
689 unsigned RegSize = IsRV64 ? 8 : 4;
690 return alignTo(NumRegs * RegSize, 16);
691}
692
693void printRegList(unsigned RlistEncode, raw_ostream &OS);
694} // namespace RISCVZC
695
696namespace RISCVVInversePseudosTable {
703
704#define GET_RISCVVInversePseudosTable_DECL
705#include "RISCVGenSearchableTables.inc"
706} // namespace RISCVVInversePseudosTable
707
708namespace RISCV {
718
728
737
747
756
764
773
781
782#define GET_RISCVVSSEGTable_DECL
783#define GET_RISCVVLSEGTable_DECL
784#define GET_RISCVVLXSEGTable_DECL
785#define GET_RISCVVSXSEGTable_DECL
786#define GET_RISCVVLETable_DECL
787#define GET_RISCVVSETable_DECL
788#define GET_RISCVVLXTable_DECL
789#define GET_RISCVVSXTable_DECL
790#define GET_RISCVNDSVLNTable_DECL
791#include "RISCVGenSearchableTables.inc"
792} // namespace RISCV
793
794} // namespace llvm
795
796#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: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)
@ TargetOverlapConstraintTypeMask
@ TargetOverlapConstraintTypeShift
@ IsRVVWideningReductionShift
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)
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:155
bool haveRequiredFeatures(const FeatureBitset &ActiveFeatures) const