From e0d6a5c90a8e399d125e7ce2afc15084f73703e3 Mon Sep 17 00:00:00 2001 From: Andrew Kaylor Date: Mon, 20 Apr 2015 22:04:09 +0000 Subject: [PATCH] [WinEH] Fix problem with mapping shared empty handler blocks. Differential Revision: http://reviews.llvm.org/D9125 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@235354 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/CodeGen/WinEHPrepare.cpp | 40 ++++++- .../CodeGen/WinEH/cppeh-shared-empty-catch.ll | 110 ++++++++++++++++++ 2 files changed, 148 insertions(+), 2 deletions(-) create mode 100644 test/CodeGen/WinEH/cppeh-shared-empty-catch.ll diff --git a/lib/CodeGen/WinEHPrepare.cpp b/lib/CodeGen/WinEHPrepare.cpp index c540a5d92e1..cba9ea530ba 100644 --- a/lib/CodeGen/WinEHPrepare.cpp +++ b/lib/CodeGen/WinEHPrepare.cpp @@ -770,6 +770,15 @@ static bool isSelectorDispatch(BasicBlock *BB, BasicBlock *&CatchHandler, return false; } +static bool isCatchBlock(BasicBlock *BB) { + for (BasicBlock::iterator II = BB->getFirstNonPHIOrDbg(), IE = BB->end(); + II != IE; ++II) { + if (match(cast(II), m_Intrinsic())) + return true; + } + return false; +} + static BasicBlock *createStubLandingPad(Function *Handler, Value *PersonalityFn) { // FIXME: Finish this! @@ -1447,11 +1456,11 @@ void WinEHPrepare::mapLandingPadBlocks(LandingPadInst *LPad, } CatchHandler *CatchAction = findCatchHandler(BB, NextBB, VisitedBlocks); + assert(CatchAction); + // See if there is any interesting code executed before the dispatch. findCleanupHandlers(Actions, BB, CatchAction->getStartBlock()); - assert(CatchAction); - // When the source program contains multiple nested try blocks the catch // handlers can get strung together in such a way that we can encounter // a dispatch for a selector that we've already had a handler for. @@ -1463,6 +1472,21 @@ void WinEHPrepare::mapLandingPadBlocks(LandingPadInst *LPad, << CatchAction->getStartBlock()->getName() << "\n"); Actions.insertCatchHandler(CatchAction); } else { + // Under some circumstances optimized IR will flow unconditionally into a + // handler block without checking the selector. This can only happen if + // the landing pad has a catch-all handler and the handler for the + // preceeding catch clause is identical to the catch-call handler + // (typically an empty catch). In this case, the handler must be shared + // by all remaining clauses. + if (isa( + CatchAction->getSelector()->stripPointerCasts())) { + DEBUG(dbgs() << " Applying early catch-all handler in block " + << CatchAction->getStartBlock()->getName() + << " to all remaining clauses.\n"); + Actions.insertCatchHandler(CatchAction); + return; + } + DEBUG(dbgs() << " Found extra catch dispatch in block " << CatchAction->getStartBlock()->getName() << "\n"); } @@ -1517,6 +1541,18 @@ CatchHandler *WinEHPrepare::findCatchHandler(BasicBlock *BB, CatchHandlerMap[BB] = Action; return Action; } + // If we encounter a block containing an llvm.eh.begincatch before we + // find a selector dispatch block, the handler is assumed to be + // reached unconditionally. This happens for catch-all blocks, but + // it can also happen for other catch handlers that have been combined + // with the catch-all handler during optimization. + if (isCatchBlock(BB)) { + PointerType *Int8PtrTy = Type::getInt8PtrTy(BB->getContext()); + Constant *NullSelector = ConstantPointerNull::get(Int8PtrTy); + CatchHandler *Action = new CatchHandler(BB, NullSelector, nullptr); + CatchHandlerMap[BB] = Action; + return Action; + } } // Visit each successor, looking for the dispatch. diff --git a/test/CodeGen/WinEH/cppeh-shared-empty-catch.ll b/test/CodeGen/WinEH/cppeh-shared-empty-catch.ll new file mode 100644 index 00000000000..0f8e805cc7c --- /dev/null +++ b/test/CodeGen/WinEH/cppeh-shared-empty-catch.ll @@ -0,0 +1,110 @@ +; RUN: opt -mtriple=x86_64-pc-windows-msvc -winehprepare -S -o - < %s | FileCheck %s + +; This test is based on the following source, built with -O2 +; +; void f() { +; try { +; g(); +; try { +; throw; +; } catch (int) { +; } +; } catch (...) { +; } +; } +; + +; ModuleID = '' +target datalayout = "e-m:w-i64:64-f80:128-n8:16:32:64-S128" +target triple = "x86_64-pc-windows-msvc" + +%rtti.TypeDescriptor2 = type { i8**, i8*, [3 x i8] } +%eh.CatchHandlerType = type { i32, i8* } +%eh.ThrowInfo = type { i32, i32, i32, i32 } + +$"\01??_R0H@8" = comdat any + +@"\01??_7type_info@@6B@" = external constant i8* +@"\01??_R0H@8" = linkonce_odr global %rtti.TypeDescriptor2 { i8** @"\01??_7type_info@@6B@", i8* null, [3 x i8] c".H\00" }, comdat +@llvm.eh.handlertype.H.0 = private unnamed_addr constant %eh.CatchHandlerType { i32 0, i8* bitcast (%rtti.TypeDescriptor2* @"\01??_R0H@8" to i8*) }, section "llvm.metadata" + +; CHECK-LABEL: define void @"\01?f@@YAXXZ"() +; CHECK: entry: +; CHECK: call void (...) @llvm.frameescape() +; CHECK: invoke void @"\01?g@@YAXXZ"() + +; Function Attrs: nounwind +define void @"\01?f@@YAXXZ"() #0 { +entry: + invoke void @"\01?g@@YAXXZ"() + to label %invoke.cont unwind label %lpad + +; CHECK-LABEL: invoke.cont: +; CHECK: invoke void @_CxxThrowException(i8* null, %eh.ThrowInfo* null) +; CHECK: to label %unreachable unwind label %[[LPAD1_LABEL:lpad[0-9]+]] + +invoke.cont: ; preds = %entry + invoke void @_CxxThrowException(i8* null, %eh.ThrowInfo* null) #4 + to label %unreachable unwind label %lpad1 + +lpad: ; preds = %entry + %0 = landingpad { i8*, i32 } personality i8* bitcast (i32 (...)* @__CxxFrameHandler3 to i8*) + catch i8* null + %1 = extractvalue { i8*, i32 } %0, 0 + br label %catch2 + +; Note: Even though this landing pad has two catch clauses, it only has one action because both +; handlers do the same thing. +; CHECK: [[LPAD1_LABEL]]: +; CHECK: landingpad { i8*, i32 } personality i8* bitcast (i32 (...)* @__CxxFrameHandler3 to i8*) +; CHECK-NEXT: catch %eh.CatchHandlerType* @llvm.eh.handlertype.H.0 +; CHECK-NEXT: catch i8* null +; CHECK-NEXT: [[RECOVER:\%.+]] = call i8* (...) @llvm.eh.actions(i32 1, i8* null, i32 -1, i8* (i8*, i8*)* @"\01?f@@YAXXZ.catch1") +; CHECK-NEXT: indirectbr i8* [[RECOVER]], [label %try.cont4] + +lpad1: ; preds = %invoke.cont + %2 = landingpad { i8*, i32 } personality i8* bitcast (i32 (...)* @__CxxFrameHandler3 to i8*) + catch %eh.CatchHandlerType* @llvm.eh.handlertype.H.0 + catch i8* null + %3 = extractvalue { i8*, i32 } %2, 0 + br label %catch2 + +catch2: ; preds = %lpad1, %lpad + %exn.slot.0 = phi i8* [ %3, %lpad1 ], [ %1, %lpad ] + tail call void @llvm.eh.begincatch(i8* %exn.slot.0, i8* null) #3 + tail call void @llvm.eh.endcatch() #3 + br label %try.cont4 + +try.cont4: ; preds = %catch, %catch2 + ret void + +unreachable: ; preds = %invoke.cont + unreachable + +; CHECK: } +} + +declare void @"\01?g@@YAXXZ"() #1 + +declare i32 @__CxxFrameHandler3(...) + +declare void @_CxxThrowException(i8*, %eh.ThrowInfo*) + +; Function Attrs: nounwind readnone +declare i32 @llvm.eh.typeid.for(i8*) #2 + +; Function Attrs: nounwind +declare void @llvm.eh.begincatch(i8* nocapture, i8* nocapture) #3 + +; Function Attrs: nounwind +declare void @llvm.eh.endcatch() #3 + +attributes #0 = { nounwind "less-precise-fpmad"="false" "no-frame-pointer-elim"="false" "no-infs-fp-math"="false" "no-nans-fp-math"="false" "no-realign-stack" "stack-protector-buffer-size"="8" "unsafe-fp-math"="false" "use-soft-float"="false" } +attributes #1 = { "less-precise-fpmad"="false" "no-frame-pointer-elim"="false" "no-infs-fp-math"="false" "no-nans-fp-math"="false" "no-realign-stack" "stack-protector-buffer-size"="8" "unsafe-fp-math"="false" "use-soft-float"="false" } +attributes #2 = { nounwind readnone } +attributes #3 = { nounwind } +attributes #4 = { noreturn } + +!llvm.ident = !{!0} + +!0 = !{!"clang version 3.7.0 (trunk 235112) (llvm/trunk 235121)"} -- 2.34.1