Skip to content

Commit de5ce09

Browse files
committed
use new borrowed locals analysis in MaybeRequiresStorage
1 parent a4c1ca3 commit de5ce09

File tree

1 file changed

+39
-26
lines changed

1 file changed

+39
-26
lines changed

compiler/rustc_mir_dataflow/src/impls/storage_liveness.rs

Lines changed: 39 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
pub use super::*;
22

3-
use crate::{CallReturnPlaces, GenKill, ResultsClonedCursor};
3+
use crate::impls::{BorrowedLocalsResults, BorrowedLocalsResultsCursor};
4+
use crate::{CallReturnPlaces, GenKill};
45
use rustc_middle::mir::visit::{NonMutatingUseContext, PlaceContext, Visitor};
56
use rustc_middle::mir::*;
67
use std::borrow::Cow;
@@ -146,28 +147,26 @@ impl<'tcx> crate::GenKillAnalysis<'tcx> for MaybeStorageDead {
146147
}
147148
}
148149

149-
type BorrowedLocalsResults<'res, 'mir, 'tcx> =
150-
ResultsClonedCursor<'res, 'mir, 'tcx, MaybeBorrowedLocals>;
151-
152150
/// Dataflow analysis that determines whether each local requires storage at a
153151
/// given location; i.e. whether its storage can go away without being observed.
154-
pub struct MaybeRequiresStorage<'res, 'mir, 'tcx> {
155-
borrowed_locals: BorrowedLocalsResults<'res, 'mir, 'tcx>,
156-
}
157-
158-
impl<'res, 'mir, 'tcx> MaybeRequiresStorage<'res, 'mir, 'tcx> {
159-
pub fn new(borrowed_locals: BorrowedLocalsResults<'res, 'mir, 'tcx>) -> Self {
160-
MaybeRequiresStorage { borrowed_locals }
161-
}
152+
pub struct MaybeRequiresStorage<'a, 'mir, 'tcx> {
153+
body: &'mir Body<'tcx>,
154+
borrowed_locals: RefCell<BorrowedLocalsResultsCursor<'a, 'mir, 'tcx>>,
162155
}
163156

164-
impl crate::CloneAnalysis for MaybeRequiresStorage<'_, '_, '_> {
165-
fn clone_analysis(&self) -> Self {
166-
Self { borrowed_locals: self.borrowed_locals.new_cursor() }
157+
impl<'a, 'mir, 'tcx> MaybeRequiresStorage<'a, 'mir, 'tcx> {
158+
pub fn new(
159+
body: &'mir Body<'tcx>,
160+
borrowed_locals: &'a BorrowedLocalsResults<'mir, 'tcx>,
161+
) -> Self {
162+
MaybeRequiresStorage {
163+
body,
164+
borrowed_locals: RefCell::new(BorrowedLocalsResultsCursor::new(body, borrowed_locals)),
165+
}
167166
}
168167
}
169168

170-
impl<'tcx> crate::AnalysisDomain<'tcx> for MaybeRequiresStorage<'_, '_, 'tcx> {
169+
impl<'a, 'mir, 'tcx> crate::AnalysisDomain<'tcx> for MaybeRequiresStorage<'a, 'mir, 'tcx> {
171170
type Domain = BitSet<Local>;
172171

173172
const NAME: &'static str = "requires_storage";
@@ -186,7 +185,7 @@ impl<'tcx> crate::AnalysisDomain<'tcx> for MaybeRequiresStorage<'_, '_, 'tcx> {
186185
}
187186
}
188187

189-
impl<'tcx> crate::GenKillAnalysis<'tcx> for MaybeRequiresStorage<'_, '_, 'tcx> {
188+
impl<'a, 'mir, 'tcx> crate::GenKillAnalysis<'tcx> for MaybeRequiresStorage<'a, 'mir, 'tcx> {
190189
type Idx = Local;
191190

