dd5752e8a716995acf13e39dec8cc2ed3aa91174
[oota-llvm.git] / test / CodeGen / X86 / seh-safe-div.ll
1 ; RUN: llc -mtriple x86_64-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 (...)* @__C_specific_handler to i8*) {
27 entry:
28   %r = alloca i32, align 4
29   invoke void @try_body(i32* %r, i32* %n, i32* %d)
30           to label %__try.cont unwind label %lpad0
31
32 lpad0:
33   %cs0 = catchswitch within none [label %handler0] unwind label %lpad1
34
35 handler0:
36   %p0 = catchpad within %cs0 [i8* bitcast (i32 (i8*, i8*)* @safe_div_filt0 to i8*)]
37   call void @puts(i8* getelementptr ([27 x i8], [27 x i8]* @str1, i32 0, i32 0)) [ "funclet"(token %p0) ]
38   store i32 -1, i32* %r, align 4
39   catchret from %p0 to label %__try.cont
40
41 lpad1:
42   %cs1 = catchswitch within none [label %handler1] unwind to caller
43
44 handler1:
45   %p1 = catchpad within %cs1 [i8* bitcast (i32 (i8*, i8*)* @safe_div_filt1 to i8*)]
46   call void @puts(i8* getelementptr ([29 x i8], [29 x i8]* @str2, i32 0, i32 0)) [ "funclet"(token %p1) ]
47   store i32 -2, i32* %r, align 4
48   catchret from %p1 to label %__try.cont
49
50 __try.cont:
51   %safe_ret = load i32, i32* %r, align 4
52   ret i32 %safe_ret
53 }
54
55 ; Normal path code
56
57 ; CHECK: {{^}}safe_div:
58 ; CHECK: .seh_proc safe_div
59 ; CHECK: .seh_handler __C_specific_handler, @unwind, @except
60 ; CHECK: .Ltmp0:
61 ; CHECK: leaq [[rloc:.*\(%rbp\)]], %rcx
62 ; CHECK: callq try_body
63 ; CHECK-NEXT: .Ltmp1
64 ; CHECK: [[cont_bb:\.LBB0_[0-9]+]]:
65 ; CHECK: movl [[rloc]], %eax
66 ; CHECK: retq
67
68 ; Landing pad code
69
70 ; CHECK: [[handler0:\.LBB0_[0-9]+]]: # %handler0
71 ; CHECK: callq puts
72 ; CHECK: movl $-1, [[rloc]]
73 ; CHECK: jmp [[cont_bb]]
74
75 ; CHECK: [[handler1:\.LBB0_[0-9]+]]: # %handler1
76 ; CHECK: callq puts
77 ; CHECK: movl $-2, [[rloc]]
78 ; CHECK: jmp [[cont_bb]]
79
80 ; CHECK: .seh_handlerdata
81 ; CHECK-NEXT: .long (.Llsda_end0-.Llsda_begin0)/16
82 ; CHECK-NEXT: .Llsda_begin0:
83 ; CHECK-NEXT: .long .Ltmp0@IMGREL+1
84 ; CHECK-NEXT: .long .Ltmp1@IMGREL+1
85 ; CHECK-NEXT: .long safe_div_filt0@IMGREL
86 ; CHECK-NEXT: .long [[handler0]]@IMGREL
87 ; CHECK-NEXT: .long .Ltmp0@IMGREL+1
88 ; CHECK-NEXT: .long .Ltmp1@IMGREL+1
89 ; CHECK-NEXT: .long safe_div_filt1@IMGREL
90 ; CHECK-NEXT: .long [[handler1]]@IMGREL
91 ; CHECK-NEXT: .Llsda_end0:
92 ; CHECK: .text
93 ; CHECK: .seh_endproc
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 ; The exception code can be retreived with two loads, one for the record
120 ; pointer and one for the code.  The values of local variables can be
121 ; accessed via rbp, but that would require additional not yet implemented LLVM
122 ; support.
123
124 define i32 @safe_div_filt0(i8* %eh_ptrs, i8* %rbp) {
125   %eh_ptrs_c = bitcast i8* %eh_ptrs to i32**
126   %eh_rec = load i32*, i32** %eh_ptrs_c
127   %eh_code = load i32, i32* %eh_rec
128   ; EXCEPTION_ACCESS_VIOLATION = 0xC0000005
129   %cmp = icmp eq i32 %eh_code, 3221225477
130   %filt.res = zext i1 %cmp to i32
131   ret i32 %filt.res
132 }
133
134 define i32 @safe_div_filt1(i8* %eh_ptrs, i8* %rbp) {
135   %eh_ptrs_c = bitcast i8* %eh_ptrs to i32**
136   %eh_rec = load i32*, i32** %eh_ptrs_c
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 @__C_specific_handler(...)
166 declare i32 @llvm.eh.typeid.for(i8*) readnone nounwind
167 declare void @puts(i8*)
168 declare void @printf(i8*, ...)
169 declare void @abort()