1 ; RUN: llc -mtriple x86_64-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
29 invoke void @try_body(i32* %r, i32* %n, i32* %d)
30 to label %__try.cont unwind label %lpad
33 %vals = landingpad { i8*, i32 } personality i8* bitcast (i32 (...)* @__C_specific_handler to i8*)
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
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
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
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
58 resume { i8*, i32 } %vals
61 %safe_ret = load i32, i32* %r, align 4
67 ; CHECK: {{^}}safe_div:
68 ; CHECK: .seh_proc safe_div
69 ; CHECK: .seh_handler __C_specific_handler, @unwind, @except
71 ; CHECK: leaq [[rloc:.*\(%rsp\)]], %rcx
72 ; CHECK: callq try_body
74 ; CHECK: [[cont_bb:\.LBB0_[0-9]+]]:
75 ; CHECK: movl [[rloc]], %eax
80 ; CHECK: [[handler0:\.Ltmp[0-9]+]]: # Block address taken
83 ; CHECK: movl $-1, [[rloc]]
84 ; CHECK: jmp [[cont_bb]]
86 ; CHECK: [[handler1:\.Ltmp[0-9]+]]: # Block address taken
89 ; CHECK: movl $-2, [[rloc]]
90 ; CHECK: jmp [[cont_bb]]
92 ; CHECK: .seh_handlerdata
94 ; CHECK-NEXT: .long .Ltmp0@IMGREL
95 ; CHECK-NEXT: .long .Ltmp1@IMGREL+1
96 ; CHECK-NEXT: .long safe_div_filt0@IMGREL
97 ; CHECK-NEXT: .long [[handler0]]@IMGREL
98 ; CHECK-NEXT: .long .Ltmp0@IMGREL
99 ; CHECK-NEXT: .long .Ltmp1@IMGREL+1
100 ; CHECK-NEXT: .long safe_div_filt1@IMGREL
101 ; CHECK-NEXT: .long [[handler1]]@IMGREL
103 ; CHECK: .seh_endproc
106 define void @try_body(i32* %r, i32* %n, i32* %d) {
108 %0 = load i32, i32* %n, align 4
109 %1 = load i32, i32* %d, align 4
110 %div = sdiv i32 %0, %1
111 store i32 %div, i32* %r, align 4
115 ; The prototype of these filter functions is:
116 ; int filter(EXCEPTION_POINTERS *eh_ptrs, void *rbp);
118 ; The definition of EXCEPTION_POINTERS is:
119 ; typedef struct _EXCEPTION_POINTERS {
120 ; EXCEPTION_RECORD *ExceptionRecord;
121 ; CONTEXT *ContextRecord;
122 ; } EXCEPTION_POINTERS;
124 ; The definition of EXCEPTION_RECORD is:
125 ; typedef struct _EXCEPTION_RECORD {
126 ; DWORD ExceptionCode;
128 ; } EXCEPTION_RECORD;
130 ; The exception code can be retreived with two loads, one for the record
131 ; pointer and one for the code. The values of local variables can be
132 ; accessed via rbp, but that would require additional not yet implemented LLVM
135 define i32 @safe_div_filt0(i8* %eh_ptrs, i8* %rbp) {
136 %eh_ptrs_c = bitcast i8* %eh_ptrs to i32**
137 %eh_rec = load i32*, i32** %eh_ptrs_c
138 %eh_code = load i32, i32* %eh_rec
139 ; EXCEPTION_ACCESS_VIOLATION = 0xC0000005
140 %cmp = icmp eq i32 %eh_code, 3221225477
141 %filt.res = zext i1 %cmp to i32
145 define i32 @safe_div_filt1(i8* %eh_ptrs, i8* %rbp) {
146 %eh_ptrs_c = bitcast i8* %eh_ptrs to i32**
147 %eh_rec = load i32*, i32** %eh_ptrs_c
148 %eh_code = load i32, i32* %eh_rec
149 ; EXCEPTION_INT_DIVIDE_BY_ZERO = 0xC0000094
150 %cmp = icmp eq i32 %eh_code, 3221225620
151 %filt.res = zext i1 %cmp to i32
155 @str_result = internal constant [21 x i8] c"safe_div result: %d\0A\00"
158 %d.addr = alloca i32, align 4
159 %n.addr = alloca i32, align 4
161 store i32 10, i32* %n.addr, align 4
162 store i32 2, i32* %d.addr, align 4
163 %r1 = 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 %r1)
166 store i32 10, i32* %n.addr, align 4
167 store i32 0, i32* %d.addr, align 4
168 %r2 = call i32 @safe_div(i32* %n.addr, i32* %d.addr)
169 call void (i8*, ...) @printf(i8* getelementptr ([21 x i8], [21 x i8]* @str_result, i32 0, i32 0), i32 %r2)
171 %r3 = call i32 @safe_div(i32* %n.addr, i32* null)
172 call void (i8*, ...) @printf(i8* getelementptr ([21 x i8], [21 x i8]* @str_result, i32 0, i32 0), i32 %r3)
176 declare i32 @__C_specific_handler(...)
177 declare i32 @llvm.eh.typeid.for(i8*) readnone nounwind
178 declare void @puts(i8*)
179 declare void @printf(i8*, ...)
180 declare void @abort()