Skip to content

Commit 3ccfe76

Browse files
committed
JumpThreading: Re-enable and fix Not ops on non-booleans
1 parent 3002af6 commit 3ccfe76

File tree

4 files changed

+71
-24
lines changed

4 files changed

+71
-24
lines changed

Diff for: compiler/rustc_mir_transform/src/jump_threading.rs

+11-18
Original file line numberDiff line numberDiff line change
@@ -148,14 +148,6 @@ impl Condition {
148148
fn matches(&self, value: ScalarInt) -> bool {
149149
(self.value == value) == (self.polarity == Polarity::Eq)
150150
}
151-
152-
fn inv(mut self) -> Self {
153-
self.polarity = match self.polarity {
154-
Polarity::Eq => Polarity::Ne,
155-
Polarity::Ne => Polarity::Eq,
156-
};
157-
self
158-
}
159151
}
160152

161153
#[derive(Copy, Clone, Debug)]
@@ -492,19 +484,20 @@ impl<'a, 'tcx> TOFinder<'a, 'tcx> {
492484
}
493485
}
494486
}
495-
// Transfer the conditions on the copy rhs, after inversing polarity.
487+
// Transfer the conditions on the copy rhs, after inverting the value of the condition.
496488
Rvalue::UnaryOp(UnOp::Not, Operand::Move(place) | Operand::Copy(place)) => {
497-
if !place.ty(self.body, self.tcx).ty.is_bool() {
498-
// Constructing the conditions by inverting the polarity
499-
// of equality is only correct for bools. That is to say,
500-
// `!a == b` is not `a != b` for integers greater than 1 bit.
501-
return;
502-
}
489+
let layout = self.ecx.layout_of(place.ty(self.body, self.tcx).ty).unwrap();
503490
let Some(conditions) = state.try_get_idx(lhs, &self.map) else { return };
504491
let Some(place) = self.map.find(place.as_ref()) else { return };
505-
// FIXME: I think This could be generalized to not bool if we
506-
// actually perform a logical not on the condition's value.
507-
let conds = conditions.map(self.arena, Condition::inv);
492+
let conds = conditions.map(self.arena, |mut cond| {
493+
cond.value = self
494+
.ecx
495+
.unary_op(UnOp::Not, &ImmTy::from_scalar_int(cond.value, layout))
496+
.unwrap()
497+
.to_scalar_int()
498+
.unwrap();
499+
cond
500+
});
508501
state.insert_value_idx(place, conds, &self.map);
509502
}
510503
// We expect `lhs ?= A`. We found `lhs = Eq(rhs, B)`.

Diff for: tests/mir-opt/jump_threading.bitwise_not.JumpThreading.panic-unwind.diff

+3-3
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
fn bitwise_not() -> i32 {
55
let mut _0: i32;
6-
let mut _1: i32;
6+
let _1: i32;
77
let mut _2: bool;
88
let mut _3: i32;
99
let mut _4: i32;
@@ -13,7 +13,6 @@
1313

1414
bb0: {
1515
StorageLive(_1);
16-
_1 = const 0_i32;
1716
_1 = const 1_i32;
1817
StorageLive(_2);
1918
StorageLive(_3);
@@ -22,7 +21,8 @@
2221
_3 = Not(move _4);
2322
StorageDead(_4);
2423
_2 = Eq(move _3, const 0_i32);
25-
switchInt(move _2) -> [0: bb2, otherwise: bb1];
24+
- switchInt(move _2) -> [0: bb2, otherwise: bb1];
25+
+ goto -> bb2;
2626
}
2727

2828
bb1: {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
- // MIR for `logical_not` before JumpThreading
2+
+ // MIR for `logical_not` after JumpThreading
3+
4+
fn logical_not() -> i32 {
5+
let mut _0: i32;
6+
let _1: bool;
7+
let mut _2: bool;
8+
let mut _3: bool;
9+
let mut _4: bool;
10+
scope 1 {
11+
debug a => _1;
12+
}
13+
14+
bb0: {
15+
StorageLive(_1);
16+
_1 = const false;
17+
StorageLive(_2);
18+
StorageLive(_3);
19+
StorageLive(_4);
20+
_4 = copy _1;
21+
_3 = Not(move _4);
22+
StorageDead(_4);
23+
_2 = Eq(move _3, const true);
24+
- switchInt(move _2) -> [0: bb2, otherwise: bb1];
25+
+ goto -> bb1;
26+
}
27+
28+
bb1: {
29+
StorageDead(_3);
30+
_0 = const 1_i32;
31+
goto -> bb3;
32+
}
33+
34+
bb2: {
35+
StorageDead(_3);
36+
_0 = const 0_i32;
37+
goto -> bb3;
38+
}
39+
40+
bb3: {
41+
StorageDead(_2);
42+
StorageDead(_1);
43+
return;
44+
}
45+
}
46+

Diff for: tests/mir-opt/jump_threading.rs

+11-3
Original file line numberDiff line numberDiff line change
@@ -533,14 +533,19 @@ fn floats() -> u32 {
533533

534534
pub fn bitwise_not() -> i32 {
535535
// CHECK-LABEL: fn bitwise_not(
536-
// CHECK: switchInt(
537536

538537
// Test for #131195, which was optimizing `!a == b` into `a != b`.
539-
let mut a: i32 = 0;
540-
a = 1;
538+
let a = 1;
541539
if !a == 0 { 1 } else { 0 }
542540
}
543541

542+
pub fn logical_not() -> i32 {
543+
// CHECK-LABEL: fn logical_not(
544+
545+
let a = false;
546+
if !a == true { 1 } else { 0 }
547+
}
548+
544549
fn main() {
545550
// CHECK-LABEL: fn main(
546551
too_complex(Ok(0));
@@ -556,6 +561,8 @@ fn main() {
556561
aggregate(7);
557562
assume(7, false);
558563
floats();
564+
bitwise_not();
565+
logical_not();
559566
}
560567

561568
// EMIT_MIR jump_threading.too_complex.JumpThreading.diff
@@ -573,3 +580,4 @@ fn main() {
573580
// EMIT_MIR jump_threading.aggregate_copy.JumpThreading.diff
574581
// EMIT_MIR jump_threading.floats.JumpThreading.diff
575582
// EMIT_MIR jump_threading.bitwise_not.JumpThreading.diff
583+
// EMIT_MIR jump_threading.logical_not.JumpThreading.diff

0 commit comments

Comments
 (0)