File: lexical_destroy_folding.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 (869 lines) | stat: -rw-r--r-- 37,646 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
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
// RUN: %target-sil-opt -copy-propagation -enable-sil-verify-all %s | %FileCheck %s

// =============================================================================
// = DECLARATIONS                                                             {{
// =============================================================================

class C {}
class D {}

sil [ossa] @barrier : $@convention(thin) () -> ()
sil [ossa] @callee_guaranteed : $@convention(thin) (@guaranteed C) -> ()
sil [ossa] @callee_owned : $@convention(thin) (@owned C) -> ()
sil [ossa] @callee_owned_owned : $@convention(thin) (@owned C, @owned C) -> ()
sil [ossa] @callee_owned_guaranteed : $@convention(thin) (@owned C, @guaranteed C) -> ()
sil [ossa] @get_owned : $@convention(thin) () -> @owned C

// =============================================================================
// = DECLARATIONS                                                             }}
// =============================================================================

// =============================================================================
// TESTS                                                                      {{
// =============================================================================

// The simplest test for LexicalDestroyFolding.  Check that an outer lexical
// owned lexical lifetime is added around the existing inner guaranteed lexical
// lifetime.  The new lifetime should begin with a move_value [lexical] and end
// with an apply.
//
// CHECK-LABEL: sil [ossa] @fold_simplest : {{.*}} {
// CHECK:         [[GET_OWNED:%[^,]+]] = function_ref @get_owned
// CHECK:         [[INSTANCE:%[^,]+]] = apply [[GET_OWNED]]
// CHECK:         [[CALEE_OWNED:%[^,]+]] = function_ref @callee_owned
// CHECK:         [[MOVE:%[^,]+]] = move_value [lexical] [[INSTANCE]]
// CHECK:         apply [[CALEE_OWNED]]([[MOVE]])
// CHECK-LABEL: } // end sil function 'fold_simplest'
sil [ossa] @fold_simplest : $@convention(thin) () -> () {
  %get_owned = function_ref @get_owned : $@convention(thin) () -> (@owned C)
  %instance = apply %get_owned() : $@convention(thin) () -> (@owned C)
  %callee_owned = function_ref @callee_owned : $@convention(thin) (@owned C) -> ()
  %lifetime = begin_borrow [lexical] %instance : $C
  %copy = copy_value %lifetime : $C
  apply %callee_owned(%copy) : $@convention(thin) (@owned C) -> ()
  end_borrow %lifetime : $C
  destroy_value %instance : $C
  %retval = tuple ()
  return %retval : $()
}

// Fold when there is a use of %original_borrowee prior to the lexical lifetime.
sil [ossa] @fold_prior_use : $@convention(thin) (@owned C) -> () {
entry(%instance : @owned $C):
  %callee_owned = function_ref @callee_owned : $@convention(thin) (@owned C) -> ()
  %callee_guaranteed = function_ref @callee_guaranteed : $@convention(thin) (@guaranteed C) -> ()
  %lifetime = begin_borrow [lexical] %instance : $C
  %copy = copy_value %lifetime : $C
  apply %callee_owned(%copy) : $@convention(thin) (@owned C) -> ()
  end_borrow %lifetime : $C
  destroy_value %instance : $C
  %retval = tuple ()
  return %retval : $()
}

// Don't fold an apply where an owned value is passed to a guaranteed parameter.
// The callee can't end ownership.
//
// CHECK-LABEL: sil [ossa] @nofold_owned_arg_to_guaranteed_param : {{.*}} {
// CHECK-NOT: move_value [lexical]
// CHECK-LABEL: } // end sil function 'nofold_owned_arg_to_guaranteed_param'
sil [ossa] @nofold_owned_arg_to_guaranteed_param : $@convention(thin) (@owned C) -> () {
entry(%instance : @owned $C):
  %lifetime = begin_borrow [lexical] %instance : $C
  %copy = copy_value %lifetime : $C
  %callee_guaranteed = function_ref @callee_guaranteed : $@convention(thin) (@guaranteed C) -> ()
  apply %callee_guaranteed(%copy) : $@convention(thin) (@guaranteed C) -> ()
  end_borrow %lifetime : $C
  destroy_value %copy : $C
  destroy_value %instance : $C
  %retval = tuple ()
  return %retval : $()
}

// Enclose a single block guaranteed lexical scope ending in a consuming use
// and destroy within a single block owned lexical scope, leaving the original
// borrowee intact on other paths.
//
// CHECK-LABEL: sil [ossa] @fold_left_only : {{.*}} {
// CHECK:         [[GET_OWNED:%[^,]+]] = function_ref @get_owned
// CHECK:         [[INSTANCE:%[^,]+]] = apply [[GET_OWNED]]
// CHECK:         [[CALLEE_OWNED:%[^,]+]] = function_ref @callee_owned
// CHECK:         cond_br undef, [[LEFT:bb[0-9]+]], [[RIGHT:bb[0-9]+]]
// CHECK:       [[LEFT]]:
// CHECK:         [[MOVE:%[^,]+]] = move_value [lexical] [[INSTANCE]]
// CHECK:         apply [[CALLEE_OWNED]]([[MOVE]])
// CHECK:         br [[EXIT:bb[0-9]+]]
// CHECK:       [[RIGHT]]:
// CHECK:         apply [[CALLEE_OWNED]]([[INSTANCE]])
// CHECK:         br [[EXIT]]
// CHECK:       [[EXIT]]:
// CHECK-LABEL: } // end sil function 'fold_left_only'
sil [ossa] @fold_left_only : $@convention(thin) () -> () {
entry:
  %get_owned = function_ref @get_owned : $@convention(thin) () -> (@owned C)
  %instance = apply %get_owned() : $@convention(thin) () -> (@owned C)
  %callee_owned = function_ref @callee_owned : $@convention(thin) (@owned C) -> ()
  cond_br undef, left, right

left:
  %lifetime = begin_borrow [lexical] %instance : $C
  %copy = copy_value %lifetime : $C
  apply %callee_owned(%copy) : $@convention(thin) (@owned C) -> ()
  end_borrow %lifetime : $C
  destroy_value %instance : $C
  br exit

right:
  apply %callee_owned(%instance) : $@convention(thin) (@owned C) -> ()
  br exit

exit:
  %retval = tuple ()
  return %retval : $()

}

