File: mandatory_inlining_ownership.sil

package info (click to toggle)
swiftlang 6.0.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,519,992 kB
  • sloc: cpp: 9,107,863; ansic: 2,040,022; asm: 1,135,751; python: 296,500; objc: 82,456; f90: 60,502; lisp: 34,951; pascal: 19,946; sh: 18,133; perl: 7,482; ml: 4,937; javascript: 4,117; makefile: 3,840; awk: 3,535; xml: 914; fortran: 619; cs: 573; ruby: 573
file content (625 lines) | stat: -rw-r--r-- 30,117 bytes parent folder | download
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
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
// RUN: %target-sil-opt -enable-sil-verify-all %s -mandatory-inlining | %FileCheck %s

sil_stage raw

import Builtin
import Swift

class C {
  var i: Builtin.Int64
  init(i: Builtin.Int64)
}

private class C2 {
  var i: Builtin.Int64 { get set }
  init(i: Builtin.Int64)
}

class Klass {}

class AnotherKlass {}

sil [transparent] [ossa] @calleeWithGuaranteed : $@convention(thin) (@guaranteed C) -> Builtin.Int64 {
bb(%0 : @guaranteed $C):
  %1 = ref_element_addr %0 : $C, #C.i
  %2 = load [trivial] %1 : $*Builtin.Int64
  return %2 : $Builtin.Int64
}

// CHECK-LABEL: sil [ossa] @callerWithOwned : $@convention(thin) (@owned C) -> Builtin.Int64 {
// CHECK: bb0(%0 : @owned $C):
// CHECK:   [[BORROW:%.*]] = begin_borrow %0 : $C
// CHECK:   [[ADDR:%.*]] = ref_element_addr [[BORROW]] : $C, #C.i
// CHECK:   [[VAL:%.*]] = load [trivial] [[ADDR]] : $*Builtin.Int64
// CHECK:   end_borrow [[BORROW]] : $C
// CHECK:   destroy_value %0 : $C
// CHECK:   return [[VAL]] : $Builtin.Int64
// CHECK-LABEL: } // end sil function 'callerWithOwned'
sil [ossa] @callerWithOwned : $@convention(thin) (@owned C) -> Builtin.Int64 {
bb(%0 : @owned $C):
  %fn = function_ref @calleeWithGuaranteed : $@convention(thin) (@guaranteed C) -> Builtin.Int64
  %call = apply %fn(%0) : $@convention(thin) (@guaranteed C) -> Builtin.Int64
  destroy_value %0 : $C
  return %call : $Builtin.Int64
}

struct MyError : Error {}

sil [transparent] [ossa] @calleeWithGuaranteedThrows : $@convention(thin) (@guaranteed C) -> (Builtin.Int64, @error Error) {
bb(%0 : @guaranteed $C):
  %1 = ref_element_addr %0 : $C, #C.i
  %2 = load [trivial] %1 : $*Builtin.Int64
  %3 = integer_literal $Builtin.Int64, 0
  %5 = builtin "cmp_eq_Int64"(%2 : $Builtin.Int64, %3 : $Builtin.Int64) : $Builtin.Int1
  cond_br %5, bb1, bb2

bb1:
  %6 = alloc_existential_box $Error, $MyError
  throw %6 : $Error

bb2:
  return %2 : $Builtin.Int64
}

// CHECK-LABEL: sil [ossa] @callerWithThrow : $@convention(thin) (@owned C) -> (Builtin.Int64, @error any Error) {
// CHECK: bb0(%0 : @owned $C):
// CHECK:   [[BORROW:%.*]] = begin_borrow %0 : $C
// CHECK:   [[ADDR:%.*]] = ref_element_addr [[BORROW]] : $C, #C.i
// CHECK:   [[VAL:%.*]] = load [trivial] [[ADDR]] : $*Builtin.Int64
// CHECK: bb{{.*}}:
// CHECK:   [[ERR:%.*]] = alloc_existential_box $any Error, $MyError
// CHECK:   end_borrow [[BORROW]] : $C
// CHECK:   destroy_value %0 : $C
// CHECK:   throw [[ERR]] : $any Error
// CHECK: bb{{.*}}:
// CHECK:   end_borrow [[BORROW]] : $C
// CHECK:   destroy_value %0 : $C
// CHECK:   return [[VAL]] : $Builtin.Int64
// CHECK-LABEL: } // end sil function 'callerWithThrow'
sil [ossa] @callerWithThrow : $@convention(thin) (@owned C) -> (Builtin.Int64, @error Error) {
bb(%0 : @owned $C):
  %fn = function_ref @calleeWithGuaranteedThrows : $@convention(thin) (@guaranteed C) -> (Builtin.Int64, @error Error)
  try_apply %fn(%0) : $@convention(thin) (@guaranteed C) -> (Builtin.Int64, @error Error), normal bb1, error bb2

bb1(%4 : $Builtin.Int64):
  destroy_value %0 : $C
  return %4 : $Builtin.Int64

bb2(%5 : @owned $Error):
  destroy_value %0 : $C
  throw %5 : $Error
}

// Partial Apply copy_value, begin_borrow

