summaryrefslogtreecommitdiffstats
path: root/tests/mir-opt/reference_prop.rs
blob: 1b9c8fe15c25e9437389041366aeb434cd3a67a1 (plain)
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
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
// unit-test: ReferencePropagation
// needs-unwind

#![feature(raw_ref_op)]
#![feature(core_intrinsics, custom_mir)]

#[inline(never)]
fn opaque(_: impl Sized) {}

fn reference_propagation<'a, T: Copy>(single: &'a T, mut multiple: &'a T) {
    // CHECK-LABEL: fn reference_propagation(

    // Propagation through a reference.
    {
        // CHECK: bb0: {
        // CHECK: [[a:_.*]] = const 5_usize;
        // CHECK: [[b:_.*]] = &[[a]];
        // CHECK: [[c:_.*]] = [[a]];

        let a = 5_usize;
        let b = &a; // This borrow is only used once.
        let c = *b; // This should be optimized.
        opaque(()); // We use opaque to separate cases into basic blocks in the MIR.
    }

    // Propagation through two references.
    {
        // CHECK: bb1: {
        // CHECK: [[a:_.*]] = const 5_usize;
        // CHECK: [[a2:_.*]] = const 7_usize;
        // CHECK: [[b:_.*]] = &[[a]];
        // CHECK: [[btmp:_.*]] = &[[a2]];
        // CHECK: [[b]] = move [[btmp]];
        // CHECK: [[c:_.*]] = (*[[b]]);

        let a = 5_usize;
        let a2 = 7_usize;
        let mut b = &a;
        b = &a2;
        // `b` is assigned twice, so we cannot propagate it.
        let c = *b;
        opaque(());
    }

    // Propagation through a borrowed reference.
    {
        // CHECK: bb2: {
        // CHECK: [[a:_.*]] = const 5_usize;
        // CHECK: [[b:_.*]] = &[[a]];
        // CHECK: [[d:_.*]] = &[[b]];
        // CHECK: [[c:_.*]] = (*[[b]]);

        let a = 5_usize;
        let b = &a;
        let d = &b;
        let c = *b; // `b` is immutably borrowed, we know its value, but do not propagate it
        opaque(d); // prevent `d` from being removed.
    }

    // Propagation through a mutably borrowed reference.
    {
        // CHECK: bb3: {
        // CHECK: [[a:_.*]] = const 5_usize;
        // CHECK: [[b:_.*]] = &[[a]];
        // CHECK: [[d:_.*]] = &raw mut [[b]];
        // CHECK: [[c:_.*]] = (*[[b]]);

        let a = 5_usize;
        let mut b = &a;
        let d = &raw mut b;
        let c = *b; // `b` is mutably borrowed, we cannot know its value.
        opaque(d); // prevent `d` from being removed.
    }

    // Propagation through an escaping borrow.
    {
        // CHECK: bb4: {
        // CHECK: [[a:_.*]] = const 7_usize;
        // CHECK: [[b:_.*]] = &[[a]];
        // CHECK: [[c:_.*]] = [[a]];

        let a = 7_usize;
        let b = &a;
        let c = *b;
        opaque(b); // `b` escapes here, but we can still replace immutable borrow
    }

    // Propagation through a transitively escaping borrow.
    {
        // CHECK: bb5: {
        // CHECK: [[a:_.*]] = const 7_usize;
        // CHECK: [[b1:_.*]] = &[[a]];
        // CHECK: [[c:_.*]] = [[a]];
        // CHECK: [[b2:_.*]] = [[b1]];
        // CHECK: [[c2:_.*]] = [[a]];
        // CHECK: [[b3:_.*]] = [[b2]];

        let a = 7_usize;
        let b1 = &a;
        let c = *b1;
        let b2 = b1;
        let c2 = *b2;
        let b3 = b2;
        // `b3` escapes here, so we can only replace immutable borrow,
        // for either `b`, `b2` or `b3`.
        opaque(b3);
    }

    // Propagation a reborrow of an argument.
    {
        // CHECK: bb6: {
        // CHECK-NOT: {{_.*}} = &(*_1);
        // CHECK: [[b:_.*]] = (*_1);

        let a = &*single;
        let b = *a; // This should be optimized as `*single`.
        opaque(());
    }

    // Propagation a reborrow of a mutated argument.
    {
        // CHECK: bb7: {
        // CHECK: [[a:_.*]] = &(*_2);
        // CHECK: [[tmp:_.*]] = &(*_1);
        // CHECK: _2 = move [[tmp]];
        // CHECK: [[b:_.*]] = (*[[a]]);

        let a = &*multiple;
        multiple = &*single;
        let b = *a; // This should not be optimized.
        opaque(());
    }

    // Fixed-point propagation through a borrowed reference.
    {
        // CHECK: bb8: {
        // CHECK: [[a:_.*]] = const 5_usize;
        // CHECK: [[b:_.*]] = &[[a]];
        // CHECK: [[d:_.*]] = &[[b]];
        // FIXME this could be [[a]]
        // CHECK: [[c:_.*]] = (*[[b]]);

        let a = 5_usize;
        let b = &a;
        let d = &b; // first round promotes debuginfo for `d`
        let c = *b; // second round propagates this dereference
        opaque(());
    }

    // Fixed-point propagation through a mutably borrowed reference.
    {
        // CHECK: bb9: {
        // CHECK: [[a:_.*]] = const 5_usize;
        // CHECK: [[b:_.*]] = &[[a]];
        // CHECK: [[d:_.*]] = &mut [[b]];
        // FIXME this could be [[a]]
        // CHECK: [[c:_.*]] = (*[[b]]);

        let a = 5_usize;
        let mut b = &a;
        let d = &mut b; // first round promotes debuginfo for `d`
        let c = *b; // second round propagates this dereference
        opaque(());
    }
}

