From 1a827441913a6348a8d60c7e02618b2ec5842289 Mon Sep 17 00:00:00 2001 From: Roman Lebedev Date: Wed, 19 May 2021 13:53:36 +0300 Subject: [PATCH] [NFCI][SimplifyCFG] removeEmptyCleanup(): use DeleteDeadBlock() This required some changes to, instead of eagerly making PHI's in the UnwindDest valid as-if the BB is already not a predecessor, to be valid while BB is still a predecessor. --- lib/Transforms/Utils/SimplifyCFG.cpp | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/lib/Transforms/Utils/SimplifyCFG.cpp b/lib/Transforms/Utils/SimplifyCFG.cpp index b0e5e532c12..e25362c346d 100644 --- a/lib/Transforms/Utils/SimplifyCFG.cpp +++ b/lib/Transforms/Utils/SimplifyCFG.cpp @@ -4486,9 +4486,6 @@ static bool removeEmptyCleanup(CleanupReturnInst *RI, DomTreeUpdater *DTU) { Value *SrcVal = DestPN.getIncomingValue(Idx); PHINode *SrcPN = dyn_cast(SrcVal); - // Remove the entry for the block we are deleting. - DestPN.removeIncomingValue(Idx, false); - bool NeedPHITranslation = SrcPN && SrcPN->getParent() == BB; for (auto *Pred : predecessors(BB)) { Value *Incoming = @@ -4516,12 +4513,15 @@ static bool removeEmptyCleanup(CleanupReturnInst *RI, DomTreeUpdater *DTU) { if (pred != BB) PN.addIncoming(&PN, pred); PN.moveBefore(InsertPt); + // Also, add a dummy incoming value for the original BB itself, + // so that the PHI is well-formed until we drop said predecessor. + PN.addIncoming(UndefValue::get(PN.getType()), BB); } } std::vector Updates; - // We use make_early_inc_range here because we may remove some predecessors. + // We use make_early_inc_range here because we will remove all predecessors. for (BasicBlock *PredBB : llvm::make_early_inc_range(predecessors(BB))) { if (UnwindDest == nullptr) { if (DTU) { @@ -4531,6 +4531,7 @@ static bool removeEmptyCleanup(CleanupReturnInst *RI, DomTreeUpdater *DTU) { removeUnwindEdge(PredBB, DTU); ++NumInvokes; } else { + BB->removePredecessor(PredBB); Instruction *TI = PredBB->getTerminator(); TI->replaceUsesOfWith(BB, UnwindDest); if (DTU) { @@ -4540,12 +4541,10 @@ static bool removeEmptyCleanup(CleanupReturnInst *RI, DomTreeUpdater *DTU) { } } - if (DTU) { + if (DTU) DTU->applyUpdates(Updates); - DTU->deleteBB(BB); - } else - // The cleanup pad is now unreachable. Zap it. - BB->eraseFromParent(); + + DeleteDeadBlock(BB, DTU); return true; }