[llvm] [llvm] Use *Map::try_emplace (NFC) (PR #143002)
Kazu Hirata via llvm-commits
llvm-commits at lists.llvm.org
Thu Jun 5 09:23:40 PDT 2025
https://github.com/kazutakahirata created https://github.com/llvm/llvm-project/pull/143002
- try_emplace(Key) is shorter than insert(std::make_pair(Key, 0)).
- try_emplace performs value initialization without value parameters.
- We overwrite values on successful insertion anyway.
>From 419acde65ba127c77d39402538459e6f52558e89 Mon Sep 17 00:00:00 2001
From: Kazu Hirata <kazu at google.com>
Date: Thu, 5 Jun 2025 08:44:22 -0700
Subject: [PATCH] [llvm] Use *Map::try_emplace (NFC)
- try_emplace(Key) is shorter than insert(std::make_pair(Key, 0)).
- try_emplace performs value initialization without value parameters.
- We overwrite values on successful insertion anyway.
---
llvm/include/llvm/ADT/MapVector.h | 7 +++----
llvm/lib/CodeGen/AsmPrinter/DwarfDebug.cpp | 2 +-
llvm/lib/MC/StringTableBuilder.cpp | 2 +-
.../WebAssembly/WebAssemblyFixIrreducibleControlFlow.cpp | 2 +-
4 files changed, 6 insertions(+), 7 deletions(-)
diff --git a/llvm/include/llvm/ADT/MapVector.h b/llvm/include/llvm/ADT/MapVector.h
index adbcda116dd56..015605fd98dff 100644
--- a/llvm/include/llvm/ADT/MapVector.h
+++ b/llvm/include/llvm/ADT/MapVector.h
@@ -99,8 +99,7 @@ class MapVector {
}
ValueT &operator[](const KeyT &Key) {
- std::pair<KeyT, typename MapType::mapped_type> Pair = std::make_pair(Key, 0);
- std::pair<typename MapType::iterator, bool> Result = Map.insert(Pair);
+ std::pair<typename MapType::iterator, bool> Result = Map.try_emplace(Key);
auto &I = Result.first->second;
if (Result.second) {
Vector.push_back(std::make_pair(Key, ValueT()));
@@ -119,7 +118,7 @@ class MapVector {
template <typename... Ts>
std::pair<iterator, bool> try_emplace(const KeyT &Key, Ts &&...Args) {
- auto [It, Inserted] = Map.insert(std::make_pair(Key, 0));
+ auto [It, Inserted] = Map.try_emplace(Key);
if (Inserted) {
It->second = Vector.size();
Vector.emplace_back(std::piecewise_construct, std::forward_as_tuple(Key),
@@ -130,7 +129,7 @@ class MapVector {
}
template <typename... Ts>
std::pair<iterator, bool> try_emplace(KeyT &&Key, Ts &&...Args) {
- auto [It, Inserted] = Map.insert(std::make_pair(Key, 0));
+ auto [It, Inserted] = Map.try_emplace(Key);
if (Inserted) {
It->second = Vector.size();
Vector.emplace_back(std::piecewise_construct,
diff --git a/llvm/lib/CodeGen/AsmPrinter/DwarfDebug.cpp b/llvm/lib/CodeGen/AsmPrinter/DwarfDebug.cpp
index f50d53eaaafca..5fb74a016a75e 100644
--- a/llvm/lib/CodeGen/AsmPrinter/DwarfDebug.cpp
+++ b/llvm/lib/CodeGen/AsmPrinter/DwarfDebug.cpp
@@ -3881,7 +3881,7 @@ void DwarfDebug::addDwarfTypeUnitType(DwarfCompileUnit &CU,
if (!TypeUnitsUnderConstruction.empty() && AddrPool.hasBeenUsed())
return;
- auto Ins = TypeSignatures.insert(std::make_pair(CTy, 0));
+ auto Ins = TypeSignatures.try_emplace(CTy);
if (!Ins.second) {
CU.addDIETypeSignature(RefDie, Ins.first->second);
return;
diff --git a/llvm/lib/MC/StringTableBuilder.cpp b/llvm/lib/MC/StringTableBuilder.cpp
index df316bae98cea..7accdc2a9e774 100644
--- a/llvm/lib/MC/StringTableBuilder.cpp
+++ b/llvm/lib/MC/StringTableBuilder.cpp
@@ -204,7 +204,7 @@ size_t StringTableBuilder::add(CachedHashStringRef S) {
assert(S.size() > COFF::NameSize && "Short string in COFF string table!");
assert(!isFinalized());
- auto P = StringIndexMap.insert(std::make_pair(S, 0));
+ auto P = StringIndexMap.try_emplace(S);
if (P.second) {
size_t Start = alignTo(Size, Alignment);
P.first->second = Start;
diff --git a/llvm/lib/Target/WebAssembly/WebAssemblyFixIrreducibleControlFlow.cpp b/llvm/lib/Target/WebAssembly/WebAssemblyFixIrreducibleControlFlow.cpp
index 6c46673c36bf0..07171d472dc2d 100644
--- a/llvm/lib/Target/WebAssembly/WebAssemblyFixIrreducibleControlFlow.cpp
+++ b/llvm/lib/Target/WebAssembly/WebAssemblyFixIrreducibleControlFlow.cpp
@@ -375,7 +375,7 @@ void WebAssemblyFixIrreducibleControlFlow::makeSingleEntryLoop(
// add them as successors.
DenseMap<MachineBasicBlock *, unsigned> Indices;
for (auto *Entry : SortedEntries) {
- auto Pair = Indices.insert(std::make_pair(Entry, 0));
+ auto Pair = Indices.try_emplace(Entry);
assert(Pair.second);
unsigned Index = MIB.getInstr()->getNumExplicitOperands() - 1;
More information about the llvm-commits
mailing list