// Enclose two independent parallel guaranteed lexical scopes within two
// independents owned lexical scopes.
//
// CHECK-LABEL: sil [ossa] @fold_two_independent_scopes : {{.*}} {
// CHECK:         [[GET_OWNED:%[^,]+]] = function_ref @get_owned
// CHECK:         [[INSTANCE:%[^,]+]] = apply [[GET_OWNED]]
// CHECK:         [[CALLEE_OWNED:%[^,]+]] = function_ref @callee_owned
// CHECK:         cond_br undef, [[LEFT:bb[0-9]+]], [[RIGHT:bb[0-9]+]]
// CHECK:       [[LEFT]]:
// CHECK:         [[MOVE_LEFT:%[^,]+]] = move_value [lexical] [[INSTANCE]]
// CHECK:         apply [[CALLEE_OWNED]]([[MOVE_LEFT]])
// CHECK:         br [[EXIT:bb[0-9]+]]
// CHECK:       [[RIGHT]]:
// CHECK:         [[MOVE_RIGHT:%[^,]+]] = move_value [lexical] [[INSTANCE]]
// CHECK:         apply [[CALLEE_OWNED]]([[MOVE_RIGHT]])
// CHECK:         br [[EXIT]]
// CHECK:       [[EXIT]]:
// CHECK-LABEL: } // end sil function 'fold_two_independent_scopes'
sil [ossa] @fold_two_independent_scopes : $@convention(thin) () -> () {
entry:
  %get_owned = function_ref @get_owned : $@convention(thin) () -> (@owned C)
  %instance = apply %get_owned() : $@convention(thin) () -> (@owned C)
  %callee_owned = function_ref @callee_owned : $@convention(thin) (@owned C) -> ()
  cond_br undef, left, right

left:
  %lifetime = begin_borrow [lexical] %instance : $C
  %copy = copy_value %lifetime : $C
  apply %callee_owned(%copy) : $@convention(thin) (@owned C) -> ()
  end_borrow %lifetime : $C
  destroy_value %instance : $C
  br exit

right:
  %lifetime_1 = begin_borrow [lexical] %instance : $C
  %copy_1 = copy_value %lifetime_1 : $C
  apply %callee_owned(%copy_1) : $@convention(thin) (@owned C) -> ()
  end_borrow %lifetime_1 : $C
  destroy_value %instance : $C
  br exit

exit:
  %retval = tuple ()
  return %retval : $()

}

// Enclose a single guaranteed lexical scope that contains two parallel
// applies of owned arguments with the destroy at a NONcanonical position
// within a single owned lexical scope.
//
// CHECK-LABEL: sil [ossa] @fold_two_parallel_applies___noncanonical_destroys : {{.*}} {
// CHECK:       {{bb[0-9]+}}:
// CHECK:         [[GET_OWNED:%[^,]+]] = function_ref @get_owned
// CHECK:         [[INSTANCE:%[^,]+]] = apply [[GET_OWNED]]
// CHECK:         [[CALLEE_OWNED:%[^,]+]] = function_ref @callee_owned
// CHECK:         [[MOVE:%[^,]+]] = move_value [lexical] [[INSTANCE]]
// CHECK:         cond_br undef, [[LEFT:bb[0-9]+]], [[RIGHT:bb[0-9]+]]
// CHECK:       [[LEFT]]:
// CHECK:         apply [[CALLEE_OWNED]]([[MOVE]])
// CHECK:         br [[EXIT:bb[0-9]+]]
// CHECK:       [[RIGHT]]:
// CHECK:         apply [[CALLEE_OWNED]]([[MOVE]])
// CHECK:         br [[EXIT]]
// CHECK:       [[EXIT]]:
// CHECK-LABEL: } // end sil function 'fold_two_parallel_applies___noncanonical_destroys'
sil [ossa] @fold_two_parallel_applies___noncanonical_destroys : $@convention(thin) () -> () {
entry:
  %get_owned = function_ref @get_owned : $@convention(thin) () -> (@owned C)
  %instance = apply %get_owned() : $@convention(thin) () -> (@owned C)
  %callee_owned = function_ref @callee_owned : $@convention(thin) (@owned C) -> ()
  %lifetime = begin_borrow [lexical] %instance : $C
  cond_br undef, left, right

left:
  %copy = copy_value %lifetime : $C
  apply %callee_owned(%copy) : $@convention(thin) (@owned C) -> ()
  br exit

right:
  %copy_2 = copy_value %lifetime : $C
  apply %callee_owned(%copy_2) : $@convention(thin) (@owned C) -> ()
  br exit

exit:
  end_borrow %lifetime : $C
  destroy_value %instance : $C
  %retval = tuple ()
  return %retval : $()
}

