Skip to content

Commit

Permalink
Transforms match into an assignment statement
Browse files Browse the repository at this point in the history
  • Loading branch information
DianQK committed Feb 20, 2024
1 parent 6dba010 commit 1446560
Show file tree
Hide file tree
Showing 9 changed files with 364 additions and 115 deletions.
6 changes: 6 additions & 0 deletions compiler/rustc_middle/src/mir/terminator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,12 @@ impl SwitchTargets {
pub fn target_for_value(&self, value: u128) -> BasicBlock {
self.iter().find_map(|(v, t)| (v == value).then_some(t)).unwrap_or_else(|| self.otherwise())
}

/// Returns true if all targets (including the fallback target) are distinct.
#[inline]
pub fn is_distinct(&self) -> bool {
self.targets.iter().collect::<FxHashSet<_>>().len() == self.targets.len()
}
}

pub struct SwitchTargetsIter<'a> {
Expand Down
222 changes: 217 additions & 5 deletions compiler/rustc_mir_transform/src/match_branches.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use rustc_index::IndexVec;
use rustc_middle::mir::*;
use rustc_middle::ty::{ParamEnv, Ty, TyCtxt};
use rustc_middle::ty::{ParamEnv, ScalarInt, Ty, TyCtxt};
use std::iter;

use super::simplify::simplify_cfg;
Expand Down Expand Up @@ -38,6 +38,11 @@ impl<'tcx> MirPass<'tcx> for MatchBranchSimplification {
should_cleanup = true;
continue;
}
if SimplifyToExp::default().simplify(tcx, &mut body.local_decls, bbs, bb_idx, param_env)
{
should_cleanup = true;
continue;
}
}

