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 mut any_replacement = false;
37 let mut storage_to_remove = DenseBitSet::new_empty(body.local_decls.len());
38 for (local, &head) in ssa.copy_classes().iter_enumerated() {
39 if local != head {
40 any_replacement = true;
41 storage_to_remove.insert(head);
42 }
43 }
44
45 if !any_replacement {
46 return;
47 }
48
49 let fully_moved = fully_moved_locals(&ssa, body);
50 debug!(?fully_moved);
51
52 Replacer { tcx, copy_classes: ssa.copy_classes(), fully_moved, storage_to_remove }
53 .visit_body_preserves_cfg(body);
54
55 crate::simplify::remove_unused_definitions(body);
56 }
57
58 fn is_required(&self) -> bool {
59 false
60 }
61}
62
63#[instrument(level = "trace", skip(ssa, body))]
73fn fully_moved_locals(ssa: &SsaLocals, body: &Body<'_>) -> DenseBitSet<Local> {
74 let mut fully_moved = DenseBitSet::new_filled(body.local_decls.len());
75
76 for (_, rvalue, _) in ssa.assignments(body) {
77 let (Rvalue::Use(Operand::Copy(place) | Operand::Move(place))
78 | Rvalue::CopyForDeref(place)) = rvalue
79 else {
80 continue;
81 };
82
83 let Some(rhs) = place.as_local() else { continue };
84 if !ssa.is_ssa(rhs) {
85 continue;
86 }
87
88 if let Rvalue::Use(Operand::Copy(_)) | Rvalue::CopyForDeref(_) = rvalue {
89 fully_moved.remove(rhs);
90 }
91 }
92
93 ssa.meet_copy_equivalence(&mut fully_moved);
94
95 fully_moved
96}
97
98struct Replacer<'a, 'tcx> {
100 tcx: TyCtxt<'tcx>,
101 fully_moved: DenseBitSet<Local>,
102 storage_to_remove: DenseBitSet<Local>,
103 copy_classes: &'a IndexSlice<Local, Local>,
104}
105
106impl<'tcx> MutVisitor<'tcx> for Replacer<'_, 'tcx> {
107 fn tcx(&self) -> TyCtxt<'tcx> {
108 self.tcx
109 }
110
111 #[tracing::instrument(level = "trace", skip(self))]
112 fn visit_local(&mut self, local: &mut Local, ctxt: PlaceContext, _: Location) {
113 let new_local = self.copy_classes[*local];
114 match ctxt {
115 PlaceContext::NonUse(NonUseContext::StorageLive | NonUseContext::StorageDead) => {}
117 _ => *local = new_local,
119 }
120 }
121
122 #[tracing::instrument(level = "trace", skip(self))]
123 fn visit_operand(&mut self, operand: &mut Operand<'tcx>, loc: Location) {
124 if let Operand::Move(place) = *operand
125 && !place.is_indirect_first_projection()
128 && !self.fully_moved.contains(place.local)
129 {
130 *operand = Operand::Copy(place);
131 }
132 self.super_operand(operand, loc);
133 }
134
135 #[tracing::instrument(level = "trace", skip(self))]
136 fn visit_statement(&mut self, stmt: &mut Statement<'tcx>, loc: Location) {
137 if let StatementKind::StorageLive(l) | StatementKind::StorageDead(l) = stmt.kind
139 && self.storage_to_remove.contains(l)
140 {
141 stmt.make_nop();
142 return;
143 }
144
145 self.super_statement(stmt, loc);
146
147 if let StatementKind::Assign(box (lhs, ref rhs)) = stmt.kind
149 && let Rvalue::Use(Operand::Copy(rhs) | Operand::Move(rhs)) | Rvalue::CopyForDeref(rhs) =
150 *rhs
151 && lhs == rhs
152 {
153 stmt.make_nop();
154 }
155 }
156}