Skip to content

Commit eccfa35

Browse files
ZequanWurnk
authored andcommitted
Fix lifetime call in landingpad blocking Simplifycfg pass
Fix lifetime call in landingpad blocks simplifycfg from removing the landingpad. Reviewed By: rnk Differential Revision: https://reviews.llvm.org/D77188
1 parent c496d84 commit eccfa35

File tree

2 files changed

+59
-21
lines changed

2 files changed

+59
-21
lines changed

llvm/lib/Transforms/Utils/SimplifyCFG.cpp

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3962,6 +3962,28 @@ bool SimplifyCFGOpt::simplifyCommonResume(ResumeInst *RI) {
39623962
return !TrivialUnwindBlocks.empty();
39633963
}
39643964

3965+
// Check if cleanup block is empty
3966+
static bool isCleanupBlockEmpty(Instruction *Inst, Instruction *RI) {
3967+
BasicBlock::iterator I = Inst->getIterator(), E = RI->getIterator();
3968+
while (++I != E) {
3969+
auto *II = dyn_cast<IntrinsicInst>(I);
3970+
if (!II)
3971+
return false;
3972+
3973+
Intrinsic::ID IntrinsicID = II->getIntrinsicID();
3974+
switch (IntrinsicID) {
3975+
case Intrinsic::dbg_declare:
3976+
case Intrinsic::dbg_value:
3977+
case Intrinsic::dbg_label:
3978+
case Intrinsic::lifetime_end:
3979+
break;
3980+
default:
3981+
return false;
3982+
}
3983+
}
3984+
return true;
3985+
}
3986+
39653987
// Simplify resume that is only used by a single (non-phi) landing pad.
39663988
bool SimplifyCFGOpt::simplifySingleResume(ResumeInst *RI) {
39673989
BasicBlock *BB = RI->getParent();
@@ -3970,10 +3992,8 @@ bool SimplifyCFGOpt::simplifySingleResume(ResumeInst *RI) {
39703992
"Resume must unwind the exception that caused control to here");
39713993

39723994
// Check that there are no other instructions except for debug intrinsics.
3973-
BasicBlock::iterator I = LPInst->getIterator(), E = RI->getIterator();
3974-
while (++I != E)
3975-
if (!isa<DbgInfoIntrinsic>(I))
3976-
return false;
3995+
if (!isCleanupBlockEmpty(LPInst, RI))
3996+
return false;
39773997

39783998
// Turn all invokes that unwind here into calls and delete the basic block.
39793999
for (pred_iterator PI = pred_begin(BB), PE = pred_end(BB); PI != PE;) {
@@ -4009,23 +4029,8 @@ static bool removeEmptyCleanup(CleanupReturnInst *RI) {
40094029
return false;
40104030

40114031
// Check that there are no other instructions except for benign intrinsics.
4012-
BasicBlock::iterator I = CPInst->getIterator(), E = RI->getIterator();
4013-
while (++I != E) {
4014-
auto *II = dyn_cast<IntrinsicInst>(I);
4015-
if (!II)
4016-
return false;
4017-
4018-
Intrinsic::ID IntrinsicID = II->getIntrinsicID();
4019-
switch (IntrinsicID) {
4020-
case Intrinsic::dbg_declare:
4021-
case Intrinsic::dbg_value:
4022-
case Intrinsic::dbg_label:
4023-
case Intrinsic::lifetime_end:
4024-
break;
4025-
default:
4026-
return false;
4027-
}
4028-
}
4032+
if (!isCleanupBlockEmpty(CPInst, RI))
4033+
return false;
40294034

40304035
// If the cleanup return we are simplifying unwinds to the caller, this will
40314036
// set UnwindDest to nullptr.
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
; RUN: opt < %s -simplifycfg -S | FileCheck %s
2+
3+
; CHECK-LABEL: define void @foo
4+
define void @foo() personality i32 (...)* @__gxx_personality_v0 {
5+
entry:
6+
; CHECK: alloca i8
7+
; CHECK: call void @llvm.lifetime.start.p0i8
8+
; CHECK: call void @bar()
9+
; CHECK: call void @llvm.lifetime.end.p0i8
10+
; CHECK: ret void
11+
%a = alloca i8
12+
call void @llvm.lifetime.start.p0i8(i64 1, i8* nonnull %a) nounwind
13+
invoke void @bar() to label %invoke.cont unwind label %lpad
14+
15+
invoke.cont:
16+
call void @llvm.lifetime.end.p0i8(i64 1, i8* nonnull %a) nounwind
17+
ret void
18+
19+
lpad:
20+
; CHECK-NOT: landingpad
21+
%b = landingpad { i8*, i32 }
22+
cleanup
23+
call void @llvm.lifetime.end.p0i8(i64 1, i8* nonnull %a) nounwind
24+
resume { i8*, i32 } %b
25+
}
26+
27+
declare void @bar()
28+
29+
declare void @llvm.lifetime.start.p0i8(i64 immarg, i8* nocapture) nounwind
30+
31+
declare void @llvm.lifetime.end.p0i8(i64 immarg, i8* nocapture) nounwind
32+
33+
declare i32 @__gxx_personality_v0(...)

0 commit comments

Comments
 (0)