diff --git a/llvm/lib/Analysis/TargetLibraryInfo.cpp b/llvm/lib/Analysis/TargetLibraryInfo.cpp --- 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 @@ return GlobalValue::dropLLVMManglingEscape(funcName); } +static DenseMap +buildIndexMap(ArrayRef StandardNames) { + DenseMap Indices; + unsigned Idx = 0; + Indices.reserve(LibFunc::NumLibFuncs); + for (const auto &Func : StandardNames) + Indices[Func] = static_cast(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 Indices = + buildIndexMap(StandardNames); + + if (auto Loc = Indices.find(funcName); Loc != Indices.end()) { + F = Loc->second; return true; } return false;