LLVM 24.0.0git
RISCVBaseInfo.cpp
Go to the documentation of this file.
1//===-- RISCVBaseInfo.cpp - Top level definitions for RISC-V MC -----------===//
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
14#include "RISCVBaseInfo.h"
15#include "RISCVMCAsmInfo.h"
16#include "llvm/MC/MCInst.h"
21
22namespace llvm {
23
24namespace RISCVSysReg {
25#define GET_SysRegsList_IMPL
26#include "RISCVGenSearchableTables.inc"
27} // namespace RISCVSysReg
28
29namespace RISCVInsnOpcode {
30#define GET_RISCVOpcodesList_IMPL
31#include "RISCVGenSearchableTables.inc"
32} // namespace RISCVInsnOpcode
33
35using namespace RISCV;
36#define GET_RISCVVInversePseudosTable_IMPL
37#include "RISCVGenSearchableTables.inc"
38} // namespace RISCVVInversePseudosTable
39
40namespace RISCV {
41#define GET_RISCVVSSEGTable_IMPL
42#define GET_RISCVVLSEGTable_IMPL
43#define GET_RISCVVLXSEGTable_IMPL
44#define GET_RISCVVSXSEGTable_IMPL
45#define GET_RISCVVLETable_IMPL
46#define GET_RISCVVSETable_IMPL
47#define GET_RISCVVLXTable_IMPL
48#define GET_RISCVVSXTable_IMPL
49#define GET_RISCVNDSVLNTable_IMPL
50#include "RISCVGenSearchableTables.inc"
51} // namespace RISCV
52
53namespace RISCVABI {
55 const Triple &TT = STI.getTargetTriple();
56 const FeatureBitset &FeatureBits = STI.getFeatureBits();
57 auto TargetABI = getTargetABI(ABIName);
58 bool IsRV64 = TT.isArch64Bit();
59 bool IsRVE = FeatureBits[RISCV::FeatureStdExtE];
60 bool IsXCheriot = FeatureBits[RISCV::FeatureVendorXCheriot];
61
62 if (!ABIName.empty() && TargetABI == ABI_Unknown) {
63 return createStringError(Twine("'") + ABIName +
64 "' is not a recognized ABI for this target");
65 }
66 if (IsRV64 &&
67 (ABIName.starts_with("ilp32") || ABIName.starts_with("il32pc64"))) {
68 return createStringError(
69 "32-bit ABIs are not supported for 64-bit targets");
70 }
71 if (!IsRV64 &&
72 (ABIName.starts_with("lp64") || ABIName.starts_with("l64pc128"))) {
73 return createStringError(
74 "64-bit ABIs are not supported for 32-bit targets");
75 }
76 if (ABIName.ends_with('f') && !FeatureBits[RISCV::FeatureStdExtF]) {
77 return createStringError(
78 "hard-float 'f' ABI can't be used for a target that doesn't "
79 "support the F instruction set extension");
80 }
81 if (ABIName.ends_with('d') && !FeatureBits[RISCV::FeatureStdExtD]) {
82 return createStringError(
83 "hard-float 'd' ABI can't be used for a target that doesn't "
84 "support the D instruction set extension");
85 }
86 if (!FeatureBits[RISCV::FeatureStdExtY] &&
87 (ABIName.starts_with("il32pc64") || ABIName.starts_with("l64pc128"))) {
88 return createStringError(Twine('\'') + ABIName +
89 "' ABI is only supported for RVY targets");
90 }
91 if (!IsRV64 && IsRVE && !IsXCheriot && TargetABI != ABI_ILP32E &&
92 TargetABI != ABI_Unknown) {
93 return createStringError("only the ilp32e ABI is supported for RV32E");
94 }
95 if (!IsRV64 && IsRVE && IsXCheriot && TargetABI != ABI_CHERIOT &&
96 TargetABI != ABI_Unknown) {
97 return createStringError("only the cheriot ABI is supported for XCheriot");
98 }
99 if (IsRV64 && IsRVE && TargetABI != ABI_LP64E && TargetABI != ABI_Unknown) {
100 return createStringError("only the lp64e ABI is supported for RV64E");
101 }
102
103 // Unconditionally fatal: no sensible default ABI to fall back to here.
104 if ((TargetABI == ABI_ILP32E ||
105 (TargetABI == ABI_Unknown && IsRVE && !IsRV64)) &&
106 FeatureBits[RISCV::FeatureStdExtD])
107 reportFatalUsageError("ILP32E cannot be used with the D ISA extension");
108
109 if (TargetABI != ABI_Unknown)
110 return TargetABI;
111
112 // If no explicit ABI is given, try to compute the default ABI.
113 auto ISAInfo = RISCVFeatures::parseFeatureBits(STI);
114 if (!ISAInfo)
115 reportFatalUsageError(ISAInfo.takeError());
116 return getTargetABI((*ISAInfo)->computeDefaultABI());
117}
118
120 auto TargetABI = StringSwitch<ABI>(ABIName)
121 .Case("ilp32", ABI_ILP32)
122 .Case("ilp32f", ABI_ILP32F)
123 .Case("ilp32d", ABI_ILP32D)
124 .Case("ilp32e", ABI_ILP32E)
125 .Case("il32pc64", ABI_IL32PC64)
126 .Case("il32pc64f", ABI_IL32PC64F)
127 .Case("il32pc64d", ABI_IL32PC64D)
128 .Case("il32pc64e", ABI_IL32PC64E)
129 .Case("lp64", ABI_LP64)
130 .Case("lp64f", ABI_LP64F)
131 .Case("lp64d", ABI_LP64D)
132 .Case("lp64e", ABI_LP64E)
133 .Case("l64pc128", ABI_L64PC128)
134 .Case("l64pc128f", ABI_L64PC128F)
135 .Case("l64pc128d", ABI_L64PC128D)
136 .Case("cheriot", ABI_CHERIOT)
138 return TargetABI;
139}
140
141// To avoid the BP value clobbered by a function call, we need to choose a
142// callee saved register to save the value. RV32E only has X8 and X9 as callee
143// saved registers and X8 will be used as fp. So we choose X9 as bp.
144MCRegister getBPReg() { return RISCV::X9; }
145
146// Returns the register holding shadow call stack pointer.
147MCRegister getSCSPReg() { return RISCV::X3; }
148
149} // namespace RISCVABI
150
151namespace RISCVFeatures {
152
153void validate(const Triple &TT, const FeatureBitset &FeatureBits) {
154 if (TT.isArch64Bit() && !FeatureBits[RISCV::Feature64Bit])
155 reportFatalUsageError("RV64 target requires an RV64 CPU");
156 if (!TT.isArch64Bit() && !FeatureBits[RISCV::Feature32Bit])
157 reportFatalUsageError("RV32 target requires an RV32 CPU");
158 if (FeatureBits[RISCV::Feature32Bit] &&
159 FeatureBits[RISCV::Feature64Bit])
160 reportFatalUsageError("RV32 and RV64 can't be combined");
161}
162
165 const FeatureBitset &FeatureBits = STI.getFeatureBits();
166 unsigned XLen = FeatureBits[RISCV::Feature64Bit] ? 64 : 32;
167 std::vector<std::string> FeatureVector;
168 // Convert FeatureBitset to FeatureVector.
169 for (const auto &Feature : STI.getAllProcessorFeatures()) {
170 if (FeatureBits[Feature.Value] &&
172 FeatureVector.push_back(std::string("+") + Feature.key());
173 }
174 return llvm::RISCVISAInfo::parseFeatures(XLen, FeatureVector);
175}
176
177} // namespace RISCVFeatures
178
179// Include the auto-generated portion of the compress emitter.
180#define GEN_UNCOMPRESS_INSTR
181#define GEN_COMPRESS_INSTR
182#include "RISCVGenCompressInstEmitter.inc"
183
184bool RISCVRVC::compress(MCInst &OutInst, const MCInst &MI,
185 const MCSubtargetInfo &STI) {
186 return compressInst(OutInst, MI, STI);
187}
188
189bool RISCVRVC::uncompress(MCInst &OutInst, const MCInst &MI,
190 const MCSubtargetInfo &STI) {
191 return uncompressInst(OutInst, MI, STI);
192}
193
194// Lookup table for fli.s for entries 2-31.
195static constexpr std::pair<uint8_t, uint8_t> LoadFP32ImmArr[] = {
196 {0b01101111, 0b00}, {0b01110000, 0b00}, {0b01110111, 0b00},
197 {0b01111000, 0b00}, {0b01111011, 0b00}, {0b01111100, 0b00},
198 {0b01111101, 0b00}, {0b01111101, 0b01}, {0b01111101, 0b10},
199 {0b01111101, 0b11}, {0b01111110, 0b00}, {0b01111110, 0b01},
200 {0b01111110, 0b10}, {0b01111110, 0b11}, {0b01111111, 0b00},
201 {0b01111111, 0b01}, {0b01111111, 0b10}, {0b01111111, 0b11},
202 {0b10000000, 0b00}, {0b10000000, 0b01}, {0b10000000, 0b10},
203 {0b10000001, 0b00}, {0b10000010, 0b00}, {0b10000011, 0b00},
204 {0b10000110, 0b00}, {0b10000111, 0b00}, {0b10001110, 0b00},
205 {0b10001111, 0b00}, {0b11111111, 0b00}, {0b11111111, 0b10},
206};
207
209 assert((&FPImm.getSemantics() == &APFloat::IEEEsingle() ||
210 &FPImm.getSemantics() == &APFloat::IEEEdouble() ||
211 &FPImm.getSemantics() == &APFloat::IEEEhalf()) &&
212 "Unexpected semantics");
213
214 // Handle the minimum normalized value which is different for each type.
215 if (FPImm.isSmallestNormalized() && !FPImm.isNegative())
216 return 1;
217
218 // Convert to single precision to use its lookup table.
219 bool LosesInfo;
222 if (Status != APFloat::opOK || LosesInfo)
223 return -1;
224
225 APInt Imm = FPImm.bitcastToAPInt();
226
227 if (Imm.extractBitsAsZExtValue(21, 0) != 0)
228 return -1;
229
230 bool Sign = Imm.extractBitsAsZExtValue(1, 31);
231 uint8_t Mantissa = Imm.extractBitsAsZExtValue(2, 21);
232 uint8_t Exp = Imm.extractBitsAsZExtValue(8, 23);
233
234 auto EMI = llvm::lower_bound(LoadFP32ImmArr, std::make_pair(Exp, Mantissa));
235 if (EMI == std::end(LoadFP32ImmArr) || EMI->first != Exp ||
236 EMI->second != Mantissa)
237 return -1;
238
239 // Table doesn't have entry 0 or 1.
240 int Entry = std::distance(std::begin(LoadFP32ImmArr), EMI) + 2;
241
242 // The only legal negative value is -1.0(entry 0). 1.0 is entry 16.
243 if (Sign) {
244 if (Entry == 16)
245 return 0;
246 return -1;
247 }
248
249 return Entry;
250}
251
253 assert(Imm != 1 && Imm != 30 && Imm != 31 && "Unsupported immediate");
254
255 // Entry 0 is -1.0, the only negative value. Entry 16 is 1.0.
256 uint32_t Sign = 0;
257 if (Imm == 0) {
258 Sign = 0b1;
259 Imm = 16;
260 }
261
262 uint32_t Exp = LoadFP32ImmArr[Imm - 2].first;
263 uint32_t Mantissa = LoadFP32ImmArr[Imm - 2].second;
264
265 uint32_t I = Sign << 31 | Exp << 23 | Mantissa << 21;
266 return bit_cast<float>(I);
267}
268
269void RISCVZC::printRegList(unsigned RlistEncode, raw_ostream &OS) {
270 assert(RlistEncode >= RLISTENCODE::RA &&
271 RlistEncode <= RLISTENCODE::RA_S0_S11 && "Invalid Rlist");
272 OS << "{ra";
273 if (RlistEncode > RISCVZC::RA) {
274 OS << ", s0";
275 if (RlistEncode == RISCVZC::RA_S0_S11)
276 OS << "-s11";
277 else if (RlistEncode > RISCVZC::RA_S0 && RlistEncode <= RISCVZC::RA_S0_S11)
278 OS << "-s" << (RlistEncode - RISCVZC::RA_S0);
279 }
280 OS << "}";
281}
282
283} // namespace llvm
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
unsigned Imm
IRTranslator LLVM IR MI
#define I(x, y, z)
Definition MD5.cpp:57
static const fltSemantics & IEEEsingle()
Definition APFloat.h:304
static const fltSemantics & IEEEdouble()
Definition APFloat.h:305
static constexpr roundingMode rmNearestTiesToEven
Definition APFloat.h:361
static const fltSemantics & IEEEhalf()
Definition APFloat.h:302
opStatus
IEEE-754R 7: Default exception handling.
Definition APFloat.h:377
LLVM_ABI opStatus convert(const fltSemantics &ToSemantics, roundingMode RM, bool *losesInfo)
Definition APFloat.cpp:6032
bool isNegative() const
Definition APFloat.h:1583
const fltSemantics & getSemantics() const
Definition APFloat.h:1591
APInt bitcastToAPInt() const
Definition APFloat.h:1475
bool isSmallestNormalized() const
Definition APFloat.h:1602
Class for arbitrary precision integers.
Definition APInt.h:78
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
Wrapper class representing physical registers. Should be passed by value.
Definition MCRegister.h:41
Generic base class for all target subtargets.
const Triple & getTargetTriple() const
const FeatureBitset & getFeatureBits() const
ArrayRef< SubtargetFeatureKV > getAllProcessorFeatures() const
Return processor features.
static LLVM_ABI bool isSupportedExtensionFeature(StringRef Ext)
static LLVM_ABI llvm::Expected< std::unique_ptr< RISCVISAInfo > > parseFeatures(unsigned XLen, const std::vector< std::string > &Features)
Parse RISC-V ISA info from feature vector.
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
bool starts_with(StringRef Prefix) const
Check if this string starts with the given Prefix.
Definition StringRef.h:258
constexpr bool empty() const
Check if the string is empty.
Definition StringRef.h:141
bool ends_with(StringRef Suffix) const
Check if this string ends with the given Suffix.
Definition StringRef.h:270
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:48
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition Twine.h:82
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition raw_ostream.h:53
ABI getTargetABI(StringRef ABIName)
MCRegister getBPReg()
MCRegister getSCSPReg()
Expected< ABI > computeTargetABI(const MCSubtargetInfo &STI, StringRef ABIName)
void validate(const Triple &TT, const FeatureBitset &FeatureBits)
llvm::Expected< std::unique_ptr< RISCVISAInfo > > parseFeatureBits(const MCSubtargetInfo &STI)
int getLoadFPImm(APFloat FPImm)
getLoadFPImm - Return a 5-bit binary encoding of the floating-point immediate value.
float getFPImm(unsigned Imm)
bool uncompress(MCInst &OutInst, const MCInst &MI, const MCSubtargetInfo &STI)
bool compress(MCInst &OutInst, const MCInst &MI, const MCSubtargetInfo &STI)
void printRegList(unsigned RlistEncode, raw_ostream &OS)
This is an optimization pass for GlobalISel generic memory operations.
Error createStringError(std::error_code EC, char const *Fmt, const Ts &... Vals)
Create formatted StringError object.
Definition Error.h:1321
static constexpr std::pair< uint8_t, uint8_t > LoadFP32ImmArr[]
auto lower_bound(R &&Range, T &&Value)
Provide wrappers to std::lower_bound which take ranges instead of having to pass begin/end explicitly...
Definition STLExtras.h:2068
To bit_cast(const From &from) noexcept
Definition bit.h:90
LLVM_ABI void reportFatalUsageError(Error Err)
Report a fatal error that does not indicate a bug in LLVM.
Definition Error.cpp:177