1use rustc_data_structures::graph::dominators::Dominators;
10use rustc_index::bit_set::DenseBitSet;
11use rustc_index::{IndexSlice, IndexVec};
12use rustc_middle::bug;
13use rustc_middle::middle::resolve_bound_vars::Set1;
14use rustc_middle::mir::visit::*;
15use rustc_middle::mir::*;
16use rustc_middle::ty::{self, TyCtxt};
17use tracing::{debug, instrument, trace};
18
19pub(super) struct SsaLocals {
20 assignments: IndexVec<Local, Set1<DefLocation>>,
22 assignment_order: Vec<Local>,
26 copy_classes: IndexVec<Local, Local>,
28 direct_uses: IndexVec<Local, u32>,
31 borrowed_locals: DenseBitSet<Local>,
33}
34
35impl SsaLocals {
36 pub(super) fn new<'tcx>(
37 tcx: TyCtxt<'tcx>,
38 body: &Body<'tcx>,
39 typing_env: ty::TypingEnv<'tcx>,
40 ) -> SsaLocals {
41 let assignment_order = Vec::with_capacity(body.local_decls.len());
42
43 let assignments = IndexVec::from_elem(Set1::Empty, &body.local_decls);
44 let dominators = body.basic_blocks.dominators();
45
46 let direct_uses = IndexVec::from_elem(0, &body.local_decls);
47 let borrowed_locals = DenseBitSet::new_empty(body.local_decls.len());
48 let mut visitor = SsaVisitor {
49 body,
50 assignments,
51 assignment_order,
52 dominators,
53 direct_uses,
54 borrowed_locals,
55 };
56
57 for local in body.args_iter() {
58 visitor.assignments[local] = Set1::One(DefLocation::Argument);
59 visitor.assignment_order.push(local);
60 }
61
62 for (bb, data) in traversal::reverse_postorder(body) {
65 visitor.visit_basic_block_data(bb, data);
66 }
67
68 for var_debug_info in &body.var_debug_info {
69 visitor.visit_var_debug_info(var_debug_info);
70 }
71
72 debug!(?visitor.borrowed_locals);
76 for local in visitor.borrowed_locals.iter() {
77 if !body.local_decls[local].ty.is_freeze(tcx, typing_env) {
78 visitor.assignments[local] = Set1::Many;
79 }
80 }
81
82 debug!(?visitor.assignments);
83 debug!(?visitor.direct_uses);
84
85 visitor
86 .assignment_order
87 .retain(|&local| matches!(visitor.assignments[local], Set1::One(_)));
88 debug!(?visitor.assignment_order);
89
90 let mut ssa = SsaLocals {
91 assignments: visitor.assignments,
92 assignment_order: visitor.assignment_order,
93 direct_uses: visitor.direct_uses,
94 borrowed_locals: visitor.borrowed_locals,
95 copy_classes: IndexVec::default(),
97 };
98 compute_copy_classes(&mut ssa, body);
99 ssa
100 }
101
102 pub(super) fn num_locals(&self) -> usize {
103 self.assignments.len()
104 }
105
106 pub(super) fn locals(&self) -> impl Iterator<Item = Local> {
107 self.assignments.indices()
108 }
109
110 pub(super) fn is_ssa(&self, local: Local) -> bool {
111 matches!(self.assignments[local], Set1::One(_))
112 }
113
114 pub(super) fn num_direct_uses(&self, local: Local) -> u32 {
116 self.direct_uses[local]
117 }
118
119 #[inline]
120 pub(super) fn assignment_dominates(
121 &self,
122 dominators: &Dominators<BasicBlock>,
123 local: Local,
124 location: Location,
125 ) -> bool {
126 match self.assignments[local] {
127 Set1::One(def) => def.dominates(location, dominators),
128 _ => false,
129 }
130 }
131
132 pub(super) fn assignments<'a, 'tcx>(
133 &'a self,
134 body: &'a Body<'tcx>,
135 ) -> impl Iterator<Item = (Local, &'a Rvalue<'tcx>, Location)> {
136 self.assignment_order.iter().filter_map(|&local| {
137 if let Set1::One(DefLocation::Assignment(loc)) = self.assignments[local] {
138 let stmt = body.stmt_at(loc).left()?;
139 let Some((target, rvalue)) = stmt.kind.as_assign() else { bug!() };
141 assert_eq!(target.as_local(), Some(local));
142 Some((local, rvalue, loc))
143 } else {
144 None
145 }
146 })
147 }
148
149 pub(super) fn copy_classes(&self) -> &IndexSlice<Local, Local> {
164 &self.copy_classes
165 }
166
167 pub(super) fn borrowed_locals(&self) -> &DenseBitSet<Local> {
169 &self.borrowed_locals
170 }
171
172 pub(super) fn meet_copy_equivalence(&self, property: &mut DenseBitSet<Local>) {
174 for (local, &head) in self.copy_classes.iter_enumerated() {
179 if !property.contains(local) {
181 property.remove(head);
182 }
183 }
184 for (local, &head) in self.copy_classes.iter_enumerated() {
185 if !property.contains(head) {
188 property.remove(local);
189 }
190 }
191
192 #[cfg(debug_assertions)]
194 for (local, &head) in self.copy_classes.iter_enumerated() {
195 assert_eq!(property.contains(local), property.contains(head));
196 }
197 }
198}
199
200struct SsaVisitor<'a, 'tcx> {
201 body: &'a Body<'tcx>,
202 dominators: &'a Dominators<BasicBlock>,
203 assignments: IndexVec<Local, Set1<DefLocation>>,
204 assignment_order: Vec<Local>,
205 direct_uses: IndexVec<Local, u32>,
206 borrowed_locals: DenseBitSet<Local>,
208}
209
210impl SsaVisitor<'_, '_> {
211 fn check_dominates(&mut self, local: Local, loc: Location) {
212 let set = &mut self.assignments[local];
213 let assign_dominates = match *set {
214 Set1::Empty | Set1::Many => false,
215 Set1::One(def) => def.dominates(loc, self.dominators),
216 };
217 if !assign_dominates {
221 *set = Set1::Many;
222 }
223 }
224}
225
226impl<'tcx> Visitor<'tcx> for SsaVisitor<'_, 'tcx> {
227 fn visit_local(&mut self, local: Local, ctxt: PlaceContext, loc: Location) {
228 match ctxt {
229 PlaceContext::MutatingUse(MutatingUseContext::Projection)
230 | PlaceContext::NonMutatingUse(NonMutatingUseContext::Projection) => bug!(),
231 PlaceContext::NonMutatingUse(NonMutatingUseContext::RawBorrow)
233 | PlaceContext::MutatingUse(_) => {
234 self.assignments[local] = Set1::Many;
235 }
236 PlaceContext::NonMutatingUse(
238 NonMutatingUseContext::SharedBorrow | NonMutatingUseContext::FakeBorrow,
239 ) => {
240 self.borrowed_locals.insert(local);
241 self.check_dominates(local, loc);
242 self.direct_uses[local] += 1;
243 }
244 PlaceContext::NonMutatingUse(_) => {
245 self.check_dominates(local, loc);
246 self.direct_uses[local] += 1;
247 }
248 PlaceContext::NonUse(_) => {}
249 }
250 }
251
252 fn visit_place(&mut self, place: &Place<'tcx>, ctxt: PlaceContext, loc: Location) {
253 let location = match ctxt {
254 PlaceContext::MutatingUse(MutatingUseContext::Store) => {
255 Some(DefLocation::Assignment(loc))
256 }
257 PlaceContext::MutatingUse(MutatingUseContext::Call) => {
258 let call = loc.block;
259 let TerminatorKind::Call { target, .. } =
260 self.body.basic_blocks[call].terminator().kind
261 else {
262 bug!()
263 };
264 Some(DefLocation::CallReturn { call, target })
265 }
266 _ => None,
267 };
268 if let Some(location) = location
269 && let Some(local) = place.as_local()
270 {
271 self.assignments[local].insert(location);
272 if let Set1::One(_) = self.assignments[local] {
273 self.assignment_order.push(local);
275 }
276 } else if place.projection.first() == Some(&PlaceElem::Deref) {
277 if ctxt.is_use() {
279 let new_ctxt = PlaceContext::NonMutatingUse(NonMutatingUseContext::Copy);
281
282 self.visit_projection(place.as_ref(), new_ctxt, loc);
283 self.check_dominates(place.local, loc);
284 }
285 } else {
286 self.visit_projection(place.as_ref(), ctxt, loc);
287 self.visit_local(place.local, ctxt, loc);
288 }
289 }
290}
291
292#[instrument(level = "trace", skip(ssa, body))]
293fn compute_copy_classes(ssa: &mut SsaLocals, body: &Body<'_>) {
294 let mut direct_uses = std::mem::take(&mut ssa.direct_uses);
295 let mut copies = IndexVec::from_fn_n(|l| l, body.local_decls.len());
296
297 for (local, rvalue, _) in ssa.assignments(body) {
298 let (Rvalue::Use(Operand::Copy(place) | Operand::Move(place))
299 | Rvalue::CopyForDeref(place)) = rvalue
300 else {
301 continue;
302 };
303
304 let Some(rhs) = place.as_local() else { continue };
305 let local_ty = body.local_decls()[local].ty;
306 let rhs_ty = body.local_decls()[rhs].ty;
307 if local_ty != rhs_ty {
308 trace!("skipped `{local:?} = {rhs:?}` due to subtyping: {local_ty} != {rhs_ty}");
310 continue;
311 }
312
313 if !ssa.is_ssa(rhs) {
314 continue;
315 }
316
317 let head = copies[rhs];
320
321 if local == RETURN_PLACE {
322 if body.local_kind(head) != LocalKind::Temp {
326 continue;
327 }
328 for h in copies.iter_mut() {
329 if *h == head {
330 *h = RETURN_PLACE;
331 }
332 }
333 } else {
334 copies[local] = head;
335 }
336 direct_uses[rhs] -= 1;
337 }
338
339 debug!(?copies);
340 debug!(?direct_uses);
341
342 #[cfg(debug_assertions)]
344 for &head in copies.iter() {
345 assert_eq!(copies[head], head);
346 }
347 debug_assert_eq!(copies[RETURN_PLACE], RETURN_PLACE);
348
349 ssa.direct_uses = direct_uses;
350 ssa.copy_classes = copies;
351}
352
353#[derive(Debug)]
354pub(crate) struct StorageLiveLocals {
355 storage_live: IndexVec<Local, Set1<DefLocation>>,
357}
358
359impl StorageLiveLocals {
360 pub(crate) fn new(
361 body: &Body<'_>,
362 always_storage_live_locals: &DenseBitSet<Local>,
363 ) -> StorageLiveLocals {
364 let mut storage_live = IndexVec::from_elem(Set1::Empty, &body.local_decls);
365 for local in always_storage_live_locals.iter() {
366 storage_live[local] = Set1::One(DefLocation::Argument);
367 }
368 for (block, bbdata) in body.basic_blocks.iter_enumerated() {
369 for (statement_index, statement) in bbdata.statements.iter().enumerate() {
370 if let StatementKind::StorageLive(local) = statement.kind {
371 storage_live[local]
372 .insert(DefLocation::Assignment(Location { block, statement_index }));
373 }
374 }
375 }
376 debug!(?storage_live);
377 StorageLiveLocals { storage_live }
378 }
379
380 #[inline]
381 pub(crate) fn has_single_storage(&self, local: Local) -> bool {
382 matches!(self.storage_live[local], Set1::One(_))
383 }
384}