[llvm-branch-commits] [llvm] PeepholeOpt: Avoid double map lookup (PR #124531)

Matt Arsenault via llvm-branch-commits llvm-branch-commits at lists.llvm.org
Wed Jan 29 19:47:25 PST 2025


https://github.com/arsenm updated https://github.com/llvm/llvm-project/pull/124531

>From 7a17a505d94807e641283f9fcf4c454f81ab6d6d Mon Sep 17 00:00:00 2001
From: Matt Arsenault <Matthew.Arsenault at amd.com>
Date: Mon, 27 Jan 2025 18:18:39 +0700
Subject: [PATCH 1/3] PeepholeOpt: Avoid double map lookup

---
 llvm/lib/CodeGen/PeepholeOptimizer.cpp | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)

diff --git a/llvm/lib/CodeGen/PeepholeOptimizer.cpp b/llvm/lib/CodeGen/PeepholeOptimizer.cpp
index bf450e3af0deee..89753a06d12b28 100644
--- a/llvm/lib/CodeGen/PeepholeOptimizer.cpp
+++ b/llvm/lib/CodeGen/PeepholeOptimizer.cpp
@@ -1035,8 +1035,10 @@ bool PeepholeOptimizer::findNextSource(RegSubRegPair RegSubReg,
         return false;
 
       // Insert the Def -> Use entry for the recently found source.
-      ValueTrackerResult CurSrcRes = RewriteMap.lookup(CurSrcPair);
-      if (CurSrcRes.isValid()) {
+      auto [InsertPt, WasInserted] = RewriteMap.try_emplace(CurSrcPair, Res);
+
+      ValueTrackerResult CurSrcRes = InsertPt->second;
+      if (!WasInserted) {
         assert(CurSrcRes == Res && "ValueTrackerResult found must match");
         // An existent entry with multiple sources is a PHI cycle we must avoid.
         // Otherwise it's an entry with a valid next source we already found.
@@ -1047,7 +1049,6 @@ bool PeepholeOptimizer::findNextSource(RegSubRegPair RegSubReg,
         }
         break;
       }
-      RewriteMap.insert(std::make_pair(CurSrcPair, Res));
 
       // ValueTrackerResult usually have one source unless it's the result from
       // a PHI instruction. Add the found PHI edges to be looked up further.

>From edc91580b603a503f01bf54e36d706ea28a35fa0 Mon Sep 17 00:00:00 2001
From: Matt Arsenault <Matthew.Arsenault at amd.com>
Date: Mon, 27 Jan 2025 20:22:36 +0700
Subject: [PATCH 2/3] Sink

---
 llvm/lib/CodeGen/PeepholeOptimizer.cpp | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/llvm/lib/CodeGen/PeepholeOptimizer.cpp b/llvm/lib/CodeGen/PeepholeOptimizer.cpp
index 89753a06d12b28..ce475765e50692 100644
--- a/llvm/lib/CodeGen/PeepholeOptimizer.cpp
+++ b/llvm/lib/CodeGen/PeepholeOptimizer.cpp
@@ -1037,8 +1037,9 @@ bool PeepholeOptimizer::findNextSource(RegSubRegPair RegSubReg,
       // Insert the Def -> Use entry for the recently found source.
       auto [InsertPt, WasInserted] = RewriteMap.try_emplace(CurSrcPair, Res);
 
-      ValueTrackerResult CurSrcRes = InsertPt->second;
       if (!WasInserted) {
+        ValueTrackerResult CurSrcRes = InsertPt->second;
+
         assert(CurSrcRes == Res && "ValueTrackerResult found must match");
         // An existent entry with multiple sources is a PHI cycle we must avoid.
         // Otherwise it's an entry with a valid next source we already found.

>From 159d5773ca2c9dea5dfc3b153457154c9bb3601b Mon Sep 17 00:00:00 2001
From: Matt Arsenault <Matthew.Arsenault at amd.com>
Date: Tue, 28 Jan 2025 13:36:54 +0700
Subject: [PATCH 3/3] Update llvm/lib/CodeGen/PeepholeOptimizer.cpp

Co-authored-by: Kazu Hirata <kazu at google.com>
---
 llvm/lib/CodeGen/PeepholeOptimizer.cpp | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/llvm/lib/CodeGen/PeepholeOptimizer.cpp b/llvm/lib/CodeGen/PeepholeOptimizer.cpp
index ce475765e50692..a8a40cdb915ed5 100644
--- a/llvm/lib/CodeGen/PeepholeOptimizer.cpp
+++ b/llvm/lib/CodeGen/PeepholeOptimizer.cpp
@@ -1038,7 +1038,7 @@ bool PeepholeOptimizer::findNextSource(RegSubRegPair RegSubReg,
       auto [InsertPt, WasInserted] = RewriteMap.try_emplace(CurSrcPair, Res);
 
       if (!WasInserted) {
-        ValueTrackerResult CurSrcRes = InsertPt->second;
+        const ValueTrackerResult &CurSrcRes = InsertPt->second;
 
         assert(CurSrcRes == Res && "ValueTrackerResult found must match");
         // An existent entry with multiple sources is a PHI cycle we must avoid.



More information about the llvm-branch-commits mailing list