fn reference_propagation_mut<'a, T: Copy>(single: &'a mut T, mut multiple: &'a mut T) {
    // CHECK-LABEL: fn reference_propagation_mut(

    // Propagation through a reference.
    {
        // CHECK: bb0: {
        // CHECK: [[a:_.*]] = const 5_usize;
        // CHECK: [[b:_.*]] = &mut [[a]];
        // CHECK: [[c:_.*]] = [[a]];

        let mut a = 5_usize;
        let b = &mut a; // This borrow is only used once.
        let c = *b; // This should be optimized.
        opaque(());
    }

    // Propagation through two references.
    {
        // CHECK: bb1: {
        // CHECK: [[a:_.*]] = const 5_usize;
        // CHECK: [[a2:_.*]] = const 7_usize;
        // CHECK: [[b:_.*]] = &mut [[a]];
        // CHECK: [[btmp:_.*]] = &mut [[a2]];
        // CHECK: [[b]] = move [[btmp]];
        // CHECK: [[c:_.*]] = (*[[b]]);

        let mut a = 5_usize;
        let mut a2 = 7_usize;
        let mut b = &mut a;
        b = &mut a2;
        // `b` is assigned twice, so we cannot propagate it.
        let c = *b;
        opaque(());
    }

    // Propagation through a borrowed reference.
    {
        // CHECK: bb2: {
        // CHECK: [[a:_.*]] = const 5_usize;
        // CHECK: [[b:_.*]] = &mut [[a]];
        // CHECK: [[d:_.*]] = &[[b]];
        // CHECK: [[c:_.*]] = (*[[b]]);

        let mut a = 5_usize;
        let b = &mut a;
        let d = &b;
        let c = *b; // `b` is immutably borrowed, we know its value, but cannot be removed.
        opaque(d); // prevent `d` from being removed.
    }

    // Propagation through a mutably borrowed reference.
    {
        // CHECK: bb3: {
        // CHECK: [[a:_.*]] = const 5_usize;
        // CHECK: [[b:_.*]] = &mut [[a]];
        // CHECK: [[d:_.*]] = &raw mut [[b]];
        // CHECK: [[c:_.*]] = (*[[b]]);

        let mut a = 5_usize;
        let mut b = &mut a;
        let d = &raw mut b;
        let c = *b; // `b` is mutably borrowed, we cannot know its value.
        opaque(d); // prevent `d` from being removed.
    }

    // Propagation through an escaping borrow.
    {
        // CHECK: bb4: {
        // CHECK: [[a:_.*]] = const 7_usize;
        // CHECK: [[b:_.*]] = &mut [[a]];
        // CHECK: [[c:_.*]] = (*[[b]]);

        let mut a = 7_usize;
        let b = &mut a;
        let c = *b;
        opaque(b); // `b` escapes here, so we can only replace immutable borrow
    }

    // Propagation through a transitively escaping borrow.
    {
        // CHECK: bb5: {
        // CHECK: [[a:_.*]] = const 7_usize;
        // CHECK: [[b1:_.*]] = &mut [[a]];
        // CHECK: [[c:_.*]] = (*[[b1]]);
        // CHECK: [[b2:_.*]] = move [[b1]];
        // CHECK: [[c2:_.*]] = (*[[b2]]);
        // CHECK: [[b3:_.*]] = move [[b2]];

        let mut a = 7_usize;
        let b1 = &mut a;
        let c = *b1;
        let b2 = b1;
        let c2 = *b2;
        let b3 = b2;
        // `b3` escapes here, so we can only replace immutable borrow,
        // for either `b`, `b2` or `b3`.
        opaque(b3);
    }

    // Propagation a reborrow of an argument.
    {
        // CHECK: bb6: {
        // CHECK-NOT: {{_.*}} = &(*_1);
        // CHECK: [[b:_.*]] = (*_1);

        let a = &mut *single;
        let b = *a; // This should be optimized as `*single`.
        opaque(());
    }

    // Propagation a reborrow of a mutated argument.
    {
        // CHECK: bb7: {
        // CHECK: [[a:_.*]] = &mut (*_2);
        // CHECK: [[tmp:_.*]] = &mut (*_1);
        // CHECK: _2 = move [[tmp]];
        // CHECK: [[b:_.*]] = (*[[a]]);

        let a = &mut *multiple;
        multiple = &mut *single;
        let b = *a; // This should not be optimized.
        opaque(());
    }

    // Fixed-point propagation through a borrowed reference.
    {
        // CHECK: bb8: {
        // CHECK: [[a:_.*]] = const 5_usize;
        // CHECK: [[b:_.*]] = &mut [[a]];
        // CHECK: [[d:_.*]] = &[[b]];
        // FIXME this could be [[a]]
        // CHECK: [[c:_.*]] = (*[[b]]);

        let mut a = 5_usize;
        let b = &mut a;
        let d = &b; // first round promotes debuginfo for `d`
        let c = *b; // second round propagates this dereference
        opaque(());
    }

    // Fixed-point propagation through a mutably borrowed reference.
    {
        // CHECK: bb9: {
        // CHECK: [[a:_.*]] = const 5_usize;
        // CHECK: [[b:_.*]] = &mut [[a]];
        // CHECK: [[d:_.*]] = &mut [[b]];
        // FIXME this could be [[a]]
        // CHECK: [[c:_.*]] = (*[[b]]);

        let mut a = 5_usize;
        let mut b = &mut a;
        let d = &mut b; // first round promotes debuginfo for `d`
        let c = *b; // second round propagates this dereference
        opaque(());
    }
}

