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);
}
/// 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 <typename R, typename Predicate,
typename Val = decltype(*adl_begin(std::declval<R>()))>
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

View File

@ -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();

View File

@ -60,8 +60,8 @@ static cl::opt<int> 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.

View File

@ -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);
}

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
// 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) {

View File

@ -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;

View File

@ -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<FrameEntry> &E) {
return Offset <= E->getOffset();
auto It = partition_point(Entries, [=](const std::unique_ptr<FrameEntry> &E) {
return E->getOffset() < Offset;
});
if (It != Entries.end() && (*It)->getOffset() == Offset)
return It->get();

View File

@ -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;

View File

@ -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;

View File

@ -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;
});
}

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
// 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];

View File

@ -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<uint64_t, uint64_t> A) {
return Address <= A.first;
});
auto It = partition_point(AddrToMD5Map, [=](std::pair<uint64_t, uint64_t> 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;
}

View File

@ -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!");

View File

@ -14,17 +14,15 @@
#include <iomanip>
#include <sstream>
using namespace llvm::MachO;
namespace llvm {
namespace MachO {
namespace detail {
template <typename C>
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<Architecture, std::string> &O) {
return Arch <= O.first;
});
auto I = partition_point(UUIDs,
[=](const std::pair<Architecture, std::string> &O) {
return O.first < Arch;
});
if (I != UUIDs.end() && Arch == I->first) {
I->second = UUID;

View File

@ -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

View File

@ -1767,8 +1767,8 @@ static void insertLineSequence(std::vector<DWARFDebugLine::Row> &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

View File

@ -973,14 +973,15 @@ static bool shouldAdjustVA(const SectionRef &Section) {
typedef std::pair<uint64_t, char> MappingSymbolPair;
static char getMappingSymbolKind(ArrayRef<MappingSymbolPair> 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<uint64_t, SectionRef> &RHS) {
return VA < RHS.first;
auto Sec = partition_point(
SectionAddresses, [VA](const std::pair<uint64_t, SectionRef> &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<uint64_t, SectionRef> &RHS) {
return Target < RHS.first;
[=](const std::pair<uint64_t, SectionRef> &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<uint64_t, StringRef, uint8_t> &RHS) {
return Target < std::get<0>(RHS);
[=](const std::tuple<uint64_t, StringRef, uint8_t> &O) {
return std::get<0>(O) <= Target;
});
if (TargetSym == TargetSectionSymbols->begin()) {
TargetSectionSymbols = &AbsoluteSymbols;
TargetSym = llvm::bsearch(
TargetSym = partition_point(
AbsoluteSymbols,
[=](const std::tuple<uint64_t, StringRef, uint8_t> &RHS) {
return Target < std::get<0>(RHS);
[=](const std::tuple<uint64_t, StringRef, uint8_t> &O) {
return std::get<0>(O) <= Target;
});
}
if (TargetSym != TargetSectionSymbols->begin()) {

View File

@ -469,13 +469,14 @@ TEST(STLExtrasTest, to_address) {
EXPECT_EQ(V1, to_address(V3));
}
TEST(STLExtrasTest, bsearch) {
TEST(STLExtrasTest, partition_point) {
std::vector<int> 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