Path: blob/main/contrib/llvm-project/llvm/lib/Target/RISCV/MCTargetDesc/RISCVBaseInfo.h
35294 views
//===-- RISCVBaseInfo.h - Top level definitions for RISC-V MC ---*- C++ -*-===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//7//8// This file contains small standalone enum definitions for the RISC-V target9// useful for the compiler back-end and the MC libraries.10//11//===----------------------------------------------------------------------===//12#ifndef LLVM_LIB_TARGET_RISCV_MCTARGETDESC_RISCVBASEINFO_H13#define LLVM_LIB_TARGET_RISCV_MCTARGETDESC_RISCVBASEINFO_H1415#include "MCTargetDesc/RISCVMCTargetDesc.h"16#include "llvm/ADT/APFloat.h"17#include "llvm/ADT/APInt.h"18#include "llvm/ADT/StringRef.h"19#include "llvm/ADT/StringSwitch.h"20#include "llvm/MC/MCInstrDesc.h"21#include "llvm/TargetParser/RISCVISAInfo.h"22#include "llvm/TargetParser/RISCVTargetParser.h"23#include "llvm/TargetParser/SubtargetFeature.h"2425namespace llvm {2627// RISCVII - This namespace holds all of the target specific flags that28// instruction info tracks. All definitions must match RISCVInstrFormats.td.29namespace RISCVII {30enum {31InstFormatPseudo = 0,32InstFormatR = 1,33InstFormatR4 = 2,34InstFormatI = 3,35InstFormatS = 4,36InstFormatB = 5,37InstFormatU = 6,38InstFormatJ = 7,39InstFormatCR = 8,40InstFormatCI = 9,41InstFormatCSS = 10,42InstFormatCIW = 11,43InstFormatCL = 12,44InstFormatCS = 13,45InstFormatCA = 14,46InstFormatCB = 15,47InstFormatCJ = 16,48InstFormatCU = 17,49InstFormatCLB = 18,50InstFormatCLH = 19,51InstFormatCSB = 20,52InstFormatCSH = 21,53InstFormatOther = 22,5455InstFormatMask = 31,56InstFormatShift = 0,5758ConstraintShift = InstFormatShift + 5,59VS2Constraint = 0b001 << ConstraintShift,60VS1Constraint = 0b010 << ConstraintShift,61VMConstraint = 0b100 << ConstraintShift,62ConstraintMask = 0b111 << ConstraintShift,6364VLMulShift = ConstraintShift + 3,65VLMulMask = 0b111 << VLMulShift,6667// Force a tail agnostic policy even this instruction has a tied destination.68ForceTailAgnosticShift = VLMulShift + 3,69ForceTailAgnosticMask = 1 << ForceTailAgnosticShift,7071// Is this a _TIED vector pseudo instruction. For these instructions we72// shouldn't skip the tied operand when converting to MC instructions.73IsTiedPseudoShift = ForceTailAgnosticShift + 1,74IsTiedPseudoMask = 1 << IsTiedPseudoShift,7576// Does this instruction have a SEW operand. It will be the last explicit77// operand unless there is a vector policy operand. Used by RVV Pseudos.78HasSEWOpShift = IsTiedPseudoShift + 1,79HasSEWOpMask = 1 << HasSEWOpShift,8081// Does this instruction have a VL operand. It will be the second to last82// explicit operand unless there is a vector policy operand. Used by RVV83// Pseudos.84HasVLOpShift = HasSEWOpShift + 1,85HasVLOpMask = 1 << HasVLOpShift,8687// Does this instruction have a vector policy operand. It will be the last88// explicit operand. Used by RVV Pseudos.89HasVecPolicyOpShift = HasVLOpShift + 1,90HasVecPolicyOpMask = 1 << HasVecPolicyOpShift,9192// Is this instruction a vector widening reduction instruction. Used by RVV93// Pseudos.94IsRVVWideningReductionShift = HasVecPolicyOpShift + 1,95IsRVVWideningReductionMask = 1 << IsRVVWideningReductionShift,9697// Does this instruction care about mask policy. If it is not, the mask policy98// could be either agnostic or undisturbed. For example, unmasked, store, and99// reduction operations result would not be affected by mask policy, so100// compiler has free to select either one.101UsesMaskPolicyShift = IsRVVWideningReductionShift + 1,102UsesMaskPolicyMask = 1 << UsesMaskPolicyShift,103104// Indicates that the result can be considered sign extended from bit 31. Some105// instructions with this flag aren't W instructions, but are either sign106// extended from a smaller size, always outputs a small integer, or put zeros107// in bits 63:31. Used by the SExtWRemoval pass.108IsSignExtendingOpWShift = UsesMaskPolicyShift + 1,109IsSignExtendingOpWMask = 1ULL << IsSignExtendingOpWShift,110111HasRoundModeOpShift = IsSignExtendingOpWShift + 1,112HasRoundModeOpMask = 1 << HasRoundModeOpShift,113114UsesVXRMShift = HasRoundModeOpShift + 1,115UsesVXRMMask = 1 << UsesVXRMShift,116117// Indicates whether these instructions can partially overlap between source118// registers and destination registers according to the vector spec.119// 0 -> not a vector pseudo120// 1 -> default value for vector pseudos. not widening or narrowing.121// 2 -> narrowing case122// 3 -> widening case123TargetOverlapConstraintTypeShift = UsesVXRMShift + 1,124TargetOverlapConstraintTypeMask = 3ULL << TargetOverlapConstraintTypeShift,125};126127// Helper functions to read TSFlags.128/// \returns the format of the instruction.129static inline unsigned getFormat(uint64_t TSFlags) {130return (TSFlags & InstFormatMask) >> InstFormatShift;131}132/// \returns the LMUL for the instruction.133static inline VLMUL getLMul(uint64_t TSFlags) {134return static_cast<VLMUL>((TSFlags & VLMulMask) >> VLMulShift);135}136/// \returns true if tail agnostic is enforced for the instruction.137static inline bool doesForceTailAgnostic(uint64_t TSFlags) {138return TSFlags & ForceTailAgnosticMask;139}140/// \returns true if this a _TIED pseudo.141static inline bool isTiedPseudo(uint64_t TSFlags) {142return TSFlags & IsTiedPseudoMask;143}144/// \returns true if there is a SEW operand for the instruction.145static inline bool hasSEWOp(uint64_t TSFlags) {146return TSFlags & HasSEWOpMask;147}148/// \returns true if there is a VL operand for the instruction.149static inline bool hasVLOp(uint64_t TSFlags) {150return TSFlags & HasVLOpMask;151}152/// \returns true if there is a vector policy operand for this instruction.153static inline bool hasVecPolicyOp(uint64_t TSFlags) {154return TSFlags & HasVecPolicyOpMask;155}156/// \returns true if it is a vector widening reduction instruction.157static inline bool isRVVWideningReduction(uint64_t TSFlags) {158return TSFlags & IsRVVWideningReductionMask;159}160/// \returns true if mask policy is valid for the instruction.161static inline bool usesMaskPolicy(uint64_t TSFlags) {162return TSFlags & UsesMaskPolicyMask;163}164165/// \returns true if there is a rounding mode operand for this instruction166static inline bool hasRoundModeOp(uint64_t TSFlags) {167return TSFlags & HasRoundModeOpMask;168}169170/// \returns true if this instruction uses vxrm171static inline bool usesVXRM(uint64_t TSFlags) { return TSFlags & UsesVXRMMask; }172173static inline unsigned getVLOpNum(const MCInstrDesc &Desc) {174const uint64_t TSFlags = Desc.TSFlags;175// This method is only called if we expect to have a VL operand, and all176// instructions with VL also have SEW.177assert(hasSEWOp(TSFlags) && hasVLOp(TSFlags));178unsigned Offset = 2;179if (hasVecPolicyOp(TSFlags))180Offset = 3;181return Desc.getNumOperands() - Offset;182}183184static inline unsigned getSEWOpNum(const MCInstrDesc &Desc) {185const uint64_t TSFlags = Desc.TSFlags;186assert(hasSEWOp(TSFlags));187unsigned Offset = 1;188if (hasVecPolicyOp(TSFlags))189Offset = 2;190return Desc.getNumOperands() - Offset;191}192193static inline unsigned getVecPolicyOpNum(const MCInstrDesc &Desc) {194assert(hasVecPolicyOp(Desc.TSFlags));195return Desc.getNumOperands() - 1;196}197198/// \returns the index to the rounding mode immediate value if any, otherwise199/// returns -1.200static inline int getFRMOpNum(const MCInstrDesc &Desc) {201const uint64_t TSFlags = Desc.TSFlags;202if (!hasRoundModeOp(TSFlags) || usesVXRM(TSFlags))203return -1;204205// The operand order206// --------------------------------------207// | n-1 (if any) | n-2 | n-3 | n-4 |208// | policy | sew | vl | frm |209// --------------------------------------210return getVLOpNum(Desc) - 1;211}212213/// \returns the index to the rounding mode immediate value if any, otherwise214/// returns -1.215static inline int getVXRMOpNum(const MCInstrDesc &Desc) {216const uint64_t TSFlags = Desc.TSFlags;217if (!hasRoundModeOp(TSFlags) || !usesVXRM(TSFlags))218return -1;219// The operand order220// --------------------------------------221// | n-1 (if any) | n-2 | n-3 | n-4 |222// | policy | sew | vl | vxrm |223// --------------------------------------224return getVLOpNum(Desc) - 1;225}226227// Is the first def operand tied to the first use operand. This is true for228// vector pseudo instructions that have a merge operand for tail/mask229// undisturbed. It's also true for vector FMA instructions where one of the230// operands is also the destination register.231static inline bool isFirstDefTiedToFirstUse(const MCInstrDesc &Desc) {232return Desc.getNumDefs() < Desc.getNumOperands() &&233Desc.getOperandConstraint(Desc.getNumDefs(), MCOI::TIED_TO) == 0;234}235236// RISC-V Specific Machine Operand Flags237enum {238MO_None = 0,239MO_CALL = 1,240MO_LO = 3,241MO_HI = 4,242MO_PCREL_LO = 5,243MO_PCREL_HI = 6,244MO_GOT_HI = 7,245MO_TPREL_LO = 8,246MO_TPREL_HI = 9,247MO_TPREL_ADD = 10,248MO_TLS_GOT_HI = 11,249MO_TLS_GD_HI = 12,250MO_TLSDESC_HI = 13,251MO_TLSDESC_LOAD_LO = 14,252MO_TLSDESC_ADD_LO = 15,253MO_TLSDESC_CALL = 16,254255// Used to differentiate between target-specific "direct" flags and "bitmask"256// flags. A machine operand can only have one "direct" flag, but can have257// multiple "bitmask" flags.258MO_DIRECT_FLAG_MASK = 31259};260} // namespace RISCVII261262namespace RISCVOp {263enum OperandType : unsigned {264OPERAND_FIRST_RISCV_IMM = MCOI::OPERAND_FIRST_TARGET,265OPERAND_UIMM1 = OPERAND_FIRST_RISCV_IMM,266OPERAND_UIMM2,267OPERAND_UIMM2_LSB0,268OPERAND_UIMM3,269OPERAND_UIMM4,270OPERAND_UIMM5,271OPERAND_UIMM5_LSB0,272OPERAND_UIMM6,273OPERAND_UIMM6_LSB0,274OPERAND_UIMM7,275OPERAND_UIMM7_LSB00,276OPERAND_UIMM8_LSB00,277OPERAND_UIMM8,278OPERAND_UIMM8_LSB000,279OPERAND_UIMM8_GE32,280OPERAND_UIMM9_LSB000,281OPERAND_UIMM10_LSB00_NONZERO,282OPERAND_UIMM12,283OPERAND_UIMM16,284OPERAND_UIMM32,285OPERAND_ZERO,286OPERAND_SIMM5,287OPERAND_SIMM5_PLUS1,288OPERAND_SIMM6,289OPERAND_SIMM6_NONZERO,290OPERAND_SIMM10_LSB0000_NONZERO,291OPERAND_SIMM12,292OPERAND_SIMM12_LSB00000,293OPERAND_UIMM20,294OPERAND_UIMMLOG2XLEN,295OPERAND_UIMMLOG2XLEN_NONZERO,296OPERAND_CLUI_IMM,297OPERAND_VTYPEI10,298OPERAND_VTYPEI11,299OPERAND_RVKRNUM,300OPERAND_RVKRNUM_0_7,301OPERAND_RVKRNUM_1_10,302OPERAND_RVKRNUM_2_14,303OPERAND_SPIMM,304OPERAND_LAST_RISCV_IMM = OPERAND_SPIMM,305// Operand is either a register or uimm5, this is used by V extension pseudo306// instructions to represent a value that be passed as AVL to either vsetvli307// or vsetivli.308OPERAND_AVL,309};310} // namespace RISCVOp311312// Describes the predecessor/successor bits used in the FENCE instruction.313namespace RISCVFenceField {314enum FenceField {315I = 8,316O = 4,317R = 2,318W = 1319};320}321322// Describes the supported floating point rounding mode encodings.323namespace RISCVFPRndMode {324enum RoundingMode {325RNE = 0,326RTZ = 1,327RDN = 2,328RUP = 3,329RMM = 4,330DYN = 7,331Invalid332};333334inline static StringRef roundingModeToString(RoundingMode RndMode) {335switch (RndMode) {336default:337llvm_unreachable("Unknown floating point rounding mode");338case RISCVFPRndMode::RNE:339return "rne";340case RISCVFPRndMode::RTZ:341return "rtz";342case RISCVFPRndMode::RDN:343return "rdn";344case RISCVFPRndMode::RUP:345return "rup";346case RISCVFPRndMode::RMM:347return "rmm";348case RISCVFPRndMode::DYN:349return "dyn";350}351}352353inline static RoundingMode stringToRoundingMode(StringRef Str) {354return StringSwitch<RoundingMode>(Str)355.Case("rne", RISCVFPRndMode::RNE)356.Case("rtz", RISCVFPRndMode::RTZ)357.Case("rdn", RISCVFPRndMode::RDN)358.Case("rup", RISCVFPRndMode::RUP)359.Case("rmm", RISCVFPRndMode::RMM)360.Case("dyn", RISCVFPRndMode::DYN)361.Default(RISCVFPRndMode::Invalid);362}363364inline static bool isValidRoundingMode(unsigned Mode) {365switch (Mode) {366default:367return false;368case RISCVFPRndMode::RNE:369case RISCVFPRndMode::RTZ:370case RISCVFPRndMode::RDN:371case RISCVFPRndMode::RUP:372case RISCVFPRndMode::RMM:373case RISCVFPRndMode::DYN:374return true;375}376}377} // namespace RISCVFPRndMode378379namespace RISCVVXRndMode {380enum RoundingMode {381RNU = 0,382RNE = 1,383RDN = 2,384ROD = 3,385};386} // namespace RISCVVXRndMode387388//===----------------------------------------------------------------------===//389// Floating-point Immediates390//391392namespace RISCVLoadFPImm {393float getFPImm(unsigned Imm);394395/// getLoadFPImm - Return a 5-bit binary encoding of the floating-point396/// immediate value. If the value cannot be represented as a 5-bit binary397/// encoding, then return -1.398int getLoadFPImm(APFloat FPImm);399} // namespace RISCVLoadFPImm400401namespace RISCVSysReg {402struct SysReg {403const char *Name;404const char *AltName;405const char *DeprecatedName;406unsigned Encoding;407// FIXME: add these additional fields when needed.408// Privilege Access: Read, Write, Read-Only.409// unsigned ReadWrite;410// Privilege Mode: User, System or Machine.411// unsigned Mode;412// Check field name.413// unsigned Extra;414// Register number without the privilege bits.415// unsigned Number;416FeatureBitset FeaturesRequired;417bool isRV32Only;418419bool haveRequiredFeatures(const FeatureBitset &ActiveFeatures) const {420// Not in 32-bit mode.421if (isRV32Only && ActiveFeatures[RISCV::Feature64Bit])422return false;423// No required feature associated with the system register.424if (FeaturesRequired.none())425return true;426return (FeaturesRequired & ActiveFeatures) == FeaturesRequired;427}428};429430#define GET_SysRegsList_DECL431#include "RISCVGenSearchableTables.inc"432} // end namespace RISCVSysReg433434namespace RISCVInsnOpcode {435struct RISCVOpcode {436const char *Name;437unsigned Value;438};439440#define GET_RISCVOpcodesList_DECL441#include "RISCVGenSearchableTables.inc"442} // end namespace RISCVInsnOpcode443444namespace RISCVABI {445446enum ABI {447ABI_ILP32,448ABI_ILP32F,449ABI_ILP32D,450ABI_ILP32E,451ABI_LP64,452ABI_LP64F,453ABI_LP64D,454ABI_LP64E,455ABI_Unknown456};457458// Returns the target ABI, or else a StringError if the requested ABIName is459// not supported for the given TT and FeatureBits combination.460ABI computeTargetABI(const Triple &TT, const FeatureBitset &FeatureBits,461StringRef ABIName);462463ABI getTargetABI(StringRef ABIName);464465// Returns the register used to hold the stack pointer after realignment.466MCRegister getBPReg();467468// Returns the register holding shadow call stack pointer.469MCRegister getSCSPReg();470471} // namespace RISCVABI472473namespace RISCVFeatures {474475// Validates if the given combination of features are valid for the target476// triple. Exits with report_fatal_error if not.477void validate(const Triple &TT, const FeatureBitset &FeatureBits);478479llvm::Expected<std::unique_ptr<RISCVISAInfo>>480parseFeatureBits(bool IsRV64, const FeatureBitset &FeatureBits);481482} // namespace RISCVFeatures483484namespace RISCVRVC {485bool compress(MCInst &OutInst, const MCInst &MI, const MCSubtargetInfo &STI);486bool uncompress(MCInst &OutInst, const MCInst &MI, const MCSubtargetInfo &STI);487} // namespace RISCVRVC488489namespace RISCVZC {490enum RLISTENCODE {491RA = 4,492RA_S0,493RA_S0_S1,494RA_S0_S2,495RA_S0_S3,496RA_S0_S4,497RA_S0_S5,498RA_S0_S6,499RA_S0_S7,500RA_S0_S8,501RA_S0_S9,502// note - to include s10, s11 must also be included503RA_S0_S11,504INVALID_RLIST,505};506507inline unsigned encodeRlist(MCRegister EndReg, bool IsRV32E = false) {508assert((!IsRV32E || EndReg <= RISCV::X9) && "Invalid Rlist for RV32E");509switch (EndReg) {510case RISCV::X1:511return RLISTENCODE::RA;512case RISCV::X8:513return RLISTENCODE::RA_S0;514case RISCV::X9:515return RLISTENCODE::RA_S0_S1;516case RISCV::X18:517return RLISTENCODE::RA_S0_S2;518case RISCV::X19:519return RLISTENCODE::RA_S0_S3;520case RISCV::X20:521return RLISTENCODE::RA_S0_S4;522case RISCV::X21:523return RLISTENCODE::RA_S0_S5;524case RISCV::X22:525return RLISTENCODE::RA_S0_S6;526case RISCV::X23:527return RLISTENCODE::RA_S0_S7;528case RISCV::X24:529return RLISTENCODE::RA_S0_S8;530case RISCV::X25:531return RLISTENCODE::RA_S0_S9;532case RISCV::X26:533return RLISTENCODE::INVALID_RLIST;534case RISCV::X27:535return RLISTENCODE::RA_S0_S11;536default:537llvm_unreachable("Undefined input.");538}539}540541inline static unsigned getStackAdjBase(unsigned RlistVal, bool IsRV64) {542assert(RlistVal != RLISTENCODE::INVALID_RLIST &&543"{ra, s0-s10} is not supported, s11 must be included.");544if (!IsRV64) {545switch (RlistVal) {546case RLISTENCODE::RA:547case RLISTENCODE::RA_S0:548case RLISTENCODE::RA_S0_S1:549case RLISTENCODE::RA_S0_S2:550return 16;551case RLISTENCODE::RA_S0_S3:552case RLISTENCODE::RA_S0_S4:553case RLISTENCODE::RA_S0_S5:554case RLISTENCODE::RA_S0_S6:555return 32;556case RLISTENCODE::RA_S0_S7:557case RLISTENCODE::RA_S0_S8:558case RLISTENCODE::RA_S0_S9:559return 48;560case RLISTENCODE::RA_S0_S11:561return 64;562}563} else {564switch (RlistVal) {565case RLISTENCODE::RA:566case RLISTENCODE::RA_S0:567return 16;568case RLISTENCODE::RA_S0_S1:569case RLISTENCODE::RA_S0_S2:570return 32;571case RLISTENCODE::RA_S0_S3:572case RLISTENCODE::RA_S0_S4:573return 48;574case RLISTENCODE::RA_S0_S5:575case RLISTENCODE::RA_S0_S6:576return 64;577case RLISTENCODE::RA_S0_S7:578case RLISTENCODE::RA_S0_S8:579return 80;580case RLISTENCODE::RA_S0_S9:581return 96;582case RLISTENCODE::RA_S0_S11:583return 112;584}585}586llvm_unreachable("Unexpected RlistVal");587}588589inline static bool getSpimm(unsigned RlistVal, unsigned &SpimmVal,590int64_t StackAdjustment, bool IsRV64) {591if (RlistVal == RLISTENCODE::INVALID_RLIST)592return false;593unsigned StackAdjBase = getStackAdjBase(RlistVal, IsRV64);594StackAdjustment -= StackAdjBase;595if (StackAdjustment % 16 != 0)596return false;597SpimmVal = StackAdjustment / 16;598if (SpimmVal > 3)599return false;600return true;601}602603void printRlist(unsigned SlistEncode, raw_ostream &OS);604} // namespace RISCVZC605606} // namespace llvm607608#endif609610611