Skip to content

Commit 9ad64b9

Browse files
committed
Only emit != assumptions if the otherwise target is reachable.
1 parent c962b7e commit 9ad64b9

10 files changed

+24
-68
lines changed

compiler/rustc_mir_transform/src/unreachable_prop.rs

+6-7
Original file line numberDiff line numberDiff line change
@@ -116,22 +116,21 @@ fn remove_successors_from_switch<'tcx>(
116116
patch.add_statement(location, StatementKind::Intrinsic(Box::new(assume)));
117117
};
118118

119+
let otherwise = targets.otherwise();
120+
let otherwise_unreachable = is_unreachable(otherwise);
121+
119122
let reachable_iter = targets.iter().filter(|&(value, bb)| {
120123
let is_unreachable = is_unreachable(bb);
121-
if is_unreachable {
122-
// We remove this target from the switch, so record the inequality using `Assume`.
124+
// We remove this target from the switch, so record the inequality using `Assume`.
125+
if is_unreachable && !otherwise_unreachable {
123126
add_assumption(BinOp::Ne, value);
124-
false
125-
} else {
126-
true
127127
}
128+
!is_unreachable
128129
});
129130

130-
let otherwise = targets.otherwise();
131131
let new_targets = SwitchTargets::new(reachable_iter, otherwise);
132132

133133
let num_targets = new_targets.all_targets().len();
134-
let otherwise_unreachable = is_unreachable(otherwise);
135134
let fully_unreachable = num_targets == 1 && otherwise_unreachable;
136135

137136
let terminator = match (num_targets, otherwise_unreachable) {

tests/mir-opt/inline/unwrap_unchecked.unwrap_unchecked.Inline.panic-abort.diff

+1-6
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
+ let mut _3: &std::option::Option<T>;
1111
+ let mut _4: isize;
1212
+ let mut _5: bool;
13-
+ let mut _6: bool;
1413
+ scope 2 {
1514
+ debug val => _0;
1615
+ }
@@ -37,14 +36,10 @@
3736
+ StorageLive(_3);
3837
+ StorageLive(_4);
3938
+ StorageLive(_5);
40-
+ StorageLive(_6);
4139
+ _4 = discriminant(_2);
42-
+ _5 = Ne(_4, const 0_isize);
40+
+ _5 = Eq(_4, const 1_isize);
4341
+ assume(move _5);
44-
+ _6 = Eq(_4, const 1_isize);
45-
+ assume(move _6);
4642
+ _0 = move ((_2 as Some).0: T);
47-
+ StorageDead(_6);
4843
+ StorageDead(_5);
4944
+ StorageDead(_4);
5045
+ StorageDead(_3);

tests/mir-opt/inline/unwrap_unchecked.unwrap_unchecked.Inline.panic-unwind.diff

+1-6
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
+ let mut _3: &std::option::Option<T>;
1111
+ let mut _4: isize;
1212
+ let mut _5: bool;
13-
+ let mut _6: bool;
1413
+ scope 2 {
1514
+ debug val => _0;
1615
+ }
@@ -37,14 +36,10 @@
3736
+ StorageLive(_3);
3837
+ StorageLive(_4);
3938
+ StorageLive(_5);
40-
+ StorageLive(_6);
4139
+ _4 = discriminant(_2);
42-
+ _5 = Ne(_4, const 0_isize);
40+
+ _5 = Eq(_4, const 1_isize);
4341
+ assume(move _5);
44-
+ _6 = Eq(_4, const 1_isize);
45-
+ assume(move _6);
4642
+ _0 = move ((_2 as Some).0: T);
47-
+ StorageDead(_6);
4843
+ StorageDead(_5);
4944
+ StorageDead(_4);
5045
+ StorageDead(_3);

tests/mir-opt/inline/unwrap_unchecked.unwrap_unchecked.PreCodegen.after.panic-abort.mir

+5-10
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@ fn unwrap_unchecked(_1: Option<T>) -> T {
77
debug self => _1;
88
let mut _2: isize;
99
let mut _3: bool;
10-
let mut _4: bool;
11-
let mut _5: &std::option::Option<T>;
10+
let mut _4: &std::option::Option<T>;
1211
scope 2 {
1312
debug val => _0;
1413
}
@@ -21,25 +20,21 @@ fn unwrap_unchecked(_1: Option<T>) -> T {
2120
}
2221
}
2322
scope 4 (inlined Option::<T>::is_some) {
24-
debug self => _5;
23+
debug self => _4;
2524
}
2625
}
2726

2827
bb0: {
29-
StorageLive(_5);
30-
StorageLive(_3);
3128
StorageLive(_4);
3229
StorageLive(_2);
30+
StorageLive(_3);
3331
_2 = discriminant(_1);
34-
_3 = Ne(_2, const 0_isize);
32+
_3 = Eq(_2, const 1_isize);
3533
assume(move _3);
36-
_4 = Eq(_2, const 1_isize);
37-
assume(move _4);
3834
_0 = move ((_1 as Some).0: T);
35+
StorageDead(_3);
3936
StorageDead(_2);
4037
StorageDead(_4);
41-
StorageDead(_3);
42-
StorageDead(_5);
4338
return;
4439
}
4540
}

tests/mir-opt/inline/unwrap_unchecked.unwrap_unchecked.PreCodegen.after.panic-unwind.mir

+5-10
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@ fn unwrap_unchecked(_1: Option<T>) -> T {
77
debug self => _1;
88
let mut _2: isize;
99
let mut _3: bool;
10-
let mut _4: bool;
11-
let mut _5: &std::option::Option<T>;
10+
let mut _4: &std::option::Option<T>;
1211
scope 2 {
1312
debug val => _0;
1413
}
@@ -21,25 +20,21 @@ fn unwrap_unchecked(_1: Option<T>) -> T {
2120
}
2221
}
2322
scope 4 (inlined Option::<T>::is_some) {
24-
debug self => _5;
23+
debug self => _4;
2524
}
2625
}
2726

