Skip to content

Commit 1ce9d8d

Browse files
authored
Rollup merge of rust-lang#131203 - clubby789:jumpthreading-not, r=compiler-errors
JumpThreading: fix bitwise not on non-booleans Fixes rust-lang#131195 Alternative to rust-lang#131201
2 parents 36b7052 + fd35e82 commit 1ce9d8d

File tree

6 files changed

+127
-12
lines changed

6 files changed

+127
-12
lines changed

Diff for: compiler/rustc_middle/src/ty/consts/int.rs

+5
Original file line numberDiff line numberDiff line change
@@ -398,6 +398,11 @@ impl ScalarInt {
398398
pub fn to_f128(self) -> Quad {
399399
self.to_float()
400400
}
401+
402+
#[inline]
403+
pub fn bitwise_not(self) -> Self {
404+
Self { data: self.size().truncate(!self.data), size: self.size }
405+
}
401406
}
402407

403408
macro_rules! from_x_for_scalar_int {

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

+2-4
Original file line numberDiff line numberDiff line change
@@ -150,10 +150,8 @@ impl Condition {
150150
}
151151

152152
fn inv(mut self) -> Self {
153-
self.polarity = match self.polarity {
154-
Polarity::Eq => Polarity::Ne,
155-
Polarity::Ne => Polarity::Eq,
156-
};
153+
self.value = self.value.bitwise_not();
154+
157155
self
158156
}
159157
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
- // MIR for `not_int` before JumpThreading
2+
+ // MIR for `not_int` after JumpThreading
3+
4+
fn not_int() -> i32 {
5+
let mut _0: i32;
6+
let _1: i32;
7+
let mut _2: bool;
8+
let mut _3: i32;
9+
let mut _4: i32;
10+
scope 1 {
11+
debug a => _1;
12+
}
13+
14+
bb0: {
15+
StorageLive(_1);
16+
_1 = const 1_i32;
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 0_i32);
24+
- switchInt(move _2) -> [0: bb2, otherwise: bb1];
25+
+ goto -> bb2;
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+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
- // MIR for `not_int` before JumpThreading
2+
+ // MIR for `not_int` after JumpThreading
3+
4+
fn not_int() -> i32 {
5+
let mut _0: i32;
6+
let _1: i32;
7+
let mut _2: bool;
8+
let mut _3: i32;
9+
let mut _4: i32;
10+
scope 1 {
11+
debug a => _1;
12+
}
13+
14+
bb0: {
15+
StorageLive(_1);
16+
_1 = const 1_i32;
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 0_i32);
24+
- switchInt(move _2) -> [0: bb2, otherwise: bb1];
25+
+ goto -> bb2;
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

+17
Original file line numberDiff line numberDiff line change
@@ -531,6 +531,21 @@ fn floats() -> u32 {
531531
if x == 0.0 { 0 } else { 1 }
532532
}
533533

534+
fn not_int() -> i32 {
535+
// CHECK-LABEL: fn not_int(
536+
// CHECK: goto -> bb2
537+
538+
// CHECK-LABEL: bb1: {
539+
// _0 = const 1_i32;
540+
541+
// CHECK-LABEL: bb2: {
542+
// _0 = const 0_i32;
543+
// Test for issue #131195, where !a == b is assumed to be equivalent to a != b
544+
// This is only the case for bools
545+
let a = 1;
546+
if !a == 0 { 1 } else { 0 }
547+
}
548+
534549
fn main() {
535550
// CHECK-LABEL: fn main(
536551
too_complex(Ok(0));
@@ -546,6 +561,7 @@ fn main() {
546561
aggregate(7);
547562
assume(7, false);
548563
floats();
564+
not_int();
549565
}
550566

551567
// EMIT_MIR jump_threading.too_complex.JumpThreading.diff
@@ -562,3 +578,4 @@ fn main() {
562578
// EMIT_MIR jump_threading.assume.JumpThreading.diff
563579
// EMIT_MIR jump_threading.aggregate_copy.JumpThreading.diff
564580
// EMIT_MIR jump_threading.floats.JumpThreading.diff
581+
// EMIT_MIR jump_threading.not_int.JumpThreading.diff

Diff for: tests/mir-opt/pre-codegen/checked_ops.step_forward.PreCodegen.after.panic-unwind.mir

+11-8
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ fn step_forward(_1: u16, _2: usize) -> u16 {
3333
StorageLive(_4);
3434
StorageLive(_3);
3535
_3 = Gt(copy _2, const 65535_usize);
36-
switchInt(move _3) -> [0: bb1, otherwise: bb5];
36+
switchInt(move _3) -> [0: bb1, otherwise: bb6];
3737
}
3838

3939
bb1: {
@@ -54,27 +54,30 @@ fn step_forward(_1: u16, _2: usize) -> u16 {
5454
bb3: {
5555
StorageDead(_5);
5656
StorageDead(_6);
57-
StorageDead(_7);
58-
goto -> bb7;
57+
goto -> bb5;
5958
}
6059

6160
bb4: {
6261
StorageDead(_5);
6362
StorageDead(_6);
64-
StorageDead(_7);
65-
goto -> bb6;
63+
goto -> bb5;
6664
}
6765

6866
bb5: {
69-
StorageDead(_3);
70-
goto -> bb6;
67+
StorageDead(_7);
68+
goto -> bb7;
7169
}
7270

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

7776
bb7: {
77+
assert(!const true, "attempt to compute `{} + {}`, which would overflow", const core::num::<impl u16>::MAX, const 1_u16) -> [success: bb8, unwind continue];
78+
}
79+
80+
bb8: {
7881
StorageLive(_8);
7982
_8 = copy _2 as u16 (IntToInt);
8083
_0 = Add(copy _1, copy _8);

0 commit comments

Comments
 (0)