sil @nativeobject_plus : $@convention(thin) (@owned Builtin.NativeObject, @owned Builtin.NativeObject) -> @owned Builtin.NativeObject
sil @partial_apply_user : $@convention(thin) (@owned @callee_owned (@owned Builtin.NativeObject) -> @owned Builtin.NativeObject) -> ()
sil @partial_apply_user_guaranteed : $@convention(thin) (@guaranteed @callee_guaranteed (@owned Builtin.NativeObject) -> @owned Builtin.NativeObject) -> ()

sil [ossa] [transparent] @test_partial_nativeobject_baz : $@convention(thin) (@owned Builtin.NativeObject, @owned Builtin.NativeObject) -> @owned Builtin.NativeObject {
bb0(%0 : @owned $Builtin.NativeObject, %1 : @owned $Builtin.NativeObject):
  %6 = function_ref @nativeobject_plus : $@convention(thin) (@owned Builtin.NativeObject, @owned Builtin.NativeObject) -> @owned Builtin.NativeObject
  %7 = apply %6(%0, %1) : $@convention(thin) (@owned Builtin.NativeObject, @owned Builtin.NativeObject) -> @owned Builtin.NativeObject
  return %7 : $Builtin.NativeObject
}

sil [ossa] [transparent] @test_partial_nativeobject_bar : $@convention(thin) (@owned @callee_owned (@owned Builtin.NativeObject) -> @owned Builtin.NativeObject, @owned Builtin.NativeObject) -> @owned Builtin.NativeObject {
bb0(%0 : @owned $@callee_owned (@owned Builtin.NativeObject) -> @owned Builtin.NativeObject, %1 : @owned $Builtin.NativeObject):
  %7 = apply %0(%1) : $@callee_owned (@owned Builtin.NativeObject) -> @owned Builtin.NativeObject
  return %7 : $Builtin.NativeObject
}

sil [ossa] [transparent] @test_partial_nativeobject_bar_guaranteed : $@convention(thin) (@guaranteed @callee_guaranteed (@owned Builtin.NativeObject) -> @owned Builtin.NativeObject, @owned Builtin.NativeObject) -> @owned Builtin.NativeObject {
bb0(%0 : @guaranteed $@callee_guaranteed (@owned Builtin.NativeObject) -> @owned Builtin.NativeObject, %1 : @owned $Builtin.NativeObject):
  %7 = apply %0(%1) : $@callee_guaranteed (@owned Builtin.NativeObject) -> @owned Builtin.NativeObject
  return %7 : $Builtin.NativeObject
}

// CHECK-LABEL: sil [transparent] [ossa] @test_partial_nativeobject_foo : $@convention(thin) (@owned Builtin.NativeObject) -> @owned Builtin.NativeObject {
// CHECK: bb0([[ARG:%.*]] : @owned $Builtin.NativeObject):
// CHECK:   [[FN:%.*]] = function_ref @test_partial_nativeobject_baz :
// CHECK:   [[ARG_COPY:%.*]] = copy_value [[ARG]]
// CHECK:   [[ARG_COPY_2:%.*]] = copy_value [[ARG_COPY]]
// CHECK:   [[PAI:%.*]] = partial_apply [[FN]]([[ARG_COPY]])
// CHECK:   [[ARG_COPY_3:%.*]] = copy_value [[ARG]]
// CHECK:   [[MOVE_ARG_COPY_3:%[^,]+]] = move_value [lexical] [[ARG_COPY_3]]
// CHECK:   [[MOVE_ARG_COPY_2:%[^,]+]] = move_value [lexical] [[ARG_COPY_2]]
// CHECK:   [[FN2:%.*]] = function_ref @nativeobject_plus :
// CHECK:   [[RESULT:%.*]] = apply [[FN2]]([[MOVE_ARG_COPY_3]], [[MOVE_ARG_COPY_2]])
// CHECK:   [[PAI_COPY:%.*]] = copy_value [[PAI]]
// CHECK:   [[OPAQUE_FN:%.*]] = function_ref @partial_apply_user
// CHECK:   apply [[OPAQUE_FN]]([[PAI_COPY]])
// CHECK:   destroy_value [[PAI]]
// CHECK:   destroy_value [[ARG]]
// CHECK:   return [[RESULT]]
// CHECK: } // end sil function 'test_partial_nativeobject_foo'
sil [transparent] [ossa] @test_partial_nativeobject_foo : $@convention(thin) (@owned Builtin.NativeObject) -> @owned Builtin.NativeObject {
bb0(%0 : @owned $Builtin.NativeObject):
  %2 = function_ref @test_partial_nativeobject_bar : $@convention(thin) (@owned @callee_owned (@owned Builtin.NativeObject) -> @owned Builtin.NativeObject, @owned Builtin.NativeObject) -> @owned Builtin.NativeObject
  %3 = function_ref @test_partial_nativeobject_baz : $@convention(thin) (@owned Builtin.NativeObject, @owned Builtin.NativeObject) -> @owned Builtin.NativeObject
  br bb1

bb1:
  %0copy1 = copy_value %0 : $Builtin.NativeObject
  %5 = partial_apply %3(%0copy1) : $@convention(thin) (@owned Builtin.NativeObject, @owned Builtin.NativeObject) -> @owned Builtin.NativeObject
  br bb2

bb2:
  %0copy2 = copy_value %0 : $Builtin.NativeObject
  %5copy1 = copy_value %5 : $@callee_owned (@owned Builtin.NativeObject) -> @owned Builtin.NativeObject
  %13 = apply %2(%5copy1, %0copy2) : $@convention(thin) (@owned @callee_owned (@owned Builtin.NativeObject) -> @owned Builtin.NativeObject, @owned Builtin.NativeObject) -> @owned Builtin.NativeObject
  br bb3

bb3:
  %5copy2 = copy_value %5 : $@callee_owned (@owned Builtin.NativeObject) -> @owned Builtin.NativeObject
  %15 = function_ref @partial_apply_user : $@convention(thin) (@owned @callee_owned (@owned Builtin.NativeObject) -> @owned Builtin.NativeObject) -> ()
  apply %15(%5copy2) : $@convention(thin) (@owned @callee_owned (@owned Builtin.NativeObject) -> @owned Builtin.NativeObject) -> ()
  destroy_value %5 : $@callee_owned (@owned Builtin.NativeObject) -> @owned Builtin.NativeObject
  destroy_value %0 : $Builtin.NativeObject
  return %13 : $Builtin.NativeObject
}

