From ce2c4ea658239771dec542249a77dcf2056d8b8a Mon Sep 17 00:00:00 2001 From: Kazu Hirata Date: Sun, 23 Mar 2025 07:36:19 -0700 Subject: [PATCH] [Analysis] Avoid repeated hash lookups (NFC) (#132584) FirstSpecialInsts caches the first special instruction for each basic block. This patch "inlines" fill into getFirstSpecialInstruction, the sole caller, to eliminate repeated hash lookups. --- .../Analysis/InstructionPrecedenceTracking.h | 3 -- .../InstructionPrecedenceTracking.cpp | 28 +++++++------------ 2 files changed, 10 insertions(+), 21 deletions(-) diff --git a/llvm/include/llvm/Analysis/InstructionPrecedenceTracking.h b/llvm/include/llvm/Analysis/InstructionPrecedenceTracking.h index 192630e62a54..7c17120375f9 100644 --- a/llvm/include/llvm/Analysis/InstructionPrecedenceTracking.h +++ b/llvm/include/llvm/Analysis/InstructionPrecedenceTracking.h @@ -33,9 +33,6 @@ class InstructionPrecedenceTracking { // special instructions. DenseMap 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 9555e2c8dd5d..a2a9c94c14ae 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);