[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.
This commit is contained in:
Kazu Hirata
2025-03-23 07:36:19 -07:00
committed by GitHub
parent da01a185f1
commit ce2c4ea658
2 changed files with 10 additions and 21 deletions

View File

@@ -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

View File

@@ -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);