[llvm] f2d16b3 - [RISCV] Use binary search on the extension tables in RISCVISAInfo.cpp. NFC
Craig Topper via llvm-commits
llvm-commits at lists.llvm.org
Fri Jun 23 17:23:35 PDT 2023
Author: Craig Topper
Date: 2023-06-23T17:23:12-07:00
New Revision: f2d16b368fcde4cc7e67fe50316eb070d82487f9
URL: https://github.com/llvm/llvm-project/commit/f2d16b368fcde4cc7e67fe50316eb070d82487f9
DIFF: https://github.com/llvm/llvm-project/commit/f2d16b368fcde4cc7e67fe50316eb070d82487f9.diff
LOG: [RISCV] Use binary search on the extension tables in RISCVISAInfo.cpp. NFC
After D153170 the tables are now sorted by extension name so we can use that to
avoid a linear search.
Reviewed By: asb, MaskRay
Differential Revision: https://reviews.llvm.org/D153598
Added:
Modified:
llvm/lib/Support/RISCVISAInfo.cpp
Removed:
################################################################################
diff --git a/llvm/lib/Support/RISCVISAInfo.cpp b/llvm/lib/Support/RISCVISAInfo.cpp
index 489b308b30d47..f9a38cf21efed 100644
--- a/llvm/lib/Support/RISCVISAInfo.cpp
+++ b/llvm/lib/Support/RISCVISAInfo.cpp
@@ -224,11 +224,12 @@ static size_t findLastNonVersionCharacter(StringRef Ext) {
}
namespace {
-struct FindByName {
- FindByName(StringRef Ext) : Ext(Ext){};
- StringRef Ext;
- bool operator()(const RISCVSupportedExtension &ExtInfo) {
- return ExtInfo.Name == Ext;
+struct LessExtName {
+ bool operator()(const RISCVSupportedExtension &LHS, StringRef RHS) {
+ return StringRef(LHS.Name) < RHS;
+ }
+ bool operator()(StringRef LHS, const RISCVSupportedExtension &RHS) {
+ return LHS < StringRef(RHS.Name);
}
};
} // namespace
@@ -239,12 +240,12 @@ findDefaultVersion(StringRef ExtName) {
// TODO: We might set default version based on profile or ISA spec.
for (auto &ExtInfo : {ArrayRef(SupportedExtensions),
ArrayRef(SupportedExperimentalExtensions)}) {
- auto ExtensionInfoIterator = llvm::find_if(ExtInfo, FindByName(ExtName));
+ auto I = llvm::lower_bound(ExtInfo, ExtName, LessExtName());
- if (ExtensionInfoIterator == ExtInfo.end()) {
+ if (I == ExtInfo.end() || I->Name != ExtName)
continue;
- }
- return ExtensionInfoIterator->Version;
+
+ return I->Version;
}
return std::nullopt;
}
@@ -279,37 +280,50 @@ static StringRef getExtensionType(StringRef Ext) {
static std::optional<RISCVExtensionVersion>
isExperimentalExtension(StringRef Ext) {
- auto ExtIterator =
- llvm::find_if(SupportedExperimentalExtensions, FindByName(Ext));
- if (ExtIterator == std::end(SupportedExperimentalExtensions))
+ auto I =
+ llvm::lower_bound(SupportedExperimentalExtensions, Ext, LessExtName());
+ if (I == std::end(SupportedExperimentalExtensions) || I->Name != Ext)
return std::nullopt;
- return ExtIterator->Version;
+ return I->Version;
}
bool RISCVISAInfo::isSupportedExtensionFeature(StringRef Ext) {
bool IsExperimental = stripExperimentalPrefix(Ext);
- if (IsExperimental)
- return llvm::any_of(SupportedExperimentalExtensions, FindByName(Ext));
- else
- return llvm::any_of(SupportedExtensions, FindByName(Ext));
+ ArrayRef<RISCVSupportedExtension> ExtInfo =
+ IsExperimental ? ArrayRef(SupportedExperimentalExtensions)
+ : ArrayRef(SupportedExtensions);
+
+ auto I = llvm::lower_bound(ExtInfo, Ext, LessExtName());
+ return I != ExtInfo.end() && I->Name == Ext;
}
bool RISCVISAInfo::isSupportedExtension(StringRef Ext) {
verifyTables();
- return llvm::any_of(SupportedExtensions, FindByName(Ext)) ||
- llvm::any_of(SupportedExperimentalExtensions, FindByName(Ext));
+
+ for (auto ExtInfo : {ArrayRef(SupportedExtensions),
+ ArrayRef(SupportedExperimentalExtensions)}) {
+ auto I = llvm::lower_bound(ExtInfo, Ext, LessExtName());
+ if (I != ExtInfo.end() && I->Name == Ext)
+ return true;
+ }
+
+ return false;
}
bool RISCVISAInfo::isSupportedExtension(StringRef Ext, unsigned MajorVersion,
unsigned MinorVersion) {
- auto FindByNameAndVersion = [=](const RISCVSupportedExtension &ExtInfo) {
- return ExtInfo.Name == Ext && (MajorVersion == ExtInfo.Version.Major) &&
- (MinorVersion == ExtInfo.Version.Minor);
- };
- return llvm::any_of(SupportedExtensions, FindByNameAndVersion) ||
- llvm::any_of(SupportedExperimentalExtensions, FindByNameAndVersion);
+ for (auto ExtInfo : {ArrayRef(SupportedExtensions),
+ ArrayRef(SupportedExperimentalExtensions)}) {
+ auto Range =
+ std::equal_range(ExtInfo.begin(), ExtInfo.end(), Ext, LessExtName());
+ for (auto I = Range.first, E = Range.second; I != E; ++I)
+ if (I->Version.Major == MajorVersion && I->Version.Minor == MinorVersion)
+ return true;
+ }
+
+ return false;
}
bool RISCVISAInfo::hasExtension(StringRef Ext) const {
@@ -549,11 +563,12 @@ RISCVISAInfo::parseFeatures(unsigned XLen,
? ArrayRef(SupportedExperimentalExtensions)
: ArrayRef(SupportedExtensions);
auto ExtensionInfoIterator =
- llvm::find_if(ExtensionInfos, FindByName(ExtName));
+ llvm::lower_bound(ExtensionInfos, ExtName, LessExtName());
// Not all features is related to ISA extension, like `relax` or
// `save-restore`, skip those feature.
- if (ExtensionInfoIterator == ExtensionInfos.end())
+ if (ExtensionInfoIterator == ExtensionInfos.end() ||
+ ExtensionInfoIterator->Name != ExtName)
continue;
if (Add)
More information about the llvm-commits
mailing list