Re-land r235154-r235156 under the existing -sehprepare flag
[oota-llvm.git] / test / CodeGen / X86 / seh-safe-div.ll
1 ; RUN: llc -sehprepare -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: [[cont_bb:\.LBB0_[0-9]+]]:
75 ; CHECK: movl [[rloc]], %eax
76 ; CHECK: retq
77
78 ; Landing pad code
79
80 ; CHECK: [[handler0:\.Ltmp[0-9]+]]: # Block address taken
81 ; CHECK: # %handler0
82 ; CHECK: callq puts
83 ; CHECK: movl $-1, [[rloc]]
84 ; CHECK: jmp [[cont_bb]]
85
86 ; CHECK: [[handler1:\.Ltmp[0-9]+]]: # Block address taken
87 ; CHECK: # %handler1
88 ; CHECK: callq puts
89 ; CHECK: movl $-2, [[rloc]]
90 ; CHECK: jmp [[cont_bb]]
91
92 ; CHECK: .seh_handlerdata
93 ; CHECK-NEXT: .long 2
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
102 ; CHECK: .text
103 ; CHECK: .seh_endproc
104
105
106 define void @try_body(i32* %r, i32* %n, i32* %d) {
107 entry:
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
112   ret void
113 }
114
115 ; The prototype of these filter functions is:
116 ; int filter(EXCEPTION_POINTERS *eh_ptrs, void *rbp);
117
118 ; The definition of EXCEPTION_POINTERS is:
119 ;   typedef struct _EXCEPTION_POINTERS {
120 ;     EXCEPTION_RECORD *ExceptionRecord;
121 ;     CONTEXT          *ContextRecord;
122 ;   } EXCEPTION_POINTERS;
123
124 ; The definition of EXCEPTION_RECORD is:
125 ;   typedef struct _EXCEPTION_RECORD {
126 ;     DWORD ExceptionCode;
127 ;     ...
128 ;   } EXCEPTION_RECORD;
129
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
133 ; support.
134
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
142   ret i32 %filt.res
143 }
144
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
152   ret i32 %filt.res
153 }
154
155 @str_result = internal constant [21 x i8] c"safe_div result: %d\0A\00"
156
157 define i32 @main() {
158   %d.addr = alloca i32, align 4
159   %n.addr = alloca i32, align 4
160
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)
165
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)
170
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)
173   ret i32 0
174 }
175
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()