Skip to content

Commit 8ec49ad

Browse files
committed
Run combine_duplicate_switch_targets after the simplification that produces them
1 parent b658050 commit 8ec49ad

File tree

4 files changed

+19
-20
lines changed

4 files changed

+19
-20
lines changed

compiler/rustc_mir_transform/src/instcombine.rs

+3-18
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
//! Performs various peephole optimizations.
22
3+
use crate::simplify::combine_duplicate_switch_targets;
34
use crate::MirPass;
45
use rustc_hir::Mutability;
5-
use rustc_middle::mir::{
6-
BinOp, Body, CastKind, Constant, ConstantKind, LocalDecls, Operand, Place, ProjectionElem,
7-
Rvalue, SourceInfo, Statement, StatementKind, SwitchTargets, Terminator, TerminatorKind, UnOp,
8-
};
6+
use rustc_middle::mir::*;
97
use rustc_middle::ty::layout::ValidityRequirement;
108
use rustc_middle::ty::util::IntTypeExt;
119
use rustc_middle::ty::{self, ParamEnv, SubstsRef, Ty, TyCtxt};
@@ -46,7 +44,7 @@ impl<'tcx> MirPass<'tcx> for InstCombine {
4644
&mut block.terminator.as_mut().unwrap(),
4745
&mut block.statements,
4846
);
49-
ctx.combine_duplicate_switch_targets(&mut block.terminator.as_mut().unwrap());
47+
combine_duplicate_switch_targets(block.terminator.as_mut().unwrap());
5048
}
5149
}
5250
}
@@ -264,19 +262,6 @@ impl<'tcx> InstCombineContext<'tcx, '_> {
264262
terminator.kind = TerminatorKind::Goto { target: destination_block };
265263
}
266264

267-
fn combine_duplicate_switch_targets(&self, terminator: &mut Terminator<'tcx>) {
268-
let TerminatorKind::SwitchInt { targets, .. } = &mut terminator.kind
269-
else { return };
270-
271-
let otherwise = targets.otherwise();
272-
if targets.iter().any(|t| t.1 == otherwise) {
273-
*targets = SwitchTargets::new(
274-
targets.iter().filter(|t| t.1 != otherwise),
275-
targets.otherwise(),
276-
);
277-
}
278-
}
279-
280265
fn combine_intrinsic_assert(
281266
&self,
282267
terminator: &mut Terminator<'tcx>,

compiler/rustc_mir_transform/src/simplify.rs

+14
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,18 @@ impl<'a, 'tcx> CfgSimplifier<'a, 'tcx> {
278278
}
279279
}
280280

281+
pub fn combine_duplicate_switch_targets(terminator: &mut Terminator<'_>) {
282+
if let TerminatorKind::SwitchInt { targets, .. } = &mut terminator.kind {
283+
let otherwise = targets.otherwise();
284+
if targets.iter().any(|t| t.1 == otherwise) {
285+
*targets = SwitchTargets::new(
286+
targets.iter().filter(|t| t.1 != otherwise),
287+
targets.otherwise(),
288+
);
289+
}
290+
}
291+
}
292+
281293
pub fn remove_duplicate_unreachable_blocks<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
282294
struct OptApplier<'tcx> {
283295
tcx: TyCtxt<'tcx>,
@@ -298,6 +310,8 @@ pub fn remove_duplicate_unreachable_blocks<'tcx>(tcx: TyCtxt<'tcx>, body: &mut B
298310
}
299311
}
300312

313+
combine_duplicate_switch_targets(terminator);
314+
301315
self.super_terminator(terminator, location);
302316
}
303317
}

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
- // + literal: Const { ty: unsafe fn(Option<T>) -> T {Option::<T>::unwrap_unchecked}, val: Value(<ZST>) }
3535
+ StorageLive(_3); // scope 0 at $DIR/unwrap_unchecked.rs:+1:9: +1:27
3636
+ _4 = discriminant(_2); // scope 1 at $SRC_DIR/core/src/option.rs:LL:COL
37-
+ switchInt(move _4) -> [0: bb1, 1: bb2, otherwise: bb1]; // scope 1 at $SRC_DIR/core/src/option.rs:LL:COL
37+
+ switchInt(move _4) -> [1: bb2, otherwise: bb1]; // scope 1 at $SRC_DIR/core/src/option.rs:LL:COL
3838
}
3939

4040
bb1: {

tests/mir-opt/instcombine_duplicate_switch_targets_e2e.ub_if_b.PreCodegen.after.mir

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ fn ub_if_b(_1: Thing) -> Thing {
1313

1414
bb0: {
1515
_2 = discriminant(_1); // scope 0 at $DIR/instcombine_duplicate_switch_targets_e2e.rs:+1:11: +1:12
16-
switchInt(move _2) -> [0: bb2, 1: bb1, otherwise: bb1]; // scope 0 at $DIR/instcombine_duplicate_switch_targets_e2e.rs:+1:5: +1:12
16+
switchInt(move _2) -> [0: bb2, otherwise: bb1]; // scope 0 at $DIR/instcombine_duplicate_switch_targets_e2e.rs:+1:5: +1:12
1717
}
1818

1919
bb1: {

0 commit comments

Comments
 (0)