fn reference_propagation_const_ptr<T: Copy>(single: *const T, mut multiple: *const T) {
    // CHECK-LABEL: fn reference_propagation_const_ptr(

    // Propagation through a reference.
    unsafe {
        // CHECK: bb0: {
        // CHECK: [[a:_.*]] = const 5_usize;
        // CHECK: [[b:_.*]] = &raw const [[a]];
        // CHECK: [[c:_.*]] = [[a]];

        let a = 5_usize;
        let b = &raw const a; // This borrow is only used once.
        let c = *b; // This should be optimized.
        opaque(());
    }

    // Propagation through two references.
    unsafe {
        // CHECK: bb1: {
        // CHECK: [[a:_.*]] = const 5_usize;
        // CHECK: [[a2:_.*]] = const 7_usize;
        // CHECK: [[b:_.*]] = &raw const [[a]];
        // CHECK: [[btmp:_.*]] = &raw const [[a2]];
        // CHECK: [[b]] = move [[btmp]];
        // CHECK: [[c:_.*]] = (*[[b]]);

        let a = 5_usize;
        let a2 = 7_usize;
        let mut b = &raw const a;
        b = &raw const a2;
        // `b` is assigned twice, so we cannot propagate it.
        let c = *b;
        opaque(());
    }

    // Propagation through a borrowed reference.
    unsafe {
        // CHECK: bb2: {
        // CHECK: [[a:_.*]] = const 5_usize;
        // CHECK: [[b:_.*]] = &raw const [[a]];
        // CHECK: [[d:_.*]] = &[[b]];
        // CHECK: [[c:_.*]] = (*[[b]]);

        let a = 5_usize;
        let b = &raw const a;
        let d = &b;
        let c = *b; // `b` is immutably borrowed, we know its value, but cannot be removed.
        opaque(d); // prevent `d` from being removed.
    }

    // Propagation through a mutably borrowed reference.
    unsafe {
        // CHECK: bb3: {
        // CHECK: [[a:_.*]] = const 5_usize;
        // CHECK: [[b:_.*]] = &raw const [[a]];
        // CHECK: [[d:_.*]] = &raw mut [[b]];
        // CHECK: [[c:_.*]] = (*[[b]]);

        let a = 5_usize;
        let mut b = &raw const a;
        let d = &raw mut b;
        let c = *b; // `b` is mutably borrowed, we cannot know its value.
        opaque(d); // prevent `d` from being removed.
    }

    // Propagation through an escaping borrow.
    unsafe {
        // CHECK: bb4: {
        // CHECK: [[a:_.*]] = const 7_usize;
        // CHECK: [[b:_.*]] = &raw const [[a]];
        // CHECK: [[c:_.*]] = [[a]];

        let a = 7_usize;
        let b = &raw const a;
        let c = *b;
        opaque(b); // `b` escapes here, so we can only replace immutable borrow
    }

    // Propagation through a transitively escaping borrow.
    unsafe {
        // CHECK: bb5: {
        // CHECK: [[a:_.*]] = const 7_usize;
        // CHECK: [[b1:_.*]] = &raw const [[a]];
        // CHECK: [[c:_.*]] = [[a]];
        // CHECK: [[b2:_.*]] = [[b1]];
        // CHECK: [[c2:_.*]] = [[a]];
        // CHECK: [[b3:_.*]] = [[b2]];

        let a = 7_usize;
        let b1 = &raw const a;
        let c = *b1;
        let b2 = b1;
        let c2 = *b2;
        let b3 = b2;
        // `b3` escapes here, so we can only replace immutable borrow,
        // for either `b`, `b2` or `b3`.
        opaque(b3);
    }

    // Propagation a reborrow of an argument.
    unsafe {
        // CHECK: bb6: {
        // CHECK-NOT: {{_.*}} = &(*_1);
        // CHECK: [[b:_.*]] = (*_1);

        let a = &raw const *single;
        let b = *a; // This should be optimized as `*single`.
        opaque(());
    }

    // Propagation a reborrow of a mutated argument.
    unsafe {
        // CHECK: bb7: {
        // CHECK: [[a:_.*]] = &raw const (*_2);
        // CHECK: [[tmp:_.*]] = &raw const (*_1);
        // CHECK: _2 = move [[tmp]];
        // CHECK: [[b:_.*]] = (*[[a]]);

        let a = &raw const *multiple;
        multiple = &raw const *single;
        let b = *a; // This should not be optimized.
        opaque(());
    }

    // Propagation through a reborrow.
    unsafe {
        // CHECK: bb8: {
        // CHECK: [[a:_.*]] = const 13_usize;
        // CHECK: [[b:_.*]] = &raw const [[a]];
        // CHECK: [[d:_.*]] = &raw const [[a]];
        // CHECK: [[c:_.*]] = [[a]];

        let a = 13_usize;
        let b = &raw const a;
        let c = &raw const *b;
        let e = *c;
        opaque(());
    }

    // Fixed-point propagation through a borrowed reference.
    unsafe {
        // CHECK: bb9: {
        // CHECK: [[a:_.*]] = const 5_usize;
        // CHECK: [[b:_.*]] = &raw const [[a]];
        // CHECK: [[d:_.*]] = &[[b]];
        // FIXME this could be [[a]]
        // CHECK: [[c:_.*]] = (*[[b]]);

        let a = 5_usize;
        let b = &raw const a;
        let d = &b; // first round promotes debuginfo for `d`
        let c = *b; // second round propagates this dereference
        opaque(());
    }

    // Fixed-point propagation through a borrowed reference.
    unsafe {
        // CHECK: bb10: {
        // CHECK: [[a:_.*]] = const 5_usize;
        // CHECK: [[b:_.*]] = &raw const [[a]];
        // CHECK: [[d:_.*]] = &mut [[b]];
        // FIXME this could be [[a]]
        // CHECK: [[c:_.*]] = (*[[b]]);

        let a = 5_usize;
        let mut b = &raw const a;
        let d = &mut b; // first round promotes debuginfo for `d`
        let c = *b; // second round propagates this dereference
        opaque(());
    }
}

