[SEH] Reimplement x64 SEH using WinEHPrepare
[oota-llvm.git] / test / CodeGen / X86 / win_eh_prepare.ll
1 ; RUN: opt -S -winehprepare -dwarfehprepare -mtriple x86_64-pc-windows-msvc < %s | FileCheck %s
2
3 ; FIXME: Add and test outlining here.
4
5 declare void @maybe_throw()
6
7 @_ZTIi = external constant i8*
8 @g = external global i32
9
10 declare i32 @__C_specific_handler(...)
11 declare i32 @__gxx_personality_seh0(...)
12 declare i32 @llvm.eh.typeid.for(i8*) readnone nounwind
13
14 define i32 @use_seh() {
15 entry:
16   invoke void @maybe_throw()
17       to label %cont unwind label %lpad
18
19 cont:
20   ret i32 0
21
22 lpad:
23   %ehvals = landingpad { i8*, i32 } personality i32 (...)* @__C_specific_handler
24       cleanup
25       catch i8* bitcast (i32 (i8*, i8*)* @filt_g to i8*)
26   %ehsel = extractvalue { i8*, i32 } %ehvals, 1
27   %filt_g_sel = call i32 @llvm.eh.typeid.for(i8* bitcast (i32 (i8*, i8*)* @filt_g to i8*))
28   %matches = icmp eq i32 %ehsel, %filt_g_sel
29   br i1 %matches, label %ret1, label %eh.resume
30
31 ret1:
32   ret i32 1
33
34 eh.resume:
35   resume { i8*, i32 } %ehvals
36 }
37
38 define internal i32 @filt_g(i8*, i8*) {
39   %g = load i32, i32* @g
40   ret i32 %g
41 }
42
43 ; CHECK-LABEL: define i32 @use_seh()
44 ; CHECK: invoke void @maybe_throw()
45 ; CHECK-NEXT: to label %cont unwind label %lpad
46 ; CHECK: landingpad
47 ; CHECK-NEXT: cleanup
48 ; CHECK-NEXT: catch
49 ; CHECK-NEXT: call i8* (...)* @llvm.eh.actions({{.*}})
50
51
52 ; A MinGW64-ish EH style. It could happen if a binary uses both MSVC CRT and
53 ; mingw CRT and is linked with LTO.
54 define i32 @use_gcc() {
55 entry:
56   invoke void @maybe_throw()
57       to label %cont unwind label %lpad
58
59 cont:
60   ret i32 0
61
62 lpad:
63   %ehvals = landingpad { i8*, i32 } personality i32 (...)* @__gxx_personality_seh0
64       cleanup
65       catch i8* bitcast (i8** @_ZTIi to i8*)
66   %ehsel = extractvalue { i8*, i32 } %ehvals, 1
67   %filt_g_sel = call i32 @llvm.eh.typeid.for(i8* bitcast (i32 (i8*, i8*)* @filt_g to i8*))
68   %matches = icmp eq i32 %ehsel, %filt_g_sel
69   br i1 %matches, label %ret1, label %eh.resume
70
71 ret1:
72   ret i32 1
73
74 eh.resume:
75   resume { i8*, i32 } %ehvals
76 }
77
78 ; CHECK-LABEL: define i32 @use_gcc()
79 ; CHECK: invoke void @maybe_throw()
80 ; CHECK-NEXT: to label %cont unwind label %lpad
81 ; CHECK: eh.resume:
82 ; CHECK: call void @_Unwind_Resume(i8* %exn.obj)