LLVM 20.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 {
55
58
64
67
68 // Force a tail agnostic policy even this instruction has a tied destination.
71
72 // Is this a _TIED vector pseudo instruction. For these instructions we
73 // shouldn't skip the tied operand when converting to MC instructions.
76
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.
81
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
84 // Pseudos.
87
88 // Does this instruction have a vector policy operand. It will be the last
89 // explicit operand. Used by RVV Pseudos.
92
93 // Is this instruction a vector widening reduction instruction. Used by RVV
94 // Pseudos.
97
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.
104
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.
111
114
117
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
126
129};
130
131// Helper functions to read TSFlags.
132/// \returns the format of the instruction.
133static inline unsigned getFormat(uint64_t TSFlags) {
134 return (TSFlags & InstFormatMask) >> InstFormatShift;
135}
136/// \returns the LMUL for the instruction.
137static inline VLMUL getLMul(uint64_t TSFlags) {
138 return static_cast<VLMUL>((TSFlags & VLMulMask) >> VLMulShift);
139}
140/// \returns true if tail agnostic is enforced for the instruction.
141static inline bool doesForceTailAgnostic(uint64_t TSFlags) {
142 return TSFlags & ForceTailAgnosticMask;
143}
144/// \returns true if this a _TIED pseudo.
145static inline bool isTiedPseudo(uint64_t TSFlags) {
146 return TSFlags & IsTiedPseudoMask;
147}
148/// \returns true if there is a SEW operand for the instruction.
149static inline bool hasSEWOp(uint64_t TSFlags) {
150 return TSFlags & HasSEWOpMask;
151}
152/// \returns true if there is a VL operand for the instruction.
153static inline bool hasVLOp(uint64_t TSFlags) {
154 return TSFlags & HasVLOpMask;
155}
156/// \returns true if there is a vector policy operand for this instruction.
157static inline bool hasVecPolicyOp(uint64_t TSFlags) {
158 return TSFlags & HasVecPolicyOpMask;
159}
160/// \returns true if it is a vector widening reduction instruction.
161static inline bool isRVVWideningReduction(uint64_t TSFlags) {
162 return TSFlags & IsRVVWideningReductionMask;
163}
164/// \returns true if mask policy is valid for the instruction.
165static inline bool usesMaskPolicy(uint64_t TSFlags) {
166 return TSFlags & UsesMaskPolicyMask;
167}
168
169/// \returns true if there is a rounding mode operand for this instruction
170static inline bool hasRoundModeOp(uint64_t TSFlags) {
171 return TSFlags & HasRoundModeOpMask;
172}
173
174/// \returns true if this instruction uses vxrm
175static inline bool usesVXRM(uint64_t TSFlags) { return TSFlags & UsesVXRMMask; }
176
177/// \returns true if the result isn't element-wise,
178/// e.g. vredsum.vs/vcompress.vm/viota.m
179static inline bool activeElementsAffectResult(uint64_t TSFlags) {
180 return TSFlags & ActiveElementsAffectResultMask;
181}
182
183static inline unsigned getVLOpNum(const MCInstrDesc &Desc) {
184 const uint64_t TSFlags = Desc.TSFlags;
185 // This method is only called if we expect to have a VL operand, and all
186 // instructions with VL also have SEW.
187 assert(hasSEWOp(TSFlags) && hasVLOp(TSFlags));
188 unsigned Offset = 2;
189 if (hasVecPolicyOp(TSFlags))
190 Offset = 3;
191 return Desc.getNumOperands() - Offset;
192}
193
194static inline unsigned getSEWOpNum(const MCInstrDesc &Desc) {
195 const uint64_t TSFlags = Desc.TSFlags;
196 assert(hasSEWOp(TSFlags));
197 unsigned Offset = 1;
198 if (hasVecPolicyOp(TSFlags))
199 Offset = 2;
200 return Desc.getNumOperands() - Offset;
201}
202
203static inline unsigned getVecPolicyOpNum(const MCInstrDesc &Desc) {
204 assert(hasVecPolicyOp(Desc.TSFlags));
205 return Desc.getNumOperands() - 1;
206}
207
208/// \returns the index to the rounding mode immediate value if any, otherwise
209/// returns -1.
210static inline int getFRMOpNum(const MCInstrDesc &Desc) {
211 const uint64_t TSFlags = Desc.TSFlags;
212 if (!hasRoundModeOp(TSFlags) || usesVXRM(TSFlags))
213 return -1;
214
215 // The operand order
216 // --------------------------------------
217 // | n-1 (if any) | n-2 | n-3 | n-4 |
218 // | policy | sew | vl | frm |
219 // --------------------------------------
220 return getVLOpNum(Desc) - 1;
221}
222
223/// \returns the index to the rounding mode immediate value if any, otherwise
224/// returns -1.
225static inline int getVXRMOpNum(const MCInstrDesc &Desc) {
226 const uint64_t TSFlags = Desc.TSFlags;
227 if (!hasRoundModeOp(TSFlags) || !usesVXRM(TSFlags))
228 return -1;
229 // The operand order
230 // --------------------------------------
231 // | n-1 (if any) | n-2 | n-3 | n-4 |
232 // | policy | sew | vl | vxrm |
233 // --------------------------------------
234 return getVLOpNum(Desc) - 1;
235}
236
237// Is the first def operand tied to the first use operand. This is true for
238// vector pseudo instructions that have a merge operand for tail/mask
239// undisturbed. It's also true for vector FMA instructions where one of the
240// operands is also the destination register.
241static inline bool isFirstDefTiedToFirstUse(const MCInstrDesc &Desc) {
242 return Desc.getNumDefs() < Desc.getNumOperands() &&
243 Desc.getOperandConstraint(Desc.getNumDefs(), MCOI::TIED_TO) == 0;
244}
245
246// RISC-V Specific Machine Operand Flags
247enum {
250 MO_LO = 3,
251 MO_HI = 4,
264
265 // Used to differentiate between target-specific "direct" flags and "bitmask"
266 // flags. A machine operand can only have one "direct" flag, but can have
267 // multiple "bitmask" flags.
270} // namespace RISCVII
271
272namespace RISCVOp {
273enum OperandType : unsigned {
315 // Operand is either a register or uimm5, this is used by V extension pseudo
316 // instructions to represent a value that be passed as AVL to either vsetvli
317 // or vsetivli.
319};
320} // namespace RISCVOp
321
322// Describes the predecessor/successor bits used in the FENCE instruction.
323namespace RISCVFenceField {
325 I = 8,
326 O = 4,
327 R = 2,
328 W = 1
330}
331
332// Describes the supported floating point rounding mode encodings.
333namespace RISCVFPRndMode {
335 RNE = 0,
336 RTZ = 1,
337 RDN = 2,
338 RUP = 3,
339 RMM = 4,
340 DYN = 7,
341 Invalid
343
345 switch (RndMode) {
346 default:
347 llvm_unreachable("Unknown floating point rounding mode");
349 return "rne";
351 return "rtz";
353 return "rdn";
355 return "rup";
357 return "rmm";
359 return "dyn";
360 }
361}
362
372}
373
374inline static bool isValidRoundingMode(unsigned Mode) {
375 switch (Mode) {
376 default:
377 return false;
384 return true;
385 }
386}
387} // namespace RISCVFPRndMode
388
389namespace RISCVVXRndMode {
391 RNU = 0,
392 RNE = 1,
393 RDN = 2,
394 ROD = 3,
395};
396} // namespace RISCVVXRndMode
397
398//===----------------------------------------------------------------------===//
399// Floating-point Immediates
400//
401
402namespace RISCVLoadFPImm {
403float getFPImm(unsigned Imm);
404
405/// getLoadFPImm - Return a 5-bit binary encoding of the floating-point
406/// immediate value. If the value cannot be represented as a 5-bit binary
407/// encoding, then return -1.
408int getLoadFPImm(APFloat FPImm);
409} // namespace RISCVLoadFPImm
410
411namespace RISCVSysReg {
412struct SysReg {
413 const char *Name;
414 const char *AltName;
415 const char *DeprecatedName;
416 unsigned Encoding;
417 // FIXME: add these additional fields when needed.
418 // Privilege Access: Read, Write, Read-Only.
419 // unsigned ReadWrite;
420 // Privilege Mode: User, System or Machine.
421 // unsigned Mode;
422 // Check field name.
423 // unsigned Extra;
424 // Register number without the privilege bits.
425 // unsigned Number;
428
429 bool haveRequiredFeatures(const FeatureBitset &ActiveFeatures) const {
430 // Not in 32-bit mode.
431 if (isRV32Only && ActiveFeatures[RISCV::Feature64Bit])
432 return false;
433 // No required feature associated with the system register.
435 return true;
436 return (FeaturesRequired & ActiveFeatures) == FeaturesRequired;
437 }
438};
439
440#define GET_SysRegsList_DECL
441#include "RISCVGenSearchableTables.inc"
442} // end namespace RISCVSysReg
443
444namespace RISCVInsnOpcode {
446 const char *Name;
447 unsigned Value;
448};
449
450#define GET_RISCVOpcodesList_DECL
451#include "RISCVGenSearchableTables.inc"
452} // end namespace RISCVInsnOpcode
453
454namespace RISCVABI {
455
456enum ABI {
467
468// Returns the target ABI, or else a StringError if the requested ABIName is
469// not supported for the given TT and FeatureBits combination.
470ABI computeTargetABI(const Triple &TT, const FeatureBitset &FeatureBits,
471 StringRef ABIName);
472
473ABI getTargetABI(StringRef ABIName);
474
475// Returns the register used to hold the stack pointer after realignment.
477
478// Returns the register holding shadow call stack pointer.
480
481} // namespace RISCVABI
482
483namespace RISCVFeatures {
484
485// Validates if the given combination of features are valid for the target
486// triple. Exits with report_fatal_error if not.
487void validate(const Triple &TT, const FeatureBitset &FeatureBits);
488
490parseFeatureBits(bool IsRV64, const FeatureBitset &FeatureBits);
491
492} // namespace RISCVFeatures
493
494namespace RISCVRVC {
495bool compress(MCInst &OutInst, const MCInst &MI, const MCSubtargetInfo &STI);
496bool uncompress(MCInst &OutInst, const MCInst &MI, const MCSubtargetInfo &STI);
497} // namespace RISCVRVC
498
499namespace RISCVZC {
501 RA = 4,
512 // note - to include s10, s11 must also be included
515};
516
517inline unsigned encodeRlist(MCRegister EndReg, bool IsRV32E = false) {
518 assert((!IsRV32E || EndReg <= RISCV::X9) && "Invalid Rlist for RV32E");
519 switch (EndReg) {
520 case RISCV::X1:
521 return RLISTENCODE::RA;
522 case RISCV::X8:
523 return RLISTENCODE::RA_S0;
524 case RISCV::X9:
526 case RISCV::X18:
528 case RISCV::X19:
530 case RISCV::X20:
532 case RISCV::X21:
534 case RISCV::X22:
536 case RISCV::X23:
538 case RISCV::X24:
540 case RISCV::X25:
542 case RISCV::X26:
544 case RISCV::X27:
546 default:
547 llvm_unreachable("Undefined input.");
548 }
549}
550
551inline static unsigned getStackAdjBase(unsigned RlistVal, bool IsRV64) {
553 "{ra, s0-s10} is not supported, s11 must be included.");
554 if (!IsRV64) {
555 switch (RlistVal) {
556 case RLISTENCODE::RA:
560 return 16;
565 return 32;
569 return 48;
571 return 64;
572 }
573 } else {
574 switch (RlistVal) {
575 case RLISTENCODE::RA:
577 return 16;
580 return 32;
583 return 48;
586 return 64;
589 return 80;
591 return 96;
593 return 112;
594 }
595 }
596 llvm_unreachable("Unexpected RlistVal");
597}
598
599inline static bool getSpimm(unsigned RlistVal, unsigned &SpimmVal,
600 int64_t StackAdjustment, bool IsRV64) {
601 if (RlistVal == RLISTENCODE::INVALID_RLIST)
602 return false;
603 unsigned StackAdjBase = getStackAdjBase(RlistVal, IsRV64);
604 StackAdjustment -= StackAdjBase;
605 if (StackAdjustment % 16 != 0)
606 return false;
607 SpimmVal = StackAdjustment / 16;
608 if (SpimmVal > 3)
609 return false;
610 return true;
611}
612
613void printRlist(unsigned SlistEncode, raw_ostream &OS);
614} // namespace RISCVZC
615
616} // namespace llvm
617
618#endif
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< RegAllocEvictionAdvisorAnalysis::AdvisorMode > Mode("regalloc-enable-advisor", cl::Hidden, cl::init(RegAllocEvictionAdvisorAnalysis::AdvisorMode::Default), cl::desc("Enable regalloc advisor mode"), cl::values(clEnumValN(RegAllocEvictionAdvisorAnalysis::AdvisorMode::Default, "default", "Default"), clEnumValN(RegAllocEvictionAdvisorAnalysis::AdvisorMode::Release, "release", "precompiled"), clEnumValN(RegAllocEvictionAdvisorAnalysis::AdvisorMode::Development, "development", "for training")))
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
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:481
Container class for subtarget features.
Instances of this class represent a single low-level machine instruction.
Definition: MCInst.h:184
Describe properties that are true of each instruction in the target description file.
Definition: MCInstrDesc.h:198
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:50
A switch()-like statement whose cases are string literals.
Definition: StringSwitch.h:44
StringSwitch & Case(StringLiteral S, T Value)
Definition: StringSwitch.h:69
R Default(T Value)
Definition: StringSwitch.h:182
Triple - Helper class for working with autoconf configuration names.
Definition: Triple.h:44
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:52
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
@ OPERAND_FIRST_TARGET
Definition: MCInstrDesc.h:78
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 isTiedPseudo(uint64_t TSFlags)
static unsigned getVLOpNum(const MCInstrDesc &Desc)
static bool doesForceTailAgnostic(uint64_t TSFlags)
static unsigned getFormat(uint64_t TSFlags)
static VLMUL getLMul(uint64_t TSFlags)
static bool hasVLOp(uint64_t TSFlags)
static int getFRMOpNum(const MCInstrDesc &Desc)
@ ActiveElementsAffectResultMask
@ TargetOverlapConstraintTypeMask
@ TargetOverlapConstraintTypeShift
@ IsRVVWideningReductionShift
Definition: RISCVBaseInfo.h:95
@ IsRVVWideningReductionMask
Definition: RISCVBaseInfo.h:96
@ ActiveElementsAffectResultShift
static int getVXRMOpNum(const MCInstrDesc &Desc)
static bool hasVecPolicyOp(uint64_t TSFlags)
static bool usesVXRM(uint64_t TSFlags)
static bool activeElementsAffectResult(uint64_t TSFlags)
static bool isRVVWideningReduction(uint64_t TSFlags)
static unsigned getSEWOpNum(const MCInstrDesc &Desc)
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_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 unsigned getStackAdjBase(unsigned RlistVal, bool IsRV64)
unsigned encodeRlist(MCRegister EndReg, bool IsRV32E=false)
void printRlist(unsigned SlistEncode, raw_ostream &OS)
static bool getSpimm(unsigned RlistVal, unsigned &SpimmVal, int64_t StackAdjustment, bool IsRV64)
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
@ Offset
Definition: DWP.cpp:480
Description of the encoding of one expression Op.
FeatureBitset FeaturesRequired
bool haveRequiredFeatures(const FeatureBitset &ActiveFeatures) const