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: define void @test1(
32 ; CHECK: store i32 %x, i32* [[Slot:%[^ ]+]]
33 ; CHECK: invoke void @f()
34 ; CHECK: to label %[[EntryCopy:[^ ]+]] unwind label %catch
37 ; CHECK-NEXT: to label %[[CatchCopy:[^ ]+]] unwind
38 ; CHECK: [[CatchCopy]]:
39 ; CHECK: [[LoadX2:%[^ ]+]] = load i32, i32* [[Slot]]
40 ; CHECK: call void @h(i32 [[LoadX2]]
41 ; CHECK: [[EntryCopy]]:
42 ; CHECK: [[LoadX1:%[^ ]+]] = load i32, i32* [[Slot]]
43 ; CHECK: call void @h(i32 [[LoadX1]]
46 define void @test2() personality i32 (...)* @__CxxFrameHandler3 {
49 to label %exit unwind label %cleanup
57 ; Need two copies of %exit's call to @f -- the subsequent ret is only
58 ; valid when coming from %entry, but on the path from %cleanup, this
59 ; might be a valid call to @f which might dynamically not return.
60 ; CHECK-LABEL: define void @test2(
62 ; CHECK: invoke void @f()
63 ; CHECK: to label %[[exit:[^ ]+]] unwind label %cleanup
65 ; CHECK: cleanuppad []
66 ; CHECK: call void @f()
67 ; CHECK-NEXT: unreachable
69 ; CHECK: call void @f()
70 ; CHECK-NEXT: ret void
73 define void @test3() personality i32 (...)* @__CxxFrameHandler3 {
76 to label %invoke.cont unwind label %catch
79 to label %exit unwind label %cleanup
81 catchpad [] to label %shared unwind label %endcatch
83 catchendpad unwind to caller
93 ; Need two copies of %shared's call to @f (similar to @test2 but
94 ; the two regions here are siblings, not parent-child).
95 ; CHECK-LABEL: define void @test3(
96 ; CHECK: invoke void @f()
97 ; CHECK: invoke void @f()
98 ; CHECK: to label %[[exit:[^ ]+]] unwind
101 ; CHECK-NEXT: to label %[[shared:[^ ]+]] unwind
103 ; CHECK: cleanuppad []
104 ; CHECK: call void @f()
105 ; CHECK-NEXT: unreachable
107 ; CHECK: call void @f()
108 ; CHECK-NEXT: unreachable
113 define void @test4() personality i32 (...)* @__CxxFrameHandler3 {
116 to label %shared unwind label %catch
119 to label %shared unwind label %endcatch
121 catchendpad unwind to caller
125 %zero.trip = icmp eq i32 %i, 0
126 br i1 %zero.trip, label %exit, label %loop
128 %i.loop = phi i32 [ %i, %shared ], [ %i.dec, %loop.tail ]
130 br i1 %b, label %left, label %right
138 %i.dec = sub i32 %i.loop, 1
139 %done = icmp eq i32 %i.dec, 0
140 br i1 %done, label %exit, label %loop
145 ; Make sure we can clone regions that have internal control
146 ; flow and SSA values. Here we need two copies of everything
147 ; from %shared to %exit.
148 ; CHECK-LABEL: define void @test4(
150 ; CHECK: to label %[[shared_E:[^ ]+]] unwind label %catch
152 ; CHECK: to label %[[shared_C:[^ ]+]] unwind label %endcatch
153 ; CHECK: [[shared_C]]:
154 ; CHECK: [[x_C:%[^ ]+]] = call i32 @g()
155 ; CHECK: [[i_C:%[^ ]+]] = call i32 @g()
156 ; CHECK: [[zt_C:%[^ ]+]] = icmp eq i32 [[i_C]], 0
157 ; CHECK: br i1 [[zt_C]], label %[[exit_C:[^ ]+]], label %[[loop_C:[^ ]+]]
158 ; CHECK: [[shared_E]]:
159 ; CHECK: [[x_E:%[^ ]+]] = call i32 @g()
160 ; CHECK: [[i_E:%[^ ]+]] = call i32 @g()
161 ; CHECK: [[zt_E:%[^ ]+]] = icmp eq i32 [[i_E]], 0
162 ; CHECK: br i1 [[zt_E]], label %[[exit_E:[^ ]+]], label %[[loop_E:[^ ]+]]
164 ; CHECK: [[iloop_C:%[^ ]+]] = phi i32 [ [[i_C]], %[[shared_C]] ], [ [[idec_C:%[^ ]+]], %[[looptail_C:[^ ]+]] ]
165 ; CHECK: [[b_C:%[^ ]+]] = call i1 @b()
166 ; CHECK: br i1 [[b_C]], label %[[left_C:[^ ]+]], label %[[right_C:[^ ]+]]
168 ; CHECK: [[iloop_E:%[^ ]+]] = phi i32 [ [[i_E]], %[[shared_E]] ], [ [[idec_E:%[^ ]+]], %[[looptail_E:[^ ]+]] ]
169 ; CHECK: [[b_E:%[^ ]+]] = call i1 @b()
170 ; CHECK: br i1 [[b_E]], label %[[left_E:[^ ]+]], label %[[right_E:[^ ]+]]
172 ; CHECK: [[y_C:%[^ ]+]] = call i32 @g()
173 ; CHECK: br label %[[looptail_C]]
175 ; CHECK: [[y_E:%[^ ]+]] = call i32 @g()
176 ; CHECK: br label %[[looptail_E]]
177 ; CHECK: [[right_C]]:
178 ; CHECK: call void @h(i32 [[x_C]])
179 ; CHECK: br label %[[looptail_C]]
180 ; CHECK: [[right_E]]:
181 ; CHECK: call void @h(i32 [[x_E]])
182 ; CHECK: br label %[[looptail_E]]
183 ; CHECK: [[looptail_C]]:
184 ; CHECK: [[idec_C]] = sub i32 [[iloop_C]], 1
185 ; CHECK: [[done_C:%[^ ]+]] = icmp eq i32 [[idec_C]], 0
186 ; CHECK: br i1 [[done_C]], label %[[exit_C]], label %[[loop_C]]
187 ; CHECK: [[looptail_E]]:
188 ; CHECK: [[idec_E]] = sub i32 [[iloop_E]], 1
189 ; CHECK: [[done_E:%[^ ]+]] = icmp eq i32 [[idec_E]], 0
190 ; CHECK: br i1 [[done_E]], label %[[exit_E]], label %[[loop_E]]
192 ; CHECK: call void @h(i32 [[x_C]])
195 ; CHECK: call void @h(i32 [[x_E]])
199 define void @test5() personality i32 (...)* @__CxxFrameHandler3 {
202 to label %exit unwind label %outer
207 to label %outer.ret unwind label %inner
210 to label %inner.catch unwind label %inner.endcatch
212 catchret %i to label %outer.post-inner
214 catchendpad unwind to caller
219 cleanupret %o unwind to caller
223 ; Simple nested case (catch-inside-cleanup). Nothing needs
224 ; to be cloned. The def and use of %x are both in %outer
225 ; and so don't need to be spilled.
226 ; CHECK-LABEL: define void @test5(
228 ; CHECK: %x = call i32 @g()
229 ; CHECK-NEXT: invoke void @f()
230 ; CHECK-NEXT: to label %outer.ret unwind label %inner
232 ; CHECK: to label %inner.catch unwind label %inner.endcatch
233 ; CHECK: inner.catch:
234 ; CHECK-NEXT: catchret %i to label %outer.post-inner
235 ; CHECK: outer.post-inner:
236 ; CHECK-NEXT: call void @h(i32 %x)
237 ; CHECK-NEXT: br label %outer.ret
240 define void @test6() personality i32 (...)* @__CxxFrameHandler3 {
243 to label %invoke.cont unwind label %left
246 to label %exit unwind label %right
252 to label %right.catch unwind label %right.end
256 catchendpad unwind to caller
260 to label %shared.cont unwind label %inner
266 cleanupret %i unwind label %right.end
270 ; %inner is a cleanup which appears both as a child of
271 ; %left and as a child of %right. Since statically we
272 ; need each funclet to have a single parent, we need to
273 ; clone the entire %inner funclet so we can have one
274 ; copy under each parent. The cleanupret in %inner
275 ; unwinds to the catchendpad for %right, so the copy
276 ; of %inner under %right should include it; the copy
277 ; of %inner under %left should instead have an
278 ; `unreachable` inserted there, but the copy under
279 ; %left still needs to be created because it's possible
280 ; the dynamic path enters %left, then enters %inner,
281 ; then calls @h, and that the call to @h doesn't return.
282 ; CHECK-LABEL: define void @test6(
286 define void @test7() personality i32 (...)* @__CxxFrameHandler3 {
289 to label %invoke.cont unwind label %left
292 to label %unreachable unwind label %right
295 invoke void @f() to label %unreachable unwind label %inner
298 to label %right.catch unwind label %right.end
300 invoke void @f() to label %unreachable unwind label %inner
302 catchendpad unwind to caller
307 cleanupret %i unwind label %right.end
311 ; Another case of a two-parent child (like @test6), this time
312 ; with the join at the entry itself instead of following a
314 ; CHECK-LABEL: define void @test7(
318 define void @test8() personality i32 (...)* @__CxxFrameHandler3 {
321 to label %invoke.cont unwind label %left
324 to label %unreachable unwind label %right
330 to label %right.catch unwind label %right.end
334 catchendpad unwind to caller
337 to label %unreachable unwind label %inner
341 to label %unreachable unwind label %inner.child
350 ; %inner is a two-parent child which itself has a child; need
351 ; to make two copies of both the %inner and %inner.child.
352 ; CHECK-LABEL: define void @test8(
356 define void @test9() personality i32 (...)* @__CxxFrameHandler3 {
359 to label %invoke.cont unwind label %left
362 to label %unreachable unwind label %right
367 to label %unreachable unwind label %right
372 to label %unreachable unwind label %left
376 ; This is an irreducible loop with two funclets that enter each other;
377 ; need to make two copies of each funclet (one a child of root, the
378 ; other a child of the opposite funclet), but also make sure not to
379 ; clone self-descendants (if we tried to do that we'd need to make an
380 ; infinite number of them). Presumably if optimizations ever generated
381 ; such a thing it would mean that one of the two cleanups was originally
382 ; the parent of the other, but that we'd somehow lost track in the CFG
383 ; of which was which along the way; generating each possibility lets
384 ; whichever case was correct execute correctly.
385 ; CHECK-LABEL: define void @test9(
388 define void @test10() personality i32 (...)* @__CxxFrameHandler3 {
391 to label %unreachable unwind label %inner
393 %cleanup = cleanuppad []
394 ; make sure we don't overlook this cleanupret and try to process
395 ; successor %outer as a child of inner.
396 cleanupret %cleanup unwind label %outer
398 %catch = catchpad [] to label %catch.body unwind label %endpad
400 catchret %catch to label %exit
402 catchendpad unwind to caller
408 ; CHECK-LABEL: define void @test10(
411 ; CHECK-NEXT: to label %unreachable unwind label %inner
413 ; CHECK-NEXT: %cleanup = cleanuppad
414 ; CHECK-NEXT: cleanupret %cleanup unwind label %outer
416 ; CHECK-NEXT: %catch = catchpad []
417 ; CHECK-NEXT: to label %catch.body unwind label %endpad
419 ; CHECK-NEXT: catchret %catch to label %exit
421 ; CHECK-NEXT: catchendpad unwind to caller
423 ; CHECK-NEXT: ret void
425 define void @test11() personality i32 (...)* @__CxxFrameHandler3 {
428 to label %exit unwind label %cleanup.outer
430 %outer = cleanuppad []
432 to label %outer.cont unwind label %cleanup.inner
436 %inner = cleanuppad []
440 to label %unreachable unwind label %merge.end
444 cleanupendpad %outer unwind to caller
448 ; merge.end will get cloned for outer and inner, but is implausible
449 ; from inner, so the invoke @f() in inner's copy of merge should be
450 ; rewritten to call @f()
451 ; CHECK-LABEL: define void @test11()
452 ; CHECK: %inner = cleanuppad []
453 ; CHECK-NEXT: call void @f()
454 ; CHECK-NEXT: unreachable
456 define void @test12() personality i32 (...)* @__CxxFrameHandler3 {
459 to label %cont unwind label %left, !dbg !8
462 to label %exit unwind label %right
470 ; This call will get cloned; make sure we can handle cloning
471 ; instructions with debug metadata attached.
472 call void @f(), !dbg !9
478 ; CHECK-LABEL: define void @test13()
480 define void @test13() personality i32 (...)* @__CxxFrameHandler3 {
489 define void @test14() personality i32 (...)* @__CxxFrameHandler3 {
492 to label %exit unwind label %catch1.pad
494 %catch1 = catchpad [i32 1]
495 to label %catch1.body unwind label %catch2.pad
497 invoke void @h(i32 1)
498 to label %catch1.body2 unwind label %catch.end
501 to label %catch1.ret unwind label %cleanup1.pad
503 %cleanup1 = cleanuppad []
505 cleanupret %cleanup1 unwind label %catch.end
507 catchret %catch1 to label %exit
509 %catch2 = catchpad [i32 2]
510 to label %catch2.body unwind label %catch.end
512 invoke void @h(i32 2)
513 to label %catch2.body2 unwind label %catch.end
516 to label %catch2.ret unwind label %cleanup2.pad
518 %cleanup2 = cleanuppad []
520 cleanupret %cleanup2 unwind label %catch.end
522 catchret %catch2 to label %exit
524 catchendpad unwind to caller
528 ; Make sure we don't clone the catchendpad even though the
529 ; cleanupendpads targeting it would naively imply that it
530 ; should get their respective parent colors (catch1 and catch2),
531 ; as well as its properly getting the root function color. The
532 ; references from the invokes ensure that if we did make clones
533 ; for each catch, they'd be reachable, as those invokes would get
535 ; CHECK-LABEL: define void @test14()
536 ; CHECK-NOT: catchendpad
537 ; CHECK: invoke void @h(i32 1)
538 ; CHECK-NEXT: unwind label %catch.end
539 ; CHECK-NOT: catchendpad
540 ; CHECK: invoke void @h(i32 2)
541 ; CHECK-NEXT: unwind label %catch.end
542 ; CHECK-NOT: catchendpad
544 ; CHECK-NEXT: catchendpad
545 ; CHECK-NOT: catchendpad
547 ;; Debug info (from test12)
549 ; Make sure the DISubprogram doesn't get cloned
550 ; CHECK-LABEL: !llvm.module.flags
551 ; CHECK-NOT: !DISubprogram
552 ; CHECK: !{{[0-9]+}} = distinct !DISubprogram(name: "test12"
553 ; CHECK-NOT: !DISubprogram
554 !llvm.module.flags = !{!0}
557 !0 = !{i32 2, !"Debug Info Version", i32 3}
558 !1 = distinct !DICompileUnit(language: DW_LANG_C_plus_plus, file: !2, producer: "compiler", isOptimized: false, runtimeVersion: 0, emissionKind: 1, enums: !3, subprograms: !4)
559 !2 = !DIFile(filename: "test.cpp", directory: ".")
562 !5 = distinct !DISubprogram(name: "test12", scope: !2, file: !2, type: !6, isLocal: false, isDefinition: true, scopeLine: 3, flags: DIFlagPrototyped, isOptimized: true, function: void ()* @test12, variables: !3)
563 !6 = !DISubroutineType(types: !7)
565 !8 = !DILocation(line: 1, scope: !5)
566 !9 = !DILocation(line: 2, scope: !5)