Skip to main content

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 rustc_mir_dataflow::Analysis;
18use tracing::{debug, instrument, trace};
19
20pub(super) struct SsaLocals {
21    /// Assignments to each local. This defines whether the local is SSA.
22    assignments: IndexVec<Local, Set1<DefLocation>>,
23    /// We visit the body in reverse postorder, to ensure each local is assigned before it is used.
24    /// We remember the order in which we saw the assignments to compute the SSA values in a single
25    /// pass.
26    assignment_order: Vec<Local>,
27    /// Copy equivalence classes between locals. See `copy_classes` for documentation.
28    copy_classes: IndexVec<Local, Local>,
29    /// Number of "direct" uses of each local, i.e. uses that are not dereferences.
30    /// We ignore non-uses (Storage statements, debuginfo).
31    direct_uses: IndexVec<Local, u32>,
32    /// Set of SSA locals that are immutably borrowed.
33    borrowed_locals: DenseBitSet<Local>,
34}
35
36impl SsaLocals {
37    pub(super) fn new<'tcx>(
38        tcx: TyCtxt<'tcx>,
39        body: &Body<'tcx>,
40        typing_env: ty::TypingEnv<'tcx>,
41    ) -> SsaLocals {
42        let assignment_order = Vec::with_capacity(body.local_decls.len());
43
44        let assignments = IndexVec::from_elem(Set1::Empty, &body.local_decls);
45        let dominators = body.basic_blocks.dominators();
46
47        let direct_uses = IndexVec::from_elem(0, &body.local_decls);
48        let borrowed_locals = DenseBitSet::new_empty(body.local_decls.len());
49        let mut visitor = SsaVisitor {
50            body,
51            assignments,
52            assignment_order,
53            dominators,
54            direct_uses,
55            borrowed_locals,
56        };
57
58        for local in body.args_iter() {
59            visitor.assignments[local] = Set1::One(DefLocation::Argument);
60            visitor.assignment_order.push(local);
61        }
62
63        // For SSA assignments, a RPO visit will see the assignment before it sees any use.
64        // We only visit reachable nodes: computing `dominates` on an unreachable node ICEs.
65        for (bb, data) in traversal::reverse_postorder(body) {
66            visitor.visit_basic_block_data(bb, data);
67        }
68
69        for var_debug_info in &body.var_debug_info {
70            visitor.visit_var_debug_info(var_debug_info);
71        }
72
73        // The immutability of shared borrows only works on `Freeze` locals. If the visitor found
74        // borrows, we need to check the types. For raw pointers and mutable borrows, the locals
75        // have already been marked as non-SSA.
76        {
    use ::tracing::__macro_support::Callsite as _;
    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
        {
            static META: ::tracing::Metadata<'static> =
                {
                    ::tracing_core::metadata::Metadata::new("event compiler/rustc_mir_transform/src/ssa.rs:76",
                        "rustc_mir_transform::ssa", ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_mir_transform/src/ssa.rs"),
                        ::tracing_core::__macro_support::Option::Some(76u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_mir_transform::ssa"),
                        ::tracing_core::field::FieldSet::new(&["visitor.borrowed_locals"],
                            ::tracing_core::callsite::Identifier(&__CALLSITE)),
                        ::tracing::metadata::Kind::EVENT)
                };
            ::tracing::callsite::DefaultCallsite::new(&META)
        };
    let enabled =
        ::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
                &&
                ::tracing::Level::DEBUG <=
                    ::tracing::level_filters::LevelFilter::current() &&
            {
                let interest = __CALLSITE.interest();
                !interest.is_never() &&
                    ::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
                        interest)
            };
    if enabled {
        (|value_set: ::tracing::field::ValueSet|
                    {
                        let meta = __CALLSITE.metadata();
                        ::tracing::Event::dispatch(meta, &value_set);
                        ;
                    })({
                #[allow(unused_imports)]
                use ::tracing::field::{debug, display, Value};
                let mut iter = __CALLSITE.metadata().fields().iter();
                __CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                    ::tracing::__macro_support::Option::Some(&debug(&visitor.borrowed_locals)
                                            as &dyn Value))])
            });
    } else { ; }
};debug!(?visitor.borrowed_locals);
77        for local in visitor.borrowed_locals.iter() {
78            if !body.local_decls[local].ty.is_freeze(tcx, typing_env) {
79                visitor.assignments[local] = Set1::Many;
80            }
81        }
82
83        {
    use ::tracing::__macro_support::Callsite as _;
    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
        {
            static META: ::tracing::Metadata<'static> =
                {
                    ::tracing_core::metadata::Metadata::new("event compiler/rustc_mir_transform/src/ssa.rs:83",
                        "rustc_mir_transform::ssa", ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_mir_transform/src/ssa.rs"),
                        ::tracing_core::__macro_support::Option::Some(83u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_mir_transform::ssa"),
                        ::tracing_core::field::FieldSet::new(&["visitor.assignments"],
                            ::tracing_core::callsite::Identifier(&__CALLSITE)),
                        ::tracing::metadata::Kind::EVENT)
                };
            ::tracing::callsite::DefaultCallsite::new(&META)
        };
    let enabled =
        ::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
                &&
                ::tracing::Level::DEBUG <=
                    ::tracing::level_filters::LevelFilter::current() &&
            {
                let interest = __CALLSITE.interest();
                !interest.is_never() &&
                    ::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
                        interest)
            };
    if enabled {
        (|value_set: ::tracing::field::ValueSet|
                    {
                        let meta = __CALLSITE.metadata();
                        ::tracing::Event::dispatch(meta, &value_set);
                        ;
                    })({
                #[allow(unused_imports)]
                use ::tracing::field::{debug, display, Value};
                let mut iter = __CALLSITE.metadata().fields().iter();
                __CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                    ::tracing::__macro_support::Option::Some(&debug(&visitor.assignments)
                                            as &dyn Value))])
            });
    } else { ; }
};debug!(?visitor.assignments);
84        {
    use ::tracing::__macro_support::Callsite as _;
    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
        {
            static META: ::tracing::Metadata<'static> =
                {
                    ::tracing_core::metadata::Metadata::new("event compiler/rustc_mir_transform/src/ssa.rs:84",
                        "rustc_mir_transform::ssa", ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_mir_transform/src/ssa.rs"),
                        ::tracing_core::__macro_support::Option::Some(84u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_mir_transform::ssa"),
                        ::tracing_core::field::FieldSet::new(&["visitor.direct_uses"],
                            ::tracing_core::callsite::Identifier(&__CALLSITE)),
                        ::tracing::metadata::Kind::EVENT)
                };
            ::tracing::callsite::DefaultCallsite::new(&META)
        };
    let enabled =
        ::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
                &&
                ::tracing::Level::DEBUG <=
                    ::tracing::level_filters::LevelFilter::current() &&
            {
                let interest = __CALLSITE.interest();
                !interest.is_never() &&
                    ::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
                        interest)
            };
    if enabled {
        (|value_set: ::tracing::field::ValueSet|
                    {
                        let meta = __CALLSITE.metadata();
                        ::tracing::Event::dispatch(meta, &value_set);
                        ;
                    })({
                #[allow(unused_imports)]
                use ::tracing::field::{debug, display, Value};
                let mut iter = __CALLSITE.metadata().fields().iter();
                __CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                    ::tracing::__macro_support::Option::Some(&debug(&visitor.direct_uses)
                                            as &dyn Value))])
            });
    } else { ; }
};debug!(?visitor.direct_uses);
85
86        visitor
87            .assignment_order
88            .retain(|&local| #[allow(non_exhaustive_omitted_patterns)] match visitor.assignments[local] {
    Set1::One(_) => true,
    _ => false,
}matches!(visitor.assignments[local], Set1::One(_)));
89        {
    use ::tracing::__macro_support::Callsite as _;
    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
        {
            static META: ::tracing::Metadata<'static> =
                {
                    ::tracing_core::metadata::Metadata::new("event compiler/rustc_mir_transform/src/ssa.rs:89",
                        "rustc_mir_transform::ssa", ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_mir_transform/src/ssa.rs"),
                        ::tracing_core::__macro_support::Option::Some(89u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_mir_transform::ssa"),
                        ::tracing_core::field::FieldSet::new(&["visitor.assignment_order"],
                            ::tracing_core::callsite::Identifier(&__CALLSITE)),
                        ::tracing::metadata::Kind::EVENT)
                };
            ::tracing::callsite::DefaultCallsite::new(&META)
        };
    let enabled =
        ::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
                &&
                ::tracing::Level::DEBUG <=
                    ::tracing::level_filters::LevelFilter::current() &&
            {
                let interest = __CALLSITE.interest();
                !interest.is_never() &&
                    ::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
                        interest)
            };
    if enabled {
        (|value_set: ::tracing::field::ValueSet|
                    {
                        let meta = __CALLSITE.metadata();
                        ::tracing::Event::dispatch(meta, &value_set);
                        ;
                    })({
                #[allow(unused_imports)]
                use ::tracing::field::{debug, display, Value};
                let mut iter = __CALLSITE.metadata().fields().iter();
                __CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                    ::tracing::__macro_support::Option::Some(&debug(&visitor.assignment_order)
                                            as &dyn Value))])
            });
    } else { ; }
};debug!(?visitor.assignment_order);
90
91        let mut ssa = SsaLocals {
92            assignments: visitor.assignments,
93            assignment_order: visitor.assignment_order,
94            direct_uses: visitor.direct_uses,
95            borrowed_locals: visitor.borrowed_locals,
96            // This is filled by `compute_copy_classes`.
97            copy_classes: IndexVec::default(),
98        };
99        compute_copy_classes(&mut ssa, body);
100        ssa
101    }
102
103    pub(super) fn num_locals(&self) -> usize {
104        self.assignments.len()
105    }
106
107    pub(super) fn locals(&self) -> impl Iterator<Item = Local> {
108        self.assignments.indices()
109    }
110
111    pub(super) fn is_ssa(&self, local: Local) -> bool {
112        #[allow(non_exhaustive_omitted_patterns)] match self.assignments[local] {
    Set1::One(_) => true,
    _ => false,
}matches!(self.assignments[local], Set1::One(_))
113    }
114
115    /// Return the number of uses if a local that are not "Deref".
116    pub(super) fn num_direct_uses(&self, local: Local) -> u32 {
117        self.direct_uses[local]
118    }
119
120    #[inline]
121    pub(super) fn assignment_dominates(
122        &self,
123        dominators: &Dominators<BasicBlock>,
124        local: Local,
125        location: Location,
126    ) -> bool {
127        match self.assignments[local] {
128            Set1::One(def) => def.dominates(location, dominators),
129            _ => false,
130        }
131    }
132
133    pub(super) fn assignments<'a, 'tcx>(
134        &'a self,
135        body: &'a Body<'tcx>,
136    ) -> impl Iterator<Item = (Local, &'a Rvalue<'tcx>, Location)> {
137        self.assignment_order.iter().filter_map(|&local| {
138            if let Set1::One(DefLocation::Assignment(loc)) = self.assignments[local] {
139                let stmt = body.stmt_at(loc).left()?;
140                // `loc` must point to a direct assignment to `local`.
141                let Some((target, rvalue)) = stmt.kind.as_assign() else { ::rustc_middle::util::bug::bug_fmt(format_args!("impossible case reached"))bug!() };
142                match (&target.as_local(), &Some(local)) {
    (left_val, right_val) => {
        if !(*left_val == *right_val) {
            let kind = ::core::panicking::AssertKind::Eq;
            ::core::panicking::assert_failed(kind, &*left_val, &*right_val,
                ::core::option::Option::None);
        }
    }
};assert_eq!(target.as_local(), Some(local));
143                Some((local, rvalue, loc))
144            } else {
145                None
146            }
147        })
148    }
149
150    /// Compute the equivalence classes for locals, based on copy statements.
151    ///
152    /// The returned vector maps each local to the one it copies. In the following case:
153    ///   _a = &mut _0
154    ///   _b = move? _a
155    ///   _c = move? _a
156    ///   _d = move? _c
157    /// We return the mapping
158    ///   _a => _a // not a copy so, represented by itself
159    ///   _b => _a
160    ///   _c => _a
161    ///   _d => _a // transitively through _c
162    ///
163    /// Exception: we do not see through the return place, as it cannot be instantiated.
164    pub(super) fn copy_classes(&self) -> &IndexSlice<Local, Local> {
165        &self.copy_classes
166    }
167
168    /// Set of SSA locals that are immutably borrowed.
169    pub(super) fn borrowed_locals(&self) -> &DenseBitSet<Local> {
170        &self.borrowed_locals
171    }
172
173    /// Make a property uniform on a copy equivalence class by removing elements.
174    pub(super) fn meet_copy_equivalence(&self, property: &mut DenseBitSet<Local>) {
175        // Consolidate to have a local iff all its copies are.
176        //
177        // `copy_classes` defines equivalence classes between locals. The `local`s that recursively
178        // move/copy the same local all have the same `head`.
179        for (local, &head) in self.copy_classes.iter_enumerated() {
180            // If any copy does not have `property`, then the head is not.
181            if !property.contains(local) {
182                property.remove(head);
183            }
184        }
185        for (local, &head) in self.copy_classes.iter_enumerated() {
186            // If any copy does not have `property`, then the head doesn't either,
187            // then no copy has `property`.
188            if !property.contains(head) {
189                property.remove(local);
190            }
191        }
192
193        // Verify that we correctly computed equivalence classes.
194        #[cfg(debug_assertions)]
195        for (local, &head) in self.copy_classes.iter_enumerated() {
196            match (&property.contains(local), &property.contains(head)) {
    (left_val, right_val) => {
        if !(*left_val == *right_val) {
            let kind = ::core::panicking::AssertKind::Eq;
            ::core::panicking::assert_failed(kind, &*left_val, &*right_val,
                ::core::option::Option::None);
        }
    }
};assert_eq!(property.contains(local), property.contains(head));
197        }
198    }
199}
200
201struct SsaVisitor<'a, 'tcx> {
202    body: &'a Body<'tcx>,
203    dominators: &'a Dominators<BasicBlock>,
204    assignments: IndexVec<Local, Set1<DefLocation>>,
205    assignment_order: Vec<Local>,
206    direct_uses: IndexVec<Local, u32>,
207    // Track locals that are immutably borrowed, so we can check their type is `Freeze` later.
208    borrowed_locals: DenseBitSet<Local>,
209}
210
211impl SsaVisitor<'_, '_> {
212    fn check_dominates(&mut self, local: Local, loc: Location) {
213        let set = &mut self.assignments[local];
214        let assign_dominates = match *set {
215            Set1::Empty | Set1::Many => false,
216            Set1::One(def) => def.dominates(loc, self.dominators),
217        };
218        // We are visiting a use that is not dominated by an assignment.
219        // Either there is a cycle involved, or we are reading for uninitialized local.
220        // Bail out.
221        if !assign_dominates {
222            *set = Set1::Many;
223        }
224    }
225}
226
227impl<'tcx> Visitor<'tcx> for SsaVisitor<'_, 'tcx> {
228    fn visit_local(&mut self, local: Local, ctxt: PlaceContext, loc: Location) {
229        if ctxt.may_observe_address() {
230            self.borrowed_locals.insert(local);
231        }
232        match ctxt {
233            PlaceContext::MutatingUse(MutatingUseContext::Projection)
234            | PlaceContext::NonMutatingUse(NonMutatingUseContext::Projection) => ::rustc_middle::util::bug::bug_fmt(format_args!("impossible case reached"))bug!(),
235            // Anything can happen with raw pointers, so remove them.
236            PlaceContext::NonMutatingUse(NonMutatingUseContext::RawBorrow)
237            | PlaceContext::MutatingUse(_) => {
238                self.assignments[local] = Set1::Many;
239            }
240            // Immutable borrows are ok, but we need to delay a check that the type is `Freeze`.
241            PlaceContext::NonMutatingUse(
242                NonMutatingUseContext::SharedBorrow | NonMutatingUseContext::FakeBorrow,
243            ) => {
244                self.check_dominates(local, loc);
245                self.direct_uses[local] += 1;
246            }
247            PlaceContext::NonMutatingUse(_) => {
248                self.check_dominates(local, loc);
249                self.direct_uses[local] += 1;
250            }
251            PlaceContext::NonUse(_) => {}
252        }
253    }
254
255    fn visit_place(&mut self, place: &Place<'tcx>, ctxt: PlaceContext, loc: Location) {
256        let location = match ctxt {
257            PlaceContext::MutatingUse(MutatingUseContext::Store) => {
258                Some(DefLocation::Assignment(loc))
259            }
260            PlaceContext::MutatingUse(MutatingUseContext::Call) => {
261                let call = loc.block;
262                let TerminatorKind::Call { target, .. } =
263                    self.body.basic_blocks[call].terminator().kind
264                else {
265                    ::rustc_middle::util::bug::bug_fmt(format_args!("impossible case reached"))bug!()
266                };
267                Some(DefLocation::CallReturn { call, target })
268            }
269            _ => None,
270        };
271        if let Some(location) = location
272            && let Some(local) = place.as_local()
273        {
274            self.assignments[local].insert(location);
275            if let Set1::One(_) = self.assignments[local] {
276                // Only record if SSA-like, to avoid growing the vector needlessly.
277                self.assignment_order.push(local);
278            }
279        } else if place.projection.first() == Some(&PlaceElem::Deref) {
280            // Do not do anything for debuginfo.
281            if ctxt.is_use() {
282                // Only change the context if it is a real use, not a "use" in debuginfo.
283                let new_ctxt = PlaceContext::NonMutatingUse(NonMutatingUseContext::Copy);
284
285                self.visit_projection(place.as_ref(), new_ctxt, loc);
286                self.check_dominates(place.local, loc);
287            }
288        } else {
289            self.visit_projection(place.as_ref(), ctxt, loc);
290            self.visit_local(place.local, ctxt, loc);
291        }
292    }
293}
294
295#[allow(clippy :: suspicious_else_formatting)]
{
    let __tracing_attr_span;
    let __tracing_attr_guard;
    if ::tracing::Level::TRACE <= ::tracing::level_filters::STATIC_MAX_LEVEL
                &&
                ::tracing::Level::TRACE <=
                    ::tracing::level_filters::LevelFilter::current() ||
            { false } {
        __tracing_attr_span =
            {
                use ::tracing::__macro_support::Callsite as _;
                static __CALLSITE: ::tracing::callsite::DefaultCallsite =
                    {
                        static META: ::tracing::Metadata<'static> =
                            {
                                ::tracing_core::metadata::Metadata::new("compute_copy_classes",
                                    "rustc_mir_transform::ssa", ::tracing::Level::TRACE,
                                    ::tracing_core::__macro_support::Option::Some("compiler/rustc_mir_transform/src/ssa.rs"),
                                    ::tracing_core::__macro_support::Option::Some(295u32),
                                    ::tracing_core::__macro_support::Option::Some("rustc_mir_transform::ssa"),
                                    ::tracing_core::field::FieldSet::new(&[],
                                        ::tracing_core::callsite::Identifier(&__CALLSITE)),
                                    ::tracing::metadata::Kind::SPAN)
                            };
                        ::tracing::callsite::DefaultCallsite::new(&META)
                    };
                let mut interest = ::tracing::subscriber::Interest::never();
                if ::tracing::Level::TRACE <=
                                    ::tracing::level_filters::STATIC_MAX_LEVEL &&
                                ::tracing::Level::TRACE <=
                                    ::tracing::level_filters::LevelFilter::current() &&
                            { interest = __CALLSITE.interest(); !interest.is_never() }
                        &&
                        ::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
                            interest) {
                    let meta = __CALLSITE.metadata();
                    ::tracing::Span::new(meta,
                        &{ meta.fields().value_set(&[]) })
                } else {
                    let span =
                        ::tracing::__macro_support::__disabled_span(__CALLSITE.metadata());
                    {};
                    span
                }
            };
        __tracing_attr_guard = __tracing_attr_span.enter();
    }

    #[warn(clippy :: suspicious_else_formatting)]
    {

        #[allow(unknown_lints, unreachable_code, clippy ::
        diverging_sub_expression, clippy :: empty_loop, clippy ::
        let_unit_value, clippy :: let_with_type_underscore, clippy ::
        needless_return, clippy :: unreachable)]
        if false {
            let __tracing_attr_fake_return: () = loop {};
            return __tracing_attr_fake_return;
        }
        {
            let mut direct_uses = std::mem::take(&mut ssa.direct_uses);
            let mut copies =
                IndexVec::from_fn_n(|l| l, body.local_decls.len());
            for (local, rvalue, _) in ssa.assignments(body) {
                let Rvalue::Use(Operand::Copy(place) | Operand::Move(place)) =
                    rvalue else { continue; };
                let Some(rhs) = place.as_local() else { continue };
                let local_ty = body.local_decls()[local].ty;
                let rhs_ty = body.local_decls()[rhs].ty;
                if local_ty != rhs_ty {
                    {
                        use ::tracing::__macro_support::Callsite as _;
                        static __CALLSITE: ::tracing::callsite::DefaultCallsite =
                            {
                                static META: ::tracing::Metadata<'static> =
                                    {
                                        ::tracing_core::metadata::Metadata::new("event compiler/rustc_mir_transform/src/ssa.rs:310",
                                            "rustc_mir_transform::ssa", ::tracing::Level::TRACE,
                                            ::tracing_core::__macro_support::Option::Some("compiler/rustc_mir_transform/src/ssa.rs"),
                                            ::tracing_core::__macro_support::Option::Some(310u32),
                                            ::tracing_core::__macro_support::Option::Some("rustc_mir_transform::ssa"),
                                            ::tracing_core::field::FieldSet::new(&["message"],
                                                ::tracing_core::callsite::Identifier(&__CALLSITE)),
                                            ::tracing::metadata::Kind::EVENT)
                                    };
                                ::tracing::callsite::DefaultCallsite::new(&META)
                            };
                        let enabled =
                            ::tracing::Level::TRACE <=
                                        ::tracing::level_filters::STATIC_MAX_LEVEL &&
                                    ::tracing::Level::TRACE <=
                                        ::tracing::level_filters::LevelFilter::current() &&
                                {
                                    let interest = __CALLSITE.interest();
                                    !interest.is_never() &&
                                        ::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
                                            interest)
                                };
                        if enabled {
                            (|value_set: ::tracing::field::ValueSet|
                                        {
                                            let meta = __CALLSITE.metadata();
                                            ::tracing::Event::dispatch(meta, &value_set);
                                            ;
                                        })({
                                    #[allow(unused_imports)]
                                    use ::tracing::field::{debug, display, Value};
                                    let mut iter = __CALLSITE.metadata().fields().iter();
                                    __CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                                        ::tracing::__macro_support::Option::Some(&format_args!("skipped `{0:?} = {1:?}` due to subtyping: {2} != {3}",
                                                                        local, rhs, local_ty, rhs_ty) as &dyn Value))])
                                });
                        } else { ; }
                    };
                    continue;
                }
                if !ssa.is_ssa(rhs) { continue; }
                let head = copies[rhs];
                if ssa.borrowed_locals().contains(local) { continue; }
                if local == RETURN_PLACE {
                    if body.local_kind(head) != LocalKind::Temp { continue; }
                    for h in copies.iter_mut() {
                        if *h == head { *h = RETURN_PLACE; }
                    }
                } else { copies[local] = head; }
                direct_uses[rhs] -= 1;
            }
            {
                use ::tracing::__macro_support::Callsite as _;
                static __CALLSITE: ::tracing::callsite::DefaultCallsite =
                    {
                        static META: ::tracing::Metadata<'static> =
                            {
                                ::tracing_core::metadata::Metadata::new("event compiler/rustc_mir_transform/src/ssa.rs:349",
                                    "rustc_mir_transform::ssa", ::tracing::Level::DEBUG,
                                    ::tracing_core::__macro_support::Option::Some("compiler/rustc_mir_transform/src/ssa.rs"),
                                    ::tracing_core::__macro_support::Option::Some(349u32),
                                    ::tracing_core::__macro_support::Option::Some("rustc_mir_transform::ssa"),
                                    ::tracing_core::field::FieldSet::new(&["copies"],
                                        ::tracing_core::callsite::Identifier(&__CALLSITE)),
                                    ::tracing::metadata::Kind::EVENT)
                            };
                        ::tracing::callsite::DefaultCallsite::new(&META)
                    };
                let enabled =
                    ::tracing::Level::DEBUG <=
                                ::tracing::level_filters::STATIC_MAX_LEVEL &&
                            ::tracing::Level::DEBUG <=
                                ::tracing::level_filters::LevelFilter::current() &&
                        {
                            let interest = __CALLSITE.interest();
                            !interest.is_never() &&
                                ::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
                                    interest)
                        };
                if enabled {
                    (|value_set: ::tracing::field::ValueSet|
                                {
                                    let meta = __CALLSITE.metadata();
                                    ::tracing::Event::dispatch(meta, &value_set);
                                    ;
                                })({
                            #[allow(unused_imports)]
                            use ::tracing::field::{debug, display, Value};
                            let mut iter = __CALLSITE.metadata().fields().iter();
                            __CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                                ::tracing::__macro_support::Option::Some(&debug(&copies) as
                                                        &dyn Value))])
                        });
                } else { ; }
            };
            {
                use ::tracing::__macro_support::Callsite as _;
                static __CALLSITE: ::tracing::callsite::DefaultCallsite =
                    {
                        static META: ::tracing::Metadata<'static> =
                            {
                                ::tracing_core::metadata::Metadata::new("event compiler/rustc_mir_transform/src/ssa.rs:350",
                                    "rustc_mir_transform::ssa", ::tracing::Level::DEBUG,
                                    ::tracing_core::__macro_support::Option::Some("compiler/rustc_mir_transform/src/ssa.rs"),
                                    ::tracing_core::__macro_support::Option::Some(350u32),
                                    ::tracing_core::__macro_support::Option::Some("rustc_mir_transform::ssa"),
                                    ::tracing_core::field::FieldSet::new(&["direct_uses"],
                                        ::tracing_core::callsite::Identifier(&__CALLSITE)),
                                    ::tracing::metadata::Kind::EVENT)
                            };
                        ::tracing::callsite::DefaultCallsite::new(&META)
                    };
                let enabled =
                    ::tracing::Level::DEBUG <=
                                ::tracing::level_filters::STATIC_MAX_LEVEL &&
                            ::tracing::Level::DEBUG <=
                                ::tracing::level_filters::LevelFilter::current() &&
                        {
                            let interest = __CALLSITE.interest();
                            !interest.is_never() &&
                                ::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
                                    interest)
                        };
                if enabled {
                    (|value_set: ::tracing::field::ValueSet|
                                {
                                    let meta = __CALLSITE.metadata();
                                    ::tracing::Event::dispatch(meta, &value_set);
                                    ;
                                })({
                            #[allow(unused_imports)]
                            use ::tracing::field::{debug, display, Value};
                            let mut iter = __CALLSITE.metadata().fields().iter();
                            __CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                                ::tracing::__macro_support::Option::Some(&debug(&direct_uses)
                                                        as &dyn Value))])
                        });
                } else { ; }
            };
            for &head in copies.iter() {
                match (&copies[head], &head) {
                    (left_val, right_val) => {
                        if !(*left_val == *right_val) {
                            let kind = ::core::panicking::AssertKind::Eq;
                            ::core::panicking::assert_failed(kind, &*left_val,
                                &*right_val, ::core::option::Option::None);
                        }
                    }
                };
            }
            if true {
                match (&copies[RETURN_PLACE], &RETURN_PLACE) {
                    (left_val, right_val) => {
                        if !(*left_val == *right_val) {
                            let kind = ::core::panicking::AssertKind::Eq;
                            ::core::panicking::assert_failed(kind, &*left_val,
                                &*right_val, ::core::option::Option::None);
                        }
                    }
                };
            };
            ssa.direct_uses = direct_uses;
            ssa.copy_classes = copies;
        }
    }
}#[instrument(level = "trace", skip(ssa, body))]
296fn compute_copy_classes(ssa: &mut SsaLocals, body: &Body<'_>) {
297    let mut direct_uses = std::mem::take(&mut ssa.direct_uses);
298    let mut copies = IndexVec::from_fn_n(|l| l, body.local_decls.len());
299
300    for (local, rvalue, _) in ssa.assignments(body) {
301        let Rvalue::Use(Operand::Copy(place) | Operand::Move(place)) = rvalue else {
302            continue;
303        };
304
305        let Some(rhs) = place.as_local() else { continue };
306        let local_ty = body.local_decls()[local].ty;
307        let rhs_ty = body.local_decls()[rhs].ty;
308        if local_ty != rhs_ty {
309            // FIXME(#112651): This can be removed afterwards.
310            trace!("skipped `{local:?} = {rhs:?}` due to subtyping: {local_ty} != {rhs_ty}");
311            continue;
312        }
313
314        if !ssa.is_ssa(rhs) {
315            continue;
316        }
317
318        // We visit in `assignment_order`, i.e. reverse post-order, so `rhs` has been
319        // visited before `local`, and we just have to copy the representing local.
320        let head = copies[rhs];
321
322        // When propagating from `head` to `local` we need to ensure that changes to the address
323        // are not observable, so at most one the locals involved can be borrowed. Additionally, we
324        // need to ensure that the definition of `head` dominates all uses of `local`. When `local`
325        // is borrowed, there might exist an indirect use of `local` that isn't dominated by the
326        // definition, so we have to reject copy propagation.
327        if ssa.borrowed_locals().contains(local) {
328            continue;
329        }
330
331        if local == RETURN_PLACE {
332            // `_0` is special, we cannot rename it. Instead, rename the class of `rhs` to
333            // `RETURN_PLACE`. This is only possible if the class head is a temporary, not an
334            // argument.
335            if body.local_kind(head) != LocalKind::Temp {
336                continue;
337            }
338            for h in copies.iter_mut() {
339                if *h == head {
340                    *h = RETURN_PLACE;
341                }
342            }
343        } else {
344            copies[local] = head;
345        }
346        direct_uses[rhs] -= 1;
347    }
348
349    debug!(?copies);
350    debug!(?direct_uses);
351
352    // Invariant: `copies` must point to the head of an equivalence class.
353    #[cfg(debug_assertions)]
354    for &head in copies.iter() {
355        assert_eq!(copies[head], head);
356    }
357    debug_assert_eq!(copies[RETURN_PLACE], RETURN_PLACE);
358
359    ssa.direct_uses = direct_uses;
360    ssa.copy_classes = copies;
361}
362
363#[derive(#[automatically_derived]
impl ::core::fmt::Debug for StorageLiveLocals {
    #[inline]
    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
        ::core::fmt::Formatter::debug_struct_field1_finish(f,
            "StorageLiveLocals", "storage_live", &&self.storage_live)
    }
}Debug)]
364pub(crate) struct StorageLiveLocals {
365    /// Set of "StorageLive" statements for each local.
366    storage_live: IndexVec<Local, Set1<DefLocation>>,
367}
368
369impl StorageLiveLocals {
370    pub(crate) fn new(
371        body: &Body<'_>,
372        always_storage_live_locals: &DenseBitSet<Local>,
373    ) -> StorageLiveLocals {
374        let mut storage_live = IndexVec::from_elem(Set1::Empty, &body.local_decls);
375        for local in always_storage_live_locals.iter() {
376            storage_live[local] = Set1::One(DefLocation::Argument);
377        }
378        for (block, bbdata) in body.basic_blocks.iter_enumerated() {
379            for (statement_index, statement) in bbdata.statements.iter().enumerate() {
380                if let StatementKind::StorageLive(local) = statement.kind {
381                    storage_live[local]
382                        .insert(DefLocation::Assignment(Location { block, statement_index }));
383                }
384            }
385        }
386        {
    use ::tracing::__macro_support::Callsite as _;
    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
        {
            static META: ::tracing::Metadata<'static> =
                {
                    ::tracing_core::metadata::Metadata::new("event compiler/rustc_mir_transform/src/ssa.rs:386",
                        "rustc_mir_transform::ssa", ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_mir_transform/src/ssa.rs"),
                        ::tracing_core::__macro_support::Option::Some(386u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_mir_transform::ssa"),
                        ::tracing_core::field::FieldSet::new(&["storage_live"],
                            ::tracing_core::callsite::Identifier(&__CALLSITE)),
                        ::tracing::metadata::Kind::EVENT)
                };
            ::tracing::callsite::DefaultCallsite::new(&META)
        };
    let enabled =
        ::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
                &&
                ::tracing::Level::DEBUG <=
                    ::tracing::level_filters::LevelFilter::current() &&
            {
                let interest = __CALLSITE.interest();
                !interest.is_never() &&
                    ::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
                        interest)
            };
    if enabled {
        (|value_set: ::tracing::field::ValueSet|
                    {
                        let meta = __CALLSITE.metadata();
                        ::tracing::Event::dispatch(meta, &value_set);
                        ;
                    })({
                #[allow(unused_imports)]
                use ::tracing::field::{debug, display, Value};
                let mut iter = __CALLSITE.metadata().fields().iter();
                __CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                    ::tracing::__macro_support::Option::Some(&debug(&storage_live)
                                            as &dyn Value))])
            });
    } else { ; }
};debug!(?storage_live);
387        StorageLiveLocals { storage_live }
388    }
389
390    #[inline]
391    pub(crate) fn has_single_storage(&self, local: Local) -> bool {
392        #[allow(non_exhaustive_omitted_patterns)] match self.storage_live[local] {
    Set1::One(_) => true,
    _ => false,
}matches!(self.storage_live[local], Set1::One(_))
393    }
394}
395
396/// A dataflow analysis that tracks locals that are maybe uninitialized.
397///
398/// This is a simpler analysis than `MaybeUninitializedPlaces`, because it does not track
399/// individual fields.
400pub(crate) struct MaybeUninitializedLocals;
401
402impl<'tcx> Analysis<'tcx> for MaybeUninitializedLocals {
403    type Domain = DenseBitSet<Local>;
404
405    const NAME: &'static str = "maybe_uninit_locals";
406
407    fn bottom_value(&self, body: &Body<'tcx>) -> Self::Domain {
408        // bottom = all locals are initialized.
409        DenseBitSet::new_empty(body.local_decls.len())
410    }
411
412    fn initialize_start_block(&self, body: &Body<'tcx>, state: &mut Self::Domain) {
413        // All locals start as uninitialized...
414        state.insert_all();
415        // ...except for arguments, which are definitely initialized.
416        for arg in body.args_iter() {
417            state.remove(arg);
418        }
419    }
420
421    fn apply_primary_statement_effect(
422        &self,
423        state: &mut Self::Domain,
424        statement: &Statement<'tcx>,
425        _location: Location,
426    ) {
427        match statement.kind {
428            // An assignment makes a local initialized.
429            StatementKind::Assign(box (place, _)) => {
430                if let Some(local) = place.as_local() {
431                    state.remove(local);
432                }
433            }
434            // Storage{Live,Dead} makes a local uninitialized.
435            StatementKind::StorageLive(local) | StatementKind::StorageDead(local) => {
436                state.insert(local);
437            }
438            _ => {}
439        }
440    }
441
442    fn apply_call_return_effect(
443        &self,
444        state: &mut Self::Domain,
445        _block: BasicBlock,
446        return_places: CallReturnPlaces<'_, 'tcx>,
447    ) {
448        // The return place of a call is initialized.
449        return_places.for_each(|place| {
450            if let Some(local) = place.as_local() {
451                state.remove(local);
452            }
453        });
454    }
455}