Skip to content

Commit 38bd83d

Browse files
committed
Auto merge of rust-lang#71911 - wesleywiser:const_prop_small_cleanups, r=oli-obk
[mir-opt] Small ConstProp cleanup
2 parents 228a0ed + 2f49d55 commit 38bd83d

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
@@ -716,8 +716,6 @@ enum ConstPropMode {
716716
OnlyInsideOwnBlock,
717717
/// The `Local` can be propagated into but reads cannot be propagated.
718718
OnlyPropagateInto,
719-
/// No propagation is allowed at all.
720-
NoPropagation,
721719
}
722720

723721
struct CanConstProp {
@@ -788,7 +786,7 @@ impl<'tcx> Visitor<'tcx> for CanConstProp {
788786
"local {:?} can't be propagated because of multiple assignments",
789787
local,
790788
);
791-
*other = ConstPropMode::NoPropagation;
789+
*other = ConstPropMode::OnlyPropagateInto;
792790
}
793791
}
794792
}
@@ -815,7 +813,7 @@ impl<'tcx> Visitor<'tcx> for CanConstProp {
815813
| MutatingUse(MutatingUseContext::Borrow)
816814
| MutatingUse(MutatingUseContext::AddressOf) => {
817815
trace!("local {:?} can't be propagaged because it's used: {:?}", local, context);
818-
self.can_const_prop[local] = ConstPropMode::NoPropagation;
816+
self.can_const_prop[local] = ConstPropMode::OnlyPropagateInto;
819817
}
820818
}
821819
}
@@ -847,31 +845,28 @@ impl<'mir, 'tcx> MutVisitor<'tcx> for ConstPropagator<'mir, 'tcx> {
847845
if let Ok(place_layout) = self.tcx.layout_of(self.param_env.and(place_ty)) {
848846
let can_const_prop = self.can_const_prop[place.local];
849847
if let Some(()) = self.const_prop(rval, place_layout, source_info, place) {
850-
if can_const_prop != ConstPropMode::NoPropagation {
851-
// This will return None for variables that are from other blocks,
852-
// so it should be okay to propagate from here on down.
853-
if let Some(value) = self.get_const(place) {
854-
if self.should_const_prop(value) {
855-
trace!("replacing {:?} with {:?}", rval, value);
856-
self.replace_with_const(rval, value, source_info);
857-
if can_const_prop == ConstPropMode::FullConstProp
858-
|| can_const_prop == ConstPropMode::OnlyInsideOwnBlock
859-
{
860-
trace!("propagated into {:?}", place);
861-
}
862-
}
863-
if can_const_prop == ConstPropMode::OnlyInsideOwnBlock {
864-
trace!(
865-
"found local restricted to its block. Will remove it from const-prop after block is finished. Local: {:?}",
866-
place.local
867-
);
868-
self.locals_of_current_block.insert(place.local);
848+
// This will return None for variables that are from other blocks,
849+
// so it should be okay to propagate from here on down.
850+
if let Some(value) = self.get_const(place) {
851+
if self.should_const_prop(value) {
852+
trace!("replacing {:?} with {:?}", rval, value);
853+
self.replace_with_const(rval, value, source_info);
854+
if can_const_prop == ConstPropMode::FullConstProp
855+
|| can_const_prop == ConstPropMode::OnlyInsideOwnBlock
856+
{
857+
trace!("propagated into {:?}", place);
869858
}
870859
}
871860
}
872-
if can_const_prop == ConstPropMode::OnlyPropagateInto
873-
|| can_const_prop == ConstPropMode::NoPropagation
874-
{
861+
if can_const_prop == ConstPropMode::OnlyInsideOwnBlock {
862+
trace!(
863+
"found local restricted to its block. Will remove it from const-prop after block is finished. Local: {:?}",
864+
place.local
865+
);
866+
self.locals_of_current_block.insert(place.local);
867+
}
868+
869+
if can_const_prop == ConstPropMode::OnlyPropagateInto {
875870
trace!("can't propagate into {:?}", place);
876871
if place.local != RETURN_PLACE {
877872
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 renamed to 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)