Index: llvm/trunk/lib/CodeGen/WinEHPrepare.cpp =================================================================== --- llvm/trunk/lib/CodeGen/WinEHPrepare.cpp +++ llvm/trunk/lib/CodeGen/WinEHPrepare.cpp @@ -98,6 +98,8 @@ SetVector &EHReturnBlocks); void findCXXEHReturnPoints(Function &F, SetVector &EHReturnBlocks); + void getPossibleReturnTargets(Function *ParentF, Function *HandlerF, + SetVector &Targets); void completeNestedLandingPad(Function *ParentFn, LandingPadInst *OutlinedLPad, const LandingPadInst *OriginalLPad, @@ -700,6 +702,14 @@ F.getEntryBlock().getFirstInsertionPt()); } + // This container stores the llvm.eh.recover and IndirectBr instructions + // that make up the body of each landing pad after it has been outlined. + // We need to defer the population of the target list for the indirectbr + // until all landing pads have been outlined so that we can handle the + // case of blocks in the target that are reached only from nested + // landing pads. + SmallVector, 4> LPadImpls; + for (LandingPadInst *LPad : LPads) { // Look for evidence that this landingpad has already been processed. bool LPadHasActionList = false; @@ -819,18 +829,28 @@ CallInst *Recover = CallInst::Create(ActionIntrin, ActionArgs, "recover", LPadBB); - // Add an indirect branch listing possible successors of the catch handlers. - SetVector ReturnTargets; - for (ActionHandler *Action : Actions) { - if (auto *CatchAction = dyn_cast(Action)) { - const auto &CatchTargets = CatchAction->getReturnTargets(); - ReturnTargets.insert(CatchTargets.begin(), CatchTargets.end()); + if (isAsynchronousEHPersonality(Personality)) { + // SEH can create the target list directly, since catch handlers + // are not outlined. + SetVector ReturnTargets; + for (ActionHandler *Action : Actions) { + if (auto *CatchAction = dyn_cast(Action)) { + const auto &CatchTargets = CatchAction->getReturnTargets(); + ReturnTargets.insert(CatchTargets.begin(), CatchTargets.end()); + } } + IndirectBrInst *Branch = + IndirectBrInst::Create(Recover, ReturnTargets.size(), LPadBB); + for (BasicBlock *Target : ReturnTargets) + Branch->addDestination(Target); + } else { + // C++ EH must defer populating the targets to handle the case of + // targets that are reached indirectly through nested landing pads. + IndirectBrInst *Branch = + IndirectBrInst::Create(Recover, 0, LPadBB); + + LPadImpls.push_back(std::make_pair(Recover, Branch)); } - IndirectBrInst *Branch = - IndirectBrInst::Create(Recover, ReturnTargets.size(), LPadBB); - for (BasicBlock *Target : ReturnTargets) - Branch->addDestination(Target); } // End for each landingpad // If nothing got outlined, there is no more processing to be done. @@ -844,6 +864,44 @@ completeNestedLandingPad(&F, LPadPair.first, LPadPair.second, FrameVarInfo); NestedLPtoOriginalLP.clear(); + // Populate the indirectbr instructions' target lists if we deferred + // doing so above. + SetVector CheckedTargets; + for (auto &LPadImplPair : LPadImpls) { + IntrinsicInst *Recover = cast(LPadImplPair.first); + IndirectBrInst *Branch = LPadImplPair.second; + + // Get a list of handlers called by + SmallVector ActionList; + parseEHActions(Recover, ActionList); + + // Add an indirect branch listing possible successors of the catch handlers. + SetVector ReturnTargets; + for (ActionHandler *Action : ActionList) { + if (auto *CA = dyn_cast(Action)) { + Function *Handler = cast(CA->getHandlerBlockOrFunc()); + getPossibleReturnTargets(&F, Handler, ReturnTargets); + } + } + for (BasicBlock *Target : ReturnTargets) { + Branch->addDestination(Target); + // The target may be a block that we excepted to get pruned. + // If it is, it may contain a call to llvm.eh.endcatch. + if (CheckedTargets.insert(Target)) { + // Earlier preparations guarantee that all calls to llvm.eh.endcatch + // will be followed by an unconditional branch. + auto *Br = dyn_cast(Target->getTerminator()); + if (Br && Br->isUnconditional() && + Br != Target->getFirstNonPHIOrDbgOrLifetime()) { + Instruction *Prev = Br->getPrevNode(); + if (match(cast(Prev), m_Intrinsic())) + Prev->eraseFromParent(); + } + } + } + } + LPadImpls.clear(); + F.addFnAttr("wineh-parent", F.getName()); // Delete any blocks that were only used by handlers that were outlined above. @@ -976,6 +1034,42 @@ RecursivelyDeleteTriviallyDeadInstructions(U); } +void WinEHPrepare::getPossibleReturnTargets(Function *ParentF, + Function *HandlerF, + SetVector &Targets) { + for (BasicBlock &BB : *HandlerF) { + // If the handler contains landing pads, check for any + // handlers that may return directly to a block in the + // parent function. + if (auto *LPI = BB.getLandingPadInst()) { + IntrinsicInst *Recover = cast(LPI->getNextNode()); + SmallVector ActionList; + parseEHActions(Recover, ActionList); + for (auto *Action : ActionList) { + if (auto *CH = dyn_cast(Action)) { + Function *NestedF = cast(CH->getHandlerBlockOrFunc()); + getPossibleReturnTargets(ParentF, NestedF, Targets); + } + } + } + + auto *Ret = dyn_cast(BB.getTerminator()); + if (!Ret) + continue; + + // Handler functions must always return a block address. + BlockAddress *BA = cast(Ret->getReturnValue()); + + // If this is the handler for a nested landing pad, the + // return address may have been remapped to a block in the + // parent handler. We're not interested in those. + if (BA->getFunction() != ParentF) + continue; + + Targets.insert(BA->getBasicBlock()); + } +} + void WinEHPrepare::completeNestedLandingPad(Function *ParentFn, LandingPadInst *OutlinedLPad, const LandingPadInst *OriginalLPad, Index: llvm/trunk/test/CodeGen/WinEH/cppeh-catch-unwind.ll =================================================================== --- llvm/trunk/test/CodeGen/WinEH/cppeh-catch-unwind.ll +++ llvm/trunk/test/CodeGen/WinEH/cppeh-catch-unwind.ll @@ -98,7 +98,7 @@ ; CHECK-NEXT: cleanup ; CHECK-NEXT: catch i8* bitcast (%rtti.TypeDescriptor2* @"\01??_R0H@8" to i8*) ; CHECK-NEXT: [[RECOVER3:\%.+]] = call i8* (...) @llvm.eh.actions(i32 1, i8* bitcast (%rtti.TypeDescriptor2* @"\01??_R0H@8" to i8*), i32 2, i8* (i8*, i8*)* @"\01?test@@YAXXZ.catch1", i32 0, void (i8*, i8*)* @"\01?test@@YAXXZ.cleanup") -; CHECK-NEXT: indirectbr i8* [[RECOVER3]], [label %try.cont] +; CHECK-NEXT: indirectbr i8* [[RECOVER3]], [label %try.cont, label %try.cont15] lpad3: ; preds = %invoke.cont2 %8 = landingpad { i8*, i32 } personality i8* bitcast (i32 (...)* @__CxxFrameHandler3 to i8*) Index: llvm/trunk/test/CodeGen/WinEH/cppeh-nested-1.ll =================================================================== --- llvm/trunk/test/CodeGen/WinEH/cppeh-nested-1.ll +++ llvm/trunk/test/CodeGen/WinEH/cppeh-nested-1.ll @@ -56,7 +56,7 @@ ; CHECK: catch i8* bitcast (%rtti.TypeDescriptor2* @"\01??_R0H@8" to i8*) ; CHECK: catch i8* bitcast (%rtti.TypeDescriptor2* @"\01??_R0M@8" to i8*) ; CHECK: [[RECOVER:\%.+]] = call i8* (...) @llvm.eh.actions(i32 1, i8* bitcast (%rtti.TypeDescriptor2* @"\01??_R0H@8" to i8*), i32 0, i8* (i8*, i8*)* @"\01?test@@YAXXZ.catch", i32 1, i8* bitcast (%rtti.TypeDescriptor2* @"\01??_R0M@8" to i8*), i32 1, i8* (i8*, i8*)* @"\01?test@@YAXXZ.catch1") -; CHECK: indirectbr i8* [[RECOVER]], [label %try.cont, label %try.cont10] +; CHECK: indirectbr i8* [[RECOVER]], [label %try.cont10, label %try.cont] lpad: ; preds = %entry %0 = landingpad { i8*, i32 } personality i8* bitcast (i32 (...)* @__CxxFrameHandler3 to i8*) Index: llvm/trunk/test/CodeGen/WinEH/cppeh-nested-2.ll =================================================================== --- llvm/trunk/test/CodeGen/WinEH/cppeh-nested-2.ll +++ llvm/trunk/test/CodeGen/WinEH/cppeh-nested-2.ll @@ -114,7 +114,7 @@ ; CHECK-SAME: i32 1, i8* bitcast (i8** @_ZTIi to i8*), i32 1, i8* (i8*, i8*)* @_Z4testv.catch1, ; CHECK-SAME: i32 0, void (i8*, i8*)* @_Z4testv.cleanup, ; CHECK-SAME: i32 1, i8* bitcast (i8** @_ZTIf to i8*), i32 0, i8* (i8*, i8*)* @_Z4testv.catch) -; CHECK-NEXT: indirectbr i8* [[RECOVER1]], [label %try.cont, label %try.cont19] +; CHECK-NEXT: indirectbr i8* [[RECOVER1]], [label %try.cont19, label %try.cont] lpad1: ; preds = %invoke.cont4, %invoke.cont %tmp3 = landingpad { i8*, i32 } personality i8* bitcast (i32 (...)* @__CxxFrameHandler3 to i8*) @@ -137,7 +137,7 @@ ; CHECK-SAME: i32 1, i8* bitcast (i8** @_ZTIi to i8*), i32 1, i8* (i8*, i8*)* @_Z4testv.catch1, ; CHECK-SAME: i32 0, void (i8*, i8*)* @_Z4testv.cleanup, ; CHECK-SAME: i32 1, i8* bitcast (i8** @_ZTIf to i8*), i32 0, i8* (i8*, i8*)* @_Z4testv.catch) -; CHECK-NEXT: indirectbr i8* [[RECOVER3]], [label %try.cont, label %try.cont19] +; CHECK-NEXT: indirectbr i8* [[RECOVER3]], [label %try.cont19, label %try.cont] lpad3: ; preds = %invoke.cont2 %tmp6 = landingpad { i8*, i32 } personality i8* bitcast (i32 (...)* @__CxxFrameHandler3 to i8*) Index: llvm/trunk/test/CodeGen/WinEH/cppeh-nested-3.ll =================================================================== --- llvm/trunk/test/CodeGen/WinEH/cppeh-nested-3.ll +++ llvm/trunk/test/CodeGen/WinEH/cppeh-nested-3.ll @@ -64,7 +64,7 @@ ; CHECK: catch i8* bitcast (%rtti.TypeDescriptor2* @"\01??_R0H@8" to i8*) ; CHECK: catch i8* bitcast (%rtti.TypeDescriptor2* @"\01??_R0M@8" to i8*) ; CHECK: [[RECOVER:\%.+]] = call i8* (...) @llvm.eh.actions(i32 1, i8* bitcast (%rtti.TypeDescriptor2* @"\01??_R0H@8" to i8*), i32 0, i8* (i8*, i8*)* @"\01?test@@YAXXZ.catch", i32 1, i8* bitcast (%rtti.TypeDescriptor2* @"\01??_R0M@8" to i8*), i32 1, i8* (i8*, i8*)* @"\01?test@@YAXXZ.catch1") -; CHECK: indirectbr i8* [[RECOVER]], [label %try.cont10, label %try.cont19] +; CHECK: indirectbr i8* [[RECOVER]], [label %try.cont19, label %try.cont10] lpad: ; preds = %entry %0 = landingpad { i8*, i32 } personality i8* bitcast (i32 (...)* @__CxxFrameHandler3 to i8*) Index: llvm/trunk/test/CodeGen/WinEH/cppeh-nested-rethrow.ll =================================================================== --- llvm/trunk/test/CodeGen/WinEH/cppeh-nested-rethrow.ll +++ llvm/trunk/test/CodeGen/WinEH/cppeh-nested-rethrow.ll @@ -0,0 +1,214 @@ +; RUN: opt -mtriple=x86_64-pc-windows-msvc -winehprepare -S -o - < %s | FileCheck %s + +; This test was generated from the following code. +; +; void test1() { +; try { +; try { +; throw 1; +; } catch(...) { throw; } +; } catch (...) { } +; } +; void test2() { +; try { +; throw 1; +; } catch(...) { +; try { +; throw; +; } catch (...) {} +; } +; } +; +; These two functions result in functionally equivalent code, but the last +; catch block contains a call to llvm.eh.endcatch that tripped up processing +; during development. +; +; The main purpose of this test is to verify that we can correctly +; handle the case of nested landing pads that return directly to a block in +; the parent function. + +; ModuleID = 'cppeh-nested-rethrow.cpp' +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.CatchableType = type { i32, i32, i32, i32, i32, i32, i32 } +%eh.CatchableTypeArray.1 = type { i32, [1 x i32] } +%eh.ThrowInfo = type { i32, i32, i32, i32 } + +$"\01??_R0H@8" = comdat any + +$"_CT??_R0H@84" = comdat any + +$_CTA1H = comdat any + +$_TI1H = 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 +@__ImageBase = external constant i8 +@"_CT??_R0H@84" = linkonce_odr unnamed_addr constant %eh.CatchableType { i32 1, i32 trunc (i64 sub nuw nsw (i64 ptrtoint (%rtti.TypeDescriptor2* @"\01??_R0H@8" to i64), i64 ptrtoint (i8* @__ImageBase to i64)) to i32), i32 0, i32 -1, i32 0, i32 4, i32 0 }, section ".xdata", comdat +@_CTA1H = linkonce_odr unnamed_addr constant %eh.CatchableTypeArray.1 { i32 1, [1 x i32] [i32 trunc (i64 sub nuw nsw (i64 ptrtoint (%eh.CatchableType* @"_CT??_R0H@84" to i64), i64 ptrtoint (i8* @__ImageBase to i64)) to i32)] }, section ".xdata", comdat +@_TI1H = linkonce_odr unnamed_addr constant %eh.ThrowInfo { i32 0, i32 0, i32 0, i32 trunc (i64 sub nuw nsw (i64 ptrtoint (%eh.CatchableTypeArray.1* @_CTA1H to i64), i64 ptrtoint (i8* @__ImageBase to i64)) to i32) }, section ".xdata", comdat + +; CHECK-LABEL: define void @"\01?test1@@YAXXZ"() +; CHECK: entry: +; CHECK: call void (...) @llvm.frameescape + +; Function Attrs: nounwind uwtable +define void @"\01?test1@@YAXXZ"() #0 { +entry: + %tmp = alloca i32, align 4 + %exn.slot = alloca i8* + %ehselector.slot = alloca i32 + store i32 1, i32* %tmp + %0 = bitcast i32* %tmp to i8* + invoke void @_CxxThrowException(i8* %0, %eh.ThrowInfo* @_TI1H) #2 + to label %unreachable unwind label %lpad + +lpad: ; preds = %entry + %1 = landingpad { i8*, i32 } personality i8* bitcast (i32 (...)* @__CxxFrameHandler3 to i8*) + catch i8* null + %2 = extractvalue { i8*, i32 } %1, 0 + store i8* %2, i8** %exn.slot + %3 = extractvalue { i8*, i32 } %1, 1 + store i32 %3, i32* %ehselector.slot + br label %catch + +catch: ; preds = %lpad + %exn = load i8*, i8** %exn.slot + call void @llvm.eh.begincatch(i8* %exn, i8* null) #1 + invoke void @_CxxThrowException(i8* null, %eh.ThrowInfo* null) #2 + to label %unreachable unwind label %lpad1 + +lpad1: ; preds = %catch + %4 = landingpad { i8*, i32 } personality i8* bitcast (i32 (...)* @__CxxFrameHandler3 to i8*) + catch i8* null + %5 = extractvalue { i8*, i32 } %4, 0 + store i8* %5, i8** %exn.slot + %6 = extractvalue { i8*, i32 } %4, 1 + store i32 %6, i32* %ehselector.slot + br label %catch2 + +catch2: ; preds = %lpad1 + %exn3 = load i8*, i8** %exn.slot + call void @llvm.eh.begincatch(i8* %exn3, i8* null) #1 + call void @llvm.eh.endcatch() #1 + br label %try.cont4 + +; This block should not be eliminated. +; CHECK: try.cont4: +try.cont4: ; preds = %catch2, %try.cont + ret void + +try.cont: ; No predecessors! + br label %try.cont4 + +unreachable: ; preds = %catch, %entry + unreachable +; CHECK: } +} + +declare void @_CxxThrowException(i8*, %eh.ThrowInfo*) + +declare i32 @__CxxFrameHandler3(...) + +; Function Attrs: nounwind +declare void @llvm.eh.begincatch(i8* nocapture, i8* nocapture) #1 + +; Function Attrs: nounwind +declare void @llvm.eh.endcatch() #1 + +; CHECK-LABEL: define void @"\01?test2@@YAXXZ"() +; CHECK: entry: +; CHECK: call void (...) @llvm.frameescape + +; Function Attrs: nounwind uwtable +define void @"\01?test2@@YAXXZ"() #0 { +entry: + %tmp = alloca i32, align 4 + %exn.slot = alloca i8* + %ehselector.slot = alloca i32 + store i32 1, i32* %tmp + %0 = bitcast i32* %tmp to i8* + invoke void @_CxxThrowException(i8* %0, %eh.ThrowInfo* @_TI1H) #2 + to label %unreachable unwind label %lpad + +lpad: ; preds = %entry + %1 = landingpad { i8*, i32 } personality i8* bitcast (i32 (...)* @__CxxFrameHandler3 to i8*) + catch i8* null + %2 = extractvalue { i8*, i32 } %1, 0 + store i8* %2, i8** %exn.slot + %3 = extractvalue { i8*, i32 } %1, 1 + store i32 %3, i32* %ehselector.slot + br label %catch + +catch: ; preds = %lpad + %exn = load i8*, i8** %exn.slot + call void @llvm.eh.begincatch(i8* %exn, i8* null) #1 + invoke void @_CxxThrowException(i8* null, %eh.ThrowInfo* null) #2 + to label %unreachable unwind label %lpad1 + +lpad1: ; preds = %catch + %4 = landingpad { i8*, i32 } personality i8* bitcast (i32 (...)* @__CxxFrameHandler3 to i8*) + catch i8* null + %5 = extractvalue { i8*, i32 } %4, 0 + store i8* %5, i8** %exn.slot + %6 = extractvalue { i8*, i32 } %4, 1 + store i32 %6, i32* %ehselector.slot + br label %catch2 + +catch2: ; preds = %lpad1 + %exn3 = load i8*, i8** %exn.slot + call void @llvm.eh.begincatch(i8* %exn3, i8* null) #1 + call void @llvm.eh.endcatch() #1 + br label %try.cont + +; This block should not be eliminated. +; CHECK: try.cont: +; The endcatch call should be eliminated. +; CHECK-NOT: call void @llvm.eh.endcatch() +try.cont: ; preds = %catch2 + call void @llvm.eh.endcatch() #1 + br label %try.cont4 + +try.cont4: ; preds = %try.cont + ret void + +unreachable: ; preds = %catch, %entry + unreachable +; CHECK: } +} + +; The outlined test1.catch handler should not contain a return instruction. +; CHECK-LABEL: define internal i8* @"\01?test1@@YAXXZ.catch"(i8*, i8*) +; CHECK-NOT: ret +; CHECK: } + +; The outlined test1.catch1 handler should return to a valid block address. +; CHECK-LABEL: define internal i8* @"\01?test1@@YAXXZ.catch1"(i8*, i8*) +; WILL-CHECK: ret i8* inttoptr ( +; CHECK-NOT: ret i8* inttoptr (i32 1 to i8*) +; CHECK: } + +; The outlined test2.catch handler should not contain a return instruction. +; CHECK-LABEL: define internal i8* @"\01?test2@@YAXXZ.catch"(i8*, i8*) +; CHECK-NOT: ret +; CHECK: } + +; The outlined test2.catch1 handler should return to a valid block address. +; CHECK-LABEL: define internal i8* @"\01?test2@@YAXXZ.catch2"(i8*, i8*) +; WILL-CHECK: ret i8* inttoptr ( +; CHECK-NOT: ret i8* inttoptr (i32 1 to i8*) +; CHECK: } + + +attributes #0 = { nounwind uwtable "less-precise-fpmad"="false" "no-frame-pointer-elim"="false" "no-infs-fp-math"="false" "no-nans-fp-math"="false" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+sse,+sse2" "unsafe-fp-math"="false" "use-soft-float"="false" } +attributes #1 = { nounwind } +attributes #2 = { noreturn } + +!llvm.module.flags = !{!0} +!llvm.ident = !{!1} + +!0 = !{i32 1, !"PIC Level", i32 2} +!1 = !{!"clang version 3.7.0 (trunk 236059)"}