Run combine_duplicate_switch_targets after the simplification that produces them
This commit is contained in:
parent
b658050846
commit
8ec49ad19a
4 changed files with 19 additions and 20 deletions
|
@ -1,11 +1,9 @@
|
||||||
//! Performs various peephole optimizations.
|
//! Performs various peephole optimizations.
|
||||||
|
|
||||||
|
use crate::simplify::combine_duplicate_switch_targets;
|
||||||
use crate::MirPass;
|
use crate::MirPass;
|
||||||
use rustc_hir::Mutability;
|
use rustc_hir::Mutability;
|
||||||
use rustc_middle::mir::{
|
use rustc_middle::mir::*;
|
||||||
BinOp, Body, CastKind, Constant, ConstantKind, LocalDecls, Operand, Place, ProjectionElem,
|
|
||||||
Rvalue, SourceInfo, Statement, StatementKind, SwitchTargets, Terminator, TerminatorKind, UnOp,
|
|
||||||
};
|
|
||||||
use rustc_middle::ty::layout::ValidityRequirement;
|
use rustc_middle::ty::layout::ValidityRequirement;
|
||||||
use rustc_middle::ty::util::IntTypeExt;
|
use rustc_middle::ty::util::IntTypeExt;
|
||||||
use rustc_middle::ty::{self, ParamEnv, SubstsRef, Ty, TyCtxt};
|
use rustc_middle::ty::{self, ParamEnv, SubstsRef, Ty, TyCtxt};
|
||||||
|
@ -46,7 +44,7 @@ impl<'tcx> MirPass<'tcx> for InstCombine {
|
||||||
&mut block.terminator.as_mut().unwrap(),
|
&mut block.terminator.as_mut().unwrap(),
|
||||||
&mut block.statements,
|
&mut block.statements,
|
||||||
);
|
);
|
||||||
ctx.combine_duplicate_switch_targets(&mut block.terminator.as_mut().unwrap());
|
combine_duplicate_switch_targets(block.terminator.as_mut().unwrap());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -264,19 +262,6 @@ impl<'tcx> InstCombineContext<'tcx, '_> {
|
||||||
terminator.kind = TerminatorKind::Goto { target: destination_block };
|
terminator.kind = TerminatorKind::Goto { target: destination_block };
|
||||||
}
|
}
|
||||||
|
|
||||||
fn combine_duplicate_switch_targets(&self, terminator: &mut Terminator<'tcx>) {
|
|
||||||
let TerminatorKind::SwitchInt { targets, .. } = &mut terminator.kind
|
|
||||||
else { return };
|
|
||||||
|
|
||||||
let otherwise = targets.otherwise();
|
|
||||||
if targets.iter().any(|t| t.1 == otherwise) {
|
|
||||||
*targets = SwitchTargets::new(
|
|
||||||
targets.iter().filter(|t| t.1 != otherwise),
|
|
||||||
targets.otherwise(),
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn combine_intrinsic_assert(
|
fn combine_intrinsic_assert(
|
||||||
&self,
|
&self,
|
||||||
terminator: &mut Terminator<'tcx>,
|
terminator: &mut Terminator<'tcx>,
|
||||||
|
|
|
@ -278,6 +278,18 @@ impl<'a, 'tcx> CfgSimplifier<'a, 'tcx> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn combine_duplicate_switch_targets(terminator: &mut Terminator<'_>) {
|
||||||
|
if let TerminatorKind::SwitchInt { targets, .. } = &mut terminator.kind {
|
||||||
|
let otherwise = targets.otherwise();
|
||||||
|
if targets.iter().any(|t| t.1 == otherwise) {
|
||||||
|
*targets = SwitchTargets::new(
|
||||||
|
targets.iter().filter(|t| t.1 != otherwise),
|
||||||
|
targets.otherwise(),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub fn remove_duplicate_unreachable_blocks<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
|
pub fn remove_duplicate_unreachable_blocks<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
|
||||||
struct OptApplier<'tcx> {
|
struct OptApplier<'tcx> {
|
||||||
tcx: TyCtxt<'tcx>,
|
tcx: TyCtxt<'tcx>,
|
||||||
|
@ -298,6 +310,8 @@ pub fn remove_duplicate_unreachable_blocks<'tcx>(tcx: TyCtxt<'tcx>, body: &mut B
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
combine_duplicate_switch_targets(terminator);
|
||||||
|
|
||||||
self.super_terminator(terminator, location);
|
self.super_terminator(terminator, location);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -34,7 +34,7 @@
|
||||||
- // + literal: Const { ty: unsafe fn(Option<T>) -> T {Option::<T>::unwrap_unchecked}, val: Value(<ZST>) }
|
- // + literal: Const { ty: unsafe fn(Option<T>) -> T {Option::<T>::unwrap_unchecked}, val: Value(<ZST>) }
|
||||||
+ StorageLive(_3); // scope 0 at $DIR/unwrap_unchecked.rs:+1:9: +1:27
|
+ StorageLive(_3); // scope 0 at $DIR/unwrap_unchecked.rs:+1:9: +1:27
|
||||||
+ _4 = discriminant(_2); // scope 1 at $SRC_DIR/core/src/option.rs:LL:COL
|
+ _4 = discriminant(_2); // scope 1 at $SRC_DIR/core/src/option.rs:LL:COL
|
||||||
+ switchInt(move _4) -> [0: bb1, 1: bb2, otherwise: bb1]; // scope 1 at $SRC_DIR/core/src/option.rs:LL:COL
|
+ switchInt(move _4) -> [1: bb2, otherwise: bb1]; // scope 1 at $SRC_DIR/core/src/option.rs:LL:COL
|
||||||
}
|
}
|
||||||
|
|
||||||
bb1: {
|
bb1: {
|
||||||
|
|
|
@ -13,7 +13,7 @@ fn ub_if_b(_1: Thing) -> Thing {
|
||||||
|
|
||||||
bb0: {
|
bb0: {
|
||||||
_2 = discriminant(_1); // scope 0 at $DIR/instcombine_duplicate_switch_targets_e2e.rs:+1:11: +1:12
|
_2 = discriminant(_1); // scope 0 at $DIR/instcombine_duplicate_switch_targets_e2e.rs:+1:11: +1:12
|
||||||
switchInt(move _2) -> [0: bb2, 1: bb1, otherwise: bb1]; // scope 0 at $DIR/instcombine_duplicate_switch_targets_e2e.rs:+1:5: +1:12
|
switchInt(move _2) -> [0: bb2, otherwise: bb1]; // scope 0 at $DIR/instcombine_duplicate_switch_targets_e2e.rs:+1:5: +1:12
|
||||||
}
|
}
|
||||||
|
|
||||||
bb1: {
|
bb1: {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue