Skip to content

Commit 49f4399

Browse files
committed
Generalize operation.
1 parent 2a3b0e3 commit 49f4399

24 files changed

+105
-105
lines changed

compiler/rustc_mir_transform/src/remove_zsts.rs

+23-14
Original file line numberDiff line numberDiff line change
@@ -110,21 +110,30 @@ impl<'tcx> MutVisitor<'tcx> for Replacer<'_, 'tcx> {
110110
}
111111

112112
fn visit_statement(&mut self, statement: &mut Statement<'tcx>, loc: Location) {
113-
if let StatementKind::Assign(box (place, _)) | StatementKind::Deinit(box place) =
114-
statement.kind
115-
{
116-
let place_ty = place.ty(self.local_decls, self.tcx).ty;
117-
if self.known_to_be_zst(place_ty)
118-
&& self.tcx.consider_optimizing(|| {
119-
format!(
120-
"RemoveZsts - Place: {:?} SourceInfo: {:?}",
121-
place, statement.source_info
122-
)
123-
})
124-
{
125-
statement.make_nop();
113+
let place_for_ty = match statement.kind {
114+
StatementKind::Assign(box (place, ref rvalue)) => {
115+
rvalue.is_safe_to_remove().then_some(place)
116+
}
117+
StatementKind::Deinit(box place)
118+
| StatementKind::SetDiscriminant { box place, variant_index: _ }
119+
| StatementKind::AscribeUserType(box (place, _), _)
120+
| StatementKind::Retag(_, box place)
121+
| StatementKind::FakeRead(box (_, place)) => Some(place),
122+
StatementKind::StorageLive(local) | StatementKind::StorageDead(local) => {
123+
Some(local.into())
126124
}
125+
StatementKind::Coverage(_) | StatementKind::Intrinsic(_) | StatementKind::Nop => None,
126+
};
127+
if let Some(place_for_ty) = place_for_ty
128+
&& let ty = place_for_ty.ty(self.local_decls, self.tcx).ty
129+
&& self.known_to_be_zst(ty)
130+
&& self.tcx.consider_optimizing(|| {
131+
format!("RemoveZsts - Place: {:?} SourceInfo: {:?}", place_for_ty, statement.source_info)
132+
})
133+
{
134+
statement.make_nop();
135+
} else {
136+
self.super_statement(statement, loc);
127137
}
128-
self.super_statement(statement, loc);
129138
}
130139
}

tests/mir-opt/const_prop/control_flow_simplification.hello.ConstProp.diff

-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
}
1515

