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 errs()
64 << "'" << ABIName
65 << "' is not a recognized ABI for this target (ignoring target-abi)\n";
66 } else if (ABIName.starts_with("ilp32") && IsRV64) {
67 errs() << "32-bit ABIs are not supported for 64-bit targets (ignoring "
68 "target-abi)\n";
69 TargetABI = ABI_Unknown;
70 } else if (ABIName.starts_with("lp64") && !IsRV64) {
71 errs() << "64-bit ABIs are not supported for 32-bit targets (ignoring "
72 "target-abi)\n";
73 TargetABI = ABI_Unknown;
74 } else if (!IsRV64 && IsRVE && !IsXCheriot && TargetABI != ABI_ILP32E &&
75 TargetABI != ABI_Unknown) {
76 // TODO: move this checking to RISCVTargetLowering and RISCVAsmParser
77 errs()
78 << "Only the ilp32e ABI is supported for RV32E (ignoring target-abi)\n";
79 TargetABI = ABI_Unknown;
80 } else if (!IsRV64 && IsRVE && IsXCheriot && TargetABI != ABI_CHERIOT &&
81 TargetABI != ABI_Unknown) {
82 errs() << "Only the cheriot ABI is supported for XCheriot (ignoring "
83 "target-abi)\n";
84 TargetABI = ABI_Unknown;
85 } else if (IsRV64 && IsRVE && TargetABI != ABI_LP64E &&
86 TargetABI != ABI_Unknown) {
87 // TODO: move this checking to RISCVTargetLowering and RISCVAsmParser
88 errs()
89 << "Only the lp64e ABI is supported for RV64E (ignoring target-abi)\n";
90 TargetABI = ABI_Unknown;
91 }
92
93 if ((TargetABI == RISCVABI::ABI::ABI_ILP32E ||
94 (TargetABI == ABI_Unknown && IsRVE && !IsRV64)) &&
95 FeatureBits[RISCV::FeatureStdExtD])
96 reportFatalUsageError("ILP32E cannot be used with the D ISA extension");
97
98 if (TargetABI != ABI_Unknown)
99 return TargetABI;
100
101 // If no explicit ABI is given, try to compute the default ABI.
102 auto ISAInfo = RISCVFeatures::parseFeatureBits(STI);
103 if (!ISAInfo)
104 reportFatalUsageError(ISAInfo.takeError());
105 return getTargetABI((*ISAInfo)->computeDefaultABI());
106}
107
109 auto TargetABI = StringSwitch<ABI>(ABIName)
110 .Case("ilp32", ABI_ILP32)
111 .Case("ilp32f", ABI_ILP32F)
112 .Case("ilp32d", ABI_ILP32D)
113 .Case("ilp32e", ABI_ILP32E)
114 .Case("il32pc64", ABI_IL32PC64)
115 .Case("il32pc64f", ABI_IL32PC64F)
116 .Case("il32pc64d", ABI_IL32PC64D)
117 .Case("il32pc64e", ABI_IL32PC64E)
118 .Case("lp64", ABI_LP64)
119 .Case("lp64f", ABI_LP64F)
120 .Case("lp64d", ABI_LP64D)
121 .Case("lp64e", ABI_LP64E)
122 .Case("l64pc128", ABI_L64PC128)
123 .Case("l64pc128f", ABI_L64PC128F)
124 .Case("l64pc128d", ABI_L64PC128D)
125 .Case("cheriot", ABI_CHERIOT)
127 return TargetABI;
128}
129
130// To avoid the BP value clobbered by a function call, we need to choose a
131// callee saved register to save the value. RV32E only has X8 and X9 as callee
132// saved registers and X8 will be used as fp. So we choose X9 as bp.
133MCRegister getBPReg() { return RISCV::X9; }
134
135// Returns the register holding shadow call stack pointer.
136MCRegister getSCSPReg() { return RISCV::X3; }
137
138} // namespace RISCVABI
139
140namespace RISCVFeatures {
141
142void validate(const Triple &TT, const FeatureBitset &FeatureBits) {
143 if (TT.isArch64Bit() && !FeatureBits[RISCV::Feature64Bit])
144 reportFatalUsageError("RV64 target requires an RV64 CPU");
145 if (!TT.isArch64Bit() && !FeatureBits[RISCV::Feature32Bit])
146 reportFatalUsageError("RV32 target requires an RV32 CPU");
147 if (FeatureBits[RISCV::Feature32Bit] &&
148 FeatureBits[RISCV::Feature64Bit])
149 reportFatalUsageError("RV32 and RV64 can't be combined");
150}
151
154 const FeatureBitset &FeatureBits = STI.getFeatureBits();
155 unsigned XLen = FeatureBits[RISCV::Feature64Bit] ? 64 : 32;
156 std::vector<std::string> FeatureVector;
157 // Convert FeatureBitset to FeatureVector.
158 for (const auto &Feature : STI.getAllProcessorFeatures()) {
159 if (FeatureBits[Feature.Value] &&
161 FeatureVector.push_back(std::string("+") + Feature.key());
162 }
163 return llvm::RISCVISAInfo::parseFeatures(XLen, FeatureVector);
164}
165
166} // namespace RISCVFeatures
167
168// Include the auto-generated portion of the compress emitter.
169#define GEN_UNCOMPRESS_INSTR
170#define GEN_COMPRESS_INSTR
171#include "RISCVGenCompressInstEmitter.inc"
172
173bool RISCVRVC::compress(MCInst &OutInst, const MCInst &MI,
174 const MCSubtargetInfo &STI) {
175 return compressInst(OutInst, MI, STI);
176}
177
178bool RISCVRVC::uncompress(MCInst &OutInst, const MCInst &MI,
179 const MCSubtargetInfo &STI) {
180 return uncompressInst(OutInst, MI, STI);
181}
182
183// Lookup table for fli.s for entries 2-31.
184static constexpr std::pair<uint8_t, uint8_t> LoadFP32ImmArr[] = {
185 {0b01101111, 0b00}, {0b01110000, 0b00}, {0b01110111, 0b00},
186 {0b01111000, 0b00}, {0b01111011, 0b00}, {0b01111100, 0b00},
187 {0b01111101, 0b00}, {0b01111101, 0b01}, {0b01111101, 0b10},
188 {0b01111101, 0b11}, {0b01111110, 0b00}, {0b01111110, 0b01},
189 {0b01111110, 0b10}, {0b01111110, 0b11}, {0b01111111, 0b00},
190 {0b01111111, 0b01}, {0b01111111, 0b10}, {0b01111111, 0b11},
191 {0b10000000, 0b00}, {0b10000000, 0b01}, {0b10000000, 0b10},
192 {0b10000001, 0b00}, {0b10000010, 0b00}, {0b10000011, 0b00},
193 {0b10000110, 0b00}, {0b10000111, 0b00}, {0b10001110, 0b00},
194 {0b10001111, 0b00}, {0b11111111, 0b00}, {0b11111111, 0b10},
195};
196
198 assert((&FPImm.getSemantics() == &APFloat::IEEEsingle() ||
199 &FPImm.getSemantics() == &APFloat::IEEEdouble() ||
200 &FPImm.getSemantics() == &APFloat::IEEEhalf()) &&
201 "Unexpected semantics");
202
203 // Handle the minimum normalized value which is different for each type.
204 if (FPImm.isSmallestNormalized() && !FPImm.isNegative())
205 return 1;
206
207 // Convert to single precision to use its lookup table.
208 bool LosesInfo;
211 if (Status != APFloat::opOK || LosesInfo)
212 return -1;
213
214 APInt Imm = FPImm.bitcastToAPInt();
215
216 if (Imm.extractBitsAsZExtValue(21, 0) != 0)
217 return -1;
218
219 bool Sign = Imm.extractBitsAsZExtValue(1, 31);
220 uint8_t Mantissa = Imm.extractBitsAsZExtValue(2, 21);
221 uint8_t Exp = Imm.extractBitsAsZExtValue(8, 23);
222
223 auto EMI = llvm::lower_bound(LoadFP32ImmArr, std::make_pair(Exp, Mantissa));
224 if (EMI == std::end(LoadFP32ImmArr) || EMI->first != Exp ||
225 EMI->second != Mantissa)
226 return -1;
227
228 // Table doesn't have entry 0 or 1.
229 int Entry = std::distance(std::begin(LoadFP32ImmArr), EMI) + 2;
230
231 // The only legal negative value is -1.0(entry 0). 1.0 is entry 16.
232 if (Sign) {
233 if (Entry == 16)
234 return 0;
235 return -1;
236 }
237
238 return Entry;
239}
240
242 assert(Imm != 1 && Imm != 30 && Imm != 31 && "Unsupported immediate");
243
244 // Entry 0 is -1.0, the only negative value. Entry 16 is 1.0.
245 uint32_t Sign = 0;
246 if (Imm == 0) {
247 Sign = 0b1;
248 Imm = 16;
249 }
250
251 uint32_t Exp = LoadFP32ImmArr[Imm - 2].first;
252 uint32_t Mantissa = LoadFP32ImmArr[Imm - 2].second;
253
254 uint32_t I = Sign << 31 | Exp << 23 | Mantissa << 21;
255 return bit_cast<float>(I);
256}
257
258void RISCVZC::printRegList(unsigned RlistEncode, raw_ostream &OS) {
259 assert(RlistEncode >= RLISTENCODE::RA &&
260 RlistEncode <= RLISTENCODE::RA_S0_S11 && "Invalid Rlist");
261 OS << "{ra";
262 if (RlistEncode > RISCVZC::RA) {
263 OS << ", s0";
264 if (RlistEncode == RISCVZC::RA_S0_S11)
265 OS << "-s11";
266 else if (RlistEncode > RISCVZC::RA_S0 && RlistEncode <= RISCVZC::RA_S0_S11)
267 OS << "-s" << (RlistEncode - RISCVZC::RA_S0);
268 }
269 OS << "}";
270}
271
272} // 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:6010
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
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
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()
ABI computeTargetABI(const MCSubtargetInfo &STI, StringRef ABIName)
MCRegister getSCSPReg()
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.
static constexpr std::pair< uint8_t, uint8_t > LoadFP32ImmArr[]
LLVM_ABI raw_fd_ostream & errs()
This returns a reference to a raw_ostream for standard error.
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:2052
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