Do not convert copies of packed projections to moves.

This commit is contained in:
Camille GILLOT 2023-08-23 16:00:42 +00:00
parent fe5f591257
commit 1c5f1762b7
4 changed files with 60 additions and 0 deletions

View File

@ -12,6 +12,7 @@
//! will still not cause any further changes.
//!
use crate::util::is_disaligned;
use rustc_index::bit_set::BitSet;
use rustc_middle::mir::visit::Visitor;
use rustc_middle::mir::*;
@ -31,6 +32,8 @@ pub fn eliminate<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>, borrowed: &BitS
.iterate_to_fixpoint()
.into_results_cursor(body);
let param_env = tcx.param_env_reveal_all_normalized(body.source.def_id());
// For blocks with a call terminator, if an argument copy can be turned into a move,
// record it as (block, argument index).
let mut call_operands_to_move = Vec::new();
@ -49,6 +52,7 @@ pub fn eliminate<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>, borrowed: &BitS
&& !place.is_indirect()
&& !borrowed.contains(place.local)
&& !state.contains(place.local)
&& !is_disaligned(tcx, body, param_env, place)
{
call_operands_to_move.push((bb, index));
}

View File

@ -0,0 +1,15 @@
- // MIR for `move_packed` before DeadStoreElimination
+ // MIR for `move_packed` after DeadStoreElimination
fn move_packed(_1: Packed) -> () {
let mut _0: ();
bb0: {
_0 = use_both(const 0_i32, (_1.1: i32)) -> [return: bb1, unwind unreachable];
}
bb1: {
return;
}
}

View File

@ -0,0 +1,15 @@
- // MIR for `move_packed` before DeadStoreElimination
+ // MIR for `move_packed` after DeadStoreElimination
fn move_packed(_1: Packed) -> () {
let mut _0: ();
bb0: {
_0 = use_both(const 0_i32, (_1.1: i32)) -> [return: bb1, unwind continue];
}
bb1: {
return;
}
}

View File

@ -2,6 +2,12 @@
// unit-test: DeadStoreElimination
// compile-flags: -Zmir-enable-passes=+CopyProp
#![feature(core_intrinsics)]
#![feature(custom_mir)]
#![allow(internal_features)]
use std::intrinsics::mir::*;
#[inline(never)]
fn use_both(_: i32, _: i32) {}
@ -10,6 +16,26 @@ fn move_simple(x: i32) {
use_both(x, x);
}
#[repr(packed)]
struct Packed {
x: u8,
y: i32,
}
// EMIT_MIR call_arg_copy.move_packed.DeadStoreElimination.diff
#[custom_mir(dialect = "analysis")]
fn move_packed(packed: Packed) {
mir!(
{
Call(RET = use_both(0, packed.y), ret)
}
ret = {
Return()
}
)
}
fn main() {
move_simple(1);
move_packed(Packed { x: 0, y: 1 });
}