[WinEH] Teach AsmPrinter about funclets
[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 %lpad
31
32 lpad:
33   %vals = landingpad { i8*, i32 }
34           catch i8* bitcast (i32 (i8*, i8*)* @safe_div_filt0 to i8*)
35           catch i8* bitcast (i32 (i8*, i8*)* @safe_div_filt1 to i8*)
36   %ehptr = extractvalue { i8*, i32 } %vals, 0
37   %sel = extractvalue { i8*, i32 } %vals, 1
38   %filt0_val = call i32 @llvm.eh.typeid.for(i8* bitcast (i32 (i8*, i8*)* @safe_div_filt0 to i8*))
39   %is_filt0 = icmp eq i32 %sel, %filt0_val
40   br i1 %is_filt0, label %handler0, label %eh.dispatch1
41
42 eh.dispatch1:
43   %filt1_val = call i32 @llvm.eh.typeid.for(i8* bitcast (i32 (i8*, i8*)* @safe_div_filt1 to i8*))
44   %is_filt1 = icmp eq i32 %sel, %filt1_val
45   br i1 %is_filt1, label %handler1, label %eh.resume
46
47 handler0:
48   call void @puts(i8* getelementptr ([27 x i8], [27 x i8]* @str1, i32 0, i32 0))
49   store i32 -1, i32* %r, align 4
50   br label %__try.cont
51
52 handler1:
53   call void @puts(i8* getelementptr ([29 x i8], [29 x i8]* @str2, i32 0, i32 0))
54   store i32 -2, i32* %r, align 4
55   br label %__try.cont
56
57 eh.resume:
58   resume { i8*, i32 } %vals
59
60 __try.cont:
61   %safe_ret = load i32, i32* %r, align 4
62   ret i32 %safe_ret
63 }
64
65 ; Normal path code
66
67 ; CHECK: {{^}}safe_div:
68 ; CHECK: .seh_proc safe_div
69 ; CHECK: .seh_handler __C_specific_handler, @unwind, @except
70 ; CHECK: .Ltmp0:
71 ; CHECK: leaq [[rloc:.*\(%rsp\)]], %rcx
72 ; CHECK: callq try_body
73 ; CHECK-NEXT: .Ltmp1
74 ; CHECK: [[cont_bb:\.LBB0_[0-9]+]]:
75 ; CHECK: movl [[rloc]], %eax
76 ; CHECK: retq
77
78 ; Landing pad code
79
80 ; CHECK: [[handler0:\.Ltmp[0-9]+]]: # Block address taken
81 ; CHECK: # %handler0
82 ; CHECK: callq puts
83 ; CHECK: movl $-1, [[rloc]]
84 ; CHECK: jmp [[cont_bb]]
85
86 ; CHECK: [[handler1:\.Ltmp[0-9]+]]: # Block address taken
87 ; CHECK: # %handler1
88 ; CHECK: callq puts
89 ; CHECK: movl $-2, [[rloc]]
90 ; CHECK: jmp [[cont_bb]]
91
92 ; CHECK: .seh_handlerdata
93 ; CHECK-NEXT: .text
94 ; CHECK-NEXT: .Ltmp{{[0-9]+}}
95 ; CHECK-NEXT: .seh_endproc
96 ; CHECK-NEXT: .section .xdata,"dr"
97 ; CHECK-NEXT: .long 2
98 ; CHECK-NEXT: .long .Ltmp0@IMGREL
99 ; CHECK-NEXT: .long .Ltmp1@IMGREL+1
100 ; CHECK-NEXT: .long safe_div_filt0@IMGREL
101 ; CHECK-NEXT: .long [[handler0]]@IMGREL
102 ; CHECK-NEXT: .long .Ltmp0@IMGREL
103 ; CHECK-NEXT: .long .Ltmp1@IMGREL+1
104 ; CHECK-NEXT: .long safe_div_filt1@IMGREL
105 ; CHECK-NEXT: .long [[handler1]]@IMGREL
106 ; CHECK: .text
107 ; CHECK: .seh_endproc
108
109
110 define void @try_body(i32* %r, i32* %n, i32* %d) {
111 entry:
112   %0 = load i32, i32* %n, align 4
113   %1 = load i32, i32* %d, align 4
114   %div = sdiv i32 %0, %1
115   store i32 %div, i32* %r, align 4
116   ret void
117 }
118
119 ; The prototype of these filter functions is:
120 ; int filter(EXCEPTION_POINTERS *eh_ptrs, void *rbp);
121
122 ; The definition of EXCEPTION_POINTERS is:
123 ;   typedef struct _EXCEPTION_POINTERS {
124 ;     EXCEPTION_RECORD *ExceptionRecord;
125 ;     CONTEXT          *ContextRecord;
126 ;   } EXCEPTION_POINTERS;
127
128 ; The definition of EXCEPTION_RECORD is:
129 ;   typedef struct _EXCEPTION_RECORD {
130 ;     DWORD ExceptionCode;
131 ;     ...
132 ;   } EXCEPTION_RECORD;
133
134 ; The exception code can be retreived with two loads, one for the record
135 ; pointer and one for the code.  The values of local variables can be
136 ; accessed via rbp, but that would require additional not yet implemented LLVM
137 ; support.
138
139 define i32 @safe_div_filt0(i8* %eh_ptrs, i8* %rbp) {
140   %eh_ptrs_c = bitcast i8* %eh_ptrs to i32**
141   %eh_rec = load i32*, i32** %eh_ptrs_c
142   %eh_code = load i32, i32* %eh_rec
143   ; EXCEPTION_ACCESS_VIOLATION = 0xC0000005
144   %cmp = icmp eq i32 %eh_code, 3221225477
145   %filt.res = zext i1 %cmp to i32
146   ret i32 %filt.res
147 }
148
149 define i32 @safe_div_filt1(i8* %eh_ptrs, i8* %rbp) {
150   %eh_ptrs_c = bitcast i8* %eh_ptrs to i32**
151   %eh_rec = load i32*, i32** %eh_ptrs_c
152   %eh_code = load i32, i32* %eh_rec
153   ; EXCEPTION_INT_DIVIDE_BY_ZERO = 0xC0000094
154   %cmp = icmp eq i32 %eh_code, 3221225620
155   %filt.res = zext i1 %cmp to i32
156   ret i32 %filt.res
157 }
158
159 @str_result = internal constant [21 x i8] c"safe_div result: %d\0A\00"
160
161 define i32 @main() {
162   %d.addr = alloca i32, align 4
163   %n.addr = alloca i32, align 4
164
165   store i32 10, i32* %n.addr, align 4
166   store i32 2, i32* %d.addr, align 4
167   %r1 = call i32 @safe_div(i32* %n.addr, i32* %d.addr)
168   call void (i8*, ...) @printf(i8* getelementptr ([21 x i8], [21 x i8]* @str_result, i32 0, i32 0), i32 %r1)
169
170   store i32 10, i32* %n.addr, align 4
171   store i32 0, i32* %d.addr, align 4
172   %r2 = call i32 @safe_div(i32* %n.addr, i32* %d.addr)
173   call void (i8*, ...) @printf(i8* getelementptr ([21 x i8], [21 x i8]* @str_result, i32 0, i32 0), i32 %r2)
174
175   %r3 = call i32 @safe_div(i32* %n.addr, i32* null)
176   call void (i8*, ...) @printf(i8* getelementptr ([21 x i8], [21 x i8]* @str_result, i32 0, i32 0), i32 %r3)
177   ret i32 0
178 }
179
180 declare i32 @__C_specific_handler(...)
181 declare i32 @llvm.eh.typeid.for(i8*) readnone nounwind
182 declare void @puts(i8*)
183 declare void @printf(i8*, ...)
184 declare void @abort()