Skip to content

Commit ce2c4ea

Browse files
[Analysis] Avoid repeated hash lookups (NFC) (llvm#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.
1 parent da01a18 commit ce2c4ea

File tree

2 files changed

+10
-21
lines changed

2 files changed

+10
-21
lines changed

llvm/include/llvm/Analysis/InstructionPrecedenceTracking.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,6 @@ class InstructionPrecedenceTracking {
3333
// special instructions.
3434
DenseMap<const BasicBlock *, const Instruction *> FirstSpecialInsts;
3535

36-
// Fills information about the given block's special instructions.
37-
void fill(const BasicBlock *BB);
38-
3936
#ifndef NDEBUG
4037
/// Asserts that the cached info for \p BB is up-to-date. This helps to catch
4138
/// the usage error of accessing a block without properly invalidating after a

llvm/lib/Analysis/InstructionPrecedenceTracking.cpp

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,17 @@ const Instruction *InstructionPrecedenceTracking::getFirstSpecialInstruction(
4747
validate(BB);
4848
#endif
4949

50-
if (!FirstSpecialInsts.contains(BB)) {
51-
fill(BB);
52-
assert(FirstSpecialInsts.contains(BB) && "Must be!");
50+
auto [It, Inserted] = FirstSpecialInsts.try_emplace(BB);
51+
if (Inserted) {
52+
for (const auto &I : *BB) {
53+
NumInstScanned++;
54+
if (isSpecialInstruction(&I)) {
55+
It->second = &I;
56+
break;
57+
}
58+
}
5359
}
54-
return FirstSpecialInsts[BB];
60+
return It->second;
5561
}
5662

5763
bool InstructionPrecedenceTracking::hasSpecialInstructions(
@@ -66,20 +72,6 @@ bool InstructionPrecedenceTracking::isPreceededBySpecialInstruction(
6672
return MaybeFirstSpecial && MaybeFirstSpecial->comesBefore(Insn);
6773
}
6874

69-
void InstructionPrecedenceTracking::fill(const BasicBlock *BB) {
70-
FirstSpecialInsts.erase(BB);
71-
for (const auto &I : *BB) {
72-
NumInstScanned++;
73-
if (isSpecialInstruction(&I)) {
74-
FirstSpecialInsts[BB] = &I;
75-
return;
76-
}
77-
}
78-
79-
// Mark this block as having no special instructions.
80-
FirstSpecialInsts[BB] = nullptr;
81-
}
82-
8375
#ifndef NDEBUG
8476
void InstructionPrecedenceTracking::validate(const BasicBlock *BB) const {
8577
auto It = FirstSpecialInsts.find(BB);

0 commit comments

Comments
 (0)