[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 03:37:09 PST 2025


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

None

>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] 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.



More information about the llvm-branch-commits mailing list