Skip to content

Commit 26833b6

Browse files
committed
remove_dead_drops: run simplify_cfg if anything changed
1 parent 5b6aa44 commit 26833b6

6 files changed

Lines changed: 50 additions & 83 deletions

compiler/rustc_mir_transform/src/remove_dead_drops.rs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ use rustc_mir_dataflow::impls::MaybeInitializedPlaces;
44
use rustc_mir_dataflow::move_paths::{LookupResult, MoveData};
55
use rustc_mir_dataflow::{Analysis, MaybeReachable};
66

7+
use super::simplify::simplify_cfg;
8+
79
pub(crate) struct RemoveDeadDrops;
810

911
impl<'tcx> crate::MirPass<'tcx> for RemoveDeadDrops {
@@ -42,10 +44,15 @@ impl<'tcx> crate::MirPass<'tcx> for RemoveDeadDrops {
4244
}
4345
}
4446

45-
for (block, target) in dead_drops {
46-
if let Some(terminator) = &mut body.basic_blocks.as_mut()[block].terminator {
47-
terminator.kind = TerminatorKind::Goto { target };
47+
if !dead_drops.is_empty() {
48+
for (block, target) in dead_drops {
49+
if let Some(terminator) = &mut body.basic_blocks.as_mut()[block].terminator {
50+
terminator.kind = TerminatorKind::Goto { target };
51+
}
4852
}
53+
54+
// Removing drop terminators may simplify the CFG, so run cleanup.
55+
simplify_cfg(tcx, body);
4956
}
5057
}
5158
}