// Enclose a single guaranteed lexical scope that contains two parallel
// applies of owned arguments with the destroy at a noncanonical position
// within a single owned lexical scope.
//
// CHECK-LABEL: sil [ossa] @fold_two_parallel_applies___canonical_destroys : {{.*}} {
// CHECK:         [[GET_OWNED:%[^,]+]] = function_ref @get_owned
// CHECK:         [[INSTANCE:%[^,]+]] = apply [[GET_OWNED]]
// CHECK:         [[CALLEE_OWNED:%[^,]+]] = function_ref @callee_owned
// CHECK:         [[MOVE:%[^,]+]] = move_value [lexical] [[INSTANCE]] : $C
// CHECK:         cond_br undef, [[LEFT:bb[0-9]+]], [[RIGHT:bb[0-9]+]]
// CHECK:       [[LEFT:bb[0-9]+]]:
// CHECK:         apply [[CALLEE_OWNED]]([[MOVE]])
// CHECK:         br [[EXIT:bb[0-9]+]]
// CHECK:       [[RIGHT]]:
// CHECK:         apply [[CALLEE_OWNED]]([[MOVE]])
// CHECK:         br [[EXIT]]
// CHECK:       [[EXIT]]:
// CHECK-LABEL: } // end sil function 'fold_two_parallel_applies___canonical_destroys'
sil [ossa] @fold_two_parallel_applies___canonical_destroys : $@convention(thin) () -> () {
entry:
  %get_owned = function_ref @get_owned : $@convention(thin) () -> (@owned C)
  %instance = apply %get_owned() : $@convention(thin) () -> (@owned C)
  %callee_owned = function_ref @callee_owned : $@convention(thin) (@owned C) -> ()
  %lifetime = begin_borrow [lexical] %instance : $C
  cond_br undef, left, right

left:
  %copy = copy_value %lifetime : $C
  apply %callee_owned(%copy) : $@convention(thin) (@owned C) -> ()
  end_borrow %lifetime : $C
  destroy_value %instance : $C
  br exit

right:
  %copy_2 = copy_value %lifetime : $C
  apply %callee_owned(%copy_2) : $@convention(thin) (@owned C) -> ()
  end_borrow %lifetime : $C
  destroy_value %instance : $C
  br exit

exit:
  %retval = tuple ()
  return %retval : $()
}

// Enclose a guaranteed lexical scope within an owned lexical scope when it
// appears in a multiblock subgraph that is parallel to another apply use of the
// borrowee that is not within the scope.
//
// CHECK-LABEL: sil [ossa] @fold_multiblock_borrow_single_apply_with_parallel_use : {{.*}} {
// CHECK:         [[GET_OWNED:%[^,]+]] = function_ref @get_owned
// CHECK:         [[INSTANCE:%[^,]+]] = apply [[GET_OWNED]]
// CHECK:         [[CALLEE_OWNED:%[^,]+]] = function_ref @callee_owned
// CHECK:         cond_br undef, [[LEFT1:bb[0-9]+]], [[RIGHT:bb[0-9]+]]                         
// CHECK:       [[LEFT1]]:                                              
// CHECK:         [[MOVE:%[^,]+]] = move_value [lexical] [[INSTANCE]]
// CHECK:         cond_br undef, [[LEFT2:bb[0-9]+]], [[LEFT3:bb[0-9]+]]                         
// CHECK:       [[LEFT2]]:                                              
// CHECK:         apply [[CALLEE_OWNED]]([[MOVE]])
// CHECK:         br [[EXIT:bb[0-9]+]]                                          
// CHECK:       [[LEFT3]]:                                              
// CHECK:         destroy_value [[MOVE]]
// CHECK:         br [[EXIT]]                                          
// CHECK:       [[RIGHT]]:                                              
// CHECK:         apply [[CALLEE_OWNED]]([[INSTANCE]])
// CHECK:         br [[EXIT]]                                          
// CHECK:       [[EXIT]]:                                              
// CHECK-LABEL: } // end sil function 'fold_multiblock_borrow_single_apply_with_parallel_use'
sil [ossa] @fold_multiblock_borrow_single_apply_with_parallel_use : $@convention(thin) () -> () {
entry:
  %get_owned = function_ref @get_owned : $@convention(thin) () -> (@owned C)
  %instance = apply %get_owned() : $@convention(thin) () -> (@owned C)
  %callee_owned = function_ref @callee_owned : $@convention(thin) (@owned C) -> ()
  cond_br undef, left1, right

left1:
  %lifetime = begin_borrow [lexical] %instance : $C
  %copy = copy_value %lifetime : $C
  cond_br undef, left2, left3

left2:
  apply %callee_owned(%copy) : $@convention(thin) (@owned C) -> ()
  end_borrow %lifetime : $C
  destroy_value %instance : $C
  br exit

left3:
  destroy_value %copy : $C
  end_borrow %lifetime : $C
  destroy_value %instance : $C
  br exit

right:
  apply %callee_owned(%instance) : $@convention(thin) (@owned C) -> ()
  br exit

exit:
  %retval = tuple ()
  return %retval : $()
}

