Skip to content

[Utils] Avoid repeated hash lookups (NFC) #130709

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Conversation

kazutakahirata
Copy link
Contributor

No description provided.

@llvmbot
Copy link
Member

llvmbot commented Mar 11, 2025

@llvm/pr-subscribers-llvm-transforms

Author: Kazu Hirata (kazutakahirata)

Changes

Full diff: https://github.com/llvm/llvm-project/pull/130709.diff

1 Files Affected:

  • (modified) llvm/lib/Transforms/Utils/LCSSA.cpp (+8-7)
diff --git a/llvm/lib/Transforms/Utils/LCSSA.cpp b/llvm/lib/Transforms/Utils/LCSSA.cpp
index d76a0c2e4ce75..c3c3cdf50a985 100644
--- a/llvm/lib/Transforms/Utils/LCSSA.cpp
+++ b/llvm/lib/Transforms/Utils/LCSSA.cpp
@@ -99,10 +99,10 @@ formLCSSAForInstructionsImpl(SmallVectorImpl<Instruction *> &Worklist,
     BasicBlock *InstBB = I->getParent();
     Loop *L = LI.getLoopFor(InstBB);
     assert(L && "Instruction belongs to a BB that's not part of a loop");
-    if (!LoopExitBlocks.count(L))
-      L->getExitBlocks(LoopExitBlocks[L]);
-    assert(LoopExitBlocks.count(L));
-    const SmallVectorImpl<BasicBlock *> &ExitBlocks = LoopExitBlocks[L];
+    auto [It, Inserted] = LoopExitBlocks.try_emplace(L);
+    if (Inserted)
+      L->getExitBlocks(It->second);
+    const SmallVectorImpl<BasicBlock *> &ExitBlocks = It->second;
 
     if (ExitBlocks.empty())
       continue;
@@ -389,9 +389,10 @@ static bool formLCSSAImpl(Loop &L, const DominatorTree &DT, const LoopInfo *LI,
   }
 #endif
 
-  if (!LoopExitBlocks.count(&L))
-    L.getExitBlocks(LoopExitBlocks[&L]);
-  const SmallVectorImpl<BasicBlock *> &ExitBlocks = LoopExitBlocks[&L];
+  auto [It, Inserted] = LoopExitBlocks.try_emplace(&L);
+  if (Inserted)
+    L.getExitBlocks(It->second);
+  const SmallVectorImpl<BasicBlock *> &ExitBlocks = It->second;
   if (ExitBlocks.empty())
     return false;
 

@kazutakahirata kazutakahirata merged commit 3339632 into llvm:main Mar 11, 2025
13 checks passed
@kazutakahirata kazutakahirata deleted the cleanup_001_repeated_hash_lookups_llvm_Utils branch March 11, 2025 14:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants