Replace NormalizeArrayLen with GVN

GVN is actually on in release, and covers all the same things (or more), with `LowerSliceLen` changed to produce `PtrMetadata`.
This commit is contained in:
Scott McMurray 2024-06-16 01:22:58 -07:00
parent 4a7b6c0e6c
commit b611b6bbb8
30 changed files with 138 additions and 218 deletions

View File

@ -88,7 +88,6 @@ mod lower_slice_len;
mod match_branches; mod match_branches;
mod mentioned_items; mod mentioned_items;
mod multiple_return_terminators; mod multiple_return_terminators;
mod normalize_array_len;
mod nrvo; mod nrvo;
mod prettify; mod prettify;
mod promote_consts; mod promote_consts;
@ -581,9 +580,6 @@ fn run_optimization_passes<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
&o1(simplify::SimplifyCfg::AfterUnreachableEnumBranching), &o1(simplify::SimplifyCfg::AfterUnreachableEnumBranching),
// Inlining may have introduced a lot of redundant code and a large move pattern. // Inlining may have introduced a lot of redundant code and a large move pattern.
// Now, we need to shrink the generated MIR. // Now, we need to shrink the generated MIR.
// Has to run after `slice::len` lowering
&normalize_array_len::NormalizeArrayLen,
&ref_prop::ReferencePropagation, &ref_prop::ReferencePropagation,
&sroa::ScalarReplacementOfAggregates, &sroa::ScalarReplacementOfAggregates,
&match_branches::MatchBranchSimplification, &match_branches::MatchBranchSimplification,

View File

@ -1,10 +1,9 @@
//! This pass lowers calls to core::slice::len to just Len op. //! This pass lowers calls to core::slice::len to just PtrMetadata op.
//! It should run before inlining! //! It should run before inlining!
use rustc_hir::def_id::DefId; use rustc_hir::def_id::DefId;
use rustc_index::IndexSlice;
use rustc_middle::mir::*; use rustc_middle::mir::*;
use rustc_middle::ty::{self, TyCtxt}; use rustc_middle::ty::TyCtxt;
pub struct LowerSliceLenCalls; pub struct LowerSliceLenCalls;
@ -29,16 +28,11 @@ pub fn lower_slice_len_calls<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
let basic_blocks = body.basic_blocks.as_mut_preserves_cfg(); let basic_blocks = body.basic_blocks.as_mut_preserves_cfg();
for block in basic_blocks { for block in basic_blocks {
// lower `<[_]>::len` calls // lower `<[_]>::len` calls
lower_slice_len_call(tcx, block, &body.local_decls, slice_len_fn_item_def_id); lower_slice_len_call(block, slice_len_fn_item_def_id);
} }
} }
fn lower_slice_len_call<'tcx>( fn lower_slice_len_call<'tcx>(block: &mut BasicBlockData<'tcx>, slice_len_fn_item_def_id: DefId) {
tcx: TyCtxt<'tcx>,
block: &mut BasicBlockData<'tcx>,
local_decls: &IndexSlice<Local, LocalDecl<'tcx>>,
slice_len_fn_item_def_id: DefId,
) {
let terminator = block.terminator(); let terminator = block.terminator();
if let TerminatorKind::Call { if let TerminatorKind::Call {
func, func,
@ -50,19 +44,17 @@ fn lower_slice_len_call<'tcx>(
} = &terminator.kind } = &terminator.kind
// some heuristics for fast rejection // some heuristics for fast rejection
&& let [arg] = &args[..] && let [arg] = &args[..]
&& let Some(arg) = arg.node.place() && let Some((fn_def_id, _)) = func.const_fn_def()
&& let ty::FnDef(fn_def_id, _) = func.ty(local_decls, tcx).kind() && fn_def_id == slice_len_fn_item_def_id
&& *fn_def_id == slice_len_fn_item_def_id
{ {
// perform modifications from something like: // perform modifications from something like:
// _5 = core::slice::<impl [u8]>::len(move _6) -> bb1 // _5 = core::slice::<impl [u8]>::len(move _6) -> bb1
// into: // into:
// _5 = Len(*_6) // _5 = PtrMetadata(move _6)
// goto bb1 // goto bb1
// make new RValue for Len // make new RValue for Len
let deref_arg = tcx.mk_place_deref(arg); let r_value = Rvalue::UnaryOp(UnOp::PtrMetadata, arg.node.clone());
let r_value = Rvalue::Len(deref_arg);
let len_statement_kind = StatementKind::Assign(Box::new((*destination, r_value))); let len_statement_kind = StatementKind::Assign(Box::new((*destination, r_value)));
let add_statement = let add_statement =
Statement { kind: len_statement_kind, source_info: terminator.source_info }; Statement { kind: len_statement_kind, source_info: terminator.source_info };

View File

@ -1,103 +0,0 @@
//! This pass eliminates casting of arrays into slices when their length
//! is taken using `.len()` method. Handy to preserve information in MIR for const prop
use crate::ssa::SsaLocals;
use rustc_index::IndexVec;
use rustc_middle::mir::visit::*;
use rustc_middle::mir::*;
use rustc_middle::ty::{self, TyCtxt};
pub struct NormalizeArrayLen;
impl<'tcx> MirPass<'tcx> for NormalizeArrayLen {
fn is_enabled(&self, sess: &rustc_session::Session) -> bool {
sess.mir_opt_level() >= 3
}
#[instrument(level = "trace", skip(self, tcx, body))]
fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
debug!(def_id = ?body.source.def_id());
normalize_array_len_calls(tcx, body)
}
}
fn normalize_array_len_calls<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
let param_env = tcx.param_env_reveal_all_normalized(body.source.def_id());
let ssa = SsaLocals::new(tcx, body, param_env);
let slice_lengths = compute_slice_length(tcx, &ssa, body);
debug!(?slice_lengths);
Replacer { tcx, slice_lengths }.visit_body_preserves_cfg(body);
}
fn compute_slice_length<'tcx>(
tcx: TyCtxt<'tcx>,
ssa: &SsaLocals,
body: &Body<'tcx>,
) -> IndexVec<Local, Option<ty::Const<'tcx>>> {
let mut slice_lengths = IndexVec::from_elem(None, &body.local_decls);
for (local, rvalue, _) in ssa.assignments(body) {
match rvalue {
Rvalue::Cast(
CastKind::PointerCoercion(ty::adjustment::PointerCoercion::Unsize),
operand,
cast_ty,
) => {
let operand_ty = operand.ty(body, tcx);
debug!(?operand_ty);
if let Some(operand_ty) = operand_ty.builtin_deref(true)
&& let ty::Array(_, len) = operand_ty.kind()
&& let Some(cast_ty) = cast_ty.builtin_deref(true)
&& let ty::Slice(..) = cast_ty.kind()
{
slice_lengths[local] = Some(*len);
}
}
// The length information is stored in the fat pointer, so we treat `operand` as a value.
Rvalue::Use(operand) => {
if let Some(rhs) = operand.place()
&& let Some(rhs) = rhs.as_local()
{
slice_lengths[local] = slice_lengths[rhs];
}
}
// The length information is stored in the fat pointer.
// Reborrowing copies length information from one pointer to the other.
Rvalue::Ref(_, _, rhs) | Rvalue::AddressOf(_, rhs) => {
if let [PlaceElem::Deref] = rhs.projection[..] {
slice_lengths[local] = slice_lengths[rhs.local];
}
}
_ => {}
}
}
slice_lengths
}
struct Replacer<'tcx> {
tcx: TyCtxt<'tcx>,
slice_lengths: IndexVec<Local, Option<ty::Const<'tcx>>>,
}
impl<'tcx> MutVisitor<'tcx> for Replacer<'tcx> {
fn tcx(&self) -> TyCtxt<'tcx> {
self.tcx
}
fn visit_rvalue(&mut self, rvalue: &mut Rvalue<'tcx>, loc: Location) {
if let Rvalue::Len(place) = rvalue
&& let [PlaceElem::Deref] = &place.projection[..]
&& let Some(len) = self.slice_lengths[place.local]
{
*rvalue = Rvalue::Use(Operand::Constant(Box::new(ConstOperand {
span: rustc_span::DUMMY_SP,
user_ty: None,
const_: Const::from_ty_const(len, self.tcx.types.usize, self.tcx),
})));
}
self.super_rvalue(rvalue, loc);
}
}