fn reference_propagation_mut_ptr<T: Copy>(single: *mut T, mut multiple: *mut T) {
    // CHECK-LABEL: fn reference_propagation_mut_ptr(

    // Propagation through a reference.
    unsafe {
        // CHECK: bb0: {
        // CHECK: [[a:_.*]] = const 5_usize;
        // CHECK: [[b:_.*]] = &raw mut [[a]];
        // CHECK: [[c:_.*]] = [[a]];

        let mut a = 5_usize;
        let b = &raw mut a; // This borrow is only used once.
        let c = *b; // This should be optimized.
        opaque(());
    }

    // Propagation through two references.
    unsafe {
        // CHECK: bb1: {
        // CHECK: [[a:_.*]] = const 5_usize;
        // CHECK: [[a2:_.*]] = const 7_usize;
        // CHECK: [[b:_.*]] = &raw mut [[a]];
        // CHECK: [[btmp:_.*]] = &raw mut [[a2]];
        // CHECK: [[b]] = move [[btmp]];
        // CHECK: [[c:_.*]] = (*[[b]]);

        let mut a = 5_usize;
        let mut a2 = 7_usize;
        let mut b = &raw mut a;
        b = &raw mut a2;
        // `b` is assigned twice, so we cannot propagate it.
        let c = *b;
        opaque(());
    }

    // Propagation through a borrowed reference.
    unsafe {
        // CHECK: bb2: {
        // CHECK: [[a:_.*]] = const 5_usize;
        // CHECK: [[b:_.*]] = &raw mut [[a]];
        // CHECK: [[d:_.*]] = &[[b]];
        // CHECK: [[c:_.*]] = (*[[b]]);

        let mut a = 5_usize;
        let b = &raw mut a;
        let d = &b;
        let c = *b; // `b` is immutably borrowed, we know its value, but cannot be removed.
        opaque(d); // prevent `d` from being removed.
    }

    // Propagation through a mutably borrowed reference.
    unsafe {
        // CHECK: bb3: {
        // CHECK: [[a:_.*]] = const 5_usize;
        // CHECK: [[b:_.*]] = &raw mut [[a]];
        // CHECK: [[d:_.*]] = &raw mut [[b]];
        // CHECK: [[c:_.*]] = (*[[b]]);

        let mut a = 5_usize;
        let mut b = &raw mut a;
        let d = &raw mut b;
        let c = *b; // `b` is mutably borrowed, we cannot know its value.
        opaque(d); // prevent `d` from being removed.
    }

    // Propagation through an escaping borrow.
    unsafe {
        // CHECK: bb4: {
        // CHECK: [[a:_.*]] = const 7_usize;
        // CHECK: [[b:_.*]] = &raw mut [[a]];
        // CHECK: [[c:_.*]] = (*[[b]]);

        let mut a = 7_usize;
        let b = &raw mut a;
        let c = *b;
        opaque(b); // `b` escapes here, so we can only replace immutable borrow
    }

    // Propagation through a transitively escaping borrow.
    unsafe {
        // CHECK: bb5: {
        // CHECK: [[a:_.*]] = const 7_usize;
        // CHECK: [[b1:_.*]] = &raw mut [[a]];
        // CHECK: [[c:_.*]] = (*[[b1]]);
        // CHECK: [[b2:_.*]] = [[b1]];
        // CHECK: [[c2:_.*]] = (*[[b2]]);
        // CHECK: [[b3:_.*]] = [[b2]];

        let mut a = 7_usize;
        let b1 = &raw mut a;
        let c = *b1;
        let b2 = b1;
        let c2 = *b2;
        let b3 = b2;
        // `b3` escapes here, so we can only replace immutable borrow,
        // for either `b`, `b2` or `b3`.
        opaque(b3);
    }

    // Propagation a reborrow of an argument.
    unsafe {
        // CHECK: bb6: {
        // CHECK-NOT: {{_.*}} = &(*_1);
        // CHECK: [[b:_.*]] = (*_1);

        let a = &raw mut *single;
        let b = *a; // This should be optimized as `*single`.
        opaque(());
    }

    // Propagation a reborrow of a mutated argument.
    unsafe {
        // CHECK: bb7: {
        // CHECK: [[a:_.*]] = &raw mut (*_2);
        // CHECK: [[tmp:_.*]] = &raw mut (*_1);
        // CHECK: _2 = move [[tmp]];
        // CHECK: [[b:_.*]] = (*[[a]]);

        let a = &raw mut *multiple;
        multiple = &raw mut *single;
        let b = *a; // This should not be optimized.
        opaque(());
    }

    // Fixed-point propagation through a borrowed reference.
    unsafe {
        // CHECK: bb8: {
        // CHECK: [[a:_.*]] = const 5_usize;
        // CHECK: [[b:_.*]] = &raw mut [[a]];
        // CHECK: [[d:_.*]] = &[[b]];
        // FIXME this could be [[a]]
        // CHECK: [[c:_.*]] = (*[[b]]);

        let mut a = 5_usize;
        let b = &raw mut a;
        let d = &b; // first round promotes debuginfo for `d`
        let c = *b; // second round propagates this dereference
        opaque(());
    }

    // Fixed-point propagation through a mutably borrowed reference.
    unsafe {
        // CHECK: bb9: {
        // CHECK: [[a:_.*]] = const 5_usize;
        // CHECK: [[b:_.*]] = &raw mut [[a]];
        // CHECK: [[d:_.*]] = &mut [[b]];
        // FIXME this could be [[a]]
        // CHECK: [[c:_.*]] = (*[[b]]);

        let mut a = 5_usize;
        let mut b = &raw mut a;
        let d = &mut b; // first round promotes debuginfo for `d`
        let c = *b; // second round propagates this dereference
        opaque(());
    }
}

