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

Matt Arsenault via llvm-branch-commits llvm-branch-commits at lists.llvm.org
Mon Jan 27 05:23:06 PST 2025


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

>From e3277459d5d9db21c5ab5af7ff885c035edbfa49 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/2] 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 2540e06cc37cf9f05052b063a7f543b170ecadf1 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/2] 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.



More information about the llvm-branch-commits mailing list