View File

@ -1,6 +1,5 @@
// skip-filecheck // skip-filecheck
// EMIT_MIR_FOR_EACH_PANIC_STRATEGY // EMIT_MIR_FOR_EACH_PANIC_STRATEGY
//@ compile-flags: -Zmir-enable-passes=-NormalizeArrayLen
// Check that we do not insert StorageDead at each target if StorageDead was never seen // Check that we do not insert StorageDead at each target if StorageDead was never seen
use std::fmt::Debug; use std::fmt::Debug;

View File

@ -1,5 +1,5 @@
- // MIR for `array_bound` before NormalizeArrayLen - // MIR for `array_bound` before GVN
+ // MIR for `array_bound` after NormalizeArrayLen + // MIR for `array_bound` after GVN
fn array_bound(_1: usize, _2: &[u8; N]) -> u8 { fn array_bound(_1: usize, _2: &[u8; N]) -> u8 {
debug index => _1; debug index => _1;
@ -24,14 +24,15 @@
_7 = &(*_2); _7 = &(*_2);
_6 = move _7 as &[u8] (PointerCoercion(Unsize)); _6 = move _7 as &[u8] (PointerCoercion(Unsize));
StorageDead(_7); StorageDead(_7);
- _5 = Len((*_6)); - _5 = PtrMetadata(move _6);
+ _5 = const N; + _5 = const N;
goto -> bb1; goto -> bb1;
} }
bb1: { bb1: {
StorageDead(_6); StorageDead(_6);
_3 = Lt(move _4, move _5); - _3 = Lt(move _4, move _5);
+ _3 = Lt(_1, move _5);
switchInt(move _3) -> [0: bb4, otherwise: bb2]; switchInt(move _3) -> [0: bb4, otherwise: bb2];
} }
@ -40,13 +41,17 @@
StorageDead(_4); StorageDead(_4);
StorageLive(_8); StorageLive(_8);
_8 = _1; _8 = _1;
_9 = Len((*_2)); - _9 = Len((*_2));
_10 = Lt(_8, _9); - _10 = Lt(_8, _9);
assert(move _10, "index out of bounds: the length is {} but the index is {}", move _9, _8) -> [success: bb3, unwind unreachable]; - assert(move _10, "index out of bounds: the length is {} but the index is {}", move _9, _8) -> [success: bb3, unwind unreachable];
+ _9 = const N;
+ _10 = Lt(_1, const N);
+ assert(move _10, "index out of bounds: the length is {} but the index is {}", const N, _1) -> [success: bb3, unwind unreachable];
} }
bb3: { bb3: {
_0 = (*_2)[_8]; - _0 = (*_2)[_8];
+ _0 = (*_2)[_1];
StorageDead(_8); StorageDead(_8);
goto -> bb5; goto -> bb5;
} }

