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