1616
bb1: {
17-
StorageLive(_2); // scope 0 at $SRC_DIR/std/src/panic.rs:LL:COL
1817
_2 = begin_panic::<&str>(const "explicit panic"); // scope 0 at $SRC_DIR/std/src/panic.rs:LL:COL
1918
// mir::Constant
2019
// + span: $SRC_DIR/std/src/panic.rs:LL:COL

tests/mir-opt/const_prop/invalid_constant.main.RemoveZsts.diff

+12-6
Original file line numberDiff line numberDiff line change
@@ -49,22 +49,28 @@
4949
_3 = [move _4]; // scope 1 at $DIR/invalid_constant.rs:+13:24: +13:60
5050
StorageDead(_4); // scope 1 at $DIR/invalid_constant.rs:+13:59: +13:60
5151
StorageDead(_5); // scope 1 at $DIR/invalid_constant.rs:+13:60: +13:61
52-
StorageLive(_6); // scope 3 at $DIR/invalid_constant.rs:+20:9: +20:31
53-
StorageLive(_7); // scope 3 at $DIR/invalid_constant.rs:+20:35: +20:73
52+
- StorageLive(_6); // scope 3 at $DIR/invalid_constant.rs:+20:9: +20:31
53+
- StorageLive(_7); // scope 3 at $DIR/invalid_constant.rs:+20:35: +20:73
54+
+ nop; // scope 3 at $DIR/invalid_constant.rs:+20:9: +20:31
55+
+ nop; // scope 3 at $DIR/invalid_constant.rs:+20:35: +20:73
5456
StorageLive(_8); // scope 6 at $DIR/invalid_constant.rs:+20:44: +20:65
5557
Deinit(_8); // scope 6 at $DIR/invalid_constant.rs:+20:44: +20:65
5658
(_8.0: u32) = const 0_u32; // scope 6 at $DIR/invalid_constant.rs:+20:44: +20:65
5759
- _7 = (_8.1: Empty); // scope 6 at $DIR/invalid_constant.rs:+20:44: +20:71
5860
- _6 = [move _7]; // scope 3 at $DIR/invalid_constant.rs:+20:34: +20:74
61+
- StorageDead(_7); // scope 3 at $DIR/invalid_constant.rs:+20:73: +20:74
5962
+ nop; // scope 6 at $DIR/invalid_constant.rs:+20:44: +20:71
6063
+ nop; // scope 3 at $DIR/invalid_constant.rs:+20:34: +20:74
61-
StorageDead(_7); // scope 3 at $DIR/invalid_constant.rs:+20:73: +20:74
64+
+ nop; // scope 3 at $DIR/invalid_constant.rs:+20:73: +20:74
6265
StorageDead(_8); // scope 3 at $DIR/invalid_constant.rs:+20:74: +20:75
63-
StorageLive(_9); // scope 5 at $DIR/invalid_constant.rs:+24:9: +24:22
66+
- StorageLive(_9); // scope 5 at $DIR/invalid_constant.rs:+24:9: +24:22
6467
- _0 = const (); // scope 0 at $DIR/invalid_constant.rs:+0:11: +27:2
68+
- StorageDead(_9); // scope 5 at $DIR/invalid_constant.rs:+27:1: +27:2
69+
- StorageDead(_6); // scope 3 at $DIR/invalid_constant.rs:+27:1: +27:2
70+
+ nop; // scope 5 at $DIR/invalid_constant.rs:+24:9: +24:22
6571
+ nop; // scope 0 at $DIR/invalid_constant.rs:+0:11: +27:2
66-
StorageDead(_9); // scope 5 at $DIR/invalid_constant.rs:+27:1: +27:2
67-
StorageDead(_6); // scope 3 at $DIR/invalid_constant.rs:+27:1: +27:2
72+
+ nop; // scope 5 at $DIR/invalid_constant.rs:+27:1: +27:2
73+
+ nop; // scope 3 at $DIR/invalid_constant.rs:+27:1: +27:2
6874
StorageDead(_3); // scope 1 at $DIR/invalid_constant.rs:+27:1: +27:2
6975
StorageDead(_1); // scope 0 at $DIR/invalid_constant.rs:+27:1: +27:2
7076
return; // scope 0 at $DIR/invalid_constant.rs:+27:2: +27:2

tests/mir-opt/const_prop/issue_66971.main.ConstProp.diff

-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
let mut _2: ((), u8, u8); // in scope 0 at $DIR/issue_66971.rs:+1:12: +1:22
88

99
bb0: {
10-
StorageLive(_1); // scope 0 at $DIR/issue_66971.rs:+1:5: +1:23
1110
StorageLive(_2); // scope 0 at $DIR/issue_66971.rs:+1:12: +1:22
1211
Deinit(_2); // scope 0 at $DIR/issue_66971.rs:+1:12: +1:22
1312
(_2.1: u8) = const 0_u8; // scope 0 at $DIR/issue_66971.rs:+1:12: +1:22
@@ -20,7 +19,6 @@
2019

2120
bb1: {
2221
StorageDead(_2); // scope 0 at $DIR/issue_66971.rs:+1:22: +1:23
23-
StorageDead(_1); // scope 0 at $DIR/issue_66971.rs:+1:23: +1:24
2422
return; // scope 0 at $DIR/issue_66971.rs:+2:2: +2:2
2523
}
2624
}

tests/mir-opt/const_prop/issue_67019.main.ConstProp.diff

-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
let mut _3: (u8, u8); // in scope 0 at $DIR/issue_67019.rs:+1:11: +1:17
99

1010
bb0: {
11-
StorageLive(_1); // scope 0 at $DIR/issue_67019.rs:+1:5: +1:20
1211
StorageLive(_2); // scope 0 at $DIR/issue_67019.rs:+1:10: +1:19
1312
StorageLive(_3); // scope 0 at $DIR/issue_67019.rs:+1:11: +1:17
1413
Deinit(_3); // scope 0 at $DIR/issue_67019.rs:+1:11: +1:17
@@ -26,7 +25,6 @@
2625

2726
bb1: {
2827
StorageDead(_2); // scope 0 at $DIR/issue_67019.rs:+1:19: +1:20
29-
StorageDead(_1); // scope 0 at $DIR/issue_67019.rs:+1:20: +1:21
3028
return; // scope 0 at $DIR/issue_67019.rs:+2:2: +2:2
3129
}
3230
}

tests/mir-opt/const_prop/scalar_literal_propagation.main.ConstProp.diff

-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
bb0: {
1414
StorageLive(_1); // scope 0 at $DIR/scalar_literal_propagation.rs:+1:9: +1:10
1515
_1 = const 1_u32; // scope 0 at $DIR/scalar_literal_propagation.rs:+1:13: +1:14
16-
StorageLive(_2); // scope 1 at $DIR/scalar_literal_propagation.rs:+2:5: +2:15
1716
StorageLive(_3); // scope 1 at $DIR/scalar_literal_propagation.rs:+2:13: +2:14
1817
- _3 = _1; // scope 1 at $DIR/scalar_literal_propagation.rs:+2:13: +2:14
1918
- _2 = consume(move _3) -> bb1; // scope 1 at $DIR/scalar_literal_propagation.rs:+2:5: +2:15
@@ -26,7 +25,6 @@
2625

2726
bb1: {
2827
StorageDead(_3); // scope 1 at $DIR/scalar_literal_propagation.rs:+2:14: +2:15
29-
StorageDead(_2); // scope 1 at $DIR/scalar_literal_propagation.rs:+2:15: +2:16
3028
StorageDead(_1); // scope 0 at $DIR/scalar_literal_propagation.rs:+3:1: +3:2
3129
return; // scope 0 at $DIR/scalar_literal_propagation.rs:+3:2: +3:2
3230
}

tests/mir-opt/const_prop/tuple_literal_propagation.main.ConstProp.diff

-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
Deinit(_1); // scope 0 at $DIR/tuple_literal_propagation.rs:+1:13: +1:19
1616
(_1.0: u32) = const 1_u32; // scope 0 at $DIR/tuple_literal_propagation.rs:+1:13: +1:19
1717
(_1.1: u32) = const 2_u32; // scope 0 at $DIR/tuple_literal_propagation.rs:+1:13: +1:19
18-
StorageLive(_2); // scope 1 at $DIR/tuple_literal_propagation.rs:+3:5: +3:15
1918
StorageLive(_3); // scope 1 at $DIR/tuple_literal_propagation.rs:+3:13: +3:14
2019
- _3 = _1; // scope 1 at $DIR/tuple_literal_propagation.rs:+3:13: +3:14
2120
+ _3 = const (1_u32, 2_u32); // scope 1 at $DIR/tuple_literal_propagation.rs:+3:13: +3:14
@@ -27,7 +26,6 @@
2726

2827
bb1: {
2928
StorageDead(_3); // scope 1 at $DIR/tuple_literal_propagation.rs:+3:14: +3:15
30-
StorageDead(_2); // scope 1 at $DIR/tuple_literal_propagation.rs:+3:15: +3:16
3129
StorageDead(_1); // scope 0 at $DIR/tuple_literal_propagation.rs:+4:1: +4:2
3230
return; // scope 0 at $DIR/tuple_literal_propagation.rs:+4:2: +4:2
3331
}

tests/mir-opt/inline/asm_unwind.main.Inline.diff

+7-7
Original file line numberDiff line numberDiff line change
@@ -19,21 +19,21 @@
1919
- // mir::Constant
2020
- // + span: $DIR/asm_unwind.rs:21:5: 21:8
2121
- // + literal: Const { ty: fn() {foo}, val: Value(<ZST>) }
22-
+ StorageLive(_2); // scope 1 at $DIR/asm_unwind.rs:15:9: 15:11
23-
+ asm!("", options(MAY_UNWIND)) -> [return: bb1, unwind: bb3]; // scope 3 at $DIR/asm_unwind.rs:16:14: 16:54
22+
+ StorageLive(_2); // scope 0 at $DIR/asm_unwind.rs:+1:5: +1:10
23+
+ asm!("", options(MAY_UNWIND)) -> [return: bb2, unwind: bb3]; // scope 3 at $DIR/asm_unwind.rs:16:14: 16:54
2424
}
2525

2626
bb1: {
27-
+ drop(_2) -> bb2; // scope 1 at $DIR/asm_unwind.rs:17:1: 17:2
28-
+ }
29-
+
30-
+ bb2: {
31-
+ StorageDead(_2); // scope 1 at $DIR/asm_unwind.rs:17:1: 17:2
27+
+ StorageDead(_2); // scope 0 at $DIR/asm_unwind.rs:+1:5: +1:10
3228
StorageDead(_1); // scope 0 at $DIR/asm_unwind.rs:+1:10: +1:11
3329
_0 = const (); // scope 0 at $DIR/asm_unwind.rs:+0:15: +2:2
3430
return; // scope 0 at $DIR/asm_unwind.rs:+2:2: +2:2
3531
+ }
3632
+
33+
+ bb2: {
34+
+ drop(_2) -> bb1; // scope 1 at $DIR/asm_unwind.rs:17:1: 17:2
35+
+ }
36+
+
3737
+ bb3 (cleanup): {
3838
+ drop(_2) -> bb4; // scope 1 at $DIR/asm_unwind.rs:17:1: 17:2
3939
+ }

tests/mir-opt/inline/cycle.g.Inline.diff

+2-2
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
- // mir::Constant
2626
// + span: $DIR/cycle.rs:12:7: 12:11
2727
// + literal: Const { ty: fn() {main}, val: Value(<ZST>) }
28-
+ StorageLive(_3); // scope 1 at $DIR/cycle.rs:6:5: 6:8
28+
+ StorageLive(_3); // scope 0 at $DIR/cycle.rs:+1:5: +1:12
2929
+ StorageLive(_4); // scope 1 at $DIR/cycle.rs:6:5: 6:6
3030
+ _4 = &_2; // scope 1 at $DIR/cycle.rs:6:5: 6:6
3131
+ StorageLive(_5); // scope 1 at $DIR/cycle.rs:6:5: 6:8
@@ -34,6 +34,7 @@
3434
}
3535