View File

@ -1,5 +1,5 @@
- // MIR for `array_bound` before NormalizeArrayLen - // MIR for `array_bound` before GVN
+ // MIR for `array_bound` after NormalizeArrayLen + // MIR for `array_bound` after GVN
fn array_bound(_1: usize, _2: &[u8; N]) -> u8 { fn array_bound(_1: usize, _2: &[u8; N]) -> u8 {
debug index => _1; debug index => _1;
@ -24,14 +24,15 @@
_7 = &(*_2); _7 = &(*_2);
_6 = move _7 as &[u8] (PointerCoercion(Unsize)); _6 = move _7 as &[u8] (PointerCoercion(Unsize));
StorageDead(_7); StorageDead(_7);
- _5 = Len((*_6)); - _5 = PtrMetadata(move _6);
+ _5 = const N; + _5 = const N;
goto -> bb1; goto -> bb1;
} }
bb1: { bb1: {
StorageDead(_6); StorageDead(_6);
_3 = Lt(move _4, move _5); - _3 = Lt(move _4, move _5);
+ _3 = Lt(_1, move _5);
switchInt(move _3) -> [0: bb4, otherwise: bb2]; switchInt(move _3) -> [0: bb4, otherwise: bb2];
} }
@ -40,13 +41,17 @@
StorageDead(_4); StorageDead(_4);
StorageLive(_8); StorageLive(_8);
_8 = _1; _8 = _1;
_9 = Len((*_2)); - _9 = Len((*_2));
_10 = Lt(_8, _9); - _10 = Lt(_8, _9);
assert(move _10, "index out of bounds: the length is {} but the index is {}", move _9, _8) -> [success: bb3, unwind continue]; - assert(move _10, "index out of bounds: the length is {} but the index is {}", move _9, _8) -> [success: bb3, unwind continue];
+ _9 = const N;
+ _10 = Lt(_1, const N);
+ assert(move _10, "index out of bounds: the length is {} but the index is {}", const N, _1) -> [success: bb3, unwind continue];
} }
bb3: { bb3: {
_0 = (*_2)[_8]; - _0 = (*_2)[_8];
+ _0 = (*_2)[_1];
StorageDead(_8); StorageDead(_8);
goto -> bb5; goto -> bb5;
} }

View File

