rustc_mir_transform/
ref_prop.rs

1use std::borrow::Cow;
2
3use rustc_data_structures::fx::FxHashSet;
4use rustc_index::IndexVec;
5use rustc_index::bit_set::DenseBitSet;
6use rustc_middle::bug;
7use rustc_middle::mir::visit::*;
8use rustc_middle::mir::*;
9use rustc_middle::ty::TyCtxt;
10use rustc_mir_dataflow::Analysis;
11use rustc_mir_dataflow::impls::{MaybeStorageDead, always_storage_live_locals};
12use tracing::{debug, instrument};
13
14use crate::ssa::{SsaLocals, StorageLiveLocals};
15
16/// Propagate references using SSA analysis.
17///
18/// MIR building may produce a lot of borrow-dereference patterns.
19///
20/// This pass aims to transform the following pattern:
21///   _1 = &raw? mut? PLACE;
22///   _3 = *_1;
23///   _4 = &raw? mut? *_1;
24///
25/// Into
26///   _1 = &raw? mut? PLACE;
27///   _3 = PLACE;
28///   _4 = &raw? mut? PLACE;
29///
30/// where `PLACE` is a direct or an indirect place expression.
31///
32/// There are 3 properties that need to be upheld for this transformation to be legal:
33/// - place stability: `PLACE` must refer to the same memory wherever it appears;
34/// - pointer liveness: we must not introduce dereferences of dangling pointers;
35/// - `&mut` borrow uniqueness.
36///
37/// # Stability
38///
39/// If `PLACE` is an indirect projection, if its of the form `(*LOCAL).PROJECTIONS` where:
40/// - `LOCAL` is SSA;
41/// - all projections in `PROJECTIONS` have a stable offset (no dereference and no indexing).
42///
43/// If `PLACE` is a direct projection of a local, we consider it as constant if:
44/// - the local is always live, or it has a single `StorageLive`;
45/// - all projections have a stable offset.
46///
47/// # Liveness
48///
49/// When performing an instantiation, we must take care not to introduce uses of dangling locals.
50/// To ensure this, we walk the body with the `MaybeStorageDead` dataflow analysis:
51/// - if we want to replace `*x` by reborrow `*y` and `y` may be dead, we allow replacement and
52///   mark storage statements on `y` for removal;
53/// - if we want to replace `*x` by non-reborrow `y` and `y` must be live, we allow replacement;
54/// - if we want to replace `*x` by non-reborrow `y` and `y` may be dead, we do not replace.
55///
56/// # Uniqueness
57///
58/// For `&mut` borrows, we also need to preserve the uniqueness property:
59/// we must avoid creating a state where we interleave uses of `*_1` and `_2`.
60/// To do it, we only perform full instantiation of mutable borrows:
61/// we replace either all or none of the occurrences of `*_1`.
62///
63/// Some care has to be taken when `_1` is copied in other locals.
64///   _1 = &raw? mut? _2;
65///   _3 = *_1;
66///   _4 = _1
67///   _5 = *_4
68/// In such cases, fully instantiating `_1` means fully instantiating all of the copies.
69///
70/// For immutable borrows, we do not need to preserve such uniqueness property,
71/// so we perform all the possible instantiations without removing the `_1 = &_2` statement.
72pub(super) struct ReferencePropagation;
73
74impl<'tcx> crate::MirPass<'tcx> for ReferencePropagation {
75    fn is_enabled(&self, sess: &rustc_session::Session) -> bool {
76        sess.mir_opt_level() >= 2
77    }
78
79    #[instrument(level = "trace", skip(self, tcx, body))]
80    fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
81        debug!(def_id = ?body.source.def_id());
82        while propagate_ssa(tcx, body) {}
83    }
84
85    fn is_required(&self) -> bool {
86        false
87    }
88}
89
90fn propagate_ssa<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) -> bool {
91    let typing_env = body.typing_env(tcx);
92    let ssa = SsaLocals::new(tcx, body, typing_env);
93
94    let mut replacer = compute_replacement(tcx, body, &ssa);
95    debug!(?replacer.targets);
96    debug!(?replacer.allowed_replacements);
97    debug!(?replacer.storage_to_remove);
98
99    replacer.visit_body_preserves_cfg(body);
100
101    if replacer.any_replacement {
102        crate::simplify::remove_unused_definitions(body);
103    }
104
105    replacer.any_replacement
106}
107
108#[derive(Copy, Clone, Debug, PartialEq, Eq)]
109enum Value<'tcx> {
110    /// Not a pointer, or we can't know.
111    Unknown,
112    /// We know the value to be a pointer to this place.
113    /// The boolean indicates whether the reference is mutable, subject the uniqueness rule.
114    Pointer(Place<'tcx>, bool),
115}
116
117/// For each local, save the place corresponding to `*local`.
118#[instrument(level = "trace", skip(tcx, body, ssa))]
119fn compute_replacement<'tcx>(
120    tcx: TyCtxt<'tcx>,
121    body: &Body<'tcx>,
122    ssa: &SsaLocals,
123) -> Replacer<'tcx> {
124    let always_live_locals = always_storage_live_locals(body);
125
126    // Compute which locals have a single `StorageLive` statement ever.
127    let storage_live = StorageLiveLocals::new(body, &always_live_locals);
128
129    // Compute `MaybeStorageDead` dataflow to check that we only replace when the pointee is
130    // definitely live.
131    let mut maybe_dead = MaybeStorageDead::new(Cow::Owned(always_live_locals))
132        .iterate_to_fixpoint(tcx, body, None)
133        .into_results_cursor(body);
134
135    // Map for each local to the pointee.
136    let mut targets = IndexVec::from_elem(Value::Unknown, &body.local_decls);
137    // Set of locals for which we will remove their storage statement. This is useful for
138    // reborrowed references.
139    let mut storage_to_remove = DenseBitSet::new_empty(body.local_decls.len());
140
141    let fully_replacable_locals = fully_replacable_locals(ssa);
142
143    // Returns true iff we can use `place` as a pointee.
144    //
145    // Note that we only need to verify that there is a single `StorageLive` statement, and we do
146    // not need to verify that it dominates all uses of that local.
147    //
148    // Consider the three statements:
149    //   SL : StorageLive(a)
150    //   DEF: b = &raw? mut? a
151    //   USE: stuff that uses *b
152    //
153    // First, we recall that DEF is checked to dominate USE. Now imagine for the sake of
154    // contradiction there is a DEF -> SL -> USE path. Consider two cases:
155    //
156    // - DEF dominates SL. We always have UB the first time control flow reaches DEF,
157    //   because the storage of `a` is dead. Since DEF dominates USE, that means we cannot
158    //   reach USE and so our optimization is ok.
159    //
160    // - DEF does not dominate SL. Then there is a `START_BLOCK -> SL` path not including DEF.
161    //   But we can extend this path to USE, meaning there is also a `START_BLOCK -> USE` path not
162    //   including DEF. This violates the DEF dominates USE condition, and so is impossible.
163    let is_constant_place = |place: Place<'_>| {
164        // We only allow `Deref` as the first projection, to avoid surprises.
165        if place.projection.first() == Some(&PlaceElem::Deref) {
166            // `place == (*some_local).xxx`, it is constant only if `some_local` is constant.
167            // We approximate constness using SSAness.
168            ssa.is_ssa(place.local) && place.projection[1..].iter().all(PlaceElem::is_stable_offset)
169        } else {
170            storage_live.has_single_storage(place.local)
171                && place.projection[..].iter().all(PlaceElem::is_stable_offset)
172        }
173    };
174
175    let mut can_perform_opt = |target: Place<'tcx>, loc: Location| {
176        if target.projection.first() == Some(&PlaceElem::Deref) {
177            // We are creating a reborrow. As `place.local` is a reference, removing the storage
178            // statements should not make it much harder for LLVM to optimize.
179            storage_to_remove.insert(target.local);
180            true
181        } else {
182            // This is a proper dereference. We can only allow it if `target` is live.
183            maybe_dead.seek_after_primary_effect(loc);
184            let maybe_dead = maybe_dead.get().contains(target.local);
185            !maybe_dead
186        }
187    };
188
189    for (local, rvalue, location) in ssa.assignments(body) {
190        debug!(?local);
191
192        // Only visit if we have something to do.
193        let Value::Unknown = targets[local] else { bug!() };
194
195        let ty = body.local_decls[local].ty;
196
197        // If this is not a reference or pointer, do nothing.
198        if !ty.is_any_ptr() {
199            debug!("not a reference or pointer");
200            continue;
201        }
202
203        // Whether the current local is subject to the uniqueness rule.
204        let needs_unique = ty.is_mutable_ptr();
205
206        // If this a mutable reference that we cannot fully replace, mark it as unknown.
207        if needs_unique && !fully_replacable_locals.contains(local) {
208            debug!("not fully replaceable");
209            continue;
210        }
211
212        debug!(?rvalue);
213        match rvalue {
214            // This is a copy, just use the value we have in store for the previous one.
215            // As we are visiting in `assignment_order`, ie. reverse postorder, `rhs` should
216            // have been visited before.
217            Rvalue::Use(Operand::Copy(place) | Operand::Move(place))
218            | Rvalue::CopyForDeref(place) => {
219                if let Some(rhs) = place.as_local()
220                    && ssa.is_ssa(rhs)
221                {
222                    let target = targets[rhs];
223                    // Only see through immutable reference and pointers, as we do not know yet if
224                    // mutable references are fully replaced.
225                    if !needs_unique && matches!(target, Value::Pointer(..)) {
226                        targets[local] = target;
227                    } else {
228                        targets[local] =
229                            Value::Pointer(tcx.mk_place_deref(rhs.into()), needs_unique);
230                    }
231                }
232            }
233            Rvalue::Ref(_, _, place) | Rvalue::RawPtr(_, place) => {
234                let mut place = *place;
235                // Try to see through `place` in order to collapse reborrow chains.
236                if place.projection.first() == Some(&PlaceElem::Deref)
237                    && let Value::Pointer(target, inner_needs_unique) = targets[place.local]
238                    // Only see through immutable reference and pointers, as we do not know yet if
239                    // mutable references are fully replaced.
240                    && !inner_needs_unique
241                    // Only collapse chain if the pointee is definitely live.
242                    && can_perform_opt(target, location)
243                {
244                    place = target.project_deeper(&place.projection[1..], tcx);
245                }
246                assert_ne!(place.local, local);
247                if is_constant_place(place) {
248                    targets[local] = Value::Pointer(place, needs_unique);
249                }
250            }
251            // We do not know what to do, so keep as not-a-pointer.
252            _ => {}
253        }
254    }
255
256    debug!(?targets);
257
258    let mut finder =
259        ReplacementFinder { targets, can_perform_opt, allowed_replacements: FxHashSet::default() };
260    let reachable_blocks = traversal::reachable_as_bitset(body);
261    for (bb, bbdata) in body.basic_blocks.iter_enumerated() {
262        // Only visit reachable blocks as we rely on dataflow.
263        if reachable_blocks.contains(bb) {
264            finder.visit_basic_block_data(bb, bbdata);
265        }
266    }
267
268    let allowed_replacements = finder.allowed_replacements;
269    return Replacer {
270        tcx,
271        targets: finder.targets,
272        storage_to_remove,
273        allowed_replacements,
274        any_replacement: false,
275    };
276
277    struct ReplacementFinder<'tcx, F> {
278        targets: IndexVec<Local, Value<'tcx>>,
279        can_perform_opt: F,
280        allowed_replacements: FxHashSet<(Local, Location)>,
281    }
282
283    impl<'tcx, F> Visitor<'tcx> for ReplacementFinder<'tcx, F>
284    where
285        F: FnMut(Place<'tcx>, Location) -> bool,
286    {
287        fn visit_place(&mut self, place: &Place<'tcx>, ctxt: PlaceContext, loc: Location) {
288            if matches!(ctxt, PlaceContext::NonUse(_)) {
289                // There is no need to check liveness for non-uses.
290                return;
291            }
292
293            if place.projection.first() != Some(&PlaceElem::Deref) {
294                // This is not a dereference, nothing to do.
295                return;
296            }
297
298            let mut place = place.as_ref();
299            loop {
300                if let Value::Pointer(target, needs_unique) = self.targets[place.local] {
301                    let perform_opt = (self.can_perform_opt)(target, loc);
302                    debug!(?place, ?target, ?needs_unique, ?perform_opt);
303
304                    // This a reborrow chain, recursively allow the replacement.
305                    //
306                    // This also allows to detect cases where `target.local` is not replacable,
307                    // and mark it as such.
308                    if let &[PlaceElem::Deref] = &target.projection[..] {
309                        assert!(perform_opt);
310                        self.allowed_replacements.insert((target.local, loc));
311                        place.local = target.local;
312                        continue;
313                    } else if perform_opt {
314                        self.allowed_replacements.insert((target.local, loc));
315                    } else if needs_unique {
316                        // This mutable reference is not fully replacable, so drop it.
317                        self.targets[place.local] = Value::Unknown;
318                    }
319                }
320
321                break;
322            }
323        }
324    }
325}
326
327/// Compute the set of locals that can be fully replaced.
328///
329/// We consider a local to be replacable iff it's only used in a `Deref` projection `*_local` or
330/// non-use position (like storage statements and debuginfo).
331fn fully_replacable_locals(ssa: &SsaLocals) -> DenseBitSet<Local> {
332    let mut replacable = DenseBitSet::new_empty(ssa.num_locals());
333
334    // First pass: for each local, whether its uses can be fully replaced.
335    for local in ssa.locals() {
336        if ssa.num_direct_uses(local) == 0 {
337            replacable.insert(local);
338        }
339    }
340
341    // Second pass: a local can only be fully replaced if all its copies can.
342    ssa.meet_copy_equivalence(&mut replacable);
343
344    replacable
345}
346
347/// Utility to help performing substitution of `*pattern` by `target`.
348struct Replacer<'tcx> {
349    tcx: TyCtxt<'tcx>,
350    targets: IndexVec<Local, Value<'tcx>>,
351    storage_to_remove: DenseBitSet<Local>,
352    allowed_replacements: FxHashSet<(Local, Location)>,
353    any_replacement: bool,
354}
355
356impl<'tcx> MutVisitor<'tcx> for Replacer<'tcx> {
357    fn tcx(&self) -> TyCtxt<'tcx> {
358        self.tcx
359    }
360
361    fn visit_var_debug_info(&mut self, debuginfo: &mut VarDebugInfo<'tcx>) {
362        // If the debuginfo is a pointer to another place:
363        // - if it's a reborrow, see through it;
364        // - if it's a direct borrow, increase `debuginfo.references`.
365        while let VarDebugInfoContents::Place(ref mut place) = debuginfo.value
366            && place.projection.is_empty()
367            && let Value::Pointer(target, _) = self.targets[place.local]
368            && target.projection.iter().all(|p| p.can_use_in_debuginfo())
369        {
370            if let Some((&PlaceElem::Deref, rest)) = target.projection.split_last() {
371                *place = Place::from(target.local).project_deeper(rest, self.tcx);
372                self.any_replacement = true;
373            } else {
374                break;
375            }
376        }
377
378        // Simplify eventual projections left inside `debuginfo`.
379        self.super_var_debug_info(debuginfo);
380    }
381
382    fn visit_place(&mut self, place: &mut Place<'tcx>, ctxt: PlaceContext, loc: Location) {
383        loop {
384            if place.projection.first() != Some(&PlaceElem::Deref) {
385                return;
386            }
387
388            let Value::Pointer(target, _) = self.targets[place.local] else { return };
389
390            let perform_opt = match ctxt {
391                PlaceContext::NonUse(NonUseContext::VarDebugInfo) => {
392                    target.projection.iter().all(|p| p.can_use_in_debuginfo())
393                }
394                PlaceContext::NonUse(_) => true,
395                _ => self.allowed_replacements.contains(&(target.local, loc)),
396            };
397
398            if !perform_opt {
399                return;
400            }
401
402            *place = target.project_deeper(&place.projection[1..], self.tcx);
403            self.any_replacement = true;
404        }
405    }
406
407    fn visit_statement(&mut self, stmt: &mut Statement<'tcx>, loc: Location) {
408        match stmt.kind {
409            StatementKind::StorageLive(l) | StatementKind::StorageDead(l)
410                if self.storage_to_remove.contains(l) =>
411            {
412                stmt.make_nop();
413            }
414            // Do not remove assignments as they may still be useful for debuginfo.
415            _ => self.super_statement(stmt, loc),
416        }
417    }
418}