Skip to content

Commit

Permalink
simplify-locals: Remove unused assignments regardless of rvalue kind
Browse files Browse the repository at this point in the history
  • Loading branch information
tmiasko committed Oct 26, 2020
1 parent 52d3782 commit 4c3e06a
Show file tree
Hide file tree
Showing 13 changed files with 36 additions and 69 deletions.
26 changes: 1 addition & 25 deletions compiler/rustc_mir/src/transform/simplify.rs
Expand Up @@ -439,31 +439,7 @@ impl Visitor<'_> for UsedLocals {
StatementKind::StorageLive(_local) | StatementKind::StorageDead(_local) => {}

StatementKind::Assign(box (ref place, ref rvalue)) => {
let can_skip = match rvalue {
Rvalue::Use(_)
| Rvalue::Discriminant(_)
| Rvalue::BinaryOp(_, _, _)
| Rvalue::CheckedBinaryOp(_, _, _)
| Rvalue::Repeat(_, _)
| Rvalue::AddressOf(_, _)
| Rvalue::Len(_)
| Rvalue::UnaryOp(_, _)
| Rvalue::Aggregate(_, _) => true,

Rvalue::Ref(..)
| Rvalue::ThreadLocalRef(..)
| Rvalue::Cast(..)
| Rvalue::NullaryOp(..) => false,
};
if can_skip {
self.visit_lhs(place, location);
} else {
self.visit_place(
place,
PlaceContext::MutatingUse(MutatingUseContext::Store),
location,
);
}
self.visit_lhs(place, location);
self.visit_rvalue(rvalue, location);
}

Expand Down
@@ -1,6 +1,5 @@
// ignore-tidy-linelength
// compile-flags:-Zprint-mono-items=eager
// compile-flags:-Zinline-in-all-cgus
// compile-flags:-Zprint-mono-items=eager -Zinline-in-all-cgus -Zmir-opt-level=0

#![deny(dead_code)]
#![feature(start)]
Expand Down
6 changes: 3 additions & 3 deletions src/test/codegen/lifetime_start_end.rs
@@ -1,4 +1,4 @@
// compile-flags: -O -C no-prepopulate-passes
// compile-flags: -O -C no-prepopulate-passes -Zmir-opt-level=0

#![crate_type = "lib"]

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

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

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

// CHECK: [[E_b:%[0-9]+]] = bitcast { i32, i32 }** %b to i8*
Expand Down
2 changes: 1 addition & 1 deletion src/test/codegen/loads.rs
@@ -1,4 +1,4 @@
// compile-flags: -C no-prepopulate-passes
// compile-flags: -C no-prepopulate-passes -Zmir-opt-level=0

#![crate_type = "lib"]

Expand Down
2 changes: 1 addition & 1 deletion src/test/codegen/naked-functions.rs
@@ -1,4 +1,4 @@
// compile-flags: -C no-prepopulate-passes
// compile-flags: -C no-prepopulate-passes -Zmir-opt-level=0

#![crate_type = "lib"]
#![feature(naked_functions)]
Expand Down
2 changes: 1 addition & 1 deletion src/test/codegen/refs.rs
@@ -1,4 +1,4 @@
// compile-flags: -C no-prepopulate-passes
// compile-flags: -C no-prepopulate-passes -Zmir-opt-level=0

#![crate_type = "lib"]

Expand Down
4 changes: 2 additions & 2 deletions src/test/incremental/hashes/closure_expressions.rs
Expand Up @@ -7,7 +7,7 @@

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

#![allow(warnings)]
#![feature(rustc_attrs)]
Expand Down Expand Up @@ -53,7 +53,7 @@ pub fn change_parameter_pattern() {
}

