Skip to content

Commit 78ee2fb

Browse files
committed
Cleanup: llvm::bsearch -> llvm::partition_point after r364719
llvm-svn: 364720
1 parent 2d2cb77 commit 78ee2fb

File tree

22 files changed

+79
-82
lines changed

22 files changed

+79
-82
lines changed

clang-tools-extra/clangd/index/Symbol.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ float quality(const Symbol &S) {
3535
}
3636

3737
SymbolSlab::const_iterator SymbolSlab::find(const SymbolID &ID) const {
38-
auto It =
39-
llvm::bsearch(Symbols, [&](const Symbol &S) { return !(S.ID < ID); });
38+
auto It = llvm::partition_point(Symbols,
39+
[&](const Symbol &S) { return S.ID < ID; });
4040
if (It != Symbols.end() && It->ID == ID)
4141
return It;
4242
return Symbols.end();

clang/lib/Tooling/Syntax/Tokens.cpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -127,8 +127,8 @@ TokenBuffer::spelledForExpandedToken(const syntax::Token *Expanded) const {
127127

128128
unsigned ExpandedIndex = Expanded - ExpandedTokens.data();
129129
// Find the first mapping that produced tokens after \p Expanded.
130-
auto It = llvm::bsearch(File.Mappings, [&](const Mapping &M) {
131-
return ExpandedIndex < M.BeginExpanded;
130+
auto It = llvm::partition_point(File.Mappings, [&](const Mapping &M) {
131+
return M.BeginExpanded <= ExpandedIndex;
132132
});
133133
// Our token could only be produced by the previous mapping.
134134
if (It == File.Mappings.begin()) {
@@ -212,8 +212,8 @@ TokenBuffer::expansionStartingAt(const syntax::Token *Spelled) const {
212212
Spelled < (File.SpelledTokens.data() + File.SpelledTokens.size()));
213213

214214
unsigned SpelledIndex = Spelled - File.SpelledTokens.data();
215-
auto M = llvm::bsearch(File.Mappings, [&](const Mapping &M) {
216-
return SpelledIndex <= M.BeginSpelled;
215+
auto M = llvm::partition_point(File.Mappings, [&](const Mapping &M) {
216+
return M.BeginSpelled < SpelledIndex;
217217
});
218218
if (M == File.Mappings.end() || M->BeginSpelled != SpelledIndex)
219219
return llvm::None;

lld/ELF/DWARF.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ Optional<RelocAddrEntry>
8282
LLDDwarfObj<ELFT>::findAux(const InputSectionBase &Sec, uint64_t Pos,
8383
ArrayRef<RelTy> Rels) const {
8484
auto It =
85-
llvm::bsearch(Rels, [=](const RelTy &A) { return Pos <= A.r_offset; });
85+
partition_point(Rels, [=](const RelTy &A) { return A.r_offset < Pos; });
8686
if (It == Rels.end() || It->r_offset != Pos)
8787
return None;
8888
const RelTy &Rel = *It;

lld/ELF/InputSection.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -1268,8 +1268,8 @@ SectionPiece *MergeInputSection::getSectionPiece(uint64_t Offset) {
12681268

12691269
// If Offset is not at beginning of a section piece, it is not in the map.
12701270
// In that case we need to do a binary search of the original section piece vector.
1271-
auto It = llvm::bsearch(Pieces,
1272-
[=](SectionPiece P) { return Offset < P.InputOff; });
1271+
auto It = partition_point(
1272+
Pieces, [=](SectionPiece P) { return P.InputOff <= Offset; });
12731273
return &It[-1];
12741274
}
12751275

llvm/include/llvm/ADT/STLExtras.h

+4-5
Original file line numberDiff line numberDiff line change
@@ -1322,13 +1322,12 @@ void stable_sort(R &&Range, Compare C) {
13221322
std::stable_sort(adl_begin(Range), adl_end(Range), C);
13231323
}
13241324

1325-
/// Binary search for the first iterator in a range where a predicate is true.
1326-
/// Requires that C is always false below some limit, and always true above it.
1325+
/// Binary search for the first iterator in a range where a predicate is false.
1326+
/// Requires that C is always true below some limit, and always false above it.
13271327
template <typename R, typename Predicate,
13281328
typename Val = decltype(*adl_begin(std::declval<R>()))>
1329-
auto bsearch(R &&Range, Predicate P) -> decltype(adl_begin(Range)) {
1330-
return std::partition_point(adl_begin(Range), adl_end(Range),
1331-
[&](const Val &V) { return !P(V); });
1329+
auto partition_point(R &&Range, Predicate P) -> decltype(adl_begin(Range)) {
1330+
return std::partition_point(adl_begin(Range), adl_end(Range), P);
13321331
}
13331332

13341333
/// Wrapper function around std::equal to detect if all elements

llvm/include/llvm/DebugInfo/DWARF/DWARFUnit.h

+4-3
Original file line numberDiff line numberDiff line change
@@ -473,9 +473,10 @@ class DWARFUnit {
473473
DWARFDie getDIEForOffset(uint32_t Offset) {
474474
extractDIEsIfNeeded(false);
475475
assert(!DieArray.empty());
476-
auto It = llvm::bsearch(DieArray, [=](const DWARFDebugInfoEntry &LHS) {
477-
return Offset <= LHS.getOffset();
478-
});
476+
auto It =
477+
llvm::partition_point(DieArray, [=](const DWARFDebugInfoEntry &DIE) {
478+
return DIE.getOffset() < Offset;
479+
});
479480
if (It != DieArray.end() && It->getOffset() == Offset)
480481
return DWARFDie(this, &*It);
481482
return DWARFDie();

llvm/lib/Analysis/ProfileSummaryInfo.cpp

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

llvm/lib/CodeGen/ExecutionDomainFix.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -337,8 +337,8 @@ void ExecutionDomainFix::visitSoftInstr(MachineInstr *mi, unsigned mask) {
337337
// Sorted insertion.
338338
// Enables giving priority to the latest domains during merging.
339339
const int Def = RDA->getReachingDef(mi, RC->getRegister(rx));
340-
auto I = llvm::bsearch(Regs, [&](int I) {
341-
return Def < RDA->getReachingDef(mi, RC->getRegister(I));
340+
auto I = partition_point(Regs, [&](int I) {
341+
return RDA->getReachingDef(mi, RC->getRegister(I)) <= Def;
342342
});
343343
Regs.insert(I, rx);
344344
}

llvm/lib/CodeGen/GlobalISel/LegalizerInfo.cpp

+4-5
Original file line numberDiff line numberDiff line change
@@ -544,11 +544,10 @@ LegalizerInfo::findAction(const SizeAndActionsVec &Vec, const uint32_t Size) {
544544
// Find the last element in Vec that has a bitsize equal to or smaller than
545545
// the requested bit size.
546546
// That is the element just before the first element that is bigger than Size.
547-
auto VecIt = llvm::bsearch(
548-
Vec, [=](const SizeAndAction &A) { return Size < A.first; });
549-
assert(VecIt != Vec.begin() && "Does Vec not start with size 1?");
550-
--VecIt;
551-
int VecIdx = VecIt - Vec.begin();
547+
auto It = partition_point(
548+
Vec, [=](const SizeAndAction &A) { return A.first <= Size; });
549+
assert(It != Vec.begin() && "Does Vec not start with size 1?");
550+
int VecIdx = It - Vec.begin() - 1;
552551

553552
LegalizeAction Action = Vec[VecIdx].second;
554553
switch (Action) {

llvm/lib/DebugInfo/DWARF/DWARFDebugAranges.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ void DWARFDebugAranges::construct() {
115115

116116
uint32_t DWARFDebugAranges::findAddress(uint64_t Address) const {
117117
RangeCollIterator It =
118-
llvm::bsearch(Aranges, [=](Range RHS) { return Address < RHS.HighPC(); });
118+
partition_point(Aranges, [=](Range R) { return R.HighPC() <= Address; });
119119
if (It != Aranges.end() && It->LowPC <= Address)
120120
return It->CUOffset;
121121
return -1U;

llvm/lib/DebugInfo/DWARF/DWARFDebugFrame.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -532,8 +532,8 @@ void DWARFDebugFrame::parse(DWARFDataExtractor Data) {
532532
}
533533

534534
FrameEntry *DWARFDebugFrame::getEntryAtOffset(uint64_t Offset) const {
535-
auto It = llvm::bsearch(Entries, [=](const std::unique_ptr<FrameEntry> &E) {
536-
return Offset <= E->getOffset();
535+
auto It = partition_point(Entries, [=](const std::unique_ptr<FrameEntry> &E) {
536+
return E->getOffset() < Offset;
537537
});
538538
if (It != Entries.end() && (*It)->getOffset() == Offset)
539539
return It->get();

llvm/lib/DebugInfo/DWARF/DWARFDebugLoc.cpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,8 @@ void DWARFDebugLoc::LocationList::dump(raw_ostream &OS, bool IsLittleEndian,
5757

5858
DWARFDebugLoc::LocationList const *
5959
DWARFDebugLoc::getLocationListAtOffset(uint64_t Offset) const {
60-
auto It = llvm::bsearch(
61-
Locations, [=](const LocationList &L) { return Offset <= L.Offset; });
60+
auto It = partition_point(
61+
Locations, [=](const LocationList &L) { return L.Offset < Offset; });
6262
if (It != Locations.end() && It->Offset == Offset)
6363
return &(*It);
6464
return nullptr;
@@ -212,8 +212,8 @@ void DWARFDebugLoclists::parse(DataExtractor data, unsigned Version) {
212212

213213
DWARFDebugLoclists::LocationList const *
214214
DWARFDebugLoclists::getLocationListAtOffset(uint64_t Offset) const {
215-
auto It = llvm::bsearch(
216-
Locations, [=](const LocationList &L) { return Offset <= L.Offset; });
215+
auto It = partition_point(
216+
Locations, [=](const LocationList &L) { return L.Offset < Offset; });
217217
if (It != Locations.end() && It->Offset == Offset)
218218
return &(*It);
219219
return nullptr;

llvm/lib/DebugInfo/DWARF/DWARFUnitIndex.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -172,8 +172,8 @@ DWARFUnitIndex::getFromOffset(uint32_t Offset) const {
172172
E2->Contributions[InfoColumn].Offset;
173173
});
174174
}
175-
auto I = llvm::bsearch(OffsetLookup, [&](Entry *E2) {
176-
return Offset < E2->Contributions[InfoColumn].Offset;
175+
auto I = partition_point(OffsetLookup, [&](Entry *E2) {
176+
return E2->Contributions[InfoColumn].Offset <= Offset;
177177
});
178178
if (I == OffsetLookup.begin())
179179
return nullptr;

llvm/lib/IR/DataLayout.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -463,8 +463,8 @@ DataLayout::AlignmentsTy::iterator
463463
DataLayout::findAlignmentLowerBound(AlignTypeEnum AlignType,
464464
uint32_t BitWidth) {
465465
auto Pair = std::make_pair((unsigned)AlignType, BitWidth);
466-
return llvm::bsearch(Alignments, [=](const LayoutAlignElem &E) {
467-
return Pair <= std::make_pair(E.AlignType, E.TypeBitWidth);
466+
return partition_point(Alignments, [=](const LayoutAlignElem &E) {
467+
return std::make_pair(E.AlignType, E.TypeBitWidth) < Pair;
468468
});
469469
}
470470

llvm/lib/IR/Function.cpp

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

llvm/lib/ProfileData/InstrProf.cpp

+5-6
Original file line numberDiff line numberDiff line change
@@ -363,16 +363,15 @@ Error InstrProfSymtab::create(Module &M, bool InLTO) {
363363

364364
uint64_t InstrProfSymtab::getFunctionHashFromAddress(uint64_t Address) {
365365
finalizeSymtab();
366-
auto Result =
367-
llvm::bsearch(AddrToMD5Map, [=](std::pair<uint64_t, uint64_t> A) {
368-
return Address <= A.first;
369-
});
366+
auto It = partition_point(AddrToMD5Map, [=](std::pair<uint64_t, uint64_t> A) {
367+
return A.first < Address;
368+
});
370369
// Raw function pointer collected by value profiler may be from
371370
// external functions that are not instrumented. They won't have
372371
// mapping data to be used by the deserializer. Force the value to
373372
// be 0 in this case.
374-
if (Result != AddrToMD5Map.end() && Result->first == Address)
375-
return (uint64_t)Result->second;
373+
if (It != AddrToMD5Map.end() && It->first == Address)
374+
return (uint64_t)It->second;
376375
return 0;
377376
}
378377

llvm/lib/Target/X86/X86InstrFMA3Info.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -158,8 +158,8 @@ const X86InstrFMA3Group *llvm::getFMA3Group(unsigned Opcode, uint64_t TSFlags) {
158158
// FMA 231 instructions have an opcode of 0xB6-0xBF
159159
unsigned FormIndex = ((BaseOpcode - 0x90) >> 4) & 0x3;
160160

161-
auto I = llvm::bsearch(Table, [=](const X86InstrFMA3Group &Group) {
162-
return Opcode <= Group.Opcodes[FormIndex];
161+
auto I = partition_point(Table, [=](const X86InstrFMA3Group &Group) {
162+
return Group.Opcodes[FormIndex] < Opcode;
163163
});
164164
assert(I != Table.end() && I->Opcodes[FormIndex] == Opcode &&
165165
"Couldn't find FMA3 opcode!");

llvm/lib/TextAPI/MachO/InterfaceFile.cpp

+7-9
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,15 @@
1414
#include <iomanip>
1515
#include <sstream>
1616

17-
using namespace llvm::MachO;
18-
1917
namespace llvm {
2018
namespace MachO {
2119
namespace detail {
2220
template <typename C>
2321
typename C::iterator addEntry(C &Container, StringRef InstallName) {
24-
auto I = llvm::bsearch(Container, [=](const InterfaceFileRef &O) {
25-
return InstallName <= O.getInstallName();
22+
auto I = partition_point(Container, [=](const InterfaceFileRef &O) {
23+
return O.getInstallName() < InstallName;
2624
});
27-
if ((I != std::end(Container)) && !(InstallName < I->getInstallName()))
25+
if (I != Container.end() && I->getInstallName() == InstallName)
2826
return I;
2927

3028
return Container.emplace(I, InstallName);
@@ -44,10 +42,10 @@ void InterfaceFile::addReexportedLibrary(StringRef InstallName,
4442
}
4543

4644
void InterfaceFile::addUUID(Architecture Arch, StringRef UUID) {
47-
auto I =
48-
llvm::bsearch(UUIDs, [=](const std::pair<Architecture, std::string> &O) {
49-
return Arch <= O.first;
50-
});
45+
auto I = partition_point(UUIDs,
46+
[=](const std::pair<Architecture, std::string> &O) {
47+
return O.first < Arch;
48+
});
5149

5250
if (I != UUIDs.end() && Arch == I->first) {
5351
I->second = UUID;

llvm/lib/Transforms/Scalar/MemCpyOptimizer.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -278,8 +278,8 @@ void MemsetRanges::addRange(int64_t Start, int64_t Size, Value *Ptr,
278278
unsigned Alignment, Instruction *Inst) {
279279
int64_t End = Start+Size;
280280

281-
range_iterator I = llvm::bsearch(
282-
Ranges, [=](const MemsetRange &O) { return Start <= O.End; });
281+
range_iterator I = partition_point(
282+
Ranges, [=](const MemsetRange &O) { return O.End < Start; });
283283

284284
// We now know that I == E, in which case we didn't find anything to merge
285285
// with, or that Start <= I->End. If End < I->Start or I == E, then we need

llvm/tools/dsymutil/DwarfLinker.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -1767,8 +1767,8 @@ static void insertLineSequence(std::vector<DWARFDebugLine::Row> &Seq,
17671767
}
17681768

17691769
object::SectionedAddress Front = Seq.front().Address;
1770-
auto InsertPoint = llvm::bsearch(
1771-
Rows, [=](const DWARFDebugLine::Row &O) { return !(O.Address < Front); });
1770+
auto InsertPoint = partition_point(
1771+
Rows, [=](const DWARFDebugLine::Row &O) { return O.Address < Front; });
17721772

17731773
// FIXME: this only removes the unneeded end_sequence if the
17741774
// sequences have been inserted in order. Using a global sort like

llvm/tools/llvm-objdump/llvm-objdump.cpp

+18-17
Original file line numberDiff line numberDiff line change
@@ -973,14 +973,15 @@ static bool shouldAdjustVA(const SectionRef &Section) {
973973
typedef std::pair<uint64_t, char> MappingSymbolPair;
974974
static char getMappingSymbolKind(ArrayRef<MappingSymbolPair> MappingSymbols,
975975
uint64_t Address) {
976-
auto Sym = bsearch(MappingSymbols, [Address](const MappingSymbolPair &Val) {
977-
return Val.first > Address;
978-
});
976+
auto It =
977+
partition_point(MappingSymbols, [Address](const MappingSymbolPair &Val) {
978+
return Val.first <= Address;
979+
});
979980
// Return zero for any address before the first mapping symbol; this means
980981
// we should use the default disassembly mode, depending on the target.
981-
if (Sym == MappingSymbols.begin())
982+
if (It == MappingSymbols.begin())
982983
return '\x00';
983-
return (Sym - 1)->second;
984+
return (It - 1)->second;
984985
}
985986

986987
static uint64_t
@@ -1119,9 +1120,9 @@ static void disassembleObject(const Target *TheTarget, const ObjectFile *Obj,
11191120
error(ExportEntry.getExportRVA(RVA));
11201121

11211122
uint64_t VA = COFFObj->getImageBase() + RVA;
1122-
auto Sec = llvm::bsearch(
1123-
SectionAddresses, [VA](const std::pair<uint64_t, SectionRef> &RHS) {
1124-
return VA < RHS.first;
1123+
auto Sec = partition_point(
1124+
SectionAddresses, [VA](const std::pair<uint64_t, SectionRef> &O) {
1125+
return O.first <= VA;
11251126
});
11261127
if (Sec != SectionAddresses.begin()) {
11271128
--Sec;
@@ -1378,10 +1379,10 @@ static void disassembleObject(const Target *TheTarget, const ObjectFile *Obj,
13781379
// N.B. We don't walk the relocations in the relocatable case yet.
13791380
auto *TargetSectionSymbols = &Symbols;
13801381
if (!Obj->isRelocatableObject()) {
1381-
auto It = llvm::bsearch(
1382+
auto It = partition_point(
13821383
SectionAddresses,
1383-
[=](const std::pair<uint64_t, SectionRef> &RHS) {
1384-
return Target < RHS.first;
1384+
[=](const std::pair<uint64_t, SectionRef> &O) {
1385+
return O.first <= Target;
13851386
});
13861387
if (It != SectionAddresses.begin()) {
13871388
--It;
@@ -1394,17 +1395,17 @@ static void disassembleObject(const Target *TheTarget, const ObjectFile *Obj,
13941395
// Find the last symbol in the section whose offset is less than
13951396
// or equal to the target. If there isn't a section that contains
13961397
// the target, find the nearest preceding absolute symbol.
1397-
auto TargetSym = llvm::bsearch(
1398+
auto TargetSym = partition_point(
13981399
*TargetSectionSymbols,
1399-
[=](const std::tuple<uint64_t, StringRef, uint8_t> &RHS) {
1400-
return Target < std::get<0>(RHS);
1400+
[=](const std::tuple<uint64_t, StringRef, uint8_t> &O) {
1401+
return std::get<0>(O) <= Target;
14011402
});
14021403
if (TargetSym == TargetSectionSymbols->begin()) {
14031404
TargetSectionSymbols = &AbsoluteSymbols;
1404-
TargetSym = llvm::bsearch(
1405+
TargetSym = partition_point(
14051406
AbsoluteSymbols,
1406-
[=](const std::tuple<uint64_t, StringRef, uint8_t> &RHS) {
1407-
return Target < std::get<0>(RHS);
1407+
[=](const std::tuple<uint64_t, StringRef, uint8_t> &O) {
1408+
return std::get<0>(O) <= Target;
14081409
});
14091410
}
14101411
if (TargetSym != TargetSectionSymbols->begin()) {

llvm/unittests/ADT/STLExtrasTest.cpp

+5-4
Original file line numberDiff line numberDiff line change
@@ -469,13 +469,14 @@ TEST(STLExtrasTest, to_address) {
469469
EXPECT_EQ(V1, to_address(V3));
470470
}
471471

472-
TEST(STLExtrasTest, bsearch) {
472+
TEST(STLExtrasTest, partition_point) {
473473
std::vector<int> V = {1, 3, 5, 7, 9};
474474

475475
// Range version.
476-
EXPECT_EQ(V.begin() + 3, bsearch(V, [](unsigned X) { return X >= 7; }));
477-
EXPECT_EQ(V.begin(), bsearch(V, [](unsigned X) { return X >= 1; }));
478-
EXPECT_EQ(V.end(), bsearch(V, [](unsigned X) { return X >= 50; }));
476+
EXPECT_EQ(V.begin() + 3,
477+
partition_point(V, [](unsigned X) { return X < 7; }));
478+
EXPECT_EQ(V.begin(), partition_point(V, [](unsigned X) { return X < 1; }));
479+
EXPECT_EQ(V.end(), partition_point(V, [](unsigned X) { return X < 50; }));
479480
}
480481

481482
} // namespace

0 commit comments

Comments
 (0)