[llvm] [Analysis] Avoid repeated hash lookups (NFC) (PR #140691)

Kazu Hirata via llvm-commits llvm-commits at lists.llvm.org
Tue May 20 00:40:08 PDT 2025


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

With this patch, we always update Inserted.  That's OK because we only
read Inserted as shown in this patch.  Without this patch, it's a
write-only variable.


>From 6a769eb67e4d3919a06c39ba54eea74955a056da Mon Sep 17 00:00:00 2001
From: Kazu Hirata <kazu at google.com>
Date: Mon, 19 May 2025 08:07:26 -0700
Subject: [PATCH] [Analysis] Avoid repeated hash lookups (NFC)

With this patch, we always update Inserted.  That's OK because we only
read Inserted as shown in this patch.  Without this patch, it's a
write-only variable.
---
 llvm/lib/Analysis/IRSimilarityIdentifier.cpp | 11 +++++------
 1 file changed, 5 insertions(+), 6 deletions(-)

diff --git a/llvm/lib/Analysis/IRSimilarityIdentifier.cpp b/llvm/lib/Analysis/IRSimilarityIdentifier.cpp
index ffb684d284a94..930b2068b3b0f 100644
--- a/llvm/lib/Analysis/IRSimilarityIdentifier.cpp
+++ b/llvm/lib/Analysis/IRSimilarityIdentifier.cpp
@@ -1306,12 +1306,11 @@ static void findCandidateStructures(
        CandIt != CandEndIt; CandIt++) {
 
     // Determine if it has an assigned structural group already.
-    CandToGroupIt = CandToGroup.find(&*CandIt);
-    if (CandToGroupIt == CandToGroup.end()) {
-      // If not, we assign it one, and add it to our mapping.
-      std::tie(CandToGroupIt, Inserted) =
-          CandToGroup.insert(std::make_pair(&*CandIt, CurrentGroupNum++));
-    }
+    // If not, we assign it one, and add it to our mapping.
+    std::tie(CandToGroupIt, Inserted) =
+        CandToGroup.try_emplace(&*CandIt, CurrentGroupNum);
+    if (Inserted)
+      ++CurrentGroupNum;
 
     // Get the structural group number from the iterator.
     OuterGroupNum = CandToGroupIt->second;



More information about the llvm-commits mailing list