llvm.org GIT mirror llvm / 3228596
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 Fangrui Song a month ago
18 changed file(s) with 70 addition(s) and 73 deletion(s). Raw diff Collapse all Expand all
13211321 std::stable_sort(adl_begin(Range), adl_end(Range), C);
13221322 }
13231323
1324 /// Binary search for the first iterator in a range where a predicate is true.
1325 /// Requires that C is always false below some limit, and always true above it.
1324 /// Binary search for the first iterator in a range where a predicate is false.
1325 /// Requires that C is always true below some limit, and always false above it.
13261326 template
13271327 typename Val = decltype(*adl_begin(std::declval()))>
1328 auto bsearch(R &&Range, Predicate P) -> decltype(adl_begin(Range)) {
1329 return std::partition_point(adl_begin(Range), adl_end(Range),
1330 [&](const Val &V) { return !P(V); });
1328 auto partition_point(R &&Range, Predicate P) -> decltype(adl_begin(Range)) {
1329 return std::partition_point(adl_begin(Range), adl_end(Range), P);
13311330 }
13321331
13331332 /// Wrapper function around std::equal to detect if all elements
472472 DWARFDie getDIEForOffset(uint32_t Offset) {
473473 extractDIEsIfNeeded(false);
474474 assert(!DieArray.empty());
475 auto It = llvm::bsearch(DieArray, [=](const DWARFDebugInfoEntry &LHS) {
476 return Offset <= LHS.getOffset();
477 });
475 auto It =
476 llvm::partition_point(DieArray, [=](const DWARFDebugInfoEntry &DIE) {
477 return DIE.getOffset() < Offset;
478 });
478479 if (It != DieArray.end() && It->getOffset() == Offset)
479480 return DWARFDie(this, &*It);
480481 return DWARFDie();
5959 // Find the summary entry for a desired percentile of counts.
6060 static const ProfileSummaryEntry &getEntryForPercentile(SummaryEntryVector &DS,
6161 uint64_t Percentile) {
62 auto It = llvm::bsearch(DS, [=](const ProfileSummaryEntry &Entry) {
63 return Percentile <= Entry.Cutoff;
62 auto It = partition_point(DS, [=](const ProfileSummaryEntry &Entry) {
63 return Entry.Cutoff < Percentile;
6464 });
6565 // The required percentile has to be <= one of the percentiles in the
6666 // detailed summary.
336336 // Sorted insertion.
337337 // Enables giving priority to the latest domains during merging.
338338 const int Def = RDA->getReachingDef(mi, RC->getRegister(rx));
339 auto I = llvm::bsearch(Regs, [&](int I) {
340 return Def < RDA->getReachingDef(mi, RC->getRegister(I));
339 auto I = partition_point(Regs, [&](int I) {
340 return RDA->getReachingDef(mi, RC->getRegister(I)) <= Def;
341341 });
342342 Regs.insert(I, rx);
343343 }
543543 // Find the last element in Vec that has a bitsize equal to or smaller than
544544 // the requested bit size.
545545 // That is the element just before the first element that is bigger than Size.
546 auto VecIt = llvm::bsearch(
547 Vec, [=](const SizeAndAction &A) { return Size < A.first; });
548 assert(VecIt != Vec.begin() && "Does Vec not start with size 1?");
549 --VecIt;
550 int VecIdx = VecIt - Vec.begin();
546 auto It = partition_point(
547 Vec, [=](const SizeAndAction &A) { return A.first <= Size; });
548 assert(It != Vec.begin() && "Does Vec not start with size 1?");
549 int VecIdx = It - Vec.begin() - 1;
551550
552551 LegalizeAction Action = Vec[VecIdx].second;
553552 switch (Action) {
114114
115115 uint32_t DWARFDebugAranges::findAddress(uint64_t Address) const {
116116 RangeCollIterator It =
117 llvm::bsearch(Aranges, [=](Range RHS) { return Address < RHS.HighPC(); });
117 partition_point(Aranges, [=](Range R) { return R.HighPC() <= Address; });
118118 if (It != Aranges.end() && It->LowPC <= Address)
119119 return It->CUOffset;
120120 return -1U;
531531 }
532532
533533 FrameEntry *DWARFDebugFrame::getEntryAtOffset(uint64_t Offset) const {
534 auto It = llvm::bsearch(Entries, [=](const std::unique_ptr &E) {
535 return Offset <= E->getOffset();
534 auto It = partition_point(Entries, [=](const std::unique_ptr &E) {
535 return E->getOffset() < Offset;
536536 });
537537 if (It != Entries.end() && (*It)->getOffset() == Offset)
538538 return It->get();
5656
5757 DWARFDebugLoc::LocationList const *
5858 DWARFDebugLoc::getLocationListAtOffset(uint64_t Offset) const {
59 auto It = llvm::bsearch(
60 Locations, [=](const LocationList &L) { return Offset <= L.Offset; });
59 auto It = partition_point(
60 Locations, [=](const LocationList &L) { return L.Offset < Offset; });
6161 if (It != Locations.end() && It->Offset == Offset)
6262 return &(*It);
6363 return nullptr;
211211
212212 DWARFDebugLoclists::LocationList const *
213213 DWARFDebugLoclists::getLocationListAtOffset(uint64_t Offset) const {
214 auto It = llvm::bsearch(
215 Locations, [=](const LocationList &L) { return Offset <= L.Offset; });
214 auto It = partition_point(
215 Locations, [=](const LocationList &L) { return L.Offset < Offset; });
216216 if (It != Locations.end() && It->Offset == Offset)
217217 return &(*It);
218218 return nullptr;
171171 E2->Contributions[InfoColumn].Offset;
172172 });
173173 }
174 auto I = llvm::bsearch(OffsetLookup, [&](Entry *E2) {
175 return Offset < E2->Contributions[InfoColumn].Offset;
174 auto I = partition_point(OffsetLookup, [&](Entry *E2) {
175 return E2->Contributions[InfoColumn].Offset <= Offset;
176176 });
177177 if (I == OffsetLookup.begin())
178178 return nullptr;
462462 DataLayout::findAlignmentLowerBound(AlignTypeEnum AlignType,
463463 uint32_t BitWidth) {
464464 auto Pair = std::make_pair((unsigned)AlignType, BitWidth);
465 return llvm::bsearch(Alignments, [=](const LayoutAlignElem &E) {
466 return Pair <= std::make_pair(E.AlignType, E.TypeBitWidth);
465 return partition_point(Alignments, [=](const LayoutAlignElem &E) {
466 return std::make_pair(E.AlignType, E.TypeBitWidth) < Pair;
467467 });
468468 }
469469
532532 // Drop "llvm." and take the first dotted component. That will be the target
533533 // if this is target specific.
534534 StringRef Target = Name.drop_front(5).split('.').first;
535 auto It = llvm::bsearch(Targets, [=](const IntrinsicTargetInfo &TI) {
536 return Target <= TI.Name;
537 });
535 auto It = partition_point(
536 Targets, [=](const IntrinsicTargetInfo &TI) { return TI.Name < Target; });
538537 // We've either found the target or just fall back to the generic set, which
539538 // is always first.
540539 const auto &TI = It != Targets.end() && It->Name == Target ? *It : Targets[0];
362362
363363 uint64_t InstrProfSymtab::getFunctionHashFromAddress(uint64_t Address) {
364364 finalizeSymtab();
365 auto Result =
366 llvm::bsearch(AddrToMD5Map, [=](std::pair A) {
367 return Address <= A.first;
368 });
365 auto It = partition_point(AddrToMD5Map, [=](std::pair A) {
366 return A.first < Address;
367 });
369368 // Raw function pointer collected by value profiler may be from
370369 // external functions that are not instrumented. They won't have
371370 // mapping data to be used by the deserializer. Force the value to
372371 // be 0 in this case.
373 if (Result != AddrToMD5Map.end() && Result->first == Address)
374 return (uint64_t)Result->second;
372 if (It != AddrToMD5Map.end() && It->first == Address)
373 return (uint64_t)It->second;
375374 return 0;
376375 }
377376
157157 // FMA 231 instructions have an opcode of 0xB6-0xBF
158158 unsigned FormIndex = ((BaseOpcode - 0x90) >> 4) & 0x3;
159159
160 auto I = llvm::bsearch(Table, [=](const X86InstrFMA3Group &Group) {
161 return Opcode <= Group.Opcodes[FormIndex];
160 auto I = partition_point(Table, [=](const X86InstrFMA3Group &Group) {
161 return Group.Opcodes[FormIndex] < Opcode;
162162 });
163163 assert(I != Table.end() && I->Opcodes[FormIndex] == Opcode &&
164164 "Couldn't find FMA3 opcode!");
1313 #include
1414 #include
1515
16 using namespace llvm::MachO;
17
1816 namespace llvm {
1917 namespace MachO {
2018 namespace detail {
2119 template
2220 typename C::iterator addEntry(C &Container, StringRef InstallName) {
23 auto I = llvm::bsearch(Container, [=](const InterfaceFileRef &O) {
24 return InstallName <= O.getInstallName();
21 auto I = partition_point(Container, [=](const InterfaceFileRef &O) {
22 return O.getInstallName() < InstallName;
2523 });
26 if ((I != std::end(Container)) && !(InstallName < I->getInstallName()))
24 if (I != Container.end() && I->getInstallName() == InstallName)
2725 return I;
2826
2927 return Container.emplace(I, InstallName);
4341 }
4442
4543 void InterfaceFile::addUUID(Architecture Arch, StringRef UUID) {
46 auto I =
47 llvm::bsearch(UUIDs, [=](const std::pair &O) {
48 return Arch <= O.first;
49 });
44 auto I = partition_point(UUIDs,
45 [=](const std::pair &O) {
46 return O.first < Arch;
47 });
5048
5149 if (I != UUIDs.end() && Arch == I->first) {
5250 I->second = UUID;
277277 unsigned Alignment, Instruction *Inst) {
278278 int64_t End = Start+Size;
279279
280 range_iterator I = llvm::bsearch(
281 Ranges, [=](const MemsetRange &O) { return Start <= O.End; });
280 range_iterator I = partition_point(
281 Ranges, [=](const MemsetRange &O) { return O.End < Start; });
282282
283283 // We now know that I == E, in which case we didn't find anything to merge
284284 // with, or that Start <= I->End. If End < I->Start or I == E, then we need
17661766 }
17671767
17681768 object::SectionedAddress Front = Seq.front().Address;
1769 auto InsertPoint = llvm::bsearch(
1770 Rows, [=](const DWARFDebugLine::Row &O) { return !(O.Address < Front); });
1769 auto InsertPoint = partition_point(
1770 Rows, [=](const DWARFDebugLine::Row &O) { return O.Address < Front; });
17711771
17721772 // FIXME: this only removes the unneeded end_sequence if the
17731773 // sequences have been inserted in order. Using a global sort like
972972 typedef std::pair MappingSymbolPair;
973973 static char getMappingSymbolKind(ArrayRef MappingSymbols,
974974 uint64_t Address) {
975 auto Sym = bsearch(MappingSymbols, [Address](const MappingSymbolPair &Val) {
976 return Val.first > Address;
977 });
975 auto It =
976 partition_point(MappingSymbols, [Address](const MappingSymbolPair &Val) {
977 return Val.first <= Address;
978 });
978979 // Return zero for any address before the first mapping symbol; this means
979980 // we should use the default disassembly mode, depending on the target.
980 if (Sym == MappingSymbols.begin())
981 if (It == MappingSymbols.begin())
981982 return '\x00';
982 return (Sym - 1)->second;
983 return (It - 1)->second;
983984 }
984985
985986 static uint64_t
11181119 error(ExportEntry.getExportRVA(RVA));
11191120
11201121 uint64_t VA = COFFObj->getImageBase() + RVA;
1121 auto Sec = llvm::bsearch(
1122 SectionAddresses, [VA](const std::pair &RHS) {
1123 return VA < RHS.first;
1122 auto Sec = partition_point(
1123 SectionAddresses, [VA](const std::pair &O) {
1124 return O.first <= VA;
11241125 });
11251126 if (Sec != SectionAddresses.begin()) {
11261127 --Sec;
13771378 // N.B. We don't walk the relocations in the relocatable case yet.
13781379 auto *TargetSectionSymbols = &Symbols;
13791380 if (!Obj->isRelocatableObject()) {
1380 auto It = llvm::bsearch(
1381 auto It = partition_point(
13811382 SectionAddresses,
1382 [=](const std::pair &RHS) {
1383 return Target < RHS.first;
1383 [=](const std::pair &O) {
1384 return O.first <= Target;
13841385 });
13851386 if (It != SectionAddresses.begin()) {
13861387 --It;
13931394 // Find the last symbol in the section whose offset is less than
13941395 // or equal to the target. If there isn't a section that contains
13951396 // the target, find the nearest preceding absolute symbol.
1396 auto TargetSym = llvm::bsearch(
1397 auto TargetSym = partition_point(
13971398 *TargetSectionSymbols,
1398 [=](const std::tuple &RHS) {
1399 return Target < std::get<0>(RHS);
1399 [=](const std::tuple &O) {
1400 return std::get<0>(O) <= Target;
14001401 });
14011402 if (TargetSym == TargetSectionSymbols->begin()) {
14021403 TargetSectionSymbols = &AbsoluteSymbols;
1403 TargetSym = llvm::bsearch(
1404 TargetSym = partition_point(
14041405 AbsoluteSymbols,
1405 [=](const std::tuple &RHS) {
1406 return Target < std::get<0>(RHS);
1406 [=](const std::tuple &O) {
1407 return std::get<0>(O) <= Target;
14071408 });
14081409 }
14091410 if (TargetSym != TargetSectionSymbols->begin()) {
468468 EXPECT_EQ(V1, to_address(V3));
469469 }
470470
471 TEST(STLExtrasTest, bsearch) {
471 TEST(STLExtrasTest, partition_point) {
472472 std::vector V = {1, 3, 5, 7, 9};
473473
474474 // Range version.
475 EXPECT_EQ(V.begin() + 3, bsearch(V, [](unsigned X) { return X >= 7; }));
476 EXPECT_EQ(V.begin(), bsearch(V, [](unsigned X) { return X >= 1; }));
477 EXPECT_EQ(V.end(), bsearch(V, [](unsigned X) { return X >= 50; }));
475 EXPECT_EQ(V.begin() + 3,
476 partition_point(V, [](unsigned X) { return X < 7; }));
477 EXPECT_EQ(V.begin(), partition_point(V, [](unsigned X) { return X < 1; }));
478 EXPECT_EQ(V.end(), partition_point(V, [](unsigned X) { return X < 50; }));
478479 }
479480
480481 } // namespace