[WinEH] Put finally pointers in the handler scope table field
[oota-llvm.git] / test / CodeGen / X86 / seh-safe-div-win32.ll
1 ; RUN: llc -mtriple i686-pc-windows-msvc < %s | FileCheck %s
2
3 ; This test case is also intended to be run manually as a complete functional
4 ; test. It should link, print something, and exit zero rather than crashing.
5 ; It is the hypothetical lowering of a C source program that looks like:
6 ;
7 ;   int safe_div(int *n, int *d) {
8 ;     int r;
9 ;     __try {
10 ;       __try {
11 ;         r = *n / *d;
12 ;       } __except(GetExceptionCode() == EXCEPTION_ACCESS_VIOLATION) {
13 ;         puts("EXCEPTION_ACCESS_VIOLATION");
14 ;         r = -1;
15 ;       }
16 ;     } __except(GetExceptionCode() == EXCEPTION_INT_DIVIDE_BY_ZERO) {
17 ;       puts("EXCEPTION_INT_DIVIDE_BY_ZERO");
18 ;       r = -2;
19 ;     }
20 ;     return r;
21 ;   }
22
23 @str1 = internal constant [27 x i8] c"EXCEPTION_ACCESS_VIOLATION\00"
24 @str2 = internal constant [29 x i8] c"EXCEPTION_INT_DIVIDE_BY_ZERO\00"
25
26 define i32 @safe_div(i32* %n, i32* %d) {
27 entry:
28   %r = alloca i32, align 4
29   store i32 42, i32* %r
30   invoke void @try_body(i32* %r, i32* %n, i32* %d)
31           to label %__try.cont unwind label %lpad
32
33 lpad:
34   %vals = landingpad { i8*, i32 } personality i8* bitcast (i32 (...)* @_except_handler3 to i8*)
35           catch i8* bitcast (i32 ()* @safe_div_filt0 to i8*)
36           catch i8* bitcast (i32 ()* @safe_div_filt1 to i8*)
37   %ehptr = extractvalue { i8*, i32 } %vals, 0
38   %sel = extractvalue { i8*, i32 } %vals, 1
39   %filt0_val = call i32 @llvm.eh.typeid.for(i8* bitcast (i32 ()* @safe_div_filt0 to i8*))
40   %is_filt0 = icmp eq i32 %sel, %filt0_val
41   br i1 %is_filt0, label %handler0, label %eh.dispatch1
42
43 eh.dispatch1:
44   %filt1_val = call i32 @llvm.eh.typeid.for(i8* bitcast (i32 ()* @safe_div_filt1 to i8*))
45   %is_filt1 = icmp eq i32 %sel, %filt1_val
46   br i1 %is_filt1, label %handler1, label %eh.resume
47
48 handler0:
49   call void @puts(i8* getelementptr ([27 x i8], [27 x i8]* @str1, i32 0, i32 0))
50   store i32 -1, i32* %r, align 4
51   br label %__try.cont
52
53 handler1:
54   call void @puts(i8* getelementptr ([29 x i8], [29 x i8]* @str2, i32 0, i32 0))
55   store i32 -2, i32* %r, align 4
56   br label %__try.cont
57
58 eh.resume:
59   resume { i8*, i32 } %vals
60
61 __try.cont:
62   %safe_ret = load i32, i32* %r, align 4
63   ret i32 %safe_ret
64 }
65
66 ; Normal path code
67
68 ; CHECK: {{^}}_safe_div:
69 ; CHECK: movl $42, [[rloc:.*\(%ebp\)]]
70 ; CHECK: leal [[rloc]],
71 ; CHECK: calll _try_body
72 ; CHECK: [[cont_bb:LBB0_[0-9]+]]:
73 ; CHECK: movl [[rloc]], %eax
74 ; CHECK: retl
75
76 ; Landing pad code
77
78 ; CHECK: [[handler0:Ltmp[0-9]+]]: # Block address taken
79 ; CHECK: # %handler0
80 ;       Restore SP
81 ; CHECK: movl {{.*}}(%ebp), %esp
82 ; CHECK: calll _puts
83 ; CHECK: jmp [[cont_bb]]
84
85 ; CHECK: [[handler1:Ltmp[0-9]+]]: # Block address taken
86 ; CHECK: # %handler1
87 ;       Restore SP
88 ; CHECK: movl {{.*}}(%ebp), %esp
89 ; CHECK: calll _puts
90 ; CHECK: jmp [[cont_bb]]
91
92 ; CHECK: .section .xdata,"dr"
93 ; CHECK: L__ehtable$safe_div:
94 ; CHECK-NEXT: .long -1
95 ; CHECK-NEXT: .long _safe_div_filt1
96 ; CHECK-NEXT: .long [[handler1]]
97 ; CHECK-NEXT: .long 0
98 ; CHECK-NEXT: .long _safe_div_filt0
99 ; CHECK-NEXT: .long [[handler0]]
100
101 define void @try_body(i32* %r, i32* %n, i32* %d) {
102 entry:
103   %0 = load i32, i32* %n, align 4
104   %1 = load i32, i32* %d, align 4
105   %div = sdiv i32 %0, %1
106   store i32 %div, i32* %r, align 4
107   ret void
108 }
109
110 ; The prototype of these filter functions is:
111 ; int filter(EXCEPTION_POINTERS *eh_ptrs, void *rbp);
112
113 ; The definition of EXCEPTION_POINTERS is:
114 ;   typedef struct _EXCEPTION_POINTERS {
115 ;     EXCEPTION_RECORD *ExceptionRecord;
116 ;     CONTEXT          *ContextRecord;
117 ;   } EXCEPTION_POINTERS;
118
119 ; The definition of EXCEPTION_RECORD is:
120 ;   typedef struct _EXCEPTION_RECORD {
121 ;     DWORD ExceptionCode;
122 ;     ...
123 ;   } EXCEPTION_RECORD;
124
125 ; FIXME: Use llvm.eh.exceptioninfo for this.
126 declare i32 @safe_div_filt0()
127 declare i32 @safe_div_filt1()
128 ; define i32 @safe_div_filt0() {
129 ;   %eh_ptrs_c = bitcast i8* %eh_ptrs to i32**
130 ;   %eh_rec = load i32*, i32** %eh_ptrs_c
131 ;   %eh_code = load i32, i32* %eh_rec
132 ;   ; EXCEPTION_ACCESS_VIOLATION = 0xC0000005
133 ;   %cmp = icmp eq i32 %eh_code, 3221225477
134 ;   %filt.res = zext i1 %cmp to i32
135 ;   ret i32 %filt.res
136 ; }
137 ; define i32 @safe_div_filt1() {
138 ;   %eh_ptrs_c = bitcast i8* %eh_ptrs to i32**
139 ;   %eh_rec = load i32*, i32** %eh_ptrs_c
140 ;   %eh_code = load i32, i32* %eh_rec
141 ;   ; EXCEPTION_INT_DIVIDE_BY_ZERO = 0xC0000094
142 ;   %cmp = icmp eq i32 %eh_code, 3221225620
143 ;   %filt.res = zext i1 %cmp to i32
144 ;   ret i32 %filt.res
145 ; }
146
147 @str_result = internal constant [21 x i8] c"safe_div result: %d\0A\00"
148
149 define i32 @main() {
150   %d.addr = alloca i32, align 4
151   %n.addr = alloca i32, align 4
152
153   store i32 10, i32* %n.addr, align 4
154   store i32 2, i32* %d.addr, align 4
155   %r1 = call i32 @safe_div(i32* %n.addr, i32* %d.addr)
156   call void (i8*, ...) @printf(i8* getelementptr ([21 x i8], [21 x i8]* @str_result, i32 0, i32 0), i32 %r1)
157
158   store i32 10, i32* %n.addr, align 4
159   store i32 0, i32* %d.addr, align 4
160   %r2 = call i32 @safe_div(i32* %n.addr, i32* %d.addr)
161   call void (i8*, ...) @printf(i8* getelementptr ([21 x i8], [21 x i8]* @str_result, i32 0, i32 0), i32 %r2)
162
163   %r3 = call i32 @safe_div(i32* %n.addr, i32* null)
164   call void (i8*, ...) @printf(i8* getelementptr ([21 x i8], [21 x i8]* @str_result, i32 0, i32 0), i32 %r3)
165   ret i32 0
166 }
167
168 declare i32 @_except_handler3(...)
169 declare i32 @llvm.eh.typeid.for(i8*) readnone nounwind
170 declare void @puts(i8*)
171 declare void @printf(i8*, ...)
172 declare void @abort()