// Enclose a guaranteed lexical scope within an owned lexical scope when it
// appears in a multiblock subgraph that is parallel to another apply use of the
// borrowee that is not within the scope.
//
// CHECK-LABEL: sil [ossa] @fold_multiblock_borrow_single_apply_and_parallel_singleblock_borrow : {{.*}} {
// CHECK:         [[GET_OWNED:%[^,]+]] = function_ref @get_owned
// CHECK:         [[INSTANCE:%[^,]+]] = apply [[GET_OWNED]]
// CHECK:         [[CALLEE_OWNED:%[^,]+]] = function_ref @callee_owned
// CHECK:         cond_br undef, [[LEFT1:bb[0-9]+]], [[RIGHT:bb[0-9]+]]                         
// CHECK:       [[LEFT1]]:                                              
// CHECK:         [[MOVE:%[^,]+]] = move_value [lexical] [[INSTANCE]]
// CHECK:         cond_br undef, [[LEFT2:bb[0-9]+]], [[LEFT3:bb[0-9]+]]                         
// CHECK:       [[LEFT2]]:                                              
// CHECK:         apply [[CALLEE_OWNED]]([[MOVE]])
// CHECK:         br [[EXIT:bb[0-9]+]]                                          
// CHECK:       [[LEFT3]]:                                              
// CHECK:         destroy_value [[MOVE]]
// CHECK:         br [[EXIT]]                                          
// CHECK:       [[RIGHT]]:                                              
// CHECK:         [[MOVE_2:%[^,]+]] = move_value [lexical] [[INSTANCE]]
// CHECK:         apply [[CALLEE_OWNED]]([[MOVE_2]])
// CHECK:         br [[EXIT]]                                          
// CHECK:       [[EXIT]]:                                              
// CHECK-LABEL: } // end sil function 'fold_multiblock_borrow_single_apply_and_parallel_singleblock_borrow'
sil [ossa] @fold_multiblock_borrow_single_apply_and_parallel_singleblock_borrow : $@convention(thin) () -> () {
entry:
  %get_owned = function_ref @get_owned : $@convention(thin) () -> (@owned C)
  %instance = apply %get_owned() : $@convention(thin) () -> (@owned C)
  %callee_owned = function_ref @callee_owned : $@convention(thin) (@owned C) -> ()
  cond_br undef, left1, right

left1:
  %lifetime = begin_borrow [lexical] %instance : $C
  %copy = copy_value %lifetime : $C
  cond_br undef, left2, left3

left2:
  apply %callee_owned(%copy) : $@convention(thin) (@owned C) -> ()
  end_borrow %lifetime : $C
  destroy_value %instance : $C
  br exit

left3:
  destroy_value %copy : $C
  end_borrow %lifetime : $C
  destroy_value %instance : $C
  br exit

right:
  %lifetime_1 = begin_borrow [lexical] %instance : $C
  %copy_1 = copy_value %lifetime_1 : $C
  apply %callee_owned(%copy_1) : $@convention(thin) (@owned C) -> ()
  end_borrow %lifetime_1 : $C
  destroy_value %instance : $C
  br exit

exit:
  %retval = tuple ()
  return %retval : $()

}

// Fold with an apply that takes the borrowee @owned twice and no use of the
// guaranteed lifetime remains.
//
// CHECK-LABEL: sil [ossa] @fold_single_block_one_apply_double_use_all_owned : {{.*}} {
// CHECK:         [[GET_OWNED:%[^,]+]] = function_ref @get_owned
// CHECK:         [[INSTANCE:%[^,]+]] = apply [[GET_OWNED]]
// CHECK:         [[LIFETIME:%[^,]+]] = move_value [lexical] [[INSTANCE]]
// CHECK:         [[LIFETIME_GUARANTEED:%[^,]+]] = begin_borrow [lexical] [[LIFETIME]]
// CHECK:         [[CALLEE_GUARANTEED:%[^,]+]] = function_ref @callee_guaranteed
// CHECK:         apply [[CALLEE_GUARANTEED]]([[LIFETIME_GUARANTEED]])
// CHECK:         end_borrow [[LIFETIME_GUARANTEED]]
// CHECK:         [[COPY:%[^,]+]] = copy_value [[LIFETIME]]
// CHECK:         [[CALLEE_OWNED_OWNED:%[^,]+]] = function_ref @callee_owned_owned
// CHECK:         apply [[CALLEE_OWNED_OWNED]]([[LIFETIME]], [[COPY]])
// CHECK-LABEL: } // end sil function 'fold_single_block_one_apply_double_use_all_owned'
sil [ossa] @fold_single_block_one_apply_double_use_all_owned : $@convention(thin) () -> () {
entry:
  %get_owned = function_ref @get_owned : $@convention(thin) () -> (@owned C)
  %instance = apply %get_owned() : $@convention(thin) () -> (@owned C)
  %lifetime = begin_borrow [lexical] %instance : $C
  %callee_guaranteed = function_ref @callee_guaranteed : $@convention(thin) (@guaranteed C) -> ()
  apply %callee_guaranteed(%lifetime) : $@convention(thin) (@guaranteed C) -> ()
  %copy_1 = copy_value %lifetime : $C
  %copy_2 = copy_value %lifetime : $C
  %callee_owned_owned = function_ref @callee_owned_owned : $@convention(thin) (@owned C, @owned C) -> ()
  apply %callee_owned_owned(%copy_1, %copy_2) : $@convention(thin) (@owned C, @owned C) -> ()
  end_borrow %lifetime : $C
  destroy_value %instance : $C
  %retval = tuple ()
  return %retval : $()
}

// Fold with an apply that takes the borrowee @owned twice and a use of the
// guaranteed lifetime remains.
//
// CHECK-LABEL: sil [ossa] @fold_single_block_one_apply_double_use_all_owned__no_remaining_uses : {{.*}} {
// CHECK:         [[GET_OWNED:%[^,]+]] = function_ref @get_owned
// CHECK:         [[INSTANCE:%[^,]+]] = apply [[GET_OWNED]]
// CHECK:         [[MOVE:%[^,]+]] = move_value [lexical] [[INSTANCE]]
// CHECK:         [[COPY:%[^,]+]] = copy_value [[MOVE]]
// CHECK:         [[CALLEE_OWNED_OWNED:%[^,]+]] = function_ref @callee_owned_owned
// CHECK:         apply [[CALLEE_OWNED_OWNED]]([[MOVE]], [[COPY]])
// CHECK-LABEL: } // end sil function 'fold_single_block_one_apply_double_use_all_owned__no_remaining_uses'
sil [ossa] @fold_single_block_one_apply_double_use_all_owned__no_remaining_uses : $@convention(thin) () -> () {
entry:
  %get_owned = function_ref @get_owned : $@convention(thin) () -> (@owned C)
  %instance = apply %get_owned() : $@convention(thin) () -> (@owned C)
  %lifetime = begin_borrow [lexical] %instance : $C
  %copy_1 = copy_value %lifetime : $C
  %copy_2 = copy_value %lifetime : $C
  %callee_owned_owned = function_ref @callee_owned_owned : $@convention(thin) (@owned C, @owned C) -> ()
  apply %callee_owned_owned(%copy_1, %copy_2) : $@convention(thin) (@owned C, @owned C) -> ()
  end_borrow %lifetime : $C
  destroy_value %instance : $C
  %retval = tuple ()
  return %retval : $()
} // end sil function 'test'

