@@ -47,29 +47,11 @@ const Instruction *InstructionPrecedenceTracking::getFirstSpecialInstruction(
47
47
validate (BB);
48
48
#endif
49
49
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];
73
55
}
74
56
75
57
bool InstructionPrecedenceTracking::hasSpecialInstructions (
@@ -84,20 +66,33 @@ bool InstructionPrecedenceTracking::isPreceededBySpecialInstruction(
84
66
return MaybeFirstSpecial && MaybeFirstSpecial->comesBefore (Insn);
85
67
}
86
68
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
+
87
83
#ifndef NDEBUG
88
84
void InstructionPrecedenceTracking::validate (const BasicBlock *BB) const {
89
85
auto It = FirstSpecialInsts.find (BB);
90
86
// Bail if we don't have anything cached for this block.
91
87
if (It == FirstSpecialInsts.end ())
92
88
return ;
93
89
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!" );
97
94
return ;
98
- assert (!isSpecialInstruction (&I) &&
99
- " Cached first special instruction is wrong!" );
100
- }
95
+ }
101
96
102
97
assert (It->second == nullptr &&
103
98
" Block is marked as having special instructions but in fact it has "
@@ -120,12 +115,8 @@ void InstructionPrecedenceTracking::insertInstructionTo(const Instruction *Inst,
120
115
void InstructionPrecedenceTracking::removeInstruction (const Instruction *Inst) {
121
116
auto *BB = Inst->getParent ();
122
117
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);
129
120
}
130
121
131
122
void InstructionPrecedenceTracking::removeUsersOf (const Instruction *Inst) {
0 commit comments