[llvm] [Analysis] Avoid repeated hash lookups (NFC) (PR #132584)
via llvm-commits
llvm-commits at lists.llvm.org
Sat Mar 22 20:44:56 PDT 2025
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-llvm-analysis
Author: Kazu Hirata (kazutakahirata)
<details>
<summary>Changes</summary>
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
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);
``````````
</details>
https://github.com/llvm/llvm-project/pull/132584
More information about the llvm-commits
mailing list