// Do NOT fold with an apply that takes both a copy of the borrow @owned and
// also the borrow itself @guaranteed.
//
// CHECK-LABEL: sil [ossa] @nofold_single_block_one_apply_double_use_one_guaranteed : {{.*}} {
// CHECK-NOT: move_value [lexical]
// CHECK-LABEL: } // end sil function 'nofold_single_block_one_apply_double_use_one_guaranteed'
sil [ossa] @nofold_single_block_one_apply_double_use_one_guaranteed : $@convention(thin) (@owned C) -> () {
entry(%instance : @owned $C):
  %lifetime = begin_borrow [lexical] %instance : $C
  %copy = copy_value %lifetime : $C
  %callee_owned_guaranteed = function_ref @callee_owned_guaranteed : $@convention(thin) (@owned C, @guaranteed C) -> ()
  apply %callee_owned_guaranteed(%copy, %lifetime) : $@convention(thin) (@owned C, @guaranteed C) -> ()
  end_borrow %lifetime : $C
  destroy_value %instance : $C
  %retval = tuple ()
  return %retval : $()
}

// Do NOT fold an apply of an owned value when the borrowee is guaranteed.
//
// CHECK-LABEL: sil [ossa] @nofold_guaranteed_borrowee : {{.*}} {
// CHECK-NOT: move_value [lexical]
// CHECK-LABEL: } // end sil function 'nofold_guaranteed_borrowee'
sil [ossa] @nofold_guaranteed_borrowee : $@convention(thin) (@guaranteed C) -> () {
entry(%instance : @guaranteed $C):
  %lifetime = begin_borrow [lexical] %instance : $C
  %callee_owned = function_ref @callee_owned : $@convention(thin) (@owned C) -> ()
  %copy = copy_value %lifetime : $C
  apply %callee_owned(%copy) : $@convention(thin) (@owned C) -> ()
  end_borrow %lifetime : $C
  %retval = tuple ()
  return %retval : $()
}

// CHECK-LABEL: sil [ossa] @nofold_nonlexical : {{.*}} {
// CHECK-NOT: move_value [lexical]
// CHECK-LABEL: } // end sil function 'nofold_nonlexical'
sil [ossa] @nofold_nonlexical : $@convention(thin) (@owned C) -> () {
entry(%instance : @owned $C):
  %callee_owned = function_ref @callee_owned : $@convention(thin) (@owned C) -> ()
  %lifetime = begin_borrow %instance : $C
  %copy = copy_value %lifetime : $C
  apply %callee_owned(%copy) : $@convention(thin) (@owned C) -> ()
  end_borrow %lifetime : $C
  destroy_value %instance : $C
  %retval = tuple ()
  return %retval : $()
}

// Fold with the last consuming use but not with a previous consuming use.
//
// CHECK-LABEL: sil [ossa] @fold_single_block_two_applies_first_only : {{.*}} {
// CHECK:         [[GET_OWNED:%[^,]+]] = function_ref @get_owned
// CHECK:         [[INSTANCE:%[^,]+]] = apply [[GET_OWNED]]
// CHECK:         [[MOVE:%[^,]+]] = move_value [lexical] [[INSTANCE]]
// CHECK:         [[LIFETIME:%[^,]+]] = begin_borrow [lexical] [[MOVE]]
// CHECK:         [[CALLEE_OWNED:%[^,]+]] = function_ref @callee_owned
// CHECK:         [[COPY:%[^,]+]] = copy_value [[LIFETIME]]
// CHECK:         apply [[CALLEE_OWNED]]([[COPY]])
// CHECK:         end_borrow [[LIFETIME]]
// CHECK:         tuple ()
// CHECK:         apply [[CALLEE_OWNED]]([[MOVE]])
// CHECK-LABEL: } // end sil function 'fold_single_block_two_applies_first_only'
sil [ossa] @fold_single_block_two_applies_first_only : $@convention(thin) () -> () {
  %get_owned = function_ref @get_owned : $@convention(thin) () -> (@owned C)
  %instance = apply %get_owned() : $@convention(thin) () -> (@owned C)
  %lifetime = begin_borrow [lexical] %instance : $C
  %callee_owned = function_ref @callee_owned : $@convention(thin) (@owned C) -> ()
  %copy_1 = copy_value %lifetime : $C
  apply %callee_owned(%copy_1) : $@convention(thin) (@owned C) -> ()
  %retval = tuple ()
  %copy_2 = copy_value %lifetime : $C
  apply %callee_owned(%copy_2) : $@convention(thin) (@owned C) -> ()
  end_borrow %lifetime : $C
  destroy_value %instance : $C
  return %retval : $()
}

