From: Fangrui Song Date: Sun, 30 Jun 2019 11:19:56 +0000 (+0000) Subject: Cleanup: llvm::bsearch -> llvm::partition_point after r364719 X-Git-Tag: android-x86-9.0-r1~1098 X-Git-Url: http://git.osdn.net/view?a=commitdiff_plain;ds=sidebyside;h=3228596b4caa43c55d9d8d57dc604004cf04783a;p=android-x86%2Fexternal-llvm.git Cleanup: llvm::bsearch -> llvm::partition_point after r364719 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@364720 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/include/llvm/ADT/STLExtras.h b/include/llvm/ADT/STLExtras.h index 4ce2e9fb72a..81dce0168c7 100644 --- a/include/llvm/ADT/STLExtras.h +++ b/include/llvm/ADT/STLExtras.h @@ -1322,13 +1322,12 @@ void stable_sort(R &&Range, Compare C) { std::stable_sort(adl_begin(Range), adl_end(Range), C); } -/// Binary search for the first iterator in a range where a predicate is true. -/// Requires that C is always false below some limit, and always true above it. +/// Binary search for the first iterator in a range where a predicate is false. +/// Requires that C is always true below some limit, and always false above it. template ()))> -auto bsearch(R &&Range, Predicate P) -> decltype(adl_begin(Range)) { - return std::partition_point(adl_begin(Range), adl_end(Range), - [&](const Val &V) { return !P(V); }); +auto partition_point(R &&Range, Predicate P) -> decltype(adl_begin(Range)) { + return std::partition_point(adl_begin(Range), adl_end(Range), P); } /// Wrapper function around std::equal to detect if all elements diff --git a/include/llvm/DebugInfo/DWARF/DWARFUnit.h b/include/llvm/DebugInfo/DWARF/DWARFUnit.h index f01b6ac0388..f9f90db3189 100644 --- a/include/llvm/DebugInfo/DWARF/DWARFUnit.h +++ b/include/llvm/DebugInfo/DWARF/DWARFUnit.h @@ -473,9 +473,10 @@ public: DWARFDie getDIEForOffset(uint32_t Offset) { extractDIEsIfNeeded(false); assert(!DieArray.empty()); - auto It = llvm::bsearch(DieArray, [=](const DWARFDebugInfoEntry &LHS) { - return Offset <= LHS.getOffset(); - }); + auto It = + llvm::partition_point(DieArray, [=](const DWARFDebugInfoEntry &DIE) { + return DIE.getOffset() < Offset; + }); if (It != DieArray.end() && It->getOffset() == Offset) return DWARFDie(this, &*It); return DWARFDie(); diff --git a/lib/Analysis/ProfileSummaryInfo.cpp b/lib/Analysis/ProfileSummaryInfo.cpp index 52e015b5bb3..dce19d6d546 100644 --- a/lib/Analysis/ProfileSummaryInfo.cpp +++ b/lib/Analysis/ProfileSummaryInfo.cpp @@ -60,8 +60,8 @@ static cl::opt ProfileSummaryColdCount( // Find the summary entry for a desired percentile of counts. static const ProfileSummaryEntry &getEntryForPercentile(SummaryEntryVector &DS, uint64_t Percentile) { - auto It = llvm::bsearch(DS, [=](const ProfileSummaryEntry &Entry) { - return Percentile <= Entry.Cutoff; + auto It = partition_point(DS, [=](const ProfileSummaryEntry &Entry) { + return Entry.Cutoff < Percentile; }); // The required percentile has to be <= one of the percentiles in the // detailed summary. diff --git a/lib/CodeGen/ExecutionDomainFix.cpp b/lib/CodeGen/ExecutionDomainFix.cpp index c350ede635d..a2dd5eee33b 100644 --- a/lib/CodeGen/ExecutionDomainFix.cpp +++ b/lib/CodeGen/ExecutionDomainFix.cpp @@ -337,8 +337,8 @@ void ExecutionDomainFix::visitSoftInstr(MachineInstr *mi, unsigned mask) { // Sorted insertion. // Enables giving priority to the latest domains during merging. const int Def = RDA->getReachingDef(mi, RC->getRegister(rx)); - auto I = llvm::bsearch(Regs, [&](int I) { - return Def < RDA->getReachingDef(mi, RC->getRegister(I)); + auto I = partition_point(Regs, [&](int I) { + return RDA->getReachingDef(mi, RC->getRegister(I)) <= Def; }); Regs.insert(I, rx); } diff --git a/lib/CodeGen/GlobalISel/LegalizerInfo.cpp b/lib/CodeGen/GlobalISel/LegalizerInfo.cpp index 0010ca72d76..14df53b9e84 100644 --- a/lib/CodeGen/GlobalISel/LegalizerInfo.cpp +++ b/lib/CodeGen/GlobalISel/LegalizerInfo.cpp @@ -544,11 +544,10 @@ LegalizerInfo::findAction(const SizeAndActionsVec &Vec, const uint32_t Size) { // Find the last element in Vec that has a bitsize equal to or smaller than // the requested bit size. // That is the element just before the first element that is bigger than Size. - auto VecIt = llvm::bsearch( - Vec, [=](const SizeAndAction &A) { return Size < A.first; }); - assert(VecIt != Vec.begin() && "Does Vec not start with size 1?"); - --VecIt; - int VecIdx = VecIt - Vec.begin(); + auto It = partition_point( + Vec, [=](const SizeAndAction &A) { return A.first <= Size; }); + assert(It != Vec.begin() && "Does Vec not start with size 1?"); + int VecIdx = It - Vec.begin() - 1; LegalizeAction Action = Vec[VecIdx].second; switch (Action) { diff --git a/lib/DebugInfo/DWARF/DWARFDebugAranges.cpp b/lib/DebugInfo/DWARF/DWARFDebugAranges.cpp index 010b0106a83..6460c9feeab 100644 --- a/lib/DebugInfo/DWARF/DWARFDebugAranges.cpp +++ b/lib/DebugInfo/DWARF/DWARFDebugAranges.cpp @@ -115,7 +115,7 @@ void DWARFDebugAranges::construct() { uint32_t DWARFDebugAranges::findAddress(uint64_t Address) const { RangeCollIterator It = - llvm::bsearch(Aranges, [=](Range RHS) { return Address < RHS.HighPC(); }); + partition_point(Aranges, [=](Range R) { return R.HighPC() <= Address; }); if (It != Aranges.end() && It->LowPC <= Address) return It->CUOffset; return -1U; diff --git a/lib/DebugInfo/DWARF/DWARFDebugFrame.cpp b/lib/DebugInfo/DWARF/DWARFDebugFrame.cpp index 65a6b57ddb5..b3f23366f2a 100644 --- a/lib/DebugInfo/DWARF/DWARFDebugFrame.cpp +++ b/lib/DebugInfo/DWARF/DWARFDebugFrame.cpp @@ -532,8 +532,8 @@ void DWARFDebugFrame::parse(DWARFDataExtractor Data) { } FrameEntry *DWARFDebugFrame::getEntryAtOffset(uint64_t Offset) const { - auto It = llvm::bsearch(Entries, [=](const std::unique_ptr &E) { - return Offset <= E->getOffset(); + auto It = partition_point(Entries, [=](const std::unique_ptr &E) { + return E->getOffset() < Offset; }); if (It != Entries.end() && (*It)->getOffset() == Offset) return It->get(); diff --git a/lib/DebugInfo/DWARF/DWARFDebugLoc.cpp b/lib/DebugInfo/DWARF/DWARFDebugLoc.cpp index e0d62215d9b..6d8f4bee77c 100644 --- a/lib/DebugInfo/DWARF/DWARFDebugLoc.cpp +++ b/lib/DebugInfo/DWARF/DWARFDebugLoc.cpp @@ -57,8 +57,8 @@ void DWARFDebugLoc::LocationList::dump(raw_ostream &OS, bool IsLittleEndian, DWARFDebugLoc::LocationList const * DWARFDebugLoc::getLocationListAtOffset(uint64_t Offset) const { - auto It = llvm::bsearch( - Locations, [=](const LocationList &L) { return Offset <= L.Offset; }); + auto It = partition_point( + Locations, [=](const LocationList &L) { return L.Offset < Offset; }); if (It != Locations.end() && It->Offset == Offset) return &(*It); return nullptr; @@ -212,8 +212,8 @@ void DWARFDebugLoclists::parse(DataExtractor data, unsigned Version) { DWARFDebugLoclists::LocationList const * DWARFDebugLoclists::getLocationListAtOffset(uint64_t Offset) const { - auto It = llvm::bsearch( - Locations, [=](const LocationList &L) { return Offset <= L.Offset; }); + auto It = partition_point( + Locations, [=](const LocationList &L) { return L.Offset < Offset; }); if (It != Locations.end() && It->Offset == Offset) return &(*It); return nullptr; diff --git a/lib/DebugInfo/DWARF/DWARFUnitIndex.cpp b/lib/DebugInfo/DWARF/DWARFUnitIndex.cpp index cff5c288d06..047c63461cc 100644 --- a/lib/DebugInfo/DWARF/DWARFUnitIndex.cpp +++ b/lib/DebugInfo/DWARF/DWARFUnitIndex.cpp @@ -172,8 +172,8 @@ DWARFUnitIndex::getFromOffset(uint32_t Offset) const { E2->Contributions[InfoColumn].Offset; }); } - auto I = llvm::bsearch(OffsetLookup, [&](Entry *E2) { - return Offset < E2->Contributions[InfoColumn].Offset; + auto I = partition_point(OffsetLookup, [&](Entry *E2) { + return E2->Contributions[InfoColumn].Offset <= Offset; }); if (I == OffsetLookup.begin()) return nullptr; diff --git a/lib/IR/DataLayout.cpp b/lib/IR/DataLayout.cpp index b8c130a54e9..6e0ebbd4a73 100644 --- a/lib/IR/DataLayout.cpp +++ b/lib/IR/DataLayout.cpp @@ -463,8 +463,8 @@ DataLayout::AlignmentsTy::iterator DataLayout::findAlignmentLowerBound(AlignTypeEnum AlignType, uint32_t BitWidth) { auto Pair = std::make_pair((unsigned)AlignType, BitWidth); - return llvm::bsearch(Alignments, [=](const LayoutAlignElem &E) { - return Pair <= std::make_pair(E.AlignType, E.TypeBitWidth); + return partition_point(Alignments, [=](const LayoutAlignElem &E) { + return std::make_pair(E.AlignType, E.TypeBitWidth) < Pair; }); } diff --git a/lib/IR/Function.cpp b/lib/IR/Function.cpp index 6a276b18023..dc28d22548d 100644 --- a/lib/IR/Function.cpp +++ b/lib/IR/Function.cpp @@ -533,9 +533,8 @@ static ArrayRef findTargetSubtable(StringRef Name) { // Drop "llvm." and take the first dotted component. That will be the target // if this is target specific. StringRef Target = Name.drop_front(5).split('.').first; - auto It = llvm::bsearch(Targets, [=](const IntrinsicTargetInfo &TI) { - return Target <= TI.Name; - }); + auto It = partition_point( + Targets, [=](const IntrinsicTargetInfo &TI) { return TI.Name < Target; }); // We've either found the target or just fall back to the generic set, which // is always first. const auto &TI = It != Targets.end() && It->Name == Target ? *It : Targets[0]; diff --git a/lib/ProfileData/InstrProf.cpp b/lib/ProfileData/InstrProf.cpp index c0b067a490e..510fd9887d9 100644 --- a/lib/ProfileData/InstrProf.cpp +++ b/lib/ProfileData/InstrProf.cpp @@ -363,16 +363,15 @@ Error InstrProfSymtab::create(Module &M, bool InLTO) { uint64_t InstrProfSymtab::getFunctionHashFromAddress(uint64_t Address) { finalizeSymtab(); - auto Result = - llvm::bsearch(AddrToMD5Map, [=](std::pair A) { - return Address <= A.first; - }); + auto It = partition_point(AddrToMD5Map, [=](std::pair A) { + return A.first < Address; + }); // Raw function pointer collected by value profiler may be from // external functions that are not instrumented. They won't have // mapping data to be used by the deserializer. Force the value to // be 0 in this case. - if (Result != AddrToMD5Map.end() && Result->first == Address) - return (uint64_t)Result->second; + if (It != AddrToMD5Map.end() && It->first == Address) + return (uint64_t)It->second; return 0; } diff --git a/lib/Target/X86/X86InstrFMA3Info.cpp b/lib/Target/X86/X86InstrFMA3Info.cpp index 77dc75386fc..25bbdddb7a2 100644 --- a/lib/Target/X86/X86InstrFMA3Info.cpp +++ b/lib/Target/X86/X86InstrFMA3Info.cpp @@ -158,8 +158,8 @@ const X86InstrFMA3Group *llvm::getFMA3Group(unsigned Opcode, uint64_t TSFlags) { // FMA 231 instructions have an opcode of 0xB6-0xBF unsigned FormIndex = ((BaseOpcode - 0x90) >> 4) & 0x3; - auto I = llvm::bsearch(Table, [=](const X86InstrFMA3Group &Group) { - return Opcode <= Group.Opcodes[FormIndex]; + auto I = partition_point(Table, [=](const X86InstrFMA3Group &Group) { + return Group.Opcodes[FormIndex] < Opcode; }); assert(I != Table.end() && I->Opcodes[FormIndex] == Opcode && "Couldn't find FMA3 opcode!"); diff --git a/lib/TextAPI/MachO/InterfaceFile.cpp b/lib/TextAPI/MachO/InterfaceFile.cpp index f1851014e70..54ba8cc3126 100644 --- a/lib/TextAPI/MachO/InterfaceFile.cpp +++ b/lib/TextAPI/MachO/InterfaceFile.cpp @@ -14,17 +14,15 @@ #include #include -using namespace llvm::MachO; - namespace llvm { namespace MachO { namespace detail { template typename C::iterator addEntry(C &Container, StringRef InstallName) { - auto I = llvm::bsearch(Container, [=](const InterfaceFileRef &O) { - return InstallName <= O.getInstallName(); + auto I = partition_point(Container, [=](const InterfaceFileRef &O) { + return O.getInstallName() < InstallName; }); - if ((I != std::end(Container)) && !(InstallName < I->getInstallName())) + if (I != Container.end() && I->getInstallName() == InstallName) return I; return Container.emplace(I, InstallName); @@ -44,10 +42,10 @@ void InterfaceFile::addReexportedLibrary(StringRef InstallName, } void InterfaceFile::addUUID(Architecture Arch, StringRef UUID) { - auto I = - llvm::bsearch(UUIDs, [=](const std::pair &O) { - return Arch <= O.first; - }); + auto I = partition_point(UUIDs, + [=](const std::pair &O) { + return O.first < Arch; + }); if (I != UUIDs.end() && Arch == I->first) { I->second = UUID; diff --git a/lib/Transforms/Scalar/MemCpyOptimizer.cpp b/lib/Transforms/Scalar/MemCpyOptimizer.cpp index cf852111283..dfedd7c6fd3 100644 --- a/lib/Transforms/Scalar/MemCpyOptimizer.cpp +++ b/lib/Transforms/Scalar/MemCpyOptimizer.cpp @@ -278,8 +278,8 @@ void MemsetRanges::addRange(int64_t Start, int64_t Size, Value *Ptr, unsigned Alignment, Instruction *Inst) { int64_t End = Start+Size; - range_iterator I = llvm::bsearch( - Ranges, [=](const MemsetRange &O) { return Start <= O.End; }); + range_iterator I = partition_point( + Ranges, [=](const MemsetRange &O) { return O.End < Start; }); // We now know that I == E, in which case we didn't find anything to merge // with, or that Start <= I->End. If End < I->Start or I == E, then we need diff --git a/tools/dsymutil/DwarfLinker.cpp b/tools/dsymutil/DwarfLinker.cpp index dc568678c82..e3034847292 100644 --- a/tools/dsymutil/DwarfLinker.cpp +++ b/tools/dsymutil/DwarfLinker.cpp @@ -1767,8 +1767,8 @@ static void insertLineSequence(std::vector &Seq, } object::SectionedAddress Front = Seq.front().Address; - auto InsertPoint = llvm::bsearch( - Rows, [=](const DWARFDebugLine::Row &O) { return !(O.Address < Front); }); + auto InsertPoint = partition_point( + Rows, [=](const DWARFDebugLine::Row &O) { return O.Address < Front; }); // FIXME: this only removes the unneeded end_sequence if the // sequences have been inserted in order. Using a global sort like diff --git a/tools/llvm-objdump/llvm-objdump.cpp b/tools/llvm-objdump/llvm-objdump.cpp index e4fbb5064bf..f45559b5497 100644 --- a/tools/llvm-objdump/llvm-objdump.cpp +++ b/tools/llvm-objdump/llvm-objdump.cpp @@ -973,14 +973,15 @@ static bool shouldAdjustVA(const SectionRef &Section) { typedef std::pair MappingSymbolPair; static char getMappingSymbolKind(ArrayRef MappingSymbols, uint64_t Address) { - auto Sym = bsearch(MappingSymbols, [Address](const MappingSymbolPair &Val) { - return Val.first > Address; - }); + auto It = + partition_point(MappingSymbols, [Address](const MappingSymbolPair &Val) { + return Val.first <= Address; + }); // Return zero for any address before the first mapping symbol; this means // we should use the default disassembly mode, depending on the target. - if (Sym == MappingSymbols.begin()) + if (It == MappingSymbols.begin()) return '\x00'; - return (Sym - 1)->second; + return (It - 1)->second; } static uint64_t @@ -1119,9 +1120,9 @@ static void disassembleObject(const Target *TheTarget, const ObjectFile *Obj, error(ExportEntry.getExportRVA(RVA)); uint64_t VA = COFFObj->getImageBase() + RVA; - auto Sec = llvm::bsearch( - SectionAddresses, [VA](const std::pair &RHS) { - return VA < RHS.first; + auto Sec = partition_point( + SectionAddresses, [VA](const std::pair &O) { + return O.first <= VA; }); if (Sec != SectionAddresses.begin()) { --Sec; @@ -1378,10 +1379,10 @@ static void disassembleObject(const Target *TheTarget, const ObjectFile *Obj, // N.B. We don't walk the relocations in the relocatable case yet. auto *TargetSectionSymbols = &Symbols; if (!Obj->isRelocatableObject()) { - auto It = llvm::bsearch( + auto It = partition_point( SectionAddresses, - [=](const std::pair &RHS) { - return Target < RHS.first; + [=](const std::pair &O) { + return O.first <= Target; }); if (It != SectionAddresses.begin()) { --It; @@ -1394,17 +1395,17 @@ static void disassembleObject(const Target *TheTarget, const ObjectFile *Obj, // Find the last symbol in the section whose offset is less than // or equal to the target. If there isn't a section that contains // the target, find the nearest preceding absolute symbol. - auto TargetSym = llvm::bsearch( + auto TargetSym = partition_point( *TargetSectionSymbols, - [=](const std::tuple &RHS) { - return Target < std::get<0>(RHS); + [=](const std::tuple &O) { + return std::get<0>(O) <= Target; }); if (TargetSym == TargetSectionSymbols->begin()) { TargetSectionSymbols = &AbsoluteSymbols; - TargetSym = llvm::bsearch( + TargetSym = partition_point( AbsoluteSymbols, - [=](const std::tuple &RHS) { - return Target < std::get<0>(RHS); + [=](const std::tuple &O) { + return std::get<0>(O) <= Target; }); } if (TargetSym != TargetSectionSymbols->begin()) { diff --git a/unittests/ADT/STLExtrasTest.cpp b/unittests/ADT/STLExtrasTest.cpp index ebb8c9f6f5d..8704ddda265 100644 --- a/unittests/ADT/STLExtrasTest.cpp +++ b/unittests/ADT/STLExtrasTest.cpp @@ -469,13 +469,14 @@ TEST(STLExtrasTest, to_address) { EXPECT_EQ(V1, to_address(V3)); } -TEST(STLExtrasTest, bsearch) { +TEST(STLExtrasTest, partition_point) { std::vector V = {1, 3, 5, 7, 9}; // Range version. - EXPECT_EQ(V.begin() + 3, bsearch(V, [](unsigned X) { return X >= 7; })); - EXPECT_EQ(V.begin(), bsearch(V, [](unsigned X) { return X >= 1; })); - EXPECT_EQ(V.end(), bsearch(V, [](unsigned X) { return X >= 50; })); + EXPECT_EQ(V.begin() + 3, + partition_point(V, [](unsigned X) { return X < 7; })); + EXPECT_EQ(V.begin(), partition_point(V, [](unsigned X) { return X < 1; })); + EXPECT_EQ(V.end(), partition_point(V, [](unsigned X) { return X < 50; })); } } // namespace