#[cfg(not(cfail1))]
#[rustc_clean(cfg="cfail2", except="hir_owner_nodes, typeck")]
#[rustc_clean(cfg="cfail2", except="hir_owner_nodes, typeck, optimized_mir")]
#[rustc_clean(cfg="cfail3")]
pub fn change_parameter_pattern() {
let _ = |(x,): (u32,)| x;
Expand Down
12 changes: 6 additions & 6 deletions src/test/mir-opt/simplify_locals.c.SimplifyLocals.diff
Expand Up @@ -4,8 +4,8 @@
fn c() -> () {
let mut _0: (); // return place in scope 0 at $DIR/simplify-locals.rs:13:8: 13:8
let _1: [u8; 10]; // in scope 0 at $DIR/simplify-locals.rs:14:9: 14:14
let mut _2: &[u8]; // in scope 0 at $DIR/simplify-locals.rs:16:20: 16:26
let mut _3: &[u8; 10]; // in scope 0 at $DIR/simplify-locals.rs:16:20: 16:26
- let mut _2: &[u8]; // in scope 0 at $DIR/simplify-locals.rs:16:20: 16:26
- let mut _3: &[u8; 10]; // in scope 0 at $DIR/simplify-locals.rs:16:20: 16:26
- let _4: &[u8; 10]; // in scope 0 at $DIR/simplify-locals.rs:16:20: 16:26
scope 1 {
debug bytes => _1; // in scope 1 at $DIR/simplify-locals.rs:14:9: 14:14
Expand All @@ -16,10 +16,10 @@
bb0: {
StorageLive(_1); // scope 0 at $DIR/simplify-locals.rs:14:9: 14:14
_1 = [const 0_u8; 10]; // scope 0 at $DIR/simplify-locals.rs:14:17: 14:26
StorageLive(_2); // scope 1 at $DIR/simplify-locals.rs:16:20: 16:26
_3 = &_1; // scope 1 at $DIR/simplify-locals.rs:16:20: 16:26
_2 = move _3 as &[u8] (Pointer(Unsize)); // scope 1 at $DIR/simplify-locals.rs:16:20: 16:26
StorageDead(_2); // scope 1 at $DIR/simplify-locals.rs:16:26: 16:27
- StorageLive(_2); // scope 1 at $DIR/simplify-locals.rs:16:20: 16:26
- _3 = &_1; // scope 1 at $DIR/simplify-locals.rs:16:20: 16:26
- _2 = move _3 as &[u8] (Pointer(Unsize)); // scope 1 at $DIR/simplify-locals.rs:16:20: 16:26
- StorageDead(_2); // scope 1 at $DIR/simplify-locals.rs:16:26: 16:27
_0 = const (); // scope 0 at $DIR/simplify-locals.rs:13:8: 17:2
StorageDead(_1); // scope 0 at $DIR/simplify-locals.rs:17:1: 17:2
return; // scope 0 at $DIR/simplify-locals.rs:17:2: 17:2
Expand Down
16 changes: 8 additions & 8 deletions src/test/mir-opt/simplify_locals.r.SimplifyLocals.diff
Expand Up @@ -4,8 +4,8 @@
fn r() -> () {
let mut _0: (); // return place in scope 0 at $DIR/simplify-locals.rs:32:8: 32:8
let mut _1: i32; // in scope 0 at $DIR/simplify-locals.rs:33:9: 33:14
let mut _2: &i32; // in scope 0 at $DIR/simplify-locals.rs:35:13: 35:15
let mut _3: &mut i32; // in scope 0 at $DIR/simplify-locals.rs:36:13: 36:19
- let mut _2: &i32; // in scope 0 at $DIR/simplify-locals.rs:35:13: 35:15
- let mut _3: &mut i32; // in scope 0 at $DIR/simplify-locals.rs:36:13: 36:19
scope 1 {
debug a => _1; // in scope 1 at $DIR/simplify-locals.rs:33:9: 33:14
scope 2 {
Expand All @@ -17,12 +17,12 @@
bb0: {
StorageLive(_1); // scope 0 at $DIR/simplify-locals.rs:33:9: 33:14
_1 = const 1_i32; // scope 0 at $DIR/simplify-locals.rs:33:17: 33:18
StorageLive(_2); // scope 1 at $DIR/simplify-locals.rs:35:13: 35:15
_2 = &_1; // scope 1 at $DIR/simplify-locals.rs:35:13: 35:15
StorageDead(_2); // scope 1 at $DIR/simplify-locals.rs:35:15: 35:16
StorageLive(_3); // scope 2 at $DIR/simplify-locals.rs:36:13: 36:19
_3 = &mut _1; // scope 2 at $DIR/simplify-locals.rs:36:13: 36:19
StorageDead(_3); // scope 2 at $DIR/simplify-locals.rs:36:19: 36:20
- StorageLive(_2); // scope 1 at $DIR/simplify-locals.rs:35:13: 35:15
- _2 = &_1; // scope 1 at $DIR/simplify-locals.rs:35:13: 35:15
- StorageDead(_2); // scope 1 at $DIR/simplify-locals.rs:35:15: 35:16
- StorageLive(_3); // scope 2 at $DIR/simplify-locals.rs:36:13: 36:19
- _3 = &mut _1; // scope 2 at $DIR/simplify-locals.rs:36:13: 36:19
- StorageDead(_3); // scope 2 at $DIR/simplify-locals.rs:36:19: 36:20
_0 = const (); // scope 0 at $DIR/simplify-locals.rs:32:8: 37:2
StorageDead(_1); // scope 0 at $DIR/simplify-locals.rs:37:1: 37:2
return; // scope 0 at $DIR/simplify-locals.rs:37:2: 37:2
Expand Down
5 changes: 1 addition & 4 deletions src/test/mir-opt/simplify_locals.t1.SimplifyLocals.diff
Expand Up @@ -5,7 +5,6 @@
let mut _0: (); // return place in scope 0 at $DIR/simplify-locals.rs:42:9: 42:9
- let _1: u32; // in scope 0 at $DIR/simplify-locals.rs:44:14: 44:15
- let mut _2: *mut u32; // in scope 0 at $DIR/simplify-locals.rs:44:14: 44:15
+ let mut _1: *mut u32; // in scope 0 at $DIR/simplify-locals.rs:44:14: 44:15
scope 1 {
}

Expand All @@ -15,9 +14,7 @@
- _2 = &/*tls*/ mut X; // scope 1 at $DIR/simplify-locals.rs:44:14: 44:15
- _1 = (*_2); // scope 1 at $DIR/simplify-locals.rs:44:14: 44:15
- StorageDead(_2); // scope 0 at $DIR/simplify-locals.rs:44:17: 44:18
+ StorageLive(_1); // scope 1 at $DIR/simplify-locals.rs:44:14: 44:15
+ _1 = &/*tls*/ mut X; // scope 1 at $DIR/simplify-locals.rs:44:14: 44:15
StorageDead(_1); // scope 0 at $DIR/simplify-locals.rs:44:17: 44:18
- StorageDead(_1); // scope 0 at $DIR/simplify-locals.rs:44:17: 44:18
_0 = const (); // scope 0 at $DIR/simplify-locals.rs:42:9: 45:2
return; // scope 0 at $DIR/simplify-locals.rs:45:2: 45:2
}
Expand Down
16 changes: 8 additions & 8 deletions src/test/mir-opt/simplify_locals.t2.SimplifyLocals.diff
Expand Up @@ -3,18 +3,18 @@

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

bb0: {
StorageLive(_1); // scope 0 at $DIR/simplify-locals.rs:50:5: 50:22
StorageLive(_2); // scope 1 at $DIR/simplify-locals.rs:50:19: 50:20
_2 = &/*tls*/ mut X; // scope 1 at $DIR/simplify-locals.rs:50:19: 50:20
_1 = &mut (*_2); // scope 1 at $DIR/simplify-locals.rs:50:14: 50:20
StorageDead(_2); // scope 0 at $DIR/simplify-locals.rs:50:22: 50:23
StorageDead(_1); // scope 0 at $DIR/simplify-locals.rs:50:22: 50:23
- StorageLive(_1); // scope 0 at $DIR/simplify-locals.rs:50:5: 50:22
- StorageLive(_2); // scope 1 at $DIR/simplify-locals.rs:50:19: 50:20
- _2 = &/*tls*/ mut X; // scope 1 at $DIR/simplify-locals.rs:50:19: 50:20
- _1 = &mut (*_2); // scope 1 at $DIR/simplify-locals.rs:50:14: 50:20
- StorageDead(_2); // scope 0 at $DIR/simplify-locals.rs:50:22: 50:23
- StorageDead(_1); // scope 0 at $DIR/simplify-locals.rs:50:22: 50:23
_0 = const (); // scope 0 at $DIR/simplify-locals.rs:48:9: 51:2
return; // scope 0 at $DIR/simplify-locals.rs:51:2: 51:2
}
Expand Down
10 changes: 2 additions & 8 deletions src/test/mir-opt/simplify_locals.t3.SimplifyLocals.diff
Expand Up @@ -6,8 +6,6 @@
- let _1: u32; // in scope 0 at $DIR/simplify-locals.rs:56:14: 56:21
- let mut _2: &mut u32; // in scope 0 at $DIR/simplify-locals.rs:56:15: 56:21
- let mut _3: *mut u32; // in scope 0 at $DIR/simplify-locals.rs:56:20: 56:21
+ let mut _1: &mut u32; // in scope 0 at $DIR/simplify-locals.rs:56:15: 56:21
+ let mut _2: *mut u32; // in scope 0 at $DIR/simplify-locals.rs:56:20: 56:21
scope 1 {
}

Expand All @@ -19,12 +17,8 @@
- _2 = &mut (*_3); // scope 1 at $DIR/simplify-locals.rs:56:15: 56:21
- _1 = (*_2); // scope 1 at $DIR/simplify-locals.rs:56:14: 56:21
- StorageDead(_3); // scope 0 at $DIR/simplify-locals.rs:56:23: 56:24
+ StorageLive(_1); // scope 1 at $DIR/simplify-locals.rs:56:15: 56:21
+ StorageLive(_2); // scope 1 at $DIR/simplify-locals.rs:56:20: 56:21
+ _2 = &/*tls*/ mut X; // scope 1 at $DIR/simplify-locals.rs:56:20: 56:21
+ _1 = &mut (*_2); // scope 1 at $DIR/simplify-locals.rs:56:15: 56:21
StorageDead(_2); // scope 0 at $DIR/simplify-locals.rs:56:23: 56:24
StorageDead(_1); // scope 0 at $DIR/simplify-locals.rs:56:23: 56:24
- StorageDead(_2); // scope 0 at $DIR/simplify-locals.rs:56:23: 56:24
- StorageDead(_1); // scope 0 at $DIR/simplify-locals.rs:56:23: 56:24
_0 = const (); // scope 0 at $DIR/simplify-locals.rs:54:9: 57:2
return; // scope 0 at $DIR/simplify-locals.rs:57:2: 57:2
}
Expand Down
Expand Up @@ -5,6 +5,7 @@

// build-fail
// normalize-stderr-test: ".nll/" -> "/"
// compile-flags: -Zmir-opt-level=0

struct S<T> {
t: T,
Expand Down

0 comments on commit 4c3e06a

Please sign in to comment.