// Don't fold with apply when guaranteed lexical value used in one but not two
// branches and the lexical scope ends after the use on the non-lexical branch.
//
// CHECK-LABEL: sil [ossa] @nofold_two_parallel_owned_uses_one_lexical : {{.*}} {
// CHECK-NOT: move_value [lexical]
// CHECK-LABEL: } // end sil function 'nofold_two_parallel_owned_uses_one_lexical'
sil [ossa] @nofold_two_parallel_owned_uses_one_lexical : $@convention(thin) (@owned C) -> () {
entry(%instance : @owned $C):
  %lifetime = begin_borrow [lexical] %instance : $C
  %callee_owned = function_ref @callee_owned : $@convention(thin) (@owned C) -> ()
  cond_br undef, left, right

left:
  %copy_1 = copy_value %lifetime : $C
  apply %callee_owned(%copy_1) : $@convention(thin) (@owned C) -> ()
  end_borrow %lifetime : $C
  destroy_value %instance : $C
  br exit

right:
  %copy_2 = copy_value %instance : $C
  apply %callee_owned(%copy_2) : $@convention(thin) (@owned C) -> ()
  end_borrow %lifetime : $C
  destroy_value %instance : $C
  br exit

exit:
  %retval = tuple ()
  return %retval : $()
}

// Fold even if there is a noncanonicalized copy which is otherwise used.
// CHECK-LABEL: sil [ossa] @fold_noncanonical_copy_with_other_use : {{.*}} {
// CHECK:       {{bb[0-9]+}}:
// CHECK:         [[GET_OWNED:%[^,]+]] = function_ref @get_owned
// CHECK:         [[INSTANCE:%[^,]+]] = apply [[GET_OWNED]]()
// CHECK:         [[MOVE:%[^,]+]] = move_value [lexical] [[INSTANCE]]
// CHECK:         [[LIFETIME:%[^,]+]] = begin_borrow [lexical] [[MOVE]]
// CHECK:         [[COPY:%[^,]+]] = copy_value [[LIFETIME]]
// CHECK:         cond_br undef, [[LEFT:bb[0-9]+]], [[RIGHT:bb[0-9]+]]
// CHECK:       [[LEFT]]:
// CHECK:         [[CALLEE_OWNED:%[^,]+]] = function_ref @callee_owned
// CHECK:         destroy_value [[COPY]]
// CHECK:         end_borrow [[LIFETIME]]
// CHECK:         apply [[CALLEE_OWNED]]([[MOVE]])
// CHECK:         br [[EXIT:bb[0-9]+]]
// CHECK:       [[RIGHT]]:
// CHECK:         [[CALLEE_GUARANTEED:%[^,]+]] = function_ref @callee_guaranteed
// CHECK:         apply [[CALLEE_GUARANTEED]]([[COPY]])
// CHECK:         destroy_value [[COPY]]
// CHECK:         end_borrow [[LIFETIME]]
// CHECK:         destroy_value [[MOVE]]
// CHECK:         br [[EXIT]]
// CHECK:       [[EXIT]]:
// CHECK-LABEL: } // end sil function 'fold_noncanonical_copy_with_other_use'
sil [ossa] @fold_noncanonical_copy_with_other_use : $@convention(thin) () -> () {
entry:
  %get_owned = function_ref @get_owned : $@convention(thin) () -> (@owned C)
  %instance = apply %get_owned() : $@convention(thin) () -> (@owned C)
  %lifetime = begin_borrow [lexical] %instance : $C
  %copy = copy_value %lifetime : $C
  cond_br undef, left, right

left:
  %callee_owned = function_ref @callee_owned : $@convention(thin) (@owned C) -> ()
  apply %callee_owned(%copy) : $@convention(thin) (@owned C) -> ()
  br exit

right:
  %callee_guaranteed = function_ref @callee_guaranteed : $@convention(thin) (@guaranteed C) -> ()
  apply %callee_guaranteed(%copy) : $@convention(thin) (@guaranteed C) -> ()
  destroy_value %copy : $C
  br exit

exit:
  end_borrow %lifetime : $C
  destroy_value %instance : $C
  %retval = tuple ()
  return %retval : $()
}

// Fold in the face of an unreachable block on one path so long as there is no
// use of %original_borrowee in that unreachable-terminated branch.
//
// CHECK-LABEL: sil [ossa] @fold_unreachable : {{.*}} {
// CHECK:         [[GET_OWNED:%[^,]+]] = function_ref @get_owned
// CHECK:         [[INSTANCE:%[^,]+]] = apply [[GET_OWNED]]
// CHECK:         [[MOVE:%[^,]+]] = move_value [lexical] [[INSTANCE]]
// CHECK:         cond_br undef, [[LEFT:bb[0-9]+]], [[RIGHT:bb[0-9]+]]
// CHECK:       [[LEFT]]:
// CHECK:         unreachable
// CHECK:       [[RIGHT]]:
// CHECK:         [[CALLEE_OWNED:%[^,]+]] = function_ref @callee_owned
// CHECK:         apply [[CALLEE_OWNED]]([[MOVE]])
// CHECK:         br [[EXIT:bb[0-9]+]]
// CHECK:       [[EXIT]]:
// CHECK-LABEL: } // end sil function 'fold_unreachable'
sil [ossa] @fold_unreachable : $@convention(thin) () -> () {
entry:
  %get_owned = function_ref @get_owned : $@convention(thin) () -> (@owned C)
  %instance = apply %get_owned() : $@convention(thin) () -> (@owned C)
  %lifetime = begin_borrow [lexical] %instance : $C
  cond_br undef, left, right

left:
  unreachable

right:
  %callee_owned = function_ref @callee_owned : $@convention(thin) (@owned C) -> ()
  %copy = copy_value %lifetime : $C
  apply %callee_owned(%copy) : $@convention(thin) (@owned C) -> ()
  end_borrow %lifetime : $C
  destroy_value %instance : $C
  br exit

exit:
  %retval = tuple ()
  return %retval : $()
}

