3 use crate::{CallReturnPlaces, GenKill, Results, ResultsRefCursor};
4 use rustc_middle::mir::visit::{NonMutatingUseContext, PlaceContext, Visitor};
5 use rustc_middle::mir::*;
6 use std::cell::RefCell;
9 pub struct MaybeStorageLive {
10 always_live_locals: BitSet<Local>,
13 impl MaybeStorageLive {
14 pub fn new(always_live_locals: BitSet<Local>) -> Self {
15 MaybeStorageLive { always_live_locals }
19 impl<'tcx> crate::AnalysisDomain<'tcx> for MaybeStorageLive {
20 type Domain = BitSet<Local>;
22 const NAME: &'static str = "maybe_storage_live";
24 fn bottom_value(&self, body: &mir::Body<'tcx>) -> Self::Domain {
26 BitSet::new_empty(body.local_decls.len())
29 fn initialize_start_block(&self, body: &mir::Body<'tcx>, on_entry: &mut Self::Domain) {
30 assert_eq!(body.local_decls.len(), self.always_live_locals.domain_size());
31 for local in self.always_live_locals.iter() {
32 on_entry.insert(local);
35 for arg in body.args_iter() {
41 impl<'tcx> crate::GenKillAnalysis<'tcx> for MaybeStorageLive {
46 trans: &mut impl GenKill<Self::Idx>,
47 stmt: &mir::Statement<'tcx>,
51 StatementKind::StorageLive(l) => trans.gen(l),
52 StatementKind::StorageDead(l) => trans.kill(l),
59 _trans: &mut impl GenKill<Self::Idx>,
60 _: &mir::Terminator<'tcx>,
63 // Terminators have no effect
66 fn call_return_effect(
68 _trans: &mut impl GenKill<Self::Idx>,
70 _return_places: CallReturnPlaces<'_, 'tcx>,
72 // Nothing to do when a call returns successfully
76 type BorrowedLocalsResults<'a, 'tcx> = ResultsRefCursor<'a, 'a, 'tcx, MaybeBorrowedLocals>;
78 /// Dataflow analysis that determines whether each local requires storage at a
79 /// given location; i.e. whether its storage can go away without being observed.
80 pub struct MaybeRequiresStorage<'mir, 'tcx> {
81 body: &'mir Body<'tcx>,
82 borrowed_locals: RefCell<BorrowedLocalsResults<'mir, 'tcx>>,
85 impl<'mir, 'tcx> MaybeRequiresStorage<'mir, 'tcx> {
87 body: &'mir Body<'tcx>,
88 borrowed_locals: &'mir Results<'tcx, MaybeBorrowedLocals>,
90 MaybeRequiresStorage {
92 borrowed_locals: RefCell::new(ResultsRefCursor::new(&body, borrowed_locals)),
97 impl<'mir, 'tcx> crate::AnalysisDomain<'tcx> for MaybeRequiresStorage<'mir, 'tcx> {
98 type Domain = BitSet<Local>;
100 const NAME: &'static str = "requires_storage";
102 fn bottom_value(&self, body: &mir::Body<'tcx>) -> Self::Domain {
104 BitSet::new_empty(body.local_decls.len())
107 fn initialize_start_block(&self, body: &mir::Body<'tcx>, on_entry: &mut Self::Domain) {
108 // The resume argument is live on function entry (we don't care about
109 // the `self` argument)
110 for arg in body.args_iter().skip(1) {
111 on_entry.insert(arg);
116 impl<'mir, 'tcx> crate::GenKillAnalysis<'tcx> for MaybeRequiresStorage<'mir, 'tcx> {
119 fn before_statement_effect(
121 trans: &mut impl GenKill<Self::Idx>,
122 stmt: &mir::Statement<'tcx>,
125 // If a place is borrowed in a statement, it needs storage for that statement.
126 self.borrowed_locals.borrow().analysis().statement_effect(trans, stmt, loc);
129 StatementKind::StorageDead(l) => trans.kill(*l),
131 // If a place is assigned to in a statement, it needs storage for that statement.
132 StatementKind::Assign(box (place, _))
133 | StatementKind::SetDiscriminant { box place, .. }
134 | StatementKind::Deinit(box place) => {
135 trans.gen(place.local);
138 // Nothing to do for these. Match exhaustively so this fails to compile when new
139 // variants are added.
140 StatementKind::AscribeUserType(..)
141 | StatementKind::Coverage(..)
142 | StatementKind::FakeRead(..)
144 | StatementKind::Retag(..)
145 | StatementKind::CopyNonOverlapping(..)
146 | StatementKind::StorageLive(..) => {}
152 trans: &mut impl GenKill<Self::Idx>,
153 _: &mir::Statement<'tcx>,
156 // If we move from a place then it only stops needing storage *after*
158 self.check_for_move(trans, loc);
161 fn before_terminator_effect(
163 trans: &mut impl GenKill<Self::Idx>,
164 terminator: &mir::Terminator<'tcx>,
167 // If a place is borrowed in a terminator, it needs storage for that terminator.
168 self.borrowed_locals.borrow().analysis().terminator_effect(trans, terminator, loc);
170 match &terminator.kind {
171 TerminatorKind::Call { destination, .. } => {
172 trans.gen(destination.local);
175 // Note that we do *not* gen the `resume_arg` of `Yield` terminators. The reason for
176 // that is that a `yield` will return from the function, and `resume_arg` is written
177 // only when the generator is later resumed. Unlike `Call`, this doesn't require the
178 // place to have storage *before* the yield, only after.
179 TerminatorKind::Yield { .. } => {}
181 TerminatorKind::InlineAsm { operands, .. } => {
184 InlineAsmOperand::Out { place, .. }
185 | InlineAsmOperand::InOut { out_place: place, .. } => {
186 if let Some(place) = place {
187 trans.gen(place.local);
190 InlineAsmOperand::In { .. }
191 | InlineAsmOperand::Const { .. }
192 | InlineAsmOperand::SymFn { .. }
193 | InlineAsmOperand::SymStatic { .. } => {}
198 // Nothing to do for these. Match exhaustively so this fails to compile when new
199 // variants are added.
200 TerminatorKind::Abort
201 | TerminatorKind::Assert { .. }
202 | TerminatorKind::Drop { .. }
203 | TerminatorKind::DropAndReplace { .. }
204 | TerminatorKind::FalseEdge { .. }
205 | TerminatorKind::FalseUnwind { .. }
206 | TerminatorKind::GeneratorDrop
207 | TerminatorKind::Goto { .. }
208 | TerminatorKind::Resume
209 | TerminatorKind::Return
210 | TerminatorKind::SwitchInt { .. }
211 | TerminatorKind::Unreachable => {}
215 fn terminator_effect(
217 trans: &mut impl GenKill<Self::Idx>,
218 terminator: &mir::Terminator<'tcx>,
221 match terminator.kind {
222 // For call terminators the destination requires storage for the call
223 // and after the call returns successfully, but not after a panic.
224 // Since `propagate_call_unwind` doesn't exist, we have to kill the
225 // destination here, and then gen it again in `call_return_effect`.
226 TerminatorKind::Call { destination, .. } => {
227 trans.kill(destination.local);
230 // The same applies to InlineAsm outputs.
231 TerminatorKind::InlineAsm { ref operands, .. } => {
232 CallReturnPlaces::InlineAsm(operands).for_each(|place| trans.kill(place.local));
235 // Nothing to do for these. Match exhaustively so this fails to compile when new
236 // variants are added.
237 TerminatorKind::Yield { .. }
238 | TerminatorKind::Abort
239 | TerminatorKind::Assert { .. }
240 | TerminatorKind::Drop { .. }
241 | TerminatorKind::DropAndReplace { .. }
242 | TerminatorKind::FalseEdge { .. }
243 | TerminatorKind::FalseUnwind { .. }
244 | TerminatorKind::GeneratorDrop
245 | TerminatorKind::Goto { .. }
246 | TerminatorKind::Resume
247 | TerminatorKind::Return
248 | TerminatorKind::SwitchInt { .. }
249 | TerminatorKind::Unreachable => {}
252 self.check_for_move(trans, loc);
255 fn call_return_effect(
257 trans: &mut impl GenKill<Self::Idx>,
259 return_places: CallReturnPlaces<'_, 'tcx>,
261 return_places.for_each(|place| trans.gen(place.local));
264 fn yield_resume_effect(
266 trans: &mut impl GenKill<Self::Idx>,
267 _resume_block: BasicBlock,
268 resume_place: mir::Place<'tcx>,
270 trans.gen(resume_place.local);
274 impl<'mir, 'tcx> MaybeRequiresStorage<'mir, 'tcx> {
275 /// Kill locals that are fully moved and have not been borrowed.
276 fn check_for_move(&self, trans: &mut impl GenKill<Local>, loc: Location) {
277 let mut visitor = MoveVisitor { trans, borrowed_locals: &self.borrowed_locals };
278 visitor.visit_location(&self.body, loc);
282 struct MoveVisitor<'a, 'mir, 'tcx, T> {
283 borrowed_locals: &'a RefCell<BorrowedLocalsResults<'mir, 'tcx>>,
287 impl<'a, 'mir, 'tcx, T> Visitor<'tcx> for MoveVisitor<'a, 'mir, 'tcx, T>
291 fn visit_local(&mut self, local: Local, context: PlaceContext, loc: Location) {
292 if PlaceContext::NonMutatingUse(NonMutatingUseContext::Move) == context {
293 let mut borrowed_locals = self.borrowed_locals.borrow_mut();
294 borrowed_locals.seek_before_primary_effect(loc);
295 if !borrowed_locals.contains(local) {
296 self.trans.kill(local);