1 ; RUN: llc -mtriple i686-pc-windows-msvc < %s | FileCheck %s
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:
7 ; int safe_div(int *n, int *d) {
12 ; } __except(GetExceptionCode() == EXCEPTION_ACCESS_VIOLATION) {
13 ; puts("EXCEPTION_ACCESS_VIOLATION");
16 ; } __except(GetExceptionCode() == EXCEPTION_INT_DIVIDE_BY_ZERO) {
17 ; puts("EXCEPTION_INT_DIVIDE_BY_ZERO");
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"
26 define i32 @safe_div(i32* %n, i32* %d) personality i8* bitcast (i32 (...)* @_except_handler3 to i8*) {
28 %r = alloca i32, align 4
30 invoke void @try_body(i32* %r, i32* %n, i32* %d)
31 to label %__try.cont unwind label %lpad
34 %vals = landingpad { i8*, i32 }
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
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
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
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
59 resume { i8*, i32 } %vals
62 %safe_ret = load i32, i32* %r, align 4
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
78 ; CHECK: [[handler0:Ltmp[0-9]+]]: # Block address taken
81 ; CHECK: movl {{.*}}(%ebp), %esp
83 ; CHECK: jmp [[cont_bb]]
85 ; CHECK: [[handler1:Ltmp[0-9]+]]: # Block address taken
88 ; CHECK: movl {{.*}}(%ebp), %esp
90 ; CHECK: jmp [[cont_bb]]
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]]
98 ; CHECK-NEXT: .long _safe_div_filt0
99 ; CHECK-NEXT: .long [[handler0]]
101 define void @try_body(i32* %r, i32* %n, i32* %d) {
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
110 ; The prototype of these filter functions is:
111 ; int filter(EXCEPTION_POINTERS *eh_ptrs, void *rbp);
113 ; The definition of EXCEPTION_POINTERS is:
114 ; typedef struct _EXCEPTION_POINTERS {
115 ; EXCEPTION_RECORD *ExceptionRecord;
116 ; CONTEXT *ContextRecord;
117 ; } EXCEPTION_POINTERS;
119 ; The definition of EXCEPTION_RECORD is:
120 ; typedef struct _EXCEPTION_RECORD {
121 ; DWORD ExceptionCode;
123 ; } EXCEPTION_RECORD;
125 define i32 @safe_div_filt0() {
126 %ebp = call i8* @llvm.frameaddress(i32 1)
127 %eh_ptrs.addr.i8 = getelementptr inbounds i8, i8* %ebp, i32 -20
128 %eh_ptrs.addr = bitcast i8* %eh_ptrs.addr.i8 to i32***
129 %eh_ptrs = load i32**, i32*** %eh_ptrs.addr
130 %eh_rec = load i32*, i32** %eh_ptrs
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
137 define i32 @safe_div_filt1() {
138 %ebp = call i8* @llvm.frameaddress(i32 1)
139 %eh_ptrs.addr.i8 = getelementptr inbounds i8, i8* %ebp, i32 -20
140 %eh_ptrs.addr = bitcast i8* %eh_ptrs.addr.i8 to i32***
141 %eh_ptrs = load i32**, i32*** %eh_ptrs.addr
142 %eh_rec = load i32*, i32** %eh_ptrs
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
150 @str_result = internal constant [21 x i8] c"safe_div result: %d\0A\00"
153 %d.addr = alloca i32, align 4
154 %n.addr = alloca i32, align 4
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)
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)
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)
171 declare i32 @_except_handler3(...)
172 declare i32 @llvm.eh.typeid.for(i8*) readnone nounwind
173 declare void @puts(i8*)
174 declare void @printf(i8*, ...)
175 declare void @abort()
176 declare i8* @llvm.frameaddress(i32)