[llvm] [GVNSink] Avoid repeated hash lookups (NFC) (PR #113023)

Kazu Hirata via llvm-commits llvm-commits at lists.llvm.org
Sun Oct 20 09:09:56 PDT 2024


https://github.com/kazutakahirata updated https://github.com/llvm/llvm-project/pull/113023

>From 076342e7ad518b7eee5a74aa89028b0c061aac15 Mon Sep 17 00:00:00 2001
From: Kazu Hirata <kazu at google.com>
Date: Fri, 18 Oct 2024 09:36:53 -0700
Subject: [PATCH 1/3] [GVNSink] Avoid repeated hash lookups (NFC)

---
 llvm/lib/Transforms/Scalar/GVNSink.cpp | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/llvm/lib/Transforms/Scalar/GVNSink.cpp b/llvm/lib/Transforms/Scalar/GVNSink.cpp
index 3dfa2dd9df27f5..42b44725240f29 100644
--- a/llvm/lib/Transforms/Scalar/GVNSink.cpp
+++ b/llvm/lib/Transforms/Scalar/GVNSink.cpp
@@ -535,12 +535,12 @@ class ValueTable {
     uint32_t e = ExpressionNumbering[exp];
     if (!e) {
       hash_code H = exp->getHashValue([=](Value *V) { return lookupOrAdd(V); });
-      auto I = HashNumbering.find(H);
-      if (I != HashNumbering.end()) {
+      auto [I, Inserted] = HashNumbering.try_emplace(H);
+      if (!Inserted) {
         e = I->second;
       } else {
         e = nextValueNumber++;
-        HashNumbering[H] = e;
+        I->second = e;
         ExpressionNumbering[exp] = e;
       }
     }

>From e1bedbf1671de0d981f447ca7e15a4c4bdec30c5 Mon Sep 17 00:00:00 2001
From: Kazu Hirata <kazu at google.com>
Date: Sat, 19 Oct 2024 15:01:04 -0700
Subject: [PATCH 2/3] Pass nextValueNumber to try_emplace.

---
 llvm/lib/Transforms/Scalar/GVNSink.cpp | 12 ++++--------
 1 file changed, 4 insertions(+), 8 deletions(-)

diff --git a/llvm/lib/Transforms/Scalar/GVNSink.cpp b/llvm/lib/Transforms/Scalar/GVNSink.cpp
index 42b44725240f29..65c537e78b6329 100644
--- a/llvm/lib/Transforms/Scalar/GVNSink.cpp
+++ b/llvm/lib/Transforms/Scalar/GVNSink.cpp
@@ -535,14 +535,10 @@ class ValueTable {
     uint32_t e = ExpressionNumbering[exp];
     if (!e) {
       hash_code H = exp->getHashValue([=](Value *V) { return lookupOrAdd(V); });
-      auto [I, Inserted] = HashNumbering.try_emplace(H);
-      if (!Inserted) {
-        e = I->second;
-      } else {
-        e = nextValueNumber++;
-        I->second = e;
-        ExpressionNumbering[exp] = e;
-      }
+      auto [I, Inserted] = HashNumbering.try_emplace(H, nextValueNumber + 1);
+      e = I->second;
+      if (Inserted)
+        ExpressionNumbering[exp] = ++nextValueNumber;
     }
     ValueNumbering[V] = e;
     return e;

>From a8181bcb2315220a5c942aec9e00e56c77b144cd Mon Sep 17 00:00:00 2001
From: Kazu Hirata <kazu at google.com>
Date: Sat, 19 Oct 2024 18:01:51 -0700
Subject: [PATCH 3/3] Fix the pre/post increment bug.

---
 llvm/lib/Transforms/Scalar/GVNSink.cpp | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/llvm/lib/Transforms/Scalar/GVNSink.cpp b/llvm/lib/Transforms/Scalar/GVNSink.cpp
index 65c537e78b6329..59dfe33d8003ec 100644
--- a/llvm/lib/Transforms/Scalar/GVNSink.cpp
+++ b/llvm/lib/Transforms/Scalar/GVNSink.cpp
@@ -535,10 +535,10 @@ class ValueTable {
     uint32_t e = ExpressionNumbering[exp];
     if (!e) {
       hash_code H = exp->getHashValue([=](Value *V) { return lookupOrAdd(V); });
-      auto [I, Inserted] = HashNumbering.try_emplace(H, nextValueNumber + 1);
+      auto [I, Inserted] = HashNumbering.try_emplace(H, nextValueNumber);
       e = I->second;
       if (Inserted)
-        ExpressionNumbering[exp] = ++nextValueNumber;
+        ExpressionNumbering[exp] = nextValueNumber++;
     }
     ValueNumbering[V] = e;
     return e;



More information about the llvm-commits mailing list