aboutsummaryrefslogtreecommitdiffstats
path: root/test/CodeGen/WinEH
diff options
context:
space:
mode:
Diffstat (limited to 'test/CodeGen/WinEH')
-rw-r--r--test/CodeGen/WinEH/cppeh-catch-all.ll97
-rw-r--r--test/CodeGen/WinEH/cppeh-catch-scalar.ll126
-rw-r--r--test/CodeGen/WinEH/cppeh-catch-unwind.ll244
-rw-r--r--test/CodeGen/WinEH/cppeh-frame-vars.ll272
-rw-r--r--test/CodeGen/WinEH/cppeh-inalloca.ll199
-rw-r--r--test/CodeGen/WinEH/cppeh-min-unwind.ll99
-rw-r--r--test/CodeGen/WinEH/cppeh-nested-1.ll199
-rw-r--r--test/CodeGen/WinEH/cppeh-nested-2.ll324
-rw-r--r--test/CodeGen/WinEH/cppeh-nested-3.ll273
-rw-r--r--test/CodeGen/WinEH/cppeh-nonalloca-frame-values.ll273
-rw-r--r--test/CodeGen/WinEH/lit.local.cfg12
-rw-r--r--test/CodeGen/WinEH/seh-simple.ll138
12 files changed, 2256 insertions, 0 deletions
diff --git a/test/CodeGen/WinEH/cppeh-catch-all.ll b/test/CodeGen/WinEH/cppeh-catch-all.ll
new file mode 100644
index 0000000..6e69862
--- /dev/null
+++ b/test/CodeGen/WinEH/cppeh-catch-all.ll
@@ -0,0 +1,97 @@
+; RUN: opt -mtriple=x86_64-pc-windows-msvc -winehprepare -S -o - < %s | FileCheck %s
+
+; This test is based on the following code:
+;
+; void test()
+; {
+; try {
+; may_throw();
+; } catch (...) {
+; handle_exception();
+; }
+; }
+;
+; Parts of the IR have been hand-edited to simplify the test case.
+; The full IR will be restored when Windows C++ EH support is complete.
+
+; ModuleID = 'catch-all.cpp'
+target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
+target triple = "x86_64-pc-windows-msvc"
+
+; The function entry in this case remains unchanged.
+; CHECK: define void @_Z4testv() #0 {
+; CHECK: entry:
+; CHECK: invoke void @_Z9may_throwv()
+; CHECK: to label %invoke.cont unwind label %[[LPAD_LABEL:lpad[0-9]+]]
+
+; Function Attrs: uwtable
+define void @_Z4testv() #0 {
+entry:
+ %exn.slot = alloca i8*
+ %ehselector.slot = alloca i32
+ invoke void @_Z9may_throwv()
+ to label %invoke.cont unwind label %lpad
+
+invoke.cont: ; preds = %entry
+ br label %try.cont
+
+; CHECK: [[LPAD_LABEL]]:{{[ ]+}}; preds = %entry
+; CHECK: landingpad { i8*, i32 } personality i8* bitcast (i32 (...)* @__CxxFrameHandler3 to i8*)
+; CHECK-NEXT: catch i8* null
+; CHECK-NEXT: [[RECOVER:\%.+]] = call i8* (...)* @llvm.eh.actions(i32 1, i8* null, i8* null, i8* (i8*, i8*)* @_Z4testv.catch)
+; CHECK-NEXT: indirectbr i8* [[RECOVER]], [label %try.cont]
+
+lpad: ; preds = %entry
+ %tmp = landingpad { i8*, i32 } personality i8* bitcast (i32 (...)* @__CxxFrameHandler3 to i8*)
+ catch i8* null
+ %tmp1 = extractvalue { i8*, i32 } %tmp, 0
+ store i8* %tmp1, i8** %exn.slot
+ %tmp2 = extractvalue { i8*, i32 } %tmp, 1
+ store i32 %tmp2, i32* %ehselector.slot
+ br label %catch
+
+; CHECK-NOT: catch:
+
+catch: ; preds = %lpad
+ %exn = load i8*, i8** %exn.slot
+ call void @llvm.eh.begincatch(i8* %exn, i8* null) #2
+ call void @_Z16handle_exceptionv()
+ br label %invoke.cont2
+
+; CHECK-NOT: invoke.cont2:
+
+invoke.cont2: ; preds = %catch
+ call void @llvm.eh.endcatch()
+ br label %try.cont
+
+try.cont: ; preds = %invoke.cont2, %invoke.cont
+ ret void
+
+; CHECK: }
+}
+
+; CHECK: define internal i8* @_Z4testv.catch(i8*, i8*) {
+; CHECK: entry:
+; CHECK: call void @_Z16handle_exceptionv()
+; CHECK: ret i8* blockaddress(@_Z4testv, %try.cont)
+; CHECK: }
+
+declare void @_Z9may_throwv() #1
+
+declare i32 @__CxxFrameHandler3(...)
+
+declare void @llvm.eh.begincatch(i8*, i8*)
+
+declare void @_Z16handle_exceptionv() #1
+
+declare void @llvm.eh.endcatch()
+
+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 = { nounwind }
+attributes #4 = { noreturn nounwind }
+
+!llvm.ident = !{!0}
+
+!0 = !{!"clang version 3.7.0 (trunk 226027)"}
diff --git a/test/CodeGen/WinEH/cppeh-catch-scalar.ll b/test/CodeGen/WinEH/cppeh-catch-scalar.ll
new file mode 100644
index 0000000..0f8a7a8
--- /dev/null
+++ b/test/CodeGen/WinEH/cppeh-catch-scalar.ll
@@ -0,0 +1,126 @@
+; RUN: opt -mtriple=x86_64-pc-windows-msvc -winehprepare -S -o - < %s | FileCheck %s
+
+; This test is based on the following code:
+;
+; void test()
+; {
+; try {
+; may_throw();
+; } catch (int i) {
+; handle_int(i);
+; }
+; }
+;
+; Parts of the IR have been hand-edited to simplify the test case.
+; The full IR will be restored when Windows C++ EH support is complete.
+
+;ModuleID = 'cppeh-catch-scalar.cpp'
+target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
+target triple = "x86_64-pc-windows-msvc"
+
+@_ZTIi = external constant i8*
+
+; The function entry will be rewritten like this.
+; CHECK: define void @_Z4testv() #0 {
+; CHECK: entry:
+; CHECK: [[I_PTR:\%.+]] = alloca i32, align 4
+; CHECK: call void (...)* @llvm.frameescape(i32* [[I_PTR]])
+; CHECK: invoke void @_Z9may_throwv()
+; CHECK: to label %invoke.cont unwind label %[[LPAD_LABEL:lpad[0-9]+]]
+
+; Function Attrs: uwtable
+define void @_Z4testv() #0 {
+entry:
+ %exn.slot = alloca i8*
+ %ehselector.slot = alloca i32
+ %i = alloca i32, align 4
+ invoke void @_Z9may_throwv()
+ to label %invoke.cont unwind label %lpad
+
+invoke.cont: ; preds = %entry
+ br label %try.cont
+
+; CHECK: [[LPAD_LABEL]]:{{[ ]+}}; preds = %entry
+; CHECK: landingpad { i8*, i32 } personality i8* bitcast (i32 (...)* @__CxxFrameHandler3 to i8*)
+; CHECK-NEXT: catch i8* bitcast (i8** @_ZTIi to i8*)
+; CHECK-NEXT: [[RECOVER:\%.+]] = call i8* (...)* @llvm.eh.actions(i32 1, i8* bitcast (i8** @_ZTIi to i8*), i32* %i, i8* (i8*, i8*)* @_Z4testv.catch)
+; CHECK-NEXT: indirectbr i8* [[RECOVER]], [label %try.cont]
+
+lpad: ; preds = %entry
+ %tmp = landingpad { i8*, i32 } personality i8* bitcast (i32 (...)* @__CxxFrameHandler3 to i8*)
+ catch i8* bitcast (i8** @_ZTIi to i8*)
+ %tmp1 = extractvalue { i8*, i32 } %tmp, 0
+ store i8* %tmp1, i8** %exn.slot
+ %tmp2 = extractvalue { i8*, i32 } %tmp, 1
+ store i32 %tmp2, i32* %ehselector.slot
+ br label %catch.dispatch
+
+; CHECK-NOT: catch-dispatch:
+
+catch.dispatch: ; preds = %lpad
+ %sel = load i32, i32* %ehselector.slot
+ %tmp3 = call i32 @llvm.eh.typeid.for(i8* bitcast (i8** @_ZTIi to i8*)) #3
+ %matches = icmp eq i32 %sel, %tmp3
+ br i1 %matches, label %catch, label %eh.resume
+
+; CHECK-NOT: catch:
+
+catch: ; preds = %catch.dispatch
+ %exn11 = load i8*, i8** %exn.slot
+ %i.i8 = bitcast i32* %i to i8*
+ call void @llvm.eh.begincatch(i8* %exn11, i8* %i.i8) #3
+ %tmp7 = load i32, i32* %i, align 4
+ call void @_Z10handle_inti(i32 %tmp7)
+ br label %invoke.cont2
+
+; CHECK-NOT: invoke.cont2:
+
+invoke.cont2: ; preds = %catch
+ call void @llvm.eh.endcatch() #3
+ br label %try.cont
+
+try.cont: ; preds = %invoke.cont2, %invoke.cont
+ ret void
+
+; CHECK-NOT: eh.resume:
+
+eh.resume: ; preds = %catch.dispatch
+ %exn3 = load i8*, i8** %exn.slot
+ %sel4 = load i32, i32* %ehselector.slot
+ %lpad.val = insertvalue { i8*, i32 } undef, i8* %exn3, 0
+ %lpad.val5 = insertvalue { i8*, i32 } %lpad.val, i32 %sel4, 1
+ resume { i8*, i32 } %lpad.val5
+
+; CHECK: }
+}
+
+; CHECK: define internal i8* @_Z4testv.catch(i8*, i8*) {
+; CHECK: entry:
+; CHECK: [[RECOVER_I:\%.+]] = call i8* @llvm.framerecover(i8* bitcast (void ()* @_Z4testv to i8*), i8* %1, i32 0)
+; CHECK: [[I_PTR1:\%.+]] = bitcast i8* [[RECOVER_I]] to i32*
+; CHECK: [[TMP:\%.+]] = load i32, i32* [[I_PTR1]], align 4
+; CHECK: call void @_Z10handle_inti(i32 [[TMP]])
+; CHECK: ret i8* blockaddress(@_Z4testv, %try.cont)
+; CHECK: }
+
+declare void @_Z9may_throwv() #1
+
+declare i32 @__CxxFrameHandler3(...)
+
+; Function Attrs: nounwind readnone
+declare i32 @llvm.eh.typeid.for(i8*) #2
+
+declare void @llvm.eh.begincatch(i8*, i8*)
+
+declare void @llvm.eh.endcatch()
+
+declare void @_Z10handle_inti(i32) #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 = { nounwind readnone }
+attributes #3 = { nounwind }
+
+!llvm.ident = !{!0}
+
+!0 = !{!"clang version 3.7.0 (trunk 227474) (llvm/trunk 227508)"}
diff --git a/test/CodeGen/WinEH/cppeh-catch-unwind.ll b/test/CodeGen/WinEH/cppeh-catch-unwind.ll
new file mode 100644
index 0000000..3db1635
--- /dev/null
+++ b/test/CodeGen/WinEH/cppeh-catch-unwind.ll
@@ -0,0 +1,244 @@
+; RUN: opt -mtriple=x86_64-pc-windows-msvc -winehprepare -S -o - < %s | FileCheck %s
+
+; This test was generated from the following source:
+;
+; void test() {
+; try {
+; SomeClass obj;
+; may_throw();
+; try {
+; may_throw();
+; } catch (int) {
+; handle_exception();
+; }
+; } catch (int) {
+; handle_exception();
+; }
+; }
+;
+; The code above was compiled with the -O2 option.
+
+; ModuleID = 'catch-unwind.cpp'
+target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
+target triple = "x86_64-pc-windows-msvc"
+
+%rtti.TypeDescriptor2 = type { i8**, i8*, [3 x i8] }
+%class.SomeClass = type { i8 }
+
+$"\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
+
+
+; CHECK-LABEL: define void @"\01?test@@YAXXZ"() #0 {
+; CHECK: entry:
+; CHECK: [[UNWIND_HELP:\%.+]] = alloca i64
+; CHECK: [[OBJ_PTR:\%.+]] = alloca %class.SomeClass
+; CHECK: [[TMP0:\%.+]] = alloca i32, align 4
+; CHECK: [[TMP1:\%.+]] = alloca i32, align 4
+; CHECK: call void (...)* @llvm.frameescape(i32* [[TMP1]], %class.SomeClass* [[OBJ_PTR]], i32* [[TMP0]])
+; CHECK: [[UNWIND_HELP_i8:\%.+]] = bitcast i64* [[UNWIND_HELP]] to i8*
+; CHECK: call void @llvm.eh.unwindhelp(i8* [[UNWIND_HELP_i8]])
+; CHECK: %call = invoke %class.SomeClass* @"\01??0SomeClass@@QEAA@XZ"(%class.SomeClass* %obj)
+; CHECK: to label %invoke.cont unwind label %[[LPAD_LABEL:lpad[0-9]+]]
+
+; Function Attrs: uwtable
+define void @"\01?test@@YAXXZ"() #0 {
+entry:
+ %obj = alloca %class.SomeClass, align 1
+ %0 = alloca i32, align 4
+ %1 = alloca i32, align 4
+ %call = invoke %class.SomeClass* @"\01??0SomeClass@@QEAA@XZ"(%class.SomeClass* %obj)
+ to label %invoke.cont unwind label %lpad
+
+; CHECK: invoke.cont:
+; CHECK: invoke void @"\01?may_throw@@YAXXZ"()
+; CHECK: to label %invoke.cont2 unwind label %[[LPAD1_LABEL:lpad[0-9]+]]
+
+invoke.cont: ; preds = %entry
+ invoke void @"\01?may_throw@@YAXXZ"()
+ to label %invoke.cont2 unwind label %lpad1
+
+; CHECK: invoke.cont2:
+; CHECK: invoke void @"\01?may_throw@@YAXXZ"()
+; CHECK: to label %try.cont unwind label %[[LPAD3_LABEL:lpad[0-9]+]]
+
+invoke.cont2: ; preds = %invoke.cont
+ invoke void @"\01?may_throw@@YAXXZ"()
+ to label %try.cont unwind label %lpad3
+
+; CHECK: [[LPAD_LABEL]]:{{[ ]+}}; preds = %entry
+; CHECK: [[LPAD_VAL:\%.+]] = landingpad { i8*, i32 } personality i8* bitcast (i32 (...)* @__CxxFrameHandler3 to i8*)
+; CHECK-NEXT: catch i8* bitcast (%rtti.TypeDescriptor2* @"\01??_R0H@8" to i8*)
+; CHECK-NEXT: [[RECOVER:\%.+]] = call i8* (...)* @llvm.eh.actions(i32 1, i8* bitcast (%rtti.TypeDescriptor2* @"\01??_R0H@8" to i8*), i32* [[TMP1]], i8* (i8*, i8*)* @"\01?test@@YAXXZ.catch")
+; CHECK-NEXT: indirectbr i8* [[RECOVER]], [label %try.cont15]
+
+lpad: ; preds = %entry
+ %2 = landingpad { i8*, i32 } personality i8* bitcast (i32 (...)* @__CxxFrameHandler3 to i8*)
+ catch i8* bitcast (%rtti.TypeDescriptor2* @"\01??_R0H@8" to i8*)
+ %3 = extractvalue { i8*, i32 } %2, 0
+ %4 = extractvalue { i8*, i32 } %2, 1
+ br label %catch.dispatch7
+
+; CHECK: [[LPAD1_LABEL]]:{{[ ]+}}; preds = %invoke.cont
+; CHECK: [[LPAD1_VAL:\%.+]] = landingpad { i8*, i32 } personality i8* bitcast (i32 (...)* @__CxxFrameHandler3 to i8*)
+; CHECK-NEXT: cleanup
+; CHECK-NEXT: catch i8* bitcast (%rtti.TypeDescriptor2* @"\01??_R0H@8" to i8*)
+; CHECK-NEXT: [[RECOVER1:\%.+]] = call i8* (...)* @llvm.eh.actions(i32 0, void (i8*, i8*)* @"\01?test@@YAXXZ.cleanup", i32 1, i8* bitcast (%rtti.TypeDescriptor2* @"\01??_R0H@8" to i8*), i32* [[TMP1]], i8* (i8*, i8*)* @"\01?test@@YAXXZ.catch")
+; CHECK-NEXT: indirectbr i8* [[RECOVER1]], [label %try.cont15]
+
+lpad1: ; preds = %invoke.cont
+ %5 = landingpad { i8*, i32 } personality i8* bitcast (i32 (...)* @__CxxFrameHandler3 to i8*)
+ cleanup
+ catch i8* bitcast (%rtti.TypeDescriptor2* @"\01??_R0H@8" to i8*)
+ %6 = extractvalue { i8*, i32 } %5, 0
+ %7 = extractvalue { i8*, i32 } %5, 1
+ br label %ehcleanup
+
+; CHECK: [[LPAD3_LABEL]]:{{[ ]+}}; preds = %invoke.cont2
+; CHECK: [[LPAD3_VAL:\%.+]] = landingpad { i8*, i32 } personality i8* bitcast (i32 (...)* @__CxxFrameHandler3 to i8*)
+; 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* [[TMP0]], i8* (i8*, i8*)* @"\01?test@@YAXXZ.catch1", i32 0, void (i8*, i8*)* @"\01?test@@YAXXZ.cleanup")
+; CHECK-NEXT: indirectbr i8* [[RECOVER3]], [label %try.cont]
+
+lpad3: ; preds = %invoke.cont2
+ %8 = landingpad { i8*, i32 } personality i8* bitcast (i32 (...)* @__CxxFrameHandler3 to i8*)
+ cleanup
+ catch i8* bitcast (%rtti.TypeDescriptor2* @"\01??_R0H@8" to i8*)
+ %9 = extractvalue { i8*, i32 } %8, 0
+ %10 = extractvalue { i8*, i32 } %8, 1
+ %11 = call i32 @llvm.eh.typeid.for(i8* bitcast (%rtti.TypeDescriptor2* @"\01??_R0H@8" to i8*)) #3
+ %matches = icmp eq i32 %10, %11
+ br i1 %matches, label %catch, label %ehcleanup
+
+; CHECK-NOT: catch:
+catch: ; preds = %lpad3
+ %12 = bitcast i32* %0 to i8*
+ call void @llvm.eh.begincatch(i8* %9, i8* %12) #3
+ invoke void @"\01?handle_exception@@YAXXZ"()
+ to label %invoke.cont6 unwind label %lpad5
+
+; CHECK-NOT: invoke.cont6:
+invoke.cont6: ; preds = %catch
+ call void @llvm.eh.endcatch() #3
+ br label %try.cont
+
+try.cont: ; preds = %invoke.cont2, %invoke.cont6
+ call void @"\01??1SomeClass@@QEAA@XZ"(%class.SomeClass* %obj) #3
+ br label %try.cont15
+
+; CHECK-NOT: lpad5:
+lpad5: ; preds = %catch
+ %13 = landingpad { i8*, i32 } personality i8* bitcast (i32 (...)* @__CxxFrameHandler3 to i8*)
+ cleanup
+ catch i8* bitcast (%rtti.TypeDescriptor2* @"\01??_R0H@8" to i8*)
+ %14 = extractvalue { i8*, i32 } %13, 0
+ %15 = extractvalue { i8*, i32 } %13, 1
+ call void @llvm.eh.endcatch() #3
+ br label %ehcleanup
+
+; CHECK-NOT: ehcleanup
+ehcleanup: ; preds = %lpad5, %lpad3, %lpad1
+ %exn.slot.0 = phi i8* [ %14, %lpad5 ], [ %9, %lpad3 ], [ %6, %lpad1 ]
+ %ehselector.slot.0 = phi i32 [ %15, %lpad5 ], [ %10, %lpad3 ], [ %7, %lpad1 ]
+ call void @"\01??1SomeClass@@QEAA@XZ"(%class.SomeClass* %obj) #3
+ br label %catch.dispatch7
+
+; CHECK-NOT: catch.dispatch7:
+catch.dispatch7: ; preds = %ehcleanup, %lpad
+ %exn.slot.1 = phi i8* [ %exn.slot.0, %ehcleanup ], [ %3, %lpad ]
+ %ehselector.slot.1 = phi i32 [ %ehselector.slot.0, %ehcleanup ], [ %4, %lpad ]
+ %16 = call i32 @llvm.eh.typeid.for(i8* bitcast (%rtti.TypeDescriptor2* @"\01??_R0H@8" to i8*)) #3
+ %matches9 = icmp eq i32 %ehselector.slot.1, %16
+ br i1 %matches9, label %catch10, label %eh.resume
+
+; CHECK-NOT: catch10:
+catch10: ; preds = %catch.dispatch7
+ %17 = bitcast i32* %1 to i8*
+ call void @llvm.eh.begincatch(i8* %exn.slot.1, i8* %17) #3
+ call void @"\01?handle_exception@@YAXXZ"()
+ br label %invoke.cont13
+
+; CHECK-NOT: invoke.cont13:
+invoke.cont13: ; preds = %catch10
+ call void @llvm.eh.endcatch() #3
+ br label %try.cont15
+
+try.cont15: ; preds = %invoke.cont13, %try.cont
+ ret void
+
+; CHECK-NOT: eh.resume
+eh.resume: ; preds = %catch.dispatch7
+ %lpad.val = insertvalue { i8*, i32 } undef, i8* %exn.slot.1, 0
+ %lpad.val18 = insertvalue { i8*, i32 } %lpad.val, i32 %ehselector.slot.1, 1
+ resume { i8*, i32 } %lpad.val18
+
+; CHECK: }
+}
+
+; CHECK-LABEL: define internal i8* @"\01?test@@YAXXZ.catch"(i8*, i8*) {
+; CHECK: entry:
+; CHECK: [[RECOVER_TMP1:\%.+]] = call i8* @llvm.framerecover(i8* bitcast (void ()* @"\01?test@@YAXXZ" to i8*), i8* %1, i32 0)
+; CHECK: [[TMP1_PTR:\%.+]] = bitcast i8* [[RECOVER_TMP1]] to i32*
+; CHECK: call void @"\01?handle_exception@@YAXXZ"()
+; CHECK: ret i8* blockaddress(@"\01?test@@YAXXZ", %try.cont15)
+; CHECK: }
+
+; CHECK-LABEL: define internal void @"\01?test@@YAXXZ.cleanup"(i8*, i8*) {
+; CHECK: entry:
+; CHECK: [[RECOVER_OBJ:\%.+]] = call i8* @llvm.framerecover(i8* bitcast (void ()* @"\01?test@@YAXXZ" to i8*), i8* %1, i32 1)
+; CHECK: [[OBJ_PTR:\%.+]] = bitcast i8* %obj.i8 to %class.SomeClass*
+; CHECK: call void @"\01??1SomeClass@@QEAA@XZ"(%class.SomeClass* [[OBJ_PTR]]) #3
+; CHECK: ret void
+; CHECK: }
+
+; CHECK-LABEL: define internal i8* @"\01?test@@YAXXZ.catch1"(i8*, i8*) {
+; CHECK: entry:
+; CHECK: [[RECOVER_TMP0:\%.+]] = call i8* @llvm.framerecover(i8* bitcast (void ()* @"\01?test@@YAXXZ" to i8*), i8* %1, i32 2)
+; CHECK: [[TMP0_PTR:\%.+]] = bitcast i8* [[RECOVER_TMP0]] to i32*
+; CHECK: invoke void @"\01?handle_exception@@YAXXZ"()
+; CHECK: to label %invoke.cont6 unwind label %[[LPAD5_LABEL:lpad[0-9]+]]
+;
+; CHECK: invoke.cont6: ; preds = %entry
+; CHECK: ret i8* blockaddress(@"\01?test@@YAXXZ", %try.cont)
+;
+; CHECK: [[LPAD5_LABEL]]:{{[ ]+}}; preds = %entry
+; CHECK: [[LPAD5_VAL:\%.+]] = landingpad { i8*, i32 } personality i8* bitcast (i32 (...)* @__CxxFrameHandler3 to i8*)
+; CHECK: cleanup
+; CHECK: catch i8* bitcast (%rtti.TypeDescriptor2* @"\01??_R0H@8" to i8*)
+; CHECK: unreachable
+; CHECK: }
+
+declare %class.SomeClass* @"\01??0SomeClass@@QEAA@XZ"(%class.SomeClass* returned) #1
+
+declare i32 @__CxxFrameHandler3(...)
+
+declare void @"\01?may_throw@@YAXXZ"() #1
+
+; 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
+
+declare void @"\01?handle_exception@@YAXXZ"() #1
+
+; Function Attrs: nounwind
+declare void @llvm.eh.endcatch() #3
+
+; Function Attrs: nounwind
+declare void @"\01??1SomeClass@@QEAA@XZ"(%class.SomeClass*) #4
+
+attributes #0 = { 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" "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" "stack-protector-buffer-size"="8" "unsafe-fp-math"="false" "use-soft-float"="false" }
+attributes #2 = { nounwind readnone }
+attributes #3 = { nounwind }
+attributes #4 = { nounwind "less-precise-fpmad"="false" "no-frame-pointer-elim"="false" "no-infs-fp-math"="false" "no-nans-fp-math"="false" "stack-protector-buffer-size"="8" "unsafe-fp-math"="false" "use-soft-float"="false" }
+
+!llvm.module.flags = !{!0}
+!llvm.ident = !{!1}
+
+!0 = !{i32 1, !"PIC Level", i32 2}
+!1 = !{!"clang version 3.7.0 (trunk 232069) (llvm/trunk 232070)"}
diff --git a/test/CodeGen/WinEH/cppeh-frame-vars.ll b/test/CodeGen/WinEH/cppeh-frame-vars.ll
new file mode 100644
index 0000000..dc5ed1c
--- /dev/null
+++ b/test/CodeGen/WinEH/cppeh-frame-vars.ll
@@ -0,0 +1,272 @@
+; RUN: opt -mtriple=x86_64-pc-windows-msvc -winehprepare -S -o - < %s | FileCheck %s
+
+; This test is based on the following code:
+;
+; struct SomeData {
+; int a;
+; int b;
+; };
+;
+; void may_throw();
+; void does_not_throw(int i);
+; void dump(int *, int, SomeData&);
+;
+; void test() {
+; int NumExceptions = 0;
+; int ExceptionVal[10];
+; SomeData Data = { 0, 0 };
+;
+; for (int i = 0; i < 10; ++i) {
+; try {
+; may_throw();
+; Data.a += i;
+; }
+; catch (int e) {
+; ExceptionVal[NumExceptions] = e;
+; ++NumExceptions;
+; if (e == i)
+; Data.b += e;
+; else
+; Data.a += e;
+; }
+; does_not_throw(NumExceptions);
+; }
+; dump(ExceptionVal, NumExceptions, Data);
+; }
+
+; ModuleID = 'cppeh-frame-vars.cpp'
+target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
+target triple = "x86_64-pc-windows-msvc"
+
+%rtti.TypeDescriptor2 = type { i8**, i8*, [3 x i8] }
+%struct.SomeData = type { 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
+
+; The function entry should be rewritten like this.
+; CHECK: define void @"\01?test@@YAXXZ"() #0 {
+; CHECK: entry:
+; CHECK: [[NUMEXCEPTIONS_PTR:\%.+]] = alloca i32, align 4
+; CHECK: [[EXCEPTIONVAL_PTR:\%.+]] = alloca [10 x i32], align 16
+; CHECK: [[DATA_PTR:\%.+]] = alloca %struct.SomeData, align 4
+; CHECK: [[I_PTR:\%.+]] = alloca i32, align 4
+; CHECK: [[E_PTR:\%.+]] = alloca i32, align 4
+; CHECK: store i32 0, i32* [[NUMEXCEPTIONS_PTR]], align 4
+; CHECK: [[TMP:\%.+]] = bitcast %struct.SomeData* [[DATA_PTR]] to i8*
+; CHECK: call void @llvm.memset(i8* [[TMP]], i8 0, i64 8, i32 4, i1 false)
+; CHECK: store i32 0, i32* [[I_PTR]], align 4
+; CHECK: call void (...)* @llvm.frameescape(i32* [[E_PTR]], i32* [[NUMEXCEPTIONS_PTR]], [10 x i32]* [[EXCEPTIONVAL_PTR]], i32* [[I_PTR]], %struct.SomeData* [[DATA_PTR]])
+; CHECK: br label %for.cond
+
+; Function Attrs: uwtable
+define void @"\01?test@@YAXXZ"() #0 {
+entry:
+ %NumExceptions = alloca i32, align 4
+ %ExceptionVal = alloca [10 x i32], align 16
+ %Data = alloca %struct.SomeData, align 4
+ %i = alloca i32, align 4
+ %exn.slot = alloca i8*
+ %ehselector.slot = alloca i32
+ %e = alloca i32, align 4
+ store i32 0, i32* %NumExceptions, align 4
+ %tmp = bitcast %struct.SomeData* %Data to i8*
+ call void @llvm.memset(i8* %tmp, i8 0, i64 8, i32 4, i1 false)
+ store i32 0, i32* %i, align 4
+ br label %for.cond
+
+for.cond: ; preds = %for.inc, %entry
+ %tmp1 = load i32, i32* %i, align 4
+ %cmp = icmp slt i32 %tmp1, 10
+ br i1 %cmp, label %for.body, label %for.end
+
+; CHECK: for.body:
+; CHECK: invoke void @"\01?may_throw@@YAXXZ"()
+; CHECK: to label %invoke.cont unwind label %[[LPAD_LABEL:lpad[0-9]+]]
+
+for.body: ; preds = %for.cond
+ invoke void @"\01?may_throw@@YAXXZ"()
+ to label %invoke.cont unwind label %lpad
+
+invoke.cont: ; preds = %for.body
+ %tmp2 = load i32, i32* %i, align 4
+ %a = getelementptr inbounds %struct.SomeData, %struct.SomeData* %Data, i32 0, i32 0
+ %tmp3 = load i32, i32* %a, align 4
+ %add = add nsw i32 %tmp3, %tmp2
+ store i32 %add, i32* %a, align 4
+ br label %try.cont
+
+; CHECK: [[LPAD_LABEL]]:{{[ ]+}}; preds = %for.body
+; CHECK: landingpad { i8*, i32 } personality i8* bitcast (i32 (...)* @__CxxFrameHandler3 to i8*)
+; CHECK-NEXT: catch i8* bitcast (%rtti.TypeDescriptor2* @"\01??_R0H@8" to i8*)
+; CHECK-NEXT: [[RECOVER:\%.+]] = call i8* (...)* @llvm.eh.actions(i32 1, i8* bitcast (%rtti.TypeDescriptor2* @"\01??_R0H@8" to i8*), i32* %e, i8* (i8*, i8*)* @"\01?test@@YAXXZ.catch")
+; CHECK-NEXT: indirectbr i8* [[RECOVER]], [label %try.cont]
+
+lpad: ; preds = %for.body
+ %tmp4 = landingpad { i8*, i32 } personality i8* bitcast (i32 (...)* @__CxxFrameHandler3 to i8*)
+ catch i8* bitcast (%rtti.TypeDescriptor2* @"\01??_R0H@8" to i8*)
+ %tmp5 = extractvalue { i8*, i32 } %tmp4, 0
+ store i8* %tmp5, i8** %exn.slot
+ %tmp6 = extractvalue { i8*, i32 } %tmp4, 1
+ store i32 %tmp6, i32* %ehselector.slot
+ br label %catch.dispatch
+
+; CHECK-NOT: catch.dispatch:
+
+catch.dispatch: ; preds = %lpad
+ %sel = load i32, i32* %ehselector.slot
+ %tmp7 = call i32 @llvm.eh.typeid.for(i8* bitcast (%rtti.TypeDescriptor2* @"\01??_R0H@8" to i8*)) #1
+ %matches = icmp eq i32 %sel, %tmp7
+ br i1 %matches, label %catch, label %eh.resume
+
+; CHECK-NOT: catch:
+
+catch: ; preds = %catch.dispatch
+ %exn = load i8*, i8** %exn.slot
+ %e.i8 = bitcast i32* %e to i8*
+ call void @llvm.eh.begincatch(i8* %exn, i8* %e.i8) #1
+ %tmp11 = load i32, i32* %e, align 4
+ %tmp12 = load i32, i32* %NumExceptions, align 4
+ %idxprom = sext i32 %tmp12 to i64
+ %arrayidx = getelementptr inbounds [10 x i32], [10 x i32]* %ExceptionVal, i32 0, i64 %idxprom
+ store i32 %tmp11, i32* %arrayidx, align 4
+ %tmp13 = load i32, i32* %NumExceptions, align 4
+ %inc = add nsw i32 %tmp13, 1
+ store i32 %inc, i32* %NumExceptions, align 4
+ %tmp14 = load i32, i32* %e, align 4
+ %tmp15 = load i32, i32* %i, align 4
+ %cmp1 = icmp eq i32 %tmp14, %tmp15
+ br i1 %cmp1, label %if.then, label %if.else
+
+; CHECK-NOT: if.then:
+
+if.then: ; preds = %catch
+ %tmp16 = load i32, i32* %e, align 4
+ %b = getelementptr inbounds %struct.SomeData, %struct.SomeData* %Data, i32 0, i32 1
+ %tmp17 = load i32, i32* %b, align 4
+ %add2 = add nsw i32 %tmp17, %tmp16
+ store i32 %add2, i32* %b, align 4
+ br label %if.end
+
+; CHECK-NOT: if.else:
+
+if.else: ; preds = %catch
+ %tmp18 = load i32, i32* %e, align 4
+ %a3 = getelementptr inbounds %struct.SomeData, %struct.SomeData* %Data, i32 0, i32 0
+ %tmp19 = load i32, i32* %a3, align 4
+ %add4 = add nsw i32 %tmp19, %tmp18
+ store i32 %add4, i32* %a3, align 4
+ br label %if.end
+
+; CHECK-NOT: if.end:
+
+if.end: ; preds = %if.else, %if.then
+ call void @llvm.eh.endcatch() #1
+ br label %try.cont
+
+try.cont: ; preds = %if.end, %invoke.cont
+ %tmp20 = load i32, i32* %NumExceptions, align 4
+ call void @"\01?does_not_throw@@YAXH@Z"(i32 %tmp20)
+ br label %for.inc
+
+for.inc: ; preds = %try.cont
+ %tmp21 = load i32, i32* %i, align 4
+ %inc5 = add nsw i32 %tmp21, 1
+ store i32 %inc5, i32* %i, align 4
+ br label %for.cond
+
+for.end: ; preds = %for.cond
+ %tmp22 = load i32, i32* %NumExceptions, align 4
+ %arraydecay = getelementptr inbounds [10 x i32], [10 x i32]* %ExceptionVal, i32 0, i32 0
+ call void @"\01?dump@@YAXPEAHHAEAUSomeData@@@Z"(i32* %arraydecay, i32 %tmp22, %struct.SomeData* dereferenceable(8) %Data)
+ ret void
+
+; CHECK-NOT: eh.resume:
+
+eh.resume: ; preds = %catch.dispatch
+ %exn6 = load i8*, i8** %exn.slot
+ %sel7 = load i32, i32* %ehselector.slot
+ %lpad.val = insertvalue { i8*, i32 } undef, i8* %exn6, 0
+ %lpad.val8 = insertvalue { i8*, i32 } %lpad.val, i32 %sel7, 1
+ resume { i8*, i32 } %lpad.val8
+
+; CHECK: }
+}
+
+; The following catch handler should be outlined.
+; CHECK-LABEL: define internal i8* @"\01?test@@YAXXZ.catch"(i8*, i8*) {
+; CHECK: entry:
+; CHECK: [[RECOVER_E:\%.+]] = call i8* @llvm.framerecover(i8* bitcast (void ()* @"\01?test@@YAXXZ" to i8*), i8* %1, i32 0)
+; CHECK: [[E_PTR1:\%.+]] = bitcast i8* [[RECOVER_E]] to i32*
+; CHECK: [[RECOVER_NUMEXCEPTIONS:\%.+]] = call i8* @llvm.framerecover(i8* bitcast (void ()* @"\01?test@@YAXXZ" to i8*), i8* %1, i32 1)
+; CHECK: [[NUMEXCEPTIONS_PTR1:\%.+]] = bitcast i8* [[RECOVER_NUMEXCEPTIONS]] to i32*
+; CHECK: [[RECOVER_EXCEPTIONVAL:\%.+]] = call i8* @llvm.framerecover(i8* bitcast (void ()* @"\01?test@@YAXXZ" to i8*), i8* %1, i32 2)
+; CHECK: [[EXCEPTIONVAL_PTR1:\%.+]] = bitcast i8* [[RECOVER_EXCEPTIONVAL]] to [10 x i32]*
+; CHECK: [[RECOVER_I:\%.+]] = call i8* @llvm.framerecover(i8* bitcast (void ()* @"\01?test@@YAXXZ" to i8*), i8* %1, i32 3)
+; CHECK: [[I_PTR1:\%.+]] = bitcast i8* [[RECOVER_I]] to i32*
+; CHECK: [[RECOVER_DATA:\%.+]] = call i8* @llvm.framerecover(i8* bitcast (void ()* @"\01?test@@YAXXZ" to i8*), i8* %1, i32 4)
+; CHECK: [[DATA_PTR1:\%.+]] = bitcast i8* [[RECOVER_DATA]] to %struct.SomeData*
+; CHECK: [[TMP:\%.+]] = load i32, i32* [[E_PTR1]], align 4
+; CHECK: [[TMP1:\%.+]] = load i32, i32* [[NUMEXCEPTIONS_PTR]], align 4
+; CHECK: [[IDXPROM:\%.+]] = sext i32 [[TMP1]] to i64
+; CHECK: [[ARRAYIDX:\%.+]] = getelementptr inbounds [10 x i32], [10 x i32]* [[EXCEPTIONVAL_PTR1]], i32 0, i64 [[IDXPROM]]
+; CHECK: store i32 [[TMP]], i32* [[ARRAYIDX]], align 4
+; CHECK: [[TMP2:\%.+]] = load i32, i32* [[NUMEXCEPTIONS_PTR1]], align 4
+; CHECK: [[INC:\%.+]] = add nsw i32 [[TMP2]], 1
+; CHECK: store i32 [[INC]], i32* [[NUMEXCEPTIONS_PTR]], align 4
+; CHECK: [[TMP3:\%.+]] = load i32, i32* [[E_PTR1]], align 4
+; CHECK: [[TMP4:\%.+]] = load i32, i32* [[I_PTR1]], align 4
+; CHECK: [[CMP:\%.+]] = icmp eq i32 [[TMP3]], [[TMP4]]
+; CHECK: br i1 [[CMP]], label %if.then, label %if.else
+;
+; CHECK: if.then: ; preds = %entry
+; CHECK: [[TMP5:\%.+]] = load i32, i32* [[E_PTR1]], align 4
+; CHECK: [[B_PTR:\%.+]] = getelementptr inbounds %struct.SomeData, %struct.SomeData* [[DATA_PTR1]], i32 0, i32 1
+; CHECK: [[TMP6:\%.+]] = load i32, i32* [[B_PTR]], align 4
+; CHECK: %add2 = add nsw i32 [[TMP6]], [[TMP5]]
+; CHECK: store i32 [[ADD:\%.+]], i32* [[B_PTR]], align 4
+; CHECK: br label %if.end
+;
+; CHECK: if.else: ; preds = %entry
+; CHECK: [[TMP7:\%.+]] = load i32, i32* %e, align 4
+; CHECK: [[A3:\%.+]] = getelementptr inbounds %struct.SomeData, %struct.SomeData* %Data, i32 0, i32 0
+; CHECK: [[TMP8:\%.+]] = load i32, i32* %a3, align 4
+; CHECK: [[ADD1:\%.+]] = add nsw i32 [[TMP8]], [[TMP7]]
+; CHECK: store i32 [[ADD1]], i32* [[A3]], align 4
+; CHECK: br label %if.end
+;
+; CHECK: if.end: ; preds = %if.else, %if.then
+; CHECK: ret i8* blockaddress(@"\01?test@@YAXXZ", %try.cont)
+; CHECK: }
+
+
+; Function Attrs: nounwind
+declare void @llvm.memset(i8* nocapture, i8, i64, i32, i1) #1
+
+declare void @"\01?may_throw@@YAXXZ"() #2
+
+declare i32 @__CxxFrameHandler3(...)
+
+; Function Attrs: nounwind readnone
+declare i32 @llvm.eh.typeid.for(i8*) #3
+
+declare void @llvm.eh.begincatch(i8*, i8*)
+
+declare void @llvm.eh.endcatch()
+
+declare void @"\01?does_not_throw@@YAXH@Z"(i32) #2
+
+declare void @"\01?dump@@YAXPEAHHAEAUSomeData@@@Z"(i32*, i32, %struct.SomeData* dereferenceable(8)) #2
+
+attributes #0 = { 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" "unsafe-fp-math"="false" "use-soft-float"="false" }
+attributes #1 = { nounwind }
+attributes #2 = { "less-precise-fpmad"="false" "no-frame-pointer-elim"="false" "no-infs-fp-math"="false" "no-nans-fp-math"="false" "stack-protector-buffer-size"="8" "unsafe-fp-math"="false" "use-soft-float"="false" }
+attributes #3 = { nounwind readnone }
+
+!llvm.module.flags = !{!0}
+!llvm.ident = !{!1}
+
+!0 = !{i32 1, !"PIC Level", i32 2}
+!1 = !{!"clang version 3.7.0 (trunk 228868)"}
diff --git a/test/CodeGen/WinEH/cppeh-inalloca.ll b/test/CodeGen/WinEH/cppeh-inalloca.ll
new file mode 100644
index 0000000..f3f3631
--- /dev/null
+++ b/test/CodeGen/WinEH/cppeh-inalloca.ll
@@ -0,0 +1,199 @@
+; RUN: opt -mtriple=i386-pc-windows-msvc -winehprepare -S -o - < %s | FileCheck %s
+
+; This test is built from the following code:
+; struct A {
+; A(int a);
+; A(const A &o);
+; ~A();
+; int a;
+; };
+;
+; void may_throw();
+;
+; int test(A a) {
+; try {
+; may_throw();
+; }
+; catch (int e) {
+; return a.a + e;
+; }
+; return 0;
+; }
+;
+; The test was built for a 32-bit Windows target and then the reference to
+; the inalloca instruction was manually sunk into the landingpad.
+
+; ModuleID = 'cppeh-inalloca.cpp'
+target datalayout = "e-m:w-p:32:32-i64:64-f80:32-n8:16:32-S32"
+target triple = "i386-pc-windows-msvc"
+
+%rtti.TypeDescriptor2 = type { i8**, i8*, [3 x i8] }
+%struct.A = type { 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
+
+; The function entry should be rewritten like this.
+; CHECK: define i32 @"\01?test@@YAHUA@@@Z"(<{ %struct.A }>* inalloca) #0 {
+; CHECK: entry:
+; CHECK: [[TMP_REGMEM:\%.+]] = alloca <{ %struct.A }>*
+; CHECK: [[TMP:\%.+]] = select i1 true, <{ %struct.A }>* %0, <{ %struct.A }>* undef
+; CHECK: store <{ %struct.A }>* [[TMP]], <{ %struct.A }>** [[TMP_REGMEM]]
+; CHECK: [[RETVAL:\%.+]] = alloca i32, align 4
+; CHECK: [[E_PTR:\%.+]] = alloca i32, align 4
+; CHECK: [[CLEANUP_SLOT:\%.+]] = alloca i32
+; CHECK: call void (...)* @llvm.frameescape(i32* %e, <{ %struct.A }>** [[TMP_REGMEM]], i32* [[RETVAL]], i32* [[CLEANUP_SLOT]])
+; CHECK: invoke void @"\01?may_throw@@YAXXZ"()
+; CHECK: to label %invoke.cont unwind label %[[LPAD_LABEL:lpad[0-9]*]]
+
+define i32 @"\01?test@@YAHUA@@@Z"(<{ %struct.A }>* inalloca) #0 {
+entry:
+ %retval = alloca i32, align 4
+ %exn.slot = alloca i8*
+ %ehselector.slot = alloca i32
+ %e = alloca i32, align 4
+ %cleanup.dest.slot = alloca i32
+ invoke void @"\01?may_throw@@YAXXZ"()
+ to label %invoke.cont unwind label %lpad
+
+invoke.cont: ; preds = %entry
+ br label %try.cont
+
+; CHECK: [[LPAD_LABEL]]:{{[ ]+}}; preds = %entry
+; CHECK: landingpad { i8*, i32 } personality i8* bitcast (i32 (...)* @__CxxFrameHandler3 to i8*)
+; CHECK-NEXT: cleanup
+; CHECK-NEXT: catch i8* bitcast (%rtti.TypeDescriptor2* @"\01??_R0H@8" to i8*)
+; CHECK-NEXT: [[RECOVER:\%recover.*]] = call i8* (...)* @llvm.eh.actions(i32 1, i8* bitcast (%rtti.TypeDescriptor2* @"\01??_R0H@8" to i8*), i32* %e, i8* (i8*, i8*)* @"\01?test@@YAHUA@@@Z.catch", i32 0, void (i8*, i8*)* @"\01?test@@YAHUA@@@Z.cleanup")
+; CHECK-NEXT: indirectbr i8* [[RECOVER]], [label %cleanup]
+
+lpad: ; preds = %entry
+ %1 = landingpad { i8*, i32 } personality i8* bitcast (i32 (...)* @__CxxFrameHandler3 to i8*)
+ cleanup
+ catch i8* bitcast (%rtti.TypeDescriptor2* @"\01??_R0H@8" to i8*)
+ %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.dispatch
+
+; CHECK-NOT: catch.dispatch:
+
+catch.dispatch: ; preds = %lpad
+ %sel = load i32, i32* %ehselector.slot
+ %4 = call i32 @llvm.eh.typeid.for(i8* bitcast (%rtti.TypeDescriptor2* @"\01??_R0H@8" to i8*)) #3
+ %matches = icmp eq i32 %sel, %4
+ br i1 %matches, label %catch, label %ehcleanup
+
+; CHECK-NOT: catch:
+
+catch: ; preds = %catch.dispatch
+ %exn = load i8*, i8** %exn.slot
+ %e.i8 = bitcast i32* %e to i8*
+ call void @llvm.eh.begincatch(i8* %exn, i8* %e.i8) #3
+ %a = getelementptr inbounds <{ %struct.A }>, <{ %struct.A }>* %0, i32 0, i32 0
+ %a1 = getelementptr inbounds %struct.A, %struct.A* %a, i32 0, i32 0
+ %tmp8 = load i32, i32* %a1, align 4
+ %tmp9 = load i32, i32* %e, align 4
+ %add = add nsw i32 %tmp8, %tmp9
+ store i32 %add, i32* %retval
+ store i32 1, i32* %cleanup.dest.slot
+ call void @llvm.eh.endcatch() #3
+ br label %cleanup
+
+try.cont: ; preds = %invoke.cont
+ store i32 0, i32* %retval
+ store i32 1, i32* %cleanup.dest.slot
+ br label %cleanup
+
+; The cleanup block should be re-written like this.
+; CHECK: cleanup:{{[ ]+}}; preds = %[[LPAD_LABEL]], %try.cont
+; CHECK-NOT: %a2 = getelementptr inbounds <{ %struct.A }>, <{ %struct.A }>* %0, i32 0, i32 0
+; CHECK: [[TMP_RELOAD:\%.+]] = load volatile <{ %struct.A }>*, <{ %struct.A }>** [[TMP_REGMEM]]
+; CHECK: [[A2:\%.+]] = getelementptr inbounds <{ %struct.A }>, <{ %struct.A }>* [[TMP_RELOAD]], i32 0, i32 0
+; CHECK: call x86_thiscallcc void @"\01??1A@@QAE@XZ"(%struct.A* [[A2]])
+; CHECK: [[TMP1:\%.+]] = load i32, i32* [[RETVAL]]
+; CHECK: ret i32 [[TMP1]]
+
+cleanup: ; preds = %try.cont, %catch
+ %a2 = getelementptr inbounds <{ %struct.A }>, <{ %struct.A }>* %0, i32 0, i32 0
+ call x86_thiscallcc void @"\01??1A@@QAE@XZ"(%struct.A* %a2) #3
+ %tmp10 = load i32, i32* %retval
+ ret i32 %tmp10
+
+; CHECK-NOT: ehcleanup:
+
+ehcleanup: ; preds = %catch.dispatch
+ %a3 = getelementptr inbounds <{ %struct.A }>, <{ %struct.A }>* %0, i32 0, i32 0
+ call x86_thiscallcc void @"\01??1A@@QAE@XZ"(%struct.A* %a3) #3
+ br label %eh.resume
+
+; CHECK-NOT: eh.resume:
+
+eh.resume: ; preds = %ehcleanup
+ %exn2 = load i8*, i8** %exn.slot
+ %sel3 = load i32, i32* %ehselector.slot
+ %lpad.val = insertvalue { i8*, i32 } undef, i8* %exn2, 0
+ %lpad.val4 = insertvalue { i8*, i32 } %lpad.val, i32 %sel3, 1
+ resume { i8*, i32 } %lpad.val4
+
+; CHECK: }
+}
+
+; The following catch handler should be outlined.
+; CHECK: define internal i8* @"\01?test@@YAHUA@@@Z.catch"(i8*, i8*) {
+; CHECK: entry:
+; CHECK: [[RECOVER_E:\%.+]] = call i8* @llvm.framerecover(i8* bitcast (i32 (<{ %struct.A }>*)* @"\01?test@@YAHUA@@@Z" to i8*), i8* %1, i32 0)
+; CHECK: [[E_PTR:\%.+]] = bitcast i8* [[RECOVER_E]] to i32*
+; CHECK: [[RECOVER_EH_TEMP:\%.+]] = call i8* @llvm.framerecover(i8* bitcast (i32 (<{ %struct.A }>*)* @"\01?test@@YAHUA@@@Z" to i8*), i8* %1, i32 1)
+; CHECK: [[EH_TEMP:\%.+]] = bitcast i8* [[RECOVER_EH_TEMP]] to <{ %struct.A }>**
+; CHECK: [[TMP_RELOAD:\%.+]] = load <{ %struct.A }>*, <{ %struct.A }>** [[EH_TEMP]]
+; CHECK: [[RECOVER_RETVAL:\%.+]] = call i8* @llvm.framerecover(i8* bitcast (i32 (<{ %struct.A }>*)* @"\01?test@@YAHUA@@@Z" to i8*), i8* %1, i32 2)
+; CHECK: [[RETVAL1:\%.+]] = bitcast i8* [[RECOVER_RETVAL]] to i32*
+; CHECK: [[RECOVER_CLEANUPSLOT:\%.+]] = call i8* @llvm.framerecover(i8* bitcast (i32 (<{ %struct.A }>*)* @"\01?test@@YAHUA@@@Z" to i8*), i8* %1, i32 3)
+; CHECK: [[CLEANUPSLOT1:\%.+]] = bitcast i8* [[RECOVER_CLEANUPSLOT]] to i32*
+; CHECK: [[E_I8PTR:\%.+]] = bitcast i32* [[E_PTR]] to i8*
+; CHECK: [[RECOVER_A:\%.+]] = getelementptr inbounds <{ %struct.A }>, <{ %struct.A }>* [[TMP_RELOAD]], i32 0, i32 0
+; CHECK: [[A1:\%.+]] = getelementptr inbounds %struct.A, %struct.A* [[RECOVER_A]], i32 0, i32 0
+; CHECK: [[TMP2:\%.+]] = load i32, i32* [[A1]], align 4
+; CHECK: [[TMP3:\%.+]] = load i32, i32* [[E_PTR]], align 4
+; CHECK: [[ADD:\%.+]] = add nsw i32 [[TMP2]], [[TMP3]]
+; CHECK: store i32 [[ADD]], i32* [[RETVAL1]]
+; CHECK: store i32 1, i32* [[CLEANUPSLOT1]]
+; CHECK: ret i8* blockaddress(@"\01?test@@YAHUA@@@Z", %cleanup)
+; CHECK: }
+
+; The following cleanup handler should be outlined.
+; CHECK: define internal void @"\01?test@@YAHUA@@@Z.cleanup"(i8*, i8*) {
+; CHECK: entry:
+; CHECK: [[RECOVER_EH_TEMP1:\%.+]] = call i8* @llvm.framerecover(i8* bitcast (i32 (<{ %struct.A }>*)* @"\01?test@@YAHUA@@@Z" to i8*), i8* %1, i32 1)
+; CHECK: [[EH_TEMP1:\%.+]] = bitcast i8* [[RECOVER_EH_TEMP]] to <{ %struct.A }>**
+; CHECK: [[TMP_RELOAD1:\%.+]] = load <{ %struct.A }>*, <{ %struct.A }>** [[EH_TEMP1]]
+; CHECK: [[A3:\%.+]] = getelementptr inbounds <{ %struct.A }>, <{ %struct.A }>* [[TMP_RELOAD1]], i32 0, i32 0
+; CHECK: call x86_thiscallcc void @"\01??1A@@QAE@XZ"(%struct.A* [[A3]])
+; CHECK: ret void
+; CHECK: }
+
+declare void @"\01?may_throw@@YAXXZ"() #0
+
+declare i32 @__CxxFrameHandler3(...)
+
+; Function Attrs: nounwind readnone
+declare i32 @llvm.eh.typeid.for(i8*) #1
+
+declare void @llvm.eh.begincatch(i8*, i8*)
+
+declare void @llvm.eh.endcatch()
+
+; Function Attrs: nounwind
+declare x86_thiscallcc void @"\01??1A@@QAE@XZ"(%struct.A*) #2
+
+attributes #0 = { "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 = { nounwind readnone }
+attributes #2 = { nounwind "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 #3 = { nounwind }
+
+!llvm.ident = !{!0}
+
+!0 = !{!"clang version 3.7.0 (trunk 228868)"}
diff --git a/test/CodeGen/WinEH/cppeh-min-unwind.ll b/test/CodeGen/WinEH/cppeh-min-unwind.ll
new file mode 100644
index 0000000..a1e97d5
--- /dev/null
+++ b/test/CodeGen/WinEH/cppeh-min-unwind.ll
@@ -0,0 +1,99 @@
+; 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"
+
+%class.SomeClass = type { [28 x i32] }
+
+; The function entry should be rewritten like this.
+; CHECK: define void @_Z4testv() #0 {
+; CHECK: entry:
+; CHECK: [[OBJ_PTR:\%.+]] = alloca %class.SomeClass, align 4
+; CHECK: call void @_ZN9SomeClassC1Ev(%class.SomeClass* [[OBJ_PTR]])
+; CHECK: call void (...)* @llvm.frameescape(%class.SomeClass* [[OBJ_PTR]])
+; CHECK: invoke void @_Z9may_throwv()
+; CHECK: to label %invoke.cont unwind label %[[LPAD_LABEL:lpad[0-9]+]]
+
+; 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
+
+; CHECK: [[LPAD_LABEL]]:{{[ ]+}}; preds = %entry
+; CHECK: landingpad { i8*, i32 } personality i8* bitcast (i32 (...)* @__CxxFrameHandler3 to i8*)
+; CHECK-NEXT: cleanup
+; CHECK-NEXT: [[RECOVER:\%.+]] = call i8* (...)* @llvm.eh.actions(i32 0, void (i8*, i8*)* @_Z4testv.cleanup)
+; CHECK-NEXT: indirectbr i8* [[RECOVER]], []
+
+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
+
+; CHECK-NOT: 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
+
+; CHECK: }
+}
+
+; This cleanup handler should be outlined.
+; CHECK: define internal void @_Z4testv.cleanup(i8*, i8*) {
+; CHECK: entry:
+; CHECK: [[RECOVER_OBJ:\%.+]] = call i8* @llvm.framerecover(i8* bitcast (void ()* @_Z4testv to i8*), i8* %1, i32 0)
+; CHECK: [[OBJ_PTR1:\%.+]] = bitcast i8* [[RECOVER_OBJ]] to %class.SomeClass*
+; CHECK: call void @_ZN9SomeClassD1Ev(%class.SomeClass* [[OBJ_PTR1]])
+; 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/test/CodeGen/WinEH/cppeh-nested-1.ll b/test/CodeGen/WinEH/cppeh-nested-1.ll
new file mode 100644
index 0000000..e94e771
--- /dev/null
+++ b/test/CodeGen/WinEH/cppeh-nested-1.ll
@@ -0,0 +1,199 @@
+; RUN: opt -mtriple=x86_64-pc-windows-msvc -winehprepare -S -o - < %s | FileCheck %s
+; XFAIL: *
+
+; This test is based on the following code:
+;
+;void test()
+;{
+; try {
+; try {
+; may_throw();
+; } catch (int i) {
+; handle_int(i);
+; }
+; } catch (float f) {
+; handle_float(f);
+; }
+; done();
+;}
+
+; ModuleID = 'cppeh-nested-1.cpp'
+target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
+target triple = "x86_64-pc-windows-msvc"
+
+%rtti.TypeDescriptor2 = type { i8**, i8*, [3 x i8] }
+
+$"\01??_R0M@8" = comdat any
+
+$"\01??_R0H@8" = comdat any
+
+@"\01??_7type_info@@6B@" = external constant i8*
+@"\01??_R0M@8" = linkonce_odr global %rtti.TypeDescriptor2 { i8** @"\01??_7type_info@@6B@", i8* null, [3 x i8] c".M\00" }, comdat
+@"\01??_R0H@8" = linkonce_odr global %rtti.TypeDescriptor2 { i8** @"\01??_7type_info@@6B@", i8* null, [3 x i8] c".H\00" }, comdat
+
+; CHECK: define void @"\01?test@@YAXXZ"() #0 {
+; CHECK: entry:
+; CHECK: %i = alloca i32, align 4
+; CHECK: %f = alloca float, align 4
+; CHECK: call void (...)* @llvm.frameescape(i32* %i, float* %f)
+; CHECK: invoke void @"\01?may_throw@@YAXXZ"()
+; CHECK: to label %invoke.cont unwind label %[[LPAD_LABEL:lpad[0-9]*]]
+
+; Function Attrs: uwtable
+define void @"\01?test@@YAXXZ"() #0 {
+entry:
+ %exn.slot = alloca i8*
+ %ehselector.slot = alloca i32
+ %i = alloca i32, align 4
+ %f = alloca float, align 4
+ invoke void @"\01?may_throw@@YAXXZ"()
+ to label %invoke.cont unwind label %lpad
+
+invoke.cont: ; preds = %entry
+ br label %try.cont
+
+; CHECK: [[LPAD_LABEL]]:
+; CHECK: landingpad { i8*, i32 } personality i8* bitcast (i32 (...)* @__CxxFrameHandler3 to i8*)
+; 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 0, i8* bitcast (%rtti.TypeDescriptor2* @"\01??_R0H@8" to i8*), i32* %i, i8* (i8*, i8*)* @"\01?test@@YAXXZ.catch" to i8*), i32 1, i8* bitcast (%rtti.TypeDescriptor2* @"\01??_R0M@8" to i8*), float* %f, i8* bitcast (i8* (i8*, i8*)* @"\01?test@@YAXXZ.catch1")
+; CHECK: indirectbr i8* [[RECOVER]], [label %try.cont, label %try.cont10]
+
+lpad: ; preds = %entry
+ %0 = landingpad { i8*, i32 } personality i8* bitcast (i32 (...)* @__CxxFrameHandler3 to i8*)
+ catch i8* bitcast (%rtti.TypeDescriptor2* @"\01??_R0H@8" to i8*)
+ catch i8* bitcast (%rtti.TypeDescriptor2* @"\01??_R0M@8" to i8*)
+ %1 = extractvalue { i8*, i32 } %0, 0
+ store i8* %1, i8** %exn.slot
+ %2 = extractvalue { i8*, i32 } %0, 1
+ store i32 %2, i32* %ehselector.slot
+ br label %catch.dispatch
+
+; CHECK-NOT: catch.dispatch:
+catch.dispatch: ; preds = %lpad
+ %sel = load i32, i32* %ehselector.slot
+ %3 = call i32 @llvm.eh.typeid.for(i8* bitcast (%rtti.TypeDescriptor2* @"\01??_R0H@8" to i8*)) #3
+ %matches = icmp eq i32 %sel, %3
+ br i1 %matches, label %catch, label %catch.dispatch3
+
+; CHECK-NOT: catch:
+catch: ; preds = %catch.dispatch
+ %exn = load i8*, i8** %exn.slot
+ %4 = bitcast i32* %i to i8*
+ call void @llvm.eh.begincatch(i8* %exn, i8* %4) #3
+ %5 = load i32, i32* %i, align 4
+ invoke void @"\01?handle_int@@YAXH@Z"(i32 %5)
+ to label %invoke.cont2 unwind label %lpad1
+
+; CHECK-NOT: invoke.cont2:
+invoke.cont2: ; preds = %catch
+ call void @llvm.eh.endcatch() #3
+ br label %try.cont
+
+try.cont: ; preds = %invoke.cont2, %invoke.cont
+ br label %try.cont10
+
+; CHECK-NOT: lpad1:
+lpad1: ; preds = %catch
+ %6 = landingpad { i8*, i32 } personality i8* bitcast (i32 (...)* @__CxxFrameHandler3 to i8*)
+ catch i8* bitcast (%rtti.TypeDescriptor2* @"\01??_R0M@8" to i8*)
+ %7 = extractvalue { i8*, i32 } %6, 0
+ store i8* %7, i8** %exn.slot
+ %8 = extractvalue { i8*, i32 } %6, 1
+ store i32 %8, i32* %ehselector.slot
+ call void @llvm.eh.endcatch() #3
+ br label %catch.dispatch3
+
+; CHECK-NOT: catch.dispatch3:
+catch.dispatch3: ; preds = %lpad1, %catch.dispatch
+ %sel4 = load i32, i32* %ehselector.slot
+ %9 = call i32 @llvm.eh.typeid.for(i8* bitcast (%rtti.TypeDescriptor2* @"\01??_R0M@8" to i8*)) #3
+ %matches5 = icmp eq i32 %sel4, %9
+ br i1 %matches5, label %catch6, label %eh.resume
+
+; CHECK-NOT: catch6:
+catch6: ; preds = %catch.dispatch3
+ %exn7 = load i8*, i8** %exn.slot
+ %10 = bitcast float* %f to i8*
+ call void @llvm.eh.begincatch(i8* %exn7, i8* %10) #3
+ %11 = load float, float* %f, align 4
+ call void @"\01?handle_float@@YAXM@Z"(float %11)
+ call void @llvm.eh.endcatch() #3
+ br label %try.cont10
+
+try.cont10: ; preds = %catch6, %try.cont
+ call void @"\01?done@@YAXXZ"()
+ ret void
+
+; CHECK-NOT: eh.resume:
+eh.resume: ; %catch.dispatch3
+ %exn11 = load i8*, i8** %exn.slot
+ %sel12 = load i32, i32* %ehselector.slot
+ %lpad.val = insertvalue { i8*, i32 } undef, i8* %exn11, 0
+ %lpad.val13 = insertvalue { i8*, i32 } %lpad.val, i32 %sel12, 1
+ resume { i8*, i32 } %lpad.val13
+; CHECK: }
+}
+
+; CHECK: define internal i8* @"\01?test@@YAXXZ.catch"(i8*, i8*) {
+; CHECK: entry:
+; CHECK: [[RECOVER_I:\%.+]] = call i8* @llvm.framerecover(i8* bitcast (void ()* @"\01?test@@YAXXZ" to i8*), i8* %1, i32 0)
+; CHECK: [[I_PTR:\%.+]] = bitcast i8* [[RECOVER_I]] to i32*
+; ------------================= FAIL here =================------------
+; CHECK: [[RECOVER_F:\%.+]] = call i8* @llvm.framerecover(i8* bitcast (void ()* @"\01?test@@YAXXZ" to i8*), i8* %1, i32 1)
+; CHECK: [[F_PTR:\%.+]] = bitcast i8* [[RECOVER_F]] to float*
+; CHECK: [[TMP1:\%.+]] = load i32, i32* [[I_PTR]], align 4
+; CHECK: invoke void @"\01?handle_int@@YAXH@Z"(i32 [[TMP1]])
+; CHECK: to label %invoke.cont2 unwind label %[[LPAD1_LABEL:lpad[0-9]*]]
+;
+; CHECK: invoke.cont2:
+; CHECK: ret i8* blockaddress(@"\01?test@@YAXXZ", %try.cont)
+;
+; CHECK: [[LPAD1_LABEL]]:{{[ ]+}}; preds = %entry
+; CHECK: [[LPAD1_VAL:\%.+]] = landingpad { i8*, i32 } personality i8* bitcast (i32 (...)* @__CxxFrameHandler3 to i8*)
+; CHECK: catch i8* bitcast (%rtti.TypeDescriptor2* @"\01??_R0M@8" to i8*)
+; ------------================= FAIL here =================------------
+; CHECK: [[RECOVER1:\%.+]] = call i8* (...)* @llvm.eh.actions({ i8*, i32 } [[LPAD1_VAL]], i32 1, i8* bitcast (%rtti.TypeDescriptor2* @"\01??_R0M@8" to i8*), float* [[F_PTR]], i8* (i8*, i8*)* @"\01?test@@YAXXZ.catch1")
+; CHECK: indirectbr i8* [[RECOVER1]], []
+;
+; CHECK: }
+
+; CHECK: define internal i8* @"\01?test@@YAXXZ.catch1"(i8*, i8*) {
+; CHECK: entry:
+; CHECK: [[RECOVER_F1:\%.+]] = call i8* @llvm.framerecover(i8* bitcast (void ()* @"\01?test@@YAXXZ" to i8*), i8* %1, i32 1)
+; CHECK: [[F_PTR1:\%.+]] = bitcast i8* [[RECOVER_F1]] to float*
+; CHECK: [[TMP2:\%.+]] = load float, float* [[F_PTR1]], align 4
+; CHECK: call void @"\01?handle_float@@YAXM@Z"(float [[TMP2]])
+; CHECK: ret i8* blockaddress(@"\01?test@@YAXXZ", %try.cont10)
+; CHECK: }
+
+
+declare void @"\01?may_throw@@YAXXZ"() #1
+
+declare i32 @__CxxFrameHandler3(...)
+
+; 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
+
+declare void @"\01?handle_int@@YAXH@Z"(i32) #1
+
+; Function Attrs: nounwind
+declare void @llvm.eh.endcatch() #3
+
+declare void @"\01?handle_float@@YAXM@Z"(float) #1
+
+declare void @"\01?done@@YAXXZ"() #1
+
+attributes #0 = { 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" "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" "stack-protector-buffer-size"="8" "unsafe-fp-math"="false" "use-soft-float"="false" }
+attributes #2 = { nounwind readnone }
+attributes #3 = { nounwind }
+
+!llvm.module.flags = !{!0}
+!llvm.ident = !{!1}
+
+!0 = !{i32 1, !"PIC Level", i32 2}
+!1 = !{!"clang version 3.7.0 (trunk 232069) (llvm/trunk 232070)"}
diff --git a/test/CodeGen/WinEH/cppeh-nested-2.ll b/test/CodeGen/WinEH/cppeh-nested-2.ll
new file mode 100644
index 0000000..3479c41
--- /dev/null
+++ b/test/CodeGen/WinEH/cppeh-nested-2.ll
@@ -0,0 +1,324 @@
+; RUN: opt -mtriple=x86_64-pc-windows-msvc -winehprepare -S -o - < %s | FileCheck %s
+
+; This test is based on the following code:
+;
+; class Inner {
+; public:
+; Inner();
+; ~Inner();
+; };
+; class Outer {
+; public:
+; Outer();
+; ~Outer();
+; };
+; void test() {
+; try {
+; Outer outer;
+; try {
+; Inner inner;
+; may_throw();
+; } catch (int i) {
+; handle_int(i);
+; }
+; } catch (float f) {
+; handle_float(f);
+; }
+; done();
+; }
+
+; ModuleID = 'nested-2.cpp'
+target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
+target triple = "x86_64-pc-windows-msvc"
+
+%class.Outer = type { i8 }
+%class.Inner = type { i8 }
+
+@_ZTIf = external constant i8*
+@_ZTIi = external constant i8*
+
+; The function entry should be rewritten like this.
+; CHECK: define void @_Z4testv() #0 {
+; CHECK: entry:
+; CHECK: %outer = alloca %class.Outer, align 1
+; CHECK: %inner = alloca %class.Inner, align 1
+; CHECK: %i = alloca i32, align 4
+; CHECK: %f = alloca float, align 4
+; CHECK: call void (...)* @llvm.frameescape(float* %f, i32* %i, %class.Outer* %outer, %class.Inner* %inner)
+; CHECK: invoke void @_ZN5OuterC1Ev(%class.Outer* %outer)
+; CHECK: to label %invoke.cont unwind label %[[LPAD_LABEL:lpad[0-9]*]]
+
+; Function Attrs: uwtable
+define void @_Z4testv() #0 {
+entry:
+ %outer = alloca %class.Outer, align 1
+ %exn.slot = alloca i8*
+ %ehselector.slot = alloca i32
+ %inner = alloca %class.Inner, align 1
+ %i = alloca i32, align 4
+ %f = alloca float, align 4
+ invoke void @_ZN5OuterC1Ev(%class.Outer* %outer)
+ to label %invoke.cont unwind label %lpad
+
+; CHECK: invoke.cont:
+; CHECK: invoke void @_ZN5InnerC1Ev(%class.Inner* %inner)
+; CHECK: to label %invoke.cont2 unwind label %[[LPAD1_LABEL:lpad[0-9]*]]
+
+invoke.cont: ; preds = %entry
+ invoke void @_ZN5InnerC1Ev(%class.Inner* %inner)
+ to label %invoke.cont2 unwind label %lpad1
+
+; CHECK: invoke.cont2:
+; CHECK: invoke void @_Z9may_throwv()
+; CHECK: to label %invoke.cont4 unwind label %[[LPAD3_LABEL:lpad[0-9]*]]
+
+invoke.cont2: ; preds = %invoke.cont
+ invoke void @_Z9may_throwv()
+ to label %invoke.cont4 unwind label %lpad3
+
+; CHECK: invoke.cont4:
+; CHECK: invoke void @_ZN5InnerD1Ev(%class.Inner* %inner)
+; CHECK: to label %invoke.cont5 unwind label %[[LPAD1_LABEL]]
+
+invoke.cont4: ; preds = %invoke.cont2
+ invoke void @_ZN5InnerD1Ev(%class.Inner* %inner)
+ to label %invoke.cont5 unwind label %lpad1
+
+; CHECK: invoke.cont5:
+; CHECK: br label %try.cont
+
+invoke.cont5: ; preds = %invoke.cont4
+ br label %try.cont
+
+; CHECK: [[LPAD_LABEL]]:
+; CHECK: landingpad { i8*, i32 } personality i8* bitcast (i32 (...)* @__CxxFrameHandler3 to i8*)
+; CHECK-NEXT: catch i8* bitcast (i8** @_ZTIf to i8*)
+; CHECK-NEXT: [[RECOVER:\%.+]] = call i8* (...)* @llvm.eh.actions(i32 1, i8* bitcast (i8** @_ZTIf to i8*), float* %f, i8* (i8*, i8*)* @_Z4testv.catch)
+; CHECK-NEXT: indirectbr i8* [[RECOVER]], [label %try.cont19]
+
+lpad: ; preds = %try.cont, %entry
+ %tmp = landingpad { i8*, i32 } personality i8* bitcast (i32 (...)* @__CxxFrameHandler3 to i8*)
+ catch i8* bitcast (i8** @_ZTIf to i8*)
+ %tmp1 = extractvalue { i8*, i32 } %tmp, 0
+ store i8* %tmp1, i8** %exn.slot
+ %tmp2 = extractvalue { i8*, i32 } %tmp, 1
+ store i32 %tmp2, i32* %ehselector.slot
+ br label %catch.dispatch11
+
+; CHECK: [[LPAD1_LABEL]]:
+; CHECK: landingpad { i8*, i32 } personality i8* bitcast (i32 (...)* @__CxxFrameHandler3 to i8*)
+; CHECK-NEXT: cleanup
+; CHECK-NEXT: catch i8* bitcast (i8** @_ZTIi to i8*)
+; CHECK-NEXT: catch i8* bitcast (i8** @_ZTIf to i8*)
+; CHECK-NEXT: [[RECOVER1:\%.+]] = call i8* (...)* @llvm.eh.actions(
+; CHECK-SAME: i32 1, i8* bitcast (i8** @_ZTIi to i8*), i32* %i, i8* (i8*, i8*)* @_Z4testv.catch1,
+; CHECK-SAME: i32 0, void (i8*, i8*)* @_Z4testv.cleanup,
+; CHECK-SAME: i32 1, i8* bitcast (i8** @_ZTIf to i8*), float* %f, i8* (i8*, i8*)* @_Z4testv.catch)
+; CHECK-NEXT: indirectbr i8* [[RECOVER1]], [label %try.cont, label %try.cont19]
+
+lpad1: ; preds = %invoke.cont4, %invoke.cont
+ %tmp3 = landingpad { i8*, i32 } personality i8* bitcast (i32 (...)* @__CxxFrameHandler3 to i8*)
+ cleanup
+ catch i8* bitcast (i8** @_ZTIi to i8*)
+ catch i8* bitcast (i8** @_ZTIf to i8*)
+ %tmp4 = extractvalue { i8*, i32 } %tmp3, 0
+ store i8* %tmp4, i8** %exn.slot
+ %tmp5 = extractvalue { i8*, i32 } %tmp3, 1
+ store i32 %tmp5, i32* %ehselector.slot
+ br label %catch.dispatch
+
+; CHECK: [[LPAD3_LABEL]]:
+; CHECK: landingpad { i8*, i32 } personality i8* bitcast (i32 (...)* @__CxxFrameHandler3 to i8*)
+; CHECK-NEXT: cleanup
+; CHECK-NEXT: catch i8* bitcast (i8** @_ZTIi to i8*)
+; CHECK-NEXT: catch i8* bitcast (i8** @_ZTIf to i8*)
+; CHECK-NEXT: [[RECOVER3:\%.+]] = call i8* (...)* @llvm.eh.actions(
+; CHECK-SAME: i32 0, void (i8*, i8*)* @_Z4testv.cleanup2,
+; CHECK-SAME: i32 1, i8* bitcast (i8** @_ZTIi to i8*), i32* %i, i8* (i8*, i8*)* @_Z4testv.catch1,
+; CHECK-SAME: i32 0, void (i8*, i8*)* @_Z4testv.cleanup,
+; CHECK-SAME: i32 1, i8* bitcast (i8** @_ZTIf to i8*), float* %f, i8* (i8*, i8*)* @_Z4testv.catch)
+; CHECK-NEXT: indirectbr i8* [[RECOVER3]], [label %try.cont, label %try.cont19]
+
+lpad3: ; preds = %invoke.cont2
+ %tmp6 = landingpad { i8*, i32 } personality i8* bitcast (i32 (...)* @__CxxFrameHandler3 to i8*)
+ cleanup
+ catch i8* bitcast (i8** @_ZTIi to i8*)
+ catch i8* bitcast (i8** @_ZTIf to i8*)
+ %tmp7 = extractvalue { i8*, i32 } %tmp6, 0
+ store i8* %tmp7, i8** %exn.slot
+ %tmp8 = extractvalue { i8*, i32 } %tmp6, 1
+ store i32 %tmp8, i32* %ehselector.slot
+ call void @_ZN5InnerD1Ev(%class.Inner* %inner)
+ br label %catch.dispatch
+
+; CHECK-NOT: catch.dispatch:
+
+catch.dispatch: ; preds = %lpad3, %lpad1
+ %sel = load i32, i32* %ehselector.slot
+ %tmp9 = call i32 @llvm.eh.typeid.for(i8* bitcast (i8** @_ZTIi to i8*)) #4
+ %matches = icmp eq i32 %sel, %tmp9
+ br i1 %matches, label %catch, label %ehcleanup
+
+; CHECK-NOT: catch:
+
+catch: ; preds = %catch.dispatch
+ %exn = load i8*, i8** %exn.slot
+ %i.i8 = bitcast i32* %i to i8*
+ call void @llvm.eh.begincatch(i8* %exn, i8* %i.i8) #4
+ %tmp13 = load i32, i32* %i, align 4
+ invoke void @_Z10handle_inti(i32 %tmp13)
+ to label %invoke.cont8 unwind label %lpad7
+
+; CHECK-NOT: invoke.cont8:
+
+invoke.cont8: ; preds = %catch
+ call void @llvm.eh.endcatch() #4
+ br label %try.cont
+
+; CHECK: try.cont:
+; CHECK: invoke void @_ZN5OuterD1Ev(%class.Outer* %outer)
+; CHECK: to label %invoke.cont9 unwind label %[[LPAD_LABEL]]
+
+try.cont: ; preds = %invoke.cont8, %invoke.cont5
+ invoke void @_ZN5OuterD1Ev(%class.Outer* %outer)
+ to label %invoke.cont9 unwind label %lpad
+
+invoke.cont9: ; preds = %try.cont
+ br label %try.cont19
+
+; CHECK-NOT: lpad7:
+
+lpad7: ; preds = %catch
+ %tmp14 = landingpad { i8*, i32 } personality i8* bitcast (i32 (...)* @__CxxFrameHandler3 to i8*)
+ cleanup
+ catch i8* bitcast (i8** @_ZTIf to i8*)
+ %tmp15 = extractvalue { i8*, i32 } %tmp14, 0
+ store i8* %tmp15, i8** %exn.slot
+ %tmp16 = extractvalue { i8*, i32 } %tmp14, 1
+ store i32 %tmp16, i32* %ehselector.slot
+ call void @llvm.eh.endcatch() #4
+ br label %ehcleanup
+
+; CHECK-NOT: ehcleanup: ; preds = %lpad7, %catch.dispatch
+
+ehcleanup: ; preds = %lpad7, %catch.dispatch
+ call void @_ZN5OuterD1Ev(%class.Outer* %outer)
+ br label %catch.dispatch11
+
+; CHECK-NOT: catch.dispatch11:
+
+catch.dispatch11: ; preds = %ehcleanup, %lpad
+ %sel12 = load i32, i32* %ehselector.slot
+ %tmp17 = call i32 @llvm.eh.typeid.for(i8* bitcast (i8** @_ZTIf to i8*)) #4
+ %matches13 = icmp eq i32 %sel12, %tmp17
+ br i1 %matches13, label %catch14, label %eh.resume
+
+; CHECK-NOT: catch14:
+
+catch14: ; preds = %catch.dispatch11
+ %exn15 = load i8*, i8** %exn.slot
+ %f.i8 = bitcast float* %f to i8*
+ call void @llvm.eh.begincatch(i8* %exn15, i8* %f.i8) #4
+ %tmp21 = load float, float* %f, align 4
+ call void @_Z12handle_floatf(float %tmp21)
+ call void @llvm.eh.endcatch() #4
+ br label %try.cont19
+
+try.cont19: ; preds = %catch14, %invoke.cont9
+ call void @_Z4donev()
+ ret void
+
+; CHECK-NOT: eh.resume:
+
+eh.resume: ; preds = %catch.dispatch11
+ %exn20 = load i8*, i8** %exn.slot
+ %sel21 = load i32, i32* %ehselector.slot
+ %lpad.val = insertvalue { i8*, i32 } undef, i8* %exn20, 0
+ %lpad.val22 = insertvalue { i8*, i32 } %lpad.val, i32 %sel21, 1
+ resume { i8*, i32 } %lpad.val22
+
+; CHECK: }
+}
+
+; This catch handler should be outlined.
+; CHECK: define internal i8* @_Z4testv.catch(i8*, i8*) {
+; CHECK: entry:
+; CHECK: [[RECOVER_F:\%.+]] = call i8* @llvm.framerecover(i8* bitcast (void ()* @_Z4testv to i8*), i8* %1, i32 0)
+; CHECK: [[F_PTR:\%.+]] = bitcast i8* [[RECOVER_F]] to float*
+; CHECK: [[TMP:\%.+]] = load float, float* [[F_PTR]], align 4
+; CHECK: call void @_Z12handle_floatf(float [[TMP]])
+; CHECK: ret i8* blockaddress(@_Z4testv, %try.cont19)
+; CHECK: }
+
+; This catch handler should be outlined.
+; CHECK: define internal i8* @_Z4testv.catch1(i8*, i8*) {
+; CHECK: entry:
+; CHECK: [[RECOVER_I:\%.+]] = call i8* @llvm.framerecover(i8* bitcast (void ()* @_Z4testv to i8*), i8* %1, i32 1)
+; CHECK: [[I_PTR:\%.+]] = bitcast i8* [[RECOVER_I]] to i32*
+; CHECK: [[TMP1:\%.+]] = load i32, i32* [[I_PTR]], align 4
+; CHECK: invoke void @_Z10handle_inti(i32 [[TMP1]])
+; CHECK: to label %invoke.cont8 unwind label %[[LPAD7_LABEL:lpad[0-9]*]]
+;
+; CHECK: invoke.cont8: ; preds = %entry
+; CHECK: ret i8* blockaddress(@_Z4testv, %try.cont)
+;
+; CHECK: [[LPAD7_LABEL]]:{{[ ]+}}; preds = %entry
+; CHECK: [[LPAD7_VAL:\%.+]] = landingpad { i8*, i32 } personality i8* bitcast (i32 (...)* @__CxxFrameHandler3 to i8*)
+; (FIXME) The nested handler body isn't being populated yet.
+; CHECK: }
+
+; This cleanup handler should be outlined.
+; CHECK: define internal void @_Z4testv.cleanup(i8*, i8*) {
+; CHECK: entry:
+; CHECK: [[RECOVER_OUTER:\%.+]] = call i8* @llvm.framerecover(i8* bitcast (void ()* @_Z4testv to i8*), i8* %1, i32 2)
+; CHECK: [[OUTER_PTR:\%.+]] = bitcast i8* [[RECOVER_OUTER]] to %class.Outer*
+; CHECK: call void @_ZN5OuterD1Ev(%class.Outer* [[OUTER_PTR]])
+; CHECK: ret void
+; CHECK: }
+
+; This cleanup handler should be outlined.
+; CHECK: define internal void @_Z4testv.cleanup2(i8*, i8*) {
+; CHECK: entry:
+; CHECK: [[RECOVER_INNER:\%.+]] = call i8* @llvm.framerecover(i8* bitcast (void ()* @_Z4testv to i8*), i8* %1, i32 3)
+; CHECK: [[INNER_PTR:\%.+]] = bitcast i8* [[RECOVER_INNER]] to %class.Inner*
+; CHECK: call void @_ZN5InnerD1Ev(%class.Inner* [[INNER_PTR]])
+; CHECK: ret void
+; CHECK: }
+
+
+
+declare void @_ZN5OuterC1Ev(%class.Outer*) #1
+
+declare i32 @__CxxFrameHandler3(...)
+
+declare void @_ZN5InnerC1Ev(%class.Inner*) #1
+
+declare void @_Z9may_throwv() #1
+
+declare void @_ZN5InnerD1Ev(%class.Inner*) #1
+
+declare void @llvm.eh.begincatch(i8*, i8*)
+
+; Function Attrs: nounwind readnone
+declare i32 @llvm.eh.typeid.for(i8*) #3
+
+declare void @_Z10handle_inti(i32) #1
+
+declare void @llvm.eh.endcatch()
+
+declare void @_ZN5OuterD1Ev(%class.Outer*) #1
+
+declare void @_Z12handle_floatf(float) #1
+
+declare void @_Z4donev() #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 = { nounwind readnone }
+attributes #4 = { nounwind }
+attributes #5 = { noreturn nounwind }
+
+!llvm.ident = !{!0}
+
+!0 = !{!"clang version 3.7.0 (trunk 226027)"}
diff --git a/test/CodeGen/WinEH/cppeh-nested-3.ll b/test/CodeGen/WinEH/cppeh-nested-3.ll
new file mode 100644
index 0000000..d28fa4d
--- /dev/null
+++ b/test/CodeGen/WinEH/cppeh-nested-3.ll
@@ -0,0 +1,273 @@
+; RUN: opt -mtriple=x86_64-pc-windows-msvc -winehprepare -S -o - < %s | FileCheck %s
+; XFAIL: *
+
+; This test is based on the following code:
+;
+;void test()
+;{
+; try {
+; try {
+; may_throw();
+; } catch (int i) {
+; try {
+; may_throw();
+; }
+; catch (int j) {
+; i = j;
+; }
+; handle_int(i);
+; }
+; } catch (float f) {
+; handle_float(f);
+; }
+; done();
+;}
+
+; ModuleID = 'cppeh-nested-3.cpp'
+target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
+target triple = "x86_64-pc-windows-msvc"
+
+%rtti.TypeDescriptor2 = type { i8**, i8*, [3 x i8] }
+
+$"\01??_R0M@8" = comdat any
+
+$"\01??_R0H@8" = comdat any
+
+@"\01??_7type_info@@6B@" = external constant i8*
+@"\01??_R0M@8" = linkonce_odr global %rtti.TypeDescriptor2 { i8** @"\01??_7type_info@@6B@", i8* null, [3 x i8] c".M\00" }, comdat
+@"\01??_R0H@8" = linkonce_odr global %rtti.TypeDescriptor2 { i8** @"\01??_7type_info@@6B@", i8* null, [3 x i8] c".H\00" }, comdat
+
+; CHECK: define void @"\01?test@@YAXXZ"() #0 {
+; CHECK: entry:
+; CHECK: %i = alloca i32, align 4
+; ------------================= FAIL here =================------------
+; CHECK: %j = alloca i32, align 4
+; CHECK: %f = alloca float, align 4
+; ------------================= FAIL here =================------------
+; CHECK: call void (...)* @llvm.frameescape(i32* %i, float* %f, int32* %j)
+; CHECK: invoke void @"\01?may_throw@@YAXXZ"()
+; CHECK: to label %invoke.cont unwind label %[[LPAD_LABEL:lpad[0-9]*]]
+
+; Function Attrs: uwtable
+define void @"\01?test@@YAXXZ"() #0 {
+entry:
+ %exn.slot = alloca i8*
+ %ehselector.slot = alloca i32
+ %i = alloca i32, align 4
+ %j = alloca i32, align 4
+ %f = alloca float, align 4
+ invoke void @"\01?may_throw@@YAXXZ"()
+ to label %invoke.cont unwind label %lpad
+
+invoke.cont: ; preds = %entry
+ br label %try.cont10
+
+; CHECK: [[LPAD_LABEL]]:
+; CHECK: landingpad { i8*, i32 } personality i8* bitcast (i32 (...)* @__CxxFrameHandler3 to i8*)
+; 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 0, i8* bitcast (%rtti.TypeDescriptor2* @"\01??_R0H@8" to i8*), i32* %i, i8* (i8*, i8*)* @"\01?test@@YAXXZ.catch" to i8*), i32 1, i8* bitcast (%rtti.TypeDescriptor2* @"\01??_R0M@8" to i8*), float* %f, i8* bitcast (i8* (i8*, i8*)* @"\01?test@@YAXXZ.catch1")
+; CHECK: indirectbr i8* [[RECOVER]], [label %try.cont10, label %try.cont19]
+
+lpad: ; preds = %entry
+ %0 = landingpad { i8*, i32 } personality i8* bitcast (i32 (...)* @__CxxFrameHandler3 to i8*)
+ catch i8* bitcast (%rtti.TypeDescriptor2* @"\01??_R0H@8" to i8*)
+ catch i8* bitcast (%rtti.TypeDescriptor2* @"\01??_R0M@8" to i8*)
+ %1 = extractvalue { i8*, i32 } %0, 0
+ store i8* %1, i8** %exn.slot
+ %2 = extractvalue { i8*, i32 } %0, 1
+ store i32 %2, i32* %ehselector.slot
+ br label %catch.dispatch
+
+; CHECK-NOT: catch.dispatch:
+catch.dispatch: ; preds = %lpad
+ %sel = load i32, i32* %ehselector.slot
+ %3 = call i32 @llvm.eh.typeid.for(i8* bitcast (%rtti.TypeDescriptor2* @"\01??_R0H@8" to i8*)) #3
+ %matches = icmp eq i32 %sel, %3
+ br i1 %matches, label %catch, label %catch.dispatch11
+
+; CHECK-NOT: catch:
+catch: ; preds = %catch.dispatch
+ %exn = load i8*, i8** %exn.slot
+ %4 = bitcast i32* %i to i8*
+ call void @llvm.eh.begincatch(i8* %exn, i8* %4) #3
+ invoke void @"\01?may_throw@@YAXXZ"()
+ to label %invoke.cont2 unwind label %lpad1
+
+; CHECK-NOT: invoke.cont2:
+invoke.cont2: ; preds = %catch
+ br label %try.cont
+
+; CHECK-NOT: lpad1:
+lpad1: ; preds = %catch
+ %5 = landingpad { i8*, i32 } personality i8* bitcast (i32 (...)* @__CxxFrameHandler3 to i8*)
+ catch i8* bitcast (%rtti.TypeDescriptor2* @"\01??_R0H@8" to i8*)
+ catch i8* bitcast (%rtti.TypeDescriptor2* @"\01??_R0M@8" to i8*)
+ %6 = extractvalue { i8*, i32 } %5, 0
+ store i8* %6, i8** %exn.slot
+ %7 = extractvalue { i8*, i32 } %5, 1
+ store i32 %7, i32* %ehselector.slot
+ br label %catch.dispatch3
+
+; CHECK-NOT: catch.dispatch3:
+catch.dispatch3: ; preds = %lpad1
+ %sel4 = load i32, i32* %ehselector.slot
+ %8 = call i32 @llvm.eh.typeid.for(i8* bitcast (%rtti.TypeDescriptor2* @"\01??_R0H@8" to i8*)) #3
+ %matches5 = icmp eq i32 %sel4, %8
+ br i1 %matches5, label %catch6, label %catch.dispatch11
+
+; CHECK-NOT: catch6:
+catch6: ; preds = %catch.dispatch3
+ %exn7 = load i8*, i8** %exn.slot
+ %9 = bitcast i32* %j to i8*
+ call void @llvm.eh.begincatch(i8* %exn7, i8* %9) #3
+ %10 = load i32, i32* %j, align 4
+ store i32 %10, i32* %i, align 4
+ call void @llvm.eh.endcatch() #3
+ br label %try.cont
+
+; CHECK-NOT: try.cont:
+try.cont: ; preds = %catch6, %invoke.cont2
+ %11 = load i32, i32* %i, align 4
+ invoke void @"\01?handle_int@@YAXH@Z"(i32 %11)
+ to label %invoke.cont9 unwind label %lpad8
+
+; CHECK-NOT: invoke.cont9:
+invoke.cont9: ; preds = %try.cont
+ call void @llvm.eh.endcatch() #3
+ br label %try.cont10
+
+try.cont10: ; preds = %invoke.cont9, %invoke.cont
+ br label %try.cont19
+
+; CHECK-NOT: lpad8:
+lpad8: ; preds = %try.cont
+ %12 = landingpad { i8*, i32 } personality i8* bitcast (i32 (...)* @__CxxFrameHandler3 to i8*)
+ catch i8* bitcast (%rtti.TypeDescriptor2* @"\01??_R0M@8" to i8*)
+ %13 = extractvalue { i8*, i32 } %12, 0
+ store i8* %13, i8** %exn.slot
+ %14 = extractvalue { i8*, i32 } %12, 1
+ store i32 %14, i32* %ehselector.slot
+ call void @llvm.eh.endcatch() #3
+ br label %catch.dispatch11
+
+; CHECK-NOT: catch.dispatch11:
+catch.dispatch11: ; preds = %lpad8, %catch.dispatch3, %catch.dispatch
+ %sel12 = load i32, i32* %ehselector.slot
+ %15 = call i32 @llvm.eh.typeid.for(i8* bitcast (%rtti.TypeDescriptor2* @"\01??_R0M@8" to i8*)) #3
+ %matches13 = icmp eq i32 %sel12, %15
+ br i1 %matches13, label %catch14, label %eh.resume
+
+; CHECK-NOT: catch14:
+catch14: ; preds = %catch.dispatch11
+ %exn15 = load i8*, i8** %exn.slot
+ %16 = bitcast float* %f to i8*
+ call void @llvm.eh.begincatch(i8* %exn15, i8* %16) #3
+ %17 = load float, float* %f, align 4
+ call void @"\01?handle_float@@YAXM@Z"(float %17)
+ call void @llvm.eh.endcatch() #3
+ br label %try.cont19
+
+try.cont19: ; preds = %catch14, %try.cont10
+ call void @"\01?done@@YAXXZ"()
+ ret void
+
+; CHECK-NOT: eh.resume:
+eh.resume: ; preds = %lpad16, %catch.dispatch11
+ %exn20 = load i8*, i8** %exn.slot
+ %sel21 = load i32, i32* %ehselector.slot
+ %lpad.val = insertvalue { i8*, i32 } undef, i8* %exn20, 0
+ %lpad.val22 = insertvalue { i8*, i32 } %lpad.val, i32 %sel21, 1
+ resume { i8*, i32 } %lpad.val22
+; CHECK: }
+}
+
+; CHECK: define internal i8* @"\01?test@@YAXXZ.catch"(i8*, i8*) {
+; CHECK: entry:
+; CHECK: [[RECOVER_I:\%.+]] = call i8* @llvm.framerecover(i8* bitcast (void ()* @"\01?test@@YAXXZ" to i8*), i8* %1, i32 0)
+; CHECK: [[I_PTR:\%.+]] = bitcast i8* [[RECOVER_I]] to i32*
+; ------------================= FAIL here =================------------
+; CHECK: [[RECOVER_F:\%.+]] = call i8* @llvm.framerecover(i8* bitcast (void ()* @"\01?test@@YAXXZ" to i8*), i8* %1, i32 1)
+; CHECK: [[F_PTR:\%.+]] = bitcast i8* [[RECOVER_F]] to float*
+; ------------================= FAIL here =================------------
+; CHECK: [[RECOVER_J:\%.+]] = call i8* @llvm.framerecover(i8* bitcast (void ()* @"\01?test@@YAXXZ" to i8*), i8* %1, i32 2)
+; CHECK: [[J_PTR:\%.+]] = bitcast i8* [[RECOVER_I]] to i32*
+; CHECK: invoke void @"\01?may_throw@@YAXXZ"()
+; CHECK: to label %invoke.cont2 unwind label %[[LPAD1_LABEL:lpad[0-9]*]]
+;
+; CHECK: invoke.cont2: ; preds = %entry
+; CHECK: [[TMP1:\%.+]] = load i32, i32* [[I_PTR]], align 4
+; CHECK: invoke void @"\01?handle_int@@YAXH@Z"(i32 [[TMP1]])
+; CHECK: to label %invoke.cont9 unwind label %[[LPAD8_LABEL:lpad[0-9]*]]
+;
+; CHECK: [[LPAD1_LABEL]]:{{[ ]+}}; preds = %entry
+; CHECK: [[LPAD1_VAL:\%.+]] = landingpad { i8*, i32 } personality i8* bitcast (i32 (...)* @__CxxFrameHandler3 to i8*)
+; CHECK: catch i8* bitcast (%rtti.TypeDescriptor2* @"\01??_R0H@8" to i8*)
+; CHECK: catch i8* bitcast (%rtti.TypeDescriptor2* @"\01??_R0M@8" to i8*)
+; ------------================= FAIL here =================------------
+; CHECK: [[RECOVER1:\%.+]] = call i8* (...)* @llvm.eh.actions({ i8*, i32 } [[LPAD1_VAL]], i32 0, i8* bitcast (%rtti.TypeDescriptor2* @"\01??_R0H@8" to i8*), i32* [[J_PTR]], i8* (i8*, i8*)* @"\01?test@@YAXXZ.catch2" to i8*), i32 1, i8* bitcast (%rtti.TypeDescriptor2* @"\01??_R0M@8" to i8*), float* [[F_PTR1]], i8* bitcast (i8* (i8*, i8*)* @"\01?test@@YAXXZ.catch1")
+; CHECK: indirectbr i8* [[RECOVER1]], []
+;
+; CHECK: invoke.cont9:
+; CHECK: ret i8* blockaddress(@"\01?test@@YAXXZ", %try.cont10)
+;
+; CHECK: [[LPAD8_LABEL]]:{{[ ]+}}; preds = %invoke.cont2
+; CHECK: [[LPAD8_VAL:\%.+]] = landingpad { i8*, i32 } personality i8* bitcast (i32 (...)* @__CxxFrameHandler3 to i8*)
+; CHECK: catch i8* bitcast (%rtti.TypeDescriptor2* @"\01??_R0M@8" to i8*)
+; ------------================= FAIL here =================------------
+; CHECK: [[RECOVER2:\%.+]] = call i8* (...)* @llvm.eh.actions({ i8*, i32 } [[LPAD8_VAL]], i32 1, i8* bitcast (%rtti.TypeDescriptor2* @"\01??_R0M@8" to i8*), float* [[F_PTR1]], i8* (i8*, i8*)* @"\01?test@@YAXXZ.catch1")
+; CHECK: indirectbr i8* [[RECOVER2]], []
+;
+; CHECK: }
+
+; CHECK: define internal i8* @"\01?test@@YAXXZ.catch1"(i8*, i8*) {
+; CHECK: entry:
+; CHECK: [[RECOVER_F1:\%.+]] = call i8* @llvm.framerecover(i8* bitcast (void ()* @"\01?test@@YAXXZ" to i8*), i8* %1, i32 1)
+; CHECK: [[F_PTR1:\%.+]] = bitcast i8* [[RECOVER_F1]] to float*
+; CHECK: [[TMP2:\%.+]] = load float, float* [[F_PTR1]], align 4
+; CHECK: call void @"\01?handle_float@@YAXM@Z"(float [[TMP2]])
+; CHECK: ret i8* blockaddress(@"\01?test@@YAXXZ", %try.cont19)
+; CHECK: }
+
+; CHECK: define internal i8* @"\01?test@@YAXXZ.catch2"(i8*, i8*) {
+; CHECK: entry:
+; ------------================= FAIL here =================------------
+; SHOULD-CHECK: [[J_PTR1:\%.+]] = call i8* @llvm.framerecover(i8* bitcast (void ()* @"\01?test@@YAXXZ" to i8*), i8* %1, i32 2)
+; CHECK: [[RECOVER_I1:\%.+]] = call i8* @llvm.framerecover(i8* bitcast (void ()* @"\01?test@@YAXXZ" to i8*), i8* %1, i32 0)
+; CHECK: [[I_PTR1:\%.+]] = bitcast i8* [[RECOVER_I1]] to i32*
+; CHECK: [[TMP3:\%.+]] = load i32, i32* [[J_PTR1]], align 4
+; CHECK: store i32 [[TMP3]], i32* [[I_PTR1]]
+; ------------================= FAIL here =================------------
+; CHECK: ret i8* blockaddress(@"\01?test@@YAXXZ.catch", %invoke.cont2)
+; CHECK: }
+
+
+declare void @"\01?may_throw@@YAXXZ"() #1
+
+declare i32 @__CxxFrameHandler3(...)
+
+; 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
+
+declare void @"\01?handle_int@@YAXH@Z"(i32) #1
+
+declare void @"\01?handle_float@@YAXM@Z"(float) #1
+
+declare void @"\01?done@@YAXXZ"() #1
+
+attributes #0 = { 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" "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" "stack-protector-buffer-size"="8" "unsafe-fp-math"="false" "use-soft-float"="false" }
+attributes #2 = { nounwind readnone }
+attributes #3 = { nounwind }
+
+!llvm.module.flags = !{!0}
+!llvm.ident = !{!1}
+
+!0 = !{i32 1, !"PIC Level", i32 2}
+!1 = !{!"clang version 3.7.0 (trunk 232069) (llvm/trunk 232070)"}
diff --git a/test/CodeGen/WinEH/cppeh-nonalloca-frame-values.ll b/test/CodeGen/WinEH/cppeh-nonalloca-frame-values.ll
new file mode 100644
index 0000000..41d9006
--- /dev/null
+++ b/test/CodeGen/WinEH/cppeh-nonalloca-frame-values.ll
@@ -0,0 +1,273 @@
+; RUN: opt -mtriple=x86_64-pc-windows-msvc -winehprepare -S -o - < %s | FileCheck %s
+
+; This test is based on the following code:
+;
+; struct SomeData {
+; int a;
+; int b;
+; };
+;
+; void may_throw();
+; void does_not_throw(int i);
+; void dump(int *, int, SomeData&);
+;
+; void test() {
+; int NumExceptions = 0;
+; int ExceptionVal[10];
+; SomeData Data = { 0, 0 };
+;
+; for (int i = 0; i < 10; ++i) {
+; try {
+; may_throw();
+; Data.a += i;
+; }
+; catch (int e) {
+; ExceptionVal[NumExceptions] = e;
+; ++NumExceptions;
+; if (e == i)
+; Data.b += e;
+; else
+; Data.a += e;
+; }
+; does_not_throw(NumExceptions);
+; }
+; dump(ExceptionVal, NumExceptions, Data);
+; }
+;
+; Unlike the cppeh-frame-vars.ll test, this test was generated using -O2
+; optimization, which results in non-alloca values being used in the
+; catch handler.
+
+; ModuleID = 'cppeh-frame-vars.cpp'
+target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
+target triple = "x86_64-pc-windows-msvc"
+
+%rtti.TypeDescriptor2 = type { i8**, i8*, [3 x i8] }
+%struct.SomeData = type { 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
+
+; The function entry should be rewritten like this.
+; CHECK: define void @"\01?test@@YAXXZ"() #0 {
+; CHECK: entry:
+; CHECK: [[NUMEXCEPTIONS_REGMEM:\%.+]] = alloca i32
+; CHECK: [[I_REGMEM:\%.+]] = alloca i32
+; CHECK: [[E_PTR:\%.+]] = alloca i32, align 4
+; CHECK: [[EXCEPTIONVAL:\%.+]] = alloca [10 x i32], align 16
+; CHECK: [[DATA_PTR:\%.+]] = alloca i64, align 8
+; CHECK: [[TMPCAST:\%.+]] = bitcast i64* [[DATA_PTR]] to %struct.SomeData*
+; CHECK: [[TMP:\%.+]] = bitcast [10 x i32]* [[EXCEPTIONVAL]] to i8*
+; CHECK: call void @llvm.lifetime.start(i64 40, i8* [[TMP]])
+; CHECK: store i64 0, i64* [[DATA_PTR]], align 8
+; CHECK: [[A_REGMEM:\%.+]] = alloca i32*
+; CHECK: [[A_PTR:\%.+]] = bitcast i64* [[DATA_PTR]] to i32*
+; CHECK: store i32* [[A_PTR]], i32** [[A_REGMEM]]
+; CHECK: [[B_PTR:\%.+]] = getelementptr inbounds %struct.SomeData, %struct.SomeData* [[TMPCAST]], i64 0, i32 1
+; CHECK: [[B_REGMEM:\%.+]] = alloca i32*
+; CHECK: store i32* [[B_PTR]], i32** [[B_REGMEM]]
+; CHECK: store i32 0, i32* [[NUMEXCEPTIONS_REGMEM]]
+; CHECK: store i32 0, i32* [[I_REGMEM]]
+; CHECK: call void (...)* @llvm.frameescape(i32* %e, i32* %NumExceptions.020.reg2mem, [10 x i32]* [[EXCEPTIONVAL]], i32* [[I_REGMEM]], i32** [[A_REGMEM]], i32** [[B_REGMEM]])
+; CHECK: br label %for.body
+
+; Function Attrs: uwtable
+define void @"\01?test@@YAXXZ"() #0 {
+entry:
+ %e = alloca i32, align 4
+ %ExceptionVal = alloca [10 x i32], align 16
+ %Data = alloca i64, align 8
+ %tmpcast = bitcast i64* %Data to %struct.SomeData*
+ %0 = bitcast [10 x i32]* %ExceptionVal to i8*
+ call void @llvm.lifetime.start(i64 40, i8* %0) #1
+ store i64 0, i64* %Data, align 8
+ %a = bitcast i64* %Data to i32*
+ %b = getelementptr inbounds %struct.SomeData, %struct.SomeData* %tmpcast, i64 0, i32 1
+ br label %for.body
+
+; CHECK: for.body:
+; CHECK-NOT: phi i32 [ 0, %entry ], [ {{\%NumExceptions.*}}, %try.cont ]
+; CHECK-NOT: phi i32 [ 0, %entry ], [ {{\%inc.*}}, %try.cont ]
+; CHECK: [[I_RELOAD:\%.+]] = load i32, i32* [[I_REGMEM]]
+; CHECK: [[NUMEXCEPTIONS_RELOAD:\%.+]] = load i32, i32* [[NUMEXCEPTIONS_REGMEM]]
+for.body: ; preds = %entry, %try.cont
+ %NumExceptions.020 = phi i32 [ 0, %entry ], [ %NumExceptions.1, %try.cont ]
+ %i.019 = phi i32 [ 0, %entry ], [ %inc5, %try.cont ]
+ invoke void @"\01?may_throw@@YAXXZ"()
+ to label %invoke.cont unwind label %lpad
+
+; CHECK: invoke.cont: ; preds = %for.body
+; CHECK: [[A_RELOAD:\%.+]] = load volatile i32*, i32** [[A_REGMEM]]
+; CHECK: [[TMP1:\%.+]] = load i32, i32* [[A_RELOAD]], align 8
+; CHECK: [[ADD:\%.+]] = add nsw i32 [[TMP1]], [[I_RELOAD]]
+; CHECK: [[A_RELOAD1:\%.+]] = load volatile i32*, i32** [[A_REGMEM]]
+; CHECK: store i32 [[ADD]], i32* [[A_RELOAD1]], align 8
+; CHECK: br label %try.cont
+invoke.cont: ; preds = %for.body
+ %1 = load i32, i32* %a, align 8, !tbaa !2
+ %add = add nsw i32 %1, %i.019
+ store i32 %add, i32* %a, align 8, !tbaa !2
+ br label %try.cont
+
+; CHECK: [[LPAD_LABEL:lpad[0-9]*]]:{{[ ]+}}; preds = %for.body
+; CHECK: landingpad { i8*, i32 } personality i8* bitcast (i32 (...)* @__CxxFrameHandler3 to i8*)
+; CHECK-NEXT: catch i8* bitcast (%rtti.TypeDescriptor2* @"\01??_R0H@8" to i8*)
+; CHECK-NEXT: [[RECOVER:\%.+]] = call i8* (...)* @llvm.eh.actions(i32 1, i8* bitcast (%rtti.TypeDescriptor2* @"\01??_R0H@8" to i8*), i32* %e, i8* (i8*, i8*)* @"\01?test@@YAXXZ.catch")
+; CHECK-NEXT: indirectbr i8* [[RECOVER]], [label %try.cont]
+
+lpad: ; preds = %for.body
+ %2 = landingpad { i8*, i32 } personality i8* bitcast (i32 (...)* @__CxxFrameHandler3 to i8*)
+ catch i8* bitcast (%rtti.TypeDescriptor2* @"\01??_R0H@8" to i8*)
+ %3 = extractvalue { i8*, i32 } %2, 1
+ %4 = tail call i32 @llvm.eh.typeid.for(i8* bitcast (%rtti.TypeDescriptor2* @"\01??_R0H@8" to i8*)) #1
+ %matches = icmp eq i32 %3, %4
+ br i1 %matches, label %catch, label %eh.resume
+
+; CHECK-NOT: catch:
+
+catch: ; preds = %lpad
+ %5 = extractvalue { i8*, i32 } %2, 0
+ %e.i8 = bitcast i32* %e to i8*
+ call void @llvm.eh.begincatch(i8* %5, i8* %e.i8) #1
+ %tmp8 = load i32, i32* %e, align 4, !tbaa !7
+ %idxprom = sext i32 %NumExceptions.020 to i64
+ %arrayidx = getelementptr inbounds [10 x i32], [10 x i32]* %ExceptionVal, i64 0, i64 %idxprom
+ store i32 %tmp8, i32* %arrayidx, align 4, !tbaa !7
+ %inc = add nsw i32 %NumExceptions.020, 1
+ %cmp1 = icmp eq i32 %tmp8, %i.019
+ br i1 %cmp1, label %if.then, label %if.else
+
+; CHECK-NOT: if.then:
+
+if.then: ; preds = %catch
+ %tmp9 = load i32, i32* %b, align 4, !tbaa !8
+ %add2 = add nsw i32 %tmp9, %i.019
+ store i32 %add2, i32* %b, align 4, !tbaa !8
+ br label %if.end
+
+; CHECK-NOT: if.else:
+
+if.else: ; preds = %catch
+ %tmp10 = load i32, i32* %a, align 8, !tbaa !2
+ %add4 = add nsw i32 %tmp10, %tmp8
+ store i32 %add4, i32* %a, align 8, !tbaa !2
+ br label %if.end
+
+; CHECK-NOT: if.end:
+
+if.end: ; preds = %if.else, %if.then
+ tail call void @llvm.eh.endcatch() #1
+ br label %try.cont
+
+; CHECK: try.cont:{{[ ]+}}; preds = %[[LPAD_LABEL]], %invoke.cont
+; CHECK-NOT: phi i32
+; CHECK: tail call void @"\01?does_not_throw@@YAXH@Z"(i32 [[NUMEXCEPTIONS_RELOAD]])
+; CHECK: [[INC:\%.+]] = add nuw nsw i32 [[I_RELOAD]], 1
+; CHECK: [[CMP:\%.+]] = icmp slt i32 [[INC]], 10
+; CHECK: store i32 [[NUMEXCEPTIONS_RELOAD]], i32* [[NUMEXCEPTIONS_REGMEM]]
+; CHECK: store i32 [[INC]], i32* [[I_REGMEM]]
+; CHECK: br i1 [[CMP]], label %for.body, label %for.end
+
+try.cont: ; preds = %if.end, %invoke.cont
+ %NumExceptions.1 = phi i32 [ %NumExceptions.020, %invoke.cont ], [ %inc, %if.end ]
+ tail call void @"\01?does_not_throw@@YAXH@Z"(i32 %NumExceptions.1)
+ %inc5 = add nuw nsw i32 %i.019, 1
+ %cmp = icmp slt i32 %inc5, 10
+ br i1 %cmp, label %for.body, label %for.end
+
+for.end: ; preds = %try.cont
+ %NumExceptions.1.lcssa = phi i32 [ %NumExceptions.1, %try.cont ]
+ %arraydecay = getelementptr inbounds [10 x i32], [10 x i32]* %ExceptionVal, i64 0, i64 0
+ call void @"\01?dump@@YAXPEAHHAEAUSomeData@@@Z"(i32* %arraydecay, i32 %NumExceptions.1.lcssa, %struct.SomeData* dereferenceable(8) %tmpcast)
+ call void @llvm.lifetime.end(i64 40, i8* %0) #1
+ ret void
+
+eh.resume: ; preds = %lpad
+ %.lcssa = phi { i8*, i32 } [ %2, %lpad ]
+ resume { i8*, i32 } %.lcssa
+}
+
+; The following catch handler should be outlined.
+; CHECK: define internal i8* @"\01?test@@YAXXZ.catch"(i8*, i8*) {
+; CHECK: entry:
+; CHECK: [[RECOVER_E:\%.+]] = call i8* @llvm.framerecover(i8* bitcast (void ()* @"\01?test@@YAXXZ" to i8*), i8* %1, i32 0)
+; CHECK: [[E_PTR:\%.+]] = bitcast i8* [[RECOVER_E]] to i32*
+; CHECK: [[RECOVER_EH_TEMP:\%.+]] = call i8* @llvm.framerecover(i8* bitcast (void ()* @"\01?test@@YAXXZ" to i8*), i8* %1, i32 1)
+; CHECK: [[EH_TEMP:\%.+]] = bitcast i8* [[RECOVER_EH_TEMP]] to i32*
+; CHECK: [[NUMEXCEPTIONS_RELOAD:\%.+]] = load i32, i32* [[EH_TEMP]]
+; CHECK: [[RECOVER_EXCEPTIONVAL:\%.+]] = call i8* @llvm.framerecover(i8* bitcast (void ()* @"\01?test@@YAXXZ" to i8*), i8* %1, i32 2)
+; CHECK: [[EXCEPTIONVAL:\%.+]] = bitcast i8* [[RECOVER_EXCEPTIONVAL]] to [10 x i32]*
+; CHECK: [[RECOVER_EH_TEMP1:\%.+]] = call i8* @llvm.framerecover(i8* bitcast (void ()* @"\01?test@@YAXXZ" to i8*), i8* %1, i32 3)
+; CHECK: [[EH_TEMP1:\%.+]] = bitcast i8* [[RECOVER_EH_TEMP1]] to i32*
+; CHECK: [[I_RELOAD:\%.+]] = load i32, i32* [[EH_TEMP1]]
+; CHECK: [[RECOVER_EH_TEMP2:\%.+]] = call i8* @llvm.framerecover(i8* bitcast (void ()* @"\01?test@@YAXXZ" to i8*), i8* %1, i32 4)
+; CHECK: [[EH_TEMP2:\%.+]] = bitcast i8* [[RECOVER_EH_TEMP2]] to i32**
+; CHECK: [[A_RELOAD:\%.+]] = load i32*, i32** [[EH_TEMP2]]
+; CHECK: [[RECOVER_EH_TEMP3:\%.+]] = call i8* @llvm.framerecover(i8* bitcast (void ()* @"\01?test@@YAXXZ" to i8*), i8* %1, i32 5)
+; CHECK: [[EH_TEMP3:\%.+]] = bitcast i8* [[RECOVER_EH_TEMP3]] to i32**
+; CHECK: [[B_RELOAD:\%.+]] = load i32*, i32** [[EH_TEMP3]]
+; CHECK: [[E_I8PTR:\%.+]] = bitcast i32* [[E_PTR]] to i8*
+; CHECK: [[TMP:\%.+]] = load i32, i32* [[E_PTR]], align 4
+; CHECK: [[IDXPROM:\%.+]] = sext i32 [[NUMEXCEPTIONS_RELOAD]] to i64
+; CHECK: [[ARRAYIDX:\%.+]] = getelementptr inbounds [10 x i32], [10 x i32]* [[EXCEPTIONVAL]], i64 0, i64 [[IDXPROM]]
+; CHECK: store i32 [[TMP]], i32* [[ARRAYIDX]], align 4
+; CHECK: [[INC:\%.+]] = add nsw i32 [[NUMEXCEPTIONS_RELOAD]], 1
+; CHECK: [[CMP:\%.+]] = icmp eq i32 [[TMP]], [[I_RELOAD]]
+; CHECK: br i1 [[CMP]], label %if.then, label %if.else
+;
+; CHECK: if.then:{{[ ]+}}; preds = %entry
+; CHECK: [[TMP1:\%.+]] = load i32, i32* [[B_RELOAD]], align 4
+; CHECK: [[ADD:\%.+]] = add nsw i32 [[TMP1]], [[I_RELOAD]]
+; CHECK: store i32 [[ADD]], i32* [[B_RELOAD]], align 4
+; CHECK: br label %if.end
+;
+; CHECK: if.else:{{[ ]+}}; preds = %entry
+; CHECK: [[TMP2:\%.+]] = load i32, i32* [[A_RELOAD]], align 8
+; CHECK: [[ADD2:\%.+]] = add nsw i32 [[TMP2]], [[TMP]]
+; CHECK: store i32 [[ADD2]], i32* [[A_RELOAD]], align 8
+; CHECK: br label %if.end
+;
+; CHECK: if.end:{{[ ]+}}; preds = %if.else, %if.then
+; CHECK: ret i8* blockaddress(@"\01?test@@YAXXZ", %try.cont)
+; CHECK: }
+
+; Function Attrs: nounwind
+declare void @llvm.lifetime.start(i64, i8* nocapture) #1
+
+declare void @"\01?may_throw@@YAXXZ"() #2
+
+declare i32 @__CxxFrameHandler3(...)
+
+; Function Attrs: nounwind readnone
+declare i32 @llvm.eh.typeid.for(i8*) #3
+
+declare void @llvm.eh.begincatch(i8*, i8*)
+
+declare void @llvm.eh.endcatch()
+
+declare void @"\01?does_not_throw@@YAXH@Z"(i32) #2
+
+declare void @"\01?dump@@YAXPEAHHAEAUSomeData@@@Z"(i32*, i32, %struct.SomeData* dereferenceable(8)) #2
+
+; Function Attrs: nounwind
+declare void @llvm.lifetime.end(i64, i8* nocapture) #1
+
+attributes #0 = { 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" "unsafe-fp-math"="false" "use-soft-float"="false" }
+attributes #1 = { nounwind }
+attributes #2 = { "less-precise-fpmad"="false" "no-frame-pointer-elim"="false" "no-infs-fp-math"="false" "no-nans-fp-math"="false" "stack-protector-buffer-size"="8" "unsafe-fp-math"="false" "use-soft-float"="false" }
+attributes #3 = { nounwind readnone }
+
+!llvm.module.flags = !{!0}
+!llvm.ident = !{!1}
+
+!0 = !{i32 1, !"PIC Level", i32 2}
+!1 = !{!"clang version 3.7.0 (trunk 228868)"}
+!2 = !{!3, !4, i64 0}
+!3 = !{!"?AUSomeData@@", !4, i64 0, !4, i64 4}
+!4 = !{!"int", !5, i64 0}
+!5 = !{!"omnipotent char", !6, i64 0}
+!6 = !{!"Simple C/C++ TBAA"}
+!7 = !{!4, !4, i64 0}
+!8 = !{!3, !4, i64 4}
diff --git a/test/CodeGen/WinEH/lit.local.cfg b/test/CodeGen/WinEH/lit.local.cfg
new file mode 100644
index 0000000..67905d7
--- /dev/null
+++ b/test/CodeGen/WinEH/lit.local.cfg
@@ -0,0 +1,12 @@
+# FIXME: For now, override suffixes to exclude any .s tests, because some of the
+# buildbots have a stray misched-copy.s output file lying around that causes
+# failures. See misched-copy.s where we try and clean up that file.
+#
+# It should be possible to remove this override once all the bots have cycled
+# cleanly.
+config.suffixes = ['.ll', '.test', '.txt']
+
+# FIXME: Add Windows on ARM support to these tests.
+if not 'X86' in config.root.targets:
+ config.unsupported = True
+
diff --git a/test/CodeGen/WinEH/seh-simple.ll b/test/CodeGen/WinEH/seh-simple.ll
new file mode 100644
index 0000000..26b3f83
--- /dev/null
+++ b/test/CodeGen/WinEH/seh-simple.ll
@@ -0,0 +1,138 @@
+; RUN: opt -S -winehprepare -sehprepare -mtriple=x86_64-windows-msvc < %s | FileCheck %s
+
+target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
+target triple = "x86_64-pc-windows-msvc"
+
+declare void @cleanup()
+declare i32 @filt()
+declare void @might_crash()
+declare i32 @__C_specific_handler(...)
+declare i32 @llvm.eh.typeid.for(i8*)
+
+define i32 @simple_except_store() {
+entry:
+ %retval = alloca i32
+ store i32 0, i32* %retval
+ invoke void @might_crash()
+ to label %return unwind label %lpad
+
+lpad:
+ %ehvals = landingpad { i8*, i32 } personality i32 (...)* @__C_specific_handler
+ catch i32 ()* @filt
+ %sel = extractvalue { i8*, i32 } %ehvals, 1
+ %filt_sel = tail call i32 @llvm.eh.typeid.for(i8* bitcast (i32 ()* @filt to i8*))
+ %matches = icmp eq i32 %sel, %filt_sel
+ br i1 %matches, label %__except, label %eh.resume
+
+__except:
+ store i32 1, i32* %retval
+ br label %return
+
+return:
+ %r = load i32, i32* %retval
+ ret i32 %r
+
+eh.resume:
+ resume { i8*, i32 } %ehvals
+}
+
+; CHECK-LABEL: define i32 @simple_except_store()
+; CHECK: landingpad { i8*, i32 }
+; CHECK-NEXT: catch i32 ()* @filt
+; CHECK-NEXT: call i8* (...)* @llvm.eh.actions(i32 1, i8* bitcast (i32 ()* @filt to i8*), i8* null, i8* blockaddress(@simple_except_store, %__except))
+; CHECK-NEXT: indirectbr {{.*}} [label %__except]
+
+define i32 @catch_all() {
+entry:
+ %retval = alloca i32
+ store i32 0, i32* %retval
+ invoke void @might_crash()
+ to label %return unwind label %lpad
+
+lpad:
+ %ehvals = landingpad { i8*, i32 } personality i32 (...)* @__C_specific_handler
+ catch i8* null
+ store i32 1, i32* %retval
+ br label %return
+
+return:
+ %r = load i32, i32* %retval
+ ret i32 %r
+}
+
+; CHECK-LABEL: define i32 @catch_all()
+; CHECK: landingpad { i8*, i32 }
+; CHECK-NEXT: catch i8* null
+; CHECK-NEXT: call i8* (...)* @llvm.eh.actions(i32 1, i8* null, i8* null, i8* blockaddress(@catch_all, %catch.all))
+; CHECK-NEXT: indirectbr {{.*}} [label %catch.all]
+;
+; CHECK: catch.all:
+; CHECK: store i32 1, i32* %retval
+
+
+define i32 @except_phi() {
+entry:
+ invoke void @might_crash()
+ to label %return unwind label %lpad
+
+lpad:
+ %ehvals = landingpad { i8*, i32 } personality i32 (...)* @__C_specific_handler
+ catch i32 ()* @filt
+ %sel = extractvalue { i8*, i32 } %ehvals, 1
+ %filt_sel = tail call i32 @llvm.eh.typeid.for(i8* bitcast (i32 ()* @filt to i8*))
+ %matches = icmp eq i32 %sel, %filt_sel
+ br i1 %matches, label %return, label %eh.resume
+
+return:
+ %r = phi i32 [0, %entry], [1, %lpad]
+ ret i32 %r
+
+eh.resume:
+ resume { i8*, i32 } %ehvals
+}
+
+; CHECK-LABEL: define i32 @except_phi()
+; CHECK: landingpad { i8*, i32 }
+; CHECK-NEXT: catch i32 ()* @filt
+; CHECK-NEXT: call i8* (...)* @llvm.eh.actions(i32 1, i8* bitcast (i32 ()* @filt to i8*), i8* null, i8* blockaddress(@except_phi, %return))
+; CHECK-NEXT: indirectbr {{.*}} [label %return]
+;
+; CHECK: return:
+; CHECK-NEXT: %r = phi i32 [ 0, %entry ], [ 1, %lpad1 ]
+; CHECK-NEXT: ret i32 %r
+
+define i32 @cleanup_and_except() {
+entry:
+ invoke void @might_crash()
+ to label %return unwind label %lpad
+
+lpad:
+ %ehvals = landingpad { i8*, i32 } personality i32 (...)* @__C_specific_handler
+ cleanup
+ catch i32 ()* @filt
+ call void @cleanup()
+ %sel = extractvalue { i8*, i32 } %ehvals, 1
+ %filt_sel = tail call i32 @llvm.eh.typeid.for(i8* bitcast (i32 ()* @filt to i8*))
+ %matches = icmp eq i32 %sel, %filt_sel
+ br i1 %matches, label %return, label %eh.resume
+
+return:
+ %r = phi i32 [0, %entry], [1, %lpad]
+ ret i32 %r
+
+eh.resume:
+ resume { i8*, i32 } %ehvals
+}
+
+; CHECK-LABEL: define i32 @cleanup_and_except()
+; CHECK: landingpad { i8*, i32 }
+; CHECK-NEXT: cleanup
+; CHECK-NEXT: catch i32 ()* @filt
+; CHECK-NEXT: call i8* (...)* @llvm.eh.actions(
+; CHECK: i32 0, void (i8*, i8*)* @cleanup_and_except.cleanup,
+; CHECK: i32 1, i8* bitcast (i32 ()* @filt to i8*), i8* null, i8* blockaddress(@cleanup_and_except, %return))
+; CHECK-NEXT: indirectbr {{.*}} [label %return]
+;
+; CHECK: return:
+; CHECK-NEXT: %r = phi i32 [ 0, %entry ], [ 1, %lpad1 ]
+; CHECK-NEXT: ret i32 %r