Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged

Conversation

kazutakahirata
Copy link
Contributor

  • 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.

- 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.
@llvmbot llvmbot added mc Machine (object) code llvm:adt labels Jul 17, 2025
@llvmbot
Copy link
Member

llvmbot commented Jul 17, 2025

@llvm/pr-subscribers-llvm-adt

@llvm/pr-subscribers-mc

Author: Kazu Hirata (kazutakahirata)

Changes
  • 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.


Full diff: https://github.com/llvm/llvm-project/pull/149257.diff

2 Files Affected:

  • (modified) llvm/include/llvm/ADT/EquivalenceClasses.h (+4-4)
  • (modified) llvm/lib/MC/MCContext.cpp (+2-3)
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());

@kazutakahirata kazutakahirata merged commit 5775851 into llvm:main Jul 17, 2025
12 checks passed
@kazutakahirata kazutakahirata deleted the cleanup_20250716_try_emplace_llvm branch July 17, 2025 14:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
llvm:adt mc Machine (object) code
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants