rustc_mir_transform/
copy_prop.rs1use rustc_index::IndexSlice;
2use rustc_index::bit_set::DenseBitSet;
3use rustc_middle::mir::visit::*;
4use rustc_middle::mir::*;
5use rustc_middle::ty::TyCtxt;
6use tracing::{debug, instrument};
7
8use crate::ssa::SsaLocals;
9
10pub(super) struct CopyProp;
21
22impl<'tcx> crate::MirPass<'tcx> for CopyProp {
23 fn is_enabled(&self, sess: &rustc_session::Session) -> bool {
24 sess.mir_opt_level() >= 1
25 }
26
27 #[instrument(level = "trace", skip(self, tcx, body))]
28 fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
29 debug!(def_id = ?body.source.def_id());
30
31 let typing_env = body.typing_env(tcx);
32 let ssa = SsaLocals::new(tcx, body, typing_env);
33 debug!(borrowed_locals = ?ssa.borrowed_locals());
34 debug!(copy_classes = ?ssa.copy_classes());
35
36 let fully_moved = fully_moved_locals(&ssa, body);
37 debug!(?fully_moved);
38
39 let mut storage_to_remove = DenseBitSet::new_empty(fully_moved.domain_size());
40 for (local, &head) in ssa.copy_classes().iter_enumerated() {
41 if local != head {
42 storage_to_remove.insert(head);
43 }
44 }
45
46 let any_replacement = ssa.copy_classes().iter_enumerated().any(|(l, &h)| l != h);
47
48 Replacer { tcx, copy_classes: ssa.copy_classes(), fully_moved, storage_to_remove }
49 .visit_body_preserves_cfg(body);
50
51 if any_replacement {
52 crate::simplify::remove_unused_definitions(body);
53 }
54 }
55
56 fn is_required(&self) -> bool {
57 false
58 }
59}
60
61#[instrument(level = "trace", skip(ssa, body))]
71fn fully_moved_locals(ssa: &SsaLocals, body: &Body<'_>) -> DenseBitSet<Local> {
72 let mut fully_moved = DenseBitSet::new_filled(body.local_decls.len());
73
74 for (_, rvalue, _) in ssa.assignments(body) {
75 let (Rvalue::Use(Operand::Copy(place) | Operand::Move(place))
76 | Rvalue::CopyForDeref(place)) = rvalue
77 else {
78 continue;
79 };
80
81 let Some(rhs) = place.as_local() else { continue };
82 if !ssa.is_ssa(rhs) {
83 continue;
84 }
85
86 if let Rvalue::Use(Operand::Copy(_)) | Rvalue::CopyForDeref(_) = rvalue {
87 fully_moved.remove(rhs);
88 }
89 }
90
91 ssa.meet_copy_equivalence(&mut fully_moved);
92
93 fully_moved
94}
95
96struct Replacer<'a, 'tcx> {
98 tcx: TyCtxt<'tcx>,
99 fully_moved: DenseBitSet<Local>,
100 storage_to_remove: DenseBitSet<Local>,
101 copy_classes: &'a IndexSlice<Local, Local>,
102}
103
104impl<'tcx> MutVisitor<'tcx> for Replacer<'_, 'tcx> {
105 fn tcx(&self) -> TyCtxt<'tcx> {
106 self.tcx
107 }
108
109 #[tracing::instrument(level = "trace", skip(self))]
110 fn visit_local(&mut self, local: &mut Local, ctxt: PlaceContext, _: Location) {
111 let new_local = self.copy_classes[*local];
112 match ctxt {
113 PlaceContext::NonUse(NonUseContext::StorageLive | NonUseContext::StorageDead) => {}
115 _ => *local = new_local,
117 }
118 }
119
120 #[tracing::instrument(level = "trace", skip(self))]
121 fn visit_operand(&mut self, operand: &mut Operand<'tcx>, loc: Location) {
122 if let Operand::Move(place) = *operand
123 && !place.is_indirect_first_projection()
126 && !self.fully_moved.contains(place.local)
127 {
128 *operand = Operand::Copy(place);
129 }
130 self.super_operand(operand, loc);
131 }
132
133 #[tracing::instrument(level = "trace", skip(self))]
134 fn visit_statement(&mut self, stmt: &mut Statement<'tcx>, loc: Location) {
135 if let StatementKind::StorageLive(l) | StatementKind::StorageDead(l) = stmt.kind
137 && self.storage_to_remove.contains(l)
138 {
139 stmt.make_nop();
140 return;
141 }
142
143 self.super_statement(stmt, loc);
144
145 if let StatementKind::Assign(box (lhs, ref rhs)) = stmt.kind
147 && let Rvalue::Use(Operand::Copy(rhs) | Operand::Move(rhs)) | Rvalue::CopyForDeref(rhs) =
148 *rhs
149 && lhs == rhs
150 {
151 stmt.make_nop();
152 }
153 }
154}