2023-04-19 10:57:17 +00:00
|
|
|
use rustc_index::IndexSlice;
|
2025-01-07 15:19:05 +00:00
|
|
|
use rustc_index::bit_set::DenseBitSet;
|
2023-01-12 18:23:48 +00:00
|
|
|
use rustc_middle::mir::visit::*;
|
|
|
|
use rustc_middle::mir::*;
|
2023-01-19 22:23:41 +00:00
|
|
|
use rustc_middle::ty::TyCtxt;
|
2024-08-28 05:03:14 +00:00
|
|
|
use tracing::{debug, instrument};
|
2023-01-12 18:23:48 +00:00
|
|
|
|
2023-01-19 22:23:41 +00:00
|
|
|
use crate::ssa::SsaLocals;
|
2023-01-12 18:23:48 +00:00
|
|
|
|
|
|
|
/// Unify locals that copy each other.
|
|
|
|
///
|
|
|
|
/// We consider patterns of the form
|
|
|
|
/// _a = rvalue
|
|
|
|
/// _b = move? _a
|
|
|
|
/// _c = move? _a
|
|
|
|
/// _d = move? _c
|
|
|
|
/// where each of the locals is only assigned once.
|
|
|
|
///
|
|
|
|
/// We want to replace all those locals by `_a`, either copied or moved.
|
2024-08-27 23:39:59 +00:00
|
|
|
pub(super) struct CopyProp;
|
2023-01-12 18:23:48 +00:00
|
|
|
|
2024-09-03 05:45:27 +00:00
|
|
|
impl<'tcx> crate::MirPass<'tcx> for CopyProp {
|
2023-01-12 18:23:48 +00:00
|
|
|
fn is_enabled(&self, sess: &rustc_session::Session) -> bool {
|
2023-01-29 18:50:33 +00:00
|
|
|
sess.mir_opt_level() >= 1
|
2023-01-12 18:23:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[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());
|
|
|
|
|
2024-11-19 15:13:55 +00:00
|
|
|
let typing_env = body.typing_env(tcx);
|
|
|
|
let ssa = SsaLocals::new(tcx, body, typing_env);
|
2023-01-12 18:23:48 +00:00
|
|
|
|
2024-08-28 00:13:17 +00:00
|
|
|
let fully_moved = fully_moved_locals(&ssa, body);
|
|
|
|
debug!(?fully_moved);
|
2023-01-12 18:23:48 +00:00
|
|
|
|
2025-01-07 15:19:05 +00:00
|
|
|
let mut storage_to_remove = DenseBitSet::new_empty(fully_moved.domain_size());
|
2024-08-28 00:13:17 +00:00
|
|
|
for (local, &head) in ssa.copy_classes().iter_enumerated() {
|
|
|
|
if local != head {
|
|
|
|
storage_to_remove.insert(head);
|
|
|
|
}
|
2023-01-12 18:23:48 +00:00
|
|
|
}
|
|
|
|
|
2024-08-28 00:13:17 +00:00
|
|
|
let any_replacement = ssa.copy_classes().iter_enumerated().any(|(l, &h)| l != h);
|
2023-01-12 18:23:48 +00:00
|
|
|
|
2024-08-28 00:13:17 +00:00
|
|
|
Replacer {
|
|
|
|
tcx,
|
|
|
|
copy_classes: ssa.copy_classes(),
|
|
|
|
fully_moved,
|
|
|
|
borrowed_locals: ssa.borrowed_locals(),
|
|
|
|
storage_to_remove,
|
|
|
|
}
|
|
|
|
.visit_body_preserves_cfg(body);
|
2023-01-12 18:23:48 +00:00
|
|
|
|
2024-08-28 00:13:17 +00:00
|
|
|
if any_replacement {
|
|
|
|
crate::simplify::remove_unused_definitions(body);
|
|
|
|
}
|
2023-01-12 18:23:48 +00:00
|
|
|
}
|
2024-12-09 19:34:51 +00:00
|
|
|
|
|
|
|
fn is_required(&self) -> bool {
|
|
|
|
false
|
|
|
|
}
|
2023-01-12 18:23:48 +00:00
|
|
|
}
|
|
|
|
|
2023-01-19 22:23:41 +00:00
|
|
|
/// `SsaLocals` computed equivalence classes between locals considering copy/move assignments.
|
2023-01-12 18:23:48 +00:00
|
|
|
///
|
|
|
|
/// This function also returns whether all the `move?` in the pattern are `move` and not copies.
|
2023-01-19 16:57:32 +00:00
|
|
|
/// A local which is in the bitset can be replaced by `move _a`. Otherwise, it must be
|
2023-01-12 18:23:48 +00:00
|
|
|
/// replaced by `copy _a`, as we cannot move multiple times from `_a`.
|
|
|
|
///
|
|
|
|
/// If an operand copies `_c`, it must happen before the assignment `_d = _c`, otherwise it is UB.
|
|
|
|
/// This means that replacing it by a copy of `_a` if ok, since this copy happens before `_c` is
|
|
|
|
/// moved, and therefore that `_d` is moved.
|
|
|
|
#[instrument(level = "trace", skip(ssa, body))]
|
2025-01-07 15:19:05 +00:00
|
|
|
fn fully_moved_locals(ssa: &SsaLocals, body: &Body<'_>) -> DenseBitSet<Local> {
|
|
|
|
let mut fully_moved = DenseBitSet::new_filled(body.local_decls.len());
|
2023-01-12 18:23:48 +00:00
|
|
|
|
2022-12-04 18:26:09 +00:00
|
|
|
for (_, rvalue, _) in ssa.assignments(body) {
|
2023-01-12 18:23:48 +00:00
|
|
|
let (Rvalue::Use(Operand::Copy(place) | Operand::Move(place))
|
|
|
|
| Rvalue::CopyForDeref(place)) = rvalue
|
|
|
|
else {
|
|
|
|
continue;
|
|
|
|
};
|
|
|
|
|
|
|
|
let Some(rhs) = place.as_local() else { continue };
|
2023-01-19 22:23:41 +00:00
|
|
|
if !ssa.is_ssa(rhs) {
|
|
|
|
continue;
|
|
|
|
}
|
2023-01-12 18:23:48 +00:00
|
|
|
|
|
|
|
if let Rvalue::Use(Operand::Copy(_)) | Rvalue::CopyForDeref(_) = rvalue {
|
|
|
|
fully_moved.remove(rhs);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-19 22:23:41 +00:00
|
|
|
ssa.meet_copy_equivalence(&mut fully_moved);
|
2023-01-12 18:23:48 +00:00
|
|
|
|
2023-01-19 22:23:41 +00:00
|
|
|
fully_moved
|
2023-01-12 18:23:48 +00:00
|
|
|
}
|
|
|
|
|
2023-01-29 18:50:33 +00:00
|
|
|
/// Utility to help performing substitution of `*pattern` by `target`.
|
2023-01-19 22:23:41 +00:00
|
|
|
struct Replacer<'a, 'tcx> {
|
2023-01-12 18:23:48 +00:00
|
|
|
tcx: TyCtxt<'tcx>,
|
2025-01-07 15:19:05 +00:00
|
|
|
fully_moved: DenseBitSet<Local>,
|
|
|
|
storage_to_remove: DenseBitSet<Local>,
|
|
|
|
borrowed_locals: &'a DenseBitSet<Local>,
|
2023-03-31 07:32:44 +00:00
|
|
|
copy_classes: &'a IndexSlice<Local, Local>,
|
2023-01-12 18:23:48 +00:00
|
|
|
}
|
|
|
|
|
2023-01-19 22:23:41 +00:00
|
|
|
impl<'tcx> MutVisitor<'tcx> for Replacer<'_, 'tcx> {
|
2023-01-12 18:23:48 +00:00
|
|
|
fn tcx(&self) -> TyCtxt<'tcx> {
|
|
|
|
self.tcx
|
|
|
|
}
|
|
|
|
|
2023-01-20 19:34:46 +00:00
|
|
|
fn visit_local(&mut self, local: &mut Local, ctxt: PlaceContext, _: Location) {
|
|
|
|
let new_local = self.copy_classes[*local];
|
2024-01-02 23:04:52 +00:00
|
|
|
// We must not unify two locals that are borrowed. But this is fine if one is borrowed and
|
|
|
|
// the other is not. We chose to check the original local, and not the target. That way, if
|
|
|
|
// the original local is borrowed and the target is not, we do not pessimize the whole class.
|
|
|
|
if self.borrowed_locals.contains(*local) {
|
2023-12-30 19:04:26 +00:00
|
|
|
return;
|
|
|
|
}
|
2023-01-20 19:34:46 +00:00
|
|
|
match ctxt {
|
|
|
|
// Do not modify the local in storage statements.
|
|
|
|
PlaceContext::NonUse(NonUseContext::StorageLive | NonUseContext::StorageDead) => {}
|
|
|
|
// The local should have been marked as non-SSA.
|
|
|
|
PlaceContext::MutatingUse(_) => assert_eq!(*local, new_local),
|
|
|
|
// We access the value.
|
|
|
|
_ => *local = new_local,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-30 19:04:26 +00:00
|
|
|
fn visit_place(&mut self, place: &mut Place<'tcx>, _: PlaceContext, loc: Location) {
|
2023-11-21 19:07:32 +00:00
|
|
|
if let Some(new_projection) = self.process_projection(place.projection, loc) {
|
2023-02-17 03:33:08 +00:00
|
|
|
place.projection = self.tcx().mk_place_elems(&new_projection);
|
2023-01-20 19:34:46 +00:00
|
|
|
}
|
|
|
|
|
2023-12-30 19:04:26 +00:00
|
|
|
// Any non-mutating use context is ok.
|
|
|
|
let ctxt = PlaceContext::NonMutatingUse(NonMutatingUseContext::Copy);
|
|
|
|
self.visit_local(&mut place.local, ctxt, loc)
|
2023-01-12 18:23:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_operand(&mut self, operand: &mut Operand<'tcx>, loc: Location) {
|
|
|
|
if let Operand::Move(place) = *operand
|
2024-08-27 22:24:10 +00:00
|
|
|
// A move out of a projection of a copy is equivalent to a copy of the original
|
|
|
|
// projection.
|
2023-08-05 09:02:39 +00:00
|
|
|
&& !place.is_indirect_first_projection()
|
2023-02-04 11:48:28 +00:00
|
|
|
&& !self.fully_moved.contains(place.local)
|
2023-01-12 18:23:48 +00:00
|
|
|
{
|
|
|
|
*operand = Operand::Copy(place);
|
|
|
|
}
|
|
|
|
self.super_operand(operand, loc);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_statement(&mut self, stmt: &mut Statement<'tcx>, loc: Location) {
|
2023-05-14 11:25:47 +00:00
|
|
|
// When removing storage statements, we need to remove both (#107511).
|
|
|
|
if let StatementKind::StorageLive(l) | StatementKind::StorageDead(l) = stmt.kind
|
|
|
|
&& self.storage_to_remove.contains(l)
|
|
|
|
{
|
|
|
|
stmt.make_nop();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
self.super_statement(stmt, loc);
|
|
|
|
|
|
|
|
// Do not leave tautological assignments around.
|
|
|
|
if let StatementKind::Assign(box (lhs, ref rhs)) = stmt.kind
|
|
|
|
&& let Rvalue::Use(Operand::Copy(rhs) | Operand::Move(rhs)) | Rvalue::CopyForDeref(rhs) =
|
|
|
|
*rhs
|
|
|
|
&& lhs == rhs
|
|
|
|
{
|
|
|
|
stmt.make_nop();
|
2023-01-12 18:23:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|