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

Matt Arsenault via llvm-commits llvm-commits at lists.llvm.org
Thu Jan 30 05:56:49 PST 2025


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

>From 5d52ad8678e906db9292f03f1b51c1ee5452dd52 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 bf450e3af0deee2..89753a06d12b28b 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 4b8c484070196bc8afd92ca47ad8325ec1cc49ba 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 89753a06d12b28b..ce475765e50692f 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 2def9c1c7af2462df5f7125fb240fc8017396028 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 ce475765e50692f..a8a40cdb915ed56 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-commits mailing list