1 //===-- RISCVISAUtils.cpp - RISC-V ISA Utilities --------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 // Utilities shared by TableGen and RISCVISAInfo.
11 //===----------------------------------------------------------------------===//
13 #include "llvm/Support/RISCVISAUtils.h"
14 #include "llvm/ADT/StringExtras.h"
19 // We rank extensions in the following order:
20 // -Single letter extensions in canonical order.
21 // -Unknown single letter extensions in alphabetical order.
22 // -Multi-letter extensions starting with 'z' sorted by canonical order of
23 // the second letter then sorted alphabetically.
24 // -Multi-letter extensions starting with 's' in alphabetical order.
25 // -(TODO) Multi-letter extensions starting with 'zxm' in alphabetical order.
26 // -X extensions in alphabetical order.
27 // -Unknown multi-letter extensions in alphabetical order.
28 // These flags are used to indicate the category. The first 6 bits store the
29 // single letter extension rank for single letter and multi-letter extensions
32 RF_Z_EXTENSION
= 1 << 6,
33 RF_S_EXTENSION
= 2 << 6,
34 RF_X_EXTENSION
= 3 << 6,
35 RF_UNKNOWN_MULTILETTER_EXTENSION
= 4 << 6,
38 // Get the rank for single-letter extension, lower value meaning higher
40 static unsigned singleLetterExtensionRank(char Ext
) {
49 size_t Pos
= RISCVISAUtils::AllStdExts
.find(Ext
);
50 if (Pos
!= StringRef::npos
)
51 return Pos
+ 2; // Skip 'e' and 'i' from above.
53 // If we got an unknown extension letter, then give it an alphabetical
54 // order, but after all known standard extensions.
55 return 2 + RISCVISAUtils::AllStdExts
.size() + (Ext
- 'a');
58 // Get the rank for multi-letter extension, lower value meaning higher
59 // priority/order in canonical order.
60 static unsigned getExtensionRank(const std::string
&ExtName
) {
61 assert(ExtName
.size() >= 1);
64 return RF_S_EXTENSION
;
66 assert(ExtName
.size() >= 2);
67 // `z` extension must be sorted by canonical order of second letter.
68 // e.g. zmx has higher rank than zax.
69 return RF_Z_EXTENSION
| singleLetterExtensionRank(ExtName
[1]);
71 return RF_X_EXTENSION
;
73 if (ExtName
.size() == 1)
74 return singleLetterExtensionRank(ExtName
[0]);
75 return RF_UNKNOWN_MULTILETTER_EXTENSION
;
79 // Compare function for extension.
80 // Only compare the extension name, ignore version comparison.
81 bool llvm::RISCVISAUtils::compareExtension(const std::string
&LHS
,
82 const std::string
&RHS
) {
83 unsigned LHSRank
= getExtensionRank(LHS
);
84 unsigned RHSRank
= getExtensionRank(RHS
);
86 // If the ranks differ, pick the lower rank.
87 if (LHSRank
!= RHSRank
)
88 return LHSRank
< RHSRank
;
90 // If the rank is same, it must be sorted by lexicographic order.