1 //===-- RISCVISAInfo.h - RISC-V ISA Information -----------------*- C++ -*-===// 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 #ifndef LLVM_SUPPORT_RISCVISAINFO_H 10 #define LLVM_SUPPORT_RISCVISAINFO_H 11 12 #include "llvm/ADT/StringMap.h" 13 #include "llvm/ADT/StringRef.h" 14 #include "llvm/Support/Compiler.h" 15 #include "llvm/Support/Error.h" 16 #include "llvm/Support/RISCVISAUtils.h" 17 18 #include <map> 19 #include <set> 20 #include <string> 21 #include <vector> 22 23 namespace llvm { 24 25 class RISCVISAInfo { 26 public: 27 RISCVISAInfo(const RISCVISAInfo &) = delete; 28 RISCVISAInfo &operator=(const RISCVISAInfo &) = delete; 29 30 /// Parse RISC-V ISA info from arch string. 31 /// If IgnoreUnknown is set, any unrecognised extension names or 32 /// extensions with unrecognised versions will be silently dropped, except 33 /// for the special case of the base 'i' and 'e' extensions, where the 34 /// default version will be used (as ignoring the base is not possible). 35 LLVM_ABI static llvm::Expected<std::unique_ptr<RISCVISAInfo>> 36 parseArchString(StringRef Arch, bool EnableExperimentalExtension, 37 bool ExperimentalExtensionVersionCheck = true); 38 39 /// Parse RISC-V ISA info from an arch string that is already in normalized 40 /// form (as defined in the psABI). Unlike parseArchString, this function 41 /// will not error for unrecognized extension names or extension versions. 42 LLVM_ABI static llvm::Expected<std::unique_ptr<RISCVISAInfo>> 43 parseNormalizedArchString(StringRef Arch); 44 45 /// Parse RISC-V ISA info from feature vector. 46 LLVM_ABI static llvm::Expected<std::unique_ptr<RISCVISAInfo>> 47 parseFeatures(unsigned XLen, const std::vector<std::string> &Features); 48 49 LLVM_ABI static llvm::Expected<std::unique_ptr<RISCVISAInfo>> 50 createFromExtMap(unsigned XLen, 51 const RISCVISAUtils::OrderedExtensionMap &Exts); 52 53 /// Convert RISC-V ISA info to a feature vector. 54 LLVM_ABI std::vector<std::string> toFeatures(bool AddAllExtensions = false, 55 bool IgnoreUnknown = true) const; 56 getExtensions()57 const RISCVISAUtils::OrderedExtensionMap &getExtensions() const { 58 return Exts; 59 } 60 getXLen()61 unsigned getXLen() const { return XLen; } getFLen()62 unsigned getFLen() const { return FLen; } getMinVLen()63 unsigned getMinVLen() const { return MinVLen; } getMaxVLen()64 unsigned getMaxVLen() const { return 65536; } getMaxELen()65 unsigned getMaxELen() const { return MaxELen; } getMaxELenFp()66 unsigned getMaxELenFp() const { return MaxELenFp; } 67 68 LLVM_ABI bool hasExtension(StringRef Ext) const; 69 LLVM_ABI std::string toString() const; 70 LLVM_ABI StringRef computeDefaultABI() const; 71 72 LLVM_ABI static bool isSupportedExtensionFeature(StringRef Ext); 73 LLVM_ABI static bool isSupportedExtension(StringRef Ext); 74 LLVM_ABI static bool isSupportedExtensionWithVersion(StringRef Ext); 75 LLVM_ABI static bool isSupportedExtension(StringRef Ext, 76 unsigned MajorVersion, 77 unsigned MinorVersion); 78 LLVM_ABI static std::string getTargetFeatureForExtension(StringRef Ext); 79 80 LLVM_ABI static void printSupportedExtensions(StringMap<StringRef> &DescMap); 81 LLVM_ABI static void 82 printEnabledExtensions(bool IsRV64, std::set<StringRef> &EnabledFeatureNames, 83 StringMap<StringRef> &DescMap); 84 85 /// Return the group id and bit position of __riscv_feature_bits. Returns 86 /// <-1, -1> if not supported. 87 LLVM_ABI static std::pair<int, int> getRISCVFeaturesBitsInfo(StringRef Ext); 88 89 // The maximum value of the group ID obtained from getRISCVFeaturesBitsInfo. 90 static constexpr unsigned FeatureBitSize = 2; 91 92 private: RISCVISAInfo(unsigned XLen)93 RISCVISAInfo(unsigned XLen) : XLen(XLen) {} 94 95 unsigned XLen; 96 unsigned FLen = 0; 97 unsigned MinVLen = 0; 98 unsigned MaxELen = 0, MaxELenFp = 0; 99 100 RISCVISAUtils::OrderedExtensionMap Exts; 101 102 Error checkDependency(); 103 104 void updateImplication(); 105 void updateCombination(); 106 107 /// Update FLen, MinVLen, MaxELen, and MaxELenFp. 108 void updateImpliedLengths(); 109 110 static llvm::Expected<std::unique_ptr<RISCVISAInfo>> 111 postProcessAndChecking(std::unique_ptr<RISCVISAInfo> &&ISAInfo); 112 }; 113 114 } // namespace llvm 115 116 #endif 117