llvm.org GIT mirror llvm / 66ef931 test / Transforms / SimplifyCFG / empty-cleanuppad.ll
66ef931

Tree @66ef931 (Download .tar.gz)

empty-cleanuppad.ll @66ef931raw · history · blame

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
; RUN: opt < %s -simplifycfg -S | FileCheck %s

; ModuleID = 'cppeh-simplify.cpp'
target datalayout = "e-m:w-i64:64-f80:128-n8:16:32:64-S128"
target triple = "x86_64-pc-windows-msvc18.0.0"


; This case arises when two objects with empty destructors are cleaned up.
;
; void f1() { 
;   S a;
;   S b;
;   g(); 
; }
;
; In this case, both cleanup pads can be eliminated and the invoke can be
; converted to a call.
;
; CHECK: define void @f1()
; CHECK: entry:
; CHECK:   call void @g()
; CHECK:   ret void
; CHECK-NOT: cleanuppad
; CHECK: }
;
define void @f1() personality i8* bitcast (i32 (...)* @__CxxFrameHandler3 to i8*) {
entry:
  invoke void @g() to label %invoke.cont unwind label %ehcleanup

invoke.cont:                                      ; preds = %entry
  ret void

ehcleanup:                                        ; preds = %entry
  %0 = cleanuppad []
  cleanupret %0 unwind label %ehcleanup.1

ehcleanup.1:                                      ; preds = %ehcleanup
  %1 = cleanuppad []
  cleanupret %1 unwind to caller
}


; This case arises when an object with an empty destructor must be cleaned up
; outside of a try-block and an object with a non-empty destructor must be
; cleaned up within the try-block.
;
; void f2() { 
;   S a;
;   try {
;     S2 b;
;     g();
;   } catch (...) {}
; }
;
; In this case, the outermost cleanup pad can be eliminated and the catch block
; should unwind to the caller (that is, exception handling continues with the
; parent frame of the caller).
;
; CHECK: define void @f2()
; CHECK: entry:
; CHECK:   invoke void @g()
; CHECK: ehcleanup:
; CHECK:   cleanuppad
; CHECK:   call void @"\01??1S2@@QEAA@XZ"(%struct.S2* %b)
; CHECK:   cleanupret %0 unwind label %catch.dispatch
; CHECK: catch.dispatch:
; CHECK:   catchpad
; CHECK: catch:
; CHECK:   catchret
; CHECK: catchendblock:                                    ; preds = %catch.dispatch
; CHECK:   catchendpad unwind to caller
; CHECK-NOT: cleanuppad
; CHECK: }
;
define void @f2() personality i8* bitcast (i32 (...)* @__CxxFrameHandler3 to i8*) {
entry:
  %b = alloca %struct.S2, align 1
  invoke void @g() to label %invoke.cont unwind label %ehcleanup

invoke.cont:                                      ; preds = %entry
  br label %try.cont

ehcleanup:                                        ; preds = %entry
  %0 = cleanuppad []
  call void @"\01??1S2@@QEAA@XZ"(%struct.S2* %b)
  cleanupret %0 unwind label %catch.dispatch

catch.dispatch:                                   ; preds = %ehcleanup
  %1 = catchpad [i8* null, i32 u0x40, i8* null] to label %catch unwind label %catchendblock

catch:                                            ; preds = %catch.dispatch
  catchret %1 to label %catchret.dest

catchret.dest:                                    ; preds = %catch
  br label %try.cont

try.cont:                                         ; preds = %catchret.dest, %invoke.cont
  ret void

catchendblock:                                    ; preds = %catch.dispatch
  catchendpad unwind label %ehcleanup.1

ehcleanup.1:                                      ; preds = %catchendblock
  %2 = cleanuppad []
  cleanupret %2 unwind to caller
}