2827
bb0: {
29-
StorageLive(_5);
30-
StorageLive(_3);
3128
StorageLive(_4);
3229
StorageLive(_2);
30+
StorageLive(_3);
3331
_2 = discriminant(_1);
34-
_3 = Ne(_2, const 0_isize);
32+
_3 = Eq(_2, const 1_isize);
3533
assume(move _3);
36-
_4 = Eq(_2, const 1_isize);
37-
assume(move _4);
3834
_0 = move ((_1 as Some).0: T);
35+
StorageDead(_3);
3936
StorageDead(_2);
4037
StorageDead(_4);
41-
StorageDead(_3);
42-
StorageDead(_5);
4338
return;
4439
}
4540
}

tests/mir-opt/pre-codegen/duplicate_switch_targets.ub_if_b.PreCodegen.after.mir

+1-4
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ fn ub_if_b(_1: Thing) -> Thing {
55
let mut _0: Thing;
66
let mut _2: isize;
77
let mut _3: bool;
8-
let mut _4: bool;
98
scope 1 (inlined unreachable_unchecked) {
109
scope 2 {
1110
scope 3 (inlined unreachable_unchecked::runtime) {
@@ -15,10 +14,8 @@ fn ub_if_b(_1: Thing) -> Thing {
1514

1615
bb0: {
1716
_2 = discriminant(_1);
18-
_3 = Ne(_2, const 1_isize);
17+
_3 = Eq(_2, const 0_isize);
1918
assume(move _3);
20-
_4 = Eq(_2, const 0_isize);
21-
assume(move _4);
2219
_0 = move _1;
2320
return;
2421
}

tests/mir-opt/uninhabited_enum_branching.main.SimplifyCfg-after-uninhabited-enum-branching.after.mir

+1-7
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,14 @@ fn main() -> () {
1212
let mut _8: isize;
1313
let _9: &str;
1414
let mut _10: bool;
15-
let mut _11: bool;
16-
let mut _12: bool;
1715

1816
bb0: {
1917
StorageLive(_1);
2018
StorageLive(_2);
2119
_2 = Test1::C;
2220
_3 = discriminant(_2);
23-
_10 = Ne(_3, const 0_isize);
21+
_10 = Eq(_3, const 2_isize);
2422
assume(move _10);
25-
_11 = Ne(_3, const 1_isize);
26-
assume(move _11);
27-
_12 = Eq(_3, const 2_isize);
28-
assume(move _12);
2923
StorageLive(_5);
3024
_5 = const "C";
3125
_1 = &(*_5);

tests/mir-opt/uninhabited_enum_branching2.main.SimplifyCfg-after-uninhabited-enum-branching.after.mir

-12
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,6 @@ fn main() -> () {
1515
let _11: &str;
1616
let _12: &str;
1717
let _13: &str;
18-
let mut _14: bool;
19-
let mut _15: bool;
20-
let mut _16: bool;
21-
let mut _17: bool;
2218
scope 1 {
2319
debug plop => _1;
2420
}
@@ -33,10 +29,6 @@ fn main() -> () {
3329
StorageLive(_4);
3430
_4 = &(_1.1: Test1);
3531
_5 = discriminant((*_4));
36-
_16 = Ne(_5, const 0_isize);
37-
assume(move _16);
38-
_17 = Ne(_5, const 1_isize);
39-
assume(move _17);
4032
switchInt(move _5) -> [2: bb3, 3: bb1, otherwise: bb2];
4133
}
4234

@@ -65,10 +57,6 @@ fn main() -> () {
6557
StorageDead(_3);
6658
StorageLive(_9);
6759
_10 = discriminant((_1.1: Test1));
68-
_14 = Ne(_10, const 0_isize);
69-
assume(move _14);
70-
_15 = Ne(_10, const 1_isize);
71-
assume(move _15);
7260
switchInt(move _10) -> [2: bb6, 3: bb5, otherwise: bb2];
7361
}
7462

tests/mir-opt/unreachable.main.UnreachablePropagation.panic-abort.diff

+2-3
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
let mut _6: bool;
1010
let mut _7: !;
1111
+ let mut _8: bool;
12-
+ let mut _9: bool;
1312
scope 1 {
1413
debug _x => _3;
1514
let _3: Empty;
@@ -27,8 +26,8 @@
2726
bb1: {
2827
_2 = discriminant(_1);
2928
- switchInt(move _2) -> [1: bb2, otherwise: bb6];
30-
+ _9 = Ne(_2, const 1_isize);
31-
+ assume(move _9);
29+
+ _8 = Ne(_2, const 1_isize);
30+
+ assume(move _8);
3231
+ goto -> bb6;
3332
}
3433

tests/mir-opt/unreachable.main.UnreachablePropagation.panic-unwind.diff

+2-3
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
let mut _6: bool;
1010
let mut _7: !;
1111
+ let mut _8: bool;
12-
+ let mut _9: bool;
1312
scope 1 {
1413
debug _x => _3;
1514
let _3: Empty;
@@ -27,8 +26,8 @@
2726
bb1: {
2827
_2 = discriminant(_1);
2928
- switchInt(move _2) -> [1: bb2, otherwise: bb6];
30-
+ _9 = Ne(_2, const 1_isize);
31-
+ assume(move _9);
29+
+ _8 = Ne(_2, const 1_isize);
30+
+ assume(move _8);
3231
+ goto -> bb6;
3332
}
3433

0 commit comments

Comments
 (0)