3636
bb1: {
37+
+ StorageDead(_3); // scope 0 at $DIR/cycle.rs:+1:5: +1:12
3738
+ StorageDead(_2); // scope 0 at $DIR/cycle.rs:+1:5: +1:12
3839
StorageDead(_1); // scope 0 at $DIR/cycle.rs:+1:12: +1:13
3940
_0 = const (); // scope 0 at $DIR/cycle.rs:+0:8: +2:2
@@ -51,7 +52,6 @@
5152
+ bb4: {
5253
+ StorageDead(_5); // scope 1 at $DIR/cycle.rs:6:5: 6:8
5354
+ StorageDead(_4); // scope 1 at $DIR/cycle.rs:6:7: 6:8
54-
+ StorageDead(_3); // scope 1 at $DIR/cycle.rs:6:8: 6:9
5555
+ drop(_2) -> bb1; // scope 1 at $DIR/cycle.rs:7:1: 7:2
5656
}
5757
}

tests/mir-opt/inline/cycle.main.Inline.diff

+2-2
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
- // mir::Constant
2626
// + span: $DIR/cycle.rs:17:7: 17:8
2727
// + literal: Const { ty: fn() {g}, val: Value(<ZST>) }
28-
+ StorageLive(_3); // scope 1 at $DIR/cycle.rs:6:5: 6:8
28+
+ StorageLive(_3); // scope 0 at $DIR/cycle.rs:+1:5: +1:9
2929
+ StorageLive(_4); // scope 1 at $DIR/cycle.rs:6:5: 6:6
3030
+ _4 = &_2; // scope 1 at $DIR/cycle.rs:6:5: 6:6
3131
+ StorageLive(_5); // scope 1 at $DIR/cycle.rs:6:5: 6:8
@@ -34,6 +34,7 @@
3434
}
3535

