charon_lib/transform/resugar/
reconstruct_copy_for_deref.rs1use 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 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 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 return ControlFlow::Continue(());
52 }
53
54 if let Some(local) = place.as_local() {
55 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 let mut collector = UseCollector {
98 body,
99 copies: &mut copies,
100 has_candidate: false,
101 current_st: None,
102 };
103 collector.visit(&body.locals); 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 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 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}