@ -1,5 +1,5 @@
- // MIR for `array_bound_mut` before NormalizeArrayLen - // MIR for `array_bound_mut` before GVN
+ // MIR for `array_bound_mut` after NormalizeArrayLen + // MIR for `array_bound_mut` after GVN
fn array_bound_mut(_1: usize, _2: &mut [u8; N]) -> u8 { fn array_bound_mut(_1: usize, _2: &mut [u8; N]) -> u8 {
debug index => _1; debug index => _1;
@ -27,14 +27,15 @@
_7 = &(*_2); _7 = &(*_2);
_6 = move _7 as &[u8] (PointerCoercion(Unsize)); _6 = move _7 as &[u8] (PointerCoercion(Unsize));
StorageDead(_7); StorageDead(_7);
- _5 = Len((*_6)); - _5 = PtrMetadata(move _6);
+ _5 = const N; + _5 = const N;
goto -> bb1; goto -> bb1;
} }
bb1: { bb1: {
StorageDead(_6); StorageDead(_6);
_3 = Lt(move _4, move _5); - _3 = Lt(move _4, move _5);
+ _3 = Lt(_1, move _5);
switchInt(move _3) -> [0: bb4, otherwise: bb2]; switchInt(move _3) -> [0: bb4, otherwise: bb2];
} }
@ -43,13 +44,17 @@
StorageDead(_4); StorageDead(_4);
StorageLive(_8); StorageLive(_8);
_8 = _1; _8 = _1;
_9 = Len((*_2)); - _9 = Len((*_2));
_10 = Lt(_8, _9); - _10 = Lt(_8, _9);
assert(move _10, "index out of bounds: the length is {} but the index is {}", move _9, _8) -> [success: bb3, unwind unreachable]; - assert(move _10, "index out of bounds: the length is {} but the index is {}", move _9, _8) -> [success: bb3, unwind unreachable];
+ _9 = const N;
+ _10 = Lt(_1, const N);
+ assert(move _10, "index out of bounds: the length is {} but the index is {}", const N, _1) -> [success: bb3, unwind unreachable];
} }
bb3: { bb3: {
_0 = (*_2)[_8]; - _0 = (*_2)[_8];
+ _0 = (*_2)[_1];
StorageDead(_8); StorageDead(_8);
goto -> bb6; goto -> bb6;
} }
@ -59,13 +64,17 @@
StorageDead(_4); StorageDead(_4);
StorageLive(_11); StorageLive(_11);
_11 = const 0_usize; _11 = const 0_usize;
_12 = Len((*_2)); - _12 = Len((*_2));
_13 = Lt(_11, _12); - _13 = Lt(_11, _12);
assert(move _13, "index out of bounds: the length is {} but the index is {}", move _12, _11) -> [success: bb5, unwind unreachable]; - assert(move _13, "index out of bounds: the length is {} but the index is {}", move _12, _11) -> [success: bb5, unwind unreachable];
+ _12 = const N;
+ _13 = Lt(const 0_usize, const N);
+ assert(move _13, "index out of bounds: the length is {} but the index is {}", const N, const 0_usize) -> [success: bb5, unwind unreachable];
} }
bb5: { bb5: {
(*_2)[_11] = const 42_u8; - (*_2)[_11] = const 42_u8;
+ (*_2)[0 of 1] = const 42_u8;
StorageDead(_11); StorageDead(_11);
_0 = const 42_u8; _0 = const 42_u8;
goto -> bb6; goto -> bb6;

View File

@ -1,5 +1,5 @@
- // MIR for `array_bound_mut` before NormalizeArrayLen - // MIR for `array_bound_mut` before GVN
+ // MIR for `array_bound_mut` after NormalizeArrayLen + // MIR for `array_bound_mut` after GVN
fn array_bound_mut(_1: usize, _2: &mut [u8; N]) -> u8 { fn array_bound_mut(_1: usize, _2: &mut [u8; N]) -> u8 {
debug index => _1; debug index => _1;
@ -27,14 +27,15 @@
_7 = &(*_2); _7 = &(*_2);
_6 = move _7 as &[u8] (PointerCoercion(Unsize)); _6 = move _7 as &[u8] (PointerCoercion(Unsize));
StorageDead(_7); StorageDead(_7);
- _5 = Len((*_6)); - _5 = PtrMetadata(move _6);
+ _5 = const N; + _5 = const N;
goto -> bb1; goto -> bb1;
} }
bb1: { bb1: {
StorageDead(_6); StorageDead(_6);
_3 = Lt(move _4, move _5); - _3 = Lt(move _4, move _5);
+ _3 = Lt(_1, move _5);
switchInt(move _3) -> [0: bb4, otherwise: bb2]; switchInt(move _3) -> [0: bb4, otherwise: bb2];
} }
@ -43,13 +44,17 @@
StorageDead(_4); StorageDead(_4);
StorageLive(_8); StorageLive(_8);
_8 = _1; _8 = _1;
_9 = Len((*_2)); - _9 = Len((*_2));
_10 = Lt(_8, _9); - _10 = Lt(_8, _9);
assert(move _10, "index out of bounds: the length is {} but the index is {}", move _9, _8) -> [success: bb3, unwind continue]; - assert(move _10, "index out of bounds: the length is {} but the index is {}", move _9, _8) -> [success: bb3, unwind continue];
+ _9 = const N;
+ _10 = Lt(_1, const N);
+ assert(move _10, "index out of bounds: the length is {} but the index is {}", const N, _1) -> [success: bb3, unwind continue];
} }
bb3: { bb3: {
_0 = (*_2)[_8]; - _0 = (*_2)[_8];
+ _0 = (*_2)[_1];
StorageDead(_8); StorageDead(_8);
goto -> bb6; goto -> bb6;
} }
@ -59,13 +64,17 @@
StorageDead(_4); StorageDead(_4);
StorageLive(_11); StorageLive(_11);
_11 = const 0_usize; _11 = const 0_usize;
_12 = Len((*_2)); - _12 = Len((*_2));
_13 = Lt(_11, _12); - _13 = Lt(_11, _12);
assert(move _13, "index out of bounds: the length is {} but the index is {}", move _12, _11) -> [success: bb5, unwind continue]; - assert(move _13, "index out of bounds: the length is {} but the index is {}", move _12, _11) -> [success: bb5, unwind continue];
+ _12 = const N;
+ _13 = Lt(const 0_usize, const N);
+ assert(move _13, "index out of bounds: the length is {} but the index is {}", const N, const 0_usize) -> [success: bb5, unwind continue];
} }
bb5: { bb5: {
(*_2)[_11] = const 42_u8; - (*_2)[_11] = const 42_u8;
+ (*_2)[0 of 1] = const 42_u8;
StorageDead(_11); StorageDead(_11);
_0 = const 42_u8; _0 = const 42_u8;
goto -> bb6; goto -> bb6;

View File

@ -1,5 +1,5 @@
- // MIR for `array_len` before NormalizeArrayLen - // MIR for `array_len` before GVN
+ // MIR for `array_len` after NormalizeArrayLen + // MIR for `array_len` after GVN
fn array_len(_1: &[u8; N]) -> usize { fn array_len(_1: &[u8; N]) -> usize {
debug arr => _1; debug arr => _1;
@ -13,7 +13,7 @@
_3 = &(*_1); _3 = &(*_1);
_2 = move _3 as &[u8] (PointerCoercion(Unsize)); _2 = move _3 as &[u8] (PointerCoercion(Unsize));
StorageDead(_3); StorageDead(_3);
- _0 = Len((*_2)); - _0 = PtrMetadata(move _2);
+ _0 = const N; + _0 = const N;
goto -> bb1; goto -> bb1;
} }

View File

@ -1,5 +1,5 @@
- // MIR for `array_len` before NormalizeArrayLen - // MIR for `array_len` before GVN
+ // MIR for `array_len` after NormalizeArrayLen + // MIR for `array_len` after GVN
fn array_len(_1: &[u8; N]) -> usize { fn array_len(_1: &[u8; N]) -> usize {
debug arr => _1; debug arr => _1;
@ -13,7 +13,7 @@
_3 = &(*_1); _3 = &(*_1);
_2 = move _3 as &[u8] (PointerCoercion(Unsize)); _2 = move _3 as &[u8] (PointerCoercion(Unsize));
StorageDead(_3); StorageDead(_3);
- _0 = Len((*_2)); - _0 = PtrMetadata(move _2);
+ _0 = const N; + _0 = const N;
goto -> bb1; goto -> bb1;
} }

View File

@ -1,5 +1,5 @@
- // MIR for `array_len_by_value` before NormalizeArrayLen - // MIR for `array_len_by_value` before GVN
+ // MIR for `array_len_by_value` after NormalizeArrayLen + // MIR for `array_len_by_value` after GVN
fn array_len_by_value(_1: [u8; N]) -> usize { fn array_len_by_value(_1: [u8; N]) -> usize {
debug arr => _1; debug arr => _1;
@ -13,7 +13,7 @@
_3 = &_1; _3 = &_1;
_2 = move _3 as &[u8] (PointerCoercion(Unsize)); _2 = move _3 as &[u8] (PointerCoercion(Unsize));
StorageDead(_3); StorageDead(_3);
- _0 = Len((*_2)); - _0 = PtrMetadata(move _2);
+ _0 = const N; + _0 = const N;
goto -> bb1; goto -> bb1;
} }

View File

@ -1,5 +1,5 @@
- // MIR for `array_len_by_value` before NormalizeArrayLen - // MIR for `array_len_by_value` before GVN
+ // MIR for `array_len_by_value` after NormalizeArrayLen + // MIR for `array_len_by_value` after GVN
fn array_len_by_value(_1: [u8; N]) -> usize { fn array_len_by_value(_1: [u8; N]) -> usize {
debug arr => _1; debug arr => _1;
@ -13,7 +13,7 @@
_3 = &_1; _3 = &_1;
_2 = move _3 as &[u8] (PointerCoercion(Unsize)); _2 = move _3 as &[u8] (PointerCoercion(Unsize));
StorageDead(_3); StorageDead(_3);
- _0 = Len((*_2)); - _0 = PtrMetadata(move _2);
+ _0 = const N; + _0 = const N;
goto -> bb1; goto -> bb1;
} }

View File

@ -1,5 +1,5 @@
- // MIR for `array_len_raw` before NormalizeArrayLen - // MIR for `array_len_raw` before GVN
+ // MIR for `array_len_raw` after NormalizeArrayLen + // MIR for `array_len_raw` after GVN
fn array_len_raw(_1: [u8; N]) -> usize { fn array_len_raw(_1: [u8; N]) -> usize {
debug arr => _1; debug arr => _1;
@ -18,7 +18,8 @@
} }
bb0: { bb0: {
StorageLive(_2); - StorageLive(_2);
+ nop;
StorageLive(_3); StorageLive(_3);
StorageLive(_4); StorageLive(_4);
_4 = &_1; _4 = &_1;
@ -32,7 +33,7 @@
StorageLive(_7); StorageLive(_7);
_7 = &(*_5); _7 = &(*_5);
_6 = &(*_7); _6 = &(*_7);
- _0 = Len((*_6)); - _0 = PtrMetadata(move _6);
+ _0 = const N; + _0 = const N;
goto -> bb1; goto -> bb1;
} }
@ -40,7 +41,8 @@
bb1: { bb1: {
StorageDead(_6); StorageDead(_6);
StorageDead(_5); StorageDead(_5);
StorageDead(_2); - StorageDead(_2);
+ nop;
StorageDead(_7); StorageDead(_7);
return; return;
} }

View File

@ -1,5 +1,5 @@
- // MIR for `array_len_raw` before NormalizeArrayLen - // MIR for `array_len_raw` before GVN
+ // MIR for `array_len_raw` after NormalizeArrayLen + // MIR for `array_len_raw` after GVN
fn array_len_raw(_1: [u8; N]) -> usize { fn array_len_raw(_1: [u8; N]) -> usize {
debug arr => _1; debug arr => _1;
@ -18,7 +18,8 @@
} }
bb0: { bb0: {
StorageLive(_2); - StorageLive(_2);
+ nop;
StorageLive(_3); StorageLive(_3);
StorageLive(_4); StorageLive(_4);
_4 = &_1; _4 = &_1;
@ -32,7 +33,7 @@
StorageLive(_7); StorageLive(_7);
_7 = &(*_5); _7 = &(*_5);
_6 = &(*_7); _6 = &(*_7);
- _0 = Len((*_6)); - _0 = PtrMetadata(move _6);
+ _0 = const N; + _0 = const N;
goto -> bb1; goto -> bb1;
} }
@ -40,7 +41,8 @@
bb1: { bb1: {
StorageDead(_6); StorageDead(_6);
StorageDead(_5); StorageDead(_5);
StorageDead(_2); - StorageDead(_2);
+ nop;
StorageDead(_7); StorageDead(_7);
return; return;
} }

View File

@ -1,5 +1,5 @@
- // MIR for `array_len_reborrow` before NormalizeArrayLen - // MIR for `array_len_reborrow` before GVN
+ // MIR for `array_len_reborrow` after NormalizeArrayLen + // MIR for `array_len_reborrow` after GVN
fn array_len_reborrow(_1: [u8; N]) -> usize { fn array_len_reborrow(_1: [u8; N]) -> usize {
debug arr => _1; debug arr => _1;
@ -17,7 +17,8 @@
} }
bb0: { bb0: {
StorageLive(_2); - StorageLive(_2);
+ nop;
StorageLive(_3); StorageLive(_3);
StorageLive(_4); StorageLive(_4);
_4 = &mut _1; _4 = &mut _1;
@ -29,7 +30,7 @@
_5 = &(*_2); _5 = &(*_2);
StorageLive(_6); StorageLive(_6);
_6 = &(*_5); _6 = &(*_5);
- _0 = Len((*_6)); - _0 = PtrMetadata(move _6);
+ _0 = const N; + _0 = const N;
goto -> bb1; goto -> bb1;
} }
@ -37,7 +38,8 @@
bb1: { bb1: {
StorageDead(_6); StorageDead(_6);
StorageDead(_5); StorageDead(_5);
StorageDead(_2); - StorageDead(_2);
+ nop;
return; return;
} }
} }

