Skip to content

Commit 4a7dcff

Browse files
committed
Implement algebraic simplifications.
1 parent 4ea9942 commit 4a7dcff

File tree

3 files changed

+39
-8
lines changed

3 files changed

+39
-8
lines changed

compiler/rustc_mir_transform/src/dataflow_const_prop.rs

+31-4
Original file line numberDiff line numberDiff line change
@@ -343,18 +343,45 @@ impl<'a, 'tcx> ConstAnalysis<'a, 'tcx> {
343343
) -> (FlatSet<ScalarTy<'tcx>>, FlatSet<bool>) {
344344
let left = self.eval_operand(left, state);
345345
let right = self.eval_operand(right, state);
346+
346347
match (left, right) {
348+
(FlatSet::Bottom, _) | (_, FlatSet::Bottom) => (FlatSet::Bottom, FlatSet::Bottom),
349+
// Both sides are known, do the actual computation.
347350
(FlatSet::Elem(left), FlatSet::Elem(right)) => {
348351
match self.ecx.overflowing_binary_op(op, &left, &right) {
349352
Ok((val, overflow, ty)) => (self.wrap_scalar(val, ty), FlatSet::Elem(overflow)),
350353
_ => (FlatSet::Top, FlatSet::Top),
351354
}
352355
}
353-
(FlatSet::Bottom, _) | (_, FlatSet::Bottom) => (FlatSet::Bottom, FlatSet::Bottom),
354-
(_, _) => {
355-
// Could attempt some algebraic simplifications here.
356-
(FlatSet::Top, FlatSet::Top)
356+
// Exactly one side is known, attempt some algebraic simplifications.
357+
(FlatSet::Elem(const_arg), _) | (_, FlatSet::Elem(const_arg)) => {
358+
let layout = const_arg.layout;
359+
if !matches!(layout.abi, rustc_target::abi::Abi::Scalar(..)) {
360+
return (FlatSet::Top, FlatSet::Top);
361+
}
362+
363+
let arg_scalar = const_arg.to_scalar();
364+
let Ok(arg_value) = arg_scalar.to_bits(layout.size) else {
365+
return (FlatSet::Top, FlatSet::Top);
366+
};
367+
368+
match op {
369+
BinOp::BitAnd if arg_value == 0 => {
370+
(self.wrap_scalar(arg_scalar, layout.ty), FlatSet::Bottom)
371+
}
372+
BinOp::BitOr
373+
if arg_value == layout.size.truncate(u128::MAX)
374+
|| (layout.ty.is_bool() && arg_value == 1) =>
375+
{
376+
(self.wrap_scalar(arg_scalar, layout.ty), FlatSet::Bottom)
377+
}
378+
BinOp::Mul if layout.ty.is_integral() && arg_value == 0 => {
379+
(self.wrap_scalar(arg_scalar, layout.ty), FlatSet::Elem(false))
380+
}
381+
_ => (FlatSet::Top, FlatSet::Top),
382+
}
357383
}
384+
(FlatSet::Top, FlatSet::Top) => (FlatSet::Top, FlatSet::Top),
358385
}
359386
}
360387

tests/mir-opt/const_prop/boolean_identities.test.ConstProp.diff

+6-3
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,13 @@
1212

1313
bb0: {
1414
StorageLive(_3);
15-
_3 = BitOr(_2, const true);
15+
- _3 = BitOr(_2, const true);
16+
+ _3 = const true;
1617
StorageLive(_5);
17-
_5 = BitAnd(_1, const false);
18-
_0 = BitAnd(move _3, move _5);
18+
- _5 = BitAnd(_1, const false);
19+
- _0 = BitAnd(move _3, move _5);
20+
+ _5 = const false;
21+
+ _0 = const false;
1922
StorageDead(_5);
2023
StorageDead(_3);
2124
return;

tests/mir-opt/const_prop/mult_by_zero.test.ConstProp.diff

+2-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
bb0: {
1010
StorageLive(_2);
1111
_2 = _1;
12-
_0 = Mul(move _2, const 0_i32);
12+
- _0 = Mul(move _2, const 0_i32);
13+
+ _0 = const 0_i32;
1314
StorageDead(_2);
1415
return;
1516
}

0 commit comments

Comments
 (0)