1 ; RUN: opt < %s -simplifycfg -S | filecheck %s
3 ; ModuleID = 'cppeh-simplify.cpp'
4 target datalayout = "e-m:w-i64:64-f80:128-n8:16:32:64-S128"
5 target triple = "x86_64-pc-windows-msvc18.0.0"
8 ; This case arises when two objects with empty destructors are cleaned up.
16 ; In this case, both cleanup pads can be eliminated and the invoke can be
17 ; converted to a call.
19 ; CHECK: define void @f1()
21 ; CHECK: call void @g()
23 ; CHECK-NOT: cleanuppad
26 define void @f1() personality i8* bitcast (i32 (...)* @__CxxFrameHandler3 to i8*) {
28 invoke void @g() to label %invoke.cont unwind label %ehcleanup
30 invoke.cont: ; preds = %entry
33 ehcleanup: ; preds = %entry
35 cleanupret %0 unwind label %ehcleanup.1
37 ehcleanup.1: ; preds = %ehcleanup
39 cleanupret %1 unwind to caller
43 ; This case arises when an object with an empty destructor must be cleaned up
44 ; outside of a try-block and an object with a non-empty destructor must be
45 ; cleaned up within the try-block.
55 ; In this case, the outermost cleanup pad can be eliminated and the catch block
56 ; should unwind to the caller (that is, exception handling continues with the
57 ; parent frame of the caller).
59 ; CHECK: define void @f2()
61 ; CHECK: invoke void @g()
64 ; CHECK: call void @"\01??1S2@@QEAA@XZ"(%struct.S2* %b)
65 ; CHECK: cleanupret %0 unwind label %catch.dispatch
66 ; CHECK: catch.dispatch:
70 ; CHECK: catchendblock: ; preds = %catch.dispatch
71 ; CHECK: catchendpad unwind to caller
72 ; CHECK-NOT: cleanuppad
75 define void @f2() personality i8* bitcast (i32 (...)* @__CxxFrameHandler3 to i8*) {
77 %b = alloca %struct.S2, align 1
78 invoke void @g() to label %invoke.cont unwind label %ehcleanup
80 invoke.cont: ; preds = %entry
83 ehcleanup: ; preds = %entry
85 call void @"\01??1S2@@QEAA@XZ"(%struct.S2* %b)
86 cleanupret %0 unwind label %catch.dispatch
88 catch.dispatch: ; preds = %ehcleanup
89 %1 = catchpad [i8* null, i8* null] to label %catch unwind label %catchendblock
91 catch: ; preds = %catch.dispatch
92 catchret %1 to label %catchret.dest
94 catchret.dest: ; preds = %catch
97 try.cont: ; preds = %catchret.dest, %invoke.cont
100 catchendblock: ; preds = %catch.dispatch
101 catchendpad unwind label %ehcleanup.1
103 ehcleanup.1: ; preds = %catchendblock
105 cleanupret %2 unwind to caller
109 ; This case arises when an object with a non-empty destructor must be cleaned up
110 ; outside of a try-block and an object with an empty destructor must be cleaned
111 ; within the try-block.
121 ; In this case the inner cleanup pad should be eliminated and the invoke of g()
122 ; should unwind directly to the catchpad.
124 ; CHECK: define void @f3()
126 ; CHECK: invoke void @g()
127 ; CHECK: to label %try.cont unwind label %catch.dispatch
128 ; CHECK: catch.dispatch:
129 ; CHECK: catchpad [i8* null, i8* null] to label %catch unwind label %catchendblock
132 ; CHECK: catchendblock:
133 ; CHECK: catchendpad unwind label %ehcleanup.1
134 ; CHECK: ehcleanup.1:
136 ; CHECK: call void @"\01??1S2@@QEAA@XZ"(%struct.S2* %a)
137 ; CHECK: cleanupret %1 unwind to caller
140 define void @f3() personality i8* bitcast (i32 (...)* @__CxxFrameHandler3 to i8*) {
142 %a = alloca %struct.S2, align 1
143 invoke void @g() to label %invoke.cont unwind label %ehcleanup
145 invoke.cont: ; preds = %entry
148 ehcleanup: ; preds = %entry
150 cleanupret %0 unwind label %catch.dispatch
152 catch.dispatch: ; preds = %ehcleanup
153 %1 = catchpad [i8* null, i8* null] to label %catch unwind label %catchendblock
155 catch: ; preds = %catch.dispatch
156 catchret %1 to label %catchret.dest
158 catchret.dest: ; preds = %catch
161 try.cont: ; preds = %catchret.dest, %invoke.cont
164 catchendblock: ; preds = %catch.dispatch
165 catchendpad unwind label %ehcleanup.1
167 ehcleanup.1: ; preds = %catchendblock
169 call void @"\01??1S2@@QEAA@XZ"(%struct.S2* %a)
170 cleanupret %2 unwind to caller
174 ; This case arises when an object with an empty destructor may require cleanup
175 ; from either inside or outside of a try-block.
185 ; In this case, the cleanuppad should be eliminated, the invoke outside of the
186 ; call block should be converted to a call and the catchendpad should unwind
187 ; to the caller (that is, that is, exception handling continues with the parent
188 ; frame of the caller).)
190 ; CHECK: define void @f4()
192 ; CHECK: call void @g
193 ; Note: The cleanuppad simplification will insert an unconditional branch here
194 ; but it will be eliminated, placing the following invoke in the entry BB.
195 ; CHECK: invoke void @g()
196 ; CHECK: to label %try.cont unwind label %catch.dispatch
197 ; CHECK: catch.dispatch:
201 ; CHECK: catchendblock:
202 ; CHECK: catchendpad unwind to caller
203 ; CHECK-NOT: cleanuppad
206 define void @f4() personality i8* bitcast (i32 (...)* @__CxxFrameHandler3 to i8*) {
209 to label %invoke.cont unwind label %ehcleanup
211 invoke.cont: ; preds = %entry
213 to label %try.cont unwind label %catch.dispatch
215 catch.dispatch: ; preds = %invoke.cont
216 %0 = catchpad [i8* null, i8* null] to label %catch unwind label %catchendblock
218 catch: ; preds = %catch.dispatch
219 catchret %0 to label %try.cont
221 try.cont: ; preds = %catch, %invoke.cont
224 catchendblock: ; preds = %catch.dispatch
225 catchendpad unwind label %ehcleanup
227 ehcleanup: ; preds = %catchendblock, %entry
229 cleanupret %1 unwind to caller
232 ; This tests the case where a terminatepad unwinds to a cleanuppad.
233 ; I'm not sure how this case would arise, but it seems to be syntactically
234 ; legal so I'm testing it.
236 ; CHECK: define void @f5()
238 ; CHECK: invoke void @g()
239 ; CHECK: to label %try.cont unwind label %terminate
241 ; CHECK: terminatepad [i7 4] unwind to caller
242 ; CHECK-NOT: cleanuppad
244 ; CHECK: invoke void @g()
245 ; CHECK: to label %try.cont.1 unwind label %terminate.1
246 ; CHECK: terminate.1:
247 ; CHECK: terminatepad [i7 4] unwind label %ehcleanup.2
248 ; CHECK-NOT: ehcleanup.1:
249 ; CHECK: ehcleanup.2:
250 ; CHECK: [[TMP:\%.+]] = cleanuppad
251 ; CHECK: call void @"\01??1S2@@QEAA@XZ"(%struct.S2* %a)
252 ; CHECK: cleanupret [[TMP]] unwind to caller
254 define void @f5() personality i8* bitcast (i32 (...)* @__CxxFrameHandler3 to i8*) {
256 %a = alloca %struct.S2, align 1
258 to label %try.cont unwind label %terminate
260 terminate: ; preds = %entry
261 terminatepad [i7 4] unwind label %ehcleanup
263 ehcleanup: ; preds = %terminate
265 cleanupret %0 unwind to caller
267 try.cont: ; preds = %entry
269 to label %try.cont.1 unwind label %terminate.1
271 terminate.1: ; preds = %try.cont
272 terminatepad [i7 4] unwind label %ehcleanup.1
274 ehcleanup.1: ; preds = %terminate.1
276 cleanupret %1 unwind label %ehcleanup.2
278 ehcleanup.2: ; preds = %ehcleanup.1
280 call void @"\01??1S2@@QEAA@XZ"(%struct.S2* %a)
281 cleanupret %2 unwind to caller
283 try.cont.1: ; preds = %try.cont
287 ; This case tests simplification of an otherwise empty cleanup pad that contains
303 ; In this case, the cleanup pad should be eliminated and the PHI node in the
304 ; cleanup pad should be sunk into the catch dispatch block.
306 ; CHECK: define i32 @f6()
308 ; CHECK: invoke void @g()
309 ; CHECK: invoke.cont:
310 ; CHECK: invoke void @g()
311 ; CHECK-NOT: ehcleanup:
312 ; CHECK-NOT: cleanuppad
313 ; CHECK: catch.dispatch:
314 ; CHECK: %state.0 = phi i32 [ 2, %invoke.cont ], [ 1, %entry ]
316 define i32 @f6() personality i8* bitcast (i32 (...)* @__CxxFrameHandler3 to i8*) {
319 to label %invoke.cont unwind label %ehcleanup
321 invoke.cont: ; preds = %entry
323 to label %return unwind label %ehcleanup
325 ehcleanup: ; preds = %invoke.cont, %entry
326 %state.0 = phi i32 [ 2, %invoke.cont ], [ 1, %entry ]
328 cleanupret %0 unwind label %catch.dispatch
330 catch.dispatch: ; preds = %ehcleanup
331 %1 = catchpad [i8* null, i8* null] to label %catch unwind label %catchendblock
333 catch: ; preds = %catch.dispatch
334 catchret %1 to label %return
336 catchendblock: ; preds = %catch.dispatch
337 catchendpad unwind to caller
339 return: ; preds = %invoke.cont, %catch
340 %retval.0 = phi i32 [ %state.0, %catch ], [ 0, %invoke.cont ]
344 ; This case tests another variation of simplification of an otherwise empty
345 ; cleanup pad that contains a PHI node.
362 ; In this case, the cleanup pad should be eliminated and the PHI node in the
363 ; cleanup pad should be merged with the PHI node in the catch dispatch block.
365 ; CHECK: define i32 @f7()
367 ; CHECK: invoke void @g()
368 ; CHECK: invoke.cont:
369 ; CHECK: invoke void @g()
370 ; CHECK: invoke.cont.1:
371 ; CHECK: invoke void @g()
372 ; CHECK-NOT: ehcleanup:
373 ; CHECK-NOT: cleanuppad
374 ; CHECK: catch.dispatch:
375 ; CHECK: %state.1 = phi i32 [ 1, %entry ], [ 3, %invoke.cont.1 ], [ 2, %invoke.cont ]
377 define i32 @f7() personality i8* bitcast (i32 (...)* @__CxxFrameHandler3 to i8*) {
380 to label %invoke.cont unwind label %catch.dispatch
382 invoke.cont: ; preds = %entry
384 to label %invoke.cont.1 unwind label %ehcleanup
386 invoke.cont.1: ; preds = %invoke.cont
388 to label %return unwind label %ehcleanup
390 ehcleanup: ; preds = %invoke.cont.1, %invoke.cont
391 %state.0 = phi i32 [ 3, %invoke.cont.1 ], [ 2, %invoke.cont ]
393 cleanupret %0 unwind label %catch.dispatch
395 catch.dispatch: ; preds = %ehcleanup, %entry
396 %state.1 = phi i32 [ %state.0, %ehcleanup ], [ 1, %entry ]
397 %1 = catchpad [i8* null, i8* null] to label %catch unwind label %catchendblock
399 catch: ; preds = %catch.dispatch
400 catchret %1 to label %return
402 catchendblock: ; preds = %catch.dispatch
403 catchendpad unwind to caller
405 return: ; preds = %invoke.cont.1, %catch
406 %retval.0 = phi i32 [ %state.1, %catch ], [ 0, %invoke.cont.1 ]
410 ; This case tests a scenario where an empty cleanup pad is not dominated by all
411 ; of the predecessors of its successor, but the successor references a PHI node
412 ; in the empty cleanup pad.
414 ; Conceptually, the case being modeled is something like this:
431 ; While that C++ syntax isn't legal, the IR below is.
433 ; In this case, the PHI node that is sunk from ehcleanup to catch.dispatch
434 ; should have an incoming value entry for path from 'foo' that references the
437 ; CHECK: define void @f8()
439 ; CHECK: invoke void @g()
440 ; CHECK: invoke.cont:
441 ; CHECK: invoke void @g()
442 ; CHECK-NOT: ehcleanup:
443 ; CHECK-NOT: cleanuppad
444 ; CHECK: catch.dispatch:
445 ; CHECK: %x = phi i32 [ 2, %invoke.cont ], [ 1, %entry ], [ %x, %catch.cont ]
447 define void @f8() personality i8* bitcast (i32 (...)* @__CxxFrameHandler3 to i8*) {
450 to label %invoke.cont unwind label %ehcleanup
452 invoke.cont: ; preds = %entry
454 to label %return unwind label %ehcleanup
456 ehcleanup: ; preds = %invoke.cont, %entry
457 %x = phi i32 [ 2, %invoke.cont ], [ 1, %entry ]
459 cleanupret %0 unwind label %catch.dispatch
461 catch.dispatch: ; preds = %ehcleanup, %catch.cont
462 %1 = catchpad [i8* null, i8* null] to label %catch unwind label %catchendblock
464 catch: ; preds = %catch.dispatch
465 call void @use_x(i32 %x)
466 catchret %1 to label %catch.cont
468 catchendblock: ; preds = %catch.dispatch
469 catchendpad unwind to caller
471 catch.cont: ; preds = %catch
473 to label %return unwind label %catch.dispatch
475 return: ; preds = %invoke.cont, %catch.cont
479 %struct.S = type { i8 }
480 %struct.S2 = type { i8 }
481 declare void @"\01??1S2@@QEAA@XZ"(%struct.S2*)
483 declare void @use_x(i32 %x)
485 declare i32 @__CxxFrameHandler3(...)