1 ; RUN: opt -inline -S %s | FileCheck %s
6 ;;; Test with a call in a funclet that needs to remain a call
7 ;;; when inlined because the funclet doesn't unwind to caller.
8 ;;; CHECK-LABEL: define void @test1(
9 define void @test1() personality void ()* @g {
12 invoke void @test1_inlinee()
13 to label %exit unwind label %cleanup
15 %pad = cleanuppad within none []
16 call void @g() [ "funclet"(token %pad) ]
17 cleanupret from %pad unwind to caller
22 define void @test1_inlinee() alwaysinline personality void ()* @g {
25 to label %exit unwind label %cleanup.inner
26 ; CHECK-NEXT: invoke void @g()
27 ; CHECK-NEXT: unwind label %[[cleanup_inner:.+]]
30 %pad.inner = cleanuppad within none []
31 call void @g() [ "funclet"(token %pad.inner) ]
32 cleanupret from %pad.inner unwind label %cleanup.outer
33 ; CHECK: [[cleanup_inner]]:
34 ; The call here needs to remain a call becuase pad.inner has a cleanupret
35 ; that stays within the inlinee.
36 ; CHECK-NEXT: %[[pad_inner:[^ ]+]] = cleanuppad within none
37 ; CHECK-NEXT: call void @g() [ "funclet"(token %[[pad_inner]]) ]
38 ; CHECK-NEXT: cleanupret from %[[pad_inner]] unwind label %[[cleanup_outer:.+]]
41 %pad.outer = cleanuppad within none []
42 call void @g() [ "funclet"(token %pad.outer) ]
43 cleanupret from %pad.outer unwind to caller
44 ; CHECK: [[cleanup_outer]]:
45 ; The call and cleanupret here need to be redirected to caller cleanup
46 ; CHECK-NEXT: %[[pad_outer:[^ ]+]] = cleanuppad within none
47 ; CHECK-NEXT: invoke void @g() [ "funclet"(token %[[pad_outer]]) ]
48 ; CHECK-NEXT: unwind label %cleanup
49 ; CHECK: cleanupret from %[[pad_outer]] unwind label %cleanup{{$}}
57 ;;; Test with an "unwind to caller" catchswitch in a parent funclet
58 ;;; that needs to remain "unwind to caller" because the parent
59 ;;; doesn't unwind to caller.
60 ;;; CHECK-LABEL: define void @test2(
61 define void @test2() personality void ()* @g {
64 invoke void @test2_inlinee()
65 to label %exit unwind label %cleanup
67 %pad = cleanuppad within none []
68 call void @g() [ "funclet"(token %pad) ]
69 cleanupret from %pad unwind to caller
74 define void @test2_inlinee() alwaysinline personality void ()* @g {
77 to label %exit unwind label %cleanup1
78 ; CHECK-NEXT: invoke void @g()
79 ; CHECK-NEXT: unwind label %[[cleanup1:.+]]
82 %outer = cleanuppad within none []
83 invoke void @g() [ "funclet"(token %outer) ]
84 to label %ret1 unwind label %catchswitch
85 ; CHECK: [[cleanup1]]:
86 ; CHECK-NEXT: %[[outer:[^ ]+]] = cleanuppad within none
87 ; CHECK-NEXT: invoke void @g() [ "funclet"(token %[[outer]]) ]
88 ; CHECK-NEXT: unwind label %[[catchswitch:.+]]
91 %cs = catchswitch within %outer [label %catch] unwind to caller
92 ; CHECK: [[catchswitch]]:
93 ; The catchswitch here needs to remain "unwind to caller" since %outer
94 ; has a cleanupret that remains within the inlinee.
95 ; CHECK-NEXT: %[[cs:[^ ]+]] = catchswitch within %[[outer]] [label %[[catch:.+]]] unwind to caller
98 %inner = catchpad within %cs []
99 call void @g() [ "funclet"(token %inner) ]
100 catchret from %inner to label %ret1
102 ; The call here needs to remain a call since it too is within %outer
103 ; CHECK: %[[inner:[^ ]+]] = catchpad within %[[cs]]
104 ; CHECK-NEXT: call void @g() [ "funclet"(token %[[inner]]) ]
107 cleanupret from %outer unwind label %cleanup2
108 ; CHECK: cleanupret from %[[outer]] unwind label %[[cleanup2:.+]]
111 %later = cleanuppad within none []
112 cleanupret from %later unwind to caller
113 ; CHECK: [[cleanup2]]:
114 ; The cleanupret here needs to get redirected to the caller cleanup
115 ; CHECK-NEXT: %[[later:[^ ]+]] = cleanuppad within none
116 ; CHECK-NEXT: cleanupret from %[[later]] unwind label %cleanup{{$}}
123 ;;; Test with a call in a cleanup that has no definitive unwind
124 ;;; destination, that must be rewritten to an invoke.
125 ;;; CHECK-LABEL: define void @test3(
126 define void @test3() personality void ()* @g {
129 invoke void @test3_inlinee()
130 to label %exit unwind label %cleanup
132 %pad = cleanuppad within none []
133 call void @g() [ "funclet"(token %pad) ]
134 cleanupret from %pad unwind to caller
139 define void @test3_inlinee() alwaysinline personality void ()* @g {
142 to label %exit unwind label %cleanup
143 ; CHECK-NEXT: invoke void @g()
144 ; CHECK-NEXT: unwind label %[[cleanup:.+]]
147 %pad = cleanuppad within none []
148 call void @g() [ "funclet"(token %pad) ]
150 ; CHECK: [[cleanup]]:
151 ; The call must be rewritten to an invoke targeting the caller cleanup
152 ; because it may well unwind to there.
153 ; CHECK-NEXT: %[[pad:[^ ]+]] = cleanuppad within none
154 ; CHECK-NEXT: invoke void @g() [ "funclet"(token %[[pad]]) ]
155 ; CHECK-NEXT: unwind label %cleanup{{$}}
162 ;;; Test with a catchswitch in a cleanup that has no definitive
163 ;;; unwind destination, that must be rewritten to unwind to the
164 ;;; inlined invoke's unwind dest
165 ;;; CHECK-LABEL: define void @test4(
166 define void @test4() personality void ()* @g {
169 invoke void @test4_inlinee()
170 to label %exit unwind label %cleanup
172 %pad = cleanuppad within none []
173 call void @g() [ "funclet"(token %pad) ]
174 cleanupret from %pad unwind to caller
179 define void @test4_inlinee() alwaysinline personality void ()* @g {
182 to label %exit unwind label %cleanup
183 ; CHECK-NEXT: invoke void @g()
184 ; CHECK-NEXT: unwind label %[[cleanup:.+]]
187 %clean = cleanuppad within none []
188 invoke void @g() [ "funclet"(token %clean) ]
189 to label %unreachable unwind label %dispatch
190 ; CHECK: [[cleanup]]:
191 ; CHECK-NEXT: %[[clean:[^ ]+]] = cleanuppad within none
192 ; CHECK-NEXT: invoke void @g() [ "funclet"(token %[[clean]]) ]
193 ; CHECK-NEXT: unwind label %[[dispatch:.+]]
196 %cs = catchswitch within %clean [label %catch] unwind to caller
197 ; CHECK: [[dispatch]]:
198 ; The catchswitch must be rewritten to unwind to %cleanup in the caller
199 ; because it may well unwind to there.
200 ; CHECK-NEXT: %[[cs:[^ ]+]] = catchswitch within %[[clean]] [label %[[catch:.+]]] unwind label %cleanup{{$}}
203 catchpad within %cs []
204 br label %unreachable
212 ;;; Test with multiple levels of nesting, and unwind dests
213 ;;; that need to be inferred from ancestors, descendants,
215 ;;; CHECK-LABEL: define void @test5(
216 define void @test5() personality void ()* @g {
219 invoke void @test5_inlinee()
220 to label %exit unwind label %cleanup
222 %pad = cleanuppad within none []
223 call void @g() [ "funclet"(token %pad) ]
224 cleanupret from %pad unwind to caller
229 define void @test5_inlinee() alwaysinline personality void ()* @g {
232 to label %cont unwind label %noinfo.root
233 ; CHECK-NEXT: invoke void @g()
234 ; CHECK-NEXT: to label %[[cont:[^ ]+]] unwind label %[[noinfo_root:.+]]
237 %noinfo.root.pad = cleanuppad within none []
238 call void @g() [ "funclet"(token %noinfo.root.pad) ]
239 invoke void @g() [ "funclet"(token %noinfo.root.pad) ]
240 to label %noinfo.root.cont unwind label %noinfo.left
241 ; CHECK: [[noinfo_root]]:
242 ; Nothing under "noinfo.root" has a definitive unwind destination, so
243 ; we must assume all of it may actually unwind, and redirect unwinds
244 ; to the cleanup in the caller.
245 ; CHECK-NEXT: %[[noinfo_root_pad:[^ ]+]] = cleanuppad within none []
246 ; CHECK-NEXT: invoke void @g() [ "funclet"(token %[[noinfo_root_pad]]) ]
247 ; CHECK-NEXT: to label %[[next:[^ ]+]] unwind label %cleanup{{$}}
249 ; CHECK-NEXT: invoke void @g() [ "funclet"(token %[[noinfo_root_pad]]) ]
250 ; CHECK-NEXT: to label %[[noinfo_root_cont:[^ ]+]] unwind label %[[noinfo_left:.+]]
253 %noinfo.left.pad = cleanuppad within %noinfo.root.pad []
254 invoke void @g() [ "funclet"(token %noinfo.left.pad) ]
255 to label %unreachable unwind label %noinfo.left.child
256 ; CHECK: [[noinfo_left]]:
257 ; CHECK-NEXT: %[[noinfo_left_pad:[^ ]+]] = cleanuppad within %[[noinfo_root_pad]]
258 ; CHECK-NEXT: invoke void @g() [ "funclet"(token %[[noinfo_left_pad]]) ]
259 ; CHECK-NEXT: unwind label %[[noinfo_left_child:.+]]
262 %noinfo.left.child.cs = catchswitch within %noinfo.left.pad [label %noinfo.left.child.catch] unwind to caller
263 ; CHECK: [[noinfo_left_child]]:
264 ; CHECK-NEXT: %[[noinfo_left_child_cs:[^ ]+]] = catchswitch within %[[noinfo_left_pad]] [label %[[noinfo_left_child_catch:[^ ]+]]] unwind label %cleanup{{$}}
266 noinfo.left.child.catch:
267 %noinfo.left.child.pad = catchpad within %noinfo.left.child.cs []
268 call void @g() [ "funclet"(token %noinfo.left.child.pad) ]
269 br label %unreachable
270 ; CHECK: [[noinfo_left_child_catch]]:
271 ; CHECK-NEXT: %[[noinfo_left_child_pad:[^ ]+]] = catchpad within %[[noinfo_left_child_cs]] []
272 ; CHECK-NEXT: invoke void @g() [ "funclet"(token %[[noinfo_left_child_pad]]) ]
273 ; CHECK-NEXT: unwind label %cleanup{{$}}
276 invoke void @g() [ "funclet"(token %noinfo.root.pad) ]
277 to label %unreachable unwind label %noinfo.right
278 ; CHECK: [[noinfo_root_cont]]:
279 ; CHECK-NEXT: invoke void @g() [ "funclet"(token %[[noinfo_root_pad]]) ]
280 ; CHECK-NEXT: unwind label %[[noinfo_right:.+]]
283 %noinfo.right.cs = catchswitch within %noinfo.root.pad [label %noinfo.right.catch] unwind to caller
284 ; CHECK: [[noinfo_right]]:
285 ; CHECK-NEXT: %[[noinfo_right_cs:[^ ]+]] = catchswitch within %[[noinfo_root_pad]] [label %[[noinfo_right_catch:[^ ]+]]] unwind label %cleanup{{$}}
288 %noinfo.right.pad = catchpad within %noinfo.right.cs []
289 invoke void @g() [ "funclet"(token %noinfo.right.pad) ]
290 to label %unreachable unwind label %noinfo.right.child
291 ; CHECK: [[noinfo_right_catch]]:
292 ; CHECK-NEXT: %[[noinfo_right_pad:[^ ]+]] = catchpad within %[[noinfo_right_cs]]
293 ; CHECK-NEXT: invoke void @g() [ "funclet"(token %[[noinfo_right_pad]]) ]
294 ; CHECK-NEXT: unwind label %[[noinfo_right_child:.+]]
297 %noinfo.right.child.pad = cleanuppad within %noinfo.right.pad []
298 call void @g() [ "funclet"(token %noinfo.right.child.pad) ]
299 br label %unreachable
300 ; CHECK: [[noinfo_right_child]]:
301 ; CHECK-NEXT: %[[noinfo_right_child_pad:[^ ]+]] = cleanuppad within %[[noinfo_right_pad]]
302 ; CHECK-NEXT: invoke void @g() [ "funclet"(token %[[noinfo_right_child_pad]]) ]
303 ; CHECK-NEXT: unwind label %cleanup{{$}}
307 to label %exit unwind label %implicit.root
309 ; CHECK-NEXT: invoke void @g()
310 ; CHECK-NEXT: unwind label %[[implicit_root:.+]]
313 %implicit.root.pad = cleanuppad within none []
314 call void @g() [ "funclet"(token %implicit.root.pad) ]
315 invoke void @g() [ "funclet"(token %implicit.root.pad) ]
316 to label %implicit.root.cont unwind label %implicit.left
317 ; CHECK: [[implicit_root]]:
318 ; There's an unwind edge to %internal in implicit.right, and we need to propagate that
319 ; fact down to implicit.right.grandchild, up to implicit.root, and down to
320 ; implicit.left.child.catch, leaving all calls and "unwind to caller" catchswitches
321 ; alone to so they don't conflict with the unwind edge in implicit.right
322 ; CHECK-NEXT: %[[implicit_root_pad:[^ ]+]] = cleanuppad within none
323 ; CHECK-NEXT: call void @g() [ "funclet"(token %[[implicit_root_pad]]) ]
324 ; CHECK-NEXT: invoke void @g() [ "funclet"(token %[[implicit_root_pad]]) ]
325 ; CHECK-NEXT: to label %[[implicit_root_cont:[^ ]+]] unwind label %[[implicit_left:.+]]
328 %implicit.left.pad = cleanuppad within %implicit.root.pad []
329 invoke void @g() [ "funclet"(token %implicit.left.pad) ]
330 to label %unreachable unwind label %implicit.left.child
331 ; CHECK: [[implicit_left]]:
332 ; CHECK-NEXT: %[[implicit_left_pad:[^ ]+]] = cleanuppad within %[[implicit_root_pad:[^ ]+]]
333 ; CHECK-NEXT: invoke void @g() [ "funclet"(token %[[implicit_left_pad]]) ]
334 ; CHECK-NEXT: unwind label %[[implicit_left_child:.+]]
337 %implicit.left.child.cs = catchswitch within %implicit.left.pad [label %implicit.left.child.catch] unwind to caller
338 ; CHECK: [[implicit_left_child]]:
339 ; CHECK-NEXT: %[[implicit_left_child_cs:[^ ]+]] = catchswitch within %[[implicit_left_pad]] [label %[[implicit_left_child_catch:[^ ]+]]] unwind to caller
341 implicit.left.child.catch:
342 %implicit.left.child.pad = catchpad within %implicit.left.child.cs []
343 call void @g() [ "funclet"(token %implicit.left.child.pad) ]
344 br label %unreachable
345 ; CHECK: [[implicit_left_child_catch]]:
346 ; CHECK-NEXT: %[[implicit_left_child_pad:[^ ]+]] = catchpad within %[[implicit_left_child_cs]]
347 ; CHECK-NEXT: call void @g() [ "funclet"(token %[[implicit_left_child_pad]]) ]
350 invoke void @g() [ "funclet"(token %implicit.root.pad) ]
351 to label %unreachable unwind label %implicit.right
352 ; CHECK: [[implicit_root_cont]]:
353 ; CHECK-NEXT: invoke void @g() [ "funclet"(token %[[implicit_root_pad]]) ]
354 ; CHECK-NEXT: unwind label %[[implicit_right:.+]]
357 %implicit.right.cs = catchswitch within %implicit.root.pad [label %implicit.right.catch] unwind label %internal
358 ; CHECK: [[implicit_right]]:
359 ; This is the unwind edge (to %internal) whose existence needs to get propagated around the "implicit" tree
360 ; CHECK-NEXT: %[[implicit_right_cs:[^ ]+]] = catchswitch within %[[implicit_root_pad]] [label %[[implicit_right_catch:[^ ]+]]] unwind label %[[internal:.+]]
362 implicit.right.catch:
363 %implicit.right.pad = catchpad within %implicit.right.cs []
364 invoke void @g() [ "funclet"(token %implicit.right.pad) ]
365 to label %unreachable unwind label %implicit.right.child
366 ; CHECK: [[implicit_right_catch]]:
367 ; CHECK-NEXT: %[[implicit_right_pad:[^ ]+]] = catchpad within %[[implicit_right_cs]]
368 ; CHECK-NEXT: invoke void @g() [ "funclet"(token %[[implicit_right_pad]]) ]
369 ; CHECK-NEXT: unwind label %[[implicit_right_child:.+]]
371 implicit.right.child:
372 %implicit.right.child.pad = cleanuppad within %implicit.right.pad []
373 invoke void @g() [ "funclet"(token %implicit.right.child.pad) ]
374 to label %unreachable unwind label %implicit.right.grandchild
375 ; CHECK: [[implicit_right_child]]:
376 ; CHECK-NEXT: %[[implicit_right_child_pad:[^ ]+]] = cleanuppad within %[[implicit_right_pad]]
377 ; CHECK-NEXT: invoke void @g() [ "funclet"(token %[[implicit_right_child_pad]]) ]
378 ; CHECK-NEXT: unwind label %[[implicit_right_grandchild:.+]]
380 implicit.right.grandchild:
381 %implicit.right.grandchild.cs = catchswitch within %implicit.right.child.pad [label %implicit.right.grandchild.catch] unwind to caller
382 ; CHECK: [[implicit_right_grandchild]]:
383 ; CHECK-NEXT: %[[implicit_right_grandchild_cs:[^ ]+]] = catchswitch within %[[implicit_right_child_pad]] [label %[[implicit_right_grandchild_catch:[^ ]+]]] unwind to caller
385 implicit.right.grandchild.catch:
386 %implicit.right.grandhcild.pad = catchpad within %implicit.right.grandchild.cs []
387 call void @g() [ "funclet"(token %implicit.right.grandhcild.pad) ]
388 br label %unreachable
389 ; CHECK: [[implicit_right_grandchild_catch]]:
390 ; CHECK-NEXT: %[[implicit_right_grandhcild_pad:[^ ]+]] = catchpad within %[[implicit_right_grandchild_cs]]
391 ; CHECK-NEXT: call void @g() [ "funclet"(token %[[implicit_right_grandhcild_pad]]) ]
394 %internal.pad = cleanuppad within none []
395 call void @g() [ "funclet"(token %internal.pad) ]
396 cleanupret from %internal.pad unwind to caller
397 ; CHECK: [[internal]]:
398 ; internal is a cleanup with a "return to caller" cleanuppad; that needs to get redirected
399 ; to %cleanup in the caller, and the call needs to get similarly rewritten to an invoke.
400 ; CHECK-NEXT: %[[internal_pad:[^ ]+]] = cleanuppad within none
401 ; CHECK-NEXT: invoke void @g() [ "funclet"(token %internal.pad.i) ]
402 ; CHECK-NEXT: to label %[[next:[^ ]+]] unwind label %cleanup{{$}}
404 ; CHECK-NEXT: cleanupret from %[[internal_pad]] unwind label %cleanup{{$}}
413 declare void @ProcessCLRException()
415 ; Make sure the logic doesn't get tripped up when the inlined invoke is
416 ; itself within a funclet in the caller.
417 ; CHECK-LABEL: define void @test6(
418 define void @test6() personality void ()* @ProcessCLRException {
421 to label %exit unwind label %callsite_parent
423 %callsite_parent.pad = cleanuppad within none []
424 ; CHECK: %callsite_parent.pad = cleanuppad within none
425 invoke void @test6_inlinee() [ "funclet"(token %callsite_parent.pad) ]
426 to label %ret unwind label %cleanup
428 cleanupret from %callsite_parent.pad unwind label %cleanup
430 %pad = cleanuppad within none []
431 call void @g() [ "funclet"(token %pad) ]
432 cleanupret from %pad unwind to caller
437 define void @test6_inlinee() alwaysinline personality void ()* @ProcessCLRException {
440 to label %exit unwind label %inlinee_cleanup
441 ; CHECK-NEXT: invoke void @g() [ "funclet"(token %callsite_parent.pad) ]
442 ; CHECK-NEXT: unwind label %[[inlinee_cleanup:.+]]
445 %inlinee.pad = cleanuppad within none []
446 call void @g() [ "funclet"(token %inlinee.pad) ]
448 ; CHECK: [[inlinee_cleanup]]:
449 ; CHECK-NEXT: %[[inlinee_pad:[^ ]+]] = cleanuppad within %callsite_parent.pad
450 ; CHECK-NEXT: invoke void @g() [ "funclet"(token %[[inlinee_pad]]) ]
451 ; CHECK-NEXT: unwind label %cleanup{{$}}