Skip to content

Commit 4c3e06a

Browse files
committed
simplify-locals: Remove unused assignments regardless of rvalue kind
1 parent 52d3782 commit 4c3e06a

13 files changed

+36
-69
lines changed

compiler/rustc_mir/src/transform/simplify.rs

+1-25
Original file line numberDiff line numberDiff line change
@@ -439,31 +439,7 @@ impl Visitor<'_> for UsedLocals {
439439
StatementKind::StorageLive(_local) | StatementKind::StorageDead(_local) => {}
440440

441441
StatementKind::Assign(box (ref place, ref rvalue)) => {
442-
let can_skip = match rvalue {
443-
Rvalue::Use(_)
444-
| Rvalue::Discriminant(_)
445-
| Rvalue::BinaryOp(_, _, _)
446-
| Rvalue::CheckedBinaryOp(_, _, _)
447-
| Rvalue::Repeat(_, _)
448-
| Rvalue::AddressOf(_, _)
449-
| Rvalue::Len(_)
450-
| Rvalue::UnaryOp(_, _)
451-
| Rvalue::Aggregate(_, _) => true,
452-
453-
Rvalue::Ref(..)
454-
| Rvalue::ThreadLocalRef(..)
455-
| Rvalue::Cast(..)
456-
| Rvalue::NullaryOp(..) => false,
457-
};
458-
if can_skip {
459-
self.visit_lhs(place, location);
460-
} else {
461-
self.visit_place(
462-
place,
463-
PlaceContext::MutatingUse(MutatingUseContext::Store),
464-
location,
465-
);
466-
}
442+
self.visit_lhs(place, location);
467443
self.visit_rvalue(rvalue, location);
468444
}
469445

src/test/codegen-units/item-collection/instantiation-through-vtable.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
// ignore-tidy-linelength
2-
// compile-flags:-Zprint-mono-items=eager
3-
// compile-flags:-Zinline-in-all-cgus
2+
// compile-flags:-Zprint-mono-items=eager -Zinline-in-all-cgus -Zmir-opt-level=0
43

54
#![deny(dead_code)]
65
#![feature(start)]

src/test/codegen/lifetime_start_end.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// compile-flags: -O -C no-prepopulate-passes
1+
// compile-flags: -O -C no-prepopulate-passes -Zmir-opt-level=0
22

33
#![crate_type = "lib"]
44