; This case arises when an object with a non-empty destructor must be cleaned up
; outside of a try-block and an object with an empty destructor must be cleaned
; within the try-block.
;
; void f3() { 
;   S2 a;
;   try {
;     S b;
;     g();
;   } catch (...) {}
; }
;
; In this case the inner cleanup pad should be eliminated and the invoke of g()
; should unwind directly to the catchpad.
;
; CHECK: define void @f3()
; CHECK: entry:
; CHECK:   invoke void @g()
; CHECK:           to label %try.cont unwind label %catch.dispatch
; CHECK: catch.dispatch:
; CHECK:   catchpad [i8* null, i32 64, i8* null]
; CHECK-NEXT: to label %catch unwind label %catchendblock
; CHECK: catch:
; CHECK:   catchret
; CHECK: catchendblock:
; CHECK:   catchendpad unwind label %ehcleanup.1
; CHECK: ehcleanup.1:
; CHECK:   cleanuppad
; CHECK:   call void @"\01??1S2@@QEAA@XZ"(%struct.S2* %a)
; CHECK:   cleanupret %1 unwind to caller
; CHECK: }
;
define void @f3() personality i8* bitcast (i32 (...)* @__CxxFrameHandler3 to i8*) {
entry:
  %a = alloca %struct.S2, align 1
  invoke void @g() to label %invoke.cont unwind label %ehcleanup

invoke.cont:                                      ; preds = %entry
  br label %try.cont

ehcleanup:                                        ; preds = %entry
  %0 = cleanuppad []
  cleanupret %0 unwind label %catch.dispatch

catch.dispatch:                                   ; preds = %ehcleanup
  %1 = catchpad [i8* null, i32 u0x40, i8* null] to label %catch unwind label %catchendblock

catch:                                            ; preds = %catch.dispatch
  catchret %1 to label %catchret.dest

catchret.dest:                                    ; preds = %catch
  br label %try.cont

try.cont:                                         ; preds = %catchret.dest, %invoke.cont
  ret void

catchendblock:                                    ; preds = %catch.dispatch
  catchendpad unwind label %ehcleanup.1

ehcleanup.1:                                      ; preds = %catchendblock
  %2 = cleanuppad []
  call void @"\01??1S2@@QEAA@XZ"(%struct.S2* %a)
  cleanupret %2 unwind to caller
}


; This case arises when an object with an empty destructor may require cleanup
; from either inside or outside of a try-block.
;
; void f4() { 
;   S a;
;   g();
;   try {
;     g();
;   } catch (...) {}
; }
;
; In this case, the cleanuppad should be eliminated, the invoke outside of the
; call block should be converted to a call and the catchendpad should unwind
; to the caller (that is, that is, exception handling continues with the parent
; frame of the caller).)
;
; CHECK: define void @f4()
; CHECK: entry:
; CHECK:   call void @g
; Note: The cleanuppad simplification will insert an unconditional branch here
;       but it will be eliminated, placing the following invoke in the entry BB. 
; CHECK:   invoke void @g()
; CHECK:           to label %try.cont unwind label %catch.dispatch
; CHECK: catch.dispatch:
; CHECK:   catchpad
; CHECK: catch:
; CHECK:   catchret
; CHECK: catchendblock:
; CHECK:   catchendpad unwind to caller
; CHECK-NOT: cleanuppad
; CHECK: }
;
define void @f4() personality i8* bitcast (i32 (...)* @__CxxFrameHandler3 to i8*) {
entry:
  invoke void @g()
          to label %invoke.cont unwind label %ehcleanup

invoke.cont:                                      ; preds = %entry
  invoke void @g()
          to label %try.cont unwind label %catch.dispatch

catch.dispatch:                                   ; preds = %invoke.cont
  %0 = catchpad [i8* null, i32 u0x40, i8* null] to label %catch unwind label %catchendblock

catch:                                            ; preds = %catch.dispatch
  catchret %0 to label %try.cont

try.cont:                                         ; preds = %catch, %invoke.cont
  ret void

catchendblock:                                    ; preds = %catch.dispatch
  catchendpad unwind label %ehcleanup

ehcleanup:                                        ; preds = %catchendblock, %entry
  %1 = cleanuppad []
  cleanupret %1 unwind to caller
}

