Skip to content

Commit

Permalink
simplify-locals: Remove unused set-discriminant statements
Browse files Browse the repository at this point in the history
Update affected ui & incremental tests to use a user declared variable
bindings instead of temporaries. The former are preserved because of
debuginfo, the latter are not.
  • Loading branch information
tmiasko committed Oct 26, 2020
1 parent e1e48ae commit 52d3782
Show file tree
Hide file tree
Showing 8 changed files with 20 additions and 27 deletions.
14 changes: 9 additions & 5 deletions compiler/rustc_mir/src/transform/simplify.rs
Expand Up @@ -427,7 +427,6 @@ impl Visitor<'_> for UsedLocals {
fn visit_statement(&mut self, statement: &Statement<'tcx>, location: Location) {
match statement.kind {
StatementKind::LlvmInlineAsm(..)
| StatementKind::SetDiscriminant { .. } // FIXME: Try to remove those as well.
| StatementKind::Retag(..)
| StatementKind::Coverage(..)
| StatementKind::FakeRead(..)
Expand Down Expand Up @@ -467,6 +466,10 @@ impl Visitor<'_> for UsedLocals {
}
self.visit_rvalue(rvalue, location);
}

StatementKind::SetDiscriminant { ref place, variant_index: _ } => {
self.visit_lhs(place, location);
}
}
}

Expand All @@ -481,10 +484,7 @@ impl Visitor<'_> for UsedLocals {
}