if should_cleanup {
Expand All @@ -48,7 +53,7 @@ impl<'tcx> MirPass<'tcx> for MatchBranchSimplification {

trait SimplifyMatch<'tcx> {
fn simplify(
&self,
&mut self,
tcx: TyCtxt<'tcx>,
local_decls: &mut IndexVec<Local, LocalDecl<'tcx>>,
bbs: &mut IndexVec<BasicBlock, BasicBlockData<'tcx>>,
Expand All @@ -72,7 +77,7 @@ trait SimplifyMatch<'tcx> {
let source_info = bbs[switch_bb_idx].terminator().source_info;
let discr_local = local_decls.push(LocalDecl::new(discr_ty, source_info.span));

// We already checked that first and second are different blocks,
// We already checked that targets are different blocks,
// and bb_idx has a different terminator from both of them.
let new_stmts = self.new_stmts(tcx, targets, param_env, bbs, discr_local.clone(), discr_ty);
let (_, first) = targets.iter().next().unwrap();
Expand All @@ -91,7 +96,7 @@ trait SimplifyMatch<'tcx> {
}

fn can_simplify(
&self,
&mut self,
tcx: TyCtxt<'tcx>,
targets: &SwitchTargets,
param_env: ParamEnv<'tcx>,
Expand Down Expand Up @@ -144,7 +149,7 @@ struct SimplifyToIf;
/// ```
impl<'tcx> SimplifyMatch<'tcx> for SimplifyToIf {
fn can_simplify(
&self,
&mut self,
tcx: TyCtxt<'tcx>,
targets: &SwitchTargets,
param_env: ParamEnv<'tcx>,
Expand Down Expand Up @@ -250,3 +255,210 @@ impl<'tcx> SimplifyMatch<'tcx> for SimplifyToIf {
new_stmts.collect()
}
}

#[derive(Default)]
struct SimplifyToExp {
transfrom_types: Vec<TransfromType>,
}

#[derive(Clone, Copy)]
enum CompareType<'tcx, 'a> {
Same(&'a StatementKind<'tcx>),
Eq(&'a Place<'tcx>, Ty<'tcx>, ScalarInt),
Discr(&'a Place<'tcx>, Ty<'tcx>),
}

enum TransfromType {
Same,
Eq,
Discr,
}

impl From<CompareType<'_, '_>> for TransfromType {
fn from(compare_type: CompareType<'_, '_>) -> Self {
match compare_type {
CompareType::Same(_) => TransfromType::Same,
CompareType::Eq(_, _, _) => TransfromType::Eq,
CompareType::Discr(_, _) => TransfromType::Discr,
}
}
}

/// If we find that the value of match is the same as the assignment,
/// merge a target block statements into the source block,
/// using cast to transform different integer types.
///
/// For example:
///
/// ```ignore (MIR)
/// bb0: {
/// switchInt(_1) -> [1: bb2, 2: bb3, 3: bb4, otherwise: bb1];
/// }
///
/// bb1: {
/// unreachable;
/// }
///
/// bb2: {
/// _0 = const 1_i16;
/// goto -> bb5;
/// }
///
/// bb3: {
/// _0 = const 2_i16;
/// goto -> bb5;
/// }
///
/// bb4: {
/// _0 = const 3_i16;
/// goto -> bb5;
/// }
/// ```
///
/// into:
///
/// ```ignore (MIR)
/// bb0: {
/// _0 = _3 as i16 (IntToInt);
/// goto -> bb5;
/// }
/// ```
impl<'tcx> SimplifyMatch<'tcx> for SimplifyToExp {
fn can_simplify(
&mut self,
tcx: TyCtxt<'tcx>,
targets: &SwitchTargets,
param_env: ParamEnv<'tcx>,
bbs: &IndexVec<BasicBlock, BasicBlockData<'tcx>>,
) -> bool {
if targets.iter().len() < 2 || targets.iter().len() > 64 {
return false;
}
// We require that the possible target blocks all be distinct.
if !targets.is_distinct() {
return false;
}
if !bbs[targets.otherwise()].is_empty_unreachable() {
return false;
}
let mut iter = targets.iter();
let (first_val, first_target) = iter.next().unwrap();
let first_terminator_kind = &bbs[first_target].terminator().kind;
// Check that destinations are identical, and if not, then don't optimize this block
if !targets
.iter()
.all(|(_, other_target)| first_terminator_kind == &bbs[other_target].terminator().kind)
{
return false;
}

let first_stmts = &bbs[first_target].statements;
let (second_val, second_target) = iter.next().unwrap();
let second_stmts = &bbs[second_target].statements;
if first_stmts.len() != second_stmts.len() {
return false;
}

let mut compare_types = Vec::new();
for (f, s) in iter::zip(first_stmts, second_stmts) {
let compare_type = match (&f.kind, &s.kind) {
// If two statements are exactly the same, we can optimize.
(f_s, s_s) if f_s == s_s => CompareType::Same(f_s),

// If two statements are assignments with the match values to the same place, we can optimize.
(
StatementKind::Assign(box (lhs_f, Rvalue::Use(Operand::Constant(f_c)))),
StatementKind::Assign(box (lhs_s, Rvalue::Use(Operand::Constant(s_c)))),
) if lhs_f == lhs_s
&& f_c.const_.ty() == s_c.const_.ty()
&& f_c.const_.ty().is_integral() =>
{
match (
f_c.const_.try_eval_scalar_int(tcx, param_env),
s_c.const_.try_eval_scalar_int(tcx, param_env),
) {
(Some(f), Some(s)) if f == s => CompareType::Eq(lhs_f, f_c.const_.ty(), f),
(Some(f), Some(s))
if Some(f) == ScalarInt::try_from_uint(first_val, f.size())
&& Some(s) == ScalarInt::try_from_uint(second_val, s.size()) =>
{
CompareType::Discr(lhs_f, f_c.const_.ty())
}
_ => return false,
}
}

// Otherwise we cannot optimize. Try another block.
_ => return false,
};
compare_types.push(compare_type);
}

for (other_val, other_target) in iter {
let other_stmts = &bbs[other_target].statements;
if compare_types.len() != other_stmts.len() {
return false;
}
for (f, s) in iter::zip(&compare_types, other_stmts) {
match (*f, &s.kind) {
(CompareType::Same(f_s), s_s) if f_s == s_s => {}
(
CompareType::Eq(lhs_f, f_ty, val),
StatementKind::Assign(box (lhs_s, Rvalue::Use(Operand::Constant(s_c)))),
) if lhs_f == lhs_s
&& s_c.const_.ty() == f_ty
&& s_c.const_.try_eval_scalar_int(tcx, param_env) == Some(val) => {}
(
CompareType::Discr(lhs_f, f_ty),
StatementKind::Assign(box (lhs_s, Rvalue::Use(Operand::Constant(s_c)))),
) if lhs_f == lhs_s && s_c.const_.ty() == f_ty => {
let Some(f) = s_c.const_.try_eval_scalar_int(tcx, param_env) else {
return false;
};
if Some(f) != ScalarInt::try_from_uint(other_val, f.size()) {
return false;
}
}
_ => return false,
}
}
}
self.transfrom_types = compare_types.into_iter().map(|c| c.into()).collect();
true
}

fn new_stmts(
&self,
_tcx: TyCtxt<'tcx>,
targets: &SwitchTargets,
_param_env: ParamEnv<'tcx>,
bbs: &IndexVec<BasicBlock, BasicBlockData<'tcx>>,
discr_local: Local,
discr_ty: Ty<'tcx>,
) -> Vec<Statement<'tcx>> {
let (_, first) = targets.iter().next().unwrap();
let first = &bbs[first];

let new_stmts =
iter::zip(&self.transfrom_types, &first.statements).map(|(t, s)| match (t, &s.kind) {
(TransfromType::Same, _) | (TransfromType::Eq, _) => (*s).clone(),
(
TransfromType::Discr,
StatementKind::Assign(box (lhs, Rvalue::Use(Operand::Constant(f_c)))),
) => {
let operand = Operand::Copy(Place::from(discr_local));
let r_val = if f_c.const_.ty() == discr_ty {
Rvalue::Use(operand)
} else {
Rvalue::Cast(CastKind::IntToInt, operand, f_c.const_.ty())
};
Statement {
source_info: s.source_info,
kind: StatementKind::Assign(Box::new((*lhs, r_val))),
}
}
_ => unreachable!(),
});
new_stmts.collect()
}
}
4 changes: 2 additions & 2 deletions tests/codegen/match-optimized.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@ pub fn exhaustive_match(e: E) -> u8 {
// CHECK-NEXT: store i8 1, ptr %_0, align 1
// CHECK-NEXT: br label %[[EXIT]]
// CHECK: [[C]]:
// CHECK-NEXT: store i8 2, ptr %_0, align 1
// CHECK-NEXT: store i8 3, ptr %_0, align 1
// CHECK-NEXT: br label %[[EXIT]]
match e {
E::A => 0,
E::B => 1,
E::C => 2,
E::C => 3,
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,37 +5,42 @@
debug i => _1;
let mut _0: u128;
let mut _2: i128;
+ let mut _3: i128;

bb0: {
_2 = discriminant(_1);
switchInt(move _2) -> [1: bb3, 2: bb4, 3: bb5, 340282366920938463463374607431768211455: bb2, otherwise: bb1];
}

bb1: {
unreachable;
}

bb2: {
_0 = const _;
goto -> bb6;
}

bb3: {
_0 = const 1_u128;
goto -> bb6;
}

bb4: {
_0 = const 2_u128;
goto -> bb6;
}

bb5: {
_0 = const 3_u128;
goto -> bb6;
}

bb6: {
- switchInt(move _2) -> [1: bb3, 2: bb4, 3: bb5, 340282366920938463463374607431768211455: bb2, otherwise: bb1];
- }
-
- bb1: {
- unreachable;
- }
-
- bb2: {
- _0 = const _;
- goto -> bb6;
- }
-
- bb3: {
- _0 = const 1_u128;
- goto -> bb6;
- }
-
- bb4: {
- _0 = const 2_u128;
- goto -> bb6;
- }
-
- bb5: {
- _0 = const 3_u128;
- goto -> bb6;
- }
-
- bb6: {
+ StorageLive(_3);
+ _3 = move _2;
+ _0 = _3 as u128 (IntToInt);
+ StorageDead(_3);
return;
}
}
Expand Down
Loading

0 comments on commit 1446560

Please sign in to comment.