; This tests the case where a terminatepad unwinds to a cleanuppad.
; I'm not sure how this case would arise, but it seems to be syntactically
; legal so I'm testing it.
;
; CHECK: define void @f5()
; CHECK: entry:
; CHECK:   invoke void @g()
; CHECK:           to label %try.cont unwind label %terminate
; CHECK: terminate:
; CHECK:   terminatepad [i7 4] unwind to caller
; CHECK-NOT: cleanuppad
; CHECK: try.cont:
; CHECK:   invoke void @g()
; CHECK:           to label %try.cont.1 unwind label %terminate.1
; CHECK: terminate.1:
; CHECK:   terminatepad [i7 4] unwind label %ehcleanup.2
; CHECK-NOT: ehcleanup.1:
; CHECK: ehcleanup.2:
; CHECK:   [[TMP:\%.+]] = cleanuppad
; CHECK:   call void @"\01??1S2@@QEAA@XZ"(%struct.S2* %a)
; CHECK:   cleanupret [[TMP]] unwind to caller
; CHECK: }
define void @f5() personality i8* bitcast (i32 (...)* @__CxxFrameHandler3 to i8*) {
entry:
  %a = alloca %struct.S2, align 1
  invoke void @g()
          to label %try.cont unwind label %terminate

terminate:                                        ; preds = %entry
  terminatepad [i7 4] unwind label %ehcleanup

ehcleanup:                                        ; preds = %terminate
  %0 = cleanuppad []
  cleanupret %0 unwind to caller

try.cont:                                         ; preds = %entry
  invoke void @g()
          to label %try.cont.1 unwind label %terminate.1

terminate.1:                                      ; preds = %try.cont
  terminatepad [i7 4] unwind label %ehcleanup.1

ehcleanup.1:                                      ; preds = %terminate.1
  %1 = cleanuppad []
  cleanupret %1 unwind label %ehcleanup.2

ehcleanup.2:                                      ; preds = %ehcleanup.1
  %2 = cleanuppad []
  call void @"\01??1S2@@QEAA@XZ"(%struct.S2* %a)
  cleanupret %2 unwind to caller

try.cont.1:                                       ; preds = %try.cont
  ret void
}

; This case tests simplification of an otherwise empty cleanup pad that contains
; a PHI node.
;
; int f6() {
;   int state = 1;
;   try {
;     S a;
;     g();
;     state = 2;
;     g();
;   } catch (...) {
;     return state;
;   }
;   return 0;
; }
;
; In this case, the cleanup pad should be eliminated and the PHI node in the
; cleanup pad should be sunk into the catch dispatch block.
;
; CHECK: define i32 @f6()
; CHECK: entry:
; CHECK:   invoke void @g()
; CHECK: invoke.cont:
; CHECK:   invoke void @g()
; CHECK-NOT: ehcleanup:
; CHECK-NOT:   cleanuppad
; CHECK: catch.dispatch:
; CHECK:   %state.0 = phi i32 [ 2, %invoke.cont ], [ 1, %entry ]
; CHECK: }
define i32 @f6() personality i8* bitcast (i32 (...)* @__CxxFrameHandler3 to i8*) {
entry:
  invoke void @g()
          to label %invoke.cont unwind label %ehcleanup

invoke.cont:                                      ; preds = %entry
  invoke void @g()
          to label %return unwind label %ehcleanup

ehcleanup:                                        ; preds = %invoke.cont, %entry
  %state.0 = phi i32 [ 2, %invoke.cont ], [ 1, %entry ]
  %0 = cleanuppad []
  cleanupret %0 unwind label %catch.dispatch

catch.dispatch:                                   ; preds = %ehcleanup
  %1 = catchpad [i8* null, i32 u0x40, i8* null] to label %catch unwind label %catchendblock

catch:                                            ; preds = %catch.dispatch
  catchret %1 to label %return

catchendblock:                                    ; preds = %catch.dispatch
  catchendpad unwind to caller

return:                                           ; preds = %invoke.cont, %catch
  %retval.0 = phi i32 [ %state.0, %catch ], [ 0, %invoke.cont ]
  ret i32 %retval.0
}

