Skip to main content

charon_lib/transform/resugar/
reconstruct_copy_for_deref.rs

1//! Reconstruct places that rustc split with `CopyForDeref` in elaborated MIR.
2//!
3//! A statement like `*tuple.1 = x` can become `_tmp = noretag read tuple.1; *_tmp = x`.
4//! The copy only reads the pointer; we put the dereference back on `tuple.1`. This also
5//! applies to shared references and boxes, and to reads of pointer metadata.
6use std::ops::ControlFlow;
7
8use crate::ids::IndexVec;
9use crate::transform::TransformCtx;
10use crate::transform::ctx::UllbcPass;
11use crate::ullbc_ast::*;
12
13#[derive(Default)]
14struct CopyInfo {
15    assignment: Option<(StmtLoc, Place)>,
16    use_loc: Option<StmtLoc>,
17    /// Whether this local is used in any way that doesn't allow the merging we want.
18    invalid: bool,
19}
20
21#[derive(Visitor)]
22struct UseCollector<'a> {
23    body: &'a ExprBody,
24    copies: &'a mut IndexVec<LocalId, CopyInfo>,
25    has_candidate: bool,
26    current_st: Option<StmtLoc>,
27}
28
29impl VisitBody for UseCollector<'_> {
30    fn visit_place(&mut self, place: &Place) -> ControlFlow<Self::Break> {
31        if let Some((subplace, ProjectionElem::Deref | ProjectionElem::PtrMetadata)) =
32            place.as_projection()
33            && let Some(local) = subplace.as_local()
34        {
35            let info = &mut self.copies[local];
36            if let Some(loc) = self.current_st
37                && (info.use_loc.is_none() || info.use_loc == self.current_st)
38            {
39                info.use_loc = self.current_st;
40                // We conservatively skip replacing into a statement that has by-move operands, to
41                // avoid cases like:
42                //   tmp = noretag copy pair._0
43                //   out = (move pair._0, copy (*tmp))
44                self.body[loc].dyn_visit_in_body(|operand: &Operand| {
45                    info.invalid |= matches!(operand, Operand::Move(_));
46                });
47            } else {
48                info.invalid = true;
49            }
50            // Don't visit the local.
51            return ControlFlow::Continue(());
52        }
53
54        if let Some(local) = place.as_local() {
55            // If we get to here then this is an invalid use.
56            self.copies[local].invalid = true;
57        }
58        self.visit_inner(place)
59    }
60
61    fn visit_ullbc_statement(&mut self, st: &Statement) -> ControlFlow<Self::Break> {
62        if let StatementKind::Assign(dest, rvalue) = &st.kind
63            && let Some(local) = dest.as_local()
64            && let Some(loc) = self.current_st
65        {
66            let info = &mut self.copies[local];
67            if info.assignment.is_some() {
68                info.invalid = true;
69            } else if let Rvalue::Use(
70                Operand::Copy(source, _) | Operand::Read(source),
71                WithRetag::No,
72            ) = rvalue
73                && (matches!(source.ty().kind(), TyKind::Ref(..)) || source.ty().is_box())
74                && !info.invalid
75            {
76                info.assignment = Some((loc, source.clone()));
77                self.has_candidate = true;
78            } else {
79                info.invalid = true;
80            }
81            self.visit(rvalue)
82        } else {
83            self.visit_inner(st)
84        }
85    }
86}
87
88pub struct Transform;
89
90impl UllbcPass for Transform {
91    fn transform_body(&self, _ctx: &mut TransformCtx, body: &mut ExprBody) {
92        let mut copies: IndexVec<LocalId, CopyInfo> =
93            body.locals.locals.map_ref(|_| CopyInfo::default());
94
95        // Look for locals that are assigned exactly once with a non-retagging copy. A local is
96        // eligible only if every use is beneath a dereference or a metadata projection.
97        let mut collector = UseCollector {
98            body,
99            copies: &mut copies,
100            has_candidate: false,
101            current_st: None,
102        };
103        collector.visit(&body.locals); // Look for uses in type annotations
104        for (block, data) in body.body.iter_enumerated() {
105            for (statement, st) in data.statements.iter().enumerate() {
106                collector.current_st = Some(StmtLoc::new(block, statement));
107                collector.visit(st);
108            }
109            collector.current_st = None;
110            collector.visit(&data.terminator);
111        }
112        if !collector.has_candidate {
113            return;
114        }
115
116        // Inlining the source is safe only if nothing can change it between the
117        // pointer read and its use. Nops do not affect that ordering.
118        let uninterrupted = |body: &ExprBody, before: StmtLoc, after: StmtLoc| {
119            before.block == after.block
120                && before.statement < after.statement
121                && body.body[before.block].statements[before.statement + 1..after.statement]
122                    .iter()
123                    .all(|st| st.kind.is_nop())
124        };
125        let mut replacements: IndexMap<LocalId, Place> = IndexMap::new();
126        for (local, info) in copies.iter_enumerated() {
127            if !info.invalid
128                && let Some((assignment, source)) = &info.assignment
129                && let Some(use_loc) = info.use_loc
130                && uninterrupted(body, *assignment, use_loc)
131            {
132                replacements.insert(local, source.clone());
133                body[*assignment].kind = StatementKind::Nop;
134            }
135        }
136        if replacements.is_empty() {
137            return;
138        }
139
140        body.body.dyn_visit_in_body_mut(|place: &mut Place| {
141            // Keep the projection and replace only its base; the visitor reaches
142            // inner projections on its own.
143            if let PlaceKind::Projection(
144                subplace,
145                ProjectionElem::Deref | ProjectionElem::PtrMetadata,
146            ) = &mut place.kind
147                && let Some(local) = subplace.as_local()
148                && let Some(source) = replacements.get(local)
149            {
150                **subplace = source.clone();
151            }
152        });
153    }
154}