// Don't fold in the face of an unreachable block on one path so long as there
// is no use of %original_borrowee in that "unterminated" branch.
//
// CHECK-LABEL: sil [ossa] @nofold_unreachable : {{.*}} {
// CHECK-NOT: move_value [lexical]
// CHECK-LABEL: } // end sil function 'nofold_unreachable'
sil [ossa] @nofold_unreachable : $@convention(thin) (@owned C) -> () {
entry(%instance : @owned $C):
  %callee_owned = function_ref @callee_owned : $@convention(thin) (@owned C) -> ()
  %lifetime = begin_borrow [lexical] %instance : $C
  cond_br undef, left, right

left:
  apply %callee_owned(%instance) : $@convention(thin) (@owned C) -> ()
  unreachable

right:
  %copy = copy_value %lifetime : $C
  apply %callee_owned(%copy) : $@convention(thin) (@owned C) -> ()
  end_borrow %lifetime : $C
  destroy_value %instance : $C
  br exit

exit:
  %retval = tuple ()
  return %retval : $()
}

// Don't fold if there's an outer borrow scope enclosing the guaranteed lexical
// scope and it has a use within the lexical scope.
//
// CHECK-LABEL: sil [ossa] @nofold_outer_borrow : {{.*}} {
// CHECK-NOT: move_value [lexical]
// CHECK-LABEL: } // end sil function 'nofold_outer_borrow'
sil [ossa] @nofold_outer_borrow : $@convention(thin) (@owned C) -> () {
entry(%instance : @owned $C):
  %outer = begin_borrow %instance : $C
  %lifetime = begin_borrow [lexical] %instance : $C
  %callee_owned = function_ref @callee_owned : $@convention(thin) (@owned C) -> ()
  %barrier = function_ref @barrier : $@convention(thin) () -> ()
  %callee_guaranteed = function_ref @callee_guaranteed : $@convention(thin) (@guaranteed C) -> ()
  %copy = copy_value %lifetime : $C
  apply %callee_guaranteed(%outer) : $@convention(thin) (@guaranteed C) -> ()
  apply %barrier() : $@convention(thin) () -> ()
  apply %callee_owned(%copy) : $@convention(thin) (@owned C) -> ()
  end_borrow %lifetime : $C
  end_borrow %outer : $C
  destroy_value %instance : $C
  %retval = tuple ()
  return %retval : $()
}

// Don't fold if there's an outer borrow scope started in one block enclosing
// the guaranteed lexical scope started in another block and it has a use
// within the lexical scope.
//
// CHECK-LABEL: sil [ossa] @nofold_outer_borrow__twoblock : {{.*}} {
// CHECK-NOT: move_value [lexical]
// CHECK-LABEL: } // end sil function 'nofold_outer_borrow__twoblock'
sil [ossa] @nofold_outer_borrow__twoblock : $@convention(thin) (@owned C) -> () {
entry(%instance : @owned $C):
  %outer = begin_borrow %instance : $C
  br exit
exit:
  %lifetime = begin_borrow [lexical] %instance : $C
  %callee_owned = function_ref @callee_owned : $@convention(thin) (@owned C) -> ()
  %barrier = function_ref @barrier : $@convention(thin) () -> ()
  %callee_guaranteed = function_ref @callee_guaranteed : $@convention(thin) (@guaranteed C) -> ()
  %copy = copy_value %lifetime : $C
  apply %callee_guaranteed(%outer) : $@convention(thin) (@guaranteed C) -> ()
  apply %barrier() : $@convention(thin) () -> ()
  apply %callee_owned(%copy) : $@convention(thin) (@owned C) -> ()
  end_borrow %lifetime : $C
  end_borrow %outer : $C
  destroy_value %instance : $C
  %retval = tuple ()
  return %retval : $()
}

// Don't fold if there are TWO layers of outer borrows, both started in one
// block enclosing the guaranteed lexical scope started--all together in the
// same block--and both have a use within the lexical scope.
//
// CHECK-LABEL: sil [ossa] @nofold_outer_borrow_2x : {{.*}} {
// CHECK-NOT: move_value [lexical]
// CHECK-LABEL: } // end sil function 'nofold_outer_borrow_2x'
sil [ossa] @nofold_outer_borrow_2x : $@convention(thin) (@owned C) -> () {
entry(%instance : @owned $C):
  %outer = begin_borrow %instance : $C
  %middle = begin_borrow %outer : $C
  br exit
exit:
  %lifetime = begin_borrow [lexical] %instance : $C
  %callee_owned = function_ref @callee_owned : $@convention(thin) (@owned C) -> ()
  %barrier = function_ref @barrier : $@convention(thin) () -> ()
  %callee_guaranteed = function_ref @callee_guaranteed : $@convention(thin) (@guaranteed C) -> ()
  %copy = copy_value %lifetime : $C
  apply %callee_guaranteed(%outer) : $@convention(thin) (@guaranteed C) -> ()
  apply %callee_guaranteed(%middle) : $@convention(thin) (@guaranteed C) -> ()
  apply %barrier() : $@convention(thin) () -> ()
  apply %callee_owned(%copy) : $@convention(thin) (@owned C) -> ()
  end_borrow %lifetime : $C
  end_borrow %middle : $C
  end_borrow %outer : $C
  destroy_value %instance : $C
  %retval = tuple ()
  return %retval : $()
}

// Don't fold if there are TWO layers of outer borrows, both started in one
// block, enclosing the guaranteed lexical scope started in another block and
// both have a use within the lexical scope.
//
// CHECK-LABEL: sil [ossa] @nofold_outer_borrow_2x__twoblock : {{.*}} {
// CHECK-NOT: move_value [lexical]
// CHECK-LABEL: } // end sil function 'nofold_outer_borrow_2x__twoblock'
sil [ossa] @nofold_outer_borrow_2x__twoblock : $@convention(thin) (@owned C) -> () {
entry(%instance : @owned $C):
  %outer = begin_borrow %instance : $C
  %middle = begin_borrow %outer : $C
  br exit
exit:
  %lifetime = begin_borrow [lexical] %instance : $C
  %callee_owned = function_ref @callee_owned : $@convention(thin) (@owned C) -> ()
  %barrier = function_ref @barrier : $@convention(thin) () -> ()
  %callee_guaranteed = function_ref @callee_guaranteed : $@convention(thin) (@guaranteed C) -> ()
  %copy = copy_value %lifetime : $C
  apply %callee_guaranteed(%outer) : $@convention(thin) (@guaranteed C) -> ()
  apply %callee_guaranteed(%middle) : $@convention(thin) (@guaranteed C) -> ()
  apply %barrier() : $@convention(thin) () -> ()
  apply %callee_owned(%copy) : $@convention(thin) (@owned C) -> ()
  end_borrow %lifetime : $C
  end_borrow %middle : $C
  end_borrow %outer : $C
  destroy_value %instance : $C
  %retval = tuple ()
  return %retval : $()
}

