diff --git a/bolt/include/bolt/Core/BinaryContext.h b/bolt/include/bolt/Core/BinaryContext.h --- a/bolt/include/bolt/Core/BinaryContext.h +++ b/bolt/include/bolt/Core/BinaryContext.h @@ -229,9 +229,6 @@ /// DWARF line info for CUs. std::map DwarfLineTablesCUMap; - /// Internal helper for removing section name from a lookup table. - void deregisterSectionName(const BinarySection &Section); - public: static Expected> createBinaryContext(const ObjectFile *File, bool IsPIC, diff --git a/bolt/lib/Core/BinaryContext.cpp b/bolt/lib/Core/BinaryContext.cpp --- a/bolt/lib/Core/BinaryContext.cpp +++ b/bolt/lib/Core/BinaryContext.cpp @@ -1959,15 +1959,14 @@ new BinarySection(*this, Name, Data, Size, Alignment, ELFType, ELFFlags)); } -void BinaryContext::deregisterSectionName(const BinarySection &Section) { - auto NameRange = NameToSection.equal_range(Section.getName().str()); - while (NameRange.first != NameRange.second) { - if (NameRange.first->second == &Section) { - NameToSection.erase(NameRange.first); - break; - } - ++NameRange.first; - } +template +static void eraseSection(std::multimap &C, + const BinarySection &Section, const T &Key) { + auto Range = C.equal_range(Key); + auto SectionIt = llvm::find_if( + make_range(Range), [&](const auto &KV) { return KV.second == &Section; }); + if (SectionIt != C.end()) + C.erase(SectionIt); } void BinaryContext::deregisterUnusedSections() { @@ -1982,7 +1981,7 @@ LLVM_DEBUG(dbgs() << "LLVM-DEBUG: deregistering " << Section->getName() << '\n';); - deregisterSectionName(*Section); + eraseSection(NameToSection, *Section, Section->getName().str()); SI = Sections.erase(SI); delete Section; } @@ -1992,16 +1991,9 @@ BinarySection *SectionPtr = &Section; auto Itr = Sections.find(SectionPtr); if (Itr != Sections.end()) { - auto Range = AddressToSection.equal_range(SectionPtr->getAddress()); - while (Range.first != Range.second) { - if (Range.first->second == SectionPtr) { - AddressToSection.erase(Range.first); - break; - } - ++Range.first; - } + eraseSection(AddressToSection, Section, Section.getAddress()); + eraseSection(NameToSection, Section, Section.getName().str()); - deregisterSectionName(*SectionPtr); Sections.erase(Itr); delete SectionPtr; return true; @@ -2015,7 +2007,7 @@ assert(Itr != Sections.end() && "Section must exist to be renamed."); Sections.erase(Itr); - deregisterSectionName(Section); + eraseSection(NameToSection, Section, Section.getName().str()); Section.Name = NewName.str(); Section.setOutputName(Section.Name); diff --git a/bolt/lib/Profile/DataAggregator.cpp b/bolt/lib/Profile/DataAggregator.cpp --- a/bolt/lib/Profile/DataAggregator.cpp +++ b/bolt/lib/Profile/DataAggregator.cpp @@ -1999,14 +1999,11 @@ continue; // Consider only the first mapping of the file for any given PID - bool PIDExists = false; auto Range = GlobalMMapInfo.equal_range(FileMMapInfo.first); - for (auto MI = Range.first; MI != Range.second; ++MI) { - if (MI->second.PID == FileMMapInfo.second.PID) { - PIDExists = true; - break; - } - } + bool PIDExists = llvm::any_of(make_range(Range), [&](const auto &MI) { + return MI.second.PID == FileMMapInfo.second.PID; + }); + if (PIDExists) continue; @@ -2030,8 +2027,7 @@ } auto Range = GlobalMMapInfo.equal_range(NameToUse); - for (auto I = Range.first; I != Range.second; ++I) { - MMapInfo &MMapInfo = I->second; + for (MMapInfo &MMapInfo : make_second_range(make_range(Range))) { if (BC->HasFixedLoadAddress && MMapInfo.MMapAddress) { // Check that the binary mapping matches one of the segments. bool MatchFound = llvm::any_of( diff --git a/bolt/lib/Profile/DataReader.cpp b/bolt/lib/Profile/DataReader.cpp --- a/bolt/lib/Profile/DataReader.cpp +++ b/bolt/lib/Profile/DataReader.cpp @@ -215,9 +215,9 @@ } }; auto Range = std::equal_range(Data.begin(), Data.end(), From, Compare()); - for (auto I = Range.first; I != Range.second; ++I) - if (I->From.Name != I->To.Name) - return *I; + for (const auto &RI : llvm::make_range(Range)) + if (RI.From.Name != RI.To.Name) + return RI; return make_error_code(llvm::errc::invalid_argument); }