[SimplifyCFG] Teach removeEmptyCleanup() to preserve DomTree

This commit is contained in:
Roman Lebedev 2020-12-19 15:38:30 +03:00
parent 4be8707e64
commit 76e74d9395
No known key found for this signature in database
GPG Key ID: 083C3EBB4A1689E0
2 changed files with 18 additions and 6 deletions

View File

@ -4205,7 +4205,7 @@ bool SimplifyCFGOpt::simplifySingleResume(ResumeInst *RI) {
return true;
}
static bool removeEmptyCleanup(CleanupReturnInst *RI) {
static bool removeEmptyCleanup(CleanupReturnInst *RI, DomTreeUpdater *DTU) {
// If this is a trivial cleanup pad that executes no instructions, it can be
// eliminated. If the cleanup pad continues to the caller, any predecessor
// that is an EH pad will be updated to continue to the caller and any
@ -4312,20 +4312,32 @@ static bool removeEmptyCleanup(CleanupReturnInst *RI) {
}
}
std::vector<DominatorTree::UpdateType> Updates;
for (pred_iterator PI = pred_begin(BB), PE = pred_end(BB); PI != PE;) {
// The iterator must be updated here because we are removing this pred.
BasicBlock *PredBB = *PI++;
if (UnwindDest == nullptr) {
removeUnwindEdge(PredBB);
if (DTU)
DTU->applyUpdatesPermissive(Updates);
Updates.clear();
removeUnwindEdge(PredBB, DTU);
++NumInvokes;
} else {
Instruction *TI = PredBB->getTerminator();
TI->replaceUsesOfWith(BB, UnwindDest);
Updates.push_back({DominatorTree::Delete, PredBB, BB});
Updates.push_back({DominatorTree::Insert, PredBB, UnwindDest});
}
}
// The cleanup pad is now unreachable. Zap it.
BB->eraseFromParent();
if (DTU) {
DTU->applyUpdatesPermissive(Updates);
DTU->deleteBB(BB);
} else
// The cleanup pad is now unreachable. Zap it.
BB->eraseFromParent();
return true;
}
@ -4372,7 +4384,7 @@ bool SimplifyCFGOpt::simplifyCleanupReturn(CleanupReturnInst *RI) {
if (mergeCleanupPad(RI))
return true;
if (removeEmptyCleanup(RI))
if (removeEmptyCleanup(RI, DTU))
return true;
return false;

View File

@ -1,7 +1,7 @@
; Tests that the dynamic allocation and deallocation of the coroutine frame is
; elided and any tail calls referencing the coroutine frame has the tail
; call attribute removed.
; RUN: opt < %s -S -inline -coro-elide -instsimplify -simplifycfg | FileCheck %s
; RUN: opt < %s -S -inline -coro-elide -instsimplify -simplifycfg -simplifycfg-require-and-preserve-domtree=1 | FileCheck %s
; RUN: opt < %s -S \
; RUN: -passes='cgscc(inline,function(coro-elide,instsimplify,simplify-cfg))' \
; RUN: -aa-pipeline='basic-aa' | FileCheck %s