From a6c2cb42d0b7ad5bafcd2a9b4784ff6f26763b07 Mon Sep 17 00:00:00 2001 From: kadmin Date: Mon, 10 Aug 2020 23:11:05 +0000 Subject: [PATCH 1/6] First iteration of simplify match branches --- src/librustc_mir/transform/match_branches.rs | 102 +++++++++++++++++++ src/librustc_mir/transform/mod.rs | 1 + src/test/mir-opt/matches_reduce_branches.rs | 13 +++ 3 files changed, 116 insertions(+) create mode 100644 src/librustc_mir/transform/match_branches.rs create mode 100644 src/test/mir-opt/matches_reduce_branches.rs diff --git a/src/librustc_mir/transform/match_branches.rs b/src/librustc_mir/transform/match_branches.rs new file mode 100644 index 0000000000000..5dc84955add6b --- /dev/null +++ b/src/librustc_mir/transform/match_branches.rs @@ -0,0 +1,102 @@ +use crate::transform::{simplify, MirPass, MirSource}; +use rustc_middle::mir::*; +use rustc_middle::ty::TyCtxt; + +pub struct MatchBranchSimplification; + +// What's the intent of this pass? +// If one block is found that switches between blocks which both go to the same place +// AND both of these blocks set a similar const in their -> +// condense into 1 block based on discriminant AND goto the destination afterwards + +impl<'tcx> MirPass<'tcx> for MatchBranchSimplification { + fn run_pass(&self, tcx: TyCtxt<'tcx>, src: MirSource<'tcx>, body: &mut Body<'tcx>) { + let param_env = tcx.param_env(src.def_id()); + let mut did_remove_blocks = false; + let bbs = body.basic_blocks_mut(); + 'outer: for bb_idx in bbs.indices() { + let (discr, val, switch_ty, targets) = match bbs[bb_idx].terminator().kind { + TerminatorKind::SwitchInt { + discr: Operand::Move(ref place), + switch_ty, + ref targets, + ref values, + .. + } if targets.len() == 2 && values.len() == 1 => { + (place.clone(), values[0], switch_ty, targets) + } + _ => continue, + }; + let (first, rest) = if let ([first], rest) = targets.split_at(1) { + (*first, rest) + } else { + unreachable!(); + }; + let first_dest = bbs[first].terminator().kind.clone(); + let same_destinations = rest + .iter() + .map(|target| &bbs[*target].terminator().kind) + .all(|t_kind| t_kind == &first_dest); + if !same_destinations { + continue; + } + let first_stmts = &bbs[first].statements; + for s in first_stmts.iter() { + match &s.kind { + StatementKind::Assign(box (_, rhs)) => { + if let Rvalue::Use(Operand::Constant(_)) = rhs { + } else { + continue 'outer; + } + } + _ => continue 'outer, + } + } + for target in rest.iter() { + for s in bbs[*target].statements.iter() { + if let StatementKind::Assign(box (ref lhs, rhs)) = &s.kind { + if let Rvalue::Use(Operand::Constant(_)) = rhs { + let has_matching_assn = first_stmts + .iter() + .find(|s| { + if let StatementKind::Assign(box (lhs_f, _)) = &s.kind { + lhs_f == lhs + } else { + false + } + }) + .is_some(); + if has_matching_assn { + continue; + } + } + } + + continue 'outer; + } + } + let (first_block, to_add) = bbs.pick2_mut(first, bb_idx); + let new_stmts = first_block.statements.iter().cloned().map(|mut s| { + if let StatementKind::Assign(box (_, ref mut rhs)) = s.kind { + let size = tcx.layout_of(param_env.and(switch_ty)).unwrap().size; + let const_cmp = Operand::const_from_scalar( + tcx, + switch_ty, + crate::interpret::Scalar::from_uint(val, size), + rustc_span::DUMMY_SP, + ); + *rhs = Rvalue::BinaryOp(BinOp::Eq, Operand::Move(discr), const_cmp); + } else { + unreachable!() + } + s + }); + to_add.statements.extend(new_stmts); + to_add.terminator_mut().kind = first_dest; + did_remove_blocks = true; + } + if did_remove_blocks { + simplify::remove_dead_blocks(body); + } + } +} diff --git a/src/librustc_mir/transform/mod.rs b/src/librustc_mir/transform/mod.rs index 3803ee78fd4d9..78deb96f5a9f1 100644 --- a/src/librustc_mir/transform/mod.rs +++ b/src/librustc_mir/transform/mod.rs @@ -29,6 +29,7 @@ pub mod generator; pub mod inline; pub mod instcombine; pub mod instrument_coverage; +pub mod match_branches; pub mod no_landing_pads; pub mod nrvo; pub mod promote_consts; diff --git a/src/test/mir-opt/matches_reduce_branches.rs b/src/test/mir-opt/matches_reduce_branches.rs new file mode 100644 index 0000000000000..6ccdaaf7bc387 --- /dev/null +++ b/src/test/mir-opt/matches_reduce_branches.rs @@ -0,0 +1,13 @@ +// compile-flags: --emit mir +// EMIT_MIR matches_reduce_branches.foo.fix_match_arms.diff + +fn foo(bar: Option<()>) { + if matches!(bar, None) { + () + } +} + +fn main() { + let _ = foo(None); + let _ = foo(Some(())); +} From 6c0f2a9446f8858e7b0f5725c6487be3edb3b87e Mon Sep 17 00:00:00 2001 From: kadmin Date: Tue, 11 Aug 2020 02:36:32 +0000 Subject: [PATCH 2/6] Update to actually use transform --- src/librustc_infer/infer/error_reporting/mod.rs | 2 +- src/librustc_mir/transform/mod.rs | 1 + src/test/mir-opt/matches_reduce_branches.rs | 3 +-- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/librustc_infer/infer/error_reporting/mod.rs b/src/librustc_infer/infer/error_reporting/mod.rs index 063246f79fe36..2622069819006 100644 --- a/src/librustc_infer/infer/error_reporting/mod.rs +++ b/src/librustc_infer/infer/error_reporting/mod.rs @@ -827,7 +827,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> { ty::GenericParamDefKind::Type { has_default, .. } => { Some((param.def_id, has_default)) } - ty::GenericParamDefKind::Const => None, // FIXME(const_generics:defaults) + ty::GenericParamDefKind::Const { .. } => None, // FIXME(const_generics:defaults) }) .peekable(); let has_default = { diff --git a/src/librustc_mir/transform/mod.rs b/src/librustc_mir/transform/mod.rs index 78deb96f5a9f1..b84514d1e9452 100644 --- a/src/librustc_mir/transform/mod.rs +++ b/src/librustc_mir/transform/mod.rs @@ -452,6 +452,7 @@ fn run_optimization_passes<'tcx>( &simplify::SimplifyCfg::new("final"), &nrvo::RenameReturnPlace, &simplify::SimplifyLocals, + &match_branches::MatchBranchSimplification, ]; let no_optimizations: &[&dyn MirPass<'tcx>] = &[ diff --git a/src/test/mir-opt/matches_reduce_branches.rs b/src/test/mir-opt/matches_reduce_branches.rs index 6ccdaaf7bc387..2d1a6d8de21a6 100644 --- a/src/test/mir-opt/matches_reduce_branches.rs +++ b/src/test/mir-opt/matches_reduce_branches.rs @@ -1,5 +1,4 @@ -// compile-flags: --emit mir -// EMIT_MIR matches_reduce_branches.foo.fix_match_arms.diff +// EMIT_MIR matches_reduce_branches.foo.MatchBranchSimplification.diff fn foo(bar: Option<()>) { if matches!(bar, None) { From bce5eb0c08b33cf4338020a933f28cd87bbd89d1 Mon Sep 17 00:00:00 2001 From: kadmin Date: Tue, 11 Aug 2020 22:04:49 +0000 Subject: [PATCH 3/6] Update w/ comments from oli This also updates a check to ensure that this is only applied to bools --- .../infer/error_reporting/mod.rs | 2 +- src/librustc_mir/transform/match_branches.rs | 93 ++++++++----------- src/librustc_mir/transform/mod.rs | 2 +- 3 files changed, 40 insertions(+), 57 deletions(-) diff --git a/src/librustc_infer/infer/error_reporting/mod.rs b/src/librustc_infer/infer/error_reporting/mod.rs index 2622069819006..063246f79fe36 100644 --- a/src/librustc_infer/infer/error_reporting/mod.rs +++ b/src/librustc_infer/infer/error_reporting/mod.rs @@ -827,7 +827,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> { ty::GenericParamDefKind::Type { has_default, .. } => { Some((param.def_id, has_default)) } - ty::GenericParamDefKind::Const { .. } => None, // FIXME(const_generics:defaults) + ty::GenericParamDefKind::Const => None, // FIXME(const_generics:defaults) }) .peekable(); let has_default = { diff --git a/src/librustc_mir/transform/match_branches.rs b/src/librustc_mir/transform/match_branches.rs index 5dc84955add6b..5fab46f029ff0 100644 --- a/src/librustc_mir/transform/match_branches.rs +++ b/src/librustc_mir/transform/match_branches.rs @@ -1,4 +1,4 @@ -use crate::transform::{simplify, MirPass, MirSource}; +use crate::transform::{MirPass, MirSource}; use rustc_middle::mir::*; use rustc_middle::ty::TyCtxt; @@ -12,10 +12,9 @@ pub struct MatchBranchSimplification; impl<'tcx> MirPass<'tcx> for MatchBranchSimplification { fn run_pass(&self, tcx: TyCtxt<'tcx>, src: MirSource<'tcx>, body: &mut Body<'tcx>) { let param_env = tcx.param_env(src.def_id()); - let mut did_remove_blocks = false; let bbs = body.basic_blocks_mut(); 'outer: for bb_idx in bbs.indices() { - let (discr, val, switch_ty, targets) = match bbs[bb_idx].terminator().kind { + let (discr, val, switch_ty, first, second) = match bbs[bb_idx].terminator().kind { TerminatorKind::SwitchInt { discr: Operand::Move(ref place), switch_ty, @@ -23,60 +22,53 @@ impl<'tcx> MirPass<'tcx> for MatchBranchSimplification { ref values, .. } if targets.len() == 2 && values.len() == 1 => { - (place.clone(), values[0], switch_ty, targets) + (place, values[0], switch_ty, targets[0], targets[1]) } + // Only optimize switch int statements _ => continue, }; - let (first, rest) = if let ([first], rest) = targets.split_at(1) { - (*first, rest) - } else { - unreachable!(); - }; - let first_dest = bbs[first].terminator().kind.clone(); - let same_destinations = rest - .iter() - .map(|target| &bbs[*target].terminator().kind) - .all(|t_kind| t_kind == &first_dest); - if !same_destinations { + + // Check that destinations are identical, and if not, then don't optimize this block + if &bbs[first].terminator().kind != &bbs[second].terminator().kind { continue; } + + // Check that blocks are assignments of consts to the same place or same statement, + // and match up 1-1, if not don't optimize this block. let first_stmts = &bbs[first].statements; - for s in first_stmts.iter() { - match &s.kind { - StatementKind::Assign(box (_, rhs)) => { - if let Rvalue::Use(Operand::Constant(_)) = rhs { - } else { - continue 'outer; - } - } - _ => continue 'outer, - } + let scnd_stmts = &bbs[second].statements; + if first_stmts.len() != scnd_stmts.len() { + continue; } - for target in rest.iter() { - for s in bbs[*target].statements.iter() { - if let StatementKind::Assign(box (ref lhs, rhs)) = &s.kind { - if let Rvalue::Use(Operand::Constant(_)) = rhs { - let has_matching_assn = first_stmts - .iter() - .find(|s| { - if let StatementKind::Assign(box (lhs_f, _)) = &s.kind { - lhs_f == lhs - } else { - false - } - }) - .is_some(); - if has_matching_assn { - continue; + for (f, s) in first_stmts.iter().zip(scnd_stmts.iter()) { + match (&f.kind, &s.kind) { + // If two statements are exactly the same just ignore them. + (f_s, s_s) if f_s == s_s => (), + + ( + 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 => { + if let Some(f_c) = f_c.literal.try_eval_bool(tcx, param_env) { + // This should also be a bool because it's writing to the same place + let s_c = s_c.literal.try_eval_bool(tcx, param_env).unwrap(); + // Check that only const assignments of opposite bool values are + // permitted. + if f_c != s_c { + continue } } + continue 'outer; } - - continue 'outer; + // If there are not exclusively assignments, then ignore this + _ => continue 'outer, } } - let (first_block, to_add) = bbs.pick2_mut(first, bb_idx); - let new_stmts = first_block.statements.iter().cloned().map(|mut s| { + // Take owenership of items now that we know we can optimize. + let discr = discr.clone(); + + bbs[bb_idx].terminator_mut().kind = TerminatorKind::Goto { target: first }; + for s in bbs[first].statements.iter_mut() { if let StatementKind::Assign(box (_, ref mut rhs)) = s.kind { let size = tcx.layout_of(param_env.and(switch_ty)).unwrap().size; let const_cmp = Operand::const_from_scalar( @@ -86,17 +78,8 @@ impl<'tcx> MirPass<'tcx> for MatchBranchSimplification { rustc_span::DUMMY_SP, ); *rhs = Rvalue::BinaryOp(BinOp::Eq, Operand::Move(discr), const_cmp); - } else { - unreachable!() } - s - }); - to_add.statements.extend(new_stmts); - to_add.terminator_mut().kind = first_dest; - did_remove_blocks = true; - } - if did_remove_blocks { - simplify::remove_dead_blocks(body); + } } } } diff --git a/src/librustc_mir/transform/mod.rs b/src/librustc_mir/transform/mod.rs index b84514d1e9452..4f26f3bb45973 100644 --- a/src/librustc_mir/transform/mod.rs +++ b/src/librustc_mir/transform/mod.rs @@ -441,6 +441,7 @@ fn run_optimization_passes<'tcx>( // with async primitives. &generator::StateTransform, &instcombine::InstCombine, + &match_branches::MatchBranchSimplification, &const_prop::ConstProp, &simplify_branches::SimplifyBranches::new("after-const-prop"), &simplify_try::SimplifyArmIdentity, @@ -452,7 +453,6 @@ fn run_optimization_passes<'tcx>( &simplify::SimplifyCfg::new("final"), &nrvo::RenameReturnPlace, &simplify::SimplifyLocals, - &match_branches::MatchBranchSimplification, ]; let no_optimizations: &[&dyn MirPass<'tcx>] = &[ From f51422b47473b49e5a7f69e64d59f6739d5e175a Mon Sep 17 00:00:00 2001 From: kadmin Date: Wed, 12 Aug 2020 08:28:14 +0000 Subject: [PATCH 4/6] Update to pick Eq or Ne --- src/librustc_mir/transform/match_branches.rs | 28 +++++++++++--------- 1 file changed, 15 insertions(+), 13 deletions(-) diff --git a/src/librustc_mir/transform/match_branches.rs b/src/librustc_mir/transform/match_branches.rs index 5fab46f029ff0..07dec23429bd6 100644 --- a/src/librustc_mir/transform/match_branches.rs +++ b/src/librustc_mir/transform/match_branches.rs @@ -52,11 +52,8 @@ impl<'tcx> MirPass<'tcx> for MatchBranchSimplification { if let Some(f_c) = f_c.literal.try_eval_bool(tcx, param_env) { // This should also be a bool because it's writing to the same place let s_c = s_c.literal.try_eval_bool(tcx, param_env).unwrap(); - // Check that only const assignments of opposite bool values are - // permitted. - if f_c != s_c { - continue - } + assert_ne!(f_c, s_c, "Unexpected match would've compared eq earlier"); + continue; } continue 'outer; } @@ -70,14 +67,19 @@ impl<'tcx> MirPass<'tcx> for MatchBranchSimplification { bbs[bb_idx].terminator_mut().kind = TerminatorKind::Goto { target: first }; for s in bbs[first].statements.iter_mut() { if let StatementKind::Assign(box (_, ref mut rhs)) = s.kind { - let size = tcx.layout_of(param_env.and(switch_ty)).unwrap().size; - let const_cmp = Operand::const_from_scalar( - tcx, - switch_ty, - crate::interpret::Scalar::from_uint(val, size), - rustc_span::DUMMY_SP, - ); - *rhs = Rvalue::BinaryOp(BinOp::Eq, Operand::Move(discr), const_cmp); + if let Rvalue::Use(Operand::Constant(c)) = rhs { + let size = tcx.layout_of(param_env.and(switch_ty)).unwrap().size; + let const_cmp = Operand::const_from_scalar( + tcx, + switch_ty, + crate::interpret::Scalar::from_uint(val, size), + rustc_span::DUMMY_SP, + ); + if let Some(c) = c.literal.try_eval_bool(tcx, param_env) { + let op = if c { BinOp::Eq } else { BinOp::Ne }; + *rhs = Rvalue::BinaryOp(op, Operand::Move(discr), const_cmp); + } + } } } } From 9ef5b884b248f5d4a9d0bf8594077e6698f1172c Mon Sep 17 00:00:00 2001 From: kadmin Date: Wed, 12 Aug 2020 21:35:32 +0000 Subject: [PATCH 5/6] bless diff Just output the current bless'd MIR diff The tests are still fairly broken rn --- src/librustc_mir/transform/match_branches.rs | 16 +++-- ...ranches.foo.MatchBranchSimplification.diff | 64 +++++++++++++++++++ 2 files changed, 75 insertions(+), 5 deletions(-) create mode 100644 src/test/mir-opt/matches_reduce_branches.foo.MatchBranchSimplification.diff diff --git a/src/librustc_mir/transform/match_branches.rs b/src/librustc_mir/transform/match_branches.rs index 07dec23429bd6..74da6d5e629b3 100644 --- a/src/librustc_mir/transform/match_branches.rs +++ b/src/librustc_mir/transform/match_branches.rs @@ -52,8 +52,11 @@ impl<'tcx> MirPass<'tcx> for MatchBranchSimplification { if let Some(f_c) = f_c.literal.try_eval_bool(tcx, param_env) { // This should also be a bool because it's writing to the same place let s_c = s_c.literal.try_eval_bool(tcx, param_env).unwrap(); - assert_ne!(f_c, s_c, "Unexpected match would've compared eq earlier"); - continue; + if f_c != s_c { + // have to check this here because f_c & s_c might have + // different spans. + continue; + } } continue 'outer; } @@ -63,9 +66,9 @@ impl<'tcx> MirPass<'tcx> for MatchBranchSimplification { } // Take owenership of items now that we know we can optimize. let discr = discr.clone(); + let (from, first) = bbs.pick2_mut(bb_idx, first); - bbs[bb_idx].terminator_mut().kind = TerminatorKind::Goto { target: first }; - for s in bbs[first].statements.iter_mut() { + let new_stmts = first.statements.iter().cloned().map(|mut s| { if let StatementKind::Assign(box (_, ref mut rhs)) = s.kind { if let Rvalue::Use(Operand::Constant(c)) = rhs { let size = tcx.layout_of(param_env.and(switch_ty)).unwrap().size; @@ -81,7 +84,10 @@ impl<'tcx> MirPass<'tcx> for MatchBranchSimplification { } } } - } + s + }); + from.statements.extend(new_stmts); + from.terminator_mut().kind = first.terminator().kind.clone(); } } } diff --git a/src/test/mir-opt/matches_reduce_branches.foo.MatchBranchSimplification.diff b/src/test/mir-opt/matches_reduce_branches.foo.MatchBranchSimplification.diff new file mode 100644 index 0000000000000..b0a861f6c5ea2 --- /dev/null +++ b/src/test/mir-opt/matches_reduce_branches.foo.MatchBranchSimplification.diff @@ -0,0 +1,64 @@ +- // MIR for `foo` before MatchBranchSimplification ++ // MIR for `foo` after MatchBranchSimplification + + fn foo(_1: std::option::Option<()>) -> () { + debug bar => _1; // in scope 0 at $DIR/matches_reduce_branches.rs:3:8: 3:11 + let mut _0: (); // return place in scope 0 at $DIR/matches_reduce_branches.rs:3:25: 3:25 + let mut _2: bool; // in scope 0 at $SRC_DIR/core/src/macros/mod.rs:LL:COL + let mut _3: isize; // in scope 0 at $DIR/matches_reduce_branches.rs:4:22: 4:26 + + bb0: { + StorageLive(_2); // scope 0 at $SRC_DIR/core/src/macros/mod.rs:LL:COL + _3 = discriminant(_1); // scope 0 at $DIR/matches_reduce_branches.rs:4:22: 4:26 +- switchInt(move _3) -> [0_isize: bb2, otherwise: bb1]; // scope 0 at $DIR/matches_reduce_branches.rs:4:22: 4:26 ++ goto -> bb2; // scope 0 at $DIR/matches_reduce_branches.rs:4:22: 4:26 + } + + bb1: { + _2 = const false; // scope 0 at $SRC_DIR/core/src/macros/mod.rs:LL:COL + // ty::Const + // + ty: bool + // + val: Value(Scalar(0x00)) + // mir::Constant + // + span: $SRC_DIR/core/src/macros/mod.rs:LL:COL + // + literal: Const { ty: bool, val: Value(Scalar(0x00)) } + goto -> bb3; // scope 0 at $SRC_DIR/core/src/macros/mod.rs:LL:COL + } + + bb2: { +- _2 = const true; // scope 0 at $SRC_DIR/core/src/macros/mod.rs:LL:COL ++ _2 = Eq(move _3, const 0_isize); // scope 0 at $SRC_DIR/core/src/macros/mod.rs:LL:COL + // ty::Const +- // + ty: bool +- // + val: Value(Scalar(0x01)) ++ // + ty: isize ++ // + val: Value(Scalar(0x0000000000000000)) + // mir::Constant +- // + span: $SRC_DIR/core/src/macros/mod.rs:LL:COL +- // + literal: Const { ty: bool, val: Value(Scalar(0x01)) } ++ // + span: $DIR/matches_reduce_branches.rs:1:1: 1:1 ++ // + literal: Const { ty: isize, val: Value(Scalar(0x0000000000000000)) } + goto -> bb3; // scope 0 at $SRC_DIR/core/src/macros/mod.rs:LL:COL + } + + bb3: { + switchInt(_2) -> [false: bb4, otherwise: bb5]; // scope 0 at $DIR/matches_reduce_branches.rs:4:5: 6:6 + } + + bb4: { + _0 = const (); // scope 0 at $DIR/matches_reduce_branches.rs:4:5: 6:6 + // ty::Const + // + ty: () + // + val: Value(Scalar()) + // mir::Constant + // + span: $DIR/matches_reduce_branches.rs:4:5: 6:6 + // + literal: Const { ty: (), val: Value(Scalar()) } + goto -> bb5; // scope 0 at $DIR/matches_reduce_branches.rs:4:5: 6:6 + } + + bb5: { + StorageDead(_2); // scope 0 at $DIR/matches_reduce_branches.rs:7:1: 7:2 + return; // scope 0 at $DIR/matches_reduce_branches.rs:7:2: 7:2 + } + } + From 46e5699af97301fc89dadfa5633d6db814df3cc6 Mon Sep 17 00:00:00 2001 From: kadmin Date: Thu, 13 Aug 2020 07:55:58 +0000 Subject: [PATCH 6/6] Add 64bit / 32bit files --- ...s.foo.MatchBranchSimplification.diff.32bit | 66 +++++++++++++++++++ ....foo.MatchBranchSimplification.diff.64bit} | 46 ++++++------- src/test/mir-opt/matches_reduce_branches.rs | 1 + 3 files changed, 91 insertions(+), 22 deletions(-) create mode 100644 src/test/mir-opt/matches_reduce_branches.foo.MatchBranchSimplification.diff.32bit rename src/test/mir-opt/{matches_reduce_branches.foo.MatchBranchSimplification.diff => matches_reduce_branches.foo.MatchBranchSimplification.diff.64bit} (75%) diff --git a/src/test/mir-opt/matches_reduce_branches.foo.MatchBranchSimplification.diff.32bit b/src/test/mir-opt/matches_reduce_branches.foo.MatchBranchSimplification.diff.32bit new file mode 100644 index 0000000000000..df94c897e92f8 --- /dev/null +++ b/src/test/mir-opt/matches_reduce_branches.foo.MatchBranchSimplification.diff.32bit @@ -0,0 +1,66 @@ +- // MIR for `foo` before MatchBranchSimplification ++ // MIR for `foo` after MatchBranchSimplification + + fn foo(_1: std::option::Option<()>) -> () { + debug bar => _1; // in scope 0 at $DIR/matches_reduce_branches.rs:4:8: 4:11 + let mut _0: (); // return place in scope 0 at $DIR/matches_reduce_branches.rs:4:25: 4:25 + let mut _2: bool; // in scope 0 at $SRC_DIR/core/src/macros/mod.rs:LL:COL + let mut _3: isize; // in scope 0 at $DIR/matches_reduce_branches.rs:5:22: 5:26 + + bb0: { + StorageLive(_2); // scope 0 at $SRC_DIR/core/src/macros/mod.rs:LL:COL + _3 = discriminant(_1); // scope 0 at $DIR/matches_reduce_branches.rs:5:22: 5:26 +- switchInt(move _3) -> [0_isize: bb2, otherwise: bb1]; // scope 0 at $DIR/matches_reduce_branches.rs:5:22: 5:26 ++ _2 = Eq(move _3, const 0_isize); // scope 0 at $SRC_DIR/core/src/macros/mod.rs:LL:COL ++ // ty::Const ++ // + ty: isize ++ // + val: Value(Scalar(0x00000000)) ++ // mir::Constant ++ // + span: $DIR/matches_reduce_branches.rs:1:1: 1:1 ++ // + literal: Const { ty: isize, val: Value(Scalar(0x00000000)) } ++ goto -> bb3; // scope 0 at $DIR/matches_reduce_branches.rs:5:22: 5:26 + } + + bb1: { + _2 = const false; // scope 0 at $SRC_DIR/core/src/macros/mod.rs:LL:COL + // ty::Const + // + ty: bool + // + val: Value(Scalar(0x00)) + // mir::Constant + // + span: $SRC_DIR/core/src/macros/mod.rs:LL:COL + // + literal: Const { ty: bool, val: Value(Scalar(0x00)) } + goto -> bb3; // scope 0 at $SRC_DIR/core/src/macros/mod.rs:LL:COL + } + + bb2: { + _2 = const true; // scope 0 at $SRC_DIR/core/src/macros/mod.rs:LL:COL + // ty::Const + // + ty: bool + // + val: Value(Scalar(0x01)) + // mir::Constant + // + span: $SRC_DIR/core/src/macros/mod.rs:LL:COL + // + literal: Const { ty: bool, val: Value(Scalar(0x01)) } + goto -> bb3; // scope 0 at $SRC_DIR/core/src/macros/mod.rs:LL:COL + } + + bb3: { + switchInt(_2) -> [false: bb4, otherwise: bb5]; // scope 0 at $DIR/matches_reduce_branches.rs:5:5: 7:6 + } + + bb4: { + _0 = const (); // scope 0 at $DIR/matches_reduce_branches.rs:5:5: 7:6 + // ty::Const + // + ty: () + // + val: Value(Scalar()) + // mir::Constant + // + span: $DIR/matches_reduce_branches.rs:5:5: 7:6 + // + literal: Const { ty: (), val: Value(Scalar()) } + goto -> bb5; // scope 0 at $DIR/matches_reduce_branches.rs:5:5: 7:6 + } + + bb5: { + StorageDead(_2); // scope 0 at $DIR/matches_reduce_branches.rs:8:1: 8:2 + return; // scope 0 at $DIR/matches_reduce_branches.rs:8:2: 8:2 + } + } + diff --git a/src/test/mir-opt/matches_reduce_branches.foo.MatchBranchSimplification.diff b/src/test/mir-opt/matches_reduce_branches.foo.MatchBranchSimplification.diff.64bit similarity index 75% rename from src/test/mir-opt/matches_reduce_branches.foo.MatchBranchSimplification.diff rename to src/test/mir-opt/matches_reduce_branches.foo.MatchBranchSimplification.diff.64bit index b0a861f6c5ea2..06849b4a5d983 100644 --- a/src/test/mir-opt/matches_reduce_branches.foo.MatchBranchSimplification.diff +++ b/src/test/mir-opt/matches_reduce_branches.foo.MatchBranchSimplification.diff.64bit @@ -2,16 +2,23 @@ + // MIR for `foo` after MatchBranchSimplification fn foo(_1: std::option::Option<()>) -> () { - debug bar => _1; // in scope 0 at $DIR/matches_reduce_branches.rs:3:8: 3:11 - let mut _0: (); // return place in scope 0 at $DIR/matches_reduce_branches.rs:3:25: 3:25 + debug bar => _1; // in scope 0 at $DIR/matches_reduce_branches.rs:4:8: 4:11 + let mut _0: (); // return place in scope 0 at $DIR/matches_reduce_branches.rs:4:25: 4:25 let mut _2: bool; // in scope 0 at $SRC_DIR/core/src/macros/mod.rs:LL:COL - let mut _3: isize; // in scope 0 at $DIR/matches_reduce_branches.rs:4:22: 4:26 + let mut _3: isize; // in scope 0 at $DIR/matches_reduce_branches.rs:5:22: 5:26 bb0: { StorageLive(_2); // scope 0 at $SRC_DIR/core/src/macros/mod.rs:LL:COL - _3 = discriminant(_1); // scope 0 at $DIR/matches_reduce_branches.rs:4:22: 4:26 -- switchInt(move _3) -> [0_isize: bb2, otherwise: bb1]; // scope 0 at $DIR/matches_reduce_branches.rs:4:22: 4:26 -+ goto -> bb2; // scope 0 at $DIR/matches_reduce_branches.rs:4:22: 4:26 + _3 = discriminant(_1); // scope 0 at $DIR/matches_reduce_branches.rs:5:22: 5:26 +- switchInt(move _3) -> [0_isize: bb2, otherwise: bb1]; // scope 0 at $DIR/matches_reduce_branches.rs:5:22: 5:26 ++ _2 = Eq(move _3, const 0_isize); // scope 0 at $SRC_DIR/core/src/macros/mod.rs:LL:COL ++ // ty::Const ++ // + ty: isize ++ // + val: Value(Scalar(0x0000000000000000)) ++ // mir::Constant ++ // + span: $DIR/matches_reduce_branches.rs:1:1: 1:1 ++ // + literal: Const { ty: isize, val: Value(Scalar(0x0000000000000000)) } ++ goto -> bb3; // scope 0 at $DIR/matches_reduce_branches.rs:5:22: 5:26 } bb1: { @@ -26,39 +33,34 @@ } bb2: { -- _2 = const true; // scope 0 at $SRC_DIR/core/src/macros/mod.rs:LL:COL -+ _2 = Eq(move _3, const 0_isize); // scope 0 at $SRC_DIR/core/src/macros/mod.rs:LL:COL + _2 = const true; // scope 0 at $SRC_DIR/core/src/macros/mod.rs:LL:COL // ty::Const -- // + ty: bool -- // + val: Value(Scalar(0x01)) -+ // + ty: isize -+ // + val: Value(Scalar(0x0000000000000000)) + // + ty: bool + // + val: Value(Scalar(0x01)) // mir::Constant -- // + span: $SRC_DIR/core/src/macros/mod.rs:LL:COL -- // + literal: Const { ty: bool, val: Value(Scalar(0x01)) } -+ // + span: $DIR/matches_reduce_branches.rs:1:1: 1:1 -+ // + literal: Const { ty: isize, val: Value(Scalar(0x0000000000000000)) } + // + span: $SRC_DIR/core/src/macros/mod.rs:LL:COL + // + literal: Const { ty: bool, val: Value(Scalar(0x01)) } goto -> bb3; // scope 0 at $SRC_DIR/core/src/macros/mod.rs:LL:COL } bb3: { - switchInt(_2) -> [false: bb4, otherwise: bb5]; // scope 0 at $DIR/matches_reduce_branches.rs:4:5: 6:6 + switchInt(_2) -> [false: bb4, otherwise: bb5]; // scope 0 at $DIR/matches_reduce_branches.rs:5:5: 7:6 } bb4: { - _0 = const (); // scope 0 at $DIR/matches_reduce_branches.rs:4:5: 6:6 + _0 = const (); // scope 0 at $DIR/matches_reduce_branches.rs:5:5: 7:6 // ty::Const // + ty: () // + val: Value(Scalar()) // mir::Constant - // + span: $DIR/matches_reduce_branches.rs:4:5: 6:6 + // + span: $DIR/matches_reduce_branches.rs:5:5: 7:6 // + literal: Const { ty: (), val: Value(Scalar()) } - goto -> bb5; // scope 0 at $DIR/matches_reduce_branches.rs:4:5: 6:6 + goto -> bb5; // scope 0 at $DIR/matches_reduce_branches.rs:5:5: 7:6 } bb5: { - StorageDead(_2); // scope 0 at $DIR/matches_reduce_branches.rs:7:1: 7:2 - return; // scope 0 at $DIR/matches_reduce_branches.rs:7:2: 7:2 + StorageDead(_2); // scope 0 at $DIR/matches_reduce_branches.rs:8:1: 8:2 + return; // scope 0 at $DIR/matches_reduce_branches.rs:8:2: 8:2 } } diff --git a/src/test/mir-opt/matches_reduce_branches.rs b/src/test/mir-opt/matches_reduce_branches.rs index 2d1a6d8de21a6..91b6bfc836bd4 100644 --- a/src/test/mir-opt/matches_reduce_branches.rs +++ b/src/test/mir-opt/matches_reduce_branches.rs @@ -1,3 +1,4 @@ +// EMIT_MIR_FOR_EACH_BIT_WIDTH // EMIT_MIR matches_reduce_branches.foo.MatchBranchSimplification.diff fn foo(bar: Option<()>) {