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