/// Removes unused definitions. Updates the used locals to reflect the changes made.
fn remove_unused_definitions<'a, 'tcx>(
used_locals: &'a mut UsedLocals,
body: &mut Body<'tcx>,
) {
fn remove_unused_definitions<'a, 'tcx>(used_locals: &'a mut UsedLocals, body: &mut Body<'tcx>) {
// The use counts are updated as we remove the statements. A local might become unused
// during the retain operation, leading to a temporary inconsistency (storage statements or
// definitions referencing the local might remain). For correctness it is crucial that this
Expand All @@ -502,6 +502,10 @@ fn remove_unused_definitions<'a, 'tcx>(
used_locals.is_used(*local)
}
StatementKind::Assign(box (place, _)) => used_locals.is_used(place.local),

StatementKind::SetDiscriminant { ref place, .. } => {
used_locals.is_used(place.local)
}
_ => true,
};

Expand Down
2 changes: 1 addition & 1 deletion src/test/incremental/hashes/enum_constructors.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
8 changes: 4 additions & 4 deletions src/test/mir-opt/simplify_locals.d1.SimplifyLocals.diff
Expand Up @@ -3,14 +3,14 @@

fn d1() -> () {
let mut _0: (); // return place in scope 0 at $DIR/simplify-locals.rs:20:9: 20:9
let mut _1: E; // in scope 0 at $DIR/simplify-locals.rs:22:13: 22:17
- let mut _1: E; // in scope 0 at $DIR/simplify-locals.rs:22:13: 22:17
scope 1 {
}

bb0: {
StorageLive(_1); // scope 0 at $DIR/simplify-locals.rs:22:13: 22:17
discriminant(_1) = 0; // scope 0 at $DIR/simplify-locals.rs:22:13: 22:17
StorageDead(_1); // scope 0 at $DIR/simplify-locals.rs:22:17: 22:18
- StorageLive(_1); // scope 0 at $DIR/simplify-locals.rs:22:13: 22:17
- discriminant(_1) = 0; // scope 0 at $DIR/simplify-locals.rs:22:13: 22:17
- StorageDead(_1); // scope 0 at $DIR/simplify-locals.rs:22:17: 22:18
_0 = const (); // scope 0 at $DIR/simplify-locals.rs:20:9: 23:2
return; // scope 0 at $DIR/simplify-locals.rs:23:2: 23:2
}
Expand Down
12 changes: 4 additions & 8 deletions src/test/mir-opt/simplify_locals.d2.SimplifyLocals.diff
Expand Up @@ -3,14 +3,13 @@

fn d2() -> () {
let mut _0: (); // return place in scope 0 at $DIR/simplify-locals.rs:26:9: 26:9
let mut _1: E; // in scope 0 at $DIR/simplify-locals.rs:28:22: 28:26
- let mut _1: E; // in scope 0 at $DIR/simplify-locals.rs:28:22: 28:26
- let mut _2: (i32, E); // in scope 0 at $DIR/simplify-locals.rs:28:5: 28:17
- let mut _3: E; // in scope 0 at $DIR/simplify-locals.rs:28:11: 28:15
+ let mut _2: E; // in scope 0 at $DIR/simplify-locals.rs:28:11: 28:15

bb0: {
StorageLive(_1); // scope 0 at $DIR/simplify-locals.rs:28:22: 28:26
discriminant(_1) = 1; // scope 0 at $DIR/simplify-locals.rs:28:22: 28:26
- StorageLive(_1); // scope 0 at $DIR/simplify-locals.rs:28:22: 28:26
- discriminant(_1) = 1; // scope 0 at $DIR/simplify-locals.rs:28:22: 28:26
- StorageLive(_2); // scope 0 at $DIR/simplify-locals.rs:28:5: 28:17
- StorageLive(_3); // scope 0 at $DIR/simplify-locals.rs:28:11: 28:15
- discriminant(_3) = 0; // scope 0 at $DIR/simplify-locals.rs:28:11: 28:15
Expand All @@ -30,10 +29,7 @@
- // mir::Constant
- // + span: $DIR/simplify-locals.rs:28:5: 28:26
- // + literal: Const { ty: E, val: Value(Scalar(0x01)) }
+ StorageLive(_2); // scope 0 at $DIR/simplify-locals.rs:28:11: 28:15
+ discriminant(_2) = 0; // scope 0 at $DIR/simplify-locals.rs:28:11: 28:15
+ StorageDead(_2); // scope 0 at $DIR/simplify-locals.rs:28:15: 28:16
StorageDead(_1); // scope 0 at $DIR/simplify-locals.rs:28:25: 28:26
- StorageDead(_1); // scope 0 at $DIR/simplify-locals.rs:28:25: 28:26
- StorageDead(_2); // scope 0 at $DIR/simplify-locals.rs:28:26: 28:27
_0 = const (); // scope 0 at $DIR/simplify-locals.rs:26:9: 29:2
return; // scope 0 at $DIR/simplify-locals.rs:29:2: 29:2
Expand Down
Expand Up @@ -3,18 +3,14 @@
fn change_loop_body() -> () {
let mut _0: (); // return place in scope 0 at $DIR/while_let_loops.rs:5:27: 5:27
let mut _1: i32; // in scope 0 at $DIR/while_let_loops.rs:6:9: 6:15
let mut _2: std::option::Option<u32>; // in scope 0 at $DIR/while_let_loops.rs:7:28: 7:32
scope 1 {
debug _x => _1; // in scope 1 at $DIR/while_let_loops.rs:6:9: 6:15
}

bb0: {
StorageLive(_1); // scope 0 at $DIR/while_let_loops.rs:6:9: 6:15
_1 = const 0_i32; // scope 0 at $DIR/while_let_loops.rs:6:18: 6:19
StorageLive(_2); // scope 1 at $DIR/while_let_loops.rs:7:28: 7:32
discriminant(_2) = 0; // scope 1 at $DIR/while_let_loops.rs:7:28: 7:32
_0 = const (); // scope 1 at $DIR/while_let_loops.rs:7:5: 10:6
StorageDead(_2); // scope 1 at $DIR/while_let_loops.rs:10:5: 10:6
StorageDead(_1); // scope 0 at $DIR/while_let_loops.rs:11:1: 11:2
return; // scope 0 at $DIR/while_let_loops.rs:11:2: 11:2
}
Expand Down
Expand Up @@ -3,18 +3,14 @@
fn change_loop_body() -> () {
let mut _0: (); // return place in scope 0 at $DIR/while_let_loops.rs:5:27: 5:27
let mut _1: i32; // in scope 0 at $DIR/while_let_loops.rs:6:9: 6:15
let mut _2: std::option::Option<u32>; // in scope 0 at $DIR/while_let_loops.rs:7:28: 7:32
scope 1 {
debug _x => _1; // in scope 1 at $DIR/while_let_loops.rs:6:9: 6:15
}

bb0: {
StorageLive(_1); // scope 0 at $DIR/while_let_loops.rs:6:9: 6:15
_1 = const 0_i32; // scope 0 at $DIR/while_let_loops.rs:6:18: 6:19
StorageLive(_2); // scope 1 at $DIR/while_let_loops.rs:7:28: 7:32
discriminant(_2) = 0; // scope 1 at $DIR/while_let_loops.rs:7:28: 7:32
_0 = const (); // scope 1 at $DIR/while_let_loops.rs:7:5: 10:6
StorageDead(_2); // scope 1 at $DIR/while_let_loops.rs:10:5: 10:6
StorageDead(_1); // scope 0 at $DIR/while_let_loops.rs:11:1: 11:2
return; // scope 0 at $DIR/while_let_loops.rs:11:2: 11:2
}
Expand Down
1 change: 1 addition & 0 deletions src/test/ui/lint/issue-69485-var-size-diffs-too-large.rs
@@ -1,5 +1,6 @@
// build-fail
// only-x86_64
// compile-flags: -Zmir-opt-level=0

fn main() {
Bug::V([0; !0]); //~ ERROR is too big for the current
Expand Down
@@ -1,5 +1,5 @@
error: the type `[u8; 18446744073709551615]` is too big for the current architecture
--> $DIR/issue-69485-var-size-diffs-too-large.rs:5:12
--> $DIR/issue-69485-var-size-diffs-too-large.rs:6:12
|
LL | Bug::V([0; !0]);
| ^^^^^^^
Expand Down

0 comments on commit 52d3782

Please sign in to comment.