xref: /llvm-project/llvm/test/CodeGen/X86/win32-eh-states.ll (revision 06210d50c05d211c9ea99d1ddfd9e474027f124f)
1; RUN: llc -mtriple=i686-pc-windows-msvc   < %s | FileCheck %s --check-prefix=X86
2; RUN: llc -mtriple=x86_64-pc-windows-msvc < %s | FileCheck %s --check-prefix=X64
3
4; Based on this source:
5; extern "C" void may_throw(int);
6; void f() {
7;   try {
8;     may_throw(1);
9;     try {
10;       may_throw(2);
11;     } catch (int) {
12;       may_throw(3);
13;     }
14;   } catch (int) {
15;     may_throw(4);
16;   }
17; }
18
19%rtti.TypeDescriptor2 = type { ptr, ptr, [3 x i8] }
20%eh.CatchHandlerType = type { i32, ptr }
21
22declare void @may_throw(i32)
23declare i32 @__CxxFrameHandler3(...)
24declare i32 @llvm.eh.typeid.for(ptr)
25
26$"\01??_R0H@8" = comdat any
27
28@"\01??_7type_info@@6B@" = external constant ptr
29@"\01??_R0H@8" = linkonce_odr global %rtti.TypeDescriptor2 { ptr @"\01??_7type_info@@6B@", ptr null, [3 x i8] c".H\00" }, comdat
30@llvm.eh.handlertype.H.0 = private unnamed_addr constant %eh.CatchHandlerType { i32 0, ptr @"\01??_R0H@8" }, section "llvm.metadata"
31
32define void @f() #0 personality ptr @__CxxFrameHandler3 {
33entry:
34  invoke void @may_throw(i32 1)
35          to label %invoke.cont unwind label %lpad.1
36
37invoke.cont:                                      ; preds = %entry
38  invoke void @may_throw(i32 2)
39          to label %try.cont.9 unwind label %lpad
40
41try.cont.9:                                       ; preds = %invoke.cont.3, %invoke.cont, %catch.7
42  ret void
43
44lpad:                                             ; preds = %catch, %entry
45  %cs1 = catchswitch within none [label %catch] unwind label %lpad.1
46
47catch:                                            ; preds = %lpad.1
48  %p1 = catchpad within %cs1 [ptr @"\01??_R0H@8", i32 0, ptr null]
49  invoke void @may_throw(i32 3) [ "funclet"(token %p1) ]
50          to label %invoke.cont.3 unwind label %lpad.1
51
52invoke.cont.3:                                    ; preds = %catch
53  catchret from %p1 to label %try.cont.9
54
55lpad.1:                                           ; preds = %invoke.cont
56  %cs2 = catchswitch within none [label %catch.7] unwind to caller
57
58catch.7:
59  %p2 = catchpad within %cs2 [ptr @"\01??_R0H@8", i32 0, ptr null]
60  call void @may_throw(i32 4) [ "funclet"(token %p2) ]
61  catchret from %p2 to label %try.cont.9
62}
63
64; X86-LABEL: _f:
65; X86: movl $-1, [[state:[-0-9]+]](%ebp)
66; X86: movl $___ehhandler$f, {{.*}}
67;
68; X86: movl $0, [[state]](%ebp)
69; X86: pushl $1
70; X86: calll _may_throw
71;
72; X86: movl $1, [[state]](%ebp)
73; X86: pushl $2
74; X86: calll _may_throw
75;
76; X86: movl $2, [[state]](%ebp)
77; X86: pushl $3
78; X86: calll _may_throw
79;
80; X86: movl $3, [[state]](%ebp)
81; X86: pushl $4
82; X86: calll _may_throw
83
84
85; X64-LABEL: f:
86; X64-LABEL: $ip2state$f:
87; X64-NEXT:   .long .Lfunc_begin0@IMGREL
88; X64-NEXT:   .long -1
89; X64-NEXT:   .long .Ltmp{{.*}}@IMGREL+1
90; X64-NEXT:   .long 0
91; X64-NEXT:   .long .Ltmp{{.*}}@IMGREL+1
92; X64-NEXT:   .long 1
93; X64-NEXT:   .long .Ltmp{{.*}}@IMGREL+1
94; X64-NEXT:   .long -1
95; X64-NEXT:   .long "?catch${{.*}}@?0?f@4HA"@IMGREL
96; X64-NEXT:   .long 2
97; X64-NEXT:   .long "?catch${{.*}}@?0?f@4HA"@IMGREL
98; X64-NEXT:   .long 3
99
100; Based on this source:
101; extern "C" void may_throw(int);
102; struct S { ~S(); };
103; void g() {
104;   S x;
105;   try {
106;     may_throw(-1);
107;   } catch (...) {
108;     may_throw(0);
109;     {
110;       S y;
111;       may_throw(1);
112;     }
113;     may_throw(2);
114;   }
115; }
116
117%struct.S = type { i8 }
118declare void @"\01??1S@@QEAA@XZ"(ptr)
119
120define void @g() personality ptr @__CxxFrameHandler3 {
121entry:
122  %x = alloca %struct.S, align 1
123  %y = alloca %struct.S, align 1
124  invoke void @may_throw(i32 -1)
125          to label %unreachable unwind label %catch.dispatch
126
127catch.dispatch:                                   ; preds = %entry
128  %0 = catchswitch within none [label %catch] unwind label %ehcleanup5
129
130catch:                                            ; preds = %catch.dispatch
131  %1 = catchpad within %0 [ptr null, i32 64, ptr null]
132  invoke void @may_throw(i32 0) [ "funclet"(token %1) ]
133          to label %invoke.cont unwind label %ehcleanup5
134
135invoke.cont:                                      ; preds = %catch
136  invoke void @may_throw(i32 1) [ "funclet"(token %1) ]
137          to label %invoke.cont2 unwind label %ehcleanup
138
139invoke.cont2:                                     ; preds = %invoke.cont
140  invoke void @"\01??1S@@QEAA@XZ"(ptr nonnull %y) [ "funclet"(token %1) ]
141          to label %invoke.cont3 unwind label %ehcleanup5
142
143invoke.cont3:                                     ; preds = %invoke.cont2
144  invoke void @may_throw(i32 2) [ "funclet"(token %1) ]
145          to label %invoke.cont4 unwind label %ehcleanup5
146
147invoke.cont4:                                     ; preds = %invoke.cont3
148  catchret from %1 to label %try.cont
149
150try.cont:                                         ; preds = %invoke.cont4
151  call void @"\01??1S@@QEAA@XZ"(ptr nonnull %x)
152  ret void
153
154ehcleanup:                                        ; preds = %invoke.cont
155  %2 = cleanuppad within %1 []
156  call void @"\01??1S@@QEAA@XZ"(ptr nonnull %y) [ "funclet"(token %2) ]
157  cleanupret from %2 unwind label %ehcleanup5
158
159ehcleanup5:                                       ; preds = %invoke.cont2, %invoke.cont3, %ehcleanup, %catch, %catch.dispatch
160  %3 = cleanuppad within none []
161  call void @"\01??1S@@QEAA@XZ"(ptr nonnull %x) [ "funclet"(token %3) ]
162  cleanupret from %3 unwind to caller
163
164unreachable:                                      ; preds = %entry
165  unreachable
166}
167
168; X86-LABEL: _g:
169; X86: movl $-1, [[state:[-0-9]+]](%ebp)
170; X86: movl $___ehhandler$g, {{.*}}
171;
172; X86: movl $1, [[state]](%ebp)
173; X86: pushl $-1
174; X86: calll _may_throw
175;
176; X86: movl $2, [[state]](%ebp)
177; X86: pushl $0
178; X86: calll _may_throw
179;
180; X86: movl $3, [[state]](%ebp)
181; X86: pushl $1
182; X86: calll _may_throw
183;
184; X86: movl $2, [[state]](%ebp)
185; X86: pushl $2
186; X86: calll _may_throw
187
188; X64-LABEL: g:
189; X64-LABEL: $ip2state$g:
190; X64-NEXT:   .long .Lfunc_begin1@IMGREL
191; X64-NEXT:   .long -1
192; X64-NEXT:   .long .Ltmp{{.*}}@IMGREL+1
193; X64-NEXT:   .long 1
194; X64-NEXT:   .long .Ltmp{{.*}}@IMGREL+1
195; X64-NEXT:   .long -1
196; X64-NEXT:   .long "?catch${{.*}}@?0?g@4HA"@IMGREL
197; X64-NEXT:   .long 2
198; X64-NEXT:   .long .Ltmp{{.*}}@IMGREL+1
199; X64-NEXT:   .long 3
200; X64-NEXT:   .long .Ltmp{{.*}}@IMGREL+1
201; X64-NEXT:   .long 2
202
203
204; X86: .safeseh ___ehhandler$f
205; X86: .safeseh ___ehhandler$g
206