1 ; RUN: opt -mtriple=x86_x64-pc-windows-msvc -S -winehprepare < %s | FileCheck %s
3 declare i32 @__CxxFrameHandler3(...)
11 define void @test1() personality i32 (...)* @__CxxFrameHandler3 {
13 ; %x def colors: {entry} subset of use colors; must spill
16 to label %noreturn unwind label %catch
19 to label %noreturn unwind label %endcatch
21 ; %x use colors: {entry, cleanup}
25 catchendpad unwind to caller
27 ; Need two copies of the call to @h, one under entry and one under catch.
28 ; Currently we generate a load for each, though we shouldn't need one
29 ; for the use in entry's copy.
30 ; CHECK-LABEL: @test1(
32 ; CHECK: store i32 %x, i32* [[Slot:%[^ ]+]]
33 ; CHECK: invoke void @f()
34 ; CHECK: to label %[[EntryCopy:[^ ]+]] unwind label %catch
36 ; CHECK: catchpad [] to label %[[CatchCopy:[^ ]+]] unwind
37 ; CHECK: [[CatchCopy]]:
38 ; CHECK: [[LoadX2:%[^ ]+]] = load i32, i32* [[Slot]]
39 ; CHECK: call void @h(i32 [[LoadX2]]
40 ; CHECK: [[EntryCopy]]:
41 ; CHECK: [[LoadX1:%[^ ]+]] = load i32, i32* [[Slot]]
42 ; CHECK: call void @h(i32 [[LoadX1]]
45 define void @test2() personality i32 (...)* @__CxxFrameHandler3 {
48 to label %exit unwind label %cleanup
56 ; Need two copies of %exit's call to @f -- the subsequent ret is only
57 ; valid when coming from %entry, but on the path from %cleanup, this
58 ; might be a valid call to @f which might dynamically not return.
59 ; CHECK-LABEL: @test2(
61 ; CHECK: invoke void @f()
62 ; CHECK: to label %[[exit:[^ ]+]] unwind label %cleanup
64 ; CHECK: cleanuppad []
65 ; CHECK: call void @f()
66 ; CHECK-NEXT: unreachable
68 ; CHECK: call void @f()
69 ; CHECK-NEXT: ret void
72 define void @test3() personality i32 (...)* @__CxxFrameHandler3 {
75 to label %invoke.cont unwind label %catch
78 to label %exit unwind label %cleanup
80 catchpad [] to label %shared unwind label %endcatch
82 catchendpad unwind to caller
92 ; Need two copies of %shared's call to @f (similar to @test2 but
93 ; the two regions here are siblings, not parent-child).
94 ; CHECK-LABEL: @test3(
95 ; CHECK: invoke void @f()
96 ; CHECK: invoke void @f()
97 ; CHECK: to label %[[exit:[^ ]+]] unwind
99 ; CHECK: catchpad [] to label %[[shared:[^ ]+]] unwind
101 ; CHECK: cleanuppad []
102 ; CHECK: call void @f()
103 ; CHECK-NEXT: unreachable
105 ; CHECK: call void @f()
106 ; CHECK-NEXT: unreachable
111 define void @test4() personality i32 (...)* @__CxxFrameHandler3 {
114 to label %shared unwind label %catch
117 to label %shared unwind label %endcatch
119 catchendpad unwind to caller
123 %zero.trip = icmp eq i32 %i, 0
124 br i1 %zero.trip, label %exit, label %loop
126 %i.loop = phi i32 [ %i, %shared ], [ %i.dec, %loop.tail ]
128 br i1 %b, label %left, label %right
136 %i.dec = sub i32 %i.loop, 1
137 %done = icmp eq i32 %i.dec, 0
138 br i1 %done, label %exit, label %loop
143 ; Make sure we can clone regions that have internal control
144 ; flow and SSA values. Here we need two copies of everything
145 ; from %shared to %exit.
146 ; CHECK-LABEL: @test4(
148 ; CHECK: to label %[[shared_E:[^ ]+]] unwind label %catch
150 ; CHECK: to label %[[shared_C:[^ ]+]] unwind label %endcatch
151 ; CHECK: [[shared_C]]:
152 ; CHECK: [[x_C:%[^ ]+]] = call i32 @g()
153 ; CHECK: [[i_C:%[^ ]+]] = call i32 @g()
154 ; CHECK: [[zt_C:%[^ ]+]] = icmp eq i32 [[i_C]], 0
155 ; CHECK: br i1 [[zt_C]], label %[[exit_C:[^ ]+]], label %[[loop_C:[^ ]+]]
156 ; CHECK: [[shared_E]]:
157 ; CHECK: [[x_E:%[^ ]+]] = call i32 @g()
158 ; CHECK: [[i_E:%[^ ]+]] = call i32 @g()
159 ; CHECK: [[zt_E:%[^ ]+]] = icmp eq i32 [[i_E]], 0
160 ; CHECK: br i1 [[zt_E]], label %[[exit_E:[^ ]+]], label %[[loop_E:[^ ]+]]
162 ; CHECK: [[iloop_C:%[^ ]+]] = phi i32 [ [[i_C]], %[[shared_C]] ], [ [[idec_C:%[^ ]+]], %[[looptail_C:[^ ]+]] ]
163 ; CHECK: [[b_C:%[^ ]+]] = call i1 @b()
164 ; CHECK: br i1 [[b_C]], label %[[left_C:[^ ]+]], label %[[right_C:[^ ]+]]
166 ; CHECK: [[iloop_E:%[^ ]+]] = phi i32 [ [[i_E]], %[[shared_E]] ], [ [[idec_E:%[^ ]+]], %[[looptail_E:[^ ]+]] ]
167 ; CHECK: [[b_E:%[^ ]+]] = call i1 @b()
168 ; CHECK: br i1 [[b_E]], label %[[left_E:[^ ]+]], label %[[right_E:[^ ]+]]
170 ; CHECK: [[y_C:%[^ ]+]] = call i32 @g()
171 ; CHECK: br label %[[looptail_C]]
173 ; CHECK: [[y_E:%[^ ]+]] = call i32 @g()
174 ; CHECK: br label %[[looptail_E]]
175 ; CHECK: [[right_C]]:
176 ; CHECK: call void @h(i32 [[x_C]])
177 ; CHECK: br label %[[looptail_C]]
178 ; CHECK: [[right_E]]:
179 ; CHECK: call void @h(i32 [[x_E]])
180 ; CHECK: br label %[[looptail_E]]
181 ; CHECK: [[looptail_C]]:
182 ; CHECK: [[idec_C]] = sub i32 [[iloop_C]], 1
183 ; CHECK: [[done_C:%[^ ]+]] = icmp eq i32 [[idec_C]], 0
184 ; CHECK: br i1 [[done_C]], label %[[exit_C]], label %[[loop_C]]
185 ; CHECK: [[looptail_E]]:
186 ; CHECK: [[idec_E]] = sub i32 [[iloop_E]], 1
187 ; CHECK: [[done_E:%[^ ]+]] = icmp eq i32 [[idec_E]], 0
188 ; CHECK: br i1 [[done_E]], label %[[exit_E]], label %[[loop_E]]
190 ; CHECK: call void @h(i32 [[x_C]])
193 ; CHECK: call void @h(i32 [[x_E]])
197 define void @test5() personality i32 (...)* @__CxxFrameHandler3 {
200 to label %exit unwind label %outer
205 to label %outer.ret unwind label %inner
208 to label %inner.catch unwind label %inner.endcatch
210 catchret %i to label %outer.post-inner
212 catchendpad unwind to caller
217 cleanupret %o unwind to caller
221 ; Simple nested case (catch-inside-cleanup). Nothing needs
222 ; to be cloned. The def and use of %x are both in %outer
223 ; and so don't need to be spilled.
224 ; CHECK-LABEL: @test5(
226 ; CHECK: %x = call i32 @g()
227 ; CHECK-NEXT: invoke void @f()
228 ; CHECK-NEXT: to label %outer.ret unwind label %inner
230 ; CHECK: to label %inner.catch unwind label %inner.endcatch
231 ; CHECK: inner.catch:
232 ; CHECK-NEXT: catchret %i to label %outer.post-inner
233 ; CHECK: outer.post-inner:
234 ; CHECK-NEXT: call void @h(i32 %x)
235 ; CHECK-NEXT: br label %outer.ret
238 define void @test6() personality i32 (...)* @__CxxFrameHandler3 {
241 to label %invoke.cont unwind label %left
244 to label %exit unwind label %right
250 to label %right.catch unwind label %right.end
254 catchendpad unwind to caller
258 to label %shared.cont unwind label %inner
264 cleanupret %i unwind label %right.end
268 ; %inner is a cleanup which appears both as a child of
269 ; %left and as a child of %right. Since statically we
270 ; need each funclet to have a single parent, we need to
271 ; clone the entire %inner funclet so we can have one
272 ; copy under each parent. The cleanupret in %inner
273 ; unwinds to the catchendpad for %right, so the copy
274 ; of %inner under %right should include it; the copy
275 ; of %inner under %left should instead have an
276 ; `unreachable` inserted there, but the copy under
277 ; %left still needs to be created because it's possible
278 ; the dynamic path enters %left, then enters %inner,
279 ; then calls @h, and that the call to @h doesn't return.
280 ; CHECK-LABEL: @test6(
284 define void @test7() personality i32 (...)* @__CxxFrameHandler3 {
287 to label %invoke.cont unwind label %left
290 to label %unreachable unwind label %right
293 invoke void @f() to label %unreachable unwind label %inner
296 to label %right.catch unwind label %right.end
298 invoke void @f() to label %unreachable unwind label %inner
300 catchendpad unwind to caller
305 cleanupret %i unwind label %right.end
309 ; Another case of a two-parent child (like @test6), this time
310 ; with the join at the entry itself instead of following a
312 ; CHECK-LABEL: @test7(
316 define void @test8() personality i32 (...)* @__CxxFrameHandler3 {
319 to label %invoke.cont unwind label %left
322 to label %unreachable unwind label %right
328 to label %right.catch unwind label %right.end
332 catchendpad unwind to caller
335 to label %unreachable unwind label %inner
339 to label %unreachable unwind label %inner.child
348 ; %inner is a two-parent child which itself has a child; need
349 ; to make two copies of both the %inner and %inner.child.
350 ; CHECK-LABEL: @test8(
354 define void @test9() personality i32 (...)* @__CxxFrameHandler3 {
357 to label %invoke.cont unwind label %left
360 to label %unreachable unwind label %right
365 to label %unreachable unwind label %right
370 to label %unreachable unwind label %left
374 ; This is an irreducible loop with two funclets that enter each other;
375 ; need to make two copies of each funclet (one a child of root, the
376 ; other a child of the opposite funclet), but also make sure not to
377 ; clone self-descendants (if we tried to do that we'd need to make an
378 ; infinite number of them). Presumably if optimizations ever generated
379 ; such a thing it would mean that one of the two cleanups was originally
380 ; the parent of the other, but that we'd somehow lost track in the CFG
381 ; of which was which along the way; generating each possibility lets
382 ; whichever case was correct execute correctly.
383 ; CHECK-LABEL: @test9(