#[custom_mir(dialect = "runtime", phase = "post-cleanup")]
fn read_through_raw(x: &mut usize) -> usize {
    // CHECK-LABEL: read_through_raw
    // CHECK: bb0: {
    // CHECK-NEXT: _0 = (*_1);
    // CHECK-NEXT: _0 = (*_1);
    // CHECK-NEXT: return;

    use std::intrinsics::mir::*;
    mir!(
        let r1: &mut usize;
        let r2: &mut usize;
        let p1: *mut usize;
        let p2: *mut usize;

        {
            r1 = &mut *x;
            r2 = &mut *r1;
            p1 = &raw mut *r1;
            p2 = &raw mut *r2;

            RET = *p1;
            RET = *p2;
            Return()
        }
    )
}

#[custom_mir(dialect = "runtime", phase = "post-cleanup")]
fn multiple_storage() {
    // CHECK-LABEL: multiple_storage
    // CHECK: _3 = (*_2);

    use std::intrinsics::mir::*;
    mir!(
        let x: i32;
        {
            StorageLive(x);
            x = 5;
            let z = &x;
            StorageDead(x);
            StorageLive(x);
            // As there are multiple `StorageLive` statements for `x`, we cannot know if this `z`'s
            // pointer address is the address of `x`, so do nothing.
            let y = *z;
            Call(RET = opaque(y), retblock, UnwindContinue())
        }

        retblock = {
            Return()
        }
    )
}

