Skip to content

Commit 87e074a

Browse files
kazutakahiratarorth
authored andcommitted
[llvm] Use *Map::try_emplace (NFC) (llvm#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.
1 parent 5f4af04 commit 87e074a

File tree

4 files changed

+6
-7
lines changed

4 files changed

+6
-7
lines changed

llvm/include/llvm/ADT/MapVector.h

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,7 @@ class MapVector {
9999
}
100100

101101
ValueT &operator[](const KeyT &Key) {
102-
std::pair<KeyT, typename MapType::mapped_type> Pair = std::make_pair(Key, 0);
103-
std::pair<typename MapType::iterator, bool> Result = Map.insert(Pair);
102+
std::pair<typename MapType::iterator, bool> Result = Map.try_emplace(Key);
104103
auto &I = Result.first->second;
105104
if (Result.second) {
106105
Vector.push_back(std::make_pair(Key, ValueT()));
@@ -119,7 +118,7 @@ class MapVector {
119118

120119
template <typename... Ts>
121120
std::pair<iterator, bool> try_emplace(const KeyT &Key, Ts &&...Args) {
122-
auto [It, Inserted] = Map.insert(std::make_pair(Key, 0));
121+
auto [It, Inserted] = Map.try_emplace(Key);
123122
if (Inserted) {
124123
It->second = Vector.size();
125124
Vector.emplace_back(std::piecewise_construct, std::forward_as_tuple(Key),
@@ -130,7 +129,7 @@ class MapVector {
130129
}
131130
template <typename... Ts>
132131
std::pair<iterator, bool> try_emplace(KeyT &&Key, Ts &&...Args) {
133-
auto [It, Inserted] = Map.insert(std::make_pair(Key, 0));
132+
auto [It, Inserted] = Map.try_emplace(Key);
134133
if (Inserted) {
135134
It->second = Vector.size();
136135
Vector.emplace_back(std::piecewise_construct,

llvm/lib/CodeGen/AsmPrinter/DwarfDebug.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3881,7 +3881,7 @@ void DwarfDebug::addDwarfTypeUnitType(DwarfCompileUnit &CU,
38813881
if (!TypeUnitsUnderConstruction.empty() && AddrPool.hasBeenUsed())
38823882
return;
38833883

3884-
auto Ins = TypeSignatures.insert(std::make_pair(CTy, 0));
3884+
auto Ins = TypeSignatures.try_emplace(CTy);
38853885
if (!Ins.second) {
38863886
CU.addDIETypeSignature(RefDie, Ins.first->second);
38873887
return;

llvm/lib/MC/StringTableBuilder.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ size_t StringTableBuilder::add(CachedHashStringRef S) {
204204
assert(S.size() > COFF::NameSize && "Short string in COFF string table!");
205205

206206
assert(!isFinalized());
207-
auto P = StringIndexMap.insert(std::make_pair(S, 0));
207+
auto P = StringIndexMap.try_emplace(S);
208208
if (P.second) {
209209
size_t Start = alignTo(Size, Alignment);
210210
P.first->second = Start;

llvm/lib/Target/WebAssembly/WebAssemblyFixIrreducibleControlFlow.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -375,7 +375,7 @@ void WebAssemblyFixIrreducibleControlFlow::makeSingleEntryLoop(
375375
// add them as successors.
376376
DenseMap<MachineBasicBlock *, unsigned> Indices;
377377
for (auto *Entry : SortedEntries) {
378-
auto Pair = Indices.insert(std::make_pair(Entry, 0));
378+
auto Pair = Indices.try_emplace(Entry);
379379
assert(Pair.second);
380380

381381
unsigned Index = MIB.getInstr()->getNumExplicitOperands() - 1;

0 commit comments

Comments
 (0)