// Make sure we do not eliminate the actual closure but do the inlining.
//
// CHECK-LABEL: sil [transparent] [ossa] @test_partial_nativeobject_foo_guaranteed_explicit_borrow : $@convention(thin) (@owned Builtin.NativeObject) -> @owned Builtin.NativeObject {
// CHECK: [[PAI:%.*]] = partial_apply [callee_guaranteed]
// CHECK: [[NATIVEOBJECT_PLUS_FUNC:%.*]] = function_ref @nativeobject_plus : $@convention(thin) (@owned Builtin.NativeObject, @owned Builtin.NativeObject) -> @owned Builtin.NativeObject
// CHECK-NEXT: apply [[NATIVEOBJECT_PLUS_FUNC]](
// CHECK: } // end sil function 'test_partial_nativeobject_foo_guaranteed_explicit_borrow'
sil [transparent] [ossa] @test_partial_nativeobject_foo_guaranteed_explicit_borrow : $@convention(thin) (@owned Builtin.NativeObject) -> @owned Builtin.NativeObject {
bb0(%0 : @owned $Builtin.NativeObject):
  %2 = function_ref @test_partial_nativeobject_bar_guaranteed : $@convention(thin) (@guaranteed @callee_guaranteed (@owned Builtin.NativeObject) -> @owned Builtin.NativeObject, @owned Builtin.NativeObject) -> @owned Builtin.NativeObject
  %3 = function_ref @test_partial_nativeobject_baz : $@convention(thin) (@owned Builtin.NativeObject, @owned Builtin.NativeObject) -> @owned Builtin.NativeObject
  br bb1

bb1:
  %0copy1 = copy_value %0 : $Builtin.NativeObject
  %5 = partial_apply [callee_guaranteed] %3(%0copy1) : $@convention(thin) (@owned Builtin.NativeObject, @owned Builtin.NativeObject) -> @owned Builtin.NativeObject
  %6 = begin_borrow %5 : $@callee_guaranteed (@owned Builtin.NativeObject) -> @owned Builtin.NativeObject
  br bb2

bb2:
  %0copy2 = copy_value %0 : $Builtin.NativeObject
  %13 = apply %2(%6, %0copy2) : $@convention(thin) (@guaranteed @callee_guaranteed (@owned Builtin.NativeObject) -> @owned Builtin.NativeObject, @owned Builtin.NativeObject) -> @owned Builtin.NativeObject
  end_borrow %6 : $@callee_guaranteed (@owned Builtin.NativeObject) -> @owned Builtin.NativeObject
  br bb3

bb3:
  %15 = function_ref @partial_apply_user_guaranteed : $@convention(thin) (@guaranteed @callee_guaranteed (@owned Builtin.NativeObject) -> @owned Builtin.NativeObject) -> ()
  apply %15(%5) : $@convention(thin) (@guaranteed @callee_guaranteed (@owned Builtin.NativeObject) -> @owned Builtin.NativeObject) -> ()
  destroy_value %5 : $@callee_guaranteed (@owned Builtin.NativeObject) -> @owned Builtin.NativeObject
  destroy_value %0 : $Builtin.NativeObject
  return %13 : $Builtin.NativeObject
}

