[llvm] [WebAssembly] Avoid repeated hash lookups (NFC) (PR #127960)

Kazu Hirata via llvm-commits llvm-commits at lists.llvm.org
Wed Feb 19 21:22:02 PST 2025


https://github.com/kazutakahirata created https://github.com/llvm/llvm-project/pull/127960

None

>From f78eb73a293acce0d6bd5ad6e5d1488abcba97ec Mon Sep 17 00:00:00 2001
From: Kazu Hirata <kazu at google.com>
Date: Wed, 19 Feb 2025 08:27:14 -0800
Subject: [PATCH] [WebAssembly] Avoid repeated hash lookups (NFC)

---
 .../WebAssembly/WebAssemblySortRegion.cpp      | 18 +++++++++---------
 1 file changed, 9 insertions(+), 9 deletions(-)

diff --git a/llvm/lib/Target/WebAssembly/WebAssemblySortRegion.cpp b/llvm/lib/Target/WebAssembly/WebAssemblySortRegion.cpp
index cd84e68aed140..0469fbf15b251 100644
--- a/llvm/lib/Target/WebAssembly/WebAssemblySortRegion.cpp
+++ b/llvm/lib/Target/WebAssembly/WebAssemblySortRegion.cpp
@@ -28,17 +28,17 @@ const SortRegion *SortRegionInfo::getRegionFor(const MachineBasicBlock *MBB) {
   // WE->contains(ML->getHeader()), but not ML->contains(WE->getHeader()).
   if ((ML && !WE) || (ML && WE && WE->contains(ML->getHeader()))) {
     // If the smallest region containing MBB is a loop
-    if (LoopMap.count(ML))
-      return LoopMap[ML].get();
-    LoopMap[ML] = std::make_unique<ConcreteSortRegion<MachineLoop>>(ML);
-    return LoopMap[ML].get();
+    auto [It, Inserted] = LoopMap.try_emplace(ML);
+    if (Inserted)
+      It->second = std::make_unique<ConcreteSortRegion<MachineLoop>>(ML);
+    return It->second.get();
   } else {
     // If the smallest region containing MBB is an exception
-    if (ExceptionMap.count(WE))
-      return ExceptionMap[WE].get();
-    ExceptionMap[WE] =
-        std::make_unique<ConcreteSortRegion<WebAssemblyException>>(WE);
-    return ExceptionMap[WE].get();
+    auto [It, Inserted] = ExceptionMap.try_emplace(WE);
+    if (Inserted)
+      It->second =
+          std::make_unique<ConcreteSortRegion<WebAssemblyException>>(WE);
+    return It->second.get();
   }
 }
 



More information about the llvm-commits mailing list