Skip to content

Commit 3d6cb74

Browse files
authored
Rollup merge of rust-lang#71911 - wesleywiser:const_prop_small_cleanups, r=oli-obk
[mir-opt] Small ConstProp cleanup
2 parents dd582fe + 2f49d55 commit 3d6cb74

File tree

8 files changed

+88
-45
lines changed

8 files changed

+88
-45
lines changed

src/librustc_mir/transform/const_prop.rs

+21-26
Original file line numberDiff line numberDiff line change
@@ -721,8 +721,6 @@ enum ConstPropMode {
721721
OnlyInsideOwnBlock,
722722
/// The `Local` can be propagated into but reads cannot be propagated.
723723
OnlyPropagateInto,
724-
/// No propagation is allowed at all.
725-
NoPropagation,
726724
}
727725

728726
struct CanConstProp {
@@ -793,7 +791,7 @@ impl<'tcx> Visitor<'tcx> for CanConstProp {
793791
"local {:?} can't be propagated because of multiple assignments",
794792
local,
795793
);
796-
*other = ConstPropMode::NoPropagation;
794+
*other = ConstPropMode::OnlyPropagateInto;
797795
}
798796
}
799797
}
@@ -820,7 +818,7 @@ impl<'tcx> Visitor<'tcx> for CanConstProp {
820818
| MutatingUse(MutatingUseContext::Borrow)
821819
| MutatingUse(MutatingUseContext::AddressOf) => {
822820
trace!("local {:?} can't be propagaged because it's used: {:?}", local, context);
823-
self.can_const_prop[local] = ConstPropMode::NoPropagation;
821+
self.can_const_prop[local] = ConstPropMode::OnlyPropagateInto;
824822
}
825823
}
826824
}
@@ -852,31 +850,28 @@ impl<'mir, 'tcx> MutVisitor<'tcx> for ConstPropagator<'mir, 'tcx> {
852850
if let Ok(place_layout) = self.tcx.layout_of(self.param_env.and(place_ty)) {
853851
let can_const_prop = self.can_const_prop[place.local];
854852
if let Some(()) = self.const_prop(rval, place_layout, source_info, place) {
855-
if can_const_prop != ConstPropMode::NoPropagation {
856-
// This will return None for variables that are from other blocks,
857-
// so it should be okay to propagate from here on down.
858-
if let Some(value) = self.get_const(place) {
859-
if self.should_const_prop(value) {
860-
trace!("replacing {:?} with {:?}", rval, value);
861-
self.replace_with_const(rval, value, source_info);
862-
if can_const_prop == ConstPropMode::FullConstProp
863-
|| can_const_prop == ConstPropMode::OnlyInsideOwnBlock
864-
{
865-
trace!("propagated into {:?}", place);
866-
}
867-
}
868-
if can_const_prop == ConstPropMode::OnlyInsideOwnBlock {
869-
trace!(
870-
"found local restricted to its block. Will remove it from const-prop after block is finished. Local: {:?}",
871-
place.local
872-
);
873-
self.locals_of_current_block.insert(place.local);
853+
// This will return None for variables that are from other blocks,
854+
// so it should be okay to propagate from here on down.
855+
if let Some(value) = self.get_const(place) {
856+
if self.should_const_prop(value) {
857+
trace!("replacing {:?} with {:?}", rval, value);
858+
self.replace_with_const(rval, value, source_info);
859+
if can_const_prop == ConstPropMode::FullConstProp
860+
|| can_const_prop == ConstPropMode::OnlyInsideOwnBlock
861+
{
862+
trace!("propagated into {:?}", place);
874863
}
875864
}
876865
}
877-
if can_const_prop == ConstPropMode::OnlyPropagateInto
878-
|| can_const_prop == ConstPropMode::NoPropagation
879-
{
866+
if can_const_prop == ConstPropMode::OnlyInsideOwnBlock {
867+
trace!(
868+
"found local restricted to its block. Will remove it from const-prop after block is finished. Local: {:?}",
869+
place.local
870+
);
871+
self.locals_of_current_block.insert(place.local);
872+
}
873+
874+
if can_const_prop == ConstPropMode::OnlyPropagateInto {
880875
trace!("can't propagate into {:?}", place);
881876
if place.local != RETURN_PLACE {
882877
Self::remove_const(&mut self.ecx, place.local);

src/test/mir-opt/const_prop/mutable_variable_aggregate_mut_ref/rustc.main.ConstProp.diff

+4-2
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,15 @@
2323
// + ty: i32
2424
// + val: Value(Scalar(0x0000002a))
2525
// mir::Constant
26-
// + span: $DIR/mutable_variable_aggregate_mut_ref.rs:5:18: 5:20
26+
- // + span: $DIR/mutable_variable_aggregate_mut_ref.rs:5:18: 5:20
27+
+ // + span: $DIR/mutable_variable_aggregate_mut_ref.rs:5:17: 5:25
2728
// + literal: Const { ty: i32, val: Value(Scalar(0x0000002a)) }
2829
// ty::Const
2930
// + ty: i32
3031
// + val: Value(Scalar(0x0000002b))
3132
// mir::Constant
32-
// + span: $DIR/mutable_variable_aggregate_mut_ref.rs:5:22: 5:24
33+
- // + span: $DIR/mutable_variable_aggregate_mut_ref.rs:5:22: 5:24
34+
+ // + span: $DIR/mutable_variable_aggregate_mut_ref.rs:5:17: 5:25
3335
// + literal: Const { ty: i32, val: Value(Scalar(0x0000002b)) }
3436
StorageLive(_2); // scope 1 at $DIR/mutable_variable_aggregate_mut_ref.rs:6:9: 6:10
3537
_2 = &mut _1; // scope 1 at $DIR/mutable_variable_aggregate_mut_ref.rs:6:13: 6:19

src/test/mir-opt/simplify-arm-identity/32bit/rustc.main.SimplifyArmIdentity.diff

+4-5
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,13 @@
3939
}
4040

4141
bb1: {
42-
((_2 as Foo).0: u8) = const 0u8; // scope 1 at $DIR/simplify-arm-identity.rs:21:21: 21:32
42+
_2 = const Dst::Foo(0u8); // scope 1 at $DIR/simplify-arm-identity.rs:21:21: 21:32
4343
// ty::Const
44-
// + ty: u8
44+
// + ty: Dst
4545
// + val: Value(Scalar(0x00))
4646
// mir::Constant
47-
// + span: $DIR/simplify-arm-identity.rs:21:30: 21:31
48-
// + literal: Const { ty: u8, val: Value(Scalar(0x00)) }
49-
discriminant(_2) = 0; // scope 1 at $DIR/simplify-arm-identity.rs:21:21: 21:32
47+
// + span: $DIR/simplify-arm-identity.rs:21:21: 21:32
48+
// + literal: Const { ty: Dst, val: Value(Scalar(0x00)) }
5049
goto -> bb4; // scope 1 at $DIR/simplify-arm-identity.rs:19:18: 22:6
5150
}
5251

src/test/mir-opt/simplify-arm-identity/64bit/rustc.main.SimplifyArmIdentity.diff

+4-5
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,13 @@
3939
}
4040

4141
bb1: {
42-
((_2 as Foo).0: u8) = const 0u8; // scope 1 at $DIR/simplify-arm-identity.rs:21:21: 21:32
42+
_2 = const Dst::Foo(0u8); // scope 1 at $DIR/simplify-arm-identity.rs:21:21: 21:32
4343
// ty::Const
44-
// + ty: u8
44+
// + ty: Dst
4545
// + val: Value(Scalar(0x00))
4646
// mir::Constant
47-
// + span: $DIR/simplify-arm-identity.rs:21:30: 21:31
48-
// + literal: Const { ty: u8, val: Value(Scalar(0x00)) }
49-
discriminant(_2) = 0; // scope 1 at $DIR/simplify-arm-identity.rs:21:21: 21:32
47+
// + span: $DIR/simplify-arm-identity.rs:21:21: 21:32
48+
// + literal: Const { ty: Dst, val: Value(Scalar(0x00)) }
5049
goto -> bb4; // scope 1 at $DIR/simplify-arm-identity.rs:19:18: 22:6
5150
}
5251

src/test/mir-opt/simplify-arm-identity/rustc.main.SimplifyArmIdentity.diff

+5-6
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,14 @@
3333
}
3434

3535
bb1: {
36-
((_2 as Foo).0: u8) = const 0u8; // scope 1 at $DIR/simplify-arm-identity.rs:20:21: 20:32
36+
_2 = const Dst::Foo(0u8); // bb1[0]: scope 1 at $DIR/simplify-arm-identity.rs:20:21: 20:32
3737
// ty::Const
38-
// + ty: u8
38+
// + ty: Dst
3939
// + val: Value(Scalar(0x00))
4040
// mir::Constant
41-
// + span: $DIR/simplify-arm-identity.rs:20:30: 20:31
42-
// + literal: Const { ty: u8, val: Value(Scalar(0x00)) }
43-
discriminant(_2) = 0; // scope 1 at $DIR/simplify-arm-identity.rs:20:21: 20:32
44-
goto -> bb4; // scope 1 at $DIR/simplify-arm-identity.rs:18:18: 21:6
41+
// + span: $DIR/simplify-arm-identity.rs:20:21: 20:32
42+
// + literal: Const { ty: Dst, val: Value(Scalar(0x00)) }
43+
goto -> bb4; // bb1[1]: scope 1 at $DIR/simplify-arm-identity.rs:18:18: 21:6
4544
}
4645

4746
bb2: {

src/test/mir-opt/simplify-locals-removes-unused-discriminant-reads.rs

+1
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,5 @@ fn main() {
99
map(None);
1010
}
1111

12+
// EMIT_MIR_FOR_EACH_BIT_WIDTH
1213
// EMIT_MIR rustc.map.SimplifyLocals.diff

src/test/mir-opt/simplify-locals-removes-unused-discriminant-reads/rustc.map.SimplifyLocals.diff src/test/mir-opt/simplify-locals-removes-unused-discriminant-reads/32bit/rustc.map.SimplifyLocals.diff

+7-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,13 @@
2424
}
2525

2626
bb2: {
27-
discriminant(_0) = 0; // scope 0 at $DIR/simplify-locals-removes-unused-discriminant-reads.rs:3:17: 3:21
27+
_0 = const std::option::Option::<std::boxed::Box<()>>::None; // scope 0 at $DIR/simplify-locals-removes-unused-discriminant-reads.rs:3:17: 3:21
28+
// ty::Const
29+
// + ty: std::option::Option<std::boxed::Box<()>>
30+
// + val: Value(Scalar(0x00000000))
31+
// mir::Constant
32+
// + span: $DIR/simplify-locals-removes-unused-discriminant-reads.rs:3:17: 3:21
33+
// + literal: Const { ty: std::option::Option<std::boxed::Box<()>>, val: Value(Scalar(0x00000000)) }
2834
goto -> bb3; // scope 0 at $DIR/simplify-locals-removes-unused-discriminant-reads.rs:2:5: 5:6
2935
}
3036

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
- // MIR for `map` before SimplifyLocals
2+
+ // MIR for `map` after SimplifyLocals
3+
4+
fn map(_1: std::option::Option<std::boxed::Box<()>>) -> std::option::Option<std::boxed::Box<()>> {
5+
debug x => _1; // in scope 0 at $DIR/simplify-locals-removes-unused-discriminant-reads.rs:1:8: 1:9
6+
let mut _0: std::option::Option<std::boxed::Box<()>>; // return place in scope 0 at $DIR/simplify-locals-removes-unused-discriminant-reads.rs:1:31: 1:46
7+
let mut _2: isize; // in scope 0 at $DIR/simplify-locals-removes-unused-discriminant-reads.rs:3:9: 3:13
8+
let _3: std::boxed::Box<()>; // in scope 0 at $DIR/simplify-locals-removes-unused-discriminant-reads.rs:4:14: 4:15
9+
- let mut _4: std::boxed::Box<()>; // in scope 0 at $DIR/simplify-locals-removes-unused-discriminant-reads.rs:4:25: 4:26
10+
- let mut _5: isize; // in scope 0 at $DIR/simplify-locals-removes-unused-discriminant-reads.rs:6:1: 6:2
11+
- let mut _6: isize; // in scope 0 at $DIR/simplify-locals-removes-unused-discriminant-reads.rs:6:1: 6:2
12+
scope 1 {
13+
debug x => _3; // in scope 1 at $DIR/simplify-locals-removes-unused-discriminant-reads.rs:4:14: 4:15
14+
}
15+
16+
bb0: {
17+
_2 = discriminant(_1); // scope 0 at $DIR/simplify-locals-removes-unused-discriminant-reads.rs:3:9: 3:13
18+
switchInt(move _2) -> [0isize: bb2, otherwise: bb1]; // scope 0 at $DIR/simplify-locals-removes-unused-discriminant-reads.rs:3:9: 3:13
19+
}
20+
21+
bb1: {
22+
_0 = move _1; // scope 1 at $DIR/simplify-locals-removes-unused-discriminant-reads.rs:4:20: 4:27
23+
goto -> bb3; // scope 0 at $DIR/simplify-locals-removes-unused-discriminant-reads.rs:2:5: 5:6
24+
}
25+
26+
bb2: {
27+
_0 = const std::option::Option::<std::boxed::Box<()>>::None; // scope 0 at $DIR/simplify-locals-removes-unused-discriminant-reads.rs:3:17: 3:21
28+
// ty::Const
29+
// + ty: std::option::Option<std::boxed::Box<()>>
30+
// + val: Value(Scalar(0x0000000000000000))
31+
// mir::Constant
32+
// + span: $DIR/simplify-locals-removes-unused-discriminant-reads.rs:3:17: 3:21
33+
// + literal: Const { ty: std::option::Option<std::boxed::Box<()>>, val: Value(Scalar(0x0000000000000000)) }
34+
goto -> bb3; // scope 0 at $DIR/simplify-locals-removes-unused-discriminant-reads.rs:2:5: 5:6
35+
}
36+
37+
bb3: {
38+
- _5 = discriminant(_1); // scope 0 at $DIR/simplify-locals-removes-unused-discriminant-reads.rs:6:1: 6:2
39+
return; // scope 0 at $DIR/simplify-locals-removes-unused-discriminant-reads.rs:6:2: 6:2
40+
}
41+
}
42+

0 commit comments

Comments
 (0)