1 //===-- RISCVBaseInfo.cpp - Top level definitions for RISCV MC ------------===//
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
7 //===----------------------------------------------------------------------===//
9 // This file contains small standalone enum definitions for the RISCV target
10 // useful for the compiler back-end and the MC libraries.
12 //===----------------------------------------------------------------------===//
14 #include "RISCVBaseInfo.h"
15 #include "llvm/ADT/ArrayRef.h"
16 #include "llvm/ADT/Triple.h"
17 #include "llvm/MC/MCInst.h"
18 #include "llvm/MC/MCRegisterInfo.h"
19 #include "llvm/MC/MCSubtargetInfo.h"
20 #include "llvm/Support/RISCVISAInfo.h"
21 #include "llvm/Support/TargetParser.h"
22 #include "llvm/Support/raw_ostream.h"
26 extern const SubtargetFeatureKV RISCVFeatureKV[RISCV::NumSubtargetFeatures];
28 namespace RISCVSysReg {
29 #define GET_SysRegsList_IMPL
30 #include "RISCVGenSearchableTables.inc"
31 } // namespace RISCVSysReg
33 namespace RISCVInsnOpcode {
34 #define GET_RISCVOpcodesList_IMPL
35 #include "RISCVGenSearchableTables.inc"
36 } // namespace RISCVInsnOpcode
39 ABI computeTargetABI(const Triple &TT, FeatureBitset FeatureBits,
41 auto TargetABI = getTargetABI(ABIName);
42 bool IsRV64 = TT.isArch64Bit();
43 bool IsRV32E = FeatureBits[RISCV::FeatureRV32E];
45 if (!ABIName.empty() && TargetABI == ABI_Unknown) {
48 << "' is not a recognized ABI for this target (ignoring target-abi)\n";
49 } else if (ABIName.startswith("ilp32") && IsRV64) {
50 errs() << "32-bit ABIs are not supported for 64-bit targets (ignoring "
52 TargetABI = ABI_Unknown;
53 } else if (ABIName.startswith("lp64") && !IsRV64) {
54 errs() << "64-bit ABIs are not supported for 32-bit targets (ignoring "
56 TargetABI = ABI_Unknown;
57 } else if (IsRV32E && TargetABI != ABI_ILP32E && TargetABI != ABI_Unknown) {
58 // TODO: move this checking to RISCVTargetLowering and RISCVAsmParser
60 << "Only the ilp32e ABI is supported for RV32E (ignoring target-abi)\n";
61 TargetABI = ABI_Unknown;
64 if (TargetABI != ABI_Unknown)
67 // If no explicit ABI is given, try to compute the default ABI.
68 auto ISAInfo = RISCVFeatures::parseFeatureBits(IsRV64, FeatureBits);
70 report_fatal_error(ISAInfo.takeError());
71 return getTargetABI((*ISAInfo)->computeDefaultABI());
74 ABI getTargetABI(StringRef ABIName) {
75 auto TargetABI = StringSwitch<ABI>(ABIName)
76 .Case("ilp32", ABI_ILP32)
77 .Case("ilp32f", ABI_ILP32F)
78 .Case("ilp32d", ABI_ILP32D)
79 .Case("ilp32e", ABI_ILP32E)
80 .Case("lp64", ABI_LP64)
81 .Case("lp64f", ABI_LP64F)
82 .Case("lp64d", ABI_LP64D)
83 .Default(ABI_Unknown);
87 // To avoid the BP value clobbered by a function call, we need to choose a
88 // callee saved register to save the value. RV32E only has X8 and X9 as callee
89 // saved registers and X8 will be used as fp. So we choose X9 as bp.
90 MCRegister getBPReg() { return RISCV::X9; }
92 // Returns the register holding shadow call stack pointer.
93 MCRegister getSCSPReg() { return RISCV::X18; }
95 } // namespace RISCVABI
97 namespace RISCVFeatures {
99 void validate(const Triple &TT, const FeatureBitset &FeatureBits) {
100 if (TT.isArch64Bit() && !FeatureBits[RISCV::Feature64Bit])
101 report_fatal_error("RV64 target requires an RV64 CPU");
102 if (!TT.isArch64Bit() && !FeatureBits[RISCV::Feature32Bit])
103 report_fatal_error("RV32 target requires an RV32 CPU");
104 if (TT.isArch64Bit() && FeatureBits[RISCV::FeatureRV32E])
105 report_fatal_error("RV32E can't be enabled for an RV64 target");
106 if (FeatureBits[RISCV::Feature32Bit] &&
107 FeatureBits[RISCV::Feature64Bit])
108 report_fatal_error("RV32 and RV64 can't be combined");
111 llvm::Expected<std::unique_ptr<RISCVISAInfo>>
112 parseFeatureBits(bool IsRV64, const FeatureBitset &FeatureBits) {
113 unsigned XLen = IsRV64 ? 64 : 32;
114 std::vector<std::string> FeatureVector;
115 // Convert FeatureBitset to FeatureVector.
116 for (auto Feature : RISCVFeatureKV) {
117 if (FeatureBits[Feature.Value] &&
118 llvm::RISCVISAInfo::isSupportedExtensionFeature(Feature.Key))
119 FeatureVector.push_back(std::string("+") + Feature.Key);
121 return llvm::RISCVISAInfo::parseFeatures(XLen, FeatureVector);
124 } // namespace RISCVFeatures
126 // Encode VTYPE into the binary format used by the the VSETVLI instruction which
127 // is used by our MC layer representation.
129 // Bits | Name | Description
130 // -----+------------+------------------------------------------------
131 // 7 | vma | Vector mask agnostic
132 // 6 | vta | Vector tail agnostic
133 // 5:3 | vsew[2:0] | Standard element width (SEW) setting
134 // 2:0 | vlmul[2:0] | Vector register group multiplier (LMUL) setting
135 unsigned RISCVVType::encodeVTYPE(RISCVII::VLMUL VLMUL, unsigned SEW,
136 bool TailAgnostic, bool MaskAgnostic) {
137 assert(isValidSEW(SEW) && "Invalid SEW");
138 unsigned VLMULBits = static_cast<unsigned>(VLMUL);
139 unsigned VSEWBits = encodeSEW(SEW);
140 unsigned VTypeI = (VSEWBits << 3) | (VLMULBits & 0x7);
149 std::pair<unsigned, bool> RISCVVType::decodeVLMUL(RISCVII::VLMUL VLMUL) {
152 llvm_unreachable("Unexpected LMUL value!");
153 case RISCVII::VLMUL::LMUL_1:
154 case RISCVII::VLMUL::LMUL_2:
155 case RISCVII::VLMUL::LMUL_4:
156 case RISCVII::VLMUL::LMUL_8:
157 return std::make_pair(1 << static_cast<unsigned>(VLMUL), false);
158 case RISCVII::VLMUL::LMUL_F2:
159 case RISCVII::VLMUL::LMUL_F4:
160 case RISCVII::VLMUL::LMUL_F8:
161 return std::make_pair(1 << (8 - static_cast<unsigned>(VLMUL)), true);
165 void RISCVVType::printVType(unsigned VType, raw_ostream &OS) {
166 unsigned Sew = getSEW(VType);
171 std::tie(LMul, Fractional) = decodeVLMUL(getVLMUL(VType));
179 if (isTailAgnostic(VType))
184 if (isMaskAgnostic(VType))
190 unsigned RISCVVType::getSEWLMULRatio(unsigned SEW, RISCVII::VLMUL VLMul) {
193 std::tie(LMul, Fractional) = decodeVLMUL(VLMul);
195 // Convert LMul to a fixed point value with 3 fractional bits.
196 LMul = Fractional ? (8 / LMul) : (LMul * 8);
198 assert(SEW >= 8 && "Unexpected SEW value");
199 return (SEW * 8) / LMul;
202 // Include the auto-generated portion of the compress emitter.
203 #define GEN_UNCOMPRESS_INSTR
204 #define GEN_COMPRESS_INSTR
205 #include "RISCVGenCompressInstEmitter.inc"
207 bool RISCVRVC::compress(MCInst &OutInst, const MCInst &MI,
208 const MCSubtargetInfo &STI) {
209 return compressInst(OutInst, MI, STI);
212 bool RISCVRVC::uncompress(MCInst &OutInst, const MCInst &MI,
213 const MCSubtargetInfo &STI) {
214 return uncompressInst(OutInst, MI, STI);