[llvm] [MC] Use *Map::try_emplace (NFC) (PR #140397)
Kazu Hirata via llvm-commits
llvm-commits at lists.llvm.org
Sat May 17 12:35:19 PDT 2025
https://github.com/kazutakahirata created https://github.com/llvm/llvm-project/pull/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.
>From affe3cef2d85406206b7c7dd680a22b9f3cc7e10 Mon Sep 17 00:00:00 2001
From: Kazu Hirata <kazu at google.com>
Date: Sat, 17 May 2025 12:29:30 -0700
Subject: [PATCH] [MC] Use *Map::try_emplace (NFC)
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.
---
llvm/lib/MC/MCContext.cpp | 17 +++++++----------
1 file changed, 7 insertions(+), 10 deletions(-)
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