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" 17 #include "llvm/MC/MCRegisterInfo.h" 18 #include "llvm/MC/MCSubtargetInfo.h" 19 #include "llvm/Support/RISCVISAInfo.h" 20 #include "llvm/Support/raw_ostream.h" 21 #include "llvm/TargetParser/TargetParser.h" 22 #include "llvm/TargetParser/Triple.h" 23 24 namespace llvm { 25 26 extern const SubtargetFeatureKV RISCVFeatureKV[RISCV::NumSubtargetFeatures]; 27 28 namespace RISCVSysReg { 29 #define GET_SysRegsList_IMPL 30 #include "RISCVGenSearchableTables.inc" 31 } // namespace RISCVSysReg 32 33 namespace RISCVInsnOpcode { 34 #define GET_RISCVOpcodesList_IMPL 35 #include "RISCVGenSearchableTables.inc" 36 } // namespace RISCVInsnOpcode 37 38 namespace RISCVABI { 39 ABI 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 != ABI_Unknown) 72 return TargetABI; 73 74 // If no explicit ABI is given, try to compute the default ABI. 75 auto ISAInfo = RISCVFeatures::parseFeatureBits(IsRV64, FeatureBits); 76 if (!ISAInfo) 77 report_fatal_error(ISAInfo.takeError()); 78 return getTargetABI((*ISAInfo)->computeDefaultABI()); 79 } 80 81 ABI getTargetABI(StringRef ABIName) { 82 auto TargetABI = StringSwitch<ABI>(ABIName) 83 .Case("ilp32", ABI_ILP32) 84 .Case("ilp32f", ABI_ILP32F) 85 .Case("ilp32d", ABI_ILP32D) 86 .Case("ilp32e", ABI_ILP32E) 87 .Case("lp64", ABI_LP64) 88 .Case("lp64f", ABI_LP64F) 89 .Case("lp64d", ABI_LP64D) 90 .Case("lp64e", ABI_LP64E) 91 .Default(ABI_Unknown); 92 return TargetABI; 93 } 94 95 // To avoid the BP value clobbered by a function call, we need to choose a 96 // callee saved register to save the value. RV32E only has X8 and X9 as callee 97 // saved registers and X8 will be used as fp. So we choose X9 as bp. 98 MCRegister getBPReg() { return RISCV::X9; } 99 100 // Returns the register holding shadow call stack pointer. 101 MCRegister getSCSPReg() { return RISCV::X3; } 102 103 } // namespace RISCVABI 104 105 namespace RISCVFeatures { 106 107 void validate(const Triple &TT, const FeatureBitset &FeatureBits) { 108 if (TT.isArch64Bit() && !FeatureBits[RISCV::Feature64Bit]) 109 report_fatal_error("RV64 target requires an RV64 CPU"); 110 if (!TT.isArch64Bit() && !FeatureBits[RISCV::Feature32Bit]) 111 report_fatal_error("RV32 target requires an RV32 CPU"); 112 if (FeatureBits[RISCV::Feature32Bit] && 113 FeatureBits[RISCV::Feature64Bit]) 114 report_fatal_error("RV32 and RV64 can't be combined"); 115 } 116 117 llvm::Expected<std::unique_ptr<RISCVISAInfo>> 118 parseFeatureBits(bool IsRV64, const FeatureBitset &FeatureBits) { 119 unsigned XLen = IsRV64 ? 64 : 32; 120 std::vector<std::string> FeatureVector; 121 // Convert FeatureBitset to FeatureVector. 122 for (auto Feature : RISCVFeatureKV) { 123 if (FeatureBits[Feature.Value] && 124 llvm::RISCVISAInfo::isSupportedExtensionFeature(Feature.Key)) 125 FeatureVector.push_back(std::string("+") + Feature.Key); 126 } 127 return llvm::RISCVISAInfo::parseFeatures(XLen, FeatureVector); 128 } 129 130 } // namespace RISCVFeatures 131 132 // Encode VTYPE into the binary format used by the the VSETVLI instruction which 133 // is used by our MC layer representation. 134 // 135 // Bits | Name | Description 136 // -----+------------+------------------------------------------------ 137 // 7 | vma | Vector mask agnostic 138 // 6 | vta | Vector tail agnostic 139 // 5:3 | vsew[2:0] | Standard element width (SEW) setting 140 // 2:0 | vlmul[2:0] | Vector register group multiplier (LMUL) setting 141 unsigned RISCVVType::encodeVTYPE(RISCVII::VLMUL VLMUL, unsigned SEW, 142 bool TailAgnostic, bool MaskAgnostic) { 143 assert(isValidSEW(SEW) && "Invalid SEW"); 144 unsigned VLMULBits = static_cast<unsigned>(VLMUL); 145 unsigned VSEWBits = encodeSEW(SEW); 146 unsigned VTypeI = (VSEWBits << 3) | (VLMULBits & 0x7); 147 if (TailAgnostic) 148 VTypeI |= 0x40; 149 if (MaskAgnostic) 150 VTypeI |= 0x80; 151 152 return VTypeI; 153 } 154 155 std::pair<unsigned, bool> RISCVVType::decodeVLMUL(RISCVII::VLMUL VLMUL) { 156 switch (VLMUL) { 157 default: 158 llvm_unreachable("Unexpected LMUL value!"); 159 case RISCVII::VLMUL::LMUL_1: 160 case RISCVII::VLMUL::LMUL_2: 161 case RISCVII::VLMUL::LMUL_4: 162 case RISCVII::VLMUL::LMUL_8: 163 return std::make_pair(1 << static_cast<unsigned>(VLMUL), false); 164 case RISCVII::VLMUL::LMUL_F2: 165 case RISCVII::VLMUL::LMUL_F4: 166 case RISCVII::VLMUL::LMUL_F8: 167 return std::make_pair(1 << (8 - static_cast<unsigned>(VLMUL)), true); 168 } 169 } 170 171 void RISCVVType::printVType(unsigned VType, raw_ostream &OS) { 172 unsigned Sew = getSEW(VType); 173 OS << "e" << Sew; 174 175 unsigned LMul; 176 bool Fractional; 177 std::tie(LMul, Fractional) = decodeVLMUL(getVLMUL(VType)); 178 179 if (Fractional) 180 OS << ", mf"; 181 else 182 OS << ", m"; 183 OS << LMul; 184 185 if (isTailAgnostic(VType)) 186 OS << ", ta"; 187 else 188 OS << ", tu"; 189 190 if (isMaskAgnostic(VType)) 191 OS << ", ma"; 192 else 193 OS << ", mu"; 194 } 195 196 unsigned RISCVVType::getSEWLMULRatio(unsigned SEW, RISCVII::VLMUL VLMul) { 197 unsigned LMul; 198 bool Fractional; 199 std::tie(LMul, Fractional) = decodeVLMUL(VLMul); 200 201 // Convert LMul to a fixed point value with 3 fractional bits. 202 LMul = Fractional ? (8 / LMul) : (LMul * 8); 203 204 assert(SEW >= 8 && "Unexpected SEW value"); 205 return (SEW * 8) / LMul; 206 } 207 208 std::optional<RISCVII::VLMUL> 209 RISCVVType::getSameRatioLMUL(unsigned SEW, RISCVII::VLMUL VLMUL, unsigned EEW) { 210 unsigned Ratio = RISCVVType::getSEWLMULRatio(SEW, VLMUL); 211 unsigned EMULFixedPoint = (EEW * 8) / Ratio; 212 bool Fractional = EMULFixedPoint < 8; 213 unsigned EMUL = Fractional ? 8 / EMULFixedPoint : EMULFixedPoint / 8; 214 if (!isValidLMUL(EMUL, Fractional)) 215 return std::nullopt; 216 return RISCVVType::encodeLMUL(EMUL, Fractional); 217 } 218 219 // Include the auto-generated portion of the compress emitter. 220 #define GEN_UNCOMPRESS_INSTR 221 #define GEN_COMPRESS_INSTR 222 #include "RISCVGenCompressInstEmitter.inc" 223 224 bool RISCVRVC::compress(MCInst &OutInst, const MCInst &MI, 225 const MCSubtargetInfo &STI) { 226 return compressInst(OutInst, MI, STI); 227 } 228 229 bool RISCVRVC::uncompress(MCInst &OutInst, const MCInst &MI, 230 const MCSubtargetInfo &STI) { 231 return uncompressInst(OutInst, MI, STI); 232 } 233 234 // Lookup table for fli.s for entries 2-31. 235 static constexpr std::pair<uint8_t, uint8_t> LoadFP32ImmArr[] = { 236 {0b01101111, 0b00}, {0b01110000, 0b00}, {0b01110111, 0b00}, 237 {0b01111000, 0b00}, {0b01111011, 0b00}, {0b01111100, 0b00}, 238 {0b01111101, 0b00}, {0b01111101, 0b01}, {0b01111101, 0b10}, 239 {0b01111101, 0b11}, {0b01111110, 0b00}, {0b01111110, 0b01}, 240 {0b01111110, 0b10}, {0b01111110, 0b11}, {0b01111111, 0b00}, 241 {0b01111111, 0b01}, {0b01111111, 0b10}, {0b01111111, 0b11}, 242 {0b10000000, 0b00}, {0b10000000, 0b01}, {0b10000000, 0b10}, 243 {0b10000001, 0b00}, {0b10000010, 0b00}, {0b10000011, 0b00}, 244 {0b10000110, 0b00}, {0b10000111, 0b00}, {0b10001110, 0b00}, 245 {0b10001111, 0b00}, {0b11111111, 0b00}, {0b11111111, 0b10}, 246 }; 247 248 int RISCVLoadFPImm::getLoadFPImm(APFloat FPImm) { 249 assert((&FPImm.getSemantics() == &APFloat::IEEEsingle() || 250 &FPImm.getSemantics() == &APFloat::IEEEdouble() || 251 &FPImm.getSemantics() == &APFloat::IEEEhalf()) && 252 "Unexpected semantics"); 253 254 // Handle the minimum normalized value which is different for each type. 255 if (FPImm.isSmallestNormalized() && !FPImm.isNegative()) 256 return 1; 257 258 // Convert to single precision to use its lookup table. 259 bool LosesInfo; 260 APFloat::opStatus Status = FPImm.convert( 261 APFloat::IEEEsingle(), APFloat::rmNearestTiesToEven, &LosesInfo); 262 if (Status != APFloat::opOK || LosesInfo) 263 return -1; 264 265 APInt Imm = FPImm.bitcastToAPInt(); 266 267 if (Imm.extractBitsAsZExtValue(21, 0) != 0) 268 return -1; 269 270 bool Sign = Imm.extractBitsAsZExtValue(1, 31); 271 uint8_t Mantissa = Imm.extractBitsAsZExtValue(2, 21); 272 uint8_t Exp = Imm.extractBitsAsZExtValue(8, 23); 273 274 auto EMI = llvm::lower_bound(LoadFP32ImmArr, std::make_pair(Exp, Mantissa)); 275 if (EMI == std::end(LoadFP32ImmArr) || EMI->first != Exp || 276 EMI->second != Mantissa) 277 return -1; 278 279 // Table doesn't have entry 0 or 1. 280 int Entry = std::distance(std::begin(LoadFP32ImmArr), EMI) + 2; 281 282 // The only legal negative value is -1.0(entry 0). 1.0 is entry 16. 283 if (Sign) { 284 if (Entry == 16) 285 return 0; 286 return -1; 287 } 288 289 return Entry; 290 } 291 292 float RISCVLoadFPImm::getFPImm(unsigned Imm) { 293 assert(Imm != 1 && Imm != 30 && Imm != 31 && "Unsupported immediate"); 294 295 // Entry 0 is -1.0, the only negative value. Entry 16 is 1.0. 296 uint32_t Sign = 0; 297 if (Imm == 0) { 298 Sign = 0b1; 299 Imm = 16; 300 } 301 302 uint32_t Exp = LoadFP32ImmArr[Imm - 2].first; 303 uint32_t Mantissa = LoadFP32ImmArr[Imm - 2].second; 304 305 uint32_t I = Sign << 31 | Exp << 23 | Mantissa << 21; 306 return bit_cast<float>(I); 307 } 308 309 void RISCVZC::printRlist(unsigned SlistEncode, raw_ostream &OS) { 310 OS << "{ra"; 311 if (SlistEncode > 4) { 312 OS << ", s0"; 313 if (SlistEncode == 15) 314 OS << "-s11"; 315 else if (SlistEncode > 5 && SlistEncode <= 14) 316 OS << "-s" << (SlistEncode - 5); 317 } 318 OS << "}"; 319 } 320 321 void RISCVZC::printSpimm(int64_t Spimm, raw_ostream &OS) { OS << Spimm; } 322 323 } // namespace llvm 324