View File

@ -1,5 +1,5 @@
- // MIR for `array_len_reborrow` before NormalizeArrayLen - // MIR for `array_len_reborrow` before GVN
+ // MIR for `array_len_reborrow` after NormalizeArrayLen + // MIR for `array_len_reborrow` after GVN
fn array_len_reborrow(_1: [u8; N]) -> usize { fn array_len_reborrow(_1: [u8; N]) -> usize {
debug arr => _1; debug arr => _1;
@ -17,7 +17,8 @@
} }
bb0: { bb0: {
StorageLive(_2); - StorageLive(_2);
+ nop;
StorageLive(_3); StorageLive(_3);
StorageLive(_4); StorageLive(_4);
_4 = &mut _1; _4 = &mut _1;
@ -29,7 +30,7 @@
_5 = &(*_2); _5 = &(*_2);
StorageLive(_6); StorageLive(_6);
_6 = &(*_5); _6 = &(*_5);
- _0 = Len((*_6)); - _0 = PtrMetadata(move _6);
+ _0 = const N; + _0 = const N;
goto -> bb1; goto -> bb1;
} }
@ -37,7 +38,8 @@
bb1: { bb1: {
StorageDead(_6); StorageDead(_6);
StorageDead(_5); StorageDead(_5);
StorageDead(_2); - StorageDead(_2);
+ nop;
return; return;
} }
} }

