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(ptr %n, ptr %d) personality ptr @__C_specific_handler {
28 %r = alloca i32, align 4
29 invoke void @try_body(ptr %r, ptr %n, ptr %d)
30 to label %__try.cont unwind label %lpad0
33 %cs0 = catchswitch within none [label %handler0] unwind label %lpad1
36 %p0 = catchpad within %cs0 [ptr @safe_div_filt0]
37 call void @puts(ptr @str1) [ "funclet"(token %p0) ]
38 store i32 -1, ptr %r, align 4
39 catchret from %p0 to label %__try.cont
42 %cs1 = catchswitch within none [label %handler1] unwind to caller
45 %p1 = catchpad within %cs1 [ptr @safe_div_filt1]
46 call void @puts(ptr @str2) [ "funclet"(token %p1) ]
47 store i32 -2, ptr %r, align 4
48 catchret from %p1 to label %__try.cont
51 %safe_ret = load i32, ptr %r, align 4
57 ; CHECK: {{^}}safe_div:
58 ; CHECK: .seh_proc safe_div
59 ; CHECK: .seh_handler __C_specific_handler, @unwind, @except
61 ; CHECK: leaq [[rloc:.*\(%rbp\)]], %rcx
62 ; CHECK: callq try_body
64 ; CHECK: [[cont_bb:\.LBB0_[0-9]+]]:
65 ; CHECK: movl [[rloc]], %eax
70 ; CHECK: [[handler1:\.LBB0_[0-9]+]]: # %handler1
72 ; CHECK: movl $-2, [[rloc]]
73 ; CHECK: jmp [[cont_bb]]
75 ; CHECK: [[handler0:\.LBB0_[0-9]+]]: # %handler0
77 ; CHECK: movl $-1, [[rloc]]
78 ; CHECK: jmp [[cont_bb]]
80 ; CHECK: .seh_handlerdata
81 ; CHECK-NEXT: .Lsafe_div$parent_frame_offset
82 ; CHECK-NEXT: .long (.Llsda_end0-.Llsda_begin0)/16
83 ; CHECK-NEXT: .Llsda_begin0:
84 ; CHECK-NEXT: .long .Ltmp0@IMGREL
85 ; CHECK-NEXT: .long .Ltmp1@IMGREL+1
86 ; CHECK-NEXT: .long safe_div_filt0@IMGREL
87 ; CHECK-NEXT: .long [[handler0]]@IMGREL
88 ; CHECK-NEXT: .long .Ltmp0@IMGREL
89 ; CHECK-NEXT: .long .Ltmp1@IMGREL+1
90 ; CHECK-NEXT: .long safe_div_filt1@IMGREL
91 ; CHECK-NEXT: .long [[handler1]]@IMGREL
92 ; CHECK-NEXT: .Llsda_end0:
96 define void @try_body(ptr %r, ptr %n, ptr %d) {
98 %0 = load i32, ptr %n, align 4
99 %1 = load i32, ptr %d, align 4
100 %div = sdiv i32 %0, %1
101 store i32 %div, ptr %r, align 4
105 ; The prototype of these filter functions is:
106 ; int filter(EXCEPTION_POINTERS *eh_ptrs, ptr rbp);
108 ; The definition of EXCEPTION_POINTERS is:
109 ; typedef struct _EXCEPTION_POINTERS {
110 ; EXCEPTION_RECORD *ExceptionRecord;
111 ; CONTEXT *ContextRecord;
112 ; } EXCEPTION_POINTERS;
114 ; The definition of EXCEPTION_RECORD is:
115 ; typedef struct _EXCEPTION_RECORD {
116 ; DWORD ExceptionCode;
118 ; } EXCEPTION_RECORD;
120 ; The exception code can be retreived with two loads, one for the record
121 ; pointer and one for the code. The values of local variables can be
122 ; accessed via rbp, but that would require additional not yet implemented LLVM
125 define i32 @safe_div_filt0(ptr %eh_ptrs, ptr %rbp) {
126 %eh_rec = load ptr, ptr %eh_ptrs
127 %eh_code = load i32, ptr %eh_rec
128 ; EXCEPTION_ACCESS_VIOLATION = 0xC0000005
129 %cmp = icmp eq i32 %eh_code, 3221225477
130 %filt.res = zext i1 %cmp to i32
134 define i32 @safe_div_filt1(ptr %eh_ptrs, ptr %rbp) {
135 %eh_rec = load ptr, ptr %eh_ptrs
136 %eh_code = load i32, ptr %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, ptr %n.addr, align 4
150 store i32 2, ptr %d.addr, align 4
151 %r1 = call i32 @safe_div(ptr %n.addr, ptr %d.addr)
152 call void (ptr, ...) @printf(ptr @str_result, i32 %r1)
154 store i32 10, ptr %n.addr, align 4
155 store i32 0, ptr %d.addr, align 4
156 %r2 = call i32 @safe_div(ptr %n.addr, ptr %d.addr)
157 call void (ptr, ...) @printf(ptr @str_result, i32 %r2)
159 %r3 = call i32 @safe_div(ptr %n.addr, ptr null)
160 call void (ptr, ...) @printf(ptr @str_result, i32 %r3)
164 declare i32 @__C_specific_handler(...)
165 declare i32 @llvm.eh.typeid.for(ptr) readnone nounwind
166 declare void @puts(ptr)
167 declare void @printf(ptr, ...)
168 declare void @abort()