3636
bb1: {
37+
+ StorageDead(_3); // scope 0 at $DIR/cycle.rs:+1:5: +1:9
3738
+ StorageDead(_2); // scope 0 at $DIR/cycle.rs:+1:5: +1:9
3839
StorageDead(_1); // scope 0 at $DIR/cycle.rs:+1:9: +1:10
3940
_0 = const (); // scope 0 at $DIR/cycle.rs:+0:11: +2:2
@@ -51,7 +52,6 @@
5152
+ bb4: {
5253
+ StorageDead(_5); // scope 1 at $DIR/cycle.rs:6:5: 6:8
5354
+ StorageDead(_4); // scope 1 at $DIR/cycle.rs:6:7: 6:8
54-
+ StorageDead(_3); // scope 1 at $DIR/cycle.rs:6:8: 6:9
5555
+ drop(_2) -> bb1; // scope 1 at $DIR/cycle.rs:7:1: 7:2
5656
}
5757
}

tests/mir-opt/inline/exponential_runtime.main.Inline.diff

+28-28
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,13 @@
1818
bb0: {
1919
StorageLive(_1); // scope 0 at $DIR/exponential_runtime.rs:+1:5: +1:22
2020
- _1 = <() as G>::call() -> bb1; // scope 0 at $DIR/exponential_runtime.rs:+1:5: +1:22
21-
+ StorageLive(_2); // scope 1 at $DIR/exponential_runtime.rs:73:9: 73:25
22-
+ StorageLive(_5); // scope 2 at $DIR/exponential_runtime.rs:61:9: 61:25
23-
+ _5 = <() as E>::call() -> bb3; // scope 2 at $DIR/exponential_runtime.rs:61:9: 61:25
21+
+ StorageLive(_2); // scope 0 at $DIR/exponential_runtime.rs:+1:5: +1:22
22+
+ StorageLive(_3); // scope 0 at $DIR/exponential_runtime.rs:+1:5: +1:22
23+
+ StorageLive(_4); // scope 0 at $DIR/exponential_runtime.rs:+1:5: +1:22
24+
+ StorageLive(_5); // scope 1 at $DIR/exponential_runtime.rs:73:9: 73:25
25+
+ StorageLive(_6); // scope 1 at $DIR/exponential_runtime.rs:73:9: 73:25
26+
+ StorageLive(_7); // scope 1 at $DIR/exponential_runtime.rs:73:9: 73:25
27+
+ _5 = <() as E>::call() -> bb4; // scope 2 at $DIR/exponential_runtime.rs:61:9: 61:25
2428
// mir::Constant
2529
- // + span: $DIR/exponential_runtime.rs:86:5: 86:20
2630
- // + literal: Const { ty: fn() {<() as G>::call}, val: Value(<ZST>) }
@@ -29,47 +33,43 @@
2933
}
3034

