d17f2217ca2fc171d2432eb85a096ba2d7694d98
[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) personality i8* bitcast (i32 (...)* @_except_handler3 to i8*) {
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 %lpad0
32
33 lpad0:
34   %p0 = catchpad [i8* bitcast (i32 ()* @safe_div_filt0 to i8*)]
35           to label %handler0 unwind label %endpad0
36
37 handler0:
38   call void @puts(i8* getelementptr ([27 x i8], [27 x i8]* @str1, i32 0, i32 0))
39   store i32 -1, i32* %r, align 4
40   catchret %p0 to label %__try.cont
41
42 endpad0:
43   catchendpad unwind label %lpad1
44
45 lpad1:
46   %p1 = catchpad [i8* bitcast (i32 ()* @safe_div_filt1 to i8*)]
47           to label %handler1 unwind label %endpad1
48
49 handler1:
50   call void @puts(i8* getelementptr ([29 x i8], [29 x i8]* @str2, i32 0, i32 0))
51   store i32 -2, i32* %r, align 4
52   catchret %p1 to label %__try.cont
53
54 endpad1:
55   catchendpad unwind to caller
56
57 __try.cont:
58   %safe_ret = load i32, i32* %r, align 4
59   ret i32 %safe_ret
60 }
61
62 ; Normal path code
63
64 ; CHECK: {{^}}_safe_div:
65 ; CHECK: movl $42, [[rloc:.*\(%ebp\)]]
66 ; CHECK: leal [[rloc]],
67 ; CHECK: calll _try_body
68 ; CHECK: [[cont_bb:LBB0_[0-9]+]]:
69 ; CHECK: movl [[rloc]], %eax
70 ; CHECK: retl
71
72 ; Landing pad code
73
74 ; CHECK: [[lpad1:LBB0_[0-9]+]]: # %lpad1
75 ;       Restore SP
76 ; CHECK: movl {{.*}}(%ebp), %esp
77 ; CHECK: calll _puts
78 ; CHECK: jmp [[cont_bb]]
79
80 ; CHECK: [[lpad0:LBB0_[0-9]+]]: # %lpad0
81 ;       Restore SP
82 ; CHECK: movl {{.*}}(%ebp), %esp
83 ; CHECK: calll _puts
84 ; CHECK: jmp [[cont_bb]]
85
86 ; CHECK: .section .xdata,"dr"
87 ; CHECK: L__ehtable$safe_div:
88 ; CHECK-NEXT: .long -1
89 ; CHECK-NEXT: .long _safe_div_filt1
90 ; CHECK-NEXT: .long [[lpad1]]
91 ; CHECK-NEXT: .long 0
92 ; CHECK-NEXT: .long _safe_div_filt0
93 ; CHECK-NEXT: .long [[lpad0]]
94
95 define void @try_body(i32* %r, i32* %n, i32* %d) {
96 entry:
97   %0 = load i32, i32* %n, align 4
98   %1 = load i32, i32* %d, align 4
99   %div = sdiv i32 %0, %1
100   store i32 %div, i32* %r, align 4
101   ret void
102 }
103
104 ; The prototype of these filter functions is:
105 ; int filter(EXCEPTION_POINTERS *eh_ptrs, void *rbp);
106
107 ; The definition of EXCEPTION_POINTERS is:
108 ;   typedef struct _EXCEPTION_POINTERS {
109 ;     EXCEPTION_RECORD *ExceptionRecord;
110 ;     CONTEXT          *ContextRecord;
111 ;   } EXCEPTION_POINTERS;
112
113 ; The definition of EXCEPTION_RECORD is:
114 ;   typedef struct _EXCEPTION_RECORD {
115 ;     DWORD ExceptionCode;
116 ;     ...
117 ;   } EXCEPTION_RECORD;
118
119 define i32 @safe_div_filt0() {
120   %ebp = call i8* @llvm.frameaddress(i32 1)
121   %eh_ptrs.addr.i8 = getelementptr inbounds i8, i8* %ebp, i32 -20
122   %eh_ptrs.addr = bitcast i8* %eh_ptrs.addr.i8 to i32***
123   %eh_ptrs = load i32**, i32*** %eh_ptrs.addr
124   %eh_rec = load i32*, i32** %eh_ptrs
125   %eh_code = load i32, i32* %eh_rec
126   ; EXCEPTION_ACCESS_VIOLATION = 0xC0000005
127   %cmp = icmp eq i32 %eh_code, 3221225477
128   %filt.res = zext i1 %cmp to i32
129   ret i32 %filt.res
130 }
131 define i32 @safe_div_filt1() {
132   %ebp = call i8* @llvm.frameaddress(i32 1)
133   %eh_ptrs.addr.i8 = getelementptr inbounds i8, i8* %ebp, i32 -20
134   %eh_ptrs.addr = bitcast i8* %eh_ptrs.addr.i8 to i32***
135   %eh_ptrs = load i32**, i32*** %eh_ptrs.addr
136   %eh_rec = load i32*, i32** %eh_ptrs
137   %eh_code = load i32, i32* %eh_rec
138   ; EXCEPTION_INT_DIVIDE_BY_ZERO = 0xC0000094
139   %cmp = icmp eq i32 %eh_code, 3221225620
140   %filt.res = zext i1 %cmp to i32
141   ret i32 %filt.res
142 }
143
144 @str_result = internal constant [21 x i8] c"safe_div result: %d\0A\00"
145
146 define i32 @main() {
147   %d.addr = alloca i32, align 4
148   %n.addr = alloca i32, align 4
149
150   store i32 10, i32* %n.addr, align 4
151   store i32 2, i32* %d.addr, align 4
152   %r1 = call i32 @safe_div(i32* %n.addr, i32* %d.addr)
153   call void (i8*, ...) @printf(i8* getelementptr ([21 x i8], [21 x i8]* @str_result, i32 0, i32 0), i32 %r1)
154
155   store i32 10, i32* %n.addr, align 4
156   store i32 0, i32* %d.addr, align 4
157   %r2 = call i32 @safe_div(i32* %n.addr, i32* %d.addr)
158   call void (i8*, ...) @printf(i8* getelementptr ([21 x i8], [21 x i8]* @str_result, i32 0, i32 0), i32 %r2)
159
160   %r3 = call i32 @safe_div(i32* %n.addr, i32* null)
161   call void (i8*, ...) @printf(i8* getelementptr ([21 x i8], [21 x i8]* @str_result, i32 0, i32 0), i32 %r3)
162   ret i32 0
163 }
164
165 declare i32 @_except_handler3(...)
166 declare i32 @llvm.eh.typeid.for(i8*) readnone nounwind
167 declare void @puts(i8*)
168 declare void @printf(i8*, ...)
169 declare void @abort()
170 declare i8* @llvm.frameaddress(i32)