a4ea8ab78c7981a5de9beb07d2dc01b574eaa69a
[oota-llvm.git] / test / CodeGen / X86 / seh-catch-all-win32.ll
1 ; RUN: llc -mtriple=i686-windows-msvc < %s | FileCheck %s
2
3 ; 32-bit catch-all has to use a filter function because that's how it saves the
4 ; exception code.
5
6 @str = linkonce_odr unnamed_addr constant [27 x i8] c"GetExceptionCode(): 0x%lx\0A\00", align 1
7
8 declare i32 @_except_handler3(...)
9 declare void @crash()
10 declare i32 @printf(i8* nocapture readonly, ...) nounwind
11 declare i32 @llvm.eh.typeid.for(i8*)
12 declare i8* @llvm.frameaddress(i32)
13 declare i8* @llvm.localrecover(i8*, i8*, i32)
14 declare void @llvm.localescape(...)
15 declare i8* @llvm.x86.seh.recoverfp(i8*, i8*)
16
17 define i32 @main() personality i8* bitcast (i32 (...)* @_except_handler3 to i8*) {
18 entry:
19   %__exceptioncode = alloca i32, align 4
20   call void (...) @llvm.localescape(i32* %__exceptioncode)
21   invoke void @crash() #5
22           to label %__try.cont unwind label %lpad
23
24 lpad:                                             ; preds = %entry
25   %0 = landingpad { i8*, i32 }
26           catch i8* bitcast (i32 ()* @"filt$main" to i8*)
27   %1 = extractvalue { i8*, i32 } %0, 1
28   %2 = call i32 @llvm.eh.typeid.for(i8* bitcast (i32 ()* @"filt$main" to i8*)) #4
29   %matches = icmp eq i32 %1, %2
30   br i1 %matches, label %__except, label %eh.resume
31
32 __except:                                         ; preds = %lpad
33   %3 = load i32, i32* %__exceptioncode, align 4
34   %call = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @str, i32 0, i32 0), i32 %3) #4
35   br label %__try.cont
36
37 __try.cont:                                       ; preds = %entry, %__except
38   ret i32 0
39
40 eh.resume:                                        ; preds = %lpad
41   resume { i8*, i32 } %0
42 }
43
44 define internal i32 @"filt$main"() {
45 entry:
46   %ebp = tail call i8* @llvm.frameaddress(i32 1)
47   %parentfp = tail call i8* @llvm.x86.seh.recoverfp(i8* bitcast (i32 ()* @main to i8*), i8* %ebp)
48   %code.i8 = tail call i8* @llvm.localrecover(i8* bitcast (i32 ()* @main to i8*), i8* %parentfp, i32 0)
49   %__exceptioncode = bitcast i8* %code.i8 to i32*
50   %info.addr = getelementptr inbounds i8, i8* %ebp, i32 -20
51   %0 = bitcast i8* %info.addr to i32***
52   %1 = load i32**, i32*** %0, align 4
53   %2 = load i32*, i32** %1, align 4
54   %3 = load i32, i32* %2, align 4
55   store i32 %3, i32* %__exceptioncode, align 4
56   ret i32 1
57 }
58
59 ; Check that we can get the exception code from eax to the printf.
60
61 ; CHECK-LABEL: _main:
62 ; CHECK: pushl %ebp
63 ; CHECK: movl %esp, %ebp
64 ;       Ensure that we push *all* the CSRs, since they are clobbered by the
65 ;       __except block.
66 ; CHECK: pushl %ebx
67 ; CHECK: pushl %edi
68 ; CHECK: pushl %esi
69
70 ; CHECK: Lmain$frame_escape_0 = [[code_offs:[-0-9]+]]
71 ; CHECK: Lmain$frame_escape_1 = [[reg_offs:[-0-9]+]]
72 ; CHECK: movl %esp, [[reg_offs]](%ebp)
73 ; CHECK: movl $L__ehtable$main,
74 ;       EH state 0
75 ; CHECK: movl $0, -16(%ebp)
76 ; CHECK: calll _crash
77 ; CHECK: popl %esi
78 ; CHECK: popl %edi
79 ; CHECK: popl %ebx
80 ; CHECK: retl
81 ; CHECK: # Block address taken
82 ;       stackrestore
83 ; CHECK: movl -24(%ebp), %esp
84 ;       EH state -1
85 ; CHECK: movl [[code_offs]](%ebp), %[[code:[a-z]+]]
86 ; CHECK: movl $-1, -16(%ebp)
87 ; CHECK-DAG: movl %[[code]], 4(%esp)
88 ; CHECK-DAG: movl $_str, (%esp)
89 ; CHECK: calll _printf
90
91 ; CHECK: .section .xdata,"dr"
92 ; CHECK: Lmain$parent_frame_offset = Lmain$frame_escape_1
93 ; CHECK: .align 4
94 ; CHECK: L__ehtable$main
95 ; CHECK-NEXT: .long -1
96 ; CHECK-NEXT: .long _filt$main
97 ; CHECK-NEXT: .long Ltmp{{[0-9]+}}
98
99 ; CHECK-LABEL: _filt$main:
100 ; CHECK: pushl %ebp
101 ; CHECK: movl %esp, %ebp
102 ; CHECK: movl (%ebp), %[[oldebp:[a-z]+]]
103 ; CHECK: movl -20(%[[oldebp]]), %[[ehinfo:[a-z]+]]
104 ; CHECK: movl (%[[ehinfo]]), %[[ehrec:[a-z]+]]
105 ; CHECK: movl (%[[ehrec]]), %[[ehcode:[a-z]+]]
106 ; CHECK: movl %[[ehcode]], {{.*}}(%{{.*}})