Skip to content

Commit 6e586b8

Browse files
committed
Simplify fn call const propagation (by wesleywiser)
1 parent 8a8c8c9 commit 6e586b8

File tree

3 files changed

+18
-24
lines changed

3 files changed

+18
-24
lines changed

Diff for: src/librustc_mir/transform/const_prop.rs

+14-18
Original file line numberDiff line numberDiff line change
@@ -1031,24 +1031,20 @@ impl<'mir, 'tcx> MutVisitor<'tcx> for ConstPropagator<'mir, 'tcx> {
10311031
// We try to, before exiting.
10321032
for opr in args {
10331033
// We see if the operand can be evaluated, and if so, we continue.
1034-
if let Some(op_ty) = self.eval_operand(&opr, source_info) {
1035-
// We must also ask if it *should* be evaluated.
1036-
if self.should_const_prop(op_ty) {
1037-
if let interpret::Operand::Immediate(immediate) = *op_ty {
1038-
// Only Immediate from here on
1039-
// FIXME(felix91gr): The code only works for Immediate. Investigate
1040-
// if it could be made to work for Indirect as well.
1041-
if let interpret::Immediate::Scalar(scalar_mu) = immediate {
1042-
// FIXME(felix91gr): This only works for Scalar
1043-
// Could probably be expanded for Scalar Tuples (inside the Immediate)
1044-
if let ScalarMaybeUndef::Scalar(scalar) = scalar_mu {
1045-
let operand_type = opr.ty(&self.local_decls, self.tcx);
1046-
*opr = self.operand_from_scalar(
1047-
scalar,
1048-
operand_type,
1049-
source_info.span,
1050-
);
1051-
}
1034+
if let Some(l) = opr.place().and_then(|p| p.as_local()) {
1035+
if let Some(value) = self.get_const(l) {
1036+
if self.should_const_prop(value) {
1037+
if let interpret::Operand::Immediate(
1038+
interpret::Immediate::Scalar(
1039+
interpret::ScalarMaybeUndef::Scalar(scalar),
1040+
),
1041+
) = *value
1042+
{
1043+
*opr = self.operand_from_scalar(
1044+
scalar,
1045+
value.layout.ty,
1046+
source_info.span,
1047+
);
10521048
}
10531049
}
10541050
}

Diff for: src/test/mir-opt/const_prop/switch_int/rustc.main.ConstProp.diff

+2-4
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,7 @@
3636
// + ty: i32
3737
// + val: Value(Scalar(0xffffffff))
3838
// mir::Constant
39-
- // + span: $DIR/switch_int.rs:9:18: 9:20
40-
+ // + span: $DIR/switch_int.rs:9:14: 9:21
39+
// + span: $DIR/switch_int.rs:9:18: 9:20
4140
// + literal: Const { ty: i32, val: Value(Scalar(0xffffffff)) }
4241
}
4342

@@ -53,8 +52,7 @@
5352
// + ty: i32
5453
// + val: Value(Scalar(0x00000000))
5554
// mir::Constant
56-
- // + span: $DIR/switch_int.rs:8:18: 8:19
57-
+ // + span: $DIR/switch_int.rs:8:14: 8:20
55+
// + span: $DIR/switch_int.rs:8:18: 8:19
5856
// + literal: Const { ty: i32, val: Value(Scalar(0x00000000)) }
5957
}
6058

Diff for: src/test/mir-opt/const_prop/switch_int/rustc.main.SimplifyBranches-after-const-prop.diff

+2-2
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
// + ty: i32
3737
// + val: Value(Scalar(0xffffffff))
3838
// mir::Constant
39-
// + span: $DIR/switch_int.rs:9:14: 9:21
39+
// + span: $DIR/switch_int.rs:9:18: 9:20
4040
// + literal: Const { ty: i32, val: Value(Scalar(0xffffffff)) }
4141
}
4242

@@ -52,7 +52,7 @@
5252
// + ty: i32
5353
// + val: Value(Scalar(0x00000000))
5454
// mir::Constant
55-
// + span: $DIR/switch_int.rs:8:14: 8:20
55+
// + span: $DIR/switch_int.rs:8:18: 8:19
5656
// + literal: Const { ty: i32, val: Value(Scalar(0x00000000)) }
5757
}
5858

0 commit comments

Comments
 (0)