From d28405972ff8e83ac5e1be53c72eb44a43f983a8 Mon Sep 17 00:00:00 2001 From: Camille GILLOT Date: Sat, 16 Sep 2023 17:13:30 +0000 Subject: [PATCH 01/28] Do not remove unused definitions inside GVN. --- compiler/rustc_mir_transform/src/gvn.rs | 12 - compiler/rustc_mir_transform/src/lib.rs | 1 + compiler/rustc_mir_transform/src/simplify.rs | 2 + .../const_debuginfo.main.ConstDebugInfo.diff | 75 ++- .../gvn.arithmetic.GVN.panic-abort.diff | 107 ++-- .../gvn.arithmetic.GVN.panic-unwind.diff | 107 ++-- ...vn.arithmetic_checked.GVN.panic-abort.diff | 109 ++-- ...n.arithmetic_checked.GVN.panic-unwind.diff | 109 ++-- .../gvn.arithmetic_float.GVN.panic-abort.diff | 66 +-- ...gvn.arithmetic_float.GVN.panic-unwind.diff | 66 +-- tests/mir-opt/gvn.cast.GVN.panic-abort.diff | 156 +++--- tests/mir-opt/gvn.cast.GVN.panic-unwind.diff | 156 +++--- .../gvn.dereferences.GVN.panic-abort.diff | 14 +- .../gvn.dereferences.GVN.panic-unwind.diff | 14 +- ...gvn.multiple_branches.GVN.panic-abort.diff | 136 ++--- ...vn.multiple_branches.GVN.panic-unwind.diff | 136 ++--- .../gvn.repeated_index.GVN.panic-abort.diff | 7 +- .../gvn.repeated_index.GVN.panic-unwind.diff | 7 +- tests/mir-opt/gvn.slices.GVN.panic-abort.diff | 41 +- .../mir-opt/gvn.slices.GVN.panic-unwind.diff | 41 +- ...xpression_elimination.GVN.panic-abort.diff | 493 ++++++++++-------- ...pression_elimination.GVN.panic-unwind.diff | 493 ++++++++++-------- .../gvn.wrap_unwrap.GVN.panic-abort.diff | 8 +- .../gvn.wrap_unwrap.GVN.panic-unwind.diff | 8 +- ...implifyComparisonIntegral.panic-abort.diff | 47 +- ...mplifyComparisonIntegral.panic-unwind.diff | 47 +- ...ondition-after-const-prop.panic-abort.diff | 8 +- ...ndition-after-const-prop.panic-unwind.diff | 8 +- 28 files changed, 1348 insertions(+), 1126 deletions(-) diff --git a/compiler/rustc_mir_transform/src/gvn.rs b/compiler/rustc_mir_transform/src/gvn.rs index eece7c3e83412..e7f36e5881a58 100644 --- a/compiler/rustc_mir_transform/src/gvn.rs +++ b/compiler/rustc_mir_transform/src/gvn.rs @@ -118,16 +118,11 @@ fn propagate_ssa<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) { let data = &mut body.basic_blocks.as_mut_preserves_cfg()[bb]; state.visit_basic_block_data(bb, data); } - let any_replacement = state.any_replacement; // For each local that is reused (`y` above), we remove its storage statements do avoid any // difficulty. Those locals are SSA, so should be easy to optimize by LLVM without storage // statements. StorageRemover { tcx, reused_locals: state.reused_locals }.visit_body_preserves_cfg(body); - - if any_replacement { - crate::simplify::remove_unused_definitions(body); - } } newtype_index! { @@ -190,7 +185,6 @@ struct VnState<'body, 'tcx> { ssa: &'body SsaLocals, dominators: &'body Dominators, reused_locals: BitSet, - any_replacement: bool, } impl<'body, 'tcx> VnState<'body, 'tcx> { @@ -212,7 +206,6 @@ impl<'body, 'tcx> VnState<'body, 'tcx> { ssa, dominators, reused_locals: BitSet::new_empty(local_decls.len()), - any_replacement: false, } } @@ -324,14 +317,12 @@ impl<'body, 'tcx> VnState<'body, 'tcx> { { *place = local.into(); self.reused_locals.insert(local); - self.any_replacement = true; } else if place_ref.local != place.local || place_ref.projection.len() < place.projection.len() { // By the invariant on `place_ref`. *place = place_ref.project_deeper(&[], self.tcx); self.reused_locals.insert(place_ref.local); - self.any_replacement = true; } Some(value) @@ -349,7 +340,6 @@ impl<'body, 'tcx> VnState<'body, 'tcx> { let value = self.simplify_place_value(place, location)?; if let Some(const_) = self.try_as_constant(value) { *operand = Operand::Constant(Box::new(const_)); - self.any_replacement = true; } Some(value) } @@ -502,13 +492,11 @@ impl<'tcx> MutVisitor<'tcx> for VnState<'_, 'tcx> { { if let Some(const_) = self.try_as_constant(value) { *rvalue = Rvalue::Use(Operand::Constant(Box::new(const_))); - self.any_replacement = true; } else if let Some(local) = self.try_as_local(value, location) && *rvalue != Rvalue::Use(Operand::Move(local.into())) { *rvalue = Rvalue::Use(Operand::Copy(local.into())); self.reused_locals.insert(local); - self.any_replacement = true; } } } diff --git a/compiler/rustc_mir_transform/src/lib.rs b/compiler/rustc_mir_transform/src/lib.rs index 9aaa54110bdfe..4ec91a55f1d2c 100644 --- a/compiler/rustc_mir_transform/src/lib.rs +++ b/compiler/rustc_mir_transform/src/lib.rs @@ -570,6 +570,7 @@ fn run_optimization_passes<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) { &separate_const_switch::SeparateConstSwitch, &const_prop::ConstProp, &gvn::GVN, + &simplify::SimplifyLocals::AfterGVN, &dataflow_const_prop::DataflowConstProp, &const_debuginfo::ConstDebugInfo, &o1(simplify_branches::SimplifyConstCondition::AfterConstProp), diff --git a/compiler/rustc_mir_transform/src/simplify.rs b/compiler/rustc_mir_transform/src/simplify.rs index 88c89e106fd56..0a1c011147ae0 100644 --- a/compiler/rustc_mir_transform/src/simplify.rs +++ b/compiler/rustc_mir_transform/src/simplify.rs @@ -366,6 +366,7 @@ pub fn remove_dead_blocks(body: &mut Body<'_>) { pub enum SimplifyLocals { BeforeConstProp, + AfterGVN, Final, } @@ -373,6 +374,7 @@ impl<'tcx> MirPass<'tcx> for SimplifyLocals { fn name(&self) -> &'static str { match &self { SimplifyLocals::BeforeConstProp => "SimplifyLocals-before-const-prop", + SimplifyLocals::AfterGVN => "SimplifyLocals-after-value-numbering", SimplifyLocals::Final => "SimplifyLocals-final", } } diff --git a/tests/mir-opt/const_debuginfo.main.ConstDebugInfo.diff b/tests/mir-opt/const_debuginfo.main.ConstDebugInfo.diff index 313e5dddbbba4..f5d822520a70a 100644 --- a/tests/mir-opt/const_debuginfo.main.ConstDebugInfo.diff +++ b/tests/mir-opt/const_debuginfo.main.ConstDebugInfo.diff @@ -4,12 +4,6 @@ fn main() -> () { let mut _0: (); let _1: u8; - let mut _5: u8; - let mut _6: u8; - let mut _7: u8; - let mut _8: u8; - let mut _12: u32; - let mut _13: u32; scope 1 { - debug x => _1; + debug x => const 1_u8; @@ -25,34 +19,34 @@ scope 4 { - debug sum => _4; + debug sum => const 6_u8; - let _9: &str; + let _5: &str; scope 5 { -- debug s => _9; +- debug s => _5; + debug s => const "hello, world!"; - let _14: bool; - let _15: bool; - let _16: u32; + let _8: bool; + let _9: bool; + let _10: u32; scope 6 { -- debug ((f: (bool, bool, u32)).0: bool) => _14; -- debug ((f: (bool, bool, u32)).1: bool) => _15; -- debug ((f: (bool, bool, u32)).2: u32) => _16; +- debug ((f: (bool, bool, u32)).0: bool) => _8; +- debug ((f: (bool, bool, u32)).1: bool) => _9; +- debug ((f: (bool, bool, u32)).2: u32) => _10; + debug ((f: (bool, bool, u32)).0: bool) => const true; + debug ((f: (bool, bool, u32)).1: bool) => const false; + debug ((f: (bool, bool, u32)).2: u32) => const 123_u32; - let _10: std::option::Option; + let _6: std::option::Option; scope 7 { -- debug o => _10; +- debug o => _6; + debug o => const Option::::Some(99_u16); - let _17: u32; - let _18: u32; + let _11: u32; + let _12: u32; scope 8 { -- debug ((p: Point).0: u32) => _17; -- debug ((p: Point).1: u32) => _18; +- debug ((p: Point).0: u32) => _11; +- debug ((p: Point).1: u32) => _12; + debug ((p: Point).0: u32) => const 32_u32; + debug ((p: Point).1: u32) => const 32_u32; - let _11: u32; + let _7: u32; scope 9 { -- debug a => _11; +- debug a => _7; + debug a => const 64_u32; } } @@ -69,30 +63,27 @@ _2 = const 2_u8; _3 = const 3_u8; StorageLive(_4); - StorageLive(_5); - _5 = const 3_u8; _4 = const 6_u8; - StorageDead(_5); + StorageLive(_5); + _5 = const "hello, world!"; + StorageLive(_8); StorageLive(_9); - _9 = const "hello, world!"; - StorageLive(_14); - StorageLive(_15); - StorageLive(_16); - _14 = const true; - _15 = const false; - _16 = const 123_u32; StorageLive(_10); - _10 = const Option::::Some(99_u16); - _17 = const 32_u32; - _18 = const 32_u32; - StorageLive(_11); - _11 = const 64_u32; - StorageDead(_11); - StorageDead(_10); - StorageDead(_14); - StorageDead(_15); - StorageDead(_16); + _8 = const true; + _9 = const false; + _10 = const 123_u32; + StorageLive(_6); + _6 = const Option::::Some(99_u16); + _11 = const 32_u32; + _12 = const 32_u32; + StorageLive(_7); + _7 = const 64_u32; + StorageDead(_7); + StorageDead(_6); + StorageDead(_8); StorageDead(_9); + StorageDead(_10); + StorageDead(_5); StorageDead(_4); return; } diff --git a/tests/mir-opt/gvn.arithmetic.GVN.panic-abort.diff b/tests/mir-opt/gvn.arithmetic.GVN.panic-abort.diff index 3f5173c189e12..17ba0d3cef2d4 100644 --- a/tests/mir-opt/gvn.arithmetic.GVN.panic-abort.diff +++ b/tests/mir-opt/gvn.arithmetic.GVN.panic-abort.diff @@ -67,11 +67,11 @@ bb0: { StorageLive(_2); StorageLive(_3); -- StorageLive(_4); -- _4 = _1; + StorageLive(_4); + _4 = _1; - _3 = Add(move _4, const 0_u64); -- StorageDead(_4); + _3 = Add(_1, const 0_u64); + StorageDead(_4); _2 = opaque::(move _3) -> [return: bb1, unwind unreachable]; } @@ -80,11 +80,11 @@ StorageDead(_2); StorageLive(_5); StorageLive(_6); -- StorageLive(_7); -- _7 = _1; + StorageLive(_7); + _7 = _1; - _6 = Sub(move _7, const 0_u64); -- StorageDead(_7); + _6 = Sub(_1, const 0_u64); + StorageDead(_7); _5 = opaque::(move _6) -> [return: bb2, unwind unreachable]; } @@ -93,11 +93,11 @@ StorageDead(_5); StorageLive(_8); StorageLive(_9); -- StorageLive(_10); -- _10 = _1; + StorageLive(_10); + _10 = _1; - _9 = Mul(move _10, const 0_u64); -- StorageDead(_10); + _9 = Mul(_1, const 0_u64); + StorageDead(_10); _8 = opaque::(move _9) -> [return: bb3, unwind unreachable]; } @@ -106,11 +106,11 @@ StorageDead(_8); StorageLive(_11); StorageLive(_12); -- StorageLive(_13); -- _13 = _1; + StorageLive(_13); + _13 = _1; - _12 = Mul(move _13, const 1_u64); -- StorageDead(_13); + _12 = Mul(_1, const 1_u64); + StorageDead(_13); _11 = opaque::(move _12) -> [return: bb4, unwind unreachable]; } @@ -119,8 +119,8 @@ StorageDead(_11); StorageLive(_14); StorageLive(_15); -- StorageLive(_16); -- _16 = _1; + StorageLive(_16); + _16 = _1; _17 = Eq(const 0_u64, const 0_u64); - assert(!move _17, "attempt to divide `{}` by zero", _16) -> [success: bb5, unwind unreachable]; + assert(!_17, "attempt to divide `{}` by zero", _1) -> [success: bb5, unwind unreachable]; @@ -128,8 +128,8 @@ bb5: { - _15 = Div(move _16, const 0_u64); -- StorageDead(_16); + _15 = Div(_1, const 0_u64); + StorageDead(_16); _14 = opaque::(move _15) -> [return: bb6, unwind unreachable]; } @@ -138,8 +138,8 @@ StorageDead(_14); StorageLive(_18); StorageLive(_19); -- StorageLive(_20); -- _20 = _1; + StorageLive(_20); + _20 = _1; _21 = Eq(const 1_u64, const 0_u64); - assert(!move _21, "attempt to divide `{}` by zero", _20) -> [success: bb7, unwind unreachable]; + assert(!_21, "attempt to divide `{}` by zero", _1) -> [success: bb7, unwind unreachable]; @@ -147,8 +147,8 @@ bb7: { - _19 = Div(move _20, const 1_u64); -- StorageDead(_20); + _19 = Div(_1, const 1_u64); + StorageDead(_20); _18 = opaque::(move _19) -> [return: bb8, unwind unreachable]; } @@ -157,8 +157,8 @@ StorageDead(_18); StorageLive(_22); StorageLive(_23); -- StorageLive(_24); -- _24 = _1; + StorageLive(_24); + _24 = _1; - _25 = Eq(_24, const 0_u64); - assert(!move _25, "attempt to divide `{}` by zero", const 0_u64) -> [success: bb9, unwind unreachable]; + _25 = Eq(_1, const 0_u64); @@ -167,8 +167,8 @@ bb9: { - _23 = Div(const 0_u64, move _24); -- StorageDead(_24); + _23 = Div(const 0_u64, _1); + StorageDead(_24); _22 = opaque::(move _23) -> [return: bb10, unwind unreachable]; } @@ -177,17 +177,18 @@ StorageDead(_22); StorageLive(_26); StorageLive(_27); -- StorageLive(_28); -- _28 = _1; + StorageLive(_28); + _28 = _1; - _29 = Eq(_28, const 0_u64); - assert(!move _29, "attempt to divide `{}` by zero", const 1_u64) -> [success: bb11, unwind unreachable]; ++ _29 = _25; + assert(!_25, "attempt to divide `{}` by zero", const 1_u64) -> [success: bb11, unwind unreachable]; } bb11: { - _27 = Div(const 1_u64, move _28); -- StorageDead(_28); + _27 = Div(const 1_u64, _1); + StorageDead(_28); _26 = opaque::(move _27) -> [return: bb12, unwind unreachable]; } @@ -196,17 +197,18 @@ StorageDead(_26); StorageLive(_30); StorageLive(_31); -- StorageLive(_32); -- _32 = _1; + StorageLive(_32); + _32 = _1; - _33 = Eq(const 0_u64, const 0_u64); - assert(!move _33, "attempt to calculate the remainder of `{}` with a divisor of zero", _32) -> [success: bb13, unwind unreachable]; ++ _33 = _17; + assert(!_17, "attempt to calculate the remainder of `{}` with a divisor of zero", _1) -> [success: bb13, unwind unreachable]; } bb13: { - _31 = Rem(move _32, const 0_u64); -- StorageDead(_32); + _31 = Rem(_1, const 0_u64); + StorageDead(_32); _30 = opaque::(move _31) -> [return: bb14, unwind unreachable]; } @@ -215,17 +217,18 @@ StorageDead(_30); StorageLive(_34); StorageLive(_35); -- StorageLive(_36); -- _36 = _1; + StorageLive(_36); + _36 = _1; - _37 = Eq(const 1_u64, const 0_u64); - assert(!move _37, "attempt to calculate the remainder of `{}` with a divisor of zero", _36) -> [success: bb15, unwind unreachable]; ++ _37 = _21; + assert(!_21, "attempt to calculate the remainder of `{}` with a divisor of zero", _1) -> [success: bb15, unwind unreachable]; } bb15: { - _35 = Rem(move _36, const 1_u64); -- StorageDead(_36); + _35 = Rem(_1, const 1_u64); + StorageDead(_36); _34 = opaque::(move _35) -> [return: bb16, unwind unreachable]; } @@ -234,17 +237,18 @@ StorageDead(_34); StorageLive(_38); StorageLive(_39); -- StorageLive(_40); -- _40 = _1; + StorageLive(_40); + _40 = _1; - _41 = Eq(_40, const 0_u64); - assert(!move _41, "attempt to calculate the remainder of `{}` with a divisor of zero", const 0_u64) -> [success: bb17, unwind unreachable]; ++ _41 = _25; + assert(!_25, "attempt to calculate the remainder of `{}` with a divisor of zero", const 0_u64) -> [success: bb17, unwind unreachable]; } bb17: { - _39 = Rem(const 0_u64, move _40); -- StorageDead(_40); + _39 = Rem(const 0_u64, _1); + StorageDead(_40); _38 = opaque::(move _39) -> [return: bb18, unwind unreachable]; } @@ -253,17 +257,18 @@ StorageDead(_38); StorageLive(_42); StorageLive(_43); -- StorageLive(_44); -- _44 = _1; + StorageLive(_44); + _44 = _1; - _45 = Eq(_44, const 0_u64); - assert(!move _45, "attempt to calculate the remainder of `{}` with a divisor of zero", const 1_u64) -> [success: bb19, unwind unreachable]; ++ _45 = _25; + assert(!_25, "attempt to calculate the remainder of `{}` with a divisor of zero", const 1_u64) -> [success: bb19, unwind unreachable]; } bb19: { - _43 = Rem(const 1_u64, move _44); -- StorageDead(_44); + _43 = Rem(const 1_u64, _1); + StorageDead(_44); _42 = opaque::(move _43) -> [return: bb20, unwind unreachable]; } @@ -272,11 +277,11 @@ StorageDead(_42); StorageLive(_46); StorageLive(_47); -- StorageLive(_48); -- _48 = _1; + StorageLive(_48); + _48 = _1; - _47 = BitAnd(move _48, const 0_u64); -- StorageDead(_48); + _47 = BitAnd(_1, const 0_u64); + StorageDead(_48); _46 = opaque::(move _47) -> [return: bb21, unwind unreachable]; } @@ -285,11 +290,11 @@ StorageDead(_46); StorageLive(_49); StorageLive(_50); -- StorageLive(_51); -- _51 = _1; + StorageLive(_51); + _51 = _1; - _50 = BitOr(move _51, const 0_u64); -- StorageDead(_51); + _50 = BitOr(_1, const 0_u64); + StorageDead(_51); _49 = opaque::(move _50) -> [return: bb22, unwind unreachable]; } @@ -298,11 +303,11 @@ StorageDead(_49); StorageLive(_52); StorageLive(_53); -- StorageLive(_54); -- _54 = _1; + StorageLive(_54); + _54 = _1; - _53 = BitXor(move _54, const 0_u64); -- StorageDead(_54); + _53 = BitXor(_1, const 0_u64); + StorageDead(_54); _52 = opaque::(move _53) -> [return: bb23, unwind unreachable]; } @@ -311,11 +316,11 @@ StorageDead(_52); StorageLive(_55); StorageLive(_56); -- StorageLive(_57); -- _57 = _1; + StorageLive(_57); + _57 = _1; - _56 = Shr(move _57, const 0_i32); -- StorageDead(_57); + _56 = Shr(_1, const 0_i32); + StorageDead(_57); _55 = opaque::(move _56) -> [return: bb24, unwind unreachable]; } @@ -324,11 +329,11 @@ StorageDead(_55); StorageLive(_58); StorageLive(_59); -- StorageLive(_60); -- _60 = _1; + StorageLive(_60); + _60 = _1; - _59 = Shl(move _60, const 0_i32); -- StorageDead(_60); + _59 = Shl(_1, const 0_i32); + StorageDead(_60); _58 = opaque::(move _59) -> [return: bb25, unwind unreachable]; } diff --git a/tests/mir-opt/gvn.arithmetic.GVN.panic-unwind.diff b/tests/mir-opt/gvn.arithmetic.GVN.panic-unwind.diff index 38da21d91d41d..f14fd409bea1c 100644 --- a/tests/mir-opt/gvn.arithmetic.GVN.panic-unwind.diff +++ b/tests/mir-opt/gvn.arithmetic.GVN.panic-unwind.diff @@ -67,11 +67,11 @@ bb0: { StorageLive(_2); StorageLive(_3); -- StorageLive(_4); -- _4 = _1; + StorageLive(_4); + _4 = _1; - _3 = Add(move _4, const 0_u64); -- StorageDead(_4); + _3 = Add(_1, const 0_u64); + StorageDead(_4); _2 = opaque::(move _3) -> [return: bb1, unwind continue]; } @@ -80,11 +80,11 @@ StorageDead(_2); StorageLive(_5); StorageLive(_6); -- StorageLive(_7); -- _7 = _1; + StorageLive(_7); + _7 = _1; - _6 = Sub(move _7, const 0_u64); -- StorageDead(_7); + _6 = Sub(_1, const 0_u64); + StorageDead(_7); _5 = opaque::(move _6) -> [return: bb2, unwind continue]; } @@ -93,11 +93,11 @@ StorageDead(_5); StorageLive(_8); StorageLive(_9); -- StorageLive(_10); -- _10 = _1; + StorageLive(_10); + _10 = _1; - _9 = Mul(move _10, const 0_u64); -- StorageDead(_10); + _9 = Mul(_1, const 0_u64); + StorageDead(_10); _8 = opaque::(move _9) -> [return: bb3, unwind continue]; } @@ -106,11 +106,11 @@ StorageDead(_8); StorageLive(_11); StorageLive(_12); -- StorageLive(_13); -- _13 = _1; + StorageLive(_13); + _13 = _1; - _12 = Mul(move _13, const 1_u64); -- StorageDead(_13); + _12 = Mul(_1, const 1_u64); + StorageDead(_13); _11 = opaque::(move _12) -> [return: bb4, unwind continue]; } @@ -119,8 +119,8 @@ StorageDead(_11); StorageLive(_14); StorageLive(_15); -- StorageLive(_16); -- _16 = _1; + StorageLive(_16); + _16 = _1; _17 = Eq(const 0_u64, const 0_u64); - assert(!move _17, "attempt to divide `{}` by zero", _16) -> [success: bb5, unwind continue]; + assert(!_17, "attempt to divide `{}` by zero", _1) -> [success: bb5, unwind continue]; @@ -128,8 +128,8 @@ bb5: { - _15 = Div(move _16, const 0_u64); -- StorageDead(_16); + _15 = Div(_1, const 0_u64); + StorageDead(_16); _14 = opaque::(move _15) -> [return: bb6, unwind continue]; } @@ -138,8 +138,8 @@ StorageDead(_14); StorageLive(_18); StorageLive(_19); -- StorageLive(_20); -- _20 = _1; + StorageLive(_20); + _20 = _1; _21 = Eq(const 1_u64, const 0_u64); - assert(!move _21, "attempt to divide `{}` by zero", _20) -> [success: bb7, unwind continue]; + assert(!_21, "attempt to divide `{}` by zero", _1) -> [success: bb7, unwind continue]; @@ -147,8 +147,8 @@ bb7: { - _19 = Div(move _20, const 1_u64); -- StorageDead(_20); + _19 = Div(_1, const 1_u64); + StorageDead(_20); _18 = opaque::(move _19) -> [return: bb8, unwind continue]; } @@ -157,8 +157,8 @@ StorageDead(_18); StorageLive(_22); StorageLive(_23); -- StorageLive(_24); -- _24 = _1; + StorageLive(_24); + _24 = _1; - _25 = Eq(_24, const 0_u64); - assert(!move _25, "attempt to divide `{}` by zero", const 0_u64) -> [success: bb9, unwind continue]; + _25 = Eq(_1, const 0_u64); @@ -167,8 +167,8 @@ bb9: { - _23 = Div(const 0_u64, move _24); -- StorageDead(_24); + _23 = Div(const 0_u64, _1); + StorageDead(_24); _22 = opaque::(move _23) -> [return: bb10, unwind continue]; } @@ -177,17 +177,18 @@ StorageDead(_22); StorageLive(_26); StorageLive(_27); -- StorageLive(_28); -- _28 = _1; + StorageLive(_28); + _28 = _1; - _29 = Eq(_28, const 0_u64); - assert(!move _29, "attempt to divide `{}` by zero", const 1_u64) -> [success: bb11, unwind continue]; ++ _29 = _25; + assert(!_25, "attempt to divide `{}` by zero", const 1_u64) -> [success: bb11, unwind continue]; } bb11: { - _27 = Div(const 1_u64, move _28); -- StorageDead(_28); + _27 = Div(const 1_u64, _1); + StorageDead(_28); _26 = opaque::(move _27) -> [return: bb12, unwind continue]; } @@ -196,17 +197,18 @@ StorageDead(_26); StorageLive(_30); StorageLive(_31); -- StorageLive(_32); -- _32 = _1; + StorageLive(_32); + _32 = _1; - _33 = Eq(const 0_u64, const 0_u64); - assert(!move _33, "attempt to calculate the remainder of `{}` with a divisor of zero", _32) -> [success: bb13, unwind continue]; ++ _33 = _17; + assert(!_17, "attempt to calculate the remainder of `{}` with a divisor of zero", _1) -> [success: bb13, unwind continue]; } bb13: { - _31 = Rem(move _32, const 0_u64); -- StorageDead(_32); + _31 = Rem(_1, const 0_u64); + StorageDead(_32); _30 = opaque::(move _31) -> [return: bb14, unwind continue]; } @@ -215,17 +217,18 @@ StorageDead(_30); StorageLive(_34); StorageLive(_35); -- StorageLive(_36); -- _36 = _1; + StorageLive(_36); + _36 = _1; - _37 = Eq(const 1_u64, const 0_u64); - assert(!move _37, "attempt to calculate the remainder of `{}` with a divisor of zero", _36) -> [success: bb15, unwind continue]; ++ _37 = _21; + assert(!_21, "attempt to calculate the remainder of `{}` with a divisor of zero", _1) -> [success: bb15, unwind continue]; } bb15: { - _35 = Rem(move _36, const 1_u64); -- StorageDead(_36); + _35 = Rem(_1, const 1_u64); + StorageDead(_36); _34 = opaque::(move _35) -> [return: bb16, unwind continue]; } @@ -234,17 +237,18 @@ StorageDead(_34); StorageLive(_38); StorageLive(_39); -- StorageLive(_40); -- _40 = _1; + StorageLive(_40); + _40 = _1; - _41 = Eq(_40, const 0_u64); - assert(!move _41, "attempt to calculate the remainder of `{}` with a divisor of zero", const 0_u64) -> [success: bb17, unwind continue]; ++ _41 = _25; + assert(!_25, "attempt to calculate the remainder of `{}` with a divisor of zero", const 0_u64) -> [success: bb17, unwind continue]; } bb17: { - _39 = Rem(const 0_u64, move _40); -- StorageDead(_40); + _39 = Rem(const 0_u64, _1); + StorageDead(_40); _38 = opaque::(move _39) -> [return: bb18, unwind continue]; } @@ -253,17 +257,18 @@ StorageDead(_38); StorageLive(_42); StorageLive(_43); -- StorageLive(_44); -- _44 = _1; + StorageLive(_44); + _44 = _1; - _45 = Eq(_44, const 0_u64); - assert(!move _45, "attempt to calculate the remainder of `{}` with a divisor of zero", const 1_u64) -> [success: bb19, unwind continue]; ++ _45 = _25; + assert(!_25, "attempt to calculate the remainder of `{}` with a divisor of zero", const 1_u64) -> [success: bb19, unwind continue]; } bb19: { - _43 = Rem(const 1_u64, move _44); -- StorageDead(_44); + _43 = Rem(const 1_u64, _1); + StorageDead(_44); _42 = opaque::(move _43) -> [return: bb20, unwind continue]; } @@ -272,11 +277,11 @@ StorageDead(_42); StorageLive(_46); StorageLive(_47); -- StorageLive(_48); -- _48 = _1; + StorageLive(_48); + _48 = _1; - _47 = BitAnd(move _48, const 0_u64); -- StorageDead(_48); + _47 = BitAnd(_1, const 0_u64); + StorageDead(_48); _46 = opaque::(move _47) -> [return: bb21, unwind continue]; } @@ -285,11 +290,11 @@ StorageDead(_46); StorageLive(_49); StorageLive(_50); -- StorageLive(_51); -- _51 = _1; + StorageLive(_51); + _51 = _1; - _50 = BitOr(move _51, const 0_u64); -- StorageDead(_51); + _50 = BitOr(_1, const 0_u64); + StorageDead(_51); _49 = opaque::(move _50) -> [return: bb22, unwind continue]; } @@ -298,11 +303,11 @@ StorageDead(_49); StorageLive(_52); StorageLive(_53); -- StorageLive(_54); -- _54 = _1; + StorageLive(_54); + _54 = _1; - _53 = BitXor(move _54, const 0_u64); -- StorageDead(_54); + _53 = BitXor(_1, const 0_u64); + StorageDead(_54); _52 = opaque::(move _53) -> [return: bb23, unwind continue]; } @@ -311,11 +316,11 @@ StorageDead(_52); StorageLive(_55); StorageLive(_56); -- StorageLive(_57); -- _57 = _1; + StorageLive(_57); + _57 = _1; - _56 = Shr(move _57, const 0_i32); -- StorageDead(_57); + _56 = Shr(_1, const 0_i32); + StorageDead(_57); _55 = opaque::(move _56) -> [return: bb24, unwind continue]; } @@ -324,11 +329,11 @@ StorageDead(_55); StorageLive(_58); StorageLive(_59); -- StorageLive(_60); -- _60 = _1; + StorageLive(_60); + _60 = _1; - _59 = Shl(move _60, const 0_i32); -- StorageDead(_60); + _59 = Shl(_1, const 0_i32); + StorageDead(_60); _58 = opaque::(move _59) -> [return: bb25, unwind continue]; } diff --git a/tests/mir-opt/gvn.arithmetic_checked.GVN.panic-abort.diff b/tests/mir-opt/gvn.arithmetic_checked.GVN.panic-abort.diff index 0c342799e0794..e586e4ac8898c 100644 --- a/tests/mir-opt/gvn.arithmetic_checked.GVN.panic-abort.diff +++ b/tests/mir-opt/gvn.arithmetic_checked.GVN.panic-abort.diff @@ -75,8 +75,8 @@ bb0: { StorageLive(_2); StorageLive(_3); -- StorageLive(_4); -- _4 = _1; + StorageLive(_4); + _4 = _1; - _5 = CheckedAdd(_4, const 0_u64); - assert(!move (_5.1: bool), "attempt to compute `{} + {}`, which would overflow", move _4, const 0_u64) -> [success: bb1, unwind unreachable]; + _5 = CheckedAdd(_1, const 0_u64); @@ -85,7 +85,7 @@ bb1: { _3 = move (_5.0: u64); -- StorageDead(_4); + StorageDead(_4); _2 = opaque::(move _3) -> [return: bb2, unwind unreachable]; } @@ -94,8 +94,8 @@ StorageDead(_2); StorageLive(_6); StorageLive(_7); -- StorageLive(_8); -- _8 = _1; + StorageLive(_8); + _8 = _1; - _9 = CheckedSub(_8, const 0_u64); - assert(!move (_9.1: bool), "attempt to compute `{} - {}`, which would overflow", move _8, const 0_u64) -> [success: bb3, unwind unreachable]; + _9 = CheckedSub(_1, const 0_u64); @@ -104,7 +104,7 @@ bb3: { _7 = move (_9.0: u64); -- StorageDead(_8); + StorageDead(_8); _6 = opaque::(move _7) -> [return: bb4, unwind unreachable]; } @@ -113,8 +113,8 @@ StorageDead(_6); StorageLive(_10); StorageLive(_11); -- StorageLive(_12); -- _12 = _1; + StorageLive(_12); + _12 = _1; - _13 = CheckedMul(_12, const 0_u64); - assert(!move (_13.1: bool), "attempt to compute `{} * {}`, which would overflow", move _12, const 0_u64) -> [success: bb5, unwind unreachable]; + _13 = CheckedMul(_1, const 0_u64); @@ -123,7 +123,7 @@ bb5: { _11 = move (_13.0: u64); -- StorageDead(_12); + StorageDead(_12); _10 = opaque::(move _11) -> [return: bb6, unwind unreachable]; } @@ -132,8 +132,8 @@ StorageDead(_10); StorageLive(_14); StorageLive(_15); -- StorageLive(_16); -- _16 = _1; + StorageLive(_16); + _16 = _1; - _17 = CheckedMul(_16, const 1_u64); - assert(!move (_17.1: bool), "attempt to compute `{} * {}`, which would overflow", move _16, const 1_u64) -> [success: bb7, unwind unreachable]; + _17 = CheckedMul(_1, const 1_u64); @@ -142,7 +142,7 @@ bb7: { _15 = move (_17.0: u64); -- StorageDead(_16); + StorageDead(_16); _14 = opaque::(move _15) -> [return: bb8, unwind unreachable]; } @@ -151,8 +151,8 @@ StorageDead(_14); StorageLive(_18); StorageLive(_19); -- StorageLive(_20); -- _20 = _1; + StorageLive(_20); + _20 = _1; _21 = Eq(const 0_u64, const 0_u64); - assert(!move _21, "attempt to divide `{}` by zero", _20) -> [success: bb9, unwind unreachable]; + assert(!_21, "attempt to divide `{}` by zero", _1) -> [success: bb9, unwind unreachable]; @@ -160,8 +160,8 @@ bb9: { - _19 = Div(move _20, const 0_u64); -- StorageDead(_20); + _19 = Div(_1, const 0_u64); + StorageDead(_20); _18 = opaque::(move _19) -> [return: bb10, unwind unreachable]; } @@ -170,8 +170,8 @@ StorageDead(_18); StorageLive(_22); StorageLive(_23); -- StorageLive(_24); -- _24 = _1; + StorageLive(_24); + _24 = _1; _25 = Eq(const 1_u64, const 0_u64); - assert(!move _25, "attempt to divide `{}` by zero", _24) -> [success: bb11, unwind unreachable]; + assert(!_25, "attempt to divide `{}` by zero", _1) -> [success: bb11, unwind unreachable]; @@ -179,8 +179,8 @@ bb11: { - _23 = Div(move _24, const 1_u64); -- StorageDead(_24); + _23 = Div(_1, const 1_u64); + StorageDead(_24); _22 = opaque::(move _23) -> [return: bb12, unwind unreachable]; } @@ -189,8 +189,8 @@ StorageDead(_22); StorageLive(_26); StorageLive(_27); -- StorageLive(_28); -- _28 = _1; + StorageLive(_28); + _28 = _1; - _29 = Eq(_28, const 0_u64); - assert(!move _29, "attempt to divide `{}` by zero", const 0_u64) -> [success: bb13, unwind unreachable]; + _29 = Eq(_1, const 0_u64); @@ -199,8 +199,8 @@ bb13: { - _27 = Div(const 0_u64, move _28); -- StorageDead(_28); + _27 = Div(const 0_u64, _1); + StorageDead(_28); _26 = opaque::(move _27) -> [return: bb14, unwind unreachable]; } @@ -209,17 +209,18 @@ StorageDead(_26); StorageLive(_30); StorageLive(_31); -- StorageLive(_32); -- _32 = _1; + StorageLive(_32); + _32 = _1; - _33 = Eq(_32, const 0_u64); - assert(!move _33, "attempt to divide `{}` by zero", const 1_u64) -> [success: bb15, unwind unreachable]; ++ _33 = _29; + assert(!_29, "attempt to divide `{}` by zero", const 1_u64) -> [success: bb15, unwind unreachable]; } bb15: { - _31 = Div(const 1_u64, move _32); -- StorageDead(_32); + _31 = Div(const 1_u64, _1); + StorageDead(_32); _30 = opaque::(move _31) -> [return: bb16, unwind unreachable]; } @@ -228,17 +229,18 @@ StorageDead(_30); StorageLive(_34); StorageLive(_35); -- StorageLive(_36); -- _36 = _1; + StorageLive(_36); + _36 = _1; - _37 = Eq(const 0_u64, const 0_u64); - assert(!move _37, "attempt to calculate the remainder of `{}` with a divisor of zero", _36) -> [success: bb17, unwind unreachable]; ++ _37 = _21; + assert(!_21, "attempt to calculate the remainder of `{}` with a divisor of zero", _1) -> [success: bb17, unwind unreachable]; } bb17: { - _35 = Rem(move _36, const 0_u64); -- StorageDead(_36); + _35 = Rem(_1, const 0_u64); + StorageDead(_36); _34 = opaque::(move _35) -> [return: bb18, unwind unreachable]; } @@ -247,17 +249,18 @@ StorageDead(_34); StorageLive(_38); StorageLive(_39); -- StorageLive(_40); -- _40 = _1; + StorageLive(_40); + _40 = _1; - _41 = Eq(const 1_u64, const 0_u64); - assert(!move _41, "attempt to calculate the remainder of `{}` with a divisor of zero", _40) -> [success: bb19, unwind unreachable]; ++ _41 = _25; + assert(!_25, "attempt to calculate the remainder of `{}` with a divisor of zero", _1) -> [success: bb19, unwind unreachable]; } bb19: { - _39 = Rem(move _40, const 1_u64); -- StorageDead(_40); + _39 = Rem(_1, const 1_u64); + StorageDead(_40); _38 = opaque::(move _39) -> [return: bb20, unwind unreachable]; } @@ -266,17 +269,18 @@ StorageDead(_38); StorageLive(_42); StorageLive(_43); -- StorageLive(_44); -- _44 = _1; + StorageLive(_44); + _44 = _1; - _45 = Eq(_44, const 0_u64); - assert(!move _45, "attempt to calculate the remainder of `{}` with a divisor of zero", const 0_u64) -> [success: bb21, unwind unreachable]; ++ _45 = _29; + assert(!_29, "attempt to calculate the remainder of `{}` with a divisor of zero", const 0_u64) -> [success: bb21, unwind unreachable]; } bb21: { - _43 = Rem(const 0_u64, move _44); -- StorageDead(_44); + _43 = Rem(const 0_u64, _1); + StorageDead(_44); _42 = opaque::(move _43) -> [return: bb22, unwind unreachable]; } @@ -285,17 +289,18 @@ StorageDead(_42); StorageLive(_46); StorageLive(_47); -- StorageLive(_48); -- _48 = _1; + StorageLive(_48); + _48 = _1; - _49 = Eq(_48, const 0_u64); - assert(!move _49, "attempt to calculate the remainder of `{}` with a divisor of zero", const 1_u64) -> [success: bb23, unwind unreachable]; ++ _49 = _29; + assert(!_29, "attempt to calculate the remainder of `{}` with a divisor of zero", const 1_u64) -> [success: bb23, unwind unreachable]; } bb23: { - _47 = Rem(const 1_u64, move _48); -- StorageDead(_48); + _47 = Rem(const 1_u64, _1); + StorageDead(_48); _46 = opaque::(move _47) -> [return: bb24, unwind unreachable]; } @@ -304,11 +309,11 @@ StorageDead(_46); StorageLive(_50); StorageLive(_51); -- StorageLive(_52); -- _52 = _1; + StorageLive(_52); + _52 = _1; - _51 = BitAnd(move _52, const 0_u64); -- StorageDead(_52); + _51 = BitAnd(_1, const 0_u64); + StorageDead(_52); _50 = opaque::(move _51) -> [return: bb25, unwind unreachable]; } @@ -317,11 +322,11 @@ StorageDead(_50); StorageLive(_53); StorageLive(_54); -- StorageLive(_55); -- _55 = _1; + StorageLive(_55); + _55 = _1; - _54 = BitOr(move _55, const 0_u64); -- StorageDead(_55); + _54 = BitOr(_1, const 0_u64); + StorageDead(_55); _53 = opaque::(move _54) -> [return: bb26, unwind unreachable]; } @@ -330,11 +335,11 @@ StorageDead(_53); StorageLive(_56); StorageLive(_57); -- StorageLive(_58); -- _58 = _1; + StorageLive(_58); + _58 = _1; - _57 = BitXor(move _58, const 0_u64); -- StorageDead(_58); + _57 = BitXor(_1, const 0_u64); + StorageDead(_58); _56 = opaque::(move _57) -> [return: bb27, unwind unreachable]; } @@ -343,8 +348,8 @@ StorageDead(_56); StorageLive(_59); StorageLive(_60); -- StorageLive(_61); -- _61 = _1; + StorageLive(_61); + _61 = _1; _62 = const 0_i32 as u32 (IntToInt); - _63 = Lt(move _62, const 64_u32); - assert(move _63, "attempt to shift right by `{}`, which would overflow", const 0_i32) -> [success: bb28, unwind unreachable]; @@ -354,8 +359,8 @@ bb28: { - _60 = Shr(move _61, const 0_i32); -- StorageDead(_61); + _60 = Shr(_1, const 0_i32); + StorageDead(_61); _59 = opaque::(move _60) -> [return: bb29, unwind unreachable]; } @@ -364,18 +369,20 @@ StorageDead(_59); StorageLive(_64); StorageLive(_65); -- StorageLive(_66); -- _66 = _1; + StorageLive(_66); + _66 = _1; - _67 = const 0_i32 as u32 (IntToInt); - _68 = Lt(move _67, const 64_u32); - assert(move _68, "attempt to shift left by `{}`, which would overflow", const 0_i32) -> [success: bb30, unwind unreachable]; ++ _67 = _62; ++ _68 = _63; + assert(_63, "attempt to shift left by `{}`, which would overflow", const 0_i32) -> [success: bb30, unwind unreachable]; } bb30: { - _65 = Shl(move _66, const 0_i32); -- StorageDead(_66); + _65 = Shl(_1, const 0_i32); + StorageDead(_66); _64 = opaque::(move _65) -> [return: bb31, unwind unreachable]; } diff --git a/tests/mir-opt/gvn.arithmetic_checked.GVN.panic-unwind.diff b/tests/mir-opt/gvn.arithmetic_checked.GVN.panic-unwind.diff index 7813c29b962d2..f58a9116b8fe7 100644 --- a/tests/mir-opt/gvn.arithmetic_checked.GVN.panic-unwind.diff +++ b/tests/mir-opt/gvn.arithmetic_checked.GVN.panic-unwind.diff @@ -75,8 +75,8 @@ bb0: { StorageLive(_2); StorageLive(_3); -- StorageLive(_4); -- _4 = _1; + StorageLive(_4); + _4 = _1; - _5 = CheckedAdd(_4, const 0_u64); - assert(!move (_5.1: bool), "attempt to compute `{} + {}`, which would overflow", move _4, const 0_u64) -> [success: bb1, unwind continue]; + _5 = CheckedAdd(_1, const 0_u64); @@ -85,7 +85,7 @@ bb1: { _3 = move (_5.0: u64); -- StorageDead(_4); + StorageDead(_4); _2 = opaque::(move _3) -> [return: bb2, unwind continue]; } @@ -94,8 +94,8 @@ StorageDead(_2); StorageLive(_6); StorageLive(_7); -- StorageLive(_8); -- _8 = _1; + StorageLive(_8); + _8 = _1; - _9 = CheckedSub(_8, const 0_u64); - assert(!move (_9.1: bool), "attempt to compute `{} - {}`, which would overflow", move _8, const 0_u64) -> [success: bb3, unwind continue]; + _9 = CheckedSub(_1, const 0_u64); @@ -104,7 +104,7 @@ bb3: { _7 = move (_9.0: u64); -- StorageDead(_8); + StorageDead(_8); _6 = opaque::(move _7) -> [return: bb4, unwind continue]; } @@ -113,8 +113,8 @@ StorageDead(_6); StorageLive(_10); StorageLive(_11); -- StorageLive(_12); -- _12 = _1; + StorageLive(_12); + _12 = _1; - _13 = CheckedMul(_12, const 0_u64); - assert(!move (_13.1: bool), "attempt to compute `{} * {}`, which would overflow", move _12, const 0_u64) -> [success: bb5, unwind continue]; + _13 = CheckedMul(_1, const 0_u64); @@ -123,7 +123,7 @@ bb5: { _11 = move (_13.0: u64); -- StorageDead(_12); + StorageDead(_12); _10 = opaque::(move _11) -> [return: bb6, unwind continue]; } @@ -132,8 +132,8 @@ StorageDead(_10); StorageLive(_14); StorageLive(_15); -- StorageLive(_16); -- _16 = _1; + StorageLive(_16); + _16 = _1; - _17 = CheckedMul(_16, const 1_u64); - assert(!move (_17.1: bool), "attempt to compute `{} * {}`, which would overflow", move _16, const 1_u64) -> [success: bb7, unwind continue]; + _17 = CheckedMul(_1, const 1_u64); @@ -142,7 +142,7 @@ bb7: { _15 = move (_17.0: u64); -- StorageDead(_16); + StorageDead(_16); _14 = opaque::(move _15) -> [return: bb8, unwind continue]; } @@ -151,8 +151,8 @@ StorageDead(_14); StorageLive(_18); StorageLive(_19); -- StorageLive(_20); -- _20 = _1; + StorageLive(_20); + _20 = _1; _21 = Eq(const 0_u64, const 0_u64); - assert(!move _21, "attempt to divide `{}` by zero", _20) -> [success: bb9, unwind continue]; + assert(!_21, "attempt to divide `{}` by zero", _1) -> [success: bb9, unwind continue]; @@ -160,8 +160,8 @@ bb9: { - _19 = Div(move _20, const 0_u64); -- StorageDead(_20); + _19 = Div(_1, const 0_u64); + StorageDead(_20); _18 = opaque::(move _19) -> [return: bb10, unwind continue]; } @@ -170,8 +170,8 @@ StorageDead(_18); StorageLive(_22); StorageLive(_23); -- StorageLive(_24); -- _24 = _1; + StorageLive(_24); + _24 = _1; _25 = Eq(const 1_u64, const 0_u64); - assert(!move _25, "attempt to divide `{}` by zero", _24) -> [success: bb11, unwind continue]; + assert(!_25, "attempt to divide `{}` by zero", _1) -> [success: bb11, unwind continue]; @@ -179,8 +179,8 @@ bb11: { - _23 = Div(move _24, const 1_u64); -- StorageDead(_24); + _23 = Div(_1, const 1_u64); + StorageDead(_24); _22 = opaque::(move _23) -> [return: bb12, unwind continue]; } @@ -189,8 +189,8 @@ StorageDead(_22); StorageLive(_26); StorageLive(_27); -- StorageLive(_28); -- _28 = _1; + StorageLive(_28); + _28 = _1; - _29 = Eq(_28, const 0_u64); - assert(!move _29, "attempt to divide `{}` by zero", const 0_u64) -> [success: bb13, unwind continue]; + _29 = Eq(_1, const 0_u64); @@ -199,8 +199,8 @@ bb13: { - _27 = Div(const 0_u64, move _28); -- StorageDead(_28); + _27 = Div(const 0_u64, _1); + StorageDead(_28); _26 = opaque::(move _27) -> [return: bb14, unwind continue]; } @@ -209,17 +209,18 @@ StorageDead(_26); StorageLive(_30); StorageLive(_31); -- StorageLive(_32); -- _32 = _1; + StorageLive(_32); + _32 = _1; - _33 = Eq(_32, const 0_u64); - assert(!move _33, "attempt to divide `{}` by zero", const 1_u64) -> [success: bb15, unwind continue]; ++ _33 = _29; + assert(!_29, "attempt to divide `{}` by zero", const 1_u64) -> [success: bb15, unwind continue]; } bb15: { - _31 = Div(const 1_u64, move _32); -- StorageDead(_32); + _31 = Div(const 1_u64, _1); + StorageDead(_32); _30 = opaque::(move _31) -> [return: bb16, unwind continue]; } @@ -228,17 +229,18 @@ StorageDead(_30); StorageLive(_34); StorageLive(_35); -- StorageLive(_36); -- _36 = _1; + StorageLive(_36); + _36 = _1; - _37 = Eq(const 0_u64, const 0_u64); - assert(!move _37, "attempt to calculate the remainder of `{}` with a divisor of zero", _36) -> [success: bb17, unwind continue]; ++ _37 = _21; + assert(!_21, "attempt to calculate the remainder of `{}` with a divisor of zero", _1) -> [success: bb17, unwind continue]; } bb17: { - _35 = Rem(move _36, const 0_u64); -- StorageDead(_36); + _35 = Rem(_1, const 0_u64); + StorageDead(_36); _34 = opaque::(move _35) -> [return: bb18, unwind continue]; } @@ -247,17 +249,18 @@ StorageDead(_34); StorageLive(_38); StorageLive(_39); -- StorageLive(_40); -- _40 = _1; + StorageLive(_40); + _40 = _1; - _41 = Eq(const 1_u64, const 0_u64); - assert(!move _41, "attempt to calculate the remainder of `{}` with a divisor of zero", _40) -> [success: bb19, unwind continue]; ++ _41 = _25; + assert(!_25, "attempt to calculate the remainder of `{}` with a divisor of zero", _1) -> [success: bb19, unwind continue]; } bb19: { - _39 = Rem(move _40, const 1_u64); -- StorageDead(_40); + _39 = Rem(_1, const 1_u64); + StorageDead(_40); _38 = opaque::(move _39) -> [return: bb20, unwind continue]; } @@ -266,17 +269,18 @@ StorageDead(_38); StorageLive(_42); StorageLive(_43); -- StorageLive(_44); -- _44 = _1; + StorageLive(_44); + _44 = _1; - _45 = Eq(_44, const 0_u64); - assert(!move _45, "attempt to calculate the remainder of `{}` with a divisor of zero", const 0_u64) -> [success: bb21, unwind continue]; ++ _45 = _29; + assert(!_29, "attempt to calculate the remainder of `{}` with a divisor of zero", const 0_u64) -> [success: bb21, unwind continue]; } bb21: { - _43 = Rem(const 0_u64, move _44); -- StorageDead(_44); + _43 = Rem(const 0_u64, _1); + StorageDead(_44); _42 = opaque::(move _43) -> [return: bb22, unwind continue]; } @@ -285,17 +289,18 @@ StorageDead(_42); StorageLive(_46); StorageLive(_47); -- StorageLive(_48); -- _48 = _1; + StorageLive(_48); + _48 = _1; - _49 = Eq(_48, const 0_u64); - assert(!move _49, "attempt to calculate the remainder of `{}` with a divisor of zero", const 1_u64) -> [success: bb23, unwind continue]; ++ _49 = _29; + assert(!_29, "attempt to calculate the remainder of `{}` with a divisor of zero", const 1_u64) -> [success: bb23, unwind continue]; } bb23: { - _47 = Rem(const 1_u64, move _48); -- StorageDead(_48); + _47 = Rem(const 1_u64, _1); + StorageDead(_48); _46 = opaque::(move _47) -> [return: bb24, unwind continue]; } @@ -304,11 +309,11 @@ StorageDead(_46); StorageLive(_50); StorageLive(_51); -- StorageLive(_52); -- _52 = _1; + StorageLive(_52); + _52 = _1; - _51 = BitAnd(move _52, const 0_u64); -- StorageDead(_52); + _51 = BitAnd(_1, const 0_u64); + StorageDead(_52); _50 = opaque::(move _51) -> [return: bb25, unwind continue]; } @@ -317,11 +322,11 @@ StorageDead(_50); StorageLive(_53); StorageLive(_54); -- StorageLive(_55); -- _55 = _1; + StorageLive(_55); + _55 = _1; - _54 = BitOr(move _55, const 0_u64); -- StorageDead(_55); + _54 = BitOr(_1, const 0_u64); + StorageDead(_55); _53 = opaque::(move _54) -> [return: bb26, unwind continue]; } @@ -330,11 +335,11 @@ StorageDead(_53); StorageLive(_56); StorageLive(_57); -- StorageLive(_58); -- _58 = _1; + StorageLive(_58); + _58 = _1; - _57 = BitXor(move _58, const 0_u64); -- StorageDead(_58); + _57 = BitXor(_1, const 0_u64); + StorageDead(_58); _56 = opaque::(move _57) -> [return: bb27, unwind continue]; } @@ -343,8 +348,8 @@ StorageDead(_56); StorageLive(_59); StorageLive(_60); -- StorageLive(_61); -- _61 = _1; + StorageLive(_61); + _61 = _1; _62 = const 0_i32 as u32 (IntToInt); - _63 = Lt(move _62, const 64_u32); - assert(move _63, "attempt to shift right by `{}`, which would overflow", const 0_i32) -> [success: bb28, unwind continue]; @@ -354,8 +359,8 @@ bb28: { - _60 = Shr(move _61, const 0_i32); -- StorageDead(_61); + _60 = Shr(_1, const 0_i32); + StorageDead(_61); _59 = opaque::(move _60) -> [return: bb29, unwind continue]; } @@ -364,18 +369,20 @@ StorageDead(_59); StorageLive(_64); StorageLive(_65); -- StorageLive(_66); -- _66 = _1; + StorageLive(_66); + _66 = _1; - _67 = const 0_i32 as u32 (IntToInt); - _68 = Lt(move _67, const 64_u32); - assert(move _68, "attempt to shift left by `{}`, which would overflow", const 0_i32) -> [success: bb30, unwind continue]; ++ _67 = _62; ++ _68 = _63; + assert(_63, "attempt to shift left by `{}`, which would overflow", const 0_i32) -> [success: bb30, unwind continue]; } bb30: { - _65 = Shl(move _66, const 0_i32); -- StorageDead(_66); + _65 = Shl(_1, const 0_i32); + StorageDead(_66); _64 = opaque::(move _65) -> [return: bb31, unwind continue]; } diff --git a/tests/mir-opt/gvn.arithmetic_float.GVN.panic-abort.diff b/tests/mir-opt/gvn.arithmetic_float.GVN.panic-abort.diff index 7d5ac8353fe43..b332100eaf03c 100644 --- a/tests/mir-opt/gvn.arithmetic_float.GVN.panic-abort.diff +++ b/tests/mir-opt/gvn.arithmetic_float.GVN.panic-abort.diff @@ -37,11 +37,11 @@ bb0: { StorageLive(_2); StorageLive(_3); -- StorageLive(_4); -- _4 = _1; + StorageLive(_4); + _4 = _1; - _3 = Add(move _4, const 0f64); -- StorageDead(_4); + _3 = Add(_1, const 0f64); + StorageDead(_4); _2 = opaque::(move _3) -> [return: bb1, unwind unreachable]; } @@ -50,11 +50,11 @@ StorageDead(_2); StorageLive(_5); StorageLive(_6); -- StorageLive(_7); -- _7 = _1; + StorageLive(_7); + _7 = _1; - _6 = Sub(move _7, const 0f64); -- StorageDead(_7); + _6 = Sub(_1, const 0f64); + StorageDead(_7); _5 = opaque::(move _6) -> [return: bb2, unwind unreachable]; } @@ -63,11 +63,11 @@ StorageDead(_5); StorageLive(_8); StorageLive(_9); -- StorageLive(_10); -- _10 = _1; + StorageLive(_10); + _10 = _1; - _9 = Mul(move _10, const 0f64); -- StorageDead(_10); + _9 = Mul(_1, const 0f64); + StorageDead(_10); _8 = opaque::(move _9) -> [return: bb3, unwind unreachable]; } @@ -76,11 +76,11 @@ StorageDead(_8); StorageLive(_11); StorageLive(_12); -- StorageLive(_13); -- _13 = _1; + StorageLive(_13); + _13 = _1; - _12 = Div(move _13, const 0f64); -- StorageDead(_13); + _12 = Div(_1, const 0f64); + StorageDead(_13); _11 = opaque::(move _12) -> [return: bb4, unwind unreachable]; } @@ -89,11 +89,11 @@ StorageDead(_11); StorageLive(_14); StorageLive(_15); -- StorageLive(_16); -- _16 = _1; + StorageLive(_16); + _16 = _1; - _15 = Div(const 0f64, move _16); -- StorageDead(_16); + _15 = Div(const 0f64, _1); + StorageDead(_16); _14 = opaque::(move _15) -> [return: bb5, unwind unreachable]; } @@ -102,11 +102,11 @@ StorageDead(_14); StorageLive(_17); StorageLive(_18); -- StorageLive(_19); -- _19 = _1; + StorageLive(_19); + _19 = _1; - _18 = Rem(move _19, const 0f64); -- StorageDead(_19); + _18 = Rem(_1, const 0f64); + StorageDead(_19); _17 = opaque::(move _18) -> [return: bb6, unwind unreachable]; } @@ -115,11 +115,11 @@ StorageDead(_17); StorageLive(_20); StorageLive(_21); -- StorageLive(_22); -- _22 = _1; + StorageLive(_22); + _22 = _1; - _21 = Rem(const 0f64, move _22); -- StorageDead(_22); + _21 = Rem(const 0f64, _1); + StorageDead(_22); _20 = opaque::(move _21) -> [return: bb7, unwind unreachable]; } @@ -128,14 +128,14 @@ StorageDead(_20); StorageLive(_23); StorageLive(_24); -- StorageLive(_25); -- _25 = _1; -- StorageLive(_26); -- _26 = _1; + StorageLive(_25); + _25 = _1; + StorageLive(_26); + _26 = _1; - _24 = Eq(move _25, move _26); -- StorageDead(_26); -- StorageDead(_25); + _24 = Eq(_1, _1); + StorageDead(_26); + StorageDead(_25); _23 = opaque::(move _24) -> [return: bb8, unwind unreachable]; } @@ -144,14 +144,14 @@ StorageDead(_23); StorageLive(_27); StorageLive(_28); -- StorageLive(_29); -- _29 = _1; -- StorageLive(_30); -- _30 = _1; + StorageLive(_29); + _29 = _1; + StorageLive(_30); + _30 = _1; - _28 = Ne(move _29, move _30); -- StorageDead(_30); -- StorageDead(_29); + _28 = Ne(_1, _1); + StorageDead(_30); + StorageDead(_29); _27 = opaque::(move _28) -> [return: bb9, unwind unreachable]; } diff --git a/tests/mir-opt/gvn.arithmetic_float.GVN.panic-unwind.diff b/tests/mir-opt/gvn.arithmetic_float.GVN.panic-unwind.diff index 36c26dc66051b..28664cb0ac8ca 100644 --- a/tests/mir-opt/gvn.arithmetic_float.GVN.panic-unwind.diff +++ b/tests/mir-opt/gvn.arithmetic_float.GVN.panic-unwind.diff @@ -37,11 +37,11 @@ bb0: { StorageLive(_2); StorageLive(_3); -- StorageLive(_4); -- _4 = _1; + StorageLive(_4); + _4 = _1; - _3 = Add(move _4, const 0f64); -- StorageDead(_4); + _3 = Add(_1, const 0f64); + StorageDead(_4); _2 = opaque::(move _3) -> [return: bb1, unwind continue]; } @@ -50,11 +50,11 @@ StorageDead(_2); StorageLive(_5); StorageLive(_6); -- StorageLive(_7); -- _7 = _1; + StorageLive(_7); + _7 = _1; - _6 = Sub(move _7, const 0f64); -- StorageDead(_7); + _6 = Sub(_1, const 0f64); + StorageDead(_7); _5 = opaque::(move _6) -> [return: bb2, unwind continue]; } @@ -63,11 +63,11 @@ StorageDead(_5); StorageLive(_8); StorageLive(_9); -- StorageLive(_10); -- _10 = _1; + StorageLive(_10); + _10 = _1; - _9 = Mul(move _10, const 0f64); -- StorageDead(_10); + _9 = Mul(_1, const 0f64); + StorageDead(_10); _8 = opaque::(move _9) -> [return: bb3, unwind continue]; } @@ -76,11 +76,11 @@ StorageDead(_8); StorageLive(_11); StorageLive(_12); -- StorageLive(_13); -- _13 = _1; + StorageLive(_13); + _13 = _1; - _12 = Div(move _13, const 0f64); -- StorageDead(_13); + _12 = Div(_1, const 0f64); + StorageDead(_13); _11 = opaque::(move _12) -> [return: bb4, unwind continue]; } @@ -89,11 +89,11 @@ StorageDead(_11); StorageLive(_14); StorageLive(_15); -- StorageLive(_16); -- _16 = _1; + StorageLive(_16); + _16 = _1; - _15 = Div(const 0f64, move _16); -- StorageDead(_16); + _15 = Div(const 0f64, _1); + StorageDead(_16); _14 = opaque::(move _15) -> [return: bb5, unwind continue]; } @@ -102,11 +102,11 @@ StorageDead(_14); StorageLive(_17); StorageLive(_18); -- StorageLive(_19); -- _19 = _1; + StorageLive(_19); + _19 = _1; - _18 = Rem(move _19, const 0f64); -- StorageDead(_19); + _18 = Rem(_1, const 0f64); + StorageDead(_19); _17 = opaque::(move _18) -> [return: bb6, unwind continue]; } @@ -115,11 +115,11 @@ StorageDead(_17); StorageLive(_20); StorageLive(_21); -- StorageLive(_22); -- _22 = _1; + StorageLive(_22); + _22 = _1; - _21 = Rem(const 0f64, move _22); -- StorageDead(_22); + _21 = Rem(const 0f64, _1); + StorageDead(_22); _20 = opaque::(move _21) -> [return: bb7, unwind continue]; } @@ -128,14 +128,14 @@ StorageDead(_20); StorageLive(_23); StorageLive(_24); -- StorageLive(_25); -- _25 = _1; -- StorageLive(_26); -- _26 = _1; + StorageLive(_25); + _25 = _1; + StorageLive(_26); + _26 = _1; - _24 = Eq(move _25, move _26); -- StorageDead(_26); -- StorageDead(_25); + _24 = Eq(_1, _1); + StorageDead(_26); + StorageDead(_25); _23 = opaque::(move _24) -> [return: bb8, unwind continue]; } @@ -144,14 +144,14 @@ StorageDead(_23); StorageLive(_27); StorageLive(_28); -- StorageLive(_29); -- _29 = _1; -- StorageLive(_30); -- _30 = _1; + StorageLive(_29); + _29 = _1; + StorageLive(_30); + _30 = _1; - _28 = Ne(move _29, move _30); -- StorageDead(_30); -- StorageDead(_29); + _28 = Ne(_1, _1); + StorageDead(_30); + StorageDead(_29); _27 = opaque::(move _28) -> [return: bb9, unwind continue]; } diff --git a/tests/mir-opt/gvn.cast.GVN.panic-abort.diff b/tests/mir-opt/gvn.cast.GVN.panic-abort.diff index 513fe60b65d93..37b0d92931f68 100644 --- a/tests/mir-opt/gvn.cast.GVN.panic-abort.diff +++ b/tests/mir-opt/gvn.cast.GVN.panic-abort.diff @@ -105,18 +105,22 @@ bb0: { - StorageLive(_1); ++ nop; _1 = const 1_i64; - StorageLive(_2); ++ nop; _2 = const 1_u64; - StorageLive(_3); ++ nop; _3 = const 1f64; StorageLive(_4); StorageLive(_5); -- StorageLive(_6); + StorageLive(_6); - _6 = _1; - _5 = move _6 as u8 (IntToInt); -- StorageDead(_6); ++ _6 = const 1_i64; + _5 = const 1_i64 as u8 (IntToInt); + StorageDead(_6); _4 = opaque::(move _5) -> [return: bb1, unwind unreachable]; } @@ -125,11 +129,12 @@ StorageDead(_4); StorageLive(_7); StorageLive(_8); -- StorageLive(_9); + StorageLive(_9); - _9 = _1; - _8 = move _9 as u16 (IntToInt); -- StorageDead(_9); ++ _9 = const 1_i64; + _8 = const 1_i64 as u16 (IntToInt); + StorageDead(_9); _7 = opaque::(move _8) -> [return: bb2, unwind unreachable]; } @@ -138,11 +143,12 @@ StorageDead(_7); StorageLive(_10); StorageLive(_11); -- StorageLive(_12); + StorageLive(_12); - _12 = _1; - _11 = move _12 as u32 (IntToInt); -- StorageDead(_12); ++ _12 = const 1_i64; + _11 = const 1_i64 as u32 (IntToInt); + StorageDead(_12); _10 = opaque::(move _11) -> [return: bb3, unwind unreachable]; } @@ -151,11 +157,12 @@ StorageDead(_10); StorageLive(_13); StorageLive(_14); -- StorageLive(_15); + StorageLive(_15); - _15 = _1; - _14 = move _15 as u64 (IntToInt); -- StorageDead(_15); ++ _15 = const 1_i64; + _14 = const 1_i64 as u64 (IntToInt); + StorageDead(_15); _13 = opaque::(move _14) -> [return: bb4, unwind unreachable]; } @@ -164,11 +171,12 @@ StorageDead(_13); StorageLive(_16); StorageLive(_17); -- StorageLive(_18); + StorageLive(_18); - _18 = _1; - _17 = move _18 as i8 (IntToInt); -- StorageDead(_18); ++ _18 = const 1_i64; + _17 = const 1_i64 as i8 (IntToInt); + StorageDead(_18); _16 = opaque::(move _17) -> [return: bb5, unwind unreachable]; } @@ -177,11 +185,12 @@ StorageDead(_16); StorageLive(_19); StorageLive(_20); -- StorageLive(_21); + StorageLive(_21); - _21 = _1; - _20 = move _21 as i16 (IntToInt); -- StorageDead(_21); ++ _21 = const 1_i64; + _20 = const 1_i64 as i16 (IntToInt); + StorageDead(_21); _19 = opaque::(move _20) -> [return: bb6, unwind unreachable]; } @@ -190,11 +199,12 @@ StorageDead(_19); StorageLive(_22); StorageLive(_23); -- StorageLive(_24); + StorageLive(_24); - _24 = _1; - _23 = move _24 as i32 (IntToInt); -- StorageDead(_24); ++ _24 = const 1_i64; + _23 = const 1_i64 as i32 (IntToInt); + StorageDead(_24); _22 = opaque::(move _23) -> [return: bb7, unwind unreachable]; } @@ -202,22 +212,24 @@ StorageDead(_23); StorageDead(_22); StorageLive(_25); -- StorageLive(_26); + StorageLive(_26); - _26 = _1; - _25 = opaque::(move _26) -> [return: bb8, unwind unreachable]; ++ _26 = const 1_i64; + _25 = opaque::(const 1_i64) -> [return: bb8, unwind unreachable]; } bb8: { -- StorageDead(_26); + StorageDead(_26); StorageDead(_25); StorageLive(_27); StorageLive(_28); -- StorageLive(_29); + StorageLive(_29); - _29 = _1; - _28 = move _29 as f32 (IntToFloat); -- StorageDead(_29); ++ _29 = const 1_i64; + _28 = const 1_i64 as f32 (IntToFloat); + StorageDead(_29); _27 = opaque::(move _28) -> [return: bb9, unwind unreachable]; } @@ -226,11 +238,12 @@ StorageDead(_27); StorageLive(_30); StorageLive(_31); -- StorageLive(_32); + StorageLive(_32); - _32 = _1; - _31 = move _32 as f64 (IntToFloat); -- StorageDead(_32); ++ _32 = const 1_i64; + _31 = const 1_i64 as f64 (IntToFloat); + StorageDead(_32); _30 = opaque::(move _31) -> [return: bb10, unwind unreachable]; } @@ -239,11 +252,12 @@ StorageDead(_30); StorageLive(_33); StorageLive(_34); -- StorageLive(_35); + StorageLive(_35); - _35 = _2; - _34 = move _35 as u8 (IntToInt); -- StorageDead(_35); ++ _35 = const 1_u64; + _34 = const 1_u64 as u8 (IntToInt); + StorageDead(_35); _33 = opaque::(move _34) -> [return: bb11, unwind unreachable]; } @@ -252,11 +266,12 @@ StorageDead(_33); StorageLive(_36); StorageLive(_37); -- StorageLive(_38); + StorageLive(_38); - _38 = _2; - _37 = move _38 as u16 (IntToInt); -- StorageDead(_38); ++ _38 = const 1_u64; + _37 = const 1_u64 as u16 (IntToInt); + StorageDead(_38); _36 = opaque::(move _37) -> [return: bb12, unwind unreachable]; } @@ -265,11 +280,12 @@ StorageDead(_36); StorageLive(_39); StorageLive(_40); -- StorageLive(_41); + StorageLive(_41); - _41 = _2; - _40 = move _41 as u32 (IntToInt); -- StorageDead(_41); ++ _41 = const 1_u64; + _40 = const 1_u64 as u32 (IntToInt); + StorageDead(_41); _39 = opaque::(move _40) -> [return: bb13, unwind unreachable]; } @@ -277,22 +293,24 @@ StorageDead(_40); StorageDead(_39); StorageLive(_42); -- StorageLive(_43); + StorageLive(_43); - _43 = _2; - _42 = opaque::(move _43) -> [return: bb14, unwind unreachable]; ++ _43 = const 1_u64; + _42 = opaque::(const 1_u64) -> [return: bb14, unwind unreachable]; } bb14: { -- StorageDead(_43); + StorageDead(_43); StorageDead(_42); StorageLive(_44); StorageLive(_45); -- StorageLive(_46); + StorageLive(_46); - _46 = _2; - _45 = move _46 as i8 (IntToInt); -- StorageDead(_46); ++ _46 = const 1_u64; + _45 = const 1_u64 as i8 (IntToInt); + StorageDead(_46); _44 = opaque::(move _45) -> [return: bb15, unwind unreachable]; } @@ -301,11 +319,12 @@ StorageDead(_44); StorageLive(_47); StorageLive(_48); -- StorageLive(_49); + StorageLive(_49); - _49 = _2; - _48 = move _49 as i16 (IntToInt); -- StorageDead(_49); ++ _49 = const 1_u64; + _48 = const 1_u64 as i16 (IntToInt); + StorageDead(_49); _47 = opaque::(move _48) -> [return: bb16, unwind unreachable]; } @@ -314,11 +333,12 @@ StorageDead(_47); StorageLive(_50); StorageLive(_51); -- StorageLive(_52); + StorageLive(_52); - _52 = _2; - _51 = move _52 as i32 (IntToInt); -- StorageDead(_52); ++ _52 = const 1_u64; + _51 = const 1_u64 as i32 (IntToInt); + StorageDead(_52); _50 = opaque::(move _51) -> [return: bb17, unwind unreachable]; } @@ -327,11 +347,12 @@ StorageDead(_50); StorageLive(_53); StorageLive(_54); -- StorageLive(_55); + StorageLive(_55); - _55 = _2; - _54 = move _55 as i64 (IntToInt); -- StorageDead(_55); ++ _55 = const 1_u64; + _54 = const 1_u64 as i64 (IntToInt); + StorageDead(_55); _53 = opaque::(move _54) -> [return: bb18, unwind unreachable]; } @@ -340,11 +361,12 @@ StorageDead(_53); StorageLive(_56); StorageLive(_57); -- StorageLive(_58); + StorageLive(_58); - _58 = _2; - _57 = move _58 as f32 (IntToFloat); -- StorageDead(_58); ++ _58 = const 1_u64; + _57 = const 1_u64 as f32 (IntToFloat); + StorageDead(_58); _56 = opaque::(move _57) -> [return: bb19, unwind unreachable]; } @@ -353,11 +375,12 @@ StorageDead(_56); StorageLive(_59); StorageLive(_60); -- StorageLive(_61); + StorageLive(_61); - _61 = _2; - _60 = move _61 as f64 (IntToFloat); -- StorageDead(_61); ++ _61 = const 1_u64; + _60 = const 1_u64 as f64 (IntToFloat); + StorageDead(_61); _59 = opaque::(move _60) -> [return: bb20, unwind unreachable]; } @@ -366,11 +389,12 @@ StorageDead(_59); StorageLive(_62); StorageLive(_63); -- StorageLive(_64); + StorageLive(_64); - _64 = _3; - _63 = move _64 as u8 (FloatToInt); -- StorageDead(_64); ++ _64 = const 1f64; + _63 = const 1f64 as u8 (FloatToInt); + StorageDead(_64); _62 = opaque::(move _63) -> [return: bb21, unwind unreachable]; } @@ -379,11 +403,12 @@ StorageDead(_62); StorageLive(_65); StorageLive(_66); -- StorageLive(_67); + StorageLive(_67); - _67 = _3; - _66 = move _67 as u16 (FloatToInt); -- StorageDead(_67); ++ _67 = const 1f64; + _66 = const 1f64 as u16 (FloatToInt); + StorageDead(_67); _65 = opaque::(move _66) -> [return: bb22, unwind unreachable]; } @@ -392,11 +417,12 @@ StorageDead(_65); StorageLive(_68); StorageLive(_69); -- StorageLive(_70); + StorageLive(_70); - _70 = _3; - _69 = move _70 as u32 (FloatToInt); -- StorageDead(_70); ++ _70 = const 1f64; + _69 = const 1f64 as u32 (FloatToInt); + StorageDead(_70); _68 = opaque::(move _69) -> [return: bb23, unwind unreachable]; } @@ -405,11 +431,12 @@ StorageDead(_68); StorageLive(_71); StorageLive(_72); -- StorageLive(_73); + StorageLive(_73); - _73 = _3; - _72 = move _73 as u64 (FloatToInt); -- StorageDead(_73); ++ _73 = const 1f64; + _72 = const 1f64 as u64 (FloatToInt); + StorageDead(_73); _71 = opaque::(move _72) -> [return: bb24, unwind unreachable]; } @@ -418,11 +445,12 @@ StorageDead(_71); StorageLive(_74); StorageLive(_75); -- StorageLive(_76); + StorageLive(_76); - _76 = _3; - _75 = move _76 as i8 (FloatToInt); -- StorageDead(_76); ++ _76 = const 1f64; + _75 = const 1f64 as i8 (FloatToInt); + StorageDead(_76); _74 = opaque::(move _75) -> [return: bb25, unwind unreachable]; } @@ -431,11 +459,12 @@ StorageDead(_74); StorageLive(_77); StorageLive(_78); -- StorageLive(_79); + StorageLive(_79); - _79 = _3; - _78 = move _79 as i16 (FloatToInt); -- StorageDead(_79); ++ _79 = const 1f64; + _78 = const 1f64 as i16 (FloatToInt); + StorageDead(_79); _77 = opaque::(move _78) -> [return: bb26, unwind unreachable]; } @@ -444,11 +473,12 @@ StorageDead(_77); StorageLive(_80); StorageLive(_81); -- StorageLive(_82); + StorageLive(_82); - _82 = _3; - _81 = move _82 as i32 (FloatToInt); -- StorageDead(_82); ++ _82 = const 1f64; + _81 = const 1f64 as i32 (FloatToInt); + StorageDead(_82); _80 = opaque::(move _81) -> [return: bb27, unwind unreachable]; } @@ -457,11 +487,12 @@ StorageDead(_80); StorageLive(_83); StorageLive(_84); -- StorageLive(_85); + StorageLive(_85); - _85 = _3; - _84 = move _85 as i64 (FloatToInt); -- StorageDead(_85); ++ _85 = const 1f64; + _84 = const 1f64 as i64 (FloatToInt); + StorageDead(_85); _83 = opaque::(move _84) -> [return: bb28, unwind unreachable]; } @@ -470,11 +501,12 @@ StorageDead(_83); StorageLive(_86); StorageLive(_87); -- StorageLive(_88); + StorageLive(_88); - _88 = _3; - _87 = move _88 as f32 (FloatToFloat); -- StorageDead(_88); ++ _88 = const 1f64; + _87 = const 1f64 as f32 (FloatToFloat); + StorageDead(_88); _86 = opaque::(move _87) -> [return: bb29, unwind unreachable]; } @@ -482,19 +514,23 @@ StorageDead(_87); StorageDead(_86); StorageLive(_89); -- StorageLive(_90); + StorageLive(_90); - _90 = _3; - _89 = opaque::(move _90) -> [return: bb30, unwind unreachable]; ++ _90 = const 1f64; + _89 = opaque::(const 1f64) -> [return: bb30, unwind unreachable]; } bb30: { -- StorageDead(_90); + StorageDead(_90); StorageDead(_89); _0 = const (); - StorageDead(_3); - StorageDead(_2); - StorageDead(_1); ++ nop; ++ nop; ++ nop; return; } } diff --git a/tests/mir-opt/gvn.cast.GVN.panic-unwind.diff b/tests/mir-opt/gvn.cast.GVN.panic-unwind.diff index 33192ed8de033..fbdec455188ab 100644 --- a/tests/mir-opt/gvn.cast.GVN.panic-unwind.diff +++ b/tests/mir-opt/gvn.cast.GVN.panic-unwind.diff @@ -105,18 +105,22 @@ bb0: { - StorageLive(_1); ++ nop; _1 = const 1_i64; - StorageLive(_2); ++ nop; _2 = const 1_u64; - StorageLive(_3); ++ nop; _3 = const 1f64; StorageLive(_4); StorageLive(_5); -- StorageLive(_6); + StorageLive(_6); - _6 = _1; - _5 = move _6 as u8 (IntToInt); -- StorageDead(_6); ++ _6 = const 1_i64; + _5 = const 1_i64 as u8 (IntToInt); + StorageDead(_6); _4 = opaque::(move _5) -> [return: bb1, unwind continue]; } @@ -125,11 +129,12 @@ StorageDead(_4); StorageLive(_7); StorageLive(_8); -- StorageLive(_9); + StorageLive(_9); - _9 = _1; - _8 = move _9 as u16 (IntToInt); -- StorageDead(_9); ++ _9 = const 1_i64; + _8 = const 1_i64 as u16 (IntToInt); + StorageDead(_9); _7 = opaque::(move _8) -> [return: bb2, unwind continue]; } @@ -138,11 +143,12 @@ StorageDead(_7); StorageLive(_10); StorageLive(_11); -- StorageLive(_12); + StorageLive(_12); - _12 = _1; - _11 = move _12 as u32 (IntToInt); -- StorageDead(_12); ++ _12 = const 1_i64; + _11 = const 1_i64 as u32 (IntToInt); + StorageDead(_12); _10 = opaque::(move _11) -> [return: bb3, unwind continue]; } @@ -151,11 +157,12 @@ StorageDead(_10); StorageLive(_13); StorageLive(_14); -- StorageLive(_15); + StorageLive(_15); - _15 = _1; - _14 = move _15 as u64 (IntToInt); -- StorageDead(_15); ++ _15 = const 1_i64; + _14 = const 1_i64 as u64 (IntToInt); + StorageDead(_15); _13 = opaque::(move _14) -> [return: bb4, unwind continue]; } @@ -164,11 +171,12 @@ StorageDead(_13); StorageLive(_16); StorageLive(_17); -- StorageLive(_18); + StorageLive(_18); - _18 = _1; - _17 = move _18 as i8 (IntToInt); -- StorageDead(_18); ++ _18 = const 1_i64; + _17 = const 1_i64 as i8 (IntToInt); + StorageDead(_18); _16 = opaque::(move _17) -> [return: bb5, unwind continue]; } @@ -177,11 +185,12 @@ StorageDead(_16); StorageLive(_19); StorageLive(_20); -- StorageLive(_21); + StorageLive(_21); - _21 = _1; - _20 = move _21 as i16 (IntToInt); -- StorageDead(_21); ++ _21 = const 1_i64; + _20 = const 1_i64 as i16 (IntToInt); + StorageDead(_21); _19 = opaque::(move _20) -> [return: bb6, unwind continue]; } @@ -190,11 +199,12 @@ StorageDead(_19); StorageLive(_22); StorageLive(_23); -- StorageLive(_24); + StorageLive(_24); - _24 = _1; - _23 = move _24 as i32 (IntToInt); -- StorageDead(_24); ++ _24 = const 1_i64; + _23 = const 1_i64 as i32 (IntToInt); + StorageDead(_24); _22 = opaque::(move _23) -> [return: bb7, unwind continue]; } @@ -202,22 +212,24 @@ StorageDead(_23); StorageDead(_22); StorageLive(_25); -- StorageLive(_26); + StorageLive(_26); - _26 = _1; - _25 = opaque::(move _26) -> [return: bb8, unwind continue]; ++ _26 = const 1_i64; + _25 = opaque::(const 1_i64) -> [return: bb8, unwind continue]; } bb8: { -- StorageDead(_26); + StorageDead(_26); StorageDead(_25); StorageLive(_27); StorageLive(_28); -- StorageLive(_29); + StorageLive(_29); - _29 = _1; - _28 = move _29 as f32 (IntToFloat); -- StorageDead(_29); ++ _29 = const 1_i64; + _28 = const 1_i64 as f32 (IntToFloat); + StorageDead(_29); _27 = opaque::(move _28) -> [return: bb9, unwind continue]; } @@ -226,11 +238,12 @@ StorageDead(_27); StorageLive(_30); StorageLive(_31); -- StorageLive(_32); + StorageLive(_32); - _32 = _1; - _31 = move _32 as f64 (IntToFloat); -- StorageDead(_32); ++ _32 = const 1_i64; + _31 = const 1_i64 as f64 (IntToFloat); + StorageDead(_32); _30 = opaque::(move _31) -> [return: bb10, unwind continue]; } @@ -239,11 +252,12 @@ StorageDead(_30); StorageLive(_33); StorageLive(_34); -- StorageLive(_35); + StorageLive(_35); - _35 = _2; - _34 = move _35 as u8 (IntToInt); -- StorageDead(_35); ++ _35 = const 1_u64; + _34 = const 1_u64 as u8 (IntToInt); + StorageDead(_35); _33 = opaque::(move _34) -> [return: bb11, unwind continue]; } @@ -252,11 +266,12 @@ StorageDead(_33); StorageLive(_36); StorageLive(_37); -- StorageLive(_38); + StorageLive(_38); - _38 = _2; - _37 = move _38 as u16 (IntToInt); -- StorageDead(_38); ++ _38 = const 1_u64; + _37 = const 1_u64 as u16 (IntToInt); + StorageDead(_38); _36 = opaque::(move _37) -> [return: bb12, unwind continue]; } @@ -265,11 +280,12 @@ StorageDead(_36); StorageLive(_39); StorageLive(_40); -- StorageLive(_41); + StorageLive(_41); - _41 = _2; - _40 = move _41 as u32 (IntToInt); -- StorageDead(_41); ++ _41 = const 1_u64; + _40 = const 1_u64 as u32 (IntToInt); + StorageDead(_41); _39 = opaque::(move _40) -> [return: bb13, unwind continue]; } @@ -277,22 +293,24 @@ StorageDead(_40); StorageDead(_39); StorageLive(_42); -- StorageLive(_43); + StorageLive(_43); - _43 = _2; - _42 = opaque::(move _43) -> [return: bb14, unwind continue]; ++ _43 = const 1_u64; + _42 = opaque::(const 1_u64) -> [return: bb14, unwind continue]; } bb14: { -- StorageDead(_43); + StorageDead(_43); StorageDead(_42); StorageLive(_44); StorageLive(_45); -- StorageLive(_46); + StorageLive(_46); - _46 = _2; - _45 = move _46 as i8 (IntToInt); -- StorageDead(_46); ++ _46 = const 1_u64; + _45 = const 1_u64 as i8 (IntToInt); + StorageDead(_46); _44 = opaque::(move _45) -> [return: bb15, unwind continue]; } @@ -301,11 +319,12 @@ StorageDead(_44); StorageLive(_47); StorageLive(_48); -- StorageLive(_49); + StorageLive(_49); - _49 = _2; - _48 = move _49 as i16 (IntToInt); -- StorageDead(_49); ++ _49 = const 1_u64; + _48 = const 1_u64 as i16 (IntToInt); + StorageDead(_49); _47 = opaque::(move _48) -> [return: bb16, unwind continue]; } @@ -314,11 +333,12 @@ StorageDead(_47); StorageLive(_50); StorageLive(_51); -- StorageLive(_52); + StorageLive(_52); - _52 = _2; - _51 = move _52 as i32 (IntToInt); -- StorageDead(_52); ++ _52 = const 1_u64; + _51 = const 1_u64 as i32 (IntToInt); + StorageDead(_52); _50 = opaque::(move _51) -> [return: bb17, unwind continue]; } @@ -327,11 +347,12 @@ StorageDead(_50); StorageLive(_53); StorageLive(_54); -- StorageLive(_55); + StorageLive(_55); - _55 = _2; - _54 = move _55 as i64 (IntToInt); -- StorageDead(_55); ++ _55 = const 1_u64; + _54 = const 1_u64 as i64 (IntToInt); + StorageDead(_55); _53 = opaque::(move _54) -> [return: bb18, unwind continue]; } @@ -340,11 +361,12 @@ StorageDead(_53); StorageLive(_56); StorageLive(_57); -- StorageLive(_58); + StorageLive(_58); - _58 = _2; - _57 = move _58 as f32 (IntToFloat); -- StorageDead(_58); ++ _58 = const 1_u64; + _57 = const 1_u64 as f32 (IntToFloat); + StorageDead(_58); _56 = opaque::(move _57) -> [return: bb19, unwind continue]; } @@ -353,11 +375,12 @@ StorageDead(_56); StorageLive(_59); StorageLive(_60); -- StorageLive(_61); + StorageLive(_61); - _61 = _2; - _60 = move _61 as f64 (IntToFloat); -- StorageDead(_61); ++ _61 = const 1_u64; + _60 = const 1_u64 as f64 (IntToFloat); + StorageDead(_61); _59 = opaque::(move _60) -> [return: bb20, unwind continue]; } @@ -366,11 +389,12 @@ StorageDead(_59); StorageLive(_62); StorageLive(_63); -- StorageLive(_64); + StorageLive(_64); - _64 = _3; - _63 = move _64 as u8 (FloatToInt); -- StorageDead(_64); ++ _64 = const 1f64; + _63 = const 1f64 as u8 (FloatToInt); + StorageDead(_64); _62 = opaque::(move _63) -> [return: bb21, unwind continue]; } @@ -379,11 +403,12 @@ StorageDead(_62); StorageLive(_65); StorageLive(_66); -- StorageLive(_67); + StorageLive(_67); - _67 = _3; - _66 = move _67 as u16 (FloatToInt); -- StorageDead(_67); ++ _67 = const 1f64; + _66 = const 1f64 as u16 (FloatToInt); + StorageDead(_67); _65 = opaque::(move _66) -> [return: bb22, unwind continue]; } @@ -392,11 +417,12 @@ StorageDead(_65); StorageLive(_68); StorageLive(_69); -- StorageLive(_70); + StorageLive(_70); - _70 = _3; - _69 = move _70 as u32 (FloatToInt); -- StorageDead(_70); ++ _70 = const 1f64; + _69 = const 1f64 as u32 (FloatToInt); + StorageDead(_70); _68 = opaque::(move _69) -> [return: bb23, unwind continue]; } @@ -405,11 +431,12 @@ StorageDead(_68); StorageLive(_71); StorageLive(_72); -- StorageLive(_73); + StorageLive(_73); - _73 = _3; - _72 = move _73 as u64 (FloatToInt); -- StorageDead(_73); ++ _73 = const 1f64; + _72 = const 1f64 as u64 (FloatToInt); + StorageDead(_73); _71 = opaque::(move _72) -> [return: bb24, unwind continue]; } @@ -418,11 +445,12 @@ StorageDead(_71); StorageLive(_74); StorageLive(_75); -- StorageLive(_76); + StorageLive(_76); - _76 = _3; - _75 = move _76 as i8 (FloatToInt); -- StorageDead(_76); ++ _76 = const 1f64; + _75 = const 1f64 as i8 (FloatToInt); + StorageDead(_76); _74 = opaque::(move _75) -> [return: bb25, unwind continue]; } @@ -431,11 +459,12 @@ StorageDead(_74); StorageLive(_77); StorageLive(_78); -- StorageLive(_79); + StorageLive(_79); - _79 = _3; - _78 = move _79 as i16 (FloatToInt); -- StorageDead(_79); ++ _79 = const 1f64; + _78 = const 1f64 as i16 (FloatToInt); + StorageDead(_79); _77 = opaque::(move _78) -> [return: bb26, unwind continue]; } @@ -444,11 +473,12 @@ StorageDead(_77); StorageLive(_80); StorageLive(_81); -- StorageLive(_82); + StorageLive(_82); - _82 = _3; - _81 = move _82 as i32 (FloatToInt); -- StorageDead(_82); ++ _82 = const 1f64; + _81 = const 1f64 as i32 (FloatToInt); + StorageDead(_82); _80 = opaque::(move _81) -> [return: bb27, unwind continue]; } @@ -457,11 +487,12 @@ StorageDead(_80); StorageLive(_83); StorageLive(_84); -- StorageLive(_85); + StorageLive(_85); - _85 = _3; - _84 = move _85 as i64 (FloatToInt); -- StorageDead(_85); ++ _85 = const 1f64; + _84 = const 1f64 as i64 (FloatToInt); + StorageDead(_85); _83 = opaque::(move _84) -> [return: bb28, unwind continue]; } @@ -470,11 +501,12 @@ StorageDead(_83); StorageLive(_86); StorageLive(_87); -- StorageLive(_88); + StorageLive(_88); - _88 = _3; - _87 = move _88 as f32 (FloatToFloat); -- StorageDead(_88); ++ _88 = const 1f64; + _87 = const 1f64 as f32 (FloatToFloat); + StorageDead(_88); _86 = opaque::(move _87) -> [return: bb29, unwind continue]; } @@ -482,19 +514,23 @@ StorageDead(_87); StorageDead(_86); StorageLive(_89); -- StorageLive(_90); + StorageLive(_90); - _90 = _3; - _89 = opaque::(move _90) -> [return: bb30, unwind continue]; ++ _90 = const 1f64; + _89 = opaque::(const 1f64) -> [return: bb30, unwind continue]; } bb30: { -- StorageDead(_90); + StorageDead(_90); StorageDead(_89); _0 = const (); - StorageDead(_3); - StorageDead(_2); - StorageDead(_1); ++ nop; ++ nop; ++ nop; return; } } diff --git a/tests/mir-opt/gvn.dereferences.GVN.panic-abort.diff b/tests/mir-opt/gvn.dereferences.GVN.panic-abort.diff index ee320cf678701..46bf13985daf8 100644 --- a/tests/mir-opt/gvn.dereferences.GVN.panic-abort.diff +++ b/tests/mir-opt/gvn.dereferences.GVN.panic-abort.diff @@ -116,6 +116,7 @@ _18 = &(*_1); StorageLive(_19); - StorageLive(_20); ++ nop; _20 = (*_18); - _19 = opaque::(move _20) -> [return: bb7, unwind unreachable]; + _19 = opaque::(_20) -> [return: bb7, unwind unreachable]; @@ -123,16 +124,18 @@ bb7: { - StorageDead(_20); ++ nop; StorageDead(_19); StorageLive(_21); -- StorageLive(_22); + StorageLive(_22); - _22 = (*_18); - _21 = opaque::(move _22) -> [return: bb8, unwind unreachable]; ++ _22 = _20; + _21 = opaque::(_20) -> [return: bb8, unwind unreachable]; } bb8: { -- StorageDead(_22); + StorageDead(_22); StorageDead(_21); StorageLive(_23); StorageLive(_24); @@ -163,6 +166,7 @@ StorageDead(_27); StorageLive(_29); - StorageLive(_30); ++ nop; _30 = ((*_3).0: u32); - _29 = opaque::(move _30) -> [return: bb12, unwind unreachable]; + _29 = opaque::(_30) -> [return: bb12, unwind unreachable]; @@ -170,16 +174,18 @@ bb12: { - StorageDead(_30); ++ nop; StorageDead(_29); StorageLive(_31); -- StorageLive(_32); + StorageLive(_32); - _32 = ((*_3).0: u32); - _31 = opaque::(move _32) -> [return: bb13, unwind unreachable]; ++ _32 = _30; + _31 = opaque::(_30) -> [return: bb13, unwind unreachable]; } bb13: { -- StorageDead(_32); + StorageDead(_32); StorageDead(_31); _0 = const (); StorageDead(_18); diff --git a/tests/mir-opt/gvn.dereferences.GVN.panic-unwind.diff b/tests/mir-opt/gvn.dereferences.GVN.panic-unwind.diff index f627b4d59887a..3e731ead859e6 100644 --- a/tests/mir-opt/gvn.dereferences.GVN.panic-unwind.diff +++ b/tests/mir-opt/gvn.dereferences.GVN.panic-unwind.diff @@ -116,6 +116,7 @@ _18 = &(*_1); StorageLive(_19); - StorageLive(_20); ++ nop; _20 = (*_18); - _19 = opaque::(move _20) -> [return: bb7, unwind continue]; + _19 = opaque::(_20) -> [return: bb7, unwind continue]; @@ -123,16 +124,18 @@ bb7: { - StorageDead(_20); ++ nop; StorageDead(_19); StorageLive(_21); -- StorageLive(_22); + StorageLive(_22); - _22 = (*_18); - _21 = opaque::(move _22) -> [return: bb8, unwind continue]; ++ _22 = _20; + _21 = opaque::(_20) -> [return: bb8, unwind continue]; } bb8: { -- StorageDead(_22); + StorageDead(_22); StorageDead(_21); StorageLive(_23); StorageLive(_24); @@ -163,6 +166,7 @@ StorageDead(_27); StorageLive(_29); - StorageLive(_30); ++ nop; _30 = ((*_3).0: u32); - _29 = opaque::(move _30) -> [return: bb12, unwind continue]; + _29 = opaque::(_30) -> [return: bb12, unwind continue]; @@ -170,16 +174,18 @@ bb12: { - StorageDead(_30); ++ nop; StorageDead(_29); StorageLive(_31); -- StorageLive(_32); + StorageLive(_32); - _32 = ((*_3).0: u32); - _31 = opaque::(move _32) -> [return: bb13, unwind continue]; ++ _32 = _30; + _31 = opaque::(_30) -> [return: bb13, unwind continue]; } bb13: { -- StorageDead(_32); + StorageDead(_32); StorageDead(_31); _0 = const (); StorageDead(_18); diff --git a/tests/mir-opt/gvn.multiple_branches.GVN.panic-abort.diff b/tests/mir-opt/gvn.multiple_branches.GVN.panic-abort.diff index 0a66900283b61..29ca1ba5902e1 100644 --- a/tests/mir-opt/gvn.multiple_branches.GVN.panic-abort.diff +++ b/tests/mir-opt/gvn.multiple_branches.GVN.panic-abort.diff @@ -39,9 +39,9 @@ let mut _34: u8; bb0: { -- StorageLive(_4); -- StorageLive(_5); -- _5 = _1; + StorageLive(_4); + StorageLive(_5); + _5 = _1; - switchInt(move _5) -> [0: bb4, otherwise: bb1]; + switchInt(_1) -> [0: bb4, otherwise: bb1]; } @@ -49,121 +49,130 @@ bb1: { StorageLive(_6); - StorageLive(_7); -- StorageLive(_8); -- _8 = _2; -- StorageLive(_9); -- _9 = _3; ++ nop; + StorageLive(_8); + _8 = _2; + StorageLive(_9); + _9 = _3; - _7 = Add(move _8, move _9); -- StorageDead(_9); -- StorageDead(_8); -- _6 = opaque::(move _7) -> [return: bb2, unwind unreachable]; + _7 = Add(_2, _3); + StorageDead(_9); + StorageDead(_8); +- _6 = opaque::(move _7) -> [return: bb2, unwind unreachable]; + _6 = opaque::(_7) -> [return: bb2, unwind unreachable]; } bb2: { - StorageDead(_7); ++ nop; StorageDead(_6); StorageLive(_10); -- StorageLive(_11); -- StorageLive(_12); -- _12 = _2; -- StorageLive(_13); -- _13 = _3; + StorageLive(_11); + StorageLive(_12); + _12 = _2; + StorageLive(_13); + _13 = _3; - _11 = Add(move _12, move _13); -- StorageDead(_13); -- StorageDead(_12); ++ _11 = _7; + StorageDead(_13); + StorageDead(_12); - _10 = opaque::(move _11) -> [return: bb3, unwind unreachable]; + _10 = opaque::(_7) -> [return: bb3, unwind unreachable]; } bb3: { -- StorageDead(_11); + StorageDead(_11); StorageDead(_10); -- _4 = const (); + _4 = const (); goto -> bb7; } bb4: { StorageLive(_14); - StorageLive(_15); -- StorageLive(_16); -- _16 = _2; -- StorageLive(_17); -- _17 = _3; ++ nop; + StorageLive(_16); + _16 = _2; + StorageLive(_17); + _17 = _3; - _15 = Add(move _16, move _17); -- StorageDead(_17); -- StorageDead(_16); -- _14 = opaque::(move _15) -> [return: bb5, unwind unreachable]; + _15 = Add(_2, _3); + StorageDead(_17); + StorageDead(_16); +- _14 = opaque::(move _15) -> [return: bb5, unwind unreachable]; + _14 = opaque::(_15) -> [return: bb5, unwind unreachable]; } bb5: { - StorageDead(_15); ++ nop; StorageDead(_14); StorageLive(_18); -- StorageLive(_19); -- StorageLive(_20); -- _20 = _2; -- StorageLive(_21); -- _21 = _3; + StorageLive(_19); + StorageLive(_20); + _20 = _2; + StorageLive(_21); + _21 = _3; - _19 = Add(move _20, move _21); -- StorageDead(_21); -- StorageDead(_20); ++ _19 = _15; + StorageDead(_21); + StorageDead(_20); - _18 = opaque::(move _19) -> [return: bb6, unwind unreachable]; + _18 = opaque::(_15) -> [return: bb6, unwind unreachable]; } bb6: { -- StorageDead(_19); + StorageDead(_19); StorageDead(_18); -- _4 = const (); + _4 = const (); goto -> bb7; } bb7: { -- StorageDead(_5); -- StorageDead(_4); + StorageDead(_5); + StorageDead(_4); StorageLive(_22); - StorageLive(_23); -- StorageLive(_24); -- _24 = _2; -- StorageLive(_25); -- _25 = _3; ++ nop; + StorageLive(_24); + _24 = _2; + StorageLive(_25); + _25 = _3; - _23 = Add(move _24, move _25); -- StorageDead(_25); -- StorageDead(_24); -- _22 = opaque::(move _23) -> [return: bb8, unwind unreachable]; + _23 = Add(_2, _3); + StorageDead(_25); + StorageDead(_24); +- _22 = opaque::(move _23) -> [return: bb8, unwind unreachable]; + _22 = opaque::(_23) -> [return: bb8, unwind unreachable]; } bb8: { - StorageDead(_23); ++ nop; StorageDead(_22); -- StorageLive(_26); -- _26 = _1; + StorageLive(_26); + _26 = _1; - switchInt(move _26) -> [0: bb11, otherwise: bb9]; + switchInt(_1) -> [0: bb11, otherwise: bb9]; } bb9: { StorageLive(_27); -- StorageLive(_28); -- StorageLive(_29); -- _29 = _2; -- StorageLive(_30); -- _30 = _3; + StorageLive(_28); + StorageLive(_29); + _29 = _2; + StorageLive(_30); + _30 = _3; - _28 = Add(move _29, move _30); -- StorageDead(_30); -- StorageDead(_29); ++ _28 = _23; + StorageDead(_30); + StorageDead(_29); - _27 = opaque::(move _28) -> [return: bb10, unwind unreachable]; + _27 = opaque::(_23) -> [return: bb10, unwind unreachable]; } bb10: { -- StorageDead(_28); + StorageDead(_28); StorageDead(_27); _0 = const (); goto -> bb13; @@ -171,27 +180,28 @@ bb11: { StorageLive(_31); -- StorageLive(_32); -- StorageLive(_33); -- _33 = _2; -- StorageLive(_34); -- _34 = _3; + StorageLive(_32); + StorageLive(_33); + _33 = _2; + StorageLive(_34); + _34 = _3; - _32 = Add(move _33, move _34); -- StorageDead(_34); -- StorageDead(_33); ++ _32 = _23; + StorageDead(_34); + StorageDead(_33); - _31 = opaque::(move _32) -> [return: bb12, unwind unreachable]; + _31 = opaque::(_23) -> [return: bb12, unwind unreachable]; } bb12: { -- StorageDead(_32); + StorageDead(_32); StorageDead(_31); _0 = const (); goto -> bb13; } bb13: { -- StorageDead(_26); + StorageDead(_26); return; } } diff --git a/tests/mir-opt/gvn.multiple_branches.GVN.panic-unwind.diff b/tests/mir-opt/gvn.multiple_branches.GVN.panic-unwind.diff index 0199f2720a989..5394dc8be8a04 100644 --- a/tests/mir-opt/gvn.multiple_branches.GVN.panic-unwind.diff +++ b/tests/mir-opt/gvn.multiple_branches.GVN.panic-unwind.diff @@ -39,9 +39,9 @@ let mut _34: u8; bb0: { -- StorageLive(_4); -- StorageLive(_5); -- _5 = _1; + StorageLive(_4); + StorageLive(_5); + _5 = _1; - switchInt(move _5) -> [0: bb4, otherwise: bb1]; + switchInt(_1) -> [0: bb4, otherwise: bb1]; } @@ -49,121 +49,130 @@ bb1: { StorageLive(_6); - StorageLive(_7); -- StorageLive(_8); -- _8 = _2; -- StorageLive(_9); -- _9 = _3; ++ nop; + StorageLive(_8); + _8 = _2; + StorageLive(_9); + _9 = _3; - _7 = Add(move _8, move _9); -- StorageDead(_9); -- StorageDead(_8); -- _6 = opaque::(move _7) -> [return: bb2, unwind continue]; + _7 = Add(_2, _3); + StorageDead(_9); + StorageDead(_8); +- _6 = opaque::(move _7) -> [return: bb2, unwind continue]; + _6 = opaque::(_7) -> [return: bb2, unwind continue]; } bb2: { - StorageDead(_7); ++ nop; StorageDead(_6); StorageLive(_10); -- StorageLive(_11); -- StorageLive(_12); -- _12 = _2; -- StorageLive(_13); -- _13 = _3; + StorageLive(_11); + StorageLive(_12); + _12 = _2; + StorageLive(_13); + _13 = _3; - _11 = Add(move _12, move _13); -- StorageDead(_13); -- StorageDead(_12); ++ _11 = _7; + StorageDead(_13); + StorageDead(_12); - _10 = opaque::(move _11) -> [return: bb3, unwind continue]; + _10 = opaque::(_7) -> [return: bb3, unwind continue]; } bb3: { -- StorageDead(_11); + StorageDead(_11); StorageDead(_10); -- _4 = const (); + _4 = const (); goto -> bb7; } bb4: { StorageLive(_14); - StorageLive(_15); -- StorageLive(_16); -- _16 = _2; -- StorageLive(_17); -- _17 = _3; ++ nop; + StorageLive(_16); + _16 = _2; + StorageLive(_17); + _17 = _3; - _15 = Add(move _16, move _17); -- StorageDead(_17); -- StorageDead(_16); -- _14 = opaque::(move _15) -> [return: bb5, unwind continue]; + _15 = Add(_2, _3); + StorageDead(_17); + StorageDead(_16); +- _14 = opaque::(move _15) -> [return: bb5, unwind continue]; + _14 = opaque::(_15) -> [return: bb5, unwind continue]; } bb5: { - StorageDead(_15); ++ nop; StorageDead(_14); StorageLive(_18); -- StorageLive(_19); -- StorageLive(_20); -- _20 = _2; -- StorageLive(_21); -- _21 = _3; + StorageLive(_19); + StorageLive(_20); + _20 = _2; + StorageLive(_21); + _21 = _3; - _19 = Add(move _20, move _21); -- StorageDead(_21); -- StorageDead(_20); ++ _19 = _15; + StorageDead(_21); + StorageDead(_20); - _18 = opaque::(move _19) -> [return: bb6, unwind continue]; + _18 = opaque::(_15) -> [return: bb6, unwind continue]; } bb6: { -- StorageDead(_19); + StorageDead(_19); StorageDead(_18); -- _4 = const (); + _4 = const (); goto -> bb7; } bb7: { -- StorageDead(_5); -- StorageDead(_4); + StorageDead(_5); + StorageDead(_4); StorageLive(_22); - StorageLive(_23); -- StorageLive(_24); -- _24 = _2; -- StorageLive(_25); -- _25 = _3; ++ nop; + StorageLive(_24); + _24 = _2; + StorageLive(_25); + _25 = _3; - _23 = Add(move _24, move _25); -- StorageDead(_25); -- StorageDead(_24); -- _22 = opaque::(move _23) -> [return: bb8, unwind continue]; + _23 = Add(_2, _3); + StorageDead(_25); + StorageDead(_24); +- _22 = opaque::(move _23) -> [return: bb8, unwind continue]; + _22 = opaque::(_23) -> [return: bb8, unwind continue]; } bb8: { - StorageDead(_23); ++ nop; StorageDead(_22); -- StorageLive(_26); -- _26 = _1; + StorageLive(_26); + _26 = _1; - switchInt(move _26) -> [0: bb11, otherwise: bb9]; + switchInt(_1) -> [0: bb11, otherwise: bb9]; } bb9: { StorageLive(_27); -- StorageLive(_28); -- StorageLive(_29); -- _29 = _2; -- StorageLive(_30); -- _30 = _3; + StorageLive(_28); + StorageLive(_29); + _29 = _2; + StorageLive(_30); + _30 = _3; - _28 = Add(move _29, move _30); -- StorageDead(_30); -- StorageDead(_29); ++ _28 = _23; + StorageDead(_30); + StorageDead(_29); - _27 = opaque::(move _28) -> [return: bb10, unwind continue]; + _27 = opaque::(_23) -> [return: bb10, unwind continue]; } bb10: { -- StorageDead(_28); + StorageDead(_28); StorageDead(_27); _0 = const (); goto -> bb13; @@ -171,27 +180,28 @@ bb11: { StorageLive(_31); -- StorageLive(_32); -- StorageLive(_33); -- _33 = _2; -- StorageLive(_34); -- _34 = _3; + StorageLive(_32); + StorageLive(_33); + _33 = _2; + StorageLive(_34); + _34 = _3; - _32 = Add(move _33, move _34); -- StorageDead(_34); -- StorageDead(_33); ++ _32 = _23; + StorageDead(_34); + StorageDead(_33); - _31 = opaque::(move _32) -> [return: bb12, unwind continue]; + _31 = opaque::(_23) -> [return: bb12, unwind continue]; } bb12: { -- StorageDead(_32); + StorageDead(_32); StorageDead(_31); _0 = const (); goto -> bb13; } bb13: { -- StorageDead(_26); + StorageDead(_26); return; } } diff --git a/tests/mir-opt/gvn.repeated_index.GVN.panic-abort.diff b/tests/mir-opt/gvn.repeated_index.GVN.panic-abort.diff index 4c29523d6b282..6e542d7f964ce 100644 --- a/tests/mir-opt/gvn.repeated_index.GVN.panic-abort.diff +++ b/tests/mir-opt/gvn.repeated_index.GVN.panic-abort.diff @@ -23,11 +23,11 @@ bb0: { StorageLive(_3); -- StorageLive(_4); -- _4 = _1; + StorageLive(_4); + _4 = _1; - _3 = [move _4; N]; -- StorageDead(_4); + _3 = [_1; N]; + StorageDead(_4); StorageLive(_5); StorageLive(_6); StorageLive(_7); @@ -55,6 +55,7 @@ - _13 = Len(_3); - _14 = Lt(_12, _13); - assert(move _14, "index out of bounds: the length is {} but the index is {}", move _13, _12) -> [success: bb3, unwind unreachable]; ++ _13 = _8; + _14 = Lt(_2, _8); + assert(move _14, "index out of bounds: the length is {} but the index is {}", _8, _2) -> [success: bb3, unwind unreachable]; } diff --git a/tests/mir-opt/gvn.repeated_index.GVN.panic-unwind.diff b/tests/mir-opt/gvn.repeated_index.GVN.panic-unwind.diff index e44f54cf3cf3c..a07cd49ec7c6f 100644 --- a/tests/mir-opt/gvn.repeated_index.GVN.panic-unwind.diff +++ b/tests/mir-opt/gvn.repeated_index.GVN.panic-unwind.diff @@ -23,11 +23,11 @@ bb0: { StorageLive(_3); -- StorageLive(_4); -- _4 = _1; + StorageLive(_4); + _4 = _1; - _3 = [move _4; N]; -- StorageDead(_4); + _3 = [_1; N]; + StorageDead(_4); StorageLive(_5); StorageLive(_6); StorageLive(_7); @@ -55,6 +55,7 @@ - _13 = Len(_3); - _14 = Lt(_12, _13); - assert(move _14, "index out of bounds: the length is {} but the index is {}", move _13, _12) -> [success: bb3, unwind continue]; ++ _13 = _8; + _14 = Lt(_2, _8); + assert(move _14, "index out of bounds: the length is {} but the index is {}", _8, _2) -> [success: bb3, unwind continue]; } diff --git a/tests/mir-opt/gvn.slices.GVN.panic-abort.diff b/tests/mir-opt/gvn.slices.GVN.panic-abort.diff index de3d28d057594..d924c70d617dd 100644 --- a/tests/mir-opt/gvn.slices.GVN.panic-abort.diff +++ b/tests/mir-opt/gvn.slices.GVN.panic-abort.diff @@ -85,30 +85,32 @@ bb0: { - StorageLive(_1); ++ nop; _1 = const "my favourite slice"; StorageLive(_2); -- StorageLive(_3); -- _3 = _1; + StorageLive(_3); + _3 = _1; - _2 = opaque::<&str>(move _3) -> [return: bb1, unwind unreachable]; + _2 = opaque::<&str>(_1) -> [return: bb1, unwind unreachable]; } bb1: { -- StorageDead(_3); + StorageDead(_3); StorageDead(_2); StorageLive(_4); _4 = _1; StorageLive(_5); -- StorageLive(_6); + StorageLive(_6); - _6 = _4; - _5 = opaque::<&str>(move _6) -> [return: bb2, unwind unreachable]; ++ _6 = _1; + _5 = opaque::<&str>(_1) -> [return: bb2, unwind unreachable]; } bb2: { -- StorageDead(_6); + StorageDead(_6); StorageDead(_5); -- StorageLive(_7); + StorageLive(_7); StorageLive(_8); StorageLive(_9); StorageLive(_10); @@ -149,22 +151,23 @@ bb5: { StorageDead(_19); StorageDead(_18); -- _7 = const (); + _7 = const (); StorageDead(_17); StorageDead(_16); StorageDead(_15); StorageDead(_13); StorageDead(_10); StorageDead(_8); -- StorageDead(_7); + StorageDead(_7); - StorageLive(_29); ++ nop; StorageLive(_30); _30 = &(*_1); _29 = move _30 as &[u8] (Transmute); StorageDead(_30); StorageLive(_31); -- StorageLive(_32); -- _32 = _29; + StorageLive(_32); + _32 = _29; - _31 = opaque::<&[u8]>(move _32) -> [return: bb7, unwind unreachable]; + _31 = opaque::<&[u8]>(_29) -> [return: bb7, unwind unreachable]; } @@ -173,10 +176,12 @@ StorageDead(_19); StorageDead(_18); - StorageLive(_21); ++ nop; _21 = core::panicking::AssertKind::Eq; StorageLive(_22); -- StorageLive(_23); + StorageLive(_23); - _23 = move _21; ++ _23 = _21; StorageLive(_24); StorageLive(_25); _25 = &(*_15); @@ -192,9 +197,9 @@ } bb7: { -- StorageDead(_32); + StorageDead(_32); StorageDead(_31); -- StorageLive(_33); + StorageLive(_33); StorageLive(_34); StorageLive(_35); StorageLive(_36); @@ -235,18 +240,20 @@ bb10: { StorageDead(_45); StorageDead(_44); -- _33 = const (); + _33 = const (); StorageDead(_43); StorageDead(_42); StorageDead(_41); StorageDead(_39); StorageDead(_36); StorageDead(_34); -- StorageDead(_33); + StorageDead(_33); _0 = const (); - StorageDead(_29); ++ nop; StorageDead(_4); - StorageDead(_1); ++ nop; return; } @@ -254,10 +261,12 @@ StorageDead(_45); StorageDead(_44); - StorageLive(_47); ++ nop; _47 = core::panicking::AssertKind::Eq; StorageLive(_48); -- StorageLive(_49); + StorageLive(_49); - _49 = move _47; ++ _49 = _47; StorageLive(_50); StorageLive(_51); _51 = &(*_41); diff --git a/tests/mir-opt/gvn.slices.GVN.panic-unwind.diff b/tests/mir-opt/gvn.slices.GVN.panic-unwind.diff index f22bb25436f05..63225aa0a1016 100644 --- a/tests/mir-opt/gvn.slices.GVN.panic-unwind.diff +++ b/tests/mir-opt/gvn.slices.GVN.panic-unwind.diff @@ -85,30 +85,32 @@ bb0: { - StorageLive(_1); ++ nop; _1 = const "my favourite slice"; StorageLive(_2); -- StorageLive(_3); -- _3 = _1; + StorageLive(_3); + _3 = _1; - _2 = opaque::<&str>(move _3) -> [return: bb1, unwind continue]; + _2 = opaque::<&str>(_1) -> [return: bb1, unwind continue]; } bb1: { -- StorageDead(_3); + StorageDead(_3); StorageDead(_2); StorageLive(_4); _4 = _1; StorageLive(_5); -- StorageLive(_6); + StorageLive(_6); - _6 = _4; - _5 = opaque::<&str>(move _6) -> [return: bb2, unwind continue]; ++ _6 = _1; + _5 = opaque::<&str>(_1) -> [return: bb2, unwind continue]; } bb2: { -- StorageDead(_6); + StorageDead(_6); StorageDead(_5); -- StorageLive(_7); + StorageLive(_7); StorageLive(_8); StorageLive(_9); StorageLive(_10); @@ -149,22 +151,23 @@ bb5: { StorageDead(_19); StorageDead(_18); -- _7 = const (); + _7 = const (); StorageDead(_17); StorageDead(_16); StorageDead(_15); StorageDead(_13); StorageDead(_10); StorageDead(_8); -- StorageDead(_7); + StorageDead(_7); - StorageLive(_29); ++ nop; StorageLive(_30); _30 = &(*_1); _29 = move _30 as &[u8] (Transmute); StorageDead(_30); StorageLive(_31); -- StorageLive(_32); -- _32 = _29; + StorageLive(_32); + _32 = _29; - _31 = opaque::<&[u8]>(move _32) -> [return: bb7, unwind continue]; + _31 = opaque::<&[u8]>(_29) -> [return: bb7, unwind continue]; } @@ -173,10 +176,12 @@ StorageDead(_19); StorageDead(_18); - StorageLive(_21); ++ nop; _21 = core::panicking::AssertKind::Eq; StorageLive(_22); -- StorageLive(_23); + StorageLive(_23); - _23 = move _21; ++ _23 = _21; StorageLive(_24); StorageLive(_25); _25 = &(*_15); @@ -192,9 +197,9 @@ } bb7: { -- StorageDead(_32); + StorageDead(_32); StorageDead(_31); -- StorageLive(_33); + StorageLive(_33); StorageLive(_34); StorageLive(_35); StorageLive(_36); @@ -235,18 +240,20 @@ bb10: { StorageDead(_45); StorageDead(_44); -- _33 = const (); + _33 = const (); StorageDead(_43); StorageDead(_42); StorageDead(_41); StorageDead(_39); StorageDead(_36); StorageDead(_34); -- StorageDead(_33); + StorageDead(_33); _0 = const (); - StorageDead(_29); ++ nop; StorageDead(_4); - StorageDead(_1); ++ nop; return; } @@ -254,10 +261,12 @@ StorageDead(_45); StorageDead(_44); - StorageLive(_47); ++ nop; _47 = core::panicking::AssertKind::Eq; StorageLive(_48); -- StorageLive(_49); + StorageLive(_49); - _49 = move _47; ++ _49 = _47; StorageLive(_50); StorageLive(_51); _51 = &(*_41); diff --git a/tests/mir-opt/gvn.subexpression_elimination.GVN.panic-abort.diff b/tests/mir-opt/gvn.subexpression_elimination.GVN.panic-abort.diff index bf866e2f4d22f..6c8513e32816a 100644 --- a/tests/mir-opt/gvn.subexpression_elimination.GVN.panic-abort.diff +++ b/tests/mir-opt/gvn.subexpression_elimination.GVN.panic-abort.diff @@ -197,61 +197,68 @@ bb0: { StorageLive(_4); - StorageLive(_5); -- StorageLive(_6); -- _6 = _1; -- StorageLive(_7); -- _7 = _2; ++ nop; + StorageLive(_6); + _6 = _1; + StorageLive(_7); + _7 = _2; - _5 = Add(move _6, move _7); -- StorageDead(_7); -- StorageDead(_6); -- _4 = opaque::(move _5) -> [return: bb1, unwind unreachable]; + _5 = Add(_1, _2); + StorageDead(_7); + StorageDead(_6); +- _4 = opaque::(move _5) -> [return: bb1, unwind unreachable]; + _4 = opaque::(_5) -> [return: bb1, unwind unreachable]; } bb1: { - StorageDead(_5); ++ nop; StorageDead(_4); StorageLive(_8); - StorageLive(_9); -- StorageLive(_10); -- _10 = _1; -- StorageLive(_11); -- _11 = _2; ++ nop; + StorageLive(_10); + _10 = _1; + StorageLive(_11); + _11 = _2; - _9 = Mul(move _10, move _11); -- StorageDead(_11); -- StorageDead(_10); -- _8 = opaque::(move _9) -> [return: bb2, unwind unreachable]; + _9 = Mul(_1, _2); + StorageDead(_11); + StorageDead(_10); +- _8 = opaque::(move _9) -> [return: bb2, unwind unreachable]; + _8 = opaque::(_9) -> [return: bb2, unwind unreachable]; } bb2: { - StorageDead(_9); ++ nop; StorageDead(_8); StorageLive(_12); - StorageLive(_13); -- StorageLive(_14); -- _14 = _1; -- StorageLive(_15); -- _15 = _2; ++ nop; + StorageLive(_14); + _14 = _1; + StorageLive(_15); + _15 = _2; - _13 = Sub(move _14, move _15); -- StorageDead(_15); -- StorageDead(_14); -- _12 = opaque::(move _13) -> [return: bb3, unwind unreachable]; + _13 = Sub(_1, _2); + StorageDead(_15); + StorageDead(_14); +- _12 = opaque::(move _13) -> [return: bb3, unwind unreachable]; + _12 = opaque::(_13) -> [return: bb3, unwind unreachable]; } bb3: { - StorageDead(_13); ++ nop; StorageDead(_12); StorageLive(_16); - StorageLive(_17); -- StorageLive(_18); -- _18 = _1; -- StorageLive(_19); -- _19 = _2; ++ nop; + StorageLive(_18); + _18 = _1; + StorageLive(_19); + _19 = _2; - _20 = Eq(_19, const 0_u64); - assert(!move _20, "attempt to divide `{}` by zero", _18) -> [success: bb4, unwind unreachable]; + _20 = Eq(_2, const 0_u64); @@ -260,131 +267,145 @@ bb4: { - _17 = Div(move _18, move _19); -- StorageDead(_19); -- StorageDead(_18); -- _16 = opaque::(move _17) -> [return: bb5, unwind unreachable]; + _17 = Div(_1, _2); + StorageDead(_19); + StorageDead(_18); +- _16 = opaque::(move _17) -> [return: bb5, unwind unreachable]; + _16 = opaque::(_17) -> [return: bb5, unwind unreachable]; } bb5: { - StorageDead(_17); ++ nop; StorageDead(_16); StorageLive(_21); - StorageLive(_22); -- StorageLive(_23); -- _23 = _1; -- StorageLive(_24); -- _24 = _2; ++ nop; + StorageLive(_23); + _23 = _1; + StorageLive(_24); + _24 = _2; - _25 = Eq(_24, const 0_u64); - assert(!move _25, "attempt to calculate the remainder of `{}` with a divisor of zero", _23) -> [success: bb6, unwind unreachable]; ++ _25 = _20; + assert(!_20, "attempt to calculate the remainder of `{}` with a divisor of zero", _1) -> [success: bb6, unwind unreachable]; } bb6: { - _22 = Rem(move _23, move _24); -- StorageDead(_24); -- StorageDead(_23); -- _21 = opaque::(move _22) -> [return: bb7, unwind unreachable]; + _22 = Rem(_1, _2); + StorageDead(_24); + StorageDead(_23); +- _21 = opaque::(move _22) -> [return: bb7, unwind unreachable]; + _21 = opaque::(_22) -> [return: bb7, unwind unreachable]; } bb7: { - StorageDead(_22); ++ nop; StorageDead(_21); StorageLive(_26); - StorageLive(_27); -- StorageLive(_28); -- _28 = _1; -- StorageLive(_29); -- _29 = _2; ++ nop; + StorageLive(_28); + _28 = _1; + StorageLive(_29); + _29 = _2; - _27 = BitAnd(move _28, move _29); -- StorageDead(_29); -- StorageDead(_28); -- _26 = opaque::(move _27) -> [return: bb8, unwind unreachable]; + _27 = BitAnd(_1, _2); + StorageDead(_29); + StorageDead(_28); +- _26 = opaque::(move _27) -> [return: bb8, unwind unreachable]; + _26 = opaque::(_27) -> [return: bb8, unwind unreachable]; } bb8: { - StorageDead(_27); ++ nop; StorageDead(_26); StorageLive(_30); - StorageLive(_31); -- StorageLive(_32); -- _32 = _1; -- StorageLive(_33); -- _33 = _2; ++ nop; + StorageLive(_32); + _32 = _1; + StorageLive(_33); + _33 = _2; - _31 = BitOr(move _32, move _33); -- StorageDead(_33); -- StorageDead(_32); -- _30 = opaque::(move _31) -> [return: bb9, unwind unreachable]; + _31 = BitOr(_1, _2); + StorageDead(_33); + StorageDead(_32); +- _30 = opaque::(move _31) -> [return: bb9, unwind unreachable]; + _30 = opaque::(_31) -> [return: bb9, unwind unreachable]; } bb9: { - StorageDead(_31); ++ nop; StorageDead(_30); StorageLive(_34); - StorageLive(_35); -- StorageLive(_36); -- _36 = _1; -- StorageLive(_37); -- _37 = _2; ++ nop; + StorageLive(_36); + _36 = _1; + StorageLive(_37); + _37 = _2; - _35 = BitXor(move _36, move _37); -- StorageDead(_37); -- StorageDead(_36); -- _34 = opaque::(move _35) -> [return: bb10, unwind unreachable]; + _35 = BitXor(_1, _2); + StorageDead(_37); + StorageDead(_36); +- _34 = opaque::(move _35) -> [return: bb10, unwind unreachable]; + _34 = opaque::(_35) -> [return: bb10, unwind unreachable]; } bb10: { - StorageDead(_35); ++ nop; StorageDead(_34); StorageLive(_38); - StorageLive(_39); -- StorageLive(_40); -- _40 = _1; -- StorageLive(_41); -- _41 = _2; ++ nop; + StorageLive(_40); + _40 = _1; + StorageLive(_41); + _41 = _2; - _39 = Shl(move _40, move _41); -- StorageDead(_41); -- StorageDead(_40); -- _38 = opaque::(move _39) -> [return: bb11, unwind unreachable]; + _39 = Shl(_1, _2); + StorageDead(_41); + StorageDead(_40); +- _38 = opaque::(move _39) -> [return: bb11, unwind unreachable]; + _38 = opaque::(_39) -> [return: bb11, unwind unreachable]; } bb11: { - StorageDead(_39); ++ nop; StorageDead(_38); StorageLive(_42); - StorageLive(_43); -- StorageLive(_44); -- _44 = _1; -- StorageLive(_45); -- _45 = _2; ++ nop; + StorageLive(_44); + _44 = _1; + StorageLive(_45); + _45 = _2; - _43 = Shr(move _44, move _45); -- StorageDead(_45); -- StorageDead(_44); -- _42 = opaque::(move _43) -> [return: bb12, unwind unreachable]; + _43 = Shr(_1, _2); + StorageDead(_45); + StorageDead(_44); +- _42 = opaque::(move _43) -> [return: bb12, unwind unreachable]; + _42 = opaque::(_43) -> [return: bb12, unwind unreachable]; } bb12: { - StorageDead(_43); ++ nop; StorageDead(_42); StorageLive(_46); StorageLive(_47); -- StorageLive(_48); -- _48 = _1; + StorageLive(_48); + _48 = _1; - _47 = move _48 as u32 (IntToInt); -- StorageDead(_48); + _47 = _1 as u32 (IntToInt); + StorageDead(_48); _46 = opaque::(move _47) -> [return: bb13, unwind unreachable]; } @@ -393,11 +414,11 @@ StorageDead(_46); StorageLive(_49); StorageLive(_50); -- StorageLive(_51); -- _51 = _1; + StorageLive(_51); + _51 = _1; - _50 = move _51 as f32 (IntToFloat); -- StorageDead(_51); + _50 = _1 as f32 (IntToFloat); + StorageDead(_51); _49 = opaque::(move _50) -> [return: bb14, unwind unreachable]; } @@ -406,25 +427,29 @@ StorageDead(_49); StorageLive(_52); - StorageLive(_53); -- StorageLive(_54); -- _54 = _1; ++ nop; + StorageLive(_54); + _54 = _1; - _53 = S::(move _54); -- StorageDead(_54); -- _52 = opaque::>(move _53) -> [return: bb15, unwind unreachable]; + _53 = S::(_1); + StorageDead(_54); +- _52 = opaque::>(move _53) -> [return: bb15, unwind unreachable]; + _52 = opaque::>(_53) -> [return: bb15, unwind unreachable]; } bb15: { - StorageDead(_53); ++ nop; StorageDead(_52); StorageLive(_55); - StorageLive(_56); -- StorageLive(_57); -- StorageLive(_58); -- _58 = _1; ++ nop; + StorageLive(_57); + StorageLive(_58); + _58 = _1; - _57 = S::(move _58); -- StorageDead(_58); ++ _57 = _53; + StorageDead(_58); - _56 = (_57.0: u64); - _55 = opaque::(move _56) -> [return: bb16, unwind unreachable]; + _56 = (_53.0: u64); @@ -433,24 +458,26 @@ bb16: { - StorageDead(_56); -- StorageDead(_57); ++ nop; + StorageDead(_57); StorageDead(_55); StorageLive(_59); StorageLive(_60); -- StorageLive(_61); -- StorageLive(_62); -- _62 = _1; -- StorageLive(_63); -- _63 = _2; + StorageLive(_61); + StorageLive(_62); + _62 = _1; + StorageLive(_63); + _63 = _2; - _61 = Add(move _62, move _63); -- StorageDead(_63); -- StorageDead(_62); ++ _61 = _5; + StorageDead(_63); + StorageDead(_62); StorageLive(_64); _64 = _3; - _60 = Add(move _61, move _64); + _60 = Add(_5, move _64); StorageDead(_64); -- StorageDead(_61); + StorageDead(_61); _59 = opaque::(move _60) -> [return: bb17, unwind unreachable]; } @@ -459,20 +486,21 @@ StorageDead(_59); StorageLive(_65); StorageLive(_66); -- StorageLive(_67); -- StorageLive(_68); -- _68 = _1; -- StorageLive(_69); -- _69 = _2; + StorageLive(_67); + StorageLive(_68); + _68 = _1; + StorageLive(_69); + _69 = _2; - _67 = Mul(move _68, move _69); -- StorageDead(_69); -- StorageDead(_68); ++ _67 = _9; + StorageDead(_69); + StorageDead(_68); StorageLive(_70); _70 = _3; - _66 = Add(move _67, move _70); + _66 = Add(_9, move _70); StorageDead(_70); -- StorageDead(_67); + StorageDead(_67); _65 = opaque::(move _66) -> [return: bb18, unwind unreachable]; } @@ -481,20 +509,21 @@ StorageDead(_65); StorageLive(_71); StorageLive(_72); -- StorageLive(_73); -- StorageLive(_74); -- _74 = _1; -- StorageLive(_75); -- _75 = _2; + StorageLive(_73); + StorageLive(_74); + _74 = _1; + StorageLive(_75); + _75 = _2; - _73 = Sub(move _74, move _75); -- StorageDead(_75); -- StorageDead(_74); ++ _73 = _13; + StorageDead(_75); + StorageDead(_74); StorageLive(_76); _76 = _3; - _72 = Add(move _73, move _76); + _72 = Add(_13, move _76); StorageDead(_76); -- StorageDead(_73); + StorageDead(_73); _71 = opaque::(move _72) -> [return: bb19, unwind unreachable]; } @@ -503,26 +532,28 @@ StorageDead(_71); StorageLive(_77); StorageLive(_78); -- StorageLive(_79); -- StorageLive(_80); -- _80 = _1; -- StorageLive(_81); -- _81 = _2; + StorageLive(_79); + StorageLive(_80); + _80 = _1; + StorageLive(_81); + _81 = _2; - _82 = Eq(_81, const 0_u64); - assert(!move _82, "attempt to divide `{}` by zero", _80) -> [success: bb20, unwind unreachable]; ++ _82 = _20; + assert(!_20, "attempt to divide `{}` by zero", _1) -> [success: bb20, unwind unreachable]; } bb20: { - _79 = Div(move _80, move _81); -- StorageDead(_81); -- StorageDead(_80); ++ _79 = _17; + StorageDead(_81); + StorageDead(_80); StorageLive(_83); _83 = _3; - _78 = Add(move _79, move _83); + _78 = Add(_17, move _83); StorageDead(_83); -- StorageDead(_79); + StorageDead(_79); _77 = opaque::(move _78) -> [return: bb21, unwind unreachable]; } @@ -531,26 +562,28 @@ StorageDead(_77); StorageLive(_84); StorageLive(_85); -- StorageLive(_86); -- StorageLive(_87); -- _87 = _1; -- StorageLive(_88); -- _88 = _2; + StorageLive(_86); + StorageLive(_87); + _87 = _1; + StorageLive(_88); + _88 = _2; - _89 = Eq(_88, const 0_u64); - assert(!move _89, "attempt to calculate the remainder of `{}` with a divisor of zero", _87) -> [success: bb22, unwind unreachable]; ++ _89 = _20; + assert(!_20, "attempt to calculate the remainder of `{}` with a divisor of zero", _1) -> [success: bb22, unwind unreachable]; } bb22: { - _86 = Rem(move _87, move _88); -- StorageDead(_88); -- StorageDead(_87); ++ _86 = _22; + StorageDead(_88); + StorageDead(_87); StorageLive(_90); _90 = _3; - _85 = Add(move _86, move _90); + _85 = Add(_22, move _90); StorageDead(_90); -- StorageDead(_86); + StorageDead(_86); _84 = opaque::(move _85) -> [return: bb23, unwind unreachable]; } @@ -559,20 +592,21 @@ StorageDead(_84); StorageLive(_91); StorageLive(_92); -- StorageLive(_93); -- StorageLive(_94); -- _94 = _1; -- StorageLive(_95); -- _95 = _2; + StorageLive(_93); + StorageLive(_94); + _94 = _1; + StorageLive(_95); + _95 = _2; - _93 = BitAnd(move _94, move _95); -- StorageDead(_95); -- StorageDead(_94); ++ _93 = _27; + StorageDead(_95); + StorageDead(_94); StorageLive(_96); _96 = _3; - _92 = Add(move _93, move _96); + _92 = Add(_27, move _96); StorageDead(_96); -- StorageDead(_93); + StorageDead(_93); _91 = opaque::(move _92) -> [return: bb24, unwind unreachable]; } @@ -581,20 +615,21 @@ StorageDead(_91); StorageLive(_97); StorageLive(_98); -- StorageLive(_99); -- StorageLive(_100); -- _100 = _1; -- StorageLive(_101); -- _101 = _2; + StorageLive(_99); + StorageLive(_100); + _100 = _1; + StorageLive(_101); + _101 = _2; - _99 = BitOr(move _100, move _101); -- StorageDead(_101); -- StorageDead(_100); ++ _99 = _31; + StorageDead(_101); + StorageDead(_100); StorageLive(_102); _102 = _3; - _98 = Add(move _99, move _102); + _98 = Add(_31, move _102); StorageDead(_102); -- StorageDead(_99); + StorageDead(_99); _97 = opaque::(move _98) -> [return: bb25, unwind unreachable]; } @@ -603,20 +638,21 @@ StorageDead(_97); StorageLive(_103); StorageLive(_104); -- StorageLive(_105); -- StorageLive(_106); -- _106 = _1; -- StorageLive(_107); -- _107 = _2; + StorageLive(_105); + StorageLive(_106); + _106 = _1; + StorageLive(_107); + _107 = _2; - _105 = BitXor(move _106, move _107); -- StorageDead(_107); -- StorageDead(_106); ++ _105 = _35; + StorageDead(_107); + StorageDead(_106); StorageLive(_108); _108 = _3; - _104 = Add(move _105, move _108); + _104 = Add(_35, move _108); StorageDead(_108); -- StorageDead(_105); + StorageDead(_105); _103 = opaque::(move _104) -> [return: bb26, unwind unreachable]; } @@ -625,20 +661,21 @@ StorageDead(_103); StorageLive(_109); StorageLive(_110); -- StorageLive(_111); -- StorageLive(_112); -- _112 = _1; -- StorageLive(_113); -- _113 = _2; + StorageLive(_111); + StorageLive(_112); + _112 = _1; + StorageLive(_113); + _113 = _2; - _111 = Shl(move _112, move _113); -- StorageDead(_113); -- StorageDead(_112); ++ _111 = _39; + StorageDead(_113); + StorageDead(_112); StorageLive(_114); _114 = _3; - _110 = Add(move _111, move _114); + _110 = Add(_39, move _114); StorageDead(_114); -- StorageDead(_111); + StorageDead(_111); _109 = opaque::(move _110) -> [return: bb27, unwind unreachable]; } @@ -647,20 +684,21 @@ StorageDead(_109); StorageLive(_115); StorageLive(_116); -- StorageLive(_117); -- StorageLive(_118); -- _118 = _1; -- StorageLive(_119); -- _119 = _2; + StorageLive(_117); + StorageLive(_118); + _118 = _1; + StorageLive(_119); + _119 = _2; - _117 = Shr(move _118, move _119); -- StorageDead(_119); -- StorageDead(_118); ++ _117 = _43; + StorageDead(_119); + StorageDead(_118); StorageLive(_120); _120 = _3; - _116 = Add(move _117, move _120); + _116 = Add(_43, move _120); StorageDead(_120); -- StorageDead(_117); + StorageDead(_117); _115 = opaque::(move _116) -> [return: bb28, unwind unreachable]; } @@ -668,68 +706,77 @@ StorageDead(_116); StorageDead(_115); StorageLive(_121); -- StorageLive(_122); -- StorageLive(_123); -- _123 = _1; + StorageLive(_122); + StorageLive(_123); + _123 = _1; - _122 = S::(move _123); -- StorageDead(_123); ++ _122 = _53; + StorageDead(_123); - _121 = opaque::>(move _122) -> [return: bb29, unwind unreachable]; + _121 = opaque::>(_53) -> [return: bb29, unwind unreachable]; } bb29: { -- StorageDead(_122); + StorageDead(_122); StorageDead(_121); StorageLive(_124); -- StorageLive(_125); -- StorageLive(_126); -- StorageLive(_127); -- _127 = _1; + StorageLive(_125); + StorageLive(_126); + StorageLive(_127); + _127 = _1; - _126 = S::(move _127); -- StorageDead(_127); ++ _126 = _53; + StorageDead(_127); - _125 = (_126.0: u64); - _124 = opaque::(move _125) -> [return: bb30, unwind unreachable]; ++ _125 = _56; + _124 = opaque::(_56) -> [return: bb30, unwind unreachable]; } bb30: { -- StorageDead(_125); -- StorageDead(_126); + StorageDead(_125); + StorageDead(_126); StorageDead(_124); StorageLive(_128); _128 = &_3; StorageLive(_129); - StorageLive(_130); - StorageLive(_131); ++ nop; ++ nop; _131 = (*_128); -- StorageLive(_132); -- _132 = _1; + StorageLive(_132); + _132 = _1; - _130 = Add(move _131, move _132); -- StorageDead(_132); ++ _130 = Add(_131, _1); + StorageDead(_132); - StorageDead(_131); - _129 = opaque::(move _130) -> [return: bb31, unwind unreachable]; -+ _130 = Add(_131, _1); ++ nop; + _129 = opaque::(_130) -> [return: bb31, unwind unreachable]; } bb31: { - StorageDead(_130); ++ nop; StorageDead(_129); StorageLive(_133); -- StorageLive(_134); -- StorageLive(_135); + StorageLive(_134); + StorageLive(_135); - _135 = (*_128); -- StorageLive(_136); -- _136 = _1; ++ _135 = _131; + StorageLive(_136); + _136 = _1; - _134 = Add(move _135, move _136); -- StorageDead(_136); -- StorageDead(_135); ++ _134 = _130; + StorageDead(_136); + StorageDead(_135); - _133 = opaque::(move _134) -> [return: bb32, unwind unreachable]; + _133 = opaque::(_130) -> [return: bb32, unwind unreachable]; } bb32: { -- StorageDead(_134); + StorageDead(_134); StorageDead(_133); StorageLive(_137); _137 = &mut _3; @@ -737,11 +784,11 @@ StorageLive(_139); StorageLive(_140); _140 = (*_137); -- StorageLive(_141); -- _141 = _1; + StorageLive(_141); + _141 = _1; - _139 = Add(move _140, move _141); -- StorageDead(_141); + _139 = Add(move _140, _1); + StorageDead(_141); StorageDead(_140); _138 = opaque::(move _139) -> [return: bb33, unwind unreachable]; } @@ -753,11 +800,11 @@ StorageLive(_143); StorageLive(_144); _144 = (*_137); -- StorageLive(_145); -- _145 = _1; + StorageLive(_145); + _145 = _1; - _143 = Add(move _144, move _145); -- StorageDead(_145); + _143 = Add(move _144, _1); + StorageDead(_145); StorageDead(_144); _142 = opaque::(move _143) -> [return: bb34, unwind unreachable]; } @@ -765,18 +812,18 @@ bb34: { StorageDead(_143); StorageDead(_142); -- StorageLive(_146); + StorageLive(_146); StorageLive(_147); _147 = &raw const _3; StorageLive(_148); StorageLive(_149); StorageLive(_150); _150 = (*_147); -- StorageLive(_151); -- _151 = _1; + StorageLive(_151); + _151 = _1; - _149 = Add(move _150, move _151); -- StorageDead(_151); + _149 = Add(move _150, _1); + StorageDead(_151); StorageDead(_150); _148 = opaque::(move _149) -> [return: bb35, unwind unreachable]; } @@ -788,11 +835,11 @@ StorageLive(_153); StorageLive(_154); _154 = (*_147); -- StorageLive(_155); -- _155 = _1; + StorageLive(_155); + _155 = _1; - _153 = Add(move _154, move _155); -- StorageDead(_155); + _153 = Add(move _154, _1); + StorageDead(_155); StorageDead(_154); _152 = opaque::(move _153) -> [return: bb36, unwind unreachable]; } @@ -806,11 +853,11 @@ StorageLive(_158); StorageLive(_159); _159 = (*_156); -- StorageLive(_160); -- _160 = _1; + StorageLive(_160); + _160 = _1; - _158 = Add(move _159, move _160); -- StorageDead(_160); + _158 = Add(move _159, _1); + StorageDead(_160); StorageDead(_159); _157 = opaque::(move _158) -> [return: bb37, unwind unreachable]; } @@ -822,11 +869,11 @@ StorageLive(_162); StorageLive(_163); _163 = (*_156); -- StorageLive(_164); -- _164 = _1; + StorageLive(_164); + _164 = _1; - _162 = Add(move _163, move _164); -- StorageDead(_164); + _162 = Add(move _163, _1); + StorageDead(_164); StorageDead(_163); _161 = opaque::(move _162) -> [return: bb38, unwind unreachable]; } @@ -834,44 +881,50 @@ bb38: { StorageDead(_162); StorageDead(_161); -- _146 = const (); + _146 = const (); StorageDead(_156); StorageDead(_147); -- StorageDead(_146); + StorageDead(_146); StorageLive(_165); _165 = &_3; StorageLive(_166); - StorageLive(_167); - StorageLive(_168); ++ nop; ++ nop; _168 = (*_165); -- StorageLive(_169); -- _169 = _1; + StorageLive(_169); + _169 = _1; - _167 = Add(move _168, move _169); -- StorageDead(_169); ++ _167 = Add(_168, _1); + StorageDead(_169); - StorageDead(_168); - _166 = opaque::(move _167) -> [return: bb39, unwind unreachable]; -+ _167 = Add(_168, _1); ++ nop; + _166 = opaque::(_167) -> [return: bb39, unwind unreachable]; } bb39: { - StorageDead(_167); ++ nop; StorageDead(_166); StorageLive(_170); -- StorageLive(_171); -- StorageLive(_172); + StorageLive(_171); + StorageLive(_172); - _172 = (*_165); -- StorageLive(_173); -- _173 = _1; ++ _172 = _168; + StorageLive(_173); + _173 = _1; - _171 = Add(move _172, move _173); -- StorageDead(_173); -- StorageDead(_172); ++ _171 = _167; + StorageDead(_173); + StorageDead(_172); - _170 = opaque::(move _171) -> [return: bb40, unwind unreachable]; + _170 = opaque::(_167) -> [return: bb40, unwind unreachable]; } bb40: { -- StorageDead(_171); + StorageDead(_171); StorageDead(_170); _0 = const (); StorageDead(_165); diff --git a/tests/mir-opt/gvn.subexpression_elimination.GVN.panic-unwind.diff b/tests/mir-opt/gvn.subexpression_elimination.GVN.panic-unwind.diff index 68b052907192d..fb2c089827deb 100644 --- a/tests/mir-opt/gvn.subexpression_elimination.GVN.panic-unwind.diff +++ b/tests/mir-opt/gvn.subexpression_elimination.GVN.panic-unwind.diff @@ -197,61 +197,68 @@ bb0: { StorageLive(_4); - StorageLive(_5); -- StorageLive(_6); -- _6 = _1; -- StorageLive(_7); -- _7 = _2; ++ nop; + StorageLive(_6); + _6 = _1; + StorageLive(_7); + _7 = _2; - _5 = Add(move _6, move _7); -- StorageDead(_7); -- StorageDead(_6); -- _4 = opaque::(move _5) -> [return: bb1, unwind continue]; + _5 = Add(_1, _2); + StorageDead(_7); + StorageDead(_6); +- _4 = opaque::(move _5) -> [return: bb1, unwind continue]; + _4 = opaque::(_5) -> [return: bb1, unwind continue]; } bb1: { - StorageDead(_5); ++ nop; StorageDead(_4); StorageLive(_8); - StorageLive(_9); -- StorageLive(_10); -- _10 = _1; -- StorageLive(_11); -- _11 = _2; ++ nop; + StorageLive(_10); + _10 = _1; + StorageLive(_11); + _11 = _2; - _9 = Mul(move _10, move _11); -- StorageDead(_11); -- StorageDead(_10); -- _8 = opaque::(move _9) -> [return: bb2, unwind continue]; + _9 = Mul(_1, _2); + StorageDead(_11); + StorageDead(_10); +- _8 = opaque::(move _9) -> [return: bb2, unwind continue]; + _8 = opaque::(_9) -> [return: bb2, unwind continue]; } bb2: { - StorageDead(_9); ++ nop; StorageDead(_8); StorageLive(_12); - StorageLive(_13); -- StorageLive(_14); -- _14 = _1; -- StorageLive(_15); -- _15 = _2; ++ nop; + StorageLive(_14); + _14 = _1; + StorageLive(_15); + _15 = _2; - _13 = Sub(move _14, move _15); -- StorageDead(_15); -- StorageDead(_14); -- _12 = opaque::(move _13) -> [return: bb3, unwind continue]; + _13 = Sub(_1, _2); + StorageDead(_15); + StorageDead(_14); +- _12 = opaque::(move _13) -> [return: bb3, unwind continue]; + _12 = opaque::(_13) -> [return: bb3, unwind continue]; } bb3: { - StorageDead(_13); ++ nop; StorageDead(_12); StorageLive(_16); - StorageLive(_17); -- StorageLive(_18); -- _18 = _1; -- StorageLive(_19); -- _19 = _2; ++ nop; + StorageLive(_18); + _18 = _1; + StorageLive(_19); + _19 = _2; - _20 = Eq(_19, const 0_u64); - assert(!move _20, "attempt to divide `{}` by zero", _18) -> [success: bb4, unwind continue]; + _20 = Eq(_2, const 0_u64); @@ -260,131 +267,145 @@ bb4: { - _17 = Div(move _18, move _19); -- StorageDead(_19); -- StorageDead(_18); -- _16 = opaque::(move _17) -> [return: bb5, unwind continue]; + _17 = Div(_1, _2); + StorageDead(_19); + StorageDead(_18); +- _16 = opaque::(move _17) -> [return: bb5, unwind continue]; + _16 = opaque::(_17) -> [return: bb5, unwind continue]; } bb5: { - StorageDead(_17); ++ nop; StorageDead(_16); StorageLive(_21); - StorageLive(_22); -- StorageLive(_23); -- _23 = _1; -- StorageLive(_24); -- _24 = _2; ++ nop; + StorageLive(_23); + _23 = _1; + StorageLive(_24); + _24 = _2; - _25 = Eq(_24, const 0_u64); - assert(!move _25, "attempt to calculate the remainder of `{}` with a divisor of zero", _23) -> [success: bb6, unwind continue]; ++ _25 = _20; + assert(!_20, "attempt to calculate the remainder of `{}` with a divisor of zero", _1) -> [success: bb6, unwind continue]; } bb6: { - _22 = Rem(move _23, move _24); -- StorageDead(_24); -- StorageDead(_23); -- _21 = opaque::(move _22) -> [return: bb7, unwind continue]; + _22 = Rem(_1, _2); + StorageDead(_24); + StorageDead(_23); +- _21 = opaque::(move _22) -> [return: bb7, unwind continue]; + _21 = opaque::(_22) -> [return: bb7, unwind continue]; } bb7: { - StorageDead(_22); ++ nop; StorageDead(_21); StorageLive(_26); - StorageLive(_27); -- StorageLive(_28); -- _28 = _1; -- StorageLive(_29); -- _29 = _2; ++ nop; + StorageLive(_28); + _28 = _1; + StorageLive(_29); + _29 = _2; - _27 = BitAnd(move _28, move _29); -- StorageDead(_29); -- StorageDead(_28); -- _26 = opaque::(move _27) -> [return: bb8, unwind continue]; + _27 = BitAnd(_1, _2); + StorageDead(_29); + StorageDead(_28); +- _26 = opaque::(move _27) -> [return: bb8, unwind continue]; + _26 = opaque::(_27) -> [return: bb8, unwind continue]; } bb8: { - StorageDead(_27); ++ nop; StorageDead(_26); StorageLive(_30); - StorageLive(_31); -- StorageLive(_32); -- _32 = _1; -- StorageLive(_33); -- _33 = _2; ++ nop; + StorageLive(_32); + _32 = _1; + StorageLive(_33); + _33 = _2; - _31 = BitOr(move _32, move _33); -- StorageDead(_33); -- StorageDead(_32); -- _30 = opaque::(move _31) -> [return: bb9, unwind continue]; + _31 = BitOr(_1, _2); + StorageDead(_33); + StorageDead(_32); +- _30 = opaque::(move _31) -> [return: bb9, unwind continue]; + _30 = opaque::(_31) -> [return: bb9, unwind continue]; } bb9: { - StorageDead(_31); ++ nop; StorageDead(_30); StorageLive(_34); - StorageLive(_35); -- StorageLive(_36); -- _36 = _1; -- StorageLive(_37); -- _37 = _2; ++ nop; + StorageLive(_36); + _36 = _1; + StorageLive(_37); + _37 = _2; - _35 = BitXor(move _36, move _37); -- StorageDead(_37); -- StorageDead(_36); -- _34 = opaque::(move _35) -> [return: bb10, unwind continue]; + _35 = BitXor(_1, _2); + StorageDead(_37); + StorageDead(_36); +- _34 = opaque::(move _35) -> [return: bb10, unwind continue]; + _34 = opaque::(_35) -> [return: bb10, unwind continue]; } bb10: { - StorageDead(_35); ++ nop; StorageDead(_34); StorageLive(_38); - StorageLive(_39); -- StorageLive(_40); -- _40 = _1; -- StorageLive(_41); -- _41 = _2; ++ nop; + StorageLive(_40); + _40 = _1; + StorageLive(_41); + _41 = _2; - _39 = Shl(move _40, move _41); -- StorageDead(_41); -- StorageDead(_40); -- _38 = opaque::(move _39) -> [return: bb11, unwind continue]; + _39 = Shl(_1, _2); + StorageDead(_41); + StorageDead(_40); +- _38 = opaque::(move _39) -> [return: bb11, unwind continue]; + _38 = opaque::(_39) -> [return: bb11, unwind continue]; } bb11: { - StorageDead(_39); ++ nop; StorageDead(_38); StorageLive(_42); - StorageLive(_43); -- StorageLive(_44); -- _44 = _1; -- StorageLive(_45); -- _45 = _2; ++ nop; + StorageLive(_44); + _44 = _1; + StorageLive(_45); + _45 = _2; - _43 = Shr(move _44, move _45); -- StorageDead(_45); -- StorageDead(_44); -- _42 = opaque::(move _43) -> [return: bb12, unwind continue]; + _43 = Shr(_1, _2); + StorageDead(_45); + StorageDead(_44); +- _42 = opaque::(move _43) -> [return: bb12, unwind continue]; + _42 = opaque::(_43) -> [return: bb12, unwind continue]; } bb12: { - StorageDead(_43); ++ nop; StorageDead(_42); StorageLive(_46); StorageLive(_47); -- StorageLive(_48); -- _48 = _1; + StorageLive(_48); + _48 = _1; - _47 = move _48 as u32 (IntToInt); -- StorageDead(_48); + _47 = _1 as u32 (IntToInt); + StorageDead(_48); _46 = opaque::(move _47) -> [return: bb13, unwind continue]; } @@ -393,11 +414,11 @@ StorageDead(_46); StorageLive(_49); StorageLive(_50); -- StorageLive(_51); -- _51 = _1; + StorageLive(_51); + _51 = _1; - _50 = move _51 as f32 (IntToFloat); -- StorageDead(_51); + _50 = _1 as f32 (IntToFloat); + StorageDead(_51); _49 = opaque::(move _50) -> [return: bb14, unwind continue]; } @@ -406,25 +427,29 @@ StorageDead(_49); StorageLive(_52); - StorageLive(_53); -- StorageLive(_54); -- _54 = _1; ++ nop; + StorageLive(_54); + _54 = _1; - _53 = S::(move _54); -- StorageDead(_54); -- _52 = opaque::>(move _53) -> [return: bb15, unwind continue]; + _53 = S::(_1); + StorageDead(_54); +- _52 = opaque::>(move _53) -> [return: bb15, unwind continue]; + _52 = opaque::>(_53) -> [return: bb15, unwind continue]; } bb15: { - StorageDead(_53); ++ nop; StorageDead(_52); StorageLive(_55); - StorageLive(_56); -- StorageLive(_57); -- StorageLive(_58); -- _58 = _1; ++ nop; + StorageLive(_57); + StorageLive(_58); + _58 = _1; - _57 = S::(move _58); -- StorageDead(_58); ++ _57 = _53; + StorageDead(_58); - _56 = (_57.0: u64); - _55 = opaque::(move _56) -> [return: bb16, unwind continue]; + _56 = (_53.0: u64); @@ -433,24 +458,26 @@ bb16: { - StorageDead(_56); -- StorageDead(_57); ++ nop; + StorageDead(_57); StorageDead(_55); StorageLive(_59); StorageLive(_60); -- StorageLive(_61); -- StorageLive(_62); -- _62 = _1; -- StorageLive(_63); -- _63 = _2; + StorageLive(_61); + StorageLive(_62); + _62 = _1; + StorageLive(_63); + _63 = _2; - _61 = Add(move _62, move _63); -- StorageDead(_63); -- StorageDead(_62); ++ _61 = _5; + StorageDead(_63); + StorageDead(_62); StorageLive(_64); _64 = _3; - _60 = Add(move _61, move _64); + _60 = Add(_5, move _64); StorageDead(_64); -- StorageDead(_61); + StorageDead(_61); _59 = opaque::(move _60) -> [return: bb17, unwind continue]; } @@ -459,20 +486,21 @@ StorageDead(_59); StorageLive(_65); StorageLive(_66); -- StorageLive(_67); -- StorageLive(_68); -- _68 = _1; -- StorageLive(_69); -- _69 = _2; + StorageLive(_67); + StorageLive(_68); + _68 = _1; + StorageLive(_69); + _69 = _2; - _67 = Mul(move _68, move _69); -- StorageDead(_69); -- StorageDead(_68); ++ _67 = _9; + StorageDead(_69); + StorageDead(_68); StorageLive(_70); _70 = _3; - _66 = Add(move _67, move _70); + _66 = Add(_9, move _70); StorageDead(_70); -- StorageDead(_67); + StorageDead(_67); _65 = opaque::(move _66) -> [return: bb18, unwind continue]; } @@ -481,20 +509,21 @@ StorageDead(_65); StorageLive(_71); StorageLive(_72); -- StorageLive(_73); -- StorageLive(_74); -- _74 = _1; -- StorageLive(_75); -- _75 = _2; + StorageLive(_73); + StorageLive(_74); + _74 = _1; + StorageLive(_75); + _75 = _2; - _73 = Sub(move _74, move _75); -- StorageDead(_75); -- StorageDead(_74); ++ _73 = _13; + StorageDead(_75); + StorageDead(_74); StorageLive(_76); _76 = _3; - _72 = Add(move _73, move _76); + _72 = Add(_13, move _76); StorageDead(_76); -- StorageDead(_73); + StorageDead(_73); _71 = opaque::(move _72) -> [return: bb19, unwind continue]; } @@ -503,26 +532,28 @@ StorageDead(_71); StorageLive(_77); StorageLive(_78); -- StorageLive(_79); -- StorageLive(_80); -- _80 = _1; -- StorageLive(_81); -- _81 = _2; + StorageLive(_79); + StorageLive(_80); + _80 = _1; + StorageLive(_81); + _81 = _2; - _82 = Eq(_81, const 0_u64); - assert(!move _82, "attempt to divide `{}` by zero", _80) -> [success: bb20, unwind continue]; ++ _82 = _20; + assert(!_20, "attempt to divide `{}` by zero", _1) -> [success: bb20, unwind continue]; } bb20: { - _79 = Div(move _80, move _81); -- StorageDead(_81); -- StorageDead(_80); ++ _79 = _17; + StorageDead(_81); + StorageDead(_80); StorageLive(_83); _83 = _3; - _78 = Add(move _79, move _83); + _78 = Add(_17, move _83); StorageDead(_83); -- StorageDead(_79); + StorageDead(_79); _77 = opaque::(move _78) -> [return: bb21, unwind continue]; } @@ -531,26 +562,28 @@ StorageDead(_77); StorageLive(_84); StorageLive(_85); -- StorageLive(_86); -- StorageLive(_87); -- _87 = _1; -- StorageLive(_88); -- _88 = _2; + StorageLive(_86); + StorageLive(_87); + _87 = _1; + StorageLive(_88); + _88 = _2; - _89 = Eq(_88, const 0_u64); - assert(!move _89, "attempt to calculate the remainder of `{}` with a divisor of zero", _87) -> [success: bb22, unwind continue]; ++ _89 = _20; + assert(!_20, "attempt to calculate the remainder of `{}` with a divisor of zero", _1) -> [success: bb22, unwind continue]; } bb22: { - _86 = Rem(move _87, move _88); -- StorageDead(_88); -- StorageDead(_87); ++ _86 = _22; + StorageDead(_88); + StorageDead(_87); StorageLive(_90); _90 = _3; - _85 = Add(move _86, move _90); + _85 = Add(_22, move _90); StorageDead(_90); -- StorageDead(_86); + StorageDead(_86); _84 = opaque::(move _85) -> [return: bb23, unwind continue]; } @@ -559,20 +592,21 @@ StorageDead(_84); StorageLive(_91); StorageLive(_92); -- StorageLive(_93); -- StorageLive(_94); -- _94 = _1; -- StorageLive(_95); -- _95 = _2; + StorageLive(_93); + StorageLive(_94); + _94 = _1; + StorageLive(_95); + _95 = _2; - _93 = BitAnd(move _94, move _95); -- StorageDead(_95); -- StorageDead(_94); ++ _93 = _27; + StorageDead(_95); + StorageDead(_94); StorageLive(_96); _96 = _3; - _92 = Add(move _93, move _96); + _92 = Add(_27, move _96); StorageDead(_96); -- StorageDead(_93); + StorageDead(_93); _91 = opaque::(move _92) -> [return: bb24, unwind continue]; } @@ -581,20 +615,21 @@ StorageDead(_91); StorageLive(_97); StorageLive(_98); -- StorageLive(_99); -- StorageLive(_100); -- _100 = _1; -- StorageLive(_101); -- _101 = _2; + StorageLive(_99); + StorageLive(_100); + _100 = _1; + StorageLive(_101); + _101 = _2; - _99 = BitOr(move _100, move _101); -- StorageDead(_101); -- StorageDead(_100); ++ _99 = _31; + StorageDead(_101); + StorageDead(_100); StorageLive(_102); _102 = _3; - _98 = Add(move _99, move _102); + _98 = Add(_31, move _102); StorageDead(_102); -- StorageDead(_99); + StorageDead(_99); _97 = opaque::(move _98) -> [return: bb25, unwind continue]; } @@ -603,20 +638,21 @@ StorageDead(_97); StorageLive(_103); StorageLive(_104); -- StorageLive(_105); -- StorageLive(_106); -- _106 = _1; -- StorageLive(_107); -- _107 = _2; + StorageLive(_105); + StorageLive(_106); + _106 = _1; + StorageLive(_107); + _107 = _2; - _105 = BitXor(move _106, move _107); -- StorageDead(_107); -- StorageDead(_106); ++ _105 = _35; + StorageDead(_107); + StorageDead(_106); StorageLive(_108); _108 = _3; - _104 = Add(move _105, move _108); + _104 = Add(_35, move _108); StorageDead(_108); -- StorageDead(_105); + StorageDead(_105); _103 = opaque::(move _104) -> [return: bb26, unwind continue]; } @@ -625,20 +661,21 @@ StorageDead(_103); StorageLive(_109); StorageLive(_110); -- StorageLive(_111); -- StorageLive(_112); -- _112 = _1; -- StorageLive(_113); -- _113 = _2; + StorageLive(_111); + StorageLive(_112); + _112 = _1; + StorageLive(_113); + _113 = _2; - _111 = Shl(move _112, move _113); -- StorageDead(_113); -- StorageDead(_112); ++ _111 = _39; + StorageDead(_113); + StorageDead(_112); StorageLive(_114); _114 = _3; - _110 = Add(move _111, move _114); + _110 = Add(_39, move _114); StorageDead(_114); -- StorageDead(_111); + StorageDead(_111); _109 = opaque::(move _110) -> [return: bb27, unwind continue]; } @@ -647,20 +684,21 @@ StorageDead(_109); StorageLive(_115); StorageLive(_116); -- StorageLive(_117); -- StorageLive(_118); -- _118 = _1; -- StorageLive(_119); -- _119 = _2; + StorageLive(_117); + StorageLive(_118); + _118 = _1; + StorageLive(_119); + _119 = _2; - _117 = Shr(move _118, move _119); -- StorageDead(_119); -- StorageDead(_118); ++ _117 = _43; + StorageDead(_119); + StorageDead(_118); StorageLive(_120); _120 = _3; - _116 = Add(move _117, move _120); + _116 = Add(_43, move _120); StorageDead(_120); -- StorageDead(_117); + StorageDead(_117); _115 = opaque::(move _116) -> [return: bb28, unwind continue]; } @@ -668,68 +706,77 @@ StorageDead(_116); StorageDead(_115); StorageLive(_121); -- StorageLive(_122); -- StorageLive(_123); -- _123 = _1; + StorageLive(_122); + StorageLive(_123); + _123 = _1; - _122 = S::(move _123); -- StorageDead(_123); ++ _122 = _53; + StorageDead(_123); - _121 = opaque::>(move _122) -> [return: bb29, unwind continue]; + _121 = opaque::>(_53) -> [return: bb29, unwind continue]; } bb29: { -- StorageDead(_122); + StorageDead(_122); StorageDead(_121); StorageLive(_124); -- StorageLive(_125); -- StorageLive(_126); -- StorageLive(_127); -- _127 = _1; + StorageLive(_125); + StorageLive(_126); + StorageLive(_127); + _127 = _1; - _126 = S::(move _127); -- StorageDead(_127); ++ _126 = _53; + StorageDead(_127); - _125 = (_126.0: u64); - _124 = opaque::(move _125) -> [return: bb30, unwind continue]; ++ _125 = _56; + _124 = opaque::(_56) -> [return: bb30, unwind continue]; } bb30: { -- StorageDead(_125); -- StorageDead(_126); + StorageDead(_125); + StorageDead(_126); StorageDead(_124); StorageLive(_128); _128 = &_3; StorageLive(_129); - StorageLive(_130); - StorageLive(_131); ++ nop; ++ nop; _131 = (*_128); -- StorageLive(_132); -- _132 = _1; + StorageLive(_132); + _132 = _1; - _130 = Add(move _131, move _132); -- StorageDead(_132); ++ _130 = Add(_131, _1); + StorageDead(_132); - StorageDead(_131); - _129 = opaque::(move _130) -> [return: bb31, unwind continue]; -+ _130 = Add(_131, _1); ++ nop; + _129 = opaque::(_130) -> [return: bb31, unwind continue]; } bb31: { - StorageDead(_130); ++ nop; StorageDead(_129); StorageLive(_133); -- StorageLive(_134); -- StorageLive(_135); + StorageLive(_134); + StorageLive(_135); - _135 = (*_128); -- StorageLive(_136); -- _136 = _1; ++ _135 = _131; + StorageLive(_136); + _136 = _1; - _134 = Add(move _135, move _136); -- StorageDead(_136); -- StorageDead(_135); ++ _134 = _130; + StorageDead(_136); + StorageDead(_135); - _133 = opaque::(move _134) -> [return: bb32, unwind continue]; + _133 = opaque::(_130) -> [return: bb32, unwind continue]; } bb32: { -- StorageDead(_134); + StorageDead(_134); StorageDead(_133); StorageLive(_137); _137 = &mut _3; @@ -737,11 +784,11 @@ StorageLive(_139); StorageLive(_140); _140 = (*_137); -- StorageLive(_141); -- _141 = _1; + StorageLive(_141); + _141 = _1; - _139 = Add(move _140, move _141); -- StorageDead(_141); + _139 = Add(move _140, _1); + StorageDead(_141); StorageDead(_140); _138 = opaque::(move _139) -> [return: bb33, unwind continue]; } @@ -753,11 +800,11 @@ StorageLive(_143); StorageLive(_144); _144 = (*_137); -- StorageLive(_145); -- _145 = _1; + StorageLive(_145); + _145 = _1; - _143 = Add(move _144, move _145); -- StorageDead(_145); + _143 = Add(move _144, _1); + StorageDead(_145); StorageDead(_144); _142 = opaque::(move _143) -> [return: bb34, unwind continue]; } @@ -765,18 +812,18 @@ bb34: { StorageDead(_143); StorageDead(_142); -- StorageLive(_146); + StorageLive(_146); StorageLive(_147); _147 = &raw const _3; StorageLive(_148); StorageLive(_149); StorageLive(_150); _150 = (*_147); -- StorageLive(_151); -- _151 = _1; + StorageLive(_151); + _151 = _1; - _149 = Add(move _150, move _151); -- StorageDead(_151); + _149 = Add(move _150, _1); + StorageDead(_151); StorageDead(_150); _148 = opaque::(move _149) -> [return: bb35, unwind continue]; } @@ -788,11 +835,11 @@ StorageLive(_153); StorageLive(_154); _154 = (*_147); -- StorageLive(_155); -- _155 = _1; + StorageLive(_155); + _155 = _1; - _153 = Add(move _154, move _155); -- StorageDead(_155); + _153 = Add(move _154, _1); + StorageDead(_155); StorageDead(_154); _152 = opaque::(move _153) -> [return: bb36, unwind continue]; } @@ -806,11 +853,11 @@ StorageLive(_158); StorageLive(_159); _159 = (*_156); -- StorageLive(_160); -- _160 = _1; + StorageLive(_160); + _160 = _1; - _158 = Add(move _159, move _160); -- StorageDead(_160); + _158 = Add(move _159, _1); + StorageDead(_160); StorageDead(_159); _157 = opaque::(move _158) -> [return: bb37, unwind continue]; } @@ -822,11 +869,11 @@ StorageLive(_162); StorageLive(_163); _163 = (*_156); -- StorageLive(_164); -- _164 = _1; + StorageLive(_164); + _164 = _1; - _162 = Add(move _163, move _164); -- StorageDead(_164); + _162 = Add(move _163, _1); + StorageDead(_164); StorageDead(_163); _161 = opaque::(move _162) -> [return: bb38, unwind continue]; } @@ -834,44 +881,50 @@ bb38: { StorageDead(_162); StorageDead(_161); -- _146 = const (); + _146 = const (); StorageDead(_156); StorageDead(_147); -- StorageDead(_146); + StorageDead(_146); StorageLive(_165); _165 = &_3; StorageLive(_166); - StorageLive(_167); - StorageLive(_168); ++ nop; ++ nop; _168 = (*_165); -- StorageLive(_169); -- _169 = _1; + StorageLive(_169); + _169 = _1; - _167 = Add(move _168, move _169); -- StorageDead(_169); ++ _167 = Add(_168, _1); + StorageDead(_169); - StorageDead(_168); - _166 = opaque::(move _167) -> [return: bb39, unwind continue]; -+ _167 = Add(_168, _1); ++ nop; + _166 = opaque::(_167) -> [return: bb39, unwind continue]; } bb39: { - StorageDead(_167); ++ nop; StorageDead(_166); StorageLive(_170); -- StorageLive(_171); -- StorageLive(_172); + StorageLive(_171); + StorageLive(_172); - _172 = (*_165); -- StorageLive(_173); -- _173 = _1; ++ _172 = _168; + StorageLive(_173); + _173 = _1; - _171 = Add(move _172, move _173); -- StorageDead(_173); -- StorageDead(_172); ++ _171 = _167; + StorageDead(_173); + StorageDead(_172); - _170 = opaque::(move _171) -> [return: bb40, unwind continue]; + _170 = opaque::(_167) -> [return: bb40, unwind continue]; } bb40: { -- StorageDead(_171); + StorageDead(_171); StorageDead(_170); _0 = const (); StorageDead(_165); diff --git a/tests/mir-opt/gvn.wrap_unwrap.GVN.panic-abort.diff b/tests/mir-opt/gvn.wrap_unwrap.GVN.panic-abort.diff index f33845502ad94..2dd51934778ca 100644 --- a/tests/mir-opt/gvn.wrap_unwrap.GVN.panic-abort.diff +++ b/tests/mir-opt/gvn.wrap_unwrap.GVN.panic-abort.diff @@ -15,11 +15,11 @@ bb0: { StorageLive(_2); -- StorageLive(_3); -- _3 = _1; + StorageLive(_3); + _3 = _1; - _2 = Option::::Some(move _3); -- StorageDead(_3); + _2 = Option::::Some(_1); + StorageDead(_3); _4 = discriminant(_2); switchInt(move _4) -> [0: bb1, 1: bb3, otherwise: bb2]; } @@ -35,9 +35,11 @@ bb3: { - StorageLive(_5); ++ nop; _5 = ((_2 as Some).0: T); _0 = _5; - StorageDead(_5); ++ nop; StorageDead(_2); return; } diff --git a/tests/mir-opt/gvn.wrap_unwrap.GVN.panic-unwind.diff b/tests/mir-opt/gvn.wrap_unwrap.GVN.panic-unwind.diff index edc05f99fe2f7..610d70576c9fd 100644 --- a/tests/mir-opt/gvn.wrap_unwrap.GVN.panic-unwind.diff +++ b/tests/mir-opt/gvn.wrap_unwrap.GVN.panic-unwind.diff @@ -15,11 +15,11 @@ bb0: { StorageLive(_2); -- StorageLive(_3); -- _3 = _1; + StorageLive(_3); + _3 = _1; - _2 = Option::::Some(move _3); -- StorageDead(_3); + _2 = Option::::Some(_1); + StorageDead(_3); _4 = discriminant(_2); switchInt(move _4) -> [0: bb1, 1: bb3, otherwise: bb2]; } @@ -35,9 +35,11 @@ bb3: { - StorageLive(_5); ++ nop; _5 = ((_2 as Some).0: T); _0 = _5; - StorageDead(_5); ++ nop; StorageDead(_2); return; } diff --git a/tests/mir-opt/issue_76432.test.SimplifyComparisonIntegral.panic-abort.diff b/tests/mir-opt/issue_76432.test.SimplifyComparisonIntegral.panic-abort.diff index 9d8f272abea3a..b2539f391d1a5 100644 --- a/tests/mir-opt/issue_76432.test.SimplifyComparisonIntegral.panic-abort.diff +++ b/tests/mir-opt/issue_76432.test.SimplifyComparisonIntegral.panic-abort.diff @@ -7,22 +7,18 @@ let _2: &[T]; let mut _3: &[T; 3]; let _4: [T; 3]; - let mut _5: T; - let mut _6: T; - let mut _7: T; - let mut _8: usize; - let mut _9: usize; - let mut _10: bool; - let mut _14: !; + let mut _5: usize; + let mut _6: bool; + let mut _10: !; scope 1 { debug v => _2; - let _11: &T; - let _12: &T; - let _13: &T; + let _7: &T; + let _8: &T; + let _9: &T; scope 2 { - debug v1 => _11; - debug v2 => _12; - debug v3 => _13; + debug v1 => _7; + debug v2 => _8; + debug v3 => _9; } } @@ -33,26 +29,25 @@ _3 = &_4; _2 = move _3 as &[T] (PointerCoercion(Unsize)); StorageDead(_3); - _8 = const 3_usize; - _9 = const 3_usize; - _10 = const true; + _5 = const 3_usize; + _6 = const true; goto -> bb2; } bb1: { - _14 = core::panicking::panic(const "internal error: entered unreachable code") -> unwind unreachable; + _10 = core::panicking::panic(const "internal error: entered unreachable code") -> unwind unreachable; } bb2: { - StorageLive(_11); - _11 = &(*_2)[0 of 3]; - StorageLive(_12); - _12 = &(*_2)[1 of 3]; - StorageLive(_13); - _13 = &(*_2)[2 of 3]; - StorageDead(_13); - StorageDead(_12); - StorageDead(_11); + StorageLive(_7); + _7 = &(*_2)[0 of 3]; + StorageLive(_8); + _8 = &(*_2)[1 of 3]; + StorageLive(_9); + _9 = &(*_2)[2 of 3]; + StorageDead(_9); + StorageDead(_8); + StorageDead(_7); StorageDead(_4); return; } diff --git a/tests/mir-opt/issue_76432.test.SimplifyComparisonIntegral.panic-unwind.diff b/tests/mir-opt/issue_76432.test.SimplifyComparisonIntegral.panic-unwind.diff index 738b0b1b3e5ac..ff7f12c093ce4 100644 --- a/tests/mir-opt/issue_76432.test.SimplifyComparisonIntegral.panic-unwind.diff +++ b/tests/mir-opt/issue_76432.test.SimplifyComparisonIntegral.panic-unwind.diff @@ -7,22 +7,18 @@ let _2: &[T]; let mut _3: &[T; 3]; let _4: [T; 3]; - let mut _5: T; - let mut _6: T; - let mut _7: T; - let mut _8: usize; - let mut _9: usize; - let mut _10: bool; - let mut _14: !; + let mut _5: usize; + let mut _6: bool; + let mut _10: !; scope 1 { debug v => _2; - let _11: &T; - let _12: &T; - let _13: &T; + let _7: &T; + let _8: &T; + let _9: &T; scope 2 { - debug v1 => _11; - debug v2 => _12; - debug v3 => _13; + debug v1 => _7; + debug v2 => _8; + debug v3 => _9; } } @@ -33,26 +29,25 @@ _3 = &_4; _2 = move _3 as &[T] (PointerCoercion(Unsize)); StorageDead(_3); - _8 = const 3_usize; - _9 = const 3_usize; - _10 = const true; + _5 = const 3_usize; + _6 = const true; goto -> bb2; } bb1: { - _14 = core::panicking::panic(const "internal error: entered unreachable code") -> unwind continue; + _10 = core::panicking::panic(const "internal error: entered unreachable code") -> unwind continue; } bb2: { - StorageLive(_11); - _11 = &(*_2)[0 of 3]; - StorageLive(_12); - _12 = &(*_2)[1 of 3]; - StorageLive(_13); - _13 = &(*_2)[2 of 3]; - StorageDead(_13); - StorageDead(_12); - StorageDead(_11); + StorageLive(_7); + _7 = &(*_2)[0 of 3]; + StorageLive(_8); + _8 = &(*_2)[1 of 3]; + StorageLive(_9); + _9 = &(*_2)[2 of 3]; + StorageDead(_9); + StorageDead(_8); + StorageDead(_7); StorageDead(_4); return; } diff --git a/tests/mir-opt/simplify_if.main.SimplifyConstCondition-after-const-prop.panic-abort.diff b/tests/mir-opt/simplify_if.main.SimplifyConstCondition-after-const-prop.panic-abort.diff index d395715836064..64a435f224579 100644 --- a/tests/mir-opt/simplify_if.main.SimplifyConstCondition-after-const-prop.panic-abort.diff +++ b/tests/mir-opt/simplify_if.main.SimplifyConstCondition-after-const-prop.panic-abort.diff @@ -3,18 +3,15 @@ fn main() -> () { let mut _0: (); - let mut _1: bool; - let _2: (); + let _1: (); bb0: { - StorageLive(_1); - _1 = const false; - switchInt(const false) -> [0: bb3, otherwise: bb1]; + goto -> bb3; } bb1: { - _2 = noop() -> [return: bb2, unwind unreachable]; + _1 = noop() -> [return: bb2, unwind unreachable]; } bb2: { @@ -26,7 +23,6 @@ } bb4: { - StorageDead(_1); return; } } diff --git a/tests/mir-opt/simplify_if.main.SimplifyConstCondition-after-const-prop.panic-unwind.diff b/tests/mir-opt/simplify_if.main.SimplifyConstCondition-after-const-prop.panic-unwind.diff index 81903c64dbd4b..146e00686ed8d 100644 --- a/tests/mir-opt/simplify_if.main.SimplifyConstCondition-after-const-prop.panic-unwind.diff +++ b/tests/mir-opt/simplify_if.main.SimplifyConstCondition-after-const-prop.panic-unwind.diff @@ -3,18 +3,15 @@ fn main() -> () { let mut _0: (); - let mut _1: bool; - let _2: (); + let _1: (); bb0: { - StorageLive(_1); - _1 = const false; - switchInt(const false) -> [0: bb3, otherwise: bb1]; + goto -> bb3; } bb1: { - _2 = noop() -> [return: bb2, unwind continue]; + _1 = noop() -> [return: bb2, unwind continue]; } bb2: { @@ -26,7 +23,6 @@ } bb4: { - StorageDead(_1); return; } } From afd631cc0c3049d1e862fefa6fc0b778f660bad8 Mon Sep 17 00:00:00 2001 From: Camille GILLOT Date: Sat, 23 Sep 2023 15:08:41 +0000 Subject: [PATCH 02/28] Do not visit rvalues twice. --- compiler/rustc_mir_transform/src/gvn.rs | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/compiler/rustc_mir_transform/src/gvn.rs b/compiler/rustc_mir_transform/src/gvn.rs index e7f36e5881a58..2add66ee81bb9 100644 --- a/compiler/rustc_mir_transform/src/gvn.rs +++ b/compiler/rustc_mir_transform/src/gvn.rs @@ -484,20 +484,23 @@ impl<'tcx> MutVisitor<'tcx> for VnState<'_, 'tcx> { } fn visit_statement(&mut self, stmt: &mut Statement<'tcx>, location: Location) { - self.super_statement(stmt, location); if let StatementKind::Assign(box (_, ref mut rvalue)) = stmt.kind // Do not try to simplify a constant, it's already in canonical shape. && !matches!(rvalue, Rvalue::Use(Operand::Constant(_))) - && let Some(value) = self.simplify_rvalue(rvalue, location) { - if let Some(const_) = self.try_as_constant(value) { - *rvalue = Rvalue::Use(Operand::Constant(Box::new(const_))); - } else if let Some(local) = self.try_as_local(value, location) - && *rvalue != Rvalue::Use(Operand::Move(local.into())) + if let Some(value) = self.simplify_rvalue(rvalue, location) { - *rvalue = Rvalue::Use(Operand::Copy(local.into())); - self.reused_locals.insert(local); + if let Some(const_) = self.try_as_constant(value) { + *rvalue = Rvalue::Use(Operand::Constant(Box::new(const_))); + } else if let Some(local) = self.try_as_local(value, location) + && *rvalue != Rvalue::Use(Operand::Move(local.into())) + { + *rvalue = Rvalue::Use(Operand::Copy(local.into())); + self.reused_locals.insert(local); + } } + } else { + self.super_statement(stmt, location); } } } From 38c86b079866d495062632483ec33cf569cb6f27 Mon Sep 17 00:00:00 2001 From: Camille GILLOT Date: Tue, 19 Sep 2023 20:12:48 +0000 Subject: [PATCH 03/28] Evaluate computed values to constants. --- .../src/interpret/discriminant.rs | 11 +- .../src/interpret/intrinsics.rs | 2 +- .../rustc_const_eval/src/interpret/operand.rs | 10 + .../rustc_const_eval/src/interpret/step.rs | 2 +- compiler/rustc_middle/src/mir/consts.rs | 12 + .../src/dataflow_const_prop.rs | 8 +- compiler/rustc_mir_transform/src/gvn.rs | 296 +++++++++++++++++- .../gvn.arithmetic.GVN.panic-abort.diff | 18 +- .../gvn.arithmetic.GVN.panic-unwind.diff | 18 +- ...vn.arithmetic_checked.GVN.panic-abort.diff | 31 +- ...n.arithmetic_checked.GVN.panic-unwind.diff | 31 +- tests/mir-opt/gvn.cast.GVN.panic-abort.diff | 135 ++++---- tests/mir-opt/gvn.cast.GVN.panic-unwind.diff | 135 ++++---- tests/mir-opt/gvn.slices.GVN.panic-abort.diff | 14 +- .../mir-opt/gvn.slices.GVN.panic-unwind.diff | 14 +- 15 files changed, 546 insertions(+), 191 deletions(-) diff --git a/compiler/rustc_const_eval/src/interpret/discriminant.rs b/compiler/rustc_const_eval/src/interpret/discriminant.rs index 8dab45d65ee11..fd17367037438 100644 --- a/compiler/rustc_const_eval/src/interpret/discriminant.rs +++ b/compiler/rustc_const_eval/src/interpret/discriminant.rs @@ -1,7 +1,8 @@ //! Functions for reading and writing discriminants of multi-variant layouts (enums and coroutines). -use rustc_middle::ty::layout::{LayoutOf, PrimitiveExt, TyAndLayout}; -use rustc_middle::{mir, ty}; +use rustc_middle::mir; +use rustc_middle::ty::layout::{LayoutOf, PrimitiveExt}; +use rustc_middle::ty::{self, Ty}; use rustc_target::abi::{self, TagEncoding}; use rustc_target::abi::{VariantIdx, Variants}; @@ -244,11 +245,11 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> { pub fn discriminant_for_variant( &self, - layout: TyAndLayout<'tcx>, + ty: Ty<'tcx>, variant: VariantIdx, ) -> InterpResult<'tcx, ImmTy<'tcx, M::Provenance>> { - let discr_layout = self.layout_of(layout.ty.discriminant_ty(*self.tcx))?; - let discr_value = match layout.ty.discriminant_for_variant(*self.tcx, variant) { + let discr_layout = self.layout_of(ty.discriminant_ty(*self.tcx))?; + let discr_value = match ty.discriminant_for_variant(*self.tcx, variant) { Some(discr) => { // This type actually has discriminants. assert_eq!(discr.ty, discr_layout.ty); diff --git a/compiler/rustc_const_eval/src/interpret/intrinsics.rs b/compiler/rustc_const_eval/src/interpret/intrinsics.rs index c97207a61ac3b..c48857fcf3cd2 100644 --- a/compiler/rustc_const_eval/src/interpret/intrinsics.rs +++ b/compiler/rustc_const_eval/src/interpret/intrinsics.rs @@ -218,7 +218,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> { sym::discriminant_value => { let place = self.deref_pointer(&args[0])?; let variant = self.read_discriminant(&place)?; - let discr = self.discriminant_for_variant(place.layout, variant)?; + let discr = self.discriminant_for_variant(place.layout.ty, variant)?; self.write_immediate(*discr, dest)?; } sym::exact_div => { diff --git a/compiler/rustc_const_eval/src/interpret/operand.rs b/compiler/rustc_const_eval/src/interpret/operand.rs index 6716888290dc1..255dd1eba97f2 100644 --- a/compiler/rustc_const_eval/src/interpret/operand.rs +++ b/compiler/rustc_const_eval/src/interpret/operand.rs @@ -169,6 +169,16 @@ impl<'tcx, Prov: Provenance> ImmTy<'tcx, Prov> { ImmTy { imm: val.into(), layout } } + #[inline] + pub fn from_scalar_pair(a: Scalar, b: Scalar, layout: TyAndLayout<'tcx>) -> Self { + debug_assert!( + matches!(layout.abi, Abi::ScalarPair(..)), + "`ImmTy::from_scalar_pair` on non-scalar-pair layout" + ); + let imm = Immediate::ScalarPair(a, b); + ImmTy { imm, layout } + } + #[inline(always)] pub fn from_immediate(imm: Immediate, layout: TyAndLayout<'tcx>) -> Self { debug_assert!( diff --git a/compiler/rustc_const_eval/src/interpret/step.rs b/compiler/rustc_const_eval/src/interpret/step.rs index 79cbda545f1f4..8c34d05042b1d 100644 --- a/compiler/rustc_const_eval/src/interpret/step.rs +++ b/compiler/rustc_const_eval/src/interpret/step.rs @@ -297,7 +297,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> { Discriminant(place) => { let op = self.eval_place_to_op(place, None)?; let variant = self.read_discriminant(&op)?; - let discr = self.discriminant_for_variant(op.layout, variant)?; + let discr = self.discriminant_for_variant(op.layout.ty, variant)?; self.write_immediate(*discr, &dest)?; } } diff --git a/compiler/rustc_middle/src/mir/consts.rs b/compiler/rustc_middle/src/mir/consts.rs index 4e429f316e806..9e3ee3f2bd349 100644 --- a/compiler/rustc_middle/src/mir/consts.rs +++ b/compiler/rustc_middle/src/mir/consts.rs @@ -172,6 +172,18 @@ impl<'tcx> ConstValue<'tcx> { let end = end.try_into().unwrap(); Some(data.inner().inspect_with_uninit_and_ptr_outside_interpreter(start..end)) } + + pub fn has_provenance(&self, tcx: TyCtxt<'tcx>, size: Size) -> bool { + let (alloc, start, end) = match *self { + ConstValue::ZeroSized | ConstValue::Scalar(Scalar::Int(_)) => return false, + ConstValue::Scalar(Scalar::Ptr(..)) => return true, + ConstValue::Slice { data, meta } => (data, Size::ZERO, Size::from_bytes(meta)), + ConstValue::Indirect { alloc_id, offset } => { + (tcx.global_alloc(alloc_id).unwrap_memory(), offset, offset + size) + } + }; + !alloc.inner().provenance().range_empty(super::AllocRange::from(start..end), &tcx) + } } /////////////////////////////////////////////////////////////////////////// diff --git a/compiler/rustc_mir_transform/src/dataflow_const_prop.rs b/compiler/rustc_mir_transform/src/dataflow_const_prop.rs index 2c29978173ff5..fd067cb234bdb 100644 --- a/compiler/rustc_mir_transform/src/dataflow_const_prop.rs +++ b/compiler/rustc_mir_transform/src/dataflow_const_prop.rs @@ -406,7 +406,8 @@ impl<'a, 'tcx> ConstAnalysis<'a, 'tcx> { TrackElem::Variant(idx) => self.ecx.project_downcast(op, idx).ok(), TrackElem::Discriminant => { let variant = self.ecx.read_discriminant(op).ok()?; - let discr_value = self.ecx.discriminant_for_variant(op.layout, variant).ok()?; + let discr_value = + self.ecx.discriminant_for_variant(op.layout.ty, variant).ok()?; Some(discr_value.into()) } TrackElem::DerefLen => { @@ -507,7 +508,8 @@ impl<'a, 'tcx> ConstAnalysis<'a, 'tcx> { return None; } let enum_ty_layout = self.tcx.layout_of(self.param_env.and(enum_ty)).ok()?; - let discr_value = self.ecx.discriminant_for_variant(enum_ty_layout, variant_index).ok()?; + let discr_value = + self.ecx.discriminant_for_variant(enum_ty_layout.ty, variant_index).ok()?; Some(discr_value.to_scalar()) } @@ -854,7 +856,7 @@ impl<'tcx> Visitor<'tcx> for OperandCollector<'tcx, '_, '_, '_> { } } -struct DummyMachine; +pub(crate) struct DummyMachine; impl<'mir, 'tcx: 'mir> rustc_const_eval::interpret::Machine<'mir, 'tcx> for DummyMachine { rustc_const_eval::interpret::compile_time_machine!(<'mir, 'tcx>); diff --git a/compiler/rustc_mir_transform/src/gvn.rs b/compiler/rustc_mir_transform/src/gvn.rs index 2add66ee81bb9..c38ebdf512b8f 100644 --- a/compiler/rustc_mir_transform/src/gvn.rs +++ b/compiler/rustc_mir_transform/src/gvn.rs @@ -53,18 +53,24 @@ //! _c = *_b // replaced by _c = _a //! ``` +use rustc_const_eval::interpret::{ImmTy, InterpCx, MemPlaceMeta, OpTy, Projectable, Scalar}; use rustc_data_structures::fx::{FxHashMap, FxIndexSet}; use rustc_data_structures::graph::dominators::Dominators; use rustc_index::bit_set::BitSet; use rustc_index::IndexVec; use rustc_macros::newtype_index; +use rustc_middle::mir::interpret::GlobalAlloc; use rustc_middle::mir::visit::*; use rustc_middle::mir::*; -use rustc_middle::ty::{self, Ty, TyCtxt}; -use rustc_target::abi::{VariantIdx, FIRST_VARIANT}; +use rustc_middle::ty::layout::LayoutOf; +use rustc_middle::ty::{self, Ty, TyCtxt, TypeAndMut}; +use rustc_span::DUMMY_SP; +use rustc_target::abi::{self, Abi, Size, VariantIdx, FIRST_VARIANT}; +use crate::dataflow_const_prop::DummyMachine; use crate::ssa::{AssignedValue, SsaLocals}; use crate::MirPass; +use either::Either; pub struct GVN; @@ -129,6 +135,12 @@ newtype_index! { struct VnIndex {} } +#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)] +enum AddressKind { + Ref(BorrowKind), + Address(Mutability), +} + #[derive(Debug, PartialEq, Eq, Hash)] enum Value<'tcx> { // Root values. @@ -145,6 +157,7 @@ enum Value<'tcx> { /// The address of a place. Address { place: Place<'tcx>, + kind: AddressKind, /// Give each borrow and pointer a different provenance, so we don't merge them. provenance: usize, }, @@ -172,6 +185,7 @@ enum Value<'tcx> { struct VnState<'body, 'tcx> { tcx: TyCtxt<'tcx>, + ecx: InterpCx<'tcx, 'tcx, DummyMachine>, param_env: ty::ParamEnv<'tcx>, local_decls: &'body LocalDecls<'tcx>, /// Value stored in each local. @@ -179,6 +193,8 @@ struct VnState<'body, 'tcx> { /// First local to be assigned that value. rev_locals: FxHashMap>, values: FxIndexSet>, + /// Values evaluated as constants if possible. + evaluated: IndexVec>>, /// Counter to generate different values. /// This is an option to stop creating opaques during replacement. next_opaque: Option, @@ -197,11 +213,13 @@ impl<'body, 'tcx> VnState<'body, 'tcx> { ) -> Self { VnState { tcx, + ecx: InterpCx::new(tcx, DUMMY_SP, param_env, DummyMachine), param_env, local_decls, locals: IndexVec::from_elem(None, local_decls), rev_locals: FxHashMap::default(), values: FxIndexSet::default(), + evaluated: IndexVec::new(), next_opaque: Some(0), ssa, dominators, @@ -211,8 +229,14 @@ impl<'body, 'tcx> VnState<'body, 'tcx> { #[instrument(level = "trace", skip(self), ret)] fn insert(&mut self, value: Value<'tcx>) -> VnIndex { - let (index, _) = self.values.insert_full(value); - VnIndex::from_usize(index) + let (index, new) = self.values.insert_full(value); + let index = VnIndex::from_usize(index); + if new { + let evaluated = self.eval_to_const(index); + let _index = self.evaluated.push(evaluated); + debug_assert_eq!(index, _index); + } + index } /// Create a new `Value` for which we have no information at all, except that it is distinct @@ -227,9 +251,9 @@ impl<'body, 'tcx> VnState<'body, 'tcx> { /// Create a new `Value::Address` distinct from all the others. #[instrument(level = "trace", skip(self), ret)] - fn new_pointer(&mut self, place: Place<'tcx>) -> Option { + fn new_pointer(&mut self, place: Place<'tcx>, kind: AddressKind) -> Option { let next_opaque = self.next_opaque.as_mut()?; - let value = Value::Address { place, provenance: *next_opaque }; + let value = Value::Address { place, kind, provenance: *next_opaque }; *next_opaque += 1; Some(self.insert(value)) } @@ -251,6 +275,176 @@ impl<'body, 'tcx> VnState<'body, 'tcx> { } } + #[instrument(level = "trace", skip(self), ret)] + fn eval_to_const(&mut self, value: VnIndex) -> Option> { + use Value::*; + let op = match *self.get(value) { + Opaque(_) => return None, + // Do not bother evaluating repeat expressions. This would uselessly consume memory. + Repeat(..) => return None, + + Constant(ref constant) => self.ecx.eval_mir_constant(constant, None, None).ok()?, + Aggregate(ty, variant, ref fields) => { + let fields = fields + .iter() + .map(|&f| self.evaluated[f].as_ref()) + .collect::>>()?; + let variant = if ty.is_enum() { Some(variant) } else { None }; + let ty = self.ecx.layout_of(ty).ok()?; + let alloc_id = self + .ecx + .intern_with_temp_alloc(ty, |ecx, dest| { + let variant_dest = if let Some(variant) = variant { + ecx.project_downcast(dest, variant)? + } else { + dest.clone() + }; + for (field_index, op) in fields.into_iter().enumerate() { + let field_dest = ecx.project_field(&variant_dest, field_index)?; + ecx.copy_op(op, &field_dest, /*allow_transmute*/ false)?; + } + ecx.write_discriminant(variant.unwrap_or(FIRST_VARIANT), dest) + }) + .ok()?; + let mplace = + self.ecx.raw_const_to_mplace(ConstAlloc { alloc_id, ty: ty.ty }).ok()?; + mplace.into() + } + + Projection(base, elem) => { + let value = self.evaluated[base].as_ref()?; + let elem = match elem { + ProjectionElem::Deref => ProjectionElem::Deref, + ProjectionElem::Downcast(name, read_variant) => { + ProjectionElem::Downcast(name, read_variant) + } + ProjectionElem::Field(f, ty) => ProjectionElem::Field(f, ty), + ProjectionElem::ConstantIndex { offset, min_length, from_end } => { + ProjectionElem::ConstantIndex { offset, min_length, from_end } + } + ProjectionElem::Subslice { from, to, from_end } => { + ProjectionElem::Subslice { from, to, from_end } + } + ProjectionElem::OpaqueCast(ty) => ProjectionElem::OpaqueCast(ty), + ProjectionElem::Subtype(ty) => ProjectionElem::Subtype(ty), + // This should have been replaced by a `ConstantIndex` earlier. + ProjectionElem::Index(_) => return None, + }; + self.ecx.project(value, elem).ok()? + } + Address { place, kind, provenance: _ } => { + if !place.is_indirect_first_projection() { + return None; + } + let local = self.locals[place.local]?; + let pointer = self.evaluated[local].as_ref()?; + let mut mplace = self.ecx.deref_pointer(pointer).ok()?; + for proj in place.projection.iter().skip(1) { + // We have no call stack to associate a local with a value, so we cannot interpret indexing. + if matches!(proj, ProjectionElem::Index(_)) { + return None; + } + mplace = self.ecx.project(&mplace, proj).ok()?; + } + let pointer = mplace.to_ref(&self.ecx); + let ty = match kind { + AddressKind::Ref(bk) => Ty::new_ref( + self.tcx, + self.tcx.lifetimes.re_erased, + ty::TypeAndMut { ty: mplace.layout.ty, mutbl: bk.to_mutbl_lossy() }, + ), + AddressKind::Address(mutbl) => { + Ty::new_ptr(self.tcx, TypeAndMut { ty: mplace.layout.ty, mutbl }) + } + }; + let layout = self.ecx.layout_of(ty).ok()?; + ImmTy::from_immediate(pointer, layout).into() + } + + Discriminant(base) => { + let base = self.evaluated[base].as_ref()?; + let variant = self.ecx.read_discriminant(base).ok()?; + let discr_value = + self.ecx.discriminant_for_variant(base.layout.ty, variant).ok()?; + discr_value.into() + } + Len(slice) => { + let slice = self.evaluated[slice].as_ref()?; + let usize_layout = self.ecx.layout_of(self.tcx.types.usize).unwrap(); + let len = slice.len(&self.ecx).ok()?; + let imm = ImmTy::try_from_uint(len, usize_layout)?; + imm.into() + } + NullaryOp(null_op, ty) => { + let layout = self.ecx.layout_of(ty).ok()?; + if let NullOp::SizeOf | NullOp::AlignOf = null_op && layout.is_unsized() { + return None; + } + let val = match null_op { + NullOp::SizeOf => layout.size.bytes(), + NullOp::AlignOf => layout.align.abi.bytes(), + NullOp::OffsetOf(fields) => layout + .offset_of_subfield(&self.ecx, fields.iter().map(|f| f.index())) + .bytes(), + }; + let usize_layout = self.ecx.layout_of(self.tcx.types.usize).unwrap(); + let imm = ImmTy::try_from_uint(val, usize_layout)?; + imm.into() + } + UnaryOp(un_op, operand) => { + let operand = self.evaluated[operand].as_ref()?; + let operand = self.ecx.read_immediate(operand).ok()?; + let (val, _) = self.ecx.overflowing_unary_op(un_op, &operand).ok()?; + val.into() + } + BinaryOp(bin_op, lhs, rhs) => { + let lhs = self.evaluated[lhs].as_ref()?; + let lhs = self.ecx.read_immediate(lhs).ok()?; + let rhs = self.evaluated[rhs].as_ref()?; + let rhs = self.ecx.read_immediate(rhs).ok()?; + let (val, _) = self.ecx.overflowing_binary_op(bin_op, &lhs, &rhs).ok()?; + val.into() + } + CheckedBinaryOp(bin_op, lhs, rhs) => { + let lhs = self.evaluated[lhs].as_ref()?; + let lhs = self.ecx.read_immediate(lhs).ok()?; + let rhs = self.evaluated[rhs].as_ref()?; + let rhs = self.ecx.read_immediate(rhs).ok()?; + let (val, overflowed) = self.ecx.overflowing_binary_op(bin_op, &lhs, &rhs).ok()?; + let tuple = Ty::new_tup_from_iter( + self.tcx, + [val.layout.ty, self.tcx.types.bool].into_iter(), + ); + let tuple = self.ecx.layout_of(tuple).ok()?; + ImmTy::from_scalar_pair(val.to_scalar(), Scalar::from_bool(overflowed), tuple) + .into() + } + Cast { kind, value, from: _, to } => match kind { + CastKind::IntToInt | CastKind::IntToFloat => { + let value = self.evaluated[value].as_ref()?; + let value = self.ecx.read_immediate(value).ok()?; + let to = self.ecx.layout_of(to).ok()?; + let res = self.ecx.int_to_int_or_float(&value, to).ok()?; + res.into() + } + CastKind::FloatToFloat | CastKind::FloatToInt => { + let value = self.evaluated[value].as_ref()?; + let value = self.ecx.read_immediate(value).ok()?; + let to = self.ecx.layout_of(to).ok()?; + let res = self.ecx.float_to_float_or_int(&value, to).ok()?; + res.into() + } + CastKind::Transmute => { + let value = self.evaluated[value].as_ref()?; + let to = self.ecx.layout_of(to).ok()?; + value.offset(Size::ZERO, to, &self.ecx).ok()? + } + _ => return None, + }, + }; + Some(op) + } + /// Represent the *value* which would be read from `place`, and point `place` to a preexisting /// place with the same value (if that already exists). #[instrument(level = "trace", skip(self), ret)] @@ -385,7 +579,12 @@ impl<'body, 'tcx> VnState<'body, 'tcx> { let ty = rvalue.ty(self.local_decls, self.tcx); Value::Aggregate(ty, variant_index, fields?) } - Rvalue::Ref(.., place) | Rvalue::AddressOf(_, place) => return self.new_pointer(place), + Rvalue::Ref(_, borrow_kind, place) => { + return self.new_pointer(place, AddressKind::Ref(borrow_kind)); + } + Rvalue::AddressOf(mutbl, place) => { + return self.new_pointer(place, AddressKind::Address(mutbl)); + } // Operations. Rvalue::Len(ref mut place) => { @@ -424,43 +623,106 @@ impl<'body, 'tcx> VnState<'body, 'tcx> { } } +fn op_to_prop_const<'tcx>( + ecx: &mut InterpCx<'_, 'tcx, DummyMachine>, + op: &OpTy<'tcx>, +) -> Option> { + // Do not attempt to propagate unsized locals. + if op.layout.is_unsized() { + return None; + } + + // This constant is a ZST, just return an empty value. + if op.layout.is_zst() { + return Some(ConstValue::ZeroSized); + } + + // Do not synthetize too large constants. Codegen will just memcpy them, which we'd like to avoid. + if !matches!(op.layout.abi, Abi::Scalar(..) | Abi::ScalarPair(..)) { + return None; + } + + // If this constant has scalar ABI, return it as a `ConstValue::Scalar`. + if let Abi::Scalar(abi::Scalar::Initialized { .. }) = op.layout.abi + && let Ok(scalar) = ecx.read_scalar(op) + { + return Some(ConstValue::Scalar(scalar)); + } + + // If this constant is a projection of another, we can return it directly. + if let Either::Left(mplace) = op.as_mplace_or_imm() + && let MemPlaceMeta::None = mplace.meta() + { + let pointer = mplace.ptr().into_pointer_or_addr().ok()?; + let (alloc_id, offset) = pointer.into_parts(); + return if matches!(ecx.tcx.global_alloc(alloc_id), GlobalAlloc::Memory(_)) { + Some(ConstValue::Indirect { alloc_id, offset }) + } else { + None + } + } + + // Everything failed: create a new allocation to hold the data. + let alloc_id = + ecx.intern_with_temp_alloc(op.layout, |ecx, dest| ecx.copy_op(op, dest, false)).ok()?; + Some(ConstValue::Indirect { alloc_id, offset: Size::ZERO }) +} + impl<'tcx> VnState<'_, 'tcx> { /// If `index` is a `Value::Constant`, return the `Constant` to be put in the MIR. fn try_as_constant(&mut self, index: VnIndex) -> Option> { + // This was already constant in MIR, do not change it. if let Value::Constant(const_) = *self.get(index) { // Some constants may contain pointers. We need to preserve the provenance of these // pointers, but not all constants guarantee this: // - valtrees purposefully do not; // - ConstValue::Slice does not either. - match const_ { + let const_ok = match const_ { Const::Ty(c) => match c.kind() { ty::ConstKind::Value(valtree) => match valtree { // This is just an integer, keep it. - ty::ValTree::Leaf(_) => {} - ty::ValTree::Branch(_) => return None, + ty::ValTree::Leaf(_) => true, + ty::ValTree::Branch(_) => false, }, ty::ConstKind::Param(..) | ty::ConstKind::Unevaluated(..) - | ty::ConstKind::Expr(..) => {} + | ty::ConstKind::Expr(..) => true, // Should not appear in runtime MIR. ty::ConstKind::Infer(..) | ty::ConstKind::Bound(..) | ty::ConstKind::Placeholder(..) | ty::ConstKind::Error(..) => bug!(), }, - Const::Unevaluated(..) => {} + Const::Unevaluated(..) => true, // If the same slice appears twice in the MIR, we cannot guarantee that we will // give the same `AllocId` to the data. - Const::Val(ConstValue::Slice { .. }, _) => return None, + Const::Val(ConstValue::Slice { .. }, _) => false, Const::Val( ConstValue::ZeroSized | ConstValue::Scalar(_) | ConstValue::Indirect { .. }, _, - ) => {} + ) => true, + }; + if const_ok { + return Some(ConstOperand { span: rustc_span::DUMMY_SP, user_ty: None, const_ }); } - Some(ConstOperand { span: rustc_span::DUMMY_SP, user_ty: None, const_ }) - } else { - None } + + let op = self.evaluated[index].as_ref()?; + if op.layout.is_unsized() { + // Do not attempt to propagate unsized locals. + return None; + } + + let value = op_to_prop_const(&mut self.ecx, op)?; + + // Check that we do not leak a pointer. + // Those pointers may lose part of their identity in codegen. + if value.has_provenance(self.tcx, op.layout.size) { + return None; + } + + let const_ = Const::Val(value, op.layout.ty); + Some(ConstOperand { span: rustc_span::DUMMY_SP, user_ty: None, const_ }) } /// If there is a local which is assigned `index`, and its assignment strictly dominates `loc`, diff --git a/tests/mir-opt/gvn.arithmetic.GVN.panic-abort.diff b/tests/mir-opt/gvn.arithmetic.GVN.panic-abort.diff index 17ba0d3cef2d4..d524ad242fecc 100644 --- a/tests/mir-opt/gvn.arithmetic.GVN.panic-abort.diff +++ b/tests/mir-opt/gvn.arithmetic.GVN.panic-abort.diff @@ -121,9 +121,10 @@ StorageLive(_15); StorageLive(_16); _16 = _1; - _17 = Eq(const 0_u64, const 0_u64); +- _17 = Eq(const 0_u64, const 0_u64); - assert(!move _17, "attempt to divide `{}` by zero", _16) -> [success: bb5, unwind unreachable]; -+ assert(!_17, "attempt to divide `{}` by zero", _1) -> [success: bb5, unwind unreachable]; ++ _17 = const true; ++ assert(!const true, "attempt to divide `{}` by zero", _1) -> [success: bb5, unwind unreachable]; } bb5: { @@ -140,9 +141,10 @@ StorageLive(_19); StorageLive(_20); _20 = _1; - _21 = Eq(const 1_u64, const 0_u64); +- _21 = Eq(const 1_u64, const 0_u64); - assert(!move _21, "attempt to divide `{}` by zero", _20) -> [success: bb7, unwind unreachable]; -+ assert(!_21, "attempt to divide `{}` by zero", _1) -> [success: bb7, unwind unreachable]; ++ _21 = const false; ++ assert(!const false, "attempt to divide `{}` by zero", _1) -> [success: bb7, unwind unreachable]; } bb7: { @@ -201,8 +203,8 @@ _32 = _1; - _33 = Eq(const 0_u64, const 0_u64); - assert(!move _33, "attempt to calculate the remainder of `{}` with a divisor of zero", _32) -> [success: bb13, unwind unreachable]; -+ _33 = _17; -+ assert(!_17, "attempt to calculate the remainder of `{}` with a divisor of zero", _1) -> [success: bb13, unwind unreachable]; ++ _33 = const true; ++ assert(!const true, "attempt to calculate the remainder of `{}` with a divisor of zero", _1) -> [success: bb13, unwind unreachable]; } bb13: { @@ -221,8 +223,8 @@ _36 = _1; - _37 = Eq(const 1_u64, const 0_u64); - assert(!move _37, "attempt to calculate the remainder of `{}` with a divisor of zero", _36) -> [success: bb15, unwind unreachable]; -+ _37 = _21; -+ assert(!_21, "attempt to calculate the remainder of `{}` with a divisor of zero", _1) -> [success: bb15, unwind unreachable]; ++ _37 = const false; ++ assert(!const false, "attempt to calculate the remainder of `{}` with a divisor of zero", _1) -> [success: bb15, unwind unreachable]; } bb15: { diff --git a/tests/mir-opt/gvn.arithmetic.GVN.panic-unwind.diff b/tests/mir-opt/gvn.arithmetic.GVN.panic-unwind.diff index f14fd409bea1c..9d69353934c79 100644 --- a/tests/mir-opt/gvn.arithmetic.GVN.panic-unwind.diff +++ b/tests/mir-opt/gvn.arithmetic.GVN.panic-unwind.diff @@ -121,9 +121,10 @@ StorageLive(_15); StorageLive(_16); _16 = _1; - _17 = Eq(const 0_u64, const 0_u64); +- _17 = Eq(const 0_u64, const 0_u64); - assert(!move _17, "attempt to divide `{}` by zero", _16) -> [success: bb5, unwind continue]; -+ assert(!_17, "attempt to divide `{}` by zero", _1) -> [success: bb5, unwind continue]; ++ _17 = const true; ++ assert(!const true, "attempt to divide `{}` by zero", _1) -> [success: bb5, unwind continue]; } bb5: { @@ -140,9 +141,10 @@ StorageLive(_19); StorageLive(_20); _20 = _1; - _21 = Eq(const 1_u64, const 0_u64); +- _21 = Eq(const 1_u64, const 0_u64); - assert(!move _21, "attempt to divide `{}` by zero", _20) -> [success: bb7, unwind continue]; -+ assert(!_21, "attempt to divide `{}` by zero", _1) -> [success: bb7, unwind continue]; ++ _21 = const false; ++ assert(!const false, "attempt to divide `{}` by zero", _1) -> [success: bb7, unwind continue]; } bb7: { @@ -201,8 +203,8 @@ _32 = _1; - _33 = Eq(const 0_u64, const 0_u64); - assert(!move _33, "attempt to calculate the remainder of `{}` with a divisor of zero", _32) -> [success: bb13, unwind continue]; -+ _33 = _17; -+ assert(!_17, "attempt to calculate the remainder of `{}` with a divisor of zero", _1) -> [success: bb13, unwind continue]; ++ _33 = const true; ++ assert(!const true, "attempt to calculate the remainder of `{}` with a divisor of zero", _1) -> [success: bb13, unwind continue]; } bb13: { @@ -221,8 +223,8 @@ _36 = _1; - _37 = Eq(const 1_u64, const 0_u64); - assert(!move _37, "attempt to calculate the remainder of `{}` with a divisor of zero", _36) -> [success: bb15, unwind continue]; -+ _37 = _21; -+ assert(!_21, "attempt to calculate the remainder of `{}` with a divisor of zero", _1) -> [success: bb15, unwind continue]; ++ _37 = const false; ++ assert(!const false, "attempt to calculate the remainder of `{}` with a divisor of zero", _1) -> [success: bb15, unwind continue]; } bb15: { diff --git a/tests/mir-opt/gvn.arithmetic_checked.GVN.panic-abort.diff b/tests/mir-opt/gvn.arithmetic_checked.GVN.panic-abort.diff index e586e4ac8898c..3efe7abc976f7 100644 --- a/tests/mir-opt/gvn.arithmetic_checked.GVN.panic-abort.diff +++ b/tests/mir-opt/gvn.arithmetic_checked.GVN.panic-abort.diff @@ -153,9 +153,10 @@ StorageLive(_19); StorageLive(_20); _20 = _1; - _21 = Eq(const 0_u64, const 0_u64); +- _21 = Eq(const 0_u64, const 0_u64); - assert(!move _21, "attempt to divide `{}` by zero", _20) -> [success: bb9, unwind unreachable]; -+ assert(!_21, "attempt to divide `{}` by zero", _1) -> [success: bb9, unwind unreachable]; ++ _21 = const true; ++ assert(!const true, "attempt to divide `{}` by zero", _1) -> [success: bb9, unwind unreachable]; } bb9: { @@ -172,9 +173,10 @@ StorageLive(_23); StorageLive(_24); _24 = _1; - _25 = Eq(const 1_u64, const 0_u64); +- _25 = Eq(const 1_u64, const 0_u64); - assert(!move _25, "attempt to divide `{}` by zero", _24) -> [success: bb11, unwind unreachable]; -+ assert(!_25, "attempt to divide `{}` by zero", _1) -> [success: bb11, unwind unreachable]; ++ _25 = const false; ++ assert(!const false, "attempt to divide `{}` by zero", _1) -> [success: bb11, unwind unreachable]; } bb11: { @@ -233,8 +235,8 @@ _36 = _1; - _37 = Eq(const 0_u64, const 0_u64); - assert(!move _37, "attempt to calculate the remainder of `{}` with a divisor of zero", _36) -> [success: bb17, unwind unreachable]; -+ _37 = _21; -+ assert(!_21, "attempt to calculate the remainder of `{}` with a divisor of zero", _1) -> [success: bb17, unwind unreachable]; ++ _37 = const true; ++ assert(!const true, "attempt to calculate the remainder of `{}` with a divisor of zero", _1) -> [success: bb17, unwind unreachable]; } bb17: { @@ -253,8 +255,8 @@ _40 = _1; - _41 = Eq(const 1_u64, const 0_u64); - assert(!move _41, "attempt to calculate the remainder of `{}` with a divisor of zero", _40) -> [success: bb19, unwind unreachable]; -+ _41 = _25; -+ assert(!_25, "attempt to calculate the remainder of `{}` with a divisor of zero", _1) -> [success: bb19, unwind unreachable]; ++ _41 = const false; ++ assert(!const false, "attempt to calculate the remainder of `{}` with a divisor of zero", _1) -> [success: bb19, unwind unreachable]; } bb19: { @@ -350,11 +352,12 @@ StorageLive(_60); StorageLive(_61); _61 = _1; - _62 = const 0_i32 as u32 (IntToInt); +- _62 = const 0_i32 as u32 (IntToInt); - _63 = Lt(move _62, const 64_u32); - assert(move _63, "attempt to shift right by `{}`, which would overflow", const 0_i32) -> [success: bb28, unwind unreachable]; -+ _63 = Lt(_62, const 64_u32); -+ assert(_63, "attempt to shift right by `{}`, which would overflow", const 0_i32) -> [success: bb28, unwind unreachable]; ++ _62 = const 0_u32; ++ _63 = const true; ++ assert(const true, "attempt to shift right by `{}`, which would overflow", const 0_i32) -> [success: bb28, unwind unreachable]; } bb28: { @@ -374,9 +377,9 @@ - _67 = const 0_i32 as u32 (IntToInt); - _68 = Lt(move _67, const 64_u32); - assert(move _68, "attempt to shift left by `{}`, which would overflow", const 0_i32) -> [success: bb30, unwind unreachable]; -+ _67 = _62; -+ _68 = _63; -+ assert(_63, "attempt to shift left by `{}`, which would overflow", const 0_i32) -> [success: bb30, unwind unreachable]; ++ _67 = const 0_u32; ++ _68 = const true; ++ assert(const true, "attempt to shift left by `{}`, which would overflow", const 0_i32) -> [success: bb30, unwind unreachable]; } bb30: { diff --git a/tests/mir-opt/gvn.arithmetic_checked.GVN.panic-unwind.diff b/tests/mir-opt/gvn.arithmetic_checked.GVN.panic-unwind.diff index f58a9116b8fe7..9fbb8df79a1db 100644 --- a/tests/mir-opt/gvn.arithmetic_checked.GVN.panic-unwind.diff +++ b/tests/mir-opt/gvn.arithmetic_checked.GVN.panic-unwind.diff @@ -153,9 +153,10 @@ StorageLive(_19); StorageLive(_20); _20 = _1; - _21 = Eq(const 0_u64, const 0_u64); +- _21 = Eq(const 0_u64, const 0_u64); - assert(!move _21, "attempt to divide `{}` by zero", _20) -> [success: bb9, unwind continue]; -+ assert(!_21, "attempt to divide `{}` by zero", _1) -> [success: bb9, unwind continue]; ++ _21 = const true; ++ assert(!const true, "attempt to divide `{}` by zero", _1) -> [success: bb9, unwind continue]; } bb9: { @@ -172,9 +173,10 @@ StorageLive(_23); StorageLive(_24); _24 = _1; - _25 = Eq(const 1_u64, const 0_u64); +- _25 = Eq(const 1_u64, const 0_u64); - assert(!move _25, "attempt to divide `{}` by zero", _24) -> [success: bb11, unwind continue]; -+ assert(!_25, "attempt to divide `{}` by zero", _1) -> [success: bb11, unwind continue]; ++ _25 = const false; ++ assert(!const false, "attempt to divide `{}` by zero", _1) -> [success: bb11, unwind continue]; } bb11: { @@ -233,8 +235,8 @@ _36 = _1; - _37 = Eq(const 0_u64, const 0_u64); - assert(!move _37, "attempt to calculate the remainder of `{}` with a divisor of zero", _36) -> [success: bb17, unwind continue]; -+ _37 = _21; -+ assert(!_21, "attempt to calculate the remainder of `{}` with a divisor of zero", _1) -> [success: bb17, unwind continue]; ++ _37 = const true; ++ assert(!const true, "attempt to calculate the remainder of `{}` with a divisor of zero", _1) -> [success: bb17, unwind continue]; } bb17: { @@ -253,8 +255,8 @@ _40 = _1; - _41 = Eq(const 1_u64, const 0_u64); - assert(!move _41, "attempt to calculate the remainder of `{}` with a divisor of zero", _40) -> [success: bb19, unwind continue]; -+ _41 = _25; -+ assert(!_25, "attempt to calculate the remainder of `{}` with a divisor of zero", _1) -> [success: bb19, unwind continue]; ++ _41 = const false; ++ assert(!const false, "attempt to calculate the remainder of `{}` with a divisor of zero", _1) -> [success: bb19, unwind continue]; } bb19: { @@ -350,11 +352,12 @@ StorageLive(_60); StorageLive(_61); _61 = _1; - _62 = const 0_i32 as u32 (IntToInt); +- _62 = const 0_i32 as u32 (IntToInt); - _63 = Lt(move _62, const 64_u32); - assert(move _63, "attempt to shift right by `{}`, which would overflow", const 0_i32) -> [success: bb28, unwind continue]; -+ _63 = Lt(_62, const 64_u32); -+ assert(_63, "attempt to shift right by `{}`, which would overflow", const 0_i32) -> [success: bb28, unwind continue]; ++ _62 = const 0_u32; ++ _63 = const true; ++ assert(const true, "attempt to shift right by `{}`, which would overflow", const 0_i32) -> [success: bb28, unwind continue]; } bb28: { @@ -374,9 +377,9 @@ - _67 = const 0_i32 as u32 (IntToInt); - _68 = Lt(move _67, const 64_u32); - assert(move _68, "attempt to shift left by `{}`, which would overflow", const 0_i32) -> [success: bb30, unwind continue]; -+ _67 = _62; -+ _68 = _63; -+ assert(_63, "attempt to shift left by `{}`, which would overflow", const 0_i32) -> [success: bb30, unwind continue]; ++ _67 = const 0_u32; ++ _68 = const true; ++ assert(const true, "attempt to shift left by `{}`, which would overflow", const 0_i32) -> [success: bb30, unwind continue]; } bb30: { diff --git a/tests/mir-opt/gvn.cast.GVN.panic-abort.diff b/tests/mir-opt/gvn.cast.GVN.panic-abort.diff index 37b0d92931f68..d43198c99110d 100644 --- a/tests/mir-opt/gvn.cast.GVN.panic-abort.diff +++ b/tests/mir-opt/gvn.cast.GVN.panic-abort.diff @@ -119,9 +119,10 @@ - _6 = _1; - _5 = move _6 as u8 (IntToInt); + _6 = const 1_i64; -+ _5 = const 1_i64 as u8 (IntToInt); ++ _5 = const 1_u8; StorageDead(_6); - _4 = opaque::(move _5) -> [return: bb1, unwind unreachable]; +- _4 = opaque::(move _5) -> [return: bb1, unwind unreachable]; ++ _4 = opaque::(const 1_u8) -> [return: bb1, unwind unreachable]; } bb1: { @@ -133,9 +134,10 @@ - _9 = _1; - _8 = move _9 as u16 (IntToInt); + _9 = const 1_i64; -+ _8 = const 1_i64 as u16 (IntToInt); ++ _8 = const 1_u16; StorageDead(_9); - _7 = opaque::(move _8) -> [return: bb2, unwind unreachable]; +- _7 = opaque::(move _8) -> [return: bb2, unwind unreachable]; ++ _7 = opaque::(const 1_u16) -> [return: bb2, unwind unreachable]; } bb2: { @@ -147,9 +149,10 @@ - _12 = _1; - _11 = move _12 as u32 (IntToInt); + _12 = const 1_i64; -+ _11 = const 1_i64 as u32 (IntToInt); ++ _11 = const 1_u32; StorageDead(_12); - _10 = opaque::(move _11) -> [return: bb3, unwind unreachable]; +- _10 = opaque::(move _11) -> [return: bb3, unwind unreachable]; ++ _10 = opaque::(const 1_u32) -> [return: bb3, unwind unreachable]; } bb3: { @@ -161,9 +164,10 @@ - _15 = _1; - _14 = move _15 as u64 (IntToInt); + _15 = const 1_i64; -+ _14 = const 1_i64 as u64 (IntToInt); ++ _14 = const 1_u64; StorageDead(_15); - _13 = opaque::(move _14) -> [return: bb4, unwind unreachable]; +- _13 = opaque::(move _14) -> [return: bb4, unwind unreachable]; ++ _13 = opaque::(const 1_u64) -> [return: bb4, unwind unreachable]; } bb4: { @@ -175,9 +179,10 @@ - _18 = _1; - _17 = move _18 as i8 (IntToInt); + _18 = const 1_i64; -+ _17 = const 1_i64 as i8 (IntToInt); ++ _17 = const 1_i8; StorageDead(_18); - _16 = opaque::(move _17) -> [return: bb5, unwind unreachable]; +- _16 = opaque::(move _17) -> [return: bb5, unwind unreachable]; ++ _16 = opaque::(const 1_i8) -> [return: bb5, unwind unreachable]; } bb5: { @@ -189,9 +194,10 @@ - _21 = _1; - _20 = move _21 as i16 (IntToInt); + _21 = const 1_i64; -+ _20 = const 1_i64 as i16 (IntToInt); ++ _20 = const 1_i16; StorageDead(_21); - _19 = opaque::(move _20) -> [return: bb6, unwind unreachable]; +- _19 = opaque::(move _20) -> [return: bb6, unwind unreachable]; ++ _19 = opaque::(const 1_i16) -> [return: bb6, unwind unreachable]; } bb6: { @@ -203,9 +209,10 @@ - _24 = _1; - _23 = move _24 as i32 (IntToInt); + _24 = const 1_i64; -+ _23 = const 1_i64 as i32 (IntToInt); ++ _23 = const 1_i32; StorageDead(_24); - _22 = opaque::(move _23) -> [return: bb7, unwind unreachable]; +- _22 = opaque::(move _23) -> [return: bb7, unwind unreachable]; ++ _22 = opaque::(const 1_i32) -> [return: bb7, unwind unreachable]; } bb7: { @@ -228,9 +235,10 @@ - _29 = _1; - _28 = move _29 as f32 (IntToFloat); + _29 = const 1_i64; -+ _28 = const 1_i64 as f32 (IntToFloat); ++ _28 = const 1f32; StorageDead(_29); - _27 = opaque::(move _28) -> [return: bb9, unwind unreachable]; +- _27 = opaque::(move _28) -> [return: bb9, unwind unreachable]; ++ _27 = opaque::(const 1f32) -> [return: bb9, unwind unreachable]; } bb9: { @@ -242,9 +250,10 @@ - _32 = _1; - _31 = move _32 as f64 (IntToFloat); + _32 = const 1_i64; -+ _31 = const 1_i64 as f64 (IntToFloat); ++ _31 = const 1f64; StorageDead(_32); - _30 = opaque::(move _31) -> [return: bb10, unwind unreachable]; +- _30 = opaque::(move _31) -> [return: bb10, unwind unreachable]; ++ _30 = opaque::(const 1f64) -> [return: bb10, unwind unreachable]; } bb10: { @@ -256,9 +265,10 @@ - _35 = _2; - _34 = move _35 as u8 (IntToInt); + _35 = const 1_u64; -+ _34 = const 1_u64 as u8 (IntToInt); ++ _34 = const 1_u8; StorageDead(_35); - _33 = opaque::(move _34) -> [return: bb11, unwind unreachable]; +- _33 = opaque::(move _34) -> [return: bb11, unwind unreachable]; ++ _33 = opaque::(const 1_u8) -> [return: bb11, unwind unreachable]; } bb11: { @@ -270,9 +280,10 @@ - _38 = _2; - _37 = move _38 as u16 (IntToInt); + _38 = const 1_u64; -+ _37 = const 1_u64 as u16 (IntToInt); ++ _37 = const 1_u16; StorageDead(_38); - _36 = opaque::(move _37) -> [return: bb12, unwind unreachable]; +- _36 = opaque::(move _37) -> [return: bb12, unwind unreachable]; ++ _36 = opaque::(const 1_u16) -> [return: bb12, unwind unreachable]; } bb12: { @@ -284,9 +295,10 @@ - _41 = _2; - _40 = move _41 as u32 (IntToInt); + _41 = const 1_u64; -+ _40 = const 1_u64 as u32 (IntToInt); ++ _40 = const 1_u32; StorageDead(_41); - _39 = opaque::(move _40) -> [return: bb13, unwind unreachable]; +- _39 = opaque::(move _40) -> [return: bb13, unwind unreachable]; ++ _39 = opaque::(const 1_u32) -> [return: bb13, unwind unreachable]; } bb13: { @@ -309,9 +321,10 @@ - _46 = _2; - _45 = move _46 as i8 (IntToInt); + _46 = const 1_u64; -+ _45 = const 1_u64 as i8 (IntToInt); ++ _45 = const 1_i8; StorageDead(_46); - _44 = opaque::(move _45) -> [return: bb15, unwind unreachable]; +- _44 = opaque::(move _45) -> [return: bb15, unwind unreachable]; ++ _44 = opaque::(const 1_i8) -> [return: bb15, unwind unreachable]; } bb15: { @@ -323,9 +336,10 @@ - _49 = _2; - _48 = move _49 as i16 (IntToInt); + _49 = const 1_u64; -+ _48 = const 1_u64 as i16 (IntToInt); ++ _48 = const 1_i16; StorageDead(_49); - _47 = opaque::(move _48) -> [return: bb16, unwind unreachable]; +- _47 = opaque::(move _48) -> [return: bb16, unwind unreachable]; ++ _47 = opaque::(const 1_i16) -> [return: bb16, unwind unreachable]; } bb16: { @@ -337,9 +351,10 @@ - _52 = _2; - _51 = move _52 as i32 (IntToInt); + _52 = const 1_u64; -+ _51 = const 1_u64 as i32 (IntToInt); ++ _51 = const 1_i32; StorageDead(_52); - _50 = opaque::(move _51) -> [return: bb17, unwind unreachable]; +- _50 = opaque::(move _51) -> [return: bb17, unwind unreachable]; ++ _50 = opaque::(const 1_i32) -> [return: bb17, unwind unreachable]; } bb17: { @@ -351,9 +366,10 @@ - _55 = _2; - _54 = move _55 as i64 (IntToInt); + _55 = const 1_u64; -+ _54 = const 1_u64 as i64 (IntToInt); ++ _54 = const 1_i64; StorageDead(_55); - _53 = opaque::(move _54) -> [return: bb18, unwind unreachable]; +- _53 = opaque::(move _54) -> [return: bb18, unwind unreachable]; ++ _53 = opaque::(const 1_i64) -> [return: bb18, unwind unreachable]; } bb18: { @@ -365,9 +381,10 @@ - _58 = _2; - _57 = move _58 as f32 (IntToFloat); + _58 = const 1_u64; -+ _57 = const 1_u64 as f32 (IntToFloat); ++ _57 = const 1f32; StorageDead(_58); - _56 = opaque::(move _57) -> [return: bb19, unwind unreachable]; +- _56 = opaque::(move _57) -> [return: bb19, unwind unreachable]; ++ _56 = opaque::(const 1f32) -> [return: bb19, unwind unreachable]; } bb19: { @@ -379,9 +396,10 @@ - _61 = _2; - _60 = move _61 as f64 (IntToFloat); + _61 = const 1_u64; -+ _60 = const 1_u64 as f64 (IntToFloat); ++ _60 = const 1f64; StorageDead(_61); - _59 = opaque::(move _60) -> [return: bb20, unwind unreachable]; +- _59 = opaque::(move _60) -> [return: bb20, unwind unreachable]; ++ _59 = opaque::(const 1f64) -> [return: bb20, unwind unreachable]; } bb20: { @@ -393,9 +411,10 @@ - _64 = _3; - _63 = move _64 as u8 (FloatToInt); + _64 = const 1f64; -+ _63 = const 1f64 as u8 (FloatToInt); ++ _63 = const 1_u8; StorageDead(_64); - _62 = opaque::(move _63) -> [return: bb21, unwind unreachable]; +- _62 = opaque::(move _63) -> [return: bb21, unwind unreachable]; ++ _62 = opaque::(const 1_u8) -> [return: bb21, unwind unreachable]; } bb21: { @@ -407,9 +426,10 @@ - _67 = _3; - _66 = move _67 as u16 (FloatToInt); + _67 = const 1f64; -+ _66 = const 1f64 as u16 (FloatToInt); ++ _66 = const 1_u16; StorageDead(_67); - _65 = opaque::(move _66) -> [return: bb22, unwind unreachable]; +- _65 = opaque::(move _66) -> [return: bb22, unwind unreachable]; ++ _65 = opaque::(const 1_u16) -> [return: bb22, unwind unreachable]; } bb22: { @@ -421,9 +441,10 @@ - _70 = _3; - _69 = move _70 as u32 (FloatToInt); + _70 = const 1f64; -+ _69 = const 1f64 as u32 (FloatToInt); ++ _69 = const 1_u32; StorageDead(_70); - _68 = opaque::(move _69) -> [return: bb23, unwind unreachable]; +- _68 = opaque::(move _69) -> [return: bb23, unwind unreachable]; ++ _68 = opaque::(const 1_u32) -> [return: bb23, unwind unreachable]; } bb23: { @@ -435,9 +456,10 @@ - _73 = _3; - _72 = move _73 as u64 (FloatToInt); + _73 = const 1f64; -+ _72 = const 1f64 as u64 (FloatToInt); ++ _72 = const 1_u64; StorageDead(_73); - _71 = opaque::(move _72) -> [return: bb24, unwind unreachable]; +- _71 = opaque::(move _72) -> [return: bb24, unwind unreachable]; ++ _71 = opaque::(const 1_u64) -> [return: bb24, unwind unreachable]; } bb24: { @@ -449,9 +471,10 @@ - _76 = _3; - _75 = move _76 as i8 (FloatToInt); + _76 = const 1f64; -+ _75 = const 1f64 as i8 (FloatToInt); ++ _75 = const 1_i8; StorageDead(_76); - _74 = opaque::(move _75) -> [return: bb25, unwind unreachable]; +- _74 = opaque::(move _75) -> [return: bb25, unwind unreachable]; ++ _74 = opaque::(const 1_i8) -> [return: bb25, unwind unreachable]; } bb25: { @@ -463,9 +486,10 @@ - _79 = _3; - _78 = move _79 as i16 (FloatToInt); + _79 = const 1f64; -+ _78 = const 1f64 as i16 (FloatToInt); ++ _78 = const 1_i16; StorageDead(_79); - _77 = opaque::(move _78) -> [return: bb26, unwind unreachable]; +- _77 = opaque::(move _78) -> [return: bb26, unwind unreachable]; ++ _77 = opaque::(const 1_i16) -> [return: bb26, unwind unreachable]; } bb26: { @@ -477,9 +501,10 @@ - _82 = _3; - _81 = move _82 as i32 (FloatToInt); + _82 = const 1f64; -+ _81 = const 1f64 as i32 (FloatToInt); ++ _81 = const 1_i32; StorageDead(_82); - _80 = opaque::(move _81) -> [return: bb27, unwind unreachable]; +- _80 = opaque::(move _81) -> [return: bb27, unwind unreachable]; ++ _80 = opaque::(const 1_i32) -> [return: bb27, unwind unreachable]; } bb27: { @@ -491,9 +516,10 @@ - _85 = _3; - _84 = move _85 as i64 (FloatToInt); + _85 = const 1f64; -+ _84 = const 1f64 as i64 (FloatToInt); ++ _84 = const 1_i64; StorageDead(_85); - _83 = opaque::(move _84) -> [return: bb28, unwind unreachable]; +- _83 = opaque::(move _84) -> [return: bb28, unwind unreachable]; ++ _83 = opaque::(const 1_i64) -> [return: bb28, unwind unreachable]; } bb28: { @@ -505,9 +531,10 @@ - _88 = _3; - _87 = move _88 as f32 (FloatToFloat); + _88 = const 1f64; -+ _87 = const 1f64 as f32 (FloatToFloat); ++ _87 = const 1f32; StorageDead(_88); - _86 = opaque::(move _87) -> [return: bb29, unwind unreachable]; +- _86 = opaque::(move _87) -> [return: bb29, unwind unreachable]; ++ _86 = opaque::(const 1f32) -> [return: bb29, unwind unreachable]; } bb29: { diff --git a/tests/mir-opt/gvn.cast.GVN.panic-unwind.diff b/tests/mir-opt/gvn.cast.GVN.panic-unwind.diff index fbdec455188ab..08b97e13aa0ba 100644 --- a/tests/mir-opt/gvn.cast.GVN.panic-unwind.diff +++ b/tests/mir-opt/gvn.cast.GVN.panic-unwind.diff @@ -119,9 +119,10 @@ - _6 = _1; - _5 = move _6 as u8 (IntToInt); + _6 = const 1_i64; -+ _5 = const 1_i64 as u8 (IntToInt); ++ _5 = const 1_u8; StorageDead(_6); - _4 = opaque::(move _5) -> [return: bb1, unwind continue]; +- _4 = opaque::(move _5) -> [return: bb1, unwind continue]; ++ _4 = opaque::(const 1_u8) -> [return: bb1, unwind continue]; } bb1: { @@ -133,9 +134,10 @@ - _9 = _1; - _8 = move _9 as u16 (IntToInt); + _9 = const 1_i64; -+ _8 = const 1_i64 as u16 (IntToInt); ++ _8 = const 1_u16; StorageDead(_9); - _7 = opaque::(move _8) -> [return: bb2, unwind continue]; +- _7 = opaque::(move _8) -> [return: bb2, unwind continue]; ++ _7 = opaque::(const 1_u16) -> [return: bb2, unwind continue]; } bb2: { @@ -147,9 +149,10 @@ - _12 = _1; - _11 = move _12 as u32 (IntToInt); + _12 = const 1_i64; -+ _11 = const 1_i64 as u32 (IntToInt); ++ _11 = const 1_u32; StorageDead(_12); - _10 = opaque::(move _11) -> [return: bb3, unwind continue]; +- _10 = opaque::(move _11) -> [return: bb3, unwind continue]; ++ _10 = opaque::(const 1_u32) -> [return: bb3, unwind continue]; } bb3: { @@ -161,9 +164,10 @@ - _15 = _1; - _14 = move _15 as u64 (IntToInt); + _15 = const 1_i64; -+ _14 = const 1_i64 as u64 (IntToInt); ++ _14 = const 1_u64; StorageDead(_15); - _13 = opaque::(move _14) -> [return: bb4, unwind continue]; +- _13 = opaque::(move _14) -> [return: bb4, unwind continue]; ++ _13 = opaque::(const 1_u64) -> [return: bb4, unwind continue]; } bb4: { @@ -175,9 +179,10 @@ - _18 = _1; - _17 = move _18 as i8 (IntToInt); + _18 = const 1_i64; -+ _17 = const 1_i64 as i8 (IntToInt); ++ _17 = const 1_i8; StorageDead(_18); - _16 = opaque::(move _17) -> [return: bb5, unwind continue]; +- _16 = opaque::(move _17) -> [return: bb5, unwind continue]; ++ _16 = opaque::(const 1_i8) -> [return: bb5, unwind continue]; } bb5: { @@ -189,9 +194,10 @@ - _21 = _1; - _20 = move _21 as i16 (IntToInt); + _21 = const 1_i64; -+ _20 = const 1_i64 as i16 (IntToInt); ++ _20 = const 1_i16; StorageDead(_21); - _19 = opaque::(move _20) -> [return: bb6, unwind continue]; +- _19 = opaque::(move _20) -> [return: bb6, unwind continue]; ++ _19 = opaque::(const 1_i16) -> [return: bb6, unwind continue]; } bb6: { @@ -203,9 +209,10 @@ - _24 = _1; - _23 = move _24 as i32 (IntToInt); + _24 = const 1_i64; -+ _23 = const 1_i64 as i32 (IntToInt); ++ _23 = const 1_i32; StorageDead(_24); - _22 = opaque::(move _23) -> [return: bb7, unwind continue]; +- _22 = opaque::(move _23) -> [return: bb7, unwind continue]; ++ _22 = opaque::(const 1_i32) -> [return: bb7, unwind continue]; } bb7: { @@ -228,9 +235,10 @@ - _29 = _1; - _28 = move _29 as f32 (IntToFloat); + _29 = const 1_i64; -+ _28 = const 1_i64 as f32 (IntToFloat); ++ _28 = const 1f32; StorageDead(_29); - _27 = opaque::(move _28) -> [return: bb9, unwind continue]; +- _27 = opaque::(move _28) -> [return: bb9, unwind continue]; ++ _27 = opaque::(const 1f32) -> [return: bb9, unwind continue]; } bb9: { @@ -242,9 +250,10 @@ - _32 = _1; - _31 = move _32 as f64 (IntToFloat); + _32 = const 1_i64; -+ _31 = const 1_i64 as f64 (IntToFloat); ++ _31 = const 1f64; StorageDead(_32); - _30 = opaque::(move _31) -> [return: bb10, unwind continue]; +- _30 = opaque::(move _31) -> [return: bb10, unwind continue]; ++ _30 = opaque::(const 1f64) -> [return: bb10, unwind continue]; } bb10: { @@ -256,9 +265,10 @@ - _35 = _2; - _34 = move _35 as u8 (IntToInt); + _35 = const 1_u64; -+ _34 = const 1_u64 as u8 (IntToInt); ++ _34 = const 1_u8; StorageDead(_35); - _33 = opaque::(move _34) -> [return: bb11, unwind continue]; +- _33 = opaque::(move _34) -> [return: bb11, unwind continue]; ++ _33 = opaque::(const 1_u8) -> [return: bb11, unwind continue]; } bb11: { @@ -270,9 +280,10 @@ - _38 = _2; - _37 = move _38 as u16 (IntToInt); + _38 = const 1_u64; -+ _37 = const 1_u64 as u16 (IntToInt); ++ _37 = const 1_u16; StorageDead(_38); - _36 = opaque::(move _37) -> [return: bb12, unwind continue]; +- _36 = opaque::(move _37) -> [return: bb12, unwind continue]; ++ _36 = opaque::(const 1_u16) -> [return: bb12, unwind continue]; } bb12: { @@ -284,9 +295,10 @@ - _41 = _2; - _40 = move _41 as u32 (IntToInt); + _41 = const 1_u64; -+ _40 = const 1_u64 as u32 (IntToInt); ++ _40 = const 1_u32; StorageDead(_41); - _39 = opaque::(move _40) -> [return: bb13, unwind continue]; +- _39 = opaque::(move _40) -> [return: bb13, unwind continue]; ++ _39 = opaque::(const 1_u32) -> [return: bb13, unwind continue]; } bb13: { @@ -309,9 +321,10 @@ - _46 = _2; - _45 = move _46 as i8 (IntToInt); + _46 = const 1_u64; -+ _45 = const 1_u64 as i8 (IntToInt); ++ _45 = const 1_i8; StorageDead(_46); - _44 = opaque::(move _45) -> [return: bb15, unwind continue]; +- _44 = opaque::(move _45) -> [return: bb15, unwind continue]; ++ _44 = opaque::(const 1_i8) -> [return: bb15, unwind continue]; } bb15: { @@ -323,9 +336,10 @@ - _49 = _2; - _48 = move _49 as i16 (IntToInt); + _49 = const 1_u64; -+ _48 = const 1_u64 as i16 (IntToInt); ++ _48 = const 1_i16; StorageDead(_49); - _47 = opaque::(move _48) -> [return: bb16, unwind continue]; +- _47 = opaque::(move _48) -> [return: bb16, unwind continue]; ++ _47 = opaque::(const 1_i16) -> [return: bb16, unwind continue]; } bb16: { @@ -337,9 +351,10 @@ - _52 = _2; - _51 = move _52 as i32 (IntToInt); + _52 = const 1_u64; -+ _51 = const 1_u64 as i32 (IntToInt); ++ _51 = const 1_i32; StorageDead(_52); - _50 = opaque::(move _51) -> [return: bb17, unwind continue]; +- _50 = opaque::(move _51) -> [return: bb17, unwind continue]; ++ _50 = opaque::(const 1_i32) -> [return: bb17, unwind continue]; } bb17: { @@ -351,9 +366,10 @@ - _55 = _2; - _54 = move _55 as i64 (IntToInt); + _55 = const 1_u64; -+ _54 = const 1_u64 as i64 (IntToInt); ++ _54 = const 1_i64; StorageDead(_55); - _53 = opaque::(move _54) -> [return: bb18, unwind continue]; +- _53 = opaque::(move _54) -> [return: bb18, unwind continue]; ++ _53 = opaque::(const 1_i64) -> [return: bb18, unwind continue]; } bb18: { @@ -365,9 +381,10 @@ - _58 = _2; - _57 = move _58 as f32 (IntToFloat); + _58 = const 1_u64; -+ _57 = const 1_u64 as f32 (IntToFloat); ++ _57 = const 1f32; StorageDead(_58); - _56 = opaque::(move _57) -> [return: bb19, unwind continue]; +- _56 = opaque::(move _57) -> [return: bb19, unwind continue]; ++ _56 = opaque::(const 1f32) -> [return: bb19, unwind continue]; } bb19: { @@ -379,9 +396,10 @@ - _61 = _2; - _60 = move _61 as f64 (IntToFloat); + _61 = const 1_u64; -+ _60 = const 1_u64 as f64 (IntToFloat); ++ _60 = const 1f64; StorageDead(_61); - _59 = opaque::(move _60) -> [return: bb20, unwind continue]; +- _59 = opaque::(move _60) -> [return: bb20, unwind continue]; ++ _59 = opaque::(const 1f64) -> [return: bb20, unwind continue]; } bb20: { @@ -393,9 +411,10 @@ - _64 = _3; - _63 = move _64 as u8 (FloatToInt); + _64 = const 1f64; -+ _63 = const 1f64 as u8 (FloatToInt); ++ _63 = const 1_u8; StorageDead(_64); - _62 = opaque::(move _63) -> [return: bb21, unwind continue]; +- _62 = opaque::(move _63) -> [return: bb21, unwind continue]; ++ _62 = opaque::(const 1_u8) -> [return: bb21, unwind continue]; } bb21: { @@ -407,9 +426,10 @@ - _67 = _3; - _66 = move _67 as u16 (FloatToInt); + _67 = const 1f64; -+ _66 = const 1f64 as u16 (FloatToInt); ++ _66 = const 1_u16; StorageDead(_67); - _65 = opaque::(move _66) -> [return: bb22, unwind continue]; +- _65 = opaque::(move _66) -> [return: bb22, unwind continue]; ++ _65 = opaque::(const 1_u16) -> [return: bb22, unwind continue]; } bb22: { @@ -421,9 +441,10 @@ - _70 = _3; - _69 = move _70 as u32 (FloatToInt); + _70 = const 1f64; -+ _69 = const 1f64 as u32 (FloatToInt); ++ _69 = const 1_u32; StorageDead(_70); - _68 = opaque::(move _69) -> [return: bb23, unwind continue]; +- _68 = opaque::(move _69) -> [return: bb23, unwind continue]; ++ _68 = opaque::(const 1_u32) -> [return: bb23, unwind continue]; } bb23: { @@ -435,9 +456,10 @@ - _73 = _3; - _72 = move _73 as u64 (FloatToInt); + _73 = const 1f64; -+ _72 = const 1f64 as u64 (FloatToInt); ++ _72 = const 1_u64; StorageDead(_73); - _71 = opaque::(move _72) -> [return: bb24, unwind continue]; +- _71 = opaque::(move _72) -> [return: bb24, unwind continue]; ++ _71 = opaque::(const 1_u64) -> [return: bb24, unwind continue]; } bb24: { @@ -449,9 +471,10 @@ - _76 = _3; - _75 = move _76 as i8 (FloatToInt); + _76 = const 1f64; -+ _75 = const 1f64 as i8 (FloatToInt); ++ _75 = const 1_i8; StorageDead(_76); - _74 = opaque::(move _75) -> [return: bb25, unwind continue]; +- _74 = opaque::(move _75) -> [return: bb25, unwind continue]; ++ _74 = opaque::(const 1_i8) -> [return: bb25, unwind continue]; } bb25: { @@ -463,9 +486,10 @@ - _79 = _3; - _78 = move _79 as i16 (FloatToInt); + _79 = const 1f64; -+ _78 = const 1f64 as i16 (FloatToInt); ++ _78 = const 1_i16; StorageDead(_79); - _77 = opaque::(move _78) -> [return: bb26, unwind continue]; +- _77 = opaque::(move _78) -> [return: bb26, unwind continue]; ++ _77 = opaque::(const 1_i16) -> [return: bb26, unwind continue]; } bb26: { @@ -477,9 +501,10 @@ - _82 = _3; - _81 = move _82 as i32 (FloatToInt); + _82 = const 1f64; -+ _81 = const 1f64 as i32 (FloatToInt); ++ _81 = const 1_i32; StorageDead(_82); - _80 = opaque::(move _81) -> [return: bb27, unwind continue]; +- _80 = opaque::(move _81) -> [return: bb27, unwind continue]; ++ _80 = opaque::(const 1_i32) -> [return: bb27, unwind continue]; } bb27: { @@ -491,9 +516,10 @@ - _85 = _3; - _84 = move _85 as i64 (FloatToInt); + _85 = const 1f64; -+ _84 = const 1f64 as i64 (FloatToInt); ++ _84 = const 1_i64; StorageDead(_85); - _83 = opaque::(move _84) -> [return: bb28, unwind continue]; +- _83 = opaque::(move _84) -> [return: bb28, unwind continue]; ++ _83 = opaque::(const 1_i64) -> [return: bb28, unwind continue]; } bb28: { @@ -505,9 +531,10 @@ - _88 = _3; - _87 = move _88 as f32 (FloatToFloat); + _88 = const 1f64; -+ _87 = const 1f64 as f32 (FloatToFloat); ++ _87 = const 1f32; StorageDead(_88); - _86 = opaque::(move _87) -> [return: bb29, unwind continue]; +- _86 = opaque::(move _87) -> [return: bb29, unwind continue]; ++ _86 = opaque::(const 1f32) -> [return: bb29, unwind continue]; } bb29: { diff --git a/tests/mir-opt/gvn.slices.GVN.panic-abort.diff b/tests/mir-opt/gvn.slices.GVN.panic-abort.diff index d924c70d617dd..98255c42007d1 100644 --- a/tests/mir-opt/gvn.slices.GVN.panic-abort.diff +++ b/tests/mir-opt/gvn.slices.GVN.panic-abort.diff @@ -176,12 +176,13 @@ StorageDead(_19); StorageDead(_18); - StorageLive(_21); +- _21 = core::panicking::AssertKind::Eq; + nop; - _21 = core::panicking::AssertKind::Eq; ++ _21 = const core::panicking::AssertKind::Eq; StorageLive(_22); StorageLive(_23); - _23 = move _21; -+ _23 = _21; ++ _23 = const core::panicking::AssertKind::Eq; StorageLive(_24); StorageLive(_25); _25 = &(*_15); @@ -193,7 +194,7 @@ StorageLive(_28); _28 = Option::>::None; - _22 = core::panicking::assert_failed::<*const u8, *const u8>(move _23, move _24, move _26, move _28) -> unwind unreachable; -+ _22 = core::panicking::assert_failed::<*const u8, *const u8>(_21, move _24, move _26, move _28) -> unwind unreachable; ++ _22 = core::panicking::assert_failed::<*const u8, *const u8>(const core::panicking::AssertKind::Eq, move _24, move _26, move _28) -> unwind unreachable; } bb7: { @@ -261,12 +262,13 @@ StorageDead(_45); StorageDead(_44); - StorageLive(_47); +- _47 = core::panicking::AssertKind::Eq; + nop; - _47 = core::panicking::AssertKind::Eq; ++ _47 = const core::panicking::AssertKind::Eq; StorageLive(_48); StorageLive(_49); - _49 = move _47; -+ _49 = _47; ++ _49 = const core::panicking::AssertKind::Eq; StorageLive(_50); StorageLive(_51); _51 = &(*_41); @@ -278,7 +280,7 @@ StorageLive(_54); _54 = Option::>::None; - _48 = core::panicking::assert_failed::<*const u8, *const u8>(move _49, move _50, move _52, move _54) -> unwind unreachable; -+ _48 = core::panicking::assert_failed::<*const u8, *const u8>(_47, move _50, move _52, move _54) -> unwind unreachable; ++ _48 = core::panicking::assert_failed::<*const u8, *const u8>(const core::panicking::AssertKind::Eq, move _50, move _52, move _54) -> unwind unreachable; } } diff --git a/tests/mir-opt/gvn.slices.GVN.panic-unwind.diff b/tests/mir-opt/gvn.slices.GVN.panic-unwind.diff index 63225aa0a1016..0f79cc409f310 100644 --- a/tests/mir-opt/gvn.slices.GVN.panic-unwind.diff +++ b/tests/mir-opt/gvn.slices.GVN.panic-unwind.diff @@ -176,12 +176,13 @@ StorageDead(_19); StorageDead(_18); - StorageLive(_21); +- _21 = core::panicking::AssertKind::Eq; + nop; - _21 = core::panicking::AssertKind::Eq; ++ _21 = const core::panicking::AssertKind::Eq; StorageLive(_22); StorageLive(_23); - _23 = move _21; -+ _23 = _21; ++ _23 = const core::panicking::AssertKind::Eq; StorageLive(_24); StorageLive(_25); _25 = &(*_15); @@ -193,7 +194,7 @@ StorageLive(_28); _28 = Option::>::None; - _22 = core::panicking::assert_failed::<*const u8, *const u8>(move _23, move _24, move _26, move _28) -> unwind continue; -+ _22 = core::panicking::assert_failed::<*const u8, *const u8>(_21, move _24, move _26, move _28) -> unwind continue; ++ _22 = core::panicking::assert_failed::<*const u8, *const u8>(const core::panicking::AssertKind::Eq, move _24, move _26, move _28) -> unwind continue; } bb7: { @@ -261,12 +262,13 @@ StorageDead(_45); StorageDead(_44); - StorageLive(_47); +- _47 = core::panicking::AssertKind::Eq; + nop; - _47 = core::panicking::AssertKind::Eq; ++ _47 = const core::panicking::AssertKind::Eq; StorageLive(_48); StorageLive(_49); - _49 = move _47; -+ _49 = _47; ++ _49 = const core::panicking::AssertKind::Eq; StorageLive(_50); StorageLive(_51); _51 = &(*_41); @@ -278,7 +280,7 @@ StorageLive(_54); _54 = Option::>::None; - _48 = core::panicking::assert_failed::<*const u8, *const u8>(move _49, move _50, move _52, move _54) -> unwind continue; -+ _48 = core::panicking::assert_failed::<*const u8, *const u8>(_47, move _50, move _52, move _54) -> unwind continue; ++ _48 = core::panicking::assert_failed::<*const u8, *const u8>(const core::panicking::AssertKind::Eq, move _50, move _52, move _54) -> unwind continue; } } From db9bd9bd8c42aa06d5d822a1f04bc2007601ca43 Mon Sep 17 00:00:00 2001 From: Camille GILLOT Date: Sat, 23 Sep 2023 14:55:28 +0000 Subject: [PATCH 04/28] Do not intern too large aggregates. --- compiler/rustc_mir_transform/src/gvn.rs | 42 ++++++++++++++----------- 1 file changed, 24 insertions(+), 18 deletions(-) diff --git a/compiler/rustc_mir_transform/src/gvn.rs b/compiler/rustc_mir_transform/src/gvn.rs index c38ebdf512b8f..7800aa6372920 100644 --- a/compiler/rustc_mir_transform/src/gvn.rs +++ b/compiler/rustc_mir_transform/src/gvn.rs @@ -291,24 +291,30 @@ impl<'body, 'tcx> VnState<'body, 'tcx> { .collect::>>()?; let variant = if ty.is_enum() { Some(variant) } else { None }; let ty = self.ecx.layout_of(ty).ok()?; - let alloc_id = self - .ecx - .intern_with_temp_alloc(ty, |ecx, dest| { - let variant_dest = if let Some(variant) = variant { - ecx.project_downcast(dest, variant)? - } else { - dest.clone() - }; - for (field_index, op) in fields.into_iter().enumerate() { - let field_dest = ecx.project_field(&variant_dest, field_index)?; - ecx.copy_op(op, &field_dest, /*allow_transmute*/ false)?; - } - ecx.write_discriminant(variant.unwrap_or(FIRST_VARIANT), dest) - }) - .ok()?; - let mplace = - self.ecx.raw_const_to_mplace(ConstAlloc { alloc_id, ty: ty.ty }).ok()?; - mplace.into() + if ty.is_zst() { + ImmTy::uninit(ty).into() + } else if matches!(ty.abi, Abi::Scalar(..) | Abi::ScalarPair(..)) { + let alloc_id = self + .ecx + .intern_with_temp_alloc(ty, |ecx, dest| { + let variant_dest = if let Some(variant) = variant { + ecx.project_downcast(dest, variant)? + } else { + dest.clone() + }; + for (field_index, op) in fields.into_iter().enumerate() { + let field_dest = ecx.project_field(&variant_dest, field_index)?; + ecx.copy_op(op, &field_dest, /*allow_transmute*/ false)?; + } + ecx.write_discriminant(variant.unwrap_or(FIRST_VARIANT), dest) + }) + .ok()?; + let mplace = + self.ecx.raw_const_to_mplace(ConstAlloc { alloc_id, ty: ty.ty }).ok()?; + mplace.into() + } else { + return None; + } } Projection(base, elem) => { From 9389373127705dd4ceb460e6e152cfb9126e6be5 Mon Sep 17 00:00:00 2001 From: Camille GILLOT Date: Sat, 23 Sep 2023 07:23:14 +0000 Subject: [PATCH 05/28] Do not transmute immediates to non-immediates. --- compiler/rustc_mir_transform/src/gvn.rs | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/compiler/rustc_mir_transform/src/gvn.rs b/compiler/rustc_mir_transform/src/gvn.rs index 7800aa6372920..60ff1dd0d782a 100644 --- a/compiler/rustc_mir_transform/src/gvn.rs +++ b/compiler/rustc_mir_transform/src/gvn.rs @@ -443,6 +443,16 @@ impl<'body, 'tcx> VnState<'body, 'tcx> { CastKind::Transmute => { let value = self.evaluated[value].as_ref()?; let to = self.ecx.layout_of(to).ok()?; + // `offset` for immediates only supports scalar/scalar-pair ABIs, + // so bail out if the target is not one. + if value.as_mplace_or_imm().is_right() { + match to.abi { + Abi::Scalar(..) | Abi::ScalarPair(..) => {} + _ if to.is_zst() => {} + Abi::Aggregate { .. } if to.fields.count() == 0 => {} + _ => return None, + } + } value.offset(Size::ZERO, to, &self.ecx).ok()? } _ => return None, From 692e5286479e16b3e9b47ab842b543985e1a6ceb Mon Sep 17 00:00:00 2001 From: Camille GILLOT Date: Sat, 16 Sep 2023 09:16:04 +0000 Subject: [PATCH 06/28] Simplify projections in GVN. --- compiler/rustc_mir_transform/src/gvn.rs | 155 ++++++++++++------ compiler/rustc_mir_transform/src/lib.rs | 1 + .../gvn.dereferences.GVN.panic-abort.diff | 18 +- .../gvn.dereferences.GVN.panic-unwind.diff | 18 +- .../gvn.references.GVN.panic-abort.diff | 76 ++++++++- .../gvn.references.GVN.panic-unwind.diff | 98 +++++++++-- .../gvn.repeated_index.GVN.panic-abort.diff | 6 +- .../gvn.repeated_index.GVN.panic-unwind.diff | 6 +- tests/mir-opt/gvn.rs | 7 + tests/mir-opt/gvn.slices.GVN.panic-abort.diff | 39 +++-- .../mir-opt/gvn.slices.GVN.panic-unwind.diff | 39 +++-- ...xpression_elimination.GVN.panic-abort.diff | 30 ++-- ...pression_elimination.GVN.panic-unwind.diff | 30 ++-- 13 files changed, 396 insertions(+), 127 deletions(-) diff --git a/compiler/rustc_mir_transform/src/gvn.rs b/compiler/rustc_mir_transform/src/gvn.rs index 60ff1dd0d782a..eaa1c70a5e9b2 100644 --- a/compiler/rustc_mir_transform/src/gvn.rs +++ b/compiler/rustc_mir_transform/src/gvn.rs @@ -66,6 +66,7 @@ use rustc_middle::ty::layout::LayoutOf; use rustc_middle::ty::{self, Ty, TyCtxt, TypeAndMut}; use rustc_span::DUMMY_SP; use rustc_target::abi::{self, Abi, Size, VariantIdx, FIRST_VARIANT}; +use std::borrow::Cow; use crate::dataflow_const_prop::DummyMachine; use crate::ssa::{AssignedValue, SsaLocals}; @@ -461,6 +462,87 @@ impl<'body, 'tcx> VnState<'body, 'tcx> { Some(op) } + fn project( + &mut self, + place: PlaceRef<'tcx>, + value: VnIndex, + proj: PlaceElem<'tcx>, + ) -> Option { + let proj = match proj { + ProjectionElem::Deref => { + let ty = place.ty(self.local_decls, self.tcx).ty; + if let Some(Mutability::Not) = ty.ref_mutability() + && let Some(pointee_ty) = ty.builtin_deref(true) + && pointee_ty.ty.is_freeze(self.tcx, self.param_env) + { + // An immutable borrow `_x` always points to the same value for the + // lifetime of the borrow, so we can merge all instances of `*_x`. + ProjectionElem::Deref + } else { + return None; + } + } + ProjectionElem::Downcast(name, index) => ProjectionElem::Downcast(name, index), + ProjectionElem::Field(f, ty) => ProjectionElem::Field(f, ty), + ProjectionElem::Index(idx) => { + let idx = self.locals[idx]?; + ProjectionElem::Index(idx) + } + ProjectionElem::ConstantIndex { offset, min_length, from_end } => { + ProjectionElem::ConstantIndex { offset, min_length, from_end } + } + ProjectionElem::Subslice { from, to, from_end } => { + ProjectionElem::Subslice { from, to, from_end } + } + ProjectionElem::OpaqueCast(ty) => ProjectionElem::OpaqueCast(ty), + ProjectionElem::Subtype(ty) => ProjectionElem::Subtype(ty), + }; + + Some(self.insert(Value::Projection(value, proj))) + } + + /// Simplify the projection chain if we know better. + #[instrument(level = "trace", skip(self))] + fn simplify_place_projection(&mut self, place: &mut Place<'tcx>, location: Location) { + // If the projection is indirect, we treat the local as a value, so can replace it with + // another local. + if place.is_indirect() + && let Some(base) = self.locals[place.local] + && let Some(new_local) = self.try_as_local(base, location) + { + place.local = new_local; + self.reused_locals.insert(new_local); + } + + let mut projection = Cow::Borrowed(&place.projection[..]); + + for i in 0..projection.len() { + let elem = projection[i]; + if let ProjectionElem::Index(idx) = elem + && let Some(idx) = self.locals[idx] + { + if let Some(offset) = self.evaluated[idx].as_ref() + && let Ok(offset) = self.ecx.read_target_usize(offset) + { + projection.to_mut()[i] = ProjectionElem::ConstantIndex { + offset, + min_length: offset + 1, + from_end: false, + }; + } else if let Some(new_idx) = self.try_as_local(idx, location) { + projection.to_mut()[i] = ProjectionElem::Index(new_idx); + self.reused_locals.insert(new_idx); + } + } + } + + if projection.is_owned() { + place.projection = self.tcx.mk_place_elems(&projection); + } + + trace!(?place); + } + /// Represent the *value* which would be read from `place`, and point `place` to a preexisting /// place with the same value (if that already exists). #[instrument(level = "trace", skip(self), ret)] @@ -469,6 +551,8 @@ impl<'body, 'tcx> VnState<'body, 'tcx> { place: &mut Place<'tcx>, location: Location, ) -> Option { + self.simplify_place_projection(place, location); + // Invariant: `place` and `place_ref` point to the same value, even if they point to // different memory locations. let mut place_ref = place.as_ref(); @@ -483,53 +567,15 @@ impl<'body, 'tcx> VnState<'body, 'tcx> { place_ref = PlaceRef { local, projection: &place.projection[index..] }; } - let proj = match proj { - ProjectionElem::Deref => { - let ty = Place::ty_from( - place.local, - &place.projection[..index], - self.local_decls, - self.tcx, - ) - .ty; - if let Some(Mutability::Not) = ty.ref_mutability() - && let Some(pointee_ty) = ty.builtin_deref(true) - && pointee_ty.ty.is_freeze(self.tcx, self.param_env) - { - // An immutable borrow `_x` always points to the same value for the - // lifetime of the borrow, so we can merge all instances of `*_x`. - ProjectionElem::Deref - } else { - return None; - } - } - ProjectionElem::Field(f, ty) => ProjectionElem::Field(f, ty), - ProjectionElem::Index(idx) => { - let idx = self.locals[idx]?; - ProjectionElem::Index(idx) - } - ProjectionElem::ConstantIndex { offset, min_length, from_end } => { - ProjectionElem::ConstantIndex { offset, min_length, from_end } - } - ProjectionElem::Subslice { from, to, from_end } => { - ProjectionElem::Subslice { from, to, from_end } - } - ProjectionElem::Downcast(name, index) => ProjectionElem::Downcast(name, index), - ProjectionElem::OpaqueCast(ty) => ProjectionElem::OpaqueCast(ty), - ProjectionElem::Subtype(ty) => ProjectionElem::Subtype(ty), - }; - value = self.insert(Value::Projection(value, proj)); + let base = PlaceRef { local: place.local, projection: &place.projection[..index] }; + value = self.project(base, value, proj)?; } - if let Some(local) = self.try_as_local(value, location) - && local != place.local - // in case we had no projection to begin with. - { - *place = local.into(); - self.reused_locals.insert(local); - } else if place_ref.local != place.local - || place_ref.projection.len() < place.projection.len() - { + if let Some(new_local) = self.try_as_local(value, location) { + place_ref = PlaceRef { local: new_local, projection: &[] }; + } + + if place_ref.local != place.local || place_ref.projection.len() < place.projection.len() { // By the invariant on `place_ref`. *place = place_ref.project_deeper(&[], self.tcx); self.reused_locals.insert(place_ref.local); @@ -545,7 +591,10 @@ impl<'body, 'tcx> VnState<'body, 'tcx> { location: Location, ) -> Option { match *operand { - Operand::Constant(ref constant) => Some(self.insert(Value::Constant(constant.const_))), + Operand::Constant(ref mut constant) => { + let const_ = constant.const_.normalize(self.tcx, self.param_env); + Some(self.insert(Value::Constant(const_))) + } Operand::Copy(ref mut place) | Operand::Move(ref mut place) => { let value = self.simplify_place_value(place, location)?; if let Some(const_) = self.try_as_constant(value) { @@ -595,11 +644,13 @@ impl<'body, 'tcx> VnState<'body, 'tcx> { let ty = rvalue.ty(self.local_decls, self.tcx); Value::Aggregate(ty, variant_index, fields?) } - Rvalue::Ref(_, borrow_kind, place) => { - return self.new_pointer(place, AddressKind::Ref(borrow_kind)); + Rvalue::Ref(_, borrow_kind, ref mut place) => { + self.simplify_place_projection(place, location); + return self.new_pointer(*place, AddressKind::Ref(borrow_kind)); } - Rvalue::AddressOf(mutbl, place) => { - return self.new_pointer(place, AddressKind::Address(mutbl)); + Rvalue::AddressOf(mutbl, ref mut place) => { + self.simplify_place_projection(place, location); + return self.new_pointer(*place, AddressKind::Address(mutbl)); } // Operations. @@ -757,6 +808,10 @@ impl<'tcx> MutVisitor<'tcx> for VnState<'_, 'tcx> { self.tcx } + fn visit_place(&mut self, place: &mut Place<'tcx>, _: PlaceContext, location: Location) { + self.simplify_place_projection(place, location); + } + fn visit_operand(&mut self, operand: &mut Operand<'tcx>, location: Location) { self.simplify_operand(operand, location); } diff --git a/compiler/rustc_mir_transform/src/lib.rs b/compiler/rustc_mir_transform/src/lib.rs index 4ec91a55f1d2c..bdd5bf702c8a6 100644 --- a/compiler/rustc_mir_transform/src/lib.rs +++ b/compiler/rustc_mir_transform/src/lib.rs @@ -2,6 +2,7 @@ #![deny(rustc::untranslatable_diagnostic)] #![deny(rustc::diagnostic_outside_of_impl)] #![feature(box_patterns)] +#![feature(cow_is_borrowed)] #![feature(decl_macro)] #![feature(is_sorted)] #![feature(let_chains)] diff --git a/tests/mir-opt/gvn.dereferences.GVN.panic-abort.diff b/tests/mir-opt/gvn.dereferences.GVN.panic-abort.diff index 46bf13985daf8..a587b1e6b1de4 100644 --- a/tests/mir-opt/gvn.dereferences.GVN.panic-abort.diff +++ b/tests/mir-opt/gvn.dereferences.GVN.panic-abort.diff @@ -72,7 +72,8 @@ bb2: { StorageDead(_7); StorageDead(_6); - StorageLive(_8); +- StorageLive(_8); ++ nop; _8 = &raw const (*_1); StorageLive(_9); StorageLive(_10); @@ -92,7 +93,8 @@ bb4: { StorageDead(_12); StorageDead(_11); - StorageLive(_13); +- StorageLive(_13); ++ nop; _13 = &raw mut (*_1); StorageLive(_14); StorageLive(_15); @@ -112,7 +114,8 @@ bb6: { StorageDead(_17); StorageDead(_16); - StorageLive(_18); +- StorageLive(_18); ++ nop; _18 = &(*_1); StorageLive(_19); - StorageLive(_20); @@ -188,9 +191,12 @@ StorageDead(_32); StorageDead(_31); _0 = const (); - StorageDead(_18); - StorageDead(_13); - StorageDead(_8); +- StorageDead(_18); +- StorageDead(_13); +- StorageDead(_8); ++ nop; ++ nop; ++ nop; return; } } diff --git a/tests/mir-opt/gvn.dereferences.GVN.panic-unwind.diff b/tests/mir-opt/gvn.dereferences.GVN.panic-unwind.diff index 3e731ead859e6..6fdda5e998875 100644 --- a/tests/mir-opt/gvn.dereferences.GVN.panic-unwind.diff +++ b/tests/mir-opt/gvn.dereferences.GVN.panic-unwind.diff @@ -72,7 +72,8 @@ bb2: { StorageDead(_7); StorageDead(_6); - StorageLive(_8); +- StorageLive(_8); ++ nop; _8 = &raw const (*_1); StorageLive(_9); StorageLive(_10); @@ -92,7 +93,8 @@ bb4: { StorageDead(_12); StorageDead(_11); - StorageLive(_13); +- StorageLive(_13); ++ nop; _13 = &raw mut (*_1); StorageLive(_14); StorageLive(_15); @@ -112,7 +114,8 @@ bb6: { StorageDead(_17); StorageDead(_16); - StorageLive(_18); +- StorageLive(_18); ++ nop; _18 = &(*_1); StorageLive(_19); - StorageLive(_20); @@ -188,9 +191,12 @@ StorageDead(_32); StorageDead(_31); _0 = const (); - StorageDead(_18); - StorageDead(_13); - StorageDead(_8); +- StorageDead(_18); +- StorageDead(_13); +- StorageDead(_8); ++ nop; ++ nop; ++ nop; return; } } diff --git a/tests/mir-opt/gvn.references.GVN.panic-abort.diff b/tests/mir-opt/gvn.references.GVN.panic-abort.diff index b7ad4ab1fd3d8..885431725657b 100644 --- a/tests/mir-opt/gvn.references.GVN.panic-abort.diff +++ b/tests/mir-opt/gvn.references.GVN.panic-abort.diff @@ -20,6 +20,24 @@ let mut _15: *mut impl Sized; let _16: (); let mut _17: *mut impl Sized; + let _18: &mut impl Sized; + let mut _20: S<&mut impl Sized>; + let mut _21: &mut impl Sized; + let _22: (); + let mut _23: &impl Sized; + let _24: (); + let mut _25: &mut impl Sized; + let _26: (); + let mut _27: *const impl Sized; + let _28: (); + let mut _29: *mut impl Sized; + scope 1 { + debug r => _18; + let _19: &mut impl Sized; + scope 2 { + debug s => _19; + } + } bb0: { StorageLive(_2); @@ -94,11 +112,65 @@ bb8: { StorageDead(_17); StorageDead(_16); - _0 = const (); - drop(_1) -> [return: bb9, unwind unreachable]; +- StorageLive(_18); ++ nop; + _18 = &mut _1; +- StorageLive(_19); ++ nop; + StorageLive(_20); + StorageLive(_21); +- _21 = move _18; +- _20 = S::<&mut impl Sized>(move _21); ++ _21 = _18; ++ _20 = S::<&mut impl Sized>(_18); + StorageDead(_21); + _19 = move (_20.0: &mut impl Sized); + StorageDead(_20); + StorageLive(_22); + StorageLive(_23); + _23 = &(*_19); + _22 = opaque::<&impl Sized>(move _23) -> [return: bb9, unwind unreachable]; } bb9: { + StorageDead(_23); + StorageDead(_22); + StorageLive(_24); + StorageLive(_25); + _25 = &mut (*_19); + _24 = opaque::<&mut impl Sized>(move _25) -> [return: bb10, unwind unreachable]; + } + + bb10: { + StorageDead(_25); + StorageDead(_24); + StorageLive(_26); + StorageLive(_27); + _27 = &raw const (*_19); + _26 = opaque::<*const impl Sized>(move _27) -> [return: bb11, unwind unreachable]; + } + + bb11: { + StorageDead(_27); + StorageDead(_26); + StorageLive(_28); + StorageLive(_29); + _29 = &raw mut (*_19); + _28 = opaque::<*mut impl Sized>(move _29) -> [return: bb12, unwind unreachable]; + } + + bb12: { + StorageDead(_29); + StorageDead(_28); + _0 = const (); +- StorageDead(_19); +- StorageDead(_18); ++ nop; ++ nop; + drop(_1) -> [return: bb13, unwind unreachable]; + } + + bb13: { return; } } diff --git a/tests/mir-opt/gvn.references.GVN.panic-unwind.diff b/tests/mir-opt/gvn.references.GVN.panic-unwind.diff index 08ed4c629a6dd..106b453fe800d 100644 --- a/tests/mir-opt/gvn.references.GVN.panic-unwind.diff +++ b/tests/mir-opt/gvn.references.GVN.panic-unwind.diff @@ -20,12 +20,30 @@ let mut _15: *mut impl Sized; let _16: (); let mut _17: *mut impl Sized; + let _18: &mut impl Sized; + let mut _20: S<&mut impl Sized>; + let mut _21: &mut impl Sized; + let _22: (); + let mut _23: &impl Sized; + let _24: (); + let mut _25: &mut impl Sized; + let _26: (); + let mut _27: *const impl Sized; + let _28: (); + let mut _29: *mut impl Sized; + scope 1 { + debug r => _18; + let _19: &mut impl Sized; + scope 2 { + debug s => _19; + } + } bb0: { StorageLive(_2); StorageLive(_3); _3 = &_1; - _2 = opaque::<&impl Sized>(move _3) -> [return: bb1, unwind: bb10]; + _2 = opaque::<&impl Sized>(move _3) -> [return: bb1, unwind: bb14]; } bb1: { @@ -34,7 +52,7 @@ StorageLive(_4); StorageLive(_5); _5 = &_1; - _4 = opaque::<&impl Sized>(move _5) -> [return: bb2, unwind: bb10]; + _4 = opaque::<&impl Sized>(move _5) -> [return: bb2, unwind: bb14]; } bb2: { @@ -43,7 +61,7 @@ StorageLive(_6); StorageLive(_7); _7 = &mut _1; - _6 = opaque::<&mut impl Sized>(move _7) -> [return: bb3, unwind: bb10]; + _6 = opaque::<&mut impl Sized>(move _7) -> [return: bb3, unwind: bb14]; } bb3: { @@ -52,7 +70,7 @@ StorageLive(_8); StorageLive(_9); _9 = &mut _1; - _8 = opaque::<&mut impl Sized>(move _9) -> [return: bb4, unwind: bb10]; + _8 = opaque::<&mut impl Sized>(move _9) -> [return: bb4, unwind: bb14]; } bb4: { @@ -61,7 +79,7 @@ StorageLive(_10); StorageLive(_11); _11 = &raw const _1; - _10 = opaque::<*const impl Sized>(move _11) -> [return: bb5, unwind: bb10]; + _10 = opaque::<*const impl Sized>(move _11) -> [return: bb5, unwind: bb14]; } bb5: { @@ -70,7 +88,7 @@ StorageLive(_12); StorageLive(_13); _13 = &raw const _1; - _12 = opaque::<*const impl Sized>(move _13) -> [return: bb6, unwind: bb10]; + _12 = opaque::<*const impl Sized>(move _13) -> [return: bb6, unwind: bb14]; } bb6: { @@ -79,7 +97,7 @@ StorageLive(_14); StorageLive(_15); _15 = &raw mut _1; - _14 = opaque::<*mut impl Sized>(move _15) -> [return: bb7, unwind: bb10]; + _14 = opaque::<*mut impl Sized>(move _15) -> [return: bb7, unwind: bb14]; } bb7: { @@ -88,25 +106,79 @@ StorageLive(_16); StorageLive(_17); _17 = &raw mut _1; - _16 = opaque::<*mut impl Sized>(move _17) -> [return: bb8, unwind: bb10]; + _16 = opaque::<*mut impl Sized>(move _17) -> [return: bb8, unwind: bb14]; } bb8: { StorageDead(_17); StorageDead(_16); - _0 = const (); - drop(_1) -> [return: bb9, unwind: bb11]; +- StorageLive(_18); ++ nop; + _18 = &mut _1; +- StorageLive(_19); ++ nop; + StorageLive(_20); + StorageLive(_21); +- _21 = move _18; +- _20 = S::<&mut impl Sized>(move _21); ++ _21 = _18; ++ _20 = S::<&mut impl Sized>(_18); + StorageDead(_21); + _19 = move (_20.0: &mut impl Sized); + StorageDead(_20); + StorageLive(_22); + StorageLive(_23); + _23 = &(*_19); + _22 = opaque::<&impl Sized>(move _23) -> [return: bb9, unwind: bb14]; } bb9: { + StorageDead(_23); + StorageDead(_22); + StorageLive(_24); + StorageLive(_25); + _25 = &mut (*_19); + _24 = opaque::<&mut impl Sized>(move _25) -> [return: bb10, unwind: bb14]; + } + + bb10: { + StorageDead(_25); + StorageDead(_24); + StorageLive(_26); + StorageLive(_27); + _27 = &raw const (*_19); + _26 = opaque::<*const impl Sized>(move _27) -> [return: bb11, unwind: bb14]; + } + + bb11: { + StorageDead(_27); + StorageDead(_26); + StorageLive(_28); + StorageLive(_29); + _29 = &raw mut (*_19); + _28 = opaque::<*mut impl Sized>(move _29) -> [return: bb12, unwind: bb14]; + } + + bb12: { + StorageDead(_29); + StorageDead(_28); + _0 = const (); +- StorageDead(_19); +- StorageDead(_18); ++ nop; ++ nop; + drop(_1) -> [return: bb13, unwind: bb15]; + } + + bb13: { return; } - bb10 (cleanup): { - drop(_1) -> [return: bb11, unwind terminate(cleanup)]; + bb14 (cleanup): { + drop(_1) -> [return: bb15, unwind terminate(cleanup)]; } - bb11 (cleanup): { + bb15 (cleanup): { resume; } } diff --git a/tests/mir-opt/gvn.repeated_index.GVN.panic-abort.diff b/tests/mir-opt/gvn.repeated_index.GVN.panic-abort.diff index 6e542d7f964ce..91af1ccc33a92 100644 --- a/tests/mir-opt/gvn.repeated_index.GVN.panic-abort.diff +++ b/tests/mir-opt/gvn.repeated_index.GVN.panic-abort.diff @@ -40,7 +40,8 @@ } bb1: { - _6 = _3[_7]; +- _6 = _3[_7]; ++ _6 = _3[0 of 1]; _5 = opaque::(move _6) -> [return: bb2, unwind unreachable]; } @@ -61,7 +62,8 @@ } bb3: { - _11 = _3[_12]; +- _11 = _3[_12]; ++ _11 = _3[_2]; _10 = opaque::(move _11) -> [return: bb4, unwind unreachable]; } diff --git a/tests/mir-opt/gvn.repeated_index.GVN.panic-unwind.diff b/tests/mir-opt/gvn.repeated_index.GVN.panic-unwind.diff index a07cd49ec7c6f..f887094b509a3 100644 --- a/tests/mir-opt/gvn.repeated_index.GVN.panic-unwind.diff +++ b/tests/mir-opt/gvn.repeated_index.GVN.panic-unwind.diff @@ -40,7 +40,8 @@ } bb1: { - _6 = _3[_7]; +- _6 = _3[_7]; ++ _6 = _3[0 of 1]; _5 = opaque::(move _6) -> [return: bb2, unwind continue]; } @@ -61,7 +62,8 @@ } bb3: { - _11 = _3[_12]; +- _11 = _3[_12]; ++ _11 = _3[_2]; _10 = opaque::(move _11) -> [return: bb4, unwind continue]; } diff --git a/tests/mir-opt/gvn.rs b/tests/mir-opt/gvn.rs index fd24edc676c4b..3633f9c23cd51 100644 --- a/tests/mir-opt/gvn.rs +++ b/tests/mir-opt/gvn.rs @@ -192,6 +192,13 @@ fn references(mut x: impl Sized) { opaque(&raw const x); // should not reuse a opaque(&raw mut x); opaque(&raw mut x); // should not reuse a + + let r = &mut x; + let s = S(r).0; // Obfuscate `r`. + opaque(&*s); // This is `&*r`. + opaque(&mut *s); // This is `&*r`. + opaque(&raw const *s); // This is `&*r`. + opaque(&raw mut *s); // This is `&*r`. } fn dereferences(t: &mut u32, u: &impl Copy, s: &S) { diff --git a/tests/mir-opt/gvn.slices.GVN.panic-abort.diff b/tests/mir-opt/gvn.slices.GVN.panic-abort.diff index 98255c42007d1..6168476d6f277 100644 --- a/tests/mir-opt/gvn.slices.GVN.panic-abort.diff +++ b/tests/mir-opt/gvn.slices.GVN.panic-abort.diff @@ -125,7 +125,8 @@ StorageLive(_12); StorageLive(_13); StorageLive(_14); - _14 = &(*_4); +- _14 = &(*_4); ++ _14 = &(*_1); _13 = core::str::::as_ptr(move _14) -> [return: bb4, unwind unreachable]; } @@ -135,9 +136,11 @@ _8 = (move _9, move _12); StorageDead(_12); StorageDead(_9); - StorageLive(_15); +- StorageLive(_15); ++ nop; _15 = (_8.0: &*const u8); - StorageLive(_16); +- StorageLive(_16); ++ nop; _16 = (_8.1: &*const u8); StorageLive(_17); StorageLive(_18); @@ -153,8 +156,10 @@ StorageDead(_18); _7 = const (); StorageDead(_17); - StorageDead(_16); - StorageDead(_15); +- StorageDead(_16); +- StorageDead(_15); ++ nop; ++ nop; StorageDead(_13); StorageDead(_10); StorageDead(_8); @@ -184,11 +189,13 @@ - _23 = move _21; + _23 = const core::panicking::AssertKind::Eq; StorageLive(_24); - StorageLive(_25); +- StorageLive(_25); ++ nop; _25 = &(*_15); _24 = &(*_25); StorageLive(_26); - StorageLive(_27); +- StorageLive(_27); ++ nop; _27 = &(*_16); _26 = &(*_27); StorageLive(_28); @@ -225,9 +232,11 @@ _34 = (move _35, move _38); StorageDead(_38); StorageDead(_35); - StorageLive(_41); +- StorageLive(_41); ++ nop; _41 = (_34.0: &*const u8); - StorageLive(_42); +- StorageLive(_42); ++ nop; _42 = (_34.1: &*const u8); StorageLive(_43); StorageLive(_44); @@ -243,8 +252,10 @@ StorageDead(_44); _33 = const (); StorageDead(_43); - StorageDead(_42); - StorageDead(_41); +- StorageDead(_42); +- StorageDead(_41); ++ nop; ++ nop; StorageDead(_39); StorageDead(_36); StorageDead(_34); @@ -270,11 +281,13 @@ - _49 = move _47; + _49 = const core::panicking::AssertKind::Eq; StorageLive(_50); - StorageLive(_51); +- StorageLive(_51); ++ nop; _51 = &(*_41); _50 = &(*_51); StorageLive(_52); - StorageLive(_53); +- StorageLive(_53); ++ nop; _53 = &(*_42); _52 = &(*_53); StorageLive(_54); diff --git a/tests/mir-opt/gvn.slices.GVN.panic-unwind.diff b/tests/mir-opt/gvn.slices.GVN.panic-unwind.diff index 0f79cc409f310..f60038858089a 100644 --- a/tests/mir-opt/gvn.slices.GVN.panic-unwind.diff +++ b/tests/mir-opt/gvn.slices.GVN.panic-unwind.diff @@ -125,7 +125,8 @@ StorageLive(_12); StorageLive(_13); StorageLive(_14); - _14 = &(*_4); +- _14 = &(*_4); ++ _14 = &(*_1); _13 = core::str::::as_ptr(move _14) -> [return: bb4, unwind continue]; } @@ -135,9 +136,11 @@ _8 = (move _9, move _12); StorageDead(_12); StorageDead(_9); - StorageLive(_15); +- StorageLive(_15); ++ nop; _15 = (_8.0: &*const u8); - StorageLive(_16); +- StorageLive(_16); ++ nop; _16 = (_8.1: &*const u8); StorageLive(_17); StorageLive(_18); @@ -153,8 +156,10 @@ StorageDead(_18); _7 = const (); StorageDead(_17); - StorageDead(_16); - StorageDead(_15); +- StorageDead(_16); +- StorageDead(_15); ++ nop; ++ nop; StorageDead(_13); StorageDead(_10); StorageDead(_8); @@ -184,11 +189,13 @@ - _23 = move _21; + _23 = const core::panicking::AssertKind::Eq; StorageLive(_24); - StorageLive(_25); +- StorageLive(_25); ++ nop; _25 = &(*_15); _24 = &(*_25); StorageLive(_26); - StorageLive(_27); +- StorageLive(_27); ++ nop; _27 = &(*_16); _26 = &(*_27); StorageLive(_28); @@ -225,9 +232,11 @@ _34 = (move _35, move _38); StorageDead(_38); StorageDead(_35); - StorageLive(_41); +- StorageLive(_41); ++ nop; _41 = (_34.0: &*const u8); - StorageLive(_42); +- StorageLive(_42); ++ nop; _42 = (_34.1: &*const u8); StorageLive(_43); StorageLive(_44); @@ -243,8 +252,10 @@ StorageDead(_44); _33 = const (); StorageDead(_43); - StorageDead(_42); - StorageDead(_41); +- StorageDead(_42); +- StorageDead(_41); ++ nop; ++ nop; StorageDead(_39); StorageDead(_36); StorageDead(_34); @@ -270,11 +281,13 @@ - _49 = move _47; + _49 = const core::panicking::AssertKind::Eq; StorageLive(_50); - StorageLive(_51); +- StorageLive(_51); ++ nop; _51 = &(*_41); _50 = &(*_51); StorageLive(_52); - StorageLive(_53); +- StorageLive(_53); ++ nop; _53 = &(*_42); _52 = &(*_53); StorageLive(_54); diff --git a/tests/mir-opt/gvn.subexpression_elimination.GVN.panic-abort.diff b/tests/mir-opt/gvn.subexpression_elimination.GVN.panic-abort.diff index 6c8513e32816a..41f29959c9538 100644 --- a/tests/mir-opt/gvn.subexpression_elimination.GVN.panic-abort.diff +++ b/tests/mir-opt/gvn.subexpression_elimination.GVN.panic-abort.diff @@ -737,7 +737,8 @@ StorageDead(_125); StorageDead(_126); StorageDead(_124); - StorageLive(_128); +- StorageLive(_128); ++ nop; _128 = &_3; StorageLive(_129); - StorageLive(_130); @@ -778,7 +779,8 @@ bb32: { StorageDead(_134); StorageDead(_133); - StorageLive(_137); +- StorageLive(_137); ++ nop; _137 = &mut _3; StorageLive(_138); StorageLive(_139); @@ -813,7 +815,8 @@ StorageDead(_143); StorageDead(_142); StorageLive(_146); - StorageLive(_147); +- StorageLive(_147); ++ nop; _147 = &raw const _3; StorageLive(_148); StorageLive(_149); @@ -847,7 +850,8 @@ bb36: { StorageDead(_153); StorageDead(_152); - StorageLive(_156); +- StorageLive(_156); ++ nop; _156 = &raw mut _3; StorageLive(_157); StorageLive(_158); @@ -882,10 +886,13 @@ StorageDead(_162); StorageDead(_161); _146 = const (); - StorageDead(_156); - StorageDead(_147); +- StorageDead(_156); +- StorageDead(_147); ++ nop; ++ nop; StorageDead(_146); - StorageLive(_165); +- StorageLive(_165); ++ nop; _165 = &_3; StorageLive(_166); - StorageLive(_167); @@ -927,9 +934,12 @@ StorageDead(_171); StorageDead(_170); _0 = const (); - StorageDead(_165); - StorageDead(_137); - StorageDead(_128); +- StorageDead(_165); +- StorageDead(_137); +- StorageDead(_128); ++ nop; ++ nop; ++ nop; return; } } diff --git a/tests/mir-opt/gvn.subexpression_elimination.GVN.panic-unwind.diff b/tests/mir-opt/gvn.subexpression_elimination.GVN.panic-unwind.diff index fb2c089827deb..ca928986cf655 100644 --- a/tests/mir-opt/gvn.subexpression_elimination.GVN.panic-unwind.diff +++ b/tests/mir-opt/gvn.subexpression_elimination.GVN.panic-unwind.diff @@ -737,7 +737,8 @@ StorageDead(_125); StorageDead(_126); StorageDead(_124); - StorageLive(_128); +- StorageLive(_128); ++ nop; _128 = &_3; StorageLive(_129); - StorageLive(_130); @@ -778,7 +779,8 @@ bb32: { StorageDead(_134); StorageDead(_133); - StorageLive(_137); +- StorageLive(_137); ++ nop; _137 = &mut _3; StorageLive(_138); StorageLive(_139); @@ -813,7 +815,8 @@ StorageDead(_143); StorageDead(_142); StorageLive(_146); - StorageLive(_147); +- StorageLive(_147); ++ nop; _147 = &raw const _3; StorageLive(_148); StorageLive(_149); @@ -847,7 +850,8 @@ bb36: { StorageDead(_153); StorageDead(_152); - StorageLive(_156); +- StorageLive(_156); ++ nop; _156 = &raw mut _3; StorageLive(_157); StorageLive(_158); @@ -882,10 +886,13 @@ StorageDead(_162); StorageDead(_161); _146 = const (); - StorageDead(_156); - StorageDead(_147); +- StorageDead(_156); +- StorageDead(_147); ++ nop; ++ nop; StorageDead(_146); - StorageLive(_165); +- StorageLive(_165); ++ nop; _165 = &_3; StorageLive(_166); - StorageLive(_167); @@ -927,9 +934,12 @@ StorageDead(_171); StorageDead(_170); _0 = const (); - StorageDead(_165); - StorageDead(_137); - StorageDead(_128); +- StorageDead(_165); +- StorageDead(_137); +- StorageDead(_128); ++ nop; ++ nop; ++ nop; return; } } From 48d2157a89ffddce3287c57b3e3817a7e093b6c5 Mon Sep 17 00:00:00 2001 From: Camille GILLOT Date: Sat, 16 Sep 2023 09:35:46 +0000 Subject: [PATCH 07/28] Simplify aggregate projections. --- compiler/rustc_mir_transform/src/gvn.rs | 41 +++++++++- .../gvn.references.GVN.panic-abort.diff | 21 +++-- .../gvn.references.GVN.panic-unwind.diff | 21 +++-- tests/mir-opt/gvn.slices.GVN.panic-abort.diff | 82 +++++++++++-------- .../mir-opt/gvn.slices.GVN.panic-unwind.diff | 82 +++++++++++-------- ...xpression_elimination.GVN.panic-abort.diff | 14 ++-- ...pression_elimination.GVN.panic-unwind.diff | 14 ++-- .../gvn.wrap_unwrap.GVN.panic-abort.diff | 18 ++-- .../gvn.wrap_unwrap.GVN.panic-unwind.diff | 18 ++-- 9 files changed, 192 insertions(+), 119 deletions(-) diff --git a/compiler/rustc_mir_transform/src/gvn.rs b/compiler/rustc_mir_transform/src/gvn.rs index eaa1c70a5e9b2..70d0f22a7b909 100644 --- a/compiler/rustc_mir_transform/src/gvn.rs +++ b/compiler/rustc_mir_transform/src/gvn.rs @@ -276,6 +276,10 @@ impl<'body, 'tcx> VnState<'body, 'tcx> { } } + fn insert_scalar(&mut self, scalar: Scalar, ty: Ty<'tcx>) -> VnIndex { + self.insert(Value::Constant(Const::from_scalar(self.tcx, scalar, ty))) + } + #[instrument(level = "trace", skip(self), ret)] fn eval_to_const(&mut self, value: VnIndex) -> Option> { use Value::*; @@ -483,12 +487,33 @@ impl<'body, 'tcx> VnState<'body, 'tcx> { } } ProjectionElem::Downcast(name, index) => ProjectionElem::Downcast(name, index), - ProjectionElem::Field(f, ty) => ProjectionElem::Field(f, ty), + ProjectionElem::Field(f, ty) => { + if let Value::Aggregate(_, _, fields) = self.get(value) { + return Some(fields[f.as_usize()]); + } else if let Value::Projection(outer_value, ProjectionElem::Downcast(_, read_variant)) = self.get(value) + && let Value::Aggregate(_, written_variant, fields) = self.get(*outer_value) + && written_variant == read_variant + { + return Some(fields[f.as_usize()]); + } + ProjectionElem::Field(f, ty) + } ProjectionElem::Index(idx) => { let idx = self.locals[idx]?; ProjectionElem::Index(idx) } ProjectionElem::ConstantIndex { offset, min_length, from_end } => { + match self.get(value) { + Value::Aggregate(ty, _, operands) if ty.is_array() => { + let offset = if from_end { + operands.len() - offset as usize + } else { + offset as usize + }; + return operands.get(offset).copied(); + } + _ => {} + }; ProjectionElem::ConstantIndex { offset, min_length, from_end } } ProjectionElem::Subslice { from, to, from_end } => { @@ -679,6 +704,9 @@ impl<'body, 'tcx> VnState<'body, 'tcx> { } Rvalue::Discriminant(ref mut place) => { let place = self.simplify_place_value(place, location)?; + if let Some(discr) = self.simplify_discriminant(place) { + return Some(discr); + } Value::Discriminant(place) } @@ -688,6 +716,17 @@ impl<'body, 'tcx> VnState<'body, 'tcx> { debug!(?value); Some(self.insert(value)) } + + fn simplify_discriminant(&mut self, place: VnIndex) -> Option { + if let Value::Aggregate(enum_ty, variant, _) = *self.get(place) + && enum_ty.is_enum() + { + let discr = self.ecx.discriminant_for_variant(enum_ty, variant).ok()?; + return Some(self.insert_scalar(discr.to_scalar(), discr.layout.ty)); + } + + None + } } fn op_to_prop_const<'tcx>( diff --git a/tests/mir-opt/gvn.references.GVN.panic-abort.diff b/tests/mir-opt/gvn.references.GVN.panic-abort.diff index 885431725657b..7799c61144526 100644 --- a/tests/mir-opt/gvn.references.GVN.panic-abort.diff +++ b/tests/mir-opt/gvn.references.GVN.panic-abort.diff @@ -115,8 +115,7 @@ - StorageLive(_18); + nop; _18 = &mut _1; -- StorageLive(_19); -+ nop; + StorageLive(_19); StorageLive(_20); StorageLive(_21); - _21 = move _18; @@ -124,11 +123,13 @@ + _21 = _18; + _20 = S::<&mut impl Sized>(_18); StorageDead(_21); - _19 = move (_20.0: &mut impl Sized); +- _19 = move (_20.0: &mut impl Sized); ++ _19 = _18; StorageDead(_20); StorageLive(_22); StorageLive(_23); - _23 = &(*_19); +- _23 = &(*_19); ++ _23 = &(*_18); _22 = opaque::<&impl Sized>(move _23) -> [return: bb9, unwind unreachable]; } @@ -137,7 +138,8 @@ StorageDead(_22); StorageLive(_24); StorageLive(_25); - _25 = &mut (*_19); +- _25 = &mut (*_19); ++ _25 = &mut (*_18); _24 = opaque::<&mut impl Sized>(move _25) -> [return: bb10, unwind unreachable]; } @@ -146,7 +148,8 @@ StorageDead(_24); StorageLive(_26); StorageLive(_27); - _27 = &raw const (*_19); +- _27 = &raw const (*_19); ++ _27 = &raw const (*_18); _26 = opaque::<*const impl Sized>(move _27) -> [return: bb11, unwind unreachable]; } @@ -155,7 +158,8 @@ StorageDead(_26); StorageLive(_28); StorageLive(_29); - _29 = &raw mut (*_19); +- _29 = &raw mut (*_19); ++ _29 = &raw mut (*_18); _28 = opaque::<*mut impl Sized>(move _29) -> [return: bb12, unwind unreachable]; } @@ -163,9 +167,8 @@ StorageDead(_29); StorageDead(_28); _0 = const (); -- StorageDead(_19); + StorageDead(_19); - StorageDead(_18); -+ nop; + nop; drop(_1) -> [return: bb13, unwind unreachable]; } diff --git a/tests/mir-opt/gvn.references.GVN.panic-unwind.diff b/tests/mir-opt/gvn.references.GVN.panic-unwind.diff index 106b453fe800d..880e7913fa94c 100644 --- a/tests/mir-opt/gvn.references.GVN.panic-unwind.diff +++ b/tests/mir-opt/gvn.references.GVN.panic-unwind.diff @@ -115,8 +115,7 @@ - StorageLive(_18); + nop; _18 = &mut _1; -- StorageLive(_19); -+ nop; + StorageLive(_19); StorageLive(_20); StorageLive(_21); - _21 = move _18; @@ -124,11 +123,13 @@ + _21 = _18; + _20 = S::<&mut impl Sized>(_18); StorageDead(_21); - _19 = move (_20.0: &mut impl Sized); +- _19 = move (_20.0: &mut impl Sized); ++ _19 = _18; StorageDead(_20); StorageLive(_22); StorageLive(_23); - _23 = &(*_19); +- _23 = &(*_19); ++ _23 = &(*_18); _22 = opaque::<&impl Sized>(move _23) -> [return: bb9, unwind: bb14]; } @@ -137,7 +138,8 @@ StorageDead(_22); StorageLive(_24); StorageLive(_25); - _25 = &mut (*_19); +- _25 = &mut (*_19); ++ _25 = &mut (*_18); _24 = opaque::<&mut impl Sized>(move _25) -> [return: bb10, unwind: bb14]; } @@ -146,7 +148,8 @@ StorageDead(_24); StorageLive(_26); StorageLive(_27); - _27 = &raw const (*_19); +- _27 = &raw const (*_19); ++ _27 = &raw const (*_18); _26 = opaque::<*const impl Sized>(move _27) -> [return: bb11, unwind: bb14]; } @@ -155,7 +158,8 @@ StorageDead(_26); StorageLive(_28); StorageLive(_29); - _29 = &raw mut (*_19); +- _29 = &raw mut (*_19); ++ _29 = &raw mut (*_18); _28 = opaque::<*mut impl Sized>(move _29) -> [return: bb12, unwind: bb14]; } @@ -163,9 +167,8 @@ StorageDead(_29); StorageDead(_28); _0 = const (); -- StorageDead(_19); + StorageDead(_19); - StorageDead(_18); -+ nop; + nop; drop(_1) -> [return: bb13, unwind: bb15]; } diff --git a/tests/mir-opt/gvn.slices.GVN.panic-abort.diff b/tests/mir-opt/gvn.slices.GVN.panic-abort.diff index 6168476d6f277..9db6e068fa7d6 100644 --- a/tests/mir-opt/gvn.slices.GVN.panic-abort.diff +++ b/tests/mir-opt/gvn.slices.GVN.panic-abort.diff @@ -112,7 +112,8 @@ StorageDead(_5); StorageLive(_7); StorageLive(_8); - StorageLive(_9); +- StorageLive(_9); ++ nop; StorageLive(_10); StorageLive(_11); _11 = &(*_1); @@ -122,7 +123,8 @@ bb3: { StorageDead(_11); _9 = &_10; - StorageLive(_12); +- StorageLive(_12); ++ nop; StorageLive(_13); StorageLive(_14); - _14 = &(*_4); @@ -133,20 +135,25 @@ bb4: { StorageDead(_14); _12 = &_13; - _8 = (move _9, move _12); - StorageDead(_12); - StorageDead(_9); -- StorageLive(_15); +- _8 = (move _9, move _12); +- StorageDead(_12); +- StorageDead(_9); ++ _8 = (_9, _12); + nop; - _15 = (_8.0: &*const u8); -- StorageLive(_16); + nop; - _16 = (_8.1: &*const u8); + StorageLive(_15); +- _15 = (_8.0: &*const u8); ++ _15 = _9; + StorageLive(_16); +- _16 = (_8.1: &*const u8); ++ _16 = _12; StorageLive(_17); StorageLive(_18); - _18 = (*_15); +- _18 = (*_15); ++ _18 = (*_9); StorageLive(_19); - _19 = (*_16); +- _19 = (*_16); ++ _19 = (*_12); _17 = Eq(move _18, move _19); switchInt(move _17) -> [0: bb6, otherwise: bb5]; } @@ -156,10 +163,8 @@ StorageDead(_18); _7 = const (); StorageDead(_17); -- StorageDead(_16); -- StorageDead(_15); -+ nop; -+ nop; + StorageDead(_16); + StorageDead(_15); StorageDead(_13); StorageDead(_10); StorageDead(_8); @@ -190,13 +195,15 @@ + _23 = const core::panicking::AssertKind::Eq; StorageLive(_24); - StorageLive(_25); +- _25 = &(*_15); + nop; - _25 = &(*_15); ++ _25 = &(*_9); _24 = &(*_25); StorageLive(_26); - StorageLive(_27); +- _27 = &(*_16); + nop; - _27 = &(*_16); ++ _27 = &(*_12); _26 = &(*_27); StorageLive(_28); _28 = Option::>::None; @@ -209,7 +216,8 @@ StorageDead(_31); StorageLive(_33); StorageLive(_34); - StorageLive(_35); +- StorageLive(_35); ++ nop; StorageLive(_36); StorageLive(_37); _37 = &(*_1); @@ -219,7 +227,8 @@ bb8: { StorageDead(_37); _35 = &_36; - StorageLive(_38); +- StorageLive(_38); ++ nop; StorageLive(_39); StorageLive(_40); _40 = &(*_29); @@ -229,20 +238,25 @@ bb9: { StorageDead(_40); _38 = &_39; - _34 = (move _35, move _38); - StorageDead(_38); - StorageDead(_35); -- StorageLive(_41); +- _34 = (move _35, move _38); +- StorageDead(_38); +- StorageDead(_35); ++ _34 = (_35, _38); + nop; - _41 = (_34.0: &*const u8); -- StorageLive(_42); + nop; - _42 = (_34.1: &*const u8); + StorageLive(_41); +- _41 = (_34.0: &*const u8); ++ _41 = _35; + StorageLive(_42); +- _42 = (_34.1: &*const u8); ++ _42 = _38; StorageLive(_43); StorageLive(_44); - _44 = (*_41); +- _44 = (*_41); ++ _44 = (*_35); StorageLive(_45); - _45 = (*_42); +- _45 = (*_42); ++ _45 = (*_38); _43 = Eq(move _44, move _45); switchInt(move _43) -> [0: bb11, otherwise: bb10]; } @@ -252,10 +266,8 @@ StorageDead(_44); _33 = const (); StorageDead(_43); -- StorageDead(_42); -- StorageDead(_41); -+ nop; -+ nop; + StorageDead(_42); + StorageDead(_41); StorageDead(_39); StorageDead(_36); StorageDead(_34); @@ -282,13 +294,15 @@ + _49 = const core::panicking::AssertKind::Eq; StorageLive(_50); - StorageLive(_51); +- _51 = &(*_41); + nop; - _51 = &(*_41); ++ _51 = &(*_35); _50 = &(*_51); StorageLive(_52); - StorageLive(_53); +- _53 = &(*_42); + nop; - _53 = &(*_42); ++ _53 = &(*_38); _52 = &(*_53); StorageLive(_54); _54 = Option::>::None; diff --git a/tests/mir-opt/gvn.slices.GVN.panic-unwind.diff b/tests/mir-opt/gvn.slices.GVN.panic-unwind.diff index f60038858089a..ac7a3e7468885 100644 --- a/tests/mir-opt/gvn.slices.GVN.panic-unwind.diff +++ b/tests/mir-opt/gvn.slices.GVN.panic-unwind.diff @@ -112,7 +112,8 @@ StorageDead(_5); StorageLive(_7); StorageLive(_8); - StorageLive(_9); +- StorageLive(_9); ++ nop; StorageLive(_10); StorageLive(_11); _11 = &(*_1); @@ -122,7 +123,8 @@ bb3: { StorageDead(_11); _9 = &_10; - StorageLive(_12); +- StorageLive(_12); ++ nop; StorageLive(_13); StorageLive(_14); - _14 = &(*_4); @@ -133,20 +135,25 @@ bb4: { StorageDead(_14); _12 = &_13; - _8 = (move _9, move _12); - StorageDead(_12); - StorageDead(_9); -- StorageLive(_15); +- _8 = (move _9, move _12); +- StorageDead(_12); +- StorageDead(_9); ++ _8 = (_9, _12); + nop; - _15 = (_8.0: &*const u8); -- StorageLive(_16); + nop; - _16 = (_8.1: &*const u8); + StorageLive(_15); +- _15 = (_8.0: &*const u8); ++ _15 = _9; + StorageLive(_16); +- _16 = (_8.1: &*const u8); ++ _16 = _12; StorageLive(_17); StorageLive(_18); - _18 = (*_15); +- _18 = (*_15); ++ _18 = (*_9); StorageLive(_19); - _19 = (*_16); +- _19 = (*_16); ++ _19 = (*_12); _17 = Eq(move _18, move _19); switchInt(move _17) -> [0: bb6, otherwise: bb5]; } @@ -156,10 +163,8 @@ StorageDead(_18); _7 = const (); StorageDead(_17); -- StorageDead(_16); -- StorageDead(_15); -+ nop; -+ nop; + StorageDead(_16); + StorageDead(_15); StorageDead(_13); StorageDead(_10); StorageDead(_8); @@ -190,13 +195,15 @@ + _23 = const core::panicking::AssertKind::Eq; StorageLive(_24); - StorageLive(_25); +- _25 = &(*_15); + nop; - _25 = &(*_15); ++ _25 = &(*_9); _24 = &(*_25); StorageLive(_26); - StorageLive(_27); +- _27 = &(*_16); + nop; - _27 = &(*_16); ++ _27 = &(*_12); _26 = &(*_27); StorageLive(_28); _28 = Option::>::None; @@ -209,7 +216,8 @@ StorageDead(_31); StorageLive(_33); StorageLive(_34); - StorageLive(_35); +- StorageLive(_35); ++ nop; StorageLive(_36); StorageLive(_37); _37 = &(*_1); @@ -219,7 +227,8 @@ bb8: { StorageDead(_37); _35 = &_36; - StorageLive(_38); +- StorageLive(_38); ++ nop; StorageLive(_39); StorageLive(_40); _40 = &(*_29); @@ -229,20 +238,25 @@ bb9: { StorageDead(_40); _38 = &_39; - _34 = (move _35, move _38); - StorageDead(_38); - StorageDead(_35); -- StorageLive(_41); +- _34 = (move _35, move _38); +- StorageDead(_38); +- StorageDead(_35); ++ _34 = (_35, _38); + nop; - _41 = (_34.0: &*const u8); -- StorageLive(_42); + nop; - _42 = (_34.1: &*const u8); + StorageLive(_41); +- _41 = (_34.0: &*const u8); ++ _41 = _35; + StorageLive(_42); +- _42 = (_34.1: &*const u8); ++ _42 = _38; StorageLive(_43); StorageLive(_44); - _44 = (*_41); +- _44 = (*_41); ++ _44 = (*_35); StorageLive(_45); - _45 = (*_42); +- _45 = (*_42); ++ _45 = (*_38); _43 = Eq(move _44, move _45); switchInt(move _43) -> [0: bb11, otherwise: bb10]; } @@ -252,10 +266,8 @@ StorageDead(_44); _33 = const (); StorageDead(_43); -- StorageDead(_42); -- StorageDead(_41); -+ nop; -+ nop; + StorageDead(_42); + StorageDead(_41); StorageDead(_39); StorageDead(_36); StorageDead(_34); @@ -282,13 +294,15 @@ + _49 = const core::panicking::AssertKind::Eq; StorageLive(_50); - StorageLive(_51); +- _51 = &(*_41); + nop; - _51 = &(*_41); ++ _51 = &(*_35); _50 = &(*_51); StorageLive(_52); - StorageLive(_53); +- _53 = &(*_42); + nop; - _53 = &(*_42); ++ _53 = &(*_38); _52 = &(*_53); StorageLive(_54); _54 = Option::>::None; diff --git a/tests/mir-opt/gvn.subexpression_elimination.GVN.panic-abort.diff b/tests/mir-opt/gvn.subexpression_elimination.GVN.panic-abort.diff index 41f29959c9538..23ba2559448b8 100644 --- a/tests/mir-opt/gvn.subexpression_elimination.GVN.panic-abort.diff +++ b/tests/mir-opt/gvn.subexpression_elimination.GVN.panic-abort.diff @@ -442,8 +442,7 @@ + nop; StorageDead(_52); StorageLive(_55); -- StorageLive(_56); -+ nop; + StorageLive(_56); StorageLive(_57); StorageLive(_58); _58 = _1; @@ -452,13 +451,12 @@ StorageDead(_58); - _56 = (_57.0: u64); - _55 = opaque::(move _56) -> [return: bb16, unwind unreachable]; -+ _56 = (_53.0: u64); -+ _55 = opaque::(_56) -> [return: bb16, unwind unreachable]; ++ _56 = _1; ++ _55 = opaque::(_1) -> [return: bb16, unwind unreachable]; } bb16: { -- StorageDead(_56); -+ nop; + StorageDead(_56); StorageDead(_57); StorageDead(_55); StorageLive(_59); @@ -729,8 +727,8 @@ StorageDead(_127); - _125 = (_126.0: u64); - _124 = opaque::(move _125) -> [return: bb30, unwind unreachable]; -+ _125 = _56; -+ _124 = opaque::(_56) -> [return: bb30, unwind unreachable]; ++ _125 = _1; ++ _124 = opaque::(_1) -> [return: bb30, unwind unreachable]; } bb30: { diff --git a/tests/mir-opt/gvn.subexpression_elimination.GVN.panic-unwind.diff b/tests/mir-opt/gvn.subexpression_elimination.GVN.panic-unwind.diff index ca928986cf655..062dc6ff56190 100644 --- a/tests/mir-opt/gvn.subexpression_elimination.GVN.panic-unwind.diff +++ b/tests/mir-opt/gvn.subexpression_elimination.GVN.panic-unwind.diff @@ -442,8 +442,7 @@ + nop; StorageDead(_52); StorageLive(_55); -- StorageLive(_56); -+ nop; + StorageLive(_56); StorageLive(_57); StorageLive(_58); _58 = _1; @@ -452,13 +451,12 @@ StorageDead(_58); - _56 = (_57.0: u64); - _55 = opaque::(move _56) -> [return: bb16, unwind continue]; -+ _56 = (_53.0: u64); -+ _55 = opaque::(_56) -> [return: bb16, unwind continue]; ++ _56 = _1; ++ _55 = opaque::(_1) -> [return: bb16, unwind continue]; } bb16: { -- StorageDead(_56); -+ nop; + StorageDead(_56); StorageDead(_57); StorageDead(_55); StorageLive(_59); @@ -729,8 +727,8 @@ StorageDead(_127); - _125 = (_126.0: u64); - _124 = opaque::(move _125) -> [return: bb30, unwind continue]; -+ _125 = _56; -+ _124 = opaque::(_56) -> [return: bb30, unwind continue]; ++ _125 = _1; ++ _124 = opaque::(_1) -> [return: bb30, unwind continue]; } bb30: { diff --git a/tests/mir-opt/gvn.wrap_unwrap.GVN.panic-abort.diff b/tests/mir-opt/gvn.wrap_unwrap.GVN.panic-abort.diff index 2dd51934778ca..62710ba8fbf5f 100644 --- a/tests/mir-opt/gvn.wrap_unwrap.GVN.panic-abort.diff +++ b/tests/mir-opt/gvn.wrap_unwrap.GVN.panic-abort.diff @@ -20,8 +20,10 @@ - _2 = Option::::Some(move _3); + _2 = Option::::Some(_1); StorageDead(_3); - _4 = discriminant(_2); - switchInt(move _4) -> [0: bb1, 1: bb3, otherwise: bb2]; +- _4 = discriminant(_2); +- switchInt(move _4) -> [0: bb1, 1: bb3, otherwise: bb2]; ++ _4 = const 1_isize; ++ switchInt(const 1_isize) -> [0: bb1, 1: bb3, otherwise: bb2]; } bb1: { @@ -34,12 +36,12 @@ } bb3: { -- StorageLive(_5); -+ nop; - _5 = ((_2 as Some).0: T); - _0 = _5; -- StorageDead(_5); -+ nop; + StorageLive(_5); +- _5 = ((_2 as Some).0: T); +- _0 = _5; ++ _5 = _1; ++ _0 = _1; + StorageDead(_5); StorageDead(_2); return; } diff --git a/tests/mir-opt/gvn.wrap_unwrap.GVN.panic-unwind.diff b/tests/mir-opt/gvn.wrap_unwrap.GVN.panic-unwind.diff index 610d70576c9fd..ad46a065b1e3d 100644 --- a/tests/mir-opt/gvn.wrap_unwrap.GVN.panic-unwind.diff +++ b/tests/mir-opt/gvn.wrap_unwrap.GVN.panic-unwind.diff @@ -20,8 +20,10 @@ - _2 = Option::::Some(move _3); + _2 = Option::::Some(_1); StorageDead(_3); - _4 = discriminant(_2); - switchInt(move _4) -> [0: bb1, 1: bb3, otherwise: bb2]; +- _4 = discriminant(_2); +- switchInt(move _4) -> [0: bb1, 1: bb3, otherwise: bb2]; ++ _4 = const 1_isize; ++ switchInt(const 1_isize) -> [0: bb1, 1: bb3, otherwise: bb2]; } bb1: { @@ -34,12 +36,12 @@ } bb3: { -- StorageLive(_5); -+ nop; - _5 = ((_2 as Some).0: T); - _0 = _5; -- StorageDead(_5); -+ nop; + StorageLive(_5); +- _5 = ((_2 as Some).0: T); +- _0 = _5; ++ _5 = _1; ++ _0 = _1; + StorageDead(_5); StorageDead(_2); return; } From f110f22060b86dd2873d7ac6d7a4e444c9348b1d Mon Sep 17 00:00:00 2001 From: Camille GILLOT Date: Sat, 16 Sep 2023 09:36:16 +0000 Subject: [PATCH 08/28] Simplify repeat expressions. --- compiler/rustc_mir_transform/src/gvn.rs | 6 ++++++ tests/mir-opt/gvn.repeated_index.GVN.panic-abort.diff | 10 ++++++---- tests/mir-opt/gvn.repeated_index.GVN.panic-unwind.diff | 10 ++++++---- 3 files changed, 18 insertions(+), 8 deletions(-) diff --git a/compiler/rustc_mir_transform/src/gvn.rs b/compiler/rustc_mir_transform/src/gvn.rs index 70d0f22a7b909..66442849863e7 100644 --- a/compiler/rustc_mir_transform/src/gvn.rs +++ b/compiler/rustc_mir_transform/src/gvn.rs @@ -499,11 +499,17 @@ impl<'body, 'tcx> VnState<'body, 'tcx> { ProjectionElem::Field(f, ty) } ProjectionElem::Index(idx) => { + if let Value::Repeat(inner, _) = self.get(value) { + return Some(*inner); + } let idx = self.locals[idx]?; ProjectionElem::Index(idx) } ProjectionElem::ConstantIndex { offset, min_length, from_end } => { match self.get(value) { + Value::Repeat(inner, _) => { + return Some(*inner); + } Value::Aggregate(ty, _, operands) if ty.is_array() => { let offset = if from_end { operands.len() - offset as usize diff --git a/tests/mir-opt/gvn.repeated_index.GVN.panic-abort.diff b/tests/mir-opt/gvn.repeated_index.GVN.panic-abort.diff index 91af1ccc33a92..d937902e89138 100644 --- a/tests/mir-opt/gvn.repeated_index.GVN.panic-abort.diff +++ b/tests/mir-opt/gvn.repeated_index.GVN.panic-abort.diff @@ -41,8 +41,9 @@ bb1: { - _6 = _3[_7]; -+ _6 = _3[0 of 1]; - _5 = opaque::(move _6) -> [return: bb2, unwind unreachable]; +- _5 = opaque::(move _6) -> [return: bb2, unwind unreachable]; ++ _6 = _1; ++ _5 = opaque::(_1) -> [return: bb2, unwind unreachable]; } bb2: { @@ -63,8 +64,9 @@ bb3: { - _11 = _3[_12]; -+ _11 = _3[_2]; - _10 = opaque::(move _11) -> [return: bb4, unwind unreachable]; +- _10 = opaque::(move _11) -> [return: bb4, unwind unreachable]; ++ _11 = _1; ++ _10 = opaque::(_1) -> [return: bb4, unwind unreachable]; } bb4: { diff --git a/tests/mir-opt/gvn.repeated_index.GVN.panic-unwind.diff b/tests/mir-opt/gvn.repeated_index.GVN.panic-unwind.diff index f887094b509a3..dd4d24b12ea61 100644 --- a/tests/mir-opt/gvn.repeated_index.GVN.panic-unwind.diff +++ b/tests/mir-opt/gvn.repeated_index.GVN.panic-unwind.diff @@ -41,8 +41,9 @@ bb1: { - _6 = _3[_7]; -+ _6 = _3[0 of 1]; - _5 = opaque::(move _6) -> [return: bb2, unwind continue]; +- _5 = opaque::(move _6) -> [return: bb2, unwind continue]; ++ _6 = _1; ++ _5 = opaque::(_1) -> [return: bb2, unwind continue]; } bb2: { @@ -63,8 +64,9 @@ bb3: { - _11 = _3[_12]; -+ _11 = _3[_2]; - _10 = opaque::(move _11) -> [return: bb4, unwind continue]; +- _10 = opaque::(move _11) -> [return: bb4, unwind continue]; ++ _11 = _1; ++ _10 = opaque::(_1) -> [return: bb4, unwind continue]; } bb4: { From 23d4857080a3968447adbb1d55b2720dba46d666 Mon Sep 17 00:00:00 2001 From: Camille GILLOT Date: Wed, 3 May 2023 18:36:53 +0000 Subject: [PATCH 09/28] Do not compute actual aggregate type. --- compiler/rustc_mir_transform/src/gvn.rs | 58 ++++++++++++++++++++----- 1 file changed, 47 insertions(+), 11 deletions(-) diff --git a/compiler/rustc_mir_transform/src/gvn.rs b/compiler/rustc_mir_transform/src/gvn.rs index 66442849863e7..9880e239957ad 100644 --- a/compiler/rustc_mir_transform/src/gvn.rs +++ b/compiler/rustc_mir_transform/src/gvn.rs @@ -56,6 +56,7 @@ use rustc_const_eval::interpret::{ImmTy, InterpCx, MemPlaceMeta, OpTy, Projectable, Scalar}; use rustc_data_structures::fx::{FxHashMap, FxIndexSet}; use rustc_data_structures::graph::dominators::Dominators; +use rustc_hir::def::DefKind; use rustc_index::bit_set::BitSet; use rustc_index::IndexVec; use rustc_macros::newtype_index; @@ -64,6 +65,7 @@ use rustc_middle::mir::visit::*; use rustc_middle::mir::*; use rustc_middle::ty::layout::LayoutOf; use rustc_middle::ty::{self, Ty, TyCtxt, TypeAndMut}; +use rustc_span::def_id::DefId; use rustc_span::DUMMY_SP; use rustc_target::abi::{self, Abi, Size, VariantIdx, FIRST_VARIANT}; use std::borrow::Cow; @@ -136,6 +138,16 @@ newtype_index! { struct VnIndex {} } +/// Computing the aggregate's type can be quite slow, so we only keep the minimal amount of +/// information to reconstruct it when needed. +#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)] +enum AggregateTy<'tcx> { + /// Invariant: this must not be used for an empty array. + Array, + Tuple, + Def(DefId, ty::GenericArgsRef<'tcx>), +} + #[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)] enum AddressKind { Ref(BorrowKind), @@ -152,7 +164,7 @@ enum Value<'tcx> { Constant(Const<'tcx>), /// An aggregate value, either tuple/closure/struct/enum. /// This does not contain unions, as we cannot reason with the value. - Aggregate(Ty<'tcx>, VariantIdx, Vec), + Aggregate(AggregateTy<'tcx>, VariantIdx, Vec), /// This corresponds to a `[value; count]` expression. Repeat(VnIndex, ty::Const<'tcx>), /// The address of a place. @@ -289,11 +301,23 @@ impl<'body, 'tcx> VnState<'body, 'tcx> { Repeat(..) => return None, Constant(ref constant) => self.ecx.eval_mir_constant(constant, None, None).ok()?, - Aggregate(ty, variant, ref fields) => { + Aggregate(kind, variant, ref fields) => { let fields = fields .iter() .map(|&f| self.evaluated[f].as_ref()) .collect::>>()?; + let ty = match kind { + AggregateTy::Array => { + assert!(fields.len() > 0); + Ty::new_array(self.tcx, fields[0].layout.ty, fields.len() as u64) + } + AggregateTy::Tuple => { + Ty::new_tup_from_iter(self.tcx, fields.iter().map(|f| f.layout.ty)) + } + AggregateTy::Def(def_id, args) => { + self.tcx.type_of(def_id).instantiate(self.tcx, args) + } + }; let variant = if ty.is_enum() { Some(variant) } else { None }; let ty = self.ecx.layout_of(ty).ok()?; if ty.is_zst() { @@ -510,7 +534,7 @@ impl<'body, 'tcx> VnState<'body, 'tcx> { Value::Repeat(inner, _) => { return Some(*inner); } - Value::Aggregate(ty, _, operands) if ty.is_array() => { + Value::Aggregate(AggregateTy::Array, _, operands) => { let offset = if from_end { operands.len() - offset as usize } else { @@ -659,12 +683,23 @@ impl<'body, 'tcx> VnState<'body, 'tcx> { } Rvalue::NullaryOp(op, ty) => Value::NullaryOp(op, ty), Rvalue::Aggregate(box ref kind, ref mut fields) => { - let variant_index = match *kind { - AggregateKind::Array(..) - | AggregateKind::Tuple - | AggregateKind::Closure(..) - | AggregateKind::Coroutine(..) => FIRST_VARIANT, - AggregateKind::Adt(_, variant_index, _, _, None) => variant_index, + let (ty, variant_index) = match *kind { + // For empty arrays, we have not mean to recover the type. They are ZSTs + // anyway, so return them as such. + AggregateKind::Array(..) | AggregateKind::Tuple if fields.is_empty() => { + return Some(self.insert(Value::Constant(Const::zero_sized( + rvalue.ty(self.local_decls, self.tcx), + )))); + } + AggregateKind::Array(..) => (AggregateTy::Array, FIRST_VARIANT), + AggregateKind::Tuple => (AggregateTy::Tuple, FIRST_VARIANT), + AggregateKind::Closure(did, substs) + | AggregateKind::Coroutine(did, substs, _) => { + (AggregateTy::Def(did, substs), FIRST_VARIANT) + } + AggregateKind::Adt(did, variant_index, substs, _, None) => { + (AggregateTy::Def(did, substs), variant_index) + } // Do not track unions. AggregateKind::Adt(_, _, _, _, Some(_)) => return None, }; @@ -672,7 +707,6 @@ impl<'body, 'tcx> VnState<'body, 'tcx> { .iter_mut() .map(|op| self.simplify_operand(op, location).or_else(|| self.new_opaque())) .collect(); - let ty = rvalue.ty(self.local_decls, self.tcx); Value::Aggregate(ty, variant_index, fields?) } Rvalue::Ref(_, borrow_kind, ref mut place) => { @@ -725,8 +759,10 @@ impl<'body, 'tcx> VnState<'body, 'tcx> { fn simplify_discriminant(&mut self, place: VnIndex) -> Option { if let Value::Aggregate(enum_ty, variant, _) = *self.get(place) - && enum_ty.is_enum() + && let AggregateTy::Def(enum_did, enum_substs) = enum_ty + && let DefKind::Enum = self.tcx.def_kind(enum_did) { + let enum_ty = self.tcx.type_of(enum_did).instantiate(self.tcx, enum_substs); let discr = self.ecx.discriminant_for_variant(enum_ty, variant).ok()?; return Some(self.insert_scalar(discr.to_scalar(), discr.layout.ty)); } From 80a5e8522de648d70a9ef1dfba7621f730832fb3 Mon Sep 17 00:00:00 2001 From: Camille GILLOT Date: Sun, 21 May 2023 12:59:38 +0000 Subject: [PATCH 10/28] Extract simplify_aggregate. --- compiler/rustc_mir_transform/src/gvn.rs | 83 +++++++++++++------ .../gvn.aggregates.GVN.panic-abort.diff | 66 +++++++++++++++ .../gvn.aggregates.GVN.panic-unwind.diff | 66 +++++++++++++++ tests/mir-opt/gvn.rs | 10 +++ 4 files changed, 198 insertions(+), 27 deletions(-) create mode 100644 tests/mir-opt/gvn.aggregates.GVN.panic-abort.diff create mode 100644 tests/mir-opt/gvn.aggregates.GVN.panic-unwind.diff diff --git a/compiler/rustc_mir_transform/src/gvn.rs b/compiler/rustc_mir_transform/src/gvn.rs index 9880e239957ad..bbcb39226b7fe 100644 --- a/compiler/rustc_mir_transform/src/gvn.rs +++ b/compiler/rustc_mir_transform/src/gvn.rs @@ -682,33 +682,7 @@ impl<'body, 'tcx> VnState<'body, 'tcx> { Value::Repeat(op, amount) } Rvalue::NullaryOp(op, ty) => Value::NullaryOp(op, ty), - Rvalue::Aggregate(box ref kind, ref mut fields) => { - let (ty, variant_index) = match *kind { - // For empty arrays, we have not mean to recover the type. They are ZSTs - // anyway, so return them as such. - AggregateKind::Array(..) | AggregateKind::Tuple if fields.is_empty() => { - return Some(self.insert(Value::Constant(Const::zero_sized( - rvalue.ty(self.local_decls, self.tcx), - )))); - } - AggregateKind::Array(..) => (AggregateTy::Array, FIRST_VARIANT), - AggregateKind::Tuple => (AggregateTy::Tuple, FIRST_VARIANT), - AggregateKind::Closure(did, substs) - | AggregateKind::Coroutine(did, substs, _) => { - (AggregateTy::Def(did, substs), FIRST_VARIANT) - } - AggregateKind::Adt(did, variant_index, substs, _, None) => { - (AggregateTy::Def(did, substs), variant_index) - } - // Do not track unions. - AggregateKind::Adt(_, _, _, _, Some(_)) => return None, - }; - let fields: Option> = fields - .iter_mut() - .map(|op| self.simplify_operand(op, location).or_else(|| self.new_opaque())) - .collect(); - Value::Aggregate(ty, variant_index, fields?) - } + Rvalue::Aggregate(..) => self.simplify_aggregate(rvalue, location)?, Rvalue::Ref(_, borrow_kind, ref mut place) => { self.simplify_place_projection(place, location); return self.new_pointer(*place, AddressKind::Ref(borrow_kind)); @@ -769,6 +743,61 @@ impl<'body, 'tcx> VnState<'body, 'tcx> { None } + + fn simplify_aggregate( + &mut self, + rvalue: &mut Rvalue<'tcx>, + location: Location, + ) -> Option> { + let Rvalue::Aggregate(box ref kind, ref mut fields) = *rvalue else { bug!() }; + + let tcx = self.tcx; + if fields.is_empty() { + let is_zst = match *kind { + AggregateKind::Array(..) | AggregateKind::Tuple | AggregateKind::Closure(..) => { + true + } + // Only enums can be non-ZST. + AggregateKind::Adt(did, ..) => tcx.def_kind(did) != DefKind::Enum, + // Coroutines are never ZST, as they at least contain the implicit states. + AggregateKind::Coroutine(..) => false, + }; + + if is_zst { + let ty = rvalue.ty(self.local_decls, tcx); + let value = Value::Constant(Const::zero_sized(ty)); + return Some(value); + } + } + + let (ty, variant_index) = match *kind { + AggregateKind::Array(..) => { + assert!(!fields.is_empty()); + (AggregateTy::Array, FIRST_VARIANT) + } + AggregateKind::Tuple => { + assert!(!fields.is_empty()); + (AggregateTy::Tuple, FIRST_VARIANT) + } + AggregateKind::Closure(did, substs) | AggregateKind::Coroutine(did, substs, _) => { + (AggregateTy::Def(did, substs), FIRST_VARIANT) + } + AggregateKind::Adt(did, variant_index, substs, _, None) => { + (AggregateTy::Def(did, substs), variant_index) + } + // Do not track unions. + AggregateKind::Adt(_, _, _, _, Some(_)) => return None, + }; + + let fields: Option> = fields + .iter_mut() + .map(|op| self.simplify_operand(op, location).or_else(|| self.new_opaque())) + .collect(); + let fields = fields?; + + let value = Value::Aggregate(ty, variant_index, fields); + Some(value) + } } fn op_to_prop_const<'tcx>( diff --git a/tests/mir-opt/gvn.aggregates.GVN.panic-abort.diff b/tests/mir-opt/gvn.aggregates.GVN.panic-abort.diff new file mode 100644 index 0000000000000..c442835bfa99e --- /dev/null +++ b/tests/mir-opt/gvn.aggregates.GVN.panic-abort.diff @@ -0,0 +1,66 @@ +- // MIR for `aggregates` before GVN ++ // MIR for `aggregates` after GVN + + fn aggregates() -> () { + let mut _0: (); + let _1: S<[u8; 0]>; + let mut _2: [u8; 0]; + let mut _4: [u16; 0]; + let mut _6: (); + let mut _8: (); + scope 1 { + debug a_array => _1; + let _3: S<[u16; 0]>; + scope 2 { + debug b_array => _3; + let _5: S<()>; + scope 3 { + debug a_tuple => _5; + let _7: S<()>; + scope 4 { + debug b_tuple => _7; + } + } + } + } + + bb0: { + StorageLive(_1); + StorageLive(_2); +- _2 = []; +- _1 = S::<[u8; 0]>(move _2); ++ _2 = const []; ++ _1 = const S::<[u8; 0]>([]); + StorageDead(_2); + StorageLive(_3); + StorageLive(_4); +- _4 = []; +- _3 = S::<[u16; 0]>(move _4); ++ _4 = const []; ++ _3 = const S::<[u16; 0]>([]); + StorageDead(_4); + StorageLive(_5); +- StorageLive(_6); +- _6 = (); +- _5 = S::<()>(move _6); +- StorageDead(_6); ++ nop; ++ _6 = const (); ++ _5 = const S::<()>(()); ++ nop; + StorageLive(_7); + StorageLive(_8); +- _8 = (); +- _7 = S::<()>(move _8); ++ _8 = const (); ++ _7 = const S::<()>(()); + StorageDead(_8); + _0 = const (); + StorageDead(_7); + StorageDead(_5); + StorageDead(_3); + StorageDead(_1); + return; + } + } + diff --git a/tests/mir-opt/gvn.aggregates.GVN.panic-unwind.diff b/tests/mir-opt/gvn.aggregates.GVN.panic-unwind.diff new file mode 100644 index 0000000000000..c442835bfa99e --- /dev/null +++ b/tests/mir-opt/gvn.aggregates.GVN.panic-unwind.diff @@ -0,0 +1,66 @@ +- // MIR for `aggregates` before GVN ++ // MIR for `aggregates` after GVN + + fn aggregates() -> () { + let mut _0: (); + let _1: S<[u8; 0]>; + let mut _2: [u8; 0]; + let mut _4: [u16; 0]; + let mut _6: (); + let mut _8: (); + scope 1 { + debug a_array => _1; + let _3: S<[u16; 0]>; + scope 2 { + debug b_array => _3; + let _5: S<()>; + scope 3 { + debug a_tuple => _5; + let _7: S<()>; + scope 4 { + debug b_tuple => _7; + } + } + } + } + + bb0: { + StorageLive(_1); + StorageLive(_2); +- _2 = []; +- _1 = S::<[u8; 0]>(move _2); ++ _2 = const []; ++ _1 = const S::<[u8; 0]>([]); + StorageDead(_2); + StorageLive(_3); + StorageLive(_4); +- _4 = []; +- _3 = S::<[u16; 0]>(move _4); ++ _4 = const []; ++ _3 = const S::<[u16; 0]>([]); + StorageDead(_4); + StorageLive(_5); +- StorageLive(_6); +- _6 = (); +- _5 = S::<()>(move _6); +- StorageDead(_6); ++ nop; ++ _6 = const (); ++ _5 = const S::<()>(()); ++ nop; + StorageLive(_7); + StorageLive(_8); +- _8 = (); +- _7 = S::<()>(move _8); ++ _8 = const (); ++ _7 = const S::<()>(()); + StorageDead(_8); + _0 = const (); + StorageDead(_7); + StorageDead(_5); + StorageDead(_3); + StorageDead(_1); + return; + } + } + diff --git a/tests/mir-opt/gvn.rs b/tests/mir-opt/gvn.rs index 3633f9c23cd51..ff176a6597b43 100644 --- a/tests/mir-opt/gvn.rs +++ b/tests/mir-opt/gvn.rs @@ -231,6 +231,14 @@ fn slices() { assert_eq!(s.as_ptr(), u.as_ptr()); } +fn aggregates() { + let a_array: S<[u8; 0]> = S([]); + let b_array: S<[u16; 0]> = S([]); // This must not be merged with `a_array`. + + let a_tuple: S<()> = S(()); + let b_tuple: S<()> = S(()); // But this can be with `a_tuple`. +} + fn main() { subexpression_elimination(2, 4, 5); wrap_unwrap(5); @@ -243,6 +251,7 @@ fn main() { references(5); dereferences(&mut 5, &6, &S(7)); slices(); + aggregates(); } #[inline(never)] @@ -259,3 +268,4 @@ fn opaque(_: impl Sized) {} // EMIT_MIR gvn.references.GVN.diff // EMIT_MIR gvn.dereferences.GVN.diff // EMIT_MIR gvn.slices.GVN.diff +// EMIT_MIR gvn.aggregates.GVN.diff From dbf9ea30dd18db67cdb3f1271c7297876b65e675 Mon Sep 17 00:00:00 2001 From: Camille GILLOT Date: Sat, 23 Sep 2023 16:23:37 +0000 Subject: [PATCH 11/28] Transform large arrays into Repeat expressions when possible. --- compiler/rustc_mir_transform/src/gvn.rs | 14 ++++ .../gvn.aggregates.GVN.panic-abort.diff | 67 +++++++++++++++++++ .../gvn.aggregates.GVN.panic-unwind.diff | 67 +++++++++++++++++++ tests/mir-opt/gvn.rs | 4 +- 4 files changed, 151 insertions(+), 1 deletion(-) diff --git a/compiler/rustc_mir_transform/src/gvn.rs b/compiler/rustc_mir_transform/src/gvn.rs index bbcb39226b7fe..f71e9de4123d2 100644 --- a/compiler/rustc_mir_transform/src/gvn.rs +++ b/compiler/rustc_mir_transform/src/gvn.rs @@ -795,6 +795,20 @@ impl<'body, 'tcx> VnState<'body, 'tcx> { .collect(); let fields = fields?; + if let AggregateTy::Array = ty && fields.len() > 4 { + let first = fields[0]; + if fields.iter().all(|&v| v == first) { + let len = ty::Const::from_target_usize(self.tcx, fields.len().try_into().unwrap()); + if let Some(const_) = self.try_as_constant(first) { + *rvalue = Rvalue::Repeat(Operand::Constant(Box::new(const_)), len); + } else if let Some(local) = self.try_as_local(first, location) { + *rvalue = Rvalue::Repeat(Operand::Copy(local.into()), len); + self.reused_locals.insert(local); + } + return Some(Value::Repeat(first, len)); + } + } + let value = Value::Aggregate(ty, variant_index, fields); Some(value) } diff --git a/tests/mir-opt/gvn.aggregates.GVN.panic-abort.diff b/tests/mir-opt/gvn.aggregates.GVN.panic-abort.diff index c442835bfa99e..b1dae36ab6c30 100644 --- a/tests/mir-opt/gvn.aggregates.GVN.panic-abort.diff +++ b/tests/mir-opt/gvn.aggregates.GVN.panic-abort.diff @@ -8,6 +8,16 @@ let mut _4: [u16; 0]; let mut _6: (); let mut _8: (); + let mut _11: i32; + let mut _12: i32; + let mut _13: i32; + let mut _14: i32; + let mut _15: i32; + let mut _16: i32; + let mut _17: i32; + let mut _18: i32; + let mut _19: i32; + let mut _20: i32; scope 1 { debug a_array => _1; let _3: S<[u16; 0]>; @@ -19,6 +29,14 @@ let _7: S<()>; scope 4 { debug b_tuple => _7; + let _9: i32; + scope 5 { + debug val => _9; + let _10: [i32; 10]; + scope 6 { + debug array => _10; + } + } } } } @@ -55,7 +73,56 @@ + _8 = const (); + _7 = const S::<()>(()); StorageDead(_8); +- StorageLive(_9); ++ nop; + _9 = const 5_i32; + StorageLive(_10); + StorageLive(_11); +- _11 = _9; ++ _11 = const 5_i32; + StorageLive(_12); +- _12 = _9; ++ _12 = const 5_i32; + StorageLive(_13); +- _13 = _9; ++ _13 = const 5_i32; + StorageLive(_14); +- _14 = _9; ++ _14 = const 5_i32; + StorageLive(_15); +- _15 = _9; ++ _15 = const 5_i32; + StorageLive(_16); +- _16 = _9; ++ _16 = const 5_i32; + StorageLive(_17); +- _17 = _9; ++ _17 = const 5_i32; + StorageLive(_18); +- _18 = _9; ++ _18 = const 5_i32; + StorageLive(_19); +- _19 = _9; ++ _19 = const 5_i32; + StorageLive(_20); +- _20 = _9; +- _10 = [move _11, move _12, move _13, move _14, move _15, move _16, move _17, move _18, move _19, move _20]; ++ _20 = const 5_i32; ++ _10 = [const 5_i32; 10]; + StorageDead(_20); + StorageDead(_19); + StorageDead(_18); + StorageDead(_17); + StorageDead(_16); + StorageDead(_15); + StorageDead(_14); + StorageDead(_13); + StorageDead(_12); + StorageDead(_11); _0 = const (); + StorageDead(_10); +- StorageDead(_9); ++ nop; StorageDead(_7); StorageDead(_5); StorageDead(_3); diff --git a/tests/mir-opt/gvn.aggregates.GVN.panic-unwind.diff b/tests/mir-opt/gvn.aggregates.GVN.panic-unwind.diff index c442835bfa99e..b1dae36ab6c30 100644 --- a/tests/mir-opt/gvn.aggregates.GVN.panic-unwind.diff +++ b/tests/mir-opt/gvn.aggregates.GVN.panic-unwind.diff @@ -8,6 +8,16 @@ let mut _4: [u16; 0]; let mut _6: (); let mut _8: (); + let mut _11: i32; + let mut _12: i32; + let mut _13: i32; + let mut _14: i32; + let mut _15: i32; + let mut _16: i32; + let mut _17: i32; + let mut _18: i32; + let mut _19: i32; + let mut _20: i32; scope 1 { debug a_array => _1; let _3: S<[u16; 0]>; @@ -19,6 +29,14 @@ let _7: S<()>; scope 4 { debug b_tuple => _7; + let _9: i32; + scope 5 { + debug val => _9; + let _10: [i32; 10]; + scope 6 { + debug array => _10; + } + } } } } @@ -55,7 +73,56 @@ + _8 = const (); + _7 = const S::<()>(()); StorageDead(_8); +- StorageLive(_9); ++ nop; + _9 = const 5_i32; + StorageLive(_10); + StorageLive(_11); +- _11 = _9; ++ _11 = const 5_i32; + StorageLive(_12); +- _12 = _9; ++ _12 = const 5_i32; + StorageLive(_13); +- _13 = _9; ++ _13 = const 5_i32; + StorageLive(_14); +- _14 = _9; ++ _14 = const 5_i32; + StorageLive(_15); +- _15 = _9; ++ _15 = const 5_i32; + StorageLive(_16); +- _16 = _9; ++ _16 = const 5_i32; + StorageLive(_17); +- _17 = _9; ++ _17 = const 5_i32; + StorageLive(_18); +- _18 = _9; ++ _18 = const 5_i32; + StorageLive(_19); +- _19 = _9; ++ _19 = const 5_i32; + StorageLive(_20); +- _20 = _9; +- _10 = [move _11, move _12, move _13, move _14, move _15, move _16, move _17, move _18, move _19, move _20]; ++ _20 = const 5_i32; ++ _10 = [const 5_i32; 10]; + StorageDead(_20); + StorageDead(_19); + StorageDead(_18); + StorageDead(_17); + StorageDead(_16); + StorageDead(_15); + StorageDead(_14); + StorageDead(_13); + StorageDead(_12); + StorageDead(_11); _0 = const (); + StorageDead(_10); +- StorageDead(_9); ++ nop; StorageDead(_7); StorageDead(_5); StorageDead(_3); diff --git a/tests/mir-opt/gvn.rs b/tests/mir-opt/gvn.rs index ff176a6597b43..406fe106add5d 100644 --- a/tests/mir-opt/gvn.rs +++ b/tests/mir-opt/gvn.rs @@ -61,7 +61,6 @@ fn subexpression_elimination(x: u64, y: u64, mut z: u64) { let e = &z; opaque(*e + x); opaque(*e + x); - } fn wrap_unwrap(x: T) -> T { @@ -237,6 +236,9 @@ fn aggregates() { let a_tuple: S<()> = S(()); let b_tuple: S<()> = S(()); // But this can be with `a_tuple`. + + let val = 5; + let array = [val, val, val, val, val, val, val, val, val, val]; } fn main() { From 8162dc2433dac09966ae23bb4422d966a7aeab53 Mon Sep 17 00:00:00 2001 From: Camille GILLOT Date: Tue, 3 Oct 2023 18:06:47 +0000 Subject: [PATCH 12/28] Do not intern GVN temps. --- compiler/rustc_mir_transform/src/gvn.rs | 38 +++++++++++-------------- 1 file changed, 16 insertions(+), 22 deletions(-) diff --git a/compiler/rustc_mir_transform/src/gvn.rs b/compiler/rustc_mir_transform/src/gvn.rs index f71e9de4123d2..7457f2e4e6f79 100644 --- a/compiler/rustc_mir_transform/src/gvn.rs +++ b/compiler/rustc_mir_transform/src/gvn.rs @@ -53,6 +53,7 @@ //! _c = *_b // replaced by _c = _a //! ``` +use rustc_const_eval::interpret::MemoryKind; use rustc_const_eval::interpret::{ImmTy, InterpCx, MemPlaceMeta, OpTy, Projectable, Scalar}; use rustc_data_structures::fx::{FxHashMap, FxIndexSet}; use rustc_data_structures::graph::dominators::Dominators; @@ -323,24 +324,19 @@ impl<'body, 'tcx> VnState<'body, 'tcx> { if ty.is_zst() { ImmTy::uninit(ty).into() } else if matches!(ty.abi, Abi::Scalar(..) | Abi::ScalarPair(..)) { - let alloc_id = self - .ecx - .intern_with_temp_alloc(ty, |ecx, dest| { - let variant_dest = if let Some(variant) = variant { - ecx.project_downcast(dest, variant)? - } else { - dest.clone() - }; - for (field_index, op) in fields.into_iter().enumerate() { - let field_dest = ecx.project_field(&variant_dest, field_index)?; - ecx.copy_op(op, &field_dest, /*allow_transmute*/ false)?; - } - ecx.write_discriminant(variant.unwrap_or(FIRST_VARIANT), dest) - }) - .ok()?; - let mplace = - self.ecx.raw_const_to_mplace(ConstAlloc { alloc_id, ty: ty.ty }).ok()?; - mplace.into() + let dest = self.ecx.allocate(ty, MemoryKind::Stack).ok()?; + let variant_dest = if let Some(variant) = variant { + self.ecx.project_downcast(&dest, variant).ok()? + } else { + dest.clone() + }; + for (field_index, op) in fields.into_iter().enumerate() { + let field_dest = self.ecx.project_field(&variant_dest, field_index).ok()?; + self.ecx.copy_op(op, &field_dest, /*allow_transmute*/ false).ok()?; + } + self.ecx.write_discriminant(variant.unwrap_or(FIRST_VARIANT), &dest).ok()?; + self.ecx.alloc_mark_immutable(dest.ptr().provenance.unwrap()).ok()?; + dest.into() } else { return None; } @@ -846,10 +842,8 @@ fn op_to_prop_const<'tcx>( { let pointer = mplace.ptr().into_pointer_or_addr().ok()?; let (alloc_id, offset) = pointer.into_parts(); - return if matches!(ecx.tcx.global_alloc(alloc_id), GlobalAlloc::Memory(_)) { - Some(ConstValue::Indirect { alloc_id, offset }) - } else { - None + if matches!(ecx.tcx.try_get_global_alloc(alloc_id), Some(GlobalAlloc::Memory(_))) { + return Some(ConstValue::Indirect { alloc_id, offset }) } } From ebc87bf5672fd7f7a899599d2ee5a219e1d07d07 Mon Sep 17 00:00:00 2001 From: Camille GILLOT Date: Sat, 7 Oct 2023 09:19:37 +0000 Subject: [PATCH 13/28] Directly intern values instead of copying them. --- .../rustc_const_eval/src/interpret/intern.rs | 44 +++++++++++++++++++ .../rustc_const_eval/src/interpret/mod.rs | 4 +- compiler/rustc_mir_transform/src/gvn.rs | 9 ++-- 3 files changed, 53 insertions(+), 4 deletions(-) diff --git a/compiler/rustc_const_eval/src/interpret/intern.rs b/compiler/rustc_const_eval/src/interpret/intern.rs index fd89e34204f0c..4cac00c1be26e 100644 --- a/compiler/rustc_const_eval/src/interpret/intern.rs +++ b/compiler/rustc_const_eval/src/interpret/intern.rs @@ -450,6 +450,50 @@ pub fn intern_const_alloc_recursive< Ok(()) } +/// Intern `ret`, checking it references no other allocation. +#[instrument(level = "debug", skip(ecx))] +pub fn intern_const_alloc_for_constprop< + 'mir, + 'tcx: 'mir, + T, + M: CompileTimeMachine<'mir, 'tcx, T>, +>( + ecx: &mut InterpCx<'mir, 'tcx, M>, + ret: &MPlaceTy<'tcx>, +) -> InterpResult<'tcx, ()> { + let Some((size, _align)) = ecx.size_and_align_of_mplace(ret)? else { + throw_inval!(ConstPropNonsense) + }; + + let alloc_ref = ecx.get_ptr_alloc(ret.ptr(), size)?.unwrap(); + // Do not try interning a value that contains provenance. + if alloc_ref.has_provenance() { + throw_inval!(ConstPropNonsense) + } + + // remove allocation + let alloc_id = ret.ptr().provenance.unwrap(); + let Some((_, mut alloc)) = ecx.memory.alloc_map.remove(&alloc_id) else { + // Pointer not found in local memory map. It is either a pointer to the global + // map, or dangling. + if ecx.tcx.try_get_global_alloc(alloc_id).is_none() { + throw_ub!(DeadLocal) + } + // The constant is already in global memory. Do nothing. + return Ok(()); + }; + + alloc.mutability = Mutability::Not; + + // link the alloc id to the actual allocation + assert!(alloc.provenance().ptrs().is_empty()); + + let alloc = ecx.tcx.mk_const_alloc(alloc); + ecx.tcx.set_alloc_id_memory(alloc_id, alloc); + + Ok(()) +} + impl<'mir, 'tcx: 'mir, M: super::intern::CompileTimeMachine<'mir, 'tcx, !>> InterpCx<'mir, 'tcx, M> { diff --git a/compiler/rustc_const_eval/src/interpret/mod.rs b/compiler/rustc_const_eval/src/interpret/mod.rs index 13664456987e9..7d286d103adfe 100644 --- a/compiler/rustc_const_eval/src/interpret/mod.rs +++ b/compiler/rustc_const_eval/src/interpret/mod.rs @@ -21,7 +21,9 @@ mod visitor; pub use rustc_middle::mir::interpret::*; // have all the `interpret` symbols in one place: here pub use self::eval_context::{Frame, FrameInfo, InterpCx, StackPopCleanup}; -pub use self::intern::{intern_const_alloc_recursive, InternKind}; +pub use self::intern::{ + intern_const_alloc_for_constprop, intern_const_alloc_recursive, InternKind, +}; pub use self::machine::{compile_time_machine, AllocMap, Machine, MayLeak, StackPopJump}; pub use self::memory::{AllocKind, AllocRef, AllocRefMut, FnVal, Memory, MemoryKind}; pub use self::operand::{ImmTy, Immediate, OpTy, Readable}; diff --git a/compiler/rustc_mir_transform/src/gvn.rs b/compiler/rustc_mir_transform/src/gvn.rs index 7457f2e4e6f79..ba50ca0b79107 100644 --- a/compiler/rustc_mir_transform/src/gvn.rs +++ b/compiler/rustc_mir_transform/src/gvn.rs @@ -53,7 +53,7 @@ //! _c = *_b // replaced by _c = _a //! ``` -use rustc_const_eval::interpret::MemoryKind; +use rustc_const_eval::interpret::{intern_const_alloc_for_constprop, MemoryKind}; use rustc_const_eval::interpret::{ImmTy, InterpCx, MemPlaceMeta, OpTy, Projectable, Scalar}; use rustc_data_structures::fx::{FxHashMap, FxIndexSet}; use rustc_data_structures::graph::dominators::Dominators; @@ -840,10 +840,13 @@ fn op_to_prop_const<'tcx>( if let Either::Left(mplace) = op.as_mplace_or_imm() && let MemPlaceMeta::None = mplace.meta() { + intern_const_alloc_for_constprop(ecx, &mplace).ok()?; let pointer = mplace.ptr().into_pointer_or_addr().ok()?; let (alloc_id, offset) = pointer.into_parts(); - if matches!(ecx.tcx.try_get_global_alloc(alloc_id), Some(GlobalAlloc::Memory(_))) { - return Some(ConstValue::Indirect { alloc_id, offset }) + match ecx.tcx.global_alloc(alloc_id) { + GlobalAlloc::Memory(_) => return Some(ConstValue::Indirect { alloc_id, offset }), + // Fallthrough to copying the data. + _ => {} } } From ff6812cd20da84f84b2cb8540110d08bd54e8060 Mon Sep 17 00:00:00 2001 From: Camille GILLOT Date: Tue, 10 Oct 2023 15:42:23 +0000 Subject: [PATCH 14/28] Move provenance checks out of interning method. --- .../rustc_const_eval/src/interpret/intern.rs | 14 ++----------- .../rustc_const_eval/src/interpret/memory.rs | 2 +- compiler/rustc_mir_transform/src/gvn.rs | 21 +++++++++++++++---- 3 files changed, 20 insertions(+), 17 deletions(-) diff --git a/compiler/rustc_const_eval/src/interpret/intern.rs b/compiler/rustc_const_eval/src/interpret/intern.rs index 4cac00c1be26e..0d21ef7f3274c 100644 --- a/compiler/rustc_const_eval/src/interpret/intern.rs +++ b/compiler/rustc_const_eval/src/interpret/intern.rs @@ -450,7 +450,7 @@ pub fn intern_const_alloc_recursive< Ok(()) } -/// Intern `ret`, checking it references no other allocation. +/// Intern `ret`. This function assumes that `ret` references no other allocation. #[instrument(level = "debug", skip(ecx))] pub fn intern_const_alloc_for_constprop< 'mir, @@ -461,17 +461,7 @@ pub fn intern_const_alloc_for_constprop< ecx: &mut InterpCx<'mir, 'tcx, M>, ret: &MPlaceTy<'tcx>, ) -> InterpResult<'tcx, ()> { - let Some((size, _align)) = ecx.size_and_align_of_mplace(ret)? else { - throw_inval!(ConstPropNonsense) - }; - - let alloc_ref = ecx.get_ptr_alloc(ret.ptr(), size)?.unwrap(); - // Do not try interning a value that contains provenance. - if alloc_ref.has_provenance() { - throw_inval!(ConstPropNonsense) - } - - // remove allocation + // Move allocation to `tcx`. let alloc_id = ret.ptr().provenance.unwrap(); let Some((_, mut alloc)) = ecx.memory.alloc_map.remove(&alloc_id) else { // Pointer not found in local memory map. It is either a pointer to the global diff --git a/compiler/rustc_const_eval/src/interpret/memory.rs b/compiler/rustc_const_eval/src/interpret/memory.rs index d7c7e27984966..16905e93bf1f8 100644 --- a/compiler/rustc_const_eval/src/interpret/memory.rs +++ b/compiler/rustc_const_eval/src/interpret/memory.rs @@ -1011,7 +1011,7 @@ impl<'tcx, 'a, Prov: Provenance, Extra, Bytes: AllocBytes> AllocRef<'a, 'tcx, Pr } /// Returns whether the allocation has provenance anywhere in the range of the `AllocRef`. - pub(crate) fn has_provenance(&self) -> bool { + pub fn has_provenance(&self) -> bool { !self.alloc.provenance().range_empty(self.range, &self.tcx) } } diff --git a/compiler/rustc_mir_transform/src/gvn.rs b/compiler/rustc_mir_transform/src/gvn.rs index ba50ca0b79107..cced62ecf3732 100644 --- a/compiler/rustc_mir_transform/src/gvn.rs +++ b/compiler/rustc_mir_transform/src/gvn.rs @@ -832,6 +832,7 @@ fn op_to_prop_const<'tcx>( // If this constant has scalar ABI, return it as a `ConstValue::Scalar`. if let Abi::Scalar(abi::Scalar::Initialized { .. }) = op.layout.abi && let Ok(scalar) = ecx.read_scalar(op) + && scalar.try_to_int().is_ok() { return Some(ConstValue::Scalar(scalar)); } @@ -840,6 +841,14 @@ fn op_to_prop_const<'tcx>( if let Either::Left(mplace) = op.as_mplace_or_imm() && let MemPlaceMeta::None = mplace.meta() { + let (size, _align) = ecx.size_and_align_of_mplace(&mplace).ok()??; + + // Do not try interning a value that contains provenance. + let alloc_ref = ecx.get_ptr_alloc(mplace.ptr(), size).ok()??; + if alloc_ref.has_provenance() { + return None; + } + intern_const_alloc_for_constprop(ecx, &mplace).ok()?; let pointer = mplace.ptr().into_pointer_or_addr().ok()?; let (alloc_id, offset) = pointer.into_parts(); @@ -853,7 +862,13 @@ fn op_to_prop_const<'tcx>( // Everything failed: create a new allocation to hold the data. let alloc_id = ecx.intern_with_temp_alloc(op.layout, |ecx, dest| ecx.copy_op(op, dest, false)).ok()?; - Some(ConstValue::Indirect { alloc_id, offset: Size::ZERO }) + let value = ConstValue::Indirect { alloc_id, offset: Size::ZERO }; + + if !value.has_provenance(*ecx.tcx, op.layout.size) { + return Some(value); + } + + None } impl<'tcx> VnState<'_, 'tcx> { @@ -905,9 +920,7 @@ impl<'tcx> VnState<'_, 'tcx> { // Check that we do not leak a pointer. // Those pointers may lose part of their identity in codegen. - if value.has_provenance(self.tcx, op.layout.size) { - return None; - } + assert!(!value.has_provenance(self.tcx, op.layout.size)); let const_ = Const::Val(value, op.layout.ty); Some(ConstOperand { span: rustc_span::DUMMY_SP, user_ty: None, const_ }) From fbf0a0c5ff0177afda43e404b2e03a48012cfe8f Mon Sep 17 00:00:00 2001 From: Camille GILLOT Date: Wed, 11 Oct 2023 16:11:55 +0000 Subject: [PATCH 15/28] Explain why we check variant equality. --- compiler/rustc_mir_transform/src/gvn.rs | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/compiler/rustc_mir_transform/src/gvn.rs b/compiler/rustc_mir_transform/src/gvn.rs index cced62ecf3732..b9e75f5c93f02 100644 --- a/compiler/rustc_mir_transform/src/gvn.rs +++ b/compiler/rustc_mir_transform/src/gvn.rs @@ -512,6 +512,21 @@ impl<'body, 'tcx> VnState<'body, 'tcx> { return Some(fields[f.as_usize()]); } else if let Value::Projection(outer_value, ProjectionElem::Downcast(_, read_variant)) = self.get(value) && let Value::Aggregate(_, written_variant, fields) = self.get(*outer_value) + // This pass is not aware of control-flow, so we do not know whether the + // replacement we are doing is actually reachable. We could be in any arm of + // ``` + // match Some(x) { + // Some(y) => /* stuff */, + // None => /* other */, + // } + // ``` + // + // In surface rust, the current statement would be unreachable. + // + // However, from the reference chapter on enums and RFC 2195, + // accessing the wrong variant is not UB if the enum has repr. + // So it's not impossible for a series of MIR opts to generate + // a downcast to an inactive variant. && written_variant == read_variant { return Some(fields[f.as_usize()]); From 59235a79072b393155e86beb96f9618ea0a914f6 Mon Sep 17 00:00:00 2001 From: Camille GILLOT Date: Thu, 12 Oct 2023 06:35:54 +0000 Subject: [PATCH 16/28] Fortify transmute check. --- compiler/rustc_mir_transform/src/gvn.rs | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/compiler/rustc_mir_transform/src/gvn.rs b/compiler/rustc_mir_transform/src/gvn.rs index b9e75f5c93f02..483a1b3f6dfb0 100644 --- a/compiler/rustc_mir_transform/src/gvn.rs +++ b/compiler/rustc_mir_transform/src/gvn.rs @@ -471,10 +471,9 @@ impl<'body, 'tcx> VnState<'body, 'tcx> { // `offset` for immediates only supports scalar/scalar-pair ABIs, // so bail out if the target is not one. if value.as_mplace_or_imm().is_right() { - match to.abi { - Abi::Scalar(..) | Abi::ScalarPair(..) => {} - _ if to.is_zst() => {} - Abi::Aggregate { .. } if to.fields.count() == 0 => {} + match (value.layout.abi, to.abi) { + (Abi::Scalar(..), Abi::Scalar(..)) => {} + (Abi::ScalarPair(..), Abi::ScalarPair(..)) => {} _ => return None, } } From e3538d11f1ca94737e21676a54f8de65523d614c Mon Sep 17 00:00:00 2001 From: Camille GILLOT Date: Thu, 12 Oct 2023 16:15:52 +0000 Subject: [PATCH 17/28] Do not require absence of metadata. --- compiler/rustc_mir_transform/src/gvn.rs | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/compiler/rustc_mir_transform/src/gvn.rs b/compiler/rustc_mir_transform/src/gvn.rs index 483a1b3f6dfb0..c575da611c747 100644 --- a/compiler/rustc_mir_transform/src/gvn.rs +++ b/compiler/rustc_mir_transform/src/gvn.rs @@ -54,7 +54,7 @@ //! ``` use rustc_const_eval::interpret::{intern_const_alloc_for_constprop, MemoryKind}; -use rustc_const_eval::interpret::{ImmTy, InterpCx, MemPlaceMeta, OpTy, Projectable, Scalar}; +use rustc_const_eval::interpret::{ImmTy, InterpCx, OpTy, Projectable, Scalar}; use rustc_data_structures::fx::{FxHashMap, FxIndexSet}; use rustc_data_structures::graph::dominators::Dominators; use rustc_hir::def::DefKind; @@ -852,9 +852,7 @@ fn op_to_prop_const<'tcx>( } // If this constant is a projection of another, we can return it directly. - if let Either::Left(mplace) = op.as_mplace_or_imm() - && let MemPlaceMeta::None = mplace.meta() - { + if let Either::Left(mplace) = op.as_mplace_or_imm() { let (size, _align) = ecx.size_and_align_of_mplace(&mplace).ok()??; // Do not try interning a value that contains provenance. From f08dc9be17aff80e248d37a768faa6cc8738d371 Mon Sep 17 00:00:00 2001 From: Camille GILLOT Date: Thu, 12 Oct 2023 16:17:06 +0000 Subject: [PATCH 18/28] Take an AllocId in intern_const_alloc_for_constprop. --- compiler/rustc_const_eval/src/interpret/intern.rs | 3 +-- compiler/rustc_mir_transform/src/gvn.rs | 9 ++------- 2 files changed, 3 insertions(+), 9 deletions(-) diff --git a/compiler/rustc_const_eval/src/interpret/intern.rs b/compiler/rustc_const_eval/src/interpret/intern.rs index 0d21ef7f3274c..23d6d32194710 100644 --- a/compiler/rustc_const_eval/src/interpret/intern.rs +++ b/compiler/rustc_const_eval/src/interpret/intern.rs @@ -459,10 +459,9 @@ pub fn intern_const_alloc_for_constprop< M: CompileTimeMachine<'mir, 'tcx, T>, >( ecx: &mut InterpCx<'mir, 'tcx, M>, - ret: &MPlaceTy<'tcx>, + alloc_id: AllocId, ) -> InterpResult<'tcx, ()> { // Move allocation to `tcx`. - let alloc_id = ret.ptr().provenance.unwrap(); let Some((_, mut alloc)) = ecx.memory.alloc_map.remove(&alloc_id) else { // Pointer not found in local memory map. It is either a pointer to the global // map, or dangling. diff --git a/compiler/rustc_mir_transform/src/gvn.rs b/compiler/rustc_mir_transform/src/gvn.rs index c575da611c747..58844aed7839a 100644 --- a/compiler/rustc_mir_transform/src/gvn.rs +++ b/compiler/rustc_mir_transform/src/gvn.rs @@ -61,7 +61,6 @@ use rustc_hir::def::DefKind; use rustc_index::bit_set::BitSet; use rustc_index::IndexVec; use rustc_macros::newtype_index; -use rustc_middle::mir::interpret::GlobalAlloc; use rustc_middle::mir::visit::*; use rustc_middle::mir::*; use rustc_middle::ty::layout::LayoutOf; @@ -861,14 +860,10 @@ fn op_to_prop_const<'tcx>( return None; } - intern_const_alloc_for_constprop(ecx, &mplace).ok()?; let pointer = mplace.ptr().into_pointer_or_addr().ok()?; let (alloc_id, offset) = pointer.into_parts(); - match ecx.tcx.global_alloc(alloc_id) { - GlobalAlloc::Memory(_) => return Some(ConstValue::Indirect { alloc_id, offset }), - // Fallthrough to copying the data. - _ => {} - } + intern_const_alloc_for_constprop(ecx, alloc_id).ok()?; + return Some(ConstValue::Indirect { alloc_id, offset }); } // Everything failed: create a new allocation to hold the data. From 5e78b9cdb39f2cd47a0518ba568e7bfd0d1f8d95 Mon Sep 17 00:00:00 2001 From: Camille GILLOT Date: Sat, 14 Oct 2023 12:12:54 +0000 Subject: [PATCH 19/28] Disambiguate non-deterministic constants. --- compiler/rustc_middle/src/mir/consts.rs | 34 ++++++ compiler/rustc_mir_transform/src/gvn.rs | 111 +++++++++++------- .../gvn.duplicate_slice.GVN.panic-abort.diff | 38 ++++++ .../gvn.duplicate_slice.GVN.panic-unwind.diff | 38 ++++++ tests/mir-opt/gvn.rs | 54 ++++++++- tests/mir-opt/gvn.slices.GVN.panic-abort.diff | 8 +- .../mir-opt/gvn.slices.GVN.panic-unwind.diff | 8 +- 7 files changed, 238 insertions(+), 53 deletions(-) create mode 100644 tests/mir-opt/gvn.duplicate_slice.GVN.panic-abort.diff create mode 100644 tests/mir-opt/gvn.duplicate_slice.GVN.panic-unwind.diff diff --git a/compiler/rustc_middle/src/mir/consts.rs b/compiler/rustc_middle/src/mir/consts.rs index 9e3ee3f2bd349..375870a4fb5c7 100644 --- a/compiler/rustc_middle/src/mir/consts.rs +++ b/compiler/rustc_middle/src/mir/consts.rs @@ -497,6 +497,40 @@ impl<'tcx> Const<'tcx> { _ => Self::Ty(c), } } + + /// Return true if any evaluation of this constant returns the same value. + pub fn is_deterministic(&self) -> bool { + // Some constants may contain pointers. We need to preserve the provenance of these + // pointers, but not all constants guarantee this: + // - valtrees purposefully do not; + // - ConstValue::Slice does not either. + match self { + Const::Ty(c) => match c.kind() { + ty::ConstKind::Value(valtree) => match valtree { + // This is just an integer, keep it. + ty::ValTree::Leaf(_) => true, + // This branch may be a reference. Valtree references correspond to a + // different allocation each time they are evaluated. + ty::ValTree::Branch(_) => false, + }, + ty::ConstKind::Param(..) => true, + ty::ConstKind::Unevaluated(..) | ty::ConstKind::Expr(..) => false, + // Should not appear in runtime MIR. + ty::ConstKind::Infer(..) + | ty::ConstKind::Bound(..) + | ty::ConstKind::Placeholder(..) + | ty::ConstKind::Error(..) => bug!(), + }, + Const::Unevaluated(..) => false, + // If the same slice appears twice in the MIR, we cannot guarantee that we will + // give the same `AllocId` to the data. + Const::Val(ConstValue::Slice { .. }, _) => false, + Const::Val( + ConstValue::ZeroSized | ConstValue::Scalar(_) | ConstValue::Indirect { .. }, + _, + ) => true, + } + } } /// An unevaluated (potentially generic) constant used in MIR. diff --git a/compiler/rustc_mir_transform/src/gvn.rs b/compiler/rustc_mir_transform/src/gvn.rs index 58844aed7839a..de61571fdfcb5 100644 --- a/compiler/rustc_mir_transform/src/gvn.rs +++ b/compiler/rustc_mir_transform/src/gvn.rs @@ -52,6 +52,35 @@ //! _a = *_b // _b is &Freeze //! _c = *_b // replaced by _c = _a //! ``` +//! +//! # Determinism of constant propagation +//! +//! When registering a new `Value`, we attempt to opportunistically evaluate it as a constant. +//! The evaluated form is inserted in `evaluated` as an `OpTy` or `None` if evaluation failed. +//! +//! The difficulty is non-deterministic evaluation of MIR constants. Some `Const` can have +//! different runtime values each time they are evaluated. This is the case with +//! `Const::Slice` which have a new pointer each time they are evaluated, and constants that +//! contain a fn pointer (`AllocId` pointing to a `GlobalAlloc::Function`) pointing to a different +//! symbol in each codegen unit. +//! +//! Meanwhile, we want to be able to read indirect constants. For instance: +//! ``` +//! static A: &'static &'static u8 = &&63; +//! fn foo() -> u8 { +//! **A // We want to replace by 63. +//! } +//! fn bar() -> u8 { +//! b"abc"[1] // We want to replace by 'b'. +//! } +//! ``` +//! +//! The `Value::Constant` variant stores a possibly unevaluated constant. Evaluating that constant +//! may be non-deterministic. When that happens, we assign a disambiguator to ensure that we do not +//! merge the constants. See `duplicate_slice` test in `gvn.rs`. +//! +//! Second, when writing constants in MIR, we do not write `Const::Slice` or `Const` +//! that contain `AllocId`s. use rustc_const_eval::interpret::{intern_const_alloc_for_constprop, MemoryKind}; use rustc_const_eval::interpret::{ImmTy, InterpCx, OpTy, Projectable, Scalar}; @@ -161,7 +190,12 @@ enum Value<'tcx> { /// The `usize` is a counter incremented by `new_opaque`. Opaque(usize), /// Evaluated or unevaluated constant value. - Constant(Const<'tcx>), + Constant { + value: Const<'tcx>, + /// Some constants do not have a deterministic value. To avoid merging two instances of the + /// same `Const`, we assign them an additional integer index. + disambiguator: usize, + }, /// An aggregate value, either tuple/closure/struct/enum. /// This does not contain unions, as we cannot reason with the value. Aggregate(AggregateTy<'tcx>, VariantIdx, Vec), @@ -288,8 +322,24 @@ impl<'body, 'tcx> VnState<'body, 'tcx> { } } + fn insert_constant(&mut self, value: Const<'tcx>) -> Option { + let disambiguator = if value.is_deterministic() { + // The constant is deterministic, no need to disambiguate. + 0 + } else { + // Multiple mentions of this constant will yield different values, + // so assign a different `disambiguator` to ensure they do not get the same `VnIndex`. + let next_opaque = self.next_opaque.as_mut()?; + let disambiguator = *next_opaque; + *next_opaque += 1; + disambiguator + }; + Some(self.insert(Value::Constant { value, disambiguator })) + } + fn insert_scalar(&mut self, scalar: Scalar, ty: Ty<'tcx>) -> VnIndex { - self.insert(Value::Constant(Const::from_scalar(self.tcx, scalar, ty))) + self.insert_constant(Const::from_scalar(self.tcx, scalar, ty)) + .expect("scalars are deterministic") } #[instrument(level = "trace", skip(self), ret)] @@ -300,7 +350,9 @@ impl<'body, 'tcx> VnState<'body, 'tcx> { // Do not bother evaluating repeat expressions. This would uselessly consume memory. Repeat(..) => return None, - Constant(ref constant) => self.ecx.eval_mir_constant(constant, None, None).ok()?, + Constant { ref value, disambiguator: _ } => { + self.ecx.eval_mir_constant(value, None, None).ok()? + } Aggregate(kind, variant, ref fields) => { let fields = fields .iter() @@ -657,7 +709,7 @@ impl<'body, 'tcx> VnState<'body, 'tcx> { match *operand { Operand::Constant(ref mut constant) => { let const_ = constant.const_.normalize(self.tcx, self.param_env); - Some(self.insert(Value::Constant(const_))) + self.insert_constant(const_) } Operand::Copy(ref mut place) | Operand::Move(ref mut place) => { let value = self.simplify_place_value(place, location)?; @@ -691,7 +743,7 @@ impl<'body, 'tcx> VnState<'body, 'tcx> { Value::Repeat(op, amount) } Rvalue::NullaryOp(op, ty) => Value::NullaryOp(op, ty), - Rvalue::Aggregate(..) => self.simplify_aggregate(rvalue, location)?, + Rvalue::Aggregate(..) => return self.simplify_aggregate(rvalue, location), Rvalue::Ref(_, borrow_kind, ref mut place) => { self.simplify_place_projection(place, location); return self.new_pointer(*place, AddressKind::Ref(borrow_kind)); @@ -757,7 +809,7 @@ impl<'body, 'tcx> VnState<'body, 'tcx> { &mut self, rvalue: &mut Rvalue<'tcx>, location: Location, - ) -> Option> { + ) -> Option { let Rvalue::Aggregate(box ref kind, ref mut fields) = *rvalue else { bug!() }; let tcx = self.tcx; @@ -774,8 +826,7 @@ impl<'body, 'tcx> VnState<'body, 'tcx> { if is_zst { let ty = rvalue.ty(self.local_decls, tcx); - let value = Value::Constant(Const::zero_sized(ty)); - return Some(value); + return self.insert_constant(Const::zero_sized(ty)); } } @@ -814,12 +865,11 @@ impl<'body, 'tcx> VnState<'body, 'tcx> { *rvalue = Rvalue::Repeat(Operand::Copy(local.into()), len); self.reused_locals.insert(local); } - return Some(Value::Repeat(first, len)); + return Some(self.insert(Value::Repeat(first, len))); } } - let value = Value::Aggregate(ty, variant_index, fields); - Some(value) + Some(self.insert(Value::Aggregate(ty, variant_index, fields))) } } @@ -882,39 +932,12 @@ impl<'tcx> VnState<'_, 'tcx> { /// If `index` is a `Value::Constant`, return the `Constant` to be put in the MIR. fn try_as_constant(&mut self, index: VnIndex) -> Option> { // This was already constant in MIR, do not change it. - if let Value::Constant(const_) = *self.get(index) { - // Some constants may contain pointers. We need to preserve the provenance of these - // pointers, but not all constants guarantee this: - // - valtrees purposefully do not; - // - ConstValue::Slice does not either. - let const_ok = match const_ { - Const::Ty(c) => match c.kind() { - ty::ConstKind::Value(valtree) => match valtree { - // This is just an integer, keep it. - ty::ValTree::Leaf(_) => true, - ty::ValTree::Branch(_) => false, - }, - ty::ConstKind::Param(..) - | ty::ConstKind::Unevaluated(..) - | ty::ConstKind::Expr(..) => true, - // Should not appear in runtime MIR. - ty::ConstKind::Infer(..) - | ty::ConstKind::Bound(..) - | ty::ConstKind::Placeholder(..) - | ty::ConstKind::Error(..) => bug!(), - }, - Const::Unevaluated(..) => true, - // If the same slice appears twice in the MIR, we cannot guarantee that we will - // give the same `AllocId` to the data. - Const::Val(ConstValue::Slice { .. }, _) => false, - Const::Val( - ConstValue::ZeroSized | ConstValue::Scalar(_) | ConstValue::Indirect { .. }, - _, - ) => true, - }; - if const_ok { - return Some(ConstOperand { span: rustc_span::DUMMY_SP, user_ty: None, const_ }); - } + if let Value::Constant { value, disambiguator: _ } = *self.get(index) + // If the constant is not deterministic, adding an additional mention of it in MIR will + // not give the same value as the former mention. + && value.is_deterministic() + { + return Some(ConstOperand { span: rustc_span::DUMMY_SP, user_ty: None, const_: value }); } let op = self.evaluated[index].as_ref()?; diff --git a/tests/mir-opt/gvn.duplicate_slice.GVN.panic-abort.diff b/tests/mir-opt/gvn.duplicate_slice.GVN.panic-abort.diff new file mode 100644 index 0000000000000..7ae1fae68e887 --- /dev/null +++ b/tests/mir-opt/gvn.duplicate_slice.GVN.panic-abort.diff @@ -0,0 +1,38 @@ +- // MIR for `duplicate_slice` before GVN ++ // MIR for `duplicate_slice` after GVN + + fn duplicate_slice() -> (bool, bool) { + let mut _0: (bool, bool); + let mut _1: u128; + let mut _2: u128; + let mut _3: u128; + let mut _4: u128; + let mut _5: &str; + let mut _6: &str; + let mut _7: (&str,); + let mut _8: &str; + let mut _9: bool; + let mut _10: bool; + + bb0: { + _7 = (const "a",); + _1 = (_7.0: &str) as u128 (Transmute); + _5 = identity::<&str>((_7.0: &str)) -> [return: bb1, unwind unreachable]; + } + + bb1: { + _3 = _5 as u128 (Transmute); + _8 = const "a"; + _2 = _8 as u128 (Transmute); + _6 = identity::<&str>(_8) -> [return: bb2, unwind unreachable]; + } + + bb2: { + _4 = _6 as u128 (Transmute); + _9 = Eq(_1, _2); + _10 = Eq(_3, _4); + _0 = (_9, _10); + return; + } + } + diff --git a/tests/mir-opt/gvn.duplicate_slice.GVN.panic-unwind.diff b/tests/mir-opt/gvn.duplicate_slice.GVN.panic-unwind.diff new file mode 100644 index 0000000000000..8c96edaa280ea --- /dev/null +++ b/tests/mir-opt/gvn.duplicate_slice.GVN.panic-unwind.diff @@ -0,0 +1,38 @@ +- // MIR for `duplicate_slice` before GVN ++ // MIR for `duplicate_slice` after GVN + + fn duplicate_slice() -> (bool, bool) { + let mut _0: (bool, bool); + let mut _1: u128; + let mut _2: u128; + let mut _3: u128; + let mut _4: u128; + let mut _5: &str; + let mut _6: &str; + let mut _7: (&str,); + let mut _8: &str; + let mut _9: bool; + let mut _10: bool; + + bb0: { + _7 = (const "a",); + _1 = (_7.0: &str) as u128 (Transmute); + _5 = identity::<&str>((_7.0: &str)) -> [return: bb1, unwind continue]; + } + + bb1: { + _3 = _5 as u128 (Transmute); + _8 = const "a"; + _2 = _8 as u128 (Transmute); + _6 = identity::<&str>(_8) -> [return: bb2, unwind continue]; + } + + bb2: { + _4 = _6 as u128 (Transmute); + _9 = Eq(_1, _2); + _10 = Eq(_3, _4); + _0 = (_9, _10); + return; + } + } + diff --git a/tests/mir-opt/gvn.rs b/tests/mir-opt/gvn.rs index 406fe106add5d..55de186edf163 100644 --- a/tests/mir-opt/gvn.rs +++ b/tests/mir-opt/gvn.rs @@ -1,11 +1,17 @@ // skip-filecheck // unit-test: GVN // EMIT_MIR_FOR_EACH_PANIC_STRATEGY +// only-64bit #![feature(raw_ref_op)] #![feature(rustc_attrs)] +#![feature(custom_mir)] +#![feature(core_intrinsics)] #![allow(unconditional_panic)] +use std::intrinsics::mir::*; +use std::mem::transmute; + struct S(T); fn subexpression_elimination(x: u64, y: u64, mut z: u64) { @@ -225,11 +231,49 @@ fn slices() { let t = s; // This should be the same pointer, so cannot be a `Const::Slice`. opaque(t); assert_eq!(s.as_ptr(), t.as_ptr()); - let u = unsafe { std::mem::transmute::<&str, &[u8]>(s) }; + let u = unsafe { transmute::<&str, &[u8]>(s) }; opaque(u); assert_eq!(s.as_ptr(), u.as_ptr()); } +#[custom_mir(dialect = "analysis")] +fn duplicate_slice() -> (bool, bool) { + mir!( + let au: u128; + let bu: u128; + let cu: u128; + let du: u128; + let c: &str; + let d: &str; + { + let a = ("a",); + Call(au = transmute::<_, u128>(a.0), bb1) + } + bb1 = { + Call(c = identity(a.0), bb2) + } + bb2 = { + Call(cu = transmute::<_, u128>(c), bb3) + } + bb3 = { + let b = "a"; // This slice is different from `a.0`. + Call(bu = transmute::<_, u128>(b), bb4) // Hence `bu` is not `au`. + } + bb4 = { + Call(d = identity(b), bb5) // This returns a copy of `b`, which is not `a`. + } + bb5 = { + Call(du = transmute::<_, u128>(d), bb6) + } + bb6 = { + let direct = au == bu; // Must not fold to `true`... + let indirect = cu == du; // ...as this will not. + RET = (direct, indirect); + Return() + } + ) +} + fn aggregates() { let a_array: S<[u8; 0]> = S([]); let b_array: S<[u16; 0]> = S([]); // This must not be merged with `a_array`. @@ -253,12 +297,19 @@ fn main() { references(5); dereferences(&mut 5, &6, &S(7)); slices(); + let (direct, indirect) = duplicate_slice(); + assert_eq!(direct, indirect); aggregates(); } #[inline(never)] fn opaque(_: impl Sized) {} +#[inline(never)] +fn identity(x: T) -> T { + x +} + // EMIT_MIR gvn.subexpression_elimination.GVN.diff // EMIT_MIR gvn.wrap_unwrap.GVN.diff // EMIT_MIR gvn.repeated_index.GVN.diff @@ -270,4 +321,5 @@ fn opaque(_: impl Sized) {} // EMIT_MIR gvn.references.GVN.diff // EMIT_MIR gvn.dereferences.GVN.diff // EMIT_MIR gvn.slices.GVN.diff +// EMIT_MIR gvn.duplicate_slice.GVN.diff // EMIT_MIR gvn.aggregates.GVN.diff diff --git a/tests/mir-opt/gvn.slices.GVN.panic-abort.diff b/tests/mir-opt/gvn.slices.GVN.panic-abort.diff index 9db6e068fa7d6..ec44998031249 100644 --- a/tests/mir-opt/gvn.slices.GVN.panic-abort.diff +++ b/tests/mir-opt/gvn.slices.GVN.panic-abort.diff @@ -207,8 +207,8 @@ _26 = &(*_27); StorageLive(_28); _28 = Option::>::None; -- _22 = core::panicking::assert_failed::<*const u8, *const u8>(move _23, move _24, move _26, move _28) -> unwind unreachable; -+ _22 = core::panicking::assert_failed::<*const u8, *const u8>(const core::panicking::AssertKind::Eq, move _24, move _26, move _28) -> unwind unreachable; +- _22 = assert_failed::<*const u8, *const u8>(move _23, move _24, move _26, move _28) -> unwind unreachable; ++ _22 = assert_failed::<*const u8, *const u8>(const core::panicking::AssertKind::Eq, move _24, move _26, move _28) -> unwind unreachable; } bb7: { @@ -306,8 +306,8 @@ _52 = &(*_53); StorageLive(_54); _54 = Option::>::None; -- _48 = core::panicking::assert_failed::<*const u8, *const u8>(move _49, move _50, move _52, move _54) -> unwind unreachable; -+ _48 = core::panicking::assert_failed::<*const u8, *const u8>(const core::panicking::AssertKind::Eq, move _50, move _52, move _54) -> unwind unreachable; +- _48 = assert_failed::<*const u8, *const u8>(move _49, move _50, move _52, move _54) -> unwind unreachable; ++ _48 = assert_failed::<*const u8, *const u8>(const core::panicking::AssertKind::Eq, move _50, move _52, move _54) -> unwind unreachable; } } diff --git a/tests/mir-opt/gvn.slices.GVN.panic-unwind.diff b/tests/mir-opt/gvn.slices.GVN.panic-unwind.diff index ac7a3e7468885..56a78ca869461 100644 --- a/tests/mir-opt/gvn.slices.GVN.panic-unwind.diff +++ b/tests/mir-opt/gvn.slices.GVN.panic-unwind.diff @@ -207,8 +207,8 @@ _26 = &(*_27); StorageLive(_28); _28 = Option::>::None; -- _22 = core::panicking::assert_failed::<*const u8, *const u8>(move _23, move _24, move _26, move _28) -> unwind continue; -+ _22 = core::panicking::assert_failed::<*const u8, *const u8>(const core::panicking::AssertKind::Eq, move _24, move _26, move _28) -> unwind continue; +- _22 = assert_failed::<*const u8, *const u8>(move _23, move _24, move _26, move _28) -> unwind continue; ++ _22 = assert_failed::<*const u8, *const u8>(const core::panicking::AssertKind::Eq, move _24, move _26, move _28) -> unwind continue; } bb7: { @@ -306,8 +306,8 @@ _52 = &(*_53); StorageLive(_54); _54 = Option::>::None; -- _48 = core::panicking::assert_failed::<*const u8, *const u8>(move _49, move _50, move _52, move _54) -> unwind continue; -+ _48 = core::panicking::assert_failed::<*const u8, *const u8>(const core::panicking::AssertKind::Eq, move _50, move _52, move _54) -> unwind continue; +- _48 = assert_failed::<*const u8, *const u8>(move _49, move _50, move _52, move _54) -> unwind continue; ++ _48 = assert_failed::<*const u8, *const u8>(const core::panicking::AssertKind::Eq, move _50, move _52, move _54) -> unwind continue; } } From f6aa3ee7e8a6c28199ebf09810b330b46adf8610 Mon Sep 17 00:00:00 2001 From: Camille GILLOT Date: Sat, 14 Oct 2023 12:53:18 +0000 Subject: [PATCH 20/28] Complete comments. --- compiler/rustc_middle/src/mir/consts.rs | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/compiler/rustc_middle/src/mir/consts.rs b/compiler/rustc_middle/src/mir/consts.rs index 375870a4fb5c7..d427b4ec3f4df 100644 --- a/compiler/rustc_middle/src/mir/consts.rs +++ b/compiler/rustc_middle/src/mir/consts.rs @@ -498,7 +498,8 @@ impl<'tcx> Const<'tcx> { } } - /// Return true if any evaluation of this constant returns the same value. + /// Return true if any evaluation of this constant always returns the same value, + /// taking into account even pointer identity tests. pub fn is_deterministic(&self) -> bool { // Some constants may contain pointers. We need to preserve the provenance of these // pointers, but not all constants guarantee this: @@ -506,14 +507,10 @@ impl<'tcx> Const<'tcx> { // - ConstValue::Slice does not either. match self { Const::Ty(c) => match c.kind() { - ty::ConstKind::Value(valtree) => match valtree { - // This is just an integer, keep it. - ty::ValTree::Leaf(_) => true, - // This branch may be a reference. Valtree references correspond to a - // different allocation each time they are evaluated. - ty::ValTree::Branch(_) => false, - }, ty::ConstKind::Param(..) => true, + // A valtree may be a reference. Valtree references correspond to a + // different allocation each time they are evaluated. + ty::ConstKind::Value(_) => false, ty::ConstKind::Unevaluated(..) | ty::ConstKind::Expr(..) => false, // Should not appear in runtime MIR. ty::ConstKind::Infer(..) From 50559ceec491a40ac079aa6cd7943f63387060fd Mon Sep 17 00:00:00 2001 From: Camille GILLOT Date: Sat, 14 Oct 2023 13:31:32 +0000 Subject: [PATCH 21/28] Valtrees for primitive types are fine. --- compiler/rustc_middle/src/mir/consts.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/compiler/rustc_middle/src/mir/consts.rs b/compiler/rustc_middle/src/mir/consts.rs index d427b4ec3f4df..88d4a37f21090 100644 --- a/compiler/rustc_middle/src/mir/consts.rs +++ b/compiler/rustc_middle/src/mir/consts.rs @@ -509,8 +509,9 @@ impl<'tcx> Const<'tcx> { Const::Ty(c) => match c.kind() { ty::ConstKind::Param(..) => true, // A valtree may be a reference. Valtree references correspond to a - // different allocation each time they are evaluated. - ty::ConstKind::Value(_) => false, + // different allocation each time they are evaluated. Valtrees for primitive + // types are fine though. + ty::ConstKind::Value(_) => c.ty().is_primitive(), ty::ConstKind::Unevaluated(..) | ty::ConstKind::Expr(..) => false, // Should not appear in runtime MIR. ty::ConstKind::Infer(..) From ac0228da5950cb4bde01cb74da5ee6a85f75d304 Mon Sep 17 00:00:00 2001 From: Camille GILLOT Date: Mon, 16 Oct 2023 21:44:36 +0000 Subject: [PATCH 22/28] FileCheck gvn. --- .../gvn.aggregates.GVN.panic-abort.diff | 133 --- .../gvn.aggregates.GVN.panic-unwind.diff | 133 --- ...vn.arithmetic_checked.GVN.panic-abort.diff | 294 ------- ...n.arithmetic_checked.GVN.panic-unwind.diff | 294 ------- .../gvn.comparison.GVN.panic-abort.diff | 94 ++ .../gvn.comparison.GVN.panic-unwind.diff | 94 ++ tests/mir-opt/gvn.repeat.GVN.panic-abort.diff | 79 ++ .../mir-opt/gvn.repeat.GVN.panic-unwind.diff | 79 ++ tests/mir-opt/gvn.rs | 413 +++++++-- ...xpression_elimination.GVN.panic-abort.diff | 806 +++++++++--------- ...pression_elimination.GVN.panic-unwind.diff | 806 +++++++++--------- 11 files changed, 1526 insertions(+), 1699 deletions(-) delete mode 100644 tests/mir-opt/gvn.aggregates.GVN.panic-abort.diff delete mode 100644 tests/mir-opt/gvn.aggregates.GVN.panic-unwind.diff create mode 100644 tests/mir-opt/gvn.comparison.GVN.panic-abort.diff create mode 100644 tests/mir-opt/gvn.comparison.GVN.panic-unwind.diff create mode 100644 tests/mir-opt/gvn.repeat.GVN.panic-abort.diff create mode 100644 tests/mir-opt/gvn.repeat.GVN.panic-unwind.diff diff --git a/tests/mir-opt/gvn.aggregates.GVN.panic-abort.diff b/tests/mir-opt/gvn.aggregates.GVN.panic-abort.diff deleted file mode 100644 index b1dae36ab6c30..0000000000000 --- a/tests/mir-opt/gvn.aggregates.GVN.panic-abort.diff +++ /dev/null @@ -1,133 +0,0 @@ -- // MIR for `aggregates` before GVN -+ // MIR for `aggregates` after GVN - - fn aggregates() -> () { - let mut _0: (); - let _1: S<[u8; 0]>; - let mut _2: [u8; 0]; - let mut _4: [u16; 0]; - let mut _6: (); - let mut _8: (); - let mut _11: i32; - let mut _12: i32; - let mut _13: i32; - let mut _14: i32; - let mut _15: i32; - let mut _16: i32; - let mut _17: i32; - let mut _18: i32; - let mut _19: i32; - let mut _20: i32; - scope 1 { - debug a_array => _1; - let _3: S<[u16; 0]>; - scope 2 { - debug b_array => _3; - let _5: S<()>; - scope 3 { - debug a_tuple => _5; - let _7: S<()>; - scope 4 { - debug b_tuple => _7; - let _9: i32; - scope 5 { - debug val => _9; - let _10: [i32; 10]; - scope 6 { - debug array => _10; - } - } - } - } - } - } - - bb0: { - StorageLive(_1); - StorageLive(_2); -- _2 = []; -- _1 = S::<[u8; 0]>(move _2); -+ _2 = const []; -+ _1 = const S::<[u8; 0]>([]); - StorageDead(_2); - StorageLive(_3); - StorageLive(_4); -- _4 = []; -- _3 = S::<[u16; 0]>(move _4); -+ _4 = const []; -+ _3 = const S::<[u16; 0]>([]); - StorageDead(_4); - StorageLive(_5); -- StorageLive(_6); -- _6 = (); -- _5 = S::<()>(move _6); -- StorageDead(_6); -+ nop; -+ _6 = const (); -+ _5 = const S::<()>(()); -+ nop; - StorageLive(_7); - StorageLive(_8); -- _8 = (); -- _7 = S::<()>(move _8); -+ _8 = const (); -+ _7 = const S::<()>(()); - StorageDead(_8); -- StorageLive(_9); -+ nop; - _9 = const 5_i32; - StorageLive(_10); - StorageLive(_11); -- _11 = _9; -+ _11 = const 5_i32; - StorageLive(_12); -- _12 = _9; -+ _12 = const 5_i32; - StorageLive(_13); -- _13 = _9; -+ _13 = const 5_i32; - StorageLive(_14); -- _14 = _9; -+ _14 = const 5_i32; - StorageLive(_15); -- _15 = _9; -+ _15 = const 5_i32; - StorageLive(_16); -- _16 = _9; -+ _16 = const 5_i32; - StorageLive(_17); -- _17 = _9; -+ _17 = const 5_i32; - StorageLive(_18); -- _18 = _9; -+ _18 = const 5_i32; - StorageLive(_19); -- _19 = _9; -+ _19 = const 5_i32; - StorageLive(_20); -- _20 = _9; -- _10 = [move _11, move _12, move _13, move _14, move _15, move _16, move _17, move _18, move _19, move _20]; -+ _20 = const 5_i32; -+ _10 = [const 5_i32; 10]; - StorageDead(_20); - StorageDead(_19); - StorageDead(_18); - StorageDead(_17); - StorageDead(_16); - StorageDead(_15); - StorageDead(_14); - StorageDead(_13); - StorageDead(_12); - StorageDead(_11); - _0 = const (); - StorageDead(_10); -- StorageDead(_9); -+ nop; - StorageDead(_7); - StorageDead(_5); - StorageDead(_3); - StorageDead(_1); - return; - } - } - diff --git a/tests/mir-opt/gvn.aggregates.GVN.panic-unwind.diff b/tests/mir-opt/gvn.aggregates.GVN.panic-unwind.diff deleted file mode 100644 index b1dae36ab6c30..0000000000000 --- a/tests/mir-opt/gvn.aggregates.GVN.panic-unwind.diff +++ /dev/null @@ -1,133 +0,0 @@ -- // MIR for `aggregates` before GVN -+ // MIR for `aggregates` after GVN - - fn aggregates() -> () { - let mut _0: (); - let _1: S<[u8; 0]>; - let mut _2: [u8; 0]; - let mut _4: [u16; 0]; - let mut _6: (); - let mut _8: (); - let mut _11: i32; - let mut _12: i32; - let mut _13: i32; - let mut _14: i32; - let mut _15: i32; - let mut _16: i32; - let mut _17: i32; - let mut _18: i32; - let mut _19: i32; - let mut _20: i32; - scope 1 { - debug a_array => _1; - let _3: S<[u16; 0]>; - scope 2 { - debug b_array => _3; - let _5: S<()>; - scope 3 { - debug a_tuple => _5; - let _7: S<()>; - scope 4 { - debug b_tuple => _7; - let _9: i32; - scope 5 { - debug val => _9; - let _10: [i32; 10]; - scope 6 { - debug array => _10; - } - } - } - } - } - } - - bb0: { - StorageLive(_1); - StorageLive(_2); -- _2 = []; -- _1 = S::<[u8; 0]>(move _2); -+ _2 = const []; -+ _1 = const S::<[u8; 0]>([]); - StorageDead(_2); - StorageLive(_3); - StorageLive(_4); -- _4 = []; -- _3 = S::<[u16; 0]>(move _4); -+ _4 = const []; -+ _3 = const S::<[u16; 0]>([]); - StorageDead(_4); - StorageLive(_5); -- StorageLive(_6); -- _6 = (); -- _5 = S::<()>(move _6); -- StorageDead(_6); -+ nop; -+ _6 = const (); -+ _5 = const S::<()>(()); -+ nop; - StorageLive(_7); - StorageLive(_8); -- _8 = (); -- _7 = S::<()>(move _8); -+ _8 = const (); -+ _7 = const S::<()>(()); - StorageDead(_8); -- StorageLive(_9); -+ nop; - _9 = const 5_i32; - StorageLive(_10); - StorageLive(_11); -- _11 = _9; -+ _11 = const 5_i32; - StorageLive(_12); -- _12 = _9; -+ _12 = const 5_i32; - StorageLive(_13); -- _13 = _9; -+ _13 = const 5_i32; - StorageLive(_14); -- _14 = _9; -+ _14 = const 5_i32; - StorageLive(_15); -- _15 = _9; -+ _15 = const 5_i32; - StorageLive(_16); -- _16 = _9; -+ _16 = const 5_i32; - StorageLive(_17); -- _17 = _9; -+ _17 = const 5_i32; - StorageLive(_18); -- _18 = _9; -+ _18 = const 5_i32; - StorageLive(_19); -- _19 = _9; -+ _19 = const 5_i32; - StorageLive(_20); -- _20 = _9; -- _10 = [move _11, move _12, move _13, move _14, move _15, move _16, move _17, move _18, move _19, move _20]; -+ _20 = const 5_i32; -+ _10 = [const 5_i32; 10]; - StorageDead(_20); - StorageDead(_19); - StorageDead(_18); - StorageDead(_17); - StorageDead(_16); - StorageDead(_15); - StorageDead(_14); - StorageDead(_13); - StorageDead(_12); - StorageDead(_11); - _0 = const (); - StorageDead(_10); -- StorageDead(_9); -+ nop; - StorageDead(_7); - StorageDead(_5); - StorageDead(_3); - StorageDead(_1); - return; - } - } - diff --git a/tests/mir-opt/gvn.arithmetic_checked.GVN.panic-abort.diff b/tests/mir-opt/gvn.arithmetic_checked.GVN.panic-abort.diff index 3efe7abc976f7..6633df3ae702c 100644 --- a/tests/mir-opt/gvn.arithmetic_checked.GVN.panic-abort.diff +++ b/tests/mir-opt/gvn.arithmetic_checked.GVN.panic-abort.diff @@ -20,57 +20,6 @@ let mut _15: u64; let mut _16: u64; let mut _17: (u64, bool); - let _18: (); - let mut _19: u64; - let mut _20: u64; - let mut _21: bool; - let _22: (); - let mut _23: u64; - let mut _24: u64; - let mut _25: bool; - let _26: (); - let mut _27: u64; - let mut _28: u64; - let mut _29: bool; - let _30: (); - let mut _31: u64; - let mut _32: u64; - let mut _33: bool; - let _34: (); - let mut _35: u64; - let mut _36: u64; - let mut _37: bool; - let _38: (); - let mut _39: u64; - let mut _40: u64; - let mut _41: bool; - let _42: (); - let mut _43: u64; - let mut _44: u64; - let mut _45: bool; - let _46: (); - let mut _47: u64; - let mut _48: u64; - let mut _49: bool; - let _50: (); - let mut _51: u64; - let mut _52: u64; - let _53: (); - let mut _54: u64; - let mut _55: u64; - let _56: (); - let mut _57: u64; - let mut _58: u64; - let _59: (); - let mut _60: u64; - let mut _61: u64; - let mut _62: u32; - let mut _63: bool; - let _64: (); - let mut _65: u64; - let mut _66: u64; - let mut _67: u32; - let mut _68: bool; bb0: { StorageLive(_2); @@ -149,249 +98,6 @@ bb8: { StorageDead(_15); StorageDead(_14); - StorageLive(_18); - StorageLive(_19); - StorageLive(_20); - _20 = _1; -- _21 = Eq(const 0_u64, const 0_u64); -- assert(!move _21, "attempt to divide `{}` by zero", _20) -> [success: bb9, unwind unreachable]; -+ _21 = const true; -+ assert(!const true, "attempt to divide `{}` by zero", _1) -> [success: bb9, unwind unreachable]; - } - - bb9: { -- _19 = Div(move _20, const 0_u64); -+ _19 = Div(_1, const 0_u64); - StorageDead(_20); - _18 = opaque::(move _19) -> [return: bb10, unwind unreachable]; - } - - bb10: { - StorageDead(_19); - StorageDead(_18); - StorageLive(_22); - StorageLive(_23); - StorageLive(_24); - _24 = _1; -- _25 = Eq(const 1_u64, const 0_u64); -- assert(!move _25, "attempt to divide `{}` by zero", _24) -> [success: bb11, unwind unreachable]; -+ _25 = const false; -+ assert(!const false, "attempt to divide `{}` by zero", _1) -> [success: bb11, unwind unreachable]; - } - - bb11: { -- _23 = Div(move _24, const 1_u64); -+ _23 = Div(_1, const 1_u64); - StorageDead(_24); - _22 = opaque::(move _23) -> [return: bb12, unwind unreachable]; - } - - bb12: { - StorageDead(_23); - StorageDead(_22); - StorageLive(_26); - StorageLive(_27); - StorageLive(_28); - _28 = _1; -- _29 = Eq(_28, const 0_u64); -- assert(!move _29, "attempt to divide `{}` by zero", const 0_u64) -> [success: bb13, unwind unreachable]; -+ _29 = Eq(_1, const 0_u64); -+ assert(!_29, "attempt to divide `{}` by zero", const 0_u64) -> [success: bb13, unwind unreachable]; - } - - bb13: { -- _27 = Div(const 0_u64, move _28); -+ _27 = Div(const 0_u64, _1); - StorageDead(_28); - _26 = opaque::(move _27) -> [return: bb14, unwind unreachable]; - } - - bb14: { - StorageDead(_27); - StorageDead(_26); - StorageLive(_30); - StorageLive(_31); - StorageLive(_32); - _32 = _1; -- _33 = Eq(_32, const 0_u64); -- assert(!move _33, "attempt to divide `{}` by zero", const 1_u64) -> [success: bb15, unwind unreachable]; -+ _33 = _29; -+ assert(!_29, "attempt to divide `{}` by zero", const 1_u64) -> [success: bb15, unwind unreachable]; - } - - bb15: { -- _31 = Div(const 1_u64, move _32); -+ _31 = Div(const 1_u64, _1); - StorageDead(_32); - _30 = opaque::(move _31) -> [return: bb16, unwind unreachable]; - } - - bb16: { - StorageDead(_31); - StorageDead(_30); - StorageLive(_34); - StorageLive(_35); - StorageLive(_36); - _36 = _1; -- _37 = Eq(const 0_u64, const 0_u64); -- assert(!move _37, "attempt to calculate the remainder of `{}` with a divisor of zero", _36) -> [success: bb17, unwind unreachable]; -+ _37 = const true; -+ assert(!const true, "attempt to calculate the remainder of `{}` with a divisor of zero", _1) -> [success: bb17, unwind unreachable]; - } - - bb17: { -- _35 = Rem(move _36, const 0_u64); -+ _35 = Rem(_1, const 0_u64); - StorageDead(_36); - _34 = opaque::(move _35) -> [return: bb18, unwind unreachable]; - } - - bb18: { - StorageDead(_35); - StorageDead(_34); - StorageLive(_38); - StorageLive(_39); - StorageLive(_40); - _40 = _1; -- _41 = Eq(const 1_u64, const 0_u64); -- assert(!move _41, "attempt to calculate the remainder of `{}` with a divisor of zero", _40) -> [success: bb19, unwind unreachable]; -+ _41 = const false; -+ assert(!const false, "attempt to calculate the remainder of `{}` with a divisor of zero", _1) -> [success: bb19, unwind unreachable]; - } - - bb19: { -- _39 = Rem(move _40, const 1_u64); -+ _39 = Rem(_1, const 1_u64); - StorageDead(_40); - _38 = opaque::(move _39) -> [return: bb20, unwind unreachable]; - } - - bb20: { - StorageDead(_39); - StorageDead(_38); - StorageLive(_42); - StorageLive(_43); - StorageLive(_44); - _44 = _1; -- _45 = Eq(_44, const 0_u64); -- assert(!move _45, "attempt to calculate the remainder of `{}` with a divisor of zero", const 0_u64) -> [success: bb21, unwind unreachable]; -+ _45 = _29; -+ assert(!_29, "attempt to calculate the remainder of `{}` with a divisor of zero", const 0_u64) -> [success: bb21, unwind unreachable]; - } - - bb21: { -- _43 = Rem(const 0_u64, move _44); -+ _43 = Rem(const 0_u64, _1); - StorageDead(_44); - _42 = opaque::(move _43) -> [return: bb22, unwind unreachable]; - } - - bb22: { - StorageDead(_43); - StorageDead(_42); - StorageLive(_46); - StorageLive(_47); - StorageLive(_48); - _48 = _1; -- _49 = Eq(_48, const 0_u64); -- assert(!move _49, "attempt to calculate the remainder of `{}` with a divisor of zero", const 1_u64) -> [success: bb23, unwind unreachable]; -+ _49 = _29; -+ assert(!_29, "attempt to calculate the remainder of `{}` with a divisor of zero", const 1_u64) -> [success: bb23, unwind unreachable]; - } - - bb23: { -- _47 = Rem(const 1_u64, move _48); -+ _47 = Rem(const 1_u64, _1); - StorageDead(_48); - _46 = opaque::(move _47) -> [return: bb24, unwind unreachable]; - } - - bb24: { - StorageDead(_47); - StorageDead(_46); - StorageLive(_50); - StorageLive(_51); - StorageLive(_52); - _52 = _1; -- _51 = BitAnd(move _52, const 0_u64); -+ _51 = BitAnd(_1, const 0_u64); - StorageDead(_52); - _50 = opaque::(move _51) -> [return: bb25, unwind unreachable]; - } - - bb25: { - StorageDead(_51); - StorageDead(_50); - StorageLive(_53); - StorageLive(_54); - StorageLive(_55); - _55 = _1; -- _54 = BitOr(move _55, const 0_u64); -+ _54 = BitOr(_1, const 0_u64); - StorageDead(_55); - _53 = opaque::(move _54) -> [return: bb26, unwind unreachable]; - } - - bb26: { - StorageDead(_54); - StorageDead(_53); - StorageLive(_56); - StorageLive(_57); - StorageLive(_58); - _58 = _1; -- _57 = BitXor(move _58, const 0_u64); -+ _57 = BitXor(_1, const 0_u64); - StorageDead(_58); - _56 = opaque::(move _57) -> [return: bb27, unwind unreachable]; - } - - bb27: { - StorageDead(_57); - StorageDead(_56); - StorageLive(_59); - StorageLive(_60); - StorageLive(_61); - _61 = _1; -- _62 = const 0_i32 as u32 (IntToInt); -- _63 = Lt(move _62, const 64_u32); -- assert(move _63, "attempt to shift right by `{}`, which would overflow", const 0_i32) -> [success: bb28, unwind unreachable]; -+ _62 = const 0_u32; -+ _63 = const true; -+ assert(const true, "attempt to shift right by `{}`, which would overflow", const 0_i32) -> [success: bb28, unwind unreachable]; - } - - bb28: { -- _60 = Shr(move _61, const 0_i32); -+ _60 = Shr(_1, const 0_i32); - StorageDead(_61); - _59 = opaque::(move _60) -> [return: bb29, unwind unreachable]; - } - - bb29: { - StorageDead(_60); - StorageDead(_59); - StorageLive(_64); - StorageLive(_65); - StorageLive(_66); - _66 = _1; -- _67 = const 0_i32 as u32 (IntToInt); -- _68 = Lt(move _67, const 64_u32); -- assert(move _68, "attempt to shift left by `{}`, which would overflow", const 0_i32) -> [success: bb30, unwind unreachable]; -+ _67 = const 0_u32; -+ _68 = const true; -+ assert(const true, "attempt to shift left by `{}`, which would overflow", const 0_i32) -> [success: bb30, unwind unreachable]; - } - - bb30: { -- _65 = Shl(move _66, const 0_i32); -+ _65 = Shl(_1, const 0_i32); - StorageDead(_66); - _64 = opaque::(move _65) -> [return: bb31, unwind unreachable]; - } - - bb31: { - StorageDead(_65); - StorageDead(_64); _0 = const (); return; } diff --git a/tests/mir-opt/gvn.arithmetic_checked.GVN.panic-unwind.diff b/tests/mir-opt/gvn.arithmetic_checked.GVN.panic-unwind.diff index 9fbb8df79a1db..d100a77fee5f3 100644 --- a/tests/mir-opt/gvn.arithmetic_checked.GVN.panic-unwind.diff +++ b/tests/mir-opt/gvn.arithmetic_checked.GVN.panic-unwind.diff @@ -20,57 +20,6 @@ let mut _15: u64; let mut _16: u64; let mut _17: (u64, bool); - let _18: (); - let mut _19: u64; - let mut _20: u64; - let mut _21: bool; - let _22: (); - let mut _23: u64; - let mut _24: u64; - let mut _25: bool; - let _26: (); - let mut _27: u64; - let mut _28: u64; - let mut _29: bool; - let _30: (); - let mut _31: u64; - let mut _32: u64; - let mut _33: bool; - let _34: (); - let mut _35: u64; - let mut _36: u64; - let mut _37: bool; - let _38: (); - let mut _39: u64; - let mut _40: u64; - let mut _41: bool; - let _42: (); - let mut _43: u64; - let mut _44: u64; - let mut _45: bool; - let _46: (); - let mut _47: u64; - let mut _48: u64; - let mut _49: bool; - let _50: (); - let mut _51: u64; - let mut _52: u64; - let _53: (); - let mut _54: u64; - let mut _55: u64; - let _56: (); - let mut _57: u64; - let mut _58: u64; - let _59: (); - let mut _60: u64; - let mut _61: u64; - let mut _62: u32; - let mut _63: bool; - let _64: (); - let mut _65: u64; - let mut _66: u64; - let mut _67: u32; - let mut _68: bool; bb0: { StorageLive(_2); @@ -149,249 +98,6 @@ bb8: { StorageDead(_15); StorageDead(_14); - StorageLive(_18); - StorageLive(_19); - StorageLive(_20); - _20 = _1; -- _21 = Eq(const 0_u64, const 0_u64); -- assert(!move _21, "attempt to divide `{}` by zero", _20) -> [success: bb9, unwind continue]; -+ _21 = const true; -+ assert(!const true, "attempt to divide `{}` by zero", _1) -> [success: bb9, unwind continue]; - } - - bb9: { -- _19 = Div(move _20, const 0_u64); -+ _19 = Div(_1, const 0_u64); - StorageDead(_20); - _18 = opaque::(move _19) -> [return: bb10, unwind continue]; - } - - bb10: { - StorageDead(_19); - StorageDead(_18); - StorageLive(_22); - StorageLive(_23); - StorageLive(_24); - _24 = _1; -- _25 = Eq(const 1_u64, const 0_u64); -- assert(!move _25, "attempt to divide `{}` by zero", _24) -> [success: bb11, unwind continue]; -+ _25 = const false; -+ assert(!const false, "attempt to divide `{}` by zero", _1) -> [success: bb11, unwind continue]; - } - - bb11: { -- _23 = Div(move _24, const 1_u64); -+ _23 = Div(_1, const 1_u64); - StorageDead(_24); - _22 = opaque::(move _23) -> [return: bb12, unwind continue]; - } - - bb12: { - StorageDead(_23); - StorageDead(_22); - StorageLive(_26); - StorageLive(_27); - StorageLive(_28); - _28 = _1; -- _29 = Eq(_28, const 0_u64); -- assert(!move _29, "attempt to divide `{}` by zero", const 0_u64) -> [success: bb13, unwind continue]; -+ _29 = Eq(_1, const 0_u64); -+ assert(!_29, "attempt to divide `{}` by zero", const 0_u64) -> [success: bb13, unwind continue]; - } - - bb13: { -- _27 = Div(const 0_u64, move _28); -+ _27 = Div(const 0_u64, _1); - StorageDead(_28); - _26 = opaque::(move _27) -> [return: bb14, unwind continue]; - } - - bb14: { - StorageDead(_27); - StorageDead(_26); - StorageLive(_30); - StorageLive(_31); - StorageLive(_32); - _32 = _1; -- _33 = Eq(_32, const 0_u64); -- assert(!move _33, "attempt to divide `{}` by zero", const 1_u64) -> [success: bb15, unwind continue]; -+ _33 = _29; -+ assert(!_29, "attempt to divide `{}` by zero", const 1_u64) -> [success: bb15, unwind continue]; - } - - bb15: { -- _31 = Div(const 1_u64, move _32); -+ _31 = Div(const 1_u64, _1); - StorageDead(_32); - _30 = opaque::(move _31) -> [return: bb16, unwind continue]; - } - - bb16: { - StorageDead(_31); - StorageDead(_30); - StorageLive(_34); - StorageLive(_35); - StorageLive(_36); - _36 = _1; -- _37 = Eq(const 0_u64, const 0_u64); -- assert(!move _37, "attempt to calculate the remainder of `{}` with a divisor of zero", _36) -> [success: bb17, unwind continue]; -+ _37 = const true; -+ assert(!const true, "attempt to calculate the remainder of `{}` with a divisor of zero", _1) -> [success: bb17, unwind continue]; - } - - bb17: { -- _35 = Rem(move _36, const 0_u64); -+ _35 = Rem(_1, const 0_u64); - StorageDead(_36); - _34 = opaque::(move _35) -> [return: bb18, unwind continue]; - } - - bb18: { - StorageDead(_35); - StorageDead(_34); - StorageLive(_38); - StorageLive(_39); - StorageLive(_40); - _40 = _1; -- _41 = Eq(const 1_u64, const 0_u64); -- assert(!move _41, "attempt to calculate the remainder of `{}` with a divisor of zero", _40) -> [success: bb19, unwind continue]; -+ _41 = const false; -+ assert(!const false, "attempt to calculate the remainder of `{}` with a divisor of zero", _1) -> [success: bb19, unwind continue]; - } - - bb19: { -- _39 = Rem(move _40, const 1_u64); -+ _39 = Rem(_1, const 1_u64); - StorageDead(_40); - _38 = opaque::(move _39) -> [return: bb20, unwind continue]; - } - - bb20: { - StorageDead(_39); - StorageDead(_38); - StorageLive(_42); - StorageLive(_43); - StorageLive(_44); - _44 = _1; -- _45 = Eq(_44, const 0_u64); -- assert(!move _45, "attempt to calculate the remainder of `{}` with a divisor of zero", const 0_u64) -> [success: bb21, unwind continue]; -+ _45 = _29; -+ assert(!_29, "attempt to calculate the remainder of `{}` with a divisor of zero", const 0_u64) -> [success: bb21, unwind continue]; - } - - bb21: { -- _43 = Rem(const 0_u64, move _44); -+ _43 = Rem(const 0_u64, _1); - StorageDead(_44); - _42 = opaque::(move _43) -> [return: bb22, unwind continue]; - } - - bb22: { - StorageDead(_43); - StorageDead(_42); - StorageLive(_46); - StorageLive(_47); - StorageLive(_48); - _48 = _1; -- _49 = Eq(_48, const 0_u64); -- assert(!move _49, "attempt to calculate the remainder of `{}` with a divisor of zero", const 1_u64) -> [success: bb23, unwind continue]; -+ _49 = _29; -+ assert(!_29, "attempt to calculate the remainder of `{}` with a divisor of zero", const 1_u64) -> [success: bb23, unwind continue]; - } - - bb23: { -- _47 = Rem(const 1_u64, move _48); -+ _47 = Rem(const 1_u64, _1); - StorageDead(_48); - _46 = opaque::(move _47) -> [return: bb24, unwind continue]; - } - - bb24: { - StorageDead(_47); - StorageDead(_46); - StorageLive(_50); - StorageLive(_51); - StorageLive(_52); - _52 = _1; -- _51 = BitAnd(move _52, const 0_u64); -+ _51 = BitAnd(_1, const 0_u64); - StorageDead(_52); - _50 = opaque::(move _51) -> [return: bb25, unwind continue]; - } - - bb25: { - StorageDead(_51); - StorageDead(_50); - StorageLive(_53); - StorageLive(_54); - StorageLive(_55); - _55 = _1; -- _54 = BitOr(move _55, const 0_u64); -+ _54 = BitOr(_1, const 0_u64); - StorageDead(_55); - _53 = opaque::(move _54) -> [return: bb26, unwind continue]; - } - - bb26: { - StorageDead(_54); - StorageDead(_53); - StorageLive(_56); - StorageLive(_57); - StorageLive(_58); - _58 = _1; -- _57 = BitXor(move _58, const 0_u64); -+ _57 = BitXor(_1, const 0_u64); - StorageDead(_58); - _56 = opaque::(move _57) -> [return: bb27, unwind continue]; - } - - bb27: { - StorageDead(_57); - StorageDead(_56); - StorageLive(_59); - StorageLive(_60); - StorageLive(_61); - _61 = _1; -- _62 = const 0_i32 as u32 (IntToInt); -- _63 = Lt(move _62, const 64_u32); -- assert(move _63, "attempt to shift right by `{}`, which would overflow", const 0_i32) -> [success: bb28, unwind continue]; -+ _62 = const 0_u32; -+ _63 = const true; -+ assert(const true, "attempt to shift right by `{}`, which would overflow", const 0_i32) -> [success: bb28, unwind continue]; - } - - bb28: { -- _60 = Shr(move _61, const 0_i32); -+ _60 = Shr(_1, const 0_i32); - StorageDead(_61); - _59 = opaque::(move _60) -> [return: bb29, unwind continue]; - } - - bb29: { - StorageDead(_60); - StorageDead(_59); - StorageLive(_64); - StorageLive(_65); - StorageLive(_66); - _66 = _1; -- _67 = const 0_i32 as u32 (IntToInt); -- _68 = Lt(move _67, const 64_u32); -- assert(move _68, "attempt to shift left by `{}`, which would overflow", const 0_i32) -> [success: bb30, unwind continue]; -+ _67 = const 0_u32; -+ _68 = const true; -+ assert(const true, "attempt to shift left by `{}`, which would overflow", const 0_i32) -> [success: bb30, unwind continue]; - } - - bb30: { -- _65 = Shl(move _66, const 0_i32); -+ _65 = Shl(_1, const 0_i32); - StorageDead(_66); - _64 = opaque::(move _65) -> [return: bb31, unwind continue]; - } - - bb31: { - StorageDead(_65); - StorageDead(_64); _0 = const (); return; } diff --git a/tests/mir-opt/gvn.comparison.GVN.panic-abort.diff b/tests/mir-opt/gvn.comparison.GVN.panic-abort.diff new file mode 100644 index 0000000000000..ee3b9da2122bd --- /dev/null +++ b/tests/mir-opt/gvn.comparison.GVN.panic-abort.diff @@ -0,0 +1,94 @@ +- // MIR for `comparison` before GVN ++ // MIR for `comparison` after GVN + + fn comparison(_1: u64, _2: u64) -> () { + debug x => _1; + debug y => _2; + let mut _0: (); + let _3: (); + let mut _4: bool; + let mut _5: u64; + let mut _6: u64; + let _7: (); + let mut _8: bool; + let mut _9: u64; + let mut _10: u64; + let _11: (); + let mut _12: bool; + let mut _13: u64; + let mut _14: u64; + let _15: (); + let mut _16: bool; + let mut _17: u64; + let mut _18: u64; + + bb0: { + StorageLive(_3); + StorageLive(_4); + StorageLive(_5); + _5 = _1; + StorageLive(_6); + _6 = _1; +- _4 = Eq(move _5, move _6); ++ _4 = Eq(_1, _1); + StorageDead(_6); + StorageDead(_5); + _3 = opaque::(move _4) -> [return: bb1, unwind unreachable]; + } + + bb1: { + StorageDead(_4); + StorageDead(_3); + StorageLive(_7); + StorageLive(_8); + StorageLive(_9); + _9 = _1; + StorageLive(_10); + _10 = _1; +- _8 = Ne(move _9, move _10); ++ _8 = Ne(_1, _1); + StorageDead(_10); + StorageDead(_9); + _7 = opaque::(move _8) -> [return: bb2, unwind unreachable]; + } + + bb2: { + StorageDead(_8); + StorageDead(_7); + StorageLive(_11); + StorageLive(_12); + StorageLive(_13); + _13 = _1; + StorageLive(_14); + _14 = _2; +- _12 = Eq(move _13, move _14); ++ _12 = Eq(_1, _2); + StorageDead(_14); + StorageDead(_13); + _11 = opaque::(move _12) -> [return: bb3, unwind unreachable]; + } + + bb3: { + StorageDead(_12); + StorageDead(_11); + StorageLive(_15); + StorageLive(_16); + StorageLive(_17); + _17 = _1; + StorageLive(_18); + _18 = _2; +- _16 = Ne(move _17, move _18); ++ _16 = Ne(_1, _2); + StorageDead(_18); + StorageDead(_17); + _15 = opaque::(move _16) -> [return: bb4, unwind unreachable]; + } + + bb4: { + StorageDead(_16); + StorageDead(_15); + _0 = const (); + return; + } + } + diff --git a/tests/mir-opt/gvn.comparison.GVN.panic-unwind.diff b/tests/mir-opt/gvn.comparison.GVN.panic-unwind.diff new file mode 100644 index 0000000000000..a1408fe3434cb --- /dev/null +++ b/tests/mir-opt/gvn.comparison.GVN.panic-unwind.diff @@ -0,0 +1,94 @@ +- // MIR for `comparison` before GVN ++ // MIR for `comparison` after GVN + + fn comparison(_1: u64, _2: u64) -> () { + debug x => _1; + debug y => _2; + let mut _0: (); + let _3: (); + let mut _4: bool; + let mut _5: u64; + let mut _6: u64; + let _7: (); + let mut _8: bool; + let mut _9: u64; + let mut _10: u64; + let _11: (); + let mut _12: bool; + let mut _13: u64; + let mut _14: u64; + let _15: (); + let mut _16: bool; + let mut _17: u64; + let mut _18: u64; + + bb0: { + StorageLive(_3); + StorageLive(_4); + StorageLive(_5); + _5 = _1; + StorageLive(_6); + _6 = _1; +- _4 = Eq(move _5, move _6); ++ _4 = Eq(_1, _1); + StorageDead(_6); + StorageDead(_5); + _3 = opaque::(move _4) -> [return: bb1, unwind continue]; + } + + bb1: { + StorageDead(_4); + StorageDead(_3); + StorageLive(_7); + StorageLive(_8); + StorageLive(_9); + _9 = _1; + StorageLive(_10); + _10 = _1; +- _8 = Ne(move _9, move _10); ++ _8 = Ne(_1, _1); + StorageDead(_10); + StorageDead(_9); + _7 = opaque::(move _8) -> [return: bb2, unwind continue]; + } + + bb2: { + StorageDead(_8); + StorageDead(_7); + StorageLive(_11); + StorageLive(_12); + StorageLive(_13); + _13 = _1; + StorageLive(_14); + _14 = _2; +- _12 = Eq(move _13, move _14); ++ _12 = Eq(_1, _2); + StorageDead(_14); + StorageDead(_13); + _11 = opaque::(move _12) -> [return: bb3, unwind continue]; + } + + bb3: { + StorageDead(_12); + StorageDead(_11); + StorageLive(_15); + StorageLive(_16); + StorageLive(_17); + _17 = _1; + StorageLive(_18); + _18 = _2; +- _16 = Ne(move _17, move _18); ++ _16 = Ne(_1, _2); + StorageDead(_18); + StorageDead(_17); + _15 = opaque::(move _16) -> [return: bb4, unwind continue]; + } + + bb4: { + StorageDead(_16); + StorageDead(_15); + _0 = const (); + return; + } + } + diff --git a/tests/mir-opt/gvn.repeat.GVN.panic-abort.diff b/tests/mir-opt/gvn.repeat.GVN.panic-abort.diff new file mode 100644 index 0000000000000..37915f8578d21 --- /dev/null +++ b/tests/mir-opt/gvn.repeat.GVN.panic-abort.diff @@ -0,0 +1,79 @@ +- // MIR for `repeat` before GVN ++ // MIR for `repeat` after GVN + + fn repeat() -> () { + let mut _0: (); + let _1: i32; + let mut _3: i32; + let mut _4: i32; + let mut _5: i32; + let mut _6: i32; + let mut _7: i32; + let mut _8: i32; + let mut _9: i32; + let mut _10: i32; + let mut _11: i32; + let mut _12: i32; + scope 1 { + debug val => _1; + let _2: [i32; 10]; + scope 2 { + debug array => _2; + } + } + + bb0: { +- StorageLive(_1); ++ nop; + _1 = const 5_i32; + StorageLive(_2); + StorageLive(_3); +- _3 = _1; ++ _3 = const 5_i32; + StorageLive(_4); +- _4 = _1; ++ _4 = const 5_i32; + StorageLive(_5); +- _5 = _1; ++ _5 = const 5_i32; + StorageLive(_6); +- _6 = _1; ++ _6 = const 5_i32; + StorageLive(_7); +- _7 = _1; ++ _7 = const 5_i32; + StorageLive(_8); +- _8 = _1; ++ _8 = const 5_i32; + StorageLive(_9); +- _9 = _1; ++ _9 = const 5_i32; + StorageLive(_10); +- _10 = _1; ++ _10 = const 5_i32; + StorageLive(_11); +- _11 = _1; ++ _11 = const 5_i32; + StorageLive(_12); +- _12 = _1; +- _2 = [move _3, move _4, move _5, move _6, move _7, move _8, move _9, move _10, move _11, move _12]; ++ _12 = const 5_i32; ++ _2 = [const 5_i32; 10]; + StorageDead(_12); + StorageDead(_11); + StorageDead(_10); + StorageDead(_9); + StorageDead(_8); + StorageDead(_7); + StorageDead(_6); + StorageDead(_5); + StorageDead(_4); + StorageDead(_3); + _0 = const (); + StorageDead(_2); +- StorageDead(_1); ++ nop; + return; + } + } + diff --git a/tests/mir-opt/gvn.repeat.GVN.panic-unwind.diff b/tests/mir-opt/gvn.repeat.GVN.panic-unwind.diff new file mode 100644 index 0000000000000..37915f8578d21 --- /dev/null +++ b/tests/mir-opt/gvn.repeat.GVN.panic-unwind.diff @@ -0,0 +1,79 @@ +- // MIR for `repeat` before GVN ++ // MIR for `repeat` after GVN + + fn repeat() -> () { + let mut _0: (); + let _1: i32; + let mut _3: i32; + let mut _4: i32; + let mut _5: i32; + let mut _6: i32; + let mut _7: i32; + let mut _8: i32; + let mut _9: i32; + let mut _10: i32; + let mut _11: i32; + let mut _12: i32; + scope 1 { + debug val => _1; + let _2: [i32; 10]; + scope 2 { + debug array => _2; + } + } + + bb0: { +- StorageLive(_1); ++ nop; + _1 = const 5_i32; + StorageLive(_2); + StorageLive(_3); +- _3 = _1; ++ _3 = const 5_i32; + StorageLive(_4); +- _4 = _1; ++ _4 = const 5_i32; + StorageLive(_5); +- _5 = _1; ++ _5 = const 5_i32; + StorageLive(_6); +- _6 = _1; ++ _6 = const 5_i32; + StorageLive(_7); +- _7 = _1; ++ _7 = const 5_i32; + StorageLive(_8); +- _8 = _1; ++ _8 = const 5_i32; + StorageLive(_9); +- _9 = _1; ++ _9 = const 5_i32; + StorageLive(_10); +- _10 = _1; ++ _10 = const 5_i32; + StorageLive(_11); +- _11 = _1; ++ _11 = const 5_i32; + StorageLive(_12); +- _12 = _1; +- _2 = [move _3, move _4, move _5, move _6, move _7, move _8, move _9, move _10, move _11, move _12]; ++ _12 = const 5_i32; ++ _2 = [const 5_i32; 10]; + StorageDead(_12); + StorageDead(_11); + StorageDead(_10); + StorageDead(_9); + StorageDead(_8); + StorageDead(_7); + StorageDead(_6); + StorageDead(_5); + StorageDead(_4); + StorageDead(_3); + _0 = const (); + StorageDead(_2); +- StorageDead(_1); ++ nop; + return; + } + } + diff --git a/tests/mir-opt/gvn.rs b/tests/mir-opt/gvn.rs index 55de186edf163..1c14f818044f7 100644 --- a/tests/mir-opt/gvn.rs +++ b/tests/mir-opt/gvn.rs @@ -1,4 +1,3 @@ -// skip-filecheck // unit-test: GVN // EMIT_MIR_FOR_EACH_PANIC_STRATEGY // only-64bit @@ -15,48 +14,120 @@ use std::mem::transmute; struct S(T); fn subexpression_elimination(x: u64, y: u64, mut z: u64) { + // CHECK-LABEL: fn subexpression_elimination( + + // CHECK: [[add:_.*]] = Add(_1, _2); + // CHECK: opaque::([[add]]) opaque(x + y); + // CHECK: [[mul:_.*]] = Mul(_1, _2); + // CHECK: opaque::([[mul]]) opaque(x * y); + // CHECK: [[sub:_.*]] = Sub(_1, _2); + // CHECK: opaque::([[sub]]) opaque(x - y); + // CHECK: [[div:_.*]] = Div(_1, _2); + // CHECK: opaque::([[div]]) opaque(x / y); + // CHECK: [[rem:_.*]] = Rem(_1, _2); + // CHECK: opaque::([[rem]]) opaque(x % y); + // CHECK: [[and:_.*]] = BitAnd(_1, _2); + // CHECK: opaque::([[and]]) opaque(x & y); + // CHECK: [[or:_.*]] = BitOr(_1, _2); + // CHECK: opaque::([[or]]) opaque(x | y); + // CHECK: [[xor:_.*]] = BitXor(_1, _2); + // CHECK: opaque::([[xor]]) opaque(x ^ y); + // CHECK: [[shl:_.*]] = Shl(_1, _2); + // CHECK: opaque::([[shl]]) opaque(x << y); + // CHECK: [[shr:_.*]] = Shr(_1, _2); + // CHECK: opaque::([[shr]]) opaque(x >> y); + // CHECK: [[int:_.*]] = _1 as u32 (IntToInt); + // CHECK: opaque::([[int]]) opaque(x as u32); + // CHECK: [[float:_.*]] = _1 as f32 (IntToFloat); + // CHECK: opaque::([[float]]) opaque(x as f32); + // CHECK: [[wrap:_.*]] = S::(_1); + // CHECK: opaque::>([[wrap]]) opaque(S(x)); + // CHECK: opaque::(_1) opaque(S(x).0); // Those are duplicates to substitute somehow. - opaque((x + y) + z); - opaque((x * y) + z); - opaque((x - y) + z); - opaque((x / y) + z); - opaque((x % y) + z); - opaque((x & y) + z); - opaque((x | y) + z); - opaque((x ^ y) + z); - opaque((x << y) + z); - opaque((x >> y) + z); + // CHECK: opaque::([[add]]) + opaque(x + y); + // CHECK: opaque::([[mul]]) + opaque(x * y); + // CHECK: opaque::([[sub]]) + opaque(x - y); + // CHECK: opaque::([[div]]) + opaque(x / y); + // CHECK: opaque::([[rem]]) + opaque(x % y); + // CHECK: opaque::([[and]]) + opaque(x & y); + // CHECK: opaque::([[or]]) + opaque(x | y); + // CHECK: opaque::([[xor]]) + opaque(x ^ y); + // CHECK: opaque::([[shl]]) + opaque(x << y); + // CHECK: opaque::([[shr]]) + opaque(x >> y); + // CHECK: opaque::([[int]]) + opaque(x as u32); + // CHECK: opaque::([[float]]) + opaque(x as f32); + // CHECK: opaque::>([[wrap]]) opaque(S(x)); + // CHECK: opaque::(_1) opaque(S(x).0); + // We can substitute through a complex expression. + // CHECK: [[compound:_.*]] = Sub([[mul]], _2); + // CHECK: opaque::([[compound]]) + // CHECK: opaque::([[compound]]) + opaque((x * y) - y); + opaque((x * y) - y); + // We can substitute through an immutable reference too. + // CHECK: [[ref:_.*]] = &_3; + // CHECK: [[deref:_.*]] = (*[[ref]]); + // CHECK: [[addref:_.*]] = Add([[deref]], _1); + // CHECK: opaque::([[addref]]) + // CHECK: opaque::([[addref]]) let a = &z; opaque(*a + x); opaque(*a + x); // But not through a mutable reference or a pointer. + // CHECK: [[mut:_.*]] = &mut _3; + // CHECK: [[addmut:_.*]] = Add( + // CHECK: opaque::(move [[addmut]]) + // CHECK: [[addmut2:_.*]] = Add( + // CHECK: opaque::(move [[addmut2]]) let b = &mut z; opaque(*b + x); opaque(*b + x); unsafe { + // CHECK: [[raw:_.*]] = &raw const _3; + // CHECK: [[addraw:_.*]] = Add( + // CHECK: opaque::(move [[addraw]]) + // CHECK: [[addraw2:_.*]] = Add( + // CHECK: opaque::(move [[addraw2]]) let c = &raw const z; opaque(*c + x); opaque(*c + x); + // CHECK: [[ptr:_.*]] = &raw mut _3; + // CHECK: [[addptr:_.*]] = Add( + // CHECK: opaque::(move [[addptr]]) + // CHECK: [[addptr2:_.*]] = Add( + // CHECK: opaque::(move [[addptr2]]) let d = &raw mut z; opaque(*d + x); opaque(*d + x); @@ -64,12 +135,21 @@ fn subexpression_elimination(x: u64, y: u64, mut z: u64) { // We can substitute again, but not with the earlier computations. // Important: `e` is not `a`! + // CHECK: [[ref2:_.*]] = &_3; + // CHECK: [[deref2:_.*]] = (*[[ref2]]); + // CHECK: [[addref2:_.*]] = Add([[deref2]], _1); + // CHECK: opaque::([[addref2]]) + // CHECK: opaque::([[addref2]]) let e = &z; opaque(*e + x); opaque(*e + x); } fn wrap_unwrap(x: T) -> T { + // CHECK-LABEL: fn wrap_unwrap( + // CHECK: [[some:_.*]] = Option::::Some(_1); + // CHECK: switchInt(const 1_isize) + // CHECK: _0 = _1; match Some(x) { Some(y) => y, None => panic!(), @@ -77,155 +157,354 @@ fn wrap_unwrap(x: T) -> T { } fn repeated_index(x: T, idx: usize) { + // CHECK-LABEL: fn repeated_index( + // CHECK: [[a:_.*]] = [_1; N]; let a = [x; N]; + // CHECK: opaque::(_1) opaque(a[0]); + // CHECK: opaque::(_1) opaque(a[idx]); } +/// Verify symbolic integer arithmetic simplifications. fn arithmetic(x: u64) { + // CHECK-LABEL: fn arithmetic( + // CHECK: [[add:_.*]] = Add(_1, const 0_u64); + // CHECK: opaque::(move [[add]]) opaque(x + 0); + // CHECK: [[sub:_.*]] = Sub(_1, const 0_u64); + // CHECK: opaque::(move [[sub]]) opaque(x - 0); + // CHECK: [[mul0:_.*]] = Mul(_1, const 0_u64); + // CHECK: opaque::(move [[mul0]]) opaque(x * 0); + // CHECK: [[mul1:_.*]] = Mul(_1, const 1_u64); + // CHECK: opaque::(move [[mul1]]) opaque(x * 1); + // CHECK: [[div0:_.*]] = Div(_1, const 0_u64); + // CHECK: opaque::(move [[div0]]) opaque(x / 0); + // CHECK: [[div1:_.*]] = Div(_1, const 1_u64); + // CHECK: opaque::(move [[div1]]) opaque(x / 1); + // CHECK: [[zdiv:_.*]] = Div(const 0_u64, _1); + // CHECK: opaque::(move [[zdiv]]) opaque(0 / x); + // CHECK: [[odiv:_.*]] = Div(const 1_u64, _1); + // CHECK: opaque::(move [[odiv]]) opaque(1 / x); + // CHECK: [[rem0:_.*]] = Rem(_1, const 0_u64); + // CHECK: opaque::(move [[rem0]]) opaque(x % 0); + // CHECK: [[rem1:_.*]] = Rem(_1, const 1_u64); + // CHECK: opaque::(move [[rem1]]) opaque(x % 1); + // CHECK: [[zrem:_.*]] = Rem(const 0_u64, _1); + // CHECK: opaque::(move [[zrem]]) opaque(0 % x); + // CHECK: [[orem:_.*]] = Rem(const 1_u64, _1); + // CHECK: opaque::(move [[orem]]) opaque(1 % x); + // CHECK: [[and:_.*]] = BitAnd(_1, const 0_u64); + // CHECK: opaque::(move [[and]]) opaque(x & 0); + // CHECK: [[or:_.*]] = BitOr(_1, const 0_u64); + // CHECK: opaque::(move [[or]]) opaque(x | 0); + // CHECK: [[xor:_.*]] = BitXor(_1, const 0_u64); + // CHECK: opaque::(move [[xor]]) opaque(x ^ 0); + // CHECK: [[shr:_.*]] = Shr(_1, const 0_i32); + // CHECK: opaque::(move [[shr]]) opaque(x >> 0); + // CHECK: [[shl:_.*]] = Shl(_1, const 0_i32); + // CHECK: opaque::(move [[shl]]) opaque(x << 0); } +fn comparison(x: u64, y: u64) { + // CHECK-LABEL: fn comparison( + // CHECK: [[eqxx:_.*]] = Eq(_1, _1); + // CHECK: opaque::(move [[eqxx]]) + opaque(x == x); + // CHECK: [[nexx:_.*]] = Ne(_1, _1); + // CHECK: opaque::(move [[nexx]]) + opaque(x != x); + // CHECK: [[eqxy:_.*]] = Eq(_1, _2); + // CHECK: opaque::(move [[eqxy]]) + opaque(x == y); + // CHECK: [[nexy:_.*]] = Ne(_1, _2); + // CHECK: opaque::(move [[nexy]]) + opaque(x != y); +} + +/// Verify symbolic integer arithmetic simplifications on checked ops. #[rustc_inherit_overflow_checks] fn arithmetic_checked(x: u64) { + // CHECK-LABEL: fn arithmetic_checked( + // CHECK: [[cadd:_.*]] = CheckedAdd(_1, const 0_u64); + // CHECK: [[add:_.*]] = move ([[cadd]].0: u64); + // CHECK: opaque::(move [[add]]) opaque(x + 0); + // CHECK: [[csub:_.*]] = CheckedSub(_1, const 0_u64); + // CHECK: [[sub:_.*]] = move ([[csub]].0: u64); + // CHECK: opaque::(move [[sub]]) opaque(x - 0); + // CHECK: [[cmul0:_.*]] = CheckedMul(_1, const 0_u64); + // CHECK: [[mul0:_.*]] = move ([[cmul0]].0: u64); + // CHECK: opaque::(move [[mul0]]) opaque(x * 0); + // CHECK: [[cmul1:_.*]] = CheckedMul(_1, const 1_u64); + // CHECK: [[mul1:_.*]] = move ([[cmul1]].0: u64); + // CHECK: opaque::(move [[mul1]]) opaque(x * 1); - opaque(x / 0); - opaque(x / 1); - opaque(0 / x); - opaque(1 / x); - opaque(x % 0); - opaque(x % 1); - opaque(0 % x); - opaque(1 % x); - opaque(x & 0); - opaque(x | 0); - opaque(x ^ 0); - opaque(x >> 0); - opaque(x << 0); } +/// Verify that we do not apply arithmetic simplifications on floats. fn arithmetic_float(x: f64) { + // CHECK-LABEL: fn arithmetic_float( + // CHECK: [[add:_.*]] = Add(_1, const 0f64); + // CHECK: opaque::(move [[add]]) opaque(x + 0.); + // CHECK: [[sub:_.*]] = Sub(_1, const 0f64); + // CHECK: opaque::(move [[sub]]) opaque(x - 0.); + // CHECK: [[mul:_.*]] = Mul(_1, const 0f64); + // CHECK: opaque::(move [[mul]]) opaque(x * 0.); + // CHECK: [[div0:_.*]] = Div(_1, const 0f64); + // CHECK: opaque::(move [[div0]]) opaque(x / 0.); + // CHECK: [[zdiv:_.*]] = Div(const 0f64, _1); + // CHECK: opaque::(move [[zdiv]]) opaque(0. / x); + // CHECK: [[rem0:_.*]] = Rem(_1, const 0f64); + // CHECK: opaque::(move [[rem0]]) opaque(x % 0.); + // CHECK: [[zrem:_.*]] = Rem(const 0f64, _1); + // CHECK: opaque::(move [[zrem]]) opaque(0. % x); // Those are not simplifiable to `true`/`false`, thanks to NaNs. + // CHECK: [[eq:_.*]] = Eq(_1, _1); + // CHECK: opaque::(move [[eq]]) opaque(x == x); + // CHECK: [[ne:_.*]] = Ne(_1, _1); + // CHECK: opaque::(move [[ne]]) opaque(x != x); } fn cast() { + // CHECK-LABEL: fn cast( let i = 1_i64; let u = 1_u64; let f = 1_f64; + // CHECK: opaque::(const 1_u8) opaque(i as u8); + // CHECK: opaque::(const 1_u16) opaque(i as u16); + // CHECK: opaque::(const 1_u32) opaque(i as u32); + // CHECK: opaque::(const 1_u64) opaque(i as u64); + // CHECK: opaque::(const 1_i8) opaque(i as i8); + // CHECK: opaque::(const 1_i16) opaque(i as i16); + // CHECK: opaque::(const 1_i32) opaque(i as i32); + // CHECK: opaque::(const 1_i64) opaque(i as i64); + // CHECK: opaque::(const 1f32) opaque(i as f32); + // CHECK: opaque::(const 1f64) opaque(i as f64); + // CHECK: opaque::(const 1_u8) opaque(u as u8); + // CHECK: opaque::(const 1_u16) opaque(u as u16); + // CHECK: opaque::(const 1_u32) opaque(u as u32); + // CHECK: opaque::(const 1_u64) opaque(u as u64); + // CHECK: opaque::(const 1_i8) opaque(u as i8); + // CHECK: opaque::(const 1_i16) opaque(u as i16); + // CHECK: opaque::(const 1_i32) opaque(u as i32); + // CHECK: opaque::(const 1_i64) opaque(u as i64); + // CHECK: opaque::(const 1f32) opaque(u as f32); + // CHECK: opaque::(const 1f64) opaque(u as f64); + // CHECK: opaque::(const 1_u8) opaque(f as u8); + // CHECK: opaque::(const 1_u16) opaque(f as u16); + // CHECK: opaque::(const 1_u32) opaque(f as u32); + // CHECK: opaque::(const 1_u64) opaque(f as u64); + // CHECK: opaque::(const 1_i8) opaque(f as i8); + // CHECK: opaque::(const 1_i16) opaque(f as i16); + // CHECK: opaque::(const 1_i32) opaque(f as i32); + // CHECK: opaque::(const 1_i64) opaque(f as i64); + // CHECK: opaque::(const 1f32) opaque(f as f32); + // CHECK: opaque::(const 1f64) opaque(f as f64); } fn multiple_branches(t: bool, x: u8, y: u8) { + // CHECK-LABEL: fn multiple_branches( + // CHECK: switchInt(_1) -> [0: [[bbf:bb.*]], otherwise: [[bbt:bb.*]]]; if t { - opaque(x + y); // a - opaque(x + y); // should reuse a + // CHECK: [[bbt]]: { + // CHECK: [[a:_.*]] = Add(_2, _3); + // CHECK: opaque::([[a]]) + // CHECK: opaque::([[a]]) + // CHECK: goto -> [[bbc:bb.*]]; + opaque(x + y); + opaque(x + y); } else { - opaque(x + y); // b - opaque(x + y); // shoud reuse b + // CHECK: [[bbf]]: { + // CHECK: [[b:_.*]] = Add(_2, _3); + // CHECK: opaque::([[b]]) + // CHECK: opaque::([[b]]) + // CHECK: goto -> [[bbc:bb.*]]; + opaque(x + y); + opaque(x + y); } - opaque(x + y); // c + // Neither `a` nor `b` dominate `c`, so we cannot reuse any of them. + // CHECK: [[bbc]]: { + // CHECK: [[c:_.*]] = Add(_2, _3); + // CHECK: opaque::([[c]]) + opaque(x + y); + + // `c` dominates both calls, so we can reuse it. if t { - opaque(x + y); // should reuse c + // CHECK: opaque::([[c]]) + opaque(x + y); } else { - opaque(x + y); // should reuse c + // CHECK: opaque::([[c]]) + opaque(x + y); } } +/// Verify that we do not reuse a `&raw? mut?` rvalue. fn references(mut x: impl Sized) { + // CHECK-LABEL: fn references( + // CHECK: [[ref1:_.*]] = &_1; + // CHECK: opaque::<&impl Sized>(move [[ref1]]) opaque(&x); - opaque(&x); // should not reuse a + // CHECK: [[ref2:_.*]] = &_1; + // CHECK: opaque::<&impl Sized>(move [[ref2]]) + opaque(&x); + // CHECK: [[ref3:_.*]] = &mut _1; + // CHECK: opaque::<&mut impl Sized>(move [[ref3]]) + opaque(&mut x); + // CHECK: [[ref4:_.*]] = &mut _1; + // CHECK: opaque::<&mut impl Sized>(move [[ref4]]) opaque(&mut x); - opaque(&mut x); // should not reuse a + // CHECK: [[ref5:_.*]] = &raw const _1; + // CHECK: opaque::<*const impl Sized>(move [[ref5]]) + opaque(&raw const x); + // CHECK: [[ref6:_.*]] = &raw const _1; + // CHECK: opaque::<*const impl Sized>(move [[ref6]]) opaque(&raw const x); - opaque(&raw const x); // should not reuse a + // CHECK: [[ref7:_.*]] = &raw mut _1; + // CHECK: opaque::<*mut impl Sized>(move [[ref7]]) + opaque(&raw mut x); + // CHECK: [[ref8:_.*]] = &raw mut _1; + // CHECK: opaque::<*mut impl Sized>(move [[ref8]]) opaque(&raw mut x); - opaque(&raw mut x); // should not reuse a let r = &mut x; - let s = S(r).0; // Obfuscate `r`. - opaque(&*s); // This is `&*r`. - opaque(&mut *s); // This is `&*r`. - opaque(&raw const *s); // This is `&*r`. - opaque(&raw mut *s); // This is `&*r`. + let s = S(r).0; // Obfuscate `r`. Following lines should still reborrow `r`. + // CHECK: [[ref9:_.*]] = &mut _1; + // CHECK: [[ref10:_.*]] = &(*[[ref9]]); + // CHECK: opaque::<&impl Sized>(move [[ref10]]) + opaque(&*s); + // CHECK: [[ref11:_.*]] = &mut (*[[ref9]]); + // CHECK: opaque::<&mut impl Sized>(move [[ref11]]) + opaque(&mut *s); + // CHECK: [[ref12:_.*]] = &raw const (*[[ref9]]); + // CHECK: opaque::<*const impl Sized>(move [[ref12]]) + opaque(&raw const *s); + // CHECK: [[ref12:_.*]] = &raw mut (*[[ref9]]); + // CHECK: opaque::<*mut impl Sized>(move [[ref12]]) + opaque(&raw mut *s); } fn dereferences(t: &mut u32, u: &impl Copy, s: &S) { + // CHECK-LABEL: fn dereferences( + + // Do not reuse dereferences of `&mut`. + // CHECK: [[st1:_.*]] = (*_1); + // CHECK: opaque::(move [[st1]]) + // CHECK: [[st2:_.*]] = (*_1); + // CHECK: opaque::(move [[st2]]) + opaque(*t); opaque(*t); - opaque(*t); // this cannot reuse a, as x is &mut. + + // Do not reuse dereferences of `*const`. + // CHECK: [[raw:_.*]] = &raw const (*_1); + // CHECK: [[st3:_.*]] = (*[[raw]]); + // CHECK: opaque::(move [[st3]]) + // CHECK: [[st4:_.*]] = (*[[raw]]); + // CHECK: opaque::(move [[st4]]) let z = &raw const *t; unsafe { opaque(*z) }; - unsafe { opaque(*z) }; // this cannot reuse a, as x is *const. + unsafe { opaque(*z) }; + + // Do not reuse dereferences of `*mut`. + // CHECK: [[ptr:_.*]] = &raw mut (*_1); + // CHECK: [[st5:_.*]] = (*[[ptr]]); + // CHECK: opaque::(move [[st5]]) + // CHECK: [[st6:_.*]] = (*[[ptr]]); + // CHECK: opaque::(move [[st6]]) let z = &raw mut *t; unsafe { opaque(*z) }; - unsafe { opaque(*z) }; // this cannot reuse a, as x is *mut. + unsafe { opaque(*z) }; + + // We can reuse dereferences of `&Freeze`. + // CHECK: [[ref:_.*]] = &(*_1); + // CHECK: [[st7:_.*]] = (*[[ref]]); + // CHECK: opaque::([[st7]]) + // CHECK: opaque::([[st7]]) let z = &*t; opaque(*z); - opaque(*z); // this can reuse, as `z` is immutable ref, Freeze and Copy. - opaque(&*z); // but not for a reborrow. + opaque(*z); + // But not in reborrows. + // CHECK: [[reborrow:_.*]] = &(*[[ref]]); + // CHECK: opaque::<&u32>(move [[reborrow]]) + opaque(&*z); + + // `*u` is not Freeze, so we cannot reuse. + // CHECK: [[st8:_.*]] = (*_2); + // CHECK: opaque::(move [[st8]]) + // CHECK: [[st9:_.*]] = (*_2); + // CHECK: opaque::(move [[st9]]) + opaque(*u); opaque(*u); - opaque(*u); // this cannot reuse, as `z` is not Freeze. + + // `*s` is not Copy, by `(*s).0` is, so we can reuse. + // CHECK: [[st10:_.*]] = ((*_3).0: u32); + // CHECK: opaque::([[st10]]) + // CHECK: opaque::([[st10]]) + opaque(s.0); opaque(s.0); - opaque(s.0); // *s is not Copy, by (*s).0 is, so we can reuse. } fn slices() { + // CHECK-LABEL: fn slices( + // CHECK: {{_.*}} = const " + // CHECK-NOT: {{_.*}} = const " let s = "my favourite slice"; // This is a `Const::Slice` in MIR. opaque(s); let t = s; // This should be the same pointer, so cannot be a `Const::Slice`. @@ -238,6 +517,7 @@ fn slices() { #[custom_mir(dialect = "analysis")] fn duplicate_slice() -> (bool, bool) { + // CHECK-LABEL: fn duplicate_slice( mir!( let au: u128; let bu: u128; @@ -246,41 +526,50 @@ fn duplicate_slice() -> (bool, bool) { let c: &str; let d: &str; { + // CHECK: [[a:_.*]] = (const "a",); + // CHECK: [[au:_.*]] = ([[a]].0: &str) as u128 (Transmute); let a = ("a",); Call(au = transmute::<_, u128>(a.0), bb1) } bb1 = { + // CHECK: [[c:_.*]] = identity::<&str>(([[a]].0: &str)) Call(c = identity(a.0), bb2) } bb2 = { + // CHECK: [[cu:_.*]] = [[c]] as u128 (Transmute); Call(cu = transmute::<_, u128>(c), bb3) } bb3 = { - let b = "a"; // This slice is different from `a.0`. - Call(bu = transmute::<_, u128>(b), bb4) // Hence `bu` is not `au`. + // This slice is different from `a.0`. Hence `bu` is not `au`. + // CHECK: [[b:_.*]] = const "a"; + // CHECK: [[bu:_.*]] = [[b]] as u128 (Transmute); + let b = "a"; + Call(bu = transmute::<_, u128>(b), bb4) } bb4 = { - Call(d = identity(b), bb5) // This returns a copy of `b`, which is not `a`. + // This returns a copy of `b`, which is not `a`. + // CHECK: [[d:_.*]] = identity::<&str>([[b]]) + Call(d = identity(b), bb5) } bb5 = { + // CHECK: [[du:_.*]] = [[d]] as u128 (Transmute); Call(du = transmute::<_, u128>(d), bb6) } bb6 = { - let direct = au == bu; // Must not fold to `true`... - let indirect = cu == du; // ...as this will not. + // `direct` must not fold to `true`, as `indirect` will not. + // CHECK: = Eq([[au]], [[bu]]); + // CHECK: = Eq([[cu]], [[du]]); + let direct = au == bu; + let indirect = cu == du; RET = (direct, indirect); Return() } ) } -fn aggregates() { - let a_array: S<[u8; 0]> = S([]); - let b_array: S<[u16; 0]> = S([]); // This must not be merged with `a_array`. - - let a_tuple: S<()> = S(()); - let b_tuple: S<()> = S(()); // But this can be with `a_tuple`. - +fn repeat() { + // CHECK-LABEL: fn repeat( + // CHECK: = [const 5_i32; 10]; let val = 5; let array = [val, val, val, val, val, val, val, val, val, val]; } @@ -290,6 +579,7 @@ fn main() { wrap_unwrap(5); repeated_index::(5, 3); arithmetic(5); + comparison(5, 6); arithmetic_checked(5); arithmetic_float(5.); cast(); @@ -299,7 +589,7 @@ fn main() { slices(); let (direct, indirect) = duplicate_slice(); assert_eq!(direct, indirect); - aggregates(); + repeat(); } #[inline(never)] @@ -314,6 +604,7 @@ fn identity(x: T) -> T { // EMIT_MIR gvn.wrap_unwrap.GVN.diff // EMIT_MIR gvn.repeated_index.GVN.diff // EMIT_MIR gvn.arithmetic.GVN.diff +// EMIT_MIR gvn.comparison.GVN.diff // EMIT_MIR gvn.arithmetic_checked.GVN.diff // EMIT_MIR gvn.arithmetic_float.GVN.diff // EMIT_MIR gvn.cast.GVN.diff @@ -322,4 +613,4 @@ fn identity(x: T) -> T { // EMIT_MIR gvn.dereferences.GVN.diff // EMIT_MIR gvn.slices.GVN.diff // EMIT_MIR gvn.duplicate_slice.GVN.diff -// EMIT_MIR gvn.aggregates.GVN.diff +// EMIT_MIR gvn.repeat.GVN.diff diff --git a/tests/mir-opt/gvn.subexpression_elimination.GVN.panic-abort.diff b/tests/mir-opt/gvn.subexpression_elimination.GVN.panic-abort.diff index 23ba2559448b8..0a747d3aef0f5 100644 --- a/tests/mir-opt/gvn.subexpression_elimination.GVN.panic-abort.diff +++ b/tests/mir-opt/gvn.subexpression_elimination.GVN.panic-abort.diff @@ -65,11 +65,11 @@ let mut _60: u64; let mut _61: u64; let mut _62: u64; - let mut _63: u64; + let _63: (); let mut _64: u64; - let _65: (); + let mut _65: u64; let mut _66: u64; - let mut _67: u64; + let _67: (); let mut _68: u64; let mut _69: u64; let mut _70: u64; @@ -77,25 +77,25 @@ let mut _72: u64; let mut _73: u64; let mut _74: u64; - let mut _75: u64; - let mut _76: u64; - let _77: (); + let mut _75: bool; + let _76: (); + let mut _77: u64; let mut _78: u64; let mut _79: u64; - let mut _80: u64; - let mut _81: u64; - let mut _82: bool; + let mut _80: bool; + let _81: (); + let mut _82: u64; let mut _83: u64; - let _84: (); - let mut _85: u64; + let mut _84: u64; + let _85: (); let mut _86: u64; let mut _87: u64; let mut _88: u64; - let mut _89: bool; + let _89: (); let mut _90: u64; - let _91: (); + let mut _91: u64; let mut _92: u64; - let mut _93: u64; + let _93: (); let mut _94: u64; let mut _95: u64; let mut _96: u64; @@ -103,93 +103,91 @@ let mut _98: u64; let mut _99: u64; let mut _100: u64; - let mut _101: u64; - let mut _102: u64; - let _103: (); - let mut _104: u64; - let mut _105: u64; + let _101: (); + let mut _102: u32; + let mut _103: u64; + let _104: (); + let mut _105: f32; let mut _106: u64; - let mut _107: u64; - let mut _108: u64; - let _109: (); - let mut _110: u64; + let _107: (); + let mut _108: S; + let mut _109: u64; + let _110: (); let mut _111: u64; - let mut _112: u64; + let mut _112: S; let mut _113: u64; - let mut _114: u64; - let _115: (); + let _114: (); + let mut _115: u64; let mut _116: u64; let mut _117: u64; let mut _118: u64; let mut _119: u64; - let mut _120: u64; - let _121: (); - let mut _122: S; + let _120: (); + let mut _121: u64; + let mut _122: u64; let mut _123: u64; - let _124: (); + let mut _124: u64; let mut _125: u64; - let mut _126: S; - let mut _127: u64; - let _128: &u64; - let _129: (); + let _126: &u64; + let _127: (); + let mut _128: u64; + let mut _129: u64; let mut _130: u64; - let mut _131: u64; + let _131: (); let mut _132: u64; - let _133: (); + let mut _133: u64; let mut _134: u64; - let mut _135: u64; - let mut _136: u64; - let _138: (); + let _136: (); + let mut _137: u64; + let mut _138: u64; let mut _139: u64; - let mut _140: u64; + let _140: (); let mut _141: u64; - let _142: (); + let mut _142: u64; let mut _143: u64; - let mut _144: u64; - let mut _145: u64; + let _144: (); let _146: (); - let _148: (); + let mut _147: u64; + let mut _148: u64; let mut _149: u64; - let mut _150: u64; + let _150: (); let mut _151: u64; - let _152: (); + let mut _152: u64; let mut _153: u64; - let mut _154: u64; - let mut _155: u64; - let _157: (); + let _155: (); + let mut _156: u64; + let mut _157: u64; let mut _158: u64; - let mut _159: u64; + let _159: (); let mut _160: u64; - let _161: (); + let mut _161: u64; let mut _162: u64; - let mut _163: u64; - let mut _164: u64; - let _166: (); + let _164: (); + let mut _165: u64; + let mut _166: u64; let mut _167: u64; - let mut _168: u64; + let _168: (); let mut _169: u64; - let _170: (); + let mut _170: u64; let mut _171: u64; - let mut _172: u64; - let mut _173: u64; scope 1 { - debug a => _128; - let _137: &mut u64; + debug a => _126; + let _135: &mut u64; scope 2 { - debug b => _137; - let _165: &u64; + debug b => _135; + let _163: &u64; scope 3 { - let _147: *const u64; + let _145: *const u64; scope 4 { - debug c => _147; - let _156: *mut u64; + debug c => _145; + let _154: *mut u64; scope 5 { - debug d => _156; + debug d => _154; } } } scope 6 { - debug e => _165; + debug e => _163; } } } @@ -400,30 +398,36 @@ + nop; StorageDead(_42); StorageLive(_46); - StorageLive(_47); +- StorageLive(_47); ++ nop; StorageLive(_48); _48 = _1; - _47 = move _48 as u32 (IntToInt); + _47 = _1 as u32 (IntToInt); StorageDead(_48); - _46 = opaque::(move _47) -> [return: bb13, unwind unreachable]; +- _46 = opaque::(move _47) -> [return: bb13, unwind unreachable]; ++ _46 = opaque::(_47) -> [return: bb13, unwind unreachable]; } bb13: { - StorageDead(_47); +- StorageDead(_47); ++ nop; StorageDead(_46); StorageLive(_49); - StorageLive(_50); +- StorageLive(_50); ++ nop; StorageLive(_51); _51 = _1; - _50 = move _51 as f32 (IntToFloat); + _50 = _1 as f32 (IntToFloat); StorageDead(_51); - _49 = opaque::(move _50) -> [return: bb14, unwind unreachable]; +- _49 = opaque::(move _50) -> [return: bb14, unwind unreachable]; ++ _49 = opaque::(_50) -> [return: bb14, unwind unreachable]; } bb14: { - StorageDead(_50); +- StorageDead(_50); ++ nop; StorageDead(_49); StorageLive(_52); - StorageLive(_53); @@ -462,479 +466,497 @@ StorageLive(_59); StorageLive(_60); StorageLive(_61); + _61 = _1; StorageLive(_62); - _62 = _1; - StorageLive(_63); - _63 = _2; -- _61 = Add(move _62, move _63); -+ _61 = _5; - StorageDead(_63); + _62 = _2; +- _60 = Add(move _61, move _62); ++ _60 = _5; StorageDead(_62); - StorageLive(_64); - _64 = _3; -- _60 = Add(move _61, move _64); -+ _60 = Add(_5, move _64); - StorageDead(_64); StorageDead(_61); - _59 = opaque::(move _60) -> [return: bb17, unwind unreachable]; +- _59 = opaque::(move _60) -> [return: bb17, unwind unreachable]; ++ _59 = opaque::(_5) -> [return: bb17, unwind unreachable]; } bb17: { StorageDead(_60); StorageDead(_59); + StorageLive(_63); + StorageLive(_64); StorageLive(_65); + _65 = _1; StorageLive(_66); + _66 = _2; +- _64 = Mul(move _65, move _66); ++ _64 = _9; + StorageDead(_66); + StorageDead(_65); +- _63 = opaque::(move _64) -> [return: bb18, unwind unreachable]; ++ _63 = opaque::(_9) -> [return: bb18, unwind unreachable]; + } + + bb18: { + StorageDead(_64); + StorageDead(_63); StorageLive(_67); StorageLive(_68); - _68 = _1; StorageLive(_69); - _69 = _2; -- _67 = Mul(move _68, move _69); -+ _67 = _9; - StorageDead(_69); - StorageDead(_68); + _69 = _1; StorageLive(_70); - _70 = _3; -- _66 = Add(move _67, move _70); -+ _66 = Add(_9, move _70); + _70 = _2; +- _68 = Sub(move _69, move _70); ++ _68 = _13; StorageDead(_70); - StorageDead(_67); - _65 = opaque::(move _66) -> [return: bb18, unwind unreachable]; + StorageDead(_69); +- _67 = opaque::(move _68) -> [return: bb19, unwind unreachable]; ++ _67 = opaque::(_13) -> [return: bb19, unwind unreachable]; } - bb18: { - StorageDead(_66); - StorageDead(_65); + bb19: { + StorageDead(_68); + StorageDead(_67); StorageLive(_71); StorageLive(_72); StorageLive(_73); + _73 = _1; StorageLive(_74); - _74 = _1; - StorageLive(_75); - _75 = _2; -- _73 = Sub(move _74, move _75); -+ _73 = _13; - StorageDead(_75); + _74 = _2; +- _75 = Eq(_74, const 0_u64); +- assert(!move _75, "attempt to divide `{}` by zero", _73) -> [success: bb20, unwind unreachable]; ++ _75 = _20; ++ assert(!_20, "attempt to divide `{}` by zero", _1) -> [success: bb20, unwind unreachable]; + } + + bb20: { +- _72 = Div(move _73, move _74); ++ _72 = _17; StorageDead(_74); - StorageLive(_76); - _76 = _3; -- _72 = Add(move _73, move _76); -+ _72 = Add(_13, move _76); - StorageDead(_76); StorageDead(_73); - _71 = opaque::(move _72) -> [return: bb19, unwind unreachable]; +- _71 = opaque::(move _72) -> [return: bb21, unwind unreachable]; ++ _71 = opaque::(_17) -> [return: bb21, unwind unreachable]; } - bb19: { + bb21: { StorageDead(_72); StorageDead(_71); + StorageLive(_76); StorageLive(_77); StorageLive(_78); + _78 = _1; StorageLive(_79); - StorageLive(_80); - _80 = _1; - StorageLive(_81); - _81 = _2; -- _82 = Eq(_81, const 0_u64); -- assert(!move _82, "attempt to divide `{}` by zero", _80) -> [success: bb20, unwind unreachable]; -+ _82 = _20; -+ assert(!_20, "attempt to divide `{}` by zero", _1) -> [success: bb20, unwind unreachable]; + _79 = _2; +- _80 = Eq(_79, const 0_u64); +- assert(!move _80, "attempt to calculate the remainder of `{}` with a divisor of zero", _78) -> [success: bb22, unwind unreachable]; ++ _80 = _20; ++ assert(!_20, "attempt to calculate the remainder of `{}` with a divisor of zero", _1) -> [success: bb22, unwind unreachable]; } - bb20: { -- _79 = Div(move _80, move _81); -+ _79 = _17; - StorageDead(_81); - StorageDead(_80); - StorageLive(_83); - _83 = _3; -- _78 = Add(move _79, move _83); -+ _78 = Add(_17, move _83); - StorageDead(_83); + bb22: { +- _77 = Rem(move _78, move _79); ++ _77 = _22; StorageDead(_79); - _77 = opaque::(move _78) -> [return: bb21, unwind unreachable]; + StorageDead(_78); +- _76 = opaque::(move _77) -> [return: bb23, unwind unreachable]; ++ _76 = opaque::(_22) -> [return: bb23, unwind unreachable]; } - bb21: { - StorageDead(_78); + bb23: { StorageDead(_77); + StorageDead(_76); + StorageLive(_81); + StorageLive(_82); + StorageLive(_83); + _83 = _1; StorageLive(_84); + _84 = _2; +- _82 = BitAnd(move _83, move _84); ++ _82 = _27; + StorageDead(_84); + StorageDead(_83); +- _81 = opaque::(move _82) -> [return: bb24, unwind unreachable]; ++ _81 = opaque::(_27) -> [return: bb24, unwind unreachable]; + } + + bb24: { + StorageDead(_82); + StorageDead(_81); StorageLive(_85); StorageLive(_86); StorageLive(_87); _87 = _1; StorageLive(_88); _88 = _2; -- _89 = Eq(_88, const 0_u64); -- assert(!move _89, "attempt to calculate the remainder of `{}` with a divisor of zero", _87) -> [success: bb22, unwind unreachable]; -+ _89 = _20; -+ assert(!_20, "attempt to calculate the remainder of `{}` with a divisor of zero", _1) -> [success: bb22, unwind unreachable]; - } - - bb22: { -- _86 = Rem(move _87, move _88); -+ _86 = _22; +- _86 = BitOr(move _87, move _88); ++ _86 = _31; StorageDead(_88); StorageDead(_87); - StorageLive(_90); - _90 = _3; -- _85 = Add(move _86, move _90); -+ _85 = Add(_22, move _90); - StorageDead(_90); - StorageDead(_86); - _84 = opaque::(move _85) -> [return: bb23, unwind unreachable]; +- _85 = opaque::(move _86) -> [return: bb25, unwind unreachable]; ++ _85 = opaque::(_31) -> [return: bb25, unwind unreachable]; } - bb23: { + bb25: { + StorageDead(_86); StorageDead(_85); - StorageDead(_84); + StorageLive(_89); + StorageLive(_90); StorageLive(_91); + _91 = _1; StorageLive(_92); + _92 = _2; +- _90 = BitXor(move _91, move _92); ++ _90 = _35; + StorageDead(_92); + StorageDead(_91); +- _89 = opaque::(move _90) -> [return: bb26, unwind unreachable]; ++ _89 = opaque::(_35) -> [return: bb26, unwind unreachable]; + } + + bb26: { + StorageDead(_90); + StorageDead(_89); StorageLive(_93); StorageLive(_94); - _94 = _1; StorageLive(_95); - _95 = _2; -- _93 = BitAnd(move _94, move _95); -+ _93 = _27; - StorageDead(_95); - StorageDead(_94); + _95 = _1; StorageLive(_96); - _96 = _3; -- _92 = Add(move _93, move _96); -+ _92 = Add(_27, move _96); + _96 = _2; +- _94 = Shl(move _95, move _96); ++ _94 = _39; StorageDead(_96); - StorageDead(_93); - _91 = opaque::(move _92) -> [return: bb24, unwind unreachable]; + StorageDead(_95); +- _93 = opaque::(move _94) -> [return: bb27, unwind unreachable]; ++ _93 = opaque::(_39) -> [return: bb27, unwind unreachable]; } - bb24: { - StorageDead(_92); - StorageDead(_91); + bb27: { + StorageDead(_94); + StorageDead(_93); StorageLive(_97); StorageLive(_98); StorageLive(_99); + _99 = _1; StorageLive(_100); - _100 = _1; - StorageLive(_101); - _101 = _2; -- _99 = BitOr(move _100, move _101); -+ _99 = _31; - StorageDead(_101); + _100 = _2; +- _98 = Shr(move _99, move _100); ++ _98 = _43; StorageDead(_100); - StorageLive(_102); - _102 = _3; -- _98 = Add(move _99, move _102); -+ _98 = Add(_31, move _102); - StorageDead(_102); StorageDead(_99); - _97 = opaque::(move _98) -> [return: bb25, unwind unreachable]; +- _97 = opaque::(move _98) -> [return: bb28, unwind unreachable]; ++ _97 = opaque::(_43) -> [return: bb28, unwind unreachable]; } - bb25: { + bb28: { StorageDead(_98); StorageDead(_97); + StorageLive(_101); + StorageLive(_102); StorageLive(_103); + _103 = _1; +- _102 = move _103 as u32 (IntToInt); ++ _102 = _47; + StorageDead(_103); +- _101 = opaque::(move _102) -> [return: bb29, unwind unreachable]; ++ _101 = opaque::(_47) -> [return: bb29, unwind unreachable]; + } + + bb29: { + StorageDead(_102); + StorageDead(_101); StorageLive(_104); StorageLive(_105); StorageLive(_106); _106 = _1; - StorageLive(_107); - _107 = _2; -- _105 = BitXor(move _106, move _107); -+ _105 = _35; - StorageDead(_107); +- _105 = move _106 as f32 (IntToFloat); ++ _105 = _50; StorageDead(_106); - StorageLive(_108); - _108 = _3; -- _104 = Add(move _105, move _108); -+ _104 = Add(_35, move _108); - StorageDead(_108); - StorageDead(_105); - _103 = opaque::(move _104) -> [return: bb26, unwind unreachable]; +- _104 = opaque::(move _105) -> [return: bb30, unwind unreachable]; ++ _104 = opaque::(_50) -> [return: bb30, unwind unreachable]; } - bb26: { + bb30: { + StorageDead(_105); StorageDead(_104); - StorageDead(_103); + StorageLive(_107); + StorageLive(_108); StorageLive(_109); + _109 = _1; +- _108 = S::(move _109); ++ _108 = _53; + StorageDead(_109); +- _107 = opaque::>(move _108) -> [return: bb31, unwind unreachable]; ++ _107 = opaque::>(_53) -> [return: bb31, unwind unreachable]; + } + + bb31: { + StorageDead(_108); + StorageDead(_107); StorageLive(_110); StorageLive(_111); StorageLive(_112); - _112 = _1; StorageLive(_113); - _113 = _2; -- _111 = Shl(move _112, move _113); -+ _111 = _39; + _113 = _1; +- _112 = S::(move _113); ++ _112 = _53; StorageDead(_113); - StorageDead(_112); - StorageLive(_114); - _114 = _3; -- _110 = Add(move _111, move _114); -+ _110 = Add(_39, move _114); - StorageDead(_114); - StorageDead(_111); - _109 = opaque::(move _110) -> [return: bb27, unwind unreachable]; +- _111 = (_112.0: u64); +- _110 = opaque::(move _111) -> [return: bb32, unwind unreachable]; ++ _111 = _1; ++ _110 = opaque::(_1) -> [return: bb32, unwind unreachable]; } - bb27: { + bb32: { + StorageDead(_111); + StorageDead(_112); StorageDead(_110); - StorageDead(_109); - StorageLive(_115); + StorageLive(_114); +- StorageLive(_115); ++ nop; StorageLive(_116); StorageLive(_117); + _117 = _1; StorageLive(_118); - _118 = _1; + _118 = _2; +- _116 = Mul(move _117, move _118); ++ _116 = _9; + StorageDead(_118); + StorageDead(_117); StorageLive(_119); _119 = _2; -- _117 = Shr(move _118, move _119); -+ _117 = _43; +- _115 = Sub(move _116, move _119); ++ _115 = Sub(_9, _2); StorageDead(_119); - StorageDead(_118); - StorageLive(_120); - _120 = _3; -- _116 = Add(move _117, move _120); -+ _116 = Add(_43, move _120); - StorageDead(_120); - StorageDead(_117); - _115 = opaque::(move _116) -> [return: bb28, unwind unreachable]; + StorageDead(_116); +- _114 = opaque::(move _115) -> [return: bb33, unwind unreachable]; ++ _114 = opaque::(_115) -> [return: bb33, unwind unreachable]; } - bb28: { - StorageDead(_116); - StorageDead(_115); + bb33: { +- StorageDead(_115); ++ nop; + StorageDead(_114); + StorageLive(_120); StorageLive(_121); StorageLive(_122); StorageLive(_123); _123 = _1; -- _122 = S::(move _123); -+ _122 = _53; + StorageLive(_124); + _124 = _2; +- _122 = Mul(move _123, move _124); ++ _122 = _9; + StorageDead(_124); StorageDead(_123); -- _121 = opaque::>(move _122) -> [return: bb29, unwind unreachable]; -+ _121 = opaque::>(_53) -> [return: bb29, unwind unreachable]; + StorageLive(_125); + _125 = _2; +- _121 = Sub(move _122, move _125); ++ _121 = _115; + StorageDead(_125); + StorageDead(_122); +- _120 = opaque::(move _121) -> [return: bb34, unwind unreachable]; ++ _120 = opaque::(_115) -> [return: bb34, unwind unreachable]; } - bb29: { - StorageDead(_122); + bb34: { StorageDead(_121); - StorageLive(_124); - StorageLive(_125); - StorageLive(_126); + StorageDead(_120); +- StorageLive(_126); ++ nop; + _126 = &_3; StorageLive(_127); - _127 = _1; -- _126 = S::(move _127); -+ _126 = _53; - StorageDead(_127); -- _125 = (_126.0: u64); -- _124 = opaque::(move _125) -> [return: bb30, unwind unreachable]; -+ _125 = _1; -+ _124 = opaque::(_1) -> [return: bb30, unwind unreachable]; - } - - bb30: { - StorageDead(_125); - StorageDead(_126); - StorageDead(_124); - StorageLive(_128); +- StorageLive(_129); + nop; - _128 = &_3; - StorageLive(_129); -- StorageLive(_130); -- StorageLive(_131); + nop; + _129 = (*_126); + StorageLive(_130); + _130 = _1; +- _128 = Add(move _129, move _130); ++ _128 = Add(_129, _1); + StorageDead(_130); +- StorageDead(_129); +- _127 = opaque::(move _128) -> [return: bb35, unwind unreachable]; + nop; - _131 = (*_128); - StorageLive(_132); - _132 = _1; -- _130 = Add(move _131, move _132); -+ _130 = Add(_131, _1); - StorageDead(_132); -- StorageDead(_131); -- _129 = opaque::(move _130) -> [return: bb31, unwind unreachable]; -+ nop; -+ _129 = opaque::(_130) -> [return: bb31, unwind unreachable]; ++ _127 = opaque::(_128) -> [return: bb35, unwind unreachable]; } - bb31: { -- StorageDead(_130); + bb35: { +- StorageDead(_128); + nop; - StorageDead(_129); + StorageDead(_127); + StorageLive(_131); + StorageLive(_132); StorageLive(_133); +- _133 = (*_126); ++ _133 = _129; StorageLive(_134); - StorageLive(_135); -- _135 = (*_128); -+ _135 = _131; - StorageLive(_136); - _136 = _1; -- _134 = Add(move _135, move _136); -+ _134 = _130; - StorageDead(_136); - StorageDead(_135); -- _133 = opaque::(move _134) -> [return: bb32, unwind unreachable]; -+ _133 = opaque::(_130) -> [return: bb32, unwind unreachable]; - } - - bb32: { + _134 = _1; +- _132 = Add(move _133, move _134); ++ _132 = _128; StorageDead(_134); StorageDead(_133); -- StorageLive(_137); +- _131 = opaque::(move _132) -> [return: bb36, unwind unreachable]; ++ _131 = opaque::(_128) -> [return: bb36, unwind unreachable]; + } + + bb36: { + StorageDead(_132); + StorageDead(_131); +- StorageLive(_135); + nop; - _137 = &mut _3; + _135 = &mut _3; + StorageLive(_136); + StorageLive(_137); StorageLive(_138); + _138 = (*_135); StorageLive(_139); - StorageLive(_140); - _140 = (*_137); - StorageLive(_141); - _141 = _1; -- _139 = Add(move _140, move _141); -+ _139 = Add(move _140, _1); - StorageDead(_141); - StorageDead(_140); - _138 = opaque::(move _139) -> [return: bb33, unwind unreachable]; - } - - bb33: { + _139 = _1; +- _137 = Add(move _138, move _139); ++ _137 = Add(move _138, _1); StorageDead(_139); StorageDead(_138); - StorageLive(_142); - StorageLive(_143); - StorageLive(_144); - _144 = (*_137); - StorageLive(_145); - _145 = _1; -- _143 = Add(move _144, move _145); -+ _143 = Add(move _144, _1); - StorageDead(_145); - StorageDead(_144); - _142 = opaque::(move _143) -> [return: bb34, unwind unreachable]; + _136 = opaque::(move _137) -> [return: bb37, unwind unreachable]; } - bb34: { + bb37: { + StorageDead(_137); + StorageDead(_136); + StorageLive(_140); + StorageLive(_141); + StorageLive(_142); + _142 = (*_135); + StorageLive(_143); + _143 = _1; +- _141 = Add(move _142, move _143); ++ _141 = Add(move _142, _1); StorageDead(_143); StorageDead(_142); - StorageLive(_146); -- StorageLive(_147); + _140 = opaque::(move _141) -> [return: bb38, unwind unreachable]; + } + + bb38: { + StorageDead(_141); + StorageDead(_140); + StorageLive(_144); +- StorageLive(_145); + nop; - _147 = &raw const _3; + _145 = &raw const _3; + StorageLive(_146); + StorageLive(_147); StorageLive(_148); + _148 = (*_145); StorageLive(_149); - StorageLive(_150); - _150 = (*_147); - StorageLive(_151); - _151 = _1; -- _149 = Add(move _150, move _151); -+ _149 = Add(move _150, _1); - StorageDead(_151); - StorageDead(_150); - _148 = opaque::(move _149) -> [return: bb35, unwind unreachable]; - } - - bb35: { + _149 = _1; +- _147 = Add(move _148, move _149); ++ _147 = Add(move _148, _1); StorageDead(_149); StorageDead(_148); - StorageLive(_152); - StorageLive(_153); - StorageLive(_154); - _154 = (*_147); - StorageLive(_155); - _155 = _1; -- _153 = Add(move _154, move _155); -+ _153 = Add(move _154, _1); - StorageDead(_155); - StorageDead(_154); - _152 = opaque::(move _153) -> [return: bb36, unwind unreachable]; + _146 = opaque::(move _147) -> [return: bb39, unwind unreachable]; } - bb36: { + bb39: { + StorageDead(_147); + StorageDead(_146); + StorageLive(_150); + StorageLive(_151); + StorageLive(_152); + _152 = (*_145); + StorageLive(_153); + _153 = _1; +- _151 = Add(move _152, move _153); ++ _151 = Add(move _152, _1); StorageDead(_153); StorageDead(_152); -- StorageLive(_156); + _150 = opaque::(move _151) -> [return: bb40, unwind unreachable]; + } + + bb40: { + StorageDead(_151); + StorageDead(_150); +- StorageLive(_154); + nop; - _156 = &raw mut _3; + _154 = &raw mut _3; + StorageLive(_155); + StorageLive(_156); StorageLive(_157); + _157 = (*_154); StorageLive(_158); - StorageLive(_159); - _159 = (*_156); - StorageLive(_160); - _160 = _1; -- _158 = Add(move _159, move _160); -+ _158 = Add(move _159, _1); - StorageDead(_160); - StorageDead(_159); - _157 = opaque::(move _158) -> [return: bb37, unwind unreachable]; - } - - bb37: { + _158 = _1; +- _156 = Add(move _157, move _158); ++ _156 = Add(move _157, _1); StorageDead(_158); StorageDead(_157); - StorageLive(_161); - StorageLive(_162); - StorageLive(_163); - _163 = (*_156); - StorageLive(_164); - _164 = _1; -- _162 = Add(move _163, move _164); -+ _162 = Add(move _163, _1); - StorageDead(_164); - StorageDead(_163); - _161 = opaque::(move _162) -> [return: bb38, unwind unreachable]; + _155 = opaque::(move _156) -> [return: bb41, unwind unreachable]; } - bb38: { + bb41: { + StorageDead(_156); + StorageDead(_155); + StorageLive(_159); + StorageLive(_160); + StorageLive(_161); + _161 = (*_154); + StorageLive(_162); + _162 = _1; +- _160 = Add(move _161, move _162); ++ _160 = Add(move _161, _1); StorageDead(_162); StorageDead(_161); - _146 = const (); -- StorageDead(_156); -- StorageDead(_147); + _159 = opaque::(move _160) -> [return: bb42, unwind unreachable]; + } + + bb42: { + StorageDead(_160); + StorageDead(_159); + _144 = const (); +- StorageDead(_154); +- StorageDead(_145); + nop; + nop; - StorageDead(_146); -- StorageLive(_165); + StorageDead(_144); +- StorageLive(_163); + nop; - _165 = &_3; - StorageLive(_166); -- StorageLive(_167); -- StorageLive(_168); + _163 = &_3; + StorageLive(_164); +- StorageLive(_165); +- StorageLive(_166); + nop; + nop; - _168 = (*_165); - StorageLive(_169); - _169 = _1; -- _167 = Add(move _168, move _169); -+ _167 = Add(_168, _1); - StorageDead(_169); -- StorageDead(_168); -- _166 = opaque::(move _167) -> [return: bb39, unwind unreachable]; + _166 = (*_163); + StorageLive(_167); + _167 = _1; +- _165 = Add(move _166, move _167); ++ _165 = Add(_166, _1); + StorageDead(_167); +- StorageDead(_166); +- _164 = opaque::(move _165) -> [return: bb43, unwind unreachable]; + nop; -+ _166 = opaque::(_167) -> [return: bb39, unwind unreachable]; ++ _164 = opaque::(_165) -> [return: bb43, unwind unreachable]; } - bb39: { -- StorageDead(_167); + bb43: { +- StorageDead(_165); + nop; - StorageDead(_166); + StorageDead(_164); + StorageLive(_168); + StorageLive(_169); StorageLive(_170); +- _170 = (*_163); ++ _170 = _166; StorageLive(_171); - StorageLive(_172); -- _172 = (*_165); -+ _172 = _168; - StorageLive(_173); - _173 = _1; -- _171 = Add(move _172, move _173); -+ _171 = _167; - StorageDead(_173); - StorageDead(_172); -- _170 = opaque::(move _171) -> [return: bb40, unwind unreachable]; -+ _170 = opaque::(_167) -> [return: bb40, unwind unreachable]; - } - - bb40: { + _171 = _1; +- _169 = Add(move _170, move _171); ++ _169 = _165; StorageDead(_171); StorageDead(_170); +- _168 = opaque::(move _169) -> [return: bb44, unwind unreachable]; ++ _168 = opaque::(_165) -> [return: bb44, unwind unreachable]; + } + + bb44: { + StorageDead(_169); + StorageDead(_168); _0 = const (); -- StorageDead(_165); -- StorageDead(_137); -- StorageDead(_128); +- StorageDead(_163); +- StorageDead(_135); +- StorageDead(_126); + nop; + nop; + nop; diff --git a/tests/mir-opt/gvn.subexpression_elimination.GVN.panic-unwind.diff b/tests/mir-opt/gvn.subexpression_elimination.GVN.panic-unwind.diff index 062dc6ff56190..119a4d9bbe908 100644 --- a/tests/mir-opt/gvn.subexpression_elimination.GVN.panic-unwind.diff +++ b/tests/mir-opt/gvn.subexpression_elimination.GVN.panic-unwind.diff @@ -65,11 +65,11 @@ let mut _60: u64; let mut _61: u64; let mut _62: u64; - let mut _63: u64; + let _63: (); let mut _64: u64; - let _65: (); + let mut _65: u64; let mut _66: u64; - let mut _67: u64; + let _67: (); let mut _68: u64; let mut _69: u64; let mut _70: u64; @@ -77,25 +77,25 @@ let mut _72: u64; let mut _73: u64; let mut _74: u64; - let mut _75: u64; - let mut _76: u64; - let _77: (); + let mut _75: bool; + let _76: (); + let mut _77: u64; let mut _78: u64; let mut _79: u64; - let mut _80: u64; - let mut _81: u64; - let mut _82: bool; + let mut _80: bool; + let _81: (); + let mut _82: u64; let mut _83: u64; - let _84: (); - let mut _85: u64; + let mut _84: u64; + let _85: (); let mut _86: u64; let mut _87: u64; let mut _88: u64; - let mut _89: bool; + let _89: (); let mut _90: u64; - let _91: (); + let mut _91: u64; let mut _92: u64; - let mut _93: u64; + let _93: (); let mut _94: u64; let mut _95: u64; let mut _96: u64; @@ -103,93 +103,91 @@ let mut _98: u64; let mut _99: u64; let mut _100: u64; - let mut _101: u64; - let mut _102: u64; - let _103: (); - let mut _104: u64; - let mut _105: u64; + let _101: (); + let mut _102: u32; + let mut _103: u64; + let _104: (); + let mut _105: f32; let mut _106: u64; - let mut _107: u64; - let mut _108: u64; - let _109: (); - let mut _110: u64; + let _107: (); + let mut _108: S; + let mut _109: u64; + let _110: (); let mut _111: u64; - let mut _112: u64; + let mut _112: S; let mut _113: u64; - let mut _114: u64; - let _115: (); + let _114: (); + let mut _115: u64; let mut _116: u64; let mut _117: u64; let mut _118: u64; let mut _119: u64; - let mut _120: u64; - let _121: (); - let mut _122: S; + let _120: (); + let mut _121: u64; + let mut _122: u64; let mut _123: u64; - let _124: (); + let mut _124: u64; let mut _125: u64; - let mut _126: S; - let mut _127: u64; - let _128: &u64; - let _129: (); + let _126: &u64; + let _127: (); + let mut _128: u64; + let mut _129: u64; let mut _130: u64; - let mut _131: u64; + let _131: (); let mut _132: u64; - let _133: (); + let mut _133: u64; let mut _134: u64; - let mut _135: u64; - let mut _136: u64; - let _138: (); + let _136: (); + let mut _137: u64; + let mut _138: u64; let mut _139: u64; - let mut _140: u64; + let _140: (); let mut _141: u64; - let _142: (); + let mut _142: u64; let mut _143: u64; - let mut _144: u64; - let mut _145: u64; + let _144: (); let _146: (); - let _148: (); + let mut _147: u64; + let mut _148: u64; let mut _149: u64; - let mut _150: u64; + let _150: (); let mut _151: u64; - let _152: (); + let mut _152: u64; let mut _153: u64; - let mut _154: u64; - let mut _155: u64; - let _157: (); + let _155: (); + let mut _156: u64; + let mut _157: u64; let mut _158: u64; - let mut _159: u64; + let _159: (); let mut _160: u64; - let _161: (); + let mut _161: u64; let mut _162: u64; - let mut _163: u64; - let mut _164: u64; - let _166: (); + let _164: (); + let mut _165: u64; + let mut _166: u64; let mut _167: u64; - let mut _168: u64; + let _168: (); let mut _169: u64; - let _170: (); + let mut _170: u64; let mut _171: u64; - let mut _172: u64; - let mut _173: u64; scope 1 { - debug a => _128; - let _137: &mut u64; + debug a => _126; + let _135: &mut u64; scope 2 { - debug b => _137; - let _165: &u64; + debug b => _135; + let _163: &u64; scope 3 { - let _147: *const u64; + let _145: *const u64; scope 4 { - debug c => _147; - let _156: *mut u64; + debug c => _145; + let _154: *mut u64; scope 5 { - debug d => _156; + debug d => _154; } } } scope 6 { - debug e => _165; + debug e => _163; } } } @@ -400,30 +398,36 @@ + nop; StorageDead(_42); StorageLive(_46); - StorageLive(_47); +- StorageLive(_47); ++ nop; StorageLive(_48); _48 = _1; - _47 = move _48 as u32 (IntToInt); + _47 = _1 as u32 (IntToInt); StorageDead(_48); - _46 = opaque::(move _47) -> [return: bb13, unwind continue]; +- _46 = opaque::(move _47) -> [return: bb13, unwind continue]; ++ _46 = opaque::(_47) -> [return: bb13, unwind continue]; } bb13: { - StorageDead(_47); +- StorageDead(_47); ++ nop; StorageDead(_46); StorageLive(_49); - StorageLive(_50); +- StorageLive(_50); ++ nop; StorageLive(_51); _51 = _1; - _50 = move _51 as f32 (IntToFloat); + _50 = _1 as f32 (IntToFloat); StorageDead(_51); - _49 = opaque::(move _50) -> [return: bb14, unwind continue]; +- _49 = opaque::(move _50) -> [return: bb14, unwind continue]; ++ _49 = opaque::(_50) -> [return: bb14, unwind continue]; } bb14: { - StorageDead(_50); +- StorageDead(_50); ++ nop; StorageDead(_49); StorageLive(_52); - StorageLive(_53); @@ -462,479 +466,497 @@ StorageLive(_59); StorageLive(_60); StorageLive(_61); + _61 = _1; StorageLive(_62); - _62 = _1; - StorageLive(_63); - _63 = _2; -- _61 = Add(move _62, move _63); -+ _61 = _5; - StorageDead(_63); + _62 = _2; +- _60 = Add(move _61, move _62); ++ _60 = _5; StorageDead(_62); - StorageLive(_64); - _64 = _3; -- _60 = Add(move _61, move _64); -+ _60 = Add(_5, move _64); - StorageDead(_64); StorageDead(_61); - _59 = opaque::(move _60) -> [return: bb17, unwind continue]; +- _59 = opaque::(move _60) -> [return: bb17, unwind continue]; ++ _59 = opaque::(_5) -> [return: bb17, unwind continue]; } bb17: { StorageDead(_60); StorageDead(_59); + StorageLive(_63); + StorageLive(_64); StorageLive(_65); + _65 = _1; StorageLive(_66); + _66 = _2; +- _64 = Mul(move _65, move _66); ++ _64 = _9; + StorageDead(_66); + StorageDead(_65); +- _63 = opaque::(move _64) -> [return: bb18, unwind continue]; ++ _63 = opaque::(_9) -> [return: bb18, unwind continue]; + } + + bb18: { + StorageDead(_64); + StorageDead(_63); StorageLive(_67); StorageLive(_68); - _68 = _1; StorageLive(_69); - _69 = _2; -- _67 = Mul(move _68, move _69); -+ _67 = _9; - StorageDead(_69); - StorageDead(_68); + _69 = _1; StorageLive(_70); - _70 = _3; -- _66 = Add(move _67, move _70); -+ _66 = Add(_9, move _70); + _70 = _2; +- _68 = Sub(move _69, move _70); ++ _68 = _13; StorageDead(_70); - StorageDead(_67); - _65 = opaque::(move _66) -> [return: bb18, unwind continue]; + StorageDead(_69); +- _67 = opaque::(move _68) -> [return: bb19, unwind continue]; ++ _67 = opaque::(_13) -> [return: bb19, unwind continue]; } - bb18: { - StorageDead(_66); - StorageDead(_65); + bb19: { + StorageDead(_68); + StorageDead(_67); StorageLive(_71); StorageLive(_72); StorageLive(_73); + _73 = _1; StorageLive(_74); - _74 = _1; - StorageLive(_75); - _75 = _2; -- _73 = Sub(move _74, move _75); -+ _73 = _13; - StorageDead(_75); + _74 = _2; +- _75 = Eq(_74, const 0_u64); +- assert(!move _75, "attempt to divide `{}` by zero", _73) -> [success: bb20, unwind continue]; ++ _75 = _20; ++ assert(!_20, "attempt to divide `{}` by zero", _1) -> [success: bb20, unwind continue]; + } + + bb20: { +- _72 = Div(move _73, move _74); ++ _72 = _17; StorageDead(_74); - StorageLive(_76); - _76 = _3; -- _72 = Add(move _73, move _76); -+ _72 = Add(_13, move _76); - StorageDead(_76); StorageDead(_73); - _71 = opaque::(move _72) -> [return: bb19, unwind continue]; +- _71 = opaque::(move _72) -> [return: bb21, unwind continue]; ++ _71 = opaque::(_17) -> [return: bb21, unwind continue]; } - bb19: { + bb21: { StorageDead(_72); StorageDead(_71); + StorageLive(_76); StorageLive(_77); StorageLive(_78); + _78 = _1; StorageLive(_79); - StorageLive(_80); - _80 = _1; - StorageLive(_81); - _81 = _2; -- _82 = Eq(_81, const 0_u64); -- assert(!move _82, "attempt to divide `{}` by zero", _80) -> [success: bb20, unwind continue]; -+ _82 = _20; -+ assert(!_20, "attempt to divide `{}` by zero", _1) -> [success: bb20, unwind continue]; + _79 = _2; +- _80 = Eq(_79, const 0_u64); +- assert(!move _80, "attempt to calculate the remainder of `{}` with a divisor of zero", _78) -> [success: bb22, unwind continue]; ++ _80 = _20; ++ assert(!_20, "attempt to calculate the remainder of `{}` with a divisor of zero", _1) -> [success: bb22, unwind continue]; } - bb20: { -- _79 = Div(move _80, move _81); -+ _79 = _17; - StorageDead(_81); - StorageDead(_80); - StorageLive(_83); - _83 = _3; -- _78 = Add(move _79, move _83); -+ _78 = Add(_17, move _83); - StorageDead(_83); + bb22: { +- _77 = Rem(move _78, move _79); ++ _77 = _22; StorageDead(_79); - _77 = opaque::(move _78) -> [return: bb21, unwind continue]; + StorageDead(_78); +- _76 = opaque::(move _77) -> [return: bb23, unwind continue]; ++ _76 = opaque::(_22) -> [return: bb23, unwind continue]; } - bb21: { - StorageDead(_78); + bb23: { StorageDead(_77); + StorageDead(_76); + StorageLive(_81); + StorageLive(_82); + StorageLive(_83); + _83 = _1; StorageLive(_84); + _84 = _2; +- _82 = BitAnd(move _83, move _84); ++ _82 = _27; + StorageDead(_84); + StorageDead(_83); +- _81 = opaque::(move _82) -> [return: bb24, unwind continue]; ++ _81 = opaque::(_27) -> [return: bb24, unwind continue]; + } + + bb24: { + StorageDead(_82); + StorageDead(_81); StorageLive(_85); StorageLive(_86); StorageLive(_87); _87 = _1; StorageLive(_88); _88 = _2; -- _89 = Eq(_88, const 0_u64); -- assert(!move _89, "attempt to calculate the remainder of `{}` with a divisor of zero", _87) -> [success: bb22, unwind continue]; -+ _89 = _20; -+ assert(!_20, "attempt to calculate the remainder of `{}` with a divisor of zero", _1) -> [success: bb22, unwind continue]; - } - - bb22: { -- _86 = Rem(move _87, move _88); -+ _86 = _22; +- _86 = BitOr(move _87, move _88); ++ _86 = _31; StorageDead(_88); StorageDead(_87); - StorageLive(_90); - _90 = _3; -- _85 = Add(move _86, move _90); -+ _85 = Add(_22, move _90); - StorageDead(_90); - StorageDead(_86); - _84 = opaque::(move _85) -> [return: bb23, unwind continue]; +- _85 = opaque::(move _86) -> [return: bb25, unwind continue]; ++ _85 = opaque::(_31) -> [return: bb25, unwind continue]; } - bb23: { + bb25: { + StorageDead(_86); StorageDead(_85); - StorageDead(_84); + StorageLive(_89); + StorageLive(_90); StorageLive(_91); + _91 = _1; StorageLive(_92); + _92 = _2; +- _90 = BitXor(move _91, move _92); ++ _90 = _35; + StorageDead(_92); + StorageDead(_91); +- _89 = opaque::(move _90) -> [return: bb26, unwind continue]; ++ _89 = opaque::(_35) -> [return: bb26, unwind continue]; + } + + bb26: { + StorageDead(_90); + StorageDead(_89); StorageLive(_93); StorageLive(_94); - _94 = _1; StorageLive(_95); - _95 = _2; -- _93 = BitAnd(move _94, move _95); -+ _93 = _27; - StorageDead(_95); - StorageDead(_94); + _95 = _1; StorageLive(_96); - _96 = _3; -- _92 = Add(move _93, move _96); -+ _92 = Add(_27, move _96); + _96 = _2; +- _94 = Shl(move _95, move _96); ++ _94 = _39; StorageDead(_96); - StorageDead(_93); - _91 = opaque::(move _92) -> [return: bb24, unwind continue]; + StorageDead(_95); +- _93 = opaque::(move _94) -> [return: bb27, unwind continue]; ++ _93 = opaque::(_39) -> [return: bb27, unwind continue]; } - bb24: { - StorageDead(_92); - StorageDead(_91); + bb27: { + StorageDead(_94); + StorageDead(_93); StorageLive(_97); StorageLive(_98); StorageLive(_99); + _99 = _1; StorageLive(_100); - _100 = _1; - StorageLive(_101); - _101 = _2; -- _99 = BitOr(move _100, move _101); -+ _99 = _31; - StorageDead(_101); + _100 = _2; +- _98 = Shr(move _99, move _100); ++ _98 = _43; StorageDead(_100); - StorageLive(_102); - _102 = _3; -- _98 = Add(move _99, move _102); -+ _98 = Add(_31, move _102); - StorageDead(_102); StorageDead(_99); - _97 = opaque::(move _98) -> [return: bb25, unwind continue]; +- _97 = opaque::(move _98) -> [return: bb28, unwind continue]; ++ _97 = opaque::(_43) -> [return: bb28, unwind continue]; } - bb25: { + bb28: { StorageDead(_98); StorageDead(_97); + StorageLive(_101); + StorageLive(_102); StorageLive(_103); + _103 = _1; +- _102 = move _103 as u32 (IntToInt); ++ _102 = _47; + StorageDead(_103); +- _101 = opaque::(move _102) -> [return: bb29, unwind continue]; ++ _101 = opaque::(_47) -> [return: bb29, unwind continue]; + } + + bb29: { + StorageDead(_102); + StorageDead(_101); StorageLive(_104); StorageLive(_105); StorageLive(_106); _106 = _1; - StorageLive(_107); - _107 = _2; -- _105 = BitXor(move _106, move _107); -+ _105 = _35; - StorageDead(_107); +- _105 = move _106 as f32 (IntToFloat); ++ _105 = _50; StorageDead(_106); - StorageLive(_108); - _108 = _3; -- _104 = Add(move _105, move _108); -+ _104 = Add(_35, move _108); - StorageDead(_108); - StorageDead(_105); - _103 = opaque::(move _104) -> [return: bb26, unwind continue]; +- _104 = opaque::(move _105) -> [return: bb30, unwind continue]; ++ _104 = opaque::(_50) -> [return: bb30, unwind continue]; } - bb26: { + bb30: { + StorageDead(_105); StorageDead(_104); - StorageDead(_103); + StorageLive(_107); + StorageLive(_108); StorageLive(_109); + _109 = _1; +- _108 = S::(move _109); ++ _108 = _53; + StorageDead(_109); +- _107 = opaque::>(move _108) -> [return: bb31, unwind continue]; ++ _107 = opaque::>(_53) -> [return: bb31, unwind continue]; + } + + bb31: { + StorageDead(_108); + StorageDead(_107); StorageLive(_110); StorageLive(_111); StorageLive(_112); - _112 = _1; StorageLive(_113); - _113 = _2; -- _111 = Shl(move _112, move _113); -+ _111 = _39; + _113 = _1; +- _112 = S::(move _113); ++ _112 = _53; StorageDead(_113); - StorageDead(_112); - StorageLive(_114); - _114 = _3; -- _110 = Add(move _111, move _114); -+ _110 = Add(_39, move _114); - StorageDead(_114); - StorageDead(_111); - _109 = opaque::(move _110) -> [return: bb27, unwind continue]; +- _111 = (_112.0: u64); +- _110 = opaque::(move _111) -> [return: bb32, unwind continue]; ++ _111 = _1; ++ _110 = opaque::(_1) -> [return: bb32, unwind continue]; } - bb27: { + bb32: { + StorageDead(_111); + StorageDead(_112); StorageDead(_110); - StorageDead(_109); - StorageLive(_115); + StorageLive(_114); +- StorageLive(_115); ++ nop; StorageLive(_116); StorageLive(_117); + _117 = _1; StorageLive(_118); - _118 = _1; + _118 = _2; +- _116 = Mul(move _117, move _118); ++ _116 = _9; + StorageDead(_118); + StorageDead(_117); StorageLive(_119); _119 = _2; -- _117 = Shr(move _118, move _119); -+ _117 = _43; +- _115 = Sub(move _116, move _119); ++ _115 = Sub(_9, _2); StorageDead(_119); - StorageDead(_118); - StorageLive(_120); - _120 = _3; -- _116 = Add(move _117, move _120); -+ _116 = Add(_43, move _120); - StorageDead(_120); - StorageDead(_117); - _115 = opaque::(move _116) -> [return: bb28, unwind continue]; + StorageDead(_116); +- _114 = opaque::(move _115) -> [return: bb33, unwind continue]; ++ _114 = opaque::(_115) -> [return: bb33, unwind continue]; } - bb28: { - StorageDead(_116); - StorageDead(_115); + bb33: { +- StorageDead(_115); ++ nop; + StorageDead(_114); + StorageLive(_120); StorageLive(_121); StorageLive(_122); StorageLive(_123); _123 = _1; -- _122 = S::(move _123); -+ _122 = _53; + StorageLive(_124); + _124 = _2; +- _122 = Mul(move _123, move _124); ++ _122 = _9; + StorageDead(_124); StorageDead(_123); -- _121 = opaque::>(move _122) -> [return: bb29, unwind continue]; -+ _121 = opaque::>(_53) -> [return: bb29, unwind continue]; + StorageLive(_125); + _125 = _2; +- _121 = Sub(move _122, move _125); ++ _121 = _115; + StorageDead(_125); + StorageDead(_122); +- _120 = opaque::(move _121) -> [return: bb34, unwind continue]; ++ _120 = opaque::(_115) -> [return: bb34, unwind continue]; } - bb29: { - StorageDead(_122); + bb34: { StorageDead(_121); - StorageLive(_124); - StorageLive(_125); - StorageLive(_126); + StorageDead(_120); +- StorageLive(_126); ++ nop; + _126 = &_3; StorageLive(_127); - _127 = _1; -- _126 = S::(move _127); -+ _126 = _53; - StorageDead(_127); -- _125 = (_126.0: u64); -- _124 = opaque::(move _125) -> [return: bb30, unwind continue]; -+ _125 = _1; -+ _124 = opaque::(_1) -> [return: bb30, unwind continue]; - } - - bb30: { - StorageDead(_125); - StorageDead(_126); - StorageDead(_124); - StorageLive(_128); +- StorageLive(_129); + nop; - _128 = &_3; - StorageLive(_129); -- StorageLive(_130); -- StorageLive(_131); + nop; + _129 = (*_126); + StorageLive(_130); + _130 = _1; +- _128 = Add(move _129, move _130); ++ _128 = Add(_129, _1); + StorageDead(_130); +- StorageDead(_129); +- _127 = opaque::(move _128) -> [return: bb35, unwind continue]; + nop; - _131 = (*_128); - StorageLive(_132); - _132 = _1; -- _130 = Add(move _131, move _132); -+ _130 = Add(_131, _1); - StorageDead(_132); -- StorageDead(_131); -- _129 = opaque::(move _130) -> [return: bb31, unwind continue]; -+ nop; -+ _129 = opaque::(_130) -> [return: bb31, unwind continue]; ++ _127 = opaque::(_128) -> [return: bb35, unwind continue]; } - bb31: { -- StorageDead(_130); + bb35: { +- StorageDead(_128); + nop; - StorageDead(_129); + StorageDead(_127); + StorageLive(_131); + StorageLive(_132); StorageLive(_133); +- _133 = (*_126); ++ _133 = _129; StorageLive(_134); - StorageLive(_135); -- _135 = (*_128); -+ _135 = _131; - StorageLive(_136); - _136 = _1; -- _134 = Add(move _135, move _136); -+ _134 = _130; - StorageDead(_136); - StorageDead(_135); -- _133 = opaque::(move _134) -> [return: bb32, unwind continue]; -+ _133 = opaque::(_130) -> [return: bb32, unwind continue]; - } - - bb32: { + _134 = _1; +- _132 = Add(move _133, move _134); ++ _132 = _128; StorageDead(_134); StorageDead(_133); -- StorageLive(_137); +- _131 = opaque::(move _132) -> [return: bb36, unwind continue]; ++ _131 = opaque::(_128) -> [return: bb36, unwind continue]; + } + + bb36: { + StorageDead(_132); + StorageDead(_131); +- StorageLive(_135); + nop; - _137 = &mut _3; + _135 = &mut _3; + StorageLive(_136); + StorageLive(_137); StorageLive(_138); + _138 = (*_135); StorageLive(_139); - StorageLive(_140); - _140 = (*_137); - StorageLive(_141); - _141 = _1; -- _139 = Add(move _140, move _141); -+ _139 = Add(move _140, _1); - StorageDead(_141); - StorageDead(_140); - _138 = opaque::(move _139) -> [return: bb33, unwind continue]; - } - - bb33: { + _139 = _1; +- _137 = Add(move _138, move _139); ++ _137 = Add(move _138, _1); StorageDead(_139); StorageDead(_138); - StorageLive(_142); - StorageLive(_143); - StorageLive(_144); - _144 = (*_137); - StorageLive(_145); - _145 = _1; -- _143 = Add(move _144, move _145); -+ _143 = Add(move _144, _1); - StorageDead(_145); - StorageDead(_144); - _142 = opaque::(move _143) -> [return: bb34, unwind continue]; + _136 = opaque::(move _137) -> [return: bb37, unwind continue]; } - bb34: { + bb37: { + StorageDead(_137); + StorageDead(_136); + StorageLive(_140); + StorageLive(_141); + StorageLive(_142); + _142 = (*_135); + StorageLive(_143); + _143 = _1; +- _141 = Add(move _142, move _143); ++ _141 = Add(move _142, _1); StorageDead(_143); StorageDead(_142); - StorageLive(_146); -- StorageLive(_147); + _140 = opaque::(move _141) -> [return: bb38, unwind continue]; + } + + bb38: { + StorageDead(_141); + StorageDead(_140); + StorageLive(_144); +- StorageLive(_145); + nop; - _147 = &raw const _3; + _145 = &raw const _3; + StorageLive(_146); + StorageLive(_147); StorageLive(_148); + _148 = (*_145); StorageLive(_149); - StorageLive(_150); - _150 = (*_147); - StorageLive(_151); - _151 = _1; -- _149 = Add(move _150, move _151); -+ _149 = Add(move _150, _1); - StorageDead(_151); - StorageDead(_150); - _148 = opaque::(move _149) -> [return: bb35, unwind continue]; - } - - bb35: { + _149 = _1; +- _147 = Add(move _148, move _149); ++ _147 = Add(move _148, _1); StorageDead(_149); StorageDead(_148); - StorageLive(_152); - StorageLive(_153); - StorageLive(_154); - _154 = (*_147); - StorageLive(_155); - _155 = _1; -- _153 = Add(move _154, move _155); -+ _153 = Add(move _154, _1); - StorageDead(_155); - StorageDead(_154); - _152 = opaque::(move _153) -> [return: bb36, unwind continue]; + _146 = opaque::(move _147) -> [return: bb39, unwind continue]; } - bb36: { + bb39: { + StorageDead(_147); + StorageDead(_146); + StorageLive(_150); + StorageLive(_151); + StorageLive(_152); + _152 = (*_145); + StorageLive(_153); + _153 = _1; +- _151 = Add(move _152, move _153); ++ _151 = Add(move _152, _1); StorageDead(_153); StorageDead(_152); -- StorageLive(_156); + _150 = opaque::(move _151) -> [return: bb40, unwind continue]; + } + + bb40: { + StorageDead(_151); + StorageDead(_150); +- StorageLive(_154); + nop; - _156 = &raw mut _3; + _154 = &raw mut _3; + StorageLive(_155); + StorageLive(_156); StorageLive(_157); + _157 = (*_154); StorageLive(_158); - StorageLive(_159); - _159 = (*_156); - StorageLive(_160); - _160 = _1; -- _158 = Add(move _159, move _160); -+ _158 = Add(move _159, _1); - StorageDead(_160); - StorageDead(_159); - _157 = opaque::(move _158) -> [return: bb37, unwind continue]; - } - - bb37: { + _158 = _1; +- _156 = Add(move _157, move _158); ++ _156 = Add(move _157, _1); StorageDead(_158); StorageDead(_157); - StorageLive(_161); - StorageLive(_162); - StorageLive(_163); - _163 = (*_156); - StorageLive(_164); - _164 = _1; -- _162 = Add(move _163, move _164); -+ _162 = Add(move _163, _1); - StorageDead(_164); - StorageDead(_163); - _161 = opaque::(move _162) -> [return: bb38, unwind continue]; + _155 = opaque::(move _156) -> [return: bb41, unwind continue]; } - bb38: { + bb41: { + StorageDead(_156); + StorageDead(_155); + StorageLive(_159); + StorageLive(_160); + StorageLive(_161); + _161 = (*_154); + StorageLive(_162); + _162 = _1; +- _160 = Add(move _161, move _162); ++ _160 = Add(move _161, _1); StorageDead(_162); StorageDead(_161); - _146 = const (); -- StorageDead(_156); -- StorageDead(_147); + _159 = opaque::(move _160) -> [return: bb42, unwind continue]; + } + + bb42: { + StorageDead(_160); + StorageDead(_159); + _144 = const (); +- StorageDead(_154); +- StorageDead(_145); + nop; + nop; - StorageDead(_146); -- StorageLive(_165); + StorageDead(_144); +- StorageLive(_163); + nop; - _165 = &_3; - StorageLive(_166); -- StorageLive(_167); -- StorageLive(_168); + _163 = &_3; + StorageLive(_164); +- StorageLive(_165); +- StorageLive(_166); + nop; + nop; - _168 = (*_165); - StorageLive(_169); - _169 = _1; -- _167 = Add(move _168, move _169); -+ _167 = Add(_168, _1); - StorageDead(_169); -- StorageDead(_168); -- _166 = opaque::(move _167) -> [return: bb39, unwind continue]; + _166 = (*_163); + StorageLive(_167); + _167 = _1; +- _165 = Add(move _166, move _167); ++ _165 = Add(_166, _1); + StorageDead(_167); +- StorageDead(_166); +- _164 = opaque::(move _165) -> [return: bb43, unwind continue]; + nop; -+ _166 = opaque::(_167) -> [return: bb39, unwind continue]; ++ _164 = opaque::(_165) -> [return: bb43, unwind continue]; } - bb39: { -- StorageDead(_167); + bb43: { +- StorageDead(_165); + nop; - StorageDead(_166); + StorageDead(_164); + StorageLive(_168); + StorageLive(_169); StorageLive(_170); +- _170 = (*_163); ++ _170 = _166; StorageLive(_171); - StorageLive(_172); -- _172 = (*_165); -+ _172 = _168; - StorageLive(_173); - _173 = _1; -- _171 = Add(move _172, move _173); -+ _171 = _167; - StorageDead(_173); - StorageDead(_172); -- _170 = opaque::(move _171) -> [return: bb40, unwind continue]; -+ _170 = opaque::(_167) -> [return: bb40, unwind continue]; - } - - bb40: { + _171 = _1; +- _169 = Add(move _170, move _171); ++ _169 = _165; StorageDead(_171); StorageDead(_170); +- _168 = opaque::(move _169) -> [return: bb44, unwind continue]; ++ _168 = opaque::(_165) -> [return: bb44, unwind continue]; + } + + bb44: { + StorageDead(_169); + StorageDead(_168); _0 = const (); -- StorageDead(_165); -- StorageDead(_137); -- StorageDead(_128); +- StorageDead(_163); +- StorageDead(_135); +- StorageDead(_126); + nop; + nop; + nop; From c4cc9ca0603cfdf5921a82d929d680db94d1d072 Mon Sep 17 00:00:00 2001 From: Camille GILLOT Date: Sat, 21 Oct 2023 09:59:03 +0000 Subject: [PATCH 23/28] Do not merge fn pointer casts. --- compiler/rustc_mir_transform/src/gvn.rs | 9 ++ .../miri/tests/pass/function_pointers.rs | 5 +- .../gvn.fn_pointers.GVN.panic-abort.diff | 118 ++++++++++++++++++ .../gvn.fn_pointers.GVN.panic-unwind.diff | 118 ++++++++++++++++++ tests/mir-opt/gvn.rs | 25 ++++ 5 files changed, 272 insertions(+), 3 deletions(-) create mode 100644 tests/mir-opt/gvn.fn_pointers.GVN.panic-abort.diff create mode 100644 tests/mir-opt/gvn.fn_pointers.GVN.panic-unwind.diff diff --git a/compiler/rustc_mir_transform/src/gvn.rs b/compiler/rustc_mir_transform/src/gvn.rs index de61571fdfcb5..cf8a0fdb97e2d 100644 --- a/compiler/rustc_mir_transform/src/gvn.rs +++ b/compiler/rustc_mir_transform/src/gvn.rs @@ -92,6 +92,7 @@ use rustc_index::IndexVec; use rustc_macros::newtype_index; use rustc_middle::mir::visit::*; use rustc_middle::mir::*; +use rustc_middle::ty::adjustment::PointerCoercion; use rustc_middle::ty::layout::LayoutOf; use rustc_middle::ty::{self, Ty, TyCtxt, TypeAndMut}; use rustc_span::def_id::DefId; @@ -761,6 +762,14 @@ impl<'body, 'tcx> VnState<'body, 'tcx> { Rvalue::Cast(kind, ref mut value, to) => { let from = value.ty(self.local_decls, self.tcx); let value = self.simplify_operand(value, location)?; + if let CastKind::PointerCoercion( + PointerCoercion::ReifyFnPointer | PointerCoercion::ClosureFnPointer(_), + ) = kind + { + // Each reification of a generic fn may get a different pointer. + // Do not try to merge them. + return self.new_opaque(); + } Value::Cast { kind, value, from, to } } Rvalue::BinaryOp(op, box (ref mut lhs, ref mut rhs)) => { diff --git a/src/tools/miri/tests/pass/function_pointers.rs b/src/tools/miri/tests/pass/function_pointers.rs index 1c99a96feda94..8e58692a0c74a 100644 --- a/src/tools/miri/tests/pass/function_pointers.rs +++ b/src/tools/miri/tests/pass/function_pointers.rs @@ -80,9 +80,8 @@ fn main() { // but Miri currently uses a fixed address for monomorphic functions. assert!(return_fn_ptr(i) == i); assert!(return_fn_ptr(i) as unsafe fn() -> i32 == i as fn() -> i32 as unsafe fn() -> i32); - // We don't check anything for `f`. Miri gives it many different addresses - // but mir-opts can turn them into the same address. - let _val = return_fn_ptr(f) != f; + // Miri gives it many different addresses to different reifications of a generic function. + assert!(return_fn_ptr(f) != f); // However, if we only turn `f` into a function pointer and use that pointer, // it is equal to itself. let f2 = f as fn() -> i32; diff --git a/tests/mir-opt/gvn.fn_pointers.GVN.panic-abort.diff b/tests/mir-opt/gvn.fn_pointers.GVN.panic-abort.diff new file mode 100644 index 0000000000000..d8248d22d38b9 --- /dev/null +++ b/tests/mir-opt/gvn.fn_pointers.GVN.panic-abort.diff @@ -0,0 +1,118 @@ +- // MIR for `fn_pointers` before GVN ++ // MIR for `fn_pointers` after GVN + + fn fn_pointers() -> () { + let mut _0: (); + let _1: fn(u8) -> u8; + let _2: (); + let mut _3: fn(u8) -> u8; + let _5: (); + let mut _6: fn(u8) -> u8; + let mut _9: {closure@$DIR/gvn.rs:591:19: 591:21}; + let _10: (); + let mut _11: fn(); + let mut _13: {closure@$DIR/gvn.rs:591:19: 591:21}; + let _14: (); + let mut _15: fn(); + scope 1 { + debug f => _1; + let _4: fn(u8) -> u8; + scope 2 { + debug g => _4; + let _7: {closure@$DIR/gvn.rs:591:19: 591:21}; + scope 3 { + debug closure => _7; + let _8: fn(); + scope 4 { + debug cf => _8; + let _12: fn(); + scope 5 { + debug cg => _12; + } + } + } + } + } + + bb0: { +- StorageLive(_1); ++ nop; + _1 = identity:: as fn(u8) -> u8 (PointerCoercion(ReifyFnPointer)); + StorageLive(_2); + StorageLive(_3); + _3 = _1; +- _2 = opaque:: u8>(move _3) -> [return: bb1, unwind unreachable]; ++ _2 = opaque:: u8>(_1) -> [return: bb1, unwind unreachable]; + } + + bb1: { + StorageDead(_3); + StorageDead(_2); +- StorageLive(_4); ++ nop; + _4 = identity:: as fn(u8) -> u8 (PointerCoercion(ReifyFnPointer)); + StorageLive(_5); + StorageLive(_6); + _6 = _4; +- _5 = opaque:: u8>(move _6) -> [return: bb2, unwind unreachable]; ++ _5 = opaque:: u8>(_4) -> [return: bb2, unwind unreachable]; + } + + bb2: { + StorageDead(_6); + StorageDead(_5); +- StorageLive(_7); +- _7 = {closure@$DIR/gvn.rs:591:19: 591:21}; +- StorageLive(_8); ++ nop; ++ _7 = const ZeroSized: {closure@$DIR/gvn.rs:591:19: 591:21}; ++ nop; + StorageLive(_9); +- _9 = _7; +- _8 = move _9 as fn() (PointerCoercion(ClosureFnPointer(Normal))); ++ _9 = const ZeroSized: {closure@$DIR/gvn.rs:591:19: 591:21}; ++ _8 = const ZeroSized: {closure@$DIR/gvn.rs:591:19: 591:21} as fn() (PointerCoercion(ClosureFnPointer(Normal))); + StorageDead(_9); + StorageLive(_10); + StorageLive(_11); + _11 = _8; +- _10 = opaque::(move _11) -> [return: bb3, unwind unreachable]; ++ _10 = opaque::(_8) -> [return: bb3, unwind unreachable]; + } + + bb3: { + StorageDead(_11); + StorageDead(_10); +- StorageLive(_12); ++ nop; + StorageLive(_13); +- _13 = _7; +- _12 = move _13 as fn() (PointerCoercion(ClosureFnPointer(Normal))); ++ _13 = const ZeroSized: {closure@$DIR/gvn.rs:591:19: 591:21}; ++ _12 = const ZeroSized: {closure@$DIR/gvn.rs:591:19: 591:21} as fn() (PointerCoercion(ClosureFnPointer(Normal))); + StorageDead(_13); + StorageLive(_14); + StorageLive(_15); + _15 = _12; +- _14 = opaque::(move _15) -> [return: bb4, unwind unreachable]; ++ _14 = opaque::(_12) -> [return: bb4, unwind unreachable]; + } + + bb4: { + StorageDead(_15); + StorageDead(_14); + _0 = const (); +- StorageDead(_12); +- StorageDead(_8); +- StorageDead(_7); +- StorageDead(_4); +- StorageDead(_1); ++ nop; ++ nop; ++ nop; ++ nop; ++ nop; + return; + } + } + diff --git a/tests/mir-opt/gvn.fn_pointers.GVN.panic-unwind.diff b/tests/mir-opt/gvn.fn_pointers.GVN.panic-unwind.diff new file mode 100644 index 0000000000000..e38a3d8520927 --- /dev/null +++ b/tests/mir-opt/gvn.fn_pointers.GVN.panic-unwind.diff @@ -0,0 +1,118 @@ +- // MIR for `fn_pointers` before GVN ++ // MIR for `fn_pointers` after GVN + + fn fn_pointers() -> () { + let mut _0: (); + let _1: fn(u8) -> u8; + let _2: (); + let mut _3: fn(u8) -> u8; + let _5: (); + let mut _6: fn(u8) -> u8; + let mut _9: {closure@$DIR/gvn.rs:591:19: 591:21}; + let _10: (); + let mut _11: fn(); + let mut _13: {closure@$DIR/gvn.rs:591:19: 591:21}; + let _14: (); + let mut _15: fn(); + scope 1 { + debug f => _1; + let _4: fn(u8) -> u8; + scope 2 { + debug g => _4; + let _7: {closure@$DIR/gvn.rs:591:19: 591:21}; + scope 3 { + debug closure => _7; + let _8: fn(); + scope 4 { + debug cf => _8; + let _12: fn(); + scope 5 { + debug cg => _12; + } + } + } + } + } + + bb0: { +- StorageLive(_1); ++ nop; + _1 = identity:: as fn(u8) -> u8 (PointerCoercion(ReifyFnPointer)); + StorageLive(_2); + StorageLive(_3); + _3 = _1; +- _2 = opaque:: u8>(move _3) -> [return: bb1, unwind continue]; ++ _2 = opaque:: u8>(_1) -> [return: bb1, unwind continue]; + } + + bb1: { + StorageDead(_3); + StorageDead(_2); +- StorageLive(_4); ++ nop; + _4 = identity:: as fn(u8) -> u8 (PointerCoercion(ReifyFnPointer)); + StorageLive(_5); + StorageLive(_6); + _6 = _4; +- _5 = opaque:: u8>(move _6) -> [return: bb2, unwind continue]; ++ _5 = opaque:: u8>(_4) -> [return: bb2, unwind continue]; + } + + bb2: { + StorageDead(_6); + StorageDead(_5); +- StorageLive(_7); +- _7 = {closure@$DIR/gvn.rs:591:19: 591:21}; +- StorageLive(_8); ++ nop; ++ _7 = const ZeroSized: {closure@$DIR/gvn.rs:591:19: 591:21}; ++ nop; + StorageLive(_9); +- _9 = _7; +- _8 = move _9 as fn() (PointerCoercion(ClosureFnPointer(Normal))); ++ _9 = const ZeroSized: {closure@$DIR/gvn.rs:591:19: 591:21}; ++ _8 = const ZeroSized: {closure@$DIR/gvn.rs:591:19: 591:21} as fn() (PointerCoercion(ClosureFnPointer(Normal))); + StorageDead(_9); + StorageLive(_10); + StorageLive(_11); + _11 = _8; +- _10 = opaque::(move _11) -> [return: bb3, unwind continue]; ++ _10 = opaque::(_8) -> [return: bb3, unwind continue]; + } + + bb3: { + StorageDead(_11); + StorageDead(_10); +- StorageLive(_12); ++ nop; + StorageLive(_13); +- _13 = _7; +- _12 = move _13 as fn() (PointerCoercion(ClosureFnPointer(Normal))); ++ _13 = const ZeroSized: {closure@$DIR/gvn.rs:591:19: 591:21}; ++ _12 = const ZeroSized: {closure@$DIR/gvn.rs:591:19: 591:21} as fn() (PointerCoercion(ClosureFnPointer(Normal))); + StorageDead(_13); + StorageLive(_14); + StorageLive(_15); + _15 = _12; +- _14 = opaque::(move _15) -> [return: bb4, unwind continue]; ++ _14 = opaque::(_12) -> [return: bb4, unwind continue]; + } + + bb4: { + StorageDead(_15); + StorageDead(_14); + _0 = const (); +- StorageDead(_12); +- StorageDead(_8); +- StorageDead(_7); +- StorageDead(_4); +- StorageDead(_1); ++ nop; ++ nop; ++ nop; ++ nop; ++ nop; + return; + } + } + diff --git a/tests/mir-opt/gvn.rs b/tests/mir-opt/gvn.rs index 1c14f818044f7..7ce851905e069 100644 --- a/tests/mir-opt/gvn.rs +++ b/tests/mir-opt/gvn.rs @@ -574,6 +574,29 @@ fn repeat() { let array = [val, val, val, val, val, val, val, val, val, val]; } +/// Verify that we do not merge fn pointers created by casts. +fn fn_pointers() { + // CHECK-LABEL: fn fn_pointers( + // CHECK: [[f:_.*]] = identity:: as fn(u8) -> u8 (PointerCoercion(ReifyFnPointer + // CHECK: opaque:: u8>([[f]]) + let f = identity as fn(u8) -> u8; + opaque(f); + // CHECK: [[g:_.*]] = identity:: as fn(u8) -> u8 (PointerCoercion(ReifyFnPointer + // CHECK: opaque:: u8>([[g]]) + let g = identity as fn(u8) -> u8; + opaque(g); + + // CHECK: [[cf:_.*]] = const {{.*}} as fn() (PointerCoercion(ClosureFnPointer + // CHECK: opaque::([[cf]]) + let closure = || {}; + let cf = closure as fn(); + opaque(cf); + // CHECK: [[cg:_.*]] = const {{.*}} as fn() (PointerCoercion(ClosureFnPointer + // CHECK: opaque::([[cg]]) + let cg = closure as fn(); + opaque(cg); +} + fn main() { subexpression_elimination(2, 4, 5); wrap_unwrap(5); @@ -590,6 +613,7 @@ fn main() { let (direct, indirect) = duplicate_slice(); assert_eq!(direct, indirect); repeat(); + fn_pointers(); } #[inline(never)] @@ -614,3 +638,4 @@ fn identity(x: T) -> T { // EMIT_MIR gvn.slices.GVN.diff // EMIT_MIR gvn.duplicate_slice.GVN.diff // EMIT_MIR gvn.repeat.GVN.diff +// EMIT_MIR gvn.fn_pointers.GVN.diff From d80eb3a498fe6dea75ea37142aae81ba26783efc Mon Sep 17 00:00:00 2001 From: Camille GILLOT Date: Sun, 22 Oct 2023 14:49:00 +0000 Subject: [PATCH 24/28] Verify that the alloc_id is Memory. --- compiler/rustc_mir_transform/src/gvn.rs | 7 ++++++- .../gvn.indirect_static.GVN.panic-abort.diff | 19 +++++++++++++++++++ .../gvn.indirect_static.GVN.panic-unwind.diff | 19 +++++++++++++++++++ tests/mir-opt/gvn.rs | 14 ++++++++++++++ 4 files changed, 58 insertions(+), 1 deletion(-) create mode 100644 tests/mir-opt/gvn.indirect_static.GVN.panic-abort.diff create mode 100644 tests/mir-opt/gvn.indirect_static.GVN.panic-unwind.diff diff --git a/compiler/rustc_mir_transform/src/gvn.rs b/compiler/rustc_mir_transform/src/gvn.rs index cf8a0fdb97e2d..cfa315fe61aba 100644 --- a/compiler/rustc_mir_transform/src/gvn.rs +++ b/compiler/rustc_mir_transform/src/gvn.rs @@ -90,6 +90,7 @@ use rustc_hir::def::DefKind; use rustc_index::bit_set::BitSet; use rustc_index::IndexVec; use rustc_macros::newtype_index; +use rustc_middle::mir::interpret::GlobalAlloc; use rustc_middle::mir::visit::*; use rustc_middle::mir::*; use rustc_middle::ty::adjustment::PointerCoercion; @@ -922,7 +923,11 @@ fn op_to_prop_const<'tcx>( let pointer = mplace.ptr().into_pointer_or_addr().ok()?; let (alloc_id, offset) = pointer.into_parts(); intern_const_alloc_for_constprop(ecx, alloc_id).ok()?; - return Some(ConstValue::Indirect { alloc_id, offset }); + if matches!(ecx.tcx.global_alloc(alloc_id), GlobalAlloc::Memory(_)) { + // `alloc_id` may point to a static. Codegen will choke on an `Indirect` with anything + // by `GlobalAlloc::Memory`, so do fall through to copying if needed. + return Some(ConstValue::Indirect { alloc_id, offset }); + } } // Everything failed: create a new allocation to hold the data. diff --git a/tests/mir-opt/gvn.indirect_static.GVN.panic-abort.diff b/tests/mir-opt/gvn.indirect_static.GVN.panic-abort.diff new file mode 100644 index 0000000000000..f853942bbb664 --- /dev/null +++ b/tests/mir-opt/gvn.indirect_static.GVN.panic-abort.diff @@ -0,0 +1,19 @@ +- // MIR for `indirect_static` before GVN ++ // MIR for `indirect_static` after GVN + + fn indirect_static() -> () { + let mut _0: (); + let mut _1: &std::option::Option; + let mut _2: u8; + + bb0: { + _1 = const {ALLOC0: &Option}; + _2 = (((*_1) as variant#1).0: u8); + return; + } + } + + ALLOC0 (static: A, size: 2, align: 1) { + 00 __ │ .░ + } + diff --git a/tests/mir-opt/gvn.indirect_static.GVN.panic-unwind.diff b/tests/mir-opt/gvn.indirect_static.GVN.panic-unwind.diff new file mode 100644 index 0000000000000..f853942bbb664 --- /dev/null +++ b/tests/mir-opt/gvn.indirect_static.GVN.panic-unwind.diff @@ -0,0 +1,19 @@ +- // MIR for `indirect_static` before GVN ++ // MIR for `indirect_static` after GVN + + fn indirect_static() -> () { + let mut _0: (); + let mut _1: &std::option::Option; + let mut _2: u8; + + bb0: { + _1 = const {ALLOC0: &Option}; + _2 = (((*_1) as variant#1).0: u8); + return; + } + } + + ALLOC0 (static: A, size: 2, align: 1) { + 00 __ │ .░ + } + diff --git a/tests/mir-opt/gvn.rs b/tests/mir-opt/gvn.rs index 7ce851905e069..10a66ced026e4 100644 --- a/tests/mir-opt/gvn.rs +++ b/tests/mir-opt/gvn.rs @@ -597,6 +597,18 @@ fn fn_pointers() { opaque(cg); } +/// Verify that we do not create a `ConstValue::Indirect` backed by a static's AllocId. +#[custom_mir(dialect = "analysis")] +fn indirect_static() { + static A: Option = None; + + mir!({ + let ptr = Static(A); + let out = Field::(Variant(*ptr, 1), 0); + Return() + }) +} + fn main() { subexpression_elimination(2, 4, 5); wrap_unwrap(5); @@ -614,6 +626,7 @@ fn main() { assert_eq!(direct, indirect); repeat(); fn_pointers(); + indirect_static(); } #[inline(never)] @@ -639,3 +652,4 @@ fn identity(x: T) -> T { // EMIT_MIR gvn.duplicate_slice.GVN.diff // EMIT_MIR gvn.repeat.GVN.diff // EMIT_MIR gvn.fn_pointers.GVN.diff +// EMIT_MIR gvn.indirect_static.GVN.diff From eda1928baa868495cd3481358390f8a4e286a659 Mon Sep 17 00:00:00 2001 From: Camille GILLOT Date: Mon, 23 Oct 2023 18:17:32 +0000 Subject: [PATCH 25/28] Typo. --- src/tools/miri/tests/pass/function_pointers.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/tools/miri/tests/pass/function_pointers.rs b/src/tools/miri/tests/pass/function_pointers.rs index 8e58692a0c74a..36679b7180a64 100644 --- a/src/tools/miri/tests/pass/function_pointers.rs +++ b/src/tools/miri/tests/pass/function_pointers.rs @@ -80,7 +80,7 @@ fn main() { // but Miri currently uses a fixed address for monomorphic functions. assert!(return_fn_ptr(i) == i); assert!(return_fn_ptr(i) as unsafe fn() -> i32 == i as fn() -> i32 as unsafe fn() -> i32); - // Miri gives it many different addresses to different reifications of a generic function. + // Miri gives different addresses to different reifications of a generic function. assert!(return_fn_ptr(f) != f); // However, if we only turn `f` into a function pointer and use that pointer, // it is equal to itself. From 72f0e0e79535246c31b0b1e992f9b0ef8a072dc4 Mon Sep 17 00:00:00 2001 From: Camille GILLOT Date: Mon, 23 Oct 2023 18:27:54 +0000 Subject: [PATCH 26/28] Rename has_provance and tweaks comments. --- .../rustc_const_eval/src/interpret/intern.rs | 5 ++++- compiler/rustc_middle/src/mir/consts.rs | 19 +++++++++++-------- compiler/rustc_mir_transform/src/gvn.rs | 12 +++++++++--- 3 files changed, 24 insertions(+), 12 deletions(-) diff --git a/compiler/rustc_const_eval/src/interpret/intern.rs b/compiler/rustc_const_eval/src/interpret/intern.rs index 23d6d32194710..3d90e95c09c79 100644 --- a/compiler/rustc_const_eval/src/interpret/intern.rs +++ b/compiler/rustc_const_eval/src/interpret/intern.rs @@ -474,9 +474,12 @@ pub fn intern_const_alloc_for_constprop< alloc.mutability = Mutability::Not; - // link the alloc id to the actual allocation + // We are not doing recursive interning, so we don't currently support provenance. + // (If this assertion ever triggers, we should just implement a + // proper recursive interning loop.) assert!(alloc.provenance().ptrs().is_empty()); + // Link the alloc id to the actual allocation let alloc = ecx.tcx.mk_const_alloc(alloc); ecx.tcx.set_alloc_id_memory(alloc_id, alloc); diff --git a/compiler/rustc_middle/src/mir/consts.rs b/compiler/rustc_middle/src/mir/consts.rs index 88d4a37f21090..9ddf3903d04ce 100644 --- a/compiler/rustc_middle/src/mir/consts.rs +++ b/compiler/rustc_middle/src/mir/consts.rs @@ -173,16 +173,19 @@ impl<'tcx> ConstValue<'tcx> { Some(data.inner().inspect_with_uninit_and_ptr_outside_interpreter(start..end)) } - pub fn has_provenance(&self, tcx: TyCtxt<'tcx>, size: Size) -> bool { - let (alloc, start, end) = match *self { + /// Check if a constant may contain provenance information. This is used by MIR opts. + pub fn may_have_provenance(&self, tcx: TyCtxt<'tcx>, size: Size) -> bool { + match *self { ConstValue::ZeroSized | ConstValue::Scalar(Scalar::Int(_)) => return false, ConstValue::Scalar(Scalar::Ptr(..)) => return true, - ConstValue::Slice { data, meta } => (data, Size::ZERO, Size::from_bytes(meta)), - ConstValue::Indirect { alloc_id, offset } => { - (tcx.global_alloc(alloc_id).unwrap_memory(), offset, offset + size) - } - }; - !alloc.inner().provenance().range_empty(super::AllocRange::from(start..end), &tcx) + ConstValue::Slice { data, meta: _ } => !data.inner().provenance().ptrs().is_empty(), + ConstValue::Indirect { alloc_id, offset } => !tcx + .global_alloc(alloc_id) + .unwrap_memory() + .inner() + .provenance() + .range_empty(super::AllocRange::from(offset..offset + size), &tcx), + } } } diff --git a/compiler/rustc_mir_transform/src/gvn.rs b/compiler/rustc_mir_transform/src/gvn.rs index cfa315fe61aba..3f30f399588f8 100644 --- a/compiler/rustc_mir_transform/src/gvn.rs +++ b/compiler/rustc_mir_transform/src/gvn.rs @@ -910,11 +910,13 @@ fn op_to_prop_const<'tcx>( return Some(ConstValue::Scalar(scalar)); } - // If this constant is a projection of another, we can return it directly. + // If this constant is already represented as an `Allocation`, + // try putting it into global memory to return it. if let Either::Left(mplace) = op.as_mplace_or_imm() { let (size, _align) = ecx.size_and_align_of_mplace(&mplace).ok()??; // Do not try interning a value that contains provenance. + // Due to https://github.com/rust-lang/rust/issues/79738, doing so could lead to bugs. let alloc_ref = ecx.get_ptr_alloc(mplace.ptr(), size).ok()??; if alloc_ref.has_provenance() { return None; @@ -935,7 +937,10 @@ fn op_to_prop_const<'tcx>( ecx.intern_with_temp_alloc(op.layout, |ecx, dest| ecx.copy_op(op, dest, false)).ok()?; let value = ConstValue::Indirect { alloc_id, offset: Size::ZERO }; - if !value.has_provenance(*ecx.tcx, op.layout.size) { + // Check that we do not leak a pointer. + // Those pointers may lose part of their identity in codegen. + // See https://github.com/rust-lang/rust/issues/79738. + if !value.may_have_provenance(*ecx.tcx, op.layout.size) { return Some(value); } @@ -964,7 +969,8 @@ impl<'tcx> VnState<'_, 'tcx> { // Check that we do not leak a pointer. // Those pointers may lose part of their identity in codegen. - assert!(!value.has_provenance(self.tcx, op.layout.size)); + // See https://github.com/rust-lang/rust/issues/79738. + assert!(!value.may_have_provenance(self.tcx, op.layout.size)); let const_ = Const::Val(value, op.layout.ty); Some(ConstOperand { span: rustc_span::DUMMY_SP, user_ty: None, const_ }) From 856161886a9b2bea4621441c4f6c2ea358cddf56 Mon Sep 17 00:00:00 2001 From: Camille GILLOT Date: Mon, 23 Oct 2023 19:37:01 +0000 Subject: [PATCH 27/28] Directly check provenance from the AllocId. --- compiler/rustc_mir_transform/src/gvn.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/compiler/rustc_mir_transform/src/gvn.rs b/compiler/rustc_mir_transform/src/gvn.rs index 3f30f399588f8..39065962c6959 100644 --- a/compiler/rustc_mir_transform/src/gvn.rs +++ b/compiler/rustc_mir_transform/src/gvn.rs @@ -940,7 +940,7 @@ fn op_to_prop_const<'tcx>( // Check that we do not leak a pointer. // Those pointers may lose part of their identity in codegen. // See https://github.com/rust-lang/rust/issues/79738. - if !value.may_have_provenance(*ecx.tcx, op.layout.size) { + if ecx.tcx.global_alloc(alloc_id).unwrap_memory().inner().provenance().ptrs().is_empty() { return Some(value); } From 24be43356e31d6b0ee2ec8bf912c0a325f236a1d Mon Sep 17 00:00:00 2001 From: Camille Gillot Date: Fri, 27 Oct 2023 20:51:25 +0200 Subject: [PATCH 28/28] Apply suggestions from code review Co-authored-by: Ralf Jung --- compiler/rustc_middle/src/mir/consts.rs | 11 +++++++---- compiler/rustc_mir_transform/src/gvn.rs | 7 +++++-- 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/compiler/rustc_middle/src/mir/consts.rs b/compiler/rustc_middle/src/mir/consts.rs index 9ddf3903d04ce..3cb2e349ce084 100644 --- a/compiler/rustc_middle/src/mir/consts.rs +++ b/compiler/rustc_middle/src/mir/consts.rs @@ -174,10 +174,13 @@ impl<'tcx> ConstValue<'tcx> { } /// Check if a constant may contain provenance information. This is used by MIR opts. + /// Can return `true` even if there is no provenance. pub fn may_have_provenance(&self, tcx: TyCtxt<'tcx>, size: Size) -> bool { match *self { ConstValue::ZeroSized | ConstValue::Scalar(Scalar::Int(_)) => return false, ConstValue::Scalar(Scalar::Ptr(..)) => return true, + // It's hard to find out the part of the allocation we point to; + // just conservatively check everything. ConstValue::Slice { data, meta: _ } => !data.inner().provenance().ptrs().is_empty(), ConstValue::Indirect { alloc_id, offset } => !tcx .global_alloc(alloc_id) @@ -504,10 +507,10 @@ impl<'tcx> Const<'tcx> { /// Return true if any evaluation of this constant always returns the same value, /// taking into account even pointer identity tests. pub fn is_deterministic(&self) -> bool { - // Some constants may contain pointers. We need to preserve the provenance of these - // pointers, but not all constants guarantee this: - // - valtrees purposefully do not; - // - ConstValue::Slice does not either. + // Some constants may generate fresh allocations for pointers they contain, + // so using the same constant twice can yield two different results: + // - valtrees purposefully generate new allocations + // - ConstValue::Slice also generate new allocations match self { Const::Ty(c) => match c.kind() { ty::ConstKind::Param(..) => true, diff --git a/compiler/rustc_mir_transform/src/gvn.rs b/compiler/rustc_mir_transform/src/gvn.rs index 39065962c6959..de0dc25808b30 100644 --- a/compiler/rustc_mir_transform/src/gvn.rs +++ b/compiler/rustc_mir_transform/src/gvn.rs @@ -917,6 +917,7 @@ fn op_to_prop_const<'tcx>( // Do not try interning a value that contains provenance. // Due to https://github.com/rust-lang/rust/issues/79738, doing so could lead to bugs. + // FIXME: remove this hack once that issue is fixed. let alloc_ref = ecx.get_ptr_alloc(mplace.ptr(), size).ok()??; if alloc_ref.has_provenance() { return None; @@ -928,6 +929,8 @@ fn op_to_prop_const<'tcx>( if matches!(ecx.tcx.global_alloc(alloc_id), GlobalAlloc::Memory(_)) { // `alloc_id` may point to a static. Codegen will choke on an `Indirect` with anything // by `GlobalAlloc::Memory`, so do fall through to copying if needed. + // FIXME: find a way to treat this more uniformly + // (probably by fixing codegen) return Some(ConstValue::Indirect { alloc_id, offset }); } } @@ -939,7 +942,7 @@ fn op_to_prop_const<'tcx>( // Check that we do not leak a pointer. // Those pointers may lose part of their identity in codegen. - // See https://github.com/rust-lang/rust/issues/79738. + // FIXME: remove this hack once https://github.com/rust-lang/rust/issues/79738 is fixed. if ecx.tcx.global_alloc(alloc_id).unwrap_memory().inner().provenance().ptrs().is_empty() { return Some(value); } @@ -969,7 +972,7 @@ impl<'tcx> VnState<'_, 'tcx> { // Check that we do not leak a pointer. // Those pointers may lose part of their identity in codegen. - // See https://github.com/rust-lang/rust/issues/79738. + // FIXME: remove this hack once https://github.com/rust-lang/rust/issues/79738 is fixed. assert!(!value.may_have_provenance(self.tcx, op.layout.size)); let const_ = Const::Val(value, op.layout.ty);