Outline cleanup handlers for native Windows C++ exception handling

Differential Revision: http://reviews.llvm.org/D7865

llvm-svn: 231117
diff --git a/llvm/test/CodeGen/X86/cppeh-catch-all.ll b/llvm/test/CodeGen/X86/cppeh-catch-all.ll
index 814e97d..c8d54ac 100644
--- a/llvm/test/CodeGen/X86/cppeh-catch-all.ll
+++ b/llvm/test/CodeGen/X86/cppeh-catch-all.ll
@@ -52,8 +52,8 @@
   ret void
 }
 
-; CHECK: define i8* @_Z4testv.catch(i8*, i8*) {
-; CHECK: catch.entry:
+; CHECK: define internal i8* @_Z4testv.catch(i8*, i8*) {
+; CHECK: entry:
 ; CHECK:   %eh.alloc = call i8* @llvm.framerecover(i8* bitcast (void ()* @_Z4testv to i8*), i8* %1)
 ; CHECK:   %eh.data = bitcast i8* %eh.alloc to %struct._Z4testv.ehdata*
 ; CHECK:   %eh.obj.ptr = getelementptr inbounds %struct._Z4testv.ehdata, %struct._Z4testv.ehdata* %eh.data, i32 0, i32 1
diff --git a/llvm/test/CodeGen/X86/cppeh-catch-scalar.ll b/llvm/test/CodeGen/X86/cppeh-catch-scalar.ll
index 9c16a9b..ce3f1b52 100644
--- a/llvm/test/CodeGen/X86/cppeh-catch-scalar.ll
+++ b/llvm/test/CodeGen/X86/cppeh-catch-scalar.ll
@@ -83,8 +83,8 @@
   resume { i8*, i32 } %lpad.val5
 }
 
-; CHECK-LABEL: define i8* @_Z4testv.catch(i8*, i8*) {
-; CHECK: catch.entry:
+; CHECK: define internal i8* @_Z4testv.catch(i8*, i8*) {
+; CHECK: entry:
 ; CHECK:   %eh.alloc = call i8* @llvm.framerecover(i8* bitcast (void ()* @_Z4testv to i8*), i8* %1)
 ; CHECK:   %eh.data = bitcast i8* %eh.alloc to %struct._Z4testv.ehdata*
 ; CHECK:   %eh.obj.ptr = getelementptr inbounds %struct._Z4testv.ehdata, %struct._Z4testv.ehdata* %eh.data, i32 0, i32 1
diff --git a/llvm/test/CodeGen/X86/cppeh-frame-vars.ll b/llvm/test/CodeGen/X86/cppeh-frame-vars.ll
index 471aeed..6d66440 100644
--- a/llvm/test/CodeGen/X86/cppeh-frame-vars.ll
+++ b/llvm/test/CodeGen/X86/cppeh-frame-vars.ll
@@ -177,8 +177,8 @@
 }
 
 ; The following catch handler should be outlined.
-; CHECK-LABEL: define i8* @"\01?test@@YAXXZ.catch"(i8*, i8*) {
-; CHECK: catch.entry:
+; CHECK-LABEL: define internal i8* @"\01?test@@YAXXZ.catch"(i8*, i8*) {
+; CHECK: entry:
 ; CHECK:   %eh.alloc = call i8* @llvm.framerecover(i8* bitcast (void ()* @"\01?test@@YAXXZ" to i8*), i8* %1)
 ; CHECK:   %eh.data = bitcast i8* %eh.alloc to %"struct.\01?test@@YAXXZ.ehdata"*
 ; CHECK:   %eh.obj.ptr = getelementptr inbounds %"struct.\01?test@@YAXXZ.ehdata", %"struct.\01?test@@YAXXZ.ehdata"* %eh.data, i32 0, i32 1
@@ -201,7 +201,7 @@
 ; CHECK:   %cmp1 = icmp eq i32 %tmp14, %tmp15
 ; CHECK:   br i1 %cmp1, label %if.then, label %if.else
 ;
-; CHECK: if.then:                                          ; preds = %catch.entry
+; CHECK: if.then:                                          ; preds = %entry
 ; CHECK:   %tmp16 = load i32, i32* %e, align 4
 ; CHECK:   %b = getelementptr inbounds %struct.SomeData, %struct.SomeData* %Data, i32 0, i32 1
 ; CHECK:   %tmp17 = load i32, i32* %b, align 4
@@ -209,7 +209,7 @@
 ; CHECK:   store i32 %add2, i32* %b, align 4
 ; CHECK:   br label %if.end
 ;
-; CHECK: if.else:                                          ; preds = %catch.entry
+; CHECK: if.else:                                          ; preds = %entry
 ; CHECK:   %tmp18 = load i32, i32* %e, align 4
 ; CHECK:   %a3 = getelementptr inbounds %struct.SomeData, %struct.SomeData* %Data, i32 0, i32 0
 ; CHECK:   %tmp19 = load i32, i32* %a3, align 4
diff --git a/llvm/test/CodeGen/X86/cppeh-inalloca.ll b/llvm/test/CodeGen/X86/cppeh-inalloca.ll
index 61f5a0c..72175a6 100644
--- a/llvm/test/CodeGen/X86/cppeh-inalloca.ll
+++ b/llvm/test/CodeGen/X86/cppeh-inalloca.ll
@@ -130,8 +130,8 @@
 }
 
 ; The following catch handler should be outlined.
-; CHECK: define i8* @"\01?test@@YAHUA@@@Z.catch"(i8*, i8*) {
-; CHECK: catch.entry:
+; CHECK: define internal i8* @"\01?test@@YAHUA@@@Z.catch"(i8*, i8*) {
+; CHECK: entry:
 ; CHECK:   %eh.alloc = call i8* @llvm.framerecover(i8* bitcast (i32 (<{ %struct.A }>*)* @"\01?test@@YAHUA@@@Z" to i8*), i8* %1)
 ; CHECK:   %eh.data = bitcast i8* %eh.alloc to %"struct.\01?test@@YAHUA@@@Z.ehdata"*
 ; CHECK:   %eh.obj.ptr = getelementptr inbounds %"struct.\01?test@@YAHUA@@@Z.ehdata", %"struct.\01?test@@YAHUA@@@Z.ehdata"* %eh.data, i32 0, i32 1
diff --git a/llvm/test/CodeGen/X86/cppeh-min-unwind.ll b/llvm/test/CodeGen/X86/cppeh-min-unwind.ll
new file mode 100644
index 0000000..a7b9738
--- /dev/null
+++ b/llvm/test/CodeGen/X86/cppeh-min-unwind.ll
@@ -0,0 +1,92 @@
+; RUN: opt -mtriple=x86_64-pc-windows-msvc -winehprepare -S -o - < %s | FileCheck %s
+
+; This test was generated from the following source:
+;
+; class SomeClass {
+; public:
+;   SomeClass();
+;   ~SomeClass();
+; };
+;
+; void test() {
+;   SomeClass obj;
+;   may_throw();
+; }
+
+
+; ModuleID = 'min-unwind.cpp'
+target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
+target triple = "x86_64-pc-windows-msvc"
+
+; This structure should be created for the frame allocation.
+; CHECK: %struct._Z4testv.ehdata = type { i32, i8*, %class.SomeClass }
+
+%class.SomeClass = type { [28 x i32] }
+
+; The function entry should be rewritten like this.
+; CHECK: define void @_Z4testv() #0 {
+; CHECK: entry:
+; CHECK:   %frame.alloc = call i8* @llvm.frameallocate(i32 128)
+; CHECK:   %eh.data = bitcast i8* %frame.alloc to %struct._Z4testv.ehdata*
+; CHECK-NOT:  %obj = alloca %class.SomeClass, align 4
+; CHECK:   %obj = getelementptr inbounds %struct._Z4testv.ehdata, %struct._Z4testv.ehdata* %eh.data, i32 0, i32 2
+
+; Function Attrs: uwtable
+define void @_Z4testv() #0 {
+entry:
+  %obj = alloca %class.SomeClass, align 4
+  %exn.slot = alloca i8*
+  %ehselector.slot = alloca i32
+  call void @_ZN9SomeClassC1Ev(%class.SomeClass* %obj)
+  invoke void @_Z9may_throwv()
+          to label %invoke.cont unwind label %lpad
+
+invoke.cont:                                      ; preds = %entry
+  call void @_ZN9SomeClassD1Ev(%class.SomeClass* %obj)
+  ret void
+
+lpad:                                             ; preds = %entry
+  %tmp = landingpad { i8*, i32 } personality i8* bitcast (i32 (...)* @__CxxFrameHandler3 to i8*)
+          cleanup
+  %tmp1 = extractvalue { i8*, i32 } %tmp, 0
+  store i8* %tmp1, i8** %exn.slot
+  %tmp2 = extractvalue { i8*, i32 } %tmp, 1
+  store i32 %tmp2, i32* %ehselector.slot
+  call void @_ZN9SomeClassD1Ev(%class.SomeClass* %obj)
+  br label %eh.resume
+
+eh.resume:                                        ; preds = %lpad
+  %exn = load i8*, i8** %exn.slot
+  %sel = load i32, i32* %ehselector.slot
+  %lpad.val = insertvalue { i8*, i32 } undef, i8* %exn, 0
+  %lpad.val2 = insertvalue { i8*, i32 } %lpad.val, i32 %sel, 1
+  resume { i8*, i32 } %lpad.val2
+}
+
+; This cleanup handler should be outlined.
+; CHECK: define internal void @_Z4testv.cleanup(i8*, i8*) {
+; CHECK: entry:
+; CHECK:   %eh.alloc = call i8* @llvm.framerecover(i8* bitcast (void ()* @_Z4testv to i8*), i8* %1)
+; CHECK:   %eh.data = bitcast i8* %eh.alloc to %struct._Z4testv.ehdata*
+; CHECK:   %obj = getelementptr inbounds %struct._Z4testv.ehdata, %struct._Z4testv.ehdata* %eh.data, i32 0, i32 2
+; CHECK:   call void @_ZN9SomeClassD1Ev(%class.SomeClass* %obj)
+; CHECK:   ret void
+; CHECK: }
+
+declare void @_ZN9SomeClassC1Ev(%class.SomeClass*) #1
+
+declare void @_Z9may_throwv() #1
+
+declare i32 @__CxxFrameHandler3(...)
+
+declare void @_ZN9SomeClassD1Ev(%class.SomeClass*) #1
+
+attributes #0 = { uwtable "less-precise-fpmad"="false" "no-frame-pointer-elim"="true" "no-frame-pointer-elim-non-leaf" "no-infs-fp-math"="false" "no-nans-fp-math"="false" "stack-protector-buffer-size"="8" "unsafe-fp-math"="false" "use-soft-float"="false" }
+attributes #1 = { "less-precise-fpmad"="false" "no-frame-pointer-elim"="true" "no-frame-pointer-elim-non-leaf" "no-infs-fp-math"="false" "no-nans-fp-math"="false" "stack-protector-buffer-size"="8" "unsafe-fp-math"="false" "use-soft-float"="false" }
+attributes #2 = { noinline noreturn nounwind }
+attributes #3 = { noreturn nounwind }
+attributes #4 = { nounwind }
+
+!llvm.ident = !{!0}
+
+!0 = !{!"clang version 3.7.0 (trunk 226027)"}
diff --git a/llvm/test/CodeGen/X86/cppeh-nonalloca-frame-values.ll b/llvm/test/CodeGen/X86/cppeh-nonalloca-frame-values.ll
index 987b264..1239f26 100644
--- a/llvm/test/CodeGen/X86/cppeh-nonalloca-frame-values.ll
+++ b/llvm/test/CodeGen/X86/cppeh-nonalloca-frame-values.ll
@@ -185,8 +185,8 @@
 }
 
 ; The following catch handler should be outlined.
-; CHECK: define i8* @"\01?test@@YAXXZ.catch"(i8*, i8*) {
-; CHECK: catch.entry:
+; CHECK: define internal i8* @"\01?test@@YAXXZ.catch"(i8*, i8*) {
+; CHECK: entry:
 ; CHECK:   %eh.alloc = call i8* @llvm.framerecover(i8* bitcast (void ()* @"\01?test@@YAXXZ" to i8*), i8* %1)
 ; CHECK:   %eh.data = bitcast i8* %eh.alloc to %"struct.\01?test@@YAXXZ.ehdata"*
 ; CHECK:   %eh.obj.ptr = getelementptr inbounds %"struct.\01?test@@YAXXZ.ehdata", %"struct.\01?test@@YAXXZ.ehdata"* %eh.data, i32 0, i32 1
@@ -210,13 +210,13 @@
 ; CHECK:   %cmp1 = icmp eq i32 %tmp8, %i.019.reload
 ; CHECK:   br i1 %cmp1, label %if.then, label %if.else
 ;
-; CHECK: if.then:                                          ; preds = %catch.entry
+; CHECK: if.then:                                          ; preds = %entry
 ; CHECK:   %tmp9 = load i32, i32* %b.reload, align 4, !tbaa !8
 ; CHECK:   %add2 = add nsw i32 %tmp9, %i.019.reload
 ; CHECK:   store i32 %add2, i32* %b.reload, align 4, !tbaa !8
 ; CHECK:   br label %if.end
 ;
-; CHECK: if.else:                                          ; preds = %catch.entry
+; CHECK: if.else:                                          ; preds = %entry
 ; CHECK:   %tmp10 = load i32, i32* %a.reload, align 8, !tbaa !2
 ; CHECK:   %add4 = add nsw i32 %tmp10, %tmp8
 ; CHECK:   store i32 %add4, i32* %a.reload, align 8, !tbaa !2