View File

@ -1,20 +1,20 @@
// EMIT_MIR_FOR_EACH_PANIC_STRATEGY // EMIT_MIR_FOR_EACH_PANIC_STRATEGY
//@ test-mir-pass: NormalizeArrayLen //@ test-mir-pass: GVN
//@ compile-flags: -Zmir-enable-passes=+LowerSliceLenCalls //@ compile-flags: -Zmir-enable-passes=+LowerSliceLenCalls
// EMIT_MIR lower_array_len.array_bound.NormalizeArrayLen.diff // EMIT_MIR lower_array_len.array_bound.GVN.diff
pub fn array_bound<const N: usize>(index: usize, slice: &[u8; N]) -> u8 { pub fn array_bound<const N: usize>(index: usize, slice: &[u8; N]) -> u8 {
// CHECK-LABEL: fn array_bound( // CHECK-LABEL: fn array_bound(
// CHECK: [[len:_.*]] = const N; // CHECK: [[len:_.*]] = const N;
// CHECK: Lt(move {{_.*}}, move [[len]]); // CHECK: Lt(_1, move [[len]]);
if index < slice.len() { slice[index] } else { 42 } if index < slice.len() { slice[index] } else { 42 }
} }
// EMIT_MIR lower_array_len.array_bound_mut.NormalizeArrayLen.diff // EMIT_MIR lower_array_len.array_bound_mut.GVN.diff
pub fn array_bound_mut<const N: usize>(index: usize, slice: &mut [u8; N]) -> u8 { pub fn array_bound_mut<const N: usize>(index: usize, slice: &mut [u8; N]) -> u8 {
// CHECK-LABEL: fn array_bound_mut( // CHECK-LABEL: fn array_bound_mut(
// CHECK: [[len:_.*]] = const N; // CHECK: [[len:_.*]] = const N;
// CHECK: Lt(move {{_.*}}, move [[len]]); // CHECK: Lt(_1, move [[len]]);
if index < slice.len() { if index < slice.len() {
slice[index] slice[index]
} else { } else {
@ -24,21 +24,21 @@ pub fn array_bound_mut<const N: usize>(index: usize, slice: &mut [u8; N]) -> u8
} }
} }
// EMIT_MIR lower_array_len.array_len.NormalizeArrayLen.diff // EMIT_MIR lower_array_len.array_len.GVN.diff
pub fn array_len<const N: usize>(arr: &[u8; N]) -> usize { pub fn array_len<const N: usize>(arr: &[u8; N]) -> usize {
// CHECK-LABEL: fn array_len( // CHECK-LABEL: fn array_len(
// CHECK: _0 = const N; // CHECK: _0 = const N;
arr.len() arr.len()
} }
// EMIT_MIR lower_array_len.array_len_by_value.NormalizeArrayLen.diff // EMIT_MIR lower_array_len.array_len_by_value.GVN.diff
pub fn array_len_by_value<const N: usize>(arr: [u8; N]) -> usize { pub fn array_len_by_value<const N: usize>(arr: [u8; N]) -> usize {
// CHECK-LABEL: fn array_len_by_value( // CHECK-LABEL: fn array_len_by_value(
// CHECK: _0 = const N; // CHECK: _0 = const N;
arr.len() arr.len()
} }
// EMIT_MIR lower_array_len.array_len_reborrow.NormalizeArrayLen.diff // EMIT_MIR lower_array_len.array_len_reborrow.GVN.diff
pub fn array_len_reborrow<const N: usize>(mut arr: [u8; N]) -> usize { pub fn array_len_reborrow<const N: usize>(mut arr: [u8; N]) -> usize {
// CHECK-LABEL: fn array_len_reborrow( // CHECK-LABEL: fn array_len_reborrow(
// CHECK: _0 = const N; // CHECK: _0 = const N;
@ -47,7 +47,7 @@ pub fn array_len_reborrow<const N: usize>(mut arr: [u8; N]) -> usize {
arr.len() arr.len()
} }
// EMIT_MIR lower_array_len.array_len_raw.NormalizeArrayLen.diff // EMIT_MIR lower_array_len.array_len_raw.GVN.diff
pub fn array_len_raw<const N: usize>(arr: [u8; N]) -> usize { pub fn array_len_raw<const N: usize>(arr: [u8; N]) -> usize {
// CHECK-LABEL: fn array_len_raw( // CHECK-LABEL: fn array_len_raw(
// CHECK: _0 = const N; // CHECK: _0 = const N;

View File

@ -21,7 +21,7 @@
StorageLive(_6); StorageLive(_6);
_6 = &(*_2); _6 = &(*_2);
- _5 = core::slice::<impl [u8]>::len(move _6) -> [return: bb1, unwind unreachable]; - _5 = core::slice::<impl [u8]>::len(move _6) -> [return: bb1, unwind unreachable];
+ _5 = Len((*_6)); + _5 = PtrMetadata(move _6);
+ goto -> bb1; + goto -> bb1;
} }

View File

@ -21,7 +21,7 @@
StorageLive(_6); StorageLive(_6);
_6 = &(*_2); _6 = &(*_2);
- _5 = core::slice::<impl [u8]>::len(move _6) -> [return: bb1, unwind continue]; - _5 = core::slice::<impl [u8]>::len(move _6) -> [return: bb1, unwind continue];
+ _5 = Len((*_6)); + _5 = PtrMetadata(move _6);
+ goto -> bb1; + goto -> bb1;
} }

