Path: blob/main/contrib/llvm-project/llvm/lib/Target/RISCV/MCTargetDesc/RISCVBaseInfo.cpp
35294 views
//===-- RISCVBaseInfo.cpp - Top level definitions for RISC-V MC -----------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//7//8// This file contains small standalone enum definitions for the RISC-V target9// useful for the compiler back-end and the MC libraries.10//11//===----------------------------------------------------------------------===//1213#include "RISCVBaseInfo.h"14#include "llvm/ADT/ArrayRef.h"15#include "llvm/MC/MCInst.h"16#include "llvm/MC/MCRegisterInfo.h"17#include "llvm/MC/MCSubtargetInfo.h"18#include "llvm/Support/raw_ostream.h"19#include "llvm/TargetParser/TargetParser.h"20#include "llvm/TargetParser/Triple.h"2122namespace llvm {2324extern const SubtargetFeatureKV RISCVFeatureKV[RISCV::NumSubtargetFeatures];2526namespace RISCVSysReg {27#define GET_SysRegsList_IMPL28#include "RISCVGenSearchableTables.inc"29} // namespace RISCVSysReg3031namespace RISCVInsnOpcode {32#define GET_RISCVOpcodesList_IMPL33#include "RISCVGenSearchableTables.inc"34} // namespace RISCVInsnOpcode3536namespace RISCVABI {37ABI computeTargetABI(const Triple &TT, const FeatureBitset &FeatureBits,38StringRef ABIName) {39auto TargetABI = getTargetABI(ABIName);40bool IsRV64 = TT.isArch64Bit();41bool IsRVE = FeatureBits[RISCV::FeatureStdExtE];4243if (!ABIName.empty() && TargetABI == ABI_Unknown) {44errs()45<< "'" << ABIName46<< "' is not a recognized ABI for this target (ignoring target-abi)\n";47} else if (ABIName.starts_with("ilp32") && IsRV64) {48errs() << "32-bit ABIs are not supported for 64-bit targets (ignoring "49"target-abi)\n";50TargetABI = ABI_Unknown;51} else if (ABIName.starts_with("lp64") && !IsRV64) {52errs() << "64-bit ABIs are not supported for 32-bit targets (ignoring "53"target-abi)\n";54TargetABI = ABI_Unknown;55} else if (!IsRV64 && IsRVE && TargetABI != ABI_ILP32E &&56TargetABI != ABI_Unknown) {57// TODO: move this checking to RISCVTargetLowering and RISCVAsmParser58errs()59<< "Only the ilp32e ABI is supported for RV32E (ignoring target-abi)\n";60TargetABI = ABI_Unknown;61} else if (IsRV64 && IsRVE && TargetABI != ABI_LP64E &&62TargetABI != ABI_Unknown) {63// TODO: move this checking to RISCVTargetLowering and RISCVAsmParser64errs()65<< "Only the lp64e ABI is supported for RV64E (ignoring target-abi)\n";66TargetABI = ABI_Unknown;67}6869if ((TargetABI == RISCVABI::ABI::ABI_ILP32E ||70(TargetABI == ABI_Unknown && IsRVE && !IsRV64)) &&71FeatureBits[RISCV::FeatureStdExtD])72report_fatal_error("ILP32E cannot be used with the D ISA extension");7374if (TargetABI != ABI_Unknown)75return TargetABI;7677// If no explicit ABI is given, try to compute the default ABI.78auto ISAInfo = RISCVFeatures::parseFeatureBits(IsRV64, FeatureBits);79if (!ISAInfo)80report_fatal_error(ISAInfo.takeError());81return getTargetABI((*ISAInfo)->computeDefaultABI());82}8384ABI getTargetABI(StringRef ABIName) {85auto TargetABI = StringSwitch<ABI>(ABIName)86.Case("ilp32", ABI_ILP32)87.Case("ilp32f", ABI_ILP32F)88.Case("ilp32d", ABI_ILP32D)89.Case("ilp32e", ABI_ILP32E)90.Case("lp64", ABI_LP64)91.Case("lp64f", ABI_LP64F)92.Case("lp64d", ABI_LP64D)93.Case("lp64e", ABI_LP64E)94.Default(ABI_Unknown);95return TargetABI;96}9798// To avoid the BP value clobbered by a function call, we need to choose a99// callee saved register to save the value. RV32E only has X8 and X9 as callee100// saved registers and X8 will be used as fp. So we choose X9 as bp.101MCRegister getBPReg() { return RISCV::X9; }102103// Returns the register holding shadow call stack pointer.104MCRegister getSCSPReg() { return RISCV::X3; }105106} // namespace RISCVABI107108namespace RISCVFeatures {109110void validate(const Triple &TT, const FeatureBitset &FeatureBits) {111if (TT.isArch64Bit() && !FeatureBits[RISCV::Feature64Bit])112report_fatal_error("RV64 target requires an RV64 CPU");113if (!TT.isArch64Bit() && !FeatureBits[RISCV::Feature32Bit])114report_fatal_error("RV32 target requires an RV32 CPU");115if (FeatureBits[RISCV::Feature32Bit] &&116FeatureBits[RISCV::Feature64Bit])117report_fatal_error("RV32 and RV64 can't be combined");118}119120llvm::Expected<std::unique_ptr<RISCVISAInfo>>121parseFeatureBits(bool IsRV64, const FeatureBitset &FeatureBits) {122unsigned XLen = IsRV64 ? 64 : 32;123std::vector<std::string> FeatureVector;124// Convert FeatureBitset to FeatureVector.125for (auto Feature : RISCVFeatureKV) {126if (FeatureBits[Feature.Value] &&127llvm::RISCVISAInfo::isSupportedExtensionFeature(Feature.Key))128FeatureVector.push_back(std::string("+") + Feature.Key);129}130return llvm::RISCVISAInfo::parseFeatures(XLen, FeatureVector);131}132133} // namespace RISCVFeatures134135// Include the auto-generated portion of the compress emitter.136#define GEN_UNCOMPRESS_INSTR137#define GEN_COMPRESS_INSTR138#include "RISCVGenCompressInstEmitter.inc"139140bool RISCVRVC::compress(MCInst &OutInst, const MCInst &MI,141const MCSubtargetInfo &STI) {142return compressInst(OutInst, MI, STI);143}144145bool RISCVRVC::uncompress(MCInst &OutInst, const MCInst &MI,146const MCSubtargetInfo &STI) {147return uncompressInst(OutInst, MI, STI);148}149150// Lookup table for fli.s for entries 2-31.151static constexpr std::pair<uint8_t, uint8_t> LoadFP32ImmArr[] = {152{0b01101111, 0b00}, {0b01110000, 0b00}, {0b01110111, 0b00},153{0b01111000, 0b00}, {0b01111011, 0b00}, {0b01111100, 0b00},154{0b01111101, 0b00}, {0b01111101, 0b01}, {0b01111101, 0b10},155{0b01111101, 0b11}, {0b01111110, 0b00}, {0b01111110, 0b01},156{0b01111110, 0b10}, {0b01111110, 0b11}, {0b01111111, 0b00},157{0b01111111, 0b01}, {0b01111111, 0b10}, {0b01111111, 0b11},158{0b10000000, 0b00}, {0b10000000, 0b01}, {0b10000000, 0b10},159{0b10000001, 0b00}, {0b10000010, 0b00}, {0b10000011, 0b00},160{0b10000110, 0b00}, {0b10000111, 0b00}, {0b10001110, 0b00},161{0b10001111, 0b00}, {0b11111111, 0b00}, {0b11111111, 0b10},162};163164int RISCVLoadFPImm::getLoadFPImm(APFloat FPImm) {165assert((&FPImm.getSemantics() == &APFloat::IEEEsingle() ||166&FPImm.getSemantics() == &APFloat::IEEEdouble() ||167&FPImm.getSemantics() == &APFloat::IEEEhalf()) &&168"Unexpected semantics");169170// Handle the minimum normalized value which is different for each type.171if (FPImm.isSmallestNormalized() && !FPImm.isNegative())172return 1;173174// Convert to single precision to use its lookup table.175bool LosesInfo;176APFloat::opStatus Status = FPImm.convert(177APFloat::IEEEsingle(), APFloat::rmNearestTiesToEven, &LosesInfo);178if (Status != APFloat::opOK || LosesInfo)179return -1;180181APInt Imm = FPImm.bitcastToAPInt();182183if (Imm.extractBitsAsZExtValue(21, 0) != 0)184return -1;185186bool Sign = Imm.extractBitsAsZExtValue(1, 31);187uint8_t Mantissa = Imm.extractBitsAsZExtValue(2, 21);188uint8_t Exp = Imm.extractBitsAsZExtValue(8, 23);189190auto EMI = llvm::lower_bound(LoadFP32ImmArr, std::make_pair(Exp, Mantissa));191if (EMI == std::end(LoadFP32ImmArr) || EMI->first != Exp ||192EMI->second != Mantissa)193return -1;194195// Table doesn't have entry 0 or 1.196int Entry = std::distance(std::begin(LoadFP32ImmArr), EMI) + 2;197198// The only legal negative value is -1.0(entry 0). 1.0 is entry 16.199if (Sign) {200if (Entry == 16)201return 0;202return -1;203}204205return Entry;206}207208float RISCVLoadFPImm::getFPImm(unsigned Imm) {209assert(Imm != 1 && Imm != 30 && Imm != 31 && "Unsupported immediate");210211// Entry 0 is -1.0, the only negative value. Entry 16 is 1.0.212uint32_t Sign = 0;213if (Imm == 0) {214Sign = 0b1;215Imm = 16;216}217218uint32_t Exp = LoadFP32ImmArr[Imm - 2].first;219uint32_t Mantissa = LoadFP32ImmArr[Imm - 2].second;220221uint32_t I = Sign << 31 | Exp << 23 | Mantissa << 21;222return bit_cast<float>(I);223}224225void RISCVZC::printRlist(unsigned SlistEncode, raw_ostream &OS) {226OS << "{ra";227if (SlistEncode > 4) {228OS << ", s0";229if (SlistEncode == 15)230OS << "-s11";231else if (SlistEncode > 5 && SlistEncode <= 14)232OS << "-s" << (SlistEncode - 5);233}234OS << "}";235}236237} // namespace llvm238239240