Skip to content
This repository was archived by the owner on Feb 5, 2019. It is now read-only.

Commit 80ad955

Browse files
majnemeralexcrichton
authored andcommitted
[SimplifyCFG] Don't kill empty cleanuppads with multiple uses
A basic block could contain: %cp = cleanuppad [] cleanupret from %cp unwind to caller This basic block is empty and is thus a candidate for removal. However, there can be other uses of %cp outside of this basic block. This is only possible in unreachable blocks. Make our transform more correct by checking that the pad has a single user before removing the BB. This fixes PR28005. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@271816 91177308-0d34-0410-b5e6-96231b3b80d8
1 parent a73c41e commit 80ad955

File tree

2 files changed

+40
-11
lines changed

2 files changed

+40
-11
lines changed

lib/Transforms/Utils/SimplifyCFG.cpp

+16-11
Original file line numberDiff line numberDiff line change
@@ -2417,7 +2417,7 @@ static Value *ensureValueAvailableInSuccessor(Value *V, BasicBlock *BB,
24172417
// where OtherBB is the single other predecessor of BB's only successor.
24182418
PHINode *PHI = nullptr;
24192419
BasicBlock *Succ = BB->getSingleSuccessor();
2420-
2420+
24212421
for (auto I = Succ->begin(); isa<PHINode>(I); ++I)
24222422
if (cast<PHINode>(I)->getIncomingValueForBlock(BB) == V) {
24232423
PHI = cast<PHINode>(I);
@@ -2561,7 +2561,7 @@ static bool mergeConditionalStoreToAddress(BasicBlock *PTB, BasicBlock *PFB,
25612561

25622562
QStore->eraseFromParent();
25632563
PStore->eraseFromParent();
2564-
2564+
25652565
return true;
25662566
}
25672567

@@ -2593,7 +2593,7 @@ static bool mergeConditionalStores(BranchInst *PBI, BranchInst *QBI) {
25932593
// We model triangles as a type of diamond with a nullptr "true" block.
25942594
// Triangles are canonicalized so that the fallthrough edge is represented by
25952595
// a true condition, as in the diagram above.
2596-
//
2596+
//
25972597
BasicBlock *PTB = PBI->getSuccessor(0);
25982598
BasicBlock *PFB = PBI->getSuccessor(1);
25992599
BasicBlock *QTB = QBI->getSuccessor(0);
@@ -2652,7 +2652,7 @@ static bool mergeConditionalStores(BranchInst *PBI, BranchInst *QBI) {
26522652
if (StoreInst *SI = dyn_cast<StoreInst>(&I))
26532653
QStoreAddresses.insert(SI->getPointerOperand());
26542654
}
2655-
2655+
26562656
set_intersect(PStoreAddresses, QStoreAddresses);
26572657
// set_intersect mutates PStoreAddresses in place. Rename it here to make it
26582658
// clear what it contains.
@@ -3381,7 +3381,12 @@ bool SimplifyCFGOpt::SimplifyCleanupReturn(CleanupReturnInst *RI) {
33813381
// This isn't an empty cleanup.
33823382
return false;
33833383

3384-
// Check that there are no other instructions except for debug intrinsics.
3384+
// We cannot kill the pad if it has multiple uses. This typically arises
3385+
// from unreachable basic blocks.
3386+
if (!CPInst->hasOneUse())
3387+
return false;
3388+
3389+
// Check that there are no other instructions except for benign intrinsics.
33853390
BasicBlock::iterator I = CPInst->getIterator(), E = RI->getIterator();
33863391
while (++I != E)
33873392
if (!isa<DbgInfoIntrinsic>(I))
@@ -3818,17 +3823,17 @@ static bool EliminateDeadSwitchCases(SwitchInst *SI, AssumptionCache *AC,
38183823
}
38193824
}
38203825

3821-
// If we can prove that the cases must cover all possible values, the
3822-
// default destination becomes dead and we can remove it. If we know some
3826+
// If we can prove that the cases must cover all possible values, the
3827+
// default destination becomes dead and we can remove it. If we know some
38233828
// of the bits in the value, we can use that to more precisely compute the
38243829
// number of possible unique case values.
38253830
bool HasDefault =
38263831
!isa<UnreachableInst>(SI->getDefaultDest()->getFirstNonPHIOrDbg());
3827-
const unsigned NumUnknownBits = Bits -
3832+
const unsigned NumUnknownBits = Bits -
38283833
(KnownZero.Or(KnownOne)).countPopulation();
38293834
assert(NumUnknownBits <= Bits);
38303835
if (HasDefault && DeadCases.empty() &&
3831-
NumUnknownBits < 64 /* avoid overflow */ &&
3836+
NumUnknownBits < 64 /* avoid overflow */ &&
38323837
SI->getNumCases() == (1ULL << NumUnknownBits)) {
38333838
DEBUG(dbgs() << "SimplifyCFG: switch default is dead.\n");
38343839
BasicBlock *NewDefault = SplitBlockPredecessors(SI->getDefaultDest(),
@@ -4584,7 +4589,7 @@ static void reuseTableCompare(User *PhiUser, BasicBlock *PhiBlock,
45844589
assert((CaseConst == TrueConst || CaseConst == FalseConst) &&
45854590
"Expect true or false as compare result.");
45864591
}
4587-
4592+
45884593
// Check if the branch instruction dominates the phi node. It's a simple
45894594
// dominance check, but sufficient for our needs.
45904595
// Although this check is invariant in the calling loops, it's better to do it
@@ -5149,7 +5154,7 @@ bool SimplifyCFGOpt::SimplifyCondBranch(BranchInst *BI, IRBuilder<> &Builder) {
51495154
if (PBI != BI && PBI->isConditional())
51505155
if (mergeConditionalStores(PBI, BI))
51515156
return SimplifyCFG(BB, TTI, BonusInstThreshold, AC) | true;
5152-
5157+
51535158
return false;
51545159
}
51555160

test/Transforms/SimplifyCFG/empty-cleanuppad.ll

+24
Original file line numberDiff line numberDiff line change
@@ -405,6 +405,30 @@ return: ; preds = %invoke.cont, %catch
405405
ret void
406406
}
407407

408+
; CHECK-LABEL: define void @f10(
409+
define void @f10(i32 %V) personality i32 (...)* @__CxxFrameHandler3 {
410+
entry:
411+
invoke void @g()
412+
to label %unreachable unwind label %cleanup
413+
; CHECK: call void @g()
414+
; CHECK-NEXT: unreachable
415+
416+
unreachable:
417+
unreachable
418+
419+
cleanup:
420+
%cp = cleanuppad within none []
421+
switch i32 %V, label %cleanupret1 [
422+
i32 0, label %cleanupret2
423+
]
424+
425+
cleanupret1:
426+
cleanupret from %cp unwind to caller
427+
428+
cleanupret2:
429+
cleanupret from %cp unwind to caller
430+
}
431+
408432
%struct.S = type { i8 }
409433
%struct.S2 = type { i8 }
410434
declare void @"\01??1S2@@QEAA@XZ"(%struct.S2*)

0 commit comments

Comments
 (0)