[llvm] 37f3e53 - [RISCV] Simplify RISCVISAInfo::compareExtension. NFCI
Craig Topper via llvm-commits
llvm-commits at lists.llvm.org
Mon Mar 20 22:26:02 PDT 2023
Author: Craig Topper
Date: 2023-03-20T22:25:11-07:00
New Revision: 37f3e53c5be9f61c75394ce0558f54e67647de00
URL: https://github.com/llvm/llvm-project/commit/37f3e53c5be9f61c75394ce0558f54e67647de00
DIFF: https://github.com/llvm/llvm-project/commit/37f3e53c5be9f61c75394ce0558f54e67647de00.diff
LOG: [RISCV] Simplify RISCVISAInfo::compareExtension. NFCI
Instead of having a separate single letter and multiletter ranking
use a unified rank that assigns multiletter a larger value than
single letter.
Once we've ranked the extensions, then we compare using these ranks.
Reviewed By: kito-cheng
Differential Revision: https://reviews.llvm.org/D146273
Added:
Modified:
llvm/lib/Support/RISCVISAInfo.cpp
Removed:
################################################################################
diff --git a/llvm/lib/Support/RISCVISAInfo.cpp b/llvm/lib/Support/RISCVISAInfo.cpp
index 2ef3ffa58079e..603b1f3d64737 100644
--- a/llvm/lib/Support/RISCVISAInfo.cpp
+++ b/llvm/lib/Support/RISCVISAInfo.cpp
@@ -266,78 +266,70 @@ bool RISCVISAInfo::hasExtension(StringRef Ext) const {
return Exts.count(Ext.str()) != 0;
}
+// We rank extensions in the following order:
+// -Single letter extensions in canonical order.
+// -Unknown single letter extensions in alphabetical order.
+// -Multi-letter extensions starting with 's' in alphabetical order.
+// -Multi-letter extensions starting with 'z' sorted by canonical order of
+// the second letter then sorted alphabetically.
+// -X extensions in alphabetical order.
+// These flags are used to indicate the category. The first 6 bits store the
+// single letter extension rank for single letter and multi-letter extensions
+// starting with 'z'.
+enum RankFlags {
+ RF_S_EXTENSION = 1 << 6,
+ RF_Z_EXTENSION = 1 << 7,
+ RF_X_EXTENSION = 1 << 8,
+};
+
// Get the rank for single-letter extension, lower value meaning higher
// priority.
-static int singleLetterExtensionRank(char Ext) {
+static unsigned singleLetterExtensionRank(char Ext) {
+ assert(Ext >= 'a' && Ext <= 'z');
switch (Ext) {
case 'i':
- return -2;
+ return 0;
case 'e':
- return -1;
- default:
- break;
+ return 1;
}
size_t Pos = AllStdExts.find(Ext);
- int Rank;
- if (Pos == StringRef::npos)
- // If we got an unknown extension letter, then give it an alphabetical
- // order, but after all known standard extensions.
- Rank = AllStdExts.size() + (Ext - 'a');
- else
- Rank = Pos;
+ if (Pos != StringRef::npos)
+ return Pos + 2; // Skip 'e' and 'i' from above.
- return Rank;
+ // If we got an unknown extension letter, then give it an alphabetical
+ // order, but after all known standard extensions.
+ return 2 + AllStdExts.size() + (Ext - 'a');
}
// Get the rank for multi-letter extension, lower value meaning higher
// priority/order in canonical order.
-static int multiLetterExtensionRank(const std::string &ExtName) {
- assert(ExtName.length() >= 2);
- int HighOrder;
- int LowOrder = 0;
- // The order between multi-char extensions: s -> h -> z -> x.
- char ExtClass = ExtName[0];
- switch (ExtClass) {
+static unsigned getExtensionRank(const std::string &ExtName) {
+ assert(ExtName.size() >= 1);
+ switch (ExtName[0]) {
case 's':
- HighOrder = 0;
- break;
+ return RF_S_EXTENSION;
case 'z':
- HighOrder = 1;
+ assert(ExtName.size() >= 2);
// `z` extension must be sorted by canonical order of second letter.
// e.g. zmx has higher rank than zax.
- LowOrder = singleLetterExtensionRank(ExtName[1]);
- break;
+ return RF_Z_EXTENSION | singleLetterExtensionRank(ExtName[1]);
case 'x':
- HighOrder = 2;
- break;
+ return RF_X_EXTENSION;
default:
- llvm_unreachable("Unknown prefix for multi-char extension");
- return -1;
+ assert(ExtName.size() == 1);
+ return singleLetterExtensionRank(ExtName[0]);
}
-
- return (HighOrder << 8) + LowOrder;
}
// Compare function for extension.
// Only compare the extension name, ignore version comparison.
bool RISCVISAInfo::compareExtension(const std::string &LHS,
const std::string &RHS) {
- size_t LHSLen = LHS.length();
- size_t RHSLen = RHS.length();
- if (LHSLen == 1 && RHSLen != 1)
- return true;
-
- if (LHSLen != 1 && RHSLen == 1)
- return false;
-
- if (LHSLen == 1 && RHSLen == 1)
- return singleLetterExtensionRank(LHS[0]) <
- singleLetterExtensionRank(RHS[0]);
+ unsigned LHSRank = getExtensionRank(LHS);
+ unsigned RHSRank = getExtensionRank(RHS);
- // Both are multi-char ext here.
- int LHSRank = multiLetterExtensionRank(LHS);
- int RHSRank = multiLetterExtensionRank(RHS);
+ // If the ranks
diff er, pick the lower rank.
if (LHSRank != RHSRank)
return LHSRank < RHSRank;
More information about the llvm-commits
mailing list