#[custom_mir(dialect = "runtime", phase = "post-cleanup")]
fn dominate_storage() {
    // CHECK-LABEL: dominate_storage
    // CHECK: _5 = (*_2);

    use std::intrinsics::mir::*;
    mir!(
        let x: i32;
        let r: &i32;
        let c: i32;
        let d: bool;
        { Goto(bb0) }
        bb0 = {
            x = 5;
            r = &x;
            Goto(bb1)
        }
        bb1 = {
            let c = *r;
            Call(RET = opaque(c), bb2, UnwindContinue())
        }
        bb2 = {
            StorageDead(x);
            StorageLive(x);
            let d = true;
            match d { false => bb2, _ => bb0 }
        }
    )
}

#[custom_mir(dialect = "runtime", phase = "post-cleanup")]
fn maybe_dead(m: bool) {
    // CHECK-LABEL: fn maybe_dead(
    // CHECK: (*_5) = const 7_i32;

    use std::intrinsics::mir::*;
    mir!(
        let x: i32;
        let y: i32;
        {
            StorageLive(x);
            StorageLive(y);
            x = 5;
            y = 5;
            let a = &x;
            let b = &mut y;
            // As we don't replace `b` in `bb2`, we cannot replace it here either.
            *b = 7;
            // But this can still be replaced.
            let u = *a;
            match m { true => bb1, _ => bb2 }
        }
        bb1 = {
            StorageDead(x);
            StorageDead(y);
            Call(RET = opaque(u), bb2, UnwindContinue())
        }
        bb2 = {
            // As `x` may be `StorageDead`, `a` may be dangling, so we do nothing.
            let z = *a;
            Call(RET = opaque(z), bb3, UnwindContinue())
        }
        bb3 = {
            // As `y` may be `StorageDead`, `b` may be dangling, so we do nothing.
            // This implies that we also do not substitute `b` in `bb0`.
            let t = *b;
            Call(RET = opaque(t), retblock, UnwindContinue())
        }
        retblock = {
            Return()
        }
    )
}

