From a4824774dc6cc54a8a69fec74eea6e2ed415a109 Mon Sep 17 00:00:00 2001 From: David Majnemer Date: Tue, 5 Jan 2016 06:27:50 +0000 Subject: [PATCH] [SimplifyCFG] Remove redundant catchpads Remove duplicate catchpad handlers from a catchswitch. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@256814 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Transforms/Utils/Local.cpp | 19 +++++++++++++++++-- test/Transforms/SimplifyCFG/wineh-unreachable.ll | 18 ++++++++++++++++++ 2 files changed, 35 insertions(+), 2 deletions(-) diff --git a/lib/Transforms/Utils/Local.cpp b/lib/Transforms/Utils/Local.cpp index e75163f323d..3845ca2e7e5 100644 --- a/lib/Transforms/Utils/Local.cpp +++ b/lib/Transforms/Utils/Local.cpp @@ -1305,8 +1305,9 @@ static bool markAliveBlocks(Function &F, } } - // Turn invokes that call 'nounwind' functions into ordinary calls. - if (InvokeInst *II = dyn_cast(BB->getTerminator())) { + TerminatorInst *Terminator = BB->getTerminator(); + if (auto *II = dyn_cast(Terminator)) { + // Turn invokes that call 'nounwind' functions into ordinary calls. Value *Callee = II->getCalledValue(); if (isa(Callee) || isa(Callee)) { changeToUnreachable(II, true); @@ -1321,6 +1322,20 @@ static bool markAliveBlocks(Function &F, changeToCall(II); Changed = true; } + } else if (auto *CatchSwitch = dyn_cast(Terminator)) { + // Remove catchpads which cannot be reached. + SmallPtrSet HandlersSeen; + for (CatchSwitchInst::handler_iterator I = CatchSwitch->handler_begin(), + E = CatchSwitch->handler_end(); + I != E; ++I) { + BasicBlock *HandlerBB = *I; + if (!HandlersSeen.insert(HandlerBB).second) { + CatchSwitch->removeHandler(I); + --I; + --E; + Changed = true; + } + } } Changed |= ConstantFoldTerminator(BB, true); diff --git a/test/Transforms/SimplifyCFG/wineh-unreachable.ll b/test/Transforms/SimplifyCFG/wineh-unreachable.ll index 670119467da..d871e17b6b0 100644 --- a/test/Transforms/SimplifyCFG/wineh-unreachable.ll +++ b/test/Transforms/SimplifyCFG/wineh-unreachable.ll @@ -81,3 +81,21 @@ catch.body: exit: unreachable } + +; CHECK-LABEL: define void @test6() +define void @test6() personality i8* bitcast (void ()* @Personality to i8*) { +entry: + invoke void @f() + to label %exit unwind label %catch.pad + +catch.pad: + %cs1 = catchswitch within none [label %catch.body, label %catch.body] unwind to caller + ; CHECK: catchswitch within none [label %catch.body] unwind to caller + +catch.body: + %catch = catchpad within %cs1 [i8* null, i32 0, i8* null] + catchret from %catch to label %exit + +exit: + ret void +} -- 2.11.0