Skip to content

Commit

Permalink
MatchBranchSimplification: Consider empty-unreachable otherwise branch
Browse files Browse the repository at this point in the history
  • Loading branch information
clubby789 committed Oct 4, 2024
1 parent a71df24 commit 67c9f97
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 19 deletions.
7 changes: 7 additions & 0 deletions compiler/rustc_data_structures/src/packed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@ impl Pu128 {
}
}

impl From<Pu128> for u128 {
#[inline]
fn from(value: Pu128) -> Self {
value.get()
}
}

impl From<u128> for Pu128 {
#[inline]
fn from(value: u128) -> Self {
Expand Down
11 changes: 11 additions & 0 deletions compiler/rustc_middle/src/mir/terminator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,17 @@ impl SwitchTargets {
&mut self.targets
}

/// Returns a slice with all considered values (not including the fallback).
#[inline]
pub fn all_values(&self) -> &[Pu128] {
&self.values
}

#[inline]
pub fn all_values_mut(&mut self) -> &mut [Pu128] {
&mut self.values
}

/// Finds the `BasicBlock` to which this `SwitchInt` will branch given the
/// specific value. This cannot fail, as it'll return the `otherwise`
/// branch if there's not a specific match for the value.
Expand Down
30 changes: 21 additions & 9 deletions compiler/rustc_mir_transform/src/match_branches.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use rustc_middle::ty::layout::{IntegerExt, TyAndLayout};
use rustc_middle::ty::{ParamEnv, ScalarInt, Ty, TyCtxt};
use rustc_target::abi::Integer;
use rustc_type_ir::TyKind::*;
use tracing::instrument;

use super::simplify::simplify_cfg;

Expand Down Expand Up @@ -57,7 +58,7 @@ impl<'tcx> crate::MirPass<'tcx> for MatchBranchSimplification {
}

trait SimplifyMatch<'tcx> {
/// Simplifies a match statement, returning true if the simplification succeeds, false
/// Simplifies a match statement, returning `Some` if the simplification succeeds, `None`
/// otherwise. Generic code is written here, and we generally don't need a custom
/// implementation.
fn simplify(
Expand Down Expand Up @@ -156,6 +157,7 @@ struct SimplifyToIf;
/// }
/// ```
impl<'tcx> SimplifyMatch<'tcx> for SimplifyToIf {
#[instrument(level = "debug", skip(self, tcx), ret)]
fn can_simplify(
&mut self,
tcx: TyCtxt<'tcx>,
Expand All @@ -164,12 +166,15 @@ impl<'tcx> SimplifyMatch<'tcx> for SimplifyToIf {
bbs: &IndexSlice<BasicBlock, BasicBlockData<'tcx>>,
_discr_ty: Ty<'tcx>,
) -> Option<()> {
if targets.iter().len() != 1 {
return None;
}
let (first, second) = match targets.all_targets() {
&[first, otherwise] => (first, otherwise),
&[first, second, otherwise] if bbs[otherwise].is_empty_unreachable() => (first, second),
_ => {
return None;
}
};

// We require that the possible target blocks all be distinct.
let (_, first) = targets.iter().next().unwrap();
let second = targets.otherwise();
if first == second {
return None;
}
Expand Down Expand Up @@ -218,8 +223,14 @@ impl<'tcx> SimplifyMatch<'tcx> for SimplifyToIf {
discr_local: Local,
discr_ty: Ty<'tcx>,
) {
let (val, first) = targets.iter().next().unwrap();
let second = targets.otherwise();
let ((val, first), second) = match (targets.all_targets(), targets.all_values()) {
(&[first, otherwise], &[val]) => ((val, first), otherwise),
(&[first, second, otherwise], &[val, _]) if bbs[otherwise].is_empty_unreachable() => {
((val, first), second)
}
_ => unreachable!(),
};

// We already checked that first and second are different blocks,
// and bb_idx has a different terminator from both of them.
let first = &bbs[first];
Expand Down Expand Up @@ -294,7 +305,7 @@ struct SimplifyToExp {
transform_kinds: Vec<TransformKind>,
}

#[derive(Clone, Copy)]
#[derive(Clone, Copy, Debug)]
enum ExpectedTransformKind<'a, 'tcx> {
/// Identical statements.
Same(&'a StatementKind<'tcx>),
Expand Down Expand Up @@ -359,6 +370,7 @@ impl From<ExpectedTransformKind<'_, '_>> for TransformKind {
/// }
/// ```
impl<'tcx> SimplifyMatch<'tcx> for SimplifyToExp {
#[instrument(level = "debug", skip(self, tcx), ret)]
fn can_simplify(
&mut self,
tcx: TyCtxt<'tcx>,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,20 @@ fn num_to_digit(_1: char) -> u32 {
bb1: {
StorageLive(_3);
_3 = discriminant(_2);
switchInt(move _3) -> [1: bb2, 0: bb6, otherwise: bb8];
StorageDead(_2);
switchInt(move _3) -> [1: bb2, otherwise: bb7];
}

bb2: {
StorageDead(_3);
StorageDead(_2);
StorageLive(_4);
_4 = char::methods::<impl char>::to_digit(move _1, const 8_u32) -> [return: bb3, unwind continue];
}

bb3: {
StorageLive(_5);
_5 = discriminant(_4);
switchInt(move _5) -> [0: bb4, 1: bb5, otherwise: bb8];
switchInt(move _5) -> [0: bb4, 1: bb5, otherwise: bb6];
}

bb4: {
Expand All @@ -49,21 +49,20 @@ fn num_to_digit(_1: char) -> u32 {
_0 = move ((_4 as Some).0: u32);
StorageDead(_5);
StorageDead(_4);
goto -> bb7;
goto -> bb8;
}

bb6: {
StorageDead(_3);
StorageDead(_2);
_0 = const 0_u32;
goto -> bb7;
unreachable;
}

bb7: {
return;
StorageDead(_3);
_0 = const 0_u32;
goto -> bb8;
}

bb8: {
unreachable;
return;
}
}
3 changes: 3 additions & 0 deletions tests/mir-opt/matches_reduce_branches.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ fn foo(bar: Option<()>) {

// EMIT_MIR matches_reduce_branches.my_is_some.MatchBranchSimplification.diff
fn my_is_some<T>(bar: &Option<T>) -> bool {
// CHECK-LABEL: fn my_is_some(
// CHECK: = Eq(
// CHECK: return
matches!(bar, Some(_))
}

Expand Down

0 comments on commit 67c9f97

Please sign in to comment.