// Make sure we do the inlining and delete the closure.
// CHECK-LABEL: sil [transparent] [ossa] @test_partial_nativeobject_foo_guaranteed_explicit_borrow_dead_pa : $@convention(thin) (@owned Builtin.NativeObject) -> @owned Builtin.NativeObject {
// CHECK-NOT: partial_apply
// CHECK: [[NATIVEOBJECT_PLUS_FUNC:%.*]] = function_ref @nativeobject_plus : $@convention(thin) (@owned Builtin.NativeObject, @owned Builtin.NativeObject) -> @owned Builtin.NativeObject
// CHECK-NEXT: apply [[NATIVEOBJECT_PLUS_FUNC]](
// CHECK-NOT: partial_apply
// CHECK: } // end sil function 'test_partial_nativeobject_foo_guaranteed_explicit_borrow_dead_pa'
sil [transparent] [ossa] @test_partial_nativeobject_foo_guaranteed_explicit_borrow_dead_pa : $@convention(thin) (@owned Builtin.NativeObject) -> @owned Builtin.NativeObject {
bb0(%0 : @owned $Builtin.NativeObject):
  %2 = function_ref @test_partial_nativeobject_bar_guaranteed : $@convention(thin) (@guaranteed @callee_guaranteed (@owned Builtin.NativeObject) -> @owned Builtin.NativeObject, @owned Builtin.NativeObject) -> @owned Builtin.NativeObject
  %3 = function_ref @test_partial_nativeobject_baz : $@convention(thin) (@owned Builtin.NativeObject, @owned Builtin.NativeObject) -> @owned Builtin.NativeObject
  br bb1

bb1:
  %0copy1 = copy_value %0 : $Builtin.NativeObject
  %5 = partial_apply [callee_guaranteed] %3(%0copy1) : $@convention(thin) (@owned Builtin.NativeObject, @owned Builtin.NativeObject) -> @owned Builtin.NativeObject
  %6 = begin_borrow %5 : $@callee_guaranteed (@owned Builtin.NativeObject) -> @owned Builtin.NativeObject
  br bb2

bb2:
  %0copy2 = copy_value %0 : $Builtin.NativeObject
  %13 = apply %2(%6, %0copy2) : $@convention(thin) (@guaranteed @callee_guaranteed (@owned Builtin.NativeObject) -> @owned Builtin.NativeObject, @owned Builtin.NativeObject) -> @owned Builtin.NativeObject
  end_borrow %6 : $@callee_guaranteed (@owned Builtin.NativeObject) -> @owned Builtin.NativeObject
  br bb3

bb3:
  destroy_value %5 : $@callee_guaranteed (@owned Builtin.NativeObject) -> @owned Builtin.NativeObject
  destroy_value %0 : $Builtin.NativeObject
  return %13 : $Builtin.NativeObject
}

// CHECK-LABEL: sil [transparent] [ossa] @test_partial_nativeobject_foo_guaranteed_implicit_borrow : $@convention(thin) (@owned Builtin.NativeObject) -> @owned Builtin.NativeObject {
// We shouldn't delete the PAI here.
// CHECK: [[PAI:%.*]] = partial_apply [callee_guaranteed]
// CHECK: [[NATIVEOBJECT_PLUS_FUNC:%.*]] = function_ref @nativeobject_plus : $@convention(thin) (@owned Builtin.NativeObject, @owned Builtin.NativeObject) -> @owned Builtin.NativeObject
// CHECK: apply [[NATIVEOBJECT_PLUS_FUNC]](
// CHECK: } // end sil function 'test_partial_nativeobject_foo_guaranteed_implicit_borrow'
sil [transparent] [ossa] @test_partial_nativeobject_foo_guaranteed_implicit_borrow : $@convention(thin) (@owned Builtin.NativeObject) -> @owned Builtin.NativeObject {
bb0(%0 : @owned $Builtin.NativeObject):
  %2 = function_ref @test_partial_nativeobject_bar_guaranteed : $@convention(thin) (@guaranteed @callee_guaranteed (@owned Builtin.NativeObject) -> @owned Builtin.NativeObject, @owned Builtin.NativeObject) -> @owned Builtin.NativeObject
  %3 = function_ref @test_partial_nativeobject_baz : $@convention(thin) (@owned Builtin.NativeObject, @owned Builtin.NativeObject) -> @owned Builtin.NativeObject
  br bb1

bb1:
  %0copy1 = copy_value %0 : $Builtin.NativeObject
  %5 = partial_apply [callee_guaranteed] %3(%0copy1) : $@convention(thin) (@owned Builtin.NativeObject, @owned Builtin.NativeObject) -> @owned Builtin.NativeObject
  br bb2

bb2:
  %0copy2 = copy_value %0 : $Builtin.NativeObject
  %13 = apply %2(%5, %0copy2) : $@convention(thin) (@guaranteed @callee_guaranteed (@owned Builtin.NativeObject) -> @owned Builtin.NativeObject, @owned Builtin.NativeObject) -> @owned Builtin.NativeObject
  br bb3

bb3:
  %15 = function_ref @partial_apply_user_guaranteed : $@convention(thin) (@guaranteed @callee_guaranteed (@owned Builtin.NativeObject) -> @owned Builtin.NativeObject) -> ()
  apply %15(%5) : $@convention(thin) (@guaranteed @callee_guaranteed (@owned Builtin.NativeObject) -> @owned Builtin.NativeObject) -> ()
  destroy_value %5 : $@callee_guaranteed (@owned Builtin.NativeObject) -> @owned Builtin.NativeObject
  destroy_value %0 : $Builtin.NativeObject
  return %13 : $Builtin.NativeObject
}

sil [transparent] [ossa] @term_ossa_checked_cast_addr_br_takealways_callee : $@convention(thin) (@owned Builtin.NativeObject) -> () {
bb0(%0 : @owned $Builtin.NativeObject):
  %1 = alloc_stack $Builtin.NativeObject
  %2 = alloc_stack $Klass
  store %0 to [init] %1 : $*Builtin.NativeObject
  checked_cast_addr_br take_always Builtin.NativeObject in %1 : $*Builtin.NativeObject to Klass in %2 : $*Klass, bb1, bb2

bb1:
  destroy_addr %2 : $*Klass
  br bb3

bb2:
  br bb3

bb3:
  dealloc_stack %2 : $*Klass
  dealloc_stack %1 : $*Builtin.NativeObject
  %9999 = tuple()
  return %9999 : $()
}

