Skip to content

Commit

Permalink
JumpThreading: fix bitwise not on non-booleans
Browse files Browse the repository at this point in the history
  • Loading branch information
clubby789 committed Oct 3, 2024
1 parent f7c8928 commit eb99fae
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 12 deletions.
8 changes: 8 additions & 0 deletions compiler/rustc_middle/src/ty/consts/int.rs
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,14 @@ impl ScalarInt {
}
}

impl std::ops::Not for ScalarInt {
type Output = ScalarInt;

fn not(self) -> Self::Output {
ScalarInt { data: self.size().truncate(!self.data), size: self.size }
}
}

macro_rules! from_x_for_scalar_int {
($($ty:ty),*) => {
$(
Expand Down
6 changes: 2 additions & 4 deletions compiler/rustc_mir_transform/src/jump_threading.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,10 +150,8 @@ impl Condition {
}

fn inv(mut self) -> Self {
self.polarity = match self.polarity {
Polarity::Eq => Polarity::Ne,
Polarity::Ne => Polarity::Eq,
};
self.value = !self.value;

self
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
- // MIR for `not_int` before JumpThreading
+ // MIR for `not_int` after JumpThreading

fn not_int() -> i32 {
let mut _0: i32;
let _1: i32;
let mut _2: bool;
let mut _3: i32;
let mut _4: i32;
scope 1 {
debug a => _1;
}

bb0: {
StorageLive(_1);
_1 = const 1_i32;
StorageLive(_2);
StorageLive(_3);
StorageLive(_4);
_4 = copy _1;
_3 = Not(move _4);
StorageDead(_4);
_2 = Eq(move _3, const 0_i32);
- switchInt(move _2) -> [0: bb2, otherwise: bb1];
+ goto -> bb2;
}

bb1: {
StorageDead(_3);
_0 = const 1_i32;
goto -> bb3;
}

bb2: {
StorageDead(_3);
_0 = const 0_i32;
goto -> bb3;
}

bb3: {
StorageDead(_2);
StorageDead(_1);
return;
}
}

18 changes: 18 additions & 0 deletions tests/mir-opt/jump_threading.rs
Original file line number Diff line number Diff line change
Expand Up @@ -531,6 +531,22 @@ fn floats() -> u32 {
if x == 0.0 { 0 } else { 1 }
}

fn not_int() -> i32 {
// CHECK-LABEL: fn not_int(
// CHECK: goto -> bb2

// CHECK-LABEL: bb1: {
// _0 = const 1_i32;

// CHECK-LABEL: bb2: {
// _0 = const 0_i32;

// Test for issue #131195, where !a == b is assumed to be equivalent to a != b
// This is only the case for bools
let a = 1;
if !a == 0 { 1 } else { 0 }
}

fn main() {
// CHECK-LABEL: fn main(
too_complex(Ok(0));
Expand All @@ -546,6 +562,7 @@ fn main() {
aggregate(7);
assume(7, false);
floats();
not_int();
}

// EMIT_MIR jump_threading.too_complex.JumpThreading.diff
Expand All @@ -562,3 +579,4 @@ fn main() {
// EMIT_MIR jump_threading.assume.JumpThreading.diff
// EMIT_MIR jump_threading.aggregate_copy.JumpThreading.diff
// EMIT_MIR jump_threading.floats.JumpThreading.diff
// EMIT_MIR jump_threading.not_int.JumpThreading.diff
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ fn step_forward(_1: u16, _2: usize) -> u16 {
StorageLive(_4);
StorageLive(_3);
_3 = Gt(copy _2, const 65535_usize);
switchInt(move _3) -> [0: bb1, otherwise: bb5];
switchInt(move _3) -> [0: bb1, otherwise: bb6];
}

bb1: {
Expand All @@ -54,27 +54,30 @@ fn step_forward(_1: u16, _2: usize) -> u16 {
bb3: {
StorageDead(_5);
StorageDead(_6);
StorageDead(_7);
goto -> bb7;
goto -> bb5;
}

bb4: {
StorageDead(_5);
StorageDead(_6);
StorageDead(_7);
goto -> bb6;
goto -> bb5;
}

bb5: {
StorageDead(_3);
goto -> bb6;
StorageDead(_7);
goto -> bb7;
}

bb6: {
assert(!const true, "attempt to compute `{} + {}`, which would overflow", const core::num::<impl u16>::MAX, const 1_u16) -> [success: bb7, unwind continue];
StorageDead(_3);
goto -> bb7;
}

bb7: {
assert(!const true, "attempt to compute `{} + {}`, which would overflow", const core::num::<impl u16>::MAX, const 1_u16) -> [success: bb8, unwind continue];
}

bb8: {
StorageLive(_8);
_8 = copy _2 as u16 (IntToInt);
_0 = Add(copy _1, copy _8);
Expand Down

0 comments on commit eb99fae

Please sign in to comment.