Skip to content

Commit 7d950f0

Browse files
committed
[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
1 parent 3946535 commit 7d950f0

File tree

1 file changed

+16
-5
lines changed

1 file changed

+16
-5
lines changed

llvm/lib/Analysis/TargetLibraryInfo.cpp

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
//===----------------------------------------------------------------------===//
1212

1313
#include "llvm/Analysis/TargetLibraryInfo.h"
14+
#include "llvm/ADT/DenseMap.h"
1415
#include "llvm/IR/Constants.h"
1516
#include "llvm/InitializePasses.h"
1617
#include "llvm/Support/CommandLine.h"
@@ -940,16 +941,26 @@ static StringRef sanitizeFunctionName(StringRef funcName) {
940941
return GlobalValue::dropLLVMManglingEscape(funcName);
941942
}
942943

944+
static DenseMap<StringRef, LibFunc>
945+
buildIndexMap(ArrayRef<StringLiteral> StandardNames) {
946+
DenseMap<StringRef, LibFunc> Indices;
947+
unsigned Idx = 0;
948+
Indices.reserve(LibFunc::NumLibFuncs);
949+
for (const auto &Func : StandardNames)
950+
Indices[Func] = static_cast<LibFunc>(Idx++);
951+
return Indices;
952+
}
953+
943954
bool TargetLibraryInfoImpl::getLibFunc(StringRef funcName, LibFunc &F) const {
944955
funcName = sanitizeFunctionName(funcName);
945956
if (funcName.empty())
946957
return false;
947958

948-
const auto *Start = std::begin(StandardNames);
949-
const auto *End = std::end(StandardNames);
950-
const auto *I = std::lower_bound(Start, End, funcName);
951-
if (I != End && *I == funcName) {
952-
F = (LibFunc)(I - Start);
959+
static const DenseMap<StringRef, LibFunc> Indices =
960+
buildIndexMap(StandardNames);
961+
962+
if (auto Loc = Indices.find(funcName); Loc != Indices.end()) {
963+
F = Loc->second;
953964
return true;
954965
}
955966
return false;

0 commit comments

Comments
 (0)