0ad385375f72d7498b0a7fbc60887814777d6241
[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   %p = catchpad [i8* bitcast (i32 ()* @"filt$main" to i8*)]
26           to label %__except unwind label %endpad
27
28 __except:                                         ; preds = %lpad
29   %code = load i32, i32* %__exceptioncode, align 4
30   %call = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @str, i32 0, i32 0), i32 %code) #4
31   catchret %p to label %__try.cont
32
33 endpad:                                        ; preds = %lpad
34   catchendpad unwind to caller
35
36 __try.cont:                                       ; preds = %entry, %__except
37   ret i32 0
38 }
39
40 define internal i32 @"filt$main"() {
41 entry:
42   %ebp = tail call i8* @llvm.frameaddress(i32 1)
43   %parentfp = tail call i8* @llvm.x86.seh.recoverfp(i8* bitcast (i32 ()* @main to i8*), i8* %ebp)
44   %code.i8 = tail call i8* @llvm.localrecover(i8* bitcast (i32 ()* @main to i8*), i8* %parentfp, i32 0)
45   %__exceptioncode = bitcast i8* %code.i8 to i32*
46   %info.addr = getelementptr inbounds i8, i8* %ebp, i32 -20
47   %0 = bitcast i8* %info.addr to i32***
48   %1 = load i32**, i32*** %0, align 4
49   %2 = load i32*, i32** %1, align 4
50   %3 = load i32, i32* %2, align 4
51   store i32 %3, i32* %__exceptioncode, align 4
52   ret i32 1
53 }
54
55 ; Check that we can get the exception code from eax to the printf.
56
57 ; CHECK-LABEL: _main:
58 ; CHECK: pushl %ebp
59 ; CHECK: movl %esp, %ebp
60 ;       Ensure that we push *all* the CSRs, since they are clobbered by the
61 ;       __except block.
62 ; CHECK: pushl %ebx
63 ; CHECK: pushl %edi
64 ; CHECK: pushl %esi
65
66 ; CHECK: Lmain$frame_escape_0 = [[code_offs:[-0-9]+]]
67 ; CHECK: Lmain$frame_escape_1 = [[reg_offs:[-0-9]+]]
68 ; CHECK: movl %esp, [[reg_offs]](%ebp)
69 ; CHECK: movl $L__ehtable$main,
70 ;       EH state 0
71 ; CHECK: movl $0, -16(%ebp)
72 ; CHECK: calll _crash
73 ; CHECK: popl %esi
74 ; CHECK: popl %edi
75 ; CHECK: popl %ebx
76 ; CHECK: retl
77 ; CHECK: LBB0_[[lpbb:[0-9]+]]: # %lpad{{$}}
78 ;       stackrestore
79 ; CHECK: movl -24(%ebp), %esp
80 ;       EH state -1
81 ; CHECK: movl [[code_offs]](%ebp), %[[code:[a-z]+]]
82 ; CHECK-DAG: movl %[[code]], 4(%esp)
83 ; CHECK-DAG: movl $_str, (%esp)
84 ; CHECK: calll _printf
85
86 ; CHECK: .section .xdata,"dr"
87 ; CHECK: Lmain$parent_frame_offset = Lmain$frame_escape_1
88 ; CHECK: .align 4
89 ; CHECK: L__ehtable$main
90 ; CHECK-NEXT: .long -1
91 ; CHECK-NEXT: .long _filt$main
92 ; CHECK-NEXT: .long LBB0_[[lpbb]]
93
94 ; CHECK-LABEL: _filt$main:
95 ; CHECK: pushl %ebp
96 ; CHECK: movl %esp, %ebp
97 ; CHECK: movl (%ebp), %[[oldebp:[a-z]+]]
98 ; CHECK: movl -20(%[[oldebp]]), %[[ehinfo:[a-z]+]]
99 ; CHECK: movl (%[[ehinfo]]), %[[ehrec:[a-z]+]]
100 ; CHECK: movl (%[[ehrec]]), %[[ehcode:[a-z]+]]
101 ; CHECK: movl %[[ehcode]], {{.*}}(%{{.*}})