sil [transparent] [ossa] @term_ossa_checked_cast_addr_br_takealways_callee2 : $@convention(thin) (@in @thick Klass.Type) -> () {
bb0(%0 : $*@thick Klass.Type):
  %2 = alloc_stack $AnotherKlass
  checked_cast_addr_br take_always Klass in %0 : $*@thick Klass.Type to Klass in %2 : $*AnotherKlass, bb1, bb2

bb1:
  destroy_addr %2 : $*AnotherKlass
  br bb3

bb2:
  br bb3

bb3:
  dealloc_stack %2 : $*AnotherKlass
  %9999 = tuple()
  return %9999 : $()
}

// CHECK-LABEL: sil [ossa] @term_nonossa_checked_cast_addr_br_takealways_caller : $@convention(thin) (@owned Builtin.NativeObject) -> () {
// CHECK: bb0([[ARG:%.*]] :
// CHECK-NEXT:   [[ARG_COPY:%.*]] = copy_value [[ARG]]
// CHECK-NEXT:   [[ARG_MOVE:%[^,]+]] = move_value [lexical] [[ARG_COPY]] : $Builtin.NativeObject
// CHECK-NEXT:   [[SRC_ADDR:%.*]] = alloc_stack $Builtin.NativeObject
// CHECK-NEXT:   [[DEST_ADDR:%.*]] = alloc_stack $Klass
// CHECK-NEXT:   store [[ARG_MOVE]] to [init] [[SRC_ADDR]]
// CHECK-NEXT:   [[RELOADED_ARG:%.*]] = load [take] [[SRC_ADDR]]
// CHECK-NEXT:   checked_cast_br Builtin.NativeObject in [[RELOADED_ARG]] : $Builtin.NativeObject to Klass, [[SUCCESS_BB:bb[0-9]+]], [[FAILURE_BB:bb[0-9]+]]
//
// ==> On success, we store the value into dest. The destroy is not from the
// ==> optimizer, but from the code.
// CHECK: [[SUCCESS_BB]]([[CAST_VALUE:%.*]] :
// CHECK-NEXT:   store [[CAST_VALUE]] to [init] [[DEST_ADDR]]
// CHECK-NEXT:   destroy_addr [[DEST_ADDR]]
// CHECK-NEXT:   br [[CONT_BB:bb[0-9]+]]
//
// ==> take_always implies we destroy in failure
// CHECK: [[FAILURE_BB]]([[FAILURE_ARG:%.*]] :
// CHECK-NEXT:   destroy_value [[FAILURE_ARG]]
// CHECK-NEXT:   br [[CONT_BB]]
//
// CHECK: [[CONT_BB]]:
// CHECK:   destroy_value [[ARG]]
// CHECK: } // end sil function 'term_nonossa_checked_cast_addr_br_takealways_caller'
sil [ossa] @term_nonossa_checked_cast_addr_br_takealways_caller : $@convention(thin) (@owned Builtin.NativeObject) -> () {
bb0(%0 : @owned $Builtin.NativeObject):
  %3 = function_ref @term_ossa_checked_cast_addr_br_takealways_callee : $@convention(thin) (@owned Builtin.NativeObject) -> ()
  %1 = copy_value %0 : $Builtin.NativeObject
  apply %3(%1) : $@convention(thin) (@owned Builtin.NativeObject) -> ()
  destroy_value %0 : $Builtin.NativeObject
  %9999 = tuple()
  return %9999 : $()
}

sil [ossa] @get_klass_type : $@convention(thin) () -> @out @thick Klass.Type

// CHECK-LABEL: sil [ossa] @term_nonossa_checked_cast_addr_br_takealways_caller2 :
// CHECK: checked_cast_br Klass in {{.*}} : $@thick Klass.Type to Klass, [[SUCCESS_BB:bb[0-9]+]], [[FAILURE_BB:bb[0-9]+]], forwarding: @owned
// CHECK: [[SUCCESS_BB]]({{.*}}@owned $AnotherKlass):
// CHECK: [[FAILURE_BB]]([[FAILURE_ARG:%.*]] :
// CHECK: } // end sil function 'term_nonossa_checked_cast_addr_br_takealways_caller2'
sil [ossa] @term_nonossa_checked_cast_addr_br_takealways_caller2 : $@convention(thin) () -> () {
bb0:
  %res = alloc_stack $@thick Klass.Type
  %2 = function_ref @get_klass_type : $@convention(thin) () -> @out @thick Klass.Type
  apply %2(%res) : $@convention(thin) () -> @out @thick Klass.Type
  %3 = function_ref @term_ossa_checked_cast_addr_br_takealways_callee2 : $@convention(thin) (@in @thick Klass.Type) -> ()
  apply %3(%res) : $@convention(thin) (@in @thick Klass.Type) -> ()
  dealloc_stack %res : $*@thick Klass.Type
  %9999 = tuple()
  return %9999 : $()
}

