[llvm] 7d950f0 - [NFC][TLI] Replace std::lower_bound call in getLibFunc with DenseMap lookup
Dhruv Chawla via llvm-commits
llvm-commits at lists.llvm.org
Tue Aug 15 10:13:33 PDT 2023
Author: Dhruv Chawla
Date: 2023-08-15T22:37:18+05:30
New Revision: 7d950f040e3da66ec83f91b58d8c2220483d6335
URL: https://github.com/llvm/llvm-project/commit/7d950f040e3da66ec83f91b58d8c2220483d6335
DIFF: https://github.com/llvm/llvm-project/commit/7d950f040e3da66ec83f91b58d8c2220483d6335.diff
LOG: [NFC][TLI] Replace std::lower_bound call in getLibFunc with DenseMap lookup
While std::lower_bound takes logarithmic time (relative to the length of
the array) to execute, DenseMap gives better performance characteristics
as it traverses few (if any) elements when collisions do occur,
especially when the number of elements are known in advance.
This gives a speedup of 0.24%:
https://llvm-compile-time-tracker.com/compare.php?from=ac00cca3d9c6c3e9118ebbe47aa5b3ba1ee7404f&to=7f3d4c8ce8cee3a236a2328e46b2a8374672b46e&stat=instructions:u
Differential Revision: https://reviews.llvm.org/D157951
Added:
Modified:
llvm/lib/Analysis/TargetLibraryInfo.cpp
Removed:
################################################################################
diff --git a/llvm/lib/Analysis/TargetLibraryInfo.cpp b/llvm/lib/Analysis/TargetLibraryInfo.cpp
index bb9dbd0eb0e1b8..15ba6468a30708 100644
--- a/llvm/lib/Analysis/TargetLibraryInfo.cpp
+++ b/llvm/lib/Analysis/TargetLibraryInfo.cpp
@@ -11,6 +11,7 @@
//===----------------------------------------------------------------------===//
#include "llvm/Analysis/TargetLibraryInfo.h"
+#include "llvm/ADT/DenseMap.h"
#include "llvm/IR/Constants.h"
#include "llvm/InitializePasses.h"
#include "llvm/Support/CommandLine.h"
@@ -940,16 +941,26 @@ static StringRef sanitizeFunctionName(StringRef funcName) {
return GlobalValue::dropLLVMManglingEscape(funcName);
}
+static DenseMap<StringRef, LibFunc>
+buildIndexMap(ArrayRef<StringLiteral> StandardNames) {
+ DenseMap<StringRef, LibFunc> Indices;
+ unsigned Idx = 0;
+ Indices.reserve(LibFunc::NumLibFuncs);
+ for (const auto &Func : StandardNames)
+ Indices[Func] = static_cast<LibFunc>(Idx++);
+ return Indices;
+}
+
bool TargetLibraryInfoImpl::getLibFunc(StringRef funcName, LibFunc &F) const {
funcName = sanitizeFunctionName(funcName);
if (funcName.empty())
return false;
- const auto *Start = std::begin(StandardNames);
- const auto *End = std::end(StandardNames);
- const auto *I = std::lower_bound(Start, End, funcName);
- if (I != End && *I == funcName) {
- F = (LibFunc)(I - Start);
+ static const DenseMap<StringRef, LibFunc> Indices =
+ buildIndexMap(StandardNames);
+
+ if (auto Loc = Indices.find(funcName); Loc != Indices.end()) {
+ F = Loc->second;
return true;
}
return false;
More information about the llvm-commits
mailing list