; This case tests another variation of simplification of an otherwise empty
; cleanup pad that contains a PHI node.
;
; int f7() {
;   int state = 1;
;   try {
;     g();
;     state = 2;
;     S a;
;     g();
;     state = 3;
;     g();
;   } catch (...) {
;     return state;
;   }
;   return 0;
; }
;
; In this case, the cleanup pad should be eliminated and the PHI node in the
; cleanup pad should be merged with the PHI node in the catch dispatch block.
;
; CHECK: define i32 @f7()
; CHECK: entry:
; CHECK:   invoke void @g()
; CHECK: invoke.cont:
; CHECK:   invoke void @g()
; CHECK: invoke.cont.1:
; CHECK:   invoke void @g()
; CHECK-NOT: ehcleanup:
; CHECK-NOT:   cleanuppad
; CHECK: catch.dispatch:
; CHECK:   %state.1 = phi i32 [ 1, %entry ], [ 3, %invoke.cont.1 ], [ 2, %invoke.cont ]
; CHECK: }
define i32 @f7() personality i8* bitcast (i32 (...)* @__CxxFrameHandler3 to i8*) {
entry:
  invoke void @g()
          to label %invoke.cont unwind label %catch.dispatch

invoke.cont:                                      ; preds = %entry
  invoke void @g()
          to label %invoke.cont.1 unwind label %ehcleanup

invoke.cont.1:                                    ; preds = %invoke.cont
  invoke void @g()
          to label %return unwind label %ehcleanup

ehcleanup:                                        ; preds = %invoke.cont.1, %invoke.cont
  %state.0 = phi i32 [ 3, %invoke.cont.1 ], [ 2, %invoke.cont ]
  %0 = cleanuppad []
  cleanupret %0 unwind label %catch.dispatch

catch.dispatch:                                   ; preds = %ehcleanup, %entry
  %state.1 = phi i32 [ %state.0, %ehcleanup ], [ 1, %entry ]
  %1 = catchpad [i8* null, i32 u0x40, i8* null] to label %catch unwind label %catchendblock

catch:                                            ; preds = %catch.dispatch
  catchret %1 to label %return

catchendblock:                                    ; preds = %catch.dispatch
  catchendpad unwind to caller

return:                                           ; preds = %invoke.cont.1, %catch
  %retval.0 = phi i32 [ %state.1, %catch ], [ 0, %invoke.cont.1 ]
  ret i32 %retval.0
}

; This case tests a scenario where an empty cleanup pad is not dominated by all
; of the predecessors of its successor, but the successor references a PHI node
; in the empty cleanup pad.
;
; Conceptually, the case being modeled is something like this:
;
; int f8() {
;   int x = 1;
;   try {
;     S a;
;     g();
;     x = 2;
; retry:
;     g();
;     return
;   } catch (...) {
;     use_x(x);
;   }
;   goto retry;
; }
;
; While that C++ syntax isn't legal, the IR below is.
;
; In this case, the PHI node that is sunk from ehcleanup to catch.dispatch
; should have an incoming value entry for path from 'foo' that references the
; PHI node itself.
;
; CHECK: define void @f8()
; CHECK: entry:
; CHECK:   invoke void @g()
; CHECK: invoke.cont:
; CHECK:   invoke void @g()
; CHECK-NOT: ehcleanup:
; CHECK-NOT:   cleanuppad
; CHECK: catch.dispatch:
; CHECK:   %x = phi i32 [ 2, %invoke.cont ], [ 1, %entry ], [ %x, %catch.cont ] 
; CHECK: }
define void @f8() personality i8* bitcast (i32 (...)* @__CxxFrameHandler3 to i8*) {
entry:
  invoke void @g()
          to label %invoke.cont unwind label %ehcleanup

invoke.cont:                                      ; preds = %entry
  invoke void @g()
          to label %return unwind label %ehcleanup

ehcleanup:                                        ; preds = %invoke.cont, %entry
  %x = phi i32 [ 2, %invoke.cont ], [ 1, %entry ]
  %0 = cleanuppad []
  cleanupret %0 unwind label %catch.dispatch

catch.dispatch:                                   ; preds = %ehcleanup, %catch.cont
  %1 = catchpad [i8* null, i32 u0x40, i8* null] to label %catch unwind label %catchendblock

catch:                                            ; preds = %catch.dispatch
  call void @use_x(i32 %x)
  catchret %1 to label %catch.cont

catchendblock:                                    ; preds = %catch.dispatch
  catchendpad unwind to caller

catch.cont:                                       ; preds = %catch
  invoke void @g()
          to label %return unwind label %catch.dispatch

return:                                           ; preds = %invoke.cont, %catch.cont
  ret void
}

%struct.S = type { i8 }
%struct.S2 = type { i8 }
declare void @"\01??1S2@@QEAA@XZ"(%struct.S2*)
declare void @g()
declare void @use_x(i32 %x)

declare i32 @__CxxFrameHandler3(...)