rustc_mir_dataflow/impls/
storage_liveness.rs1use std::borrow::Cow;
2use std::cell::RefCell;
3
4use rustc_index::bit_set::DenseBitSet;
5use rustc_middle::mir::visit::{NonMutatingUseContext, PlaceContext, Visitor};
6use rustc_middle::mir::*;
7
8use super::MaybeBorrowedLocals;
9use crate::{Analysis, GenKill, ResultsCursor};
10
11pub fn always_storage_live_locals(body: &Body<'_>) -> DenseBitSet<Local> {
15 let mut always_live_locals = DenseBitSet::new_filled(body.local_decls.len());
16
17 for block in &*body.basic_blocks {
18 for statement in &block.statements {
19 if let StatementKind::StorageLive(l) | StatementKind::StorageDead(l) = statement.kind {
20 always_live_locals.remove(l);
21 }
22 }
23 }
24
25 always_live_locals
26}
27
28pub struct MaybeStorageLive<'a> {
29 always_live_locals: Cow<'a, DenseBitSet<Local>>,
30}
31
32impl<'a> MaybeStorageLive<'a> {
33 pub fn new(always_live_locals: Cow<'a, DenseBitSet<Local>>) -> Self {
34 MaybeStorageLive { always_live_locals }
35 }
36}
37
38impl<'a, 'tcx> Analysis<'tcx> for MaybeStorageLive<'a> {
39 type Domain = DenseBitSet<Local>;
40
41 const NAME: &'static str = "maybe_storage_live";
42
43 fn bottom_value(&self, body: &Body<'tcx>) -> Self::Domain {
44 DenseBitSet::new_empty(body.local_decls.len())
46 }
47
48 fn initialize_start_block(&self, body: &Body<'tcx>, state: &mut Self::Domain) {
49 state.union(&*self.always_live_locals);
50
51 for arg in body.args_iter() {
52 state.insert(arg);
53 }
54 }
55
56 fn apply_primary_statement_effect(
57 &self,
58 state: &mut Self::Domain,
59 stmt: &Statement<'tcx>,
60 _: Location,
61 ) {
62 match stmt.kind {
63 StatementKind::StorageLive(l) => state.gen_(l),
64 StatementKind::StorageDead(l) => state.kill(l),
65 _ => (),
66 }
67 }
68}
69
70pub struct MaybeStorageDead<'a> {
71 always_live_locals: Cow<'a, DenseBitSet<Local>>,
72}
73
74impl<'a> MaybeStorageDead<'a> {
75 pub fn new(always_live_locals: Cow<'a, DenseBitSet<Local>>) -> Self {
76 MaybeStorageDead { always_live_locals }
77 }
78}
79
80impl<'a, 'tcx> Analysis<'tcx> for MaybeStorageDead<'a> {
81 type Domain = DenseBitSet<Local>;
82
83 const NAME: &'static str = "maybe_storage_dead";
84
85 fn bottom_value(&self, body: &Body<'tcx>) -> Self::Domain {
86 DenseBitSet::new_empty(body.local_decls.len())
88 }
89
90 fn initialize_start_block(&self, body: &Body<'tcx>, state: &mut Self::Domain) {
91 match (&body.local_decls.len(), &self.always_live_locals.domain_size()) {
(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!(body.local_decls.len(), self.always_live_locals.domain_size());
92 for local in body.vars_and_temps_iter() {
94 if !self.always_live_locals.contains(local) {
95 state.insert(local);
96 }
97 }
98 }
99
100 fn apply_primary_statement_effect(
101 &self,
102 state: &mut Self::Domain,
103 stmt: &Statement<'tcx>,
104 _: Location,
105 ) {
106 match stmt.kind {
107 StatementKind::StorageLive(l) => state.kill(l),
108 StatementKind::StorageDead(l) => state.gen_(l),
109 _ => (),
110 }
111 }
112}
113
114type BorrowedLocalsResults<'mir, 'tcx> = ResultsCursor<'mir, 'tcx, MaybeBorrowedLocals>;
115
116pub struct MaybeRequiresStorage<'mir, 'tcx> {
119 borrowed_locals: RefCell<BorrowedLocalsResults<'mir, 'tcx>>,
120}
121
122impl<'mir, 'tcx> MaybeRequiresStorage<'mir, 'tcx> {
123 pub fn new(borrowed_locals: BorrowedLocalsResults<'mir, 'tcx>) -> Self {
124 MaybeRequiresStorage { borrowed_locals: RefCell::new(borrowed_locals) }
125 }
126}
127
128impl<'tcx> Analysis<'tcx> for MaybeRequiresStorage<'_, 'tcx> {
129 type Domain = DenseBitSet<Local>;
130
131 const NAME: &'static str = "requires_storage";
132
133 fn bottom_value(&self, body: &Body<'tcx>) -> Self::Domain {
134 DenseBitSet::new_empty(body.local_decls.len())
136 }
137
138 fn initialize_start_block(&self, body: &Body<'tcx>, state: &mut Self::Domain) {
139 for arg in body.args_iter().skip(1) {
142 state.insert(arg);
143 }
144 }
145
146 fn apply_early_statement_effect(
147 &self,
148 state: &mut Self::Domain,
149 stmt: &Statement<'tcx>,
150 loc: Location,
151 ) {
152 MaybeBorrowedLocals::transfer_function(state).visit_statement(stmt, loc);
154
155 match &stmt.kind {
156 StatementKind::StorageDead(l) => state.kill(*l),
157
158 StatementKind::Assign(box (place, _))
160 | StatementKind::SetDiscriminant { box place, .. } => {
161 state.gen_(place.local);
162 }
163
164 StatementKind::AscribeUserType(..)
167 | StatementKind::PlaceMention(..)
168 | StatementKind::Coverage(..)
169 | StatementKind::FakeRead(..)
170 | StatementKind::ConstEvalCounter
171 | StatementKind::Nop
172 | StatementKind::Intrinsic(..)
173 | StatementKind::BackwardIncompatibleDropHint { .. }
174 | StatementKind::StorageLive(..) => {}
175 }
176 }
177
178 fn apply_primary_statement_effect(
179 &self,
180 state: &mut Self::Domain,
181 _: &Statement<'tcx>,
182 loc: Location,
183 ) {
184 self.check_for_move(state, loc);
187 }
188
189 fn apply_early_terminator_effect(
190 &self,
191 state: &mut Self::Domain,
192 terminator: &Terminator<'tcx>,
193 loc: Location,
194 ) {
195 MaybeBorrowedLocals::transfer_function(state).visit_terminator(terminator, loc);
197
198 match &terminator.kind {
199 TerminatorKind::Call { destination, .. } => {
200 state.gen_(destination.local);
201 }
202
203 TerminatorKind::Yield { .. } => {}
208
209 TerminatorKind::InlineAsm { operands, .. } => {
210 for op in operands {
211 match op {
212 InlineAsmOperand::Out { place, .. }
213 | InlineAsmOperand::InOut { out_place: place, .. } => {
214 if let Some(place) = place {
215 state.gen_(place.local);
216 }
217 }
218 InlineAsmOperand::In { .. }
219 | InlineAsmOperand::Const { .. }
220 | InlineAsmOperand::SymFn { .. }
221 | InlineAsmOperand::SymStatic { .. }
222 | InlineAsmOperand::Label { .. } => {}
223 }
224 }
225 }
226
227 TerminatorKind::UnwindTerminate(_)
230 | TerminatorKind::Assert { .. }
231 | TerminatorKind::Drop { .. }
232 | TerminatorKind::FalseEdge { .. }
233 | TerminatorKind::FalseUnwind { .. }
234 | TerminatorKind::CoroutineDrop
235 | TerminatorKind::Goto { .. }
236 | TerminatorKind::UnwindResume
237 | TerminatorKind::Return
238 | TerminatorKind::TailCall { .. }
239 | TerminatorKind::SwitchInt { .. }
240 | TerminatorKind::Unreachable => {}
241 }
242 }
243
244 fn apply_primary_terminator_effect<'t>(
245 &self,
246 state: &mut Self::Domain,
247 terminator: &'t Terminator<'tcx>,
248 loc: Location,
249 ) -> TerminatorEdges<'t, 'tcx> {
250 match terminator.kind {
251 TerminatorKind::Call { destination, .. } => {
256 state.kill(destination.local);
257 }
258
259 TerminatorKind::InlineAsm { ref operands, .. } => {
261 CallReturnPlaces::InlineAsm(operands).for_each(|place| state.kill(place.local));
262 }
263
264 TerminatorKind::Yield { .. }
267 | TerminatorKind::UnwindTerminate(_)
268 | TerminatorKind::Assert { .. }
269 | TerminatorKind::Drop { .. }
270 | TerminatorKind::FalseEdge { .. }
271 | TerminatorKind::FalseUnwind { .. }
272 | TerminatorKind::CoroutineDrop
273 | TerminatorKind::Goto { .. }
274 | TerminatorKind::UnwindResume
275 | TerminatorKind::Return
276 | TerminatorKind::TailCall { .. }
277 | TerminatorKind::SwitchInt { .. }
278 | TerminatorKind::Unreachable => {}
279 }
280
281 self.check_for_move(state, loc);
282 terminator.edges()
283 }
284
285 fn apply_call_return_effect(
286 &self,
287 state: &mut Self::Domain,
288 _block: BasicBlock,
289 return_places: CallReturnPlaces<'_, 'tcx>,
290 ) {
291 return_places.for_each(|place| state.gen_(place.local));
292 }
293}
294
295impl<'tcx> MaybeRequiresStorage<'_, 'tcx> {
296 fn check_for_move(&self, state: &mut <Self as Analysis<'tcx>>::Domain, loc: Location) {
298 let mut borrowed_locals = self.borrowed_locals.borrow_mut();
299 let body = borrowed_locals.body();
300 let mut visitor = MoveVisitor { state, borrowed_locals: &mut borrowed_locals };
301 visitor.visit_location(body, loc);
302 }
303}
304
305struct MoveVisitor<'a, 'mir, 'tcx> {
306 borrowed_locals: &'a mut BorrowedLocalsResults<'mir, 'tcx>,
307 state: &'a mut DenseBitSet<Local>,
308}
309
310impl<'tcx> Visitor<'tcx> for MoveVisitor<'_, '_, 'tcx> {
311 fn visit_local(&mut self, local: Local, context: PlaceContext, loc: Location) {
312 if PlaceContext::NonMutatingUse(NonMutatingUseContext::Move) == context {
313 self.borrowed_locals.seek_before_primary_effect(loc);
314 if !self.borrowed_locals.get().contains(local) {
315 self.state.kill(local);
316 }
317 }
318 }
319}