rustc_mir_transform/
ssa.rs

1//! We denote as "SSA" the set of locals that verify the following properties:
2//! 1/ They are only assigned-to once, either as a function parameter, or in an assign statement;
3//! 2/ This single assignment dominates all uses;
4//!
5//! As we do not track indirect assignments, a local that has its address taken (via a borrow or raw
6//! borrow operator) is considered non-SSA. However, it is UB to modify through an immutable borrow
7//! of a `Freeze` local. Those can still be considered to be SSA.
8
9use 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 to each local. This defines whether the local is SSA.
21    assignments: IndexVec<Local, Set1<DefLocation>>,
22    /// We visit the body in reverse postorder, to ensure each local is assigned before it is used.
23    /// We remember the order in which we saw the assignments to compute the SSA values in a single
24    /// pass.
25    assignment_order: Vec<Local>,
26    /// Copy equivalence classes between locals. See `copy_classes` for documentation.
27    copy_classes: IndexVec<Local, Local>,
28    /// Number of "direct" uses of each local, ie. uses that are not dereferences.
29    /// We ignore non-uses (Storage statements, debuginfo).
30    direct_uses: IndexVec<Local, u32>,
31    /// Set of SSA locals that are immutably borrowed.
32    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 SSA assignments, a RPO visit will see the assignment before it sees any use.
63        // We only visit reachable nodes: computing `dominates` on an unreachable node ICEs.
64        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        // The immutability of shared borrows only works on `Freeze` locals. If the visitor found
73        // borrows, we need to check the types. For raw pointers and mutable borrows, the locals
74        // have already been marked as non-SSA.
75        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            // This is filled by `compute_copy_classes`.
96            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    /// Return the number of uses if a local that are not "Deref".
115    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                // `loc` must point to a direct assignment to `local`.
140                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    /// Compute the equivalence classes for locals, based on copy statements.
150    ///
151    /// The returned vector maps each local to the one it copies. In the following case:
152    ///   _a = &mut _0
153    ///   _b = move? _a
154    ///   _c = move? _a
155    ///   _d = move? _c
156    /// We return the mapping
157    ///   _a => _a // not a copy so, represented by itself
158    ///   _b => _a
159    ///   _c => _a
160    ///   _d => _a // transitively through _c
161    ///
162    /// Exception: we do not see through the return place, as it cannot be instantiated.
163    pub(super) fn copy_classes(&self) -> &IndexSlice<Local, Local> {
164        &self.copy_classes
165    }
166
167    /// Set of SSA locals that are immutably borrowed.
168    pub(super) fn borrowed_locals(&self) -> &DenseBitSet<Local> {
169        &self.borrowed_locals
170    }
171
172    /// Make a property uniform on a copy equivalence class by removing elements.
173    pub(super) fn meet_copy_equivalence(&self, property: &mut DenseBitSet<Local>) {
174        // Consolidate to have a local iff all its copies are.
175        //
176        // `copy_classes` defines equivalence classes between locals. The `local`s that recursively
177        // move/copy the same local all have the same `head`.
178        for (local, &head) in self.copy_classes.iter_enumerated() {
179            // If any copy does not have `property`, then the head is not.
180            if !property.contains(local) {
181                property.remove(head);
182            }
183        }
184        for (local, &head) in self.copy_classes.iter_enumerated() {
185            // If any copy does not have `property`, then the head doesn't either,
186            // then no copy has `property`.
187            if !property.contains(head) {
188                property.remove(local);
189            }
190        }
191
192        // Verify that we correctly computed equivalence classes.
193        #[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    // Track locals that are immutably borrowed, so we can check their type is `Freeze` later.
207    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        // We are visiting a use that is not dominated by an assignment.
218        // Either there is a cycle involved, or we are reading for uninitialized local.
219        // Bail out.
220        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            // Anything can happen with raw pointers, so remove them.
232            PlaceContext::NonMutatingUse(NonMutatingUseContext::RawBorrow)
233            | PlaceContext::MutatingUse(_) => {
234                self.assignments[local] = Set1::Many;
235            }
236            // Immutable borrows are ok, but we need to delay a check that the type is `Freeze`.
237            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                // Only record if SSA-like, to avoid growing the vector needlessly.
274                self.assignment_order.push(local);
275            }
276        } else if place.projection.first() == Some(&PlaceElem::Deref) {
277            // Do not do anything for debuginfo.
278            if ctxt.is_use() {
279                // Only change the context if it is a real use, not a "use" in debuginfo.
280                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            // FIXME(#112651): This can be removed afterwards.
309            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        // We visit in `assignment_order`, ie. reverse post-order, so `rhs` has been
318        // visited before `local`, and we just have to copy the representing local.
319        let head = copies[rhs];
320
321        if local == RETURN_PLACE {
322            // `_0` is special, we cannot rename it. Instead, rename the class of `rhs` to
323            // `RETURN_PLACE`. This is only possible if the class head is a temporary, not an
324            // argument.
325            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    // Invariant: `copies` must point to the head of an equivalence class.
343    #[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    /// Set of "StorageLive" statements for each local.
356    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}