Skip to content

Commit 7c0802b

Browse files
committed
Remove other Rvalues
1 parent 0e0a71a commit 7c0802b

File tree

6 files changed

+120
-145
lines changed

6 files changed

+120
-145
lines changed

src/librustc_mir/transform/simplify.rs

+30-12
Original file line numberDiff line numberDiff line change
@@ -368,21 +368,39 @@ impl<'a, 'tcx> Visitor<'tcx> for DeclMarker<'a, 'tcx> {
368368
if location.statement_index != block.statements.len() {
369369
let stmt = &block.statements[location.statement_index];
370370

371+
fn can_skip_constant(c: &ty::Const<'tcx>) -> bool {
372+
// Keep assignments from unevaluated constants around, since the
373+
// evaluation may report errors, even if the use of the constant
374+
// is dead code.
375+
!matches!(c.val, ty::ConstKind::Unevaluated(..))
376+
}
377+
378+
fn can_skip_operand(o: &Operand<'_>) -> bool {
379+
match o {
380+
Operand::Copy(p) | Operand::Move(p) => !p.is_indirect(),
381+
Operand::Constant(c) => can_skip_constant(c.literal),
382+
}
383+
}
384+
371385
if let StatementKind::Assign(box (dest, rvalue)) = &stmt.kind {
372386
if !dest.is_indirect() && dest.local == *local {
373-
if let Rvalue::Use(Operand::Constant(c)) = rvalue {
374-
match c.literal.val {
375-
// Keep assignments from unevaluated constants around, since the
376-
// evaluation may report errors, even if the use of the constant
377-
// is dead code.
378-
ty::ConstKind::Unevaluated(..) => {}
379-
_ => {
380-
trace!("skipping store of const value {:?} to {:?}", c, dest);
381-
return;
382-
}
387+
let can_skip = match rvalue {
388+
Rvalue::Use(op) => can_skip_operand(op),
389+
Rvalue::Discriminant(_) => true,
390+
Rvalue::BinaryOp(_, l, r) | Rvalue::CheckedBinaryOp(_, l, r) => {
391+
can_skip_operand(l) && can_skip_operand(r)
383392
}
384-
} else if let Rvalue::Discriminant(d) = rvalue {
385-
trace!("skipping store of discriminant value {:?} to {:?}", d, dest);
393+
Rvalue::Repeat(op, c) => can_skip_operand(op) && can_skip_constant(c),
394+
Rvalue::AddressOf(_, _) => true,
395+
Rvalue::Len(_) => true,
396+
Rvalue::UnaryOp(_, op) => can_skip_operand(op),
397+
Rvalue::Aggregate(_, operands) => operands.iter().all(can_skip_operand),
398+
399+
_ => false,
400+
};
401+
402+
if can_skip {
403+
trace!("skipping store of {:?} to {:?}", rvalue, dest);
386404
return;
387405
}
388406
}

src/test/mir-opt/const_allocation3/32bit/rustc.main.ConstProp.after.mir

+8-8
Original file line numberDiff line numberDiff line change
@@ -30,30 +30,30 @@ fn main() -> () {
3030
}
3131

3232
alloc0 (static: FOO, size: 4, align: 4) {
33-
alloc10+0╼ │ ╾──╼
33+
alloc9+0─╼ │ ╾──╼
3434
}
3535

36-
alloc10 (size: 168, align: 1) {
36+
alloc9 (size: 168, align: 1) {
3737
0x00 │ ab ab ab ab ab ab ab ab ab ab ab ab ab ab ab ab │ ................
38-
0x10 │ ab ab ab ab ab ab ab ab ab ab ab ab ╾alloc5+0─╼ │ ............╾──╼
38+
0x10 │ ab ab ab ab ab ab ab ab ab ab ab ab ╾alloc4+0─╼ │ ............╾──╼
3939
0x20 │ 01 ef cd ab 00 00 00 00 00 00 00 00 00 00 00 00 │ ................
4040
0x30 │ 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 │ ................
4141
0x40 │ 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 │ ................
4242
0x50 │ 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 │ ................
4343
0x60 │ 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 │ ................
4444
0x70 │ 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 │ ................
45-
0x80 │ 00 00 00 00 00 00 00 00 00 00 ╾alloc7+0─╼ 00 00 │ ..........╾──╼..
46-
0x90 │ ╾alloc8+99╼ 00 00 00 00 00 00 00 00 00 00 00 00 │ ╾──╼............
45+
0x80 │ 00 00 00 00 00 00 00 00 00 00 ╾alloc6+0─╼ 00 00 │ ..........╾──╼..
46+
0x90 │ ╾alloc7+99╼ 00 00 00 00 00 00 00 00 00 00 00 00 │ ╾──╼............
4747
0xa0 │ 00 00 00 00 00 00 00 00 │ ........
4848
}
4949

50-
alloc5 (size: 4, align: 4) {
50+
alloc4 (size: 4, align: 4) {
5151
2a 00 00 00 │ *...
5252
}
5353

54-
alloc7 (fn: main)
54+
alloc6 (fn: main)
5555

56-
alloc8 (size: 100, align: 1) {
56+
alloc7 (size: 100, align: 1) {
5757
0x00 │ 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 │ ................
5858
0x10 │ 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 │ ................
5959
0x20 │ 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 │ ................

src/test/mir-opt/const_allocation3/64bit/rustc.main.ConstProp.after.mir

+7-7
Original file line numberDiff line numberDiff line change
@@ -30,31 +30,31 @@ fn main() -> () {
3030
}
3131

3232
alloc0 (static: FOO, size: 8, align: 8) {
33-
╾──────alloc10+0──────╼ │ ╾──────╼
33+
╾──────alloc9+0───────╼ │ ╾──────╼
3434
}
3535

36-
alloc10 (size: 180, align: 1) {
36+
alloc9 (size: 180, align: 1) {
3737
0x00 │ ab ab ab ab ab ab ab ab ab ab ab ab ab ab ab ab │ ................
38-
0x10 │ ab ab ab ab ab ab ab ab ab ab ab ab ╾─alloc5+0─ │ ............╾───
38+
0x10 │ ab ab ab ab ab ab ab ab ab ab ab ab ╾─alloc4+0─ │ ............╾───
3939
0x20 │ ──────────╼ 01 ef cd ab 00 00 00 00 00 00 00 00 │ ───╼............
4040
0x30 │ 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 │ ................
4141
0x40 │ 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 │ ................
4242
0x50 │ 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 │ ................
4343
0x60 │ 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 │ ................
4444
0x70 │ 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 │ ................
4545
0x80 │ 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ╾──── │ ..............╾─
46-
0x90 │ ────alloc7+0────╼ 00 00 ╾──────alloc8+99──────╼ │ ─────╼..╾──────╼
46+
0x90 │ ────alloc6+0────╼ 00 00 ╾──────alloc7+99──────╼ │ ─────╼..╾──────╼
4747
0xa0 │ 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 │ ................
4848
0xb0 │ 00 00 00 00 │ ....
4949
}
5050

51-
alloc5 (size: 4, align: 4) {
51+
alloc4 (size: 4, align: 4) {
5252
2a 00 00 00 │ *...
5353
}
5454

55-
alloc7 (fn: main)
55+
alloc6 (fn: main)
5656

57-
alloc8 (size: 100, align: 1) {
57+
alloc7 (size: 100, align: 1) {
5858
0x00 │ 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 │ ................
5959
0x10 │ 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 │ ................
6060
0x20 │ 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 │ ................

src/test/mir-opt/const_prop/optimizes_into_variable/32bit/rustc.main.SimplifyLocals.after.mir

+10-50
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,14 @@
33
fn main() -> () {
44
let mut _0: (); // return place in scope 0 at $DIR/optimizes_into_variable.rs:11:11: 11:11
55
let _1: i32; // in scope 0 at $DIR/optimizes_into_variable.rs:12:9: 12:10
6-
let mut _3: [i32; 6]; // in scope 0 at $DIR/optimizes_into_variable.rs:13:13: 13:31
76
scope 1 {
87
debug x => _1; // in scope 1 at $DIR/optimizes_into_variable.rs:12:9: 12:10
98
let _2: i32; // in scope 1 at $DIR/optimizes_into_variable.rs:13:9: 13:10
109
scope 2 {
1110
debug y => _2; // in scope 2 at $DIR/optimizes_into_variable.rs:13:9: 13:10
12-
let _4: u32; // in scope 2 at $DIR/optimizes_into_variable.rs:14:9: 14:10
11+
let _3: u32; // in scope 2 at $DIR/optimizes_into_variable.rs:14:9: 14:10
1312
scope 3 {
14-
debug z => _4; // in scope 3 at $DIR/optimizes_into_variable.rs:14:9: 14:10
13+
debug z => _3; // in scope 3 at $DIR/optimizes_into_variable.rs:14:9: 14:10
1514
}
1615
}
1716
}
@@ -26,70 +25,31 @@ fn main() -> () {
2625
// + span: $DIR/optimizes_into_variable.rs:12:13: 12:18
2726
// + literal: Const { ty: i32, val: Value(Scalar(0x00000004)) }
2827
StorageLive(_2); // bb0[2]: scope 1 at $DIR/optimizes_into_variable.rs:13:9: 13:10
29-
StorageLive(_3); // bb0[3]: scope 1 at $DIR/optimizes_into_variable.rs:13:13: 13:31
30-
_3 = [const 0i32, const 1i32, const 2i32, const 3i32, const 4i32, const 5i32]; // bb0[4]: scope 1 at $DIR/optimizes_into_variable.rs:13:13: 13:31
31-
// ty::Const
32-
// + ty: i32
33-
// + val: Value(Scalar(0x00000000))
34-
// mir::Constant
35-
// + span: $DIR/optimizes_into_variable.rs:13:14: 13:15
36-
// + literal: Const { ty: i32, val: Value(Scalar(0x00000000)) }
37-
// ty::Const
38-
// + ty: i32
39-
// + val: Value(Scalar(0x00000001))
40-
// mir::Constant
41-
// + span: $DIR/optimizes_into_variable.rs:13:17: 13:18
42-
// + literal: Const { ty: i32, val: Value(Scalar(0x00000001)) }
43-
// ty::Const
44-
// + ty: i32
45-
// + val: Value(Scalar(0x00000002))
46-
// mir::Constant
47-
// + span: $DIR/optimizes_into_variable.rs:13:20: 13:21
48-
// + literal: Const { ty: i32, val: Value(Scalar(0x00000002)) }
49-
// ty::Const
50-
// + ty: i32
51-
// + val: Value(Scalar(0x00000003))
52-
// mir::Constant
53-
// + span: $DIR/optimizes_into_variable.rs:13:23: 13:24
54-
// + literal: Const { ty: i32, val: Value(Scalar(0x00000003)) }
55-
// ty::Const
56-
// + ty: i32
57-
// + val: Value(Scalar(0x00000004))
58-
// mir::Constant
59-
// + span: $DIR/optimizes_into_variable.rs:13:26: 13:27
60-
// + literal: Const { ty: i32, val: Value(Scalar(0x00000004)) }
61-
// ty::Const
62-
// + ty: i32
63-
// + val: Value(Scalar(0x00000005))
64-
// mir::Constant
65-
// + span: $DIR/optimizes_into_variable.rs:13:29: 13:30
66-
// + literal: Const { ty: i32, val: Value(Scalar(0x00000005)) }
67-
_2 = const 3i32; // bb0[5]: scope 1 at $DIR/optimizes_into_variable.rs:13:13: 13:34
28+
_2 = const 3i32; // bb0[3]: scope 1 at $DIR/optimizes_into_variable.rs:13:13: 13:34
6829
// ty::Const
6930
// + ty: i32
7031
// + val: Value(Scalar(0x00000003))
7132
// mir::Constant
7233
// + span: $DIR/optimizes_into_variable.rs:13:13: 13:34
7334
// + literal: Const { ty: i32, val: Value(Scalar(0x00000003)) }
74-
StorageDead(_3); // bb0[6]: scope 1 at $DIR/optimizes_into_variable.rs:13:34: 13:35
75-
StorageLive(_4); // bb0[7]: scope 2 at $DIR/optimizes_into_variable.rs:14:9: 14:10
76-
_4 = const 42u32; // bb0[8]: scope 2 at $DIR/optimizes_into_variable.rs:14:13: 14:38
35+
StorageLive(_3); // bb0[4]: scope 2 at $DIR/optimizes_into_variable.rs:14:9: 14:10
36+
_3 = const 42u32; // bb0[5]: scope 2 at $DIR/optimizes_into_variable.rs:14:13: 14:38
7737
// ty::Const
7838
// + ty: u32
7939
// + val: Value(Scalar(0x0000002a))
8040
// mir::Constant
8141
// + span: $DIR/optimizes_into_variable.rs:14:13: 14:38
8242
// + literal: Const { ty: u32, val: Value(Scalar(0x0000002a)) }
83-
_0 = const (); // bb0[9]: scope 0 at $DIR/optimizes_into_variable.rs:11:11: 15:2
43+
_0 = const (); // bb0[6]: scope 0 at $DIR/optimizes_into_variable.rs:11:11: 15:2
8444
// ty::Const
8545
// + ty: ()
8646
// + val: Value(Scalar(<ZST>))
8747
// mir::Constant
8848
// + span: $DIR/optimizes_into_variable.rs:11:11: 15:2
8949
// + literal: Const { ty: (), val: Value(Scalar(<ZST>)) }
90-
StorageDead(_4); // bb0[10]: scope 2 at $DIR/optimizes_into_variable.rs:15:1: 15:2
91-
StorageDead(_2); // bb0[11]: scope 1 at $DIR/optimizes_into_variable.rs:15:1: 15:2
92-
StorageDead(_1); // bb0[12]: scope 0 at $DIR/optimizes_into_variable.rs:15:1: 15:2
93-
return; // bb0[13]: scope 0 at $DIR/optimizes_into_variable.rs:15:2: 15:2
50+
StorageDead(_3); // bb0[7]: scope 2 at $DIR/optimizes_into_variable.rs:15:1: 15:2
51+
StorageDead(_2); // bb0[8]: scope 1 at $DIR/optimizes_into_variable.rs:15:1: 15:2
52+
StorageDead(_1); // bb0[9]: scope 0 at $DIR/optimizes_into_variable.rs:15:1: 15:2
53+
return; // bb0[10]: scope 0 at $DIR/optimizes_into_variable.rs:15:2: 15:2
9454
}
9555
}

0 commit comments

Comments
 (0)