rustc_mir_dataflow/
value_analysis.rs

1use std::fmt::{Debug, Formatter};
2use std::ops::Range;
3
4use rustc_abi::{FieldIdx, VariantIdx};
5use rustc_data_structures::captures::Captures;
6use rustc_data_structures::fx::{FxHashMap, FxIndexSet, StdEntry};
7use rustc_data_structures::stack::ensure_sufficient_stack;
8use rustc_index::IndexVec;
9use rustc_index::bit_set::DenseBitSet;
10use rustc_middle::mir::tcx::PlaceTy;
11use rustc_middle::mir::visit::{MutatingUseContext, PlaceContext, Visitor};
12use rustc_middle::mir::*;
13use rustc_middle::ty::{self, Ty, TyCtxt};
14use tracing::debug;
15
16use crate::JoinSemiLattice;
17use crate::lattice::{HasBottom, HasTop};
18
19rustc_index::newtype_index!(
20    /// This index uniquely identifies a place.
21    ///
22    /// Not every place has a `PlaceIndex`, and not every `PlaceIndex` corresponds to a tracked
23    /// place. However, every tracked place and all places along its projection have a `PlaceIndex`.
24    pub struct PlaceIndex {}
25);
26
27rustc_index::newtype_index!(
28    /// This index uniquely identifies a tracked place and therefore a slot in [`State`].
29    ///
30    /// It is an implementation detail of this module.
31    struct ValueIndex {}
32);
33
34/// See [`State`].
35#[derive(PartialEq, Eq, Debug)]
36pub struct StateData<V> {
37    bottom: V,
38    /// This map only contains values that are not `⊥`.
39    map: FxHashMap<ValueIndex, V>,
40}
41
42impl<V: HasBottom> StateData<V> {
43    fn new() -> StateData<V> {
44        StateData { bottom: V::BOTTOM, map: FxHashMap::default() }
45    }
46
47    fn get(&self, idx: ValueIndex) -> &V {
48        self.map.get(&idx).unwrap_or(&self.bottom)
49    }
50
51    fn insert(&mut self, idx: ValueIndex, elem: V) {
52        if elem.is_bottom() {
53            self.map.remove(&idx);
54        } else {
55            self.map.insert(idx, elem);
56        }
57    }
58}
59
60impl<V: Clone> Clone for StateData<V> {
61    fn clone(&self) -> Self {
62        StateData { bottom: self.bottom.clone(), map: self.map.clone() }
63    }
64
65    fn clone_from(&mut self, source: &Self) {
66        self.map.clone_from(&source.map)
67    }
68}
69
70impl<V: JoinSemiLattice + Clone> JoinSemiLattice for StateData<V> {
71    fn join(&mut self, other: &Self) -> bool {
72        let mut changed = false;
73        #[allow(rustc::potential_query_instability)]
74        for (i, v) in other.map.iter() {
75            match self.map.entry(*i) {
76                StdEntry::Vacant(e) => {
77                    e.insert(v.clone());
78                    changed = true
79                }
80                StdEntry::Occupied(e) => changed |= e.into_mut().join(v),
81            }
82        }
83        changed
84    }
85}
86
87/// Dataflow state.
88///
89/// Every instance specifies a lattice that represents the possible values of a single tracked
90/// place. If we call this lattice `V` and set of tracked places `P`, then a [`State`] is an
91/// element of `{unreachable} ∪ (P -> V)`. This again forms a lattice, where the bottom element is
92/// `unreachable` and the top element is the mapping `p ↦ ⊤`. Note that the mapping `p ↦ ⊥` is not
93/// the bottom element (because joining an unreachable and any other reachable state yields a
94/// reachable state). All operations on unreachable states are ignored.
95///
96/// Flooding means assigning a value (by default `⊤`) to all tracked projections of a given place.
97#[derive(PartialEq, Eq, Debug)]
98pub enum State<V> {
99    Unreachable,
100    Reachable(StateData<V>),
101}
102
103impl<V: Clone> Clone for State<V> {
104    fn clone(&self) -> Self {
105        match self {
106            Self::Reachable(x) => Self::Reachable(x.clone()),
107            Self::Unreachable => Self::Unreachable,
108        }
109    }
110
111    fn clone_from(&mut self, source: &Self) {
112        match (&mut *self, source) {
113            (Self::Reachable(x), Self::Reachable(y)) => {
114                x.clone_from(&y);
115            }
116            _ => *self = source.clone(),
117        }
118    }
119}
120
121impl<V: Clone + HasBottom> State<V> {
122    pub fn new_reachable() -> State<V> {
123        State::Reachable(StateData::new())
124    }
125
126    pub fn all_bottom(&self) -> bool {
127        match self {
128            State::Unreachable => false,
129            State::Reachable(ref values) =>
130            {
131                #[allow(rustc::potential_query_instability)]
132                values.map.values().all(V::is_bottom)
133            }
134        }
135    }
136
137    pub fn is_reachable(&self) -> bool {
138        matches!(self, State::Reachable(_))
139    }
140
141    /// Assign `value` to all places that are contained in `place` or may alias one.
142    pub fn flood_with(&mut self, place: PlaceRef<'_>, map: &Map<'_>, value: V) {
143        self.flood_with_tail_elem(place, None, map, value)
144    }
145
146    /// Assign `TOP` to all places that are contained in `place` or may alias one.
147    pub fn flood(&mut self, place: PlaceRef<'_>, map: &Map<'_>)
148    where
149        V: HasTop,
150    {
151        self.flood_with(place, map, V::TOP)
152    }
153
154    /// Assign `value` to the discriminant of `place` and all places that may alias it.
155    fn flood_discr_with(&mut self, place: PlaceRef<'_>, map: &Map<'_>, value: V) {
156        self.flood_with_tail_elem(place, Some(TrackElem::Discriminant), map, value)
157    }
158
159    /// Assign `TOP` to the discriminant of `place` and all places that may alias it.
160    pub fn flood_discr(&mut self, place: PlaceRef<'_>, map: &Map<'_>)
161    where
162        V: HasTop,
163    {
164        self.flood_discr_with(place, map, V::TOP)
165    }
166
167    /// This method is the most general version of the `flood_*` method.
168    ///
169    /// Assign `value` on the given place and all places that may alias it. In particular, when
170    /// the given place has a variant downcast, we invoke the function on all the other variants.
171    ///
172    /// `tail_elem` allows to support discriminants that are not a place in MIR, but that we track
173    /// as such.
174    pub fn flood_with_tail_elem(
175        &mut self,
176        place: PlaceRef<'_>,
177        tail_elem: Option<TrackElem>,
178        map: &Map<'_>,
179        value: V,
180    ) {
181        let State::Reachable(values) = self else { return };
182        map.for_each_aliasing_place(place, tail_elem, &mut |vi| values.insert(vi, value.clone()));
183    }
184
185    /// Low-level method that assigns to a place.
186    /// This does nothing if the place is not tracked.
187    ///
188    /// The target place must have been flooded before calling this method.
189    fn insert_idx(&mut self, target: PlaceIndex, result: ValueOrPlace<V>, map: &Map<'_>) {
190        match result {
191            ValueOrPlace::Value(value) => self.insert_value_idx(target, value, map),
192            ValueOrPlace::Place(source) => self.insert_place_idx(target, source, map),
193        }
194    }
195
196    /// Low-level method that assigns a value to a place.
197    /// This does nothing if the place is not tracked.
198    ///
199    /// The target place must have been flooded before calling this method.
200    pub fn insert_value_idx(&mut self, target: PlaceIndex, value: V, map: &Map<'_>) {
201        let State::Reachable(values) = self else { return };
202        if let Some(value_index) = map.places[target].value_index {
203            values.insert(value_index, value)
204        }
205    }
206
207    /// Copies `source` to `target`, including all tracked places beneath.
208    ///
209    /// If `target` contains a place that is not contained in `source`, it will be overwritten with
210    /// Top. Also, because this will copy all entries one after another, it may only be used for
211    /// places that are non-overlapping or identical.
212    ///
213    /// The target place must have been flooded before calling this method.
214    pub fn insert_place_idx(&mut self, target: PlaceIndex, source: PlaceIndex, map: &Map<'_>) {
215        let State::Reachable(values) = self else { return };
216
217        // If both places are tracked, we copy the value to the target.
218        // If the target is tracked, but the source is not, we do nothing, as invalidation has
219        // already been performed.
220        if let Some(target_value) = map.places[target].value_index {
221            if let Some(source_value) = map.places[source].value_index {
222                values.insert(target_value, values.get(source_value).clone());
223            }
224        }
225        for target_child in map.children(target) {
226            // Try to find corresponding child and recurse. Reasoning is similar as above.
227            let projection = map.places[target_child].proj_elem.unwrap();
228            if let Some(source_child) = map.projections.get(&(source, projection)) {
229                self.insert_place_idx(target_child, *source_child, map);
230            }
231        }
232    }
233
234    /// Helper method to interpret `target = result`.
235    pub fn assign(&mut self, target: PlaceRef<'_>, result: ValueOrPlace<V>, map: &Map<'_>)
236    where
237        V: HasTop,
238    {
239        self.flood(target, map);
240        if let Some(target) = map.find(target) {
241            self.insert_idx(target, result, map);
242        }
243    }
244
245    /// Helper method for assignments to a discriminant.
246    pub fn assign_discr(&mut self, target: PlaceRef<'_>, result: ValueOrPlace<V>, map: &Map<'_>)
247    where
248        V: HasTop,
249    {
250        self.flood_discr(target, map);
251        if let Some(target) = map.find_discr(target) {
252            self.insert_idx(target, result, map);
253        }
254    }
255
256    /// Retrieve the value stored for a place, or `None` if it is not tracked.
257    pub fn try_get(&self, place: PlaceRef<'_>, map: &Map<'_>) -> Option<V> {
258        let place = map.find(place)?;
259        self.try_get_idx(place, map)
260    }
261
262    /// Retrieve the discriminant stored for a place, or `None` if it is not tracked.
263    pub fn try_get_discr(&self, place: PlaceRef<'_>, map: &Map<'_>) -> Option<V> {
264        let place = map.find_discr(place)?;
265        self.try_get_idx(place, map)
266    }
267
268    /// Retrieve the slice length stored for a place, or `None` if it is not tracked.
269    pub fn try_get_len(&self, place: PlaceRef<'_>, map: &Map<'_>) -> Option<V> {
270        let place = map.find_len(place)?;
271        self.try_get_idx(place, map)
272    }
273
274    /// Retrieve the value stored for a place index, or `None` if it is not tracked.
275    pub fn try_get_idx(&self, place: PlaceIndex, map: &Map<'_>) -> Option<V> {
276        match self {
277            State::Reachable(values) => {
278                map.places[place].value_index.map(|v| values.get(v).clone())
279            }
280            State::Unreachable => None,
281        }
282    }
283
284    /// Retrieve the value stored for a place, or ⊤ if it is not tracked.
285    ///
286    /// This method returns ⊥ if the place is tracked and the state is unreachable.
287    pub fn get(&self, place: PlaceRef<'_>, map: &Map<'_>) -> V
288    where
289        V: HasBottom + HasTop,
290    {
291        match self {
292            State::Reachable(_) => self.try_get(place, map).unwrap_or(V::TOP),
293            // Because this is unreachable, we can return any value we want.
294            State::Unreachable => V::BOTTOM,
295        }
296    }
297
298    /// Retrieve the value stored for a place, or ⊤ if it is not tracked.
299    ///
300    /// This method returns ⊥ the current state is unreachable.
301    pub fn get_discr(&self, place: PlaceRef<'_>, map: &Map<'_>) -> V
302    where
303        V: HasBottom + HasTop,
304    {
305        match self {
306            State::Reachable(_) => self.try_get_discr(place, map).unwrap_or(V::TOP),
307            // Because this is unreachable, we can return any value we want.
308            State::Unreachable => V::BOTTOM,
309        }
310    }
311
312    /// Retrieve the value stored for a place, or ⊤ if it is not tracked.
313    ///
314    /// This method returns ⊥ the current state is unreachable.
315    pub fn get_len(&self, place: PlaceRef<'_>, map: &Map<'_>) -> V
316    where
317        V: HasBottom + HasTop,
318    {
319        match self {
320            State::Reachable(_) => self.try_get_len(place, map).unwrap_or(V::TOP),
321            // Because this is unreachable, we can return any value we want.
322            State::Unreachable => V::BOTTOM,
323        }
324    }
325
326    /// Retrieve the value stored for a place index, or ⊤ if it is not tracked.
327    ///
328    /// This method returns ⊥ the current state is unreachable.
329    pub fn get_idx(&self, place: PlaceIndex, map: &Map<'_>) -> V
330    where
331        V: HasBottom + HasTop,
332    {
333        match self {
334            State::Reachable(values) => {
335                map.places[place].value_index.map(|v| values.get(v).clone()).unwrap_or(V::TOP)
336            }
337            State::Unreachable => {
338                // Because this is unreachable, we can return any value we want.
339                V::BOTTOM
340            }
341        }
342    }
343}
344
345impl<V: JoinSemiLattice + Clone> JoinSemiLattice for State<V> {
346    fn join(&mut self, other: &Self) -> bool {
347        match (&mut *self, other) {
348            (_, State::Unreachable) => false,
349            (State::Unreachable, _) => {
350                *self = other.clone();
351                true
352            }
353            (State::Reachable(this), State::Reachable(ref other)) => this.join(other),
354        }
355    }
356}
357
358/// Partial mapping from [`Place`] to [`PlaceIndex`], where some places also have a [`ValueIndex`].
359///
360/// This data structure essentially maintains a tree of places and their projections. Some
361/// additional bookkeeping is done, to speed up traversal over this tree:
362/// - For iteration, every [`PlaceInfo`] contains an intrusive linked list of its children.
363/// - To directly get the child for a specific projection, there is a `projections` map.
364#[derive(Debug)]
365pub struct Map<'tcx> {
366    locals: IndexVec<Local, Option<PlaceIndex>>,
367    projections: FxHashMap<(PlaceIndex, TrackElem), PlaceIndex>,
368    places: IndexVec<PlaceIndex, PlaceInfo<'tcx>>,
369    value_count: usize,
370    // The Range corresponds to a slice into `inner_values_buffer`.
371    inner_values: IndexVec<PlaceIndex, Range<usize>>,
372    inner_values_buffer: Vec<ValueIndex>,
373}
374
375impl<'tcx> Map<'tcx> {
376    /// Returns a map that only tracks places whose type has scalar layout.
377    ///
378    /// This is currently the only way to create a [`Map`]. The way in which the tracked places are
379    /// chosen is an implementation detail and may not be relied upon (other than that their type
380    /// are scalars).
381    pub fn new(tcx: TyCtxt<'tcx>, body: &Body<'tcx>, value_limit: Option<usize>) -> Self {
382        let mut map = Self {
383            locals: IndexVec::from_elem(None, &body.local_decls),
384            projections: FxHashMap::default(),
385            places: IndexVec::new(),
386            value_count: 0,
387            inner_values: IndexVec::new(),
388            inner_values_buffer: Vec::new(),
389        };
390        let exclude = excluded_locals(body);
391        map.register(tcx, body, exclude, value_limit);
392        debug!("registered {} places ({} nodes in total)", map.value_count, map.places.len());
393        map
394    }
395
396    /// Register all non-excluded places that have scalar layout.
397    #[tracing::instrument(level = "trace", skip(self, tcx, body))]
398    fn register(
399        &mut self,
400        tcx: TyCtxt<'tcx>,
401        body: &Body<'tcx>,
402        exclude: DenseBitSet<Local>,
403        value_limit: Option<usize>,
404    ) {
405        // Start by constructing the places for each bare local.
406        for (local, decl) in body.local_decls.iter_enumerated() {
407            if exclude.contains(local) {
408                continue;
409            }
410
411            // Create a place for the local.
412            debug_assert!(self.locals[local].is_none());
413            let place = self.places.push(PlaceInfo::new(decl.ty, None));
414            self.locals[local] = Some(place);
415        }
416
417        // Collect syntactic places and assignments between them.
418        let mut collector =
419            PlaceCollector { tcx, body, map: self, assignments: Default::default() };
420        collector.visit_body(body);
421        let PlaceCollector { mut assignments, .. } = collector;
422
423        // Just collecting syntactic places is not enough. We may need to propagate this pattern:
424        //      _1 = (const 5u32, const 13i64);
425        //      _2 = _1;
426        //      _3 = (_2.0 as u32);
427        //
428        // `_1.0` does not appear, but we still need to track it. This is achieved by propagating
429        // projections from assignments. We recorded an assignment between `_2` and `_1`, so we
430        // want `_1` and `_2` to have the same sub-places.
431        //
432        // This is what this fixpoint loop does. While we are still creating places, run through
433        // all the assignments, and register places for children.
434        let mut num_places = 0;
435        while num_places < self.places.len() {
436            num_places = self.places.len();
437
438            for assign in 0.. {
439                let Some(&(lhs, rhs)) = assignments.get_index(assign) else { break };
440
441                // Mirror children from `lhs` in `rhs`.
442                let mut child = self.places[lhs].first_child;
443                while let Some(lhs_child) = child {
444                    let PlaceInfo { ty, proj_elem, next_sibling, .. } = self.places[lhs_child];
445                    let rhs_child =
446                        self.register_place(ty, rhs, proj_elem.expect("child is not a projection"));
447                    assignments.insert((lhs_child, rhs_child));
448                    child = next_sibling;
449                }
450
451                // Conversely, mirror children from `rhs` in `lhs`.
452                let mut child = self.places[rhs].first_child;
453                while let Some(rhs_child) = child {
454                    let PlaceInfo { ty, proj_elem, next_sibling, .. } = self.places[rhs_child];
455                    let lhs_child =
456                        self.register_place(ty, lhs, proj_elem.expect("child is not a projection"));
457                    assignments.insert((lhs_child, rhs_child));
458                    child = next_sibling;
459                }
460            }
461        }
462        drop(assignments);
463
464        // Create values for places whose type have scalar layout.
465        let typing_env = body.typing_env(tcx);
466        for place_info in self.places.iter_mut() {
467            // The user requires a bound on the number of created values.
468            if let Some(value_limit) = value_limit
469                && self.value_count >= value_limit
470            {
471                break;
472            }
473
474            if let Ok(ty) = tcx.try_normalize_erasing_regions(typing_env, place_info.ty) {
475                place_info.ty = ty;
476            }
477
478            // Allocate a value slot if it doesn't have one, and the user requested one.
479            assert!(place_info.value_index.is_none());
480            if let Ok(layout) = tcx.layout_of(typing_env.as_query_input(place_info.ty))
481                && layout.backend_repr.is_scalar()
482            {
483                place_info.value_index = Some(self.value_count.into());
484                self.value_count += 1;
485            }
486        }
487
488        // Pre-compute the tree of ValueIndex nested in each PlaceIndex.
489        // `inner_values_buffer[inner_values[place]]` is the set of all the values
490        // reachable by projecting `place`.
491        self.inner_values_buffer = Vec::with_capacity(self.value_count);
492        self.inner_values = IndexVec::from_elem(0..0, &self.places);
493        for local in body.local_decls.indices() {
494            if let Some(place) = self.locals[local] {
495                self.cache_preorder_invoke(place);
496            }
497        }
498
499        // Trim useless places.
500        for opt_place in self.locals.iter_mut() {
501            if let Some(place) = *opt_place
502                && self.inner_values[place].is_empty()
503            {
504                *opt_place = None;
505            }
506        }
507        #[allow(rustc::potential_query_instability)]
508        self.projections.retain(|_, child| !self.inner_values[*child].is_empty());
509    }
510
511    #[tracing::instrument(level = "trace", skip(self), ret)]
512    fn register_place(&mut self, ty: Ty<'tcx>, base: PlaceIndex, elem: TrackElem) -> PlaceIndex {
513        *self.projections.entry((base, elem)).or_insert_with(|| {
514            let next = self.places.push(PlaceInfo::new(ty, Some(elem)));
515            self.places[next].next_sibling = self.places[base].first_child;
516            self.places[base].first_child = Some(next);
517            next
518        })
519    }
520
521    /// Precompute the list of values inside `root` and store it inside
522    /// as a slice within `inner_values_buffer`.
523    fn cache_preorder_invoke(&mut self, root: PlaceIndex) {
524        let start = self.inner_values_buffer.len();
525        if let Some(vi) = self.places[root].value_index {
526            self.inner_values_buffer.push(vi);
527        }
528
529        // We manually iterate instead of using `children` as we need to mutate `self`.
530        let mut next_child = self.places[root].first_child;
531        while let Some(child) = next_child {
532            ensure_sufficient_stack(|| self.cache_preorder_invoke(child));
533            next_child = self.places[child].next_sibling;
534        }
535
536        let end = self.inner_values_buffer.len();
537        self.inner_values[root] = start..end;
538    }
539}
540
541struct PlaceCollector<'a, 'tcx> {
542    tcx: TyCtxt<'tcx>,
543    body: &'a Body<'tcx>,
544    map: &'a mut Map<'tcx>,
545    assignments: FxIndexSet<(PlaceIndex, PlaceIndex)>,
546}
547
548impl<'tcx> PlaceCollector<'_, 'tcx> {
549    #[tracing::instrument(level = "trace", skip(self))]
550    fn register_place(&mut self, place: Place<'tcx>) -> Option<PlaceIndex> {
551        // Create a place for this projection.
552        let mut place_index = self.map.locals[place.local]?;
553        let mut ty = PlaceTy::from_ty(self.body.local_decls[place.local].ty);
554        tracing::trace!(?place_index, ?ty);
555
556        if let ty::Ref(_, ref_ty, _) | ty::RawPtr(ref_ty, _) = ty.ty.kind()
557            && let ty::Slice(..) = ref_ty.kind()
558        {
559            self.map.register_place(self.tcx.types.usize, place_index, TrackElem::DerefLen);
560        } else if ty.ty.is_enum() {
561            let discriminant_ty = ty.ty.discriminant_ty(self.tcx);
562            self.map.register_place(discriminant_ty, place_index, TrackElem::Discriminant);
563        }
564
565        for proj in place.projection {
566            let track_elem = proj.try_into().ok()?;
567            ty = ty.projection_ty(self.tcx, proj);
568            place_index = self.map.register_place(ty.ty, place_index, track_elem);
569            tracing::trace!(?proj, ?place_index, ?ty);
570
571            if let ty::Ref(_, ref_ty, _) | ty::RawPtr(ref_ty, _) = ty.ty.kind()
572                && let ty::Slice(..) = ref_ty.kind()
573            {
574                self.map.register_place(self.tcx.types.usize, place_index, TrackElem::DerefLen);
575            } else if ty.ty.is_enum() {
576                let discriminant_ty = ty.ty.discriminant_ty(self.tcx);
577                self.map.register_place(discriminant_ty, place_index, TrackElem::Discriminant);
578            }
579        }
580
581        Some(place_index)
582    }
583}
584
585impl<'tcx> Visitor<'tcx> for PlaceCollector<'_, 'tcx> {
586    #[tracing::instrument(level = "trace", skip(self))]
587    fn visit_place(&mut self, place: &Place<'tcx>, ctxt: PlaceContext, _: Location) {
588        if !ctxt.is_use() {
589            return;
590        }
591
592        self.register_place(*place);
593    }
594
595    fn visit_assign(&mut self, lhs: &Place<'tcx>, rhs: &Rvalue<'tcx>, location: Location) {
596        self.super_assign(lhs, rhs, location);
597
598        match rhs {
599            Rvalue::Use(Operand::Move(rhs) | Operand::Copy(rhs)) | Rvalue::CopyForDeref(rhs) => {
600                let Some(lhs) = self.register_place(*lhs) else { return };
601                let Some(rhs) = self.register_place(*rhs) else { return };
602                self.assignments.insert((lhs, rhs));
603            }
604            Rvalue::Aggregate(kind, fields) => {
605                let Some(mut lhs) = self.register_place(*lhs) else { return };
606                match **kind {
607                    // Do not propagate unions.
608                    AggregateKind::Adt(_, _, _, _, Some(_)) => return,
609                    AggregateKind::Adt(_, variant, _, _, None) => {
610                        let ty = self.map.places[lhs].ty;
611                        if ty.is_enum() {
612                            lhs = self.map.register_place(ty, lhs, TrackElem::Variant(variant));
613                        }
614                    }
615                    AggregateKind::RawPtr(..)
616                    | AggregateKind::Array(_)
617                    | AggregateKind::Tuple
618                    | AggregateKind::Closure(..)
619                    | AggregateKind::Coroutine(..)
620                    | AggregateKind::CoroutineClosure(..) => {}
621                }
622                for (index, field) in fields.iter_enumerated() {
623                    if let Some(rhs) = field.place()
624                        && let Some(rhs) = self.register_place(rhs)
625                    {
626                        let lhs = self.map.register_place(
627                            self.map.places[rhs].ty,
628                            lhs,
629                            TrackElem::Field(index),
630                        );
631                        self.assignments.insert((lhs, rhs));
632                    }
633                }
634            }
635            _ => {}
636        }
637    }
638}
639
640impl<'tcx> Map<'tcx> {
641    /// Applies a single projection element, yielding the corresponding child.
642    pub fn apply(&self, place: PlaceIndex, elem: TrackElem) -> Option<PlaceIndex> {
643        self.projections.get(&(place, elem)).copied()
644    }
645
646    /// Locates the given place, if it exists in the tree.
647    fn find_extra(
648        &self,
649        place: PlaceRef<'_>,
650        extra: impl IntoIterator<Item = TrackElem>,
651    ) -> Option<PlaceIndex> {
652        let mut index = *self.locals[place.local].as_ref()?;
653
654        for &elem in place.projection {
655            index = self.apply(index, elem.try_into().ok()?)?;
656        }
657        for elem in extra {
658            index = self.apply(index, elem)?;
659        }
660
661        Some(index)
662    }
663
664    /// Locates the given place, if it exists in the tree.
665    pub fn find(&self, place: PlaceRef<'_>) -> Option<PlaceIndex> {
666        self.find_extra(place, [])
667    }
668
669    /// Locates the given place and applies `Discriminant`, if it exists in the tree.
670    pub fn find_discr(&self, place: PlaceRef<'_>) -> Option<PlaceIndex> {
671        self.find_extra(place, [TrackElem::Discriminant])
672    }
673
674    /// Locates the given place and applies `DerefLen`, if it exists in the tree.
675    pub fn find_len(&self, place: PlaceRef<'_>) -> Option<PlaceIndex> {
676        self.find_extra(place, [TrackElem::DerefLen])
677    }
678
679    /// Iterate over all direct children.
680    fn children(
681        &self,
682        parent: PlaceIndex,
683    ) -> impl Iterator<Item = PlaceIndex> + Captures<'_> + Captures<'tcx> {
684        Children::new(self, parent)
685    }
686
687    /// Invoke a function on the given place and all places that may alias it.
688    ///
689    /// In particular, when the given place has a variant downcast, we invoke the function on all
690    /// the other variants.
691    ///
692    /// `tail_elem` allows to support discriminants that are not a place in MIR, but that we track
693    /// as such.
694    fn for_each_aliasing_place(
695        &self,
696        place: PlaceRef<'_>,
697        tail_elem: Option<TrackElem>,
698        f: &mut impl FnMut(ValueIndex),
699    ) {
700        if place.is_indirect_first_projection() {
701            // We do not track indirect places.
702            return;
703        }
704        let Some(mut index) = self.locals[place.local] else {
705            // The local is not tracked at all, so it does not alias anything.
706            return;
707        };
708        let elems = place.projection.iter().map(|&elem| elem.try_into()).chain(tail_elem.map(Ok));
709        for elem in elems {
710            // A field aliases the parent place.
711            if let Some(vi) = self.places[index].value_index {
712                f(vi);
713            }
714
715            let Ok(elem) = elem else { return };
716            let sub = self.apply(index, elem);
717            if let TrackElem::Variant(..) | TrackElem::Discriminant = elem {
718                // Enum variant fields and enum discriminants alias each another.
719                self.for_each_variant_sibling(index, sub, f);
720            }
721            if let Some(sub) = sub {
722                index = sub
723            } else {
724                return;
725            }
726        }
727        self.for_each_value_inside(index, f);
728    }
729
730    /// Invoke the given function on all the descendants of the given place, except one branch.
731    fn for_each_variant_sibling(
732        &self,
733        parent: PlaceIndex,
734        preserved_child: Option<PlaceIndex>,
735        f: &mut impl FnMut(ValueIndex),
736    ) {
737        for sibling in self.children(parent) {
738            let elem = self.places[sibling].proj_elem;
739            // Only invalidate variants and discriminant. Fields (for coroutines) are not
740            // invalidated by assignment to a variant.
741            if let Some(TrackElem::Variant(..) | TrackElem::Discriminant) = elem
742                // Only invalidate the other variants, the current one is fine.
743                && Some(sibling) != preserved_child
744            {
745                self.for_each_value_inside(sibling, f);
746            }
747        }
748    }
749
750    /// Invoke a function on each value in the given place and all descendants.
751    fn for_each_value_inside(&self, root: PlaceIndex, f: &mut impl FnMut(ValueIndex)) {
752        let range = self.inner_values[root].clone();
753        let values = &self.inner_values_buffer[range];
754        for &v in values {
755            f(v)
756        }
757    }
758
759    /// Invoke a function on each value in the given place and all descendants.
760    pub fn for_each_projection_value<O>(
761        &self,
762        root: PlaceIndex,
763        value: O,
764        project: &mut impl FnMut(TrackElem, &O) -> Option<O>,
765        f: &mut impl FnMut(PlaceIndex, &O),
766    ) {
767        // Fast path is there is nothing to do.
768        if self.inner_values[root].is_empty() {
769            return;
770        }
771
772        if self.places[root].value_index.is_some() {
773            f(root, &value)
774        }
775
776        for child in self.children(root) {
777            let elem = self.places[child].proj_elem.unwrap();
778            if let Some(value) = project(elem, &value) {
779                self.for_each_projection_value(child, value, project, f);
780            }
781        }
782    }
783}
784
785/// This is the information tracked for every [`PlaceIndex`] and is stored by [`Map`].
786///
787/// Together, `first_child` and `next_sibling` form an intrusive linked list, which is used to
788/// model a tree structure (a replacement for a member like `children: Vec<PlaceIndex>`).
789#[derive(Debug)]
790struct PlaceInfo<'tcx> {
791    /// Type of the referenced place.
792    ty: Ty<'tcx>,
793
794    /// We store a [`ValueIndex`] if and only if the placed is tracked by the analysis.
795    value_index: Option<ValueIndex>,
796
797    /// The projection used to go from parent to this node (only None for root).
798    proj_elem: Option<TrackElem>,
799
800    /// The leftmost child.
801    first_child: Option<PlaceIndex>,
802
803    /// Index of the sibling to the right of this node.
804    next_sibling: Option<PlaceIndex>,
805}
806
807impl<'tcx> PlaceInfo<'tcx> {
808    fn new(ty: Ty<'tcx>, proj_elem: Option<TrackElem>) -> Self {
809        Self { ty, next_sibling: None, first_child: None, proj_elem, value_index: None }
810    }
811}
812
813struct Children<'a, 'tcx> {
814    map: &'a Map<'tcx>,
815    next: Option<PlaceIndex>,
816}
817
818impl<'a, 'tcx> Children<'a, 'tcx> {
819    fn new(map: &'a Map<'tcx>, parent: PlaceIndex) -> Self {
820        Self { map, next: map.places[parent].first_child }
821    }
822}
823
824impl Iterator for Children<'_, '_> {
825    type Item = PlaceIndex;
826
827    fn next(&mut self) -> Option<Self::Item> {
828        match self.next {
829            Some(child) => {
830                self.next = self.map.places[child].next_sibling;
831                Some(child)
832            }
833            None => None,
834        }
835    }
836}
837
838/// Used as the result of an operand or r-value.
839#[derive(Debug)]
840pub enum ValueOrPlace<V> {
841    Value(V),
842    Place(PlaceIndex),
843}
844
845impl<V: HasTop> ValueOrPlace<V> {
846    pub const TOP: Self = ValueOrPlace::Value(V::TOP);
847}
848
849/// The set of projection elements that can be used by a tracked place.
850///
851/// Although only field projections are currently allowed, this could change in the future.
852#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
853pub enum TrackElem {
854    Field(FieldIdx),
855    Variant(VariantIdx),
856    Discriminant,
857    // Length of a slice.
858    DerefLen,
859}
860
861impl<V, T> TryFrom<ProjectionElem<V, T>> for TrackElem {
862    type Error = ();
863
864    fn try_from(value: ProjectionElem<V, T>) -> Result<Self, Self::Error> {
865        match value {
866            ProjectionElem::Field(field, _) => Ok(TrackElem::Field(field)),
867            ProjectionElem::Downcast(_, idx) => Ok(TrackElem::Variant(idx)),
868            _ => Err(()),
869        }
870    }
871}
872
873/// Invokes `f` on all direct fields of `ty`.
874pub fn iter_fields<'tcx>(
875    ty: Ty<'tcx>,
876    tcx: TyCtxt<'tcx>,
877    typing_env: ty::TypingEnv<'tcx>,
878    mut f: impl FnMut(Option<VariantIdx>, FieldIdx, Ty<'tcx>),
879) {
880    match ty.kind() {
881        ty::Tuple(list) => {
882            for (field, ty) in list.iter().enumerate() {
883                f(None, field.into(), ty);
884            }
885        }
886        ty::Adt(def, args) => {
887            if def.is_union() {
888                return;
889            }
890            for (v_index, v_def) in def.variants().iter_enumerated() {
891                let variant = if def.is_struct() { None } else { Some(v_index) };
892                for (f_index, f_def) in v_def.fields.iter().enumerate() {
893                    let field_ty = f_def.ty(tcx, args);
894                    let field_ty = tcx
895                        .try_normalize_erasing_regions(typing_env, field_ty)
896                        .unwrap_or_else(|_| tcx.erase_regions(field_ty));
897                    f(variant, f_index.into(), field_ty);
898                }
899            }
900        }
901        ty::Closure(_, args) => {
902            iter_fields(args.as_closure().tupled_upvars_ty(), tcx, typing_env, f);
903        }
904        ty::Coroutine(_, args) => {
905            iter_fields(args.as_coroutine().tupled_upvars_ty(), tcx, typing_env, f);
906        }
907        ty::CoroutineClosure(_, args) => {
908            iter_fields(args.as_coroutine_closure().tupled_upvars_ty(), tcx, typing_env, f);
909        }
910        _ => (),
911    }
912}
913
914/// Returns all locals with projections that have their reference or address taken.
915pub fn excluded_locals(body: &Body<'_>) -> DenseBitSet<Local> {
916    struct Collector {
917        result: DenseBitSet<Local>,
918    }
919
920    impl<'tcx> Visitor<'tcx> for Collector {
921        fn visit_place(&mut self, place: &Place<'tcx>, context: PlaceContext, _location: Location) {
922            if (context.is_borrow()
923                || context.is_address_of()
924                || context.is_drop()
925                || context == PlaceContext::MutatingUse(MutatingUseContext::AsmOutput))
926                && !place.is_indirect()
927            {
928                // A pointer to a place could be used to access other places with the same local,
929                // hence we have to exclude the local completely.
930                self.result.insert(place.local);
931            }
932        }
933    }
934
935    let mut collector = Collector { result: DenseBitSet::new_empty(body.local_decls.len()) };
936    collector.visit_body(body);
937    collector.result
938}
939
940fn debug_with_context_rec<V: Debug + Eq + HasBottom>(
941    place: PlaceIndex,
942    place_str: &str,
943    new: &StateData<V>,
944    old: Option<&StateData<V>>,
945    map: &Map<'_>,
946    f: &mut Formatter<'_>,
947) -> std::fmt::Result {
948    if let Some(value) = map.places[place].value_index {
949        match old {
950            None => writeln!(f, "{}: {:?}", place_str, new.get(value))?,
951            Some(old) => {
952                if new.get(value) != old.get(value) {
953                    writeln!(f, "\u{001f}-{}: {:?}", place_str, old.get(value))?;
954                    writeln!(f, "\u{001f}+{}: {:?}", place_str, new.get(value))?;
955                }
956            }
957        }
958    }
959
960    for child in map.children(place) {
961        let info_elem = map.places[child].proj_elem.unwrap();
962        let child_place_str = match info_elem {
963            TrackElem::Discriminant => {
964                format!("discriminant({place_str})")
965            }
966            TrackElem::Variant(idx) => {
967                format!("({place_str} as {idx:?})")
968            }
969            TrackElem::Field(field) => {
970                if place_str.starts_with('*') {
971                    format!("({}).{}", place_str, field.index())
972                } else {
973                    format!("{}.{}", place_str, field.index())
974                }
975            }
976            TrackElem::DerefLen => {
977                format!("Len(*{})", place_str)
978            }
979        };
980        debug_with_context_rec(child, &child_place_str, new, old, map, f)?;
981    }
982
983    Ok(())
984}
985
986pub fn debug_with_context<V: Debug + Eq + HasBottom>(
987    new: &StateData<V>,
988    old: Option<&StateData<V>>,
989    map: &Map<'_>,
990    f: &mut Formatter<'_>,
991) -> std::fmt::Result {
992    for (local, place) in map.locals.iter_enumerated() {
993        if let Some(place) = place {
994            debug_with_context_rec(*place, &format!("{local:?}"), new, old, map, f)?;
995        }
996    }
997    Ok(())
998}