192191
fn before_statement_effect(
@@ -196,7 +195,13 @@ impl<'tcx> crate::GenKillAnalysis<'tcx> for MaybeRequiresStorage<'_, '_, 'tcx> {
196195
loc: Location,
197196
) {
198197
// If a place is borrowed in a statement, it needs storage for that statement.
199-
self.borrowed_locals.mut_analysis().statement_effect(trans, stmt, loc);
198+
let borrowed_locals_at_loc = self.borrowed_locals.borrow_mut().get(loc);
199+
for i in 0..self.body.local_decls().len() {
200+
let local = Local::from_usize(i);
201+
if borrowed_locals_at_loc.contains(local) {
202+
trans.gen(local);
203+
}
204+
}
200205

201206
match &stmt.kind {
202207
StatementKind::StorageDead(l) => trans.kill(*l),
@@ -239,8 +244,14 @@ impl<'tcx> crate::GenKillAnalysis<'tcx> for MaybeRequiresStorage<'_, '_, 'tcx> {
239244
terminator: &mir::Terminator<'tcx>,
240245
loc: Location,
241246
) {
242-
// If a place is borrowed in a terminator, it needs storage for that terminator.
243-
self.borrowed_locals.mut_analysis().terminator_effect(trans, terminator, loc);
247+
// If a place is borrowed in a statement, it needs storage for that statement.
248+
let borrowed_locals_at_loc = self.borrowed_locals.borrow_mut().get(loc);
249+
for i in 0..self.body.local_decls().len() {
250+
let local = Local::from_usize(i);
251+
if borrowed_locals_at_loc.contains(local) {
252+
trans.gen(local);
253+
}
254+
}
244255

245256
match &terminator.kind {
246257
TerminatorKind::Call { destination, .. } => {
@@ -344,7 +355,7 @@ impl<'tcx> crate::GenKillAnalysis<'tcx> for MaybeRequiresStorage<'_, '_, 'tcx> {
344355
}
345356
}
346357

347-
impl<'tcx> MaybeRequiresStorage<'_, '_, 'tcx> {
358+
impl<'a, 'mir, 'tcx> MaybeRequiresStorage<'a, 'mir, 'tcx> {
348359
/// Kill locals that are fully moved and have not been borrowed.
349360
fn check_for_move(&mut self, trans: &mut impl GenKill<Local>, loc: Location) {
350361
let body = self.borrowed_locals.body();
@@ -353,19 +364,21 @@ impl<'tcx> MaybeRequiresStorage<'_, '_, 'tcx> {
353364
}
354365
}
355366

356-
struct MoveVisitor<'a, 'res, 'mir, 'tcx, T> {
357-
borrowed_locals: &'a mut BorrowedLocalsResults<'res, 'mir, 'tcx>,
367+
struct MoveVisitor<'a, 'b, 'mir, 'tcx, T> {
368+
borrowed_locals: &'a RefCell<BorrowedLocalsResultsCursor<'b, 'mir, 'tcx>>,
358369
trans: &'a mut T,
359370
}
360371

361-
impl<'tcx, T> Visitor<'tcx> for MoveVisitor<'_, '_, '_, 'tcx, T>
372+
impl<'a, 'b, 'mir, 'tcx, T> Visitor<'tcx> for MoveVisitor<'a, 'b, 'mir, 'tcx, T>
362373
where
363374
T: GenKill<Local>,
364375
{
376+
#[instrument(skip(self), level = "debug")]
365377
fn visit_local(&mut self, local: Local, context: PlaceContext, loc: Location) {
366378
if PlaceContext::NonMutatingUse(NonMutatingUseContext::Move) == context {
367-
self.borrowed_locals.seek_before_primary_effect(loc);
368-
if !self.borrowed_locals.contains(local) {
379+
let borrowed_locals = self.borrowed_locals.borrow_mut().get(loc);
380+
debug!(?borrowed_locals);
381+
if !borrowed_locals.contains(local) {
369382
self.trans.kill(local);
370383
}
371384
}

0 commit comments

Comments
 (0)