sil [transparent] [ossa] @term_ossa_checked_cast_addr_br_takeonsuccess_callee : $@convention(thin) (@owned Builtin.NativeObject) -> () {
bb0(%0 : @owned $Builtin.NativeObject):
  %1 = alloc_stack $Builtin.NativeObject
  %2 = alloc_stack $Klass
  store %0 to [init] %1 : $*Builtin.NativeObject
  checked_cast_addr_br take_on_success Builtin.NativeObject in %1 : $*Builtin.NativeObject to Klass in %2 : $*Klass, bb1, bb2

bb1:
  destroy_addr %2 : $*Klass
  br bb3

bb2:
  destroy_addr %1 : $*Builtin.NativeObject
  br bb3

bb3:
  dealloc_stack %2 : $*Klass
  dealloc_stack %1 : $*Builtin.NativeObject
  %9999 = tuple()
  return %9999 : $()
}

// CHECK-LABEL: sil [ossa] @checked_cast_addr_br_takeonsuccess_caller : $@convention(thin) (@owned Builtin.NativeObject) -> () {
// CHECK: bb0([[ARG:%.*]] :
// CHECK-NEXT:   [[ARG_COPY:%.*]] = copy_value [[ARG]]
// CHECK-NEXT:   [[ARG_MOVE:%[^,]+]] = move_value [lexical] [[ARG_COPY]] : $Builtin.NativeObject
// CHECK-NEXT:   [[SRC_ADDR:%.*]] = alloc_stack $Builtin.NativeObject
// CHECK-NEXT:   [[DEST_ADDR:%.*]] = alloc_stack $Klass
// CHECK-NEXT:   store [[ARG_MOVE]] to [init] [[SRC_ADDR]]
// CHECK-NEXT:   [[RELOADED_ARG:%.*]] = load [take] [[SRC_ADDR]]
// CHECK-NEXT:   checked_cast_br Builtin.NativeObject in [[RELOADED_ARG]] : $Builtin.NativeObject to Klass, [[SUCCESS_BB:bb[0-9]+]], [[FAILURE_BB:bb[0-9]+]]
//
// CHECK: [[SUCCESS_BB]]([[CAST_VALUE:%.*]] :
// ==> On success, we store into dest and destroy dest.
// CHECK-NEXT:   store [[CAST_VALUE]] to [init] [[DEST_ADDR]]
// CHECK-NEXT:   destroy_addr [[DEST_ADDR]]
// CHECK-NEXT:   br [[CONT_BB:bb[0-9]+]]
//
// ==> Since we are doing a take on success and we failed... store the original
// ==> value back into the memory slot.
// CHECK: [[FAILURE_BB]]([[FAIL_ARG:%.*]] :
// CHECK-NEXT:   store [[FAIL_ARG]] to [init] [[SRC_ADDR]]
// CHECK-NEXT:   destroy_addr [[SRC_ADDR]]
// CHECK-NEXT:   br [[CONT_BB]]
//
// CHECK: } // end sil function 'checked_cast_addr_br_takeonsuccess_caller'
sil [ossa] @checked_cast_addr_br_takeonsuccess_caller : $@convention(thin) (@owned Builtin.NativeObject) -> () {
bb0(%0 : @owned $Builtin.NativeObject):
  %2 = function_ref @term_ossa_checked_cast_addr_br_takeonsuccess_callee : $@convention(thin) (@owned Builtin.NativeObject) -> ()
  %1 = copy_value %0 : $Builtin.NativeObject
  apply %2(%1) : $@convention(thin) (@owned Builtin.NativeObject) -> ()
  destroy_value %0 : $Builtin.NativeObject
  %9999 = tuple()
  return %9999 : $()
}

sil [transparent] [ossa] @term_ossa_checked_cast_addr_br_copyonsuccess_callee : $@convention(thin) (@owned Builtin.NativeObject) -> () {
bb0(%0 : @owned $Builtin.NativeObject):
  %1 = alloc_stack $Builtin.NativeObject
  %2 = alloc_stack $Klass
  store %0 to [init] %1 : $*Builtin.NativeObject
  checked_cast_addr_br copy_on_success Builtin.NativeObject in %1 : $*Builtin.NativeObject to Klass in %2 : $*Klass, bb1, bb2

bb1:
  destroy_addr %2 : $*Klass
  destroy_addr %1 : $*Builtin.NativeObject
  br bb3

bb2:
  destroy_addr %1 : $*Builtin.NativeObject
  br bb3

bb3:
  dealloc_stack %2 : $*Klass
  dealloc_stack %1 : $*Builtin.NativeObject
  %9999 = tuple()
  return %9999 : $()
}

