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) {
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 } personality i8* bitcast (i32 (...)* @_except_handler3 to i8*)
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: jmp [[cont_bb]]
83 ; CHECK: [[handler1:Ltmp[0-9]+]]: # Block address taken
86 ; CHECK: jmp [[cont_bb]]
88 ; CHECK: .section .xdata,"dr"
89 ; CHECK-NEXT: L__ehtable$safe_div:
90 ; CHECK-NEXT: .long -1
91 ; CHECK-NEXT: .long _safe_div_filt0
92 ; CHECK-NEXT: .long [[handler0]]
94 ; CHECK-NEXT: .long _safe_div_filt1
95 ; CHECK-NEXT: .long [[handler1]]
97 define void @try_body(i32* %r, i32* %n, i32* %d) {
99 %0 = load i32, i32* %n, align 4
100 %1 = load i32, i32* %d, align 4
101 %div = sdiv i32 %0, %1
102 store i32 %div, i32* %r, align 4
106 ; The prototype of these filter functions is:
107 ; int filter(EXCEPTION_POINTERS *eh_ptrs, void *rbp);
109 ; The definition of EXCEPTION_POINTERS is:
110 ; typedef struct _EXCEPTION_POINTERS {
111 ; EXCEPTION_RECORD *ExceptionRecord;
112 ; CONTEXT *ContextRecord;
113 ; } EXCEPTION_POINTERS;
115 ; The definition of EXCEPTION_RECORD is:
116 ; typedef struct _EXCEPTION_RECORD {
117 ; DWORD ExceptionCode;
119 ; } EXCEPTION_RECORD;
121 ; FIXME: Use llvm.eh.exceptioninfo for this.
122 declare i32 @safe_div_filt0()
123 declare i32 @safe_div_filt1()
124 ; define i32 @safe_div_filt0() {
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
133 ; define i32 @safe_div_filt1() {
134 ; %eh_ptrs_c = bitcast i8* %eh_ptrs to i32**
135 ; %eh_rec = load i32*, i32** %eh_ptrs_c
136 ; %eh_code = load i32, i32* %eh_rec
137 ; ; EXCEPTION_INT_DIVIDE_BY_ZERO = 0xC0000094
138 ; %cmp = icmp eq i32 %eh_code, 3221225620
139 ; %filt.res = zext i1 %cmp to i32
143 @str_result = internal constant [21 x i8] c"safe_div result: %d\0A\00"
146 %d.addr = alloca i32, align 4
147 %n.addr = alloca i32, align 4
149 store i32 10, i32* %n.addr, align 4
150 store i32 2, i32* %d.addr, align 4
151 %r1 = call i32 @safe_div(i32* %n.addr, i32* %d.addr)
152 call void (i8*, ...) @printf(i8* getelementptr ([21 x i8], [21 x i8]* @str_result, i32 0, i32 0), i32 %r1)
154 store i32 10, i32* %n.addr, align 4
155 store i32 0, i32* %d.addr, align 4
156 %r2 = call i32 @safe_div(i32* %n.addr, i32* %d.addr)
157 call void (i8*, ...) @printf(i8* getelementptr ([21 x i8], [21 x i8]* @str_result, i32 0, i32 0), i32 %r2)
159 %r3 = call i32 @safe_div(i32* %n.addr, i32* null)
160 call void (i8*, ...) @printf(i8* getelementptr ([21 x i8], [21 x i8]* @str_result, i32 0, i32 0), i32 %r3)
164 declare i32 @_except_handler3(...)
165 declare i32 @llvm.eh.typeid.for(i8*) readnone nounwind
166 declare void @puts(i8*)
167 declare void @printf(i8*, ...)
168 declare void @abort()