// Don't fold when a different borrow of the enclosing %original_borrowee end
// in the middle of the lexical scope.
//
// CHECK-LABEL: sil [ossa] @nofold_outer_borrow_2x_end_inside__twoblock : {{.*}} {
// CHECK-NOT: move_value [lexical]
// CHECK-LABEL: } // end sil function 'nofold_outer_borrow_2x_end_inside__twoblock'
sil [ossa] @nofold_outer_borrow_2x_end_inside__twoblock : $@convention(thin) (@owned C) -> () {
entry(%instance : @owned $C):
  %outer = begin_borrow %instance : $C
  %middle = begin_borrow %outer : $C
  br exit
exit:
  %lifetime = begin_borrow [lexical] %instance : $C
  %callee_owned = function_ref @callee_owned : $@convention(thin) (@owned C) -> ()
  %barrier = function_ref @barrier : $@convention(thin) () -> ()
  %callee_guaranteed = function_ref @callee_guaranteed : $@convention(thin) (@guaranteed C) -> ()
  %copy = copy_value %lifetime : $C
  apply %callee_guaranteed(%outer) : $@convention(thin) (@guaranteed C) -> ()
  apply %callee_guaranteed(%middle) : $@convention(thin) (@guaranteed C) -> ()
  end_borrow %middle : $C
  end_borrow %outer : $C
  apply %barrier() : $@convention(thin) () -> ()
  apply %callee_owned(%copy) : $@convention(thin) (@owned C) -> ()
  end_borrow %lifetime : $C
  destroy_value %instance : $C
  %retval = tuple ()
  return %retval : $()
}

// Don't fold if the borrow scope begins in an unreachable-from-entry block.
//
// CHECK-LABEL: sil [ossa] @nofold_deadblock : $@convention(thin) () -> () {
// CHECK-NOT: move_value [lexical]
// CHECK-LABEL: } // end sil function 'nofold_deadblock'
sil [ossa] @nofold_deadblock : $@convention(thin) () -> () {
entry:
  br exit
dead:
  %get_owned = function_ref @get_owned : $@convention(thin) () -> @owned C
  %instance = apply %get_owned() : $@convention(thin) () -> @owned C
  %lifetime = begin_borrow [lexical] %instance : $C
  %callee_owned = function_ref @callee_owned : $@convention(thin) (@owned C) -> ()
  %copy = copy_value %lifetime : $C
  apply %callee_owned(%copy) : $@convention(thin) (@owned C) -> ()
  end_borrow %lifetime : $C
  destroy_value %instance : $C
  br exit

exit:
  %retval = tuple ()
  return %retval : $()
}

// Don't fold applies of the borrowed value.
//
// CHECK-LABEL: sil [ossa] @nofold_closure : $@convention(thin) (@owned () -> ()) -> () {
// CHECK-NOT: move_value [lexical]
// CHECK-LABEL: } // end sil function 'nofold_closure'
sil [ossa] @nofold_closure : $@convention(thin) (@owned @convention(thick) () -> ()) -> () {
entry(%instance : @owned $@convention(thick) () -> ()):
  %lifetime = begin_borrow [lexical] %instance : $@convention(thick) () -> ()
  apply %lifetime() : $@convention(thick) () -> ()
  end_borrow %lifetime : $@convention(thick) () -> ()
  destroy_value %instance : $@convention(thick) () -> ()
  %retval = tuple ()
  return %retval : $()
}

// Lexical destroy folding cannot handle a PointerEscape.
//
// CHECK-LABEL: sil [ossa] @dont_fold_over_escape : $@convention(thin) (@owned C) -> @owned D {
// CHECK: bb0(%0 : @owned $C):
// CHECK:   [[CAST:%.*]] = unchecked_bitwise_cast %0 : $C to $D
// CHECK:   [[BORROW:%.*]] = begin_borrow [lexical] %0 : $C
// CHECK:   [[COPY0:%.*]] = copy_value [[BORROW]] : $C
// CHECK:   apply %{{.*}}([[COPY0]]) : $@convention(thin) (@owned C) -> ()
// CHECK:   [[COPY1:%.*]] = copy_value [[CAST]] : $D
// CHECK:   destroy_value %0 : $C
// CHECK-LABEL: } // end sil function 'dont_fold_over_escape'
sil [ossa] @dont_fold_over_escape : $@convention(thin) (@owned C) -> @owned D {
bb0(%0 : @owned $C):
  %callee_owned = function_ref @callee_owned : $@convention(thin) (@owned C) -> ()
  %cast = unchecked_bitwise_cast %0 : $C to $D
  %lifetime = begin_borrow [lexical] %0 : $C
  %copy0 = copy_value %lifetime : $C
  apply %callee_owned(%copy0) : $@convention(thin) (@owned C) -> ()
  end_borrow %lifetime : $C
  %copy = copy_value %cast : $D
  destroy_value %0 : $C
  return %copy : $D
}

// =============================================================================
// TESTS                                                                      }}
// =============================================================================