[SEH] Update SEH codegen tests to use the new IR
[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   %p0 = catchpad [i8* bitcast (i32 (i8*, i8*)* @safe_div_filt0 to i8*)]
34           to label %handler0 unwind label %endpad0
35
36 handler0:
37   call void @puts(i8* getelementptr ([27 x i8], [27 x i8]* @str1, i32 0, i32 0))
38   store i32 -1, i32* %r, align 4
39   catchret %p0 to label %__try.cont
40
41 endpad0:
42   catchendpad unwind label %lpad1
43
44 lpad1:
45   %p1 = catchpad [i8* bitcast (i32 (i8*, i8*)* @safe_div_filt1 to i8*)]
46           to label %handler1 unwind label %endpad1
47
48 handler1:
49   call void @puts(i8* getelementptr ([29 x i8], [29 x i8]* @str2, i32 0, i32 0))
50   store i32 -2, i32* %r, align 4
51   catchret %p1 to label %__try.cont
52
53 endpad1:
54   catchendpad unwind to caller
55
56 __try.cont:
57   %safe_ret = load i32, i32* %r, align 4
58   ret i32 %safe_ret
59 }
60
61 ; Normal path code
62
63 ; CHECK: {{^}}safe_div:
64 ; CHECK: .seh_proc safe_div
65 ; CHECK: .seh_handler __C_specific_handler, @unwind, @except
66 ; CHECK: .Ltmp0:
67 ; CHECK: leaq [[rloc:.*\(%rbp\)]], %rcx
68 ; CHECK: callq try_body
69 ; CHECK-NEXT: .Ltmp1
70 ; CHECK: [[cont_bb:\.LBB0_[0-9]+]]:
71 ; CHECK: movl [[rloc]], %eax
72 ; CHECK: retq
73
74 ; Landing pad code
75
76 ; CHECK: [[lpad1:\.LBB0_[0-9]+]]: # %lpad1
77 ; CHECK: callq puts
78 ; CHECK: movl $-2, [[rloc]]
79 ; CHECK: jmp [[cont_bb]]
80
81 ; CHECK: [[lpad0:\.LBB0_[0-9]+]]: # %lpad0
82 ; CHECK: callq puts
83 ; CHECK: movl $-1, [[rloc]]
84 ; CHECK: jmp [[cont_bb]]
85
86 ; CHECK: .seh_handlerdata
87 ; CHECK-NEXT: .long (.Llsda_end0-.Llsda_begin0)/16
88 ; CHECK-NEXT: .Llsda_begin0:
89 ; CHECK-NEXT: .long .Ltmp0@IMGREL+1
90 ; CHECK-NEXT: .long .Ltmp1@IMGREL+1
91 ; CHECK-NEXT: .long safe_div_filt0@IMGREL
92 ; CHECK-NEXT: .long [[lpad0]]@IMGREL
93 ; CHECK-NEXT: .long .Ltmp0@IMGREL+1
94 ; CHECK-NEXT: .long .Ltmp1@IMGREL+1
95 ; CHECK-NEXT: .long safe_div_filt1@IMGREL
96 ; CHECK-NEXT: .long [[lpad1]]@IMGREL
97 ; CHECK-NEXT: .Llsda_end0:
98 ; CHECK: .text
99 ; CHECK: .seh_endproc
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 ; The exception code can be retreived with two loads, one for the record
126 ; pointer and one for the code.  The values of local variables can be
127 ; accessed via rbp, but that would require additional not yet implemented LLVM
128 ; support.
129
130 define i32 @safe_div_filt0(i8* %eh_ptrs, i8* %rbp) {
131   %eh_ptrs_c = bitcast i8* %eh_ptrs to i32**
132   %eh_rec = load i32*, i32** %eh_ptrs_c
133   %eh_code = load i32, i32* %eh_rec
134   ; EXCEPTION_ACCESS_VIOLATION = 0xC0000005
135   %cmp = icmp eq i32 %eh_code, 3221225477
136   %filt.res = zext i1 %cmp to i32
137   ret i32 %filt.res
138 }
139
140 define i32 @safe_div_filt1(i8* %eh_ptrs, i8* %rbp) {
141   %eh_ptrs_c = bitcast i8* %eh_ptrs to i32**
142   %eh_rec = load i32*, i32** %eh_ptrs_c
143   %eh_code = load i32, i32* %eh_rec
144   ; EXCEPTION_INT_DIVIDE_BY_ZERO = 0xC0000094
145   %cmp = icmp eq i32 %eh_code, 3221225620
146   %filt.res = zext i1 %cmp to i32
147   ret i32 %filt.res
148 }
149
150 @str_result = internal constant [21 x i8] c"safe_div result: %d\0A\00"
151
152 define i32 @main() {
153   %d.addr = alloca i32, align 4
154   %n.addr = alloca i32, align 4
155
156   store i32 10, i32* %n.addr, align 4
157   store i32 2, i32* %d.addr, align 4
158   %r1 = call i32 @safe_div(i32* %n.addr, i32* %d.addr)
159   call void (i8*, ...) @printf(i8* getelementptr ([21 x i8], [21 x i8]* @str_result, i32 0, i32 0), i32 %r1)
160
161   store i32 10, i32* %n.addr, align 4
162   store i32 0, i32* %d.addr, align 4
163   %r2 = call i32 @safe_div(i32* %n.addr, i32* %d.addr)
164   call void (i8*, ...) @printf(i8* getelementptr ([21 x i8], [21 x i8]* @str_result, i32 0, i32 0), i32 %r2)
165
166   %r3 = call i32 @safe_div(i32* %n.addr, i32* null)
167   call void (i8*, ...) @printf(i8* getelementptr ([21 x i8], [21 x i8]* @str_result, i32 0, i32 0), i32 %r3)
168   ret i32 0
169 }
170
171 declare i32 @__C_specific_handler(...)
172 declare i32 @llvm.eh.typeid.for(i8*) readnone nounwind
173 declare void @puts(i8*)
174 declare void @printf(i8*, ...)
175 declare void @abort()