View File

@ -19,7 +19,7 @@ pub fn slice_index_usize(slice: &[u32], index: usize) -> u32 {
// EMIT_MIR slice_index.slice_get_mut_usize.PreCodegen.after.mir // EMIT_MIR slice_index.slice_get_mut_usize.PreCodegen.after.mir
pub fn slice_get_mut_usize(slice: &mut [u32], index: usize) -> Option<&mut u32> { pub fn slice_get_mut_usize(slice: &mut [u32], index: usize) -> Option<&mut u32> {
// CHECK-LABEL: slice_get_mut_usize // CHECK-LABEL: slice_get_mut_usize
// CHECK: [[LEN:_[0-9]+]] = Len((*_1)) // CHECK: [[LEN:_[0-9]+]] = PtrMetadata(_1)
// CHECK: Lt(_2, move [[LEN]]) // CHECK: Lt(_2, move [[LEN]])
// CHECK-NOT: precondition_check // CHECK-NOT: precondition_check
slice.get_mut(index) slice.get_mut(index)

View File

@ -23,7 +23,7 @@ fn slice_get_mut_usize(_1: &mut [u32], _2: usize) -> Option<&mut u32> {
StorageLive(_7); StorageLive(_7);
StorageLive(_4); StorageLive(_4);
StorageLive(_3); StorageLive(_3);
_3 = Len((*_1)); _3 = PtrMetadata(_1);
_4 = Lt(_2, move _3); _4 = Lt(_2, move _3);
switchInt(move _4) -> [0: bb1, otherwise: bb2]; switchInt(move _4) -> [0: bb1, otherwise: bb2];
} }

View File

@ -23,7 +23,7 @@ fn slice_get_mut_usize(_1: &mut [u32], _2: usize) -> Option<&mut u32> {
StorageLive(_7); StorageLive(_7);
StorageLive(_4); StorageLive(_4);
StorageLive(_3); StorageLive(_3);
_3 = Len((*_1)); _3 = PtrMetadata(_1);
_4 = Lt(_2, move _3); _4 = Lt(_2, move _3);
switchInt(move _4) -> [0: bb1, otherwise: bb2]; switchInt(move _4) -> [0: bb1, otherwise: bb2];
} }

View File

@ -90,7 +90,7 @@ fn enumerated_loop(_1: &[T], _2: impl Fn(usize, &T)) -> () {
StorageLive(_6); StorageLive(_6);
StorageLive(_4); StorageLive(_4);
StorageLive(_5); StorageLive(_5);
_3 = Len((*_1)); _3 = PtrMetadata(_1);
_4 = &raw const (*_1); _4 = &raw const (*_1);
_5 = _4 as *const T (PtrToPtr); _5 = _4 as *const T (PtrToPtr);
_6 = NonNull::<T> { pointer: _5 }; _6 = NonNull::<T> { pointer: _5 };

View File

@ -65,7 +65,7 @@ fn enumerated_loop(_1: &[T], _2: impl Fn(usize, &T)) -> () {
StorageLive(_6); StorageLive(_6);
StorageLive(_4); StorageLive(_4);
StorageLive(_5); StorageLive(_5);
_3 = Len((*_1)); _3 = PtrMetadata(_1);
_4 = &raw const (*_1); _4 = &raw const (*_1);
_5 = _4 as *const T (PtrToPtr); _5 = _4 as *const T (PtrToPtr);
_6 = NonNull::<T> { pointer: _5 }; _6 = NonNull::<T> { pointer: _5 };

View File

@ -57,7 +57,7 @@ fn forward_loop(_1: &[T], _2: impl Fn(&T)) -> () {
StorageLive(_6); StorageLive(_6);
StorageLive(_4); StorageLive(_4);
StorageLive(_5); StorageLive(_5);
_3 = Len((*_1)); _3 = PtrMetadata(_1);
_4 = &raw const (*_1); _4 = &raw const (*_1);
_5 = _4 as *const T (PtrToPtr); _5 = _4 as *const T (PtrToPtr);
_6 = NonNull::<T> { pointer: _5 }; _6 = NonNull::<T> { pointer: _5 };

View File

@ -57,7 +57,7 @@ fn forward_loop(_1: &[T], _2: impl Fn(&T)) -> () {
StorageLive(_6); StorageLive(_6);
StorageLive(_4); StorageLive(_4);
StorageLive(_5); StorageLive(_5);
_3 = Len((*_1)); _3 = PtrMetadata(_1);
_4 = &raw const (*_1); _4 = &raw const (*_1);
_5 = _4 as *const T (PtrToPtr); _5 = _4 as *const T (PtrToPtr);
_6 = NonNull::<T> { pointer: _5 }; _6 = NonNull::<T> { pointer: _5 };

View File

@ -40,7 +40,7 @@ fn range_loop(_1: &[T], _2: impl Fn(usize, &T)) -> () {
} }
bb0: { bb0: {
_3 = Len((*_1)); _3 = PtrMetadata(_1);
StorageLive(_4); StorageLive(_4);
_4 = const 0_usize; _4 = const 0_usize;
goto -> bb1; goto -> bb1;

View File

@ -40,7 +40,7 @@ fn range_loop(_1: &[T], _2: impl Fn(usize, &T)) -> () {
} }
bb0: { bb0: {
_3 = Len((*_1)); _3 = PtrMetadata(_1);
StorageLive(_4); StorageLive(_4);
_4 = const 0_usize; _4 = const 0_usize;
goto -> bb1; goto -> bb1;

View File

@ -65,7 +65,7 @@ fn reverse_loop(_1: &[T], _2: impl Fn(&T)) -> () {
StorageLive(_6); StorageLive(_6);
StorageLive(_4); StorageLive(_4);
StorageLive(_5); StorageLive(_5);
_3 = Len((*_1)); _3 = PtrMetadata(_1);
_4 = &raw const (*_1); _4 = &raw const (*_1);
_5 = _4 as *const T (PtrToPtr); _5 = _4 as *const T (PtrToPtr);
_6 = NonNull::<T> { pointer: _5 }; _6 = NonNull::<T> { pointer: _5 };

View File

@ -65,7 +65,7 @@ fn reverse_loop(_1: &[T], _2: impl Fn(&T)) -> () {
StorageLive(_6); StorageLive(_6);
StorageLive(_4); StorageLive(_4);
StorageLive(_5); StorageLive(_5);
_3 = Len((*_1)); _3 = PtrMetadata(_1);
_4 = &raw const (*_1); _4 = &raw const (*_1);
_5 = _4 as *const T (PtrToPtr); _5 = _4 as *const T (PtrToPtr);
_6 = NonNull::<T> { pointer: _5 }; _6 = NonNull::<T> { pointer: _5 };