fn mut_raw_then_mut_shr() -> (i32, i32) {
    // CHECK-LABEL: fn mut_raw_then_mut_shr(
    // CHECK-NOT: (*{{_.*}})

    let mut x = 2;
    let xref = &mut x;
    let xraw = &mut *xref as *mut _;
    let xshr = &*xref;
    // Verify that we completely replace with `x` in both cases.
    let a = *xshr;
    unsafe { *xraw = 4; }
    (a, x)
}

fn unique_with_copies() {
    // CHECK-LABEL: fn unique_with_copies(
    // CHECK: [[a:_.*]] = const 0_i32;
    // CHECK: [[x:_.*]] = &raw mut [[a]];
    // CHECK-NOT: [[a]]
    // CHECK: [[tmp:_.*]] = (*[[x]]);
    // CHECK-NEXT: opaque::<i32>(move [[tmp]])
    // CHECK-NOT: [[a]]
    // CHECK: StorageDead([[a]]);
    // CHECK-NOT: [[a]]
    // CHECK: [[tmp:_.*]] = (*[[x]]);
    // CHECK-NEXT: opaque::<i32>(move [[tmp]])

    let y = {
        let mut a = 0;
        let x = &raw mut a;
        // `*y` is not replacable below, so we must not replace `*x`.
        unsafe { opaque(*x) };
        x
    };
    // But rewriting as `*x` is ok.
    unsafe { opaque(*y) };
}