// CHECK-LABEL: sil [ossa] @checked_cast_addr_br_copyonsuccess_caller : $@convention(thin) (@owned Builtin.NativeObject) -> () {
// CHECK: bb0([[ARG:%.*]] :
// CHECK-NEXT:   [[ARG_COPY:%.*]] = copy_value [[ARG]]
// CHECK-NEXT:   [[ARG_MOVE:%[^,]+]] = move_value [lexical] [[ARG_COPY]]
// CHECK-NEXT:   [[SRC_ADDR:%.*]] = alloc_stack $Builtin.NativeObject
// CHECK-NEXT:   [[DEST_ADDR:%.*]] = alloc_stack $Klass
// CHECK-NEXT:   store [[ARG_MOVE]] to [init] [[SRC_ADDR]]
// CHECK-NEXT:   [[RELOADED_ARG:%.*]] = load_borrow [[SRC_ADDR]]
// CHECK-NEXT:   checked_cast_br Builtin.NativeObject in [[RELOADED_ARG]] : $Builtin.NativeObject to Klass, [[SUCCESS_BB:bb[0-9]+]], [[FAILURE_BB:bb[0-9]+]]
//
// CHECK: [[SUCCESS_BB]]([[CAST_VALUE:%.*]] : @guaranteed
// CHECK-NEXT:   [[CAST_VALUE_COPY:%.*]] = copy_value [[CAST_VALUE]]
// CHECK-NEXT:   end_borrow [[RELOADED_ARG]]
// CHECK-NEXT:   store [[CAST_VALUE_COPY]] to [init] [[DEST_ADDR]]
// CHECK-NEXT:   destroy_addr [[DEST_ADDR]]
// CHECK-NEXT:   destroy_addr [[SRC_ADDR]]
// CHECK-NEXT:   br [[CONT_BB:bb[0-9]+]]
//
// CHECK: [[FAILURE_BB]]([[FAILURE_BORROWED_ARG:%.*]] :
// CHECK-NEXT:   end_borrow [[RELOADED_ARG]]
// CHECK-NEXT:   destroy_addr [[SRC_ADDR]]
// CHECK-NEXT:   br [[CONT_BB]]
//
// CHECK: } // end sil function 'checked_cast_addr_br_copyonsuccess_caller'
sil [ossa] @checked_cast_addr_br_copyonsuccess_caller : $@convention(thin) (@owned Builtin.NativeObject) -> () {
bb0(%0 : @owned $Builtin.NativeObject):
  %1 = function_ref @term_ossa_checked_cast_addr_br_copyonsuccess_callee : $@convention(thin) (@owned Builtin.NativeObject) -> ()
  %2 = copy_value %0 : $Builtin.NativeObject
  apply %1(%2) : $@convention(thin) (@owned Builtin.NativeObject) -> ()
  destroy_value %0 : $Builtin.NativeObject
  %9999 = tuple()
  return %9999 : $()
}

///////////////////////
// Begin Apply Tests //
///////////////////////

// Make sure that we do not violate any ownership invariants after inlining this
// code.

sil @get_hidden_int_field_of_klass : $@convention(method) (@guaranteed Klass) -> Builtin.Int32
sil @int_klass_pair_user : $@convention(method) (Builtin.Int32, @guaranteed Klass) -> ()

sil [transparent] [ossa] @begin_apply_callee : $@yield_once @convention(method) (@guaranteed Klass) -> @yields @inout Builtin.Int32 {
bb0(%0 : @guaranteed $Klass):
  %2 = alloc_stack $Builtin.Int32
  %3 = function_ref @get_hidden_int_field_of_klass : $@convention(method) (@guaranteed Klass) -> Builtin.Int32
  %4 = apply %3(%0) : $@convention(method) (@guaranteed Klass) -> Builtin.Int32
  store %4 to [trivial] %2 : $*Builtin.Int32
  yield %2 : $*Builtin.Int32, resume bb1, unwind bb2

bb1:
  %7 = load [trivial] %2 : $*Builtin.Int32
  %8 = function_ref @int_klass_pair_user : $@convention(method) (Builtin.Int32, @guaranteed Klass) -> ()
  %9 = apply %8(%7, %0) : $@convention(method) (Builtin.Int32, @guaranteed Klass) -> ()
  dealloc_stack %2 : $*Builtin.Int32
  %11 = tuple ()
  return %11 : $()

bb2:
  %13 = load [trivial] %2 : $*Builtin.Int32
  %14 = function_ref @int_klass_pair_user : $@convention(method) (Builtin.Int32, @guaranteed Klass) -> ()
  %15 = apply %14(%13, %0) : $@convention(method) (Builtin.Int32, @guaranteed Klass) -> ()
  dealloc_stack %2 : $*Builtin.Int32
  unwind
}

// CHECK-LABEL: sil [ossa] @begin_apply_caller : $@convention(method) (@guaranteed Klass) -> @error any Error {
// CHECK-NOT: begin_apply
// CHECK: } // end sil function 'begin_apply_caller'
sil [ossa] @begin_apply_caller : $@convention(method) (@guaranteed Klass) -> @error Error {
bb0(%0 : @guaranteed $Klass):
  %6 = copy_value %0 : $Klass
  %12 = function_ref @begin_apply_callee : $@yield_once @convention(method) (@guaranteed Klass) -> @yields @inout Builtin.Int32
  (%13, %14) = begin_apply %12(%6) : $@yield_once @convention(method) (@guaranteed Klass) -> @yields @inout Builtin.Int32
  end_apply %14
  destroy_value %6 : $Klass
  %19 = tuple ()
  return %19 : $()
}

