[llvm] 5775851 - [llvm] Use *Map::try_emplace (NFC) (#149257)

via llvm-commits llvm-commits at lists.llvm.org
Thu Jul 17 07:23:11 PDT 2025


Author: Kazu Hirata
Date: 2025-07-17T07:23:07-07:00
New Revision: 577585198637fc2ced2a4fdf20f91c58fb74c717

URL: https://github.com/llvm/llvm-project/commit/577585198637fc2ced2a4fdf20f91c58fb74c717
DIFF: https://github.com/llvm/llvm-project/commit/577585198637fc2ced2a4fdf20f91c58fb74c717.diff

LOG: [llvm] Use *Map::try_emplace (NFC) (#149257)

- try_emplace(Key) is shorter than insert({Key, nullptr}).
- try_emplace performs value initialization without value parameters.
- We overwrite values on successful insertion anyway.

While we are at it, this patch simplifies the code with structured
binding.

Added: 
    

Modified: 
    llvm/include/llvm/ADT/EquivalenceClasses.h
    llvm/lib/MC/MCContext.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/include/llvm/ADT/EquivalenceClasses.h b/llvm/include/llvm/ADT/EquivalenceClasses.h
index b1009f8b49992..1a2331c1a0322 100644
--- a/llvm/include/llvm/ADT/EquivalenceClasses.h
+++ b/llvm/include/llvm/ADT/EquivalenceClasses.h
@@ -218,12 +218,12 @@ template <class ElemTy> class EquivalenceClasses {
   /// insert - Insert a new value into the union/find set, ignoring the request
   /// if the value already exists.
   const ECValue &insert(const ElemTy &Data) {
-    auto I = TheMapping.insert({Data, nullptr});
-    if (!I.second)
-      return *I.first->second;
+    auto [I, Inserted] = TheMapping.try_emplace(Data);
+    if (!Inserted)
+      return *I->second;
 
     auto *ECV = new (ECValueAllocator) ECValue(Data);
-    I.first->second = ECV;
+    I->second = ECV;
     Members.push_back(ECV);
     return *ECV;
   }

diff  --git a/llvm/lib/MC/MCContext.cpp b/llvm/lib/MC/MCContext.cpp
index 070be621a4b2c..12b3fbab8fb8f 100644
--- a/llvm/lib/MC/MCContext.cpp
+++ b/llvm/lib/MC/MCContext.cpp
@@ -734,9 +734,8 @@ MCSectionGOFF *MCContext::getGOFFSection(SectionKind Kind, StringRef Name,
       UniqueName.append("/").append(P->getName());
   }
   // Do the lookup. If we don't have a hit, return a new section.
-  auto IterBool = GOFFUniquingMap.insert(std::make_pair(UniqueName, nullptr));
-  auto Iter = IterBool.first;
-  if (!IterBool.second)
+  auto [Iter, Inserted] = GOFFUniquingMap.try_emplace(UniqueName);
+  if (!Inserted)
     return Iter->second;
 
   StringRef CachedName = StringRef(Iter->first.c_str(), Name.size());


        


More information about the llvm-commits mailing list