fn debuginfo() {
    // CHECK-LABEL: fn debuginfo(
    // FIXME: This features waits for DWARF implicit pointers in LLVM.
    // CHECK: debug ref_mut_u8 => _{{.*}};
    // CHECK: debug field => _{{.*}};
    // CHECK: debug reborrow => _{{.*}};
    // CHECK: debug variant_field => _{{.*}};
    // CHECK: debug constant_index => _{{.*}};
    // CHECK: debug subslice => _{{.*}};
    // CHECK: debug constant_index_from_end => _{{.*}};
    // CHECK: debug multiple_borrow => _{{.*}};

    struct T(u8);

    let ref_mut_u8 = &mut 5_u8;
    let field = &T(0).0;

    // Verify that we don't emit `&*` in debuginfo.
    let reborrow = &*ref_mut_u8;

    match Some(0) {
        None => {}
        Some(ref variant_field) => {}
    }

    // `constant_index_from_end` and `subslice` should not be promoted, as their value depends
    // on the slice length.
    if let [_, ref constant_index, subslice @ .., ref constant_index_from_end] = &[6; 10][..] {
    }

    let multiple_borrow = &&&mut T(6).0;
}

fn many_debuginfo() {
    // CHECK-LABEL: fn many_debuginfo(
    // FIXME: This features waits for DWARF implicit pointers in LLVM.
    // CHECK: debug many_borrow => _{{.*}};

    let a = 0;

    // Verify that we do not ICE on deeply nested borrows.
    let many_borrow =
        &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
        &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
        &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
        &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
        &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&a;
}

fn main() {
    let mut x = 5_usize;
    let mut y = 7_usize;
    reference_propagation(&x, &y);
    reference_propagation_mut(&mut x, &mut y);
    reference_propagation_const_ptr(&raw const x, &raw const y);
    reference_propagation_mut_ptr(&raw mut x, &raw mut y);
    read_through_raw(&mut x);
    multiple_storage();
    dominate_storage();
    maybe_dead(true);
    mut_raw_then_mut_shr();
    unique_with_copies();
    debuginfo();
    many_debuginfo();
}

// EMIT_MIR reference_prop.reference_propagation.ReferencePropagation.diff
// EMIT_MIR reference_prop.reference_propagation_mut.ReferencePropagation.diff
// EMIT_MIR reference_prop.reference_propagation_const_ptr.ReferencePropagation.diff
// EMIT_MIR reference_prop.reference_propagation_mut_ptr.ReferencePropagation.diff
// EMIT_MIR reference_prop.read_through_raw.ReferencePropagation.diff
// EMIT_MIR reference_prop.multiple_storage.ReferencePropagation.diff
// EMIT_MIR reference_prop.dominate_storage.ReferencePropagation.diff
// EMIT_MIR reference_prop.maybe_dead.ReferencePropagation.diff
// EMIT_MIR reference_prop.mut_raw_then_mut_shr.ReferencePropagation.diff
// EMIT_MIR reference_prop.unique_with_copies.ReferencePropagation.diff
// EMIT_MIR reference_prop.debuginfo.ReferencePropagation.diff