// CHECK-LABEL: sil [ossa] @begin_apply_caller_2 : $@convention(method) (@guaranteed Klass) -> @error any Error {
// CHECK-NOT: begin_apply
// CHECK: } // end sil function 'begin_apply_caller_2'
sil [ossa] @begin_apply_caller_2 : $@convention(method) (@guaranteed Klass) -> @error Error {
bb0(%0 : @guaranteed $Klass):
  %6 = copy_value %0 : $Klass
  %12 = function_ref @begin_apply_callee : $@yield_once @convention(method) (@guaranteed Klass) -> @yields @inout Builtin.Int32
  (%13, %14) = begin_apply %12(%6) : $@yield_once @convention(method) (@guaranteed Klass) -> @yields @inout Builtin.Int32
  abort_apply %14
  destroy_value %6 : $Klass
  %19 = tuple ()
  return %19 : $()
}

// CHECK-LABEL: sil [ossa] @begin_apply_caller_3 : $@convention(method) (@guaranteed Klass) -> @error any Error {
// CHECK-NOT: begin_apply
// CHECK: } // end sil function 'begin_apply_caller_3'
sil [ossa] @begin_apply_caller_3 : $@convention(method) (@guaranteed Klass) -> @error Error {
bb0(%0 : @guaranteed $Klass):
  %6 = copy_value %0 : $Klass
  %12 = function_ref @begin_apply_callee : $@yield_once @convention(method) (@guaranteed Klass) -> @yields @inout Builtin.Int32
  (%13, %14) = begin_apply %12(%6) : $@yield_once @convention(method) (@guaranteed Klass) -> @yields @inout Builtin.Int32
  cond_br undef, bb1, bb2

bb1:
  end_apply %14
  br bb3

bb2:
  abort_apply %14
  br bb3

bb3:
  destroy_value %6 : $Klass
  %19 = tuple ()
  return %19 : $()
}

sil [ossa] [transparent] @devirt_callee : $@yield_once @convention(method) (@guaranteed C) -> @yields @inout Builtin.Int64 {
bb0(%0 : @guaranteed $C):
  %1 = alloc_stack $Builtin.Int64
  %1a = integer_literal $Builtin.Int64, 0
  store %1a to [trivial] %1 : $*Builtin.Int64
  yield %1 : $*Builtin.Int64, resume bb1, unwind bb2

bb1:
  dealloc_stack %1 : $*Builtin.Int64
  %6 = tuple ()
  return %6 : $()

bb2:
  dealloc_stack %1 : $*Builtin.Int64
  unwind
}

// Just make sure we actually inlined the begin_apply. We just want to make sure
// we are not breaking ownership invariants by not properly borrowing %0.
// CHECK-LABEL: sil [ossa] @begin_apply_devirt_caller : $@convention(method) (@owned C2) -> @error any Error {
// CHECK-NOT: begin_apply
// CHECK: } // end sil function 'begin_apply_devirt_caller'
sil [ossa] @begin_apply_devirt_caller : $@convention(method) (@owned C2) -> @error Error {
bb0(%0 : @owned $C2):
  %1 = class_method %0 : $C2, #C2.i!modify : (C2) -> () -> (), $@yield_once @convention(method) (@guaranteed C2) -> @yields @inout Builtin.Int64
  (%mem, %tok) = begin_apply %1(%0) : $@yield_once @convention(method) (@guaranteed C2) -> @yields @inout Builtin.Int64
  br bb1

bb1:
  end_apply %tok
  destroy_value %0 : $C2
  %9999 = tuple()
  return %9999 : $()
}

// CHECK-LABEL: sil [ossa] @begin_apply_devirt_caller_2 : $@convention(method) (@owned C2) -> @error any Error {
// CHECK-NOT: begin_apply
// CHECK: } // end sil function 'begin_apply_devirt_caller_2'
sil [ossa] @begin_apply_devirt_caller_2 : $@convention(method) (@owned C2) -> @error Error {
bb0(%0 : @owned $C2):
  %1 = class_method %0 : $C2, #C2.i!modify : (C2) -> () -> (), $@yield_once @convention(method) (@guaranteed C2) -> @yields @inout Builtin.Int64
  (%mem, %tok) = begin_apply %1(%0) : $@yield_once @convention(method) (@guaranteed C2) -> @yields @inout Builtin.Int64
  br bb1

bb1:
  abort_apply %tok
  destroy_value %0 : $C2
  %9999 = tuple()
  return %9999 : $()
}

// CHECK-LABEL: sil [ossa] @begin_apply_devirt_caller_3 : $@convention(method) (@owned C2) -> @error any Error {
// CHECK-NOT: begin_apply
// CHECK: } // end sil function 'begin_apply_devirt_caller_3'
sil [ossa] @begin_apply_devirt_caller_3 : $@convention(method) (@owned C2) -> @error Error {
bb0(%0 : @owned $C2):
  %1 = class_method %0 : $C2, #C2.i!modify : (C2) -> () -> (), $@yield_once @convention(method) (@guaranteed C2) -> @yields @inout Builtin.Int64
  (%mem, %tok) = begin_apply %1(%0) : $@yield_once @convention(method) (@guaranteed C2) -> @yields @inout Builtin.Int64
  cond_br undef, bb1, bb2

bb1:
  end_apply %tok
  br bb3

bb2:
  abort_apply %tok
  br bb3

bb3:
  destroy_value %0 : $C2
  %9999 = tuple()
  return %9999 : $()
}


sil_vtable C2 {
  #C2.i!modify: (C2) -> () -> () : @devirt_callee
}