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) { 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 } 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 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: .LBB0_7: 75 ; CHECK: movl [[rloc]], %eax 76 ; CHECK: retq 77 78 ; Landing pad code 79 80 ; CHECK: .Ltmp3: 81 ; CHECK: movl $1, %[[sel:[a-z]+]] 82 ; CHECK: .Ltmp4 83 ; CHECK: movl $2, %[[sel]] 84 ; CHECK: .L{{.*}}: 85 ; CHECK: cmpl $1, %[[sel]] 86 87 ; CHECK: # %handler0 88 ; CHECK: callq puts 89 ; CHECK: movl $-1, [[rloc]] 90 ; CHECK: jmp .LBB0_7 91 92 ; CHECK: cmpl $2, %[[sel]] 93 94 ; CHECK: # %handler1 95 ; CHECK: callq puts 96 ; CHECK: movl $-2, [[rloc]] 97 ; CHECK: jmp .LBB0_7 98 99 ; FIXME: EH preparation should eliminate the 'resume' instr and we should not do 100 ; the previous 'cmp;jeq'. 101 ; CHECK-NOT: _Unwind_Resume 102 ; CHECK: ud2 103 104 ; CHECK: .seh_handlerdata 105 ; CHECK: .long 2 106 ; CHECK: .long .Ltmp0@IMGREL 107 ; CHECK: .long .Ltmp1@IMGREL+1 108 ; CHECK: .long safe_div_filt0@IMGREL 109 ; CHECK: .long .Ltmp3@IMGREL 110 ; CHECK: .long .Ltmp0@IMGREL 111 ; CHECK: .long .Ltmp1@IMGREL+1 112 ; CHECK: .long safe_div_filt1@IMGREL 113 ; CHECK: .long .Ltmp4@IMGREL 114 ; CHECK: .text 115 ; CHECK: .seh_endproc 116 117 118 define void @try_body(i32* %r, i32* %n, i32* %d) { 119 entry: 120 %0 = load i32, i32* %n, align 4 121 %1 = load i32, i32* %d, align 4 122 %div = sdiv i32 %0, %1 123 store i32 %div, i32* %r, align 4 124 ret void 125 } 126 127 ; The prototype of these filter functions is: 128 ; int filter(EXCEPTION_POINTERS *eh_ptrs, void *rbp); 129 130 ; The definition of EXCEPTION_POINTERS is: 131 ; typedef struct _EXCEPTION_POINTERS { 132 ; EXCEPTION_RECORD *ExceptionRecord; 133 ; CONTEXT *ContextRecord; 134 ; } EXCEPTION_POINTERS; 135 136 ; The definition of EXCEPTION_RECORD is: 137 ; typedef struct _EXCEPTION_RECORD { 138 ; DWORD ExceptionCode; 139 ; ... 140 ; } EXCEPTION_RECORD; 141 142 ; The exception code can be retreived with two loads, one for the record 143 ; pointer and one for the code. The values of local variables can be 144 ; accessed via rbp, but that would require additional not yet implemented LLVM 145 ; support. 146 147 define i32 @safe_div_filt0(i8* %eh_ptrs, i8* %rbp) { 148 %eh_ptrs_c = bitcast i8* %eh_ptrs to i32** 149 %eh_rec = load i32*, i32** %eh_ptrs_c 150 %eh_code = load i32, i32* %eh_rec 151 ; EXCEPTION_ACCESS_VIOLATION = 0xC0000005 152 %cmp = icmp eq i32 %eh_code, 3221225477 153 %filt.res = zext i1 %cmp to i32 154 ret i32 %filt.res 155 } 156 157 define i32 @safe_div_filt1(i8* %eh_ptrs, i8* %rbp) { 158 %eh_ptrs_c = bitcast i8* %eh_ptrs to i32** 159 %eh_rec = load i32*, i32** %eh_ptrs_c 160 %eh_code = load i32, i32* %eh_rec 161 ; EXCEPTION_INT_DIVIDE_BY_ZERO = 0xC0000094 162 %cmp = icmp eq i32 %eh_code, 3221225620 163 %filt.res = zext i1 %cmp to i32 164 ret i32 %filt.res 165 } 166 167 @str_result = internal constant [21 x i8] c"safe_div result: %d\0A\00" 168 169 define i32 @main() { 170 %d.addr = alloca i32, align 4 171 %n.addr = alloca i32, align 4 172 173 store i32 10, i32* %n.addr, align 4 174 store i32 2, i32* %d.addr, align 4 175 %r1 = call i32 @safe_div(i32* %n.addr, i32* %d.addr) 176 call void (i8*, ...) @printf(i8* getelementptr ([21 x i8], [21 x i8]* @str_result, i32 0, i32 0), i32 %r1) 177 178 store i32 10, i32* %n.addr, align 4 179 store i32 0, i32* %d.addr, align 4 180 %r2 = call i32 @safe_div(i32* %n.addr, i32* %d.addr) 181 call void (i8*, ...) @printf(i8* getelementptr ([21 x i8], [21 x i8]* @str_result, i32 0, i32 0), i32 %r2) 182 183 %r3 = call i32 @safe_div(i32* %n.addr, i32* null) 184 call void (i8*, ...) @printf(i8* getelementptr ([21 x i8], [21 x i8]* @str_result, i32 0, i32 0), i32 %r3) 185 ret i32 0 186 } 187 188 define void @_Unwind_Resume() { 189 call void @abort() 190 unreachable 191 } 192 193 declare i32 @__C_specific_handler(...) 194 declare i32 @llvm.eh.typeid.for(i8*) readnone nounwind 195 declare void @puts(i8*) 196 declare void @printf(i8*, ...) 197 declare void @abort() 198