From 0955fc316218c839658feddf1d1464f158b78f0f Mon Sep 17 00:00:00 2001 From: David Majnemer Date: Thu, 1 Oct 2015 21:04:13 +0000 Subject: [PATCH] [WinEH] Stop BranchFolding from merging across funclets BranchFolding would merge two funclets together, this is not OK. Disable this and strengthen the assertion in FuncletLayout. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@249069 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/CodeGen/BranchFolding.cpp | 6 +++-- lib/CodeGen/FuncletLayout.cpp | 14 +++------- test/CodeGen/X86/funclet-layout.ll | 53 ++++++++++++++++++++++++++++++++++++-- 3 files changed, 58 insertions(+), 15 deletions(-) diff --git a/lib/CodeGen/BranchFolding.cpp b/lib/CodeGen/BranchFolding.cpp index 2ef8889091f..2cc851f3973 100644 --- a/lib/CodeGen/BranchFolding.cpp +++ b/lib/CodeGen/BranchFolding.cpp @@ -94,9 +94,11 @@ bool BranchFolderPass::runOnMachineFunction(MachineFunction &MF) { TargetPassConfig *PassConfig = &getAnalysis(); // TailMerge can create jump into if branches that make CFG irreducible for - // HW that requires structurized CFG. + // HW that requires structurized CFG. It can also cause BBs to get shared + // between funclets. bool EnableTailMerge = !MF.getTarget().requiresStructuredCFG() && - PassConfig->getEnableTailMerge(); + !MF.getMMI().hasEHFunclets() && + PassConfig->getEnableTailMerge(); BranchFolder Folder(EnableTailMerge, /*CommonHoist=*/true, getAnalysis(), getAnalysis()); diff --git a/lib/CodeGen/FuncletLayout.cpp b/lib/CodeGen/FuncletLayout.cpp index e1e185b4752..4fdb66700b2 100644 --- a/lib/CodeGen/FuncletLayout.cpp +++ b/lib/CodeGen/FuncletLayout.cpp @@ -39,17 +39,9 @@ collectFuncletMembers(DenseMap &FuncletMembership, int Funclet, MachineBasicBlock *MBB) { // Don't revisit blocks. if (FuncletMembership.count(MBB) > 0) { - // FIXME: This is a hack, we need to assert this unconditionally. - bool IsProbablyUnreachableBlock = - MBB->empty() || - (MBB->succ_empty() && !MBB->getFirstTerminator()->isReturn() && - MBB->size() == 1); - - if (!IsProbablyUnreachableBlock) { - if (FuncletMembership[MBB] != Funclet) { - assert(false && "MBB is part of two funclets!"); - report_fatal_error("MBB is part of two funclets!"); - } + if (FuncletMembership[MBB] != Funclet) { + assert(false && "MBB is part of two funclets!"); + report_fatal_error("MBB is part of two funclets!"); } return; } diff --git a/test/CodeGen/X86/funclet-layout.ll b/test/CodeGen/X86/funclet-layout.ll index 43587671d2d..d31ae2312c2 100644 --- a/test/CodeGen/X86/funclet-layout.ll +++ b/test/CodeGen/X86/funclet-layout.ll @@ -4,6 +4,10 @@ target datalayout = "e-m:w-i64:64-f80:128-n8:16:32:64-S128" target triple = "x86_64-pc-windows-msvc" %eh.ThrowInfo = type { i32, i32, i32, i32 } +%rtti.TypeDescriptor2 = type { i8**, i8*, [3 x i8] } + +@"\01??_7type_info@@6B@" = external constant i8* +@"\01??_R0H@8" = internal global %rtti.TypeDescriptor2 { i8** @"\01??_7type_info@@6B@", i8* null, [3 x i8] c".H\00" } define void @test1(i1 %B) personality i32 (...)* @__CxxFrameHandler3 { entry: @@ -38,7 +42,7 @@ unreachable: ; CHECK: retq ; The catch funclet contains %catch and %catchret -; CHECK: # %catch +; CHECK: # %catch{{$}} ; CHECK: # %catchret ; CHECK: retq @@ -94,9 +98,54 @@ unreachable: ; preds = %catch, %entry ; CHECK: retq ; The outer catch funclet contains %catch and %try.cont -; CHECK: # %catch +; CHECK: # %catch{{$}} +; CHECK: # %try.cont{{$}} +; CHECK: retq + + +define void @test3() #0 personality i8* bitcast (i32 (...)* @__CxxFrameHandler3 to i8*) { +entry: + invoke void @g() + to label %try.cont unwind label %catch.dispatch + +catch.dispatch: ; preds = %entry + %0 = catchpad [%rtti.TypeDescriptor2* @"\01??_R0H@8", i32 0, i8* null] + to label %catch.2 unwind label %catch.dispatch.1 + +catch.2: ; preds = %catch.dispatch + tail call void @exit(i32 0) #2 + unreachable + +catch.dispatch.1: ; preds = %catch.dispatch + %1 = catchpad [i8* null, i32 64, i8* null] + to label %catch unwind label %catchendblock + +catch: ; preds = %catch.dispatch.1 + tail call void @exit(i32 0) #2 + unreachable + +catchendblock: ; preds = %catch.dispatch.1 + catchendpad unwind to caller + +try.cont: ; preds = %entry + ret void +} + +; CHECK-LABEL: test3: + +; The entry funclet contains %entry and %try.cont +; CHECK: # %entry ; CHECK: # %try.cont ; CHECK: retq +; The catch(int) funclet contains %catch.2 +; CHECK: # %catch.2 +; CHECK: callq exit + +; The catch(...) funclet contains %catch +; CHECK: # %catch{{$}} +; CHECK: callq exit + +declare void @exit(i32) noreturn nounwind declare void @_CxxThrowException(i8*, %eh.ThrowInfo*) declare i32 @__CxxFrameHandler3(...) -- 2.11.0