3135
bb1: {
32-
+ StorageDead(_3); // scope 1 at $DIR/exponential_runtime.rs:74:25: 74:26
33-
+ StorageLive(_4); // scope 1 at $DIR/exponential_runtime.rs:75:9: 75:25
34-
+ _4 = <() as F>::call() -> bb2; // scope 1 at $DIR/exponential_runtime.rs:75:9: 75:25
35-
+ // mir::Constant
36-
+ // + span: $DIR/exponential_runtime.rs:75:9: 75:23
37-
+ // + literal: Const { ty: fn() {<() as F>::call}, val: Value(<ZST>) }
38-
+ }
39-
+
40-
+ bb2: {
41-
+ StorageDead(_4); // scope 1 at $DIR/exponential_runtime.rs:75:25: 75:26
36+
+ StorageDead(_4); // scope 0 at $DIR/exponential_runtime.rs:+1:5: +1:22
37+
+ StorageDead(_3); // scope 0 at $DIR/exponential_runtime.rs:+1:5: +1:22
38+
+ StorageDead(_2); // scope 0 at $DIR/exponential_runtime.rs:+1:5: +1:22
4239
StorageDead(_1); // scope 0 at $DIR/exponential_runtime.rs:+1:22: +1:23
4340
_0 = const (); // scope 0 at $DIR/exponential_runtime.rs:+0:11: +2:2
4441
return; // scope 0 at $DIR/exponential_runtime.rs:+2:2: +2:2
4542
+ }
4643
+
44+
+ bb2: {
45+
+ StorageDead(_7); // scope 1 at $DIR/exponential_runtime.rs:73:9: 73:25
46+
+ StorageDead(_6); // scope 1 at $DIR/exponential_runtime.rs:73:9: 73:25
47+
+ StorageDead(_5); // scope 1 at $DIR/exponential_runtime.rs:73:9: 73:25
48+
+ _3 = <() as F>::call() -> bb3; // scope 1 at $DIR/exponential_runtime.rs:74:9: 74:25
49+
+ // mir::Constant
50+
+ // + span: $DIR/exponential_runtime.rs:74:9: 74:23
51+
+ // + literal: Const { ty: fn() {<() as F>::call}, val: Value(<ZST>) }
52+
+ }
53+
+
4754
+ bb3: {
48-
+ StorageDead(_5); // scope 2 at $DIR/exponential_runtime.rs:61:25: 61:26
49-
+ StorageLive(_6); // scope 2 at $DIR/exponential_runtime.rs:62:9: 62:25
50-
+ _6 = <() as E>::call() -> bb4; // scope 2 at $DIR/exponential_runtime.rs:62:9: 62:25
55+
+ _4 = <() as F>::call() -> bb1; // scope 1 at $DIR/exponential_runtime.rs:75:9: 75:25
5156
+ // mir::Constant
52-
+ // + span: $DIR/exponential_runtime.rs:62:9: 62:23
53-
+ // + literal: Const { ty: fn() {<() as E>::call}, val: Value(<ZST>) }
57+
+ // + span: $DIR/exponential_runtime.rs:75:9: 75:23
58+
+ // + literal: Const { ty: fn() {<() as F>::call}, val: Value(<ZST>) }
5459
+ }
5560
+
5661
+ bb4: {
57-
+ StorageDead(_6); // scope 2 at $DIR/exponential_runtime.rs:62:25: 62:26
58-
+ StorageLive(_7); // scope 2 at $DIR/exponential_runtime.rs:63:9: 63:25
59-
+ _7 = <() as E>::call() -> bb5; // scope 2 at $DIR/exponential_runtime.rs:63:9: 63:25
62+
+ _6 = <() as E>::call() -> bb5; // scope 2 at $DIR/exponential_runtime.rs:62:9: 62:25
6063
+ // mir::Constant
61-
+ // + span: $DIR/exponential_runtime.rs:63:9: 63:23
64+
+ // + span: $DIR/exponential_runtime.rs:62:9: 62:23
6265
+ // + literal: Const { ty: fn() {<() as E>::call}, val: Value(<ZST>) }
6366
+ }
6467
+
6568
+ bb5: {
66-
+ StorageDead(_7); // scope 2 at $DIR/exponential_runtime.rs:63:25: 63:26
67-
+ StorageDead(_2); // scope 1 at $DIR/exponential_runtime.rs:73:25: 73:26
68-
+ StorageLive(_3); // scope 1 at $DIR/exponential_runtime.rs:74:9: 74:25
69-
+ _3 = <() as F>::call() -> bb1; // scope 1 at $DIR/exponential_runtime.rs:74:9: 74:25
69+
+ _7 = <() as E>::call() -> bb2; // scope 2 at $DIR/exponential_runtime.rs:63:9: 63:25
7070
+ // mir::Constant
71-
+ // + span: $DIR/exponential_runtime.rs:74:9: 74:23
72-
+ // + literal: Const { ty: fn() {<() as F>::call}, val: Value(<ZST>) }
71+
+ // + span: $DIR/exponential_runtime.rs:63:9: 63:23
72+
+ // + literal: Const { ty: fn() {<() as E>::call}, val: Value(<ZST>) }
7373
}
7474
}
7575

0 commit comments

Comments
 (0)