Skip to content

Commit

Permalink
Adjust JumpThreading Not tests
Browse files Browse the repository at this point in the history
  • Loading branch information
clubby789 committed Oct 5, 2024
1 parent c1af400 commit 4416842
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 9 deletions.
9 changes: 4 additions & 5 deletions compiler/rustc_mir_transform/src/jump_threading.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ impl Condition {
(self.value == value) == (self.polarity == Polarity::Eq)
}

fn inv(mut self, is_bool: bool) -> Self {
fn not(mut self, is_bool: bool) -> Self {
self.value = self.value.not(is_bool);
self
}
Expand Down Expand Up @@ -489,14 +489,13 @@ impl<'a, 'tcx> TOFinder<'a, 'tcx> {
}
}
}
// Transfer the conditions on the copy rhs, after inversing polarity.
// Transfer the conditions on the copy rhs, after inverting the value of the condition.
Rvalue::UnaryOp(UnOp::Not, Operand::Move(place) | Operand::Copy(place)) => {
// If this is a boolean we need to perform a logical, not bitwise, NOT
let is_bool = place.ty(self.body, self.tcx).ty.is_bool();
let Some(conditions) = state.try_get_idx(lhs, &self.map) else { return };
let Some(place) = self.map.find(place.as_ref()) else { return };
// FIXME: I think This could be generalized to not bool if we
// actually perform a logical not on the condition's value.
let conds = conditions.map(self.arena, |cond| cond.inv(is_bool));
let conds = conditions.map(self.arena, |cond| cond.not(is_bool));
state.insert_value_idx(place, conds, &self.map);
}
// We expect `lhs ?= A`. We found `lhs = Eq(rhs, B)`.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

fn bitwise_not() -> i32 {
let mut _0: i32;
let mut _1: i32;
let _1: i32;
let mut _2: bool;
let mut _3: i32;
let mut _4: i32;
Expand All @@ -13,7 +13,6 @@

bb0: {
StorageLive(_1);
_1 = const 0_i32;
_1 = const 1_i32;
StorageLive(_2);
StorageLive(_3);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
- // MIR for `logical_not` before JumpThreading
+ // MIR for `logical_not` after JumpThreading

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

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

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

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

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

13 changes: 11 additions & 2 deletions tests/mir-opt/jump_threading.rs
Original file line number Diff line number Diff line change
Expand Up @@ -535,11 +535,17 @@ pub fn bitwise_not() -> i32 {
// CHECK-LABEL: fn bitwise_not(

// Test for #131195, which was optimizing `!a == b` into `a != b`.
let mut a: i32 = 0;
a = 1;
let a = 1;
if !a == 0 { 1 } else { 0 }
}

pub fn logical_not() -> i32 {
// CHECK-LABEL: fn logical_not(

let a = false;
if !a == true { 1 } else { 0 }
}

fn main() {
// CHECK-LABEL: fn main(
too_complex(Ok(0));
Expand All @@ -555,6 +561,8 @@ fn main() {
aggregate(7);
assume(7, false);
floats();
bitwise_not();
logical_not();
}

// EMIT_MIR jump_threading.too_complex.JumpThreading.diff
Expand All @@ -572,3 +580,4 @@ fn main() {
// EMIT_MIR jump_threading.aggregate_copy.JumpThreading.diff
// EMIT_MIR jump_threading.floats.JumpThreading.diff
// EMIT_MIR jump_threading.bitwise_not.JumpThreading.diff
// EMIT_MIR jump_threading.logical_not.JumpThreading.diff

0 comments on commit 4416842

Please sign in to comment.