@@ -18,10 +18,10 @@ pub fn test() {
1818
// CHECK: [[S_b:%[0-9]+]] = bitcast { i32, i32 }** %b to i8*
1919
// CHECK: call void @llvm.lifetime.start{{.*}}(i{{[0-9 ]+}}, i8* [[S_b]])
2020

21-
// CHECK: [[S__4:%[0-9]+]] = bitcast { i32, i32 }* %_4 to i8*
21+
// CHECK: [[S__4:%[0-9]+]] = bitcast { i32, i32 }* %_5 to i8*
2222
// CHECK: call void @llvm.lifetime.start{{.*}}(i{{[0-9 ]+}}, i8* [[S__4]])
2323

24-
// CHECK: [[E__4:%[0-9]+]] = bitcast { i32, i32 }* %_4 to i8*
24+
// CHECK: [[E__4:%[0-9]+]] = bitcast { i32, i32 }* %_5 to i8*
2525
// CHECK: call void @llvm.lifetime.end{{.*}}(i{{[0-9 ]+}}, i8* [[E__4]])
2626

2727
// CHECK: [[E_b:%[0-9]+]] = bitcast { i32, i32 }** %b to i8*

src/test/codegen/loads.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// compile-flags: -C no-prepopulate-passes
1+
// compile-flags: -C no-prepopulate-passes -Zmir-opt-level=0
22

33
#![crate_type = "lib"]
44

src/test/codegen/naked-functions.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// compile-flags: -C no-prepopulate-passes
1+
// compile-flags: -C no-prepopulate-passes -Zmir-opt-level=0
22

33
#![crate_type = "lib"]
44
#![feature(naked_functions)]

src/test/codegen/refs.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// compile-flags: -C no-prepopulate-passes
1+
// compile-flags: -C no-prepopulate-passes -Zmir-opt-level=0
22

33
#![crate_type = "lib"]
44

src/test/incremental/hashes/closure_expressions.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
// build-pass (FIXME(62277): could be check-pass?)
99
// revisions: cfail1 cfail2 cfail3
10-
// compile-flags: -Z query-dep-graph -Zincremental-ignore-spans
10+
// compile-flags: -Z query-dep-graph -Zincremental-ignore-spans -Zmir-opt-level=0
1111

1212
#![allow(warnings)]
1313
#![feature(rustc_attrs)]
@@ -53,7 +53,7 @@ pub fn change_parameter_pattern() {
5353
}
5454

5555
#[cfg(not(cfail1))]
56-
#[rustc_clean(cfg="cfail2", except="hir_owner_nodes, typeck")]
56+
#[rustc_clean(cfg="cfail2", except="hir_owner_nodes, typeck, optimized_mir")]
5757
#[rustc_clean(cfg="cfail3")]
5858
pub fn change_parameter_pattern() {
5959
let _ = |(x,): (u32,)| x;

src/test/mir-opt/simplify_locals.c.SimplifyLocals.diff

+6-6
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
fn c() -> () {
55
let mut _0: (); // return place in scope 0 at $DIR/simplify-locals.rs:13:8: 13:8
66
let _1: [u8; 10]; // in scope 0 at $DIR/simplify-locals.rs:14:9: 14:14
7-
let mut _2: &[u8]; // in scope 0 at $DIR/simplify-locals.rs:16:20: 16:26
8-
let mut _3: &[u8; 10]; // in scope 0 at $DIR/simplify-locals.rs:16:20: 16:26
7+
- let mut _2: &[u8]; // in scope 0 at $DIR/simplify-locals.rs:16:20: 16:26
8+
- let mut _3: &[u8; 10]; // in scope 0 at $DIR/simplify-locals.rs:16:20: 16:26
99
- let _4: &[u8; 10]; // in scope 0 at $DIR/simplify-locals.rs:16:20: 16:26
1010
scope 1 {
1111
debug bytes => _1; // in scope 1 at $DIR/simplify-locals.rs:14:9: 14:14
@@ -16,10 +16,10 @@
1616
bb0: {
1717
StorageLive(_1); // scope 0 at $DIR/simplify-locals.rs:14:9: 14:14
1818
_1 = [const 0_u8; 10]; // scope 0 at $DIR/simplify-locals.rs:14:17: 14:26
19-
StorageLive(_2); // scope 1 at $DIR/simplify-locals.rs:16:20: 16:26
20-
_3 = &_1; // scope 1 at $DIR/simplify-locals.rs:16:20: 16:26
21-
_2 = move _3 as &[u8] (Pointer(Unsize)); // scope 1 at $DIR/simplify-locals.rs:16:20: 16:26
22-
StorageDead(_2); // scope 1 at $DIR/simplify-locals.rs:16:26: 16:27
19+
- StorageLive(_2); // scope 1 at $DIR/simplify-locals.rs:16:20: 16:26
20+
- _3 = &_1; // scope 1 at $DIR/simplify-locals.rs:16:20: 16:26
21+
- _2 = move _3 as &[u8] (Pointer(Unsize)); // scope 1 at $DIR/simplify-locals.rs:16:20: 16:26
22+
- StorageDead(_2); // scope 1 at $DIR/simplify-locals.rs:16:26: 16:27
2323
_0 = const (); // scope 0 at $DIR/simplify-locals.rs:13:8: 17:2
2424
StorageDead(_1); // scope 0 at $DIR/simplify-locals.rs:17:1: 17:2
2525
return; // scope 0 at $DIR/simplify-locals.rs:17:2: 17:2

src/test/mir-opt/simplify_locals.r.SimplifyLocals.diff

+8-8
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
fn r() -> () {
55
let mut _0: (); // return place in scope 0 at $DIR/simplify-locals.rs:32:8: 32:8
66
let mut _1: i32; // in scope 0 at $DIR/simplify-locals.rs:33:9: 33:14
7-
let mut _2: &i32; // in scope 0 at $DIR/simplify-locals.rs:35:13: 35:15
8-
let mut _3: &mut i32; // in scope 0 at $DIR/simplify-locals.rs:36:13: 36:19
7+
- let mut _2: &i32; // in scope 0 at $DIR/simplify-locals.rs:35:13: 35:15
8+
- let mut _3: &mut i32; // in scope 0 at $DIR/simplify-locals.rs:36:13: 36:19
99
scope 1 {
1010
debug a => _1; // in scope 1 at $DIR/simplify-locals.rs:33:9: 33:14
1111
scope 2 {
@@ -17,12 +17,12 @@
1717
bb0: {
1818
StorageLive(_1); // scope 0 at $DIR/simplify-locals.rs:33:9: 33:14
1919
_1 = const 1_i32; // scope 0 at $DIR/simplify-locals.rs:33:17: 33:18
20-
StorageLive(_2); // scope 1 at $DIR/simplify-locals.rs:35:13: 35:15
21-
_2 = &_1; // scope 1 at $DIR/simplify-locals.rs:35:13: 35:15
22-
StorageDead(_2); // scope 1 at $DIR/simplify-locals.rs:35:15: 35:16
23-
StorageLive(_3); // scope 2 at $DIR/simplify-locals.rs:36:13: 36:19
24-
_3 = &mut _1; // scope 2 at $DIR/simplify-locals.rs:36:13: 36:19
25-
StorageDead(_3); // scope 2 at $DIR/simplify-locals.rs:36:19: 36:20
20+
- StorageLive(_2); // scope 1 at $DIR/simplify-locals.rs:35:13: 35:15
21+
- _2 = &_1; // scope 1 at $DIR/simplify-locals.rs:35:13: 35:15
22+
- StorageDead(_2); // scope 1 at $DIR/simplify-locals.rs:35:15: 35:16
23+
- StorageLive(_3); // scope 2 at $DIR/simplify-locals.rs:36:13: 36:19
24+
- _3 = &mut _1; // scope 2 at $DIR/simplify-locals.rs:36:13: 36:19
25+
- StorageDead(_3); // scope 2 at $DIR/simplify-locals.rs:36:19: 36:20
2626
_0 = const (); // scope 0 at $DIR/simplify-locals.rs:32:8: 37:2
2727
StorageDead(_1); // scope 0 at $DIR/simplify-locals.rs:37:1: 37:2
2828
return; // scope 0 at $DIR/simplify-locals.rs:37:2: 37:2

src/test/mir-opt/simplify_locals.t1.SimplifyLocals.diff

+1-4
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
let mut _0: (); // return place in scope 0 at $DIR/simplify-locals.rs:42:9: 42:9
66
- let _1: u32; // in scope 0 at $DIR/simplify-locals.rs:44:14: 44:15
77
- let mut _2: *mut u32; // in scope 0 at $DIR/simplify-locals.rs:44:14: 44:15
8-
+ let mut _1: *mut u32; // in scope 0 at $DIR/simplify-locals.rs:44:14: 44:15
98
scope 1 {
109
}
1110

@@ -15,9 +14,7 @@
1514
- _2 = &/*tls*/ mut X; // scope 1 at $DIR/simplify-locals.rs:44:14: 44:15
1615
- _1 = (*_2); // scope 1 at $DIR/simplify-locals.rs:44:14: 44:15
1716
- StorageDead(_2); // scope 0 at $DIR/simplify-locals.rs:44:17: 44:18
18-
+ StorageLive(_1); // scope 1 at $DIR/simplify-locals.rs:44:14: 44:15
19-
+ _1 = &/*tls*/ mut X; // scope 1 at $DIR/simplify-locals.rs:44:14: 44:15
20-
StorageDead(_1); // scope 0 at $DIR/simplify-locals.rs:44:17: 44:18
17+
- StorageDead(_1); // scope 0 at $DIR/simplify-locals.rs:44:17: 44:18
2118
_0 = const (); // scope 0 at $DIR/simplify-locals.rs:42:9: 45:2
2219
return; // scope 0 at $DIR/simplify-locals.rs:45:2: 45:2
2320
}

src/test/mir-opt/simplify_locals.t2.SimplifyLocals.diff

+8-8
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,18 @@
33

44
fn t2() -> () {
55
let mut _0: (); // return place in scope 0 at $DIR/simplify-locals.rs:48:9: 48:9
6-
let _1: &mut u32; // in scope 0 at $DIR/simplify-locals.rs:50:14: 50:20
7-
let mut _2: *mut u32; // in scope 0 at $DIR/simplify-locals.rs:50:19: 50:20
6+
- let _1: &mut u32; // in scope 0 at $DIR/simplify-locals.rs:50:14: 50:20
7+
- let mut _2: *mut u32; // in scope 0 at $DIR/simplify-locals.rs:50:19: 50:20
88
scope 1 {
99
}
1010

1111
bb0: {
12-
StorageLive(_1); // scope 0 at $DIR/simplify-locals.rs:50:5: 50:22
13-
StorageLive(_2); // scope 1 at $DIR/simplify-locals.rs:50:19: 50:20
14-
_2 = &/*tls*/ mut X; // scope 1 at $DIR/simplify-locals.rs:50:19: 50:20
15-
_1 = &mut (*_2); // scope 1 at $DIR/simplify-locals.rs:50:14: 50:20
16-
StorageDead(_2); // scope 0 at $DIR/simplify-locals.rs:50:22: 50:23
17-
StorageDead(_1); // scope 0 at $DIR/simplify-locals.rs:50:22: 50:23
12+
- StorageLive(_1); // scope 0 at $DIR/simplify-locals.rs:50:5: 50:22
13+
- StorageLive(_2); // scope 1 at $DIR/simplify-locals.rs:50:19: 50:20
14+
- _2 = &/*tls*/ mut X; // scope 1 at $DIR/simplify-locals.rs:50:19: 50:20
15+
- _1 = &mut (*_2); // scope 1 at $DIR/simplify-locals.rs:50:14: 50:20
16+
- StorageDead(_2); // scope 0 at $DIR/simplify-locals.rs:50:22: 50:23
17+
- StorageDead(_1); // scope 0 at $DIR/simplify-locals.rs:50:22: 50:23
1818
_0 = const (); // scope 0 at $DIR/simplify-locals.rs:48:9: 51:2
1919
return; // scope 0 at $DIR/simplify-locals.rs:51:2: 51:2
2020
}

src/test/mir-opt/simplify_locals.t3.SimplifyLocals.diff

+2-8
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@
66
- let _1: u32; // in scope 0 at $DIR/simplify-locals.rs:56:14: 56:21
77
- let mut _2: &mut u32; // in scope 0 at $DIR/simplify-locals.rs:56:15: 56:21
88
- let mut _3: *mut u32; // in scope 0 at $DIR/simplify-locals.rs:56:20: 56:21
9-
+ let mut _1: &mut u32; // in scope 0 at $DIR/simplify-locals.rs:56:15: 56:21
10-
+ let mut _2: *mut u32; // in scope 0 at $DIR/simplify-locals.rs:56:20: 56:21
119
scope 1 {
1210
}
1311

@@ -19,12 +17,8 @@
1917
- _2 = &mut (*_3); // scope 1 at $DIR/simplify-locals.rs:56:15: 56:21
2018
- _1 = (*_2); // scope 1 at $DIR/simplify-locals.rs:56:14: 56:21
2119
- StorageDead(_3); // scope 0 at $DIR/simplify-locals.rs:56:23: 56:24
22-
+ StorageLive(_1); // scope 1 at $DIR/simplify-locals.rs:56:15: 56:21
23-
+ StorageLive(_2); // scope 1 at $DIR/simplify-locals.rs:56:20: 56:21
24-
+ _2 = &/*tls*/ mut X; // scope 1 at $DIR/simplify-locals.rs:56:20: 56:21
25-
+ _1 = &mut (*_2); // scope 1 at $DIR/simplify-locals.rs:56:15: 56:21
26-
StorageDead(_2); // scope 0 at $DIR/simplify-locals.rs:56:23: 56:24
27-
StorageDead(_1); // scope 0 at $DIR/simplify-locals.rs:56:23: 56:24
20+
- StorageDead(_2); // scope 0 at $DIR/simplify-locals.rs:56:23: 56:24
21+
- StorageDead(_1); // scope 0 at $DIR/simplify-locals.rs:56:23: 56:24
2822
_0 = const (); // scope 0 at $DIR/simplify-locals.rs:54:9: 57:2
2923
return; // scope 0 at $DIR/simplify-locals.rs:57:2: 57:2
3024
}

src/test/ui/recursion/issue-38591-non-regular-dropck-recursion.rs

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
// build-fail
77
// normalize-stderr-test: ".nll/" -> "/"
8+
// compile-flags: -Zmir-opt-level=0
89

910
struct S<T> {
1011
t: T,

0 commit comments

Comments
 (0)