[llvm] 7108dec - [MC] Use *Map::try_emplace (NFC) (#140397)

via llvm-commits llvm-commits at lists.llvm.org
Sat May 17 14:28:23 PDT 2025


Author: Kazu Hirata
Date: 2025-05-17T14:28:19-07:00
New Revision: 7108deca41f1ef05e399287bb5b37301e484123d

URL: https://github.com/llvm/llvm-project/commit/7108deca41f1ef05e399287bb5b37301e484123d
DIFF: https://github.com/llvm/llvm-project/commit/7108deca41f1ef05e399287bb5b37301e484123d.diff

LOG: [MC] Use *Map::try_emplace (NFC) (#140397)

try_emplace with is much shorter and simpler if we are
default-constructing the value.

While I'm at it, this patch uses structured bindings to receive the
return value from try_emplace.

Added: 
    

Modified: 
    llvm/lib/MC/MCContext.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/lib/MC/MCContext.cpp b/llvm/lib/MC/MCContext.cpp
index cda1276497b51..dc92acafb25d2 100644
--- a/llvm/lib/MC/MCContext.cpp
+++ b/llvm/lib/MC/MCContext.cpp
@@ -619,16 +619,16 @@ MCSectionELF *MCContext::getELFSection(const Twine &Section, unsigned Type,
       Buffer.append(LinkedToSym->getName());
     support::endian::write(Buffer, UniqueID, endianness::native);
     StringRef UniqueMapKey = StringRef(Buffer);
-    EntryNewPair = ELFUniquingMap.insert(std::make_pair(UniqueMapKey, nullptr));
+    EntryNewPair = ELFUniquingMap.try_emplace(UniqueMapKey);
   } else if (!Section.isSingleStringRef()) {
     SmallString<128> Buffer;
     StringRef UniqueMapKey = Section.toStringRef(Buffer);
     SectionLen = UniqueMapKey.size();
-    EntryNewPair = ELFUniquingMap.insert(std::make_pair(UniqueMapKey, nullptr));
+    EntryNewPair = ELFUniquingMap.try_emplace(UniqueMapKey);
   } else {
     StringRef UniqueMapKey = Section.getSingleStringRef();
     SectionLen = UniqueMapKey.size();
-    EntryNewPair = ELFUniquingMap.insert(std::make_pair(UniqueMapKey, nullptr));
+    EntryNewPair = ELFUniquingMap.try_emplace(UniqueMapKey);
   }
 
   if (!EntryNewPair.second)
@@ -696,10 +696,8 @@ MCSectionGOFF *MCContext::getGOFFSection(StringRef Section, SectionKind Kind,
                                          MCSection *Parent,
                                          uint32_t Subsection) {
   // Do the lookup. If we don't have a hit, return a new section.
-  auto IterBool =
-      GOFFUniquingMap.insert(std::make_pair(Section.str(), nullptr));
-  auto Iter = IterBool.first;
-  if (!IterBool.second)
+  auto [Iter, Inserted] = GOFFUniquingMap.try_emplace(Section.str());
+  if (!Inserted)
     return Iter->second;
 
   StringRef CachedName = Iter->first;
@@ -731,9 +729,8 @@ MCSectionCOFF *MCContext::getCOFFSection(StringRef Section,
 
   // Do the lookup, if we have a hit, return it.
   COFFSectionKey T{Section, COMDATSymName, Selection, UniqueID};
-  auto IterBool = COFFUniquingMap.insert(std::make_pair(T, nullptr));
-  auto Iter = IterBool.first;
-  if (!IterBool.second)
+  auto [Iter, Inserted] = COFFUniquingMap.try_emplace(T);
+  if (!Inserted)
     return Iter->second;
 
   StringRef CachedName = Iter->first.SectionName;


        


More information about the llvm-commits mailing list