Skip to content

Conversation

@kazutakahirata
Copy link
Contributor

FirstSpecialInsts caches the first special instruction for each basic
block. This patch "inlines" fill into getFirstSpecialInstruction, the
sole caller, to eliminate repeated hash lookups.

FirstSpecialInsts caches the first special instruction for each basic
block.  This patch "inlines" fill into getFirstSpecialInstruction, the
sole caller, to eliminate repeated hash lookups.
@kazutakahirata kazutakahirata requested a review from nikic March 23, 2025 03:44
@llvmbot llvmbot added the llvm:analysis Includes value tracking, cost tables and constant folding label Mar 23, 2025
@llvmbot
Copy link
Member

llvmbot commented Mar 23, 2025

@llvm/pr-subscribers-llvm-analysis

Author: Kazu Hirata (kazutakahirata)

Changes

FirstSpecialInsts caches the first special instruction for each basic
block. This patch "inlines" fill into getFirstSpecialInstruction, the
sole caller, to eliminate repeated hash lookups.


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

2 Files Affected:

  • (modified) llvm/include/llvm/Analysis/InstructionPrecedenceTracking.h (-3)
  • (modified) llvm/lib/Analysis/InstructionPrecedenceTracking.cpp (+10-18)
diff --git a/llvm/include/llvm/Analysis/InstructionPrecedenceTracking.h b/llvm/include/llvm/Analysis/InstructionPrecedenceTracking.h
index 192630e62a54c..7c17120375f9c 100644
--- a/llvm/include/llvm/Analysis/InstructionPrecedenceTracking.h
+++ b/llvm/include/llvm/Analysis/InstructionPrecedenceTracking.h
@@ -33,9 +33,6 @@ class InstructionPrecedenceTracking {
   // special instructions.
   DenseMap<const BasicBlock *, const Instruction *> FirstSpecialInsts;
 
-  // Fills information about the given block's special instructions.
-  void fill(const BasicBlock *BB);
-
 #ifndef NDEBUG
   /// Asserts that the cached info for \p BB is up-to-date. This helps to catch
   /// the usage error of accessing a block without properly invalidating after a
diff --git a/llvm/lib/Analysis/InstructionPrecedenceTracking.cpp b/llvm/lib/Analysis/InstructionPrecedenceTracking.cpp
index 9555e2c8dd5dd..a2a9c94c14ae8 100644
--- a/llvm/lib/Analysis/InstructionPrecedenceTracking.cpp
+++ b/llvm/lib/Analysis/InstructionPrecedenceTracking.cpp
@@ -47,11 +47,17 @@ const Instruction *InstructionPrecedenceTracking::getFirstSpecialInstruction(
     validate(BB);
 #endif
 
-  if (!FirstSpecialInsts.contains(BB)) {
-    fill(BB);
-    assert(FirstSpecialInsts.contains(BB) && "Must be!");
+  auto [It, Inserted] = FirstSpecialInsts.try_emplace(BB);
+  if (Inserted) {
+    for (const auto &I : *BB) {
+      NumInstScanned++;
+      if (isSpecialInstruction(&I)) {
+        It->second = &I;
+        break;
+      }
+    }
   }
-  return FirstSpecialInsts[BB];
+  return It->second;
 }
 
 bool InstructionPrecedenceTracking::hasSpecialInstructions(
@@ -66,20 +72,6 @@ bool InstructionPrecedenceTracking::isPreceededBySpecialInstruction(
   return MaybeFirstSpecial && MaybeFirstSpecial->comesBefore(Insn);
 }
 
-void InstructionPrecedenceTracking::fill(const BasicBlock *BB) {
-  FirstSpecialInsts.erase(BB);
-  for (const auto &I : *BB) {
-    NumInstScanned++;
-    if (isSpecialInstruction(&I)) {
-      FirstSpecialInsts[BB] = &I;
-      return;
-    }
-  }
-
-  // Mark this block as having no special instructions.
-  FirstSpecialInsts[BB] = nullptr;
-}
-
 #ifndef NDEBUG
 void InstructionPrecedenceTracking::validate(const BasicBlock *BB) const {
   auto It = FirstSpecialInsts.find(BB);

@kazutakahirata kazutakahirata merged commit ce2c4ea into llvm:main Mar 23, 2025
13 checks passed
@kazutakahirata kazutakahirata deleted the cleanup_001_repeated_hash_lookups_llvm_Analysis branch March 23, 2025 14:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

llvm:analysis Includes value tracking, cost tables and constant folding

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants