Index: llvm/lib/Transforms/Utils/SimplifyCFG.cpp =================================================================== --- llvm/lib/Transforms/Utils/SimplifyCFG.cpp +++ llvm/lib/Transforms/Utils/SimplifyCFG.cpp @@ -3962,6 +3962,25 @@ return !TrivialUnwindBlocks.empty(); } +// Check if it is one of the specified intrinsics +static bool isIntrinsics(BasicBlock::iterator I) { + auto *II = dyn_cast(I); + if (!II) + return false; + + Intrinsic::ID IntrinsicID = II->getIntrinsicID(); + switch (IntrinsicID) { + case Intrinsic::dbg_declare: + case Intrinsic::dbg_value: + case Intrinsic::dbg_label: + case Intrinsic::lifetime_end: + break; + default: + return false; + } + return true; +} + // Simplify resume that is only used by a single (non-phi) landing pad. bool SimplifyCFGOpt::simplifySingleResume(ResumeInst *RI) { BasicBlock *BB = RI->getParent(); @@ -3972,7 +3991,7 @@ // Check that there are no other instructions except for debug intrinsics. BasicBlock::iterator I = LPInst->getIterator(), E = RI->getIterator(); while (++I != E) - if (!isa(I)) + if (!isIntrinsics(I)) return false; // Turn all invokes that unwind here into calls and delete the basic block. @@ -4010,22 +4029,9 @@ // Check that there are no other instructions except for benign intrinsics. BasicBlock::iterator I = CPInst->getIterator(), E = RI->getIterator(); - while (++I != E) { - auto *II = dyn_cast(I); - if (!II) - return false; - - Intrinsic::ID IntrinsicID = II->getIntrinsicID(); - switch (IntrinsicID) { - case Intrinsic::dbg_declare: - case Intrinsic::dbg_value: - case Intrinsic::dbg_label: - case Intrinsic::lifetime_end: - break; - default: + while (++I != E) + if (!isIntrinsics(I)) return false; - } - } // If the cleanup return we are simplifying unwinds to the caller, this will // set UnwindDest to nullptr. Index: llvm/test/Transforms/SimplifyCFG/lifetime-landingpad.ll =================================================================== --- /dev/null +++ llvm/test/Transforms/SimplifyCFG/lifetime-landingpad.ll @@ -0,0 +1,33 @@ +; RUN: opt < %s -simplifycfg -S | FileCheck %s + +; CHECK-LABEL: define void @foo +define void @foo() personality i32 (...)* @__gxx_personality_v0 { +entry: +; CHECK: alloca i8 +; CHECK: call void @llvm.lifetime.start.p0i8 +; CHECK: call void @bar() +; CHECK: call void @llvm.lifetime.end.p0i8 +; CHECK: ret void + %a = alloca i8 + call void @llvm.lifetime.start.p0i8(i64 1, i8* nonnull %a) nounwind + invoke void @bar() to label %invoke.cont unwind label %lpad + +invoke.cont: + call void @llvm.lifetime.end.p0i8(i64 1, i8* nonnull %a) nounwind + ret void + +lpad: +; CHECK-NOT: landingpad + %b = landingpad { i8*, i32 } + cleanup + call void @llvm.lifetime.end.p0i8(i64 1, i8* nonnull %a) nounwind + resume { i8*, i32 } %b +} + +declare void @bar() + +declare void @llvm.lifetime.start.p0i8(i64 immarg, i8* nocapture) nounwind + +declare void @llvm.lifetime.end.p0i8(i64 immarg, i8* nocapture) nounwind + +declare i32 @__gxx_personality_v0(...)