-
Notifications
You must be signed in to change notification settings - Fork 14.4k
[llvm] Use *Map::try_emplace (NFC) #143002
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
[llvm] Use *Map::try_emplace (NFC) #143002
Conversation
kazutakahirata
commented
Jun 5, 2025
- 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.
- 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/pr-subscribers-debuginfo @llvm/pr-subscribers-llvm-adt Author: Kazu Hirata (kazutakahirata) Changes
Full diff: https://github.com/llvm/llvm-project/pull/143002.diff 4 Files Affected:
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;
|
@llvm/pr-subscribers-mc Author: Kazu Hirata (kazutakahirata) Changes
Full diff: https://github.com/llvm/llvm-project/pull/143002.diff 4 Files Affected:
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;
|
@llvm/pr-subscribers-backend-webassembly Author: Kazu Hirata (kazutakahirata) Changes
Full diff: https://github.com/llvm/llvm-project/pull/143002.diff 4 Files Affected:
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;
|
- 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.
- 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.