tests/mir-opt/building/uniform_array_move_out.move_out_by_subslice.CleanupPostBorrowck.after.mir

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -16,53 +16,45 @@ fn move_out_by_subslice() -> () {
1616
bb0: {
1717
StorageLive(_1);
1818
StorageLive(_2);
19-
_2 = Box::<i32>::new(const 1_i32) -> [return: bb1, unwind: bb9];
19+
_2 = Box::<i32>::new(const 1_i32) -> [return: bb1, unwind: bb7];
2020
}
2121

2222
bb1: {
2323
StorageLive(_3);
24-
_3 = Box::<i32>::new(const 2_i32) -> [return: bb2, unwind: bb8];
24+
_3 = Box::<i32>::new(const 2_i32) -> [return: bb2, unwind: bb6];
2525
}
2626

2727
bb2: {
2828
_1 = [move _2, move _3];
29-
goto -> bb3;
30-
}
31-
32-
bb3: {
3329
StorageDead(_3);
34-
goto -> bb4;
35-
}
36-
37-
bb4: {
3830
StorageDead(_2);
3931
nop;
4032
PlaceMention(_1);
4133
StorageLive(_4);
4234
_4 = move _1[0..2];
4335
_0 = const ();
44-
drop(_4) -> [return: bb5, unwind: bb7];
36+
drop(_4) -> [return: bb3, unwind: bb5];
4537
}
4638

47-
bb5: {
39+
bb3: {
4840
StorageDead(_4);
49-
drop(_1) -> [return: bb6, unwind: bb9];
41+
drop(_1) -> [return: bb4, unwind: bb7];
5042
}
5143

52-
bb6: {
44+
bb4: {
5345
StorageDead(_1);
5446
return;
5547
}
5648

57-
bb7 (cleanup): {
58-
drop(_1) -> [return: bb9, unwind terminate(cleanup)];
49+
bb5 (cleanup): {
50+
drop(_1) -> [return: bb7, unwind terminate(cleanup)];
5951
}
6052

61-
bb8 (cleanup): {
62-
drop(_2) -> [return: bb9, unwind terminate(cleanup)];
53+
bb6 (cleanup): {
54+
drop(_2) -> [return: bb7, unwind terminate(cleanup)];
6355
}
6456

65-
bb9 (cleanup): {
57+
bb7 (cleanup): {
6658
resume;
6759
}
6860
}

tests/mir-opt/building/uniform_array_move_out.move_out_from_end.CleanupPostBorrowck.after.mir

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -16,53 +16,45 @@ fn move_out_from_end() -> () {
1616
bb0: {
1717
StorageLive(_1);
1818
StorageLive(_2);
19-
_2 = Box::<i32>::new(const 1_i32) -> [return: bb1, unwind: bb9];
19+
_2 = Box::<i32>::new(const 1_i32) -> [return: bb1, unwind: bb7];
2020
}
2121

2222
bb1: {
2323
StorageLive(_3);
24-
_3 = Box::<i32>::new(const 2_i32) -> [return: bb2, unwind: bb8];
24+
_3 = Box::<i32>::new(const 2_i32) -> [return: bb2, unwind: bb6];
2525
}
2626

2727
bb2: {
2828
_1 = [move _2, move _3];
29-
goto -> bb3;
30-
}
31-
32-
bb3: {
3329
StorageDead(_3);
34-
goto -> bb4;
35-
}
36-
37-
bb4: {
3830
StorageDead(_2);
3931
nop;
4032
PlaceMention(_1);
4133
StorageLive(_4);
4234
_4 = move _1[1 of 2];
4335
_0 = const ();
44-
drop(_4) -> [return: bb5, unwind: bb7];
36+
drop(_4) -> [return: bb3, unwind: bb5];
4537
}
4638

47-
bb5: {
39+
bb3: {
4840
StorageDead(_4);
49-
drop(_1) -> [return: bb6, unwind: bb9];
41+
drop(_1) -> [return: bb4, unwind: bb7];
5042
}
5143

52-
bb6: {
44+
bb4: {
5345
StorageDead(_1);
5446
return;
5547
}
5648

57-
bb7 (cleanup): {
58-
drop(_1) -> [return: bb9, unwind terminate(cleanup)];
49+
bb5 (cleanup): {
50+
drop(_1) -> [return: bb7, unwind terminate(cleanup)];
5951
}
6052

61-
bb8 (cleanup): {
62-
drop(_2) -> [return: bb9, unwind terminate(cleanup)];
53+
bb6 (cleanup): {
54+
drop(_2) -> [return: bb7, unwind terminate(cleanup)];
6355
}
6456

65-
bb9 (cleanup): {
57+
bb7 (cleanup): {
6658
resume;
6759
}
6860
}

tests/mir-opt/building/write_box_via_move.box_new.CleanupPostBorrowck.after.mir

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ fn box_new(_1: T) -> Box<[T; 1024]> {
1313

1414
bb0: {
1515
StorageLive(_2);
16-
_2 = Box::<[T; 1024]>::new_uninit() -> [return: bb1, unwind: bb7];
16+
_2 = Box::<[T; 1024]>::new_uninit() -> [return: bb1, unwind: bb4];
1717
}
1818

1919
bb1: {
@@ -26,33 +26,21 @@ fn box_new(_1: T) -> Box<[T; 1024]> {
2626
((((*_4).1: std::mem::ManuallyDrop<[T; 1024]>).0: std::mem::MaybeDangling<[T; 1024]>).0: [T; 1024]) = [move _5; 1024];
2727
StorageDead(_5);
2828
_3 = move _4;
29-
goto -> bb2;
30-
}
31-
32-
bb2: {
3329
StorageDead(_4);
34-
_0 = Box::<MaybeUninit<[T; 1024]>>::assume_init(move _3) -> [return: bb3, unwind: bb5];
30+
_0 = Box::<MaybeUninit<[T; 1024]>>::assume_init(move _3) -> [return: bb2, unwind: bb3];
3531
}
3632

37-
bb3: {
33+
bb2: {
3834
StorageDead(_3);
39-
goto -> bb4;
40-
}
41-
42-
bb4: {
4335
StorageDead(_2);
4436
return;
4537
}
4638

47-
bb5 (cleanup): {
48-
drop(_3) -> [return: bb6, unwind terminate(cleanup)];
49-
}
50-
51-
bb6 (cleanup): {
52-
goto -> bb7;
39+
bb3 (cleanup): {
40+
drop(_3) -> [return: bb4, unwind terminate(cleanup)];
5341
}
5442

55-
bb7 (cleanup): {
43+
bb4 (cleanup): {
5644
resume;
5745
}
5846
}

tests/mir-opt/building/write_box_via_move.vec_macro.CleanupPostBorrowck.after.mir

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,30 +8,26 @@ fn vec_macro() -> Vec<i32> {
88
bb0: {
99
StorageLive(_1);
1010
StorageLive(_2);
11-
_2 = Box::<[i32; 8]>::new_uninit() -> [return: bb1, unwind: bb5];
11+
_2 = Box::<[i32; 8]>::new_uninit() -> [return: bb1, unwind: bb4];
1212
}
1313

1414
bb1: {
1515
((((*_2).1: std::mem::ManuallyDrop<[i32; 8]>).0: std::mem::MaybeDangling<[i32; 8]>).0: [i32; 8]) = [const 0_i32, const 1_i32, const 2_i32, const 3_i32, const 4_i32, const 5_i32, const 6_i32, const 7_i32];
1616
_1 = move _2;
17-
goto -> bb2;
18-
}
19-
20-
bb2: {
2117
StorageDead(_2);
22-
_0 = std::boxed::box_assume_init_into_vec_unsafe::<i32, 8>(move _1) -> [return: bb3, unwind: bb4];
18+
_0 = std::boxed::box_assume_init_into_vec_unsafe::<i32, 8>(move _1) -> [return: bb2, unwind: bb3];
2319
}
2420

25-
bb3: {
21+
bb2: {
2622
StorageDead(_1);
2723
return;
2824
}
2925

30-
bb4 (cleanup): {
31-
drop(_1) -> [return: bb5, unwind terminate(cleanup)];
26+
bb3 (cleanup): {
27+
drop(_1) -> [return: bb4, unwind terminate(cleanup)];
3228
}
3329

34-
bb5 (cleanup): {
30+
bb4 (cleanup): {
3531
resume;
3632
}
3733
}

tests/mir-opt/building/write_via_move.box_new.CleanupPostBorrowck.after.mir

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ fn box_new(_1: T) -> Box<[T; 1024]> {
2020

2121
bb0: {
2222
StorageLive(_2);
23-
_2 = Box::<[T; 1024]>::new_uninit() -> [return: bb1, unwind: bb7];
23+
_2 = Box::<[T; 1024]>::new_uninit() -> [return: bb1, unwind: bb5];
2424
}
2525

2626
bb1: {
@@ -30,7 +30,7 @@ fn box_new(_1: T) -> Box<[T; 1024]> {
3030
StorageLive(_5);
3131
_5 = &mut (*_2);
3232
_4 = &mut (*_5);
33-
_3 = MaybeUninit::<[T; 1024]>::as_mut_ptr(move _4) -> [return: bb2, unwind: bb6];
33+
_3 = MaybeUninit::<[T; 1024]>::as_mut_ptr(move _4) -> [return: bb2, unwind: bb4];
3434
}
3535

3636
bb2: {
@@ -48,29 +48,21 @@ fn box_new(_1: T) -> Box<[T; 1024]> {
4848
StorageDead(_6);
4949
StorageLive(_9);
5050
_9 = move _2;
51-
_0 = Box::<MaybeUninit<[T; 1024]>>::assume_init(move _9) -> [return: bb3, unwind: bb5];
51+
_0 = Box::<MaybeUninit<[T; 1024]>>::assume_init(move _9) -> [return: bb3, unwind: bb4];
5252
}
5353

5454
bb3: {
5555
StorageDead(_9);
5656
StorageDead(_3);
57-
goto -> bb4;
58-
}
59-
60-
bb4: {
6157
StorageDead(_2);
6258
return;
6359
}
6460

65-
bb5 (cleanup): {
66-
goto -> bb6;
67-
}
68-
69-
bb6 (cleanup): {
70-
drop(_2) -> [return: bb7, unwind terminate(cleanup)];
61+
bb4 (cleanup): {
62+
drop(_2) -> [return: bb5, unwind terminate(cleanup)];
7163
}
7264

73-
bb7 (cleanup): {
65+
bb5 (cleanup): {
7466
resume;
7567
}
7668
}

0 commit comments

Comments
 (0)