Skip to content

Commit 3781a46

Browse files
committed
Revert "[IPT] Restructure cache to allow lazy update following invalidation [NFC]"
This reverts commit baea663. Causes crashes, e.g. https://lab.llvm.org/buildbot/#/builders/77/builds/10715.
1 parent 408075e commit 3781a46

File tree

2 files changed

+32
-40
lines changed

2 files changed

+32
-40
lines changed

llvm/include/llvm/Analysis/InstructionPrecedenceTracking.h

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,14 @@ class BasicBlock;
2828
class Instruction;
2929

3030
class InstructionPrecedenceTracking {
31-
// Maps a block to the topmost instruction which *might* be a special
32-
// instruction in it. This value is lazily updated on query to point to the
33-
// topmost special instruction, but we allow it to point before that for
34-
// efficient invalidation. If the value is nullptr, it means that it is
35-
// known that this block does not contain any special instructions.
31+
// Maps a block to the topmost special instruction in it. If the value is
32+
// nullptr, it means that it is known that this block does not contain any
33+
// special instructions.
3634
DenseMap<const BasicBlock *, const Instruction *> FirstSpecialInsts;
3735

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

llvm/lib/Analysis/InstructionPrecedenceTracking.cpp

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

50-
if (!FirstSpecialInsts.count(BB))
51-
// Seed the lazy scan
52-
FirstSpecialInsts[BB] = &*BB->begin();
53-
54-
auto *CurI = FirstSpecialInsts[BB];
55-
if (!CurI || isSpecialInstruction(CurI))
56-
// We found a cached definite result
57-
return CurI;
58-
59-
// Otherwise, scan forward until we find a definite result, then cache that.
60-
auto *Res = [&]() -> const Instruction * {
61-
for (auto &I : make_range(CurI->getIterator(), BB->end())) {
62-
NumInstScanned++;
63-
if (isSpecialInstruction(&I))
64-
// Found next special instruction
65-
return &I;
66-
}
67-
// Mark this block as having no special instructions.
68-
return nullptr;
69-
}();
70-
71-
FirstSpecialInsts[BB] = Res;
72-
return Res;
50+
if (FirstSpecialInsts.find(BB) == FirstSpecialInsts.end()) {
51+
fill(BB);
52+
assert(FirstSpecialInsts.find(BB) != FirstSpecialInsts.end() && "Must be!");
53+
}
54+
return FirstSpecialInsts[BB];
7355
}
7456

7557
bool InstructionPrecedenceTracking::hasSpecialInstructions(
@@ -84,20 +66,33 @@ bool InstructionPrecedenceTracking::isPreceededBySpecialInstruction(
8466
return MaybeFirstSpecial && MaybeFirstSpecial->comesBefore(Insn);
8567
}
8668

69+
void InstructionPrecedenceTracking::fill(const BasicBlock *BB) {
70+
FirstSpecialInsts.erase(BB);
71+
for (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+
8783
#ifndef NDEBUG
8884
void InstructionPrecedenceTracking::validate(const BasicBlock *BB) const {
8985
auto It = FirstSpecialInsts.find(BB);
9086
// Bail if we don't have anything cached for this block.
9187
if (It == FirstSpecialInsts.end())
9288
return;
9389

94-
for (const Instruction &I : *BB) {
95-
if (&I == It->second)
96-
// No special instruction before cached result
90+
for (const Instruction &Insn : *BB)
91+
if (isSpecialInstruction(&Insn)) {
92+
assert(It->second == &Insn &&
93+
"Cached first special instruction is wrong!");
9794
return;
98-
assert(!isSpecialInstruction(&I) &&
99-
"Cached first special instruction is wrong!");
100-
}
95+
}
10196

10297
assert(It->second == nullptr &&
10398
"Block is marked as having special instructions but in fact it has "
@@ -120,12 +115,8 @@ void InstructionPrecedenceTracking::insertInstructionTo(const Instruction *Inst,
120115
void InstructionPrecedenceTracking::removeInstruction(const Instruction *Inst) {
121116
auto *BB = Inst->getParent();
122117
assert(BB && "must be called before instruction is actually removed");
123-
if (FirstSpecialInsts.count(BB) && FirstSpecialInsts[BB] == Inst) {
124-
if (Inst->isTerminator())
125-
FirstSpecialInsts[BB] = nullptr;
126-
else
127-
FirstSpecialInsts[BB] = &*std::next(Inst->getIterator());
128-
}
118+
if (FirstSpecialInsts.count(BB) && FirstSpecialInsts[BB] == Inst)
119+
FirstSpecialInsts.erase(BB);
129120
}
130121

131122
void InstructionPrecedenceTracking::removeUsersOf(const Instruction *Inst) {

0 commit comments

Comments
 (0)