Skip to content
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

[Transforms] Avoid repeated hash lookups (NFC) #131556

Conversation

kazutakahirata
Copy link
Contributor

No description provided.

@llvmbot
Copy link
Member

llvmbot commented Mar 17, 2025

@llvm/pr-subscribers-llvm-transforms

Author: Kazu Hirata (kazutakahirata)

Changes

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

1 Files Affected:

  • (modified) llvm/lib/Transforms/Utils/BasicBlockUtils.cpp (+8-8)
diff --git a/llvm/lib/Transforms/Utils/BasicBlockUtils.cpp b/llvm/lib/Transforms/Utils/BasicBlockUtils.cpp
index f81420460f08d..04b2d1a38ab60 100644
--- a/llvm/lib/Transforms/Utils/BasicBlockUtils.cpp
+++ b/llvm/lib/Transforms/Utils/BasicBlockUtils.cpp
@@ -511,7 +511,7 @@ DbgVariableRecordsRemoveRedundantDbgInstrsUsingForwardScan(BasicBlock *BB) {
         continue;
       DebugVariable Key(DVR.getVariable(), std::nullopt,
                         DVR.getDebugLoc()->getInlinedAt());
-      auto VMI = VariableMap.find(Key);
+      auto [VMI, Inserted] = VariableMap.try_emplace(Key);
       // A dbg.assign with no linked instructions can be treated like a
       // dbg.value (i.e. can be deleted).
       bool IsDbgValueKind =
@@ -520,12 +520,12 @@ DbgVariableRecordsRemoveRedundantDbgInstrsUsingForwardScan(BasicBlock *BB) {
       // Update the map if we found a new value/expression describing the
       // variable, or if the variable wasn't mapped already.
       SmallVector<Value *, 4> Values(DVR.location_ops());
-      if (VMI == VariableMap.end() || VMI->second.first != Values ||
+      if (Inserted || VMI->second.first != Values ||
           VMI->second.second != DVR.getExpression()) {
         if (IsDbgValueKind)
-          VariableMap[Key] = {Values, DVR.getExpression()};
+          VMI->second = {Values, DVR.getExpression()};
         else
-          VariableMap[Key] = {Values, nullptr};
+          VMI->second = {Values, nullptr};
         continue;
       }
       // Don't delete dbg.assign intrinsics that are linked to instructions.
@@ -591,7 +591,7 @@ static bool removeRedundantDbgInstrsUsingForwardScan(BasicBlock *BB) {
     if (DbgValueInst *DVI = dyn_cast<DbgValueInst>(&I)) {
       DebugVariable Key(DVI->getVariable(), std::nullopt,
                         DVI->getDebugLoc()->getInlinedAt());
-      auto VMI = VariableMap.find(Key);
+      auto [VMI, Inserted] = VariableMap.try_emplace(Key);
       auto *DAI = dyn_cast<DbgAssignIntrinsic>(DVI);
       // A dbg.assign with no linked instructions can be treated like a
       // dbg.value (i.e. can be deleted).
@@ -600,15 +600,15 @@ static bool removeRedundantDbgInstrsUsingForwardScan(BasicBlock *BB) {
       // Update the map if we found a new value/expression describing the
       // variable, or if the variable wasn't mapped already.
       SmallVector<Value *, 4> Values(DVI->getValues());
-      if (VMI == VariableMap.end() || VMI->second.first != Values ||
+      if (Inserted || VMI->second.first != Values ||
           VMI->second.second != DVI->getExpression()) {
         // Use a sentinel value (nullptr) for the DIExpression when we see a
         // linked dbg.assign so that the next debug intrinsic will never match
         // it (i.e. always treat linked dbg.assigns as if they're unique).
         if (IsDbgValueKind)
-          VariableMap[Key] = {Values, DVI->getExpression()};
+          VMI->second = {Values, DVI->getExpression()};
         else
-          VariableMap[Key] = {Values, nullptr};
+          VMI->second = {Values, nullptr};
         continue;
       }
 

@kazutakahirata kazutakahirata merged commit 9455df9 into llvm:main Mar 17, 2025
13 checks passed
@kazutakahirata kazutakahirata deleted the cleanup_001_repeated_hash_lookups_llvm_Utils branch March 17, 2025 14:43
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