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
This commit is contained in:
Fangrui Song 2019-06-30 11:19:56 +00:00
parent 726837ec5a
commit 3228596b4c
18 changed files with 70 additions and 73 deletions

View File

@ -1322,13 +1322,12 @@ void stable_sort(R &&Range, Compare C) {
std::stable_sort(adl_begin(Range), adl_end(Range), 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. /// Binary search for the first iterator in a range where a predicate is false.
/// Requires that C is always false below some limit, and always true above it. /// Requires that C is always true below some limit, and always false above it.
template <typename R, typename Predicate, template <typename R, typename Predicate,
typename Val = decltype(*adl_begin(std::declval<R>()))> typename Val = decltype(*adl_begin(std::declval<R>()))>
auto bsearch(R &&Range, Predicate P) -> decltype(adl_begin(Range)) { auto partition_point(R &&Range, Predicate P) -> decltype(adl_begin(Range)) {
return std::partition_point(adl_begin(Range), adl_end(Range), return std::partition_point(adl_begin(Range), adl_end(Range), P);
[&](const Val &V) { return !P(V); });
} }
/// Wrapper function around std::equal to detect if all elements /// Wrapper function around std::equal to detect if all elements

View File

@ -473,9 +473,10 @@ public:
DWARFDie getDIEForOffset(uint32_t Offset) { DWARFDie getDIEForOffset(uint32_t Offset) {
extractDIEsIfNeeded(false); extractDIEsIfNeeded(false);
assert(!DieArray.empty()); assert(!DieArray.empty());
auto It = llvm::bsearch(DieArray, [=](const DWARFDebugInfoEntry &LHS) { auto It =
return Offset <= LHS.getOffset(); llvm::partition_point(DieArray, [=](const DWARFDebugInfoEntry &DIE) {
}); return DIE.getOffset() < Offset;
});
if (It != DieArray.end() && It->getOffset() == Offset) if (It != DieArray.end() && It->getOffset() == Offset)
return DWARFDie(this, &*It); return DWARFDie(this, &*It);
return DWARFDie(); return DWARFDie();

View File

@ -60,8 +60,8 @@ static cl::opt<int> ProfileSummaryColdCount(
// Find the summary entry for a desired percentile of counts. // Find the summary entry for a desired percentile of counts.
static const ProfileSummaryEntry &getEntryForPercentile(SummaryEntryVector &DS, static const ProfileSummaryEntry &getEntryForPercentile(SummaryEntryVector &DS,
uint64_t Percentile) { uint64_t Percentile) {
auto It = llvm::bsearch(DS, [=](const ProfileSummaryEntry &Entry) { auto It = partition_point(DS, [=](const ProfileSummaryEntry &Entry) {
return Percentile <= Entry.Cutoff; return Entry.Cutoff < Percentile;
}); });
// The required percentile has to be <= one of the percentiles in the // The required percentile has to be <= one of the percentiles in the
// detailed summary. // detailed summary.

View File

@ -337,8 +337,8 @@ void ExecutionDomainFix::visitSoftInstr(MachineInstr *mi, unsigned mask) {
// Sorted insertion. // Sorted insertion.
// Enables giving priority to the latest domains during merging. // Enables giving priority to the latest domains during merging.
const int Def = RDA->getReachingDef(mi, RC->getRegister(rx)); const int Def = RDA->getReachingDef(mi, RC->getRegister(rx));
auto I = llvm::bsearch(Regs, [&](int I) { auto I = partition_point(Regs, [&](int I) {
return Def < RDA->getReachingDef(mi, RC->getRegister(I)); return RDA->getReachingDef(mi, RC->getRegister(I)) <= Def;
}); });
Regs.insert(I, rx); Regs.insert(I, rx);
} }

View File

@ -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 // Find the last element in Vec that has a bitsize equal to or smaller than
// the requested bit size. // the requested bit size.
// That is the element just before the first element that is bigger than Size. // That is the element just before the first element that is bigger than Size.
auto VecIt = llvm::bsearch( auto It = partition_point(
Vec, [=](const SizeAndAction &A) { return Size < A.first; }); Vec, [=](const SizeAndAction &A) { return A.first <= Size; });
assert(VecIt != Vec.begin() && "Does Vec not start with size 1?"); assert(It != Vec.begin() && "Does Vec not start with size 1?");
--VecIt; int VecIdx = It - Vec.begin() - 1;
int VecIdx = VecIt - Vec.begin();
LegalizeAction Action = Vec[VecIdx].second; LegalizeAction Action = Vec[VecIdx].second;
switch (Action) { switch (Action) {

View File

@ -115,7 +115,7 @@ void DWARFDebugAranges::construct() {
uint32_t DWARFDebugAranges::findAddress(uint64_t Address) const { uint32_t DWARFDebugAranges::findAddress(uint64_t Address) const {
RangeCollIterator It = 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) if (It != Aranges.end() && It->LowPC <= Address)
return It->CUOffset; return It->CUOffset;
return -1U; return -1U;

View File

@ -532,8 +532,8 @@ void DWARFDebugFrame::parse(DWARFDataExtractor Data) {
} }
FrameEntry *DWARFDebugFrame::getEntryAtOffset(uint64_t Offset) const { FrameEntry *DWARFDebugFrame::getEntryAtOffset(uint64_t Offset) const {
auto It = llvm::bsearch(Entries, [=](const std::unique_ptr<FrameEntry> &E) { auto It = partition_point(Entries, [=](const std::unique_ptr<FrameEntry> &E) {
return Offset <= E->getOffset(); return E->getOffset() < Offset;
}); });
if (It != Entries.end() && (*It)->getOffset() == Offset) if (It != Entries.end() && (*It)->getOffset() == Offset)
return It->get(); return It->get();

View File

@ -57,8 +57,8 @@ void DWARFDebugLoc::LocationList::dump(raw_ostream &OS, bool IsLittleEndian,
DWARFDebugLoc::LocationList const * DWARFDebugLoc::LocationList const *
DWARFDebugLoc::getLocationListAtOffset(uint64_t Offset) const { DWARFDebugLoc::getLocationListAtOffset(uint64_t Offset) const {
auto It = llvm::bsearch( auto It = partition_point(
Locations, [=](const LocationList &L) { return Offset <= L.Offset; }); Locations, [=](const LocationList &L) { return L.Offset < Offset; });
if (It != Locations.end() && It->Offset == Offset) if (It != Locations.end() && It->Offset == Offset)
return &(*It); return &(*It);
return nullptr; return nullptr;
@ -212,8 +212,8 @@ void DWARFDebugLoclists::parse(DataExtractor data, unsigned Version) {
DWARFDebugLoclists::LocationList const * DWARFDebugLoclists::LocationList const *
DWARFDebugLoclists::getLocationListAtOffset(uint64_t Offset) const { DWARFDebugLoclists::getLocationListAtOffset(uint64_t Offset) const {
auto It = llvm::bsearch( auto It = partition_point(
Locations, [=](const LocationList &L) { return Offset <= L.Offset; }); Locations, [=](const LocationList &L) { return L.Offset < Offset; });
if (It != Locations.end() && It->Offset == Offset) if (It != Locations.end() && It->Offset == Offset)
return &(*It); return &(*It);
return nullptr; return nullptr;

View File

@ -172,8 +172,8 @@ DWARFUnitIndex::getFromOffset(uint32_t Offset) const {
E2->Contributions[InfoColumn].Offset; E2->Contributions[InfoColumn].Offset;
}); });
} }
auto I = llvm::bsearch(OffsetLookup, [&](Entry *E2) { auto I = partition_point(OffsetLookup, [&](Entry *E2) {
return Offset < E2->Contributions[InfoColumn].Offset; return E2->Contributions[InfoColumn].Offset <= Offset;
}); });
if (I == OffsetLookup.begin()) if (I == OffsetLookup.begin())
return nullptr; return nullptr;

View File

@ -463,8 +463,8 @@ DataLayout::AlignmentsTy::iterator
DataLayout::findAlignmentLowerBound(AlignTypeEnum AlignType, DataLayout::findAlignmentLowerBound(AlignTypeEnum AlignType,
uint32_t BitWidth) { uint32_t BitWidth) {
auto Pair = std::make_pair((unsigned)AlignType, BitWidth); auto Pair = std::make_pair((unsigned)AlignType, BitWidth);
return llvm::bsearch(Alignments, [=](const LayoutAlignElem &E) { return partition_point(Alignments, [=](const LayoutAlignElem &E) {
return Pair <= std::make_pair(E.AlignType, E.TypeBitWidth); return std::make_pair(E.AlignType, E.TypeBitWidth) < Pair;
}); });
} }

View File

@ -533,9 +533,8 @@ static ArrayRef<const char *> findTargetSubtable(StringRef Name) {
// Drop "llvm." and take the first dotted component. That will be the target // Drop "llvm." and take the first dotted component. That will be the target
// if this is target specific. // if this is target specific.
StringRef Target = Name.drop_front(5).split('.').first; StringRef Target = Name.drop_front(5).split('.').first;
auto It = llvm::bsearch(Targets, [=](const IntrinsicTargetInfo &TI) { auto It = partition_point(
return Target <= TI.Name; Targets, [=](const IntrinsicTargetInfo &TI) { return TI.Name < Target; });
});
// We've either found the target or just fall back to the generic set, which // We've either found the target or just fall back to the generic set, which
// is always first. // is always first.
const auto &TI = It != Targets.end() && It->Name == Target ? *It : Targets[0]; const auto &TI = It != Targets.end() && It->Name == Target ? *It : Targets[0];

View File

@ -363,16 +363,15 @@ Error InstrProfSymtab::create(Module &M, bool InLTO) {
uint64_t InstrProfSymtab::getFunctionHashFromAddress(uint64_t Address) { uint64_t InstrProfSymtab::getFunctionHashFromAddress(uint64_t Address) {
finalizeSymtab(); finalizeSymtab();
auto Result = auto It = partition_point(AddrToMD5Map, [=](std::pair<uint64_t, uint64_t> A) {
llvm::bsearch(AddrToMD5Map, [=](std::pair<uint64_t, uint64_t> A) { return A.first < Address;
return Address <= A.first; });
});
// Raw function pointer collected by value profiler may be from // Raw function pointer collected by value profiler may be from
// external functions that are not instrumented. They won't have // external functions that are not instrumented. They won't have
// mapping data to be used by the deserializer. Force the value to // mapping data to be used by the deserializer. Force the value to
// be 0 in this case. // be 0 in this case.
if (Result != AddrToMD5Map.end() && Result->first == Address) if (It != AddrToMD5Map.end() && It->first == Address)
return (uint64_t)Result->second; return (uint64_t)It->second;
return 0; return 0;
} }

View File

@ -158,8 +158,8 @@ const X86InstrFMA3Group *llvm::getFMA3Group(unsigned Opcode, uint64_t TSFlags) {
// FMA 231 instructions have an opcode of 0xB6-0xBF // FMA 231 instructions have an opcode of 0xB6-0xBF
unsigned FormIndex = ((BaseOpcode - 0x90) >> 4) & 0x3; unsigned FormIndex = ((BaseOpcode - 0x90) >> 4) & 0x3;
auto I = llvm::bsearch(Table, [=](const X86InstrFMA3Group &Group) { auto I = partition_point(Table, [=](const X86InstrFMA3Group &Group) {
return Opcode <= Group.Opcodes[FormIndex]; return Group.Opcodes[FormIndex] < Opcode;
}); });
assert(I != Table.end() && I->Opcodes[FormIndex] == Opcode && assert(I != Table.end() && I->Opcodes[FormIndex] == Opcode &&
"Couldn't find FMA3 opcode!"); "Couldn't find FMA3 opcode!");

View File

@ -14,17 +14,15 @@
#include <iomanip> #include <iomanip>
#include <sstream> #include <sstream>
using namespace llvm::MachO;
namespace llvm { namespace llvm {
namespace MachO { namespace MachO {
namespace detail { namespace detail {
template <typename C> template <typename C>
typename C::iterator addEntry(C &Container, StringRef InstallName) { typename C::iterator addEntry(C &Container, StringRef InstallName) {
auto I = llvm::bsearch(Container, [=](const InterfaceFileRef &O) { auto I = partition_point(Container, [=](const InterfaceFileRef &O) {
return InstallName <= O.getInstallName(); return O.getInstallName() < InstallName;
}); });
if ((I != std::end(Container)) && !(InstallName < I->getInstallName())) if (I != Container.end() && I->getInstallName() == InstallName)
return I; return I;
return Container.emplace(I, InstallName); return Container.emplace(I, InstallName);
@ -44,10 +42,10 @@ void InterfaceFile::addReexportedLibrary(StringRef InstallName,
} }
void InterfaceFile::addUUID(Architecture Arch, StringRef UUID) { void InterfaceFile::addUUID(Architecture Arch, StringRef UUID) {
auto I = auto I = partition_point(UUIDs,
llvm::bsearch(UUIDs, [=](const std::pair<Architecture, std::string> &O) { [=](const std::pair<Architecture, std::string> &O) {
return Arch <= O.first; return O.first < Arch;
}); });
if (I != UUIDs.end() && Arch == I->first) { if (I != UUIDs.end() && Arch == I->first) {
I->second = UUID; I->second = UUID;

View File

@ -278,8 +278,8 @@ void MemsetRanges::addRange(int64_t Start, int64_t Size, Value *Ptr,
unsigned Alignment, Instruction *Inst) { unsigned Alignment, Instruction *Inst) {
int64_t End = Start+Size; int64_t End = Start+Size;
range_iterator I = llvm::bsearch( range_iterator I = partition_point(
Ranges, [=](const MemsetRange &O) { return Start <= O.End; }); Ranges, [=](const MemsetRange &O) { return O.End < Start; });
// We now know that I == E, in which case we didn't find anything to merge // 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 // with, or that Start <= I->End. If End < I->Start or I == E, then we need

View File

@ -1767,8 +1767,8 @@ static void insertLineSequence(std::vector<DWARFDebugLine::Row> &Seq,
} }
object::SectionedAddress Front = Seq.front().Address; object::SectionedAddress Front = Seq.front().Address;
auto InsertPoint = llvm::bsearch( auto InsertPoint = partition_point(
Rows, [=](const DWARFDebugLine::Row &O) { return !(O.Address < Front); }); Rows, [=](const DWARFDebugLine::Row &O) { return O.Address < Front; });
// FIXME: this only removes the unneeded end_sequence if the // FIXME: this only removes the unneeded end_sequence if the
// sequences have been inserted in order. Using a global sort like // sequences have been inserted in order. Using a global sort like

View File

@ -973,14 +973,15 @@ static bool shouldAdjustVA(const SectionRef &Section) {
typedef std::pair<uint64_t, char> MappingSymbolPair; typedef std::pair<uint64_t, char> MappingSymbolPair;
static char getMappingSymbolKind(ArrayRef<MappingSymbolPair> MappingSymbols, static char getMappingSymbolKind(ArrayRef<MappingSymbolPair> MappingSymbols,
uint64_t Address) { uint64_t Address) {
auto Sym = bsearch(MappingSymbols, [Address](const MappingSymbolPair &Val) { auto It =
return Val.first > Address; partition_point(MappingSymbols, [Address](const MappingSymbolPair &Val) {
}); return Val.first <= Address;
});
// Return zero for any address before the first mapping symbol; this means // Return zero for any address before the first mapping symbol; this means
// we should use the default disassembly mode, depending on the target. // we should use the default disassembly mode, depending on the target.
if (Sym == MappingSymbols.begin()) if (It == MappingSymbols.begin())
return '\x00'; return '\x00';
return (Sym - 1)->second; return (It - 1)->second;
} }
static uint64_t static uint64_t
@ -1119,9 +1120,9 @@ static void disassembleObject(const Target *TheTarget, const ObjectFile *Obj,
error(ExportEntry.getExportRVA(RVA)); error(ExportEntry.getExportRVA(RVA));
uint64_t VA = COFFObj->getImageBase() + RVA; uint64_t VA = COFFObj->getImageBase() + RVA;
auto Sec = llvm::bsearch( auto Sec = partition_point(
SectionAddresses, [VA](const std::pair<uint64_t, SectionRef> &RHS) { SectionAddresses, [VA](const std::pair<uint64_t, SectionRef> &O) {
return VA < RHS.first; return O.first <= VA;
}); });
if (Sec != SectionAddresses.begin()) { if (Sec != SectionAddresses.begin()) {
--Sec; --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. // N.B. We don't walk the relocations in the relocatable case yet.
auto *TargetSectionSymbols = &Symbols; auto *TargetSectionSymbols = &Symbols;
if (!Obj->isRelocatableObject()) { if (!Obj->isRelocatableObject()) {
auto It = llvm::bsearch( auto It = partition_point(
SectionAddresses, SectionAddresses,
[=](const std::pair<uint64_t, SectionRef> &RHS) { [=](const std::pair<uint64_t, SectionRef> &O) {
return Target < RHS.first; return O.first <= Target;
}); });
if (It != SectionAddresses.begin()) { if (It != SectionAddresses.begin()) {
--It; --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 // 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 // or equal to the target. If there isn't a section that contains
// the target, find the nearest preceding absolute symbol. // the target, find the nearest preceding absolute symbol.
auto TargetSym = llvm::bsearch( auto TargetSym = partition_point(
*TargetSectionSymbols, *TargetSectionSymbols,
[=](const std::tuple<uint64_t, StringRef, uint8_t> &RHS) { [=](const std::tuple<uint64_t, StringRef, uint8_t> &O) {
return Target < std::get<0>(RHS); return std::get<0>(O) <= Target;
}); });
if (TargetSym == TargetSectionSymbols->begin()) { if (TargetSym == TargetSectionSymbols->begin()) {
TargetSectionSymbols = &AbsoluteSymbols; TargetSectionSymbols = &AbsoluteSymbols;
TargetSym = llvm::bsearch( TargetSym = partition_point(
AbsoluteSymbols, AbsoluteSymbols,
[=](const std::tuple<uint64_t, StringRef, uint8_t> &RHS) { [=](const std::tuple<uint64_t, StringRef, uint8_t> &O) {
return Target < std::get<0>(RHS); return std::get<0>(O) <= Target;
}); });
} }
if (TargetSym != TargetSectionSymbols->begin()) { if (TargetSym != TargetSectionSymbols->begin()) {

View File

@ -469,13 +469,14 @@ TEST(STLExtrasTest, to_address) {
EXPECT_EQ(V1, to_address(V3)); EXPECT_EQ(V1, to_address(V3));
} }
TEST(STLExtrasTest, bsearch) { TEST(STLExtrasTest, partition_point) {
std::vector<int> V = {1, 3, 5, 7, 9}; std::vector<int> V = {1, 3, 5, 7, 9};
// Range version. // Range version.
EXPECT_EQ(V.begin() + 3, bsearch(V, [](unsigned X) { return X >= 7; })); EXPECT_EQ(V.begin() + 3,
EXPECT_EQ(V.begin(), bsearch(V, [](unsigned X) { return X >= 1; })); partition_point(V, [](unsigned X) { return X < 7; }));
EXPECT_EQ(V.end(), bsearch(V, [](unsigned X) { return X >= 50; })); 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 } // namespace