LLVM 19.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 "llvm/ADT/ArrayRef.h"
16#include "llvm/MC/MCInst.h"
23
24namespace llvm {
25
26extern const SubtargetFeatureKV RISCVFeatureKV[RISCV::NumSubtargetFeatures];
27
28namespace RISCVSysReg {
29#define GET_SysRegsList_IMPL
30#include "RISCVGenSearchableTables.inc"
31} // namespace RISCVSysReg
32
33namespace RISCVInsnOpcode {
34#define GET_RISCVOpcodesList_IMPL
35#include "RISCVGenSearchableTables.inc"
36} // namespace RISCVInsnOpcode
37
38namespace RISCVABI {
39ABI computeTargetABI(const Triple &TT, const FeatureBitset &FeatureBits,
40 StringRef ABIName) {
41 auto TargetABI = getTargetABI(ABIName);
42 bool IsRV64 = TT.isArch64Bit();
43 bool IsRVE = FeatureBits[RISCV::FeatureRVE];
44
45 if (!ABIName.empty() && TargetABI == ABI_Unknown) {
46 errs()
47 << "'" << ABIName
48 << "' is not a recognized ABI for this target (ignoring target-abi)\n";
49 } else if (ABIName.starts_with("ilp32") && IsRV64) {
50 errs() << "32-bit ABIs are not supported for 64-bit targets (ignoring "
51 "target-abi)\n";
52 TargetABI = ABI_Unknown;
53 } else if (ABIName.starts_with("lp64") && !IsRV64) {
54 errs() << "64-bit ABIs are not supported for 32-bit targets (ignoring "
55 "target-abi)\n";
56 TargetABI = ABI_Unknown;
57 } else if (!IsRV64 && IsRVE && TargetABI != ABI_ILP32E &&
58 TargetABI != ABI_Unknown) {
59 // TODO: move this checking to RISCVTargetLowering and RISCVAsmParser
60 errs()
61 << "Only the ilp32e ABI is supported for RV32E (ignoring target-abi)\n";
62 TargetABI = ABI_Unknown;
63 } else if (IsRV64 && IsRVE && TargetABI != ABI_LP64E &&
64 TargetABI != ABI_Unknown) {
65 // TODO: move this checking to RISCVTargetLowering and RISCVAsmParser
66 errs()
67 << "Only the lp64e ABI is supported for RV64E (ignoring target-abi)\n";
68 TargetABI = ABI_Unknown;
69 }
70
71 if ((TargetABI == RISCVABI::ABI::ABI_ILP32E ||
72 (TargetABI == ABI_Unknown && IsRVE && !IsRV64)) &&
73 FeatureBits[RISCV::FeatureStdExtD])
74 report_fatal_error("ILP32E cannot be used with the D ISA extension");
75
76 if (TargetABI != ABI_Unknown)
77 return TargetABI;
78
79 // If no explicit ABI is given, try to compute the default ABI.
80 auto ISAInfo = RISCVFeatures::parseFeatureBits(IsRV64, FeatureBits);
81 if (!ISAInfo)
82 report_fatal_error(ISAInfo.takeError());
83 return getTargetABI((*ISAInfo)->computeDefaultABI());
84}
85
87 auto TargetABI = StringSwitch<ABI>(ABIName)
88 .Case("ilp32", ABI_ILP32)
89 .Case("ilp32f", ABI_ILP32F)
90 .Case("ilp32d", ABI_ILP32D)
91 .Case("ilp32e", ABI_ILP32E)
92 .Case("lp64", ABI_LP64)
93 .Case("lp64f", ABI_LP64F)
94 .Case("lp64d", ABI_LP64D)
95 .Case("lp64e", ABI_LP64E)
97 return TargetABI;
98}
99
100// To avoid the BP value clobbered by a function call, we need to choose a
101// callee saved register to save the value. RV32E only has X8 and X9 as callee
102// saved registers and X8 will be used as fp. So we choose X9 as bp.
103MCRegister getBPReg() { return RISCV::X9; }
104
105// Returns the register holding shadow call stack pointer.
106MCRegister getSCSPReg() { return RISCV::X3; }
107
108} // namespace RISCVABI
109
110namespace RISCVFeatures {
111
112void validate(const Triple &TT, const FeatureBitset &FeatureBits) {
113 if (TT.isArch64Bit() && !FeatureBits[RISCV::Feature64Bit])
114 report_fatal_error("RV64 target requires an RV64 CPU");
115 if (!TT.isArch64Bit() && !FeatureBits[RISCV::Feature32Bit])
116 report_fatal_error("RV32 target requires an RV32 CPU");
117 if (FeatureBits[RISCV::Feature32Bit] &&
118 FeatureBits[RISCV::Feature64Bit])
119 report_fatal_error("RV32 and RV64 can't be combined");
120}
121
123parseFeatureBits(bool IsRV64, const FeatureBitset &FeatureBits) {
124 unsigned XLen = IsRV64 ? 64 : 32;
125 std::vector<std::string> FeatureVector;
126 // Convert FeatureBitset to FeatureVector.
127 for (auto Feature : RISCVFeatureKV) {
128 if (FeatureBits[Feature.Value] &&
130 FeatureVector.push_back(std::string("+") + Feature.Key);
131 }
132 return llvm::RISCVISAInfo::parseFeatures(XLen, FeatureVector);
133}
134
135} // namespace RISCVFeatures
136
137// Include the auto-generated portion of the compress emitter.
138#define GEN_UNCOMPRESS_INSTR
139#define GEN_COMPRESS_INSTR
140#include "RISCVGenCompressInstEmitter.inc"
141
142bool RISCVRVC::compress(MCInst &OutInst, const MCInst &MI,
143 const MCSubtargetInfo &STI) {
144 return compressInst(OutInst, MI, STI);
145}
146
147bool RISCVRVC::uncompress(MCInst &OutInst, const MCInst &MI,
148 const MCSubtargetInfo &STI) {
149 return uncompressInst(OutInst, MI, STI);
150}
151
152// Lookup table for fli.s for entries 2-31.
153static constexpr std::pair<uint8_t, uint8_t> LoadFP32ImmArr[] = {
154 {0b01101111, 0b00}, {0b01110000, 0b00}, {0b01110111, 0b00},
155 {0b01111000, 0b00}, {0b01111011, 0b00}, {0b01111100, 0b00},
156 {0b01111101, 0b00}, {0b01111101, 0b01}, {0b01111101, 0b10},
157 {0b01111101, 0b11}, {0b01111110, 0b00}, {0b01111110, 0b01},
158 {0b01111110, 0b10}, {0b01111110, 0b11}, {0b01111111, 0b00},
159 {0b01111111, 0b01}, {0b01111111, 0b10}, {0b01111111, 0b11},
160 {0b10000000, 0b00}, {0b10000000, 0b01}, {0b10000000, 0b10},
161 {0b10000001, 0b00}, {0b10000010, 0b00}, {0b10000011, 0b00},
162 {0b10000110, 0b00}, {0b10000111, 0b00}, {0b10001110, 0b00},
163 {0b10001111, 0b00}, {0b11111111, 0b00}, {0b11111111, 0b10},
164};
165
167 assert((&FPImm.getSemantics() == &APFloat::IEEEsingle() ||
168 &FPImm.getSemantics() == &APFloat::IEEEdouble() ||
169 &FPImm.getSemantics() == &APFloat::IEEEhalf()) &&
170 "Unexpected semantics");
171
172 // Handle the minimum normalized value which is different for each type.
173 if (FPImm.isSmallestNormalized() && !FPImm.isNegative())
174 return 1;
175
176 // Convert to single precision to use its lookup table.
177 bool LosesInfo;
180 if (Status != APFloat::opOK || LosesInfo)
181 return -1;
182
183 APInt Imm = FPImm.bitcastToAPInt();
184
185 if (Imm.extractBitsAsZExtValue(21, 0) != 0)
186 return -1;
187
188 bool Sign = Imm.extractBitsAsZExtValue(1, 31);
189 uint8_t Mantissa = Imm.extractBitsAsZExtValue(2, 21);
190 uint8_t Exp = Imm.extractBitsAsZExtValue(8, 23);
191
192 auto EMI = llvm::lower_bound(LoadFP32ImmArr, std::make_pair(Exp, Mantissa));
193 if (EMI == std::end(LoadFP32ImmArr) || EMI->first != Exp ||
194 EMI->second != Mantissa)
195 return -1;
196
197 // Table doesn't have entry 0 or 1.
198 int Entry = std::distance(std::begin(LoadFP32ImmArr), EMI) + 2;
199
200 // The only legal negative value is -1.0(entry 0). 1.0 is entry 16.
201 if (Sign) {
202 if (Entry == 16)
203 return 0;
204 return -1;
205 }
206
207 return Entry;
208}
209
210float RISCVLoadFPImm::getFPImm(unsigned Imm) {
211 assert(Imm != 1 && Imm != 30 && Imm != 31 && "Unsupported immediate");
212
213 // Entry 0 is -1.0, the only negative value. Entry 16 is 1.0.
214 uint32_t Sign = 0;
215 if (Imm == 0) {
216 Sign = 0b1;
217 Imm = 16;
218 }
219
220 uint32_t Exp = LoadFP32ImmArr[Imm - 2].first;
221 uint32_t Mantissa = LoadFP32ImmArr[Imm - 2].second;
222
223 uint32_t I = Sign << 31 | Exp << 23 | Mantissa << 21;
224 return bit_cast<float>(I);
225}
226
227void RISCVZC::printRlist(unsigned SlistEncode, raw_ostream &OS) {
228 OS << "{ra";
229 if (SlistEncode > 4) {
230 OS << ", s0";
231 if (SlistEncode == 15)
232 OS << "-s11";
233 else if (SlistEncode > 5 && SlistEncode <= 14)
234 OS << "-s" << (SlistEncode - 5);
235 }
236 OS << "}";
237}
238
239void RISCVZC::printSpimm(int64_t Spimm, raw_ostream &OS) { OS << Spimm; }
240
241} // namespace llvm
IRTranslator LLVM IR MI
#define I(x, y, z)
Definition: MD5.cpp:58
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
raw_pwrite_stream & OS
opStatus convert(const fltSemantics &ToSemantics, roundingMode RM, bool *losesInfo)
Definition: APFloat.cpp:5196
bool isNegative() const
Definition: APFloat.h:1295
const fltSemantics & getSemantics() const
Definition: APFloat.h:1303
APInt bitcastToAPInt() const
Definition: APFloat.h:1210
bool isSmallestNormalized() const
Definition: APFloat.h:1315
Class for arbitrary precision integers.
Definition: APInt.h:76
Tagged union holding either a T or a Error.
Definition: Error.h:474
Container class for subtarget features.
Instances of this class represent a single low-level machine instruction.
Definition: MCInst.h:184
Wrapper class representing physical registers. Should be passed by value.
Definition: MCRegister.h:33
Generic base class for all target subtargets.
static bool isSupportedExtensionFeature(StringRef Ext)
static llvm::Expected< std::unique_ptr< RISCVISAInfo > > parseFeatures(unsigned XLen, const std::vector< std::string > &Features)
Parse RISC-V ISA info from feature vector.
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
bool starts_with(StringRef Prefix) const
Check if this string starts with the given Prefix.
Definition: StringRef.h:257
constexpr bool empty() const
empty - Check if the string is empty.
Definition: StringRef.h:134
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
ABI getTargetABI(StringRef ABIName)
ABI computeTargetABI(const Triple &TT, const FeatureBitset &FeatureBits, StringRef ABIName)
MCRegister getBPReg()
MCRegister getSCSPReg()
void validate(const Triple &TT, const FeatureBitset &FeatureBits)
llvm::Expected< std::unique_ptr< RISCVISAInfo > > parseFeatureBits(bool IsRV64, const FeatureBitset &FeatureBits)
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 printRlist(unsigned SlistEncode, raw_ostream &OS)
void printSpimm(int64_t Spimm, raw_ostream &OS)
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
static constexpr std::pair< uint8_t, uint8_t > LoadFP32ImmArr[]
void report_fatal_error(Error Err, bool gen_crash_diag=true)
Report a serious error, calling any installed error handler.
Definition: Error.cpp:156
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:1963
const SubtargetFeatureKV RISCVFeatureKV[RISCV::NumSubtargetFeatures]
static const fltSemantics & IEEEsingle() LLVM_READNONE
Definition: APFloat.cpp:249
static constexpr roundingMode rmNearestTiesToEven
Definition: APFloat.h:230
static const fltSemantics & IEEEdouble() LLVM_READNONE
Definition: APFloat.cpp:250
static const fltSemantics & IEEEhalf() LLVM_READNONE
Definition: APFloat.cpp:247
opStatus
IEEE-754R 7: Default exception handling.
Definition: APFloat.h:246
Used to provide key value pairs for feature and CPU bit flags.