rustc_mir_dataflow/impls/
liveness.rs

1use rustc_index::bit_set::DenseBitSet;
2use rustc_middle::mir::visit::{MutatingUseContext, NonMutatingUseContext, PlaceContext, Visitor};
3use rustc_middle::mir::{
4    self, CallReturnPlaces, Local, Location, Place, StatementKind, TerminatorEdges,
5};
6
7use crate::{Analysis, Backward, GenKill};
8
9/// A [live-variable dataflow analysis][liveness].
10///
11/// This analysis considers references as being used only at the point of the
12/// borrow. In other words, this analysis does not track uses because of references that already
13/// exist. See [this `mir-dataflow` test][flow-test] for an example. You almost never want to use
14/// this analysis without also looking at the results of [`MaybeBorrowedLocals`].
15///
16/// ## Field-(in)sensitivity
17///
18/// As the name suggests, this analysis is field insensitive. If a projection of a variable `x` is
19/// assigned to (e.g. `x.0 = 42`), it does not "define" `x` as far as liveness is concerned. In fact,
20/// such an assignment is currently marked as a "use" of `x` in an attempt to be maximally
21/// conservative.
22///
23/// [`MaybeBorrowedLocals`]: super::MaybeBorrowedLocals
24/// [flow-test]: https://github.com/rust-lang/rust/blob/a08c47310c7d49cbdc5d7afb38408ba519967ecd/src/test/ui/mir-dataflow/liveness-ptr.rs
25/// [liveness]: https://en.wikipedia.org/wiki/Live_variable_analysis
26pub struct MaybeLiveLocals;
27
28impl<'tcx> Analysis<'tcx> for MaybeLiveLocals {
29    type Domain = DenseBitSet<Local>;
30    type Direction = Backward;
31
32    const NAME: &'static str = "liveness";
33
34    fn bottom_value(&self, body: &mir::Body<'tcx>) -> Self::Domain {
35        // bottom = not live
36        DenseBitSet::new_empty(body.local_decls.len())
37    }
38
39    fn initialize_start_block(&self, _: &mir::Body<'tcx>, _: &mut Self::Domain) {
40        // No variables are live until we observe a use
41    }
42
43    fn apply_primary_statement_effect(
44        &mut self,
45        state: &mut Self::Domain,
46        statement: &mir::Statement<'tcx>,
47        location: Location,
48    ) {
49        TransferFunction(state).visit_statement(statement, location);
50    }
51
52    fn apply_primary_terminator_effect<'mir>(
53        &mut self,
54        state: &mut Self::Domain,
55        terminator: &'mir mir::Terminator<'tcx>,
56        location: Location,
57    ) -> TerminatorEdges<'mir, 'tcx> {
58        TransferFunction(state).visit_terminator(terminator, location);
59        terminator.edges()
60    }
61
62    fn apply_call_return_effect(
63        &mut self,
64        state: &mut Self::Domain,
65        _block: mir::BasicBlock,
66        return_places: CallReturnPlaces<'_, 'tcx>,
67    ) {
68        if let CallReturnPlaces::Yield(resume_place) = return_places {
69            YieldResumeEffect(state).visit_place(
70                &resume_place,
71                PlaceContext::MutatingUse(MutatingUseContext::Yield),
72                Location::START,
73            )
74        } else {
75            return_places.for_each(|place| {
76                if let Some(local) = place.as_local() {
77                    state.kill(local);
78                }
79            });
80        }
81    }
82}
83
84pub struct TransferFunction<'a>(pub &'a mut DenseBitSet<Local>);
85
86impl<'tcx> Visitor<'tcx> for TransferFunction<'_> {
87    fn visit_place(&mut self, place: &mir::Place<'tcx>, context: PlaceContext, location: Location) {
88        if let PlaceContext::MutatingUse(MutatingUseContext::Yield) = context {
89            // The resume place is evaluated and assigned to only after coroutine resumes, so its
90            // effect is handled separately in `call_resume_effect`.
91            return;
92        }
93
94        match DefUse::for_place(*place, context) {
95            DefUse::Def => {
96                if let PlaceContext::MutatingUse(
97                    MutatingUseContext::Call | MutatingUseContext::AsmOutput,
98                ) = context
99                {
100                    // For the associated terminators, this is only a `Def` when the terminator
101                    // returns "successfully." As such, we handle this case separately in
102                    // `call_return_effect` above. However, if the place looks like `*_5`, this is
103                    // still unconditionally a use of `_5`.
104                } else {
105                    self.0.kill(place.local);
106                }
107            }
108            DefUse::Use => self.0.gen_(place.local),
109            DefUse::PartialWrite | DefUse::NonUse => {}
110        }
111
112        self.visit_projection(place.as_ref(), context, location);
113    }
114
115    fn visit_local(&mut self, local: Local, context: PlaceContext, _: Location) {
116        DefUse::apply(self.0, local.into(), context);
117    }
118}
119
120struct YieldResumeEffect<'a>(&'a mut DenseBitSet<Local>);
121
122impl<'tcx> Visitor<'tcx> for YieldResumeEffect<'_> {
123    fn visit_place(&mut self, place: &mir::Place<'tcx>, context: PlaceContext, location: Location) {
124        DefUse::apply(self.0, *place, context);
125        self.visit_projection(place.as_ref(), context, location);
126    }
127
128    fn visit_local(&mut self, local: Local, context: PlaceContext, _: Location) {
129        DefUse::apply(self.0, local.into(), context);
130    }
131}
132
133#[derive(Eq, PartialEq, Clone)]
134pub enum DefUse {
135    /// Full write to the local.
136    Def,
137    /// Read of any part of the local.
138    Use,
139    /// Partial write to the local.
140    PartialWrite,
141    /// Non-use, like debuginfo.
142    NonUse,
143}
144
145impl DefUse {
146    fn apply(state: &mut DenseBitSet<Local>, place: Place<'_>, context: PlaceContext) {
147        match DefUse::for_place(place, context) {
148            DefUse::Def => state.kill(place.local),
149            DefUse::Use => state.gen_(place.local),
150            DefUse::PartialWrite | DefUse::NonUse => {}
151        }
152    }
153
154    pub fn for_place(place: Place<'_>, context: PlaceContext) -> DefUse {
155        match context {
156            PlaceContext::NonUse(_) => DefUse::NonUse,
157
158            PlaceContext::MutatingUse(
159                MutatingUseContext::Call
160                | MutatingUseContext::Yield
161                | MutatingUseContext::AsmOutput
162                | MutatingUseContext::Store
163                | MutatingUseContext::Deinit,
164            ) => {
165                // Treat derefs as a use of the base local. `*p = 4` is not a def of `p` but a use.
166                if place.is_indirect() {
167                    DefUse::Use
168                } else if place.projection.is_empty() {
169                    DefUse::Def
170                } else {
171                    DefUse::PartialWrite
172                }
173            }
174
175            // Setting the discriminant is not a use because it does no reading, but it is also not
176            // a def because it does not overwrite the whole place
177            PlaceContext::MutatingUse(MutatingUseContext::SetDiscriminant) => {
178                if place.is_indirect() { DefUse::Use } else { DefUse::PartialWrite }
179            }
180
181            // All other contexts are uses...
182            PlaceContext::MutatingUse(
183                MutatingUseContext::RawBorrow
184                | MutatingUseContext::Borrow
185                | MutatingUseContext::Drop
186                | MutatingUseContext::Retag,
187            )
188            | PlaceContext::NonMutatingUse(
189                NonMutatingUseContext::RawBorrow
190                | NonMutatingUseContext::Copy
191                | NonMutatingUseContext::Inspect
192                | NonMutatingUseContext::Move
193                | NonMutatingUseContext::PlaceMention
194                | NonMutatingUseContext::FakeBorrow
195                | NonMutatingUseContext::SharedBorrow,
196            ) => DefUse::Use,
197
198            PlaceContext::MutatingUse(MutatingUseContext::Projection)
199            | PlaceContext::NonMutatingUse(NonMutatingUseContext::Projection) => {
200                unreachable!("A projection could be a def or a use and must be handled separately")
201            }
202        }
203    }
204}
205
206/// Like `MaybeLiveLocals`, but does not mark locals as live if they are used in a dead assignment.
207///
208/// This is basically written for dead store elimination and nothing else.
209///
210/// All of the caveats of `MaybeLiveLocals` apply.
211pub struct MaybeTransitiveLiveLocals<'a> {
212    always_live: &'a DenseBitSet<Local>,
213}
214
215impl<'a> MaybeTransitiveLiveLocals<'a> {
216    /// The `always_alive` set is the set of locals to which all stores should unconditionally be
217    /// considered live.
218    ///
219    /// This should include at least all locals that are ever borrowed.
220    pub fn new(always_live: &'a DenseBitSet<Local>) -> Self {
221        MaybeTransitiveLiveLocals { always_live }
222    }
223}
224
225impl<'a, 'tcx> Analysis<'tcx> for MaybeTransitiveLiveLocals<'a> {
226    type Domain = DenseBitSet<Local>;
227    type Direction = Backward;
228
229    const NAME: &'static str = "transitive liveness";
230
231    fn bottom_value(&self, body: &mir::Body<'tcx>) -> Self::Domain {
232        // bottom = not live
233        DenseBitSet::new_empty(body.local_decls.len())
234    }
235
236    fn initialize_start_block(&self, _: &mir::Body<'tcx>, _: &mut Self::Domain) {
237        // No variables are live until we observe a use
238    }
239
240    fn apply_primary_statement_effect(
241        &mut self,
242        state: &mut Self::Domain,
243        statement: &mir::Statement<'tcx>,
244        location: Location,
245    ) {
246        // Compute the place that we are storing to, if any
247        let destination = match &statement.kind {
248            StatementKind::Assign(assign) => assign.1.is_safe_to_remove().then_some(assign.0),
249            StatementKind::SetDiscriminant { place, .. } | StatementKind::Deinit(place) => {
250                Some(**place)
251            }
252            StatementKind::FakeRead(_)
253            | StatementKind::StorageLive(_)
254            | StatementKind::StorageDead(_)
255            | StatementKind::Retag(..)
256            | StatementKind::AscribeUserType(..)
257            | StatementKind::PlaceMention(..)
258            | StatementKind::Coverage(..)
259            | StatementKind::Intrinsic(..)
260            | StatementKind::ConstEvalCounter
261            | StatementKind::BackwardIncompatibleDropHint { .. }
262            | StatementKind::Nop => None,
263        };
264        if let Some(destination) = destination {
265            if !destination.is_indirect()
266                && !state.contains(destination.local)
267                && !self.always_live.contains(destination.local)
268            {
269                // This store is dead
270                return;
271            }
272        }
273        TransferFunction(state).visit_statement(statement, location);
274    }
275
276    fn apply_primary_terminator_effect<'mir>(
277        &mut self,
278        state: &mut Self::Domain,
279        terminator: &'mir mir::Terminator<'tcx>,
280        location: Location,
281    ) -> TerminatorEdges<'mir, 'tcx> {
282        TransferFunction(state).visit_terminator(terminator, location);
283        terminator.edges()
284    }
285
286    fn apply_call_return_effect(
287        &mut self,
288        state: &mut Self::Domain,
289        _block: mir::BasicBlock,
290        return_places: CallReturnPlaces<'_, 'tcx>,
291    ) {
292        if let CallReturnPlaces::Yield(resume_place) = return_places {
293            YieldResumeEffect(state).visit_place(
294                &resume_place,
295                PlaceContext::MutatingUse(MutatingUseContext::Yield),
296                Location::START,
297            )
298        } else {
299            return_places.for_each(|place| {
300                if let Some(local) = place.as_local() {
301                    state.remove(local);
302                }
303            });
304        }
305    }
306}