Skip to content

Commit db32af7

Browse files
Merge pull request #6857 from aschwaighofer/fix_iter_invalid_devirtualizer
Fix a iteration invalidation bug
2 parents 4db0ac6 + a2c6410 commit db32af7

File tree

1 file changed

+14
-11
lines changed

1 file changed

+14
-11
lines changed

lib/SILOptimizer/Transforms/Devirtualizer.cpp

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ bool Devirtualizer::devirtualizeAppliesInFunction(SILFunction &F,
5555
llvm::SmallVector<SILInstruction *, 8> DeadApplies;
5656
llvm::SmallVector<ApplySite, 8> NewApplies;
5757

58+
SmallVector<FullApplySite, 16> Applies;
5859
for (auto &BB : F) {
5960
for (auto It = BB.begin(), End = BB.end(); It != End;) {
6061
auto &I = *It++;
@@ -64,20 +65,22 @@ bool Devirtualizer::devirtualizeAppliesInFunction(SILFunction &F,
6465
auto Apply = FullApplySite::isa(&I);
6566
if (!Apply)
6667
continue;
68+
Applies.push_back(Apply);
69+
}
70+
}
71+
for (auto Apply : Applies) {
72+
auto NewInstPair = tryDevirtualizeApply(Apply, CHA);
73+
if (!NewInstPair.second)
74+
continue;
6775

68-
auto NewInstPair = tryDevirtualizeApply(Apply, CHA);
69-
if (!NewInstPair.second)
70-
continue;
71-
72-
Changed = true;
76+
Changed = true;
7377

74-
auto *AI = Apply.getInstruction();
75-
if (!isa<TryApplyInst>(AI))
76-
AI->replaceAllUsesWith(NewInstPair.first);
78+
auto *AI = Apply.getInstruction();
79+
if (!isa<TryApplyInst>(AI))
80+
AI->replaceAllUsesWith(NewInstPair.first);
7781

78-
DeadApplies.push_back(AI);
79-
NewApplies.push_back(NewInstPair.second);
80-
}
82+
DeadApplies.push_back(AI);
83+
NewApplies.push_back(NewInstPair.second);
8184
}
8285

8386
// Remove all the now-dead applies.

0 commit comments

Comments
 (0)